aic7xxx_osm.c 82 KB

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