aic7xxx_osm.c 77 KB

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