aic7xxx_osm.c 77 KB

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