dpt_i2o.c 86 KB

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