aic7xxx_osm.c 73 KB

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