aic7xxx_osm.c 82 KB

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