dpt_i2o.c 93 KB

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