aic7xxx_osm.c 72 KB

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