aic7xxx_osm.c 71 KB

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