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