aic7xxx_osm.c 72 KB

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