dpt_i2o.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701
  1. /***************************************************************************
  2. dpti.c - description
  3. -------------------
  4. begin : Thu Sep 7 2000
  5. copyright : (C) 2000 by Adaptec
  6. July 30, 2001 First version being submitted
  7. for inclusion in the kernel. V2.4
  8. See Documentation/scsi/dpti.txt for history, notes, license info
  9. and credits
  10. ***************************************************************************/
  11. /***************************************************************************
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. ***************************************************************************/
  19. /***************************************************************************
  20. * Sat Dec 20 2003 Go Taniguchi <go@turbolinux.co.jp>
  21. - Support 2.6 kernel and DMA-mapping
  22. - ioctl fix for raid tools
  23. - use schedule_timeout in long long loop
  24. **************************************************************************/
  25. /*#define DEBUG 1 */
  26. /*#define UARTDELAY 1 */
  27. #include <linux/module.h>
  28. MODULE_AUTHOR("Deanna Bonds, with _lots_ of help from Mark Salyzyn");
  29. MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
  30. ////////////////////////////////////////////////////////////////
  31. #include <linux/ioctl.h> /* For SCSI-Passthrough */
  32. #include <asm/uaccess.h>
  33. #include <linux/stat.h>
  34. #include <linux/slab.h> /* for kmalloc() */
  35. #include <linux/pci.h> /* for PCI support */
  36. #include <linux/proc_fs.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/delay.h> /* for udelay */
  39. #include <linux/interrupt.h>
  40. #include <linux/kernel.h> /* for printk */
  41. #include <linux/sched.h>
  42. #include <linux/reboot.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/timer.h>
  46. #include <linux/string.h>
  47. #include <linux/ioport.h>
  48. #include <linux/mutex.h>
  49. #include <asm/processor.h> /* for boot_cpu_data */
  50. #include <asm/pgtable.h>
  51. #include <asm/io.h> /* for virt_to_bus, etc. */
  52. #include <scsi/scsi.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsi_device.h>
  55. #include <scsi/scsi_host.h>
  56. #include <scsi/scsi_tcq.h>
  57. #include "dpt/dptsig.h"
  58. #include "dpti.h"
  59. /*============================================================================
  60. * Create a binary signature - this is read by dptsig
  61. * Needed for our management apps
  62. *============================================================================
  63. */
  64. static DEFINE_MUTEX(adpt_mutex);
  65. static dpt_sig_S DPTI_sig = {
  66. {'d', 'P', 't', 'S', 'i', 'G'}, SIG_VERSION,
  67. #ifdef __i386__
  68. PROC_INTEL, PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
  69. #elif defined(__ia64__)
  70. PROC_INTEL, PROC_IA64,
  71. #elif defined(__sparc__)
  72. PROC_ULTRASPARC, PROC_ULTRASPARC,
  73. #elif defined(__alpha__)
  74. PROC_ALPHA, PROC_ALPHA,
  75. #else
  76. (-1),(-1),
  77. #endif
  78. FT_HBADRVR, 0, OEM_DPT, OS_LINUX, CAP_OVERLAP, DEV_ALL,
  79. ADF_ALL_SC5, 0, 0, DPT_VERSION, DPT_REVISION, DPT_SUBREVISION,
  80. DPT_MONTH, DPT_DAY, DPT_YEAR, "Adaptec Linux I2O RAID Driver"
  81. };
  82. /*============================================================================
  83. * Globals
  84. *============================================================================
  85. */
  86. static DEFINE_MUTEX(adpt_configuration_lock);
  87. static struct i2o_sys_tbl *sys_tbl;
  88. static dma_addr_t sys_tbl_pa;
  89. static int sys_tbl_ind;
  90. static int sys_tbl_len;
  91. static adpt_hba* hba_chain = NULL;
  92. static int hba_count = 0;
  93. static struct class *adpt_sysfs_class;
  94. static long adpt_unlocked_ioctl(struct file *, unsigned int, unsigned long);
  95. #ifdef CONFIG_COMPAT
  96. static long compat_adpt_ioctl(struct file *, unsigned int, unsigned long);
  97. #endif
  98. static const struct file_operations adpt_fops = {
  99. .unlocked_ioctl = adpt_unlocked_ioctl,
  100. .open = adpt_open,
  101. .release = adpt_close,
  102. #ifdef CONFIG_COMPAT
  103. .compat_ioctl = compat_adpt_ioctl,
  104. #endif
  105. .llseek = noop_llseek,
  106. };
  107. /* Structures and definitions for synchronous message posting.
  108. * See adpt_i2o_post_wait() for description
  109. * */
  110. struct adpt_i2o_post_wait_data
  111. {
  112. int status;
  113. u32 id;
  114. adpt_wait_queue_head_t *wq;
  115. struct adpt_i2o_post_wait_data *next;
  116. };
  117. static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
  118. static u32 adpt_post_wait_id = 0;
  119. static DEFINE_SPINLOCK(adpt_post_wait_lock);
  120. /*============================================================================
  121. * Functions
  122. *============================================================================
  123. */
  124. static inline int dpt_dma64(adpt_hba *pHba)
  125. {
  126. return (sizeof(dma_addr_t) > 4 && (pHba)->dma64);
  127. }
  128. static inline u32 dma_high(dma_addr_t addr)
  129. {
  130. return upper_32_bits(addr);
  131. }
  132. static inline u32 dma_low(dma_addr_t addr)
  133. {
  134. return (u32)addr;
  135. }
  136. static u8 adpt_read_blink_led(adpt_hba* host)
  137. {
  138. if (host->FwDebugBLEDflag_P) {
  139. if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
  140. return readb(host->FwDebugBLEDvalue_P);
  141. }
  142. }
  143. return 0;
  144. }
  145. /*============================================================================
  146. * Scsi host template interface functions
  147. *============================================================================
  148. */
  149. static struct pci_device_id dptids[] = {
  150. { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  151. { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  152. { 0, }
  153. };
  154. MODULE_DEVICE_TABLE(pci,dptids);
  155. static int adpt_detect(struct scsi_host_template* sht)
  156. {
  157. struct pci_dev *pDev = NULL;
  158. adpt_hba *pHba;
  159. adpt_hba *next;
  160. PINFO("Detecting Adaptec I2O RAID controllers...\n");
  161. /* search for all Adatpec I2O RAID cards */
  162. while ((pDev = pci_get_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
  163. if(pDev->device == PCI_DPT_DEVICE_ID ||
  164. pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
  165. if(adpt_install_hba(sht, pDev) ){
  166. PERROR("Could not Init an I2O RAID device\n");
  167. PERROR("Will not try to detect others.\n");
  168. return hba_count-1;
  169. }
  170. pci_dev_get(pDev);
  171. }
  172. }
  173. /* In INIT state, Activate IOPs */
  174. for (pHba = hba_chain; pHba; pHba = next) {
  175. next = pHba->next;
  176. // Activate does get status , init outbound, and get hrt
  177. if (adpt_i2o_activate_hba(pHba) < 0) {
  178. adpt_i2o_delete_hba(pHba);
  179. }
  180. }
  181. /* Active IOPs in HOLD state */
  182. rebuild_sys_tab:
  183. if (hba_chain == NULL)
  184. return 0;
  185. /*
  186. * If build_sys_table fails, we kill everything and bail
  187. * as we can't init the IOPs w/o a system table
  188. */
  189. if (adpt_i2o_build_sys_table() < 0) {
  190. adpt_i2o_sys_shutdown();
  191. return 0;
  192. }
  193. PDEBUG("HBA's in HOLD state\n");
  194. /* If IOP don't get online, we need to rebuild the System table */
  195. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  196. if (adpt_i2o_online_hba(pHba) < 0) {
  197. adpt_i2o_delete_hba(pHba);
  198. goto rebuild_sys_tab;
  199. }
  200. }
  201. /* Active IOPs now in OPERATIONAL state */
  202. PDEBUG("HBA's in OPERATIONAL state\n");
  203. printk("dpti: If you have a lot of devices this could take a few minutes.\n");
  204. for (pHba = hba_chain; pHba; pHba = next) {
  205. next = pHba->next;
  206. printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
  207. if (adpt_i2o_lct_get(pHba) < 0){
  208. adpt_i2o_delete_hba(pHba);
  209. continue;
  210. }
  211. if (adpt_i2o_parse_lct(pHba) < 0){
  212. adpt_i2o_delete_hba(pHba);
  213. continue;
  214. }
  215. adpt_inquiry(pHba);
  216. }
  217. adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
  218. if (IS_ERR(adpt_sysfs_class)) {
  219. printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
  220. adpt_sysfs_class = NULL;
  221. }
  222. for (pHba = hba_chain; pHba; pHba = next) {
  223. next = pHba->next;
  224. if (adpt_scsi_host_alloc(pHba, sht) < 0){
  225. adpt_i2o_delete_hba(pHba);
  226. continue;
  227. }
  228. pHba->initialized = TRUE;
  229. pHba->state &= ~DPTI_STATE_RESET;
  230. if (adpt_sysfs_class) {
  231. struct device *dev = device_create(adpt_sysfs_class,
  232. NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
  233. "dpti%d", pHba->unit);
  234. if (IS_ERR(dev)) {
  235. printk(KERN_WARNING"dpti%d: unable to "
  236. "create device in dpt_i2o class\n",
  237. pHba->unit);
  238. }
  239. }
  240. }
  241. // Register our control device node
  242. // nodes will need to be created in /dev to access this
  243. // the nodes can not be created from within the driver
  244. if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
  245. adpt_i2o_sys_shutdown();
  246. return 0;
  247. }
  248. return hba_count;
  249. }
  250. /*
  251. * scsi_unregister will be called AFTER we return.
  252. */
  253. static int adpt_release(struct Scsi_Host *host)
  254. {
  255. adpt_hba* pHba = (adpt_hba*) host->hostdata[0];
  256. // adpt_i2o_quiesce_hba(pHba);
  257. adpt_i2o_delete_hba(pHba);
  258. scsi_unregister(host);
  259. return 0;
  260. }
  261. static void adpt_inquiry(adpt_hba* pHba)
  262. {
  263. u32 msg[17];
  264. u32 *mptr;
  265. u32 *lenptr;
  266. int direction;
  267. int scsidir;
  268. u32 len;
  269. u32 reqlen;
  270. u8* buf;
  271. dma_addr_t addr;
  272. u8 scb[16];
  273. s32 rcode;
  274. memset(msg, 0, sizeof(msg));
  275. buf = dma_alloc_coherent(&pHba->pDev->dev, 80, &addr, GFP_KERNEL);
  276. if(!buf){
  277. printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
  278. return;
  279. }
  280. memset((void*)buf, 0, 36);
  281. len = 36;
  282. direction = 0x00000000;
  283. scsidir =0x40000000; // DATA IN (iop<--dev)
  284. if (dpt_dma64(pHba))
  285. reqlen = 17; // SINGLE SGE, 64 bit
  286. else
  287. reqlen = 14; // SINGLE SGE, 32 bit
  288. /* Stick the headers on */
  289. msg[0] = reqlen<<16 | SGL_OFFSET_12;
  290. msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
  291. msg[2] = 0;
  292. msg[3] = 0;
  293. // Adaptec/DPT Private stuff
  294. msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
  295. msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
  296. /* Direction, disconnect ok | sense data | simple queue , CDBLen */
  297. // I2O_SCB_FLAG_ENABLE_DISCONNECT |
  298. // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
  299. // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
  300. msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
  301. mptr=msg+7;
  302. memset(scb, 0, sizeof(scb));
  303. // Write SCSI command into the message - always 16 byte block
  304. scb[0] = INQUIRY;
  305. scb[1] = 0;
  306. scb[2] = 0;
  307. scb[3] = 0;
  308. scb[4] = 36;
  309. scb[5] = 0;
  310. // Don't care about the rest of scb
  311. memcpy(mptr, scb, sizeof(scb));
  312. mptr+=4;
  313. lenptr=mptr++; /* Remember me - fill in when we know */
  314. /* Now fill in the SGList and command */
  315. *lenptr = len;
  316. if (dpt_dma64(pHba)) {
  317. *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
  318. *mptr++ = 1 << PAGE_SHIFT;
  319. *mptr++ = 0xD0000000|direction|len;
  320. *mptr++ = dma_low(addr);
  321. *mptr++ = dma_high(addr);
  322. } else {
  323. *mptr++ = 0xD0000000|direction|len;
  324. *mptr++ = addr;
  325. }
  326. // Send it on it's way
  327. rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
  328. if (rcode != 0) {
  329. sprintf(pHba->detail, "Adaptec I2O RAID");
  330. printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
  331. if (rcode != -ETIME && rcode != -EINTR)
  332. dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
  333. } else {
  334. memset(pHba->detail, 0, sizeof(pHba->detail));
  335. memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
  336. memcpy(&(pHba->detail[16]), " Model: ", 8);
  337. memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
  338. memcpy(&(pHba->detail[40]), " FW: ", 4);
  339. memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
  340. pHba->detail[48] = '\0'; /* precautionary */
  341. dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
  342. }
  343. adpt_i2o_status_get(pHba);
  344. return ;
  345. }
  346. static int adpt_slave_configure(struct scsi_device * device)
  347. {
  348. struct Scsi_Host *host = device->host;
  349. adpt_hba* pHba;
  350. pHba = (adpt_hba *) host->hostdata[0];
  351. if (host->can_queue && device->tagged_supported) {
  352. scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
  353. host->can_queue - 1);
  354. } else {
  355. scsi_adjust_queue_depth(device, 0, 1);
  356. }
  357. return 0;
  358. }
  359. static int adpt_queue_lck(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
  360. {
  361. adpt_hba* pHba = NULL;
  362. struct adpt_device* pDev = NULL; /* dpt per device information */
  363. cmd->scsi_done = done;
  364. /*
  365. * SCSI REQUEST_SENSE commands will be executed automatically by the
  366. * Host Adapter for any errors, so they should not be executed
  367. * explicitly unless the Sense Data is zero indicating that no error
  368. * occurred.
  369. */
  370. if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
  371. cmd->result = (DID_OK << 16);
  372. cmd->scsi_done(cmd);
  373. return 0;
  374. }
  375. pHba = (adpt_hba*)cmd->device->host->hostdata[0];
  376. if (!pHba) {
  377. return FAILED;
  378. }
  379. rmb();
  380. /*
  381. * TODO: I need to block here if I am processing ioctl cmds
  382. * but if the outstanding cmds all finish before the ioctl,
  383. * the scsi-core will not know to start sending cmds to me again.
  384. * I need to a way to restart the scsi-cores queues or should I block
  385. * calling scsi_done on the outstanding cmds instead
  386. * for now we don't set the IOCTL state
  387. */
  388. if(((pHba->state) & DPTI_STATE_IOCTL) || ((pHba->state) & DPTI_STATE_RESET)) {
  389. pHba->host->last_reset = jiffies;
  390. pHba->host->resetting = 1;
  391. return 1;
  392. }
  393. // TODO if the cmd->device if offline then I may need to issue a bus rescan
  394. // followed by a get_lct to see if the device is there anymore
  395. if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
  396. /*
  397. * First command request for this device. Set up a pointer
  398. * to the device structure. This should be a TEST_UNIT_READY
  399. * command from scan_scsis_single.
  400. */
  401. if ((pDev = adpt_find_device(pHba, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun)) == NULL) {
  402. // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response
  403. // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
  404. cmd->result = (DID_NO_CONNECT << 16);
  405. cmd->scsi_done(cmd);
  406. return 0;
  407. }
  408. cmd->device->hostdata = pDev;
  409. }
  410. pDev->pScsi_dev = cmd->device;
  411. /*
  412. * If we are being called from when the device is being reset,
  413. * delay processing of the command until later.
  414. */
  415. if (pDev->state & DPTI_DEV_RESET ) {
  416. return FAILED;
  417. }
  418. return adpt_scsi_to_i2o(pHba, cmd, pDev);
  419. }
  420. static DEF_SCSI_QCMD(adpt_queue)
  421. static int adpt_bios_param(struct scsi_device *sdev, struct block_device *dev,
  422. sector_t capacity, int geom[])
  423. {
  424. int heads=-1;
  425. int sectors=-1;
  426. int cylinders=-1;
  427. // *** First lets set the default geometry ****
  428. // If the capacity is less than ox2000
  429. if (capacity < 0x2000 ) { // floppy
  430. heads = 18;
  431. sectors = 2;
  432. }
  433. // else if between 0x2000 and 0x20000
  434. else if (capacity < 0x20000) {
  435. heads = 64;
  436. sectors = 32;
  437. }
  438. // else if between 0x20000 and 0x40000
  439. else if (capacity < 0x40000) {
  440. heads = 65;
  441. sectors = 63;
  442. }
  443. // else if between 0x4000 and 0x80000
  444. else if (capacity < 0x80000) {
  445. heads = 128;
  446. sectors = 63;
  447. }
  448. // else if greater than 0x80000
  449. else {
  450. heads = 255;
  451. sectors = 63;
  452. }
  453. cylinders = sector_div(capacity, heads * sectors);
  454. // Special case if CDROM
  455. if(sdev->type == 5) { // CDROM
  456. heads = 252;
  457. sectors = 63;
  458. cylinders = 1111;
  459. }
  460. geom[0] = heads;
  461. geom[1] = sectors;
  462. geom[2] = cylinders;
  463. PDEBUG("adpt_bios_param: exit\n");
  464. return 0;
  465. }
  466. static const char *adpt_info(struct Scsi_Host *host)
  467. {
  468. adpt_hba* pHba;
  469. pHba = (adpt_hba *) host->hostdata[0];
  470. return (char *) (pHba->detail);
  471. }
  472. static int adpt_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  473. int length, int inout)
  474. {
  475. struct adpt_device* d;
  476. int id;
  477. int chan;
  478. int len = 0;
  479. int begin = 0;
  480. int pos = 0;
  481. adpt_hba* pHba;
  482. int unit;
  483. *start = buffer;
  484. if (inout == TRUE) {
  485. /*
  486. * The user has done a write and wants us to take the
  487. * data in the buffer and do something with it.
  488. * proc_scsiwrite calls us with inout = 1
  489. *
  490. * Read data from buffer (writing to us) - NOT SUPPORTED
  491. */
  492. return -EINVAL;
  493. }
  494. /*
  495. * inout = 0 means the user has done a read and wants information
  496. * returned, so we write information about the cards into the buffer
  497. * proc_scsiread() calls us with inout = 0
  498. */
  499. // Find HBA (host bus adapter) we are looking for
  500. mutex_lock(&adpt_configuration_lock);
  501. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  502. if (pHba->host == host) {
  503. break; /* found adapter */
  504. }
  505. }
  506. mutex_unlock(&adpt_configuration_lock);
  507. if (pHba == NULL) {
  508. return 0;
  509. }
  510. host = pHba->host;
  511. len = sprintf(buffer , "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
  512. len += sprintf(buffer+len, "%s\n", pHba->detail);
  513. len += sprintf(buffer+len, "SCSI Host=scsi%d Control Node=/dev/%s irq=%d\n",
  514. pHba->host->host_no, pHba->name, host->irq);
  515. len += sprintf(buffer+len, "\tpost fifo size = %d\n\treply fifo size = %d\n\tsg table size = %d\n\n",
  516. host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
  517. pos = begin + len;
  518. /* CHECKPOINT */
  519. if(pos > offset + length) {
  520. goto stop_output;
  521. }
  522. if(pos <= offset) {
  523. /*
  524. * If we haven't even written to where we last left
  525. * off (the last time we were called), reset the
  526. * beginning pointer.
  527. */
  528. len = 0;
  529. begin = pos;
  530. }
  531. len += sprintf(buffer+len, "Devices:\n");
  532. for(chan = 0; chan < MAX_CHANNEL; chan++) {
  533. for(id = 0; id < MAX_ID; id++) {
  534. d = pHba->channel[chan].device[id];
  535. while(d){
  536. len += sprintf(buffer+len,"\t%-24.24s", d->pScsi_dev->vendor);
  537. len += sprintf(buffer+len," Rev: %-8.8s\n", d->pScsi_dev->rev);
  538. pos = begin + len;
  539. /* CHECKPOINT */
  540. if(pos > offset + length) {
  541. goto stop_output;
  542. }
  543. if(pos <= offset) {
  544. len = 0;
  545. begin = pos;
  546. }
  547. unit = d->pI2o_dev->lct_data.tid;
  548. len += sprintf(buffer+len, "\tTID=%d, (Channel=%d, Target=%d, Lun=%d) (%s)\n\n",
  549. unit, (int)d->scsi_channel, (int)d->scsi_id, (int)d->scsi_lun,
  550. scsi_device_online(d->pScsi_dev)? "online":"offline");
  551. pos = begin + len;
  552. /* CHECKPOINT */
  553. if(pos > offset + length) {
  554. goto stop_output;
  555. }
  556. if(pos <= offset) {
  557. len = 0;
  558. begin = pos;
  559. }
  560. d = d->next_lun;
  561. }
  562. }
  563. }
  564. /*
  565. * begin is where we last checked our position with regards to offset
  566. * begin is always less than offset. len is relative to begin. It
  567. * is the number of bytes written past begin
  568. *
  569. */
  570. stop_output:
  571. /* stop the output and calculate the correct length */
  572. *(buffer + len) = '\0';
  573. *start = buffer + (offset - begin); /* Start of wanted data */
  574. len -= (offset - begin);
  575. if(len > length) {
  576. len = length;
  577. } else if(len < 0){
  578. len = 0;
  579. **start = '\0';
  580. }
  581. return len;
  582. }
  583. /*
  584. * Turn a struct scsi_cmnd * into a unique 32 bit 'context'.
  585. */
  586. static u32 adpt_cmd_to_context(struct scsi_cmnd *cmd)
  587. {
  588. return (u32)cmd->serial_number;
  589. }
  590. /*
  591. * Go from a u32 'context' to a struct scsi_cmnd * .
  592. * This could probably be made more efficient.
  593. */
  594. static struct scsi_cmnd *
  595. adpt_cmd_from_context(adpt_hba * pHba, u32 context)
  596. {
  597. struct scsi_cmnd * cmd;
  598. struct scsi_device * d;
  599. if (context == 0)
  600. return NULL;
  601. spin_unlock(pHba->host->host_lock);
  602. shost_for_each_device(d, pHba->host) {
  603. unsigned long flags;
  604. spin_lock_irqsave(&d->list_lock, flags);
  605. list_for_each_entry(cmd, &d->cmd_list, list) {
  606. if (((u32)cmd->serial_number == context)) {
  607. spin_unlock_irqrestore(&d->list_lock, flags);
  608. scsi_device_put(d);
  609. spin_lock(pHba->host->host_lock);
  610. return cmd;
  611. }
  612. }
  613. spin_unlock_irqrestore(&d->list_lock, flags);
  614. }
  615. spin_lock(pHba->host->host_lock);
  616. return NULL;
  617. }
  618. /*
  619. * Turn a pointer to ioctl reply data into an u32 'context'
  620. */
  621. static u32 adpt_ioctl_to_context(adpt_hba * pHba, void *reply)
  622. {
  623. #if BITS_PER_LONG == 32
  624. return (u32)(unsigned long)reply;
  625. #else
  626. ulong flags = 0;
  627. u32 nr, i;
  628. spin_lock_irqsave(pHba->host->host_lock, flags);
  629. nr = ARRAY_SIZE(pHba->ioctl_reply_context);
  630. for (i = 0; i < nr; i++) {
  631. if (pHba->ioctl_reply_context[i] == NULL) {
  632. pHba->ioctl_reply_context[i] = reply;
  633. break;
  634. }
  635. }
  636. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  637. if (i >= nr) {
  638. kfree (reply);
  639. printk(KERN_WARNING"%s: Too many outstanding "
  640. "ioctl commands\n", pHba->name);
  641. return (u32)-1;
  642. }
  643. return i;
  644. #endif
  645. }
  646. /*
  647. * Go from an u32 'context' to a pointer to ioctl reply data.
  648. */
  649. static void *adpt_ioctl_from_context(adpt_hba *pHba, u32 context)
  650. {
  651. #if BITS_PER_LONG == 32
  652. return (void *)(unsigned long)context;
  653. #else
  654. void *p = pHba->ioctl_reply_context[context];
  655. pHba->ioctl_reply_context[context] = NULL;
  656. return p;
  657. #endif
  658. }
  659. /*===========================================================================
  660. * Error Handling routines
  661. *===========================================================================
  662. */
  663. static int adpt_abort(struct scsi_cmnd * cmd)
  664. {
  665. adpt_hba* pHba = NULL; /* host bus adapter structure */
  666. struct adpt_device* dptdevice; /* dpt per device information */
  667. u32 msg[5];
  668. int rcode;
  669. if(cmd->serial_number == 0){
  670. return FAILED;
  671. }
  672. pHba = (adpt_hba*) cmd->device->host->hostdata[0];
  673. printk(KERN_INFO"%s: Trying to Abort\n",pHba->name);
  674. if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
  675. printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
  676. return FAILED;
  677. }
  678. memset(msg, 0, sizeof(msg));
  679. msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
  680. msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
  681. msg[2] = 0;
  682. msg[3]= 0;
  683. msg[4] = adpt_cmd_to_context(cmd);
  684. if (pHba->host)
  685. spin_lock_irq(pHba->host->host_lock);
  686. rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER);
  687. if (pHba->host)
  688. spin_unlock_irq(pHba->host->host_lock);
  689. if (rcode != 0) {
  690. if(rcode == -EOPNOTSUPP ){
  691. printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
  692. return FAILED;
  693. }
  694. printk(KERN_INFO"%s: Abort failed.\n",pHba->name);
  695. return FAILED;
  696. }
  697. printk(KERN_INFO"%s: Abort complete.\n",pHba->name);
  698. return SUCCESS;
  699. }
  700. #define I2O_DEVICE_RESET 0x27
  701. // This is the same for BLK and SCSI devices
  702. // NOTE this is wrong in the i2o.h definitions
  703. // This is not currently supported by our adapter but we issue it anyway
  704. static int adpt_device_reset(struct scsi_cmnd* cmd)
  705. {
  706. adpt_hba* pHba;
  707. u32 msg[4];
  708. u32 rcode;
  709. int old_state;
  710. struct adpt_device* d = cmd->device->hostdata;
  711. pHba = (void*) cmd->device->host->hostdata[0];
  712. printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
  713. if (!d) {
  714. printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
  715. return FAILED;
  716. }
  717. memset(msg, 0, sizeof(msg));
  718. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  719. msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
  720. msg[2] = 0;
  721. msg[3] = 0;
  722. if (pHba->host)
  723. spin_lock_irq(pHba->host->host_lock);
  724. old_state = d->state;
  725. d->state |= DPTI_DEV_RESET;
  726. rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
  727. d->state = old_state;
  728. if (pHba->host)
  729. spin_unlock_irq(pHba->host->host_lock);
  730. if (rcode != 0) {
  731. if(rcode == -EOPNOTSUPP ){
  732. printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
  733. return FAILED;
  734. }
  735. printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
  736. return FAILED;
  737. } else {
  738. printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
  739. return SUCCESS;
  740. }
  741. }
  742. #define I2O_HBA_BUS_RESET 0x87
  743. // This version of bus reset is called by the eh_error handler
  744. static int adpt_bus_reset(struct scsi_cmnd* cmd)
  745. {
  746. adpt_hba* pHba;
  747. u32 msg[4];
  748. u32 rcode;
  749. pHba = (adpt_hba*)cmd->device->host->hostdata[0];
  750. memset(msg, 0, sizeof(msg));
  751. printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->device->channel,pHba->channel[cmd->device->channel].tid );
  752. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  753. msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->device->channel].tid);
  754. msg[2] = 0;
  755. msg[3] = 0;
  756. if (pHba->host)
  757. spin_lock_irq(pHba->host->host_lock);
  758. rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
  759. if (pHba->host)
  760. spin_unlock_irq(pHba->host->host_lock);
  761. if (rcode != 0) {
  762. printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
  763. return FAILED;
  764. } else {
  765. printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
  766. return SUCCESS;
  767. }
  768. }
  769. // This version of reset is called by the eh_error_handler
  770. static int __adpt_reset(struct scsi_cmnd* cmd)
  771. {
  772. adpt_hba* pHba;
  773. int rcode;
  774. pHba = (adpt_hba*)cmd->device->host->hostdata[0];
  775. printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n",pHba->name,cmd->device->channel,pHba->channel[cmd->device->channel].tid );
  776. rcode = adpt_hba_reset(pHba);
  777. if(rcode == 0){
  778. printk(KERN_WARNING"%s: HBA reset complete\n",pHba->name);
  779. return SUCCESS;
  780. } else {
  781. printk(KERN_WARNING"%s: HBA reset failed (%x)\n",pHba->name, rcode);
  782. return FAILED;
  783. }
  784. }
  785. static int adpt_reset(struct scsi_cmnd* cmd)
  786. {
  787. int rc;
  788. spin_lock_irq(cmd->device->host->host_lock);
  789. rc = __adpt_reset(cmd);
  790. spin_unlock_irq(cmd->device->host->host_lock);
  791. return rc;
  792. }
  793. // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
  794. static int adpt_hba_reset(adpt_hba* pHba)
  795. {
  796. int rcode;
  797. pHba->state |= DPTI_STATE_RESET;
  798. // Activate does get status , init outbound, and get hrt
  799. if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
  800. printk(KERN_ERR "%s: Could not activate\n", pHba->name);
  801. adpt_i2o_delete_hba(pHba);
  802. return rcode;
  803. }
  804. if ((rcode=adpt_i2o_build_sys_table()) < 0) {
  805. adpt_i2o_delete_hba(pHba);
  806. return rcode;
  807. }
  808. PDEBUG("%s: in HOLD state\n",pHba->name);
  809. if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
  810. adpt_i2o_delete_hba(pHba);
  811. return rcode;
  812. }
  813. PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
  814. if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
  815. adpt_i2o_delete_hba(pHba);
  816. return rcode;
  817. }
  818. if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
  819. adpt_i2o_delete_hba(pHba);
  820. return rcode;
  821. }
  822. pHba->state &= ~DPTI_STATE_RESET;
  823. adpt_fail_posted_scbs(pHba);
  824. return 0; /* return success */
  825. }
  826. /*===========================================================================
  827. *
  828. *===========================================================================
  829. */
  830. static void adpt_i2o_sys_shutdown(void)
  831. {
  832. adpt_hba *pHba, *pNext;
  833. struct adpt_i2o_post_wait_data *p1, *old;
  834. printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
  835. printk(KERN_INFO" This could take a few minutes if there are many devices attached\n");
  836. /* Delete all IOPs from the controller chain */
  837. /* They should have already been released by the
  838. * scsi-core
  839. */
  840. for (pHba = hba_chain; pHba; pHba = pNext) {
  841. pNext = pHba->next;
  842. adpt_i2o_delete_hba(pHba);
  843. }
  844. /* Remove any timedout entries from the wait queue. */
  845. // spin_lock_irqsave(&adpt_post_wait_lock, flags);
  846. /* Nothing should be outstanding at this point so just
  847. * free them
  848. */
  849. for(p1 = adpt_post_wait_queue; p1;) {
  850. old = p1;
  851. p1 = p1->next;
  852. kfree(old);
  853. }
  854. // spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  855. adpt_post_wait_queue = NULL;
  856. printk(KERN_INFO "Adaptec I2O controllers down.\n");
  857. }
  858. static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev)
  859. {
  860. adpt_hba* pHba = NULL;
  861. adpt_hba* p = NULL;
  862. ulong base_addr0_phys = 0;
  863. ulong base_addr1_phys = 0;
  864. u32 hba_map0_area_size = 0;
  865. u32 hba_map1_area_size = 0;
  866. void __iomem *base_addr_virt = NULL;
  867. void __iomem *msg_addr_virt = NULL;
  868. int dma64 = 0;
  869. int raptorFlag = FALSE;
  870. if(pci_enable_device(pDev)) {
  871. return -EINVAL;
  872. }
  873. if (pci_request_regions(pDev, "dpt_i2o")) {
  874. PERROR("dpti: adpt_config_hba: pci request region failed\n");
  875. return -EINVAL;
  876. }
  877. pci_set_master(pDev);
  878. /*
  879. * See if we should enable dma64 mode.
  880. */
  881. if (sizeof(dma_addr_t) > 4 &&
  882. pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
  883. if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
  884. dma64 = 1;
  885. }
  886. if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
  887. return -EINVAL;
  888. /* adapter only supports message blocks below 4GB */
  889. pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
  890. base_addr0_phys = pci_resource_start(pDev,0);
  891. hba_map0_area_size = pci_resource_len(pDev,0);
  892. // Check if standard PCI card or single BAR Raptor
  893. if(pDev->device == PCI_DPT_DEVICE_ID){
  894. if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
  895. // Raptor card with this device id needs 4M
  896. hba_map0_area_size = 0x400000;
  897. } else { // Not Raptor - it is a PCI card
  898. if(hba_map0_area_size > 0x100000 ){
  899. hba_map0_area_size = 0x100000;
  900. }
  901. }
  902. } else {// Raptor split BAR config
  903. // Use BAR1 in this configuration
  904. base_addr1_phys = pci_resource_start(pDev,1);
  905. hba_map1_area_size = pci_resource_len(pDev,1);
  906. raptorFlag = TRUE;
  907. }
  908. #if BITS_PER_LONG == 64
  909. /*
  910. * The original Adaptec 64 bit driver has this comment here:
  911. * "x86_64 machines need more optimal mappings"
  912. *
  913. * I assume some HBAs report ridiculously large mappings
  914. * and we need to limit them on platforms with IOMMUs.
  915. */
  916. if (raptorFlag == TRUE) {
  917. if (hba_map0_area_size > 128)
  918. hba_map0_area_size = 128;
  919. if (hba_map1_area_size > 524288)
  920. hba_map1_area_size = 524288;
  921. } else {
  922. if (hba_map0_area_size > 524288)
  923. hba_map0_area_size = 524288;
  924. }
  925. #endif
  926. base_addr_virt = ioremap(base_addr0_phys,hba_map0_area_size);
  927. if (!base_addr_virt) {
  928. pci_release_regions(pDev);
  929. PERROR("dpti: adpt_config_hba: io remap failed\n");
  930. return -EINVAL;
  931. }
  932. if(raptorFlag == TRUE) {
  933. msg_addr_virt = ioremap(base_addr1_phys, hba_map1_area_size );
  934. if (!msg_addr_virt) {
  935. PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
  936. iounmap(base_addr_virt);
  937. pci_release_regions(pDev);
  938. return -EINVAL;
  939. }
  940. } else {
  941. msg_addr_virt = base_addr_virt;
  942. }
  943. // Allocate and zero the data structure
  944. pHba = kzalloc(sizeof(adpt_hba), GFP_KERNEL);
  945. if (!pHba) {
  946. if (msg_addr_virt != base_addr_virt)
  947. iounmap(msg_addr_virt);
  948. iounmap(base_addr_virt);
  949. pci_release_regions(pDev);
  950. return -ENOMEM;
  951. }
  952. mutex_lock(&adpt_configuration_lock);
  953. if(hba_chain != NULL){
  954. for(p = hba_chain; p->next; p = p->next);
  955. p->next = pHba;
  956. } else {
  957. hba_chain = pHba;
  958. }
  959. pHba->next = NULL;
  960. pHba->unit = hba_count;
  961. sprintf(pHba->name, "dpti%d", hba_count);
  962. hba_count++;
  963. mutex_unlock(&adpt_configuration_lock);
  964. pHba->pDev = pDev;
  965. pHba->base_addr_phys = base_addr0_phys;
  966. // Set up the Virtual Base Address of the I2O Device
  967. pHba->base_addr_virt = base_addr_virt;
  968. pHba->msg_addr_virt = msg_addr_virt;
  969. pHba->irq_mask = base_addr_virt+0x30;
  970. pHba->post_port = base_addr_virt+0x40;
  971. pHba->reply_port = base_addr_virt+0x44;
  972. pHba->hrt = NULL;
  973. pHba->lct = NULL;
  974. pHba->lct_size = 0;
  975. pHba->status_block = NULL;
  976. pHba->post_count = 0;
  977. pHba->state = DPTI_STATE_RESET;
  978. pHba->pDev = pDev;
  979. pHba->devices = NULL;
  980. pHba->dma64 = dma64;
  981. // Initializing the spinlocks
  982. spin_lock_init(&pHba->state_lock);
  983. spin_lock_init(&adpt_post_wait_lock);
  984. if(raptorFlag == 0){
  985. printk(KERN_INFO "Adaptec I2O RAID controller"
  986. " %d at %p size=%x irq=%d%s\n",
  987. hba_count-1, base_addr_virt,
  988. hba_map0_area_size, pDev->irq,
  989. dma64 ? " (64-bit DMA)" : "");
  990. } else {
  991. printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d%s\n",
  992. hba_count-1, pDev->irq,
  993. dma64 ? " (64-bit DMA)" : "");
  994. printk(KERN_INFO" BAR0 %p - size= %x\n",base_addr_virt,hba_map0_area_size);
  995. printk(KERN_INFO" BAR1 %p - size= %x\n",msg_addr_virt,hba_map1_area_size);
  996. }
  997. if (request_irq (pDev->irq, adpt_isr, IRQF_SHARED, pHba->name, pHba)) {
  998. printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
  999. adpt_i2o_delete_hba(pHba);
  1000. return -EINVAL;
  1001. }
  1002. return 0;
  1003. }
  1004. static void adpt_i2o_delete_hba(adpt_hba* pHba)
  1005. {
  1006. adpt_hba* p1;
  1007. adpt_hba* p2;
  1008. struct i2o_device* d;
  1009. struct i2o_device* next;
  1010. int i;
  1011. int j;
  1012. struct adpt_device* pDev;
  1013. struct adpt_device* pNext;
  1014. mutex_lock(&adpt_configuration_lock);
  1015. // scsi_unregister calls our adpt_release which
  1016. // does a quiese
  1017. if(pHba->host){
  1018. free_irq(pHba->host->irq, pHba);
  1019. }
  1020. p2 = NULL;
  1021. for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
  1022. if(p1 == pHba) {
  1023. if(p2) {
  1024. p2->next = p1->next;
  1025. } else {
  1026. hba_chain = p1->next;
  1027. }
  1028. break;
  1029. }
  1030. }
  1031. hba_count--;
  1032. mutex_unlock(&adpt_configuration_lock);
  1033. iounmap(pHba->base_addr_virt);
  1034. pci_release_regions(pHba->pDev);
  1035. if(pHba->msg_addr_virt != pHba->base_addr_virt){
  1036. iounmap(pHba->msg_addr_virt);
  1037. }
  1038. if(pHba->FwDebugBuffer_P)
  1039. iounmap(pHba->FwDebugBuffer_P);
  1040. if(pHba->hrt) {
  1041. dma_free_coherent(&pHba->pDev->dev,
  1042. pHba->hrt->num_entries * pHba->hrt->entry_len << 2,
  1043. pHba->hrt, pHba->hrt_pa);
  1044. }
  1045. if(pHba->lct) {
  1046. dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
  1047. pHba->lct, pHba->lct_pa);
  1048. }
  1049. if(pHba->status_block) {
  1050. dma_free_coherent(&pHba->pDev->dev, sizeof(i2o_status_block),
  1051. pHba->status_block, pHba->status_block_pa);
  1052. }
  1053. if(pHba->reply_pool) {
  1054. dma_free_coherent(&pHba->pDev->dev,
  1055. pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
  1056. pHba->reply_pool, pHba->reply_pool_pa);
  1057. }
  1058. for(d = pHba->devices; d ; d = next){
  1059. next = d->next;
  1060. kfree(d);
  1061. }
  1062. for(i = 0 ; i < pHba->top_scsi_channel ; i++){
  1063. for(j = 0; j < MAX_ID; j++){
  1064. if(pHba->channel[i].device[j] != NULL){
  1065. for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
  1066. pNext = pDev->next_lun;
  1067. kfree(pDev);
  1068. }
  1069. }
  1070. }
  1071. }
  1072. pci_dev_put(pHba->pDev);
  1073. if (adpt_sysfs_class)
  1074. device_destroy(adpt_sysfs_class,
  1075. MKDEV(DPTI_I2O_MAJOR, pHba->unit));
  1076. kfree(pHba);
  1077. if(hba_count <= 0){
  1078. unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);
  1079. if (adpt_sysfs_class) {
  1080. class_destroy(adpt_sysfs_class);
  1081. adpt_sysfs_class = NULL;
  1082. }
  1083. }
  1084. }
  1085. static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u32 lun)
  1086. {
  1087. struct adpt_device* d;
  1088. if(chan < 0 || chan >= MAX_CHANNEL)
  1089. return NULL;
  1090. if( pHba->channel[chan].device == NULL){
  1091. printk(KERN_DEBUG"Adaptec I2O RAID: Trying to find device before they are allocated\n");
  1092. return NULL;
  1093. }
  1094. d = pHba->channel[chan].device[id];
  1095. if(!d || d->tid == 0) {
  1096. return NULL;
  1097. }
  1098. /* If it is the only lun at that address then this should match*/
  1099. if(d->scsi_lun == lun){
  1100. return d;
  1101. }
  1102. /* else we need to look through all the luns */
  1103. for(d=d->next_lun ; d ; d = d->next_lun){
  1104. if(d->scsi_lun == lun){
  1105. return d;
  1106. }
  1107. }
  1108. return NULL;
  1109. }
  1110. static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
  1111. {
  1112. // I used my own version of the WAIT_QUEUE_HEAD
  1113. // to handle some version differences
  1114. // When embedded in the kernel this could go back to the vanilla one
  1115. ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
  1116. int status = 0;
  1117. ulong flags = 0;
  1118. struct adpt_i2o_post_wait_data *p1, *p2;
  1119. struct adpt_i2o_post_wait_data *wait_data =
  1120. kmalloc(sizeof(struct adpt_i2o_post_wait_data), GFP_ATOMIC);
  1121. DECLARE_WAITQUEUE(wait, current);
  1122. if (!wait_data)
  1123. return -ENOMEM;
  1124. /*
  1125. * The spin locking is needed to keep anyone from playing
  1126. * with the queue pointers and id while we do the same
  1127. */
  1128. spin_lock_irqsave(&adpt_post_wait_lock, flags);
  1129. // TODO we need a MORE unique way of getting ids
  1130. // to support async LCT get
  1131. wait_data->next = adpt_post_wait_queue;
  1132. adpt_post_wait_queue = wait_data;
  1133. adpt_post_wait_id++;
  1134. adpt_post_wait_id &= 0x7fff;
  1135. wait_data->id = adpt_post_wait_id;
  1136. spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  1137. wait_data->wq = &adpt_wq_i2o_post;
  1138. wait_data->status = -ETIMEDOUT;
  1139. add_wait_queue(&adpt_wq_i2o_post, &wait);
  1140. msg[2] |= 0x80000000 | ((u32)wait_data->id);
  1141. timeout *= HZ;
  1142. if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
  1143. set_current_state(TASK_INTERRUPTIBLE);
  1144. if(pHba->host)
  1145. spin_unlock_irq(pHba->host->host_lock);
  1146. if (!timeout)
  1147. schedule();
  1148. else{
  1149. timeout = schedule_timeout(timeout);
  1150. if (timeout == 0) {
  1151. // I/O issued, but cannot get result in
  1152. // specified time. Freeing resorces is
  1153. // dangerous.
  1154. status = -ETIME;
  1155. }
  1156. }
  1157. if(pHba->host)
  1158. spin_lock_irq(pHba->host->host_lock);
  1159. }
  1160. remove_wait_queue(&adpt_wq_i2o_post, &wait);
  1161. if(status == -ETIMEDOUT){
  1162. printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
  1163. // We will have to free the wait_data memory during shutdown
  1164. return status;
  1165. }
  1166. /* Remove the entry from the queue. */
  1167. p2 = NULL;
  1168. spin_lock_irqsave(&adpt_post_wait_lock, flags);
  1169. for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
  1170. if(p1 == wait_data) {
  1171. if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
  1172. status = -EOPNOTSUPP;
  1173. }
  1174. if(p2) {
  1175. p2->next = p1->next;
  1176. } else {
  1177. adpt_post_wait_queue = p1->next;
  1178. }
  1179. break;
  1180. }
  1181. }
  1182. spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
  1183. kfree(wait_data);
  1184. return status;
  1185. }
  1186. static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
  1187. {
  1188. u32 m = EMPTY_QUEUE;
  1189. u32 __iomem *msg;
  1190. ulong timeout = jiffies + 30*HZ;
  1191. do {
  1192. rmb();
  1193. m = readl(pHba->post_port);
  1194. if (m != EMPTY_QUEUE) {
  1195. break;
  1196. }
  1197. if(time_after(jiffies,timeout)){
  1198. printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
  1199. return -ETIMEDOUT;
  1200. }
  1201. schedule_timeout_uninterruptible(1);
  1202. } while(m == EMPTY_QUEUE);
  1203. msg = pHba->msg_addr_virt + m;
  1204. memcpy_toio(msg, data, len);
  1205. wmb();
  1206. //post message
  1207. writel(m, pHba->post_port);
  1208. wmb();
  1209. return 0;
  1210. }
  1211. static void adpt_i2o_post_wait_complete(u32 context, int status)
  1212. {
  1213. struct adpt_i2o_post_wait_data *p1 = NULL;
  1214. /*
  1215. * We need to search through the adpt_post_wait
  1216. * queue to see if the given message is still
  1217. * outstanding. If not, it means that the IOP
  1218. * took longer to respond to the message than we
  1219. * had allowed and timer has already expired.
  1220. * Not much we can do about that except log
  1221. * it for debug purposes, increase timeout, and recompile
  1222. *
  1223. * Lock needed to keep anyone from moving queue pointers
  1224. * around while we're looking through them.
  1225. */
  1226. context &= 0x7fff;
  1227. spin_lock(&adpt_post_wait_lock);
  1228. for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
  1229. if(p1->id == context) {
  1230. p1->status = status;
  1231. spin_unlock(&adpt_post_wait_lock);
  1232. wake_up_interruptible(p1->wq);
  1233. return;
  1234. }
  1235. }
  1236. spin_unlock(&adpt_post_wait_lock);
  1237. // If this happens we lose commands that probably really completed
  1238. printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
  1239. printk(KERN_DEBUG" Tasks in wait queue:\n");
  1240. for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
  1241. printk(KERN_DEBUG" %d\n",p1->id);
  1242. }
  1243. return;
  1244. }
  1245. static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
  1246. {
  1247. u32 msg[8];
  1248. u8* status;
  1249. dma_addr_t addr;
  1250. u32 m = EMPTY_QUEUE ;
  1251. ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
  1252. if(pHba->initialized == FALSE) { // First time reset should be quick
  1253. timeout = jiffies + (25*HZ);
  1254. } else {
  1255. adpt_i2o_quiesce_hba(pHba);
  1256. }
  1257. do {
  1258. rmb();
  1259. m = readl(pHba->post_port);
  1260. if (m != EMPTY_QUEUE) {
  1261. break;
  1262. }
  1263. if(time_after(jiffies,timeout)){
  1264. printk(KERN_WARNING"Timeout waiting for message!\n");
  1265. return -ETIMEDOUT;
  1266. }
  1267. schedule_timeout_uninterruptible(1);
  1268. } while (m == EMPTY_QUEUE);
  1269. status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
  1270. if(status == NULL) {
  1271. adpt_send_nop(pHba, m);
  1272. printk(KERN_ERR"IOP reset failed - no free memory.\n");
  1273. return -ENOMEM;
  1274. }
  1275. memset(status,0,4);
  1276. msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
  1277. msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
  1278. msg[2]=0;
  1279. msg[3]=0;
  1280. msg[4]=0;
  1281. msg[5]=0;
  1282. msg[6]=dma_low(addr);
  1283. msg[7]=dma_high(addr);
  1284. memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
  1285. wmb();
  1286. writel(m, pHba->post_port);
  1287. wmb();
  1288. while(*status == 0){
  1289. if(time_after(jiffies,timeout)){
  1290. printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
  1291. /* We lose 4 bytes of "status" here, but we cannot
  1292. free these because controller may awake and corrupt
  1293. those bytes at any time */
  1294. /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
  1295. return -ETIMEDOUT;
  1296. }
  1297. rmb();
  1298. schedule_timeout_uninterruptible(1);
  1299. }
  1300. if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
  1301. PDEBUG("%s: Reset in progress...\n", pHba->name);
  1302. // Here we wait for message frame to become available
  1303. // indicated that reset has finished
  1304. do {
  1305. rmb();
  1306. m = readl(pHba->post_port);
  1307. if (m != EMPTY_QUEUE) {
  1308. break;
  1309. }
  1310. if(time_after(jiffies,timeout)){
  1311. printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
  1312. /* We lose 4 bytes of "status" here, but we
  1313. cannot free these because controller may
  1314. awake and corrupt those bytes at any time */
  1315. /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
  1316. return -ETIMEDOUT;
  1317. }
  1318. schedule_timeout_uninterruptible(1);
  1319. } while (m == EMPTY_QUEUE);
  1320. // Flush the offset
  1321. adpt_send_nop(pHba, m);
  1322. }
  1323. adpt_i2o_status_get(pHba);
  1324. if(*status == 0x02 ||
  1325. pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
  1326. printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
  1327. pHba->name);
  1328. } else {
  1329. PDEBUG("%s: Reset completed.\n", pHba->name);
  1330. }
  1331. dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
  1332. #ifdef UARTDELAY
  1333. // This delay is to allow someone attached to the card through the debug UART to
  1334. // set up the dump levels that they want before the rest of the initialization sequence
  1335. adpt_delay(20000);
  1336. #endif
  1337. return 0;
  1338. }
  1339. static int adpt_i2o_parse_lct(adpt_hba* pHba)
  1340. {
  1341. int i;
  1342. int max;
  1343. int tid;
  1344. struct i2o_device *d;
  1345. i2o_lct *lct = pHba->lct;
  1346. u8 bus_no = 0;
  1347. s16 scsi_id;
  1348. s16 scsi_lun;
  1349. u32 buf[10]; // larger than 7, or 8 ...
  1350. struct adpt_device* pDev;
  1351. if (lct == NULL) {
  1352. printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
  1353. return -1;
  1354. }
  1355. max = lct->table_size;
  1356. max -= 3;
  1357. max /= 9;
  1358. for(i=0;i<max;i++) {
  1359. if( lct->lct_entry[i].user_tid != 0xfff){
  1360. /*
  1361. * If we have hidden devices, we need to inform the upper layers about
  1362. * the possible maximum id reference to handle device access when
  1363. * an array is disassembled. This code has no other purpose but to
  1364. * allow us future access to devices that are currently hidden
  1365. * behind arrays, hotspares or have not been configured (JBOD mode).
  1366. */
  1367. if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
  1368. lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
  1369. lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  1370. continue;
  1371. }
  1372. tid = lct->lct_entry[i].tid;
  1373. // I2O_DPT_DEVICE_INFO_GROUP_NO;
  1374. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
  1375. continue;
  1376. }
  1377. bus_no = buf[0]>>16;
  1378. scsi_id = buf[1];
  1379. scsi_lun = (buf[2]>>8 )&0xff;
  1380. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1381. printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
  1382. continue;
  1383. }
  1384. if (scsi_id >= MAX_ID){
  1385. printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
  1386. continue;
  1387. }
  1388. if(bus_no > pHba->top_scsi_channel){
  1389. pHba->top_scsi_channel = bus_no;
  1390. }
  1391. if(scsi_id > pHba->top_scsi_id){
  1392. pHba->top_scsi_id = scsi_id;
  1393. }
  1394. if(scsi_lun > pHba->top_scsi_lun){
  1395. pHba->top_scsi_lun = scsi_lun;
  1396. }
  1397. continue;
  1398. }
  1399. d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
  1400. if(d==NULL)
  1401. {
  1402. printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
  1403. return -ENOMEM;
  1404. }
  1405. d->controller = pHba;
  1406. d->next = NULL;
  1407. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  1408. d->flags = 0;
  1409. tid = d->lct_data.tid;
  1410. adpt_i2o_report_hba_unit(pHba, d);
  1411. adpt_i2o_install_device(pHba, d);
  1412. }
  1413. bus_no = 0;
  1414. for(d = pHba->devices; d ; d = d->next) {
  1415. if(d->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT ||
  1416. d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PORT){
  1417. tid = d->lct_data.tid;
  1418. // TODO get the bus_no from hrt-but for now they are in order
  1419. //bus_no =
  1420. if(bus_no > pHba->top_scsi_channel){
  1421. pHba->top_scsi_channel = bus_no;
  1422. }
  1423. pHba->channel[bus_no].type = d->lct_data.class_id;
  1424. pHba->channel[bus_no].tid = tid;
  1425. if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
  1426. {
  1427. pHba->channel[bus_no].scsi_id = buf[1];
  1428. PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
  1429. }
  1430. // TODO remove - this is just until we get from hrt
  1431. bus_no++;
  1432. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1433. printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. // Setup adpt_device table
  1439. for(d = pHba->devices; d ; d = d->next) {
  1440. if(d->lct_data.class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
  1441. d->lct_data.class_id == I2O_CLASS_SCSI_PERIPHERAL ||
  1442. d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  1443. tid = d->lct_data.tid;
  1444. scsi_id = -1;
  1445. // I2O_DPT_DEVICE_INFO_GROUP_NO;
  1446. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
  1447. bus_no = buf[0]>>16;
  1448. scsi_id = buf[1];
  1449. scsi_lun = (buf[2]>>8 )&0xff;
  1450. if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
  1451. continue;
  1452. }
  1453. if (scsi_id >= MAX_ID) {
  1454. continue;
  1455. }
  1456. if( pHba->channel[bus_no].device[scsi_id] == NULL){
  1457. pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
  1458. if(pDev == NULL) {
  1459. return -ENOMEM;
  1460. }
  1461. pHba->channel[bus_no].device[scsi_id] = pDev;
  1462. } else {
  1463. for( pDev = pHba->channel[bus_no].device[scsi_id];
  1464. pDev->next_lun; pDev = pDev->next_lun){
  1465. }
  1466. pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
  1467. if(pDev->next_lun == NULL) {
  1468. return -ENOMEM;
  1469. }
  1470. pDev = pDev->next_lun;
  1471. }
  1472. pDev->tid = tid;
  1473. pDev->scsi_channel = bus_no;
  1474. pDev->scsi_id = scsi_id;
  1475. pDev->scsi_lun = scsi_lun;
  1476. pDev->pI2o_dev = d;
  1477. d->owner = pDev;
  1478. pDev->type = (buf[0])&0xff;
  1479. pDev->flags = (buf[0]>>8)&0xff;
  1480. if(scsi_id > pHba->top_scsi_id){
  1481. pHba->top_scsi_id = scsi_id;
  1482. }
  1483. if(scsi_lun > pHba->top_scsi_lun){
  1484. pHba->top_scsi_lun = scsi_lun;
  1485. }
  1486. }
  1487. if(scsi_id == -1){
  1488. printk(KERN_WARNING"Could not find SCSI ID for %s\n",
  1489. d->lct_data.identity_tag);
  1490. }
  1491. }
  1492. }
  1493. return 0;
  1494. }
  1495. /*
  1496. * Each I2O controller has a chain of devices on it - these match
  1497. * the useful parts of the LCT of the board.
  1498. */
  1499. static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
  1500. {
  1501. mutex_lock(&adpt_configuration_lock);
  1502. d->controller=pHba;
  1503. d->owner=NULL;
  1504. d->next=pHba->devices;
  1505. d->prev=NULL;
  1506. if (pHba->devices != NULL){
  1507. pHba->devices->prev=d;
  1508. }
  1509. pHba->devices=d;
  1510. *d->dev_name = 0;
  1511. mutex_unlock(&adpt_configuration_lock);
  1512. return 0;
  1513. }
  1514. static int adpt_open(struct inode *inode, struct file *file)
  1515. {
  1516. int minor;
  1517. adpt_hba* pHba;
  1518. mutex_lock(&adpt_mutex);
  1519. //TODO check for root access
  1520. //
  1521. minor = iminor(inode);
  1522. if (minor >= hba_count) {
  1523. mutex_unlock(&adpt_mutex);
  1524. return -ENXIO;
  1525. }
  1526. mutex_lock(&adpt_configuration_lock);
  1527. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1528. if (pHba->unit == minor) {
  1529. break; /* found adapter */
  1530. }
  1531. }
  1532. if (pHba == NULL) {
  1533. mutex_unlock(&adpt_configuration_lock);
  1534. mutex_unlock(&adpt_mutex);
  1535. return -ENXIO;
  1536. }
  1537. // if(pHba->in_use){
  1538. // mutex_unlock(&adpt_configuration_lock);
  1539. // return -EBUSY;
  1540. // }
  1541. pHba->in_use = 1;
  1542. mutex_unlock(&adpt_configuration_lock);
  1543. mutex_unlock(&adpt_mutex);
  1544. return 0;
  1545. }
  1546. static int adpt_close(struct inode *inode, struct file *file)
  1547. {
  1548. int minor;
  1549. adpt_hba* pHba;
  1550. minor = iminor(inode);
  1551. if (minor >= hba_count) {
  1552. return -ENXIO;
  1553. }
  1554. mutex_lock(&adpt_configuration_lock);
  1555. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1556. if (pHba->unit == minor) {
  1557. break; /* found adapter */
  1558. }
  1559. }
  1560. mutex_unlock(&adpt_configuration_lock);
  1561. if (pHba == NULL) {
  1562. return -ENXIO;
  1563. }
  1564. pHba->in_use = 0;
  1565. return 0;
  1566. }
  1567. static int adpt_i2o_passthru(adpt_hba* pHba, u32 __user *arg)
  1568. {
  1569. u32 msg[MAX_MESSAGE_SIZE];
  1570. u32* reply = NULL;
  1571. u32 size = 0;
  1572. u32 reply_size = 0;
  1573. u32 __user *user_msg = arg;
  1574. u32 __user * user_reply = NULL;
  1575. void *sg_list[pHba->sg_tablesize];
  1576. u32 sg_offset = 0;
  1577. u32 sg_count = 0;
  1578. int sg_index = 0;
  1579. u32 i = 0;
  1580. u32 rcode = 0;
  1581. void *p = NULL;
  1582. dma_addr_t addr;
  1583. ulong flags = 0;
  1584. memset(&msg, 0, MAX_MESSAGE_SIZE*4);
  1585. // get user msg size in u32s
  1586. if(get_user(size, &user_msg[0])){
  1587. return -EFAULT;
  1588. }
  1589. size = size>>16;
  1590. user_reply = &user_msg[size];
  1591. if(size > MAX_MESSAGE_SIZE){
  1592. return -EFAULT;
  1593. }
  1594. size *= 4; // Convert to bytes
  1595. /* Copy in the user's I2O command */
  1596. if(copy_from_user(msg, user_msg, size)) {
  1597. return -EFAULT;
  1598. }
  1599. get_user(reply_size, &user_reply[0]);
  1600. reply_size = reply_size>>16;
  1601. if(reply_size > REPLY_FRAME_SIZE){
  1602. reply_size = REPLY_FRAME_SIZE;
  1603. }
  1604. reply_size *= 4;
  1605. reply = kzalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
  1606. if(reply == NULL) {
  1607. printk(KERN_WARNING"%s: Could not allocate reply buffer\n",pHba->name);
  1608. return -ENOMEM;
  1609. }
  1610. sg_offset = (msg[0]>>4)&0xf;
  1611. msg[2] = 0x40000000; // IOCTL context
  1612. msg[3] = adpt_ioctl_to_context(pHba, reply);
  1613. if (msg[3] == (u32)-1)
  1614. return -EBUSY;
  1615. memset(sg_list,0, sizeof(sg_list[0])*pHba->sg_tablesize);
  1616. if(sg_offset) {
  1617. // TODO add 64 bit API
  1618. struct sg_simple_element *sg = (struct sg_simple_element*) (msg+sg_offset);
  1619. sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
  1620. if (sg_count > pHba->sg_tablesize){
  1621. printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", pHba->name,sg_count);
  1622. kfree (reply);
  1623. return -EINVAL;
  1624. }
  1625. for(i = 0; i < sg_count; i++) {
  1626. int sg_size;
  1627. if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
  1628. printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",pHba->name,i, sg[i].flag_count);
  1629. rcode = -EINVAL;
  1630. goto cleanup;
  1631. }
  1632. sg_size = sg[i].flag_count & 0xffffff;
  1633. /* Allocate memory for the transfer */
  1634. p = dma_alloc_coherent(&pHba->pDev->dev, sg_size, &addr, GFP_KERNEL);
  1635. if(!p) {
  1636. printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  1637. pHba->name,sg_size,i,sg_count);
  1638. rcode = -ENOMEM;
  1639. goto cleanup;
  1640. }
  1641. sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
  1642. /* Copy in the user's SG buffer if necessary */
  1643. if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
  1644. // sg_simple_element API is 32 bit
  1645. if (copy_from_user(p,(void __user *)(ulong)sg[i].addr_bus, sg_size)) {
  1646. printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",pHba->name,i);
  1647. rcode = -EFAULT;
  1648. goto cleanup;
  1649. }
  1650. }
  1651. /* sg_simple_element API is 32 bit, but addr < 4GB */
  1652. sg[i].addr_bus = addr;
  1653. }
  1654. }
  1655. do {
  1656. if(pHba->host)
  1657. spin_lock_irqsave(pHba->host->host_lock, flags);
  1658. // This state stops any new commands from enterring the
  1659. // controller while processing the ioctl
  1660. // pHba->state |= DPTI_STATE_IOCTL;
  1661. // We can't set this now - The scsi subsystem sets host_blocked and
  1662. // the queue empties and stops. We need a way to restart the queue
  1663. rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
  1664. if (rcode != 0)
  1665. printk("adpt_i2o_passthru: post wait failed %d %p\n",
  1666. rcode, reply);
  1667. // pHba->state &= ~DPTI_STATE_IOCTL;
  1668. if(pHba->host)
  1669. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  1670. } while(rcode == -ETIMEDOUT);
  1671. if(rcode){
  1672. goto cleanup;
  1673. }
  1674. if(sg_offset) {
  1675. /* Copy back the Scatter Gather buffers back to user space */
  1676. u32 j;
  1677. // TODO add 64 bit API
  1678. struct sg_simple_element* sg;
  1679. int sg_size;
  1680. // re-acquire the original message to handle correctly the sg copy operation
  1681. memset(&msg, 0, MAX_MESSAGE_SIZE*4);
  1682. // get user msg size in u32s
  1683. if(get_user(size, &user_msg[0])){
  1684. rcode = -EFAULT;
  1685. goto cleanup;
  1686. }
  1687. size = size>>16;
  1688. size *= 4;
  1689. if (size > MAX_MESSAGE_SIZE) {
  1690. rcode = -EINVAL;
  1691. goto cleanup;
  1692. }
  1693. /* Copy in the user's I2O command */
  1694. if (copy_from_user (msg, user_msg, size)) {
  1695. rcode = -EFAULT;
  1696. goto cleanup;
  1697. }
  1698. sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
  1699. // TODO add 64 bit API
  1700. sg = (struct sg_simple_element*)(msg + sg_offset);
  1701. for (j = 0; j < sg_count; j++) {
  1702. /* Copy out the SG list to user's buffer if necessary */
  1703. if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
  1704. sg_size = sg[j].flag_count & 0xffffff;
  1705. // sg_simple_element API is 32 bit
  1706. if (copy_to_user((void __user *)(ulong)sg[j].addr_bus,sg_list[j], sg_size)) {
  1707. printk(KERN_WARNING"%s: Could not copy %p TO user %x\n",pHba->name, sg_list[j], sg[j].addr_bus);
  1708. rcode = -EFAULT;
  1709. goto cleanup;
  1710. }
  1711. }
  1712. }
  1713. }
  1714. /* Copy back the reply to user space */
  1715. if (reply_size) {
  1716. // we wrote our own values for context - now restore the user supplied ones
  1717. if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
  1718. printk(KERN_WARNING"%s: Could not copy message context FROM user\n",pHba->name);
  1719. rcode = -EFAULT;
  1720. }
  1721. if(copy_to_user(user_reply, reply, reply_size)) {
  1722. printk(KERN_WARNING"%s: Could not copy reply TO user\n",pHba->name);
  1723. rcode = -EFAULT;
  1724. }
  1725. }
  1726. cleanup:
  1727. if (rcode != -ETIME && rcode != -EINTR) {
  1728. struct sg_simple_element *sg =
  1729. (struct sg_simple_element*) (msg +sg_offset);
  1730. kfree (reply);
  1731. while(sg_index) {
  1732. if(sg_list[--sg_index]) {
  1733. dma_free_coherent(&pHba->pDev->dev,
  1734. sg[sg_index].flag_count & 0xffffff,
  1735. sg_list[sg_index],
  1736. sg[sg_index].addr_bus);
  1737. }
  1738. }
  1739. }
  1740. return rcode;
  1741. }
  1742. #if defined __ia64__
  1743. static void adpt_ia64_info(sysInfo_S* si)
  1744. {
  1745. // This is all the info we need for now
  1746. // We will add more info as our new
  1747. // managmenent utility requires it
  1748. si->processorType = PROC_IA64;
  1749. }
  1750. #endif
  1751. #if defined __sparc__
  1752. static void adpt_sparc_info(sysInfo_S* si)
  1753. {
  1754. // This is all the info we need for now
  1755. // We will add more info as our new
  1756. // managmenent utility requires it
  1757. si->processorType = PROC_ULTRASPARC;
  1758. }
  1759. #endif
  1760. #if defined __alpha__
  1761. static void adpt_alpha_info(sysInfo_S* si)
  1762. {
  1763. // This is all the info we need for now
  1764. // We will add more info as our new
  1765. // managmenent utility requires it
  1766. si->processorType = PROC_ALPHA;
  1767. }
  1768. #endif
  1769. #if defined __i386__
  1770. static void adpt_i386_info(sysInfo_S* si)
  1771. {
  1772. // This is all the info we need for now
  1773. // We will add more info as our new
  1774. // managmenent utility requires it
  1775. switch (boot_cpu_data.x86) {
  1776. case CPU_386:
  1777. si->processorType = PROC_386;
  1778. break;
  1779. case CPU_486:
  1780. si->processorType = PROC_486;
  1781. break;
  1782. case CPU_586:
  1783. si->processorType = PROC_PENTIUM;
  1784. break;
  1785. default: // Just in case
  1786. si->processorType = PROC_PENTIUM;
  1787. break;
  1788. }
  1789. }
  1790. #endif
  1791. /*
  1792. * This routine returns information about the system. This does not effect
  1793. * any logic and if the info is wrong - it doesn't matter.
  1794. */
  1795. /* Get all the info we can not get from kernel services */
  1796. static int adpt_system_info(void __user *buffer)
  1797. {
  1798. sysInfo_S si;
  1799. memset(&si, 0, sizeof(si));
  1800. si.osType = OS_LINUX;
  1801. si.osMajorVersion = 0;
  1802. si.osMinorVersion = 0;
  1803. si.osRevision = 0;
  1804. si.busType = SI_PCI_BUS;
  1805. si.processorFamily = DPTI_sig.dsProcessorFamily;
  1806. #if defined __i386__
  1807. adpt_i386_info(&si);
  1808. #elif defined (__ia64__)
  1809. adpt_ia64_info(&si);
  1810. #elif defined(__sparc__)
  1811. adpt_sparc_info(&si);
  1812. #elif defined (__alpha__)
  1813. adpt_alpha_info(&si);
  1814. #else
  1815. si.processorType = 0xff ;
  1816. #endif
  1817. if (copy_to_user(buffer, &si, sizeof(si))){
  1818. printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
  1819. return -EFAULT;
  1820. }
  1821. return 0;
  1822. }
  1823. static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
  1824. {
  1825. int minor;
  1826. int error = 0;
  1827. adpt_hba* pHba;
  1828. ulong flags = 0;
  1829. void __user *argp = (void __user *)arg;
  1830. minor = iminor(inode);
  1831. if (minor >= DPTI_MAX_HBA){
  1832. return -ENXIO;
  1833. }
  1834. mutex_lock(&adpt_configuration_lock);
  1835. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  1836. if (pHba->unit == minor) {
  1837. break; /* found adapter */
  1838. }
  1839. }
  1840. mutex_unlock(&adpt_configuration_lock);
  1841. if(pHba == NULL){
  1842. return -ENXIO;
  1843. }
  1844. while((volatile u32) pHba->state & DPTI_STATE_RESET )
  1845. schedule_timeout_uninterruptible(2);
  1846. switch (cmd) {
  1847. // TODO: handle 3 cases
  1848. case DPT_SIGNATURE:
  1849. if (copy_to_user(argp, &DPTI_sig, sizeof(DPTI_sig))) {
  1850. return -EFAULT;
  1851. }
  1852. break;
  1853. case I2OUSRCMD:
  1854. return adpt_i2o_passthru(pHba, argp);
  1855. case DPT_CTRLINFO:{
  1856. drvrHBAinfo_S HbaInfo;
  1857. #define FLG_OSD_PCI_VALID 0x0001
  1858. #define FLG_OSD_DMA 0x0002
  1859. #define FLG_OSD_I2O 0x0004
  1860. memset(&HbaInfo, 0, sizeof(HbaInfo));
  1861. HbaInfo.drvrHBAnum = pHba->unit;
  1862. HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
  1863. HbaInfo.blinkState = adpt_read_blink_led(pHba);
  1864. HbaInfo.pciBusNum = pHba->pDev->bus->number;
  1865. HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn);
  1866. HbaInfo.Interrupt = pHba->pDev->irq;
  1867. HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
  1868. if(copy_to_user(argp, &HbaInfo, sizeof(HbaInfo))){
  1869. printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
  1870. return -EFAULT;
  1871. }
  1872. break;
  1873. }
  1874. case DPT_SYSINFO:
  1875. return adpt_system_info(argp);
  1876. case DPT_BLINKLED:{
  1877. u32 value;
  1878. value = (u32)adpt_read_blink_led(pHba);
  1879. if (copy_to_user(argp, &value, sizeof(value))) {
  1880. return -EFAULT;
  1881. }
  1882. break;
  1883. }
  1884. case I2ORESETCMD:
  1885. if(pHba->host)
  1886. spin_lock_irqsave(pHba->host->host_lock, flags);
  1887. adpt_hba_reset(pHba);
  1888. if(pHba->host)
  1889. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  1890. break;
  1891. case I2ORESCANCMD:
  1892. adpt_rescan(pHba);
  1893. break;
  1894. default:
  1895. return -EINVAL;
  1896. }
  1897. return error;
  1898. }
  1899. static long adpt_unlocked_ioctl(struct file *file, uint cmd, ulong arg)
  1900. {
  1901. struct inode *inode;
  1902. long ret;
  1903. inode = file->f_dentry->d_inode;
  1904. mutex_lock(&adpt_mutex);
  1905. ret = adpt_ioctl(inode, file, cmd, arg);
  1906. mutex_unlock(&adpt_mutex);
  1907. return ret;
  1908. }
  1909. #ifdef CONFIG_COMPAT
  1910. static long compat_adpt_ioctl(struct file *file,
  1911. unsigned int cmd, unsigned long arg)
  1912. {
  1913. struct inode *inode;
  1914. long ret;
  1915. inode = file->f_dentry->d_inode;
  1916. mutex_lock(&adpt_mutex);
  1917. switch(cmd) {
  1918. case DPT_SIGNATURE:
  1919. case I2OUSRCMD:
  1920. case DPT_CTRLINFO:
  1921. case DPT_SYSINFO:
  1922. case DPT_BLINKLED:
  1923. case I2ORESETCMD:
  1924. case I2ORESCANCMD:
  1925. case (DPT_TARGET_BUSY & 0xFFFF):
  1926. case DPT_TARGET_BUSY:
  1927. ret = adpt_ioctl(inode, file, cmd, arg);
  1928. break;
  1929. default:
  1930. ret = -ENOIOCTLCMD;
  1931. }
  1932. mutex_unlock(&adpt_mutex);
  1933. return ret;
  1934. }
  1935. #endif
  1936. static irqreturn_t adpt_isr(int irq, void *dev_id)
  1937. {
  1938. struct scsi_cmnd* cmd;
  1939. adpt_hba* pHba = dev_id;
  1940. u32 m;
  1941. void __iomem *reply;
  1942. u32 status=0;
  1943. u32 context;
  1944. ulong flags = 0;
  1945. int handled = 0;
  1946. if (pHba == NULL){
  1947. printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
  1948. return IRQ_NONE;
  1949. }
  1950. if(pHba->host)
  1951. spin_lock_irqsave(pHba->host->host_lock, flags);
  1952. while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
  1953. m = readl(pHba->reply_port);
  1954. if(m == EMPTY_QUEUE){
  1955. // Try twice then give up
  1956. rmb();
  1957. m = readl(pHba->reply_port);
  1958. if(m == EMPTY_QUEUE){
  1959. // This really should not happen
  1960. printk(KERN_ERR"dpti: Could not get reply frame\n");
  1961. goto out;
  1962. }
  1963. }
  1964. if (pHba->reply_pool_pa <= m &&
  1965. m < pHba->reply_pool_pa +
  1966. (pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4)) {
  1967. reply = (u8 *)pHba->reply_pool +
  1968. (m - pHba->reply_pool_pa);
  1969. } else {
  1970. /* Ick, we should *never* be here */
  1971. printk(KERN_ERR "dpti: reply frame not from pool\n");
  1972. reply = (u8 *)bus_to_virt(m);
  1973. }
  1974. if (readl(reply) & MSG_FAIL) {
  1975. u32 old_m = readl(reply+28);
  1976. void __iomem *msg;
  1977. u32 old_context;
  1978. PDEBUG("%s: Failed message\n",pHba->name);
  1979. if(old_m >= 0x100000){
  1980. printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
  1981. writel(m,pHba->reply_port);
  1982. continue;
  1983. }
  1984. // Transaction context is 0 in failed reply frame
  1985. msg = pHba->msg_addr_virt + old_m;
  1986. old_context = readl(msg+12);
  1987. writel(old_context, reply+12);
  1988. adpt_send_nop(pHba, old_m);
  1989. }
  1990. context = readl(reply+8);
  1991. if(context & 0x40000000){ // IOCTL
  1992. void *p = adpt_ioctl_from_context(pHba, readl(reply+12));
  1993. if( p != NULL) {
  1994. memcpy_fromio(p, reply, REPLY_FRAME_SIZE * 4);
  1995. }
  1996. // All IOCTLs will also be post wait
  1997. }
  1998. if(context & 0x80000000){ // Post wait message
  1999. status = readl(reply+16);
  2000. if(status >> 24){
  2001. status &= 0xffff; /* Get detail status */
  2002. } else {
  2003. status = I2O_POST_WAIT_OK;
  2004. }
  2005. if(!(context & 0x40000000)) {
  2006. cmd = adpt_cmd_from_context(pHba,
  2007. readl(reply+12));
  2008. if(cmd != NULL) {
  2009. printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
  2010. }
  2011. }
  2012. adpt_i2o_post_wait_complete(context, status);
  2013. } else { // SCSI message
  2014. cmd = adpt_cmd_from_context (pHba, readl(reply+12));
  2015. if(cmd != NULL){
  2016. scsi_dma_unmap(cmd);
  2017. if(cmd->serial_number != 0) { // If not timedout
  2018. adpt_i2o_to_scsi(reply, cmd);
  2019. }
  2020. }
  2021. }
  2022. writel(m, pHba->reply_port);
  2023. wmb();
  2024. rmb();
  2025. }
  2026. handled = 1;
  2027. out: if(pHba->host)
  2028. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  2029. return IRQ_RETVAL(handled);
  2030. }
  2031. static s32 adpt_scsi_to_i2o(adpt_hba* pHba, struct scsi_cmnd* cmd, struct adpt_device* d)
  2032. {
  2033. int i;
  2034. u32 msg[MAX_MESSAGE_SIZE];
  2035. u32* mptr;
  2036. u32* lptr;
  2037. u32 *lenptr;
  2038. int direction;
  2039. int scsidir;
  2040. int nseg;
  2041. u32 len;
  2042. u32 reqlen;
  2043. s32 rcode;
  2044. dma_addr_t addr;
  2045. memset(msg, 0 , sizeof(msg));
  2046. len = scsi_bufflen(cmd);
  2047. direction = 0x00000000;
  2048. scsidir = 0x00000000; // DATA NO XFER
  2049. if(len) {
  2050. /*
  2051. * Set SCBFlags to indicate if data is being transferred
  2052. * in or out, or no data transfer
  2053. * Note: Do not have to verify index is less than 0 since
  2054. * cmd->cmnd[0] is an unsigned char
  2055. */
  2056. switch(cmd->sc_data_direction){
  2057. case DMA_FROM_DEVICE:
  2058. scsidir =0x40000000; // DATA IN (iop<--dev)
  2059. break;
  2060. case DMA_TO_DEVICE:
  2061. direction=0x04000000; // SGL OUT
  2062. scsidir =0x80000000; // DATA OUT (iop-->dev)
  2063. break;
  2064. case DMA_NONE:
  2065. break;
  2066. case DMA_BIDIRECTIONAL:
  2067. scsidir =0x40000000; // DATA IN (iop<--dev)
  2068. // Assume In - and continue;
  2069. break;
  2070. default:
  2071. printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
  2072. pHba->name, cmd->cmnd[0]);
  2073. cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
  2074. cmd->scsi_done(cmd);
  2075. return 0;
  2076. }
  2077. }
  2078. // msg[0] is set later
  2079. // I2O_CMD_SCSI_EXEC
  2080. msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
  2081. msg[2] = 0;
  2082. msg[3] = adpt_cmd_to_context(cmd); /* Want SCSI control block back */
  2083. // Our cards use the transaction context as the tag for queueing
  2084. // Adaptec/DPT Private stuff
  2085. msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
  2086. msg[5] = d->tid;
  2087. /* Direction, disconnect ok | sense data | simple queue , CDBLen */
  2088. // I2O_SCB_FLAG_ENABLE_DISCONNECT |
  2089. // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
  2090. // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
  2091. msg[6] = scsidir|0x20a00000|cmd->cmd_len;
  2092. mptr=msg+7;
  2093. // Write SCSI command into the message - always 16 byte block
  2094. memset(mptr, 0, 16);
  2095. memcpy(mptr, cmd->cmnd, cmd->cmd_len);
  2096. mptr+=4;
  2097. lenptr=mptr++; /* Remember me - fill in when we know */
  2098. if (dpt_dma64(pHba)) {
  2099. reqlen = 16; // SINGLE SGE
  2100. *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
  2101. *mptr++ = 1 << PAGE_SHIFT;
  2102. } else {
  2103. reqlen = 14; // SINGLE SGE
  2104. }
  2105. /* Now fill in the SGList and command */
  2106. nseg = scsi_dma_map(cmd);
  2107. BUG_ON(nseg < 0);
  2108. if (nseg) {
  2109. struct scatterlist *sg;
  2110. len = 0;
  2111. scsi_for_each_sg(cmd, sg, nseg, i) {
  2112. lptr = mptr;
  2113. *mptr++ = direction|0x10000000|sg_dma_len(sg);
  2114. len+=sg_dma_len(sg);
  2115. addr = sg_dma_address(sg);
  2116. *mptr++ = dma_low(addr);
  2117. if (dpt_dma64(pHba))
  2118. *mptr++ = dma_high(addr);
  2119. /* Make this an end of list */
  2120. if (i == nseg - 1)
  2121. *lptr = direction|0xD0000000|sg_dma_len(sg);
  2122. }
  2123. reqlen = mptr - msg;
  2124. *lenptr = len;
  2125. if(cmd->underflow && len != cmd->underflow){
  2126. printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
  2127. len, cmd->underflow);
  2128. }
  2129. } else {
  2130. *lenptr = len = 0;
  2131. reqlen = 12;
  2132. }
  2133. /* Stick the headers on */
  2134. msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
  2135. // Send it on it's way
  2136. rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
  2137. if (rcode == 0) {
  2138. return 0;
  2139. }
  2140. return rcode;
  2141. }
  2142. static s32 adpt_scsi_host_alloc(adpt_hba* pHba, struct scsi_host_template *sht)
  2143. {
  2144. struct Scsi_Host *host;
  2145. host = scsi_host_alloc(sht, sizeof(adpt_hba*));
  2146. if (host == NULL) {
  2147. printk("%s: scsi_host_alloc returned NULL\n", pHba->name);
  2148. return -1;
  2149. }
  2150. host->hostdata[0] = (unsigned long)pHba;
  2151. pHba->host = host;
  2152. host->irq = pHba->pDev->irq;
  2153. /* no IO ports, so don't have to set host->io_port and
  2154. * host->n_io_port
  2155. */
  2156. host->io_port = 0;
  2157. host->n_io_port = 0;
  2158. /* see comments in scsi_host.h */
  2159. host->max_id = 16;
  2160. host->max_lun = 256;
  2161. host->max_channel = pHba->top_scsi_channel + 1;
  2162. host->cmd_per_lun = 1;
  2163. host->unique_id = (u32)sys_tbl_pa + pHba->unit;
  2164. host->sg_tablesize = pHba->sg_tablesize;
  2165. host->can_queue = pHba->post_fifo_size;
  2166. return 0;
  2167. }
  2168. static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
  2169. {
  2170. adpt_hba* pHba;
  2171. u32 hba_status;
  2172. u32 dev_status;
  2173. u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits
  2174. // I know this would look cleaner if I just read bytes
  2175. // but the model I have been using for all the rest of the
  2176. // io is in 4 byte words - so I keep that model
  2177. u16 detailed_status = readl(reply+16) &0xffff;
  2178. dev_status = (detailed_status & 0xff);
  2179. hba_status = detailed_status >> 8;
  2180. // calculate resid for sg
  2181. scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
  2182. pHba = (adpt_hba*) cmd->device->host->hostdata[0];
  2183. cmd->sense_buffer[0] = '\0'; // initialize sense valid flag to false
  2184. if(!(reply_flags & MSG_FAIL)) {
  2185. switch(detailed_status & I2O_SCSI_DSC_MASK) {
  2186. case I2O_SCSI_DSC_SUCCESS:
  2187. cmd->result = (DID_OK << 16);
  2188. // handle underflow
  2189. if (readl(reply+20) < cmd->underflow) {
  2190. cmd->result = (DID_ERROR <<16);
  2191. printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
  2192. }
  2193. break;
  2194. case I2O_SCSI_DSC_REQUEST_ABORTED:
  2195. cmd->result = (DID_ABORT << 16);
  2196. break;
  2197. case I2O_SCSI_DSC_PATH_INVALID:
  2198. case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
  2199. case I2O_SCSI_DSC_SELECTION_TIMEOUT:
  2200. case I2O_SCSI_DSC_COMMAND_TIMEOUT:
  2201. case I2O_SCSI_DSC_NO_ADAPTER:
  2202. case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
  2203. printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%d) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
  2204. pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun, hba_status, dev_status, cmd->cmnd[0]);
  2205. cmd->result = (DID_TIME_OUT << 16);
  2206. break;
  2207. case I2O_SCSI_DSC_ADAPTER_BUSY:
  2208. case I2O_SCSI_DSC_BUS_BUSY:
  2209. cmd->result = (DID_BUS_BUSY << 16);
  2210. break;
  2211. case I2O_SCSI_DSC_SCSI_BUS_RESET:
  2212. case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
  2213. cmd->result = (DID_RESET << 16);
  2214. break;
  2215. case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
  2216. printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
  2217. cmd->result = (DID_PARITY << 16);
  2218. break;
  2219. case I2O_SCSI_DSC_UNABLE_TO_ABORT:
  2220. case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
  2221. case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
  2222. case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
  2223. case I2O_SCSI_DSC_AUTOSENSE_FAILED:
  2224. case I2O_SCSI_DSC_DATA_OVERRUN:
  2225. case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
  2226. case I2O_SCSI_DSC_SEQUENCE_FAILURE:
  2227. case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
  2228. case I2O_SCSI_DSC_PROVIDE_FAILURE:
  2229. case I2O_SCSI_DSC_REQUEST_TERMINATED:
  2230. case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
  2231. case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
  2232. case I2O_SCSI_DSC_MESSAGE_RECEIVED:
  2233. case I2O_SCSI_DSC_INVALID_CDB:
  2234. case I2O_SCSI_DSC_LUN_INVALID:
  2235. case I2O_SCSI_DSC_SCSI_TID_INVALID:
  2236. case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
  2237. case I2O_SCSI_DSC_NO_NEXUS:
  2238. case I2O_SCSI_DSC_CDB_RECEIVED:
  2239. case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
  2240. case I2O_SCSI_DSC_QUEUE_FROZEN:
  2241. case I2O_SCSI_DSC_REQUEST_INVALID:
  2242. default:
  2243. printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
  2244. pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
  2245. hba_status, dev_status, cmd->cmnd[0]);
  2246. cmd->result = (DID_ERROR << 16);
  2247. break;
  2248. }
  2249. // copy over the request sense data if it was a check
  2250. // condition status
  2251. if (dev_status == SAM_STAT_CHECK_CONDITION) {
  2252. u32 len = min(SCSI_SENSE_BUFFERSIZE, 40);
  2253. // Copy over the sense data
  2254. memcpy_fromio(cmd->sense_buffer, (reply+28) , len);
  2255. if(cmd->sense_buffer[0] == 0x70 /* class 7 */ &&
  2256. cmd->sense_buffer[2] == DATA_PROTECT ){
  2257. /* This is to handle an array failed */
  2258. cmd->result = (DID_TIME_OUT << 16);
  2259. printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
  2260. pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
  2261. hba_status, dev_status, cmd->cmnd[0]);
  2262. }
  2263. }
  2264. } else {
  2265. /* In this condtion we could not talk to the tid
  2266. * the card rejected it. We should signal a retry
  2267. * for a limitted number of retries.
  2268. */
  2269. cmd->result = (DID_TIME_OUT << 16);
  2270. printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%d) tid=%d, cmd=0x%x\n",
  2271. pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
  2272. ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
  2273. }
  2274. cmd->result |= (dev_status);
  2275. if(cmd->scsi_done != NULL){
  2276. cmd->scsi_done(cmd);
  2277. }
  2278. return cmd->result;
  2279. }
  2280. static s32 adpt_rescan(adpt_hba* pHba)
  2281. {
  2282. s32 rcode;
  2283. ulong flags = 0;
  2284. if(pHba->host)
  2285. spin_lock_irqsave(pHba->host->host_lock, flags);
  2286. if ((rcode=adpt_i2o_lct_get(pHba)) < 0)
  2287. goto out;
  2288. if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0)
  2289. goto out;
  2290. rcode = 0;
  2291. out: if(pHba->host)
  2292. spin_unlock_irqrestore(pHba->host->host_lock, flags);
  2293. return rcode;
  2294. }
  2295. static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
  2296. {
  2297. int i;
  2298. int max;
  2299. int tid;
  2300. struct i2o_device *d;
  2301. i2o_lct *lct = pHba->lct;
  2302. u8 bus_no = 0;
  2303. s16 scsi_id;
  2304. s16 scsi_lun;
  2305. u32 buf[10]; // at least 8 u32's
  2306. struct adpt_device* pDev = NULL;
  2307. struct i2o_device* pI2o_dev = NULL;
  2308. if (lct == NULL) {
  2309. printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
  2310. return -1;
  2311. }
  2312. max = lct->table_size;
  2313. max -= 3;
  2314. max /= 9;
  2315. // Mark each drive as unscanned
  2316. for (d = pHba->devices; d; d = d->next) {
  2317. pDev =(struct adpt_device*) d->owner;
  2318. if(!pDev){
  2319. continue;
  2320. }
  2321. pDev->state |= DPTI_DEV_UNSCANNED;
  2322. }
  2323. printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
  2324. for(i=0;i<max;i++) {
  2325. if( lct->lct_entry[i].user_tid != 0xfff){
  2326. continue;
  2327. }
  2328. if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
  2329. lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
  2330. lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
  2331. tid = lct->lct_entry[i].tid;
  2332. if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
  2333. printk(KERN_ERR"%s: Could not query device\n",pHba->name);
  2334. continue;
  2335. }
  2336. bus_no = buf[0]>>16;
  2337. if (bus_no >= MAX_CHANNEL) { /* Something wrong skip it */
  2338. printk(KERN_WARNING
  2339. "%s: Channel number %d out of range\n",
  2340. pHba->name, bus_no);
  2341. continue;
  2342. }
  2343. scsi_id = buf[1];
  2344. scsi_lun = (buf[2]>>8 )&0xff;
  2345. pDev = pHba->channel[bus_no].device[scsi_id];
  2346. /* da lun */
  2347. while(pDev) {
  2348. if(pDev->scsi_lun == scsi_lun) {
  2349. break;
  2350. }
  2351. pDev = pDev->next_lun;
  2352. }
  2353. if(!pDev ) { // Something new add it
  2354. d = kmalloc(sizeof(struct i2o_device),
  2355. GFP_ATOMIC);
  2356. if(d==NULL)
  2357. {
  2358. printk(KERN_CRIT "Out of memory for I2O device data.\n");
  2359. return -ENOMEM;
  2360. }
  2361. d->controller = pHba;
  2362. d->next = NULL;
  2363. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  2364. d->flags = 0;
  2365. adpt_i2o_report_hba_unit(pHba, d);
  2366. adpt_i2o_install_device(pHba, d);
  2367. pDev = pHba->channel[bus_no].device[scsi_id];
  2368. if( pDev == NULL){
  2369. pDev =
  2370. kzalloc(sizeof(struct adpt_device),
  2371. GFP_ATOMIC);
  2372. if(pDev == NULL) {
  2373. return -ENOMEM;
  2374. }
  2375. pHba->channel[bus_no].device[scsi_id] = pDev;
  2376. } else {
  2377. while (pDev->next_lun) {
  2378. pDev = pDev->next_lun;
  2379. }
  2380. pDev = pDev->next_lun =
  2381. kzalloc(sizeof(struct adpt_device),
  2382. GFP_ATOMIC);
  2383. if(pDev == NULL) {
  2384. return -ENOMEM;
  2385. }
  2386. }
  2387. pDev->tid = d->lct_data.tid;
  2388. pDev->scsi_channel = bus_no;
  2389. pDev->scsi_id = scsi_id;
  2390. pDev->scsi_lun = scsi_lun;
  2391. pDev->pI2o_dev = d;
  2392. d->owner = pDev;
  2393. pDev->type = (buf[0])&0xff;
  2394. pDev->flags = (buf[0]>>8)&0xff;
  2395. // Too late, SCSI system has made up it's mind, but what the hey ...
  2396. if(scsi_id > pHba->top_scsi_id){
  2397. pHba->top_scsi_id = scsi_id;
  2398. }
  2399. if(scsi_lun > pHba->top_scsi_lun){
  2400. pHba->top_scsi_lun = scsi_lun;
  2401. }
  2402. continue;
  2403. } // end of new i2o device
  2404. // We found an old device - check it
  2405. while(pDev) {
  2406. if(pDev->scsi_lun == scsi_lun) {
  2407. if(!scsi_device_online(pDev->pScsi_dev)) {
  2408. printk(KERN_WARNING"%s: Setting device (%d,%d,%d) back online\n",
  2409. pHba->name,bus_no,scsi_id,scsi_lun);
  2410. if (pDev->pScsi_dev) {
  2411. scsi_device_set_state(pDev->pScsi_dev, SDEV_RUNNING);
  2412. }
  2413. }
  2414. d = pDev->pI2o_dev;
  2415. if(d->lct_data.tid != tid) { // something changed
  2416. pDev->tid = tid;
  2417. memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
  2418. if (pDev->pScsi_dev) {
  2419. pDev->pScsi_dev->changed = TRUE;
  2420. pDev->pScsi_dev->removable = TRUE;
  2421. }
  2422. }
  2423. // Found it - mark it scanned
  2424. pDev->state = DPTI_DEV_ONLINE;
  2425. break;
  2426. }
  2427. pDev = pDev->next_lun;
  2428. }
  2429. }
  2430. }
  2431. for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
  2432. pDev =(struct adpt_device*) pI2o_dev->owner;
  2433. if(!pDev){
  2434. continue;
  2435. }
  2436. // Drive offline drives that previously existed but could not be found
  2437. // in the LCT table
  2438. if (pDev->state & DPTI_DEV_UNSCANNED){
  2439. pDev->state = DPTI_DEV_OFFLINE;
  2440. printk(KERN_WARNING"%s: Device (%d,%d,%d) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
  2441. if (pDev->pScsi_dev) {
  2442. scsi_device_set_state(pDev->pScsi_dev, SDEV_OFFLINE);
  2443. }
  2444. }
  2445. }
  2446. return 0;
  2447. }
  2448. static void adpt_fail_posted_scbs(adpt_hba* pHba)
  2449. {
  2450. struct scsi_cmnd* cmd = NULL;
  2451. struct scsi_device* d = NULL;
  2452. shost_for_each_device(d, pHba->host) {
  2453. unsigned long flags;
  2454. spin_lock_irqsave(&d->list_lock, flags);
  2455. list_for_each_entry(cmd, &d->cmd_list, list) {
  2456. if(cmd->serial_number == 0){
  2457. continue;
  2458. }
  2459. cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
  2460. cmd->scsi_done(cmd);
  2461. }
  2462. spin_unlock_irqrestore(&d->list_lock, flags);
  2463. }
  2464. }
  2465. /*============================================================================
  2466. * Routines from i2o subsystem
  2467. *============================================================================
  2468. */
  2469. /*
  2470. * Bring an I2O controller into HOLD state. See the spec.
  2471. */
  2472. static int adpt_i2o_activate_hba(adpt_hba* pHba)
  2473. {
  2474. int rcode;
  2475. if(pHba->initialized ) {
  2476. if (adpt_i2o_status_get(pHba) < 0) {
  2477. if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
  2478. printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
  2479. return rcode;
  2480. }
  2481. if (adpt_i2o_status_get(pHba) < 0) {
  2482. printk(KERN_INFO "HBA not responding.\n");
  2483. return -1;
  2484. }
  2485. }
  2486. if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
  2487. printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
  2488. return -1;
  2489. }
  2490. if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
  2491. pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
  2492. pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
  2493. pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
  2494. adpt_i2o_reset_hba(pHba);
  2495. if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
  2496. printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
  2497. return -1;
  2498. }
  2499. }
  2500. } else {
  2501. if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
  2502. printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
  2503. return rcode;
  2504. }
  2505. }
  2506. if (adpt_i2o_init_outbound_q(pHba) < 0) {
  2507. return -1;
  2508. }
  2509. /* In HOLD state */
  2510. if (adpt_i2o_hrt_get(pHba) < 0) {
  2511. return -1;
  2512. }
  2513. return 0;
  2514. }
  2515. /*
  2516. * Bring a controller online into OPERATIONAL state.
  2517. */
  2518. static int adpt_i2o_online_hba(adpt_hba* pHba)
  2519. {
  2520. if (adpt_i2o_systab_send(pHba) < 0) {
  2521. adpt_i2o_delete_hba(pHba);
  2522. return -1;
  2523. }
  2524. /* In READY state */
  2525. if (adpt_i2o_enable_hba(pHba) < 0) {
  2526. adpt_i2o_delete_hba(pHba);
  2527. return -1;
  2528. }
  2529. /* In OPERATIONAL state */
  2530. return 0;
  2531. }
  2532. static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
  2533. {
  2534. u32 __iomem *msg;
  2535. ulong timeout = jiffies + 5*HZ;
  2536. while(m == EMPTY_QUEUE){
  2537. rmb();
  2538. m = readl(pHba->post_port);
  2539. if(m != EMPTY_QUEUE){
  2540. break;
  2541. }
  2542. if(time_after(jiffies,timeout)){
  2543. printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
  2544. return 2;
  2545. }
  2546. schedule_timeout_uninterruptible(1);
  2547. }
  2548. msg = (u32 __iomem *)(pHba->msg_addr_virt + m);
  2549. writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
  2550. writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
  2551. writel( 0,&msg[2]);
  2552. wmb();
  2553. writel(m, pHba->post_port);
  2554. wmb();
  2555. return 0;
  2556. }
  2557. static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
  2558. {
  2559. u8 *status;
  2560. dma_addr_t addr;
  2561. u32 __iomem *msg = NULL;
  2562. int i;
  2563. ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
  2564. u32 m;
  2565. do {
  2566. rmb();
  2567. m = readl(pHba->post_port);
  2568. if (m != EMPTY_QUEUE) {
  2569. break;
  2570. }
  2571. if(time_after(jiffies,timeout)){
  2572. printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
  2573. return -ETIMEDOUT;
  2574. }
  2575. schedule_timeout_uninterruptible(1);
  2576. } while(m == EMPTY_QUEUE);
  2577. msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
  2578. status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
  2579. if (!status) {
  2580. adpt_send_nop(pHba, m);
  2581. printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
  2582. pHba->name);
  2583. return -ENOMEM;
  2584. }
  2585. memset(status, 0, 4);
  2586. writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
  2587. writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
  2588. writel(0, &msg[2]);
  2589. writel(0x0106, &msg[3]); /* Transaction context */
  2590. writel(4096, &msg[4]); /* Host page frame size */
  2591. writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]); /* Outbound msg frame size and Initcode */
  2592. writel(0xD0000004, &msg[6]); /* Simple SG LE, EOB */
  2593. writel((u32)addr, &msg[7]);
  2594. writel(m, pHba->post_port);
  2595. wmb();
  2596. // Wait for the reply status to come back
  2597. do {
  2598. if (*status) {
  2599. if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
  2600. break;
  2601. }
  2602. }
  2603. rmb();
  2604. if(time_after(jiffies,timeout)){
  2605. printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
  2606. /* We lose 4 bytes of "status" here, but we
  2607. cannot free these because controller may
  2608. awake and corrupt those bytes at any time */
  2609. /* dma_free_coherent(&pHba->pDev->dev, 4, status, addr); */
  2610. return -ETIMEDOUT;
  2611. }
  2612. schedule_timeout_uninterruptible(1);
  2613. } while (1);
  2614. // If the command was successful, fill the fifo with our reply
  2615. // message packets
  2616. if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
  2617. dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
  2618. return -2;
  2619. }
  2620. dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
  2621. if(pHba->reply_pool != NULL) {
  2622. dma_free_coherent(&pHba->pDev->dev,
  2623. pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
  2624. pHba->reply_pool, pHba->reply_pool_pa);
  2625. }
  2626. pHba->reply_pool = dma_alloc_coherent(&pHba->pDev->dev,
  2627. pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
  2628. &pHba->reply_pool_pa, GFP_KERNEL);
  2629. if (!pHba->reply_pool) {
  2630. printk(KERN_ERR "%s: Could not allocate reply pool\n", pHba->name);
  2631. return -ENOMEM;
  2632. }
  2633. memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
  2634. for(i = 0; i < pHba->reply_fifo_size; i++) {
  2635. writel(pHba->reply_pool_pa + (i * REPLY_FRAME_SIZE * 4),
  2636. pHba->reply_port);
  2637. wmb();
  2638. }
  2639. adpt_i2o_status_get(pHba);
  2640. return 0;
  2641. }
  2642. /*
  2643. * I2O System Table. Contains information about
  2644. * all the IOPs in the system. Used to inform IOPs
  2645. * about each other's existence.
  2646. *
  2647. * sys_tbl_ver is the CurrentChangeIndicator that is
  2648. * used by IOPs to track changes.
  2649. */
  2650. static s32 adpt_i2o_status_get(adpt_hba* pHba)
  2651. {
  2652. ulong timeout;
  2653. u32 m;
  2654. u32 __iomem *msg;
  2655. u8 *status_block=NULL;
  2656. if(pHba->status_block == NULL) {
  2657. pHba->status_block = dma_alloc_coherent(&pHba->pDev->dev,
  2658. sizeof(i2o_status_block),
  2659. &pHba->status_block_pa, GFP_KERNEL);
  2660. if(pHba->status_block == NULL) {
  2661. printk(KERN_ERR
  2662. "dpti%d: Get Status Block failed; Out of memory. \n",
  2663. pHba->unit);
  2664. return -ENOMEM;
  2665. }
  2666. }
  2667. memset(pHba->status_block, 0, sizeof(i2o_status_block));
  2668. status_block = (u8*)(pHba->status_block);
  2669. timeout = jiffies+TMOUT_GETSTATUS*HZ;
  2670. do {
  2671. rmb();
  2672. m = readl(pHba->post_port);
  2673. if (m != EMPTY_QUEUE) {
  2674. break;
  2675. }
  2676. if(time_after(jiffies,timeout)){
  2677. printk(KERN_ERR "%s: Timeout waiting for message !\n",
  2678. pHba->name);
  2679. return -ETIMEDOUT;
  2680. }
  2681. schedule_timeout_uninterruptible(1);
  2682. } while(m==EMPTY_QUEUE);
  2683. msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
  2684. writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
  2685. writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
  2686. writel(1, &msg[2]);
  2687. writel(0, &msg[3]);
  2688. writel(0, &msg[4]);
  2689. writel(0, &msg[5]);
  2690. writel( dma_low(pHba->status_block_pa), &msg[6]);
  2691. writel( dma_high(pHba->status_block_pa), &msg[7]);
  2692. writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
  2693. //post message
  2694. writel(m, pHba->post_port);
  2695. wmb();
  2696. while(status_block[87]!=0xff){
  2697. if(time_after(jiffies,timeout)){
  2698. printk(KERN_ERR"dpti%d: Get status timeout.\n",
  2699. pHba->unit);
  2700. return -ETIMEDOUT;
  2701. }
  2702. rmb();
  2703. schedule_timeout_uninterruptible(1);
  2704. }
  2705. // Set up our number of outbound and inbound messages
  2706. pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
  2707. if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
  2708. pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
  2709. }
  2710. pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
  2711. if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
  2712. pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
  2713. }
  2714. // Calculate the Scatter Gather list size
  2715. if (dpt_dma64(pHba)) {
  2716. pHba->sg_tablesize
  2717. = ((pHba->status_block->inbound_frame_size * 4
  2718. - 14 * sizeof(u32))
  2719. / (sizeof(struct sg_simple_element) + sizeof(u32)));
  2720. } else {
  2721. pHba->sg_tablesize
  2722. = ((pHba->status_block->inbound_frame_size * 4
  2723. - 12 * sizeof(u32))
  2724. / sizeof(struct sg_simple_element));
  2725. }
  2726. if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
  2727. pHba->sg_tablesize = SG_LIST_ELEMENTS;
  2728. }
  2729. #ifdef DEBUG
  2730. printk("dpti%d: State = ",pHba->unit);
  2731. switch(pHba->status_block->iop_state) {
  2732. case 0x01:
  2733. printk("INIT\n");
  2734. break;
  2735. case 0x02:
  2736. printk("RESET\n");
  2737. break;
  2738. case 0x04:
  2739. printk("HOLD\n");
  2740. break;
  2741. case 0x05:
  2742. printk("READY\n");
  2743. break;
  2744. case 0x08:
  2745. printk("OPERATIONAL\n");
  2746. break;
  2747. case 0x10:
  2748. printk("FAILED\n");
  2749. break;
  2750. case 0x11:
  2751. printk("FAULTED\n");
  2752. break;
  2753. default:
  2754. printk("%x (unknown!!)\n",pHba->status_block->iop_state);
  2755. }
  2756. #endif
  2757. return 0;
  2758. }
  2759. /*
  2760. * Get the IOP's Logical Configuration Table
  2761. */
  2762. static int adpt_i2o_lct_get(adpt_hba* pHba)
  2763. {
  2764. u32 msg[8];
  2765. int ret;
  2766. u32 buf[16];
  2767. if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
  2768. pHba->lct_size = pHba->status_block->expected_lct_size;
  2769. }
  2770. do {
  2771. if (pHba->lct == NULL) {
  2772. pHba->lct = dma_alloc_coherent(&pHba->pDev->dev,
  2773. pHba->lct_size, &pHba->lct_pa,
  2774. GFP_ATOMIC);
  2775. if(pHba->lct == NULL) {
  2776. printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
  2777. pHba->name);
  2778. return -ENOMEM;
  2779. }
  2780. }
  2781. memset(pHba->lct, 0, pHba->lct_size);
  2782. msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
  2783. msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
  2784. msg[2] = 0;
  2785. msg[3] = 0;
  2786. msg[4] = 0xFFFFFFFF; /* All devices */
  2787. msg[5] = 0x00000000; /* Report now */
  2788. msg[6] = 0xD0000000|pHba->lct_size;
  2789. msg[7] = (u32)pHba->lct_pa;
  2790. if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
  2791. printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n",
  2792. pHba->name, ret);
  2793. printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
  2794. return ret;
  2795. }
  2796. if ((pHba->lct->table_size << 2) > pHba->lct_size) {
  2797. pHba->lct_size = pHba->lct->table_size << 2;
  2798. dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
  2799. pHba->lct, pHba->lct_pa);
  2800. pHba->lct = NULL;
  2801. }
  2802. } while (pHba->lct == NULL);
  2803. PDEBUG("%s: Hardware resource table read.\n", pHba->name);
  2804. // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
  2805. if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
  2806. pHba->FwDebugBufferSize = buf[1];
  2807. pHba->FwDebugBuffer_P = ioremap(pHba->base_addr_phys + buf[0],
  2808. pHba->FwDebugBufferSize);
  2809. if (pHba->FwDebugBuffer_P) {
  2810. pHba->FwDebugFlags_P = pHba->FwDebugBuffer_P +
  2811. FW_DEBUG_FLAGS_OFFSET;
  2812. pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P +
  2813. FW_DEBUG_BLED_OFFSET;
  2814. pHba->FwDebugBLEDflag_P = pHba->FwDebugBLEDvalue_P + 1;
  2815. pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P +
  2816. FW_DEBUG_STR_LENGTH_OFFSET;
  2817. pHba->FwDebugBuffer_P += buf[2];
  2818. pHba->FwDebugFlags = 0;
  2819. }
  2820. }
  2821. return 0;
  2822. }
  2823. static int adpt_i2o_build_sys_table(void)
  2824. {
  2825. adpt_hba* pHba = hba_chain;
  2826. int count = 0;
  2827. if (sys_tbl)
  2828. dma_free_coherent(&pHba->pDev->dev, sys_tbl_len,
  2829. sys_tbl, sys_tbl_pa);
  2830. sys_tbl_len = sizeof(struct i2o_sys_tbl) + // Header + IOPs
  2831. (hba_count) * sizeof(struct i2o_sys_tbl_entry);
  2832. sys_tbl = dma_alloc_coherent(&pHba->pDev->dev,
  2833. sys_tbl_len, &sys_tbl_pa, GFP_KERNEL);
  2834. if (!sys_tbl) {
  2835. printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");
  2836. return -ENOMEM;
  2837. }
  2838. memset(sys_tbl, 0, sys_tbl_len);
  2839. sys_tbl->num_entries = hba_count;
  2840. sys_tbl->version = I2OVERSION;
  2841. sys_tbl->change_ind = sys_tbl_ind++;
  2842. for(pHba = hba_chain; pHba; pHba = pHba->next) {
  2843. u64 addr;
  2844. // Get updated Status Block so we have the latest information
  2845. if (adpt_i2o_status_get(pHba)) {
  2846. sys_tbl->num_entries--;
  2847. continue; // try next one
  2848. }
  2849. sys_tbl->iops[count].org_id = pHba->status_block->org_id;
  2850. sys_tbl->iops[count].iop_id = pHba->unit + 2;
  2851. sys_tbl->iops[count].seg_num = 0;
  2852. sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
  2853. sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
  2854. sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
  2855. sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
  2856. sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
  2857. sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
  2858. addr = pHba->base_addr_phys + 0x40;
  2859. sys_tbl->iops[count].inbound_low = dma_low(addr);
  2860. sys_tbl->iops[count].inbound_high = dma_high(addr);
  2861. count++;
  2862. }
  2863. #ifdef DEBUG
  2864. {
  2865. u32 *table = (u32*)sys_tbl;
  2866. printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
  2867. for(count = 0; count < (sys_tbl_len >>2); count++) {
  2868. printk(KERN_INFO "sys_tbl[%d] = %0#10x\n",
  2869. count, table[count]);
  2870. }
  2871. }
  2872. #endif
  2873. return 0;
  2874. }
  2875. /*
  2876. * Dump the information block associated with a given unit (TID)
  2877. */
  2878. static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
  2879. {
  2880. char buf[64];
  2881. int unit = d->lct_data.tid;
  2882. printk(KERN_INFO "TID %3.3d ", unit);
  2883. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
  2884. {
  2885. buf[16]=0;
  2886. printk(" Vendor: %-12.12s", buf);
  2887. }
  2888. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
  2889. {
  2890. buf[16]=0;
  2891. printk(" Device: %-12.12s", buf);
  2892. }
  2893. if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
  2894. {
  2895. buf[8]=0;
  2896. printk(" Rev: %-12.12s\n", buf);
  2897. }
  2898. #ifdef DEBUG
  2899. printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
  2900. printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
  2901. printk(KERN_INFO "\tFlags: ");
  2902. if(d->lct_data.device_flags&(1<<0))
  2903. printk("C"); // ConfigDialog requested
  2904. if(d->lct_data.device_flags&(1<<1))
  2905. printk("U"); // Multi-user capable
  2906. if(!(d->lct_data.device_flags&(1<<4)))
  2907. printk("P"); // Peer service enabled!
  2908. if(!(d->lct_data.device_flags&(1<<5)))
  2909. printk("M"); // Mgmt service enabled!
  2910. printk("\n");
  2911. #endif
  2912. }
  2913. #ifdef DEBUG
  2914. /*
  2915. * Do i2o class name lookup
  2916. */
  2917. static const char *adpt_i2o_get_class_name(int class)
  2918. {
  2919. int idx = 16;
  2920. static char *i2o_class_name[] = {
  2921. "Executive",
  2922. "Device Driver Module",
  2923. "Block Device",
  2924. "Tape Device",
  2925. "LAN Interface",
  2926. "WAN Interface",
  2927. "Fibre Channel Port",
  2928. "Fibre Channel Device",
  2929. "SCSI Device",
  2930. "ATE Port",
  2931. "ATE Device",
  2932. "Floppy Controller",
  2933. "Floppy Device",
  2934. "Secondary Bus Port",
  2935. "Peer Transport Agent",
  2936. "Peer Transport",
  2937. "Unknown"
  2938. };
  2939. switch(class&0xFFF) {
  2940. case I2O_CLASS_EXECUTIVE:
  2941. idx = 0; break;
  2942. case I2O_CLASS_DDM:
  2943. idx = 1; break;
  2944. case I2O_CLASS_RANDOM_BLOCK_STORAGE:
  2945. idx = 2; break;
  2946. case I2O_CLASS_SEQUENTIAL_STORAGE:
  2947. idx = 3; break;
  2948. case I2O_CLASS_LAN:
  2949. idx = 4; break;
  2950. case I2O_CLASS_WAN:
  2951. idx = 5; break;
  2952. case I2O_CLASS_FIBRE_CHANNEL_PORT:
  2953. idx = 6; break;
  2954. case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
  2955. idx = 7; break;
  2956. case I2O_CLASS_SCSI_PERIPHERAL:
  2957. idx = 8; break;
  2958. case I2O_CLASS_ATE_PORT:
  2959. idx = 9; break;
  2960. case I2O_CLASS_ATE_PERIPHERAL:
  2961. idx = 10; break;
  2962. case I2O_CLASS_FLOPPY_CONTROLLER:
  2963. idx = 11; break;
  2964. case I2O_CLASS_FLOPPY_DEVICE:
  2965. idx = 12; break;
  2966. case I2O_CLASS_BUS_ADAPTER_PORT:
  2967. idx = 13; break;
  2968. case I2O_CLASS_PEER_TRANSPORT_AGENT:
  2969. idx = 14; break;
  2970. case I2O_CLASS_PEER_TRANSPORT:
  2971. idx = 15; break;
  2972. }
  2973. return i2o_class_name[idx];
  2974. }
  2975. #endif
  2976. static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
  2977. {
  2978. u32 msg[6];
  2979. int ret, size = sizeof(i2o_hrt);
  2980. do {
  2981. if (pHba->hrt == NULL) {
  2982. pHba->hrt = dma_alloc_coherent(&pHba->pDev->dev,
  2983. size, &pHba->hrt_pa, GFP_KERNEL);
  2984. if (pHba->hrt == NULL) {
  2985. printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
  2986. return -ENOMEM;
  2987. }
  2988. }
  2989. msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
  2990. msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
  2991. msg[2]= 0;
  2992. msg[3]= 0;
  2993. msg[4]= (0xD0000000 | size); /* Simple transaction */
  2994. msg[5]= (u32)pHba->hrt_pa; /* Dump it here */
  2995. if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
  2996. printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
  2997. return ret;
  2998. }
  2999. if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
  3000. int newsize = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
  3001. dma_free_coherent(&pHba->pDev->dev, size,
  3002. pHba->hrt, pHba->hrt_pa);
  3003. size = newsize;
  3004. pHba->hrt = NULL;
  3005. }
  3006. } while(pHba->hrt == NULL);
  3007. return 0;
  3008. }
  3009. /*
  3010. * Query one scalar group value or a whole scalar group.
  3011. */
  3012. static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid,
  3013. int group, int field, void *buf, int buflen)
  3014. {
  3015. u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
  3016. u8 *opblk_va;
  3017. dma_addr_t opblk_pa;
  3018. u8 *resblk_va;
  3019. dma_addr_t resblk_pa;
  3020. int size;
  3021. /* 8 bytes for header */
  3022. resblk_va = dma_alloc_coherent(&pHba->pDev->dev,
  3023. sizeof(u8) * (8 + buflen), &resblk_pa, GFP_KERNEL);
  3024. if (resblk_va == NULL) {
  3025. printk(KERN_CRIT "%s: query scalar failed; Out of memory.\n", pHba->name);
  3026. return -ENOMEM;
  3027. }
  3028. opblk_va = dma_alloc_coherent(&pHba->pDev->dev,
  3029. sizeof(opblk), &opblk_pa, GFP_KERNEL);
  3030. if (opblk_va == NULL) {
  3031. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  3032. resblk_va, resblk_pa);
  3033. printk(KERN_CRIT "%s: query operatio failed; Out of memory.\n",
  3034. pHba->name);
  3035. return -ENOMEM;
  3036. }
  3037. if (field == -1) /* whole group */
  3038. opblk[4] = -1;
  3039. memcpy(opblk_va, opblk, sizeof(opblk));
  3040. size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid,
  3041. opblk_va, opblk_pa, sizeof(opblk),
  3042. resblk_va, resblk_pa, sizeof(u8)*(8+buflen));
  3043. dma_free_coherent(&pHba->pDev->dev, sizeof(opblk), opblk_va, opblk_pa);
  3044. if (size == -ETIME) {
  3045. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  3046. resblk_va, resblk_pa);
  3047. printk(KERN_WARNING "%s: issue params failed; Timed out.\n", pHba->name);
  3048. return -ETIME;
  3049. } else if (size == -EINTR) {
  3050. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  3051. resblk_va, resblk_pa);
  3052. printk(KERN_WARNING "%s: issue params failed; Interrupted.\n", pHba->name);
  3053. return -EINTR;
  3054. }
  3055. memcpy(buf, resblk_va+8, buflen); /* cut off header */
  3056. dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
  3057. resblk_va, resblk_pa);
  3058. if (size < 0)
  3059. return size;
  3060. return buflen;
  3061. }
  3062. /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
  3063. *
  3064. * This function can be used for all UtilParamsGet/Set operations.
  3065. * The OperationBlock is given in opblk-buffer,
  3066. * and results are returned in resblk-buffer.
  3067. * Note that the minimum sized resblk is 8 bytes and contains
  3068. * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
  3069. */
  3070. static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid,
  3071. void *opblk_va, dma_addr_t opblk_pa, int oplen,
  3072. void *resblk_va, dma_addr_t resblk_pa, int reslen)
  3073. {
  3074. u32 msg[9];
  3075. u32 *res = (u32 *)resblk_va;
  3076. int wait_status;
  3077. msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
  3078. msg[1] = cmd << 24 | HOST_TID << 12 | tid;
  3079. msg[2] = 0;
  3080. msg[3] = 0;
  3081. msg[4] = 0;
  3082. msg[5] = 0x54000000 | oplen; /* OperationBlock */
  3083. msg[6] = (u32)opblk_pa;
  3084. msg[7] = 0xD0000000 | reslen; /* ResultBlock */
  3085. msg[8] = (u32)resblk_pa;
  3086. if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
  3087. printk("adpt_i2o_issue_params: post_wait failed (%p)\n", resblk_va);
  3088. return wait_status; /* -DetailedStatus */
  3089. }
  3090. if (res[1]&0x00FF0000) { /* BlockStatus != SUCCESS */
  3091. printk(KERN_WARNING "%s: %s - Error:\n ErrorInfoSize = 0x%02x, "
  3092. "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
  3093. pHba->name,
  3094. (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
  3095. : "PARAMS_GET",
  3096. res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
  3097. return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
  3098. }
  3099. return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */
  3100. }
  3101. static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
  3102. {
  3103. u32 msg[4];
  3104. int ret;
  3105. adpt_i2o_status_get(pHba);
  3106. /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
  3107. if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
  3108. (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
  3109. return 0;
  3110. }
  3111. msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  3112. msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
  3113. msg[2] = 0;
  3114. msg[3] = 0;
  3115. if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
  3116. printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
  3117. pHba->unit, -ret);
  3118. } else {
  3119. printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
  3120. }
  3121. adpt_i2o_status_get(pHba);
  3122. return ret;
  3123. }
  3124. /*
  3125. * Enable IOP. Allows the IOP to resume external operations.
  3126. */
  3127. static int adpt_i2o_enable_hba(adpt_hba* pHba)
  3128. {
  3129. u32 msg[4];
  3130. int ret;
  3131. adpt_i2o_status_get(pHba);
  3132. if(!pHba->status_block){
  3133. return -ENOMEM;
  3134. }
  3135. /* Enable only allowed on READY state */
  3136. if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
  3137. return 0;
  3138. if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
  3139. return -EINVAL;
  3140. msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
  3141. msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
  3142. msg[2]= 0;
  3143. msg[3]= 0;
  3144. if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
  3145. printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n",
  3146. pHba->name, ret);
  3147. } else {
  3148. PDEBUG("%s: Enabled.\n", pHba->name);
  3149. }
  3150. adpt_i2o_status_get(pHba);
  3151. return ret;
  3152. }
  3153. static int adpt_i2o_systab_send(adpt_hba* pHba)
  3154. {
  3155. u32 msg[12];
  3156. int ret;
  3157. msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
  3158. msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
  3159. msg[2] = 0;
  3160. msg[3] = 0;
  3161. msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
  3162. msg[5] = 0; /* Segment 0 */
  3163. /*
  3164. * Provide three SGL-elements:
  3165. * System table (SysTab), Private memory space declaration and
  3166. * Private i/o space declaration
  3167. */
  3168. msg[6] = 0x54000000 | sys_tbl_len;
  3169. msg[7] = (u32)sys_tbl_pa;
  3170. msg[8] = 0x54000000 | 0;
  3171. msg[9] = 0;
  3172. msg[10] = 0xD4000000 | 0;
  3173. msg[11] = 0;
  3174. if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
  3175. printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n",
  3176. pHba->name, ret);
  3177. }
  3178. #ifdef DEBUG
  3179. else {
  3180. PINFO("%s: SysTab set.\n", pHba->name);
  3181. }
  3182. #endif
  3183. return ret;
  3184. }
  3185. /*============================================================================
  3186. *
  3187. *============================================================================
  3188. */
  3189. #ifdef UARTDELAY
  3190. static static void adpt_delay(int millisec)
  3191. {
  3192. int i;
  3193. for (i = 0; i < millisec; i++) {
  3194. udelay(1000); /* delay for one millisecond */
  3195. }
  3196. }
  3197. #endif
  3198. static struct scsi_host_template driver_template = {
  3199. .module = THIS_MODULE,
  3200. .name = "dpt_i2o",
  3201. .proc_name = "dpt_i2o",
  3202. .proc_info = adpt_proc_info,
  3203. .info = adpt_info,
  3204. .queuecommand = adpt_queue,
  3205. .eh_abort_handler = adpt_abort,
  3206. .eh_device_reset_handler = adpt_device_reset,
  3207. .eh_bus_reset_handler = adpt_bus_reset,
  3208. .eh_host_reset_handler = adpt_reset,
  3209. .bios_param = adpt_bios_param,
  3210. .slave_configure = adpt_slave_configure,
  3211. .can_queue = MAX_TO_IOP_MESSAGES,
  3212. .this_id = 7,
  3213. .cmd_per_lun = 1,
  3214. .use_clustering = ENABLE_CLUSTERING,
  3215. };
  3216. static int __init adpt_init(void)
  3217. {
  3218. int error;
  3219. adpt_hba *pHba, *next;
  3220. printk("Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
  3221. error = adpt_detect(&driver_template);
  3222. if (error < 0)
  3223. return error;
  3224. if (hba_chain == NULL)
  3225. return -ENODEV;
  3226. for (pHba = hba_chain; pHba; pHba = pHba->next) {
  3227. error = scsi_add_host(pHba->host, &pHba->pDev->dev);
  3228. if (error)
  3229. goto fail;
  3230. scsi_scan_host(pHba->host);
  3231. }
  3232. return 0;
  3233. fail:
  3234. for (pHba = hba_chain; pHba; pHba = next) {
  3235. next = pHba->next;
  3236. scsi_remove_host(pHba->host);
  3237. }
  3238. return error;
  3239. }
  3240. static void __exit adpt_exit(void)
  3241. {
  3242. adpt_hba *pHba, *next;
  3243. for (pHba = hba_chain; pHba; pHba = pHba->next)
  3244. scsi_remove_host(pHba->host);
  3245. for (pHba = hba_chain; pHba; pHba = next) {
  3246. next = pHba->next;
  3247. adpt_release(pHba->host);
  3248. }
  3249. }
  3250. module_init(adpt_init);
  3251. module_exit(adpt_exit);
  3252. MODULE_LICENSE("GPL");