aic7xxx_osm.c 82 KB

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