eata.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. /*
  2. * eata.c - Low-level driver for EATA/DMA SCSI host adapters.
  3. *
  4. * 03 Jun 2003 Rev. 8.10 for linux-2.5.70
  5. * + Update for new IRQ API.
  6. * + Use "goto" when appropriate.
  7. * + Drop eata.h.
  8. * + Update for new module_param API.
  9. * + Module parameters can now be specified only in the
  10. * same format as the kernel boot options.
  11. *
  12. * boot option old module param
  13. * ----------- ------------------
  14. * addr,... io_port=addr,...
  15. * lc:[y|n] linked_comm=[1|0]
  16. * mq:xx max_queue_depth=xx
  17. * tm:[0|1|2] tag_mode=[0|1|2]
  18. * et:[y|n] ext_tran=[1|0]
  19. * rs:[y|n] rev_scan=[1|0]
  20. * ip:[y|n] isa_probe=[1|0]
  21. * ep:[y|n] eisa_probe=[1|0]
  22. * pp:[y|n] pci_probe=[1|0]
  23. *
  24. * A valid example using the new parameter format is:
  25. * modprobe eata "eata=0x7410,0x230,lc:y,tm:0,mq:4,ep:n"
  26. *
  27. * which is equivalent to the old format:
  28. * modprobe eata io_port=0x7410,0x230 linked_comm=1 tag_mode=0 \
  29. * max_queue_depth=4 eisa_probe=0
  30. *
  31. * 12 Feb 2003 Rev. 8.04 for linux 2.5.60
  32. * + Release irq before calling scsi_register.
  33. *
  34. * 12 Nov 2002 Rev. 8.02 for linux 2.5.47
  35. * + Release driver_lock before calling scsi_register.
  36. *
  37. * 11 Nov 2002 Rev. 8.01 for linux 2.5.47
  38. * + Fixed bios_param and scsicam_bios_param calling parameters.
  39. *
  40. * 28 Oct 2002 Rev. 8.00 for linux 2.5.44-ac4
  41. * + Use new tcq and adjust_queue_depth api.
  42. * + New command line option (tm:[0-2]) to choose the type of tags:
  43. * 0 -> disable tagging ; 1 -> simple tags ; 2 -> ordered tags.
  44. * Default is tm:0 (tagged commands disabled).
  45. * For compatibility the "tc:" option is an alias of the "tm:"
  46. * option; tc:n is equivalent to tm:0 and tc:y is equivalent to
  47. * tm:1.
  48. * + The tagged_comm module parameter has been removed, use tag_mode
  49. * instead, equivalent to the "tm:" boot option.
  50. *
  51. * 10 Oct 2002 Rev. 7.70 for linux 2.5.42
  52. * + Foreport from revision 6.70.
  53. *
  54. * 25 Jun 2002 Rev. 6.70 for linux 2.4.19
  55. * + This release is the first one tested on a Big Endian platform:
  56. * fixed endian-ness problem due to bitfields;
  57. * fixed endian-ness problem in read_pio.
  58. * + Added new options for selectively probing ISA, EISA and PCI bus:
  59. *
  60. * Boot option Parameter name Default according to
  61. *
  62. * ip:[y|n] isa_probe=[1|0] CONFIG_ISA defined
  63. * ep:[y|n] eisa_probe=[1|0] CONFIG_EISA defined
  64. * pp:[y|n] pci_probe=[1|0] CONFIG_PCI defined
  65. *
  66. * The default action is to perform probing if the corresponding
  67. * bus is configured and to skip probing otherwise.
  68. *
  69. * + If pci_probe is in effect and a list of I/O ports is specified
  70. * as parameter or boot option, pci_enable_device() is performed
  71. * on all pci devices matching PCI_CLASS_STORAGE_SCSI.
  72. *
  73. * 21 Feb 2002 Rev. 6.52 for linux 2.4.18
  74. * + Backport from rev. 7.22 (use io_request_lock).
  75. *
  76. * 20 Feb 2002 Rev. 7.22 for linux 2.5.5
  77. * + Remove any reference to virt_to_bus().
  78. * + Fix pio hang while detecting multiple HBAs.
  79. * + Fixed a board detection bug: in a system with
  80. * multiple ISA/EISA boards, all but the first one
  81. * were erroneously detected as PCI.
  82. *
  83. * 01 Jan 2002 Rev. 7.20 for linux 2.5.1
  84. * + Use the dynamic DMA mapping API.
  85. *
  86. * 19 Dec 2001 Rev. 7.02 for linux 2.5.1
  87. * + Use SCpnt->sc_data_direction if set.
  88. * + Use sglist.page instead of sglist.address.
  89. *
  90. * 11 Dec 2001 Rev. 7.00 for linux 2.5.1
  91. * + Use host->host_lock instead of io_request_lock.
  92. *
  93. * 1 May 2001 Rev. 6.05 for linux 2.4.4
  94. * + Clean up all pci related routines.
  95. * + Fix data transfer direction for opcode SEND_CUE_SHEET (0x5d)
  96. *
  97. * 30 Jan 2001 Rev. 6.04 for linux 2.4.1
  98. * + Call pci_resource_start after pci_enable_device.
  99. *
  100. * 25 Jan 2001 Rev. 6.03 for linux 2.4.0
  101. * + "check_region" call replaced by "request_region".
  102. *
  103. * 22 Nov 2000 Rev. 6.02 for linux 2.4.0-test11
  104. * + Return code checked when calling pci_enable_device.
  105. * + Removed old scsi error handling support.
  106. * + The obsolete boot option flag eh:n is silently ignored.
  107. * + Removed error messages while a disk drive is powered up at
  108. * boot time.
  109. * + Improved boot messages: all tagged capable device are
  110. * indicated as "tagged" or "soft-tagged" :
  111. * - "soft-tagged" means that the driver is trying to do its
  112. * own tagging (i.e. the tc:y option is in effect);
  113. * - "tagged" means that the device supports tagged commands,
  114. * but the driver lets the HBA be responsible for tagging
  115. * support.
  116. *
  117. * 16 Sep 1999 Rev. 5.11 for linux 2.2.12 and 2.3.18
  118. * + Updated to the new __setup interface for boot command line options.
  119. * + When loaded as a module, accepts the new parameter boot_options
  120. * which value is a string with the same format of the kernel boot
  121. * command line options. A valid example is:
  122. * modprobe eata 'boot_options="0x7410,0x230,lc:y,tc:n,mq:4"'
  123. *
  124. * 9 Sep 1999 Rev. 5.10 for linux 2.2.12 and 2.3.17
  125. * + 64bit cleanup for Linux/Alpha platform support
  126. * (contribution from H.J. Lu).
  127. *
  128. * 22 Jul 1999 Rev. 5.00 for linux 2.2.10 and 2.3.11
  129. * + Removed pre-2.2 source code compatibility.
  130. * + Added call to pci_set_master.
  131. *
  132. * 26 Jul 1998 Rev. 4.33 for linux 2.0.35 and 2.1.111
  133. * + Added command line option (rs:[y|n]) to reverse the scan order
  134. * of PCI boards. The default is rs:y, which reverses the BIOS order
  135. * while registering PCI boards. The default value rs:y generates
  136. * the same order of all previous revisions of this driver.
  137. * Pls. note that "BIOS order" might have been reversed itself
  138. * after the 2.1.9x PCI modifications in the linux kernel.
  139. * The rs value is ignored when the explicit list of addresses
  140. * is used by the "eata=port0,port1,..." command line option.
  141. * + Added command line option (et:[y|n]) to force use of extended
  142. * translation (255 heads, 63 sectors) as disk geometry.
  143. * The default is et:n, which uses the disk geometry returned
  144. * by scsicam_bios_param. The default value et:n is compatible with
  145. * all previous revisions of this driver.
  146. *
  147. * 28 May 1998 Rev. 4.32 for linux 2.0.33 and 2.1.104
  148. * Increased busy timeout from 10 msec. to 200 msec. while
  149. * processing interrupts.
  150. *
  151. * 16 May 1998 Rev. 4.31 for linux 2.0.33 and 2.1.102
  152. * Improved abort handling during the eh recovery process.
  153. *
  154. * 13 May 1998 Rev. 4.30 for linux 2.0.33 and 2.1.101
  155. * The driver is now fully SMP safe, including the
  156. * abort and reset routines.
  157. * Added command line options (eh:[y|n]) to choose between
  158. * new_eh_code and the old scsi code.
  159. * If linux version >= 2.1.101 the default is eh:y, while the eh
  160. * option is ignored for previous releases and the old scsi code
  161. * is used.
  162. *
  163. * 18 Apr 1998 Rev. 4.20 for linux 2.0.33 and 2.1.97
  164. * Reworked interrupt handler.
  165. *
  166. * 11 Apr 1998 rev. 4.05 for linux 2.0.33 and 2.1.95
  167. * Major reliability improvement: when a batch with overlapping
  168. * requests is detected, requests are queued one at a time
  169. * eliminating any possible board or drive reordering.
  170. *
  171. * 10 Apr 1998 rev. 4.04 for linux 2.0.33 and 2.1.95
  172. * Improved SMP support (if linux version >= 2.1.95).
  173. *
  174. * 9 Apr 1998 rev. 4.03 for linux 2.0.33 and 2.1.94
  175. * Added support for new PCI code and IO-APIC remapping of irqs.
  176. * Performance improvement: when sequential i/o is detected,
  177. * always use direct sort instead of reverse sort.
  178. *
  179. * 4 Apr 1998 rev. 4.02 for linux 2.0.33 and 2.1.92
  180. * io_port is now unsigned long.
  181. *
  182. * 17 Mar 1998 rev. 4.01 for linux 2.0.33 and 2.1.88
  183. * Use new scsi error handling code (if linux version >= 2.1.88).
  184. * Use new interrupt code.
  185. *
  186. * 12 Sep 1997 rev. 3.11 for linux 2.0.30 and 2.1.55
  187. * Use of udelay inside the wait loops to avoid timeout
  188. * problems with fast cpus.
  189. * Removed check about useless calls to the interrupt service
  190. * routine (reported on SMP systems only).
  191. * At initialization time "sorted/unsorted" is displayed instead
  192. * of "linked/unlinked" to reinforce the fact that "linking" is
  193. * nothing but "elevator sorting" in the actual implementation.
  194. *
  195. * 17 May 1997 rev. 3.10 for linux 2.0.30 and 2.1.38
  196. * Use of serial_number_at_timeout in abort and reset processing.
  197. * Use of the __initfunc and __initdata macro in setup code.
  198. * Minor cleanups in the list_statistics code.
  199. * Increased controller busy timeout in order to better support
  200. * slow SCSI devices.
  201. *
  202. * 24 Feb 1997 rev. 3.00 for linux 2.0.29 and 2.1.26
  203. * When loading as a module, parameter passing is now supported
  204. * both in 2.0 and in 2.1 style.
  205. * Fixed data transfer direction for some SCSI opcodes.
  206. * Immediate acknowledge to request sense commands.
  207. * Linked commands to each disk device are now reordered by elevator
  208. * sorting. Rare cases in which reordering of write requests could
  209. * cause wrong results are managed.
  210. * Fixed spurious timeouts caused by long simple queue tag sequences.
  211. * New command line option (tm:[0-3]) to choose the type of tags:
  212. * 0 -> mixed (default); 1 -> simple; 2 -> head; 3 -> ordered.
  213. *
  214. * 18 Jan 1997 rev. 2.60 for linux 2.1.21 and 2.0.28
  215. * Added command line options to enable/disable linked commands
  216. * (lc:[y|n]), tagged commands (tc:[y|n]) and to set the max queue
  217. * depth (mq:xx). Default is "eata=lc:n,tc:n,mq:16".
  218. * Improved command linking.
  219. * Documented how to setup RAID-0 with DPT SmartRAID boards.
  220. *
  221. * 8 Jan 1997 rev. 2.50 for linux 2.1.20 and 2.0.27
  222. * Added linked command support.
  223. * Improved detection of PCI boards using ISA base addresses.
  224. *
  225. * 3 Dec 1996 rev. 2.40 for linux 2.1.14 and 2.0.27
  226. * Added support for tagged commands and queue depth adjustment.
  227. *
  228. * 22 Nov 1996 rev. 2.30 for linux 2.1.12 and 2.0.26
  229. * When CONFIG_PCI is defined, BIOS32 is used to include in the
  230. * list of i/o ports to be probed all the PCI SCSI controllers.
  231. * The list of i/o ports to be probed can be overwritten by the
  232. * "eata=port0,port1,...." boot command line option.
  233. * Scatter/gather lists are now allocated by a number of kmalloc
  234. * calls, in order to avoid the previous size limit of 64Kb.
  235. *
  236. * 16 Nov 1996 rev. 2.20 for linux 2.1.10 and 2.0.25
  237. * Added support for EATA 2.0C, PCI, multichannel and wide SCSI.
  238. *
  239. * 27 Sep 1996 rev. 2.12 for linux 2.1.0
  240. * Portability cleanups (virtual/bus addressing, little/big endian
  241. * support).
  242. *
  243. * 09 Jul 1996 rev. 2.11 for linux 2.0.4
  244. * Number of internal retries is now limited.
  245. *
  246. * 16 Apr 1996 rev. 2.10 for linux 1.3.90
  247. * New argument "reset_flags" to the reset routine.
  248. *
  249. * 6 Jul 1995 rev. 2.01 for linux 1.3.7
  250. * Update required by the new /proc/scsi support.
  251. *
  252. * 11 Mar 1995 rev. 2.00 for linux 1.2.0
  253. * Fixed a bug which prevented media change detection for removable
  254. * disk drives.
  255. *
  256. * 23 Feb 1995 rev. 1.18 for linux 1.1.94
  257. * Added a check for scsi_register returning NULL.
  258. *
  259. * 11 Feb 1995 rev. 1.17 for linux 1.1.91
  260. * Now DEBUG_RESET is disabled by default.
  261. * Register a board even if it does not assert DMA protocol support
  262. * (DPT SK2011B does not report correctly the dmasup bit).
  263. *
  264. * 9 Feb 1995 rev. 1.16 for linux 1.1.90
  265. * Use host->wish_block instead of host->block.
  266. * New list of Data Out SCSI commands.
  267. *
  268. * 8 Feb 1995 rev. 1.15 for linux 1.1.89
  269. * Cleared target_time_out counter while performing a reset.
  270. * All external symbols renamed to avoid possible name conflicts.
  271. *
  272. * 28 Jan 1995 rev. 1.14 for linux 1.1.86
  273. * Added module support.
  274. * Log and do a retry when a disk drive returns a target status
  275. * different from zero on a recovered error.
  276. *
  277. * 24 Jan 1995 rev. 1.13 for linux 1.1.85
  278. * Use optimized board configuration, with a measured performance
  279. * increase in the range 10%-20% on i/o throughput.
  280. *
  281. * 16 Jan 1995 rev. 1.12 for linux 1.1.81
  282. * Fix mscp structure comments (no functional change).
  283. * Display a message if check_region detects a port address
  284. * already in use.
  285. *
  286. * 17 Dec 1994 rev. 1.11 for linux 1.1.74
  287. * Use the scsicam_bios_param routine. This allows an easy
  288. * migration path from disk partition tables created using
  289. * different SCSI drivers and non optimal disk geometry.
  290. *
  291. * 15 Dec 1994 rev. 1.10 for linux 1.1.74
  292. * Added support for ISA EATA boards (DPT PM2011, DPT PM2021).
  293. * The host->block flag is set for all the detected ISA boards.
  294. * The detect routine no longer enforces LEVEL triggering
  295. * for EISA boards, it just prints a warning message.
  296. *
  297. * 30 Nov 1994 rev. 1.09 for linux 1.1.68
  298. * Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
  299. * Added optional support for using a single board at a time.
  300. *
  301. * 18 Nov 1994 rev. 1.08 for linux 1.1.64
  302. * Forces sg_tablesize = 64 and can_queue = 64 if these
  303. * values are not correctly detected (DPT PM2012).
  304. *
  305. * 14 Nov 1994 rev. 1.07 for linux 1.1.63 Final BETA release.
  306. * 04 Aug 1994 rev. 1.00 for linux 1.1.39 First BETA release.
  307. *
  308. *
  309. * This driver is based on the CAM (Common Access Method Committee)
  310. * EATA (Enhanced AT Bus Attachment) rev. 2.0A, using DMA protocol.
  311. *
  312. * Copyright (C) 1994-2003 Dario Ballabio (ballabio_dario@emc.com)
  313. *
  314. * Alternate email: dario.ballabio@inwind.it, dario.ballabio@tiscalinet.it
  315. *
  316. * Redistribution and use in source and binary forms, with or without
  317. * modification, are permitted provided that redistributions of source
  318. * code retain the above copyright notice and this comment without
  319. * modification.
  320. *
  321. */
  322. /*
  323. *
  324. * Here is a brief description of the DPT SCSI host adapters.
  325. * All these boards provide an EATA/DMA compatible programming interface
  326. * and are fully supported by this driver in any configuration, including
  327. * multiple SCSI channels:
  328. *
  329. * PM2011B/9X - Entry Level ISA
  330. * PM2021A/9X - High Performance ISA
  331. * PM2012A Old EISA
  332. * PM2012B Old EISA
  333. * PM2022A/9X - Entry Level EISA
  334. * PM2122A/9X - High Performance EISA
  335. * PM2322A/9X - Extra High Performance EISA
  336. * PM3021 - SmartRAID Adapter for ISA
  337. * PM3222 - SmartRAID Adapter for EISA (PM3222W is 16-bit wide SCSI)
  338. * PM3224 - SmartRAID Adapter for PCI (PM3224W is 16-bit wide SCSI)
  339. * PM33340UW - SmartRAID Adapter for PCI ultra wide multichannel
  340. *
  341. * The above list is just an indication: as a matter of fact all DPT
  342. * boards using the EATA/DMA protocol are supported by this driver,
  343. * since they use exactely the same programming interface.
  344. *
  345. * The DPT PM2001 provides only the EATA/PIO interface and hence is not
  346. * supported by this driver.
  347. *
  348. * This code has been tested with up to 3 Distributed Processing Technology
  349. * PM2122A/9X (DPT SCSI BIOS v002.D1, firmware v05E.0) EISA controllers,
  350. * in any combination of private and shared IRQ.
  351. * PCI support has been tested using up to 2 DPT PM3224W (DPT SCSI BIOS
  352. * v003.D0, firmware v07G.0).
  353. *
  354. * DPT SmartRAID boards support "Hardware Array" - a group of disk drives
  355. * which are all members of the same RAID-0, RAID-1 or RAID-5 array implemented
  356. * in host adapter hardware. Hardware Arrays are fully compatible with this
  357. * driver, since they look to it as a single disk drive.
  358. *
  359. * WARNING: to create a RAID-0 "Hardware Array" you must select "Other Unix"
  360. * as the current OS in the DPTMGR "Initial System Installation" menu.
  361. * Otherwise RAID-0 is generated as an "Array Group" (i.e. software RAID-0),
  362. * which is not supported by the actual SCSI subsystem.
  363. * To get the "Array Group" functionality, the Linux MD driver must be used
  364. * instead of the DPT "Array Group" feature.
  365. *
  366. * Multiple ISA, EISA and PCI boards can be configured in the same system.
  367. * It is suggested to put all the EISA boards on the same IRQ level, all
  368. * the PCI boards on another IRQ level, while ISA boards cannot share
  369. * interrupts.
  370. *
  371. * If you configure multiple boards on the same IRQ, the interrupt must
  372. * be _level_ triggered (not _edge_ triggered).
  373. *
  374. * This driver detects EATA boards by probes at fixed port addresses,
  375. * so no BIOS32 or PCI BIOS support is required.
  376. * The suggested way to detect a generic EATA PCI board is to force on it
  377. * any unused EISA address, even if there are other controllers on the EISA
  378. * bus, or even if you system has no EISA bus at all.
  379. * Do not force any ISA address on EATA PCI boards.
  380. *
  381. * If PCI bios support is configured into the kernel, BIOS32 is used to
  382. * include in the list of i/o ports to be probed all the PCI SCSI controllers.
  383. *
  384. * Due to a DPT BIOS "feature", it might not be possible to force an EISA
  385. * address on more than a single DPT PCI board, so in this case you have to
  386. * let the PCI BIOS assign the addresses.
  387. *
  388. * The sequence of detection probes is:
  389. *
  390. * - ISA 0x1F0;
  391. * - PCI SCSI controllers (only if BIOS32 is available);
  392. * - EISA/PCI 0x1C88 through 0xFC88 (corresponding to EISA slots 1 to 15);
  393. * - ISA 0x170, 0x230, 0x330.
  394. *
  395. * The above list of detection probes can be totally replaced by the
  396. * boot command line option: "eata=port0,port1,port2,...", where the
  397. * port0, port1... arguments are ISA/EISA/PCI addresses to be probed.
  398. * For example using "eata=0x7410,0x7450,0x230", the driver probes
  399. * only the two PCI addresses 0x7410 and 0x7450 and the ISA address 0x230,
  400. * in this order; "eata=0" totally disables this driver.
  401. *
  402. * After the optional list of detection probes, other possible command line
  403. * options are:
  404. *
  405. * et:y force use of extended translation (255 heads, 63 sectors);
  406. * et:n use disk geometry detected by scsicam_bios_param;
  407. * rs:y reverse scan order while detecting PCI boards;
  408. * rs:n use BIOS order while detecting PCI boards;
  409. * lc:y enables linked commands;
  410. * lc:n disables linked commands;
  411. * tm:0 disables tagged commands (same as tc:n);
  412. * tm:1 use simple queue tags (same as tc:y);
  413. * tm:2 use ordered queue tags (same as tc:2);
  414. * mq:xx set the max queue depth to the value xx (2 <= xx <= 32).
  415. *
  416. * The default value is: "eata=lc:n,mq:16,tm:0,et:n,rs:n".
  417. * An example using the list of detection probes could be:
  418. * "eata=0x7410,0x230,lc:y,tm:2,mq:4,et:n".
  419. *
  420. * When loading as a module, parameters can be specified as well.
  421. * The above example would be (use 1 in place of y and 0 in place of n):
  422. *
  423. * modprobe eata io_port=0x7410,0x230 linked_comm=1 \
  424. * max_queue_depth=4 ext_tran=0 tag_mode=2 \
  425. * rev_scan=1
  426. *
  427. * ----------------------------------------------------------------------------
  428. * In this implementation, linked commands are designed to work with any DISK
  429. * or CD-ROM, since this linking has only the intent of clustering (time-wise)
  430. * and reordering by elevator sorting commands directed to each device,
  431. * without any relation with the actual SCSI protocol between the controller
  432. * and the device.
  433. * If Q is the queue depth reported at boot time for each device (also named
  434. * cmds/lun) and Q > 2, whenever there is already an active command to the
  435. * device all other commands to the same device (up to Q-1) are kept waiting
  436. * in the elevator sorting queue. When the active command completes, the
  437. * commands in this queue are sorted by sector address. The sort is chosen
  438. * between increasing or decreasing by minimizing the seek distance between
  439. * the sector of the commands just completed and the sector of the first
  440. * command in the list to be sorted.
  441. * Trivial math assures that the unsorted average seek distance when doing
  442. * random seeks over S sectors is S/3.
  443. * When (Q-1) requests are uniformly distributed over S sectors, the average
  444. * distance between two adjacent requests is S/((Q-1) + 1), so the sorted
  445. * average seek distance for (Q-1) random requests over S sectors is S/Q.
  446. * The elevator sorting hence divides the seek distance by a factor Q/3.
  447. * The above pure geometric remarks are valid in all cases and the
  448. * driver effectively reduces the seek distance by the predicted factor
  449. * when there are Q concurrent read i/o operations on the device, but this
  450. * does not necessarily results in a noticeable performance improvement:
  451. * your mileage may vary....
  452. *
  453. * Note: command reordering inside a batch of queued commands could cause
  454. * wrong results only if there is at least one write request and the
  455. * intersection (sector-wise) of all requests is not empty.
  456. * When the driver detects a batch including overlapping requests
  457. * (a really rare event) strict serial (pid) order is enforced.
  458. * ----------------------------------------------------------------------------
  459. * The extended translation option (et:y) is useful when using large physical
  460. * disks/arrays. It could also be useful when switching between Adaptec boards
  461. * and DPT boards without reformatting the disk.
  462. * When a boot disk is partitioned with extended translation, in order to
  463. * be able to boot it with a DPT board is could be necessary to add to
  464. * lilo.conf additional commands as in the following example:
  465. *
  466. * fix-table
  467. * disk=/dev/sda bios=0x80 sectors=63 heads=128 cylindres=546
  468. *
  469. * where the above geometry should be replaced with the one reported at
  470. * power up by the DPT controller.
  471. * ----------------------------------------------------------------------------
  472. *
  473. * The boards are named EATA0, EATA1,... according to the detection order.
  474. *
  475. * In order to support multiple ISA boards in a reliable way,
  476. * the driver sets host->wish_block = 1 for all ISA boards.
  477. */
  478. #include <linux/string.h>
  479. #include <linux/kernel.h>
  480. #include <linux/ioport.h>
  481. #include <linux/delay.h>
  482. #include <linux/proc_fs.h>
  483. #include <linux/blkdev.h>
  484. #include <linux/interrupt.h>
  485. #include <linux/stat.h>
  486. #include <linux/pci.h>
  487. #include <linux/init.h>
  488. #include <linux/ctype.h>
  489. #include <linux/spinlock.h>
  490. #include <linux/dma-mapping.h>
  491. #include <linux/slab.h>
  492. #include <asm/byteorder.h>
  493. #include <asm/dma.h>
  494. #include <asm/io.h>
  495. #include <asm/irq.h>
  496. #include <scsi/scsi.h>
  497. #include <scsi/scsi_cmnd.h>
  498. #include <scsi/scsi_device.h>
  499. #include <scsi/scsi_host.h>
  500. #include <scsi/scsi_tcq.h>
  501. #include <scsi/scsicam.h>
  502. static int eata2x_detect(struct scsi_host_template *);
  503. static int eata2x_release(struct Scsi_Host *);
  504. static int eata2x_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
  505. static int eata2x_eh_abort(struct scsi_cmnd *);
  506. static int eata2x_eh_host_reset(struct scsi_cmnd *);
  507. static int eata2x_bios_param(struct scsi_device *, struct block_device *,
  508. sector_t, int *);
  509. static int eata2x_slave_configure(struct scsi_device *);
  510. static struct scsi_host_template driver_template = {
  511. .name = "EATA/DMA 2.0x rev. 8.10.00 ",
  512. .detect = eata2x_detect,
  513. .release = eata2x_release,
  514. .queuecommand = eata2x_queuecommand,
  515. .eh_abort_handler = eata2x_eh_abort,
  516. .eh_host_reset_handler = eata2x_eh_host_reset,
  517. .bios_param = eata2x_bios_param,
  518. .slave_configure = eata2x_slave_configure,
  519. .this_id = 7,
  520. .unchecked_isa_dma = 1,
  521. .use_clustering = ENABLE_CLUSTERING,
  522. };
  523. #if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
  524. #error "Adjust your <asm/byteorder.h> defines"
  525. #endif
  526. /* Subversion values */
  527. #define ISA 0
  528. #define ESA 1
  529. #undef FORCE_CONFIG
  530. #undef DEBUG_LINKED_COMMANDS
  531. #undef DEBUG_DETECT
  532. #undef DEBUG_PCI_DETECT
  533. #undef DEBUG_INTERRUPT
  534. #undef DEBUG_RESET
  535. #undef DEBUG_GENERATE_ERRORS
  536. #undef DEBUG_GENERATE_ABORTS
  537. #undef DEBUG_GEOMETRY
  538. #define MAX_ISA 4
  539. #define MAX_VESA 0
  540. #define MAX_EISA 15
  541. #define MAX_PCI 16
  542. #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
  543. #define MAX_CHANNEL 4
  544. #define MAX_LUN 32
  545. #define MAX_TARGET 32
  546. #define MAX_MAILBOXES 64
  547. #define MAX_SGLIST 64
  548. #define MAX_LARGE_SGLIST 122
  549. #define MAX_INTERNAL_RETRIES 64
  550. #define MAX_CMD_PER_LUN 2
  551. #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
  552. #define SKIP ULONG_MAX
  553. #define FREE 0
  554. #define IN_USE 1
  555. #define LOCKED 2
  556. #define IN_RESET 3
  557. #define IGNORE 4
  558. #define READY 5
  559. #define ABORTING 6
  560. #define NO_DMA 0xff
  561. #define MAXLOOP 10000
  562. #define TAG_DISABLED 0
  563. #define TAG_SIMPLE 1
  564. #define TAG_ORDERED 2
  565. #define REG_CMD 7
  566. #define REG_STATUS 7
  567. #define REG_AUX_STATUS 8
  568. #define REG_DATA 0
  569. #define REG_DATA2 1
  570. #define REG_SEE 6
  571. #define REG_LOW 2
  572. #define REG_LM 3
  573. #define REG_MID 4
  574. #define REG_MSB 5
  575. #define REGION_SIZE 9UL
  576. #define MAX_ISA_ADDR 0x03ff
  577. #define MIN_EISA_ADDR 0x1c88
  578. #define MAX_EISA_ADDR 0xfc88
  579. #define BSY_ASSERTED 0x80
  580. #define DRQ_ASSERTED 0x08
  581. #define ABSY_ASSERTED 0x01
  582. #define IRQ_ASSERTED 0x02
  583. #define READ_CONFIG_PIO 0xf0
  584. #define SET_CONFIG_PIO 0xf1
  585. #define SEND_CP_PIO 0xf2
  586. #define RECEIVE_SP_PIO 0xf3
  587. #define TRUNCATE_XFR_PIO 0xf4
  588. #define RESET_PIO 0xf9
  589. #define READ_CONFIG_DMA 0xfd
  590. #define SET_CONFIG_DMA 0xfe
  591. #define SEND_CP_DMA 0xff
  592. #define ASOK 0x00
  593. #define ASST 0x01
  594. #define YESNO(a) ((a) ? 'y' : 'n')
  595. #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
  596. /* "EATA", in Big Endian format */
  597. #define EATA_SIG_BE 0x45415441
  598. /* Number of valid bytes in the board config structure for EATA 2.0x */
  599. #define EATA_2_0A_SIZE 28
  600. #define EATA_2_0B_SIZE 30
  601. #define EATA_2_0C_SIZE 34
  602. /* Board info structure */
  603. struct eata_info {
  604. u_int32_t data_len; /* Number of valid bytes after this field */
  605. u_int32_t sign; /* ASCII "EATA" signature */
  606. #if defined(__BIG_ENDIAN_BITFIELD)
  607. unchar version : 4,
  608. : 4;
  609. unchar haaval : 1,
  610. ata : 1,
  611. drqvld : 1,
  612. dmasup : 1,
  613. morsup : 1,
  614. trnxfr : 1,
  615. tarsup : 1,
  616. ocsena : 1;
  617. #else
  618. unchar : 4, /* unused low nibble */
  619. version : 4; /* EATA version, should be 0x1 */
  620. unchar ocsena : 1, /* Overlap Command Support Enabled */
  621. tarsup : 1, /* Target Mode Supported */
  622. trnxfr : 1, /* Truncate Transfer Cmd NOT Necessary */
  623. morsup : 1, /* More Supported */
  624. dmasup : 1, /* DMA Supported */
  625. drqvld : 1, /* DRQ Index (DRQX) is valid */
  626. ata : 1, /* This is an ATA device */
  627. haaval : 1; /* Host Adapter Address Valid */
  628. #endif
  629. ushort cp_pad_len; /* Number of pad bytes after cp_len */
  630. unchar host_addr[4]; /* Host Adapter SCSI ID for channels 3, 2, 1, 0 */
  631. u_int32_t cp_len; /* Number of valid bytes in cp */
  632. u_int32_t sp_len; /* Number of valid bytes in sp */
  633. ushort queue_size; /* Max number of cp that can be queued */
  634. ushort unused;
  635. ushort scatt_size; /* Max number of entries in scatter/gather table */
  636. #if defined(__BIG_ENDIAN_BITFIELD)
  637. unchar drqx : 2,
  638. second : 1,
  639. irq_tr : 1,
  640. irq : 4;
  641. unchar sync;
  642. unchar : 4,
  643. res1 : 1,
  644. large_sg : 1,
  645. forcaddr : 1,
  646. isaena : 1;
  647. unchar max_chan : 3,
  648. max_id : 5;
  649. unchar max_lun;
  650. unchar eisa : 1,
  651. pci : 1,
  652. idquest : 1,
  653. m1 : 1,
  654. : 4;
  655. #else
  656. unchar irq : 4, /* Interrupt Request assigned to this controller */
  657. irq_tr : 1, /* 0 for edge triggered, 1 for level triggered */
  658. second : 1, /* 1 if this is a secondary (not primary) controller */
  659. drqx : 2; /* DRQ Index (0=DMA0, 1=DMA7, 2=DMA6, 3=DMA5) */
  660. unchar sync; /* 1 if scsi target id 7...0 is running sync scsi */
  661. /* Structure extension defined in EATA 2.0B */
  662. unchar isaena : 1, /* ISA i/o addressing is disabled/enabled */
  663. forcaddr : 1, /* Port address has been forced */
  664. large_sg : 1, /* 1 if large SG lists are supported */
  665. res1 : 1,
  666. : 4;
  667. unchar max_id : 5, /* Max SCSI target ID number */
  668. max_chan : 3; /* Max SCSI channel number on this board */
  669. /* Structure extension defined in EATA 2.0C */
  670. unchar max_lun; /* Max SCSI LUN number */
  671. unchar
  672. : 4,
  673. m1 : 1, /* This is a PCI with an M1 chip installed */
  674. idquest : 1, /* RAIDNUM returned is questionable */
  675. pci : 1, /* This board is PCI */
  676. eisa : 1; /* This board is EISA */
  677. #endif
  678. unchar raidnum; /* Uniquely identifies this HBA in a system */
  679. unchar notused;
  680. ushort ipad[247];
  681. };
  682. /* Board config structure */
  683. struct eata_config {
  684. ushort len; /* Number of bytes following this field */
  685. #if defined(__BIG_ENDIAN_BITFIELD)
  686. unchar : 4,
  687. tarena : 1,
  688. mdpena : 1,
  689. ocena : 1,
  690. edis : 1;
  691. #else
  692. unchar edis : 1, /* Disable EATA interface after config command */
  693. ocena : 1, /* Overlapped Commands Enabled */
  694. mdpena : 1, /* Transfer all Modified Data Pointer Messages */
  695. tarena : 1, /* Target Mode Enabled for this controller */
  696. : 4;
  697. #endif
  698. unchar cpad[511];
  699. };
  700. /* Returned status packet structure */
  701. struct mssp {
  702. #if defined(__BIG_ENDIAN_BITFIELD)
  703. unchar eoc : 1,
  704. adapter_status : 7;
  705. #else
  706. unchar adapter_status : 7, /* State related to current command */
  707. eoc : 1; /* End Of Command (1 = command completed) */
  708. #endif
  709. unchar target_status; /* SCSI status received after data transfer */
  710. unchar unused[2];
  711. u_int32_t inv_res_len; /* Number of bytes not transferred */
  712. u_int32_t cpp_index; /* Index of address set in cp */
  713. char mess[12];
  714. };
  715. struct sg_list {
  716. unsigned int address; /* Segment Address */
  717. unsigned int num_bytes; /* Segment Length */
  718. };
  719. /* MailBox SCSI Command Packet */
  720. struct mscp {
  721. #if defined(__BIG_ENDIAN_BITFIELD)
  722. unchar din : 1,
  723. dout : 1,
  724. interp : 1,
  725. : 1,
  726. sg : 1,
  727. reqsen :1,
  728. init : 1,
  729. sreset : 1;
  730. unchar sense_len;
  731. unchar unused[3];
  732. unchar : 7,
  733. fwnest : 1;
  734. unchar : 5,
  735. hbaci : 1,
  736. iat : 1,
  737. phsunit : 1;
  738. unchar channel : 3,
  739. target : 5;
  740. unchar one : 1,
  741. dispri : 1,
  742. luntar : 1,
  743. lun : 5;
  744. #else
  745. unchar sreset :1, /* SCSI Bus Reset Signal should be asserted */
  746. init :1, /* Re-initialize controller and self test */
  747. reqsen :1, /* Transfer Request Sense Data to addr using DMA */
  748. sg :1, /* Use Scatter/Gather */
  749. :1,
  750. interp :1, /* The controller interprets cp, not the target */
  751. dout :1, /* Direction of Transfer is Out (Host to Target) */
  752. din :1; /* Direction of Transfer is In (Target to Host) */
  753. unchar sense_len; /* Request Sense Length */
  754. unchar unused[3];
  755. unchar fwnest : 1, /* Send command to a component of an Array Group */
  756. : 7;
  757. unchar phsunit : 1, /* Send to Target Physical Unit (bypass RAID) */
  758. iat : 1, /* Inhibit Address Translation */
  759. hbaci : 1, /* Inhibit HBA Caching for this command */
  760. : 5;
  761. unchar target : 5, /* SCSI target ID */
  762. channel : 3; /* SCSI channel number */
  763. unchar lun : 5, /* SCSI logical unit number */
  764. luntar : 1, /* This cp is for Target (not LUN) */
  765. dispri : 1, /* Disconnect Privilege granted */
  766. one : 1; /* 1 */
  767. #endif
  768. unchar mess[3]; /* Massage to/from Target */
  769. unchar cdb[12]; /* Command Descriptor Block */
  770. u_int32_t data_len; /* If sg=0 Data Length, if sg=1 sglist length */
  771. u_int32_t cpp_index; /* Index of address to be returned in sp */
  772. u_int32_t data_address; /* If sg=0 Data Address, if sg=1 sglist address */
  773. u_int32_t sp_dma_addr; /* Address where sp is DMA'ed when cp completes */
  774. u_int32_t sense_addr; /* Address where Sense Data is DMA'ed on error */
  775. /* Additional fields begin here. */
  776. struct scsi_cmnd *SCpnt;
  777. /* All the cp structure is zero filled by queuecommand except the
  778. following CP_TAIL_SIZE bytes, initialized by detect */
  779. dma_addr_t cp_dma_addr; /* dma handle for this cp structure */
  780. struct sg_list *sglist; /* pointer to the allocated SG list */
  781. };
  782. #define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
  783. struct hostdata {
  784. struct mscp cp[MAX_MAILBOXES]; /* Mailboxes for this board */
  785. unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
  786. unsigned int last_cp_used; /* Index of last mailbox used */
  787. unsigned int iocount; /* Total i/o done for this board */
  788. int board_number; /* Number of this board */
  789. char board_name[16]; /* Name of this board */
  790. int in_reset; /* True if board is doing a reset */
  791. int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
  792. int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If 1 redo i/o on target */
  793. unsigned int retries; /* Number of internal retries */
  794. unsigned long last_retried_pid; /* Pid of last retried command */
  795. unsigned char subversion; /* Bus type, either ISA or EISA/PCI */
  796. unsigned char protocol_rev; /* EATA 2.0 rev., 'A' or 'B' or 'C' */
  797. unsigned char is_pci; /* 1 is bus type is PCI */
  798. struct pci_dev *pdev; /* pdev for PCI bus, NULL otherwise */
  799. struct mssp *sp_cpu_addr; /* cpu addr for DMA buffer sp */
  800. dma_addr_t sp_dma_addr; /* dma handle for DMA buffer sp */
  801. struct mssp sp; /* Local copy of sp buffer */
  802. };
  803. static struct Scsi_Host *sh[MAX_BOARDS];
  804. static const char *driver_name = "EATA";
  805. static char sha[MAX_BOARDS];
  806. /* Initialize num_boards so that ihdlr can work while detect is in progress */
  807. static unsigned int num_boards = MAX_BOARDS;
  808. static unsigned long io_port[] = {
  809. /* Space for MAX_INT_PARAM ports usable while loading as a module */
  810. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  811. SKIP, SKIP,
  812. /* First ISA */
  813. 0x1f0,
  814. /* Space for MAX_PCI ports possibly reported by PCI_BIOS */
  815. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  816. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  817. /* MAX_EISA ports */
  818. 0x1c88, 0x2c88, 0x3c88, 0x4c88, 0x5c88, 0x6c88, 0x7c88, 0x8c88,
  819. 0x9c88, 0xac88, 0xbc88, 0xcc88, 0xdc88, 0xec88, 0xfc88,
  820. /* Other (MAX_ISA - 1) ports */
  821. 0x170, 0x230, 0x330,
  822. /* End of list */
  823. 0x0
  824. };
  825. /* Device is Big Endian */
  826. #define H2DEV(x) cpu_to_be32(x)
  827. #define DEV2H(x) be32_to_cpu(x)
  828. #define H2DEV16(x) cpu_to_be16(x)
  829. #define DEV2H16(x) be16_to_cpu(x)
  830. /* But transfer orientation from the 16 bit data register is Little Endian */
  831. #define REG2H(x) le16_to_cpu(x)
  832. static irqreturn_t do_interrupt_handler(int, void *);
  833. static void flush_dev(struct scsi_device *, unsigned long, struct hostdata *,
  834. unsigned int);
  835. static int do_trace = 0;
  836. static int setup_done = 0;
  837. static int link_statistics;
  838. static int ext_tran = 0;
  839. static int rev_scan = 1;
  840. #if defined(CONFIG_SCSI_EATA_TAGGED_QUEUE)
  841. static int tag_mode = TAG_SIMPLE;
  842. #else
  843. static int tag_mode = TAG_DISABLED;
  844. #endif
  845. #if defined(CONFIG_SCSI_EATA_LINKED_COMMANDS)
  846. static int linked_comm = 1;
  847. #else
  848. static int linked_comm = 0;
  849. #endif
  850. #if defined(CONFIG_SCSI_EATA_MAX_TAGS)
  851. static int max_queue_depth = CONFIG_SCSI_EATA_MAX_TAGS;
  852. #else
  853. static int max_queue_depth = MAX_CMD_PER_LUN;
  854. #endif
  855. #if defined(CONFIG_ISA)
  856. static int isa_probe = 1;
  857. #else
  858. static int isa_probe = 0;
  859. #endif
  860. #if defined(CONFIG_EISA)
  861. static int eisa_probe = 1;
  862. #else
  863. static int eisa_probe = 0;
  864. #endif
  865. #if defined(CONFIG_PCI)
  866. static int pci_probe = 1;
  867. #else
  868. static int pci_probe = 0;
  869. #endif
  870. #define MAX_INT_PARAM 10
  871. #define MAX_BOOT_OPTIONS_SIZE 256
  872. static char boot_options[MAX_BOOT_OPTIONS_SIZE];
  873. #if defined(MODULE)
  874. #include <linux/module.h>
  875. #include <linux/moduleparam.h>
  876. module_param_string(eata, boot_options, MAX_BOOT_OPTIONS_SIZE, 0);
  877. MODULE_PARM_DESC(eata, " equivalent to the \"eata=...\" kernel boot option."
  878. " Example: modprobe eata \"eata=0x7410,0x230,lc:y,tm:0,mq:4,ep:n\"");
  879. MODULE_AUTHOR("Dario Ballabio");
  880. MODULE_LICENSE("GPL");
  881. MODULE_DESCRIPTION("EATA/DMA SCSI Driver");
  882. #endif
  883. static int eata2x_slave_configure(struct scsi_device *dev)
  884. {
  885. int tqd, utqd;
  886. char *tag_suffix, *link_suffix;
  887. utqd = MAX_CMD_PER_LUN;
  888. tqd = max_queue_depth;
  889. if (TLDEV(dev->type) && dev->tagged_supported) {
  890. if (tag_mode == TAG_SIMPLE) {
  891. tag_suffix = ", simple tags";
  892. } else if (tag_mode == TAG_ORDERED) {
  893. tag_suffix = ", ordered tags";
  894. } else {
  895. tag_suffix = ", no tags";
  896. }
  897. scsi_change_queue_depth(dev, tqd);
  898. } else if (TLDEV(dev->type) && linked_comm) {
  899. scsi_change_queue_depth(dev, tqd);
  900. tag_suffix = ", untagged";
  901. } else {
  902. scsi_change_queue_depth(dev, utqd);
  903. tag_suffix = "";
  904. }
  905. if (TLDEV(dev->type) && linked_comm && dev->queue_depth > 2)
  906. link_suffix = ", sorted";
  907. else if (TLDEV(dev->type))
  908. link_suffix = ", unsorted";
  909. else
  910. link_suffix = "";
  911. sdev_printk(KERN_INFO, dev,
  912. "cmds/lun %d%s%s.\n",
  913. dev->queue_depth, link_suffix, tag_suffix);
  914. return 0;
  915. }
  916. static int wait_on_busy(unsigned long iobase, unsigned int loop)
  917. {
  918. while (inb(iobase + REG_AUX_STATUS) & ABSY_ASSERTED) {
  919. udelay(1L);
  920. if (--loop == 0)
  921. return 1;
  922. }
  923. return 0;
  924. }
  925. static int do_dma(unsigned long iobase, unsigned long addr, unchar cmd)
  926. {
  927. unsigned char *byaddr;
  928. unsigned long devaddr;
  929. if (wait_on_busy(iobase, (addr ? MAXLOOP * 100 : MAXLOOP)))
  930. return 1;
  931. if (addr) {
  932. devaddr = H2DEV(addr);
  933. byaddr = (unsigned char *)&devaddr;
  934. outb(byaddr[3], iobase + REG_LOW);
  935. outb(byaddr[2], iobase + REG_LM);
  936. outb(byaddr[1], iobase + REG_MID);
  937. outb(byaddr[0], iobase + REG_MSB);
  938. }
  939. outb(cmd, iobase + REG_CMD);
  940. return 0;
  941. }
  942. static int read_pio(unsigned long iobase, ushort * start, ushort * end)
  943. {
  944. unsigned int loop = MAXLOOP;
  945. ushort *p;
  946. for (p = start; p <= end; p++) {
  947. while (!(inb(iobase + REG_STATUS) & DRQ_ASSERTED)) {
  948. udelay(1L);
  949. if (--loop == 0)
  950. return 1;
  951. }
  952. loop = MAXLOOP;
  953. *p = REG2H(inw(iobase));
  954. }
  955. return 0;
  956. }
  957. static struct pci_dev *get_pci_dev(unsigned long port_base)
  958. {
  959. #if defined(CONFIG_PCI)
  960. unsigned int addr;
  961. struct pci_dev *dev = NULL;
  962. while ((dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
  963. addr = pci_resource_start(dev, 0);
  964. #if defined(DEBUG_PCI_DETECT)
  965. printk("%s: get_pci_dev, bus %d, devfn 0x%x, addr 0x%x.\n",
  966. driver_name, dev->bus->number, dev->devfn, addr);
  967. #endif
  968. /* we are in so much trouble for a pci hotplug system with this driver
  969. * anyway, so doing this at least lets people unload the driver and not
  970. * cause memory problems, but in general this is a bad thing to do (this
  971. * driver needs to be converted to the proper PCI api someday... */
  972. pci_dev_put(dev);
  973. if (addr + PCI_BASE_ADDRESS_0 == port_base)
  974. return dev;
  975. }
  976. #endif /* end CONFIG_PCI */
  977. return NULL;
  978. }
  979. static void enable_pci_ports(void)
  980. {
  981. #if defined(CONFIG_PCI)
  982. struct pci_dev *dev = NULL;
  983. while ((dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
  984. #if defined(DEBUG_PCI_DETECT)
  985. printk("%s: enable_pci_ports, bus %d, devfn 0x%x.\n",
  986. driver_name, dev->bus->number, dev->devfn);
  987. #endif
  988. if (pci_enable_device(dev))
  989. printk
  990. ("%s: warning, pci_enable_device failed, bus %d devfn 0x%x.\n",
  991. driver_name, dev->bus->number, dev->devfn);
  992. }
  993. #endif /* end CONFIG_PCI */
  994. }
  995. static int port_detect(unsigned long port_base, unsigned int j,
  996. struct scsi_host_template *tpnt)
  997. {
  998. unsigned char irq, dma_channel, subversion, i, is_pci = 0;
  999. unsigned char protocol_rev;
  1000. struct eata_info info;
  1001. char *bus_type, dma_name[16];
  1002. struct pci_dev *pdev;
  1003. /* Allowed DMA channels for ISA (0 indicates reserved) */
  1004. unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
  1005. struct Scsi_Host *shost;
  1006. struct hostdata *ha;
  1007. char name[16];
  1008. sprintf(name, "%s%d", driver_name, j);
  1009. if (!request_region(port_base, REGION_SIZE, driver_name)) {
  1010. #if defined(DEBUG_DETECT)
  1011. printk("%s: address 0x%03lx in use, skipping probe.\n", name,
  1012. port_base);
  1013. #endif
  1014. goto fail;
  1015. }
  1016. if (do_dma(port_base, 0, READ_CONFIG_PIO)) {
  1017. #if defined(DEBUG_DETECT)
  1018. printk("%s: detect, do_dma failed at 0x%03lx.\n", name,
  1019. port_base);
  1020. #endif
  1021. goto freelock;
  1022. }
  1023. /* Read the info structure */
  1024. if (read_pio(port_base, (ushort *) & info, (ushort *) & info.ipad[0])) {
  1025. #if defined(DEBUG_DETECT)
  1026. printk("%s: detect, read_pio failed at 0x%03lx.\n", name,
  1027. port_base);
  1028. #endif
  1029. goto freelock;
  1030. }
  1031. info.data_len = DEV2H(info.data_len);
  1032. info.sign = DEV2H(info.sign);
  1033. info.cp_pad_len = DEV2H16(info.cp_pad_len);
  1034. info.cp_len = DEV2H(info.cp_len);
  1035. info.sp_len = DEV2H(info.sp_len);
  1036. info.scatt_size = DEV2H16(info.scatt_size);
  1037. info.queue_size = DEV2H16(info.queue_size);
  1038. /* Check the controller "EATA" signature */
  1039. if (info.sign != EATA_SIG_BE) {
  1040. #if defined(DEBUG_DETECT)
  1041. printk("%s: signature 0x%04x discarded.\n", name, info.sign);
  1042. #endif
  1043. goto freelock;
  1044. }
  1045. if (info.data_len < EATA_2_0A_SIZE) {
  1046. printk
  1047. ("%s: config structure size (%d bytes) too short, detaching.\n",
  1048. name, info.data_len);
  1049. goto freelock;
  1050. } else if (info.data_len == EATA_2_0A_SIZE)
  1051. protocol_rev = 'A';
  1052. else if (info.data_len == EATA_2_0B_SIZE)
  1053. protocol_rev = 'B';
  1054. else
  1055. protocol_rev = 'C';
  1056. if (protocol_rev != 'A' && info.forcaddr) {
  1057. printk("%s: warning, port address has been forced.\n", name);
  1058. bus_type = "PCI";
  1059. is_pci = 1;
  1060. subversion = ESA;
  1061. } else if (port_base > MAX_EISA_ADDR
  1062. || (protocol_rev == 'C' && info.pci)) {
  1063. bus_type = "PCI";
  1064. is_pci = 1;
  1065. subversion = ESA;
  1066. } else if (port_base >= MIN_EISA_ADDR
  1067. || (protocol_rev == 'C' && info.eisa)) {
  1068. bus_type = "EISA";
  1069. subversion = ESA;
  1070. } else if (protocol_rev == 'C' && !info.eisa && !info.pci) {
  1071. bus_type = "ISA";
  1072. subversion = ISA;
  1073. } else if (port_base > MAX_ISA_ADDR) {
  1074. bus_type = "PCI";
  1075. is_pci = 1;
  1076. subversion = ESA;
  1077. } else {
  1078. bus_type = "ISA";
  1079. subversion = ISA;
  1080. }
  1081. if (!info.haaval || info.ata) {
  1082. printk
  1083. ("%s: address 0x%03lx, unusable %s board (%d%d), detaching.\n",
  1084. name, port_base, bus_type, info.haaval, info.ata);
  1085. goto freelock;
  1086. }
  1087. if (info.drqvld) {
  1088. if (subversion == ESA)
  1089. printk("%s: warning, weird %s board using DMA.\n", name,
  1090. bus_type);
  1091. subversion = ISA;
  1092. dma_channel = dma_channel_table[3 - info.drqx];
  1093. } else {
  1094. if (subversion == ISA)
  1095. printk("%s: warning, weird %s board not using DMA.\n",
  1096. name, bus_type);
  1097. subversion = ESA;
  1098. dma_channel = NO_DMA;
  1099. }
  1100. if (!info.dmasup)
  1101. printk("%s: warning, DMA protocol support not asserted.\n",
  1102. name);
  1103. irq = info.irq;
  1104. if (subversion == ESA && !info.irq_tr)
  1105. printk
  1106. ("%s: warning, LEVEL triggering is suggested for IRQ %u.\n",
  1107. name, irq);
  1108. if (is_pci) {
  1109. pdev = get_pci_dev(port_base);
  1110. if (!pdev)
  1111. printk
  1112. ("%s: warning, failed to get pci_dev structure.\n",
  1113. name);
  1114. } else
  1115. pdev = NULL;
  1116. if (pdev && (irq != pdev->irq)) {
  1117. printk("%s: IRQ %u mapped to IO-APIC IRQ %u.\n", name, irq,
  1118. pdev->irq);
  1119. irq = pdev->irq;
  1120. }
  1121. /* Board detected, allocate its IRQ */
  1122. if (request_irq(irq, do_interrupt_handler,
  1123. (subversion == ESA) ? IRQF_SHARED : 0,
  1124. driver_name, (void *)&sha[j])) {
  1125. printk("%s: unable to allocate IRQ %u, detaching.\n", name,
  1126. irq);
  1127. goto freelock;
  1128. }
  1129. if (subversion == ISA && request_dma(dma_channel, driver_name)) {
  1130. printk("%s: unable to allocate DMA channel %u, detaching.\n",
  1131. name, dma_channel);
  1132. goto freeirq;
  1133. }
  1134. #if defined(FORCE_CONFIG)
  1135. {
  1136. struct eata_config *cf;
  1137. dma_addr_t cf_dma_addr;
  1138. cf = pci_zalloc_consistent(pdev, sizeof(struct eata_config),
  1139. &cf_dma_addr);
  1140. if (!cf) {
  1141. printk
  1142. ("%s: config, pci_alloc_consistent failed, detaching.\n",
  1143. name);
  1144. goto freedma;
  1145. }
  1146. /* Set board configuration */
  1147. cf->len = (ushort) H2DEV16((ushort) 510);
  1148. cf->ocena = 1;
  1149. if (do_dma(port_base, cf_dma_addr, SET_CONFIG_DMA)) {
  1150. printk
  1151. ("%s: busy timeout sending configuration, detaching.\n",
  1152. name);
  1153. pci_free_consistent(pdev, sizeof(struct eata_config),
  1154. cf, cf_dma_addr);
  1155. goto freedma;
  1156. }
  1157. }
  1158. #endif
  1159. sh[j] = shost = scsi_register(tpnt, sizeof(struct hostdata));
  1160. if (shost == NULL) {
  1161. printk("%s: unable to register host, detaching.\n", name);
  1162. goto freedma;
  1163. }
  1164. shost->io_port = port_base;
  1165. shost->unique_id = port_base;
  1166. shost->n_io_port = REGION_SIZE;
  1167. shost->dma_channel = dma_channel;
  1168. shost->irq = irq;
  1169. shost->sg_tablesize = (ushort) info.scatt_size;
  1170. shost->this_id = (ushort) info.host_addr[3];
  1171. shost->can_queue = (ushort) info.queue_size;
  1172. shost->cmd_per_lun = MAX_CMD_PER_LUN;
  1173. ha = (struct hostdata *)shost->hostdata;
  1174. memset(ha, 0, sizeof(struct hostdata));
  1175. ha->subversion = subversion;
  1176. ha->protocol_rev = protocol_rev;
  1177. ha->is_pci = is_pci;
  1178. ha->pdev = pdev;
  1179. ha->board_number = j;
  1180. if (ha->subversion == ESA)
  1181. shost->unchecked_isa_dma = 0;
  1182. else {
  1183. unsigned long flags;
  1184. shost->unchecked_isa_dma = 1;
  1185. flags = claim_dma_lock();
  1186. disable_dma(dma_channel);
  1187. clear_dma_ff(dma_channel);
  1188. set_dma_mode(dma_channel, DMA_MODE_CASCADE);
  1189. enable_dma(dma_channel);
  1190. release_dma_lock(flags);
  1191. }
  1192. strcpy(ha->board_name, name);
  1193. /* DPT PM2012 does not allow to detect sg_tablesize correctly */
  1194. if (shost->sg_tablesize > MAX_SGLIST || shost->sg_tablesize < 2) {
  1195. printk("%s: detect, wrong n. of SG lists %d, fixed.\n",
  1196. ha->board_name, shost->sg_tablesize);
  1197. shost->sg_tablesize = MAX_SGLIST;
  1198. }
  1199. /* DPT PM2012 does not allow to detect can_queue correctly */
  1200. if (shost->can_queue > MAX_MAILBOXES || shost->can_queue < 2) {
  1201. printk("%s: detect, wrong n. of mbox %d, fixed.\n",
  1202. ha->board_name, shost->can_queue);
  1203. shost->can_queue = MAX_MAILBOXES;
  1204. }
  1205. if (protocol_rev != 'A') {
  1206. if (info.max_chan > 0 && info.max_chan < MAX_CHANNEL)
  1207. shost->max_channel = info.max_chan;
  1208. if (info.max_id > 7 && info.max_id < MAX_TARGET)
  1209. shost->max_id = info.max_id + 1;
  1210. if (info.large_sg && shost->sg_tablesize == MAX_SGLIST)
  1211. shost->sg_tablesize = MAX_LARGE_SGLIST;
  1212. }
  1213. if (protocol_rev == 'C') {
  1214. if (info.max_lun > 7 && info.max_lun < MAX_LUN)
  1215. shost->max_lun = info.max_lun + 1;
  1216. }
  1217. if (dma_channel == NO_DMA)
  1218. sprintf(dma_name, "%s", "BMST");
  1219. else
  1220. sprintf(dma_name, "DMA %u", dma_channel);
  1221. for (i = 0; i < shost->can_queue; i++)
  1222. ha->cp[i].cp_dma_addr = pci_map_single(ha->pdev,
  1223. &ha->cp[i],
  1224. sizeof(struct mscp),
  1225. PCI_DMA_BIDIRECTIONAL);
  1226. for (i = 0; i < shost->can_queue; i++) {
  1227. size_t sz = shost->sg_tablesize *sizeof(struct sg_list);
  1228. gfp_t gfp_mask = (shost->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC;
  1229. ha->cp[i].sglist = kmalloc(sz, gfp_mask);
  1230. if (!ha->cp[i].sglist) {
  1231. printk
  1232. ("%s: kmalloc SGlist failed, mbox %d, detaching.\n",
  1233. ha->board_name, i);
  1234. goto release;
  1235. }
  1236. }
  1237. if (!(ha->sp_cpu_addr = pci_alloc_consistent(ha->pdev,
  1238. sizeof(struct mssp),
  1239. &ha->sp_dma_addr))) {
  1240. printk("%s: pci_alloc_consistent failed, detaching.\n", ha->board_name);
  1241. goto release;
  1242. }
  1243. if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
  1244. max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
  1245. if (max_queue_depth < MAX_CMD_PER_LUN)
  1246. max_queue_depth = MAX_CMD_PER_LUN;
  1247. if (tag_mode != TAG_DISABLED && tag_mode != TAG_SIMPLE)
  1248. tag_mode = TAG_ORDERED;
  1249. if (j == 0) {
  1250. printk
  1251. ("EATA/DMA 2.0x: Copyright (C) 1994-2003 Dario Ballabio.\n");
  1252. printk
  1253. ("%s config options -> tm:%d, lc:%c, mq:%d, rs:%c, et:%c, "
  1254. "ip:%c, ep:%c, pp:%c.\n", driver_name, tag_mode,
  1255. YESNO(linked_comm), max_queue_depth, YESNO(rev_scan),
  1256. YESNO(ext_tran), YESNO(isa_probe), YESNO(eisa_probe),
  1257. YESNO(pci_probe));
  1258. }
  1259. printk("%s: 2.0%c, %s 0x%03lx, IRQ %u, %s, SG %d, MB %d.\n",
  1260. ha->board_name, ha->protocol_rev, bus_type,
  1261. (unsigned long)shost->io_port, shost->irq, dma_name,
  1262. shost->sg_tablesize, shost->can_queue);
  1263. if (shost->max_id > 8 || shost->max_lun > 8)
  1264. printk
  1265. ("%s: wide SCSI support enabled, max_id %u, max_lun %llu.\n",
  1266. ha->board_name, shost->max_id, shost->max_lun);
  1267. for (i = 0; i <= shost->max_channel; i++)
  1268. printk("%s: SCSI channel %u enabled, host target ID %d.\n",
  1269. ha->board_name, i, info.host_addr[3 - i]);
  1270. #if defined(DEBUG_DETECT)
  1271. printk("%s: Vers. 0x%x, ocs %u, tar %u, trnxfr %u, more %u, SYNC 0x%x, "
  1272. "sec. %u, infol %d, cpl %d spl %d.\n", name, info.version,
  1273. info.ocsena, info.tarsup, info.trnxfr, info.morsup, info.sync,
  1274. info.second, info.data_len, info.cp_len, info.sp_len);
  1275. if (protocol_rev == 'B' || protocol_rev == 'C')
  1276. printk("%s: isaena %u, forcaddr %u, max_id %u, max_chan %u, "
  1277. "large_sg %u, res1 %u.\n", name, info.isaena,
  1278. info.forcaddr, info.max_id, info.max_chan, info.large_sg,
  1279. info.res1);
  1280. if (protocol_rev == 'C')
  1281. printk("%s: max_lun %u, m1 %u, idquest %u, pci %u, eisa %u, "
  1282. "raidnum %u.\n", name, info.max_lun, info.m1,
  1283. info.idquest, info.pci, info.eisa, info.raidnum);
  1284. #endif
  1285. if (ha->pdev) {
  1286. pci_set_master(ha->pdev);
  1287. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32)))
  1288. printk("%s: warning, pci_set_dma_mask failed.\n",
  1289. ha->board_name);
  1290. }
  1291. return 1;
  1292. freedma:
  1293. if (subversion == ISA)
  1294. free_dma(dma_channel);
  1295. freeirq:
  1296. free_irq(irq, &sha[j]);
  1297. freelock:
  1298. release_region(port_base, REGION_SIZE);
  1299. fail:
  1300. return 0;
  1301. release:
  1302. eata2x_release(shost);
  1303. return 0;
  1304. }
  1305. static void internal_setup(char *str, int *ints)
  1306. {
  1307. int i, argc = ints[0];
  1308. char *cur = str, *pc;
  1309. if (argc > 0) {
  1310. if (argc > MAX_INT_PARAM)
  1311. argc = MAX_INT_PARAM;
  1312. for (i = 0; i < argc; i++)
  1313. io_port[i] = ints[i + 1];
  1314. io_port[i] = 0;
  1315. setup_done = 1;
  1316. }
  1317. while (cur && (pc = strchr(cur, ':'))) {
  1318. int val = 0, c = *++pc;
  1319. if (c == 'n' || c == 'N')
  1320. val = 0;
  1321. else if (c == 'y' || c == 'Y')
  1322. val = 1;
  1323. else
  1324. val = (int)simple_strtoul(pc, NULL, 0);
  1325. if (!strncmp(cur, "lc:", 3))
  1326. linked_comm = val;
  1327. else if (!strncmp(cur, "tm:", 3))
  1328. tag_mode = val;
  1329. else if (!strncmp(cur, "tc:", 3))
  1330. tag_mode = val;
  1331. else if (!strncmp(cur, "mq:", 3))
  1332. max_queue_depth = val;
  1333. else if (!strncmp(cur, "ls:", 3))
  1334. link_statistics = val;
  1335. else if (!strncmp(cur, "et:", 3))
  1336. ext_tran = val;
  1337. else if (!strncmp(cur, "rs:", 3))
  1338. rev_scan = val;
  1339. else if (!strncmp(cur, "ip:", 3))
  1340. isa_probe = val;
  1341. else if (!strncmp(cur, "ep:", 3))
  1342. eisa_probe = val;
  1343. else if (!strncmp(cur, "pp:", 3))
  1344. pci_probe = val;
  1345. if ((cur = strchr(cur, ',')))
  1346. ++cur;
  1347. }
  1348. return;
  1349. }
  1350. static int option_setup(char *str)
  1351. {
  1352. int ints[MAX_INT_PARAM];
  1353. char *cur = str;
  1354. int i = 1;
  1355. while (cur && isdigit(*cur) && i < MAX_INT_PARAM) {
  1356. ints[i++] = simple_strtoul(cur, NULL, 0);
  1357. if ((cur = strchr(cur, ',')) != NULL)
  1358. cur++;
  1359. }
  1360. ints[0] = i - 1;
  1361. internal_setup(cur, ints);
  1362. return 1;
  1363. }
  1364. static void add_pci_ports(void)
  1365. {
  1366. #if defined(CONFIG_PCI)
  1367. unsigned int addr, k;
  1368. struct pci_dev *dev = NULL;
  1369. for (k = 0; k < MAX_PCI; k++) {
  1370. if (!(dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev)))
  1371. break;
  1372. if (pci_enable_device(dev)) {
  1373. #if defined(DEBUG_PCI_DETECT)
  1374. printk
  1375. ("%s: detect, bus %d, devfn 0x%x, pci_enable_device failed.\n",
  1376. driver_name, dev->bus->number, dev->devfn);
  1377. #endif
  1378. continue;
  1379. }
  1380. addr = pci_resource_start(dev, 0);
  1381. #if defined(DEBUG_PCI_DETECT)
  1382. printk("%s: detect, seq. %d, bus %d, devfn 0x%x, addr 0x%x.\n",
  1383. driver_name, k, dev->bus->number, dev->devfn, addr);
  1384. #endif
  1385. /* Order addresses according to rev_scan value */
  1386. io_port[MAX_INT_PARAM + (rev_scan ? (MAX_PCI - k) : (1 + k))] =
  1387. addr + PCI_BASE_ADDRESS_0;
  1388. }
  1389. pci_dev_put(dev);
  1390. #endif /* end CONFIG_PCI */
  1391. }
  1392. static int eata2x_detect(struct scsi_host_template *tpnt)
  1393. {
  1394. unsigned int j = 0, k;
  1395. tpnt->proc_name = "eata2x";
  1396. if (strlen(boot_options))
  1397. option_setup(boot_options);
  1398. #if defined(MODULE)
  1399. /* io_port could have been modified when loading as a module */
  1400. if (io_port[0] != SKIP) {
  1401. setup_done = 1;
  1402. io_port[MAX_INT_PARAM] = 0;
  1403. }
  1404. #endif
  1405. for (k = MAX_INT_PARAM; io_port[k]; k++)
  1406. if (io_port[k] == SKIP)
  1407. continue;
  1408. else if (io_port[k] <= MAX_ISA_ADDR) {
  1409. if (!isa_probe)
  1410. io_port[k] = SKIP;
  1411. } else if (io_port[k] >= MIN_EISA_ADDR
  1412. && io_port[k] <= MAX_EISA_ADDR) {
  1413. if (!eisa_probe)
  1414. io_port[k] = SKIP;
  1415. }
  1416. if (pci_probe) {
  1417. if (!setup_done)
  1418. add_pci_ports();
  1419. else
  1420. enable_pci_ports();
  1421. }
  1422. for (k = 0; io_port[k]; k++) {
  1423. if (io_port[k] == SKIP)
  1424. continue;
  1425. if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt))
  1426. j++;
  1427. }
  1428. num_boards = j;
  1429. return j;
  1430. }
  1431. static void map_dma(unsigned int i, struct hostdata *ha)
  1432. {
  1433. unsigned int k, pci_dir;
  1434. int count;
  1435. struct scatterlist *sg;
  1436. struct mscp *cpp;
  1437. struct scsi_cmnd *SCpnt;
  1438. cpp = &ha->cp[i];
  1439. SCpnt = cpp->SCpnt;
  1440. pci_dir = SCpnt->sc_data_direction;
  1441. if (SCpnt->sense_buffer)
  1442. cpp->sense_addr =
  1443. H2DEV(pci_map_single(ha->pdev, SCpnt->sense_buffer,
  1444. SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE));
  1445. cpp->sense_len = SCSI_SENSE_BUFFERSIZE;
  1446. if (!scsi_sg_count(SCpnt)) {
  1447. cpp->data_len = 0;
  1448. return;
  1449. }
  1450. count = pci_map_sg(ha->pdev, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
  1451. pci_dir);
  1452. BUG_ON(!count);
  1453. scsi_for_each_sg(SCpnt, sg, count, k) {
  1454. cpp->sglist[k].address = H2DEV(sg_dma_address(sg));
  1455. cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(sg));
  1456. }
  1457. cpp->sg = 1;
  1458. cpp->data_address = H2DEV(pci_map_single(ha->pdev, cpp->sglist,
  1459. scsi_sg_count(SCpnt) *
  1460. sizeof(struct sg_list),
  1461. pci_dir));
  1462. cpp->data_len = H2DEV((scsi_sg_count(SCpnt) * sizeof(struct sg_list)));
  1463. }
  1464. static void unmap_dma(unsigned int i, struct hostdata *ha)
  1465. {
  1466. unsigned int pci_dir;
  1467. struct mscp *cpp;
  1468. struct scsi_cmnd *SCpnt;
  1469. cpp = &ha->cp[i];
  1470. SCpnt = cpp->SCpnt;
  1471. pci_dir = SCpnt->sc_data_direction;
  1472. if (DEV2H(cpp->sense_addr))
  1473. pci_unmap_single(ha->pdev, DEV2H(cpp->sense_addr),
  1474. DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
  1475. if (scsi_sg_count(SCpnt))
  1476. pci_unmap_sg(ha->pdev, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
  1477. pci_dir);
  1478. if (!DEV2H(cpp->data_len))
  1479. pci_dir = PCI_DMA_BIDIRECTIONAL;
  1480. if (DEV2H(cpp->data_address))
  1481. pci_unmap_single(ha->pdev, DEV2H(cpp->data_address),
  1482. DEV2H(cpp->data_len), pci_dir);
  1483. }
  1484. static void sync_dma(unsigned int i, struct hostdata *ha)
  1485. {
  1486. unsigned int pci_dir;
  1487. struct mscp *cpp;
  1488. struct scsi_cmnd *SCpnt;
  1489. cpp = &ha->cp[i];
  1490. SCpnt = cpp->SCpnt;
  1491. pci_dir = SCpnt->sc_data_direction;
  1492. if (DEV2H(cpp->sense_addr))
  1493. pci_dma_sync_single_for_cpu(ha->pdev, DEV2H(cpp->sense_addr),
  1494. DEV2H(cpp->sense_len),
  1495. PCI_DMA_FROMDEVICE);
  1496. if (scsi_sg_count(SCpnt))
  1497. pci_dma_sync_sg_for_cpu(ha->pdev, scsi_sglist(SCpnt),
  1498. scsi_sg_count(SCpnt), pci_dir);
  1499. if (!DEV2H(cpp->data_len))
  1500. pci_dir = PCI_DMA_BIDIRECTIONAL;
  1501. if (DEV2H(cpp->data_address))
  1502. pci_dma_sync_single_for_cpu(ha->pdev,
  1503. DEV2H(cpp->data_address),
  1504. DEV2H(cpp->data_len), pci_dir);
  1505. }
  1506. static void scsi_to_dev_dir(unsigned int i, struct hostdata *ha)
  1507. {
  1508. unsigned int k;
  1509. static const unsigned char data_out_cmds[] = {
  1510. 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
  1511. 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
  1512. 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
  1513. };
  1514. static const unsigned char data_none_cmds[] = {
  1515. 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
  1516. 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
  1517. 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
  1518. };
  1519. struct mscp *cpp;
  1520. struct scsi_cmnd *SCpnt;
  1521. cpp = &ha->cp[i];
  1522. SCpnt = cpp->SCpnt;
  1523. if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1524. cpp->din = 1;
  1525. cpp->dout = 0;
  1526. return;
  1527. } else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
  1528. cpp->din = 0;
  1529. cpp->dout = 1;
  1530. return;
  1531. } else if (SCpnt->sc_data_direction == DMA_NONE) {
  1532. cpp->din = 0;
  1533. cpp->dout = 0;
  1534. return;
  1535. }
  1536. if (SCpnt->sc_data_direction != DMA_BIDIRECTIONAL)
  1537. panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n",
  1538. ha->board_name);
  1539. for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
  1540. if (SCpnt->cmnd[0] == data_out_cmds[k]) {
  1541. cpp->dout = 1;
  1542. break;
  1543. }
  1544. if ((cpp->din = !cpp->dout))
  1545. for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
  1546. if (SCpnt->cmnd[0] == data_none_cmds[k]) {
  1547. cpp->din = 0;
  1548. break;
  1549. }
  1550. }
  1551. static int eata2x_queuecommand_lck(struct scsi_cmnd *SCpnt,
  1552. void (*done) (struct scsi_cmnd *))
  1553. {
  1554. struct Scsi_Host *shost = SCpnt->device->host;
  1555. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  1556. unsigned int i, k;
  1557. struct mscp *cpp;
  1558. if (SCpnt->host_scribble)
  1559. panic("%s: qcomm, SCpnt %p already active.\n",
  1560. ha->board_name, SCpnt);
  1561. /* i is the mailbox number, look for the first free mailbox
  1562. starting from last_cp_used */
  1563. i = ha->last_cp_used + 1;
  1564. for (k = 0; k < shost->can_queue; k++, i++) {
  1565. if (i >= shost->can_queue)
  1566. i = 0;
  1567. if (ha->cp_stat[i] == FREE) {
  1568. ha->last_cp_used = i;
  1569. break;
  1570. }
  1571. }
  1572. if (k == shost->can_queue) {
  1573. printk("%s: qcomm, no free mailbox.\n", ha->board_name);
  1574. return 1;
  1575. }
  1576. /* Set pointer to control packet structure */
  1577. cpp = &ha->cp[i];
  1578. memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
  1579. /* Set pointer to status packet structure, Big Endian format */
  1580. cpp->sp_dma_addr = H2DEV(ha->sp_dma_addr);
  1581. SCpnt->scsi_done = done;
  1582. cpp->cpp_index = i;
  1583. SCpnt->host_scribble = (unsigned char *)&cpp->cpp_index;
  1584. if (do_trace)
  1585. scmd_printk(KERN_INFO, SCpnt,
  1586. "qcomm, mbox %d.\n", i);
  1587. cpp->reqsen = 1;
  1588. cpp->dispri = 1;
  1589. #if 0
  1590. if (SCpnt->device->type == TYPE_TAPE)
  1591. cpp->hbaci = 1;
  1592. #endif
  1593. cpp->one = 1;
  1594. cpp->channel = SCpnt->device->channel;
  1595. cpp->target = SCpnt->device->id;
  1596. cpp->lun = SCpnt->device->lun;
  1597. cpp->SCpnt = SCpnt;
  1598. memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
  1599. /* Use data transfer direction SCpnt->sc_data_direction */
  1600. scsi_to_dev_dir(i, ha);
  1601. /* Map DMA buffers and SG list */
  1602. map_dma(i, ha);
  1603. if (linked_comm && SCpnt->device->queue_depth > 2
  1604. && TLDEV(SCpnt->device->type)) {
  1605. ha->cp_stat[i] = READY;
  1606. flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), ha, 0);
  1607. return 0;
  1608. }
  1609. /* Send control packet to the board */
  1610. if (do_dma(shost->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
  1611. unmap_dma(i, ha);
  1612. SCpnt->host_scribble = NULL;
  1613. scmd_printk(KERN_INFO, SCpnt, "qcomm, adapter busy.\n");
  1614. return 1;
  1615. }
  1616. ha->cp_stat[i] = IN_USE;
  1617. return 0;
  1618. }
  1619. static DEF_SCSI_QCMD(eata2x_queuecommand)
  1620. static int eata2x_eh_abort(struct scsi_cmnd *SCarg)
  1621. {
  1622. struct Scsi_Host *shost = SCarg->device->host;
  1623. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  1624. unsigned int i;
  1625. if (SCarg->host_scribble == NULL) {
  1626. scmd_printk(KERN_INFO, SCarg, "abort, cmd inactive.\n");
  1627. return SUCCESS;
  1628. }
  1629. i = *(unsigned int *)SCarg->host_scribble;
  1630. scmd_printk(KERN_WARNING, SCarg, "abort, mbox %d.\n", i);
  1631. if (i >= shost->can_queue)
  1632. panic("%s: abort, invalid SCarg->host_scribble.\n", ha->board_name);
  1633. if (wait_on_busy(shost->io_port, MAXLOOP)) {
  1634. printk("%s: abort, timeout error.\n", ha->board_name);
  1635. return FAILED;
  1636. }
  1637. if (ha->cp_stat[i] == FREE) {
  1638. printk("%s: abort, mbox %d is free.\n", ha->board_name, i);
  1639. return SUCCESS;
  1640. }
  1641. if (ha->cp_stat[i] == IN_USE) {
  1642. printk("%s: abort, mbox %d is in use.\n", ha->board_name, i);
  1643. if (SCarg != ha->cp[i].SCpnt)
  1644. panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
  1645. ha->board_name, i, SCarg, ha->cp[i].SCpnt);
  1646. if (inb(shost->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)
  1647. printk("%s: abort, mbox %d, interrupt pending.\n",
  1648. ha->board_name, i);
  1649. return FAILED;
  1650. }
  1651. if (ha->cp_stat[i] == IN_RESET) {
  1652. printk("%s: abort, mbox %d is in reset.\n", ha->board_name, i);
  1653. return FAILED;
  1654. }
  1655. if (ha->cp_stat[i] == LOCKED) {
  1656. printk("%s: abort, mbox %d is locked.\n", ha->board_name, i);
  1657. return SUCCESS;
  1658. }
  1659. if (ha->cp_stat[i] == READY || ha->cp_stat[i] == ABORTING) {
  1660. unmap_dma(i, ha);
  1661. SCarg->result = DID_ABORT << 16;
  1662. SCarg->host_scribble = NULL;
  1663. ha->cp_stat[i] = FREE;
  1664. printk("%s, abort, mbox %d ready, DID_ABORT, done.\n",
  1665. ha->board_name, i);
  1666. SCarg->scsi_done(SCarg);
  1667. return SUCCESS;
  1668. }
  1669. panic("%s: abort, mbox %d, invalid cp_stat.\n", ha->board_name, i);
  1670. }
  1671. static int eata2x_eh_host_reset(struct scsi_cmnd *SCarg)
  1672. {
  1673. unsigned int i, time, k, c, limit = 0;
  1674. int arg_done = 0;
  1675. struct scsi_cmnd *SCpnt;
  1676. struct Scsi_Host *shost = SCarg->device->host;
  1677. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  1678. scmd_printk(KERN_INFO, SCarg, "reset, enter.\n");
  1679. spin_lock_irq(shost->host_lock);
  1680. if (SCarg->host_scribble == NULL)
  1681. printk("%s: reset, inactive.\n", ha->board_name);
  1682. if (ha->in_reset) {
  1683. printk("%s: reset, exit, already in reset.\n", ha->board_name);
  1684. spin_unlock_irq(shost->host_lock);
  1685. return FAILED;
  1686. }
  1687. if (wait_on_busy(shost->io_port, MAXLOOP)) {
  1688. printk("%s: reset, exit, timeout error.\n", ha->board_name);
  1689. spin_unlock_irq(shost->host_lock);
  1690. return FAILED;
  1691. }
  1692. ha->retries = 0;
  1693. for (c = 0; c <= shost->max_channel; c++)
  1694. for (k = 0; k < shost->max_id; k++) {
  1695. ha->target_redo[k][c] = 1;
  1696. ha->target_to[k][c] = 0;
  1697. }
  1698. for (i = 0; i < shost->can_queue; i++) {
  1699. if (ha->cp_stat[i] == FREE)
  1700. continue;
  1701. if (ha->cp_stat[i] == LOCKED) {
  1702. ha->cp_stat[i] = FREE;
  1703. printk("%s: reset, locked mbox %d forced free.\n",
  1704. ha->board_name, i);
  1705. continue;
  1706. }
  1707. if (!(SCpnt = ha->cp[i].SCpnt))
  1708. panic("%s: reset, mbox %d, SCpnt == NULL.\n", ha->board_name, i);
  1709. if (ha->cp_stat[i] == READY || ha->cp_stat[i] == ABORTING) {
  1710. ha->cp_stat[i] = ABORTING;
  1711. printk("%s: reset, mbox %d aborting.\n",
  1712. ha->board_name, i);
  1713. }
  1714. else {
  1715. ha->cp_stat[i] = IN_RESET;
  1716. printk("%s: reset, mbox %d in reset.\n",
  1717. ha->board_name, i);
  1718. }
  1719. if (SCpnt->host_scribble == NULL)
  1720. panic("%s: reset, mbox %d, garbled SCpnt.\n", ha->board_name, i);
  1721. if (*(unsigned int *)SCpnt->host_scribble != i)
  1722. panic("%s: reset, mbox %d, index mismatch.\n", ha->board_name, i);
  1723. if (SCpnt->scsi_done == NULL)
  1724. panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n",
  1725. ha->board_name, i);
  1726. if (SCpnt == SCarg)
  1727. arg_done = 1;
  1728. }
  1729. if (do_dma(shost->io_port, 0, RESET_PIO)) {
  1730. printk("%s: reset, cannot reset, timeout error.\n", ha->board_name);
  1731. spin_unlock_irq(shost->host_lock);
  1732. return FAILED;
  1733. }
  1734. printk("%s: reset, board reset done, enabling interrupts.\n", ha->board_name);
  1735. #if defined(DEBUG_RESET)
  1736. do_trace = 1;
  1737. #endif
  1738. ha->in_reset = 1;
  1739. spin_unlock_irq(shost->host_lock);
  1740. /* FIXME: use a sleep instead */
  1741. time = jiffies;
  1742. while ((jiffies - time) < (10 * HZ) && limit++ < 200000)
  1743. udelay(100L);
  1744. spin_lock_irq(shost->host_lock);
  1745. printk("%s: reset, interrupts disabled, loops %d.\n", ha->board_name, limit);
  1746. for (i = 0; i < shost->can_queue; i++) {
  1747. if (ha->cp_stat[i] == IN_RESET) {
  1748. SCpnt = ha->cp[i].SCpnt;
  1749. unmap_dma(i, ha);
  1750. SCpnt->result = DID_RESET << 16;
  1751. SCpnt->host_scribble = NULL;
  1752. /* This mailbox is still waiting for its interrupt */
  1753. ha->cp_stat[i] = LOCKED;
  1754. printk
  1755. ("%s, reset, mbox %d locked, DID_RESET, done.\n",
  1756. ha->board_name, i);
  1757. }
  1758. else if (ha->cp_stat[i] == ABORTING) {
  1759. SCpnt = ha->cp[i].SCpnt;
  1760. unmap_dma(i, ha);
  1761. SCpnt->result = DID_RESET << 16;
  1762. SCpnt->host_scribble = NULL;
  1763. /* This mailbox was never queued to the adapter */
  1764. ha->cp_stat[i] = FREE;
  1765. printk
  1766. ("%s, reset, mbox %d aborting, DID_RESET, done.\n",
  1767. ha->board_name, i);
  1768. }
  1769. else
  1770. /* Any other mailbox has already been set free by interrupt */
  1771. continue;
  1772. SCpnt->scsi_done(SCpnt);
  1773. }
  1774. ha->in_reset = 0;
  1775. do_trace = 0;
  1776. if (arg_done)
  1777. printk("%s: reset, exit, done.\n", ha->board_name);
  1778. else
  1779. printk("%s: reset, exit.\n", ha->board_name);
  1780. spin_unlock_irq(shost->host_lock);
  1781. return SUCCESS;
  1782. }
  1783. int eata2x_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1784. sector_t capacity, int *dkinfo)
  1785. {
  1786. unsigned int size = capacity;
  1787. if (ext_tran || (scsicam_bios_param(bdev, capacity, dkinfo) < 0)) {
  1788. dkinfo[0] = 255;
  1789. dkinfo[1] = 63;
  1790. dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
  1791. }
  1792. #if defined (DEBUG_GEOMETRY)
  1793. printk("%s: bios_param, head=%d, sec=%d, cyl=%d.\n", driver_name,
  1794. dkinfo[0], dkinfo[1], dkinfo[2]);
  1795. #endif
  1796. return 0;
  1797. }
  1798. static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
  1799. unsigned int rev)
  1800. {
  1801. unsigned int i, j, k, y;
  1802. unsigned long x;
  1803. for (i = 0; i < n - 1; i++) {
  1804. k = i;
  1805. for (j = k + 1; j < n; j++)
  1806. if (rev) {
  1807. if (sk[j] > sk[k])
  1808. k = j;
  1809. } else {
  1810. if (sk[j] < sk[k])
  1811. k = j;
  1812. }
  1813. if (k != i) {
  1814. x = sk[k];
  1815. sk[k] = sk[i];
  1816. sk[i] = x;
  1817. y = da[k];
  1818. da[k] = da[i];
  1819. da[i] = y;
  1820. }
  1821. }
  1822. return;
  1823. }
  1824. static int reorder(struct hostdata *ha, unsigned long cursec,
  1825. unsigned int ihdlr, unsigned int il[], unsigned int n_ready)
  1826. {
  1827. struct scsi_cmnd *SCpnt;
  1828. struct mscp *cpp;
  1829. unsigned int k, n;
  1830. unsigned int rev = 0, s = 1, r = 1;
  1831. unsigned int input_only = 1, overlap = 0;
  1832. unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
  1833. unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
  1834. unsigned long ioseek = 0;
  1835. static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
  1836. static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
  1837. static unsigned int readysorted = 0, revcount = 0;
  1838. static unsigned long seeksorted = 0, seeknosort = 0;
  1839. if (link_statistics && !(++flushcount % link_statistics))
  1840. printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"
  1841. " av %ldK as %ldK.\n", flushcount, batchcount,
  1842. inputcount, ovlcount, readycount, readysorted, sortcount,
  1843. revcount, seeknosort / (readycount + 1),
  1844. seeksorted / (readycount + 1));
  1845. if (n_ready <= 1)
  1846. return 0;
  1847. for (n = 0; n < n_ready; n++) {
  1848. k = il[n];
  1849. cpp = &ha->cp[k];
  1850. SCpnt = cpp->SCpnt;
  1851. if (!cpp->din)
  1852. input_only = 0;
  1853. if (blk_rq_pos(SCpnt->request) < minsec)
  1854. minsec = blk_rq_pos(SCpnt->request);
  1855. if (blk_rq_pos(SCpnt->request) > maxsec)
  1856. maxsec = blk_rq_pos(SCpnt->request);
  1857. sl[n] = blk_rq_pos(SCpnt->request);
  1858. ioseek += blk_rq_sectors(SCpnt->request);
  1859. if (!n)
  1860. continue;
  1861. if (sl[n] < sl[n - 1])
  1862. s = 0;
  1863. if (sl[n] > sl[n - 1])
  1864. r = 0;
  1865. if (link_statistics) {
  1866. if (sl[n] > sl[n - 1])
  1867. seek += sl[n] - sl[n - 1];
  1868. else
  1869. seek += sl[n - 1] - sl[n];
  1870. }
  1871. }
  1872. if (link_statistics) {
  1873. if (cursec > sl[0])
  1874. seek += cursec - sl[0];
  1875. else
  1876. seek += sl[0] - cursec;
  1877. }
  1878. if (cursec > ((maxsec + minsec) / 2))
  1879. rev = 1;
  1880. if (ioseek > ((maxsec - minsec) / 2))
  1881. rev = 0;
  1882. if (!((rev && r) || (!rev && s)))
  1883. sort(sl, il, n_ready, rev);
  1884. if (!input_only)
  1885. for (n = 0; n < n_ready; n++) {
  1886. k = il[n];
  1887. cpp = &ha->cp[k];
  1888. SCpnt = cpp->SCpnt;
  1889. ll[n] = blk_rq_sectors(SCpnt->request);
  1890. pl[n] = SCpnt->serial_number;
  1891. if (!n)
  1892. continue;
  1893. if ((sl[n] == sl[n - 1])
  1894. || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
  1895. || (rev && ((sl[n] + ll[n]) > sl[n - 1])))
  1896. overlap = 1;
  1897. }
  1898. if (overlap)
  1899. sort(pl, il, n_ready, 0);
  1900. if (link_statistics) {
  1901. if (cursec > sl[0])
  1902. iseek = cursec - sl[0];
  1903. else
  1904. iseek = sl[0] - cursec;
  1905. batchcount++;
  1906. readycount += n_ready;
  1907. seeknosort += seek / 1024;
  1908. if (input_only)
  1909. inputcount++;
  1910. if (overlap) {
  1911. ovlcount++;
  1912. seeksorted += iseek / 1024;
  1913. } else
  1914. seeksorted += (iseek + maxsec - minsec) / 1024;
  1915. if (rev && !r) {
  1916. revcount++;
  1917. readysorted += n_ready;
  1918. }
  1919. if (!rev && !s) {
  1920. sortcount++;
  1921. readysorted += n_ready;
  1922. }
  1923. }
  1924. #if defined(DEBUG_LINKED_COMMANDS)
  1925. if (link_statistics && (overlap || !(flushcount % link_statistics)))
  1926. for (n = 0; n < n_ready; n++) {
  1927. k = il[n];
  1928. cpp = &ha->cp[k];
  1929. SCpnt = cpp->SCpnt;
  1930. scmd_printk(KERN_INFO, SCpnt,
  1931. "%s mb %d fc %d nr %d sec %ld ns %u"
  1932. " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
  1933. (ihdlr ? "ihdlr" : "qcomm"),
  1934. k, flushcount,
  1935. n_ready, blk_rq_pos(SCpnt->request),
  1936. blk_rq_sectors(SCpnt->request), cursec, YESNO(s),
  1937. YESNO(r), YESNO(rev), YESNO(input_only),
  1938. YESNO(overlap), cpp->din);
  1939. }
  1940. #endif
  1941. return overlap;
  1942. }
  1943. static void flush_dev(struct scsi_device *dev, unsigned long cursec,
  1944. struct hostdata *ha, unsigned int ihdlr)
  1945. {
  1946. struct scsi_cmnd *SCpnt;
  1947. struct mscp *cpp;
  1948. unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
  1949. for (k = 0; k < dev->host->can_queue; k++) {
  1950. if (ha->cp_stat[k] != READY && ha->cp_stat[k] != IN_USE)
  1951. continue;
  1952. cpp = &ha->cp[k];
  1953. SCpnt = cpp->SCpnt;
  1954. if (SCpnt->device != dev)
  1955. continue;
  1956. if (ha->cp_stat[k] == IN_USE)
  1957. return;
  1958. il[n_ready++] = k;
  1959. }
  1960. if (reorder(ha, cursec, ihdlr, il, n_ready))
  1961. n_ready = 1;
  1962. for (n = 0; n < n_ready; n++) {
  1963. k = il[n];
  1964. cpp = &ha->cp[k];
  1965. SCpnt = cpp->SCpnt;
  1966. if (do_dma(dev->host->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
  1967. scmd_printk(KERN_INFO, SCpnt,
  1968. "%s, mbox %d, adapter"
  1969. " busy, will abort.\n",
  1970. (ihdlr ? "ihdlr" : "qcomm"),
  1971. k);
  1972. ha->cp_stat[k] = ABORTING;
  1973. continue;
  1974. }
  1975. ha->cp_stat[k] = IN_USE;
  1976. }
  1977. }
  1978. static irqreturn_t ihdlr(struct Scsi_Host *shost)
  1979. {
  1980. struct scsi_cmnd *SCpnt;
  1981. unsigned int i, k, c, status, tstatus, reg;
  1982. struct mssp *spp;
  1983. struct mscp *cpp;
  1984. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  1985. int irq = shost->irq;
  1986. /* Check if this board need to be serviced */
  1987. if (!(inb(shost->io_port + REG_AUX_STATUS) & IRQ_ASSERTED))
  1988. goto none;
  1989. ha->iocount++;
  1990. if (do_trace)
  1991. printk("%s: ihdlr, enter, irq %d, count %d.\n", ha->board_name, irq,
  1992. ha->iocount);
  1993. /* Check if this board is still busy */
  1994. if (wait_on_busy(shost->io_port, 20 * MAXLOOP)) {
  1995. reg = inb(shost->io_port + REG_STATUS);
  1996. printk
  1997. ("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
  1998. ha->board_name, irq, reg, ha->iocount);
  1999. goto none;
  2000. }
  2001. spp = &ha->sp;
  2002. /* Make a local copy just before clearing the interrupt indication */
  2003. memcpy(spp, ha->sp_cpu_addr, sizeof(struct mssp));
  2004. /* Clear the completion flag and cp pointer on the dynamic copy of sp */
  2005. memset(ha->sp_cpu_addr, 0, sizeof(struct mssp));
  2006. /* Read the status register to clear the interrupt indication */
  2007. reg = inb(shost->io_port + REG_STATUS);
  2008. #if defined (DEBUG_INTERRUPT)
  2009. {
  2010. unsigned char *bytesp;
  2011. int cnt;
  2012. bytesp = (unsigned char *)spp;
  2013. if (ha->iocount < 200) {
  2014. printk("sp[] =");
  2015. for (cnt = 0; cnt < 15; cnt++)
  2016. printk(" 0x%x", bytesp[cnt]);
  2017. printk("\n");
  2018. }
  2019. }
  2020. #endif
  2021. /* Reject any sp with supspect data */
  2022. if (spp->eoc == 0 && ha->iocount > 1)
  2023. printk
  2024. ("%s: ihdlr, spp->eoc == 0, irq %d, reg 0x%x, count %d.\n",
  2025. ha->board_name, irq, reg, ha->iocount);
  2026. if (spp->cpp_index < 0 || spp->cpp_index >= shost->can_queue)
  2027. printk
  2028. ("%s: ihdlr, bad spp->cpp_index %d, irq %d, reg 0x%x, count %d.\n",
  2029. ha->board_name, spp->cpp_index, irq, reg, ha->iocount);
  2030. if (spp->eoc == 0 || spp->cpp_index < 0
  2031. || spp->cpp_index >= shost->can_queue)
  2032. goto handled;
  2033. /* Find the mailbox to be serviced on this board */
  2034. i = spp->cpp_index;
  2035. cpp = &(ha->cp[i]);
  2036. #if defined(DEBUG_GENERATE_ABORTS)
  2037. if ((ha->iocount > 500) && ((ha->iocount % 500) < 3))
  2038. goto handled;
  2039. #endif
  2040. if (ha->cp_stat[i] == IGNORE) {
  2041. ha->cp_stat[i] = FREE;
  2042. goto handled;
  2043. } else if (ha->cp_stat[i] == LOCKED) {
  2044. ha->cp_stat[i] = FREE;
  2045. printk("%s: ihdlr, mbox %d unlocked, count %d.\n", ha->board_name, i,
  2046. ha->iocount);
  2047. goto handled;
  2048. } else if (ha->cp_stat[i] == FREE) {
  2049. printk("%s: ihdlr, mbox %d is free, count %d.\n", ha->board_name, i,
  2050. ha->iocount);
  2051. goto handled;
  2052. } else if (ha->cp_stat[i] == IN_RESET)
  2053. printk("%s: ihdlr, mbox %d is in reset.\n", ha->board_name, i);
  2054. else if (ha->cp_stat[i] != IN_USE)
  2055. panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
  2056. ha->board_name, i, ha->cp_stat[i]);
  2057. ha->cp_stat[i] = FREE;
  2058. SCpnt = cpp->SCpnt;
  2059. if (SCpnt == NULL)
  2060. panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", ha->board_name, i);
  2061. if (SCpnt->host_scribble == NULL)
  2062. panic("%s: ihdlr, mbox %d, SCpnt %p garbled.\n", ha->board_name,
  2063. i, SCpnt);
  2064. if (*(unsigned int *)SCpnt->host_scribble != i)
  2065. panic("%s: ihdlr, mbox %d, index mismatch %d.\n",
  2066. ha->board_name, i,
  2067. *(unsigned int *)SCpnt->host_scribble);
  2068. sync_dma(i, ha);
  2069. if (linked_comm && SCpnt->device->queue_depth > 2
  2070. && TLDEV(SCpnt->device->type))
  2071. flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), ha, 1);
  2072. tstatus = status_byte(spp->target_status);
  2073. #if defined(DEBUG_GENERATE_ERRORS)
  2074. if ((ha->iocount > 500) && ((ha->iocount % 200) < 2))
  2075. spp->adapter_status = 0x01;
  2076. #endif
  2077. switch (spp->adapter_status) {
  2078. case ASOK: /* status OK */
  2079. /* Forces a reset if a disk drive keeps returning BUSY */
  2080. if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
  2081. status = DID_ERROR << 16;
  2082. /* If there was a bus reset, redo operation on each target */
  2083. else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
  2084. && ha->target_redo[SCpnt->device->id][SCpnt->
  2085. device->
  2086. channel])
  2087. status = DID_BUS_BUSY << 16;
  2088. /* Works around a flaw in scsi.c */
  2089. else if (tstatus == CHECK_CONDITION
  2090. && SCpnt->device->type == TYPE_DISK
  2091. && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
  2092. status = DID_BUS_BUSY << 16;
  2093. else
  2094. status = DID_OK << 16;
  2095. if (tstatus == GOOD)
  2096. ha->target_redo[SCpnt->device->id][SCpnt->device->
  2097. channel] = 0;
  2098. if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
  2099. (!(tstatus == CHECK_CONDITION && ha->iocount <= 1000 &&
  2100. (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
  2101. printk("%s: ihdlr, target %d.%d:%d, "
  2102. "target_status 0x%x, sense key 0x%x.\n",
  2103. ha->board_name,
  2104. SCpnt->device->channel, SCpnt->device->id,
  2105. (u8)SCpnt->device->lun,
  2106. spp->target_status, SCpnt->sense_buffer[2]);
  2107. ha->target_to[SCpnt->device->id][SCpnt->device->channel] = 0;
  2108. if (ha->last_retried_pid == SCpnt->serial_number)
  2109. ha->retries = 0;
  2110. break;
  2111. case ASST: /* Selection Time Out */
  2112. case 0x02: /* Command Time Out */
  2113. if (ha->target_to[SCpnt->device->id][SCpnt->device->channel] > 1)
  2114. status = DID_ERROR << 16;
  2115. else {
  2116. status = DID_TIME_OUT << 16;
  2117. ha->target_to[SCpnt->device->id][SCpnt->device->
  2118. channel]++;
  2119. }
  2120. break;
  2121. /* Perform a limited number of internal retries */
  2122. case 0x03: /* SCSI Bus Reset Received */
  2123. case 0x04: /* Initial Controller Power-up */
  2124. for (c = 0; c <= shost->max_channel; c++)
  2125. for (k = 0; k < shost->max_id; k++)
  2126. ha->target_redo[k][c] = 1;
  2127. if (SCpnt->device->type != TYPE_TAPE
  2128. && ha->retries < MAX_INTERNAL_RETRIES) {
  2129. #if defined(DID_SOFT_ERROR)
  2130. status = DID_SOFT_ERROR << 16;
  2131. #else
  2132. status = DID_BUS_BUSY << 16;
  2133. #endif
  2134. ha->retries++;
  2135. ha->last_retried_pid = SCpnt->serial_number;
  2136. } else
  2137. status = DID_ERROR << 16;
  2138. break;
  2139. case 0x05: /* Unexpected Bus Phase */
  2140. case 0x06: /* Unexpected Bus Free */
  2141. case 0x07: /* Bus Parity Error */
  2142. case 0x08: /* SCSI Hung */
  2143. case 0x09: /* Unexpected Message Reject */
  2144. case 0x0a: /* SCSI Bus Reset Stuck */
  2145. case 0x0b: /* Auto Request-Sense Failed */
  2146. case 0x0c: /* Controller Ram Parity Error */
  2147. default:
  2148. status = DID_ERROR << 16;
  2149. break;
  2150. }
  2151. SCpnt->result = status | spp->target_status;
  2152. #if defined(DEBUG_INTERRUPT)
  2153. if (SCpnt->result || do_trace)
  2154. #else
  2155. if ((spp->adapter_status != ASOK && ha->iocount > 1000) ||
  2156. (spp->adapter_status != ASOK &&
  2157. spp->adapter_status != ASST && ha->iocount <= 1000) ||
  2158. do_trace || msg_byte(spp->target_status))
  2159. #endif
  2160. scmd_printk(KERN_INFO, SCpnt, "ihdlr, mbox %2d, err 0x%x:%x,"
  2161. " reg 0x%x, count %d.\n",
  2162. i, spp->adapter_status, spp->target_status,
  2163. reg, ha->iocount);
  2164. unmap_dma(i, ha);
  2165. /* Set the command state to inactive */
  2166. SCpnt->host_scribble = NULL;
  2167. SCpnt->scsi_done(SCpnt);
  2168. if (do_trace)
  2169. printk("%s: ihdlr, exit, irq %d, count %d.\n", ha->board_name,
  2170. irq, ha->iocount);
  2171. handled:
  2172. return IRQ_HANDLED;
  2173. none:
  2174. return IRQ_NONE;
  2175. }
  2176. static irqreturn_t do_interrupt_handler(int dummy, void *shap)
  2177. {
  2178. struct Scsi_Host *shost;
  2179. unsigned int j;
  2180. unsigned long spin_flags;
  2181. irqreturn_t ret;
  2182. /* Check if the interrupt must be processed by this handler */
  2183. if ((j = (unsigned int)((char *)shap - sha)) >= num_boards)
  2184. return IRQ_NONE;
  2185. shost = sh[j];
  2186. spin_lock_irqsave(shost->host_lock, spin_flags);
  2187. ret = ihdlr(shost);
  2188. spin_unlock_irqrestore(shost->host_lock, spin_flags);
  2189. return ret;
  2190. }
  2191. static int eata2x_release(struct Scsi_Host *shost)
  2192. {
  2193. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  2194. unsigned int i;
  2195. for (i = 0; i < shost->can_queue; i++)
  2196. kfree((&ha->cp[i])->sglist);
  2197. for (i = 0; i < shost->can_queue; i++)
  2198. pci_unmap_single(ha->pdev, ha->cp[i].cp_dma_addr,
  2199. sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
  2200. if (ha->sp_cpu_addr)
  2201. pci_free_consistent(ha->pdev, sizeof(struct mssp),
  2202. ha->sp_cpu_addr, ha->sp_dma_addr);
  2203. free_irq(shost->irq, &sha[ha->board_number]);
  2204. if (shost->dma_channel != NO_DMA)
  2205. free_dma(shost->dma_channel);
  2206. release_region(shost->io_port, shost->n_io_port);
  2207. scsi_unregister(shost);
  2208. return 0;
  2209. }
  2210. #include "scsi_module.c"
  2211. #ifndef MODULE
  2212. __setup("eata=", option_setup);
  2213. #endif /* end MODULE */