dpt_i2o.c 92 KB

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