aic7xxx_osm.c 70 KB

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