aic7xxx_osm.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  1. /*
  2. * Adaptec AIC7xxx device driver for Linux.
  3. *
  4. * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.c#235 $
  5. *
  6. * Copyright (c) 1994 John Aycock
  7. * The University of Calgary Department of Computer Science.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, write to
  21. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F
  24. * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA
  25. * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide,
  26. * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux,
  27. * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file
  28. * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual,
  29. * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the
  30. * ANSI SCSI-2 specification (draft 10c), ...
  31. *
  32. * --------------------------------------------------------------------------
  33. *
  34. * Modifications by Daniel M. Eischen (deischen@iworks.InterWorks.org):
  35. *
  36. * Substantially modified to include support for wide and twin bus
  37. * adapters, DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes,
  38. * SCB paging, and other rework of the code.
  39. *
  40. * --------------------------------------------------------------------------
  41. * Copyright (c) 1994-2000 Justin T. Gibbs.
  42. * Copyright (c) 2000-2001 Adaptec Inc.
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms, with or without
  46. * modification, are permitted provided that the following conditions
  47. * are met:
  48. * 1. Redistributions of source code must retain the above copyright
  49. * notice, this list of conditions, and the following disclaimer,
  50. * without modification.
  51. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  52. * substantially similar to the "NO WARRANTY" disclaimer below
  53. * ("Disclaimer") and any redistribution must be conditioned upon
  54. * including a substantially similar Disclaimer requirement for further
  55. * binary redistribution.
  56. * 3. Neither the names of the above-listed copyright holders nor the names
  57. * of any contributors may be used to endorse or promote products derived
  58. * from this software without specific prior written permission.
  59. *
  60. * Alternatively, this software may be distributed under the terms of the
  61. * GNU General Public License ("GPL") version 2 as published by the Free
  62. * Software Foundation.
  63. *
  64. * NO WARRANTY
  65. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  66. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  67. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  68. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  69. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  70. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  71. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  72. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  73. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  74. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  75. * POSSIBILITY OF SUCH DAMAGES.
  76. *
  77. *---------------------------------------------------------------------------
  78. *
  79. * Thanks also go to (in alphabetical order) the following:
  80. *
  81. * Rory Bolt - Sequencer bug fixes
  82. * Jay Estabrook - Initial DEC Alpha support
  83. * Doug Ledford - Much needed abort/reset bug fixes
  84. * Kai Makisara - DMAing of SCBs
  85. *
  86. * A Boot time option was also added for not resetting the scsi bus.
  87. *
  88. * Form: aic7xxx=extended
  89. * aic7xxx=no_reset
  90. * aic7xxx=verbose
  91. *
  92. * Daniel M. Eischen, deischen@iworks.InterWorks.org, 1/23/97
  93. *
  94. * Id: aic7xxx.c,v 4.1 1997/06/12 08:23:42 deang Exp
  95. */
  96. /*
  97. * Further driver modifications made by Doug Ledford <dledford@redhat.com>
  98. *
  99. * Copyright (c) 1997-1999 Doug Ledford
  100. *
  101. * These changes are released under the same licensing terms as the FreeBSD
  102. * driver written by Justin Gibbs. Please see his Copyright notice above
  103. * for the exact terms and conditions covering my changes as well as the
  104. * warranty statement.
  105. *
  106. * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include
  107. * but are not limited to:
  108. *
  109. * 1: Import of the latest FreeBSD sequencer code for this driver
  110. * 2: Modification of kernel code to accommodate different sequencer semantics
  111. * 3: Extensive changes throughout kernel portion of driver to improve
  112. * abort/reset processing and error hanndling
  113. * 4: Other work contributed by various people on the Internet
  114. * 5: Changes to printk information and verbosity selection code
  115. * 6: General reliability related changes, especially in IRQ management
  116. * 7: Modifications to the default probe/attach order for supported cards
  117. * 8: SMP friendliness has been improved
  118. *
  119. */
  120. #include "aic7xxx_osm.h"
  121. #include "aic7xxx_inline.h"
  122. #include <scsi/scsicam.h>
  123. static struct scsi_transport_template *ahc_linux_transport_template = NULL;
  124. #include <linux/init.h> /* __setup */
  125. #include <linux/mm.h> /* For fetching system memory size */
  126. #include <linux/blkdev.h> /* For block_size() */
  127. #include <linux/delay.h> /* For ssleep/msleep */
  128. /*
  129. * Set this to the delay in seconds after SCSI bus reset.
  130. * Note, we honor this only for the initial bus reset.
  131. * The scsi error recovery code performs its own bus settle
  132. * delay handling for error recovery actions.
  133. */
  134. #ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
  135. #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
  136. #else
  137. #define AIC7XXX_RESET_DELAY 5000
  138. #endif
  139. /*
  140. * Control collection of SCSI transfer statistics for the /proc filesystem.
  141. *
  142. * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
  143. * NOTE: This does affect performance since it has to maintain statistics.
  144. */
  145. #ifdef CONFIG_AIC7XXX_PROC_STATS
  146. #define AIC7XXX_PROC_STATS
  147. #endif
  148. /*
  149. * To change the default number of tagged transactions allowed per-device,
  150. * add a line to the lilo.conf file like:
  151. * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
  152. * which will result in the first four devices on the first two
  153. * controllers being set to a tagged queue depth of 32.
  154. *
  155. * The tag_commands is an array of 16 to allow for wide and twin adapters.
  156. * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
  157. * for channel 1.
  158. */
  159. typedef struct {
  160. uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */
  161. } adapter_tag_info_t;
  162. /*
  163. * Modify this as you see fit for your system.
  164. *
  165. * 0 tagged queuing disabled
  166. * 1 <= n <= 253 n == max tags ever dispatched.
  167. *
  168. * The driver will throttle the number of commands dispatched to a
  169. * device if it returns queue full. For devices with a fixed maximum
  170. * queue depth, the driver will eventually determine this depth and
  171. * lock it in (a console message is printed to indicate that a lock
  172. * has occurred). On some devices, queue full is returned for a temporary
  173. * resource shortage. These devices will return queue full at varying
  174. * depths. The driver will throttle back when the queue fulls occur and
  175. * attempt to slowly increase the depth over time as the device recovers
  176. * from the resource shortage.
  177. *
  178. * In this example, the first line will disable tagged queueing for all
  179. * the devices on the first probed aic7xxx adapter.
  180. *
  181. * The second line enables tagged queueing with 4 commands/LUN for IDs
  182. * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
  183. * driver to attempt to use up to 64 tags for ID 1.
  184. *
  185. * The third line is the same as the first line.
  186. *
  187. * The fourth line disables tagged queueing for devices 0 and 3. It
  188. * enables tagged queueing for the other IDs, with 16 commands/LUN
  189. * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
  190. * IDs 2, 5-7, and 9-15.
  191. */
  192. /*
  193. * NOTE: The below structure is for reference only, the actual structure
  194. * to modify in order to change things is just below this comment block.
  195. adapter_tag_info_t aic7xxx_tag_info[] =
  196. {
  197. {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  198. {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
  199. {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  200. {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
  201. };
  202. */
  203. #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
  204. #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
  205. #else
  206. #define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
  207. #endif
  208. #define AIC7XXX_CONFIGED_TAG_COMMANDS { \
  209. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
  210. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
  211. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
  212. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
  213. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
  214. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
  215. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
  216. AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE \
  217. }
  218. /*
  219. * By default, use the number of commands specified by
  220. * the users kernel configuration.
  221. */
  222. static adapter_tag_info_t aic7xxx_tag_info[] =
  223. {
  224. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  225. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  226. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  227. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  228. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  229. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  230. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  231. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  232. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  233. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  234. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  235. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  236. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  237. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  238. {AIC7XXX_CONFIGED_TAG_COMMANDS},
  239. {AIC7XXX_CONFIGED_TAG_COMMANDS}
  240. };
  241. /*
  242. * There should be a specific return value for this in scsi.h, but
  243. * it seems that most drivers ignore it.
  244. */
  245. #define DID_UNDERFLOW DID_ERROR
  246. void
  247. ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
  248. {
  249. printk("(scsi%d:%c:%d:%d): ",
  250. ahc->platform_data->host->host_no,
  251. scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
  252. scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
  253. scb != NULL ? SCB_GET_LUN(scb) : -1);
  254. }
  255. /*
  256. * XXX - these options apply unilaterally to _all_ 274x/284x/294x
  257. * cards in the system. This should be fixed. Exceptions to this
  258. * rule are noted in the comments.
  259. */
  260. /*
  261. * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
  262. * has no effect on any later resets that might occur due to things like
  263. * SCSI bus timeouts.
  264. */
  265. static uint32_t aic7xxx_no_reset;
  266. /*
  267. * Should we force EXTENDED translation on a controller.
  268. * 0 == Use whatever is in the SEEPROM or default to off
  269. * 1 == Use whatever is in the SEEPROM or default to on
  270. */
  271. static uint32_t aic7xxx_extended;
  272. /*
  273. * PCI bus parity checking of the Adaptec controllers. This is somewhat
  274. * dubious at best. To my knowledge, this option has never actually
  275. * solved a PCI parity problem, but on certain machines with broken PCI
  276. * chipset configurations where stray PCI transactions with bad parity are
  277. * the norm rather than the exception, the error messages can be overwelming.
  278. * It's included in the driver for completeness.
  279. * 0 = Shut off PCI parity check
  280. * non-0 = reverse polarity pci parity checking
  281. */
  282. static uint32_t aic7xxx_pci_parity = ~0;
  283. /*
  284. * There are lots of broken chipsets in the world. Some of them will
  285. * violate the PCI spec when we issue byte sized memory writes to our
  286. * controller. I/O mapped register access, if allowed by the given
  287. * platform, will work in almost all cases.
  288. */
  289. uint32_t aic7xxx_allow_memio = ~0;
  290. /*
  291. * So that we can set how long each device is given as a selection timeout.
  292. * The table of values goes like this:
  293. * 0 - 256ms
  294. * 1 - 128ms
  295. * 2 - 64ms
  296. * 3 - 32ms
  297. * We default to 256ms because some older devices need a longer time
  298. * to respond to initial selection.
  299. */
  300. static uint32_t aic7xxx_seltime;
  301. /*
  302. * Certain devices do not perform any aging on commands. Should the
  303. * device be saturated by commands in one portion of the disk, it is
  304. * possible for transactions on far away sectors to never be serviced.
  305. * To handle these devices, we can periodically send an ordered tag to
  306. * force all outstanding transactions to be serviced prior to a new
  307. * transaction.
  308. */
  309. uint32_t aic7xxx_periodic_otag;
  310. /*
  311. * Module information and settable options.
  312. */
  313. static char *aic7xxx = NULL;
  314. MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
  315. MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
  316. MODULE_LICENSE("Dual BSD/GPL");
  317. MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
  318. module_param(aic7xxx, charp, 0444);
  319. MODULE_PARM_DESC(aic7xxx,
  320. "period delimited, options string.\n"
  321. " verbose Enable verbose/diagnostic logging\n"
  322. " allow_memio Allow device registers to be memory mapped\n"
  323. " debug Bitmask of debug values to enable\n"
  324. " no_probe Toggle EISA/VLB controller probing\n"
  325. " probe_eisa_vl Toggle EISA/VLB controller probing\n"
  326. " no_reset Supress initial bus resets\n"
  327. " extended Enable extended geometry on all controllers\n"
  328. " periodic_otag Send an ordered tagged transaction\n"
  329. " periodically to prevent tag starvation.\n"
  330. " This may be required by some older disk\n"
  331. " drives or RAID arrays.\n"
  332. " reverse_scan Sort PCI devices highest Bus/Slot to lowest\n"
  333. " tag_info:<tag_str> Set per-target tag depth\n"
  334. " global_tag_depth:<int> Global tag depth for every target\n"
  335. " on every bus\n"
  336. " seltime:<int> Selection Timeout\n"
  337. " (0/256ms,1/128ms,2/64ms,3/32ms)\n"
  338. "\n"
  339. " Sample /etc/modprobe.conf line:\n"
  340. " Toggle EISA/VLB probing\n"
  341. " Set tag depth on Controller 1/Target 1 to 10 tags\n"
  342. " Shorten the selection timeout to 128ms\n"
  343. "\n"
  344. " options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
  345. );
  346. static void ahc_linux_handle_scsi_status(struct ahc_softc *,
  347. struct scsi_device *,
  348. struct scb *);
  349. static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
  350. struct scsi_cmnd *cmd);
  351. static void ahc_linux_sem_timeout(u_long arg);
  352. static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
  353. static void ahc_linux_release_simq(u_long arg);
  354. static int ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
  355. static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
  356. static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
  357. struct ahc_devinfo *devinfo);
  358. static void ahc_linux_device_queue_depth(struct scsi_device *);
  359. static int ahc_linux_run_command(struct ahc_softc*,
  360. struct ahc_linux_device *,
  361. struct scsi_cmnd *);
  362. static void ahc_linux_setup_tag_info_global(char *p);
  363. static int aic7xxx_setup(char *s);
  364. static int ahc_linux_unit;
  365. /********************************* Inlines ************************************/
  366. static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
  367. static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
  368. struct ahc_dma_seg *sg,
  369. dma_addr_t addr, bus_size_t len);
  370. static __inline void
  371. ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
  372. {
  373. struct scsi_cmnd *cmd;
  374. cmd = scb->io_ctx;
  375. ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
  376. if (cmd->use_sg != 0) {
  377. struct scatterlist *sg;
  378. sg = (struct scatterlist *)cmd->request_buffer;
  379. pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
  380. cmd->sc_data_direction);
  381. } else if (cmd->request_bufflen != 0) {
  382. pci_unmap_single(ahc->dev_softc,
  383. scb->platform_data->buf_busaddr,
  384. cmd->request_bufflen,
  385. cmd->sc_data_direction);
  386. }
  387. }
  388. static __inline int
  389. ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
  390. struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
  391. {
  392. int consumed;
  393. if ((scb->sg_count + 1) > AHC_NSEG)
  394. panic("Too few segs for dma mapping. "
  395. "Increase AHC_NSEG\n");
  396. consumed = 1;
  397. sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
  398. scb->platform_data->xfer_len += len;
  399. if (sizeof(dma_addr_t) > 4
  400. && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
  401. len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
  402. sg->len = ahc_htole32(len);
  403. return (consumed);
  404. }
  405. /*
  406. * Return a string describing the driver.
  407. */
  408. static const char *
  409. ahc_linux_info(struct Scsi_Host *host)
  410. {
  411. static char buffer[512];
  412. char ahc_info[256];
  413. char *bp;
  414. struct ahc_softc *ahc;
  415. bp = &buffer[0];
  416. ahc = *(struct ahc_softc **)host->hostdata;
  417. memset(bp, 0, sizeof(buffer));
  418. strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
  419. strcat(bp, AIC7XXX_DRIVER_VERSION);
  420. strcat(bp, "\n");
  421. strcat(bp, " <");
  422. strcat(bp, ahc->description);
  423. strcat(bp, ">\n");
  424. strcat(bp, " ");
  425. ahc_controller_info(ahc, ahc_info);
  426. strcat(bp, ahc_info);
  427. strcat(bp, "\n");
  428. return (bp);
  429. }
  430. /*
  431. * Queue an SCB to the controller.
  432. */
  433. static int
  434. ahc_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
  435. {
  436. struct ahc_softc *ahc;
  437. struct ahc_linux_device *dev = scsi_transport_device_data(cmd->device);
  438. ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
  439. /*
  440. * Save the callback on completion function.
  441. */
  442. cmd->scsi_done = scsi_done;
  443. /*
  444. * Close the race of a command that was in the process of
  445. * being queued to us just as our simq was frozen. Let
  446. * DV commands through so long as we are only frozen to
  447. * perform DV.
  448. */
  449. if (ahc->platform_data->qfrozen != 0)
  450. return SCSI_MLQUEUE_HOST_BUSY;
  451. cmd->result = CAM_REQ_INPROG << 16;
  452. return ahc_linux_run_command(ahc, dev, cmd);
  453. }
  454. static inline struct scsi_target **
  455. ahc_linux_target_in_softc(struct scsi_target *starget)
  456. {
  457. struct ahc_softc *ahc =
  458. *((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
  459. unsigned int target_offset;
  460. target_offset = starget->id;
  461. if (starget->channel != 0)
  462. target_offset += 8;
  463. return &ahc->platform_data->starget[target_offset];
  464. }
  465. static int
  466. ahc_linux_target_alloc(struct scsi_target *starget)
  467. {
  468. struct ahc_softc *ahc =
  469. *((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
  470. struct seeprom_config *sc = ahc->seep_config;
  471. unsigned long flags;
  472. struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
  473. struct ahc_linux_target *targ = scsi_transport_target_data(starget);
  474. unsigned short scsirate;
  475. struct ahc_devinfo devinfo;
  476. struct ahc_initiator_tinfo *tinfo;
  477. struct ahc_tmode_tstate *tstate;
  478. char channel = starget->channel + 'A';
  479. unsigned int our_id = ahc->our_id;
  480. unsigned int target_offset;
  481. target_offset = starget->id;
  482. if (starget->channel != 0)
  483. target_offset += 8;
  484. if (starget->channel)
  485. our_id = ahc->our_id_b;
  486. ahc_lock(ahc, &flags);
  487. BUG_ON(*ahc_targp != NULL);
  488. *ahc_targp = starget;
  489. memset(targ, 0, sizeof(*targ));
  490. if (sc) {
  491. int maxsync = AHC_SYNCRATE_DT;
  492. int ultra = 0;
  493. int flags = sc->device_flags[target_offset];
  494. if (ahc->flags & AHC_NEWEEPROM_FMT) {
  495. if (flags & CFSYNCHISULTRA)
  496. ultra = 1;
  497. } else if (flags & CFULTRAEN)
  498. ultra = 1;
  499. /* AIC nutcase; 10MHz appears as ultra = 1, CFXFER = 0x04
  500. * change it to ultra=0, CFXFER = 0 */
  501. if(ultra && (flags & CFXFER) == 0x04) {
  502. ultra = 0;
  503. flags &= ~CFXFER;
  504. }
  505. if ((ahc->features & AHC_ULTRA2) != 0) {
  506. scsirate = (flags & CFXFER) | (ultra ? 0x8 : 0);
  507. } else {
  508. scsirate = (flags & CFXFER) << 4;
  509. maxsync = ultra ? AHC_SYNCRATE_ULTRA :
  510. AHC_SYNCRATE_FAST;
  511. }
  512. spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
  513. if (!(flags & CFSYNCH))
  514. spi_max_offset(starget) = 0;
  515. spi_min_period(starget) =
  516. ahc_find_period(ahc, scsirate, maxsync);
  517. tinfo = ahc_fetch_transinfo(ahc, channel, ahc->our_id,
  518. starget->id, &tstate);
  519. }
  520. ahc_compile_devinfo(&devinfo, our_id, starget->id,
  521. CAM_LUN_WILDCARD, channel,
  522. ROLE_INITIATOR);
  523. ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
  524. AHC_TRANS_GOAL, /*paused*/FALSE);
  525. ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  526. AHC_TRANS_GOAL, /*paused*/FALSE);
  527. ahc_unlock(ahc, &flags);
  528. return 0;
  529. }
  530. static void
  531. ahc_linux_target_destroy(struct scsi_target *starget)
  532. {
  533. struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
  534. *ahc_targp = NULL;
  535. }
  536. static int
  537. ahc_linux_slave_alloc(struct scsi_device *sdev)
  538. {
  539. struct ahc_softc *ahc =
  540. *((struct ahc_softc **)sdev->host->hostdata);
  541. struct scsi_target *starget = sdev->sdev_target;
  542. struct ahc_linux_target *targ = scsi_transport_target_data(starget);
  543. struct ahc_linux_device *dev;
  544. if (bootverbose)
  545. printf("%s: Slave Alloc %d\n", ahc_name(ahc), sdev->id);
  546. BUG_ON(targ->sdev[sdev->lun] != NULL);
  547. dev = scsi_transport_device_data(sdev);
  548. memset(dev, 0, sizeof(*dev));
  549. /*
  550. * We start out life using untagged
  551. * transactions of which we allow one.
  552. */
  553. dev->openings = 1;
  554. /*
  555. * Set maxtags to 0. This will be changed if we
  556. * later determine that we are dealing with
  557. * a tagged queuing capable device.
  558. */
  559. dev->maxtags = 0;
  560. targ->sdev[sdev->lun] = sdev;
  561. spi_period(starget) = 0;
  562. return 0;
  563. }
  564. static int
  565. ahc_linux_slave_configure(struct scsi_device *sdev)
  566. {
  567. struct ahc_softc *ahc;
  568. ahc = *((struct ahc_softc **)sdev->host->hostdata);
  569. if (bootverbose)
  570. printf("%s: Slave Configure %d\n", ahc_name(ahc), sdev->id);
  571. ahc_linux_device_queue_depth(sdev);
  572. /* Initial Domain Validation */
  573. if (!spi_initial_dv(sdev->sdev_target))
  574. spi_dv_device(sdev);
  575. return 0;
  576. }
  577. static void
  578. ahc_linux_slave_destroy(struct scsi_device *sdev)
  579. {
  580. struct ahc_softc *ahc;
  581. struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
  582. struct ahc_linux_target *targ = scsi_transport_target_data(sdev->sdev_target);
  583. ahc = *((struct ahc_softc **)sdev->host->hostdata);
  584. if (bootverbose)
  585. printf("%s: Slave Destroy %d\n", ahc_name(ahc), sdev->id);
  586. BUG_ON(dev->active);
  587. targ->sdev[sdev->lun] = NULL;
  588. }
  589. #if defined(__i386__)
  590. /*
  591. * Return the disk geometry for the given SCSI device.
  592. */
  593. static int
  594. ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  595. sector_t capacity, int geom[])
  596. {
  597. uint8_t *bh;
  598. int heads;
  599. int sectors;
  600. int cylinders;
  601. int ret;
  602. int extended;
  603. struct ahc_softc *ahc;
  604. u_int channel;
  605. ahc = *((struct ahc_softc **)sdev->host->hostdata);
  606. channel = sdev->channel;
  607. bh = scsi_bios_ptable(bdev);
  608. if (bh) {
  609. ret = scsi_partsize(bh, capacity,
  610. &geom[2], &geom[0], &geom[1]);
  611. kfree(bh);
  612. if (ret != -1)
  613. return (ret);
  614. }
  615. heads = 64;
  616. sectors = 32;
  617. cylinders = aic_sector_div(capacity, heads, sectors);
  618. if (aic7xxx_extended != 0)
  619. extended = 1;
  620. else if (channel == 0)
  621. extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
  622. else
  623. extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
  624. if (extended && cylinders >= 1024) {
  625. heads = 255;
  626. sectors = 63;
  627. cylinders = aic_sector_div(capacity, heads, sectors);
  628. }
  629. geom[0] = heads;
  630. geom[1] = sectors;
  631. geom[2] = cylinders;
  632. return (0);
  633. }
  634. #endif
  635. /*
  636. * Abort the current SCSI command(s).
  637. */
  638. static int
  639. ahc_linux_abort(struct scsi_cmnd *cmd)
  640. {
  641. int error;
  642. error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
  643. if (error != 0)
  644. printf("aic7xxx_abort returns 0x%x\n", error);
  645. return (error);
  646. }
  647. /*
  648. * Attempt to send a target reset message to the device that timed out.
  649. */
  650. static int
  651. ahc_linux_dev_reset(struct scsi_cmnd *cmd)
  652. {
  653. int error;
  654. error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
  655. if (error != 0)
  656. printf("aic7xxx_dev_reset returns 0x%x\n", error);
  657. return (error);
  658. }
  659. /*
  660. * Reset the SCSI bus.
  661. */
  662. static int
  663. ahc_linux_bus_reset(struct scsi_cmnd *cmd)
  664. {
  665. struct ahc_softc *ahc;
  666. int found;
  667. unsigned long flags;
  668. ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
  669. ahc_lock(ahc, &flags);
  670. found = ahc_reset_channel(ahc, cmd->device->channel + 'A',
  671. /*initiate reset*/TRUE);
  672. ahc_unlock(ahc, &flags);
  673. if (bootverbose)
  674. printf("%s: SCSI bus reset delivered. "
  675. "%d SCBs aborted.\n", ahc_name(ahc), found);
  676. return SUCCESS;
  677. }
  678. struct scsi_host_template aic7xxx_driver_template = {
  679. .module = THIS_MODULE,
  680. .name = "aic7xxx",
  681. .proc_name = "aic7xxx",
  682. .proc_info = ahc_linux_proc_info,
  683. .info = ahc_linux_info,
  684. .queuecommand = ahc_linux_queue,
  685. .eh_abort_handler = ahc_linux_abort,
  686. .eh_device_reset_handler = ahc_linux_dev_reset,
  687. .eh_bus_reset_handler = ahc_linux_bus_reset,
  688. #if defined(__i386__)
  689. .bios_param = ahc_linux_biosparam,
  690. #endif
  691. .can_queue = AHC_MAX_QUEUE,
  692. .this_id = -1,
  693. .cmd_per_lun = 2,
  694. .use_clustering = ENABLE_CLUSTERING,
  695. .slave_alloc = ahc_linux_slave_alloc,
  696. .slave_configure = ahc_linux_slave_configure,
  697. .slave_destroy = ahc_linux_slave_destroy,
  698. .target_alloc = ahc_linux_target_alloc,
  699. .target_destroy = ahc_linux_target_destroy,
  700. };
  701. /**************************** Tasklet Handler *********************************/
  702. /******************************** Macros **************************************/
  703. #define BUILD_SCSIID(ahc, cmd) \
  704. ((((cmd)->device->id << TID_SHIFT) & TID) \
  705. | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
  706. | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
  707. /******************************** Bus DMA *************************************/
  708. int
  709. ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
  710. bus_size_t alignment, bus_size_t boundary,
  711. dma_addr_t lowaddr, dma_addr_t highaddr,
  712. bus_dma_filter_t *filter, void *filterarg,
  713. bus_size_t maxsize, int nsegments,
  714. bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
  715. {
  716. bus_dma_tag_t dmat;
  717. dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
  718. if (dmat == NULL)
  719. return (ENOMEM);
  720. /*
  721. * Linux is very simplistic about DMA memory. For now don't
  722. * maintain all specification information. Once Linux supplies
  723. * better facilities for doing these operations, or the
  724. * needs of this particular driver change, we might need to do
  725. * more here.
  726. */
  727. dmat->alignment = alignment;
  728. dmat->boundary = boundary;
  729. dmat->maxsize = maxsize;
  730. *ret_tag = dmat;
  731. return (0);
  732. }
  733. void
  734. ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
  735. {
  736. free(dmat, M_DEVBUF);
  737. }
  738. int
  739. ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
  740. int flags, bus_dmamap_t *mapp)
  741. {
  742. *vaddr = pci_alloc_consistent(ahc->dev_softc,
  743. dmat->maxsize, mapp);
  744. if (*vaddr == NULL)
  745. return ENOMEM;
  746. return 0;
  747. }
  748. void
  749. ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
  750. void* vaddr, bus_dmamap_t map)
  751. {
  752. pci_free_consistent(ahc->dev_softc, dmat->maxsize,
  753. vaddr, map);
  754. }
  755. int
  756. ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
  757. void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
  758. void *cb_arg, int flags)
  759. {
  760. /*
  761. * Assume for now that this will only be used during
  762. * initialization and not for per-transaction buffer mapping.
  763. */
  764. bus_dma_segment_t stack_sg;
  765. stack_sg.ds_addr = map;
  766. stack_sg.ds_len = dmat->maxsize;
  767. cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
  768. return (0);
  769. }
  770. void
  771. ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
  772. {
  773. }
  774. int
  775. ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
  776. {
  777. /* Nothing to do */
  778. return (0);
  779. }
  780. static void
  781. ahc_linux_setup_tag_info_global(char *p)
  782. {
  783. int tags, i, j;
  784. tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
  785. printf("Setting Global Tags= %d\n", tags);
  786. for (i = 0; i < NUM_ELEMENTS(aic7xxx_tag_info); i++) {
  787. for (j = 0; j < AHC_NUM_TARGETS; j++) {
  788. aic7xxx_tag_info[i].tag_commands[j] = tags;
  789. }
  790. }
  791. }
  792. static void
  793. ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
  794. {
  795. if ((instance >= 0) && (targ >= 0)
  796. && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
  797. && (targ < AHC_NUM_TARGETS)) {
  798. aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
  799. if (bootverbose)
  800. printf("tag_info[%d:%d] = %d\n", instance, targ, value);
  801. }
  802. }
  803. static char *
  804. ahc_parse_brace_option(char *opt_name, char *opt_arg, char *end, int depth,
  805. void (*callback)(u_long, int, int, int32_t),
  806. u_long callback_arg)
  807. {
  808. char *tok_end;
  809. char *tok_end2;
  810. int i;
  811. int instance;
  812. int targ;
  813. int done;
  814. char tok_list[] = {'.', ',', '{', '}', '\0'};
  815. /* All options use a ':' name/arg separator */
  816. if (*opt_arg != ':')
  817. return (opt_arg);
  818. opt_arg++;
  819. instance = -1;
  820. targ = -1;
  821. done = FALSE;
  822. /*
  823. * Restore separator that may be in
  824. * the middle of our option argument.
  825. */
  826. tok_end = strchr(opt_arg, '\0');
  827. if (tok_end < end)
  828. *tok_end = ',';
  829. while (!done) {
  830. switch (*opt_arg) {
  831. case '{':
  832. if (instance == -1) {
  833. instance = 0;
  834. } else {
  835. if (depth > 1) {
  836. if (targ == -1)
  837. targ = 0;
  838. } else {
  839. printf("Malformed Option %s\n",
  840. opt_name);
  841. done = TRUE;
  842. }
  843. }
  844. opt_arg++;
  845. break;
  846. case '}':
  847. if (targ != -1)
  848. targ = -1;
  849. else if (instance != -1)
  850. instance = -1;
  851. opt_arg++;
  852. break;
  853. case ',':
  854. case '.':
  855. if (instance == -1)
  856. done = TRUE;
  857. else if (targ >= 0)
  858. targ++;
  859. else if (instance >= 0)
  860. instance++;
  861. opt_arg++;
  862. break;
  863. case '\0':
  864. done = TRUE;
  865. break;
  866. default:
  867. tok_end = end;
  868. for (i = 0; tok_list[i]; i++) {
  869. tok_end2 = strchr(opt_arg, tok_list[i]);
  870. if ((tok_end2) && (tok_end2 < tok_end))
  871. tok_end = tok_end2;
  872. }
  873. callback(callback_arg, instance, targ,
  874. simple_strtol(opt_arg, NULL, 0));
  875. opt_arg = tok_end;
  876. break;
  877. }
  878. }
  879. return (opt_arg);
  880. }
  881. /*
  882. * Handle Linux boot parameters. This routine allows for assigning a value
  883. * to a parameter with a ':' between the parameter and the value.
  884. * ie. aic7xxx=stpwlev:1,extended
  885. */
  886. static int
  887. aic7xxx_setup(char *s)
  888. {
  889. int i, n;
  890. char *p;
  891. char *end;
  892. static struct {
  893. const char *name;
  894. uint32_t *flag;
  895. } options[] = {
  896. { "extended", &aic7xxx_extended },
  897. { "no_reset", &aic7xxx_no_reset },
  898. { "verbose", &aic7xxx_verbose },
  899. { "allow_memio", &aic7xxx_allow_memio},
  900. #ifdef AHC_DEBUG
  901. { "debug", &ahc_debug },
  902. #endif
  903. { "periodic_otag", &aic7xxx_periodic_otag },
  904. { "pci_parity", &aic7xxx_pci_parity },
  905. { "seltime", &aic7xxx_seltime },
  906. { "tag_info", NULL },
  907. { "global_tag_depth", NULL },
  908. { "dv", NULL }
  909. };
  910. end = strchr(s, '\0');
  911. /*
  912. * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
  913. * will never be 0 in this case.
  914. */
  915. n = 0;
  916. while ((p = strsep(&s, ",.")) != NULL) {
  917. if (*p == '\0')
  918. continue;
  919. for (i = 0; i < NUM_ELEMENTS(options); i++) {
  920. n = strlen(options[i].name);
  921. if (strncmp(options[i].name, p, n) == 0)
  922. break;
  923. }
  924. if (i == NUM_ELEMENTS(options))
  925. continue;
  926. if (strncmp(p, "global_tag_depth", n) == 0) {
  927. ahc_linux_setup_tag_info_global(p + n);
  928. } else if (strncmp(p, "tag_info", n) == 0) {
  929. s = ahc_parse_brace_option("tag_info", p + n, end,
  930. 2, ahc_linux_setup_tag_info, 0);
  931. } else if (p[n] == ':') {
  932. *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
  933. } else if (strncmp(p, "verbose", n) == 0) {
  934. *(options[i].flag) = 1;
  935. } else {
  936. *(options[i].flag) ^= 0xFFFFFFFF;
  937. }
  938. }
  939. return 1;
  940. }
  941. __setup("aic7xxx=", aic7xxx_setup);
  942. uint32_t aic7xxx_verbose;
  943. int
  944. ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *template)
  945. {
  946. char buf[80];
  947. struct Scsi_Host *host;
  948. char *new_name;
  949. u_long s;
  950. template->name = ahc->description;
  951. host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
  952. if (host == NULL)
  953. return (ENOMEM);
  954. *((struct ahc_softc **)host->hostdata) = ahc;
  955. ahc_lock(ahc, &s);
  956. scsi_assign_lock(host, &ahc->platform_data->spin_lock);
  957. ahc->platform_data->host = host;
  958. host->can_queue = AHC_MAX_QUEUE;
  959. host->cmd_per_lun = 2;
  960. /* XXX No way to communicate the ID for multiple channels */
  961. host->this_id = ahc->our_id;
  962. host->irq = ahc->platform_data->irq;
  963. host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
  964. host->max_lun = AHC_NUM_LUNS;
  965. host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
  966. host->sg_tablesize = AHC_NSEG;
  967. ahc_set_unit(ahc, ahc_linux_unit++);
  968. sprintf(buf, "scsi%d", host->host_no);
  969. new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
  970. if (new_name != NULL) {
  971. strcpy(new_name, buf);
  972. ahc_set_name(ahc, new_name);
  973. }
  974. host->unique_id = ahc->unit;
  975. ahc_linux_initialize_scsi_bus(ahc);
  976. ahc_intr_enable(ahc, TRUE);
  977. ahc_unlock(ahc, &s);
  978. host->transportt = ahc_linux_transport_template;
  979. scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */
  980. scsi_scan_host(host);
  981. return (0);
  982. }
  983. /*
  984. * Place the SCSI bus into a known state by either resetting it,
  985. * or forcing transfer negotiations on the next command to any
  986. * target.
  987. */
  988. void
  989. ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
  990. {
  991. int i;
  992. int numtarg;
  993. i = 0;
  994. numtarg = 0;
  995. if (aic7xxx_no_reset != 0)
  996. ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
  997. if ((ahc->flags & AHC_RESET_BUS_A) != 0)
  998. ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
  999. else
  1000. numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
  1001. if ((ahc->features & AHC_TWIN) != 0) {
  1002. if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
  1003. ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
  1004. } else {
  1005. if (numtarg == 0)
  1006. i = 8;
  1007. numtarg += 8;
  1008. }
  1009. }
  1010. /*
  1011. * Force negotiation to async for all targets that
  1012. * will not see an initial bus reset.
  1013. */
  1014. for (; i < numtarg; i++) {
  1015. struct ahc_devinfo devinfo;
  1016. struct ahc_initiator_tinfo *tinfo;
  1017. struct ahc_tmode_tstate *tstate;
  1018. u_int our_id;
  1019. u_int target_id;
  1020. char channel;
  1021. channel = 'A';
  1022. our_id = ahc->our_id;
  1023. target_id = i;
  1024. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  1025. channel = 'B';
  1026. our_id = ahc->our_id_b;
  1027. target_id = i % 8;
  1028. }
  1029. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  1030. target_id, &tstate);
  1031. ahc_compile_devinfo(&devinfo, our_id, target_id,
  1032. CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
  1033. ahc_update_neg_request(ahc, &devinfo, tstate,
  1034. tinfo, AHC_NEG_ALWAYS);
  1035. }
  1036. /* Give the bus some time to recover */
  1037. if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
  1038. ahc_linux_freeze_simq(ahc);
  1039. init_timer(&ahc->platform_data->reset_timer);
  1040. ahc->platform_data->reset_timer.data = (u_long)ahc;
  1041. ahc->platform_data->reset_timer.expires =
  1042. jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
  1043. ahc->platform_data->reset_timer.function =
  1044. ahc_linux_release_simq;
  1045. add_timer(&ahc->platform_data->reset_timer);
  1046. }
  1047. }
  1048. int
  1049. ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
  1050. {
  1051. ahc->platform_data =
  1052. malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
  1053. if (ahc->platform_data == NULL)
  1054. return (ENOMEM);
  1055. memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
  1056. ahc->platform_data->irq = AHC_LINUX_NOIRQ;
  1057. ahc_lockinit(ahc);
  1058. init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
  1059. ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
  1060. ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
  1061. if (aic7xxx_pci_parity == 0)
  1062. ahc->flags |= AHC_DISABLE_PCI_PERR;
  1063. return (0);
  1064. }
  1065. void
  1066. ahc_platform_free(struct ahc_softc *ahc)
  1067. {
  1068. struct scsi_target *starget;
  1069. int i, j;
  1070. if (ahc->platform_data != NULL) {
  1071. if (ahc->platform_data->host != NULL) {
  1072. scsi_remove_host(ahc->platform_data->host);
  1073. scsi_host_put(ahc->platform_data->host);
  1074. }
  1075. /* destroy all of the device and target objects */
  1076. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  1077. starget = ahc->platform_data->starget[i];
  1078. if (starget != NULL) {
  1079. for (j = 0; j < AHC_NUM_LUNS; j++) {
  1080. struct ahc_linux_target *targ =
  1081. scsi_transport_target_data(starget);
  1082. if (targ->sdev[j] == NULL)
  1083. continue;
  1084. targ->sdev[j] = NULL;
  1085. }
  1086. ahc->platform_data->starget[i] = NULL;
  1087. }
  1088. }
  1089. if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
  1090. free_irq(ahc->platform_data->irq, ahc);
  1091. if (ahc->tag == BUS_SPACE_PIO
  1092. && ahc->bsh.ioport != 0)
  1093. release_region(ahc->bsh.ioport, 256);
  1094. if (ahc->tag == BUS_SPACE_MEMIO
  1095. && ahc->bsh.maddr != NULL) {
  1096. iounmap(ahc->bsh.maddr);
  1097. release_mem_region(ahc->platform_data->mem_busaddr,
  1098. 0x1000);
  1099. }
  1100. free(ahc->platform_data, M_DEVBUF);
  1101. }
  1102. }
  1103. void
  1104. ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  1105. {
  1106. ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
  1107. SCB_GET_CHANNEL(ahc, scb),
  1108. SCB_GET_LUN(scb), SCB_LIST_NULL,
  1109. ROLE_UNKNOWN, CAM_REQUEUE_REQ);
  1110. }
  1111. void
  1112. ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1113. ahc_queue_alg alg)
  1114. {
  1115. struct scsi_target *starget;
  1116. struct ahc_linux_target *targ;
  1117. struct ahc_linux_device *dev;
  1118. struct scsi_device *sdev;
  1119. u_int target_offset;
  1120. int was_queuing;
  1121. int now_queuing;
  1122. target_offset = devinfo->target;
  1123. if (devinfo->channel != 'A')
  1124. target_offset += 8;
  1125. starget = ahc->platform_data->starget[target_offset];
  1126. targ = scsi_transport_target_data(starget);
  1127. BUG_ON(targ == NULL);
  1128. sdev = targ->sdev[devinfo->lun];
  1129. if (sdev == NULL)
  1130. return;
  1131. dev = scsi_transport_device_data(sdev);
  1132. was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
  1133. switch (alg) {
  1134. default:
  1135. case AHC_QUEUE_NONE:
  1136. now_queuing = 0;
  1137. break;
  1138. case AHC_QUEUE_BASIC:
  1139. now_queuing = AHC_DEV_Q_BASIC;
  1140. break;
  1141. case AHC_QUEUE_TAGGED:
  1142. now_queuing = AHC_DEV_Q_TAGGED;
  1143. break;
  1144. }
  1145. if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
  1146. && (was_queuing != now_queuing)
  1147. && (dev->active != 0)) {
  1148. dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
  1149. dev->qfrozen++;
  1150. }
  1151. dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
  1152. if (now_queuing) {
  1153. u_int usertags;
  1154. usertags = ahc_linux_user_tagdepth(ahc, devinfo);
  1155. if (!was_queuing) {
  1156. /*
  1157. * Start out agressively and allow our
  1158. * dynamic queue depth algorithm to take
  1159. * care of the rest.
  1160. */
  1161. dev->maxtags = usertags;
  1162. dev->openings = dev->maxtags - dev->active;
  1163. }
  1164. if (dev->maxtags == 0) {
  1165. /*
  1166. * Queueing is disabled by the user.
  1167. */
  1168. dev->openings = 1;
  1169. } else if (alg == AHC_QUEUE_TAGGED) {
  1170. dev->flags |= AHC_DEV_Q_TAGGED;
  1171. if (aic7xxx_periodic_otag != 0)
  1172. dev->flags |= AHC_DEV_PERIODIC_OTAG;
  1173. } else
  1174. dev->flags |= AHC_DEV_Q_BASIC;
  1175. } else {
  1176. /* We can only have one opening. */
  1177. dev->maxtags = 0;
  1178. dev->openings = 1 - dev->active;
  1179. }
  1180. switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
  1181. case AHC_DEV_Q_BASIC:
  1182. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  1183. scsi_activate_tcq(sdev, dev->openings + dev->active);
  1184. break;
  1185. case AHC_DEV_Q_TAGGED:
  1186. scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
  1187. scsi_activate_tcq(sdev, dev->openings + dev->active);
  1188. break;
  1189. default:
  1190. /*
  1191. * We allow the OS to queue 2 untagged transactions to
  1192. * us at any time even though we can only execute them
  1193. * serially on the controller/device. This should
  1194. * remove some latency.
  1195. */
  1196. scsi_deactivate_tcq(sdev, 2);
  1197. break;
  1198. }
  1199. }
  1200. int
  1201. ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  1202. int lun, u_int tag, role_t role, uint32_t status)
  1203. {
  1204. return 0;
  1205. }
  1206. static u_int
  1207. ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  1208. {
  1209. static int warned_user;
  1210. u_int tags;
  1211. tags = 0;
  1212. if ((ahc->user_discenable & devinfo->target_mask) != 0) {
  1213. if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
  1214. if (warned_user == 0) {
  1215. printf(KERN_WARNING
  1216. "aic7xxx: WARNING: Insufficient tag_info instances\n"
  1217. "aic7xxx: for installed controllers. Using defaults\n"
  1218. "aic7xxx: Please update the aic7xxx_tag_info array in\n"
  1219. "aic7xxx: the aic7xxx_osm..c source file.\n");
  1220. warned_user++;
  1221. }
  1222. tags = AHC_MAX_QUEUE;
  1223. } else {
  1224. adapter_tag_info_t *tag_info;
  1225. tag_info = &aic7xxx_tag_info[ahc->unit];
  1226. tags = tag_info->tag_commands[devinfo->target_offset];
  1227. if (tags > AHC_MAX_QUEUE)
  1228. tags = AHC_MAX_QUEUE;
  1229. }
  1230. }
  1231. return (tags);
  1232. }
  1233. /*
  1234. * Determines the queue depth for a given device.
  1235. */
  1236. static void
  1237. ahc_linux_device_queue_depth(struct scsi_device *sdev)
  1238. {
  1239. struct ahc_devinfo devinfo;
  1240. u_int tags;
  1241. struct ahc_softc *ahc = *((struct ahc_softc **)sdev->host->hostdata);
  1242. ahc_compile_devinfo(&devinfo,
  1243. sdev->sdev_target->channel == 0
  1244. ? ahc->our_id : ahc->our_id_b,
  1245. sdev->sdev_target->id, sdev->lun,
  1246. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1247. ROLE_INITIATOR);
  1248. tags = ahc_linux_user_tagdepth(ahc, &devinfo);
  1249. if (tags != 0 && sdev->tagged_supported != 0) {
  1250. ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
  1251. ahc_print_devinfo(ahc, &devinfo);
  1252. printf("Tagged Queuing enabled. Depth %d\n", tags);
  1253. } else {
  1254. ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
  1255. }
  1256. }
  1257. static int
  1258. ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
  1259. struct scsi_cmnd *cmd)
  1260. {
  1261. struct scb *scb;
  1262. struct hardware_scb *hscb;
  1263. struct ahc_initiator_tinfo *tinfo;
  1264. struct ahc_tmode_tstate *tstate;
  1265. uint16_t mask;
  1266. struct scb_tailq *untagged_q = NULL;
  1267. /*
  1268. * Schedule us to run later. The only reason we are not
  1269. * running is because the whole controller Q is frozen.
  1270. */
  1271. if (ahc->platform_data->qfrozen != 0)
  1272. return SCSI_MLQUEUE_HOST_BUSY;
  1273. /*
  1274. * We only allow one untagged transaction
  1275. * per target in the initiator role unless
  1276. * we are storing a full busy target *lun*
  1277. * table in SCB space.
  1278. */
  1279. if (!blk_rq_tagged(cmd->request)
  1280. && (ahc->features & AHC_SCB_BTT) == 0) {
  1281. int target_offset;
  1282. target_offset = cmd->device->id + cmd->device->channel * 8;
  1283. untagged_q = &(ahc->untagged_queues[target_offset]);
  1284. if (!TAILQ_EMPTY(untagged_q))
  1285. /* if we're already executing an untagged command
  1286. * we're busy to another */
  1287. return SCSI_MLQUEUE_DEVICE_BUSY;
  1288. }
  1289. /*
  1290. * Get an scb to use.
  1291. */
  1292. scb = ahc_get_scb(ahc);
  1293. if (!scb)
  1294. return SCSI_MLQUEUE_HOST_BUSY;
  1295. scb->io_ctx = cmd;
  1296. scb->platform_data->dev = dev;
  1297. hscb = scb->hscb;
  1298. cmd->host_scribble = (char *)scb;
  1299. /*
  1300. * Fill out basics of the HSCB.
  1301. */
  1302. hscb->control = 0;
  1303. hscb->scsiid = BUILD_SCSIID(ahc, cmd);
  1304. hscb->lun = cmd->device->lun;
  1305. mask = SCB_GET_TARGET_MASK(ahc, scb);
  1306. tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
  1307. SCB_GET_OUR_ID(scb),
  1308. SCB_GET_TARGET(ahc, scb), &tstate);
  1309. hscb->scsirate = tinfo->scsirate;
  1310. hscb->scsioffset = tinfo->curr.offset;
  1311. if ((tstate->ultraenb & mask) != 0)
  1312. hscb->control |= ULTRAENB;
  1313. if ((ahc->user_discenable & mask) != 0)
  1314. hscb->control |= DISCENB;
  1315. if ((tstate->auto_negotiate & mask) != 0) {
  1316. scb->flags |= SCB_AUTO_NEGOTIATE;
  1317. scb->hscb->control |= MK_MESSAGE;
  1318. }
  1319. if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
  1320. int msg_bytes;
  1321. uint8_t tag_msgs[2];
  1322. msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
  1323. if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
  1324. hscb->control |= tag_msgs[0];
  1325. if (tag_msgs[0] == MSG_ORDERED_TASK)
  1326. dev->commands_since_idle_or_otag = 0;
  1327. } else if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
  1328. && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
  1329. hscb->control |= MSG_ORDERED_TASK;
  1330. dev->commands_since_idle_or_otag = 0;
  1331. } else {
  1332. hscb->control |= MSG_SIMPLE_TASK;
  1333. }
  1334. }
  1335. hscb->cdb_len = cmd->cmd_len;
  1336. if (hscb->cdb_len <= 12) {
  1337. memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
  1338. } else {
  1339. memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
  1340. scb->flags |= SCB_CDB32_PTR;
  1341. }
  1342. scb->platform_data->xfer_len = 0;
  1343. ahc_set_residual(scb, 0);
  1344. ahc_set_sense_residual(scb, 0);
  1345. scb->sg_count = 0;
  1346. if (cmd->use_sg != 0) {
  1347. struct ahc_dma_seg *sg;
  1348. struct scatterlist *cur_seg;
  1349. struct scatterlist *end_seg;
  1350. int nseg;
  1351. cur_seg = (struct scatterlist *)cmd->request_buffer;
  1352. nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
  1353. cmd->sc_data_direction);
  1354. end_seg = cur_seg + nseg;
  1355. /* Copy the segments into the SG list. */
  1356. sg = scb->sg_list;
  1357. /*
  1358. * The sg_count may be larger than nseg if
  1359. * a transfer crosses a 32bit page.
  1360. */
  1361. while (cur_seg < end_seg) {
  1362. dma_addr_t addr;
  1363. bus_size_t len;
  1364. int consumed;
  1365. addr = sg_dma_address(cur_seg);
  1366. len = sg_dma_len(cur_seg);
  1367. consumed = ahc_linux_map_seg(ahc, scb,
  1368. sg, addr, len);
  1369. sg += consumed;
  1370. scb->sg_count += consumed;
  1371. cur_seg++;
  1372. }
  1373. sg--;
  1374. sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
  1375. /*
  1376. * Reset the sg list pointer.
  1377. */
  1378. scb->hscb->sgptr =
  1379. ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
  1380. /*
  1381. * Copy the first SG into the "current"
  1382. * data pointer area.
  1383. */
  1384. scb->hscb->dataptr = scb->sg_list->addr;
  1385. scb->hscb->datacnt = scb->sg_list->len;
  1386. } else if (cmd->request_bufflen != 0) {
  1387. struct ahc_dma_seg *sg;
  1388. dma_addr_t addr;
  1389. sg = scb->sg_list;
  1390. addr = pci_map_single(ahc->dev_softc,
  1391. cmd->request_buffer,
  1392. cmd->request_bufflen,
  1393. cmd->sc_data_direction);
  1394. scb->platform_data->buf_busaddr = addr;
  1395. scb->sg_count = ahc_linux_map_seg(ahc, scb,
  1396. sg, addr,
  1397. cmd->request_bufflen);
  1398. sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
  1399. /*
  1400. * Reset the sg list pointer.
  1401. */
  1402. scb->hscb->sgptr =
  1403. ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
  1404. /*
  1405. * Copy the first SG into the "current"
  1406. * data pointer area.
  1407. */
  1408. scb->hscb->dataptr = sg->addr;
  1409. scb->hscb->datacnt = sg->len;
  1410. } else {
  1411. scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
  1412. scb->hscb->dataptr = 0;
  1413. scb->hscb->datacnt = 0;
  1414. scb->sg_count = 0;
  1415. }
  1416. LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
  1417. dev->openings--;
  1418. dev->active++;
  1419. dev->commands_issued++;
  1420. if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
  1421. dev->commands_since_idle_or_otag++;
  1422. scb->flags |= SCB_ACTIVE;
  1423. if (untagged_q) {
  1424. TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
  1425. scb->flags |= SCB_UNTAGGEDQ;
  1426. }
  1427. ahc_queue_scb(ahc, scb);
  1428. return 0;
  1429. }
  1430. /*
  1431. * SCSI controller interrupt handler.
  1432. */
  1433. irqreturn_t
  1434. ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
  1435. {
  1436. struct ahc_softc *ahc;
  1437. u_long flags;
  1438. int ours;
  1439. ahc = (struct ahc_softc *) dev_id;
  1440. ahc_lock(ahc, &flags);
  1441. ours = ahc_intr(ahc);
  1442. ahc_unlock(ahc, &flags);
  1443. return IRQ_RETVAL(ours);
  1444. }
  1445. void
  1446. ahc_platform_flushwork(struct ahc_softc *ahc)
  1447. {
  1448. }
  1449. void
  1450. ahc_send_async(struct ahc_softc *ahc, char channel,
  1451. u_int target, u_int lun, ac_code code, void *arg)
  1452. {
  1453. switch (code) {
  1454. case AC_TRANSFER_NEG:
  1455. {
  1456. char buf[80];
  1457. struct scsi_target *starget;
  1458. struct ahc_linux_target *targ;
  1459. struct info_str info;
  1460. struct ahc_initiator_tinfo *tinfo;
  1461. struct ahc_tmode_tstate *tstate;
  1462. int target_offset;
  1463. unsigned int target_ppr_options;
  1464. BUG_ON(target == CAM_TARGET_WILDCARD);
  1465. info.buffer = buf;
  1466. info.length = sizeof(buf);
  1467. info.offset = 0;
  1468. info.pos = 0;
  1469. tinfo = ahc_fetch_transinfo(ahc, channel,
  1470. channel == 'A' ? ahc->our_id
  1471. : ahc->our_id_b,
  1472. target, &tstate);
  1473. /*
  1474. * Don't bother reporting results while
  1475. * negotiations are still pending.
  1476. */
  1477. if (tinfo->curr.period != tinfo->goal.period
  1478. || tinfo->curr.width != tinfo->goal.width
  1479. || tinfo->curr.offset != tinfo->goal.offset
  1480. || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
  1481. if (bootverbose == 0)
  1482. break;
  1483. /*
  1484. * Don't bother reporting results that
  1485. * are identical to those last reported.
  1486. */
  1487. target_offset = target;
  1488. if (channel == 'B')
  1489. target_offset += 8;
  1490. starget = ahc->platform_data->starget[target_offset];
  1491. if (starget == NULL)
  1492. break;
  1493. targ = scsi_transport_target_data(starget);
  1494. target_ppr_options =
  1495. (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
  1496. + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
  1497. + (spi_iu(starget) ? MSG_EXT_PPR_IU_REQ : 0);
  1498. if (tinfo->curr.period == spi_period(starget)
  1499. && tinfo->curr.width == spi_width(starget)
  1500. && tinfo->curr.offset == spi_offset(starget)
  1501. && tinfo->curr.ppr_options == target_ppr_options)
  1502. if (bootverbose == 0)
  1503. break;
  1504. spi_period(starget) = tinfo->curr.period;
  1505. spi_width(starget) = tinfo->curr.width;
  1506. spi_offset(starget) = tinfo->curr.offset;
  1507. spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
  1508. spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
  1509. spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
  1510. spi_display_xfer_agreement(starget);
  1511. break;
  1512. }
  1513. case AC_SENT_BDR:
  1514. {
  1515. WARN_ON(lun != CAM_LUN_WILDCARD);
  1516. scsi_report_device_reset(ahc->platform_data->host,
  1517. channel - 'A', target);
  1518. break;
  1519. }
  1520. case AC_BUS_RESET:
  1521. if (ahc->platform_data->host != NULL) {
  1522. scsi_report_bus_reset(ahc->platform_data->host,
  1523. channel - 'A');
  1524. }
  1525. break;
  1526. default:
  1527. panic("ahc_send_async: Unexpected async event");
  1528. }
  1529. }
  1530. /*
  1531. * Calls the higher level scsi done function and frees the scb.
  1532. */
  1533. void
  1534. ahc_done(struct ahc_softc *ahc, struct scb *scb)
  1535. {
  1536. struct scsi_cmnd *cmd;
  1537. struct ahc_linux_device *dev;
  1538. LIST_REMOVE(scb, pending_links);
  1539. if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
  1540. struct scb_tailq *untagged_q;
  1541. int target_offset;
  1542. target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
  1543. untagged_q = &(ahc->untagged_queues[target_offset]);
  1544. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  1545. BUG_ON(!TAILQ_EMPTY(untagged_q));
  1546. }
  1547. if ((scb->flags & SCB_ACTIVE) == 0) {
  1548. printf("SCB %d done'd twice\n", scb->hscb->tag);
  1549. ahc_dump_card_state(ahc);
  1550. panic("Stopping for safety");
  1551. }
  1552. cmd = scb->io_ctx;
  1553. dev = scb->platform_data->dev;
  1554. dev->active--;
  1555. dev->openings++;
  1556. if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
  1557. cmd->result &= ~(CAM_DEV_QFRZN << 16);
  1558. dev->qfrozen--;
  1559. }
  1560. ahc_linux_unmap_scb(ahc, scb);
  1561. /*
  1562. * Guard against stale sense data.
  1563. * The Linux mid-layer assumes that sense
  1564. * was retrieved anytime the first byte of
  1565. * the sense buffer looks "sane".
  1566. */
  1567. cmd->sense_buffer[0] = 0;
  1568. if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
  1569. uint32_t amount_xferred;
  1570. amount_xferred =
  1571. ahc_get_transfer_length(scb) - ahc_get_residual(scb);
  1572. if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
  1573. #ifdef AHC_DEBUG
  1574. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  1575. ahc_print_path(ahc, scb);
  1576. printf("Set CAM_UNCOR_PARITY\n");
  1577. }
  1578. #endif
  1579. ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
  1580. #ifdef AHC_REPORT_UNDERFLOWS
  1581. /*
  1582. * This code is disabled by default as some
  1583. * clients of the SCSI system do not properly
  1584. * initialize the underflow parameter. This
  1585. * results in spurious termination of commands
  1586. * that complete as expected (e.g. underflow is
  1587. * allowed as command can return variable amounts
  1588. * of data.
  1589. */
  1590. } else if (amount_xferred < scb->io_ctx->underflow) {
  1591. u_int i;
  1592. ahc_print_path(ahc, scb);
  1593. printf("CDB:");
  1594. for (i = 0; i < scb->io_ctx->cmd_len; i++)
  1595. printf(" 0x%x", scb->io_ctx->cmnd[i]);
  1596. printf("\n");
  1597. ahc_print_path(ahc, scb);
  1598. printf("Saw underflow (%ld of %ld bytes). "
  1599. "Treated as error\n",
  1600. ahc_get_residual(scb),
  1601. ahc_get_transfer_length(scb));
  1602. ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1603. #endif
  1604. } else {
  1605. ahc_set_transaction_status(scb, CAM_REQ_CMP);
  1606. }
  1607. } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
  1608. ahc_linux_handle_scsi_status(ahc, cmd->device, scb);
  1609. }
  1610. if (dev->openings == 1
  1611. && ahc_get_transaction_status(scb) == CAM_REQ_CMP
  1612. && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
  1613. dev->tag_success_count++;
  1614. /*
  1615. * Some devices deal with temporary internal resource
  1616. * shortages by returning queue full. When the queue
  1617. * full occurrs, we throttle back. Slowly try to get
  1618. * back to our previous queue depth.
  1619. */
  1620. if ((dev->openings + dev->active) < dev->maxtags
  1621. && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
  1622. dev->tag_success_count = 0;
  1623. dev->openings++;
  1624. }
  1625. if (dev->active == 0)
  1626. dev->commands_since_idle_or_otag = 0;
  1627. if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
  1628. printf("Recovery SCB completes\n");
  1629. if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
  1630. || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
  1631. ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
  1632. if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
  1633. ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
  1634. up(&ahc->platform_data->eh_sem);
  1635. }
  1636. }
  1637. ahc_free_scb(ahc, scb);
  1638. ahc_linux_queue_cmd_complete(ahc, cmd);
  1639. }
  1640. static void
  1641. ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
  1642. struct scsi_device *sdev, struct scb *scb)
  1643. {
  1644. struct ahc_devinfo devinfo;
  1645. struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
  1646. ahc_compile_devinfo(&devinfo,
  1647. ahc->our_id,
  1648. sdev->sdev_target->id, sdev->lun,
  1649. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1650. ROLE_INITIATOR);
  1651. /*
  1652. * We don't currently trust the mid-layer to
  1653. * properly deal with queue full or busy. So,
  1654. * when one occurs, we tell the mid-layer to
  1655. * unconditionally requeue the command to us
  1656. * so that we can retry it ourselves. We also
  1657. * implement our own throttling mechanism so
  1658. * we don't clobber the device with too many
  1659. * commands.
  1660. */
  1661. switch (ahc_get_scsi_status(scb)) {
  1662. default:
  1663. break;
  1664. case SCSI_STATUS_CHECK_COND:
  1665. case SCSI_STATUS_CMD_TERMINATED:
  1666. {
  1667. struct scsi_cmnd *cmd;
  1668. /*
  1669. * Copy sense information to the OS's cmd
  1670. * structure if it is available.
  1671. */
  1672. cmd = scb->io_ctx;
  1673. if (scb->flags & SCB_SENSE) {
  1674. u_int sense_size;
  1675. sense_size = MIN(sizeof(struct scsi_sense_data)
  1676. - ahc_get_sense_residual(scb),
  1677. sizeof(cmd->sense_buffer));
  1678. memcpy(cmd->sense_buffer,
  1679. ahc_get_sense_buf(ahc, scb), sense_size);
  1680. if (sense_size < sizeof(cmd->sense_buffer))
  1681. memset(&cmd->sense_buffer[sense_size], 0,
  1682. sizeof(cmd->sense_buffer) - sense_size);
  1683. cmd->result |= (DRIVER_SENSE << 24);
  1684. #ifdef AHC_DEBUG
  1685. if (ahc_debug & AHC_SHOW_SENSE) {
  1686. int i;
  1687. printf("Copied %d bytes of sense data:",
  1688. sense_size);
  1689. for (i = 0; i < sense_size; i++) {
  1690. if ((i & 0xF) == 0)
  1691. printf("\n");
  1692. printf("0x%x ", cmd->sense_buffer[i]);
  1693. }
  1694. printf("\n");
  1695. }
  1696. #endif
  1697. }
  1698. break;
  1699. }
  1700. case SCSI_STATUS_QUEUE_FULL:
  1701. {
  1702. /*
  1703. * By the time the core driver has returned this
  1704. * command, all other commands that were queued
  1705. * to us but not the device have been returned.
  1706. * This ensures that dev->active is equal to
  1707. * the number of commands actually queued to
  1708. * the device.
  1709. */
  1710. dev->tag_success_count = 0;
  1711. if (dev->active != 0) {
  1712. /*
  1713. * Drop our opening count to the number
  1714. * of commands currently outstanding.
  1715. */
  1716. dev->openings = 0;
  1717. /*
  1718. ahc_print_path(ahc, scb);
  1719. printf("Dropping tag count to %d\n", dev->active);
  1720. */
  1721. if (dev->active == dev->tags_on_last_queuefull) {
  1722. dev->last_queuefull_same_count++;
  1723. /*
  1724. * If we repeatedly see a queue full
  1725. * at the same queue depth, this
  1726. * device has a fixed number of tag
  1727. * slots. Lock in this tag depth
  1728. * so we stop seeing queue fulls from
  1729. * this device.
  1730. */
  1731. if (dev->last_queuefull_same_count
  1732. == AHC_LOCK_TAGS_COUNT) {
  1733. dev->maxtags = dev->active;
  1734. ahc_print_path(ahc, scb);
  1735. printf("Locking max tag count at %d\n",
  1736. dev->active);
  1737. }
  1738. } else {
  1739. dev->tags_on_last_queuefull = dev->active;
  1740. dev->last_queuefull_same_count = 0;
  1741. }
  1742. ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
  1743. ahc_set_scsi_status(scb, SCSI_STATUS_OK);
  1744. ahc_platform_set_tags(ahc, &devinfo,
  1745. (dev->flags & AHC_DEV_Q_BASIC)
  1746. ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
  1747. break;
  1748. }
  1749. /*
  1750. * Drop down to a single opening, and treat this
  1751. * as if the target returned BUSY SCSI status.
  1752. */
  1753. dev->openings = 1;
  1754. ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
  1755. ahc_platform_set_tags(ahc, &devinfo,
  1756. (dev->flags & AHC_DEV_Q_BASIC)
  1757. ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
  1758. break;
  1759. }
  1760. }
  1761. }
  1762. static void
  1763. ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, struct scsi_cmnd *cmd)
  1764. {
  1765. /*
  1766. * Map CAM error codes into Linux Error codes. We
  1767. * avoid the conversion so that the DV code has the
  1768. * full error information available when making
  1769. * state change decisions.
  1770. */
  1771. {
  1772. u_int new_status;
  1773. switch (ahc_cmd_get_transaction_status(cmd)) {
  1774. case CAM_REQ_INPROG:
  1775. case CAM_REQ_CMP:
  1776. case CAM_SCSI_STATUS_ERROR:
  1777. new_status = DID_OK;
  1778. break;
  1779. case CAM_REQ_ABORTED:
  1780. new_status = DID_ABORT;
  1781. break;
  1782. case CAM_BUSY:
  1783. new_status = DID_BUS_BUSY;
  1784. break;
  1785. case CAM_REQ_INVALID:
  1786. case CAM_PATH_INVALID:
  1787. new_status = DID_BAD_TARGET;
  1788. break;
  1789. case CAM_SEL_TIMEOUT:
  1790. new_status = DID_NO_CONNECT;
  1791. break;
  1792. case CAM_SCSI_BUS_RESET:
  1793. case CAM_BDR_SENT:
  1794. new_status = DID_RESET;
  1795. break;
  1796. case CAM_UNCOR_PARITY:
  1797. new_status = DID_PARITY;
  1798. break;
  1799. case CAM_CMD_TIMEOUT:
  1800. new_status = DID_TIME_OUT;
  1801. break;
  1802. case CAM_UA_ABORT:
  1803. case CAM_REQ_CMP_ERR:
  1804. case CAM_AUTOSENSE_FAIL:
  1805. case CAM_NO_HBA:
  1806. case CAM_DATA_RUN_ERR:
  1807. case CAM_UNEXP_BUSFREE:
  1808. case CAM_SEQUENCE_FAIL:
  1809. case CAM_CCB_LEN_ERR:
  1810. case CAM_PROVIDE_FAIL:
  1811. case CAM_REQ_TERMIO:
  1812. case CAM_UNREC_HBA_ERROR:
  1813. case CAM_REQ_TOO_BIG:
  1814. new_status = DID_ERROR;
  1815. break;
  1816. case CAM_REQUEUE_REQ:
  1817. new_status = DID_REQUEUE;
  1818. break;
  1819. default:
  1820. /* We should never get here */
  1821. new_status = DID_ERROR;
  1822. break;
  1823. }
  1824. ahc_cmd_set_transaction_status(cmd, new_status);
  1825. }
  1826. cmd->scsi_done(cmd);
  1827. }
  1828. static void
  1829. ahc_linux_sem_timeout(u_long arg)
  1830. {
  1831. struct ahc_softc *ahc;
  1832. u_long s;
  1833. ahc = (struct ahc_softc *)arg;
  1834. ahc_lock(ahc, &s);
  1835. if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
  1836. ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
  1837. up(&ahc->platform_data->eh_sem);
  1838. }
  1839. ahc_unlock(ahc, &s);
  1840. }
  1841. static void
  1842. ahc_linux_freeze_simq(struct ahc_softc *ahc)
  1843. {
  1844. ahc->platform_data->qfrozen++;
  1845. if (ahc->platform_data->qfrozen == 1) {
  1846. scsi_block_requests(ahc->platform_data->host);
  1847. /* XXX What about Twin channels? */
  1848. ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  1849. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  1850. ROLE_INITIATOR, CAM_REQUEUE_REQ);
  1851. }
  1852. }
  1853. static void
  1854. ahc_linux_release_simq(u_long arg)
  1855. {
  1856. struct ahc_softc *ahc;
  1857. u_long s;
  1858. int unblock_reqs;
  1859. ahc = (struct ahc_softc *)arg;
  1860. unblock_reqs = 0;
  1861. ahc_lock(ahc, &s);
  1862. if (ahc->platform_data->qfrozen > 0)
  1863. ahc->platform_data->qfrozen--;
  1864. if (ahc->platform_data->qfrozen == 0)
  1865. unblock_reqs = 1;
  1866. ahc_unlock(ahc, &s);
  1867. /*
  1868. * There is still a race here. The mid-layer
  1869. * should keep its own freeze count and use
  1870. * a bottom half handler to run the queues
  1871. * so we can unblock with our own lock held.
  1872. */
  1873. if (unblock_reqs)
  1874. scsi_unblock_requests(ahc->platform_data->host);
  1875. }
  1876. static int
  1877. ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
  1878. {
  1879. struct ahc_softc *ahc;
  1880. struct ahc_linux_device *dev;
  1881. struct scb *pending_scb;
  1882. u_int saved_scbptr;
  1883. u_int active_scb_index;
  1884. u_int last_phase;
  1885. u_int saved_scsiid;
  1886. u_int cdb_byte;
  1887. int retval;
  1888. int was_paused;
  1889. int paused;
  1890. int wait;
  1891. int disconnected;
  1892. pending_scb = NULL;
  1893. paused = FALSE;
  1894. wait = FALSE;
  1895. ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
  1896. printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
  1897. ahc_name(ahc), cmd->device->channel,
  1898. cmd->device->id, cmd->device->lun,
  1899. flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
  1900. printf("CDB:");
  1901. for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
  1902. printf(" 0x%x", cmd->cmnd[cdb_byte]);
  1903. printf("\n");
  1904. spin_lock_irq(&ahc->platform_data->spin_lock);
  1905. /*
  1906. * First determine if we currently own this command.
  1907. * Start by searching the device queue. If not found
  1908. * there, check the pending_scb list. If not found
  1909. * at all, and the system wanted us to just abort the
  1910. * command, return success.
  1911. */
  1912. dev = scsi_transport_device_data(cmd->device);
  1913. if (dev == NULL) {
  1914. /*
  1915. * No target device for this command exists,
  1916. * so we must not still own the command.
  1917. */
  1918. printf("%s:%d:%d:%d: Is not an active device\n",
  1919. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1920. cmd->device->lun);
  1921. retval = SUCCESS;
  1922. goto no_cmd;
  1923. }
  1924. if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
  1925. && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
  1926. cmd->device->channel + 'A',
  1927. cmd->device->lun,
  1928. CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
  1929. printf("%s:%d:%d:%d: Command found on untagged queue\n",
  1930. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1931. cmd->device->lun);
  1932. retval = SUCCESS;
  1933. goto done;
  1934. }
  1935. /*
  1936. * See if we can find a matching cmd in the pending list.
  1937. */
  1938. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1939. if (pending_scb->io_ctx == cmd)
  1940. break;
  1941. }
  1942. if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
  1943. /* Any SCB for this device will do for a target reset */
  1944. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1945. if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
  1946. cmd->device->channel + 'A',
  1947. CAM_LUN_WILDCARD,
  1948. SCB_LIST_NULL, ROLE_INITIATOR) == 0)
  1949. break;
  1950. }
  1951. }
  1952. if (pending_scb == NULL) {
  1953. printf("%s:%d:%d:%d: Command not found\n",
  1954. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1955. cmd->device->lun);
  1956. goto no_cmd;
  1957. }
  1958. if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
  1959. /*
  1960. * We can't queue two recovery actions using the same SCB
  1961. */
  1962. retval = FAILED;
  1963. goto done;
  1964. }
  1965. /*
  1966. * Ensure that the card doesn't do anything
  1967. * behind our back and that we didn't "just" miss
  1968. * an interrupt that would affect this cmd.
  1969. */
  1970. was_paused = ahc_is_paused(ahc);
  1971. ahc_pause_and_flushwork(ahc);
  1972. paused = TRUE;
  1973. if ((pending_scb->flags & SCB_ACTIVE) == 0) {
  1974. printf("%s:%d:%d:%d: Command already completed\n",
  1975. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1976. cmd->device->lun);
  1977. goto no_cmd;
  1978. }
  1979. printf("%s: At time of recovery, card was %spaused\n",
  1980. ahc_name(ahc), was_paused ? "" : "not ");
  1981. ahc_dump_card_state(ahc);
  1982. disconnected = TRUE;
  1983. if (flag == SCB_ABORT) {
  1984. if (ahc_search_qinfifo(ahc, cmd->device->id,
  1985. cmd->device->channel + 'A',
  1986. cmd->device->lun,
  1987. pending_scb->hscb->tag,
  1988. ROLE_INITIATOR, CAM_REQ_ABORTED,
  1989. SEARCH_COMPLETE) > 0) {
  1990. printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
  1991. ahc_name(ahc), cmd->device->channel,
  1992. cmd->device->id, cmd->device->lun);
  1993. retval = SUCCESS;
  1994. goto done;
  1995. }
  1996. } else if (ahc_search_qinfifo(ahc, cmd->device->id,
  1997. cmd->device->channel + 'A',
  1998. cmd->device->lun, pending_scb->hscb->tag,
  1999. ROLE_INITIATOR, /*status*/0,
  2000. SEARCH_COUNT) > 0) {
  2001. disconnected = FALSE;
  2002. }
  2003. if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
  2004. struct scb *bus_scb;
  2005. bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
  2006. if (bus_scb == pending_scb)
  2007. disconnected = FALSE;
  2008. else if (flag != SCB_ABORT
  2009. && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
  2010. && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
  2011. disconnected = FALSE;
  2012. }
  2013. /*
  2014. * At this point, pending_scb is the scb associated with the
  2015. * passed in command. That command is currently active on the
  2016. * bus, is in the disconnected state, or we're hoping to find
  2017. * a command for the same target active on the bus to abuse to
  2018. * send a BDR. Queue the appropriate message based on which of
  2019. * these states we are in.
  2020. */
  2021. last_phase = ahc_inb(ahc, LASTPHASE);
  2022. saved_scbptr = ahc_inb(ahc, SCBPTR);
  2023. active_scb_index = ahc_inb(ahc, SCB_TAG);
  2024. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  2025. if (last_phase != P_BUSFREE
  2026. && (pending_scb->hscb->tag == active_scb_index
  2027. || (flag == SCB_DEVICE_RESET
  2028. && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
  2029. /*
  2030. * We're active on the bus, so assert ATN
  2031. * and hope that the target responds.
  2032. */
  2033. pending_scb = ahc_lookup_scb(ahc, active_scb_index);
  2034. pending_scb->flags |= SCB_RECOVERY_SCB|flag;
  2035. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  2036. ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
  2037. printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
  2038. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  2039. cmd->device->lun);
  2040. wait = TRUE;
  2041. } else if (disconnected) {
  2042. /*
  2043. * Actually re-queue this SCB in an attempt
  2044. * to select the device before it reconnects.
  2045. * In either case (selection or reselection),
  2046. * we will now issue the approprate message
  2047. * to the timed-out device.
  2048. *
  2049. * Set the MK_MESSAGE control bit indicating
  2050. * that we desire to send a message. We
  2051. * also set the disconnected flag since
  2052. * in the paging case there is no guarantee
  2053. * that our SCB control byte matches the
  2054. * version on the card. We don't want the
  2055. * sequencer to abort the command thinking
  2056. * an unsolicited reselection occurred.
  2057. */
  2058. pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
  2059. pending_scb->flags |= SCB_RECOVERY_SCB|flag;
  2060. /*
  2061. * Remove any cached copy of this SCB in the
  2062. * disconnected list in preparation for the
  2063. * queuing of our abort SCB. We use the
  2064. * same element in the SCB, SCB_NEXT, for
  2065. * both the qinfifo and the disconnected list.
  2066. */
  2067. ahc_search_disc_list(ahc, cmd->device->id,
  2068. cmd->device->channel + 'A',
  2069. cmd->device->lun, pending_scb->hscb->tag,
  2070. /*stop_on_first*/TRUE,
  2071. /*remove*/TRUE,
  2072. /*save_state*/FALSE);
  2073. /*
  2074. * In the non-paging case, the sequencer will
  2075. * never re-reference the in-core SCB.
  2076. * To make sure we are notified during
  2077. * reslection, set the MK_MESSAGE flag in
  2078. * the card's copy of the SCB.
  2079. */
  2080. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  2081. ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
  2082. ahc_outb(ahc, SCB_CONTROL,
  2083. ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
  2084. }
  2085. /*
  2086. * Clear out any entries in the QINFIFO first
  2087. * so we are the next SCB for this target
  2088. * to run.
  2089. */
  2090. ahc_search_qinfifo(ahc, cmd->device->id,
  2091. cmd->device->channel + 'A',
  2092. cmd->device->lun, SCB_LIST_NULL,
  2093. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  2094. SEARCH_COMPLETE);
  2095. ahc_qinfifo_requeue_tail(ahc, pending_scb);
  2096. ahc_outb(ahc, SCBPTR, saved_scbptr);
  2097. ahc_print_path(ahc, pending_scb);
  2098. printf("Device is disconnected, re-queuing SCB\n");
  2099. wait = TRUE;
  2100. } else {
  2101. printf("%s:%d:%d:%d: Unable to deliver message\n",
  2102. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  2103. cmd->device->lun);
  2104. retval = FAILED;
  2105. goto done;
  2106. }
  2107. no_cmd:
  2108. /*
  2109. * Our assumption is that if we don't have the command, no
  2110. * recovery action was required, so we return success. Again,
  2111. * the semantics of the mid-layer recovery engine are not
  2112. * well defined, so this may change in time.
  2113. */
  2114. retval = SUCCESS;
  2115. done:
  2116. if (paused)
  2117. ahc_unpause(ahc);
  2118. if (wait) {
  2119. struct timer_list timer;
  2120. int ret;
  2121. ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
  2122. spin_unlock_irq(&ahc->platform_data->spin_lock);
  2123. init_timer(&timer);
  2124. timer.data = (u_long)ahc;
  2125. timer.expires = jiffies + (5 * HZ);
  2126. timer.function = ahc_linux_sem_timeout;
  2127. add_timer(&timer);
  2128. printf("Recovery code sleeping\n");
  2129. down(&ahc->platform_data->eh_sem);
  2130. printf("Recovery code awake\n");
  2131. ret = del_timer_sync(&timer);
  2132. if (ret == 0) {
  2133. printf("Timer Expired\n");
  2134. retval = FAILED;
  2135. }
  2136. spin_lock_irq(&ahc->platform_data->spin_lock);
  2137. }
  2138. spin_unlock_irq(&ahc->platform_data->spin_lock);
  2139. return (retval);
  2140. }
  2141. void
  2142. ahc_platform_dump_card_state(struct ahc_softc *ahc)
  2143. {
  2144. }
  2145. static void ahc_linux_set_width(struct scsi_target *starget, int width)
  2146. {
  2147. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2148. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2149. struct ahc_devinfo devinfo;
  2150. unsigned long flags;
  2151. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2152. starget->channel + 'A', ROLE_INITIATOR);
  2153. ahc_lock(ahc, &flags);
  2154. ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
  2155. ahc_unlock(ahc, &flags);
  2156. }
  2157. static void ahc_linux_set_period(struct scsi_target *starget, int period)
  2158. {
  2159. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2160. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2161. struct ahc_tmode_tstate *tstate;
  2162. struct ahc_initiator_tinfo *tinfo
  2163. = ahc_fetch_transinfo(ahc,
  2164. starget->channel + 'A',
  2165. shost->this_id, starget->id, &tstate);
  2166. struct ahc_devinfo devinfo;
  2167. unsigned int ppr_options = tinfo->goal.ppr_options;
  2168. unsigned long flags;
  2169. unsigned long offset = tinfo->goal.offset;
  2170. struct ahc_syncrate *syncrate;
  2171. if (offset == 0)
  2172. offset = MAX_OFFSET;
  2173. if (period < 9)
  2174. period = 9; /* 12.5ns is our minimum */
  2175. if (period == 9)
  2176. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2177. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2178. starget->channel + 'A', ROLE_INITIATOR);
  2179. /* all PPR requests apart from QAS require wide transfers */
  2180. if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
  2181. if (spi_width(starget) == 0)
  2182. ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2183. }
  2184. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2185. ahc_lock(ahc, &flags);
  2186. ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
  2187. ppr_options, AHC_TRANS_GOAL, FALSE);
  2188. ahc_unlock(ahc, &flags);
  2189. }
  2190. static void ahc_linux_set_offset(struct scsi_target *starget, int offset)
  2191. {
  2192. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2193. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2194. struct ahc_tmode_tstate *tstate;
  2195. struct ahc_initiator_tinfo *tinfo
  2196. = ahc_fetch_transinfo(ahc,
  2197. starget->channel + 'A',
  2198. shost->this_id, starget->id, &tstate);
  2199. struct ahc_devinfo devinfo;
  2200. unsigned int ppr_options = 0;
  2201. unsigned int period = 0;
  2202. unsigned long flags;
  2203. struct ahc_syncrate *syncrate = NULL;
  2204. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2205. starget->channel + 'A', ROLE_INITIATOR);
  2206. if (offset != 0) {
  2207. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2208. period = tinfo->goal.period;
  2209. ppr_options = tinfo->goal.ppr_options;
  2210. }
  2211. ahc_lock(ahc, &flags);
  2212. ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
  2213. ppr_options, AHC_TRANS_GOAL, FALSE);
  2214. ahc_unlock(ahc, &flags);
  2215. }
  2216. static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
  2217. {
  2218. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2219. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2220. struct ahc_tmode_tstate *tstate;
  2221. struct ahc_initiator_tinfo *tinfo
  2222. = ahc_fetch_transinfo(ahc,
  2223. starget->channel + 'A',
  2224. shost->this_id, starget->id, &tstate);
  2225. struct ahc_devinfo devinfo;
  2226. unsigned int ppr_options = tinfo->goal.ppr_options
  2227. & ~MSG_EXT_PPR_DT_REQ;
  2228. unsigned int period = tinfo->goal.period;
  2229. unsigned int width = tinfo->goal.width;
  2230. unsigned long flags;
  2231. struct ahc_syncrate *syncrate;
  2232. if (dt) {
  2233. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2234. if (!width)
  2235. ahc_linux_set_width(starget, 1);
  2236. } else if (period == 9)
  2237. period = 10; /* if resetting DT, period must be >= 25ns */
  2238. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2239. starget->channel + 'A', ROLE_INITIATOR);
  2240. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,AHC_SYNCRATE_DT);
  2241. ahc_lock(ahc, &flags);
  2242. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2243. ppr_options, AHC_TRANS_GOAL, FALSE);
  2244. ahc_unlock(ahc, &flags);
  2245. }
  2246. #if 0
  2247. /* FIXME: This code claims to support IU and QAS. However, the actual
  2248. * sequencer code and aic7xxx_core have no support for these parameters and
  2249. * will get into a bad state if they're negotiated. Do not enable this
  2250. * unless you know what you're doing */
  2251. static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
  2252. {
  2253. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2254. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2255. struct ahc_tmode_tstate *tstate;
  2256. struct ahc_initiator_tinfo *tinfo
  2257. = ahc_fetch_transinfo(ahc,
  2258. starget->channel + 'A',
  2259. shost->this_id, starget->id, &tstate);
  2260. struct ahc_devinfo devinfo;
  2261. unsigned int ppr_options = tinfo->goal.ppr_options
  2262. & ~MSG_EXT_PPR_QAS_REQ;
  2263. unsigned int period = tinfo->goal.period;
  2264. unsigned long flags;
  2265. struct ahc_syncrate *syncrate;
  2266. if (qas)
  2267. ppr_options |= MSG_EXT_PPR_QAS_REQ;
  2268. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2269. starget->channel + 'A', ROLE_INITIATOR);
  2270. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2271. ahc_lock(ahc, &flags);
  2272. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2273. ppr_options, AHC_TRANS_GOAL, FALSE);
  2274. ahc_unlock(ahc, &flags);
  2275. }
  2276. static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
  2277. {
  2278. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2279. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2280. struct ahc_tmode_tstate *tstate;
  2281. struct ahc_initiator_tinfo *tinfo
  2282. = ahc_fetch_transinfo(ahc,
  2283. starget->channel + 'A',
  2284. shost->this_id, starget->id, &tstate);
  2285. struct ahc_devinfo devinfo;
  2286. unsigned int ppr_options = tinfo->goal.ppr_options
  2287. & ~MSG_EXT_PPR_IU_REQ;
  2288. unsigned int period = tinfo->goal.period;
  2289. unsigned long flags;
  2290. struct ahc_syncrate *syncrate;
  2291. if (iu)
  2292. ppr_options |= MSG_EXT_PPR_IU_REQ;
  2293. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2294. starget->channel + 'A', ROLE_INITIATOR);
  2295. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2296. ahc_lock(ahc, &flags);
  2297. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2298. ppr_options, AHC_TRANS_GOAL, FALSE);
  2299. ahc_unlock(ahc, &flags);
  2300. }
  2301. #endif
  2302. static struct spi_function_template ahc_linux_transport_functions = {
  2303. .set_offset = ahc_linux_set_offset,
  2304. .show_offset = 1,
  2305. .set_period = ahc_linux_set_period,
  2306. .show_period = 1,
  2307. .set_width = ahc_linux_set_width,
  2308. .show_width = 1,
  2309. .set_dt = ahc_linux_set_dt,
  2310. .show_dt = 1,
  2311. #if 0
  2312. .set_iu = ahc_linux_set_iu,
  2313. .show_iu = 1,
  2314. .set_qas = ahc_linux_set_qas,
  2315. .show_qas = 1,
  2316. #endif
  2317. };
  2318. static int __init
  2319. ahc_linux_init(void)
  2320. {
  2321. /*
  2322. * If we've been passed any parameters, process them now.
  2323. */
  2324. if (aic7xxx)
  2325. aic7xxx_setup(aic7xxx);
  2326. ahc_linux_transport_template =
  2327. spi_attach_transport(&ahc_linux_transport_functions);
  2328. if (!ahc_linux_transport_template)
  2329. return -ENODEV;
  2330. scsi_transport_reserve_target(ahc_linux_transport_template,
  2331. sizeof(struct ahc_linux_target));
  2332. scsi_transport_reserve_device(ahc_linux_transport_template,
  2333. sizeof(struct ahc_linux_device));
  2334. ahc_linux_pci_init();
  2335. ahc_linux_eisa_init();
  2336. return 0;
  2337. }
  2338. static void
  2339. ahc_linux_exit(void)
  2340. {
  2341. ahc_linux_pci_exit();
  2342. ahc_linux_eisa_exit();
  2343. spi_release_transport(ahc_linux_transport_template);
  2344. }
  2345. module_init(ahc_linux_init);
  2346. module_exit(ahc_linux_exit);