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