aic7xxx_osm.c 72 KB

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