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