aic7xxx_osm.c 71 KB

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