aic7xxx_osm.c 81 KB

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