aic7xxx_osm.c 72 KB

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