u14-34f.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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 struct 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/string.h>
  408. #include <linux/kernel.h>
  409. #include <linux/ioport.h>
  410. #include <linux/delay.h>
  411. #include <asm/io.h>
  412. #include <asm/byteorder.h>
  413. #include <linux/proc_fs.h>
  414. #include <linux/blkdev.h>
  415. #include <linux/interrupt.h>
  416. #include <linux/stat.h>
  417. #include <linux/pci.h>
  418. #include <linux/init.h>
  419. #include <linux/ctype.h>
  420. #include <linux/spinlock.h>
  421. #include <linux/slab.h>
  422. #include <asm/dma.h>
  423. #include <asm/irq.h>
  424. #include <scsi/scsi.h>
  425. #include <scsi/scsi_cmnd.h>
  426. #include <scsi/scsi_device.h>
  427. #include <scsi/scsi_host.h>
  428. #include <scsi/scsi_tcq.h>
  429. #include <scsi/scsicam.h>
  430. static int u14_34f_detect(struct scsi_host_template *);
  431. static int u14_34f_release(struct Scsi_Host *);
  432. static int u14_34f_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
  433. static int u14_34f_eh_abort(struct scsi_cmnd *);
  434. static int u14_34f_eh_host_reset(struct scsi_cmnd *);
  435. static int u14_34f_bios_param(struct scsi_device *, struct block_device *,
  436. sector_t, int *);
  437. static int u14_34f_slave_configure(struct scsi_device *);
  438. static struct scsi_host_template driver_template = {
  439. .name = "UltraStor 14F/34F rev. 8.10.00 ",
  440. .detect = u14_34f_detect,
  441. .release = u14_34f_release,
  442. .queuecommand = u14_34f_queuecommand,
  443. .eh_abort_handler = u14_34f_eh_abort,
  444. .eh_host_reset_handler = u14_34f_eh_host_reset,
  445. .bios_param = u14_34f_bios_param,
  446. .slave_configure = u14_34f_slave_configure,
  447. .this_id = 7,
  448. .unchecked_isa_dma = 1,
  449. .use_clustering = ENABLE_CLUSTERING,
  450. };
  451. #if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
  452. #error "Adjust your <asm/byteorder.h> defines"
  453. #endif
  454. /* Values for the PRODUCT_ID ports for the 14/34F */
  455. #define PRODUCT_ID1 0x56
  456. #define PRODUCT_ID2 0x40 /* NOTE: Only upper nibble is used */
  457. /* Subversion values */
  458. #define ISA 0
  459. #define ESA 1
  460. #define OP_HOST_ADAPTER 0x1
  461. #define OP_SCSI 0x2
  462. #define OP_RESET 0x4
  463. #define DTD_SCSI 0x0
  464. #define DTD_IN 0x1
  465. #define DTD_OUT 0x2
  466. #define DTD_NONE 0x3
  467. #define HA_CMD_INQUIRY 0x1
  468. #define HA_CMD_SELF_DIAG 0x2
  469. #define HA_CMD_READ_BUFF 0x3
  470. #define HA_CMD_WRITE_BUFF 0x4
  471. #undef DEBUG_LINKED_COMMANDS
  472. #undef DEBUG_DETECT
  473. #undef DEBUG_INTERRUPT
  474. #undef DEBUG_RESET
  475. #undef DEBUG_GENERATE_ERRORS
  476. #undef DEBUG_GENERATE_ABORTS
  477. #undef DEBUG_GEOMETRY
  478. #define MAX_ISA 3
  479. #define MAX_VESA 1
  480. #define MAX_EISA 0
  481. #define MAX_PCI 0
  482. #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
  483. #define MAX_CHANNEL 1
  484. #define MAX_LUN 8
  485. #define MAX_TARGET 8
  486. #define MAX_MAILBOXES 16
  487. #define MAX_SGLIST 32
  488. #define MAX_SAFE_SGLIST 16
  489. #define MAX_INTERNAL_RETRIES 64
  490. #define MAX_CMD_PER_LUN 2
  491. #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
  492. #define SKIP ULONG_MAX
  493. #define FALSE 0
  494. #define TRUE 1
  495. #define FREE 0
  496. #define IN_USE 1
  497. #define LOCKED 2
  498. #define IN_RESET 3
  499. #define IGNORE 4
  500. #define READY 5
  501. #define ABORTING 6
  502. #define NO_DMA 0xff
  503. #define MAXLOOP 10000
  504. #define TAG_DISABLED 0
  505. #define TAG_SIMPLE 1
  506. #define TAG_ORDERED 2
  507. #define REG_LCL_MASK 0
  508. #define REG_LCL_INTR 1
  509. #define REG_SYS_MASK 2
  510. #define REG_SYS_INTR 3
  511. #define REG_PRODUCT_ID1 4
  512. #define REG_PRODUCT_ID2 5
  513. #define REG_CONFIG1 6
  514. #define REG_CONFIG2 7
  515. #define REG_OGM 8
  516. #define REG_ICM 12
  517. #define REGION_SIZE 13UL
  518. #define BSY_ASSERTED 0x01
  519. #define IRQ_ASSERTED 0x01
  520. #define CMD_RESET 0xc0
  521. #define CMD_OGM_INTR 0x01
  522. #define CMD_CLR_INTR 0x01
  523. #define CMD_ENA_INTR 0x81
  524. #define ASOK 0x00
  525. #define ASST 0x91
  526. #define YESNO(a) ((a) ? 'y' : 'n')
  527. #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
  528. #define PACKED __attribute__((packed))
  529. struct sg_list {
  530. unsigned int address; /* Segment Address */
  531. unsigned int num_bytes; /* Segment Length */
  532. };
  533. /* MailBox SCSI Command Packet */
  534. struct mscp {
  535. #if defined(__BIG_ENDIAN_BITFIELD)
  536. unsigned char sg:1, ca:1, dcn:1, xdir:2, opcode:3;
  537. unsigned char lun: 3, channel:2, target:3;
  538. #else
  539. unsigned char opcode: 3, /* type of command */
  540. xdir: 2, /* data transfer direction */
  541. dcn: 1, /* disable disconnect */
  542. ca: 1, /* use cache (if available) */
  543. sg: 1; /* scatter/gather operation */
  544. unsigned char target: 3, /* SCSI target id */
  545. channel: 2, /* SCSI channel number */
  546. lun: 3; /* SCSI logical unit number */
  547. #endif
  548. unsigned int data_address PACKED; /* transfer data pointer */
  549. unsigned int data_len PACKED; /* length in bytes */
  550. unsigned int link_address PACKED; /* for linking command chains */
  551. unsigned char clink_id; /* identifies command in chain */
  552. unsigned char use_sg; /* (if sg is set) 8 bytes per list */
  553. unsigned char sense_len;
  554. unsigned char cdb_len; /* 6, 10, or 12 */
  555. unsigned char cdb[12]; /* SCSI Command Descriptor Block */
  556. unsigned char adapter_status; /* non-zero indicates HA error */
  557. unsigned char target_status; /* non-zero indicates target error */
  558. unsigned int sense_addr PACKED;
  559. /* Additional fields begin here. */
  560. struct scsi_cmnd *SCpnt;
  561. unsigned int cpp_index; /* cp index */
  562. /* All the cp structure is zero filled by queuecommand except the
  563. following CP_TAIL_SIZE bytes, initialized by detect */
  564. dma_addr_t cp_dma_addr; /* dma handle for this cp structure */
  565. struct sg_list *sglist; /* pointer to the allocated SG list */
  566. };
  567. #define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
  568. struct hostdata {
  569. struct mscp cp[MAX_MAILBOXES]; /* Mailboxes for this board */
  570. unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
  571. unsigned int last_cp_used; /* Index of last mailbox used */
  572. unsigned int iocount; /* Total i/o done for this board */
  573. int board_number; /* Number of this board */
  574. char board_name[16]; /* Name of this board */
  575. int in_reset; /* True if board is doing a reset */
  576. int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
  577. int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
  578. unsigned int retries; /* Number of internal retries */
  579. unsigned long last_retried_pid; /* Pid of last retried command */
  580. unsigned char subversion; /* Bus type, either ISA or ESA */
  581. struct pci_dev *pdev; /* Always NULL */
  582. unsigned char heads;
  583. unsigned char sectors;
  584. char board_id[256]; /* data from INQUIRY on this board */
  585. };
  586. static struct Scsi_Host *sh[MAX_BOARDS + 1];
  587. static const char *driver_name = "Ux4F";
  588. static char sha[MAX_BOARDS];
  589. static DEFINE_SPINLOCK(driver_lock);
  590. /* Initialize num_boards so that ihdlr can work while detect is in progress */
  591. static unsigned int num_boards = MAX_BOARDS;
  592. static unsigned long io_port[] = {
  593. /* Space for MAX_INT_PARAM ports usable while loading as a module */
  594. SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
  595. SKIP, SKIP,
  596. /* Possible ISA/VESA ports */
  597. 0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140,
  598. /* End of list */
  599. 0x0
  600. };
  601. #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
  602. #define BN(board) (HD(board)->board_name)
  603. /* Device is Little Endian */
  604. #define H2DEV(x) cpu_to_le32(x)
  605. #define DEV2H(x) le32_to_cpu(x)
  606. static irqreturn_t do_interrupt_handler(int, void *);
  607. static void flush_dev(struct scsi_device *, unsigned long, unsigned int, unsigned int);
  608. static int do_trace = FALSE;
  609. static int setup_done = FALSE;
  610. static int link_statistics;
  611. static int ext_tran = FALSE;
  612. #if defined(HAVE_OLD_UX4F_FIRMWARE)
  613. static int have_old_firmware = TRUE;
  614. #else
  615. static int have_old_firmware = FALSE;
  616. #endif
  617. #if defined(CONFIG_SCSI_U14_34F_TAGGED_QUEUE)
  618. static int tag_mode = TAG_SIMPLE;
  619. #else
  620. static int tag_mode = TAG_DISABLED;
  621. #endif
  622. #if defined(CONFIG_SCSI_U14_34F_LINKED_COMMANDS)
  623. static int linked_comm = TRUE;
  624. #else
  625. static int linked_comm = FALSE;
  626. #endif
  627. #if defined(CONFIG_SCSI_U14_34F_MAX_TAGS)
  628. static int max_queue_depth = CONFIG_SCSI_U14_34F_MAX_TAGS;
  629. #else
  630. static int max_queue_depth = MAX_CMD_PER_LUN;
  631. #endif
  632. #define MAX_INT_PARAM 10
  633. #define MAX_BOOT_OPTIONS_SIZE 256
  634. static char boot_options[MAX_BOOT_OPTIONS_SIZE];
  635. #if defined(MODULE)
  636. #include <linux/module.h>
  637. #include <linux/moduleparam.h>
  638. module_param_string(u14_34f, boot_options, MAX_BOOT_OPTIONS_SIZE, 0);
  639. MODULE_PARM_DESC(u14_34f, " equivalent to the \"u14-34f=...\" kernel boot " \
  640. "option." \
  641. " Example: modprobe u14-34f \"u14_34f=0x340,0x330,lc:y,tm:0,mq:4\"");
  642. MODULE_AUTHOR("Dario Ballabio");
  643. MODULE_LICENSE("GPL");
  644. MODULE_DESCRIPTION("UltraStor 14F/34F SCSI Driver");
  645. #endif
  646. static int u14_34f_slave_configure(struct scsi_device *dev) {
  647. int j, tqd, utqd;
  648. char *tag_suffix, *link_suffix;
  649. struct Scsi_Host *host = dev->host;
  650. j = ((struct hostdata *) host->hostdata)->board_number;
  651. utqd = MAX_CMD_PER_LUN;
  652. tqd = max_queue_depth;
  653. if (TLDEV(dev->type) && dev->tagged_supported)
  654. if (tag_mode == TAG_SIMPLE) {
  655. scsi_change_queue_depth(dev, tqd);
  656. tag_suffix = ", simple tags";
  657. }
  658. else if (tag_mode == TAG_ORDERED) {
  659. scsi_change_queue_depth(dev, tqd);
  660. tag_suffix = ", ordered tags";
  661. }
  662. else {
  663. scsi_change_queue_depth(dev, tqd);
  664. tag_suffix = ", no tags";
  665. }
  666. else if (TLDEV(dev->type) && linked_comm) {
  667. scsi_change_queue_depth(dev, tqd);
  668. tag_suffix = ", untagged";
  669. }
  670. else {
  671. scsi_change_queue_depth(dev, utqd);
  672. tag_suffix = "";
  673. }
  674. if (TLDEV(dev->type) && linked_comm && dev->queue_depth > 2)
  675. link_suffix = ", sorted";
  676. else if (TLDEV(dev->type))
  677. link_suffix = ", unsorted";
  678. else
  679. link_suffix = "";
  680. sdev_printk(KERN_INFO, dev, "cmds/lun %d%s%s.\n",
  681. dev->queue_depth, link_suffix, tag_suffix);
  682. return FALSE;
  683. }
  684. static int wait_on_busy(unsigned long iobase, unsigned int loop) {
  685. while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED) {
  686. udelay(1L);
  687. if (--loop == 0) return TRUE;
  688. }
  689. return FALSE;
  690. }
  691. static int board_inquiry(unsigned int j) {
  692. struct mscp *cpp;
  693. dma_addr_t id_dma_addr;
  694. unsigned int limit = 0;
  695. unsigned long time;
  696. id_dma_addr = pci_map_single(HD(j)->pdev, HD(j)->board_id,
  697. sizeof(HD(j)->board_id), PCI_DMA_BIDIRECTIONAL);
  698. cpp = &HD(j)->cp[0];
  699. cpp->cp_dma_addr = pci_map_single(HD(j)->pdev, cpp, sizeof(struct mscp),
  700. PCI_DMA_BIDIRECTIONAL);
  701. memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
  702. cpp->opcode = OP_HOST_ADAPTER;
  703. cpp->xdir = DTD_IN;
  704. cpp->data_address = H2DEV(id_dma_addr);
  705. cpp->data_len = H2DEV(sizeof(HD(j)->board_id));
  706. cpp->cdb_len = 6;
  707. cpp->cdb[0] = HA_CMD_INQUIRY;
  708. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  709. printk("%s: board_inquiry, adapter busy.\n", BN(j));
  710. return TRUE;
  711. }
  712. HD(j)->cp_stat[0] = IGNORE;
  713. /* Clear the interrupt indication */
  714. outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
  715. /* Store pointer in OGM address bytes */
  716. outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
  717. /* Issue OGM interrupt */
  718. outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
  719. spin_unlock_irq(&driver_lock);
  720. time = jiffies;
  721. while ((jiffies - time) < HZ && limit++ < 20000) udelay(100L);
  722. spin_lock_irq(&driver_lock);
  723. if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
  724. HD(j)->cp_stat[0] = FREE;
  725. printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
  726. return TRUE;
  727. }
  728. pci_unmap_single(HD(j)->pdev, cpp->cp_dma_addr, sizeof(struct mscp),
  729. PCI_DMA_BIDIRECTIONAL);
  730. pci_unmap_single(HD(j)->pdev, id_dma_addr, sizeof(HD(j)->board_id),
  731. PCI_DMA_BIDIRECTIONAL);
  732. return FALSE;
  733. }
  734. static int port_detect \
  735. (unsigned long port_base, unsigned int j, struct scsi_host_template *tpnt) {
  736. unsigned char irq, dma_channel, subversion, i;
  737. unsigned char in_byte;
  738. char *bus_type, dma_name[16];
  739. /* Allowed BIOS base addresses (NULL indicates reserved) */
  740. unsigned long bios_segment_table[8] = {
  741. 0,
  742. 0xc4000, 0xc8000, 0xcc000, 0xd0000,
  743. 0xd4000, 0xd8000, 0xdc000
  744. };
  745. /* Allowed IRQs */
  746. unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
  747. /* Allowed DMA channels for ISA (0 indicates reserved) */
  748. unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
  749. /* Head/sector mappings */
  750. struct {
  751. unsigned char heads;
  752. unsigned char sectors;
  753. } mapping_table[4] = {
  754. { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
  755. };
  756. struct config_1 {
  757. #if defined(__BIG_ENDIAN_BITFIELD)
  758. unsigned char dma_channel: 2, interrupt:2,
  759. removable_disks_as_fixed:1, bios_segment: 3;
  760. #else
  761. unsigned char bios_segment: 3, removable_disks_as_fixed: 1,
  762. interrupt: 2, dma_channel: 2;
  763. #endif
  764. } config_1;
  765. struct config_2 {
  766. #if defined(__BIG_ENDIAN_BITFIELD)
  767. unsigned char tfr_port: 2, bios_drive_number: 1,
  768. mapping_mode: 2, ha_scsi_id: 3;
  769. #else
  770. unsigned char ha_scsi_id: 3, mapping_mode: 2,
  771. bios_drive_number: 1, tfr_port: 2;
  772. #endif
  773. } config_2;
  774. char name[16];
  775. sprintf(name, "%s%d", driver_name, j);
  776. if (!request_region(port_base, REGION_SIZE, driver_name)) {
  777. #if defined(DEBUG_DETECT)
  778. printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
  779. #endif
  780. goto fail;
  781. }
  782. spin_lock_irq(&driver_lock);
  783. if (inb(port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) goto freelock;
  784. in_byte = inb(port_base + REG_PRODUCT_ID2);
  785. if ((in_byte & 0xf0) != PRODUCT_ID2) goto freelock;
  786. *(char *)&config_1 = inb(port_base + REG_CONFIG1);
  787. *(char *)&config_2 = inb(port_base + REG_CONFIG2);
  788. irq = interrupt_table[config_1.interrupt];
  789. dma_channel = dma_channel_table[config_1.dma_channel];
  790. subversion = (in_byte & 0x0f);
  791. /* Board detected, allocate its IRQ */
  792. if (request_irq(irq, do_interrupt_handler,
  793. (subversion == ESA) ? IRQF_SHARED : 0,
  794. driver_name, (void *) &sha[j])) {
  795. printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
  796. goto freelock;
  797. }
  798. if (subversion == ISA && request_dma(dma_channel, driver_name)) {
  799. printk("%s: unable to allocate DMA channel %u, detaching.\n",
  800. name, dma_channel);
  801. goto freeirq;
  802. }
  803. if (have_old_firmware) tpnt->use_clustering = DISABLE_CLUSTERING;
  804. spin_unlock_irq(&driver_lock);
  805. sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
  806. spin_lock_irq(&driver_lock);
  807. if (sh[j] == NULL) {
  808. printk("%s: unable to register host, detaching.\n", name);
  809. goto freedma;
  810. }
  811. sh[j]->io_port = port_base;
  812. sh[j]->unique_id = port_base;
  813. sh[j]->n_io_port = REGION_SIZE;
  814. sh[j]->base = bios_segment_table[config_1.bios_segment];
  815. sh[j]->irq = irq;
  816. sh[j]->sg_tablesize = MAX_SGLIST;
  817. sh[j]->this_id = config_2.ha_scsi_id;
  818. sh[j]->can_queue = MAX_MAILBOXES;
  819. sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
  820. #if defined(DEBUG_DETECT)
  821. {
  822. unsigned char sys_mask, lcl_mask;
  823. sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
  824. lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
  825. printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
  826. }
  827. #endif
  828. /* Probably a bogus host scsi id, set it to the dummy value */
  829. if (sh[j]->this_id == 0) sh[j]->this_id = -1;
  830. /* If BIOS is disabled, force enable interrupts */
  831. if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
  832. memset(HD(j), 0, sizeof(struct hostdata));
  833. HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
  834. HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
  835. HD(j)->subversion = subversion;
  836. HD(j)->pdev = NULL;
  837. HD(j)->board_number = j;
  838. if (have_old_firmware) sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
  839. if (HD(j)->subversion == ESA) {
  840. sh[j]->unchecked_isa_dma = FALSE;
  841. sh[j]->dma_channel = NO_DMA;
  842. sprintf(BN(j), "U34F%d", j);
  843. bus_type = "VESA";
  844. }
  845. else {
  846. unsigned long flags;
  847. sh[j]->unchecked_isa_dma = TRUE;
  848. flags=claim_dma_lock();
  849. disable_dma(dma_channel);
  850. clear_dma_ff(dma_channel);
  851. set_dma_mode(dma_channel, DMA_MODE_CASCADE);
  852. enable_dma(dma_channel);
  853. release_dma_lock(flags);
  854. sh[j]->dma_channel = dma_channel;
  855. sprintf(BN(j), "U14F%d", j);
  856. bus_type = "ISA";
  857. }
  858. sh[j]->max_channel = MAX_CHANNEL - 1;
  859. sh[j]->max_id = MAX_TARGET;
  860. sh[j]->max_lun = MAX_LUN;
  861. if (HD(j)->subversion == ISA && !board_inquiry(j)) {
  862. HD(j)->board_id[40] = 0;
  863. if (strcmp(&HD(j)->board_id[32], "06000600")) {
  864. printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
  865. printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
  866. BN(j), &HD(j)->board_id[32]);
  867. sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
  868. sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
  869. }
  870. }
  871. if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
  872. else sprintf(dma_name, "DMA %u", dma_channel);
  873. spin_unlock_irq(&driver_lock);
  874. for (i = 0; i < sh[j]->can_queue; i++)
  875. HD(j)->cp[i].cp_dma_addr = pci_map_single(HD(j)->pdev,
  876. &HD(j)->cp[i], sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
  877. for (i = 0; i < sh[j]->can_queue; i++)
  878. if (! ((&HD(j)->cp[i])->sglist = kmalloc(
  879. sh[j]->sg_tablesize * sizeof(struct sg_list),
  880. (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
  881. printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
  882. goto release;
  883. }
  884. if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
  885. max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
  886. if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
  887. if (tag_mode != TAG_DISABLED && tag_mode != TAG_SIMPLE)
  888. tag_mode = TAG_ORDERED;
  889. if (j == 0) {
  890. printk("UltraStor 14F/34F: Copyright (C) 1994-2003 Dario Ballabio.\n");
  891. printk("%s config options -> of:%c, tm:%d, lc:%c, mq:%d, et:%c.\n",
  892. driver_name, YESNO(have_old_firmware), tag_mode,
  893. YESNO(linked_comm), max_queue_depth, YESNO(ext_tran));
  894. }
  895. printk("%s: %s 0x%03lx, BIOS 0x%05x, IRQ %u, %s, SG %d, MB %d.\n",
  896. BN(j), bus_type, (unsigned long)sh[j]->io_port, (int)sh[j]->base,
  897. sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
  898. if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
  899. printk("%s: wide SCSI support enabled, max_id %u, max_lun %llu.\n",
  900. BN(j), sh[j]->max_id, sh[j]->max_lun);
  901. for (i = 0; i <= sh[j]->max_channel; i++)
  902. printk("%s: SCSI channel %u enabled, host target ID %d.\n",
  903. BN(j), i, sh[j]->this_id);
  904. return TRUE;
  905. freedma:
  906. if (subversion == ISA) free_dma(dma_channel);
  907. freeirq:
  908. free_irq(irq, &sha[j]);
  909. freelock:
  910. spin_unlock_irq(&driver_lock);
  911. release_region(port_base, REGION_SIZE);
  912. fail:
  913. return FALSE;
  914. release:
  915. u14_34f_release(sh[j]);
  916. return FALSE;
  917. }
  918. static void internal_setup(char *str, int *ints) {
  919. int i, argc = ints[0];
  920. char *cur = str, *pc;
  921. if (argc > 0) {
  922. if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
  923. for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
  924. io_port[i] = 0;
  925. setup_done = TRUE;
  926. }
  927. while (cur && (pc = strchr(cur, ':'))) {
  928. int val = 0, c = *++pc;
  929. if (c == 'n' || c == 'N') val = FALSE;
  930. else if (c == 'y' || c == 'Y') val = TRUE;
  931. else val = (int) simple_strtoul(pc, NULL, 0);
  932. if (!strncmp(cur, "lc:", 3)) linked_comm = val;
  933. else if (!strncmp(cur, "of:", 3)) have_old_firmware = val;
  934. else if (!strncmp(cur, "tm:", 3)) tag_mode = val;
  935. else if (!strncmp(cur, "tc:", 3)) tag_mode = val;
  936. else if (!strncmp(cur, "mq:", 3)) max_queue_depth = val;
  937. else if (!strncmp(cur, "ls:", 3)) link_statistics = val;
  938. else if (!strncmp(cur, "et:", 3)) ext_tran = val;
  939. if ((cur = strchr(cur, ','))) ++cur;
  940. }
  941. return;
  942. }
  943. static int option_setup(char *str) {
  944. int ints[MAX_INT_PARAM];
  945. char *cur = str;
  946. int i = 1;
  947. while (cur && isdigit(*cur) && i < MAX_INT_PARAM) {
  948. ints[i++] = simple_strtoul(cur, NULL, 0);
  949. if ((cur = strchr(cur, ',')) != NULL) cur++;
  950. }
  951. ints[0] = i - 1;
  952. internal_setup(cur, ints);
  953. return 1;
  954. }
  955. static int u14_34f_detect(struct scsi_host_template *tpnt) {
  956. unsigned int j = 0, k;
  957. tpnt->proc_name = "u14-34f";
  958. if(strlen(boot_options)) option_setup(boot_options);
  959. #if defined(MODULE)
  960. /* io_port could have been modified when loading as a module */
  961. if(io_port[0] != SKIP) {
  962. setup_done = TRUE;
  963. io_port[MAX_INT_PARAM] = 0;
  964. }
  965. #endif
  966. for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
  967. for (k = 0; io_port[k]; k++) {
  968. if (io_port[k] == SKIP) continue;
  969. if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
  970. }
  971. num_boards = j;
  972. return j;
  973. }
  974. static void map_dma(unsigned int i, unsigned int j) {
  975. unsigned int data_len = 0;
  976. unsigned int k, pci_dir;
  977. int count;
  978. struct scatterlist *sg;
  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. SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE));
  986. cpp->sense_len = SCSI_SENSE_BUFFERSIZE;
  987. if (scsi_bufflen(SCpnt)) {
  988. count = scsi_dma_map(SCpnt);
  989. BUG_ON(count < 0);
  990. scsi_for_each_sg(SCpnt, sg, count, k) {
  991. cpp->sglist[k].address = H2DEV(sg_dma_address(sg));
  992. cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(sg));
  993. data_len += sg->length;
  994. }
  995. cpp->sg = TRUE;
  996. cpp->use_sg = scsi_sg_count(SCpnt);
  997. cpp->data_address =
  998. H2DEV(pci_map_single(HD(j)->pdev, cpp->sglist,
  999. cpp->use_sg * sizeof(struct sg_list),
  1000. pci_dir));
  1001. cpp->data_len = H2DEV(data_len);
  1002. } else {
  1003. pci_dir = PCI_DMA_BIDIRECTIONAL;
  1004. cpp->data_len = H2DEV(scsi_bufflen(SCpnt));
  1005. }
  1006. }
  1007. static void unmap_dma(unsigned int i, unsigned int j) {
  1008. unsigned int pci_dir;
  1009. struct mscp *cpp;
  1010. struct scsi_cmnd *SCpnt;
  1011. cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
  1012. pci_dir = SCpnt->sc_data_direction;
  1013. if (DEV2H(cpp->sense_addr))
  1014. pci_unmap_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
  1015. DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
  1016. scsi_dma_unmap(SCpnt);
  1017. if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
  1018. if (DEV2H(cpp->data_address))
  1019. pci_unmap_single(HD(j)->pdev, DEV2H(cpp->data_address),
  1020. DEV2H(cpp->data_len), pci_dir);
  1021. }
  1022. static void sync_dma(unsigned int i, unsigned int j) {
  1023. unsigned int pci_dir;
  1024. struct mscp *cpp;
  1025. struct scsi_cmnd *SCpnt;
  1026. cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
  1027. pci_dir = SCpnt->sc_data_direction;
  1028. if (DEV2H(cpp->sense_addr))
  1029. pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->sense_addr),
  1030. DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
  1031. if (scsi_sg_count(SCpnt))
  1032. pci_dma_sync_sg_for_cpu(HD(j)->pdev, scsi_sglist(SCpnt),
  1033. scsi_sg_count(SCpnt), pci_dir);
  1034. if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
  1035. if (DEV2H(cpp->data_address))
  1036. pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->data_address),
  1037. DEV2H(cpp->data_len), pci_dir);
  1038. }
  1039. static void scsi_to_dev_dir(unsigned int i, unsigned int j) {
  1040. unsigned int k;
  1041. static const unsigned char data_out_cmds[] = {
  1042. 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
  1043. 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
  1044. 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
  1045. };
  1046. static const unsigned char data_none_cmds[] = {
  1047. 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
  1048. 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
  1049. 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
  1050. };
  1051. struct mscp *cpp;
  1052. struct scsi_cmnd *SCpnt;
  1053. cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
  1054. if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1055. cpp->xdir = DTD_IN;
  1056. return;
  1057. }
  1058. else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
  1059. cpp->xdir = DTD_OUT;
  1060. return;
  1061. }
  1062. else if (SCpnt->sc_data_direction == DMA_NONE) {
  1063. cpp->xdir = DTD_NONE;
  1064. return;
  1065. }
  1066. if (SCpnt->sc_data_direction != DMA_BIDIRECTIONAL)
  1067. panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n", BN(j));
  1068. cpp->xdir = DTD_IN;
  1069. for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
  1070. if (SCpnt->cmnd[0] == data_out_cmds[k]) {
  1071. cpp->xdir = DTD_OUT;
  1072. break;
  1073. }
  1074. if (cpp->xdir == DTD_IN)
  1075. for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
  1076. if (SCpnt->cmnd[0] == data_none_cmds[k]) {
  1077. cpp->xdir = DTD_NONE;
  1078. break;
  1079. }
  1080. }
  1081. static int u14_34f_queuecommand_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *)) {
  1082. unsigned int i, j, k;
  1083. struct mscp *cpp;
  1084. /* j is the board number */
  1085. j = ((struct hostdata *) SCpnt->device->host->hostdata)->board_number;
  1086. if (SCpnt->host_scribble)
  1087. panic("%s: qcomm, SCpnt %p already active.\n",
  1088. BN(j), SCpnt);
  1089. /* i is the mailbox number, look for the first free mailbox
  1090. starting from last_cp_used */
  1091. i = HD(j)->last_cp_used + 1;
  1092. for (k = 0; k < sh[j]->can_queue; k++, i++) {
  1093. if (i >= sh[j]->can_queue) i = 0;
  1094. if (HD(j)->cp_stat[i] == FREE) {
  1095. HD(j)->last_cp_used = i;
  1096. break;
  1097. }
  1098. }
  1099. if (k == sh[j]->can_queue) {
  1100. printk("%s: qcomm, no free mailbox.\n", BN(j));
  1101. return 1;
  1102. }
  1103. /* Set pointer to control packet structure */
  1104. cpp = &HD(j)->cp[i];
  1105. memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
  1106. SCpnt->scsi_done = done;
  1107. cpp->cpp_index = i;
  1108. SCpnt->host_scribble = (unsigned char *) &cpp->cpp_index;
  1109. if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%u.\n",
  1110. BN(j), i, SCpnt->device->channel, SCpnt->device->id,
  1111. (u8)SCpnt->device->lun);
  1112. cpp->opcode = OP_SCSI;
  1113. cpp->channel = SCpnt->device->channel;
  1114. cpp->target = SCpnt->device->id;
  1115. cpp->lun = (u8)SCpnt->device->lun;
  1116. cpp->SCpnt = SCpnt;
  1117. cpp->cdb_len = SCpnt->cmd_len;
  1118. memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
  1119. /* Use data transfer direction SCpnt->sc_data_direction */
  1120. scsi_to_dev_dir(i, j);
  1121. /* Map DMA buffers and SG list */
  1122. map_dma(i, j);
  1123. if (linked_comm && SCpnt->device->queue_depth > 2
  1124. && TLDEV(SCpnt->device->type)) {
  1125. HD(j)->cp_stat[i] = READY;
  1126. flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), j, FALSE);
  1127. return 0;
  1128. }
  1129. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1130. unmap_dma(i, j);
  1131. SCpnt->host_scribble = NULL;
  1132. scmd_printk(KERN_INFO, SCpnt,
  1133. "qcomm, adapter busy.\n");
  1134. return 1;
  1135. }
  1136. /* Store pointer in OGM address bytes */
  1137. outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
  1138. /* Issue OGM interrupt */
  1139. outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
  1140. HD(j)->cp_stat[i] = IN_USE;
  1141. return 0;
  1142. }
  1143. static DEF_SCSI_QCMD(u14_34f_queuecommand)
  1144. static int u14_34f_eh_abort(struct scsi_cmnd *SCarg) {
  1145. unsigned int i, j;
  1146. j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
  1147. if (SCarg->host_scribble == NULL) {
  1148. scmd_printk(KERN_INFO, SCarg, "abort, command inactive.\n");
  1149. return SUCCESS;
  1150. }
  1151. i = *(unsigned int *)SCarg->host_scribble;
  1152. scmd_printk(KERN_INFO, SCarg, "abort, mbox %d.\n", i);
  1153. if (i >= sh[j]->can_queue)
  1154. panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
  1155. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1156. printk("%s: abort, timeout error.\n", BN(j));
  1157. return FAILED;
  1158. }
  1159. if (HD(j)->cp_stat[i] == FREE) {
  1160. printk("%s: abort, mbox %d is free.\n", BN(j), i);
  1161. return SUCCESS;
  1162. }
  1163. if (HD(j)->cp_stat[i] == IN_USE) {
  1164. printk("%s: abort, mbox %d is in use.\n", BN(j), i);
  1165. if (SCarg != HD(j)->cp[i].SCpnt)
  1166. panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
  1167. BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
  1168. if (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED)
  1169. printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
  1170. return FAILED;
  1171. }
  1172. if (HD(j)->cp_stat[i] == IN_RESET) {
  1173. printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
  1174. return FAILED;
  1175. }
  1176. if (HD(j)->cp_stat[i] == LOCKED) {
  1177. printk("%s: abort, mbox %d is locked.\n", BN(j), i);
  1178. return SUCCESS;
  1179. }
  1180. if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
  1181. unmap_dma(i, j);
  1182. SCarg->result = DID_ABORT << 16;
  1183. SCarg->host_scribble = NULL;
  1184. HD(j)->cp_stat[i] = FREE;
  1185. printk("%s, abort, mbox %d ready, DID_ABORT, done.\n", BN(j), i);
  1186. SCarg->scsi_done(SCarg);
  1187. return SUCCESS;
  1188. }
  1189. panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
  1190. }
  1191. static int u14_34f_eh_host_reset(struct scsi_cmnd *SCarg) {
  1192. unsigned int i, j, k, c, limit = 0;
  1193. unsigned long time;
  1194. int arg_done = FALSE;
  1195. struct scsi_cmnd *SCpnt;
  1196. j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
  1197. scmd_printk(KERN_INFO, SCarg, "reset, enter.\n");
  1198. spin_lock_irq(sh[j]->host_lock);
  1199. if (SCarg->host_scribble == NULL)
  1200. printk("%s: reset, inactive.\n", BN(j));
  1201. if (HD(j)->in_reset) {
  1202. printk("%s: reset, exit, already in reset.\n", BN(j));
  1203. spin_unlock_irq(sh[j]->host_lock);
  1204. return FAILED;
  1205. }
  1206. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1207. printk("%s: reset, exit, timeout error.\n", BN(j));
  1208. spin_unlock_irq(sh[j]->host_lock);
  1209. return FAILED;
  1210. }
  1211. HD(j)->retries = 0;
  1212. for (c = 0; c <= sh[j]->max_channel; c++)
  1213. for (k = 0; k < sh[j]->max_id; k++) {
  1214. HD(j)->target_redo[k][c] = TRUE;
  1215. HD(j)->target_to[k][c] = 0;
  1216. }
  1217. for (i = 0; i < sh[j]->can_queue; i++) {
  1218. if (HD(j)->cp_stat[i] == FREE) continue;
  1219. if (HD(j)->cp_stat[i] == LOCKED) {
  1220. HD(j)->cp_stat[i] = FREE;
  1221. printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
  1222. continue;
  1223. }
  1224. if (!(SCpnt = HD(j)->cp[i].SCpnt))
  1225. panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
  1226. if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
  1227. HD(j)->cp_stat[i] = ABORTING;
  1228. printk("%s: reset, mbox %d aborting.\n", BN(j), i);
  1229. }
  1230. else {
  1231. HD(j)->cp_stat[i] = IN_RESET;
  1232. printk("%s: reset, mbox %d in reset.\n", BN(j), i);
  1233. }
  1234. if (SCpnt->host_scribble == NULL)
  1235. panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
  1236. if (*(unsigned int *)SCpnt->host_scribble != i)
  1237. panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
  1238. if (SCpnt->scsi_done == NULL)
  1239. panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
  1240. if (SCpnt == SCarg) arg_done = TRUE;
  1241. }
  1242. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1243. printk("%s: reset, cannot reset, timeout error.\n", BN(j));
  1244. spin_unlock_irq(sh[j]->host_lock);
  1245. return FAILED;
  1246. }
  1247. outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
  1248. printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
  1249. #if defined(DEBUG_RESET)
  1250. do_trace = TRUE;
  1251. #endif
  1252. HD(j)->in_reset = TRUE;
  1253. spin_unlock_irq(sh[j]->host_lock);
  1254. time = jiffies;
  1255. while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
  1256. spin_lock_irq(sh[j]->host_lock);
  1257. printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
  1258. for (i = 0; i < sh[j]->can_queue; i++) {
  1259. if (HD(j)->cp_stat[i] == IN_RESET) {
  1260. SCpnt = HD(j)->cp[i].SCpnt;
  1261. unmap_dma(i, j);
  1262. SCpnt->result = DID_RESET << 16;
  1263. SCpnt->host_scribble = NULL;
  1264. /* This mailbox is still waiting for its interrupt */
  1265. HD(j)->cp_stat[i] = LOCKED;
  1266. printk("%s, reset, mbox %d locked, DID_RESET, done.\n", BN(j), i);
  1267. }
  1268. else if (HD(j)->cp_stat[i] == ABORTING) {
  1269. SCpnt = HD(j)->cp[i].SCpnt;
  1270. unmap_dma(i, j);
  1271. SCpnt->result = DID_RESET << 16;
  1272. SCpnt->host_scribble = NULL;
  1273. /* This mailbox was never queued to the adapter */
  1274. HD(j)->cp_stat[i] = FREE;
  1275. printk("%s, reset, mbox %d aborting, DID_RESET, done.\n", BN(j), i);
  1276. }
  1277. else
  1278. /* Any other mailbox has already been set free by interrupt */
  1279. continue;
  1280. SCpnt->scsi_done(SCpnt);
  1281. }
  1282. HD(j)->in_reset = FALSE;
  1283. do_trace = FALSE;
  1284. if (arg_done) printk("%s: reset, exit, done.\n", BN(j));
  1285. else printk("%s: reset, exit.\n", BN(j));
  1286. spin_unlock_irq(sh[j]->host_lock);
  1287. return SUCCESS;
  1288. }
  1289. static int u14_34f_bios_param(struct scsi_device *disk,
  1290. struct block_device *bdev, sector_t capacity, int *dkinfo) {
  1291. unsigned int j = 0;
  1292. unsigned int size = capacity;
  1293. dkinfo[0] = HD(j)->heads;
  1294. dkinfo[1] = HD(j)->sectors;
  1295. dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
  1296. if (ext_tran && (scsicam_bios_param(bdev, capacity, dkinfo) < 0)) {
  1297. dkinfo[0] = 255;
  1298. dkinfo[1] = 63;
  1299. dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
  1300. }
  1301. #if defined (DEBUG_GEOMETRY)
  1302. printk ("%s: bios_param, head=%d, sec=%d, cyl=%d.\n", driver_name,
  1303. dkinfo[0], dkinfo[1], dkinfo[2]);
  1304. #endif
  1305. return FALSE;
  1306. }
  1307. static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
  1308. unsigned int rev) {
  1309. unsigned int i, j, k, y;
  1310. unsigned long x;
  1311. for (i = 0; i < n - 1; i++) {
  1312. k = i;
  1313. for (j = k + 1; j < n; j++)
  1314. if (rev) {
  1315. if (sk[j] > sk[k]) k = j;
  1316. }
  1317. else {
  1318. if (sk[j] < sk[k]) k = j;
  1319. }
  1320. if (k != i) {
  1321. x = sk[k]; sk[k] = sk[i]; sk[i] = x;
  1322. y = da[k]; da[k] = da[i]; da[i] = y;
  1323. }
  1324. }
  1325. return;
  1326. }
  1327. static int reorder(unsigned int j, unsigned long cursec,
  1328. unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
  1329. struct scsi_cmnd *SCpnt;
  1330. struct mscp *cpp;
  1331. unsigned int k, n;
  1332. unsigned int rev = FALSE, s = TRUE, r = TRUE;
  1333. unsigned int input_only = TRUE, overlap = FALSE;
  1334. unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
  1335. unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
  1336. unsigned long ioseek = 0;
  1337. static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
  1338. static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
  1339. static unsigned int readysorted = 0, revcount = 0;
  1340. static unsigned long seeksorted = 0, seeknosort = 0;
  1341. if (link_statistics && !(++flushcount % link_statistics))
  1342. printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
  1343. " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
  1344. ovlcount, readycount, readysorted, sortcount, revcount,
  1345. seeknosort / (readycount + 1),
  1346. seeksorted / (readycount + 1));
  1347. if (n_ready <= 1) return FALSE;
  1348. for (n = 0; n < n_ready; n++) {
  1349. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1350. if (!(cpp->xdir == DTD_IN)) input_only = FALSE;
  1351. if (blk_rq_pos(SCpnt->request) < minsec)
  1352. minsec = blk_rq_pos(SCpnt->request);
  1353. if (blk_rq_pos(SCpnt->request) > maxsec)
  1354. maxsec = blk_rq_pos(SCpnt->request);
  1355. sl[n] = blk_rq_pos(SCpnt->request);
  1356. ioseek += blk_rq_sectors(SCpnt->request);
  1357. if (!n) continue;
  1358. if (sl[n] < sl[n - 1]) s = FALSE;
  1359. if (sl[n] > sl[n - 1]) r = FALSE;
  1360. if (link_statistics) {
  1361. if (sl[n] > sl[n - 1])
  1362. seek += sl[n] - sl[n - 1];
  1363. else
  1364. seek += sl[n - 1] - sl[n];
  1365. }
  1366. }
  1367. if (link_statistics) {
  1368. if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
  1369. }
  1370. if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
  1371. if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
  1372. if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
  1373. if (!input_only) for (n = 0; n < n_ready; n++) {
  1374. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1375. ll[n] = blk_rq_sectors(SCpnt->request); pl[n] = SCpnt->serial_number;
  1376. if (!n) continue;
  1377. if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
  1378. || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
  1379. }
  1380. if (overlap) sort(pl, il, n_ready, FALSE);
  1381. if (link_statistics) {
  1382. if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
  1383. batchcount++; readycount += n_ready; seeknosort += seek / 1024;
  1384. if (input_only) inputcount++;
  1385. if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
  1386. else seeksorted += (iseek + maxsec - minsec) / 1024;
  1387. if (rev && !r) { revcount++; readysorted += n_ready; }
  1388. if (!rev && !s) { sortcount++; readysorted += n_ready; }
  1389. }
  1390. #if defined(DEBUG_LINKED_COMMANDS)
  1391. if (link_statistics && (overlap || !(flushcount % link_statistics)))
  1392. for (n = 0; n < n_ready; n++) {
  1393. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1394. printk("%s %d.%d:%llu mb %d fc %d nr %d sec %ld ns %u"\
  1395. " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
  1396. (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
  1397. (u8)SCpnt->lun, k, flushcount, n_ready,
  1398. blk_rq_pos(SCpnt->request), blk_rq_sectors(SCpnt->request),
  1399. cursec, YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
  1400. YESNO(overlap), cpp->xdir);
  1401. }
  1402. #endif
  1403. return overlap;
  1404. }
  1405. static void flush_dev(struct scsi_device *dev, unsigned long cursec, unsigned int j,
  1406. unsigned int ihdlr) {
  1407. struct scsi_cmnd *SCpnt;
  1408. struct mscp *cpp;
  1409. unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
  1410. for (k = 0; k < sh[j]->can_queue; k++) {
  1411. if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
  1412. cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1413. if (SCpnt->device != dev) continue;
  1414. if (HD(j)->cp_stat[k] == IN_USE) return;
  1415. il[n_ready++] = k;
  1416. }
  1417. if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
  1418. for (n = 0; n < n_ready; n++) {
  1419. k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
  1420. if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
  1421. scmd_printk(KERN_INFO, SCpnt,
  1422. "%s, mbox %d, adapter"
  1423. " busy, will abort.\n", (ihdlr ? "ihdlr" : "qcomm"),
  1424. k);
  1425. HD(j)->cp_stat[k] = ABORTING;
  1426. continue;
  1427. }
  1428. outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
  1429. outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
  1430. HD(j)->cp_stat[k] = IN_USE;
  1431. }
  1432. }
  1433. static irqreturn_t ihdlr(unsigned int j)
  1434. {
  1435. struct scsi_cmnd *SCpnt;
  1436. unsigned int i, k, c, status, tstatus, reg, ret;
  1437. struct mscp *spp, *cpp;
  1438. int irq = sh[j]->irq;
  1439. /* Check if this board need to be serviced */
  1440. if (!((reg = inb(sh[j]->io_port + REG_SYS_INTR)) & IRQ_ASSERTED)) goto none;
  1441. HD(j)->iocount++;
  1442. if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
  1443. HD(j)->iocount);
  1444. /* Check if this board is still busy */
  1445. if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
  1446. outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
  1447. printk("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
  1448. BN(j), irq, reg, HD(j)->iocount);
  1449. goto none;
  1450. }
  1451. ret = inl(sh[j]->io_port + REG_ICM);
  1452. /* Clear interrupt pending flag */
  1453. outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
  1454. /* Find the mailbox to be serviced on this board */
  1455. for (i = 0; i < sh[j]->can_queue; i++)
  1456. if (H2DEV(HD(j)->cp[i].cp_dma_addr) == ret) break;
  1457. if (i >= sh[j]->can_queue)
  1458. panic("%s: ihdlr, invalid mscp bus address %p, cp0 %p.\n", BN(j),
  1459. (void *)ret, (void *)H2DEV(HD(j)->cp[0].cp_dma_addr));
  1460. cpp = &(HD(j)->cp[i]);
  1461. spp = cpp;
  1462. #if defined(DEBUG_GENERATE_ABORTS)
  1463. if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) goto handled;
  1464. #endif
  1465. if (HD(j)->cp_stat[i] == IGNORE) {
  1466. HD(j)->cp_stat[i] = FREE;
  1467. goto handled;
  1468. }
  1469. else if (HD(j)->cp_stat[i] == LOCKED) {
  1470. HD(j)->cp_stat[i] = FREE;
  1471. printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
  1472. HD(j)->iocount);
  1473. goto handled;
  1474. }
  1475. else if (HD(j)->cp_stat[i] == FREE) {
  1476. printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
  1477. HD(j)->iocount);
  1478. goto handled;
  1479. }
  1480. else if (HD(j)->cp_stat[i] == IN_RESET)
  1481. printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
  1482. else if (HD(j)->cp_stat[i] != IN_USE)
  1483. panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
  1484. BN(j), i, HD(j)->cp_stat[i]);
  1485. HD(j)->cp_stat[i] = FREE;
  1486. SCpnt = cpp->SCpnt;
  1487. if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
  1488. if (SCpnt->host_scribble == NULL)
  1489. panic("%s: ihdlr, mbox %d, SCpnt %p garbled.\n", BN(j), i,
  1490. SCpnt);
  1491. if (*(unsigned int *)SCpnt->host_scribble != i)
  1492. panic("%s: ihdlr, mbox %d, index mismatch %d.\n",
  1493. BN(j), i, *(unsigned int *)SCpnt->host_scribble);
  1494. sync_dma(i, j);
  1495. if (linked_comm && SCpnt->device->queue_depth > 2
  1496. && TLDEV(SCpnt->device->type))
  1497. flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), j, TRUE);
  1498. tstatus = status_byte(spp->target_status);
  1499. #if defined(DEBUG_GENERATE_ERRORS)
  1500. if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
  1501. spp->adapter_status = 0x01;
  1502. #endif
  1503. switch (spp->adapter_status) {
  1504. case ASOK: /* status OK */
  1505. /* Forces a reset if a disk drive keeps returning BUSY */
  1506. if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
  1507. status = DID_ERROR << 16;
  1508. /* If there was a bus reset, redo operation on each target */
  1509. else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
  1510. && HD(j)->target_redo[scmd_id(SCpnt)][scmd_channel(SCpnt)])
  1511. status = DID_BUS_BUSY << 16;
  1512. /* Works around a flaw in scsi.c */
  1513. else if (tstatus == CHECK_CONDITION
  1514. && SCpnt->device->type == TYPE_DISK
  1515. && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
  1516. status = DID_BUS_BUSY << 16;
  1517. else
  1518. status = DID_OK << 16;
  1519. if (tstatus == GOOD)
  1520. HD(j)->target_redo[scmd_id(SCpnt)][scmd_channel(SCpnt)] = FALSE;
  1521. if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
  1522. (!(tstatus == CHECK_CONDITION && HD(j)->iocount <= 1000 &&
  1523. (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
  1524. scmd_printk(KERN_INFO, SCpnt,
  1525. "ihdlr, target_status 0x%x, sense key 0x%x.\n",
  1526. spp->target_status,
  1527. SCpnt->sense_buffer[2]);
  1528. HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)] = 0;
  1529. if (HD(j)->last_retried_pid == SCpnt->serial_number) HD(j)->retries = 0;
  1530. break;
  1531. case ASST: /* Selection Time Out */
  1532. if (HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)] > 1)
  1533. status = DID_ERROR << 16;
  1534. else {
  1535. status = DID_TIME_OUT << 16;
  1536. HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)]++;
  1537. }
  1538. break;
  1539. /* Perform a limited number of internal retries */
  1540. case 0x93: /* Unexpected bus free */
  1541. case 0x94: /* Target bus phase sequence failure */
  1542. case 0x96: /* Illegal SCSI command */
  1543. case 0xa3: /* SCSI bus reset error */
  1544. for (c = 0; c <= sh[j]->max_channel; c++)
  1545. for (k = 0; k < sh[j]->max_id; k++)
  1546. HD(j)->target_redo[k][c] = TRUE;
  1547. case 0x92: /* Data over/under-run */
  1548. if (SCpnt->device->type != TYPE_TAPE
  1549. && HD(j)->retries < MAX_INTERNAL_RETRIES) {
  1550. #if defined(DID_SOFT_ERROR)
  1551. status = DID_SOFT_ERROR << 16;
  1552. #else
  1553. status = DID_BUS_BUSY << 16;
  1554. #endif
  1555. HD(j)->retries++;
  1556. HD(j)->last_retried_pid = SCpnt->serial_number;
  1557. }
  1558. else
  1559. status = DID_ERROR << 16;
  1560. break;
  1561. case 0x01: /* Invalid command */
  1562. case 0x02: /* Invalid parameters */
  1563. case 0x03: /* Invalid data list */
  1564. case 0x84: /* SCSI bus abort error */
  1565. case 0x9b: /* Auto request sense error */
  1566. case 0x9f: /* Unexpected command complete message error */
  1567. case 0xff: /* Invalid parameter in the S/G list */
  1568. default:
  1569. status = DID_ERROR << 16;
  1570. break;
  1571. }
  1572. SCpnt->result = status | spp->target_status;
  1573. #if defined(DEBUG_INTERRUPT)
  1574. if (SCpnt->result || do_trace)
  1575. #else
  1576. if ((spp->adapter_status != ASOK && HD(j)->iocount > 1000) ||
  1577. (spp->adapter_status != ASOK &&
  1578. spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
  1579. do_trace || msg_byte(spp->target_status))
  1580. #endif
  1581. scmd_printk(KERN_INFO, SCpnt, "ihdlr, mbox %2d, err 0x%x:%x,"\
  1582. " reg 0x%x, count %d.\n",
  1583. i, spp->adapter_status, spp->target_status,
  1584. reg, HD(j)->iocount);
  1585. unmap_dma(i, j);
  1586. /* Set the command state to inactive */
  1587. SCpnt->host_scribble = NULL;
  1588. SCpnt->scsi_done(SCpnt);
  1589. if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
  1590. HD(j)->iocount);
  1591. handled:
  1592. return IRQ_HANDLED;
  1593. none:
  1594. return IRQ_NONE;
  1595. }
  1596. static irqreturn_t do_interrupt_handler(int irq, void *shap) {
  1597. unsigned int j;
  1598. unsigned long spin_flags;
  1599. irqreturn_t ret;
  1600. /* Check if the interrupt must be processed by this handler */
  1601. if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return IRQ_NONE;
  1602. spin_lock_irqsave(sh[j]->host_lock, spin_flags);
  1603. ret = ihdlr(j);
  1604. spin_unlock_irqrestore(sh[j]->host_lock, spin_flags);
  1605. return ret;
  1606. }
  1607. static int u14_34f_release(struct Scsi_Host *shpnt) {
  1608. unsigned int i, j;
  1609. for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
  1610. if (sh[j] == NULL)
  1611. panic("%s: release, invalid Scsi_Host pointer.\n", driver_name);
  1612. for (i = 0; i < sh[j]->can_queue; i++)
  1613. kfree((&HD(j)->cp[i])->sglist);
  1614. for (i = 0; i < sh[j]->can_queue; i++)
  1615. pci_unmap_single(HD(j)->pdev, HD(j)->cp[i].cp_dma_addr,
  1616. sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
  1617. free_irq(sh[j]->irq, &sha[j]);
  1618. if (sh[j]->dma_channel != NO_DMA)
  1619. free_dma(sh[j]->dma_channel);
  1620. release_region(sh[j]->io_port, sh[j]->n_io_port);
  1621. scsi_unregister(sh[j]);
  1622. return FALSE;
  1623. }
  1624. #include "scsi_module.c"
  1625. #ifndef MODULE
  1626. __setup("u14-34f=", option_setup);
  1627. #endif /* end MODULE */