u14-34f.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. /*
  2. * u14-34f.c - Low-level driver for UltraStor 14F/34F 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 u14-34f.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. * of:[y|n] have_old_firmware=[1|0]
  20. *
  21. * A valid example using the new parameter format is:
  22. * modprobe u14-34f "u14-34f=0x340,0x330,lc:y,tm:0,mq:4"
  23. *
  24. * which is equivalent to the old format:
  25. * modprobe u14-34f io_port=0x340,0x330 linked_comm=1 tag_mode=0 \
  26. * max_queue_depth=4
  27. *
  28. * With actual module code, u14-34f and u14_34f are equivalent
  29. * as module parameter names.
  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. *
  49. * 10 Oct 2002 Rev. 7.70 for linux 2.5.42
  50. * + Foreport from revision 6.70.
  51. *
  52. * 25 Jun 2002 Rev. 6.70 for linux 2.4.19
  53. * + Fixed endian-ness problem due to bitfields.
  54. *
  55. * 21 Feb 2002 Rev. 6.52 for linux 2.4.18
  56. * + Backport from rev. 7.22 (use io_request_lock).
  57. *
  58. * 20 Feb 2002 Rev. 7.22 for linux 2.5.5
  59. * + Remove any reference to virt_to_bus().
  60. * + Fix pio hang while detecting multiple HBAs.
  61. *
  62. * 01 Jan 2002 Rev. 7.20 for linux 2.5.1
  63. * + Use the dynamic DMA mapping API.
  64. *
  65. * 19 Dec 2001 Rev. 7.02 for linux 2.5.1
  66. * + Use SCpnt->sc_data_direction if set.
  67. * + Use sglist.page instead of sglist.address.
  68. *
  69. * 11 Dec 2001 Rev. 7.00 for linux 2.5.1
  70. * + Use host->host_lock instead of io_request_lock.
  71. *
  72. * 1 May 2001 Rev. 6.05 for linux 2.4.4
  73. * + Fix data transfer direction for opcode SEND_CUE_SHEET (0x5d)
  74. *
  75. * 25 Jan 2001 Rev. 6.03 for linux 2.4.0
  76. * + "check_region" call replaced by "request_region".
  77. *
  78. * 22 Nov 2000 Rev. 6.02 for linux 2.4.0-test11
  79. * + Removed old scsi error handling support.
  80. * + The obsolete boot option flag eh:n is silently ignored.
  81. * + Removed error messages while a disk drive is powered up at
  82. * boot time.
  83. * + Improved boot messages: all tagged capable device are
  84. * indicated as "tagged".
  85. *
  86. * 16 Sep 1999 Rev. 5.11 for linux 2.2.12 and 2.3.18
  87. * + Updated to the new __setup interface for boot command line options.
  88. * + When loaded as a module, accepts the new parameter boot_options
  89. * which value is a string with the same format of the kernel boot
  90. * command line options. A valid example is:
  91. * modprobe u14-34f 'boot_options="0x230,0x340,lc:y,mq:4"'
  92. *
  93. * 22 Jul 1999 Rev. 5.00 for linux 2.2.10 and 2.3.11
  94. * + Removed pre-2.2 source code compatibility.
  95. *
  96. * 26 Jul 1998 Rev. 4.33 for linux 2.0.35 and 2.1.111
  97. * Added command line option (et:[y|n]) to use the existing
  98. * translation (returned by scsicam_bios_param) as disk geometry.
  99. * The default is et:n, which uses the disk geometry jumpered
  100. * on the board.
  101. * The default value et:n is compatible with all previous revisions
  102. * of this driver.
  103. *
  104. * 28 May 1998 Rev. 4.32 for linux 2.0.33 and 2.1.104
  105. * Increased busy timeout from 10 msec. to 200 msec. while
  106. * processing interrupts.
  107. *
  108. * 18 May 1998 Rev. 4.31 for linux 2.0.33 and 2.1.102
  109. * Improved abort handling during the eh recovery process.
  110. *
  111. * 13 May 1998 Rev. 4.30 for linux 2.0.33 and 2.1.101
  112. * The driver is now fully SMP safe, including the
  113. * abort and reset routines.
  114. * Added command line options (eh:[y|n]) to choose between
  115. * new_eh_code and the old scsi code.
  116. * If linux version >= 2.1.101 the default is eh:y, while the eh
  117. * option is ignored for previous releases and the old scsi code
  118. * is used.
  119. *
  120. * 18 Apr 1998 Rev. 4.20 for linux 2.0.33 and 2.1.97
  121. * Reworked interrupt handler.
  122. *
  123. * 11 Apr 1998 rev. 4.05 for linux 2.0.33 and 2.1.95
  124. * Major reliability improvement: when a batch with overlapping
  125. * requests is detected, requests are queued one at a time
  126. * eliminating any possible board or drive reordering.
  127. *
  128. * 10 Apr 1998 rev. 4.04 for linux 2.0.33 and 2.1.95
  129. * Improved SMP support (if linux version >= 2.1.95).
  130. *
  131. * 9 Apr 1998 rev. 4.03 for linux 2.0.33 and 2.1.94
  132. * Performance improvement: when sequential i/o is detected,
  133. * always use direct sort instead of reverse sort.
  134. *
  135. * 4 Apr 1998 rev. 4.02 for linux 2.0.33 and 2.1.92
  136. * io_port is now unsigned long.
  137. *
  138. * 17 Mar 1998 rev. 4.01 for linux 2.0.33 and 2.1.88
  139. * Use new scsi error handling code (if linux version >= 2.1.88).
  140. * Use new interrupt code.
  141. *
  142. * 12 Sep 1997 rev. 3.11 for linux 2.0.30 and 2.1.55
  143. * Use of udelay inside the wait loops to avoid timeout
  144. * problems with fast cpus.
  145. * Removed check about useless calls to the interrupt service
  146. * routine (reported on SMP systems only).
  147. * At initialization time "sorted/unsorted" is displayed instead
  148. * of "linked/unlinked" to reinforce the fact that "linking" is
  149. * nothing but "elevator sorting" in the actual implementation.
  150. *
  151. * 17 May 1997 rev. 3.10 for linux 2.0.30 and 2.1.38
  152. * Use of serial_number_at_timeout in abort and reset processing.
  153. * Use of the __initfunc and __initdata macro in setup code.
  154. * Minor cleanups in the list_statistics code.
  155. *
  156. * 24 Feb 1997 rev. 3.00 for linux 2.0.29 and 2.1.26
  157. * When loading as a module, parameter passing is now supported
  158. * both in 2.0 and in 2.1 style.
  159. * Fixed data transfer direction for some SCSI opcodes.
  160. * Immediate acknowledge to request sense commands.
  161. * Linked commands to each disk device are now reordered by elevator
  162. * sorting. Rare cases in which reordering of write requests could
  163. * cause wrong results are managed.
  164. *
  165. * 18 Jan 1997 rev. 2.60 for linux 2.1.21 and 2.0.28
  166. * Added command line options to enable/disable linked commands
  167. * (lc:[y|n]), old firmware support (of:[y|n]) and to set the max
  168. * queue depth (mq:xx). Default is "u14-34f=lc:n,of:n,mq:8".
  169. * Improved command linking.
  170. *
  171. * 8 Jan 1997 rev. 2.50 for linux 2.1.20 and 2.0.27
  172. * Added linked command support.
  173. *
  174. * 3 Dec 1996 rev. 2.40 for linux 2.1.14 and 2.0.27
  175. * Added queue depth adjustment.
  176. *
  177. * 22 Nov 1996 rev. 2.30 for linux 2.1.12 and 2.0.26
  178. * The list of i/o ports to be probed can be overwritten by the
  179. * "u14-34f=port0,port1,...." boot command line option.
  180. * Scatter/gather lists are now allocated by a number of kmalloc
  181. * calls, in order to avoid the previous size limit of 64Kb.
  182. *
  183. * 16 Nov 1996 rev. 2.20 for linux 2.1.10 and 2.0.25
  184. * Added multichannel support.
  185. *
  186. * 27 Sep 1996 rev. 2.12 for linux 2.1.0
  187. * Portability cleanups (virtual/bus addressing, little/big endian
  188. * support).
  189. *
  190. * 09 Jul 1996 rev. 2.11 for linux 2.0.4
  191. * "Data over/under-run" no longer implies a redo on all targets.
  192. * Number of internal retries is now limited.
  193. *
  194. * 16 Apr 1996 rev. 2.10 for linux 1.3.90
  195. * New argument "reset_flags" to the reset routine.
  196. *
  197. * 21 Jul 1995 rev. 2.02 for linux 1.3.11
  198. * Fixed Data Transfer Direction for some SCSI commands.
  199. *
  200. * 13 Jun 1995 rev. 2.01 for linux 1.2.10
  201. * HAVE_OLD_UX4F_FIRMWARE should be defined for U34F boards when
  202. * the firmware prom is not the latest one (28008-006).
  203. *
  204. * 11 Mar 1995 rev. 2.00 for linux 1.2.0
  205. * Fixed a bug which prevented media change detection for removable
  206. * disk drives.
  207. *
  208. * 23 Feb 1995 rev. 1.18 for linux 1.1.94
  209. * Added a check for scsi_register returning NULL.
  210. *
  211. * 11 Feb 1995 rev. 1.17 for linux 1.1.91
  212. * U14F qualified to run with 32 sglists.
  213. * Now DEBUG_RESET is disabled by default.
  214. *
  215. * 9 Feb 1995 rev. 1.16 for linux 1.1.90
  216. * Use host->wish_block instead of host->block.
  217. *
  218. * 8 Feb 1995 rev. 1.15 for linux 1.1.89
  219. * Cleared target_time_out counter while performing a reset.
  220. *
  221. * 28 Jan 1995 rev. 1.14 for linux 1.1.86
  222. * Added module support.
  223. * Log and do a retry when a disk drive returns a target status
  224. * different from zero on a recovered error.
  225. * Auto detects if U14F boards have an old firmware revision.
  226. * Max number of scatter/gather lists set to 16 for all boards
  227. * (most installation run fine using 33 sglists, while other
  228. * has problems when using more than 16).
  229. *
  230. * 16 Jan 1995 rev. 1.13 for linux 1.1.81
  231. * Display a message if check_region detects a port address
  232. * already in use.
  233. *
  234. * 15 Dec 1994 rev. 1.12 for linux 1.1.74
  235. * The host->block flag is set for all the detected ISA boards.
  236. *
  237. * 30 Nov 1994 rev. 1.11 for linux 1.1.68
  238. * Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
  239. * Added optional support for using a single board at a time.
  240. *
  241. * 14 Nov 1994 rev. 1.10 for linux 1.1.63
  242. *
  243. * 28 Oct 1994 rev. 1.09 for linux 1.1.58 Final BETA release.
  244. * 16 Jul 1994 rev. 1.00 for linux 1.1.29 Initial ALPHA release.
  245. *
  246. * This driver is a total replacement of the original UltraStor
  247. * scsi driver, but it supports ONLY the 14F and 34F boards.
  248. * It can be configured in the same kernel in which the original
  249. * ultrastor driver is configured to allow the original U24F
  250. * support.
  251. *
  252. * Multiple U14F and/or U34F host adapters are supported.
  253. *
  254. * Copyright (C) 1994-2003 Dario Ballabio (ballabio_dario@emc.com)
  255. *
  256. * Alternate email: dario.ballabio@inwind.it, dario.ballabio@tiscalinet.it
  257. *
  258. * Redistribution and use in source and binary forms, with or without
  259. * modification, are permitted provided that redistributions of source
  260. * code retain the above copyright notice and this comment without
  261. * modification.
  262. *
  263. * WARNING: if your 14/34F board has an old firmware revision (see below)
  264. * you must change "#undef" into "#define" in the following
  265. * statement.
  266. */
  267. #undef HAVE_OLD_UX4F_FIRMWARE
  268. /*
  269. * The UltraStor 14F, 24F, and 34F are a family of intelligent, high
  270. * performance SCSI-2 host adapters.
  271. * Here is the scoop on the various models:
  272. *
  273. * 14F - ISA first-party DMA HA with floppy support and WD1003 emulation.
  274. * 24F - EISA Bus Master HA with floppy support and WD1003 emulation.
  275. * 34F - VESA Local-Bus Bus Master HA (no WD1003 emulation).
  276. *
  277. * This code has been tested with up to two U14F boards, using both
  278. * firmware 28004-005/38004-004 (BIOS rev. 2.00) and the latest firmware
  279. * 28004-006/38004-005 (BIOS rev. 2.01).
  280. *
  281. * The latest firmware is required in order to get reliable operations when
  282. * clustering is enabled. ENABLE_CLUSTERING provides a performance increase
  283. * up to 50% on sequential access.
  284. *
  285. * Since the Scsi_Host_Template structure is shared among all 14F and 34F,
  286. * the last setting of use_clustering is in effect for all of these boards.
  287. *
  288. * Here a sample configuration using two U14F boards:
  289. *
  290. U14F0: ISA 0x330, BIOS 0xc8000, IRQ 11, DMA 5, SG 32, MB 16, of:n, lc:y, mq:8.
  291. U14F1: ISA 0x340, BIOS 0x00000, IRQ 10, DMA 6, SG 32, MB 16, of:n, lc:y, mq:8.
  292. *
  293. * The boot controller must have its BIOS enabled, while other boards can
  294. * have their BIOS disabled, or enabled to an higher address.
  295. * Boards are named Ux4F0, Ux4F1..., according to the port address order in
  296. * the io_port[] array.
  297. *
  298. * The following facts are based on real testing results (not on
  299. * documentation) on the above U14F board.
  300. *
  301. * - The U14F board should be jumpered for bus on time less or equal to 7
  302. * microseconds, while the default is 11 microseconds. This is order to
  303. * get acceptable performance while using floppy drive and hard disk
  304. * together. The jumpering for 7 microseconds is: JP13 pin 15-16,
  305. * JP14 pin 7-8 and pin 9-10.
  306. * The reduction has a little impact on scsi performance.
  307. *
  308. * - If scsi bus length exceeds 3m., the scsi bus speed needs to be reduced
  309. * from 10Mhz to 5Mhz (do this by inserting a jumper on JP13 pin 7-8).
  310. *
  311. * - If U14F on board firmware is older than 28004-006/38004-005,
  312. * the U14F board is unable to provide reliable operations if the scsi
  313. * request length exceeds 16Kbyte. When this length is exceeded the
  314. * behavior is:
  315. * - adapter_status equal 0x96 or 0xa3 or 0x93 or 0x94;
  316. * - adapter_status equal 0 and target_status equal 2 on for all targets
  317. * in the next operation following the reset.
  318. * This sequence takes a long time (>3 seconds), so in the meantime
  319. * the SD_TIMEOUT in sd.c could expire giving rise to scsi aborts
  320. * (SD_TIMEOUT has been increased from 3 to 6 seconds in 1.1.31).
  321. * Because of this I had to DISABLE_CLUSTERING and to work around the
  322. * bus reset in the interrupt service routine, returning DID_BUS_BUSY
  323. * so that the operations are retried without complains from the scsi.c
  324. * code.
  325. * Any reset of the scsi bus is going to kill tape operations, since
  326. * no retry is allowed for tapes. Bus resets are more likely when the
  327. * scsi bus is under heavy load.
  328. * Requests using scatter/gather have a maximum length of 16 x 1024 bytes
  329. * when DISABLE_CLUSTERING is in effect, but unscattered requests could be
  330. * larger than 16Kbyte.
  331. *
  332. * The new firmware has fixed all the above problems.
  333. *
  334. * For U34F boards the latest bios prom is 38008-002 (BIOS rev. 2.01),
  335. * the latest firmware prom is 28008-006. Older firmware 28008-005 has
  336. * problems when using more than 16 scatter/gather lists.
  337. *
  338. * The list of i/o ports to be probed can be totally replaced by the
  339. * boot command line option: "u14-34f=port0,port1,port2,...", where the
  340. * port0, port1... arguments are ISA/VESA addresses to be probed.
  341. * For example using "u14-34f=0x230,0x340", the driver probes only the two
  342. * addresses 0x230 and 0x340 in this order; "u14-34f=0" totally disables
  343. * this driver.
  344. *
  345. * After the optional list of detection probes, other possible command line
  346. * options are:
  347. *
  348. * et:y use disk geometry returned by scsicam_bios_param;
  349. * et:n use disk geometry jumpered on the board;
  350. * lc:y enables linked commands;
  351. * lc:n disables linked commands;
  352. * tm:0 disables tagged commands (same as tc:n);
  353. * tm:1 use simple queue tags (same as tc:y);
  354. * tm:2 use ordered queue tags (same as tc:2);
  355. * of:y enables old firmware support;
  356. * of:n disables old firmware support;
  357. * mq:xx set the max queue depth to the value xx (2 <= xx <= 8).
  358. *
  359. * The default value is: "u14-34f=lc:n,of:n,mq:8,tm:0,et:n".
  360. * An example using the list of detection probes could be:
  361. * "u14-34f=0x230,0x340,lc:y,tm:2,of:n,mq:4,et:n".
  362. *
  363. * When loading as a module, parameters can be specified as well.
  364. * The above example would be (use 1 in place of y and 0 in place of n):
  365. *
  366. * modprobe u14-34f io_port=0x230,0x340 linked_comm=1 have_old_firmware=0 \
  367. * max_queue_depth=4 ext_tran=0 tag_mode=2
  368. *
  369. * ----------------------------------------------------------------------------
  370. * In this implementation, linked commands are designed to work with any DISK
  371. * or CD-ROM, since this linking has only the intent of clustering (time-wise)
  372. * and reordering by elevator sorting commands directed to each device,
  373. * without any relation with the actual SCSI protocol between the controller
  374. * and the device.
  375. * If Q is the queue depth reported at boot time for each device (also named
  376. * cmds/lun) and Q > 2, whenever there is already an active command to the
  377. * device all other commands to the same device (up to Q-1) are kept waiting
  378. * in the elevator sorting queue. When the active command completes, the
  379. * commands in this queue are sorted by sector address. The sort is chosen
  380. * between increasing or decreasing by minimizing the seek distance between
  381. * the sector of the commands just completed and the sector of the first
  382. * command in the list to be sorted.
  383. * Trivial math assures that the unsorted average seek distance when doing
  384. * random seeks over S sectors is S/3.
  385. * When (Q-1) requests are uniformly distributed over S sectors, the average
  386. * distance between two adjacent requests is S/((Q-1) + 1), so the sorted
  387. * average seek distance for (Q-1) random requests over S sectors is S/Q.
  388. * The elevator sorting hence divides the seek distance by a factor Q/3.
  389. * The above pure geometric remarks are valid in all cases and the
  390. * driver effectively reduces the seek distance by the predicted factor
  391. * when there are Q concurrent read i/o operations on the device, but this
  392. * does not necessarily results in a noticeable performance improvement:
  393. * your mileage may vary....
  394. *
  395. * Note: command reordering inside a batch of queued commands could cause
  396. * wrong results only if there is at least one write request and the
  397. * intersection (sector-wise) of all requests is not empty.
  398. * When the driver detects a batch including overlapping requests
  399. * (a really rare event) strict serial (pid) order is enforced.
  400. * ----------------------------------------------------------------------------
  401. *
  402. * The boards are named Ux4F0, Ux4F1,... according to the detection order.
  403. *
  404. * In order to support multiple ISA boards in a reliable way,
  405. * the driver sets host->wish_block = TRUE for all ISA boards.
  406. */
  407. #include <linux/config.h>
  408. #include <linux/string.h>
  409. #include <linux/kernel.h>
  410. #include <linux/ioport.h>
  411. #include <linux/delay.h>
  412. #include <asm/io.h>
  413. #include <asm/system.h>
  414. #include <asm/byteorder.h>
  415. #include <linux/proc_fs.h>
  416. #include <linux/blkdev.h>
  417. #include <linux/interrupt.h>
  418. #include <linux/stat.h>
  419. #include <linux/pci.h>
  420. #include <linux/init.h>
  421. #include <linux/ctype.h>
  422. #include <linux/spinlock.h>
  423. #include <asm/dma.h>
  424. #include <asm/irq.h>
  425. #include <scsi/scsi.h>
  426. #include <scsi/scsi_cmnd.h>
  427. #include <scsi/scsi_device.h>
  428. #include <scsi/scsi_host.h>
  429. #include <scsi/scsi_tcq.h>
  430. #include <scsi/scsicam.h>
  431. static int u14_34f_detect(struct scsi_host_template *);
  432. static int u14_34f_release(struct Scsi_Host *);
  433. static int u14_34f_queuecommand(struct scsi_cmnd *, void (*done)(struct scsi_cmnd *));
  434. static int u14_34f_eh_abort(struct scsi_cmnd *);
  435. static int u14_34f_eh_host_reset(struct scsi_cmnd *);
  436. static int u14_34f_bios_param(struct scsi_device *, struct block_device *,
  437. sector_t, int *);
  438. static int u14_34f_slave_configure(struct scsi_device *);
  439. static struct scsi_host_template driver_template = {
  440. .name = "UltraStor 14F/34F rev. 8.10.00 ",
  441. .detect = u14_34f_detect,
  442. .release = u14_34f_release,
  443. .queuecommand = u14_34f_queuecommand,
  444. .eh_abort_handler = u14_34f_eh_abort,
  445. .eh_host_reset_handler = u14_34f_eh_host_reset,
  446. .bios_param = u14_34f_bios_param,
  447. .slave_configure = u14_34f_slave_configure,
  448. .this_id = 7,
  449. .unchecked_isa_dma = 1,
  450. .use_clustering = ENABLE_CLUSTERING
  451. };
  452. #if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
  453. #error "Adjust your <asm/byteorder.h> defines"
  454. #endif
  455. /* Values for the PRODUCT_ID ports for the 14/34F */
  456. #define PRODUCT_ID1 0x56
  457. #define PRODUCT_ID2 0x40 /* NOTE: Only upper nibble is used */
  458. /* Subversion values */
  459. #define ISA 0
  460. #define ESA 1
  461. #define OP_HOST_ADAPTER 0x1
  462. #define OP_SCSI 0x2
  463. #define OP_RESET 0x4
  464. #define DTD_SCSI 0x0
  465. #define DTD_IN 0x1
  466. #define DTD_OUT 0x2
  467. #define DTD_NONE 0x3
  468. #define HA_CMD_INQUIRY 0x1
  469. #define HA_CMD_SELF_DIAG 0x2
  470. #define HA_CMD_READ_BUFF 0x3
  471. #define HA_CMD_WRITE_BUFF 0x4
  472. #undef DEBUG_LINKED_COMMANDS
  473. #undef DEBUG_DETECT
  474. #undef DEBUG_INTERRUPT
  475. #undef DEBUG_RESET
  476. #undef DEBUG_GENERATE_ERRORS
  477. #undef DEBUG_GENERATE_ABORTS
  478. #undef DEBUG_GEOMETRY
  479. #define MAX_ISA 3
  480. #define MAX_VESA 1
  481. #define MAX_EISA 0
  482. #define MAX_PCI 0
  483. #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
  484. #define MAX_CHANNEL 1
  485. #define MAX_LUN 8
  486. #define MAX_TARGET 8
  487. #define MAX_MAILBOXES 16
  488. #define MAX_SGLIST 32
  489. #define MAX_SAFE_SGLIST 16
  490. #define MAX_INTERNAL_RETRIES 64
  491. #define MAX_CMD_PER_LUN 2
  492. #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
  493. #define SKIP ULONG_MAX
  494. #define FALSE 0
  495. #define TRUE 1
  496. #define FREE 0
  497. #define IN_USE 1
  498. #define LOCKED 2
  499. #define IN_RESET 3
  500. #define IGNORE 4
  501. #define READY 5
  502. #define ABORTING 6
  503. #define NO_DMA 0xff
  504. #define MAXLOOP 10000
  505. #define TAG_DISABLED 0
  506. #define TAG_SIMPLE 1
  507. #define TAG_ORDERED 2
  508. #define REG_LCL_MASK 0
  509. #define REG_LCL_INTR 1
  510. #define REG_SYS_MASK 2
  511. #define REG_SYS_INTR 3
  512. #define REG_PRODUCT_ID1 4
  513. #define REG_PRODUCT_ID2 5
  514. #define REG_CONFIG1 6
  515. #define REG_CONFIG2 7
  516. #define REG_OGM 8
  517. #define REG_ICM 12
  518. #define REGION_SIZE 13UL
  519. #define BSY_ASSERTED 0x01
  520. #define IRQ_ASSERTED 0x01
  521. #define CMD_RESET 0xc0
  522. #define CMD_OGM_INTR 0x01
  523. #define CMD_CLR_INTR 0x01
  524. #define CMD_ENA_INTR 0x81
  525. #define ASOK 0x00
  526. #define ASST 0x91
  527. #define YESNO(a) ((a) ? 'y' : 'n')
  528. #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
  529. #define PACKED __attribute__((packed))
  530. struct sg_list {
  531. unsigned int address; /* Segment Address */
  532. unsigned int num_bytes; /* Segment Length */
  533. };
  534. /* MailBox SCSI Command Packet */
  535. struct mscp {
  536. #if defined(__BIG_ENDIAN_BITFIELD)
  537. unsigned char sg:1, ca:1, dcn:1, xdir:2, opcode:3;
  538. unsigned char lun: 3, channel:2, target:3;
  539. #else
  540. unsigned char opcode: 3, /* type of command */
  541. xdir: 2, /* data transfer direction */
  542. dcn: 1, /* disable disconnect */
  543. ca: 1, /* use cache (if available) */
  544. sg: 1; /* scatter/gather operation */
  545. unsigned char target: 3, /* SCSI target id */
  546. channel: 2, /* SCSI channel number */
  547. lun: 3; /* SCSI logical unit number */
  548. #endif
  549. unsigned int data_address PACKED; /* transfer data pointer */
  550. unsigned int data_len PACKED; /* length in bytes */
  551. unsigned int link_address PACKED; /* for linking command chains */
  552. unsigned char clink_id; /* identifies command in chain */
  553. unsigned char use_sg; /* (if sg is set) 8 bytes per list */
  554. unsigned char sense_len;
  555. unsigned char cdb_len; /* 6, 10, or 12 */
  556. unsigned char cdb[12]; /* SCSI Command Descriptor Block */
  557. unsigned char adapter_status; /* non-zero indicates HA error */
  558. unsigned char target_status; /* non-zero indicates target error */
  559. unsigned int sense_addr PACKED;
  560. /* Additional fields begin here. */
  561. struct scsi_cmnd *SCpnt;
  562. unsigned int cpp_index; /* cp index */
  563. /* All the cp structure is zero filled by queuecommand except the
  564. following CP_TAIL_SIZE bytes, initialized by detect */
  565. dma_addr_t cp_dma_addr; /* dma handle for this cp structure */
  566. struct sg_list *sglist; /* pointer to the allocated SG list */
  567. };
  568. #define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
  569. struct hostdata {
  570. struct mscp cp[MAX_MAILBOXES]; /* Mailboxes for this board */
  571. unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
  572. unsigned int last_cp_used; /* Index of last mailbox used */
  573. unsigned int iocount; /* Total i/o done for this board */
  574. int board_number; /* Number of this board */
  575. char board_name[16]; /* Name of this board */
  576. int in_reset; /* True if board is doing a reset */
  577. int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
  578. int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
  579. unsigned int retries; /* Number of internal retries */
  580. unsigned long last_retried_pid; /* Pid of last retried command */
  581. unsigned char subversion; /* Bus type, either ISA or ESA */
  582. struct pci_dev *pdev; /* Always NULL */
  583. unsigned char heads;
  584. unsigned char sectors;
  585. char board_id[256]; /* data from INQUIRY on this board */
  586. };
  587. static struct Scsi_Host *sh[MAX_BOARDS + 1];
  588. static const char *driver_name = "Ux4F";
  589. static char sha[MAX_BOARDS];
  590. static DEFINE_SPINLOCK(driver_lock);
  591. /* Initialize num_boards so that ihdlr can work while detect is in progress */
  592. static unsigned int num_boards = MAX_BOARDS;
  593. static unsigned long io_port[] = {
  594. /* Space for MAX_INT_PARAM ports usable while loading as a module */
  595. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  596. SKIP, SKIP,
  597. /* Possible ISA/VESA ports */
  598. 0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140,
  599. /* End of list */
  600. 0x0
  601. };
  602. #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
  603. #define BN(board) (HD(board)->board_name)
  604. /* Device is Little Endian */
  605. #define H2DEV(x) cpu_to_le32(x)
  606. #define DEV2H(x) le32_to_cpu(x)
  607. static irqreturn_t do_interrupt_handler(int, void *, struct pt_regs *);
  608. static void flush_dev(struct scsi_device *, unsigned long, unsigned int, unsigned int);
  609. static int do_trace = FALSE;
  610. static int setup_done = FALSE;
  611. static int link_statistics;
  612. static int ext_tran = FALSE;
  613. #if defined(HAVE_OLD_UX4F_FIRMWARE)
  614. static int have_old_firmware = TRUE;
  615. #else
  616. static int have_old_firmware = FALSE;
  617. #endif
  618. #if defined(CONFIG_SCSI_U14_34F_TAGGED_QUEUE)
  619. static int tag_mode = TAG_SIMPLE;
  620. #else
  621. static int tag_mode = TAG_DISABLED;
  622. #endif
  623. #if defined(CONFIG_SCSI_U14_34F_LINKED_COMMANDS)
  624. static int linked_comm = TRUE;
  625. #else
  626. static int linked_comm = FALSE;
  627. #endif
  628. #if defined(CONFIG_SCSI_U14_34F_MAX_TAGS)
  629. static int max_queue_depth = CONFIG_SCSI_U14_34F_MAX_TAGS;
  630. #else
  631. static int max_queue_depth = MAX_CMD_PER_LUN;
  632. #endif
  633. #define MAX_INT_PARAM 10
  634. #define MAX_BOOT_OPTIONS_SIZE 256
  635. static char boot_options[MAX_BOOT_OPTIONS_SIZE];
  636. #if defined(MODULE)
  637. #include <linux/module.h>
  638. #include <linux/moduleparam.h>
  639. module_param_string(u14_34f, boot_options, MAX_BOOT_OPTIONS_SIZE, 0);
  640. MODULE_PARM_DESC(u14_34f, " equivalent to the \"u14-34f=...\" kernel boot " \
  641. "option." \
  642. " Example: modprobe u14-34f \"u14_34f=0x340,0x330,lc:y,tm:0,mq:4\"");
  643. MODULE_AUTHOR("Dario Ballabio");
  644. MODULE_LICENSE("GPL");
  645. MODULE_DESCRIPTION("UltraStor 14F/34F SCSI Driver");
  646. #endif
  647. static int u14_34f_slave_configure(struct scsi_device *dev) {
  648. int j, tqd, utqd;
  649. char *tag_suffix, *link_suffix;
  650. struct Scsi_Host *host = dev->host;
  651. j = ((struct hostdata *) host->hostdata)->board_number;
  652. utqd = MAX_CMD_PER_LUN;
  653. tqd = max_queue_depth;
  654. if (TLDEV(dev->type) && dev->tagged_supported)
  655. if (tag_mode == TAG_SIMPLE) {
  656. scsi_adjust_queue_depth(dev, MSG_SIMPLE_TAG, tqd);
  657. tag_suffix = ", simple tags";
  658. }
  659. else if (tag_mode == TAG_ORDERED) {
  660. scsi_adjust_queue_depth(dev, MSG_ORDERED_TAG, tqd);
  661. tag_suffix = ", ordered tags";
  662. }
  663. else {
  664. scsi_adjust_queue_depth(dev, 0, tqd);
  665. tag_suffix = ", no tags";
  666. }
  667. else if (TLDEV(dev->type) && linked_comm) {
  668. scsi_adjust_queue_depth(dev, 0, tqd);
  669. tag_suffix = ", untagged";
  670. }
  671. else {
  672. scsi_adjust_queue_depth(dev, 0, utqd);
  673. tag_suffix = "";
  674. }
  675. if (TLDEV(dev->type) && linked_comm && dev->queue_depth > 2)
  676. link_suffix = ", sorted";
  677. else if (TLDEV(dev->type))
  678. link_suffix = ", unsorted";
  679. else
  680. link_suffix = "";
  681. printk("%s: scsi%d, channel %d, id %d, lun %d, cmds/lun %d%s%s.\n",
  682. BN(j), host->host_no, dev->channel, dev->id, dev->lun,
  683. dev->queue_depth, link_suffix, tag_suffix);
  684. return FALSE;
  685. }
  686. static int wait_on_busy(unsigned long iobase, unsigned int loop) {
  687. while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED) {
  688. udelay(1L);
  689. if (--loop == 0) return TRUE;
  690. }
  691. return FALSE;
  692. }
  693. static int board_inquiry(unsigned int j) {
  694. struct mscp *cpp;
  695. dma_addr_t id_dma_addr;
  696. unsigned int time, limit = 0;
  697. id_dma_addr = pci_map_single(HD(j)->pdev, HD(j)->board_id,
  698. sizeof(HD(j)->board_id), PCI_DMA_BIDIRECTIONAL);
  699. cpp = &HD(j)->cp[0];
  700. cpp->cp_dma_addr = pci_map_single(HD(j)->pdev, cpp, sizeof(struct mscp),
  701. PCI_DMA_BIDIRECTIONAL);
  702. memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
  703. cpp->opcode = OP_HOST_ADAPTER;
  704. cpp->xdir = DTD_IN;
  705. cpp->data_address = H2DEV(id_dma_addr);
  706. cpp->data_len = H2DEV(sizeof(HD(j)->board_id));
  707. cpp->cdb_len = 6;
  708. cpp->cdb[0] = HA_CMD_INQUIRY;
  709. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  710. printk("%s: board_inquiry, adapter busy.\n", BN(j));
  711. return TRUE;
  712. }
  713. HD(j)->cp_stat[0] = IGNORE;
  714. /* Clear the interrupt indication */
  715. outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
  716. /* Store pointer in OGM address bytes */
  717. outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
  718. /* Issue OGM interrupt */
  719. outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
  720. spin_unlock_irq(&driver_lock);
  721. time = jiffies;
  722. while ((jiffies - time) < HZ && limit++ < 20000) udelay(100L);
  723. spin_lock_irq(&driver_lock);
  724. if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
  725. HD(j)->cp_stat[0] = FREE;
  726. printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
  727. return TRUE;
  728. }
  729. pci_unmap_single(HD(j)->pdev, cpp->cp_dma_addr, sizeof(struct mscp),
  730. PCI_DMA_BIDIRECTIONAL);
  731. pci_unmap_single(HD(j)->pdev, id_dma_addr, sizeof(HD(j)->board_id),
  732. PCI_DMA_BIDIRECTIONAL);
  733. return FALSE;
  734. }
  735. static int port_detect \
  736. (unsigned long port_base, unsigned int j, struct scsi_host_template *tpnt) {
  737. unsigned char irq, dma_channel, subversion, i;
  738. unsigned char in_byte;
  739. char *bus_type, dma_name[16];
  740. /* Allowed BIOS base addresses (NULL indicates reserved) */
  741. unsigned long bios_segment_table[8] = {
  742. 0,
  743. 0xc4000, 0xc8000, 0xcc000, 0xd0000,
  744. 0xd4000, 0xd8000, 0xdc000
  745. };
  746. /* Allowed IRQs */
  747. unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
  748. /* Allowed DMA channels for ISA (0 indicates reserved) */
  749. unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
  750. /* Head/sector mappings */
  751. struct {
  752. unsigned char heads;
  753. unsigned char sectors;
  754. } mapping_table[4] = {
  755. { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
  756. };
  757. struct config_1 {
  758. #if defined(__BIG_ENDIAN_BITFIELD)
  759. unsigned char dma_channel: 2, interrupt:2,
  760. removable_disks_as_fixed:1, bios_segment: 3;
  761. #else
  762. unsigned char bios_segment: 3, removable_disks_as_fixed: 1,
  763. interrupt: 2, dma_channel: 2;
  764. #endif
  765. } config_1;
  766. struct config_2 {
  767. #if defined(__BIG_ENDIAN_BITFIELD)
  768. unsigned char tfr_port: 2, bios_drive_number: 1,
  769. mapping_mode: 2, ha_scsi_id: 3;
  770. #else
  771. unsigned char ha_scsi_id: 3, mapping_mode: 2,
  772. bios_drive_number: 1, tfr_port: 2;
  773. #endif
  774. } config_2;
  775. char name[16];
  776. sprintf(name, "%s%d", driver_name, j);
  777. if (!request_region(port_base, REGION_SIZE, driver_name)) {
  778. #if defined(DEBUG_DETECT)
  779. printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
  780. #endif
  781. goto fail;
  782. }
  783. spin_lock_irq(&driver_lock);
  784. if (inb(port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) goto freelock;
  785. in_byte = inb(port_base + REG_PRODUCT_ID2);
  786. if ((in_byte & 0xf0) != PRODUCT_ID2) goto freelock;
  787. *(char *)&config_1 = inb(port_base + REG_CONFIG1);
  788. *(char *)&config_2 = inb(port_base + REG_CONFIG2);
  789. irq = interrupt_table[config_1.interrupt];
  790. dma_channel = dma_channel_table[config_1.dma_channel];
  791. subversion = (in_byte & 0x0f);
  792. /* Board detected, allocate its IRQ */
  793. if (request_irq(irq, do_interrupt_handler,
  794. SA_INTERRUPT | ((subversion == ESA) ? SA_SHIRQ : 0),
  795. driver_name, (void *) &sha[j])) {
  796. printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
  797. goto freelock;
  798. }
  799. if (subversion == ISA && request_dma(dma_channel, driver_name)) {
  800. printk("%s: unable to allocate DMA channel %u, detaching.\n",
  801. name, dma_channel);
  802. goto freeirq;
  803. }
  804. if (have_old_firmware) tpnt->use_clustering = DISABLE_CLUSTERING;
  805. spin_unlock_irq(&driver_lock);
  806. sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
  807. spin_lock_irq(&driver_lock);
  808. if (sh[j] == NULL) {
  809. printk("%s: unable to register host, detaching.\n", name);
  810. goto freedma;
  811. }
  812. sh[j]->io_port = port_base;
  813. sh[j]->unique_id = port_base;
  814. sh[j]->n_io_port = REGION_SIZE;
  815. sh[j]->base = bios_segment_table[config_1.bios_segment];
  816. sh[j]->irq = irq;
  817. sh[j]->sg_tablesize = MAX_SGLIST;
  818. sh[j]->this_id = config_2.ha_scsi_id;
  819. sh[j]->can_queue = MAX_MAILBOXES;
  820. sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
  821. #if defined(DEBUG_DETECT)
  822. {
  823. unsigned char sys_mask, lcl_mask;
  824. sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
  825. lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
  826. printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
  827. }
  828. #endif
  829. /* Probably a bogus host scsi id, set it to the dummy value */
  830. if (sh[j]->this_id == 0) sh[j]->this_id = -1;
  831. /* If BIOS is disabled, force enable interrupts */
  832. if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
  833. memset(HD(j), 0, sizeof(struct hostdata));
  834. HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
  835. HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
  836. HD(j)->subversion = subversion;
  837. HD(j)->pdev = NULL;
  838. HD(j)->board_number = j;
  839. if (have_old_firmware) sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
  840. if (HD(j)->subversion == ESA) {
  841. sh[j]->unchecked_isa_dma = FALSE;
  842. sh[j]->dma_channel = NO_DMA;
  843. sprintf(BN(j), "U34F%d", j);
  844. bus_type = "VESA";
  845. }
  846. else {
  847. unsigned long flags;
  848. sh[j]->unchecked_isa_dma = TRUE;
  849. flags=claim_dma_lock();
  850. disable_dma(dma_channel);
  851. clear_dma_ff(dma_channel);
  852. set_dma_mode(dma_channel, DMA_MODE_CASCADE);
  853. enable_dma(dma_channel);
  854. release_dma_lock(flags);
  855. sh[j]->dma_channel = dma_channel;
  856. sprintf(BN(j), "U14F%d", j);
  857. bus_type = "ISA";
  858. }
  859. sh[j]->max_channel = MAX_CHANNEL - 1;
  860. sh[j]->max_id = MAX_TARGET;
  861. sh[j]->max_lun = MAX_LUN;
  862. if (HD(j)->subversion == ISA && !board_inquiry(j)) {
  863. HD(j)->board_id[40] = 0;
  864. if (strcmp(&HD(j)->board_id[32], "06000600")) {
  865. printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
  866. printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
  867. BN(j), &HD(j)->board_id[32]);
  868. sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
  869. sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
  870. }
  871. }
  872. if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
  873. else sprintf(dma_name, "DMA %u", dma_channel);
  874. spin_unlock_irq(&driver_lock);
  875. for (i = 0; i < sh[j]->can_queue; i++)
  876. HD(j)->cp[i].cp_dma_addr = pci_map_single(HD(j)->pdev,
  877. &HD(j)->cp[i], sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
  878. for (i = 0; i < sh[j]->can_queue; i++)
  879. if (! ((&HD(j)->cp[i])->sglist = kmalloc(
  880. sh[j]->sg_tablesize * sizeof(struct sg_list),
  881. (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
  882. printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
  883. goto release;
  884. }
  885. if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
  886. max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
  887. if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
  888. if (tag_mode != TAG_DISABLED && tag_mode != TAG_SIMPLE)
  889. tag_mode = TAG_ORDERED;
  890. if (j == 0) {
  891. printk("UltraStor 14F/34F: Copyright (C) 1994-2003 Dario Ballabio.\n");
  892. printk("%s config options -> of:%c, tm:%d, lc:%c, mq:%d, et:%c.\n",
  893. driver_name, YESNO(have_old_firmware), tag_mode,
  894. YESNO(linked_comm), max_queue_depth, YESNO(ext_tran));
  895. }
  896. printk("%s: %s 0x%03lx, BIOS 0x%05x, IRQ %u, %s, SG %d, MB %d.\n",
  897. BN(j), bus_type, (unsigned long)sh[j]->io_port, (int)sh[j]->base,
  898. sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
  899. if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
  900. printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
  901. BN(j), sh[j]->max_id, sh[j]->max_lun);
  902. for (i = 0; i <= sh[j]->max_channel; i++)
  903. printk("%s: SCSI channel %u enabled, host target ID %d.\n",
  904. BN(j), i, sh[j]->this_id);
  905. return TRUE;
  906. freedma:
  907. if (subversion == ISA) free_dma(dma_channel);
  908. freeirq:
  909. free_irq(irq, &sha[j]);
  910. freelock:
  911. spin_unlock_irq(&driver_lock);
  912. release_region(port_base, REGION_SIZE);
  913. fail:
  914. return FALSE;
  915. release:
  916. u14_34f_release(sh[j]);
  917. return FALSE;
  918. }
  919. static void internal_setup(char *str, int *ints) {
  920. int i, argc = ints[0];
  921. char *cur = str, *pc;
  922. if (argc > 0) {
  923. if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
  924. for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
  925. io_port[i] = 0;
  926. setup_done = TRUE;
  927. }
  928. while (cur && (pc = strchr(cur, ':'))) {
  929. int val = 0, c = *++pc;
  930. if (c == 'n' || c == 'N') val = FALSE;
  931. else if (c == 'y' || c == 'Y') val = TRUE;
  932. else val = (int) simple_strtoul(pc, NULL, 0);
  933. if (!strncmp(cur, "lc:", 3)) linked_comm = val;
  934. else if (!strncmp(cur, "of:", 3)) have_old_firmware = val;
  935. else if (!strncmp(cur, "tm:", 3)) tag_mode = val;
  936. else if (!strncmp(cur, "tc:", 3)) tag_mode = val;
  937. else if (!strncmp(cur, "mq:", 3)) max_queue_depth = val;
  938. else if (!strncmp(cur, "ls:", 3)) link_statistics = val;
  939. else if (!strncmp(cur, "et:", 3)) ext_tran = val;
  940. if ((cur = strchr(cur, ','))) ++cur;
  941. }
  942. return;
  943. }
  944. static int option_setup(char *str) {
  945. int ints[MAX_INT_PARAM];
  946. char *cur = str;
  947. int i = 1;
  948. while (cur && isdigit(*cur) && i <= MAX_INT_PARAM) {
  949. ints[i++] = simple_strtoul(cur, NULL, 0);
  950. if ((cur = strchr(cur, ',')) != NULL) cur++;
  951. }
  952. ints[0] = i - 1;
  953. internal_setup(cur, ints);
  954. return 1;
  955. }
  956. static int u14_34f_detect(struct scsi_host_template *tpnt) {
  957. unsigned int j = 0, k;
  958. tpnt->proc_name = "u14-34f";
  959. if(strlen(boot_options)) option_setup(boot_options);
  960. #if defined(MODULE)
  961. /* io_port could have been modified when loading as a module */
  962. if(io_port[0] != SKIP) {
  963. setup_done = TRUE;
  964. io_port[MAX_INT_PARAM] = 0;
  965. }
  966. #endif
  967. for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
  968. for (k = 0; io_port[k]; k++) {
  969. if (io_port[k] == SKIP) continue;
  970. if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
  971. }
  972. num_boards = j;
  973. return j;
  974. }
  975. static void map_dma(unsigned int i, unsigned int j) {
  976. unsigned int data_len = 0;
  977. unsigned int k, count, pci_dir;
  978. struct scatterlist *sgpnt;
  979. struct mscp *cpp;
  980. struct scsi_cmnd *SCpnt;
  981. cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
  982. pci_dir = SCpnt->sc_data_direction;
  983. if (SCpnt->sense_buffer)
  984. cpp->sense_addr = H2DEV(pci_map_single(HD(j)->pdev, SCpnt->sense_buffer,
  985. sizeof SCpnt->sense_buffer, PCI_DMA_FROMDEVICE));
  986. cpp->sense_len = sizeof SCpnt->sense_buffer;
  987. if (!SCpnt->use_sg) {
  988. /* If we get here with PCI_DMA_NONE, pci_map_single triggers a BUG() */
  989. if (!SCpnt->request_bufflen) pci_dir = PCI_DMA_BIDIRECTIONAL;
  990. if (SCpnt->request_buffer)
  991. cpp->data_address = H2DEV(pci_map_single(HD(j)->pdev,
  992. SCpnt->request_buffer, SCpnt->request_bufflen, pci_dir));
  993. cpp->data_len = H2DEV(SCpnt->request_bufflen);
  994. return;
  995. }
  996. sgpnt = (struct scatterlist *) SCpnt->request_buffer;
  997. count = pci_map_sg(HD(j)->pdev, sgpnt, SCpnt->use_sg, pci_dir);
  998. for (k = 0; k < count; k++) {
  999. cpp->sglist[k].address = H2DEV(sg_dma_address(&sgpnt[k]));
  1000. cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(&sgpnt[k]));
  1001. data_len += sgpnt[k].length;
  1002. }
  1003. cpp->sg = TRUE;
  1004. cpp->use_sg = SCpnt->use_sg;
  1005. cpp->data_address = H2DEV(pci_map_single(HD(j)->pdev, cpp->sglist,
  1006. SCpnt->use_sg * sizeof(struct sg_list), pci_dir));
  1007. cpp->data_len = H2DEV(data_len);
  1008. }
  1009. static void unmap_dma(unsigned int i, unsigned int j) {
  1010. unsigned int pci_dir;
  1011. struct mscp *cpp;
  1012. struct scsi_cmnd *SCpnt;
  1013. cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
  1014. pci_dir = SCpnt->sc_data_direction;
  1015. if (DEV2H(cpp->sense_addr))
  1016. pci_unmap_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
  1017. DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
  1018. if (SCpnt->use_sg)
  1019. pci_unmap_sg(HD(j)->pdev, SCpnt->request_buffer, SCpnt->use_sg, pci_dir);
  1020. if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
  1021. if (DEV2H(cpp->data_address))
  1022. pci_unmap_single(HD(j)->pdev, DEV2H(cpp->data_address),
  1023. DEV2H(cpp->data_len), pci_dir);
  1024. }
  1025. static void sync_dma(unsigned int i, unsigned int j) {
  1026. unsigned int pci_dir;
  1027. struct mscp *cpp;
  1028. struct scsi_cmnd *SCpnt;
  1029. cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
  1030. pci_dir = SCpnt->sc_data_direction;
  1031. if (DEV2H(cpp->sense_addr))
  1032. pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->sense_addr),
  1033. DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
  1034. if (SCpnt->use_sg)
  1035. pci_dma_sync_sg_for_cpu(HD(j)->pdev, SCpnt->request_buffer,
  1036. SCpnt->use_sg, pci_dir);
  1037. if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
  1038. if (DEV2H(cpp->data_address))
  1039. pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->data_address),
  1040. DEV2H(cpp->data_len), pci_dir);
  1041. }
  1042. static void scsi_to_dev_dir(unsigned int i, unsigned int j) {
  1043. unsigned int k;
  1044. static const unsigned char data_out_cmds[] = {
  1045. 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
  1046. 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
  1047. 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
  1048. };
  1049. static const unsigned char data_none_cmds[] = {
  1050. 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
  1051. 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
  1052. 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
  1053. };
  1054. struct mscp *cpp;
  1055. struct scsi_cmnd *SCpnt;
  1056. cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
  1057. if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1058. cpp->xdir = DTD_IN;
  1059. return;
  1060. }
  1061. else if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1062. cpp->xdir = DTD_OUT;
  1063. return;
  1064. }
  1065. else if (SCpnt->sc_data_direction == DMA_NONE) {
  1066. cpp->xdir = DTD_NONE;
  1067. return;
  1068. }
  1069. if (SCpnt->sc_data_direction != DMA_BIDIRECTIONAL)
  1070. panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n", BN(j));
  1071. cpp->xdir = DTD_IN;
  1072. for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
  1073. if (SCpnt->cmnd[0] == data_out_cmds[k]) {
  1074. cpp->xdir = DTD_OUT;
  1075. break;
  1076. }
  1077. if (cpp->xdir == DTD_IN)
  1078. for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
  1079. if (SCpnt->cmnd[0] == data_none_cmds[k]) {
  1080. cpp->xdir = DTD_NONE;
  1081. break;
  1082. }
  1083. }
  1084. static int u14_34f_queuecommand(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *)) {
  1085. unsigned int i, j, k;
  1086. struct mscp *cpp;
  1087. /* j is the board number */
  1088. j = ((struct hostdata *) SCpnt->device->host->hostdata)->board_number;
  1089. if (SCpnt->host_scribble)
  1090. panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
  1091. BN(j), SCpnt->pid, SCpnt);
  1092. /* i is the mailbox number, look for the first free mailbox
  1093. starting from last_cp_used */
  1094. i = HD(j)->last_cp_used + 1;
  1095. for (k = 0; k < sh[j]->can_queue; k++, i++) {
  1096. if (i >= sh[j]->can_queue) i = 0;
  1097. if (HD(j)->cp_stat[i] == FREE) {
  1098. HD(j)->last_cp_used = i;
  1099. break;
  1100. }
  1101. }
  1102. if (k == sh[j]->can_queue) {
  1103. printk("%s: qcomm, no free mailbox.\n", BN(j));
  1104. return 1;
  1105. }
  1106. /* Set pointer to control packet structure */
  1107. cpp = &HD(j)->cp[i];
  1108. memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
  1109. SCpnt->scsi_done = done;
  1110. cpp->cpp_index = i;
  1111. SCpnt->host_scribble = (unsigned char *) &cpp->cpp_index;
  1112. if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
  1113. BN(j), i, SCpnt->device->channel, SCpnt->device->id,
  1114. SCpnt->device->lun, SCpnt->pid);
  1115. cpp->opcode = OP_SCSI;
  1116. cpp->channel = SCpnt->device->channel;
  1117. cpp->target = SCpnt->device->id;
  1118. cpp->lun = SCpnt->device->lun;
  1119. cpp->SCpnt = SCpnt;
  1120. cpp->cdb_len = SCpnt->cmd_len;
  1121. memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
  1122. /* Use data transfer direction SCpnt->sc_data_direction */
  1123. scsi_to_dev_dir(i, j);
  1124. /* Map DMA buffers and SG list */
  1125. map_dma(i, j);
  1126. if (linked_comm && SCpnt->device->queue_depth > 2
  1127. && TLDEV(SCpnt->device->type)) {
  1128. HD(j)->cp_stat[i] = READY;
  1129. flush_dev(SCpnt->device, SCpnt->request->sector, j, FALSE);
  1130. return 0;
  1131. }
  1132. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1133. unmap_dma(i, j);
  1134. SCpnt->host_scribble = NULL;
  1135. printk("%s: qcomm, target %d.%d:%d, pid %ld, adapter busy.\n",
  1136. BN(j), SCpnt->device->channel, SCpnt->device->id, SCpnt->device->lun, SCpnt->pid);
  1137. return 1;
  1138. }
  1139. /* Store pointer in OGM address bytes */
  1140. outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
  1141. /* Issue OGM interrupt */
  1142. outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
  1143. HD(j)->cp_stat[i] = IN_USE;
  1144. return 0;
  1145. }
  1146. static int u14_34f_eh_abort(struct scsi_cmnd *SCarg) {
  1147. unsigned int i, j;
  1148. j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
  1149. if (SCarg->host_scribble == NULL) {
  1150. printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
  1151. BN(j), SCarg->device->channel, SCarg->device->id, SCarg->device->lun, SCarg->pid);
  1152. return SUCCESS;
  1153. }
  1154. i = *(unsigned int *)SCarg->host_scribble;
  1155. printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
  1156. BN(j), i, SCarg->device->channel, SCarg->device->id, SCarg->device->lun, SCarg->pid);
  1157. if (i >= sh[j]->can_queue)
  1158. panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
  1159. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1160. printk("%s: abort, timeout error.\n", BN(j));
  1161. return FAILED;
  1162. }
  1163. if (HD(j)->cp_stat[i] == FREE) {
  1164. printk("%s: abort, mbox %d is free.\n", BN(j), i);
  1165. return SUCCESS;
  1166. }
  1167. if (HD(j)->cp_stat[i] == IN_USE) {
  1168. printk("%s: abort, mbox %d is in use.\n", BN(j), i);
  1169. if (SCarg != HD(j)->cp[i].SCpnt)
  1170. panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
  1171. BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
  1172. if (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED)
  1173. printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
  1174. return FAILED;
  1175. }
  1176. if (HD(j)->cp_stat[i] == IN_RESET) {
  1177. printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
  1178. return FAILED;
  1179. }
  1180. if (HD(j)->cp_stat[i] == LOCKED) {
  1181. printk("%s: abort, mbox %d is locked.\n", BN(j), i);
  1182. return SUCCESS;
  1183. }
  1184. if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
  1185. unmap_dma(i, j);
  1186. SCarg->result = DID_ABORT << 16;
  1187. SCarg->host_scribble = NULL;
  1188. HD(j)->cp_stat[i] = FREE;
  1189. printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
  1190. BN(j), i, SCarg->pid);
  1191. SCarg->scsi_done(SCarg);
  1192. return SUCCESS;
  1193. }
  1194. panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
  1195. }
  1196. static int u14_34f_eh_host_reset(struct scsi_cmnd *SCarg) {
  1197. unsigned int i, j, time, k, c, limit = 0;
  1198. int arg_done = FALSE;
  1199. struct scsi_cmnd *SCpnt;
  1200. j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
  1201. printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
  1202. BN(j), SCarg->device->channel, SCarg->device->id, SCarg->device->lun, SCarg->pid);
  1203. spin_lock_irq(sh[j]->host_lock);
  1204. if (SCarg->host_scribble == NULL)
  1205. printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
  1206. if (HD(j)->in_reset) {
  1207. printk("%s: reset, exit, already in reset.\n", BN(j));
  1208. spin_unlock_irq(sh[j]->host_lock);
  1209. return FAILED;
  1210. }
  1211. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1212. printk("%s: reset, exit, timeout error.\n", BN(j));
  1213. spin_unlock_irq(sh[j]->host_lock);
  1214. return FAILED;
  1215. }
  1216. HD(j)->retries = 0;
  1217. for (c = 0; c <= sh[j]->max_channel; c++)
  1218. for (k = 0; k < sh[j]->max_id; k++) {
  1219. HD(j)->target_redo[k][c] = TRUE;
  1220. HD(j)->target_to[k][c] = 0;
  1221. }
  1222. for (i = 0; i < sh[j]->can_queue; i++) {
  1223. if (HD(j)->cp_stat[i] == FREE) continue;
  1224. if (HD(j)->cp_stat[i] == LOCKED) {
  1225. HD(j)->cp_stat[i] = FREE;
  1226. printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
  1227. continue;
  1228. }
  1229. if (!(SCpnt = HD(j)->cp[i].SCpnt))
  1230. panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
  1231. if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
  1232. HD(j)->cp_stat[i] = ABORTING;
  1233. printk("%s: reset, mbox %d aborting, pid %ld.\n",
  1234. BN(j), i, SCpnt->pid);
  1235. }
  1236. else {
  1237. HD(j)->cp_stat[i] = IN_RESET;
  1238. printk("%s: reset, mbox %d in reset, pid %ld.\n",
  1239. BN(j), i, SCpnt->pid);
  1240. }
  1241. if (SCpnt->host_scribble == NULL)
  1242. panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
  1243. if (*(unsigned int *)SCpnt->host_scribble != i)
  1244. panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
  1245. if (SCpnt->scsi_done == NULL)
  1246. panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
  1247. if (SCpnt == SCarg) arg_done = TRUE;
  1248. }
  1249. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1250. printk("%s: reset, cannot reset, timeout error.\n", BN(j));
  1251. spin_unlock_irq(sh[j]->host_lock);
  1252. return FAILED;
  1253. }
  1254. outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
  1255. printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
  1256. #if defined(DEBUG_RESET)
  1257. do_trace = TRUE;
  1258. #endif
  1259. HD(j)->in_reset = TRUE;
  1260. spin_unlock_irq(sh[j]->host_lock);
  1261. time = jiffies;
  1262. while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
  1263. spin_lock_irq(sh[j]->host_lock);
  1264. printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
  1265. for (i = 0; i < sh[j]->can_queue; i++) {
  1266. if (HD(j)->cp_stat[i] == IN_RESET) {
  1267. SCpnt = HD(j)->cp[i].SCpnt;
  1268. unmap_dma(i, j);
  1269. SCpnt->result = DID_RESET << 16;
  1270. SCpnt->host_scribble = NULL;
  1271. /* This mailbox is still waiting for its interrupt */
  1272. HD(j)->cp_stat[i] = LOCKED;
  1273. printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
  1274. BN(j), i, SCpnt->pid);
  1275. }
  1276. else if (HD(j)->cp_stat[i] == ABORTING) {
  1277. SCpnt = HD(j)->cp[i].SCpnt;
  1278. unmap_dma(i, j);
  1279. SCpnt->result = DID_RESET << 16;
  1280. SCpnt->host_scribble = NULL;
  1281. /* This mailbox was never queued to the adapter */
  1282. HD(j)->cp_stat[i] = FREE;
  1283. printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
  1284. BN(j), i, SCpnt->pid);
  1285. }
  1286. else
  1287. /* Any other mailbox has already been set free by interrupt */
  1288. continue;
  1289. SCpnt->scsi_done(SCpnt);
  1290. }
  1291. HD(j)->in_reset = FALSE;
  1292. do_trace = FALSE;
  1293. if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->pid);
  1294. else printk("%s: reset, exit.\n", BN(j));
  1295. spin_unlock_irq(sh[j]->host_lock);
  1296. return SUCCESS;
  1297. }
  1298. static int u14_34f_bios_param(struct scsi_device *disk,
  1299. struct block_device *bdev, sector_t capacity, int *dkinfo) {
  1300. unsigned int j = 0;
  1301. unsigned int size = capacity;
  1302. dkinfo[0] = HD(j)->heads;
  1303. dkinfo[1] = HD(j)->sectors;
  1304. dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
  1305. if (ext_tran && (scsicam_bios_param(bdev, capacity, dkinfo) < 0)) {
  1306. dkinfo[0] = 255;
  1307. dkinfo[1] = 63;
  1308. dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
  1309. }
  1310. #if defined (DEBUG_GEOMETRY)
  1311. printk ("%s: bios_param, head=%d, sec=%d, cyl=%d.\n", driver_name,
  1312. dkinfo[0], dkinfo[1], dkinfo[2]);
  1313. #endif
  1314. return FALSE;
  1315. }
  1316. static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
  1317. unsigned int rev) {
  1318. unsigned int i, j, k, y;
  1319. unsigned long x;
  1320. for (i = 0; i < n - 1; i++) {
  1321. k = i;
  1322. for (j = k + 1; j < n; j++)
  1323. if (rev) {
  1324. if (sk[j] > sk[k]) k = j;
  1325. }
  1326. else {
  1327. if (sk[j] < sk[k]) k = j;
  1328. }
  1329. if (k != i) {
  1330. x = sk[k]; sk[k] = sk[i]; sk[i] = x;
  1331. y = da[k]; da[k] = da[i]; da[i] = y;
  1332. }
  1333. }
  1334. return;
  1335. }
  1336. static int reorder(unsigned int j, unsigned long cursec,
  1337. unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
  1338. struct scsi_cmnd *SCpnt;
  1339. struct mscp *cpp;
  1340. unsigned int k, n;
  1341. unsigned int rev = FALSE, s = TRUE, r = TRUE;
  1342. unsigned int input_only = TRUE, overlap = FALSE;
  1343. unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
  1344. unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
  1345. unsigned long ioseek = 0;
  1346. static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
  1347. static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
  1348. static unsigned int readysorted = 0, revcount = 0;
  1349. static unsigned long seeksorted = 0, seeknosort = 0;
  1350. if (link_statistics && !(++flushcount % link_statistics))
  1351. printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
  1352. " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
  1353. ovlcount, readycount, readysorted, sortcount, revcount,
  1354. seeknosort / (readycount + 1),
  1355. seeksorted / (readycount + 1));
  1356. if (n_ready <= 1) return FALSE;
  1357. for (n = 0; n < n_ready; n++) {
  1358. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1359. if (!(cpp->xdir == DTD_IN)) input_only = FALSE;
  1360. if (SCpnt->request->sector < minsec) minsec = SCpnt->request->sector;
  1361. if (SCpnt->request->sector > maxsec) maxsec = SCpnt->request->sector;
  1362. sl[n] = SCpnt->request->sector;
  1363. ioseek += SCpnt->request->nr_sectors;
  1364. if (!n) continue;
  1365. if (sl[n] < sl[n - 1]) s = FALSE;
  1366. if (sl[n] > sl[n - 1]) r = FALSE;
  1367. if (link_statistics) {
  1368. if (sl[n] > sl[n - 1])
  1369. seek += sl[n] - sl[n - 1];
  1370. else
  1371. seek += sl[n - 1] - sl[n];
  1372. }
  1373. }
  1374. if (link_statistics) {
  1375. if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
  1376. }
  1377. if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
  1378. if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
  1379. if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
  1380. if (!input_only) for (n = 0; n < n_ready; n++) {
  1381. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1382. ll[n] = SCpnt->request->nr_sectors; pl[n] = SCpnt->pid;
  1383. if (!n) continue;
  1384. if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
  1385. || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
  1386. }
  1387. if (overlap) sort(pl, il, n_ready, FALSE);
  1388. if (link_statistics) {
  1389. if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
  1390. batchcount++; readycount += n_ready; seeknosort += seek / 1024;
  1391. if (input_only) inputcount++;
  1392. if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
  1393. else seeksorted += (iseek + maxsec - minsec) / 1024;
  1394. if (rev && !r) { revcount++; readysorted += n_ready; }
  1395. if (!rev && !s) { sortcount++; readysorted += n_ready; }
  1396. }
  1397. #if defined(DEBUG_LINKED_COMMANDS)
  1398. if (link_statistics && (overlap || !(flushcount % link_statistics)))
  1399. for (n = 0; n < n_ready; n++) {
  1400. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1401. printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %ld"\
  1402. " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
  1403. (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
  1404. SCpnt->lun, SCpnt->pid, k, flushcount, n_ready,
  1405. SCpnt->request->sector, SCpnt->request->nr_sectors, cursec,
  1406. YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
  1407. YESNO(overlap), cpp->xdir);
  1408. }
  1409. #endif
  1410. return overlap;
  1411. }
  1412. static void flush_dev(struct scsi_device *dev, unsigned long cursec, unsigned int j,
  1413. unsigned int ihdlr) {
  1414. struct scsi_cmnd *SCpnt;
  1415. struct mscp *cpp;
  1416. unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
  1417. for (k = 0; k < sh[j]->can_queue; k++) {
  1418. if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
  1419. cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1420. if (SCpnt->device != dev) continue;
  1421. if (HD(j)->cp_stat[k] == IN_USE) return;
  1422. il[n_ready++] = k;
  1423. }
  1424. if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
  1425. for (n = 0; n < n_ready; n++) {
  1426. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1427. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1428. printk("%s: %s, target %d.%d:%d, pid %ld, mbox %d, adapter"\
  1429. " busy, will abort.\n", BN(j), (ihdlr ? "ihdlr" : "qcomm"),
  1430. SCpnt->device->channel, SCpnt->device->id, SCpnt->device->lun, SCpnt->pid, k);
  1431. HD(j)->cp_stat[k] = ABORTING;
  1432. continue;
  1433. }
  1434. outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
  1435. outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
  1436. HD(j)->cp_stat[k] = IN_USE;
  1437. }
  1438. }
  1439. static irqreturn_t ihdlr(int irq, unsigned int j) {
  1440. struct scsi_cmnd *SCpnt;
  1441. unsigned int i, k, c, status, tstatus, reg, ret;
  1442. struct mscp *spp, *cpp;
  1443. if (sh[j]->irq != irq)
  1444. panic("%s: ihdlr, irq %d, sh[j]->irq %d.\n", BN(j), irq, sh[j]->irq);
  1445. /* Check if this board need to be serviced */
  1446. if (!((reg = inb(sh[j]->io_port + REG_SYS_INTR)) & IRQ_ASSERTED)) goto none;
  1447. HD(j)->iocount++;
  1448. if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
  1449. HD(j)->iocount);
  1450. /* Check if this board is still busy */
  1451. if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
  1452. outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
  1453. printk("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
  1454. BN(j), irq, reg, HD(j)->iocount);
  1455. goto none;
  1456. }
  1457. ret = inl(sh[j]->io_port + REG_ICM);
  1458. /* Clear interrupt pending flag */
  1459. outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
  1460. /* Find the mailbox to be serviced on this board */
  1461. for (i = 0; i < sh[j]->can_queue; i++)
  1462. if (H2DEV(HD(j)->cp[i].cp_dma_addr) == ret) break;
  1463. if (i >= sh[j]->can_queue)
  1464. panic("%s: ihdlr, invalid mscp bus address %p, cp0 %p.\n", BN(j),
  1465. (void *)ret, (void *)H2DEV(HD(j)->cp[0].cp_dma_addr));
  1466. cpp = &(HD(j)->cp[i]);
  1467. spp = cpp;
  1468. #if defined(DEBUG_GENERATE_ABORTS)
  1469. if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) goto handled;
  1470. #endif
  1471. if (HD(j)->cp_stat[i] == IGNORE) {
  1472. HD(j)->cp_stat[i] = FREE;
  1473. goto handled;
  1474. }
  1475. else if (HD(j)->cp_stat[i] == LOCKED) {
  1476. HD(j)->cp_stat[i] = FREE;
  1477. printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
  1478. HD(j)->iocount);
  1479. goto handled;
  1480. }
  1481. else if (HD(j)->cp_stat[i] == FREE) {
  1482. printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
  1483. HD(j)->iocount);
  1484. goto handled;
  1485. }
  1486. else if (HD(j)->cp_stat[i] == IN_RESET)
  1487. printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
  1488. else if (HD(j)->cp_stat[i] != IN_USE)
  1489. panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
  1490. BN(j), i, HD(j)->cp_stat[i]);
  1491. HD(j)->cp_stat[i] = FREE;
  1492. SCpnt = cpp->SCpnt;
  1493. if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
  1494. if (SCpnt->host_scribble == NULL)
  1495. panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
  1496. SCpnt->pid, SCpnt);
  1497. if (*(unsigned int *)SCpnt->host_scribble != i)
  1498. panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
  1499. BN(j), i, SCpnt->pid, *(unsigned int *)SCpnt->host_scribble);
  1500. sync_dma(i, j);
  1501. if (linked_comm && SCpnt->device->queue_depth > 2
  1502. && TLDEV(SCpnt->device->type))
  1503. flush_dev(SCpnt->device, SCpnt->request->sector, j, TRUE);
  1504. tstatus = status_byte(spp->target_status);
  1505. #if defined(DEBUG_GENERATE_ERRORS)
  1506. if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
  1507. spp->adapter_status = 0x01;
  1508. #endif
  1509. switch (spp->adapter_status) {
  1510. case ASOK: /* status OK */
  1511. /* Forces a reset if a disk drive keeps returning BUSY */
  1512. if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
  1513. status = DID_ERROR << 16;
  1514. /* If there was a bus reset, redo operation on each target */
  1515. else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
  1516. && HD(j)->target_redo[SCpnt->device->id][SCpnt->device->channel])
  1517. status = DID_BUS_BUSY << 16;
  1518. /* Works around a flaw in scsi.c */
  1519. else if (tstatus == CHECK_CONDITION
  1520. && SCpnt->device->type == TYPE_DISK
  1521. && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
  1522. status = DID_BUS_BUSY << 16;
  1523. else
  1524. status = DID_OK << 16;
  1525. if (tstatus == GOOD)
  1526. HD(j)->target_redo[SCpnt->device->id][SCpnt->device->channel] = FALSE;
  1527. if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
  1528. (!(tstatus == CHECK_CONDITION && HD(j)->iocount <= 1000 &&
  1529. (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
  1530. printk("%s: ihdlr, target %d.%d:%d, pid %ld, "\
  1531. "target_status 0x%x, sense key 0x%x.\n", BN(j),
  1532. SCpnt->device->channel, SCpnt->device->id, SCpnt->device->lun,
  1533. SCpnt->pid, spp->target_status,
  1534. SCpnt->sense_buffer[2]);
  1535. HD(j)->target_to[SCpnt->device->id][SCpnt->device->channel] = 0;
  1536. if (HD(j)->last_retried_pid == SCpnt->pid) HD(j)->retries = 0;
  1537. break;
  1538. case ASST: /* Selection Time Out */
  1539. if (HD(j)->target_to[SCpnt->device->id][SCpnt->device->channel] > 1)
  1540. status = DID_ERROR << 16;
  1541. else {
  1542. status = DID_TIME_OUT << 16;
  1543. HD(j)->target_to[SCpnt->device->id][SCpnt->device->channel]++;
  1544. }
  1545. break;
  1546. /* Perform a limited number of internal retries */
  1547. case 0x93: /* Unexpected bus free */
  1548. case 0x94: /* Target bus phase sequence failure */
  1549. case 0x96: /* Illegal SCSI command */
  1550. case 0xa3: /* SCSI bus reset error */
  1551. for (c = 0; c <= sh[j]->max_channel; c++)
  1552. for (k = 0; k < sh[j]->max_id; k++)
  1553. HD(j)->target_redo[k][c] = TRUE;
  1554. case 0x92: /* Data over/under-run */
  1555. if (SCpnt->device->type != TYPE_TAPE
  1556. && HD(j)->retries < MAX_INTERNAL_RETRIES) {
  1557. #if defined(DID_SOFT_ERROR)
  1558. status = DID_SOFT_ERROR << 16;
  1559. #else
  1560. status = DID_BUS_BUSY << 16;
  1561. #endif
  1562. HD(j)->retries++;
  1563. HD(j)->last_retried_pid = SCpnt->pid;
  1564. }
  1565. else
  1566. status = DID_ERROR << 16;
  1567. break;
  1568. case 0x01: /* Invalid command */
  1569. case 0x02: /* Invalid parameters */
  1570. case 0x03: /* Invalid data list */
  1571. case 0x84: /* SCSI bus abort error */
  1572. case 0x9b: /* Auto request sense error */
  1573. case 0x9f: /* Unexpected command complete message error */
  1574. case 0xff: /* Invalid parameter in the S/G list */
  1575. default:
  1576. status = DID_ERROR << 16;
  1577. break;
  1578. }
  1579. SCpnt->result = status | spp->target_status;
  1580. #if defined(DEBUG_INTERRUPT)
  1581. if (SCpnt->result || do_trace)
  1582. #else
  1583. if ((spp->adapter_status != ASOK && HD(j)->iocount > 1000) ||
  1584. (spp->adapter_status != ASOK &&
  1585. spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
  1586. do_trace || msg_byte(spp->target_status))
  1587. #endif
  1588. printk("%s: ihdlr, mbox %2d, err 0x%x:%x,"\
  1589. " target %d.%d:%d, pid %ld, reg 0x%x, count %d.\n",
  1590. BN(j), i, spp->adapter_status, spp->target_status,
  1591. SCpnt->device->channel, SCpnt->device->id, SCpnt->device->lun, SCpnt->pid,
  1592. reg, HD(j)->iocount);
  1593. unmap_dma(i, j);
  1594. /* Set the command state to inactive */
  1595. SCpnt->host_scribble = NULL;
  1596. SCpnt->scsi_done(SCpnt);
  1597. if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
  1598. HD(j)->iocount);
  1599. handled:
  1600. return IRQ_HANDLED;
  1601. none:
  1602. return IRQ_NONE;
  1603. }
  1604. static irqreturn_t do_interrupt_handler(int irq, void *shap,
  1605. struct pt_regs *regs) {
  1606. unsigned int j;
  1607. unsigned long spin_flags;
  1608. irqreturn_t ret;
  1609. /* Check if the interrupt must be processed by this handler */
  1610. if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return IRQ_NONE;
  1611. spin_lock_irqsave(sh[j]->host_lock, spin_flags);
  1612. ret = ihdlr(irq, j);
  1613. spin_unlock_irqrestore(sh[j]->host_lock, spin_flags);
  1614. return ret;
  1615. }
  1616. static int u14_34f_release(struct Scsi_Host *shpnt) {
  1617. unsigned int i, j;
  1618. for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
  1619. if (sh[j] == NULL) panic("%s: release, invalid Scsi_Host pointer.\n",
  1620. driver_name);
  1621. for (i = 0; i < sh[j]->can_queue; i++)
  1622. if ((&HD(j)->cp[i])->sglist) kfree((&HD(j)->cp[i])->sglist);
  1623. for (i = 0; i < sh[j]->can_queue; i++)
  1624. pci_unmap_single(HD(j)->pdev, HD(j)->cp[i].cp_dma_addr,
  1625. sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
  1626. free_irq(sh[j]->irq, &sha[j]);
  1627. if (sh[j]->dma_channel != NO_DMA) free_dma(sh[j]->dma_channel);
  1628. release_region(sh[j]->io_port, sh[j]->n_io_port);
  1629. scsi_unregister(sh[j]);
  1630. return FALSE;
  1631. }
  1632. #include "scsi_module.c"
  1633. #ifndef MODULE
  1634. __setup("u14-34f=", option_setup);
  1635. #endif /* end MODULE */