aic7xxx_osm.c 72 KB

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