aic7xxx_osm.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  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. static 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. " tag_info:<tag_str> Set per-target tag depth\n"
  333. " global_tag_depth:<int> Global tag depth for every target\n"
  334. " on every bus\n"
  335. " seltime:<int> Selection Timeout\n"
  336. " (0/256ms,1/128ms,2/64ms,3/32ms)\n"
  337. "\n"
  338. " Sample /etc/modprobe.conf line:\n"
  339. " Toggle EISA/VLB probing\n"
  340. " Set tag depth on Controller 1/Target 1 to 10 tags\n"
  341. " Shorten the selection timeout to 128ms\n"
  342. "\n"
  343. " options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
  344. );
  345. static void ahc_linux_handle_scsi_status(struct ahc_softc *,
  346. struct scsi_device *,
  347. struct scb *);
  348. static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
  349. struct scsi_cmnd *cmd);
  350. static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
  351. static void ahc_linux_release_simq(struct ahc_softc *ahc);
  352. static int ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
  353. static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
  354. static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
  355. struct ahc_devinfo *devinfo);
  356. static void ahc_linux_device_queue_depth(struct scsi_device *);
  357. static int ahc_linux_run_command(struct ahc_softc*,
  358. struct ahc_linux_device *,
  359. struct scsi_cmnd *);
  360. static void ahc_linux_setup_tag_info_global(char *p);
  361. static int aic7xxx_setup(char *s);
  362. static int ahc_linux_unit;
  363. /********************************* Inlines ************************************/
  364. static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
  365. static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
  366. struct ahc_dma_seg *sg,
  367. dma_addr_t addr, bus_size_t len);
  368. static __inline void
  369. ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
  370. {
  371. struct scsi_cmnd *cmd;
  372. cmd = scb->io_ctx;
  373. ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
  374. scsi_dma_unmap(cmd);
  375. }
  376. static __inline int
  377. ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
  378. struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
  379. {
  380. int consumed;
  381. if ((scb->sg_count + 1) > AHC_NSEG)
  382. panic("Too few segs for dma mapping. "
  383. "Increase AHC_NSEG\n");
  384. consumed = 1;
  385. sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
  386. scb->platform_data->xfer_len += len;
  387. if (sizeof(dma_addr_t) > 4
  388. && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
  389. len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
  390. sg->len = ahc_htole32(len);
  391. return (consumed);
  392. }
  393. /*
  394. * Return a string describing the driver.
  395. */
  396. static const char *
  397. ahc_linux_info(struct Scsi_Host *host)
  398. {
  399. static char buffer[512];
  400. char ahc_info[256];
  401. char *bp;
  402. struct ahc_softc *ahc;
  403. bp = &buffer[0];
  404. ahc = *(struct ahc_softc **)host->hostdata;
  405. memset(bp, 0, sizeof(buffer));
  406. strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
  407. strcat(bp, AIC7XXX_DRIVER_VERSION);
  408. strcat(bp, "\n");
  409. strcat(bp, " <");
  410. strcat(bp, ahc->description);
  411. strcat(bp, ">\n");
  412. strcat(bp, " ");
  413. ahc_controller_info(ahc, ahc_info);
  414. strcat(bp, ahc_info);
  415. strcat(bp, "\n");
  416. return (bp);
  417. }
  418. /*
  419. * Queue an SCB to the controller.
  420. */
  421. static int
  422. ahc_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
  423. {
  424. struct ahc_softc *ahc;
  425. struct ahc_linux_device *dev = scsi_transport_device_data(cmd->device);
  426. int rtn = SCSI_MLQUEUE_HOST_BUSY;
  427. unsigned long flags;
  428. ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
  429. ahc_lock(ahc, &flags);
  430. if (ahc->platform_data->qfrozen == 0) {
  431. cmd->scsi_done = scsi_done;
  432. cmd->result = CAM_REQ_INPROG << 16;
  433. rtn = ahc_linux_run_command(ahc, dev, cmd);
  434. }
  435. ahc_unlock(ahc, &flags);
  436. return rtn;
  437. }
  438. static inline struct scsi_target **
  439. ahc_linux_target_in_softc(struct scsi_target *starget)
  440. {
  441. struct ahc_softc *ahc =
  442. *((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
  443. unsigned int target_offset;
  444. target_offset = starget->id;
  445. if (starget->channel != 0)
  446. target_offset += 8;
  447. return &ahc->platform_data->starget[target_offset];
  448. }
  449. static int
  450. ahc_linux_target_alloc(struct scsi_target *starget)
  451. {
  452. struct ahc_softc *ahc =
  453. *((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
  454. struct seeprom_config *sc = ahc->seep_config;
  455. unsigned long flags;
  456. struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
  457. unsigned short scsirate;
  458. struct ahc_devinfo devinfo;
  459. struct ahc_initiator_tinfo *tinfo;
  460. struct ahc_tmode_tstate *tstate;
  461. char channel = starget->channel + 'A';
  462. unsigned int our_id = ahc->our_id;
  463. unsigned int target_offset;
  464. target_offset = starget->id;
  465. if (starget->channel != 0)
  466. target_offset += 8;
  467. if (starget->channel)
  468. our_id = ahc->our_id_b;
  469. ahc_lock(ahc, &flags);
  470. BUG_ON(*ahc_targp != NULL);
  471. *ahc_targp = starget;
  472. if (sc) {
  473. int maxsync = AHC_SYNCRATE_DT;
  474. int ultra = 0;
  475. int flags = sc->device_flags[target_offset];
  476. if (ahc->flags & AHC_NEWEEPROM_FMT) {
  477. if (flags & CFSYNCHISULTRA)
  478. ultra = 1;
  479. } else if (flags & CFULTRAEN)
  480. ultra = 1;
  481. /* AIC nutcase; 10MHz appears as ultra = 1, CFXFER = 0x04
  482. * change it to ultra=0, CFXFER = 0 */
  483. if(ultra && (flags & CFXFER) == 0x04) {
  484. ultra = 0;
  485. flags &= ~CFXFER;
  486. }
  487. if ((ahc->features & AHC_ULTRA2) != 0) {
  488. scsirate = (flags & CFXFER) | (ultra ? 0x8 : 0);
  489. } else {
  490. scsirate = (flags & CFXFER) << 4;
  491. maxsync = ultra ? AHC_SYNCRATE_ULTRA :
  492. AHC_SYNCRATE_FAST;
  493. }
  494. spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
  495. if (!(flags & CFSYNCH))
  496. spi_max_offset(starget) = 0;
  497. spi_min_period(starget) =
  498. ahc_find_period(ahc, scsirate, maxsync);
  499. tinfo = ahc_fetch_transinfo(ahc, channel, ahc->our_id,
  500. starget->id, &tstate);
  501. }
  502. ahc_compile_devinfo(&devinfo, our_id, starget->id,
  503. CAM_LUN_WILDCARD, channel,
  504. ROLE_INITIATOR);
  505. ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
  506. AHC_TRANS_GOAL, /*paused*/FALSE);
  507. ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  508. AHC_TRANS_GOAL, /*paused*/FALSE);
  509. ahc_unlock(ahc, &flags);
  510. return 0;
  511. }
  512. static void
  513. ahc_linux_target_destroy(struct scsi_target *starget)
  514. {
  515. struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
  516. *ahc_targp = NULL;
  517. }
  518. static int
  519. ahc_linux_slave_alloc(struct scsi_device *sdev)
  520. {
  521. struct ahc_softc *ahc =
  522. *((struct ahc_softc **)sdev->host->hostdata);
  523. struct scsi_target *starget = sdev->sdev_target;
  524. struct ahc_linux_device *dev;
  525. if (bootverbose)
  526. printf("%s: Slave Alloc %d\n", ahc_name(ahc), sdev->id);
  527. dev = scsi_transport_device_data(sdev);
  528. memset(dev, 0, sizeof(*dev));
  529. /*
  530. * We start out life using untagged
  531. * transactions of which we allow one.
  532. */
  533. dev->openings = 1;
  534. /*
  535. * Set maxtags to 0. This will be changed if we
  536. * later determine that we are dealing with
  537. * a tagged queuing capable device.
  538. */
  539. dev->maxtags = 0;
  540. spi_period(starget) = 0;
  541. return 0;
  542. }
  543. static int
  544. ahc_linux_slave_configure(struct scsi_device *sdev)
  545. {
  546. struct ahc_softc *ahc;
  547. ahc = *((struct ahc_softc **)sdev->host->hostdata);
  548. if (bootverbose)
  549. sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
  550. ahc_linux_device_queue_depth(sdev);
  551. /* Initial Domain Validation */
  552. if (!spi_initial_dv(sdev->sdev_target))
  553. spi_dv_device(sdev);
  554. return 0;
  555. }
  556. #if defined(__i386__)
  557. /*
  558. * Return the disk geometry for the given SCSI device.
  559. */
  560. static int
  561. ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  562. sector_t capacity, int geom[])
  563. {
  564. uint8_t *bh;
  565. int heads;
  566. int sectors;
  567. int cylinders;
  568. int ret;
  569. int extended;
  570. struct ahc_softc *ahc;
  571. u_int channel;
  572. ahc = *((struct ahc_softc **)sdev->host->hostdata);
  573. channel = sdev_channel(sdev);
  574. bh = scsi_bios_ptable(bdev);
  575. if (bh) {
  576. ret = scsi_partsize(bh, capacity,
  577. &geom[2], &geom[0], &geom[1]);
  578. kfree(bh);
  579. if (ret != -1)
  580. return (ret);
  581. }
  582. heads = 64;
  583. sectors = 32;
  584. cylinders = aic_sector_div(capacity, heads, sectors);
  585. if (aic7xxx_extended != 0)
  586. extended = 1;
  587. else if (channel == 0)
  588. extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
  589. else
  590. extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
  591. if (extended && cylinders >= 1024) {
  592. heads = 255;
  593. sectors = 63;
  594. cylinders = aic_sector_div(capacity, heads, sectors);
  595. }
  596. geom[0] = heads;
  597. geom[1] = sectors;
  598. geom[2] = cylinders;
  599. return (0);
  600. }
  601. #endif
  602. /*
  603. * Abort the current SCSI command(s).
  604. */
  605. static int
  606. ahc_linux_abort(struct scsi_cmnd *cmd)
  607. {
  608. int error;
  609. error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
  610. if (error != 0)
  611. printf("aic7xxx_abort returns 0x%x\n", error);
  612. return (error);
  613. }
  614. /*
  615. * Attempt to send a target reset message to the device that timed out.
  616. */
  617. static int
  618. ahc_linux_dev_reset(struct scsi_cmnd *cmd)
  619. {
  620. int error;
  621. error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
  622. if (error != 0)
  623. printf("aic7xxx_dev_reset returns 0x%x\n", error);
  624. return (error);
  625. }
  626. /*
  627. * Reset the SCSI bus.
  628. */
  629. static int
  630. ahc_linux_bus_reset(struct scsi_cmnd *cmd)
  631. {
  632. struct ahc_softc *ahc;
  633. int found;
  634. unsigned long flags;
  635. ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
  636. ahc_lock(ahc, &flags);
  637. found = ahc_reset_channel(ahc, scmd_channel(cmd) + 'A',
  638. /*initiate reset*/TRUE);
  639. ahc_unlock(ahc, &flags);
  640. if (bootverbose)
  641. printf("%s: SCSI bus reset delivered. "
  642. "%d SCBs aborted.\n", ahc_name(ahc), found);
  643. return SUCCESS;
  644. }
  645. struct scsi_host_template aic7xxx_driver_template = {
  646. .module = THIS_MODULE,
  647. .name = "aic7xxx",
  648. .proc_name = "aic7xxx",
  649. .proc_info = ahc_linux_proc_info,
  650. .info = ahc_linux_info,
  651. .queuecommand = ahc_linux_queue,
  652. .eh_abort_handler = ahc_linux_abort,
  653. .eh_device_reset_handler = ahc_linux_dev_reset,
  654. .eh_bus_reset_handler = ahc_linux_bus_reset,
  655. #if defined(__i386__)
  656. .bios_param = ahc_linux_biosparam,
  657. #endif
  658. .can_queue = AHC_MAX_QUEUE,
  659. .this_id = -1,
  660. .max_sectors = 8192,
  661. .cmd_per_lun = 2,
  662. .use_clustering = ENABLE_CLUSTERING,
  663. .slave_alloc = ahc_linux_slave_alloc,
  664. .slave_configure = ahc_linux_slave_configure,
  665. .target_alloc = ahc_linux_target_alloc,
  666. .target_destroy = ahc_linux_target_destroy,
  667. };
  668. /**************************** Tasklet Handler *********************************/
  669. /******************************** Macros **************************************/
  670. #define BUILD_SCSIID(ahc, cmd) \
  671. ((((cmd)->device->id << TID_SHIFT) & TID) \
  672. | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
  673. | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
  674. /******************************** Bus DMA *************************************/
  675. int
  676. ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
  677. bus_size_t alignment, bus_size_t boundary,
  678. dma_addr_t lowaddr, dma_addr_t highaddr,
  679. bus_dma_filter_t *filter, void *filterarg,
  680. bus_size_t maxsize, int nsegments,
  681. bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
  682. {
  683. bus_dma_tag_t dmat;
  684. dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
  685. if (dmat == NULL)
  686. return (ENOMEM);
  687. /*
  688. * Linux is very simplistic about DMA memory. For now don't
  689. * maintain all specification information. Once Linux supplies
  690. * better facilities for doing these operations, or the
  691. * needs of this particular driver change, we might need to do
  692. * more here.
  693. */
  694. dmat->alignment = alignment;
  695. dmat->boundary = boundary;
  696. dmat->maxsize = maxsize;
  697. *ret_tag = dmat;
  698. return (0);
  699. }
  700. void
  701. ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
  702. {
  703. free(dmat, M_DEVBUF);
  704. }
  705. int
  706. ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
  707. int flags, bus_dmamap_t *mapp)
  708. {
  709. *vaddr = pci_alloc_consistent(ahc->dev_softc,
  710. dmat->maxsize, mapp);
  711. if (*vaddr == NULL)
  712. return ENOMEM;
  713. return 0;
  714. }
  715. void
  716. ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
  717. void* vaddr, bus_dmamap_t map)
  718. {
  719. pci_free_consistent(ahc->dev_softc, dmat->maxsize,
  720. vaddr, map);
  721. }
  722. int
  723. ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
  724. void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
  725. void *cb_arg, int flags)
  726. {
  727. /*
  728. * Assume for now that this will only be used during
  729. * initialization and not for per-transaction buffer mapping.
  730. */
  731. bus_dma_segment_t stack_sg;
  732. stack_sg.ds_addr = map;
  733. stack_sg.ds_len = dmat->maxsize;
  734. cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
  735. return (0);
  736. }
  737. void
  738. ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
  739. {
  740. }
  741. int
  742. ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
  743. {
  744. /* Nothing to do */
  745. return (0);
  746. }
  747. static void
  748. ahc_linux_setup_tag_info_global(char *p)
  749. {
  750. int tags, i, j;
  751. tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
  752. printf("Setting Global Tags= %d\n", tags);
  753. for (i = 0; i < ARRAY_SIZE(aic7xxx_tag_info); i++) {
  754. for (j = 0; j < AHC_NUM_TARGETS; j++) {
  755. aic7xxx_tag_info[i].tag_commands[j] = tags;
  756. }
  757. }
  758. }
  759. static void
  760. ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
  761. {
  762. if ((instance >= 0) && (targ >= 0)
  763. && (instance < ARRAY_SIZE(aic7xxx_tag_info))
  764. && (targ < AHC_NUM_TARGETS)) {
  765. aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
  766. if (bootverbose)
  767. printf("tag_info[%d:%d] = %d\n", instance, targ, value);
  768. }
  769. }
  770. static char *
  771. ahc_parse_brace_option(char *opt_name, char *opt_arg, char *end, int depth,
  772. void (*callback)(u_long, int, int, int32_t),
  773. u_long callback_arg)
  774. {
  775. char *tok_end;
  776. char *tok_end2;
  777. int i;
  778. int instance;
  779. int targ;
  780. int done;
  781. char tok_list[] = {'.', ',', '{', '}', '\0'};
  782. /* All options use a ':' name/arg separator */
  783. if (*opt_arg != ':')
  784. return (opt_arg);
  785. opt_arg++;
  786. instance = -1;
  787. targ = -1;
  788. done = FALSE;
  789. /*
  790. * Restore separator that may be in
  791. * the middle of our option argument.
  792. */
  793. tok_end = strchr(opt_arg, '\0');
  794. if (tok_end < end)
  795. *tok_end = ',';
  796. while (!done) {
  797. switch (*opt_arg) {
  798. case '{':
  799. if (instance == -1) {
  800. instance = 0;
  801. } else {
  802. if (depth > 1) {
  803. if (targ == -1)
  804. targ = 0;
  805. } else {
  806. printf("Malformed Option %s\n",
  807. opt_name);
  808. done = TRUE;
  809. }
  810. }
  811. opt_arg++;
  812. break;
  813. case '}':
  814. if (targ != -1)
  815. targ = -1;
  816. else if (instance != -1)
  817. instance = -1;
  818. opt_arg++;
  819. break;
  820. case ',':
  821. case '.':
  822. if (instance == -1)
  823. done = TRUE;
  824. else if (targ >= 0)
  825. targ++;
  826. else if (instance >= 0)
  827. instance++;
  828. opt_arg++;
  829. break;
  830. case '\0':
  831. done = TRUE;
  832. break;
  833. default:
  834. tok_end = end;
  835. for (i = 0; tok_list[i]; i++) {
  836. tok_end2 = strchr(opt_arg, tok_list[i]);
  837. if ((tok_end2) && (tok_end2 < tok_end))
  838. tok_end = tok_end2;
  839. }
  840. callback(callback_arg, instance, targ,
  841. simple_strtol(opt_arg, NULL, 0));
  842. opt_arg = tok_end;
  843. break;
  844. }
  845. }
  846. return (opt_arg);
  847. }
  848. /*
  849. * Handle Linux boot parameters. This routine allows for assigning a value
  850. * to a parameter with a ':' between the parameter and the value.
  851. * ie. aic7xxx=stpwlev:1,extended
  852. */
  853. static int
  854. aic7xxx_setup(char *s)
  855. {
  856. int i, n;
  857. char *p;
  858. char *end;
  859. static struct {
  860. const char *name;
  861. uint32_t *flag;
  862. } options[] = {
  863. { "extended", &aic7xxx_extended },
  864. { "no_reset", &aic7xxx_no_reset },
  865. { "verbose", &aic7xxx_verbose },
  866. { "allow_memio", &aic7xxx_allow_memio},
  867. #ifdef AHC_DEBUG
  868. { "debug", &ahc_debug },
  869. #endif
  870. { "periodic_otag", &aic7xxx_periodic_otag },
  871. { "pci_parity", &aic7xxx_pci_parity },
  872. { "seltime", &aic7xxx_seltime },
  873. { "tag_info", NULL },
  874. { "global_tag_depth", NULL },
  875. { "dv", NULL }
  876. };
  877. end = strchr(s, '\0');
  878. /*
  879. * XXX ia64 gcc isn't smart enough to know that ARRAY_SIZE
  880. * will never be 0 in this case.
  881. */
  882. n = 0;
  883. while ((p = strsep(&s, ",.")) != NULL) {
  884. if (*p == '\0')
  885. continue;
  886. for (i = 0; i < ARRAY_SIZE(options); i++) {
  887. n = strlen(options[i].name);
  888. if (strncmp(options[i].name, p, n) == 0)
  889. break;
  890. }
  891. if (i == ARRAY_SIZE(options))
  892. continue;
  893. if (strncmp(p, "global_tag_depth", n) == 0) {
  894. ahc_linux_setup_tag_info_global(p + n);
  895. } else if (strncmp(p, "tag_info", n) == 0) {
  896. s = ahc_parse_brace_option("tag_info", p + n, end,
  897. 2, ahc_linux_setup_tag_info, 0);
  898. } else if (p[n] == ':') {
  899. *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
  900. } else if (strncmp(p, "verbose", n) == 0) {
  901. *(options[i].flag) = 1;
  902. } else {
  903. *(options[i].flag) ^= 0xFFFFFFFF;
  904. }
  905. }
  906. return 1;
  907. }
  908. __setup("aic7xxx=", aic7xxx_setup);
  909. uint32_t aic7xxx_verbose;
  910. int
  911. ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *template)
  912. {
  913. char buf[80];
  914. struct Scsi_Host *host;
  915. char *new_name;
  916. u_long s;
  917. int retval;
  918. template->name = ahc->description;
  919. host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
  920. if (host == NULL)
  921. return (ENOMEM);
  922. *((struct ahc_softc **)host->hostdata) = ahc;
  923. ahc->platform_data->host = host;
  924. host->can_queue = AHC_MAX_QUEUE;
  925. host->cmd_per_lun = 2;
  926. /* XXX No way to communicate the ID for multiple channels */
  927. host->this_id = ahc->our_id;
  928. host->irq = ahc->platform_data->irq;
  929. host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
  930. host->max_lun = AHC_NUM_LUNS;
  931. host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
  932. host->sg_tablesize = AHC_NSEG;
  933. ahc_lock(ahc, &s);
  934. ahc_set_unit(ahc, ahc_linux_unit++);
  935. ahc_unlock(ahc, &s);
  936. sprintf(buf, "scsi%d", host->host_no);
  937. new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
  938. if (new_name != NULL) {
  939. strcpy(new_name, buf);
  940. ahc_set_name(ahc, new_name);
  941. }
  942. host->unique_id = ahc->unit;
  943. ahc_linux_initialize_scsi_bus(ahc);
  944. ahc_intr_enable(ahc, TRUE);
  945. host->transportt = ahc_linux_transport_template;
  946. retval = scsi_add_host(host,
  947. (ahc->dev_softc ? &ahc->dev_softc->dev : NULL));
  948. if (retval) {
  949. printk(KERN_WARNING "aic7xxx: scsi_add_host failed\n");
  950. scsi_host_put(host);
  951. return retval;
  952. }
  953. scsi_scan_host(host);
  954. return 0;
  955. }
  956. /*
  957. * Place the SCSI bus into a known state by either resetting it,
  958. * or forcing transfer negotiations on the next command to any
  959. * target.
  960. */
  961. void
  962. ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
  963. {
  964. int i;
  965. int numtarg;
  966. unsigned long s;
  967. i = 0;
  968. numtarg = 0;
  969. ahc_lock(ahc, &s);
  970. if (aic7xxx_no_reset != 0)
  971. ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
  972. if ((ahc->flags & AHC_RESET_BUS_A) != 0)
  973. ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
  974. else
  975. numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
  976. if ((ahc->features & AHC_TWIN) != 0) {
  977. if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
  978. ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
  979. } else {
  980. if (numtarg == 0)
  981. i = 8;
  982. numtarg += 8;
  983. }
  984. }
  985. /*
  986. * Force negotiation to async for all targets that
  987. * will not see an initial bus reset.
  988. */
  989. for (; i < numtarg; i++) {
  990. struct ahc_devinfo devinfo;
  991. struct ahc_initiator_tinfo *tinfo;
  992. struct ahc_tmode_tstate *tstate;
  993. u_int our_id;
  994. u_int target_id;
  995. char channel;
  996. channel = 'A';
  997. our_id = ahc->our_id;
  998. target_id = i;
  999. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  1000. channel = 'B';
  1001. our_id = ahc->our_id_b;
  1002. target_id = i % 8;
  1003. }
  1004. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  1005. target_id, &tstate);
  1006. ahc_compile_devinfo(&devinfo, our_id, target_id,
  1007. CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
  1008. ahc_update_neg_request(ahc, &devinfo, tstate,
  1009. tinfo, AHC_NEG_ALWAYS);
  1010. }
  1011. ahc_unlock(ahc, &s);
  1012. /* Give the bus some time to recover */
  1013. if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
  1014. ahc_linux_freeze_simq(ahc);
  1015. msleep(AIC7XXX_RESET_DELAY);
  1016. ahc_linux_release_simq(ahc);
  1017. }
  1018. }
  1019. int
  1020. ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
  1021. {
  1022. ahc->platform_data =
  1023. malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
  1024. if (ahc->platform_data == NULL)
  1025. return (ENOMEM);
  1026. memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
  1027. ahc->platform_data->irq = AHC_LINUX_NOIRQ;
  1028. ahc_lockinit(ahc);
  1029. ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
  1030. ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
  1031. if (aic7xxx_pci_parity == 0)
  1032. ahc->flags |= AHC_DISABLE_PCI_PERR;
  1033. return (0);
  1034. }
  1035. void
  1036. ahc_platform_free(struct ahc_softc *ahc)
  1037. {
  1038. struct scsi_target *starget;
  1039. int i;
  1040. if (ahc->platform_data != NULL) {
  1041. /* destroy all of the device and target objects */
  1042. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  1043. starget = ahc->platform_data->starget[i];
  1044. if (starget != NULL) {
  1045. ahc->platform_data->starget[i] = NULL;
  1046. }
  1047. }
  1048. if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
  1049. free_irq(ahc->platform_data->irq, ahc);
  1050. if (ahc->tag == BUS_SPACE_PIO
  1051. && ahc->bsh.ioport != 0)
  1052. release_region(ahc->bsh.ioport, 256);
  1053. if (ahc->tag == BUS_SPACE_MEMIO
  1054. && ahc->bsh.maddr != NULL) {
  1055. iounmap(ahc->bsh.maddr);
  1056. release_mem_region(ahc->platform_data->mem_busaddr,
  1057. 0x1000);
  1058. }
  1059. if (ahc->platform_data->host)
  1060. scsi_host_put(ahc->platform_data->host);
  1061. free(ahc->platform_data, M_DEVBUF);
  1062. }
  1063. }
  1064. void
  1065. ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  1066. {
  1067. ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
  1068. SCB_GET_CHANNEL(ahc, scb),
  1069. SCB_GET_LUN(scb), SCB_LIST_NULL,
  1070. ROLE_UNKNOWN, CAM_REQUEUE_REQ);
  1071. }
  1072. void
  1073. ahc_platform_set_tags(struct ahc_softc *ahc, struct scsi_device *sdev,
  1074. struct ahc_devinfo *devinfo, ahc_queue_alg alg)
  1075. {
  1076. struct ahc_linux_device *dev;
  1077. int was_queuing;
  1078. int now_queuing;
  1079. if (sdev == NULL)
  1080. return;
  1081. dev = scsi_transport_device_data(sdev);
  1082. was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
  1083. switch (alg) {
  1084. default:
  1085. case AHC_QUEUE_NONE:
  1086. now_queuing = 0;
  1087. break;
  1088. case AHC_QUEUE_BASIC:
  1089. now_queuing = AHC_DEV_Q_BASIC;
  1090. break;
  1091. case AHC_QUEUE_TAGGED:
  1092. now_queuing = AHC_DEV_Q_TAGGED;
  1093. break;
  1094. }
  1095. if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
  1096. && (was_queuing != now_queuing)
  1097. && (dev->active != 0)) {
  1098. dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
  1099. dev->qfrozen++;
  1100. }
  1101. dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
  1102. if (now_queuing) {
  1103. u_int usertags;
  1104. usertags = ahc_linux_user_tagdepth(ahc, devinfo);
  1105. if (!was_queuing) {
  1106. /*
  1107. * Start out agressively and allow our
  1108. * dynamic queue depth algorithm to take
  1109. * care of the rest.
  1110. */
  1111. dev->maxtags = usertags;
  1112. dev->openings = dev->maxtags - dev->active;
  1113. }
  1114. if (dev->maxtags == 0) {
  1115. /*
  1116. * Queueing is disabled by the user.
  1117. */
  1118. dev->openings = 1;
  1119. } else if (alg == AHC_QUEUE_TAGGED) {
  1120. dev->flags |= AHC_DEV_Q_TAGGED;
  1121. if (aic7xxx_periodic_otag != 0)
  1122. dev->flags |= AHC_DEV_PERIODIC_OTAG;
  1123. } else
  1124. dev->flags |= AHC_DEV_Q_BASIC;
  1125. } else {
  1126. /* We can only have one opening. */
  1127. dev->maxtags = 0;
  1128. dev->openings = 1 - dev->active;
  1129. }
  1130. switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
  1131. case AHC_DEV_Q_BASIC:
  1132. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  1133. scsi_activate_tcq(sdev, dev->openings + dev->active);
  1134. break;
  1135. case AHC_DEV_Q_TAGGED:
  1136. scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
  1137. scsi_activate_tcq(sdev, dev->openings + dev->active);
  1138. break;
  1139. default:
  1140. /*
  1141. * We allow the OS to queue 2 untagged transactions to
  1142. * us at any time even though we can only execute them
  1143. * serially on the controller/device. This should
  1144. * remove some latency.
  1145. */
  1146. scsi_deactivate_tcq(sdev, 2);
  1147. break;
  1148. }
  1149. }
  1150. int
  1151. ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  1152. int lun, u_int tag, role_t role, uint32_t status)
  1153. {
  1154. return 0;
  1155. }
  1156. static u_int
  1157. ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  1158. {
  1159. static int warned_user;
  1160. u_int tags;
  1161. tags = 0;
  1162. if ((ahc->user_discenable & devinfo->target_mask) != 0) {
  1163. if (ahc->unit >= ARRAY_SIZE(aic7xxx_tag_info)) {
  1164. if (warned_user == 0) {
  1165. printf(KERN_WARNING
  1166. "aic7xxx: WARNING: Insufficient tag_info instances\n"
  1167. "aic7xxx: for installed controllers. Using defaults\n"
  1168. "aic7xxx: Please update the aic7xxx_tag_info array in\n"
  1169. "aic7xxx: the aic7xxx_osm..c source file.\n");
  1170. warned_user++;
  1171. }
  1172. tags = AHC_MAX_QUEUE;
  1173. } else {
  1174. adapter_tag_info_t *tag_info;
  1175. tag_info = &aic7xxx_tag_info[ahc->unit];
  1176. tags = tag_info->tag_commands[devinfo->target_offset];
  1177. if (tags > AHC_MAX_QUEUE)
  1178. tags = AHC_MAX_QUEUE;
  1179. }
  1180. }
  1181. return (tags);
  1182. }
  1183. /*
  1184. * Determines the queue depth for a given device.
  1185. */
  1186. static void
  1187. ahc_linux_device_queue_depth(struct scsi_device *sdev)
  1188. {
  1189. struct ahc_devinfo devinfo;
  1190. u_int tags;
  1191. struct ahc_softc *ahc = *((struct ahc_softc **)sdev->host->hostdata);
  1192. ahc_compile_devinfo(&devinfo,
  1193. sdev->sdev_target->channel == 0
  1194. ? ahc->our_id : ahc->our_id_b,
  1195. sdev->sdev_target->id, sdev->lun,
  1196. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1197. ROLE_INITIATOR);
  1198. tags = ahc_linux_user_tagdepth(ahc, &devinfo);
  1199. if (tags != 0 && sdev->tagged_supported != 0) {
  1200. ahc_platform_set_tags(ahc, sdev, &devinfo, AHC_QUEUE_TAGGED);
  1201. ahc_send_async(ahc, devinfo.channel, devinfo.target,
  1202. devinfo.lun, AC_TRANSFER_NEG);
  1203. ahc_print_devinfo(ahc, &devinfo);
  1204. printf("Tagged Queuing enabled. Depth %d\n", tags);
  1205. } else {
  1206. ahc_platform_set_tags(ahc, sdev, &devinfo, AHC_QUEUE_NONE);
  1207. ahc_send_async(ahc, devinfo.channel, devinfo.target,
  1208. devinfo.lun, AC_TRANSFER_NEG);
  1209. }
  1210. }
  1211. static int
  1212. ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
  1213. struct scsi_cmnd *cmd)
  1214. {
  1215. struct scb *scb;
  1216. struct hardware_scb *hscb;
  1217. struct ahc_initiator_tinfo *tinfo;
  1218. struct ahc_tmode_tstate *tstate;
  1219. uint16_t mask;
  1220. struct scb_tailq *untagged_q = NULL;
  1221. int nseg;
  1222. /*
  1223. * Schedule us to run later. The only reason we are not
  1224. * running is because the whole controller Q is frozen.
  1225. */
  1226. if (ahc->platform_data->qfrozen != 0)
  1227. return SCSI_MLQUEUE_HOST_BUSY;
  1228. /*
  1229. * We only allow one untagged transaction
  1230. * per target in the initiator role unless
  1231. * we are storing a full busy target *lun*
  1232. * table in SCB space.
  1233. */
  1234. if (!blk_rq_tagged(cmd->request)
  1235. && (ahc->features & AHC_SCB_BTT) == 0) {
  1236. int target_offset;
  1237. target_offset = cmd->device->id + cmd->device->channel * 8;
  1238. untagged_q = &(ahc->untagged_queues[target_offset]);
  1239. if (!TAILQ_EMPTY(untagged_q))
  1240. /* if we're already executing an untagged command
  1241. * we're busy to another */
  1242. return SCSI_MLQUEUE_DEVICE_BUSY;
  1243. }
  1244. /*
  1245. * Get an scb to use.
  1246. */
  1247. scb = ahc_get_scb(ahc);
  1248. if (!scb)
  1249. return SCSI_MLQUEUE_HOST_BUSY;
  1250. scb->io_ctx = cmd;
  1251. scb->platform_data->dev = dev;
  1252. hscb = scb->hscb;
  1253. cmd->host_scribble = (char *)scb;
  1254. /*
  1255. * Fill out basics of the HSCB.
  1256. */
  1257. hscb->control = 0;
  1258. hscb->scsiid = BUILD_SCSIID(ahc, cmd);
  1259. hscb->lun = cmd->device->lun;
  1260. mask = SCB_GET_TARGET_MASK(ahc, scb);
  1261. tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
  1262. SCB_GET_OUR_ID(scb),
  1263. SCB_GET_TARGET(ahc, scb), &tstate);
  1264. hscb->scsirate = tinfo->scsirate;
  1265. hscb->scsioffset = tinfo->curr.offset;
  1266. if ((tstate->ultraenb & mask) != 0)
  1267. hscb->control |= ULTRAENB;
  1268. if ((ahc->user_discenable & mask) != 0)
  1269. hscb->control |= DISCENB;
  1270. if ((tstate->auto_negotiate & mask) != 0) {
  1271. scb->flags |= SCB_AUTO_NEGOTIATE;
  1272. scb->hscb->control |= MK_MESSAGE;
  1273. }
  1274. if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
  1275. int msg_bytes;
  1276. uint8_t tag_msgs[2];
  1277. msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
  1278. if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
  1279. hscb->control |= tag_msgs[0];
  1280. if (tag_msgs[0] == MSG_ORDERED_TASK)
  1281. dev->commands_since_idle_or_otag = 0;
  1282. } else if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
  1283. && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
  1284. hscb->control |= MSG_ORDERED_TASK;
  1285. dev->commands_since_idle_or_otag = 0;
  1286. } else {
  1287. hscb->control |= MSG_SIMPLE_TASK;
  1288. }
  1289. }
  1290. hscb->cdb_len = cmd->cmd_len;
  1291. if (hscb->cdb_len <= 12) {
  1292. memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
  1293. } else {
  1294. memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
  1295. scb->flags |= SCB_CDB32_PTR;
  1296. }
  1297. scb->platform_data->xfer_len = 0;
  1298. ahc_set_residual(scb, 0);
  1299. ahc_set_sense_residual(scb, 0);
  1300. scb->sg_count = 0;
  1301. nseg = scsi_dma_map(cmd);
  1302. BUG_ON(nseg < 0);
  1303. if (nseg > 0) {
  1304. struct ahc_dma_seg *sg;
  1305. struct scatterlist *cur_seg;
  1306. int i;
  1307. /* Copy the segments into the SG list. */
  1308. sg = scb->sg_list;
  1309. /*
  1310. * The sg_count may be larger than nseg if
  1311. * a transfer crosses a 32bit page.
  1312. */
  1313. scsi_for_each_sg(cmd, cur_seg, nseg, i) {
  1314. dma_addr_t addr;
  1315. bus_size_t len;
  1316. int consumed;
  1317. addr = sg_dma_address(cur_seg);
  1318. len = sg_dma_len(cur_seg);
  1319. consumed = ahc_linux_map_seg(ahc, scb,
  1320. sg, addr, len);
  1321. sg += consumed;
  1322. scb->sg_count += consumed;
  1323. }
  1324. sg--;
  1325. sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
  1326. /*
  1327. * Reset the sg list pointer.
  1328. */
  1329. scb->hscb->sgptr =
  1330. ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
  1331. /*
  1332. * Copy the first SG into the "current"
  1333. * data pointer area.
  1334. */
  1335. scb->hscb->dataptr = scb->sg_list->addr;
  1336. scb->hscb->datacnt = scb->sg_list->len;
  1337. } else {
  1338. scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
  1339. scb->hscb->dataptr = 0;
  1340. scb->hscb->datacnt = 0;
  1341. scb->sg_count = 0;
  1342. }
  1343. LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
  1344. dev->openings--;
  1345. dev->active++;
  1346. dev->commands_issued++;
  1347. if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
  1348. dev->commands_since_idle_or_otag++;
  1349. scb->flags |= SCB_ACTIVE;
  1350. if (untagged_q) {
  1351. TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
  1352. scb->flags |= SCB_UNTAGGEDQ;
  1353. }
  1354. ahc_queue_scb(ahc, scb);
  1355. return 0;
  1356. }
  1357. /*
  1358. * SCSI controller interrupt handler.
  1359. */
  1360. irqreturn_t
  1361. ahc_linux_isr(int irq, void *dev_id)
  1362. {
  1363. struct ahc_softc *ahc;
  1364. u_long flags;
  1365. int ours;
  1366. ahc = (struct ahc_softc *) dev_id;
  1367. ahc_lock(ahc, &flags);
  1368. ours = ahc_intr(ahc);
  1369. ahc_unlock(ahc, &flags);
  1370. return IRQ_RETVAL(ours);
  1371. }
  1372. void
  1373. ahc_platform_flushwork(struct ahc_softc *ahc)
  1374. {
  1375. }
  1376. void
  1377. ahc_send_async(struct ahc_softc *ahc, char channel,
  1378. u_int target, u_int lun, ac_code code)
  1379. {
  1380. switch (code) {
  1381. case AC_TRANSFER_NEG:
  1382. {
  1383. char buf[80];
  1384. struct scsi_target *starget;
  1385. struct ahc_linux_target *targ;
  1386. struct info_str info;
  1387. struct ahc_initiator_tinfo *tinfo;
  1388. struct ahc_tmode_tstate *tstate;
  1389. int target_offset;
  1390. unsigned int target_ppr_options;
  1391. BUG_ON(target == CAM_TARGET_WILDCARD);
  1392. info.buffer = buf;
  1393. info.length = sizeof(buf);
  1394. info.offset = 0;
  1395. info.pos = 0;
  1396. tinfo = ahc_fetch_transinfo(ahc, channel,
  1397. channel == 'A' ? ahc->our_id
  1398. : ahc->our_id_b,
  1399. target, &tstate);
  1400. /*
  1401. * Don't bother reporting results while
  1402. * negotiations are still pending.
  1403. */
  1404. if (tinfo->curr.period != tinfo->goal.period
  1405. || tinfo->curr.width != tinfo->goal.width
  1406. || tinfo->curr.offset != tinfo->goal.offset
  1407. || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
  1408. if (bootverbose == 0)
  1409. break;
  1410. /*
  1411. * Don't bother reporting results that
  1412. * are identical to those last reported.
  1413. */
  1414. target_offset = target;
  1415. if (channel == 'B')
  1416. target_offset += 8;
  1417. starget = ahc->platform_data->starget[target_offset];
  1418. if (starget == NULL)
  1419. break;
  1420. targ = scsi_transport_target_data(starget);
  1421. target_ppr_options =
  1422. (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
  1423. + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
  1424. + (spi_iu(starget) ? MSG_EXT_PPR_IU_REQ : 0);
  1425. if (tinfo->curr.period == spi_period(starget)
  1426. && tinfo->curr.width == spi_width(starget)
  1427. && tinfo->curr.offset == spi_offset(starget)
  1428. && tinfo->curr.ppr_options == target_ppr_options)
  1429. if (bootverbose == 0)
  1430. break;
  1431. spi_period(starget) = tinfo->curr.period;
  1432. spi_width(starget) = tinfo->curr.width;
  1433. spi_offset(starget) = tinfo->curr.offset;
  1434. spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
  1435. spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
  1436. spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
  1437. spi_display_xfer_agreement(starget);
  1438. break;
  1439. }
  1440. case AC_SENT_BDR:
  1441. {
  1442. WARN_ON(lun != CAM_LUN_WILDCARD);
  1443. scsi_report_device_reset(ahc->platform_data->host,
  1444. channel - 'A', target);
  1445. break;
  1446. }
  1447. case AC_BUS_RESET:
  1448. if (ahc->platform_data->host != NULL) {
  1449. scsi_report_bus_reset(ahc->platform_data->host,
  1450. channel - 'A');
  1451. }
  1452. break;
  1453. default:
  1454. panic("ahc_send_async: Unexpected async event");
  1455. }
  1456. }
  1457. /*
  1458. * Calls the higher level scsi done function and frees the scb.
  1459. */
  1460. void
  1461. ahc_done(struct ahc_softc *ahc, struct scb *scb)
  1462. {
  1463. struct scsi_cmnd *cmd;
  1464. struct ahc_linux_device *dev;
  1465. LIST_REMOVE(scb, pending_links);
  1466. if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
  1467. struct scb_tailq *untagged_q;
  1468. int target_offset;
  1469. target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
  1470. untagged_q = &(ahc->untagged_queues[target_offset]);
  1471. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  1472. BUG_ON(!TAILQ_EMPTY(untagged_q));
  1473. }
  1474. if ((scb->flags & SCB_ACTIVE) == 0) {
  1475. printf("SCB %d done'd twice\n", scb->hscb->tag);
  1476. ahc_dump_card_state(ahc);
  1477. panic("Stopping for safety");
  1478. }
  1479. cmd = scb->io_ctx;
  1480. dev = scb->platform_data->dev;
  1481. dev->active--;
  1482. dev->openings++;
  1483. if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
  1484. cmd->result &= ~(CAM_DEV_QFRZN << 16);
  1485. dev->qfrozen--;
  1486. }
  1487. ahc_linux_unmap_scb(ahc, scb);
  1488. /*
  1489. * Guard against stale sense data.
  1490. * The Linux mid-layer assumes that sense
  1491. * was retrieved anytime the first byte of
  1492. * the sense buffer looks "sane".
  1493. */
  1494. cmd->sense_buffer[0] = 0;
  1495. if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
  1496. uint32_t amount_xferred;
  1497. amount_xferred =
  1498. ahc_get_transfer_length(scb) - ahc_get_residual(scb);
  1499. if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
  1500. #ifdef AHC_DEBUG
  1501. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  1502. ahc_print_path(ahc, scb);
  1503. printf("Set CAM_UNCOR_PARITY\n");
  1504. }
  1505. #endif
  1506. ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
  1507. #ifdef AHC_REPORT_UNDERFLOWS
  1508. /*
  1509. * This code is disabled by default as some
  1510. * clients of the SCSI system do not properly
  1511. * initialize the underflow parameter. This
  1512. * results in spurious termination of commands
  1513. * that complete as expected (e.g. underflow is
  1514. * allowed as command can return variable amounts
  1515. * of data.
  1516. */
  1517. } else if (amount_xferred < scb->io_ctx->underflow) {
  1518. u_int i;
  1519. ahc_print_path(ahc, scb);
  1520. printf("CDB:");
  1521. for (i = 0; i < scb->io_ctx->cmd_len; i++)
  1522. printf(" 0x%x", scb->io_ctx->cmnd[i]);
  1523. printf("\n");
  1524. ahc_print_path(ahc, scb);
  1525. printf("Saw underflow (%ld of %ld bytes). "
  1526. "Treated as error\n",
  1527. ahc_get_residual(scb),
  1528. ahc_get_transfer_length(scb));
  1529. ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1530. #endif
  1531. } else {
  1532. ahc_set_transaction_status(scb, CAM_REQ_CMP);
  1533. }
  1534. } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
  1535. ahc_linux_handle_scsi_status(ahc, cmd->device, scb);
  1536. }
  1537. if (dev->openings == 1
  1538. && ahc_get_transaction_status(scb) == CAM_REQ_CMP
  1539. && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
  1540. dev->tag_success_count++;
  1541. /*
  1542. * Some devices deal with temporary internal resource
  1543. * shortages by returning queue full. When the queue
  1544. * full occurrs, we throttle back. Slowly try to get
  1545. * back to our previous queue depth.
  1546. */
  1547. if ((dev->openings + dev->active) < dev->maxtags
  1548. && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
  1549. dev->tag_success_count = 0;
  1550. dev->openings++;
  1551. }
  1552. if (dev->active == 0)
  1553. dev->commands_since_idle_or_otag = 0;
  1554. if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
  1555. printf("Recovery SCB completes\n");
  1556. if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
  1557. || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
  1558. ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
  1559. if (ahc->platform_data->eh_done)
  1560. complete(ahc->platform_data->eh_done);
  1561. }
  1562. ahc_free_scb(ahc, scb);
  1563. ahc_linux_queue_cmd_complete(ahc, cmd);
  1564. }
  1565. static void
  1566. ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
  1567. struct scsi_device *sdev, struct scb *scb)
  1568. {
  1569. struct ahc_devinfo devinfo;
  1570. struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
  1571. ahc_compile_devinfo(&devinfo,
  1572. ahc->our_id,
  1573. sdev->sdev_target->id, sdev->lun,
  1574. sdev->sdev_target->channel == 0 ? 'A' : 'B',
  1575. ROLE_INITIATOR);
  1576. /*
  1577. * We don't currently trust the mid-layer to
  1578. * properly deal with queue full or busy. So,
  1579. * when one occurs, we tell the mid-layer to
  1580. * unconditionally requeue the command to us
  1581. * so that we can retry it ourselves. We also
  1582. * implement our own throttling mechanism so
  1583. * we don't clobber the device with too many
  1584. * commands.
  1585. */
  1586. switch (ahc_get_scsi_status(scb)) {
  1587. default:
  1588. break;
  1589. case SCSI_STATUS_CHECK_COND:
  1590. case SCSI_STATUS_CMD_TERMINATED:
  1591. {
  1592. struct scsi_cmnd *cmd;
  1593. /*
  1594. * Copy sense information to the OS's cmd
  1595. * structure if it is available.
  1596. */
  1597. cmd = scb->io_ctx;
  1598. if (scb->flags & SCB_SENSE) {
  1599. u_int sense_size;
  1600. sense_size = min(sizeof(struct scsi_sense_data)
  1601. - ahc_get_sense_residual(scb),
  1602. (u_long)sizeof(cmd->sense_buffer));
  1603. memcpy(cmd->sense_buffer,
  1604. ahc_get_sense_buf(ahc, scb), sense_size);
  1605. if (sense_size < sizeof(cmd->sense_buffer))
  1606. memset(&cmd->sense_buffer[sense_size], 0,
  1607. sizeof(cmd->sense_buffer) - sense_size);
  1608. cmd->result |= (DRIVER_SENSE << 24);
  1609. #ifdef AHC_DEBUG
  1610. if (ahc_debug & AHC_SHOW_SENSE) {
  1611. int i;
  1612. printf("Copied %d bytes of sense data:",
  1613. sense_size);
  1614. for (i = 0; i < sense_size; i++) {
  1615. if ((i & 0xF) == 0)
  1616. printf("\n");
  1617. printf("0x%x ", cmd->sense_buffer[i]);
  1618. }
  1619. printf("\n");
  1620. }
  1621. #endif
  1622. }
  1623. break;
  1624. }
  1625. case SCSI_STATUS_QUEUE_FULL:
  1626. {
  1627. /*
  1628. * By the time the core driver has returned this
  1629. * command, all other commands that were queued
  1630. * to us but not the device have been returned.
  1631. * This ensures that dev->active is equal to
  1632. * the number of commands actually queued to
  1633. * the device.
  1634. */
  1635. dev->tag_success_count = 0;
  1636. if (dev->active != 0) {
  1637. /*
  1638. * Drop our opening count to the number
  1639. * of commands currently outstanding.
  1640. */
  1641. dev->openings = 0;
  1642. /*
  1643. ahc_print_path(ahc, scb);
  1644. printf("Dropping tag count to %d\n", dev->active);
  1645. */
  1646. if (dev->active == dev->tags_on_last_queuefull) {
  1647. dev->last_queuefull_same_count++;
  1648. /*
  1649. * If we repeatedly see a queue full
  1650. * at the same queue depth, this
  1651. * device has a fixed number of tag
  1652. * slots. Lock in this tag depth
  1653. * so we stop seeing queue fulls from
  1654. * this device.
  1655. */
  1656. if (dev->last_queuefull_same_count
  1657. == AHC_LOCK_TAGS_COUNT) {
  1658. dev->maxtags = dev->active;
  1659. ahc_print_path(ahc, scb);
  1660. printf("Locking max tag count at %d\n",
  1661. dev->active);
  1662. }
  1663. } else {
  1664. dev->tags_on_last_queuefull = dev->active;
  1665. dev->last_queuefull_same_count = 0;
  1666. }
  1667. ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
  1668. ahc_set_scsi_status(scb, SCSI_STATUS_OK);
  1669. ahc_platform_set_tags(ahc, sdev, &devinfo,
  1670. (dev->flags & AHC_DEV_Q_BASIC)
  1671. ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
  1672. break;
  1673. }
  1674. /*
  1675. * Drop down to a single opening, and treat this
  1676. * as if the target returned BUSY SCSI status.
  1677. */
  1678. dev->openings = 1;
  1679. ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
  1680. ahc_platform_set_tags(ahc, sdev, &devinfo,
  1681. (dev->flags & AHC_DEV_Q_BASIC)
  1682. ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
  1683. break;
  1684. }
  1685. }
  1686. }
  1687. static void
  1688. ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, struct scsi_cmnd *cmd)
  1689. {
  1690. /*
  1691. * Map CAM error codes into Linux Error codes. We
  1692. * avoid the conversion so that the DV code has the
  1693. * full error information available when making
  1694. * state change decisions.
  1695. */
  1696. {
  1697. u_int new_status;
  1698. switch (ahc_cmd_get_transaction_status(cmd)) {
  1699. case CAM_REQ_INPROG:
  1700. case CAM_REQ_CMP:
  1701. case CAM_SCSI_STATUS_ERROR:
  1702. new_status = DID_OK;
  1703. break;
  1704. case CAM_REQ_ABORTED:
  1705. new_status = DID_ABORT;
  1706. break;
  1707. case CAM_BUSY:
  1708. new_status = DID_BUS_BUSY;
  1709. break;
  1710. case CAM_REQ_INVALID:
  1711. case CAM_PATH_INVALID:
  1712. new_status = DID_BAD_TARGET;
  1713. break;
  1714. case CAM_SEL_TIMEOUT:
  1715. new_status = DID_NO_CONNECT;
  1716. break;
  1717. case CAM_SCSI_BUS_RESET:
  1718. case CAM_BDR_SENT:
  1719. new_status = DID_RESET;
  1720. break;
  1721. case CAM_UNCOR_PARITY:
  1722. new_status = DID_PARITY;
  1723. break;
  1724. case CAM_CMD_TIMEOUT:
  1725. new_status = DID_TIME_OUT;
  1726. break;
  1727. case CAM_UA_ABORT:
  1728. case CAM_REQ_CMP_ERR:
  1729. case CAM_AUTOSENSE_FAIL:
  1730. case CAM_NO_HBA:
  1731. case CAM_DATA_RUN_ERR:
  1732. case CAM_UNEXP_BUSFREE:
  1733. case CAM_SEQUENCE_FAIL:
  1734. case CAM_CCB_LEN_ERR:
  1735. case CAM_PROVIDE_FAIL:
  1736. case CAM_REQ_TERMIO:
  1737. case CAM_UNREC_HBA_ERROR:
  1738. case CAM_REQ_TOO_BIG:
  1739. new_status = DID_ERROR;
  1740. break;
  1741. case CAM_REQUEUE_REQ:
  1742. new_status = DID_REQUEUE;
  1743. break;
  1744. default:
  1745. /* We should never get here */
  1746. new_status = DID_ERROR;
  1747. break;
  1748. }
  1749. ahc_cmd_set_transaction_status(cmd, new_status);
  1750. }
  1751. cmd->scsi_done(cmd);
  1752. }
  1753. static void
  1754. ahc_linux_freeze_simq(struct ahc_softc *ahc)
  1755. {
  1756. unsigned long s;
  1757. ahc_lock(ahc, &s);
  1758. ahc->platform_data->qfrozen++;
  1759. if (ahc->platform_data->qfrozen == 1) {
  1760. scsi_block_requests(ahc->platform_data->host);
  1761. /* XXX What about Twin channels? */
  1762. ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  1763. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  1764. ROLE_INITIATOR, CAM_REQUEUE_REQ);
  1765. }
  1766. ahc_unlock(ahc, &s);
  1767. }
  1768. static void
  1769. ahc_linux_release_simq(struct ahc_softc *ahc)
  1770. {
  1771. u_long s;
  1772. int unblock_reqs;
  1773. unblock_reqs = 0;
  1774. ahc_lock(ahc, &s);
  1775. if (ahc->platform_data->qfrozen > 0)
  1776. ahc->platform_data->qfrozen--;
  1777. if (ahc->platform_data->qfrozen == 0)
  1778. unblock_reqs = 1;
  1779. ahc_unlock(ahc, &s);
  1780. /*
  1781. * There is still a race here. The mid-layer
  1782. * should keep its own freeze count and use
  1783. * a bottom half handler to run the queues
  1784. * so we can unblock with our own lock held.
  1785. */
  1786. if (unblock_reqs)
  1787. scsi_unblock_requests(ahc->platform_data->host);
  1788. }
  1789. static int
  1790. ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
  1791. {
  1792. struct ahc_softc *ahc;
  1793. struct ahc_linux_device *dev;
  1794. struct scb *pending_scb;
  1795. u_int saved_scbptr;
  1796. u_int active_scb_index;
  1797. u_int last_phase;
  1798. u_int saved_scsiid;
  1799. u_int cdb_byte;
  1800. int retval;
  1801. int was_paused;
  1802. int paused;
  1803. int wait;
  1804. int disconnected;
  1805. unsigned long flags;
  1806. pending_scb = NULL;
  1807. paused = FALSE;
  1808. wait = FALSE;
  1809. ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
  1810. scmd_printk(KERN_INFO, cmd, "Attempting to queue a%s message\n",
  1811. flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
  1812. printf("CDB:");
  1813. for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
  1814. printf(" 0x%x", cmd->cmnd[cdb_byte]);
  1815. printf("\n");
  1816. ahc_lock(ahc, &flags);
  1817. /*
  1818. * First determine if we currently own this command.
  1819. * Start by searching the device queue. If not found
  1820. * there, check the pending_scb list. If not found
  1821. * at all, and the system wanted us to just abort the
  1822. * command, return success.
  1823. */
  1824. dev = scsi_transport_device_data(cmd->device);
  1825. if (dev == NULL) {
  1826. /*
  1827. * No target device for this command exists,
  1828. * so we must not still own the command.
  1829. */
  1830. printf("%s:%d:%d:%d: Is not an active device\n",
  1831. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1832. cmd->device->lun);
  1833. retval = SUCCESS;
  1834. goto no_cmd;
  1835. }
  1836. if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
  1837. && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
  1838. cmd->device->channel + 'A',
  1839. cmd->device->lun,
  1840. CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
  1841. printf("%s:%d:%d:%d: Command found on untagged queue\n",
  1842. ahc_name(ahc), cmd->device->channel, cmd->device->id,
  1843. cmd->device->lun);
  1844. retval = SUCCESS;
  1845. goto done;
  1846. }
  1847. /*
  1848. * See if we can find a matching cmd in the pending list.
  1849. */
  1850. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1851. if (pending_scb->io_ctx == cmd)
  1852. break;
  1853. }
  1854. if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
  1855. /* Any SCB for this device will do for a target reset */
  1856. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1857. if (ahc_match_scb(ahc, pending_scb, scmd_id(cmd),
  1858. scmd_channel(cmd) + 'A',
  1859. CAM_LUN_WILDCARD,
  1860. SCB_LIST_NULL, ROLE_INITIATOR))
  1861. break;
  1862. }
  1863. }
  1864. if (pending_scb == NULL) {
  1865. scmd_printk(KERN_INFO, cmd, "Command not found\n");
  1866. goto no_cmd;
  1867. }
  1868. if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
  1869. /*
  1870. * We can't queue two recovery actions using the same SCB
  1871. */
  1872. retval = FAILED;
  1873. goto done;
  1874. }
  1875. /*
  1876. * Ensure that the card doesn't do anything
  1877. * behind our back and that we didn't "just" miss
  1878. * an interrupt that would affect this cmd.
  1879. */
  1880. was_paused = ahc_is_paused(ahc);
  1881. ahc_pause_and_flushwork(ahc);
  1882. paused = TRUE;
  1883. if ((pending_scb->flags & SCB_ACTIVE) == 0) {
  1884. scmd_printk(KERN_INFO, cmd, "Command already completed\n");
  1885. goto no_cmd;
  1886. }
  1887. printf("%s: At time of recovery, card was %spaused\n",
  1888. ahc_name(ahc), was_paused ? "" : "not ");
  1889. ahc_dump_card_state(ahc);
  1890. disconnected = TRUE;
  1891. if (flag == SCB_ABORT) {
  1892. if (ahc_search_qinfifo(ahc, cmd->device->id,
  1893. cmd->device->channel + 'A',
  1894. cmd->device->lun,
  1895. pending_scb->hscb->tag,
  1896. ROLE_INITIATOR, CAM_REQ_ABORTED,
  1897. SEARCH_COMPLETE) > 0) {
  1898. printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
  1899. ahc_name(ahc), cmd->device->channel,
  1900. cmd->device->id, cmd->device->lun);
  1901. retval = SUCCESS;
  1902. goto done;
  1903. }
  1904. } else if (ahc_search_qinfifo(ahc, cmd->device->id,
  1905. cmd->device->channel + 'A',
  1906. cmd->device->lun, pending_scb->hscb->tag,
  1907. ROLE_INITIATOR, /*status*/0,
  1908. SEARCH_COUNT) > 0) {
  1909. disconnected = FALSE;
  1910. }
  1911. if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
  1912. struct scb *bus_scb;
  1913. bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
  1914. if (bus_scb == pending_scb)
  1915. disconnected = FALSE;
  1916. else if (flag != SCB_ABORT
  1917. && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
  1918. && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
  1919. disconnected = FALSE;
  1920. }
  1921. /*
  1922. * At this point, pending_scb is the scb associated with the
  1923. * passed in command. That command is currently active on the
  1924. * bus, is in the disconnected state, or we're hoping to find
  1925. * a command for the same target active on the bus to abuse to
  1926. * send a BDR. Queue the appropriate message based on which of
  1927. * these states we are in.
  1928. */
  1929. last_phase = ahc_inb(ahc, LASTPHASE);
  1930. saved_scbptr = ahc_inb(ahc, SCBPTR);
  1931. active_scb_index = ahc_inb(ahc, SCB_TAG);
  1932. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  1933. if (last_phase != P_BUSFREE
  1934. && (pending_scb->hscb->tag == active_scb_index
  1935. || (flag == SCB_DEVICE_RESET
  1936. && SCSIID_TARGET(ahc, saved_scsiid) == scmd_id(cmd)))) {
  1937. /*
  1938. * We're active on the bus, so assert ATN
  1939. * and hope that the target responds.
  1940. */
  1941. pending_scb = ahc_lookup_scb(ahc, active_scb_index);
  1942. pending_scb->flags |= SCB_RECOVERY_SCB|flag;
  1943. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  1944. ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
  1945. scmd_printk(KERN_INFO, cmd, "Device is active, asserting ATN\n");
  1946. wait = TRUE;
  1947. } else if (disconnected) {
  1948. /*
  1949. * Actually re-queue this SCB in an attempt
  1950. * to select the device before it reconnects.
  1951. * In either case (selection or reselection),
  1952. * we will now issue the approprate message
  1953. * to the timed-out device.
  1954. *
  1955. * Set the MK_MESSAGE control bit indicating
  1956. * that we desire to send a message. We
  1957. * also set the disconnected flag since
  1958. * in the paging case there is no guarantee
  1959. * that our SCB control byte matches the
  1960. * version on the card. We don't want the
  1961. * sequencer to abort the command thinking
  1962. * an unsolicited reselection occurred.
  1963. */
  1964. pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
  1965. pending_scb->flags |= SCB_RECOVERY_SCB|flag;
  1966. /*
  1967. * Remove any cached copy of this SCB in the
  1968. * disconnected list in preparation for the
  1969. * queuing of our abort SCB. We use the
  1970. * same element in the SCB, SCB_NEXT, for
  1971. * both the qinfifo and the disconnected list.
  1972. */
  1973. ahc_search_disc_list(ahc, cmd->device->id,
  1974. cmd->device->channel + 'A',
  1975. cmd->device->lun, pending_scb->hscb->tag,
  1976. /*stop_on_first*/TRUE,
  1977. /*remove*/TRUE,
  1978. /*save_state*/FALSE);
  1979. /*
  1980. * In the non-paging case, the sequencer will
  1981. * never re-reference the in-core SCB.
  1982. * To make sure we are notified during
  1983. * reslection, set the MK_MESSAGE flag in
  1984. * the card's copy of the SCB.
  1985. */
  1986. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  1987. ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
  1988. ahc_outb(ahc, SCB_CONTROL,
  1989. ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
  1990. }
  1991. /*
  1992. * Clear out any entries in the QINFIFO first
  1993. * so we are the next SCB for this target
  1994. * to run.
  1995. */
  1996. ahc_search_qinfifo(ahc, cmd->device->id,
  1997. cmd->device->channel + 'A',
  1998. cmd->device->lun, SCB_LIST_NULL,
  1999. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  2000. SEARCH_COMPLETE);
  2001. ahc_qinfifo_requeue_tail(ahc, pending_scb);
  2002. ahc_outb(ahc, SCBPTR, saved_scbptr);
  2003. ahc_print_path(ahc, pending_scb);
  2004. printf("Device is disconnected, re-queuing SCB\n");
  2005. wait = TRUE;
  2006. } else {
  2007. scmd_printk(KERN_INFO, cmd, "Unable to deliver message\n");
  2008. retval = FAILED;
  2009. goto done;
  2010. }
  2011. no_cmd:
  2012. /*
  2013. * Our assumption is that if we don't have the command, no
  2014. * recovery action was required, so we return success. Again,
  2015. * the semantics of the mid-layer recovery engine are not
  2016. * well defined, so this may change in time.
  2017. */
  2018. retval = SUCCESS;
  2019. done:
  2020. if (paused)
  2021. ahc_unpause(ahc);
  2022. if (wait) {
  2023. DECLARE_COMPLETION_ONSTACK(done);
  2024. ahc->platform_data->eh_done = &done;
  2025. ahc_unlock(ahc, &flags);
  2026. printf("Recovery code sleeping\n");
  2027. if (!wait_for_completion_timeout(&done, 5 * HZ)) {
  2028. ahc_lock(ahc, &flags);
  2029. ahc->platform_data->eh_done = NULL;
  2030. ahc_unlock(ahc, &flags);
  2031. printf("Timer Expired\n");
  2032. retval = FAILED;
  2033. }
  2034. printf("Recovery code awake\n");
  2035. } else
  2036. ahc_unlock(ahc, &flags);
  2037. return (retval);
  2038. }
  2039. void
  2040. ahc_platform_dump_card_state(struct ahc_softc *ahc)
  2041. {
  2042. }
  2043. static void ahc_linux_set_width(struct scsi_target *starget, int width)
  2044. {
  2045. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2046. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2047. struct ahc_devinfo devinfo;
  2048. unsigned long flags;
  2049. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2050. starget->channel + 'A', ROLE_INITIATOR);
  2051. ahc_lock(ahc, &flags);
  2052. ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
  2053. ahc_unlock(ahc, &flags);
  2054. }
  2055. static void ahc_linux_set_period(struct scsi_target *starget, int period)
  2056. {
  2057. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2058. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2059. struct ahc_tmode_tstate *tstate;
  2060. struct ahc_initiator_tinfo *tinfo
  2061. = ahc_fetch_transinfo(ahc,
  2062. starget->channel + 'A',
  2063. shost->this_id, starget->id, &tstate);
  2064. struct ahc_devinfo devinfo;
  2065. unsigned int ppr_options = tinfo->goal.ppr_options;
  2066. unsigned long flags;
  2067. unsigned long offset = tinfo->goal.offset;
  2068. struct ahc_syncrate *syncrate;
  2069. if (offset == 0)
  2070. offset = MAX_OFFSET;
  2071. if (period < 9)
  2072. period = 9; /* 12.5ns is our minimum */
  2073. if (period == 9) {
  2074. if (spi_max_width(starget))
  2075. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2076. else
  2077. /* need wide for DT and need DT for 12.5 ns */
  2078. period = 10;
  2079. }
  2080. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2081. starget->channel + 'A', ROLE_INITIATOR);
  2082. /* all PPR requests apart from QAS require wide transfers */
  2083. if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
  2084. if (spi_width(starget) == 0)
  2085. ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2086. }
  2087. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2088. ahc_lock(ahc, &flags);
  2089. ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
  2090. ppr_options, AHC_TRANS_GOAL, FALSE);
  2091. ahc_unlock(ahc, &flags);
  2092. }
  2093. static void ahc_linux_set_offset(struct scsi_target *starget, int offset)
  2094. {
  2095. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2096. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2097. struct ahc_tmode_tstate *tstate;
  2098. struct ahc_initiator_tinfo *tinfo
  2099. = ahc_fetch_transinfo(ahc,
  2100. starget->channel + 'A',
  2101. shost->this_id, starget->id, &tstate);
  2102. struct ahc_devinfo devinfo;
  2103. unsigned int ppr_options = 0;
  2104. unsigned int period = 0;
  2105. unsigned long flags;
  2106. struct ahc_syncrate *syncrate = NULL;
  2107. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2108. starget->channel + 'A', ROLE_INITIATOR);
  2109. if (offset != 0) {
  2110. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2111. period = tinfo->goal.period;
  2112. ppr_options = tinfo->goal.ppr_options;
  2113. }
  2114. ahc_lock(ahc, &flags);
  2115. ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
  2116. ppr_options, AHC_TRANS_GOAL, FALSE);
  2117. ahc_unlock(ahc, &flags);
  2118. }
  2119. static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
  2120. {
  2121. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2122. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2123. struct ahc_tmode_tstate *tstate;
  2124. struct ahc_initiator_tinfo *tinfo
  2125. = ahc_fetch_transinfo(ahc,
  2126. starget->channel + 'A',
  2127. shost->this_id, starget->id, &tstate);
  2128. struct ahc_devinfo devinfo;
  2129. unsigned int ppr_options = tinfo->goal.ppr_options
  2130. & ~MSG_EXT_PPR_DT_REQ;
  2131. unsigned int period = tinfo->goal.period;
  2132. unsigned int width = tinfo->goal.width;
  2133. unsigned long flags;
  2134. struct ahc_syncrate *syncrate;
  2135. if (dt && spi_max_width(starget)) {
  2136. ppr_options |= MSG_EXT_PPR_DT_REQ;
  2137. if (!width)
  2138. ahc_linux_set_width(starget, 1);
  2139. } else if (period == 9)
  2140. period = 10; /* if resetting DT, period must be >= 25ns */
  2141. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2142. starget->channel + 'A', ROLE_INITIATOR);
  2143. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,AHC_SYNCRATE_DT);
  2144. ahc_lock(ahc, &flags);
  2145. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2146. ppr_options, AHC_TRANS_GOAL, FALSE);
  2147. ahc_unlock(ahc, &flags);
  2148. }
  2149. #if 0
  2150. /* FIXME: This code claims to support IU and QAS. However, the actual
  2151. * sequencer code and aic7xxx_core have no support for these parameters and
  2152. * will get into a bad state if they're negotiated. Do not enable this
  2153. * unless you know what you're doing */
  2154. static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
  2155. {
  2156. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2157. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2158. struct ahc_tmode_tstate *tstate;
  2159. struct ahc_initiator_tinfo *tinfo
  2160. = ahc_fetch_transinfo(ahc,
  2161. starget->channel + 'A',
  2162. shost->this_id, starget->id, &tstate);
  2163. struct ahc_devinfo devinfo;
  2164. unsigned int ppr_options = tinfo->goal.ppr_options
  2165. & ~MSG_EXT_PPR_QAS_REQ;
  2166. unsigned int period = tinfo->goal.period;
  2167. unsigned long flags;
  2168. struct ahc_syncrate *syncrate;
  2169. if (qas)
  2170. ppr_options |= MSG_EXT_PPR_QAS_REQ;
  2171. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2172. starget->channel + 'A', ROLE_INITIATOR);
  2173. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2174. ahc_lock(ahc, &flags);
  2175. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2176. ppr_options, AHC_TRANS_GOAL, FALSE);
  2177. ahc_unlock(ahc, &flags);
  2178. }
  2179. static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
  2180. {
  2181. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2182. struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
  2183. struct ahc_tmode_tstate *tstate;
  2184. struct ahc_initiator_tinfo *tinfo
  2185. = ahc_fetch_transinfo(ahc,
  2186. starget->channel + 'A',
  2187. shost->this_id, starget->id, &tstate);
  2188. struct ahc_devinfo devinfo;
  2189. unsigned int ppr_options = tinfo->goal.ppr_options
  2190. & ~MSG_EXT_PPR_IU_REQ;
  2191. unsigned int period = tinfo->goal.period;
  2192. unsigned long flags;
  2193. struct ahc_syncrate *syncrate;
  2194. if (iu)
  2195. ppr_options |= MSG_EXT_PPR_IU_REQ;
  2196. ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
  2197. starget->channel + 'A', ROLE_INITIATOR);
  2198. syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
  2199. ahc_lock(ahc, &flags);
  2200. ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
  2201. ppr_options, AHC_TRANS_GOAL, FALSE);
  2202. ahc_unlock(ahc, &flags);
  2203. }
  2204. #endif
  2205. static void ahc_linux_get_signalling(struct Scsi_Host *shost)
  2206. {
  2207. struct ahc_softc *ahc = *(struct ahc_softc **)shost->hostdata;
  2208. unsigned long flags;
  2209. u8 mode;
  2210. if (!(ahc->features & AHC_ULTRA2)) {
  2211. /* non-LVD chipset, may not have SBLKCTL reg */
  2212. spi_signalling(shost) =
  2213. ahc->features & AHC_HVD ?
  2214. SPI_SIGNAL_HVD :
  2215. SPI_SIGNAL_SE;
  2216. return;
  2217. }
  2218. ahc_lock(ahc, &flags);
  2219. ahc_pause(ahc);
  2220. mode = ahc_inb(ahc, SBLKCTL);
  2221. ahc_unpause(ahc);
  2222. ahc_unlock(ahc, &flags);
  2223. if (mode & ENAB40)
  2224. spi_signalling(shost) = SPI_SIGNAL_LVD;
  2225. else if (mode & ENAB20)
  2226. spi_signalling(shost) = SPI_SIGNAL_SE;
  2227. else
  2228. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  2229. }
  2230. static struct spi_function_template ahc_linux_transport_functions = {
  2231. .set_offset = ahc_linux_set_offset,
  2232. .show_offset = 1,
  2233. .set_period = ahc_linux_set_period,
  2234. .show_period = 1,
  2235. .set_width = ahc_linux_set_width,
  2236. .show_width = 1,
  2237. .set_dt = ahc_linux_set_dt,
  2238. .show_dt = 1,
  2239. #if 0
  2240. .set_iu = ahc_linux_set_iu,
  2241. .show_iu = 1,
  2242. .set_qas = ahc_linux_set_qas,
  2243. .show_qas = 1,
  2244. #endif
  2245. .get_signalling = ahc_linux_get_signalling,
  2246. };
  2247. static int __init
  2248. ahc_linux_init(void)
  2249. {
  2250. /*
  2251. * If we've been passed any parameters, process them now.
  2252. */
  2253. if (aic7xxx)
  2254. aic7xxx_setup(aic7xxx);
  2255. ahc_linux_transport_template =
  2256. spi_attach_transport(&ahc_linux_transport_functions);
  2257. if (!ahc_linux_transport_template)
  2258. return -ENODEV;
  2259. scsi_transport_reserve_device(ahc_linux_transport_template,
  2260. sizeof(struct ahc_linux_device));
  2261. ahc_linux_pci_init();
  2262. ahc_linux_eisa_init();
  2263. return 0;
  2264. }
  2265. static void
  2266. ahc_linux_exit(void)
  2267. {
  2268. ahc_linux_pci_exit();
  2269. ahc_linux_eisa_exit();
  2270. spi_release_transport(ahc_linux_transport_template);
  2271. }
  2272. module_init(ahc_linux_init);
  2273. module_exit(ahc_linux_exit);