eata.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618
  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 corrisponding
  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/config.h>
  479. #include <linux/string.h>
  480. #include <linux/kernel.h>
  481. #include <linux/ioport.h>
  482. #include <linux/delay.h>
  483. #include <linux/proc_fs.h>
  484. #include <linux/blkdev.h>
  485. #include <linux/interrupt.h>
  486. #include <linux/stat.h>
  487. #include <linux/pci.h>
  488. #include <linux/init.h>
  489. #include <linux/ctype.h>
  490. #include <linux/spinlock.h>
  491. #include <asm/byteorder.h>
  492. #include <asm/dma.h>
  493. #include <asm/io.h>
  494. #include <asm/irq.h>
  495. #include <scsi/scsi.h>
  496. #include <scsi/scsi_cmnd.h>
  497. #include <scsi/scsi_device.h>
  498. #include <scsi/scsi_host.h>
  499. #include <scsi/scsi_tcq.h>
  500. #include <scsi/scsicam.h>
  501. static int eata2x_detect(struct scsi_host_template *);
  502. static int eata2x_release(struct Scsi_Host *);
  503. static int eata2x_queuecommand(struct scsi_cmnd *,
  504. void (*done) (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. static DEFINE_SPINLOCK(driver_lock);
  807. /* Initialize num_boards so that ihdlr can work while detect is in progress */
  808. static unsigned int num_boards = MAX_BOARDS;
  809. static unsigned long io_port[] = {
  810. /* Space for MAX_INT_PARAM ports usable while loading as a module */
  811. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  812. SKIP, SKIP,
  813. /* First ISA */
  814. 0x1f0,
  815. /* Space for MAX_PCI ports possibly reported by PCI_BIOS */
  816. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  817. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  818. /* MAX_EISA ports */
  819. 0x1c88, 0x2c88, 0x3c88, 0x4c88, 0x5c88, 0x6c88, 0x7c88, 0x8c88,
  820. 0x9c88, 0xac88, 0xbc88, 0xcc88, 0xdc88, 0xec88, 0xfc88,
  821. /* Other (MAX_ISA - 1) ports */
  822. 0x170, 0x230, 0x330,
  823. /* End of list */
  824. 0x0
  825. };
  826. /* Device is Big Endian */
  827. #define H2DEV(x) cpu_to_be32(x)
  828. #define DEV2H(x) be32_to_cpu(x)
  829. #define H2DEV16(x) cpu_to_be16(x)
  830. #define DEV2H16(x) be16_to_cpu(x)
  831. /* But transfer orientation from the 16 bit data register is Little Endian */
  832. #define REG2H(x) le16_to_cpu(x)
  833. static irqreturn_t do_interrupt_handler(int, void *, struct pt_regs *);
  834. static void flush_dev(struct scsi_device *, unsigned long, struct hostdata *,
  835. unsigned int);
  836. static int do_trace = 0;
  837. static int setup_done = 0;
  838. static int link_statistics;
  839. static int ext_tran = 0;
  840. static int rev_scan = 1;
  841. #if defined(CONFIG_SCSI_EATA_TAGGED_QUEUE)
  842. static int tag_mode = TAG_SIMPLE;
  843. #else
  844. static int tag_mode = TAG_DISABLED;
  845. #endif
  846. #if defined(CONFIG_SCSI_EATA_LINKED_COMMANDS)
  847. static int linked_comm = 1;
  848. #else
  849. static int linked_comm = 0;
  850. #endif
  851. #if defined(CONFIG_SCSI_EATA_MAX_TAGS)
  852. static int max_queue_depth = CONFIG_SCSI_EATA_MAX_TAGS;
  853. #else
  854. static int max_queue_depth = MAX_CMD_PER_LUN;
  855. #endif
  856. #if defined(CONFIG_ISA)
  857. static int isa_probe = 1;
  858. #else
  859. static int isa_probe = 0;
  860. #endif
  861. #if defined(CONFIG_EISA)
  862. static int eisa_probe = 1;
  863. #else
  864. static int eisa_probe = 0;
  865. #endif
  866. #if defined(CONFIG_PCI)
  867. static int pci_probe = 1;
  868. #else
  869. static int pci_probe = 0;
  870. #endif
  871. #define MAX_INT_PARAM 10
  872. #define MAX_BOOT_OPTIONS_SIZE 256
  873. static char boot_options[MAX_BOOT_OPTIONS_SIZE];
  874. #if defined(MODULE)
  875. #include <linux/module.h>
  876. #include <linux/moduleparam.h>
  877. module_param_string(eata, boot_options, MAX_BOOT_OPTIONS_SIZE, 0);
  878. MODULE_PARM_DESC(eata, " equivalent to the \"eata=...\" kernel boot option."
  879. " Example: modprobe eata \"eata=0x7410,0x230,lc:y,tm:0,mq:4,ep:n\"");
  880. MODULE_AUTHOR("Dario Ballabio");
  881. MODULE_LICENSE("GPL");
  882. MODULE_DESCRIPTION("EATA/DMA SCSI Driver");
  883. #endif
  884. static int eata2x_slave_configure(struct scsi_device *dev)
  885. {
  886. int tqd, utqd;
  887. char *tag_suffix, *link_suffix;
  888. struct Scsi_Host *shost = dev->host;
  889. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  890. utqd = MAX_CMD_PER_LUN;
  891. tqd = max_queue_depth;
  892. if (TLDEV(dev->type) && dev->tagged_supported) {
  893. if (tag_mode == TAG_SIMPLE) {
  894. scsi_adjust_queue_depth(dev, MSG_SIMPLE_TAG, tqd);
  895. tag_suffix = ", simple tags";
  896. } else if (tag_mode == TAG_ORDERED) {
  897. scsi_adjust_queue_depth(dev, MSG_ORDERED_TAG, tqd);
  898. tag_suffix = ", ordered tags";
  899. } else {
  900. scsi_adjust_queue_depth(dev, 0, tqd);
  901. tag_suffix = ", no tags";
  902. }
  903. } else if (TLDEV(dev->type) && linked_comm) {
  904. scsi_adjust_queue_depth(dev, 0, tqd);
  905. tag_suffix = ", untagged";
  906. } else {
  907. scsi_adjust_queue_depth(dev, 0, utqd);
  908. tag_suffix = "";
  909. }
  910. if (TLDEV(dev->type) && linked_comm && dev->queue_depth > 2)
  911. link_suffix = ", sorted";
  912. else if (TLDEV(dev->type))
  913. link_suffix = ", unsorted";
  914. else
  915. link_suffix = "";
  916. printk("%s: scsi%d, channel %d, id %d, lun %d, cmds/lun %d%s%s.\n",
  917. ha->board_name, shost->host_no, dev->channel, dev->id, dev->lun,
  918. dev->queue_depth, link_suffix, tag_suffix);
  919. return 0;
  920. }
  921. static int wait_on_busy(unsigned long iobase, unsigned int loop)
  922. {
  923. while (inb(iobase + REG_AUX_STATUS) & ABSY_ASSERTED) {
  924. udelay(1L);
  925. if (--loop == 0)
  926. return 1;
  927. }
  928. return 0;
  929. }
  930. static int do_dma(unsigned long iobase, unsigned long addr, unchar cmd)
  931. {
  932. unsigned char *byaddr;
  933. unsigned long devaddr;
  934. if (wait_on_busy(iobase, (addr ? MAXLOOP * 100 : MAXLOOP)))
  935. return 1;
  936. if (addr) {
  937. devaddr = H2DEV(addr);
  938. byaddr = (unsigned char *)&devaddr;
  939. outb(byaddr[3], iobase + REG_LOW);
  940. outb(byaddr[2], iobase + REG_LM);
  941. outb(byaddr[1], iobase + REG_MID);
  942. outb(byaddr[0], iobase + REG_MSB);
  943. }
  944. outb(cmd, iobase + REG_CMD);
  945. return 0;
  946. }
  947. static int read_pio(unsigned long iobase, ushort * start, ushort * end)
  948. {
  949. unsigned int loop = MAXLOOP;
  950. ushort *p;
  951. for (p = start; p <= end; p++) {
  952. while (!(inb(iobase + REG_STATUS) & DRQ_ASSERTED)) {
  953. udelay(1L);
  954. if (--loop == 0)
  955. return 1;
  956. }
  957. loop = MAXLOOP;
  958. *p = REG2H(inw(iobase));
  959. }
  960. return 0;
  961. }
  962. static struct pci_dev *get_pci_dev(unsigned long port_base)
  963. {
  964. #if defined(CONFIG_PCI)
  965. unsigned int addr;
  966. struct pci_dev *dev = NULL;
  967. while ((dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
  968. addr = pci_resource_start(dev, 0);
  969. #if defined(DEBUG_PCI_DETECT)
  970. printk("%s: get_pci_dev, bus %d, devfn 0x%x, addr 0x%x.\n",
  971. driver_name, dev->bus->number, dev->devfn, addr);
  972. #endif
  973. /* we are in so much trouble for a pci hotplug system with this driver
  974. * anyway, so doing this at least lets people unload the driver and not
  975. * cause memory problems, but in general this is a bad thing to do (this
  976. * driver needs to be converted to the proper PCI api someday... */
  977. pci_dev_put(dev);
  978. if (addr + PCI_BASE_ADDRESS_0 == port_base)
  979. return dev;
  980. }
  981. #endif /* end CONFIG_PCI */
  982. return NULL;
  983. }
  984. static void enable_pci_ports(void)
  985. {
  986. #if defined(CONFIG_PCI)
  987. struct pci_dev *dev = NULL;
  988. while ((dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
  989. #if defined(DEBUG_PCI_DETECT)
  990. printk("%s: enable_pci_ports, bus %d, devfn 0x%x.\n",
  991. driver_name, dev->bus->number, dev->devfn);
  992. #endif
  993. if (pci_enable_device(dev))
  994. printk
  995. ("%s: warning, pci_enable_device failed, bus %d devfn 0x%x.\n",
  996. driver_name, dev->bus->number, dev->devfn);
  997. }
  998. #endif /* end CONFIG_PCI */
  999. }
  1000. static int port_detect(unsigned long port_base, unsigned int j,
  1001. struct scsi_host_template *tpnt)
  1002. {
  1003. unsigned char irq, dma_channel, subversion, i, is_pci = 0;
  1004. unsigned char protocol_rev;
  1005. struct eata_info info;
  1006. char *bus_type, dma_name[16];
  1007. struct pci_dev *pdev;
  1008. /* Allowed DMA channels for ISA (0 indicates reserved) */
  1009. unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
  1010. struct Scsi_Host *shost;
  1011. struct hostdata *ha;
  1012. char name[16];
  1013. sprintf(name, "%s%d", driver_name, j);
  1014. if (!request_region(port_base, REGION_SIZE, driver_name)) {
  1015. #if defined(DEBUG_DETECT)
  1016. printk("%s: address 0x%03lx in use, skipping probe.\n", name,
  1017. port_base);
  1018. #endif
  1019. goto fail;
  1020. }
  1021. spin_lock_irq(&driver_lock);
  1022. if (do_dma(port_base, 0, READ_CONFIG_PIO)) {
  1023. #if defined(DEBUG_DETECT)
  1024. printk("%s: detect, do_dma failed at 0x%03lx.\n", name,
  1025. port_base);
  1026. #endif
  1027. goto freelock;
  1028. }
  1029. /* Read the info structure */
  1030. if (read_pio(port_base, (ushort *) & info, (ushort *) & info.ipad[0])) {
  1031. #if defined(DEBUG_DETECT)
  1032. printk("%s: detect, read_pio failed at 0x%03lx.\n", name,
  1033. port_base);
  1034. #endif
  1035. goto freelock;
  1036. }
  1037. info.data_len = DEV2H(info.data_len);
  1038. info.sign = DEV2H(info.sign);
  1039. info.cp_pad_len = DEV2H16(info.cp_pad_len);
  1040. info.cp_len = DEV2H(info.cp_len);
  1041. info.sp_len = DEV2H(info.sp_len);
  1042. info.scatt_size = DEV2H16(info.scatt_size);
  1043. info.queue_size = DEV2H16(info.queue_size);
  1044. /* Check the controller "EATA" signature */
  1045. if (info.sign != EATA_SIG_BE) {
  1046. #if defined(DEBUG_DETECT)
  1047. printk("%s: signature 0x%04x discarded.\n", name, info.sign);
  1048. #endif
  1049. goto freelock;
  1050. }
  1051. if (info.data_len < EATA_2_0A_SIZE) {
  1052. printk
  1053. ("%s: config structure size (%d bytes) too short, detaching.\n",
  1054. name, info.data_len);
  1055. goto freelock;
  1056. } else if (info.data_len == EATA_2_0A_SIZE)
  1057. protocol_rev = 'A';
  1058. else if (info.data_len == EATA_2_0B_SIZE)
  1059. protocol_rev = 'B';
  1060. else
  1061. protocol_rev = 'C';
  1062. if (protocol_rev != 'A' && info.forcaddr) {
  1063. printk("%s: warning, port address has been forced.\n", name);
  1064. bus_type = "PCI";
  1065. is_pci = 1;
  1066. subversion = ESA;
  1067. } else if (port_base > MAX_EISA_ADDR
  1068. || (protocol_rev == 'C' && info.pci)) {
  1069. bus_type = "PCI";
  1070. is_pci = 1;
  1071. subversion = ESA;
  1072. } else if (port_base >= MIN_EISA_ADDR
  1073. || (protocol_rev == 'C' && info.eisa)) {
  1074. bus_type = "EISA";
  1075. subversion = ESA;
  1076. } else if (protocol_rev == 'C' && !info.eisa && !info.pci) {
  1077. bus_type = "ISA";
  1078. subversion = ISA;
  1079. } else if (port_base > MAX_ISA_ADDR) {
  1080. bus_type = "PCI";
  1081. is_pci = 1;
  1082. subversion = ESA;
  1083. } else {
  1084. bus_type = "ISA";
  1085. subversion = ISA;
  1086. }
  1087. if (!info.haaval || info.ata) {
  1088. printk
  1089. ("%s: address 0x%03lx, unusable %s board (%d%d), detaching.\n",
  1090. name, port_base, bus_type, info.haaval, info.ata);
  1091. goto freelock;
  1092. }
  1093. if (info.drqvld) {
  1094. if (subversion == ESA)
  1095. printk("%s: warning, weird %s board using DMA.\n", name,
  1096. bus_type);
  1097. subversion = ISA;
  1098. dma_channel = dma_channel_table[3 - info.drqx];
  1099. } else {
  1100. if (subversion == ISA)
  1101. printk("%s: warning, weird %s board not using DMA.\n",
  1102. name, bus_type);
  1103. subversion = ESA;
  1104. dma_channel = NO_DMA;
  1105. }
  1106. if (!info.dmasup)
  1107. printk("%s: warning, DMA protocol support not asserted.\n",
  1108. name);
  1109. irq = info.irq;
  1110. if (subversion == ESA && !info.irq_tr)
  1111. printk
  1112. ("%s: warning, LEVEL triggering is suggested for IRQ %u.\n",
  1113. name, irq);
  1114. if (is_pci) {
  1115. pdev = get_pci_dev(port_base);
  1116. if (!pdev)
  1117. printk
  1118. ("%s: warning, failed to get pci_dev structure.\n",
  1119. name);
  1120. } else
  1121. pdev = NULL;
  1122. if (pdev && (irq != pdev->irq)) {
  1123. printk("%s: IRQ %u mapped to IO-APIC IRQ %u.\n", name, irq,
  1124. pdev->irq);
  1125. irq = pdev->irq;
  1126. }
  1127. /* Board detected, allocate its IRQ */
  1128. if (request_irq(irq, do_interrupt_handler,
  1129. SA_INTERRUPT | ((subversion == ESA) ? SA_SHIRQ : 0),
  1130. driver_name, (void *)&sha[j])) {
  1131. printk("%s: unable to allocate IRQ %u, detaching.\n", name,
  1132. irq);
  1133. goto freelock;
  1134. }
  1135. if (subversion == ISA && request_dma(dma_channel, driver_name)) {
  1136. printk("%s: unable to allocate DMA channel %u, detaching.\n",
  1137. name, dma_channel);
  1138. goto freeirq;
  1139. }
  1140. #if defined(FORCE_CONFIG)
  1141. {
  1142. struct eata_config *cf;
  1143. dma_addr_t cf_dma_addr;
  1144. cf = pci_alloc_consistent(pdev, sizeof(struct eata_config),
  1145. &cf_dma_addr);
  1146. if (!cf) {
  1147. printk
  1148. ("%s: config, pci_alloc_consistent failed, detaching.\n",
  1149. name);
  1150. goto freedma;
  1151. }
  1152. /* Set board configuration */
  1153. memset((char *)cf, 0, sizeof(struct eata_config));
  1154. cf->len = (ushort) H2DEV16((ushort) 510);
  1155. cf->ocena = 1;
  1156. if (do_dma(port_base, cf_dma_addr, SET_CONFIG_DMA)) {
  1157. printk
  1158. ("%s: busy timeout sending configuration, detaching.\n",
  1159. name);
  1160. pci_free_consistent(pdev, sizeof(struct eata_config),
  1161. cf, cf_dma_addr);
  1162. goto freedma;
  1163. }
  1164. }
  1165. #endif
  1166. spin_unlock_irq(&driver_lock);
  1167. sh[j] = shost = scsi_register(tpnt, sizeof(struct hostdata));
  1168. spin_lock_irq(&driver_lock);
  1169. if (shost == NULL) {
  1170. printk("%s: unable to register host, detaching.\n", name);
  1171. goto freedma;
  1172. }
  1173. shost->io_port = port_base;
  1174. shost->unique_id = port_base;
  1175. shost->n_io_port = REGION_SIZE;
  1176. shost->dma_channel = dma_channel;
  1177. shost->irq = irq;
  1178. shost->sg_tablesize = (ushort) info.scatt_size;
  1179. shost->this_id = (ushort) info.host_addr[3];
  1180. shost->can_queue = (ushort) info.queue_size;
  1181. shost->cmd_per_lun = MAX_CMD_PER_LUN;
  1182. ha = (struct hostdata *)shost->hostdata;
  1183. memset(ha, 0, sizeof(struct hostdata));
  1184. ha->subversion = subversion;
  1185. ha->protocol_rev = protocol_rev;
  1186. ha->is_pci = is_pci;
  1187. ha->pdev = pdev;
  1188. ha->board_number = j;
  1189. if (ha->subversion == ESA)
  1190. shost->unchecked_isa_dma = 0;
  1191. else {
  1192. unsigned long flags;
  1193. shost->unchecked_isa_dma = 1;
  1194. flags = claim_dma_lock();
  1195. disable_dma(dma_channel);
  1196. clear_dma_ff(dma_channel);
  1197. set_dma_mode(dma_channel, DMA_MODE_CASCADE);
  1198. enable_dma(dma_channel);
  1199. release_dma_lock(flags);
  1200. }
  1201. strcpy(ha->board_name, name);
  1202. /* DPT PM2012 does not allow to detect sg_tablesize correctly */
  1203. if (shost->sg_tablesize > MAX_SGLIST || shost->sg_tablesize < 2) {
  1204. printk("%s: detect, wrong n. of SG lists %d, fixed.\n",
  1205. ha->board_name, shost->sg_tablesize);
  1206. shost->sg_tablesize = MAX_SGLIST;
  1207. }
  1208. /* DPT PM2012 does not allow to detect can_queue correctly */
  1209. if (shost->can_queue > MAX_MAILBOXES || shost->can_queue < 2) {
  1210. printk("%s: detect, wrong n. of mbox %d, fixed.\n",
  1211. ha->board_name, shost->can_queue);
  1212. shost->can_queue = MAX_MAILBOXES;
  1213. }
  1214. if (protocol_rev != 'A') {
  1215. if (info.max_chan > 0 && info.max_chan < MAX_CHANNEL)
  1216. shost->max_channel = info.max_chan;
  1217. if (info.max_id > 7 && info.max_id < MAX_TARGET)
  1218. shost->max_id = info.max_id + 1;
  1219. if (info.large_sg && shost->sg_tablesize == MAX_SGLIST)
  1220. shost->sg_tablesize = MAX_LARGE_SGLIST;
  1221. }
  1222. if (protocol_rev == 'C') {
  1223. if (info.max_lun > 7 && info.max_lun < MAX_LUN)
  1224. shost->max_lun = info.max_lun + 1;
  1225. }
  1226. if (dma_channel == NO_DMA)
  1227. sprintf(dma_name, "%s", "BMST");
  1228. else
  1229. sprintf(dma_name, "DMA %u", dma_channel);
  1230. spin_unlock_irq(&driver_lock);
  1231. for (i = 0; i < shost->can_queue; i++)
  1232. ha->cp[i].cp_dma_addr = pci_map_single(ha->pdev,
  1233. &ha->cp[i],
  1234. sizeof(struct mscp),
  1235. PCI_DMA_BIDIRECTIONAL);
  1236. for (i = 0; i < shost->can_queue; i++) {
  1237. size_t sz = shost->sg_tablesize *sizeof(struct sg_list);
  1238. unsigned int gfp_mask = (shost->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC;
  1239. ha->cp[i].sglist = kmalloc(sz, gfp_mask);
  1240. if (!ha->cp[i].sglist) {
  1241. printk
  1242. ("%s: kmalloc SGlist failed, mbox %d, detaching.\n",
  1243. ha->board_name, i);
  1244. goto release;
  1245. }
  1246. }
  1247. if (!(ha->sp_cpu_addr = pci_alloc_consistent(ha->pdev,
  1248. sizeof(struct mssp),
  1249. &ha->sp_dma_addr))) {
  1250. printk("%s: pci_alloc_consistent failed, detaching.\n", ha->board_name);
  1251. goto release;
  1252. }
  1253. if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
  1254. max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
  1255. if (max_queue_depth < MAX_CMD_PER_LUN)
  1256. max_queue_depth = MAX_CMD_PER_LUN;
  1257. if (tag_mode != TAG_DISABLED && tag_mode != TAG_SIMPLE)
  1258. tag_mode = TAG_ORDERED;
  1259. if (j == 0) {
  1260. printk
  1261. ("EATA/DMA 2.0x: Copyright (C) 1994-2003 Dario Ballabio.\n");
  1262. printk
  1263. ("%s config options -> tm:%d, lc:%c, mq:%d, rs:%c, et:%c, "
  1264. "ip:%c, ep:%c, pp:%c.\n", driver_name, tag_mode,
  1265. YESNO(linked_comm), max_queue_depth, YESNO(rev_scan),
  1266. YESNO(ext_tran), YESNO(isa_probe), YESNO(eisa_probe),
  1267. YESNO(pci_probe));
  1268. }
  1269. printk("%s: 2.0%c, %s 0x%03lx, IRQ %u, %s, SG %d, MB %d.\n",
  1270. ha->board_name, ha->protocol_rev, bus_type,
  1271. (unsigned long)shost->io_port, shost->irq, dma_name,
  1272. shost->sg_tablesize, shost->can_queue);
  1273. if (shost->max_id > 8 || shost->max_lun > 8)
  1274. printk
  1275. ("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
  1276. ha->board_name, shost->max_id, shost->max_lun);
  1277. for (i = 0; i <= shost->max_channel; i++)
  1278. printk("%s: SCSI channel %u enabled, host target ID %d.\n",
  1279. ha->board_name, i, info.host_addr[3 - i]);
  1280. #if defined(DEBUG_DETECT)
  1281. printk("%s: Vers. 0x%x, ocs %u, tar %u, trnxfr %u, more %u, SYNC 0x%x, "
  1282. "sec. %u, infol %d, cpl %d spl %d.\n", name, info.version,
  1283. info.ocsena, info.tarsup, info.trnxfr, info.morsup, info.sync,
  1284. info.second, info.data_len, info.cp_len, info.sp_len);
  1285. if (protocol_rev == 'B' || protocol_rev == 'C')
  1286. printk("%s: isaena %u, forcaddr %u, max_id %u, max_chan %u, "
  1287. "large_sg %u, res1 %u.\n", name, info.isaena,
  1288. info.forcaddr, info.max_id, info.max_chan, info.large_sg,
  1289. info.res1);
  1290. if (protocol_rev == 'C')
  1291. printk("%s: max_lun %u, m1 %u, idquest %u, pci %u, eisa %u, "
  1292. "raidnum %u.\n", name, info.max_lun, info.m1,
  1293. info.idquest, info.pci, info.eisa, info.raidnum);
  1294. #endif
  1295. if (ha->pdev) {
  1296. pci_set_master(ha->pdev);
  1297. if (pci_set_dma_mask(ha->pdev, 0xffffffff))
  1298. printk("%s: warning, pci_set_dma_mask failed.\n",
  1299. ha->board_name);
  1300. }
  1301. return 1;
  1302. freedma:
  1303. if (subversion == ISA)
  1304. free_dma(dma_channel);
  1305. freeirq:
  1306. free_irq(irq, &sha[j]);
  1307. freelock:
  1308. spin_unlock_irq(&driver_lock);
  1309. release_region(port_base, REGION_SIZE);
  1310. fail:
  1311. return 0;
  1312. release:
  1313. eata2x_release(shost);
  1314. return 0;
  1315. }
  1316. static void internal_setup(char *str, int *ints)
  1317. {
  1318. int i, argc = ints[0];
  1319. char *cur = str, *pc;
  1320. if (argc > 0) {
  1321. if (argc > MAX_INT_PARAM)
  1322. argc = MAX_INT_PARAM;
  1323. for (i = 0; i < argc; i++)
  1324. io_port[i] = ints[i + 1];
  1325. io_port[i] = 0;
  1326. setup_done = 1;
  1327. }
  1328. while (cur && (pc = strchr(cur, ':'))) {
  1329. int val = 0, c = *++pc;
  1330. if (c == 'n' || c == 'N')
  1331. val = 0;
  1332. else if (c == 'y' || c == 'Y')
  1333. val = 1;
  1334. else
  1335. val = (int)simple_strtoul(pc, NULL, 0);
  1336. if (!strncmp(cur, "lc:", 3))
  1337. linked_comm = val;
  1338. else if (!strncmp(cur, "tm:", 3))
  1339. tag_mode = val;
  1340. else if (!strncmp(cur, "tc:", 3))
  1341. tag_mode = val;
  1342. else if (!strncmp(cur, "mq:", 3))
  1343. max_queue_depth = val;
  1344. else if (!strncmp(cur, "ls:", 3))
  1345. link_statistics = val;
  1346. else if (!strncmp(cur, "et:", 3))
  1347. ext_tran = val;
  1348. else if (!strncmp(cur, "rs:", 3))
  1349. rev_scan = val;
  1350. else if (!strncmp(cur, "ip:", 3))
  1351. isa_probe = val;
  1352. else if (!strncmp(cur, "ep:", 3))
  1353. eisa_probe = val;
  1354. else if (!strncmp(cur, "pp:", 3))
  1355. pci_probe = val;
  1356. if ((cur = strchr(cur, ',')))
  1357. ++cur;
  1358. }
  1359. return;
  1360. }
  1361. static int option_setup(char *str)
  1362. {
  1363. int ints[MAX_INT_PARAM];
  1364. char *cur = str;
  1365. int i = 1;
  1366. while (cur && isdigit(*cur) && i <= MAX_INT_PARAM) {
  1367. ints[i++] = simple_strtoul(cur, NULL, 0);
  1368. if ((cur = strchr(cur, ',')) != NULL)
  1369. cur++;
  1370. }
  1371. ints[0] = i - 1;
  1372. internal_setup(cur, ints);
  1373. return 1;
  1374. }
  1375. static void add_pci_ports(void)
  1376. {
  1377. #if defined(CONFIG_PCI)
  1378. unsigned int addr, k;
  1379. struct pci_dev *dev = NULL;
  1380. for (k = 0; k < MAX_PCI; k++) {
  1381. if (!(dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev)))
  1382. break;
  1383. if (pci_enable_device(dev)) {
  1384. #if defined(DEBUG_PCI_DETECT)
  1385. printk
  1386. ("%s: detect, bus %d, devfn 0x%x, pci_enable_device failed.\n",
  1387. driver_name, dev->bus->number, dev->devfn);
  1388. #endif
  1389. continue;
  1390. }
  1391. addr = pci_resource_start(dev, 0);
  1392. #if defined(DEBUG_PCI_DETECT)
  1393. printk("%s: detect, seq. %d, bus %d, devfn 0x%x, addr 0x%x.\n",
  1394. driver_name, k, dev->bus->number, dev->devfn, addr);
  1395. #endif
  1396. /* Order addresses according to rev_scan value */
  1397. io_port[MAX_INT_PARAM + (rev_scan ? (MAX_PCI - k) : (1 + k))] =
  1398. addr + PCI_BASE_ADDRESS_0;
  1399. }
  1400. pci_dev_put(dev);
  1401. #endif /* end CONFIG_PCI */
  1402. }
  1403. static int eata2x_detect(struct scsi_host_template *tpnt)
  1404. {
  1405. unsigned int j = 0, k;
  1406. tpnt->proc_name = "eata2x";
  1407. if (strlen(boot_options))
  1408. option_setup(boot_options);
  1409. #if defined(MODULE)
  1410. /* io_port could have been modified when loading as a module */
  1411. if (io_port[0] != SKIP) {
  1412. setup_done = 1;
  1413. io_port[MAX_INT_PARAM] = 0;
  1414. }
  1415. #endif
  1416. for (k = MAX_INT_PARAM; io_port[k]; k++)
  1417. if (io_port[k] == SKIP)
  1418. continue;
  1419. else if (io_port[k] <= MAX_ISA_ADDR) {
  1420. if (!isa_probe)
  1421. io_port[k] = SKIP;
  1422. } else if (io_port[k] >= MIN_EISA_ADDR
  1423. && io_port[k] <= MAX_EISA_ADDR) {
  1424. if (!eisa_probe)
  1425. io_port[k] = SKIP;
  1426. }
  1427. if (pci_probe) {
  1428. if (!setup_done)
  1429. add_pci_ports();
  1430. else
  1431. enable_pci_ports();
  1432. }
  1433. for (k = 0; io_port[k]; k++) {
  1434. if (io_port[k] == SKIP)
  1435. continue;
  1436. if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt))
  1437. j++;
  1438. }
  1439. num_boards = j;
  1440. return j;
  1441. }
  1442. static void map_dma(unsigned int i, struct hostdata *ha)
  1443. {
  1444. unsigned int k, count, pci_dir;
  1445. struct scatterlist *sgpnt;
  1446. struct mscp *cpp;
  1447. struct scsi_cmnd *SCpnt;
  1448. cpp = &ha->cp[i];
  1449. SCpnt = cpp->SCpnt;
  1450. pci_dir = SCpnt->sc_data_direction;
  1451. if (SCpnt->sense_buffer)
  1452. cpp->sense_addr =
  1453. H2DEV(pci_map_single(ha->pdev, SCpnt->sense_buffer,
  1454. sizeof SCpnt->sense_buffer, PCI_DMA_FROMDEVICE));
  1455. cpp->sense_len = sizeof SCpnt->sense_buffer;
  1456. if (!SCpnt->use_sg) {
  1457. /* If we get here with PCI_DMA_NONE, pci_map_single triggers a BUG() */
  1458. if (!SCpnt->request_bufflen)
  1459. pci_dir = PCI_DMA_BIDIRECTIONAL;
  1460. if (SCpnt->request_buffer)
  1461. cpp->data_address = H2DEV(pci_map_single(ha->pdev,
  1462. SCpnt->
  1463. request_buffer,
  1464. SCpnt->
  1465. request_bufflen,
  1466. pci_dir));
  1467. cpp->data_len = H2DEV(SCpnt->request_bufflen);
  1468. return;
  1469. }
  1470. sgpnt = (struct scatterlist *)SCpnt->request_buffer;
  1471. count = pci_map_sg(ha->pdev, sgpnt, SCpnt->use_sg, pci_dir);
  1472. for (k = 0; k < count; k++) {
  1473. cpp->sglist[k].address = H2DEV(sg_dma_address(&sgpnt[k]));
  1474. cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(&sgpnt[k]));
  1475. }
  1476. cpp->sg = 1;
  1477. cpp->data_address = H2DEV(pci_map_single(ha->pdev, cpp->sglist,
  1478. SCpnt->use_sg *
  1479. sizeof(struct sg_list),
  1480. pci_dir));
  1481. cpp->data_len = H2DEV((SCpnt->use_sg * sizeof(struct sg_list)));
  1482. }
  1483. static void unmap_dma(unsigned int i, struct hostdata *ha)
  1484. {
  1485. unsigned int pci_dir;
  1486. struct mscp *cpp;
  1487. struct scsi_cmnd *SCpnt;
  1488. cpp = &ha->cp[i];
  1489. SCpnt = cpp->SCpnt;
  1490. pci_dir = SCpnt->sc_data_direction;
  1491. if (DEV2H(cpp->sense_addr))
  1492. pci_unmap_single(ha->pdev, DEV2H(cpp->sense_addr),
  1493. DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
  1494. if (SCpnt->use_sg)
  1495. pci_unmap_sg(ha->pdev, SCpnt->request_buffer, SCpnt->use_sg,
  1496. pci_dir);
  1497. if (!DEV2H(cpp->data_len))
  1498. pci_dir = PCI_DMA_BIDIRECTIONAL;
  1499. if (DEV2H(cpp->data_address))
  1500. pci_unmap_single(ha->pdev, DEV2H(cpp->data_address),
  1501. DEV2H(cpp->data_len), pci_dir);
  1502. }
  1503. static void sync_dma(unsigned int i, struct hostdata *ha)
  1504. {
  1505. unsigned int pci_dir;
  1506. struct mscp *cpp;
  1507. struct scsi_cmnd *SCpnt;
  1508. cpp = &ha->cp[i];
  1509. SCpnt = cpp->SCpnt;
  1510. pci_dir = SCpnt->sc_data_direction;
  1511. if (DEV2H(cpp->sense_addr))
  1512. pci_dma_sync_single_for_cpu(ha->pdev, DEV2H(cpp->sense_addr),
  1513. DEV2H(cpp->sense_len),
  1514. PCI_DMA_FROMDEVICE);
  1515. if (SCpnt->use_sg)
  1516. pci_dma_sync_sg_for_cpu(ha->pdev, SCpnt->request_buffer,
  1517. SCpnt->use_sg, pci_dir);
  1518. if (!DEV2H(cpp->data_len))
  1519. pci_dir = PCI_DMA_BIDIRECTIONAL;
  1520. if (DEV2H(cpp->data_address))
  1521. pci_dma_sync_single_for_cpu(ha->pdev,
  1522. DEV2H(cpp->data_address),
  1523. DEV2H(cpp->data_len), pci_dir);
  1524. }
  1525. static void scsi_to_dev_dir(unsigned int i, struct hostdata *ha)
  1526. {
  1527. unsigned int k;
  1528. static const unsigned char data_out_cmds[] = {
  1529. 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
  1530. 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
  1531. 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
  1532. };
  1533. static const unsigned char data_none_cmds[] = {
  1534. 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
  1535. 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
  1536. 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
  1537. };
  1538. struct mscp *cpp;
  1539. struct scsi_cmnd *SCpnt;
  1540. cpp = &ha->cp[i];
  1541. SCpnt = cpp->SCpnt;
  1542. if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1543. cpp->din = 1;
  1544. cpp->dout = 0;
  1545. return;
  1546. } else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
  1547. cpp->din = 0;
  1548. cpp->dout = 1;
  1549. return;
  1550. } else if (SCpnt->sc_data_direction == DMA_NONE) {
  1551. cpp->din = 0;
  1552. cpp->dout = 0;
  1553. return;
  1554. }
  1555. if (SCpnt->sc_data_direction != DMA_BIDIRECTIONAL)
  1556. panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n",
  1557. ha->board_name);
  1558. for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
  1559. if (SCpnt->cmnd[0] == data_out_cmds[k]) {
  1560. cpp->dout = 1;
  1561. break;
  1562. }
  1563. if ((cpp->din = !cpp->dout))
  1564. for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
  1565. if (SCpnt->cmnd[0] == data_none_cmds[k]) {
  1566. cpp->din = 0;
  1567. break;
  1568. }
  1569. }
  1570. static int eata2x_queuecommand(struct scsi_cmnd *SCpnt,
  1571. void (*done) (struct scsi_cmnd *))
  1572. {
  1573. struct Scsi_Host *shost = SCpnt->device->host;
  1574. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  1575. unsigned int i, k;
  1576. struct mscp *cpp;
  1577. if (SCpnt->host_scribble)
  1578. panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
  1579. ha->board_name, SCpnt->pid, SCpnt);
  1580. /* i is the mailbox number, look for the first free mailbox
  1581. starting from last_cp_used */
  1582. i = ha->last_cp_used + 1;
  1583. for (k = 0; k < shost->can_queue; k++, i++) {
  1584. if (i >= shost->can_queue)
  1585. i = 0;
  1586. if (ha->cp_stat[i] == FREE) {
  1587. ha->last_cp_used = i;
  1588. break;
  1589. }
  1590. }
  1591. if (k == shost->can_queue) {
  1592. printk("%s: qcomm, no free mailbox.\n", ha->board_name);
  1593. return 1;
  1594. }
  1595. /* Set pointer to control packet structure */
  1596. cpp = &ha->cp[i];
  1597. memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
  1598. /* Set pointer to status packet structure, Big Endian format */
  1599. cpp->sp_dma_addr = H2DEV(ha->sp_dma_addr);
  1600. SCpnt->scsi_done = done;
  1601. cpp->cpp_index = i;
  1602. SCpnt->host_scribble = (unsigned char *)&cpp->cpp_index;
  1603. if (do_trace)
  1604. printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
  1605. ha->board_name, i, SCpnt->device->channel, SCpnt->device->id,
  1606. SCpnt->device->lun, SCpnt->pid);
  1607. cpp->reqsen = 1;
  1608. cpp->dispri = 1;
  1609. #if 0
  1610. if (SCpnt->device->type == TYPE_TAPE)
  1611. cpp->hbaci = 1;
  1612. #endif
  1613. cpp->one = 1;
  1614. cpp->channel = SCpnt->device->channel;
  1615. cpp->target = SCpnt->device->id;
  1616. cpp->lun = SCpnt->device->lun;
  1617. cpp->SCpnt = SCpnt;
  1618. memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
  1619. /* Use data transfer direction SCpnt->sc_data_direction */
  1620. scsi_to_dev_dir(i, ha);
  1621. /* Map DMA buffers and SG list */
  1622. map_dma(i, ha);
  1623. if (linked_comm && SCpnt->device->queue_depth > 2
  1624. && TLDEV(SCpnt->device->type)) {
  1625. ha->cp_stat[i] = READY;
  1626. flush_dev(SCpnt->device, SCpnt->request->sector, ha, 0);
  1627. return 0;
  1628. }
  1629. /* Send control packet to the board */
  1630. if (do_dma(shost->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
  1631. unmap_dma(i, ha);
  1632. SCpnt->host_scribble = NULL;
  1633. printk("%s: qcomm, target %d.%d:%d, pid %ld, adapter busy.\n",
  1634. ha->board_name, SCpnt->device->channel, SCpnt->device->id,
  1635. SCpnt->device->lun, SCpnt->pid);
  1636. return 1;
  1637. }
  1638. ha->cp_stat[i] = IN_USE;
  1639. return 0;
  1640. }
  1641. static int eata2x_eh_abort(struct scsi_cmnd *SCarg)
  1642. {
  1643. struct Scsi_Host *shost = SCarg->device->host;
  1644. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  1645. unsigned int i;
  1646. if (SCarg->host_scribble == NULL) {
  1647. printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
  1648. ha->board_name, SCarg->device->channel, SCarg->device->id,
  1649. SCarg->device->lun, SCarg->pid);
  1650. return SUCCESS;
  1651. }
  1652. i = *(unsigned int *)SCarg->host_scribble;
  1653. printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
  1654. ha->board_name, i, SCarg->device->channel, SCarg->device->id,
  1655. SCarg->device->lun, SCarg->pid);
  1656. if (i >= shost->can_queue)
  1657. panic("%s: abort, invalid SCarg->host_scribble.\n", ha->board_name);
  1658. if (wait_on_busy(shost->io_port, MAXLOOP)) {
  1659. printk("%s: abort, timeout error.\n", ha->board_name);
  1660. return FAILED;
  1661. }
  1662. if (ha->cp_stat[i] == FREE) {
  1663. printk("%s: abort, mbox %d is free.\n", ha->board_name, i);
  1664. return SUCCESS;
  1665. }
  1666. if (ha->cp_stat[i] == IN_USE) {
  1667. printk("%s: abort, mbox %d is in use.\n", ha->board_name, i);
  1668. if (SCarg != ha->cp[i].SCpnt)
  1669. panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
  1670. ha->board_name, i, SCarg, ha->cp[i].SCpnt);
  1671. if (inb(shost->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)
  1672. printk("%s: abort, mbox %d, interrupt pending.\n",
  1673. ha->board_name, i);
  1674. return FAILED;
  1675. }
  1676. if (ha->cp_stat[i] == IN_RESET) {
  1677. printk("%s: abort, mbox %d is in reset.\n", ha->board_name, i);
  1678. return FAILED;
  1679. }
  1680. if (ha->cp_stat[i] == LOCKED) {
  1681. printk("%s: abort, mbox %d is locked.\n", ha->board_name, i);
  1682. return SUCCESS;
  1683. }
  1684. if (ha->cp_stat[i] == READY || ha->cp_stat[i] == ABORTING) {
  1685. unmap_dma(i, ha);
  1686. SCarg->result = DID_ABORT << 16;
  1687. SCarg->host_scribble = NULL;
  1688. ha->cp_stat[i] = FREE;
  1689. printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
  1690. ha->board_name, i, SCarg->pid);
  1691. SCarg->scsi_done(SCarg);
  1692. return SUCCESS;
  1693. }
  1694. panic("%s: abort, mbox %d, invalid cp_stat.\n", ha->board_name, i);
  1695. }
  1696. static int eata2x_eh_host_reset(struct scsi_cmnd *SCarg)
  1697. {
  1698. unsigned int i, time, k, c, limit = 0;
  1699. int arg_done = 0;
  1700. struct scsi_cmnd *SCpnt;
  1701. struct Scsi_Host *shost = SCarg->device->host;
  1702. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  1703. printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
  1704. ha->board_name, SCarg->device->channel, SCarg->device->id,
  1705. SCarg->device->lun, SCarg->pid);
  1706. spin_lock_irq(shost->host_lock);
  1707. if (SCarg->host_scribble == NULL)
  1708. printk("%s: reset, pid %ld inactive.\n", ha->board_name, SCarg->pid);
  1709. if (ha->in_reset) {
  1710. printk("%s: reset, exit, already in reset.\n", ha->board_name);
  1711. spin_unlock_irq(shost->host_lock);
  1712. return FAILED;
  1713. }
  1714. if (wait_on_busy(shost->io_port, MAXLOOP)) {
  1715. printk("%s: reset, exit, timeout error.\n", ha->board_name);
  1716. spin_unlock_irq(shost->host_lock);
  1717. return FAILED;
  1718. }
  1719. ha->retries = 0;
  1720. for (c = 0; c <= shost->max_channel; c++)
  1721. for (k = 0; k < shost->max_id; k++) {
  1722. ha->target_redo[k][c] = 1;
  1723. ha->target_to[k][c] = 0;
  1724. }
  1725. for (i = 0; i < shost->can_queue; i++) {
  1726. if (ha->cp_stat[i] == FREE)
  1727. continue;
  1728. if (ha->cp_stat[i] == LOCKED) {
  1729. ha->cp_stat[i] = FREE;
  1730. printk("%s: reset, locked mbox %d forced free.\n",
  1731. ha->board_name, i);
  1732. continue;
  1733. }
  1734. if (!(SCpnt = ha->cp[i].SCpnt))
  1735. panic("%s: reset, mbox %d, SCpnt == NULL.\n", ha->board_name, i);
  1736. if (ha->cp_stat[i] == READY || ha->cp_stat[i] == ABORTING) {
  1737. ha->cp_stat[i] = ABORTING;
  1738. printk("%s: reset, mbox %d aborting, pid %ld.\n",
  1739. ha->board_name, i, SCpnt->pid);
  1740. }
  1741. else {
  1742. ha->cp_stat[i] = IN_RESET;
  1743. printk("%s: reset, mbox %d in reset, pid %ld.\n",
  1744. ha->board_name, i, SCpnt->pid);
  1745. }
  1746. if (SCpnt->host_scribble == NULL)
  1747. panic("%s: reset, mbox %d, garbled SCpnt.\n", ha->board_name, i);
  1748. if (*(unsigned int *)SCpnt->host_scribble != i)
  1749. panic("%s: reset, mbox %d, index mismatch.\n", ha->board_name, i);
  1750. if (SCpnt->scsi_done == NULL)
  1751. panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n",
  1752. ha->board_name, i);
  1753. if (SCpnt == SCarg)
  1754. arg_done = 1;
  1755. }
  1756. if (do_dma(shost->io_port, 0, RESET_PIO)) {
  1757. printk("%s: reset, cannot reset, timeout error.\n", ha->board_name);
  1758. spin_unlock_irq(shost->host_lock);
  1759. return FAILED;
  1760. }
  1761. printk("%s: reset, board reset done, enabling interrupts.\n", ha->board_name);
  1762. #if defined(DEBUG_RESET)
  1763. do_trace = 1;
  1764. #endif
  1765. ha->in_reset = 1;
  1766. spin_unlock_irq(shost->host_lock);
  1767. /* FIXME: use a sleep instead */
  1768. time = jiffies;
  1769. while ((jiffies - time) < (10 * HZ) && limit++ < 200000)
  1770. udelay(100L);
  1771. spin_lock_irq(shost->host_lock);
  1772. printk("%s: reset, interrupts disabled, loops %d.\n", ha->board_name, limit);
  1773. for (i = 0; i < shost->can_queue; i++) {
  1774. if (ha->cp_stat[i] == IN_RESET) {
  1775. SCpnt = ha->cp[i].SCpnt;
  1776. unmap_dma(i, ha);
  1777. SCpnt->result = DID_RESET << 16;
  1778. SCpnt->host_scribble = NULL;
  1779. /* This mailbox is still waiting for its interrupt */
  1780. ha->cp_stat[i] = LOCKED;
  1781. printk
  1782. ("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
  1783. ha->board_name, i, SCpnt->pid);
  1784. }
  1785. else if (ha->cp_stat[i] == ABORTING) {
  1786. SCpnt = ha->cp[i].SCpnt;
  1787. unmap_dma(i, ha);
  1788. SCpnt->result = DID_RESET << 16;
  1789. SCpnt->host_scribble = NULL;
  1790. /* This mailbox was never queued to the adapter */
  1791. ha->cp_stat[i] = FREE;
  1792. printk
  1793. ("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
  1794. ha->board_name, i, SCpnt->pid);
  1795. }
  1796. else
  1797. /* Any other mailbox has already been set free by interrupt */
  1798. continue;
  1799. SCpnt->scsi_done(SCpnt);
  1800. }
  1801. ha->in_reset = 0;
  1802. do_trace = 0;
  1803. if (arg_done)
  1804. printk("%s: reset, exit, pid %ld done.\n", ha->board_name, SCarg->pid);
  1805. else
  1806. printk("%s: reset, exit.\n", ha->board_name);
  1807. spin_unlock_irq(shost->host_lock);
  1808. return SUCCESS;
  1809. }
  1810. int eata2x_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1811. sector_t capacity, int *dkinfo)
  1812. {
  1813. unsigned int size = capacity;
  1814. if (ext_tran || (scsicam_bios_param(bdev, capacity, dkinfo) < 0)) {
  1815. dkinfo[0] = 255;
  1816. dkinfo[1] = 63;
  1817. dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
  1818. }
  1819. #if defined (DEBUG_GEOMETRY)
  1820. printk("%s: bios_param, head=%d, sec=%d, cyl=%d.\n", driver_name,
  1821. dkinfo[0], dkinfo[1], dkinfo[2]);
  1822. #endif
  1823. return 0;
  1824. }
  1825. static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
  1826. unsigned int rev)
  1827. {
  1828. unsigned int i, j, k, y;
  1829. unsigned long x;
  1830. for (i = 0; i < n - 1; i++) {
  1831. k = i;
  1832. for (j = k + 1; j < n; j++)
  1833. if (rev) {
  1834. if (sk[j] > sk[k])
  1835. k = j;
  1836. } else {
  1837. if (sk[j] < sk[k])
  1838. k = j;
  1839. }
  1840. if (k != i) {
  1841. x = sk[k];
  1842. sk[k] = sk[i];
  1843. sk[i] = x;
  1844. y = da[k];
  1845. da[k] = da[i];
  1846. da[i] = y;
  1847. }
  1848. }
  1849. return;
  1850. }
  1851. static int reorder(struct hostdata *ha, unsigned long cursec,
  1852. unsigned int ihdlr, unsigned int il[], unsigned int n_ready)
  1853. {
  1854. struct scsi_cmnd *SCpnt;
  1855. struct mscp *cpp;
  1856. unsigned int k, n;
  1857. unsigned int rev = 0, s = 1, r = 1;
  1858. unsigned int input_only = 1, overlap = 0;
  1859. unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
  1860. unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
  1861. unsigned long ioseek = 0;
  1862. static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
  1863. static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
  1864. static unsigned int readysorted = 0, revcount = 0;
  1865. static unsigned long seeksorted = 0, seeknosort = 0;
  1866. if (link_statistics && !(++flushcount % link_statistics))
  1867. printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"
  1868. " av %ldK as %ldK.\n", flushcount, batchcount,
  1869. inputcount, ovlcount, readycount, readysorted, sortcount,
  1870. revcount, seeknosort / (readycount + 1),
  1871. seeksorted / (readycount + 1));
  1872. if (n_ready <= 1)
  1873. return 0;
  1874. for (n = 0; n < n_ready; n++) {
  1875. k = il[n];
  1876. cpp = &ha->cp[k];
  1877. SCpnt = cpp->SCpnt;
  1878. if (!cpp->din)
  1879. input_only = 0;
  1880. if (SCpnt->request->sector < minsec)
  1881. minsec = SCpnt->request->sector;
  1882. if (SCpnt->request->sector > maxsec)
  1883. maxsec = SCpnt->request->sector;
  1884. sl[n] = SCpnt->request->sector;
  1885. ioseek += SCpnt->request->nr_sectors;
  1886. if (!n)
  1887. continue;
  1888. if (sl[n] < sl[n - 1])
  1889. s = 0;
  1890. if (sl[n] > sl[n - 1])
  1891. r = 0;
  1892. if (link_statistics) {
  1893. if (sl[n] > sl[n - 1])
  1894. seek += sl[n] - sl[n - 1];
  1895. else
  1896. seek += sl[n - 1] - sl[n];
  1897. }
  1898. }
  1899. if (link_statistics) {
  1900. if (cursec > sl[0])
  1901. seek += cursec - sl[0];
  1902. else
  1903. seek += sl[0] - cursec;
  1904. }
  1905. if (cursec > ((maxsec + minsec) / 2))
  1906. rev = 1;
  1907. if (ioseek > ((maxsec - minsec) / 2))
  1908. rev = 0;
  1909. if (!((rev && r) || (!rev && s)))
  1910. sort(sl, il, n_ready, rev);
  1911. if (!input_only)
  1912. for (n = 0; n < n_ready; n++) {
  1913. k = il[n];
  1914. cpp = &ha->cp[k];
  1915. SCpnt = cpp->SCpnt;
  1916. ll[n] = SCpnt->request->nr_sectors;
  1917. pl[n] = SCpnt->pid;
  1918. if (!n)
  1919. continue;
  1920. if ((sl[n] == sl[n - 1])
  1921. || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
  1922. || (rev && ((sl[n] + ll[n]) > sl[n - 1])))
  1923. overlap = 1;
  1924. }
  1925. if (overlap)
  1926. sort(pl, il, n_ready, 0);
  1927. if (link_statistics) {
  1928. if (cursec > sl[0])
  1929. iseek = cursec - sl[0];
  1930. else
  1931. iseek = sl[0] - cursec;
  1932. batchcount++;
  1933. readycount += n_ready;
  1934. seeknosort += seek / 1024;
  1935. if (input_only)
  1936. inputcount++;
  1937. if (overlap) {
  1938. ovlcount++;
  1939. seeksorted += iseek / 1024;
  1940. } else
  1941. seeksorted += (iseek + maxsec - minsec) / 1024;
  1942. if (rev && !r) {
  1943. revcount++;
  1944. readysorted += n_ready;
  1945. }
  1946. if (!rev && !s) {
  1947. sortcount++;
  1948. readysorted += n_ready;
  1949. }
  1950. }
  1951. #if defined(DEBUG_LINKED_COMMANDS)
  1952. if (link_statistics && (overlap || !(flushcount % link_statistics)))
  1953. for (n = 0; n < n_ready; n++) {
  1954. k = il[n];
  1955. cpp = &ha->cp[k];
  1956. SCpnt = cpp->SCpnt;
  1957. printk
  1958. ("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %ld"
  1959. " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
  1960. (ihdlr ? "ihdlr" : "qcomm"),
  1961. SCpnt->device->channel, SCpnt->device->id,
  1962. SCpnt->device->lun, SCpnt->pid, k, flushcount,
  1963. n_ready, SCpnt->request->sector,
  1964. SCpnt->request->nr_sectors, cursec, YESNO(s),
  1965. YESNO(r), YESNO(rev), YESNO(input_only),
  1966. YESNO(overlap), cpp->din);
  1967. }
  1968. #endif
  1969. return overlap;
  1970. }
  1971. static void flush_dev(struct scsi_device *dev, unsigned long cursec,
  1972. struct hostdata *ha, unsigned int ihdlr)
  1973. {
  1974. struct scsi_cmnd *SCpnt;
  1975. struct mscp *cpp;
  1976. unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
  1977. for (k = 0; k < dev->host->can_queue; k++) {
  1978. if (ha->cp_stat[k] != READY && ha->cp_stat[k] != IN_USE)
  1979. continue;
  1980. cpp = &ha->cp[k];
  1981. SCpnt = cpp->SCpnt;
  1982. if (SCpnt->device != dev)
  1983. continue;
  1984. if (ha->cp_stat[k] == IN_USE)
  1985. return;
  1986. il[n_ready++] = k;
  1987. }
  1988. if (reorder(ha, cursec, ihdlr, il, n_ready))
  1989. n_ready = 1;
  1990. for (n = 0; n < n_ready; n++) {
  1991. k = il[n];
  1992. cpp = &ha->cp[k];
  1993. SCpnt = cpp->SCpnt;
  1994. if (do_dma(dev->host->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
  1995. printk
  1996. ("%s: %s, target %d.%d:%d, pid %ld, mbox %d, adapter"
  1997. " busy, will abort.\n", ha->board_name,
  1998. (ihdlr ? "ihdlr" : "qcomm"),
  1999. SCpnt->device->channel, SCpnt->device->id,
  2000. SCpnt->device->lun, SCpnt->pid, k);
  2001. ha->cp_stat[k] = ABORTING;
  2002. continue;
  2003. }
  2004. ha->cp_stat[k] = IN_USE;
  2005. }
  2006. }
  2007. static irqreturn_t ihdlr(int irq, struct Scsi_Host *shost)
  2008. {
  2009. struct scsi_cmnd *SCpnt;
  2010. unsigned int i, k, c, status, tstatus, reg;
  2011. struct mssp *spp;
  2012. struct mscp *cpp;
  2013. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  2014. if (shost->irq != irq)
  2015. panic("%s: ihdlr, irq %d, shost->irq %d.\n", ha->board_name, irq,
  2016. shost->irq);
  2017. /* Check if this board need to be serviced */
  2018. if (!(inb(shost->io_port + REG_AUX_STATUS) & IRQ_ASSERTED))
  2019. goto none;
  2020. ha->iocount++;
  2021. if (do_trace)
  2022. printk("%s: ihdlr, enter, irq %d, count %d.\n", ha->board_name, irq,
  2023. ha->iocount);
  2024. /* Check if this board is still busy */
  2025. if (wait_on_busy(shost->io_port, 20 * MAXLOOP)) {
  2026. reg = inb(shost->io_port + REG_STATUS);
  2027. printk
  2028. ("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
  2029. ha->board_name, irq, reg, ha->iocount);
  2030. goto none;
  2031. }
  2032. spp = &ha->sp;
  2033. /* Make a local copy just before clearing the interrupt indication */
  2034. memcpy(spp, ha->sp_cpu_addr, sizeof(struct mssp));
  2035. /* Clear the completion flag and cp pointer on the dynamic copy of sp */
  2036. memset(ha->sp_cpu_addr, 0, sizeof(struct mssp));
  2037. /* Read the status register to clear the interrupt indication */
  2038. reg = inb(shost->io_port + REG_STATUS);
  2039. #if defined (DEBUG_INTERRUPT)
  2040. {
  2041. unsigned char *bytesp;
  2042. int cnt;
  2043. bytesp = (unsigned char *)spp;
  2044. if (ha->iocount < 200) {
  2045. printk("sp[] =");
  2046. for (cnt = 0; cnt < 15; cnt++)
  2047. printk(" 0x%x", bytesp[cnt]);
  2048. printk("\n");
  2049. }
  2050. }
  2051. #endif
  2052. /* Reject any sp with supspect data */
  2053. if (spp->eoc == 0 && ha->iocount > 1)
  2054. printk
  2055. ("%s: ihdlr, spp->eoc == 0, irq %d, reg 0x%x, count %d.\n",
  2056. ha->board_name, irq, reg, ha->iocount);
  2057. if (spp->cpp_index < 0 || spp->cpp_index >= shost->can_queue)
  2058. printk
  2059. ("%s: ihdlr, bad spp->cpp_index %d, irq %d, reg 0x%x, count %d.\n",
  2060. ha->board_name, spp->cpp_index, irq, reg, ha->iocount);
  2061. if (spp->eoc == 0 || spp->cpp_index < 0
  2062. || spp->cpp_index >= shost->can_queue)
  2063. goto handled;
  2064. /* Find the mailbox to be serviced on this board */
  2065. i = spp->cpp_index;
  2066. cpp = &(ha->cp[i]);
  2067. #if defined(DEBUG_GENERATE_ABORTS)
  2068. if ((ha->iocount > 500) && ((ha->iocount % 500) < 3))
  2069. goto handled;
  2070. #endif
  2071. if (ha->cp_stat[i] == IGNORE) {
  2072. ha->cp_stat[i] = FREE;
  2073. goto handled;
  2074. } else if (ha->cp_stat[i] == LOCKED) {
  2075. ha->cp_stat[i] = FREE;
  2076. printk("%s: ihdlr, mbox %d unlocked, count %d.\n", ha->board_name, i,
  2077. ha->iocount);
  2078. goto handled;
  2079. } else if (ha->cp_stat[i] == FREE) {
  2080. printk("%s: ihdlr, mbox %d is free, count %d.\n", ha->board_name, i,
  2081. ha->iocount);
  2082. goto handled;
  2083. } else if (ha->cp_stat[i] == IN_RESET)
  2084. printk("%s: ihdlr, mbox %d is in reset.\n", ha->board_name, i);
  2085. else if (ha->cp_stat[i] != IN_USE)
  2086. panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
  2087. ha->board_name, i, ha->cp_stat[i]);
  2088. ha->cp_stat[i] = FREE;
  2089. SCpnt = cpp->SCpnt;
  2090. if (SCpnt == NULL)
  2091. panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", ha->board_name, i);
  2092. if (SCpnt->host_scribble == NULL)
  2093. panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", ha->board_name,
  2094. i, SCpnt->pid, SCpnt);
  2095. if (*(unsigned int *)SCpnt->host_scribble != i)
  2096. panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
  2097. ha->board_name, i, SCpnt->pid,
  2098. *(unsigned int *)SCpnt->host_scribble);
  2099. sync_dma(i, ha);
  2100. if (linked_comm && SCpnt->device->queue_depth > 2
  2101. && TLDEV(SCpnt->device->type))
  2102. flush_dev(SCpnt->device, SCpnt->request->sector, ha, 1);
  2103. tstatus = status_byte(spp->target_status);
  2104. #if defined(DEBUG_GENERATE_ERRORS)
  2105. if ((ha->iocount > 500) && ((ha->iocount % 200) < 2))
  2106. spp->adapter_status = 0x01;
  2107. #endif
  2108. switch (spp->adapter_status) {
  2109. case ASOK: /* status OK */
  2110. /* Forces a reset if a disk drive keeps returning BUSY */
  2111. if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
  2112. status = DID_ERROR << 16;
  2113. /* If there was a bus reset, redo operation on each target */
  2114. else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
  2115. && ha->target_redo[SCpnt->device->id][SCpnt->
  2116. device->
  2117. channel])
  2118. status = DID_BUS_BUSY << 16;
  2119. /* Works around a flaw in scsi.c */
  2120. else if (tstatus == CHECK_CONDITION
  2121. && SCpnt->device->type == TYPE_DISK
  2122. && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
  2123. status = DID_BUS_BUSY << 16;
  2124. else
  2125. status = DID_OK << 16;
  2126. if (tstatus == GOOD)
  2127. ha->target_redo[SCpnt->device->id][SCpnt->device->
  2128. channel] = 0;
  2129. if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
  2130. (!(tstatus == CHECK_CONDITION && ha->iocount <= 1000 &&
  2131. (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
  2132. printk("%s: ihdlr, target %d.%d:%d, pid %ld, "
  2133. "target_status 0x%x, sense key 0x%x.\n",
  2134. ha->board_name,
  2135. SCpnt->device->channel, SCpnt->device->id,
  2136. SCpnt->device->lun, SCpnt->pid,
  2137. spp->target_status, SCpnt->sense_buffer[2]);
  2138. ha->target_to[SCpnt->device->id][SCpnt->device->channel] = 0;
  2139. if (ha->last_retried_pid == SCpnt->pid)
  2140. ha->retries = 0;
  2141. break;
  2142. case ASST: /* Selection Time Out */
  2143. case 0x02: /* Command Time Out */
  2144. if (ha->target_to[SCpnt->device->id][SCpnt->device->channel] > 1)
  2145. status = DID_ERROR << 16;
  2146. else {
  2147. status = DID_TIME_OUT << 16;
  2148. ha->target_to[SCpnt->device->id][SCpnt->device->
  2149. channel]++;
  2150. }
  2151. break;
  2152. /* Perform a limited number of internal retries */
  2153. case 0x03: /* SCSI Bus Reset Received */
  2154. case 0x04: /* Initial Controller Power-up */
  2155. for (c = 0; c <= shost->max_channel; c++)
  2156. for (k = 0; k < shost->max_id; k++)
  2157. ha->target_redo[k][c] = 1;
  2158. if (SCpnt->device->type != TYPE_TAPE
  2159. && ha->retries < MAX_INTERNAL_RETRIES) {
  2160. #if defined(DID_SOFT_ERROR)
  2161. status = DID_SOFT_ERROR << 16;
  2162. #else
  2163. status = DID_BUS_BUSY << 16;
  2164. #endif
  2165. ha->retries++;
  2166. ha->last_retried_pid = SCpnt->pid;
  2167. } else
  2168. status = DID_ERROR << 16;
  2169. break;
  2170. case 0x05: /* Unexpected Bus Phase */
  2171. case 0x06: /* Unexpected Bus Free */
  2172. case 0x07: /* Bus Parity Error */
  2173. case 0x08: /* SCSI Hung */
  2174. case 0x09: /* Unexpected Message Reject */
  2175. case 0x0a: /* SCSI Bus Reset Stuck */
  2176. case 0x0b: /* Auto Request-Sense Failed */
  2177. case 0x0c: /* Controller Ram Parity Error */
  2178. default:
  2179. status = DID_ERROR << 16;
  2180. break;
  2181. }
  2182. SCpnt->result = status | spp->target_status;
  2183. #if defined(DEBUG_INTERRUPT)
  2184. if (SCpnt->result || do_trace)
  2185. #else
  2186. if ((spp->adapter_status != ASOK && ha->iocount > 1000) ||
  2187. (spp->adapter_status != ASOK &&
  2188. spp->adapter_status != ASST && ha->iocount <= 1000) ||
  2189. do_trace || msg_byte(spp->target_status))
  2190. #endif
  2191. printk("%s: ihdlr, mbox %2d, err 0x%x:%x,"
  2192. " target %d.%d:%d, pid %ld, reg 0x%x, count %d.\n",
  2193. ha->board_name, i, spp->adapter_status, spp->target_status,
  2194. SCpnt->device->channel, SCpnt->device->id,
  2195. SCpnt->device->lun, SCpnt->pid, reg, ha->iocount);
  2196. unmap_dma(i, ha);
  2197. /* Set the command state to inactive */
  2198. SCpnt->host_scribble = NULL;
  2199. SCpnt->scsi_done(SCpnt);
  2200. if (do_trace)
  2201. printk("%s: ihdlr, exit, irq %d, count %d.\n", ha->board_name,
  2202. irq, ha->iocount);
  2203. handled:
  2204. return IRQ_HANDLED;
  2205. none:
  2206. return IRQ_NONE;
  2207. }
  2208. static irqreturn_t do_interrupt_handler(int irq, void *shap,
  2209. struct pt_regs *regs)
  2210. {
  2211. struct Scsi_Host *shost;
  2212. unsigned int j;
  2213. unsigned long spin_flags;
  2214. irqreturn_t ret;
  2215. /* Check if the interrupt must be processed by this handler */
  2216. if ((j = (unsigned int)((char *)shap - sha)) >= num_boards)
  2217. return IRQ_NONE;
  2218. shost = sh[j];
  2219. spin_lock_irqsave(shost->host_lock, spin_flags);
  2220. ret = ihdlr(irq, shost);
  2221. spin_unlock_irqrestore(shost->host_lock, spin_flags);
  2222. return ret;
  2223. }
  2224. static int eata2x_release(struct Scsi_Host *shost)
  2225. {
  2226. struct hostdata *ha = (struct hostdata *)shost->hostdata;
  2227. unsigned int i;
  2228. for (i = 0; i < shost->can_queue; i++)
  2229. if ((&ha->cp[i])->sglist)
  2230. kfree((&ha->cp[i])->sglist);
  2231. for (i = 0; i < shost->can_queue; i++)
  2232. pci_unmap_single(ha->pdev, ha->cp[i].cp_dma_addr,
  2233. sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
  2234. if (ha->sp_cpu_addr)
  2235. pci_free_consistent(ha->pdev, sizeof(struct mssp),
  2236. ha->sp_cpu_addr, ha->sp_dma_addr);
  2237. free_irq(shost->irq, &sha[ha->board_number]);
  2238. if (shost->dma_channel != NO_DMA)
  2239. free_dma(shost->dma_channel);
  2240. release_region(shost->io_port, shost->n_io_port);
  2241. scsi_unregister(shost);
  2242. return 0;
  2243. }
  2244. #include "scsi_module.c"
  2245. #ifndef MODULE
  2246. __setup("eata=", option_setup);
  2247. #endif /* end MODULE */