dpt_i2o.c 86 KB

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