aic7xxx_osm.c 76 KB

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