aic7xxx_osm.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  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. uint64_t
  984. ahc_linux_get_memsize(void)
  985. {
  986. struct sysinfo si;
  987. si_meminfo(&si);
  988. return ((uint64_t)si.totalram << PAGE_SHIFT);
  989. }
  990. /*
  991. * Place the SCSI bus into a known state by either resetting it,
  992. * or forcing transfer negotiations on the next command to any
  993. * target.
  994. */
  995. void
  996. ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
  997. {
  998. int i;
  999. int numtarg;
  1000. i = 0;
  1001. numtarg = 0;
  1002. if (aic7xxx_no_reset != 0)
  1003. ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
  1004. if ((ahc->flags & AHC_RESET_BUS_A) != 0)
  1005. ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
  1006. else
  1007. numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
  1008. if ((ahc->features & AHC_TWIN) != 0) {
  1009. if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
  1010. ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
  1011. } else {
  1012. if (numtarg == 0)
  1013. i = 8;
  1014. numtarg += 8;
  1015. }
  1016. }
  1017. /*
  1018. * Force negotiation to async for all targets that
  1019. * will not see an initial bus reset.
  1020. */
  1021. for (; i < numtarg; i++) {
  1022. struct ahc_devinfo devinfo;
  1023. struct ahc_initiator_tinfo *tinfo;
  1024. struct ahc_tmode_tstate *tstate;
  1025. u_int our_id;
  1026. u_int target_id;
  1027. char channel;
  1028. channel = 'A';
  1029. our_id = ahc->our_id;
  1030. target_id = i;
  1031. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  1032. channel = 'B';
  1033. our_id = ahc->our_id_b;
  1034. target_id = i % 8;
  1035. }
  1036. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  1037. target_id, &tstate);
  1038. ahc_compile_devinfo(&devinfo, our_id, target_id,
  1039. CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
  1040. ahc_update_neg_request(ahc, &devinfo, tstate,
  1041. tinfo, AHC_NEG_ALWAYS);
  1042. }
  1043. /* Give the bus some time to recover */
  1044. if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
  1045. ahc_linux_freeze_simq(ahc);
  1046. init_timer(&ahc->platform_data->reset_timer);
  1047. ahc->platform_data->reset_timer.data = (u_long)ahc;
  1048. ahc->platform_data->reset_timer.expires =
  1049. jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
  1050. ahc->platform_data->reset_timer.function =
  1051. ahc_linux_release_simq;
  1052. add_timer(&ahc->platform_data->reset_timer);
  1053. }
  1054. }
  1055. int
  1056. ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
  1057. {
  1058. ahc->platform_data =
  1059. malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
  1060. if (ahc->platform_data == NULL)
  1061. return (ENOMEM);
  1062. memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
  1063. ahc->platform_data->irq = AHC_LINUX_NOIRQ;
  1064. ahc_lockinit(ahc);
  1065. init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
  1066. ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
  1067. ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
  1068. if (aic7xxx_pci_parity == 0)
  1069. ahc->flags |= AHC_DISABLE_PCI_PERR;
  1070. return (0);
  1071. }
  1072. void
  1073. ahc_platform_free(struct ahc_softc *ahc)
  1074. {
  1075. struct scsi_target *starget;
  1076. int i, j;
  1077. if (ahc->platform_data != NULL) {
  1078. if (ahc->platform_data->host != NULL) {
  1079. scsi_remove_host(ahc->platform_data->host);
  1080. scsi_host_put(ahc->platform_data->host);
  1081. }
  1082. /* destroy all of the device and target objects */
  1083. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  1084. starget = ahc->platform_data->starget[i];
  1085. if (starget != NULL) {
  1086. for (j = 0; j < AHC_NUM_LUNS; j++) {
  1087. struct ahc_linux_target *targ =
  1088. scsi_transport_target_data(starget);
  1089. if (targ->sdev[j] == NULL)
  1090. continue;
  1091. targ->sdev[j] = NULL;
  1092. }
  1093. ahc->platform_data->starget[i] = NULL;
  1094. }
  1095. }
  1096. if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
  1097. free_irq(ahc->platform_data->irq, ahc);
  1098. if (ahc->tag == BUS_SPACE_PIO
  1099. && ahc->bsh.ioport != 0)
  1100. release_region(ahc->bsh.ioport, 256);
  1101. if (ahc->tag == BUS_SPACE_MEMIO
  1102. && ahc->bsh.maddr != NULL) {
  1103. iounmap(ahc->bsh.maddr);
  1104. release_mem_region(ahc->platform_data->mem_busaddr,
  1105. 0x1000);
  1106. }
  1107. free(ahc->platform_data, M_DEVBUF);
  1108. }
  1109. }
  1110. void
  1111. ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  1112. {
  1113. ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
  1114. SCB_GET_CHANNEL(ahc, scb),
  1115. SCB_GET_LUN(scb), SCB_LIST_NULL,
  1116. ROLE_UNKNOWN, CAM_REQUEUE_REQ);
  1117. }
  1118. void
  1119. ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1120. ahc_queue_alg alg)
  1121. {
  1122. struct scsi_target *starget;
  1123. struct ahc_linux_target *targ;
  1124. struct ahc_linux_device *dev;
  1125. struct scsi_device *sdev;
  1126. u_int target_offset;
  1127. int was_queuing;
  1128. int now_queuing;
  1129. target_offset = devinfo->target;
  1130. if (devinfo->channel != 'A')
  1131. target_offset += 8;
  1132. starget = ahc->platform_data->starget[target_offset];
  1133. targ = scsi_transport_target_data(starget);
  1134. BUG_ON(targ == NULL);
  1135. sdev = targ->sdev[devinfo->lun];
  1136. if (sdev == NULL)
  1137. return;
  1138. dev = scsi_transport_device_data(sdev);
  1139. was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
  1140. switch (alg) {
  1141. default:
  1142. case AHC_QUEUE_NONE:
  1143. now_queuing = 0;
  1144. break;
  1145. case AHC_QUEUE_BASIC:
  1146. now_queuing = AHC_DEV_Q_BASIC;
  1147. break;
  1148. case AHC_QUEUE_TAGGED:
  1149. now_queuing = AHC_DEV_Q_TAGGED;
  1150. break;
  1151. }
  1152. if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
  1153. && (was_queuing != now_queuing)
  1154. && (dev->active != 0)) {
  1155. dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
  1156. dev->qfrozen++;
  1157. }
  1158. dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
  1159. if (now_queuing) {
  1160. u_int usertags;
  1161. usertags = ahc_linux_user_tagdepth(ahc, devinfo);
  1162. if (!was_queuing) {
  1163. /*
  1164. * Start out agressively and allow our
  1165. * dynamic queue depth algorithm to take
  1166. * care of the rest.
  1167. */
  1168. dev->maxtags = usertags;
  1169. dev->openings = dev->maxtags - dev->active;
  1170. }
  1171. if (dev->maxtags == 0) {
  1172. /*
  1173. * Queueing is disabled by the user.
  1174. */
  1175. dev->openings = 1;
  1176. } else if (alg == AHC_QUEUE_TAGGED) {
  1177. dev->flags |= AHC_DEV_Q_TAGGED;
  1178. if (aic7xxx_periodic_otag != 0)
  1179. dev->flags |= AHC_DEV_PERIODIC_OTAG;
  1180. } else
  1181. dev->flags |= AHC_DEV_Q_BASIC;
  1182. } else {
  1183. /* We can only have one opening. */
  1184. dev->maxtags = 0;
  1185. dev->openings = 1 - dev->active;
  1186. }
  1187. switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
  1188. case AHC_DEV_Q_BASIC:
  1189. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  1190. scsi_activate_tcq(sdev, dev->openings + dev->active);
  1191. break;
  1192. case AHC_DEV_Q_TAGGED:
  1193. scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
  1194. scsi_activate_tcq(sdev, dev->openings + dev->active);
  1195. break;
  1196. default:
  1197. /*
  1198. * We allow the OS to queue 2 untagged transactions to
  1199. * us at any time even though we can only execute them
  1200. * serially on the controller/device. This should
  1201. * remove some latency.
  1202. */
  1203. scsi_deactivate_tcq(sdev, 2);
  1204. break;
  1205. }
  1206. }
  1207. int
  1208. ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  1209. int lun, u_int tag, role_t role, uint32_t status)
  1210. {
  1211. return 0;
  1212. }
  1213. static u_int
  1214. ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  1215. {
  1216. static int warned_user;
  1217. u_int tags;
  1218. tags = 0;
  1219. if ((ahc->user_discenable & devinfo->target_mask) != 0) {
  1220. if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
  1221. if (warned_user == 0) {
  1222. printf(KERN_WARNING
  1223. "aic7xxx: WARNING: Insufficient tag_info instances\n"
  1224. "aic7xxx: for installed controllers. Using defaults\n"
  1225. "aic7xxx: Please update the aic7xxx_tag_info array in\n"
  1226. "aic7xxx: the aic7xxx_osm..c source file.\n");
  1227. warned_user++;
  1228. }
  1229. tags = AHC_MAX_QUEUE;
  1230. } else {
  1231. adapter_tag_info_t *tag_info;
  1232. tag_info = &aic7xxx_tag_info[ahc->unit];
  1233. tags = tag_info->tag_commands[devinfo->target_offset];
  1234. if (tags > AHC_MAX_QUEUE)
  1235. tags = AHC_MAX_QUEUE;
  1236. }
  1237. }
  1238. return (tags);
  1239. }
  1240. /*
  1241. * Determines the queue depth for a given device.
  1242. */
  1243. static void
  1244. ahc_linux_device_queue_depth(struct scsi_device *sdev)
  1245. {
  1246. struct ahc_devinfo devinfo;
  1247. u_int tags;
  1248. struct ahc_softc *ahc = *((struct ahc_softc **)sdev->host->hostdata);
  1249. ahc_compile_devinfo(&devinfo,
  1250. sdev->sdev_target->channel == 0
  1251. ? ahc->our_id : ahc->our_id_b,
  1252. sdev->sdev_target->id, sdev->lun,
  1253. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1254. ROLE_INITIATOR);
  1255. tags = ahc_linux_user_tagdepth(ahc, &devinfo);
  1256. if (tags != 0 && sdev->tagged_supported != 0) {
  1257. ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
  1258. ahc_print_devinfo(ahc, &devinfo);
  1259. printf("Tagged Queuing enabled. Depth %d\n", tags);
  1260. } else {
  1261. ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
  1262. }
  1263. }
  1264. static int
  1265. ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
  1266. struct scsi_cmnd *cmd)
  1267. {
  1268. struct scb *scb;
  1269. struct hardware_scb *hscb;
  1270. struct ahc_initiator_tinfo *tinfo;
  1271. struct ahc_tmode_tstate *tstate;
  1272. uint16_t mask;
  1273. struct scb_tailq *untagged_q = NULL;
  1274. /*
  1275. * Schedule us to run later. The only reason we are not
  1276. * running is because the whole controller Q is frozen.
  1277. */
  1278. if (ahc->platform_data->qfrozen != 0)
  1279. return SCSI_MLQUEUE_HOST_BUSY;
  1280. /*
  1281. * We only allow one untagged transaction
  1282. * per target in the initiator role unless
  1283. * we are storing a full busy target *lun*
  1284. * table in SCB space.
  1285. */
  1286. if (!blk_rq_tagged(cmd->request)
  1287. && (ahc->features & AHC_SCB_BTT) == 0) {
  1288. int target_offset;
  1289. target_offset = cmd->device->id + cmd->device->channel * 8;
  1290. untagged_q = &(ahc->untagged_queues[target_offset]);
  1291. if (!TAILQ_EMPTY(untagged_q))
  1292. /* if we're already executing an untagged command
  1293. * we're busy to another */
  1294. return SCSI_MLQUEUE_DEVICE_BUSY;
  1295. }
  1296. /*
  1297. * Get an scb to use.
  1298. */
  1299. scb = ahc_get_scb(ahc);
  1300. if (!scb)
  1301. return SCSI_MLQUEUE_HOST_BUSY;
  1302. scb->io_ctx = cmd;
  1303. scb->platform_data->dev = dev;
  1304. hscb = scb->hscb;
  1305. cmd->host_scribble = (char *)scb;
  1306. /*
  1307. * Fill out basics of the HSCB.
  1308. */
  1309. hscb->control = 0;
  1310. hscb->scsiid = BUILD_SCSIID(ahc, cmd);
  1311. hscb->lun = cmd->device->lun;
  1312. mask = SCB_GET_TARGET_MASK(ahc, scb);
  1313. tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
  1314. SCB_GET_OUR_ID(scb),
  1315. SCB_GET_TARGET(ahc, scb), &tstate);
  1316. hscb->scsirate = tinfo->scsirate;
  1317. hscb->scsioffset = tinfo->curr.offset;
  1318. if ((tstate->ultraenb & mask) != 0)
  1319. hscb->control |= ULTRAENB;
  1320. if ((ahc->user_discenable & mask) != 0)
  1321. hscb->control |= DISCENB;
  1322. if ((tstate->auto_negotiate & mask) != 0) {
  1323. scb->flags |= SCB_AUTO_NEGOTIATE;
  1324. scb->hscb->control |= MK_MESSAGE;
  1325. }
  1326. if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
  1327. int msg_bytes;
  1328. uint8_t tag_msgs[2];
  1329. msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
  1330. if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
  1331. hscb->control |= tag_msgs[0];
  1332. if (tag_msgs[0] == MSG_ORDERED_TASK)
  1333. dev->commands_since_idle_or_otag = 0;
  1334. } else if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
  1335. && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
  1336. hscb->control |= MSG_ORDERED_TASK;
  1337. dev->commands_since_idle_or_otag = 0;
  1338. } else {
  1339. hscb->control |= MSG_SIMPLE_TASK;
  1340. }
  1341. }
  1342. hscb->cdb_len = cmd->cmd_len;
  1343. if (hscb->cdb_len <= 12) {
  1344. memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
  1345. } else {
  1346. memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
  1347. scb->flags |= SCB_CDB32_PTR;
  1348. }
  1349. scb->platform_data->xfer_len = 0;
  1350. ahc_set_residual(scb, 0);
  1351. ahc_set_sense_residual(scb, 0);
  1352. scb->sg_count = 0;
  1353. if (cmd->use_sg != 0) {
  1354. struct ahc_dma_seg *sg;
  1355. struct scatterlist *cur_seg;
  1356. struct scatterlist *end_seg;
  1357. int nseg;
  1358. cur_seg = (struct scatterlist *)cmd->request_buffer;
  1359. nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
  1360. cmd->sc_data_direction);
  1361. end_seg = cur_seg + nseg;
  1362. /* Copy the segments into the SG list. */
  1363. sg = scb->sg_list;
  1364. /*
  1365. * The sg_count may be larger than nseg if
  1366. * a transfer crosses a 32bit page.
  1367. */
  1368. while (cur_seg < end_seg) {
  1369. dma_addr_t addr;
  1370. bus_size_t len;
  1371. int consumed;
  1372. addr = sg_dma_address(cur_seg);
  1373. len = sg_dma_len(cur_seg);
  1374. consumed = ahc_linux_map_seg(ahc, scb,
  1375. sg, addr, len);
  1376. sg += consumed;
  1377. scb->sg_count += consumed;
  1378. cur_seg++;
  1379. }
  1380. sg--;
  1381. sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
  1382. /*
  1383. * Reset the sg list pointer.
  1384. */
  1385. scb->hscb->sgptr =
  1386. ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
  1387. /*
  1388. * Copy the first SG into the "current"
  1389. * data pointer area.
  1390. */
  1391. scb->hscb->dataptr = scb->sg_list->addr;
  1392. scb->hscb->datacnt = scb->sg_list->len;
  1393. } else if (cmd->request_bufflen != 0) {
  1394. struct ahc_dma_seg *sg;
  1395. dma_addr_t addr;
  1396. sg = scb->sg_list;
  1397. addr = pci_map_single(ahc->dev_softc,
  1398. cmd->request_buffer,
  1399. cmd->request_bufflen,
  1400. cmd->sc_data_direction);
  1401. scb->platform_data->buf_busaddr = addr;
  1402. scb->sg_count = ahc_linux_map_seg(ahc, scb,
  1403. sg, addr,
  1404. cmd->request_bufflen);
  1405. sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
  1406. /*
  1407. * Reset the sg list pointer.
  1408. */
  1409. scb->hscb->sgptr =
  1410. ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
  1411. /*
  1412. * Copy the first SG into the "current"
  1413. * data pointer area.
  1414. */
  1415. scb->hscb->dataptr = sg->addr;
  1416. scb->hscb->datacnt = sg->len;
  1417. } else {
  1418. scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
  1419. scb->hscb->dataptr = 0;
  1420. scb->hscb->datacnt = 0;
  1421. scb->sg_count = 0;
  1422. }
  1423. LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
  1424. dev->openings--;
  1425. dev->active++;
  1426. dev->commands_issued++;
  1427. if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
  1428. dev->commands_since_idle_or_otag++;
  1429. scb->flags |= SCB_ACTIVE;
  1430. if (untagged_q) {
  1431. TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
  1432. scb->flags |= SCB_UNTAGGEDQ;
  1433. }
  1434. ahc_queue_scb(ahc, scb);
  1435. return 0;
  1436. }
  1437. /*
  1438. * SCSI controller interrupt handler.
  1439. */
  1440. irqreturn_t
  1441. ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
  1442. {
  1443. struct ahc_softc *ahc;
  1444. u_long flags;
  1445. int ours;
  1446. ahc = (struct ahc_softc *) dev_id;
  1447. ahc_lock(ahc, &flags);
  1448. ours = ahc_intr(ahc);
  1449. ahc_unlock(ahc, &flags);
  1450. return IRQ_RETVAL(ours);
  1451. }
  1452. void
  1453. ahc_platform_flushwork(struct ahc_softc *ahc)
  1454. {
  1455. }
  1456. void
  1457. ahc_send_async(struct ahc_softc *ahc, char channel,
  1458. u_int target, u_int lun, ac_code code, void *arg)
  1459. {
  1460. switch (code) {
  1461. case AC_TRANSFER_NEG:
  1462. {
  1463. char buf[80];
  1464. struct scsi_target *starget;
  1465. struct ahc_linux_target *targ;
  1466. struct info_str info;
  1467. struct ahc_initiator_tinfo *tinfo;
  1468. struct ahc_tmode_tstate *tstate;
  1469. int target_offset;
  1470. unsigned int target_ppr_options;
  1471. BUG_ON(target == CAM_TARGET_WILDCARD);
  1472. info.buffer = buf;
  1473. info.length = sizeof(buf);
  1474. info.offset = 0;
  1475. info.pos = 0;
  1476. tinfo = ahc_fetch_transinfo(ahc, channel,
  1477. channel == 'A' ? ahc->our_id
  1478. : ahc->our_id_b,
  1479. target, &tstate);
  1480. /*
  1481. * Don't bother reporting results while
  1482. * negotiations are still pending.
  1483. */
  1484. if (tinfo->curr.period != tinfo->goal.period
  1485. || tinfo->curr.width != tinfo->goal.width
  1486. || tinfo->curr.offset != tinfo->goal.offset
  1487. || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
  1488. if (bootverbose == 0)
  1489. break;
  1490. /*
  1491. * Don't bother reporting results that
  1492. * are identical to those last reported.
  1493. */
  1494. target_offset = target;
  1495. if (channel == 'B')
  1496. target_offset += 8;
  1497. starget = ahc->platform_data->starget[target_offset];
  1498. if (starget == NULL)
  1499. break;
  1500. targ = scsi_transport_target_data(starget);
  1501. target_ppr_options =
  1502. (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
  1503. + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
  1504. + (spi_iu(starget) ? MSG_EXT_PPR_IU_REQ : 0);
  1505. if (tinfo->curr.period == spi_period(starget)
  1506. && tinfo->curr.width == spi_width(starget)
  1507. && tinfo->curr.offset == spi_offset(starget)
  1508. && tinfo->curr.ppr_options == target_ppr_options)
  1509. if (bootverbose == 0)
  1510. break;
  1511. spi_period(starget) = tinfo->curr.period;
  1512. spi_width(starget) = tinfo->curr.width;
  1513. spi_offset(starget) = tinfo->curr.offset;
  1514. spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
  1515. spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
  1516. spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
  1517. spi_display_xfer_agreement(starget);
  1518. break;
  1519. }
  1520. case AC_SENT_BDR:
  1521. {
  1522. WARN_ON(lun != CAM_LUN_WILDCARD);
  1523. scsi_report_device_reset(ahc->platform_data->host,
  1524. channel - 'A', target);
  1525. break;
  1526. }
  1527. case AC_BUS_RESET:
  1528. if (ahc->platform_data->host != NULL) {
  1529. scsi_report_bus_reset(ahc->platform_data->host,
  1530. channel - 'A');
  1531. }
  1532. break;
  1533. default:
  1534. panic("ahc_send_async: Unexpected async event");
  1535. }
  1536. }
  1537. /*
  1538. * Calls the higher level scsi done function and frees the scb.
  1539. */
  1540. void
  1541. ahc_done(struct ahc_softc *ahc, struct scb *scb)
  1542. {
  1543. struct scsi_cmnd *cmd;
  1544. struct ahc_linux_device *dev;
  1545. LIST_REMOVE(scb, pending_links);
  1546. if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
  1547. struct scb_tailq *untagged_q;
  1548. int target_offset;
  1549. target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
  1550. untagged_q = &(ahc->untagged_queues[target_offset]);
  1551. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  1552. BUG_ON(!TAILQ_EMPTY(untagged_q));
  1553. }
  1554. if ((scb->flags & SCB_ACTIVE) == 0) {
  1555. printf("SCB %d done'd twice\n", scb->hscb->tag);
  1556. ahc_dump_card_state(ahc);
  1557. panic("Stopping for safety");
  1558. }
  1559. cmd = scb->io_ctx;
  1560. dev = scb->platform_data->dev;
  1561. dev->active--;
  1562. dev->openings++;
  1563. if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
  1564. cmd->result &= ~(CAM_DEV_QFRZN << 16);
  1565. dev->qfrozen--;
  1566. }
  1567. ahc_linux_unmap_scb(ahc, scb);
  1568. /*
  1569. * Guard against stale sense data.
  1570. * The Linux mid-layer assumes that sense
  1571. * was retrieved anytime the first byte of
  1572. * the sense buffer looks "sane".
  1573. */
  1574. cmd->sense_buffer[0] = 0;
  1575. if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
  1576. uint32_t amount_xferred;
  1577. amount_xferred =
  1578. ahc_get_transfer_length(scb) - ahc_get_residual(scb);
  1579. if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
  1580. #ifdef AHC_DEBUG
  1581. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  1582. ahc_print_path(ahc, scb);
  1583. printf("Set CAM_UNCOR_PARITY\n");
  1584. }
  1585. #endif
  1586. ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
  1587. #ifdef AHC_REPORT_UNDERFLOWS
  1588. /*
  1589. * This code is disabled by default as some
  1590. * clients of the SCSI system do not properly
  1591. * initialize the underflow parameter. This
  1592. * results in spurious termination of commands
  1593. * that complete as expected (e.g. underflow is
  1594. * allowed as command can return variable amounts
  1595. * of data.
  1596. */
  1597. } else if (amount_xferred < scb->io_ctx->underflow) {
  1598. u_int i;
  1599. ahc_print_path(ahc, scb);
  1600. printf("CDB:");
  1601. for (i = 0; i < scb->io_ctx->cmd_len; i++)
  1602. printf(" 0x%x", scb->io_ctx->cmnd[i]);
  1603. printf("\n");
  1604. ahc_print_path(ahc, scb);
  1605. printf("Saw underflow (%ld of %ld bytes). "
  1606. "Treated as error\n",
  1607. ahc_get_residual(scb),
  1608. ahc_get_transfer_length(scb));
  1609. ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1610. #endif
  1611. } else {
  1612. ahc_set_transaction_status(scb, CAM_REQ_CMP);
  1613. }
  1614. } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
  1615. ahc_linux_handle_scsi_status(ahc, cmd->device, scb);
  1616. }
  1617. if (dev->openings == 1
  1618. && ahc_get_transaction_status(scb) == CAM_REQ_CMP
  1619. && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
  1620. dev->tag_success_count++;
  1621. /*
  1622. * Some devices deal with temporary internal resource
  1623. * shortages by returning queue full. When the queue
  1624. * full occurrs, we throttle back. Slowly try to get
  1625. * back to our previous queue depth.
  1626. */
  1627. if ((dev->openings + dev->active) < dev->maxtags
  1628. && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
  1629. dev->tag_success_count = 0;
  1630. dev->openings++;
  1631. }
  1632. if (dev->active == 0)
  1633. dev->commands_since_idle_or_otag = 0;
  1634. if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
  1635. printf("Recovery SCB completes\n");
  1636. if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
  1637. || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
  1638. ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
  1639. if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
  1640. ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
  1641. up(&ahc->platform_data->eh_sem);
  1642. }
  1643. }
  1644. ahc_free_scb(ahc, scb);
  1645. ahc_linux_queue_cmd_complete(ahc, cmd);
  1646. }
  1647. static void
  1648. ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
  1649. struct scsi_device *sdev, struct scb *scb)
  1650. {
  1651. struct ahc_devinfo devinfo;
  1652. struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
  1653. ahc_compile_devinfo(&devinfo,
  1654. ahc->our_id,
  1655. sdev->sdev_target->id, sdev->lun,
  1656. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1657. ROLE_INITIATOR);
  1658. /*
  1659. * We don't currently trust the mid-layer to
  1660. * properly deal with queue full or busy. So,
  1661. * when one occurs, we tell the mid-layer to
  1662. * unconditionally requeue the command to us
  1663. * so that we can retry it ourselves. We also
  1664. * implement our own throttling mechanism so
  1665. * we don't clobber the device with too many
  1666. * commands.
  1667. */
  1668. switch (ahc_get_scsi_status(scb)) {
  1669. default:
  1670. break;
  1671. case SCSI_STATUS_CHECK_COND:
  1672. case SCSI_STATUS_CMD_TERMINATED:
  1673. {
  1674. struct scsi_cmnd *cmd;
  1675. /*
  1676. * Copy sense information to the OS's cmd
  1677. * structure if it is available.
  1678. */
  1679. cmd = scb->io_ctx;
  1680. if (scb->flags & SCB_SENSE) {
  1681. u_int sense_size;
  1682. sense_size = MIN(sizeof(struct scsi_sense_data)
  1683. - ahc_get_sense_residual(scb),
  1684. sizeof(cmd->sense_buffer));
  1685. memcpy(cmd->sense_buffer,
  1686. ahc_get_sense_buf(ahc, scb), sense_size);
  1687. if (sense_size < sizeof(cmd->sense_buffer))
  1688. memset(&cmd->sense_buffer[sense_size], 0,
  1689. sizeof(cmd->sense_buffer) - sense_size);
  1690. cmd->result |= (DRIVER_SENSE << 24);
  1691. #ifdef AHC_DEBUG
  1692. if (ahc_debug & AHC_SHOW_SENSE) {
  1693. int i;
  1694. printf("Copied %d bytes of sense data:",
  1695. sense_size);
  1696. for (i = 0; i < sense_size; i++) {
  1697. if ((i & 0xF) == 0)
  1698. printf("\n");
  1699. printf("0x%x ", cmd->sense_buffer[i]);
  1700. }
  1701. printf("\n");
  1702. }
  1703. #endif
  1704. }
  1705. break;
  1706. }
  1707. case SCSI_STATUS_QUEUE_FULL:
  1708. {
  1709. /*
  1710. * By the time the core driver has returned this
  1711. * command, all other commands that were queued
  1712. * to us but not the device have been returned.
  1713. * This ensures that dev->active is equal to
  1714. * the number of commands actually queued to
  1715. * the device.
  1716. */
  1717. dev->tag_success_count = 0;
  1718. if (dev->active != 0) {
  1719. /*
  1720. * Drop our opening count to the number
  1721. * of commands currently outstanding.
  1722. */
  1723. dev->openings = 0;
  1724. /*
  1725. ahc_print_path(ahc, scb);
  1726. printf("Dropping tag count to %d\n", dev->active);
  1727. */
  1728. if (dev->active == dev->tags_on_last_queuefull) {
  1729. dev->last_queuefull_same_count++;
  1730. /*
  1731. * If we repeatedly see a queue full
  1732. * at the same queue depth, this
  1733. * device has a fixed number of tag
  1734. * slots. Lock in this tag depth
  1735. * so we stop seeing queue fulls from
  1736. * this device.
  1737. */
  1738. if (dev->last_queuefull_same_count
  1739. == AHC_LOCK_TAGS_COUNT) {
  1740. dev->maxtags = dev->active;
  1741. ahc_print_path(ahc, scb);
  1742. printf("Locking max tag count at %d\n",
  1743. dev->active);
  1744. }
  1745. } else {
  1746. dev->tags_on_last_queuefull = dev->active;
  1747. dev->last_queuefull_same_count = 0;
  1748. }
  1749. ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
  1750. ahc_set_scsi_status(scb, SCSI_STATUS_OK);
  1751. ahc_platform_set_tags(ahc, &devinfo,
  1752. (dev->flags & AHC_DEV_Q_BASIC)
  1753. ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
  1754. break;
  1755. }
  1756. /*
  1757. * Drop down to a single opening, and treat this
  1758. * as if the target returned BUSY SCSI status.
  1759. */
  1760. dev->openings = 1;
  1761. ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
  1762. ahc_platform_set_tags(ahc, &devinfo,
  1763. (dev->flags & AHC_DEV_Q_BASIC)
  1764. ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
  1765. break;
  1766. }
  1767. }
  1768. }
  1769. static void
  1770. ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, struct scsi_cmnd *cmd)
  1771. {
  1772. /*
  1773. * Map CAM error codes into Linux Error codes. We
  1774. * avoid the conversion so that the DV code has the
  1775. * full error information available when making
  1776. * state change decisions.
  1777. */
  1778. {
  1779. u_int new_status;
  1780. switch (ahc_cmd_get_transaction_status(cmd)) {
  1781. case CAM_REQ_INPROG:
  1782. case CAM_REQ_CMP:
  1783. case CAM_SCSI_STATUS_ERROR:
  1784. new_status = DID_OK;
  1785. break;
  1786. case CAM_REQ_ABORTED:
  1787. new_status = DID_ABORT;
  1788. break;
  1789. case CAM_BUSY:
  1790. new_status = DID_BUS_BUSY;
  1791. break;
  1792. case CAM_REQ_INVALID:
  1793. case CAM_PATH_INVALID:
  1794. new_status = DID_BAD_TARGET;
  1795. break;
  1796. case CAM_SEL_TIMEOUT:
  1797. new_status = DID_NO_CONNECT;
  1798. break;
  1799. case CAM_SCSI_BUS_RESET:
  1800. case CAM_BDR_SENT:
  1801. new_status = DID_RESET;
  1802. break;
  1803. case CAM_UNCOR_PARITY:
  1804. new_status = DID_PARITY;
  1805. break;
  1806. case CAM_CMD_TIMEOUT:
  1807. new_status = DID_TIME_OUT;
  1808. break;
  1809. case CAM_UA_ABORT:
  1810. case CAM_REQ_CMP_ERR:
  1811. case CAM_AUTOSENSE_FAIL:
  1812. case CAM_NO_HBA:
  1813. case CAM_DATA_RUN_ERR:
  1814. case CAM_UNEXP_BUSFREE:
  1815. case CAM_SEQUENCE_FAIL:
  1816. case CAM_CCB_LEN_ERR:
  1817. case CAM_PROVIDE_FAIL:
  1818. case CAM_REQ_TERMIO:
  1819. case CAM_UNREC_HBA_ERROR:
  1820. case CAM_REQ_TOO_BIG:
  1821. new_status = DID_ERROR;
  1822. break;
  1823. case CAM_REQUEUE_REQ:
  1824. new_status = DID_REQUEUE;
  1825. break;
  1826. default:
  1827. /* We should never get here */
  1828. new_status = DID_ERROR;
  1829. break;
  1830. }
  1831. ahc_cmd_set_transaction_status(cmd, new_status);
  1832. }
  1833. cmd->scsi_done(cmd);
  1834. }
  1835. static void
  1836. ahc_linux_sem_timeout(u_long arg)
  1837. {
  1838. struct ahc_softc *ahc;
  1839. u_long s;
  1840. ahc = (struct ahc_softc *)arg;
  1841. ahc_lock(ahc, &s);
  1842. if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
  1843. ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
  1844. up(&ahc->platform_data->eh_sem);
  1845. }
  1846. ahc_unlock(ahc, &s);
  1847. }
  1848. static void
  1849. ahc_linux_freeze_simq(struct ahc_softc *ahc)
  1850. {
  1851. ahc->platform_data->qfrozen++;
  1852. if (ahc->platform_data->qfrozen == 1) {
  1853. scsi_block_requests(ahc->platform_data->host);
  1854. /* XXX What about Twin channels? */
  1855. ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  1856. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  1857. ROLE_INITIATOR, CAM_REQUEUE_REQ);
  1858. }
  1859. }
  1860. static void
  1861. ahc_linux_release_simq(u_long arg)
  1862. {
  1863. struct ahc_softc *ahc;
  1864. u_long s;
  1865. int unblock_reqs;
  1866. ahc = (struct ahc_softc *)arg;
  1867. unblock_reqs = 0;
  1868. ahc_lock(ahc, &s);
  1869. if (ahc->platform_data->qfrozen > 0)
  1870. ahc->platform_data->qfrozen--;
  1871. if (ahc->platform_data->qfrozen == 0)
  1872. unblock_reqs = 1;
  1873. ahc_unlock(ahc, &s);
  1874. /*
  1875. * There is still a race here. The mid-layer
  1876. * should keep its own freeze count and use
  1877. * a bottom half handler to run the queues
  1878. * so we can unblock with our own lock held.
  1879. */
  1880. if (unblock_reqs)
  1881. scsi_unblock_requests(ahc->platform_data->host);
  1882. }
  1883. static int
  1884. ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
  1885. {
  1886. struct ahc_softc *ahc;
  1887. struct ahc_linux_device *dev;
  1888. struct scb *pending_scb;
  1889. u_int saved_scbptr;
  1890. u_int active_scb_index;
  1891. u_int last_phase;
  1892. u_int saved_scsiid;
  1893. u_int cdb_byte;
  1894. int retval;
  1895. int was_paused;
  1896. int paused;
  1897. int wait;
  1898. int disconnected;
  1899. pending_scb = NULL;
  1900. paused = FALSE;
  1901. wait = FALSE;
  1902. ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
  1903. printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
  1904. ahc_name(ahc), cmd->device->channel,
  1905. cmd->device->id, cmd->device->lun,
  1906. flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
  1907. printf("CDB:");
  1908. for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
  1909. printf(" 0x%x", cmd->cmnd[cdb_byte]);
  1910. printf("\n");
  1911. spin_lock_irq(&ahc->platform_data->spin_lock);
  1912. /*
  1913. * First determine if we currently own this command.
  1914. * Start by searching the device queue. If not found
  1915. * there, check the pending_scb list. If not found
  1916. * at all, and the system wanted us to just abort the
  1917. * command, return success.
  1918. */
  1919. dev = scsi_transport_device_data(cmd->device);
  1920. if (dev == NULL) {
  1921. /*
  1922. * No target device for this command exists,
  1923. * so we must not still own the command.
  1924. */
  1925. printf("%s:%d:%d:%d: Is not an active device\n",
  1926. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1927. cmd->device->lun);
  1928. retval = SUCCESS;
  1929. goto no_cmd;
  1930. }
  1931. if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
  1932. && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
  1933. cmd->device->channel + 'A',
  1934. cmd->device->lun,
  1935. CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
  1936. printf("%s:%d:%d:%d: Command found on untagged queue\n",
  1937. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1938. cmd->device->lun);
  1939. retval = SUCCESS;
  1940. goto done;
  1941. }
  1942. /*
  1943. * See if we can find a matching cmd in the pending list.
  1944. */
  1945. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1946. if (pending_scb->io_ctx == cmd)
  1947. break;
  1948. }
  1949. if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
  1950. /* Any SCB for this device will do for a target reset */
  1951. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1952. if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
  1953. cmd->device->channel + 'A',
  1954. CAM_LUN_WILDCARD,
  1955. SCB_LIST_NULL, ROLE_INITIATOR) == 0)
  1956. break;
  1957. }
  1958. }
  1959. if (pending_scb == NULL) {
  1960. printf("%s:%d:%d:%d: Command not found\n",
  1961. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1962. cmd->device->lun);
  1963. goto no_cmd;
  1964. }
  1965. if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
  1966. /*
  1967. * We can't queue two recovery actions using the same SCB
  1968. */
  1969. retval = FAILED;
  1970. goto done;
  1971. }
  1972. /*
  1973. * Ensure that the card doesn't do anything
  1974. * behind our back and that we didn't "just" miss
  1975. * an interrupt that would affect this cmd.
  1976. */
  1977. was_paused = ahc_is_paused(ahc);
  1978. ahc_pause_and_flushwork(ahc);
  1979. paused = TRUE;
  1980. if ((pending_scb->flags & SCB_ACTIVE) == 0) {
  1981. printf("%s:%d:%d:%d: Command already completed\n",
  1982. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1983. cmd->device->lun);
  1984. goto no_cmd;
  1985. }
  1986. printf("%s: At time of recovery, card was %spaused\n",
  1987. ahc_name(ahc), was_paused ? "" : "not ");
  1988. ahc_dump_card_state(ahc);
  1989. disconnected = TRUE;
  1990. if (flag == SCB_ABORT) {
  1991. if (ahc_search_qinfifo(ahc, cmd->device->id,
  1992. cmd->device->channel + 'A',
  1993. cmd->device->lun,
  1994. pending_scb->hscb->tag,
  1995. ROLE_INITIATOR, CAM_REQ_ABORTED,
  1996. SEARCH_COMPLETE) > 0) {
  1997. printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
  1998. ahc_name(ahc), cmd->device->channel,
  1999. cmd->device->id, cmd->device->lun);
  2000. retval = SUCCESS;
  2001. goto done;
  2002. }
  2003. } else if (ahc_search_qinfifo(ahc, cmd->device->id,
  2004. cmd->device->channel + 'A',
  2005. cmd->device->lun, pending_scb->hscb->tag,
  2006. ROLE_INITIATOR, /*status*/0,
  2007. SEARCH_COUNT) > 0) {
  2008. disconnected = FALSE;
  2009. }
  2010. if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
  2011. struct scb *bus_scb;
  2012. bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
  2013. if (bus_scb == pending_scb)
  2014. disconnected = FALSE;
  2015. else if (flag != SCB_ABORT
  2016. && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
  2017. && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
  2018. disconnected = FALSE;
  2019. }
  2020. /*
  2021. * At this point, pending_scb is the scb associated with the
  2022. * passed in command. That command is currently active on the
  2023. * bus, is in the disconnected state, or we're hoping to find
  2024. * a command for the same target active on the bus to abuse to
  2025. * send a BDR. Queue the appropriate message based on which of
  2026. * these states we are in.
  2027. */
  2028. last_phase = ahc_inb(ahc, LASTPHASE);
  2029. saved_scbptr = ahc_inb(ahc, SCBPTR);
  2030. active_scb_index = ahc_inb(ahc, SCB_TAG);
  2031. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  2032. if (last_phase != P_BUSFREE
  2033. && (pending_scb->hscb->tag == active_scb_index
  2034. || (flag == SCB_DEVICE_RESET
  2035. && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
  2036. /*
  2037. * We're active on the bus, so assert ATN
  2038. * and hope that the target responds.
  2039. */
  2040. pending_scb = ahc_lookup_scb(ahc, active_scb_index);
  2041. pending_scb->flags |= SCB_RECOVERY_SCB|flag;
  2042. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  2043. ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
  2044. printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
  2045. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  2046. cmd->device->lun);
  2047. wait = TRUE;
  2048. } else if (disconnected) {
  2049. /*
  2050. * Actually re-queue this SCB in an attempt
  2051. * to select the device before it reconnects.
  2052. * In either case (selection or reselection),
  2053. * we will now issue the approprate message
  2054. * to the timed-out device.
  2055. *
  2056. * Set the MK_MESSAGE control bit indicating
  2057. * that we desire to send a message. We
  2058. * also set the disconnected flag since
  2059. * in the paging case there is no guarantee
  2060. * that our SCB control byte matches the
  2061. * version on the card. We don't want the
  2062. * sequencer to abort the command thinking
  2063. * an unsolicited reselection occurred.
  2064. */
  2065. pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
  2066. pending_scb->flags |= SCB_RECOVERY_SCB|flag;
  2067. /*
  2068. * Remove any cached copy of this SCB in the
  2069. * disconnected list in preparation for the
  2070. * queuing of our abort SCB. We use the
  2071. * same element in the SCB, SCB_NEXT, for
  2072. * both the qinfifo and the disconnected list.
  2073. */
  2074. ahc_search_disc_list(ahc, cmd->device->id,
  2075. cmd->device->channel + 'A',
  2076. cmd->device->lun, pending_scb->hscb->tag,
  2077. /*stop_on_first*/TRUE,
  2078. /*remove*/TRUE,
  2079. /*save_state*/FALSE);
  2080. /*
  2081. * In the non-paging case, the sequencer will
  2082. * never re-reference the in-core SCB.
  2083. * To make sure we are notified during
  2084. * reslection, set the MK_MESSAGE flag in
  2085. * the card's copy of the SCB.
  2086. */
  2087. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  2088. ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
  2089. ahc_outb(ahc, SCB_CONTROL,
  2090. ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
  2091. }
  2092. /*
  2093. * Clear out any entries in the QINFIFO first
  2094. * so we are the next SCB for this target
  2095. * to run.
  2096. */
  2097. ahc_search_qinfifo(ahc, cmd->device->id,
  2098. cmd->device->channel + 'A',
  2099. cmd->device->lun, SCB_LIST_NULL,
  2100. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  2101. SEARCH_COMPLETE);
  2102. ahc_qinfifo_requeue_tail(ahc, pending_scb);
  2103. ahc_outb(ahc, SCBPTR, saved_scbptr);
  2104. ahc_print_path(ahc, pending_scb);
  2105. printf("Device is disconnected, re-queuing SCB\n");
  2106. wait = TRUE;
  2107. } else {
  2108. printf("%s:%d:%d:%d: Unable to deliver message\n",
  2109. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  2110. cmd->device->lun);
  2111. retval = FAILED;
  2112. goto done;
  2113. }
  2114. no_cmd:
  2115. /*
  2116. * Our assumption is that if we don't have the command, no
  2117. * recovery action was required, so we return success. Again,
  2118. * the semantics of the mid-layer recovery engine are not
  2119. * well defined, so this may change in time.
  2120. */
  2121. retval = SUCCESS;
  2122. done:
  2123. if (paused)
  2124. ahc_unpause(ahc);
  2125. if (wait) {
  2126. struct timer_list timer;
  2127. int ret;
  2128. ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
  2129. spin_unlock_irq(&ahc->platform_data->spin_lock);
  2130. init_timer(&timer);
  2131. timer.data = (u_long)ahc;
  2132. timer.expires = jiffies + (5 * HZ);
  2133. timer.function = ahc_linux_sem_timeout;
  2134. add_timer(&timer);
  2135. printf("Recovery code sleeping\n");
  2136. down(&ahc->platform_data->eh_sem);
  2137. printf("Recovery code awake\n");
  2138. ret = del_timer_sync(&timer);
  2139. if (ret == 0) {
  2140. printf("Timer Expired\n");
  2141. retval = FAILED;
  2142. }
  2143. spin_lock_irq(&ahc->platform_data->spin_lock);
  2144. }
  2145. spin_unlock_irq(&ahc->platform_data->spin_lock);
  2146. return (retval);
  2147. }
  2148. void
  2149. ahc_platform_dump_card_state(struct ahc_softc *ahc)
  2150. {
  2151. }
  2152. static void ahc_linux_set_width(struct scsi_target *starget, int width)
  2153. {
  2154. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2155. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2156. struct ahc_devinfo devinfo;
  2157. unsigned long flags;
  2158. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2159. starget->channel + 'A', ROLE_INITIATOR);
  2160. ahc_lock(ahc, &flags);
  2161. ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
  2162. ahc_unlock(ahc, &flags);
  2163. }
  2164. static void ahc_linux_set_period(struct scsi_target *starget, int period)
  2165. {
  2166. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2167. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2168. struct ahc_tmode_tstate *tstate;
  2169. struct ahc_initiator_tinfo *tinfo
  2170. = ahc_fetch_transinfo(ahc,
  2171. starget->channel + 'A',
  2172. shost->this_id, starget->id, &tstate);
  2173. struct ahc_devinfo devinfo;
  2174. unsigned int ppr_options = tinfo->goal.ppr_options;
  2175. unsigned long flags;
  2176. unsigned long offset = tinfo->goal.offset;
  2177. struct ahc_syncrate *syncrate;
  2178. if (offset == 0)
  2179. offset = MAX_OFFSET;
  2180. if (period < 9)
  2181. period = 9; /* 12.5ns is our minimum */
  2182. if (period == 9)
  2183. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2184. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2185. starget->channel + 'A', ROLE_INITIATOR);
  2186. /* all PPR requests apart from QAS require wide transfers */
  2187. if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
  2188. if (spi_width(starget) == 0)
  2189. ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2190. }
  2191. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2192. ahc_lock(ahc, &flags);
  2193. ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
  2194. ppr_options, AHC_TRANS_GOAL, FALSE);
  2195. ahc_unlock(ahc, &flags);
  2196. }
  2197. static void ahc_linux_set_offset(struct scsi_target *starget, int offset)
  2198. {
  2199. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2200. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2201. struct ahc_tmode_tstate *tstate;
  2202. struct ahc_initiator_tinfo *tinfo
  2203. = ahc_fetch_transinfo(ahc,
  2204. starget->channel + 'A',
  2205. shost->this_id, starget->id, &tstate);
  2206. struct ahc_devinfo devinfo;
  2207. unsigned int ppr_options = 0;
  2208. unsigned int period = 0;
  2209. unsigned long flags;
  2210. struct ahc_syncrate *syncrate = NULL;
  2211. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2212. starget->channel + 'A', ROLE_INITIATOR);
  2213. if (offset != 0) {
  2214. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2215. period = tinfo->goal.period;
  2216. ppr_options = tinfo->goal.ppr_options;
  2217. }
  2218. ahc_lock(ahc, &flags);
  2219. ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
  2220. ppr_options, AHC_TRANS_GOAL, FALSE);
  2221. ahc_unlock(ahc, &flags);
  2222. }
  2223. static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
  2224. {
  2225. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2226. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2227. struct ahc_tmode_tstate *tstate;
  2228. struct ahc_initiator_tinfo *tinfo
  2229. = ahc_fetch_transinfo(ahc,
  2230. starget->channel + 'A',
  2231. shost->this_id, starget->id, &tstate);
  2232. struct ahc_devinfo devinfo;
  2233. unsigned int ppr_options = tinfo->goal.ppr_options
  2234. & ~MSG_EXT_PPR_DT_REQ;
  2235. unsigned int period = tinfo->goal.period;
  2236. unsigned int width = tinfo->goal.width;
  2237. unsigned long flags;
  2238. struct ahc_syncrate *syncrate;
  2239. if (dt) {
  2240. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2241. if (!width)
  2242. ahc_linux_set_width(starget, 1);
  2243. } else if (period == 9)
  2244. period = 10; /* if resetting DT, period must be >= 25ns */
  2245. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2246. starget->channel + 'A', ROLE_INITIATOR);
  2247. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,AHC_SYNCRATE_DT);
  2248. ahc_lock(ahc, &flags);
  2249. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2250. ppr_options, AHC_TRANS_GOAL, FALSE);
  2251. ahc_unlock(ahc, &flags);
  2252. }
  2253. #if 0
  2254. /* FIXME: This code claims to support IU and QAS. However, the actual
  2255. * sequencer code and aic7xxx_core have no support for these parameters and
  2256. * will get into a bad state if they're negotiated. Do not enable this
  2257. * unless you know what you're doing */
  2258. static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
  2259. {
  2260. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2261. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2262. struct ahc_tmode_tstate *tstate;
  2263. struct ahc_initiator_tinfo *tinfo
  2264. = ahc_fetch_transinfo(ahc,
  2265. starget->channel + 'A',
  2266. shost->this_id, starget->id, &tstate);
  2267. struct ahc_devinfo devinfo;
  2268. unsigned int ppr_options = tinfo->goal.ppr_options
  2269. & ~MSG_EXT_PPR_QAS_REQ;
  2270. unsigned int period = tinfo->goal.period;
  2271. unsigned long flags;
  2272. struct ahc_syncrate *syncrate;
  2273. if (qas)
  2274. ppr_options |= MSG_EXT_PPR_QAS_REQ;
  2275. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2276. starget->channel + 'A', ROLE_INITIATOR);
  2277. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2278. ahc_lock(ahc, &flags);
  2279. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2280. ppr_options, AHC_TRANS_GOAL, FALSE);
  2281. ahc_unlock(ahc, &flags);
  2282. }
  2283. static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
  2284. {
  2285. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2286. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2287. struct ahc_tmode_tstate *tstate;
  2288. struct ahc_initiator_tinfo *tinfo
  2289. = ahc_fetch_transinfo(ahc,
  2290. starget->channel + 'A',
  2291. shost->this_id, starget->id, &tstate);
  2292. struct ahc_devinfo devinfo;
  2293. unsigned int ppr_options = tinfo->goal.ppr_options
  2294. & ~MSG_EXT_PPR_IU_REQ;
  2295. unsigned int period = tinfo->goal.period;
  2296. unsigned long flags;
  2297. struct ahc_syncrate *syncrate;
  2298. if (iu)
  2299. ppr_options |= MSG_EXT_PPR_IU_REQ;
  2300. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2301. starget->channel + 'A', ROLE_INITIATOR);
  2302. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2303. ahc_lock(ahc, &flags);
  2304. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2305. ppr_options, AHC_TRANS_GOAL, FALSE);
  2306. ahc_unlock(ahc, &flags);
  2307. }
  2308. #endif
  2309. static struct spi_function_template ahc_linux_transport_functions = {
  2310. .set_offset = ahc_linux_set_offset,
  2311. .show_offset = 1,
  2312. .set_period = ahc_linux_set_period,
  2313. .show_period = 1,
  2314. .set_width = ahc_linux_set_width,
  2315. .show_width = 1,
  2316. .set_dt = ahc_linux_set_dt,
  2317. .show_dt = 1,
  2318. #if 0
  2319. .set_iu = ahc_linux_set_iu,
  2320. .show_iu = 1,
  2321. .set_qas = ahc_linux_set_qas,
  2322. .show_qas = 1,
  2323. #endif
  2324. };
  2325. static int __init
  2326. ahc_linux_init(void)
  2327. {
  2328. /*
  2329. * If we've been passed any parameters, process them now.
  2330. */
  2331. if (aic7xxx)
  2332. aic7xxx_setup(aic7xxx);
  2333. ahc_linux_transport_template =
  2334. spi_attach_transport(&ahc_linux_transport_functions);
  2335. if (!ahc_linux_transport_template)
  2336. return -ENODEV;
  2337. scsi_transport_reserve_target(ahc_linux_transport_template,
  2338. sizeof(struct ahc_linux_target));
  2339. scsi_transport_reserve_device(ahc_linux_transport_template,
  2340. sizeof(struct ahc_linux_device));
  2341. ahc_linux_pci_init();
  2342. ahc_linux_eisa_init();
  2343. return 0;
  2344. }
  2345. static void
  2346. ahc_linux_exit(void)
  2347. {
  2348. ahc_linux_pci_exit();
  2349. ahc_linux_eisa_exit();
  2350. spi_release_transport(ahc_linux_transport_template);
  2351. }
  2352. module_init(ahc_linux_init);
  2353. module_exit(ahc_linux_exit);