dpt_i2o.c 86 KB

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