53c700.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /* NCR (or Symbios) 53c700 and 53c700-66 Driver
  3. *
  4. * Copyright (C) 2001 by James.Bottomley@HansenPartnership.com
  5. **-----------------------------------------------------------------------------
  6. **
  7. ** This program is free software; you can redistribute it and/or modify
  8. ** it under the terms of the GNU General Public License as published by
  9. ** the Free Software Foundation; either version 2 of the License, or
  10. ** (at your option) any later version.
  11. **
  12. ** This program is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ** GNU General Public License for more details.
  16. **
  17. ** You should have received a copy of the GNU General Public License
  18. ** along with this program; if not, write to the Free Software
  19. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. /* Notes:
  24. *
  25. * This driver is designed exclusively for these chips (virtually the
  26. * earliest of the scripts engine chips). They need their own drivers
  27. * because they are missing so many of the scripts and snazzy register
  28. * features of their elder brothers (the 710, 720 and 770).
  29. *
  30. * The 700 is the lowliest of the line, it can only do async SCSI.
  31. * The 700-66 can at least do synchronous SCSI up to 10MHz.
  32. *
  33. * The 700 chip has no host bus interface logic of its own. However,
  34. * it is usually mapped to a location with well defined register
  35. * offsets. Therefore, if you can determine the base address and the
  36. * irq your board incorporating this chip uses, you can probably use
  37. * this driver to run it (although you'll probably have to write a
  38. * minimal wrapper for the purpose---see the NCR_D700 driver for
  39. * details about how to do this).
  40. *
  41. *
  42. * TODO List:
  43. *
  44. * 1. Better statistics in the proc fs
  45. *
  46. * 2. Implement message queue (queues SCSI messages like commands) and make
  47. * the abort and device reset functions use them.
  48. * */
  49. /* CHANGELOG
  50. *
  51. * Version 2.8
  52. *
  53. * Fixed bad bug affecting tag starvation processing (previously the
  54. * driver would hang the system if too many tags starved. Also fixed
  55. * bad bug having to do with 10 byte command processing and REQUEST
  56. * SENSE (the command would loop forever getting a transfer length
  57. * mismatch in the CMD phase).
  58. *
  59. * Version 2.7
  60. *
  61. * Fixed scripts problem which caused certain devices (notably CDRWs)
  62. * to hang on initial INQUIRY. Updated NCR_700_readl/writel to use
  63. * __raw_readl/writel for parisc compatibility (Thomas
  64. * Bogendoerfer). Added missing SCp->request_bufflen initialisation
  65. * for sense requests (Ryan Bradetich).
  66. *
  67. * Version 2.6
  68. *
  69. * Following test of the 64 bit parisc kernel by Richard Hirst,
  70. * several problems have now been corrected. Also adds support for
  71. * consistent memory allocation.
  72. *
  73. * Version 2.5
  74. *
  75. * More Compatibility changes for 710 (now actually works). Enhanced
  76. * support for odd clock speeds which constrain SDTR negotiations.
  77. * correct cacheline separation for scsi messages and status for
  78. * incoherent architectures. Use of the pci mapping functions on
  79. * buffers to begin support for 64 bit drivers.
  80. *
  81. * Version 2.4
  82. *
  83. * Added support for the 53c710 chip (in 53c700 emulation mode only---no
  84. * special 53c710 instructions or registers are used).
  85. *
  86. * Version 2.3
  87. *
  88. * More endianness/cache coherency changes.
  89. *
  90. * Better bad device handling (handles devices lying about tag
  91. * queueing support and devices which fail to provide sense data on
  92. * contingent allegiance conditions)
  93. *
  94. * Many thanks to Richard Hirst <rhirst@linuxcare.com> for patiently
  95. * debugging this driver on the parisc architecture and suggesting
  96. * many improvements and bug fixes.
  97. *
  98. * Thanks also go to Linuxcare Inc. for providing several PARISC
  99. * machines for me to debug the driver on.
  100. *
  101. * Version 2.2
  102. *
  103. * Made the driver mem or io mapped; added endian invariance; added
  104. * dma cache flushing operations for architectures which need it;
  105. * added support for more varied clocking speeds.
  106. *
  107. * Version 2.1
  108. *
  109. * Initial modularisation from the D700. See NCR_D700.c for the rest of
  110. * the changelog.
  111. * */
  112. #define NCR_700_VERSION "2.8"
  113. #include <linux/kernel.h>
  114. #include <linux/types.h>
  115. #include <linux/string.h>
  116. #include <linux/slab.h>
  117. #include <linux/ioport.h>
  118. #include <linux/delay.h>
  119. #include <linux/spinlock.h>
  120. #include <linux/completion.h>
  121. #include <linux/init.h>
  122. #include <linux/proc_fs.h>
  123. #include <linux/blkdev.h>
  124. #include <linux/module.h>
  125. #include <linux/interrupt.h>
  126. #include <linux/device.h>
  127. #include <asm/dma.h>
  128. #include <asm/system.h>
  129. #include <asm/io.h>
  130. #include <asm/pgtable.h>
  131. #include <asm/byteorder.h>
  132. #include <scsi/scsi.h>
  133. #include <scsi/scsi_cmnd.h>
  134. #include <scsi/scsi_dbg.h>
  135. #include <scsi/scsi_eh.h>
  136. #include <scsi/scsi_host.h>
  137. #include <scsi/scsi_tcq.h>
  138. #include <scsi/scsi_transport.h>
  139. #include <scsi/scsi_transport_spi.h>
  140. #include "53c700.h"
  141. /* NOTE: For 64 bit drivers there are points in the code where we use
  142. * a non dereferenceable pointer to point to a structure in dma-able
  143. * memory (which is 32 bits) so that we can use all of the structure
  144. * operations but take the address at the end. This macro allows us
  145. * to truncate the 64 bit pointer down to 32 bits without the compiler
  146. * complaining */
  147. #define to32bit(x) ((__u32)((unsigned long)(x)))
  148. #ifdef NCR_700_DEBUG
  149. #define STATIC
  150. #else
  151. #define STATIC static
  152. #endif
  153. MODULE_AUTHOR("James Bottomley");
  154. MODULE_DESCRIPTION("53c700 and 53c700-66 Driver");
  155. MODULE_LICENSE("GPL");
  156. /* This is the script */
  157. #include "53c700_d.h"
  158. STATIC int NCR_700_queuecommand(struct scsi_cmnd *, void (*done)(struct scsi_cmnd *));
  159. STATIC int NCR_700_abort(struct scsi_cmnd * SCpnt);
  160. STATIC int NCR_700_bus_reset(struct scsi_cmnd * SCpnt);
  161. STATIC int NCR_700_host_reset(struct scsi_cmnd * SCpnt);
  162. STATIC void NCR_700_chip_setup(struct Scsi_Host *host);
  163. STATIC void NCR_700_chip_reset(struct Scsi_Host *host);
  164. STATIC int NCR_700_slave_alloc(struct scsi_device *SDpnt);
  165. STATIC int NCR_700_slave_configure(struct scsi_device *SDpnt);
  166. STATIC void NCR_700_slave_destroy(struct scsi_device *SDpnt);
  167. static int NCR_700_change_queue_depth(struct scsi_device *SDpnt, int depth, int reason);
  168. static int NCR_700_change_queue_type(struct scsi_device *SDpnt, int depth);
  169. STATIC struct device_attribute *NCR_700_dev_attrs[];
  170. STATIC struct scsi_transport_template *NCR_700_transport_template = NULL;
  171. static char *NCR_700_phase[] = {
  172. "",
  173. "after selection",
  174. "before command phase",
  175. "after command phase",
  176. "after status phase",
  177. "after data in phase",
  178. "after data out phase",
  179. "during data phase",
  180. };
  181. static char *NCR_700_condition[] = {
  182. "",
  183. "NOT MSG_OUT",
  184. "UNEXPECTED PHASE",
  185. "NOT MSG_IN",
  186. "UNEXPECTED MSG",
  187. "MSG_IN",
  188. "SDTR_MSG RECEIVED",
  189. "REJECT_MSG RECEIVED",
  190. "DISCONNECT_MSG RECEIVED",
  191. "MSG_OUT",
  192. "DATA_IN",
  193. };
  194. static char *NCR_700_fatal_messages[] = {
  195. "unexpected message after reselection",
  196. "still MSG_OUT after message injection",
  197. "not MSG_IN after selection",
  198. "Illegal message length received",
  199. };
  200. static char *NCR_700_SBCL_bits[] = {
  201. "IO ",
  202. "CD ",
  203. "MSG ",
  204. "ATN ",
  205. "SEL ",
  206. "BSY ",
  207. "ACK ",
  208. "REQ ",
  209. };
  210. static char *NCR_700_SBCL_to_phase[] = {
  211. "DATA_OUT",
  212. "DATA_IN",
  213. "CMD_OUT",
  214. "STATE",
  215. "ILLEGAL PHASE",
  216. "ILLEGAL PHASE",
  217. "MSG OUT",
  218. "MSG IN",
  219. };
  220. /* This translates the SDTR message offset and period to a value
  221. * which can be loaded into the SXFER_REG.
  222. *
  223. * NOTE: According to SCSI-2, the true transfer period (in ns) is
  224. * actually four times this period value */
  225. static inline __u8
  226. NCR_700_offset_period_to_sxfer(struct NCR_700_Host_Parameters *hostdata,
  227. __u8 offset, __u8 period)
  228. {
  229. int XFERP;
  230. __u8 min_xferp = (hostdata->chip710
  231. ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  232. __u8 max_offset = (hostdata->chip710
  233. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET);
  234. if(offset == 0)
  235. return 0;
  236. if(period < hostdata->min_period) {
  237. printk(KERN_WARNING "53c700: Period %dns is less than this chip's minimum, setting to %d\n", period*4, NCR_700_MIN_PERIOD*4);
  238. period = hostdata->min_period;
  239. }
  240. XFERP = (period*4 * hostdata->sync_clock)/1000 - 4;
  241. if(offset > max_offset) {
  242. printk(KERN_WARNING "53c700: Offset %d exceeds chip maximum, setting to %d\n",
  243. offset, max_offset);
  244. offset = max_offset;
  245. }
  246. if(XFERP < min_xferp) {
  247. XFERP = min_xferp;
  248. }
  249. return (offset & 0x0f) | (XFERP & 0x07)<<4;
  250. }
  251. static inline __u8
  252. NCR_700_get_SXFER(struct scsi_device *SDp)
  253. {
  254. struct NCR_700_Host_Parameters *hostdata =
  255. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  256. return NCR_700_offset_period_to_sxfer(hostdata,
  257. spi_offset(SDp->sdev_target),
  258. spi_period(SDp->sdev_target));
  259. }
  260. struct Scsi_Host *
  261. NCR_700_detect(struct scsi_host_template *tpnt,
  262. struct NCR_700_Host_Parameters *hostdata, struct device *dev)
  263. {
  264. dma_addr_t pScript, pSlots;
  265. __u8 *memory;
  266. __u32 *script;
  267. struct Scsi_Host *host;
  268. static int banner = 0;
  269. int j;
  270. if(tpnt->sdev_attrs == NULL)
  271. tpnt->sdev_attrs = NCR_700_dev_attrs;
  272. memory = dma_alloc_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  273. &pScript, GFP_KERNEL);
  274. if(memory == NULL) {
  275. printk(KERN_ERR "53c700: Failed to allocate memory for driver, detatching\n");
  276. return NULL;
  277. }
  278. script = (__u32 *)memory;
  279. hostdata->msgin = memory + MSGIN_OFFSET;
  280. hostdata->msgout = memory + MSGOUT_OFFSET;
  281. hostdata->status = memory + STATUS_OFFSET;
  282. /* all of these offsets are L1_CACHE_BYTES separated. It is fatal
  283. * if this isn't sufficient separation to avoid dma flushing issues */
  284. BUG_ON(!dma_is_consistent(hostdata->dev, pScript) && L1_CACHE_BYTES < dma_get_cache_alignment());
  285. hostdata->slots = (struct NCR_700_command_slot *)(memory + SLOTS_OFFSET);
  286. hostdata->dev = dev;
  287. pSlots = pScript + SLOTS_OFFSET;
  288. /* Fill in the missing routines from the host template */
  289. tpnt->queuecommand = NCR_700_queuecommand;
  290. tpnt->eh_abort_handler = NCR_700_abort;
  291. tpnt->eh_bus_reset_handler = NCR_700_bus_reset;
  292. tpnt->eh_host_reset_handler = NCR_700_host_reset;
  293. tpnt->can_queue = NCR_700_COMMAND_SLOTS_PER_HOST;
  294. tpnt->sg_tablesize = NCR_700_SG_SEGMENTS;
  295. tpnt->cmd_per_lun = NCR_700_CMD_PER_LUN;
  296. tpnt->use_clustering = ENABLE_CLUSTERING;
  297. tpnt->slave_configure = NCR_700_slave_configure;
  298. tpnt->slave_destroy = NCR_700_slave_destroy;
  299. tpnt->slave_alloc = NCR_700_slave_alloc;
  300. tpnt->change_queue_depth = NCR_700_change_queue_depth;
  301. tpnt->change_queue_type = NCR_700_change_queue_type;
  302. if(tpnt->name == NULL)
  303. tpnt->name = "53c700";
  304. if(tpnt->proc_name == NULL)
  305. tpnt->proc_name = "53c700";
  306. host = scsi_host_alloc(tpnt, 4);
  307. if (!host)
  308. return NULL;
  309. memset(hostdata->slots, 0, sizeof(struct NCR_700_command_slot)
  310. * NCR_700_COMMAND_SLOTS_PER_HOST);
  311. for (j = 0; j < NCR_700_COMMAND_SLOTS_PER_HOST; j++) {
  312. dma_addr_t offset = (dma_addr_t)((unsigned long)&hostdata->slots[j].SG[0]
  313. - (unsigned long)&hostdata->slots[0].SG[0]);
  314. hostdata->slots[j].pSG = (struct NCR_700_SG_List *)((unsigned long)(pSlots + offset));
  315. if(j == 0)
  316. hostdata->free_list = &hostdata->slots[j];
  317. else
  318. hostdata->slots[j-1].ITL_forw = &hostdata->slots[j];
  319. hostdata->slots[j].state = NCR_700_SLOT_FREE;
  320. }
  321. for (j = 0; j < ARRAY_SIZE(SCRIPT); j++)
  322. script[j] = bS_to_host(SCRIPT[j]);
  323. /* adjust all labels to be bus physical */
  324. for (j = 0; j < PATCHES; j++)
  325. script[LABELPATCHES[j]] = bS_to_host(pScript + SCRIPT[LABELPATCHES[j]]);
  326. /* now patch up fixed addresses. */
  327. script_patch_32(hostdata->dev, script, MessageLocation,
  328. pScript + MSGOUT_OFFSET);
  329. script_patch_32(hostdata->dev, script, StatusAddress,
  330. pScript + STATUS_OFFSET);
  331. script_patch_32(hostdata->dev, script, ReceiveMsgAddress,
  332. pScript + MSGIN_OFFSET);
  333. hostdata->script = script;
  334. hostdata->pScript = pScript;
  335. dma_sync_single_for_device(hostdata->dev, pScript, sizeof(SCRIPT), DMA_TO_DEVICE);
  336. hostdata->state = NCR_700_HOST_FREE;
  337. hostdata->cmd = NULL;
  338. host->max_id = 8;
  339. host->max_lun = NCR_700_MAX_LUNS;
  340. BUG_ON(NCR_700_transport_template == NULL);
  341. host->transportt = NCR_700_transport_template;
  342. host->unique_id = (unsigned long)hostdata->base;
  343. hostdata->eh_complete = NULL;
  344. host->hostdata[0] = (unsigned long)hostdata;
  345. /* kick the chip */
  346. NCR_700_writeb(0xff, host, CTEST9_REG);
  347. if (hostdata->chip710)
  348. hostdata->rev = (NCR_700_readb(host, CTEST8_REG)>>4) & 0x0f;
  349. else
  350. hostdata->rev = (NCR_700_readb(host, CTEST7_REG)>>4) & 0x0f;
  351. hostdata->fast = (NCR_700_readb(host, CTEST9_REG) == 0);
  352. if (banner == 0) {
  353. printk(KERN_NOTICE "53c700: Version " NCR_700_VERSION " By James.Bottomley@HansenPartnership.com\n");
  354. banner = 1;
  355. }
  356. printk(KERN_NOTICE "scsi%d: %s rev %d %s\n", host->host_no,
  357. hostdata->chip710 ? "53c710" :
  358. (hostdata->fast ? "53c700-66" : "53c700"),
  359. hostdata->rev, hostdata->differential ?
  360. "(Differential)" : "");
  361. /* reset the chip */
  362. NCR_700_chip_reset(host);
  363. if (scsi_add_host(host, dev)) {
  364. dev_printk(KERN_ERR, dev, "53c700: scsi_add_host failed\n");
  365. scsi_host_put(host);
  366. return NULL;
  367. }
  368. spi_signalling(host) = hostdata->differential ? SPI_SIGNAL_HVD :
  369. SPI_SIGNAL_SE;
  370. return host;
  371. }
  372. int
  373. NCR_700_release(struct Scsi_Host *host)
  374. {
  375. struct NCR_700_Host_Parameters *hostdata =
  376. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  377. dma_free_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  378. hostdata->script, hostdata->pScript);
  379. return 1;
  380. }
  381. static inline __u8
  382. NCR_700_identify(int can_disconnect, __u8 lun)
  383. {
  384. return IDENTIFY_BASE |
  385. ((can_disconnect) ? 0x40 : 0) |
  386. (lun & NCR_700_LUN_MASK);
  387. }
  388. /*
  389. * Function : static int data_residual (Scsi_Host *host)
  390. *
  391. * Purpose : return residual data count of what's in the chip. If you
  392. * really want to know what this function is doing, it's almost a
  393. * direct transcription of the algorithm described in the 53c710
  394. * guide, except that the DBC and DFIFO registers are only 6 bits
  395. * wide on a 53c700.
  396. *
  397. * Inputs : host - SCSI host */
  398. static inline int
  399. NCR_700_data_residual (struct Scsi_Host *host) {
  400. struct NCR_700_Host_Parameters *hostdata =
  401. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  402. int count, synchronous = 0;
  403. unsigned int ddir;
  404. if(hostdata->chip710) {
  405. count = ((NCR_700_readb(host, DFIFO_REG) & 0x7f) -
  406. (NCR_700_readl(host, DBC_REG) & 0x7f)) & 0x7f;
  407. } else {
  408. count = ((NCR_700_readb(host, DFIFO_REG) & 0x3f) -
  409. (NCR_700_readl(host, DBC_REG) & 0x3f)) & 0x3f;
  410. }
  411. if(hostdata->fast)
  412. synchronous = NCR_700_readb(host, SXFER_REG) & 0x0f;
  413. /* get the data direction */
  414. ddir = NCR_700_readb(host, CTEST0_REG) & 0x01;
  415. if (ddir) {
  416. /* Receive */
  417. if (synchronous)
  418. count += (NCR_700_readb(host, SSTAT2_REG) & 0xf0) >> 4;
  419. else
  420. if (NCR_700_readb(host, SSTAT1_REG) & SIDL_REG_FULL)
  421. ++count;
  422. } else {
  423. /* Send */
  424. __u8 sstat = NCR_700_readb(host, SSTAT1_REG);
  425. if (sstat & SODL_REG_FULL)
  426. ++count;
  427. if (synchronous && (sstat & SODR_REG_FULL))
  428. ++count;
  429. }
  430. #ifdef NCR_700_DEBUG
  431. if(count)
  432. printk("RESIDUAL IS %d (ddir %d)\n", count, ddir);
  433. #endif
  434. return count;
  435. }
  436. /* print out the SCSI wires and corresponding phase from the SBCL register
  437. * in the chip */
  438. static inline char *
  439. sbcl_to_string(__u8 sbcl)
  440. {
  441. int i;
  442. static char ret[256];
  443. ret[0]='\0';
  444. for(i=0; i<8; i++) {
  445. if((1<<i) & sbcl)
  446. strcat(ret, NCR_700_SBCL_bits[i]);
  447. }
  448. strcat(ret, NCR_700_SBCL_to_phase[sbcl & 0x07]);
  449. return ret;
  450. }
  451. static inline __u8
  452. bitmap_to_number(__u8 bitmap)
  453. {
  454. __u8 i;
  455. for(i=0; i<8 && !(bitmap &(1<<i)); i++)
  456. ;
  457. return i;
  458. }
  459. /* Pull a slot off the free list */
  460. STATIC struct NCR_700_command_slot *
  461. find_empty_slot(struct NCR_700_Host_Parameters *hostdata)
  462. {
  463. struct NCR_700_command_slot *slot = hostdata->free_list;
  464. if(slot == NULL) {
  465. /* sanity check */
  466. if(hostdata->command_slot_count != NCR_700_COMMAND_SLOTS_PER_HOST)
  467. printk(KERN_ERR "SLOTS FULL, but count is %d, should be %d\n", hostdata->command_slot_count, NCR_700_COMMAND_SLOTS_PER_HOST);
  468. return NULL;
  469. }
  470. if(slot->state != NCR_700_SLOT_FREE)
  471. /* should panic! */
  472. printk(KERN_ERR "BUSY SLOT ON FREE LIST!!!\n");
  473. hostdata->free_list = slot->ITL_forw;
  474. slot->ITL_forw = NULL;
  475. /* NOTE: set the state to busy here, not queued, since this
  476. * indicates the slot is in use and cannot be run by the IRQ
  477. * finish routine. If we cannot queue the command when it
  478. * is properly build, we then change to NCR_700_SLOT_QUEUED */
  479. slot->state = NCR_700_SLOT_BUSY;
  480. slot->flags = 0;
  481. hostdata->command_slot_count++;
  482. return slot;
  483. }
  484. STATIC void
  485. free_slot(struct NCR_700_command_slot *slot,
  486. struct NCR_700_Host_Parameters *hostdata)
  487. {
  488. if((slot->state & NCR_700_SLOT_MASK) != NCR_700_SLOT_MAGIC) {
  489. printk(KERN_ERR "53c700: SLOT %p is not MAGIC!!!\n", slot);
  490. }
  491. if(slot->state == NCR_700_SLOT_FREE) {
  492. printk(KERN_ERR "53c700: SLOT %p is FREE!!!\n", slot);
  493. }
  494. slot->resume_offset = 0;
  495. slot->cmnd = NULL;
  496. slot->state = NCR_700_SLOT_FREE;
  497. slot->ITL_forw = hostdata->free_list;
  498. hostdata->free_list = slot;
  499. hostdata->command_slot_count--;
  500. }
  501. /* This routine really does very little. The command is indexed on
  502. the ITL and (if tagged) the ITLQ lists in _queuecommand */
  503. STATIC void
  504. save_for_reselection(struct NCR_700_Host_Parameters *hostdata,
  505. struct scsi_cmnd *SCp, __u32 dsp)
  506. {
  507. /* Its just possible that this gets executed twice */
  508. if(SCp != NULL) {
  509. struct NCR_700_command_slot *slot =
  510. (struct NCR_700_command_slot *)SCp->host_scribble;
  511. slot->resume_offset = dsp;
  512. }
  513. hostdata->state = NCR_700_HOST_FREE;
  514. hostdata->cmd = NULL;
  515. }
  516. STATIC inline void
  517. NCR_700_unmap(struct NCR_700_Host_Parameters *hostdata, struct scsi_cmnd *SCp,
  518. struct NCR_700_command_slot *slot)
  519. {
  520. if(SCp->sc_data_direction != DMA_NONE &&
  521. SCp->sc_data_direction != DMA_BIDIRECTIONAL)
  522. scsi_dma_unmap(SCp);
  523. }
  524. STATIC inline void
  525. NCR_700_scsi_done(struct NCR_700_Host_Parameters *hostdata,
  526. struct scsi_cmnd *SCp, int result)
  527. {
  528. hostdata->state = NCR_700_HOST_FREE;
  529. hostdata->cmd = NULL;
  530. if(SCp != NULL) {
  531. struct NCR_700_command_slot *slot =
  532. (struct NCR_700_command_slot *)SCp->host_scribble;
  533. dma_unmap_single(hostdata->dev, slot->pCmd,
  534. MAX_COMMAND_SIZE, DMA_TO_DEVICE);
  535. if (slot->flags == NCR_700_FLAG_AUTOSENSE) {
  536. char *cmnd = NCR_700_get_sense_cmnd(SCp->device);
  537. #ifdef NCR_700_DEBUG
  538. printk(" ORIGINAL CMD %p RETURNED %d, new return is %d sense is\n",
  539. SCp, SCp->cmnd[7], result);
  540. scsi_print_sense("53c700", SCp);
  541. #endif
  542. dma_unmap_single(hostdata->dev, slot->dma_handle,
  543. SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  544. /* restore the old result if the request sense was
  545. * successful */
  546. if (result == 0)
  547. result = cmnd[7];
  548. /* restore the original length */
  549. SCp->cmd_len = cmnd[8];
  550. } else
  551. NCR_700_unmap(hostdata, SCp, slot);
  552. free_slot(slot, hostdata);
  553. #ifdef NCR_700_DEBUG
  554. if(NCR_700_get_depth(SCp->device) == 0 ||
  555. NCR_700_get_depth(SCp->device) > SCp->device->queue_depth)
  556. printk(KERN_ERR "Invalid depth in NCR_700_scsi_done(): %d\n",
  557. NCR_700_get_depth(SCp->device));
  558. #endif /* NCR_700_DEBUG */
  559. NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) - 1);
  560. SCp->host_scribble = NULL;
  561. SCp->result = result;
  562. SCp->scsi_done(SCp);
  563. } else {
  564. printk(KERN_ERR "53c700: SCSI DONE HAS NULL SCp\n");
  565. }
  566. }
  567. STATIC void
  568. NCR_700_internal_bus_reset(struct Scsi_Host *host)
  569. {
  570. /* Bus reset */
  571. NCR_700_writeb(ASSERT_RST, host, SCNTL1_REG);
  572. udelay(50);
  573. NCR_700_writeb(0, host, SCNTL1_REG);
  574. }
  575. STATIC void
  576. NCR_700_chip_setup(struct Scsi_Host *host)
  577. {
  578. struct NCR_700_Host_Parameters *hostdata =
  579. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  580. __u8 min_period;
  581. __u8 min_xferp = (hostdata->chip710 ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  582. if(hostdata->chip710) {
  583. __u8 burst_disable = 0;
  584. __u8 burst_length = 0;
  585. switch (hostdata->burst_length) {
  586. case 1:
  587. burst_length = BURST_LENGTH_1;
  588. break;
  589. case 2:
  590. burst_length = BURST_LENGTH_2;
  591. break;
  592. case 4:
  593. burst_length = BURST_LENGTH_4;
  594. break;
  595. case 8:
  596. burst_length = BURST_LENGTH_8;
  597. break;
  598. default:
  599. burst_disable = BURST_DISABLE;
  600. break;
  601. }
  602. hostdata->dcntl_extra |= COMPAT_700_MODE;
  603. NCR_700_writeb(hostdata->dcntl_extra, host, DCNTL_REG);
  604. NCR_700_writeb(burst_length | hostdata->dmode_extra,
  605. host, DMODE_710_REG);
  606. NCR_700_writeb(burst_disable | hostdata->ctest7_extra |
  607. (hostdata->differential ? DIFF : 0),
  608. host, CTEST7_REG);
  609. NCR_700_writeb(BTB_TIMER_DISABLE, host, CTEST0_REG);
  610. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY | PARITY
  611. | AUTO_ATN, host, SCNTL0_REG);
  612. } else {
  613. NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
  614. host, DMODE_700_REG);
  615. NCR_700_writeb(hostdata->differential ?
  616. DIFF : 0, host, CTEST7_REG);
  617. if(hostdata->fast) {
  618. /* this is for 700-66, does nothing on 700 */
  619. NCR_700_writeb(LAST_DIS_ENBL | ENABLE_ACTIVE_NEGATION
  620. | GENERATE_RECEIVE_PARITY, host,
  621. CTEST8_REG);
  622. } else {
  623. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY
  624. | PARITY | AUTO_ATN, host, SCNTL0_REG);
  625. }
  626. }
  627. NCR_700_writeb(1 << host->this_id, host, SCID_REG);
  628. NCR_700_writeb(0, host, SBCL_REG);
  629. NCR_700_writeb(ASYNC_OPERATION, host, SXFER_REG);
  630. NCR_700_writeb(PHASE_MM_INT | SEL_TIMEOUT_INT | GROSS_ERR_INT | UX_DISC_INT
  631. | RST_INT | PAR_ERR_INT | SELECT_INT, host, SIEN_REG);
  632. NCR_700_writeb(ABORT_INT | INT_INST_INT | ILGL_INST_INT, host, DIEN_REG);
  633. NCR_700_writeb(ENABLE_SELECT, host, SCNTL1_REG);
  634. if(hostdata->clock > 75) {
  635. printk(KERN_ERR "53c700: Clock speed %dMHz is too high: 75Mhz is the maximum this chip can be driven at\n", hostdata->clock);
  636. /* do the best we can, but the async clock will be out
  637. * of spec: sync divider 2, async divider 3 */
  638. DEBUG(("53c700: sync 2 async 3\n"));
  639. NCR_700_writeb(SYNC_DIV_2_0, host, SBCL_REG);
  640. NCR_700_writeb(ASYNC_DIV_3_0 | hostdata->dcntl_extra, host, DCNTL_REG);
  641. hostdata->sync_clock = hostdata->clock/2;
  642. } else if(hostdata->clock > 50 && hostdata->clock <= 75) {
  643. /* sync divider 1.5, async divider 3 */
  644. DEBUG(("53c700: sync 1.5 async 3\n"));
  645. NCR_700_writeb(SYNC_DIV_1_5, host, SBCL_REG);
  646. NCR_700_writeb(ASYNC_DIV_3_0 | hostdata->dcntl_extra, host, DCNTL_REG);
  647. hostdata->sync_clock = hostdata->clock*2;
  648. hostdata->sync_clock /= 3;
  649. } else if(hostdata->clock > 37 && hostdata->clock <= 50) {
  650. /* sync divider 1, async divider 2 */
  651. DEBUG(("53c700: sync 1 async 2\n"));
  652. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  653. NCR_700_writeb(ASYNC_DIV_2_0 | hostdata->dcntl_extra, host, DCNTL_REG);
  654. hostdata->sync_clock = hostdata->clock;
  655. } else if(hostdata->clock > 25 && hostdata->clock <=37) {
  656. /* sync divider 1, async divider 1.5 */
  657. DEBUG(("53c700: sync 1 async 1.5\n"));
  658. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  659. NCR_700_writeb(ASYNC_DIV_1_5 | hostdata->dcntl_extra, host, DCNTL_REG);
  660. hostdata->sync_clock = hostdata->clock;
  661. } else {
  662. DEBUG(("53c700: sync 1 async 1\n"));
  663. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  664. NCR_700_writeb(ASYNC_DIV_1_0 | hostdata->dcntl_extra, host, DCNTL_REG);
  665. /* sync divider 1, async divider 1 */
  666. hostdata->sync_clock = hostdata->clock;
  667. }
  668. /* Calculate the actual minimum period that can be supported
  669. * by our synchronous clock speed. See the 710 manual for
  670. * exact details of this calculation which is based on a
  671. * setting of the SXFER register */
  672. min_period = 1000*(4+min_xferp)/(4*hostdata->sync_clock);
  673. hostdata->min_period = NCR_700_MIN_PERIOD;
  674. if(min_period > NCR_700_MIN_PERIOD)
  675. hostdata->min_period = min_period;
  676. }
  677. STATIC void
  678. NCR_700_chip_reset(struct Scsi_Host *host)
  679. {
  680. struct NCR_700_Host_Parameters *hostdata =
  681. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  682. if(hostdata->chip710) {
  683. NCR_700_writeb(SOFTWARE_RESET_710, host, ISTAT_REG);
  684. udelay(100);
  685. NCR_700_writeb(0, host, ISTAT_REG);
  686. } else {
  687. NCR_700_writeb(SOFTWARE_RESET, host, DCNTL_REG);
  688. udelay(100);
  689. NCR_700_writeb(0, host, DCNTL_REG);
  690. }
  691. mdelay(1000);
  692. NCR_700_chip_setup(host);
  693. }
  694. /* The heart of the message processing engine is that the instruction
  695. * immediately after the INT is the normal case (and so must be CLEAR
  696. * ACK). If we want to do something else, we call that routine in
  697. * scripts and set temp to be the normal case + 8 (skipping the CLEAR
  698. * ACK) so that the routine returns correctly to resume its activity
  699. * */
  700. STATIC __u32
  701. process_extended_message(struct Scsi_Host *host,
  702. struct NCR_700_Host_Parameters *hostdata,
  703. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  704. {
  705. __u32 resume_offset = dsp, temp = dsp + 8;
  706. __u8 pun = 0xff, lun = 0xff;
  707. if(SCp != NULL) {
  708. pun = SCp->device->id;
  709. lun = SCp->device->lun;
  710. }
  711. switch(hostdata->msgin[2]) {
  712. case A_SDTR_MSG:
  713. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  714. struct scsi_target *starget = SCp->device->sdev_target;
  715. __u8 period = hostdata->msgin[3];
  716. __u8 offset = hostdata->msgin[4];
  717. if(offset == 0 || period == 0) {
  718. offset = 0;
  719. period = 0;
  720. }
  721. spi_offset(starget) = offset;
  722. spi_period(starget) = period;
  723. if(NCR_700_is_flag_set(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION)) {
  724. spi_display_xfer_agreement(starget);
  725. NCR_700_clear_flag(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION);
  726. }
  727. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  728. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  729. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  730. host, SXFER_REG);
  731. } else {
  732. /* SDTR message out of the blue, reject it */
  733. shost_printk(KERN_WARNING, host,
  734. "Unexpected SDTR msg\n");
  735. hostdata->msgout[0] = A_REJECT_MSG;
  736. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  737. script_patch_16(hostdata->dev, hostdata->script,
  738. MessageCount, 1);
  739. /* SendMsgOut returns, so set up the return
  740. * address */
  741. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  742. }
  743. break;
  744. case A_WDTR_MSG:
  745. printk(KERN_INFO "scsi%d: (%d:%d), Unsolicited WDTR after CMD, Rejecting\n",
  746. host->host_no, pun, lun);
  747. hostdata->msgout[0] = A_REJECT_MSG;
  748. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  749. script_patch_16(hostdata->dev, hostdata->script, MessageCount,
  750. 1);
  751. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  752. break;
  753. default:
  754. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  755. host->host_no, pun, lun,
  756. NCR_700_phase[(dsps & 0xf00) >> 8]);
  757. spi_print_msg(hostdata->msgin);
  758. printk("\n");
  759. /* just reject it */
  760. hostdata->msgout[0] = A_REJECT_MSG;
  761. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  762. script_patch_16(hostdata->dev, hostdata->script, MessageCount,
  763. 1);
  764. /* SendMsgOut returns, so set up the return
  765. * address */
  766. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  767. }
  768. NCR_700_writel(temp, host, TEMP_REG);
  769. return resume_offset;
  770. }
  771. STATIC __u32
  772. process_message(struct Scsi_Host *host, struct NCR_700_Host_Parameters *hostdata,
  773. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  774. {
  775. /* work out where to return to */
  776. __u32 temp = dsp + 8, resume_offset = dsp;
  777. __u8 pun = 0xff, lun = 0xff;
  778. if(SCp != NULL) {
  779. pun = SCp->device->id;
  780. lun = SCp->device->lun;
  781. }
  782. #ifdef NCR_700_DEBUG
  783. printk("scsi%d (%d:%d): message %s: ", host->host_no, pun, lun,
  784. NCR_700_phase[(dsps & 0xf00) >> 8]);
  785. spi_print_msg(hostdata->msgin);
  786. printk("\n");
  787. #endif
  788. switch(hostdata->msgin[0]) {
  789. case A_EXTENDED_MSG:
  790. resume_offset = process_extended_message(host, hostdata, SCp,
  791. dsp, dsps);
  792. break;
  793. case A_REJECT_MSG:
  794. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  795. /* Rejected our sync negotiation attempt */
  796. spi_period(SCp->device->sdev_target) =
  797. spi_offset(SCp->device->sdev_target) = 0;
  798. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  799. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  800. } else if(SCp != NULL && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION) {
  801. /* rejected our first simple tag message */
  802. scmd_printk(KERN_WARNING, SCp,
  803. "Rejected first tag queue attempt, turning off tag queueing\n");
  804. /* we're done negotiating */
  805. NCR_700_set_tag_neg_state(SCp->device, NCR_700_FINISHED_TAG_NEGOTIATION);
  806. hostdata->tag_negotiated &= ~(1<<scmd_id(SCp));
  807. SCp->device->tagged_supported = 0;
  808. scsi_deactivate_tcq(SCp->device, host->cmd_per_lun);
  809. } else {
  810. shost_printk(KERN_WARNING, host,
  811. "(%d:%d) Unexpected REJECT Message %s\n",
  812. pun, lun,
  813. NCR_700_phase[(dsps & 0xf00) >> 8]);
  814. /* however, just ignore it */
  815. }
  816. break;
  817. case A_PARITY_ERROR_MSG:
  818. printk(KERN_ERR "scsi%d (%d:%d) Parity Error!\n", host->host_no,
  819. pun, lun);
  820. NCR_700_internal_bus_reset(host);
  821. break;
  822. case A_SIMPLE_TAG_MSG:
  823. printk(KERN_INFO "scsi%d (%d:%d) SIMPLE TAG %d %s\n", host->host_no,
  824. pun, lun, hostdata->msgin[1],
  825. NCR_700_phase[(dsps & 0xf00) >> 8]);
  826. /* just ignore it */
  827. break;
  828. default:
  829. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  830. host->host_no, pun, lun,
  831. NCR_700_phase[(dsps & 0xf00) >> 8]);
  832. spi_print_msg(hostdata->msgin);
  833. printk("\n");
  834. /* just reject it */
  835. hostdata->msgout[0] = A_REJECT_MSG;
  836. dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
  837. script_patch_16(hostdata->dev, hostdata->script, MessageCount,
  838. 1);
  839. /* SendMsgOut returns, so set up the return
  840. * address */
  841. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  842. break;
  843. }
  844. NCR_700_writel(temp, host, TEMP_REG);
  845. /* set us up to receive another message */
  846. dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  847. return resume_offset;
  848. }
  849. STATIC __u32
  850. process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
  851. struct Scsi_Host *host,
  852. struct NCR_700_Host_Parameters *hostdata)
  853. {
  854. __u32 resume_offset = 0;
  855. __u8 pun = 0xff, lun=0xff;
  856. if(SCp != NULL) {
  857. pun = SCp->device->id;
  858. lun = SCp->device->lun;
  859. }
  860. if(dsps == A_GOOD_STATUS_AFTER_STATUS) {
  861. DEBUG((" COMMAND COMPLETE, status=%02x\n",
  862. hostdata->status[0]));
  863. /* OK, if TCQ still under negotiation, we now know it works */
  864. if (NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION)
  865. NCR_700_set_tag_neg_state(SCp->device,
  866. NCR_700_FINISHED_TAG_NEGOTIATION);
  867. /* check for contingent allegiance contitions */
  868. if(status_byte(hostdata->status[0]) == CHECK_CONDITION ||
  869. status_byte(hostdata->status[0]) == COMMAND_TERMINATED) {
  870. struct NCR_700_command_slot *slot =
  871. (struct NCR_700_command_slot *)SCp->host_scribble;
  872. if(slot->flags == NCR_700_FLAG_AUTOSENSE) {
  873. /* OOPS: bad device, returning another
  874. * contingent allegiance condition */
  875. scmd_printk(KERN_ERR, SCp,
  876. "broken device is looping in contingent allegiance: ignoring\n");
  877. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  878. } else {
  879. char *cmnd =
  880. NCR_700_get_sense_cmnd(SCp->device);
  881. #ifdef NCR_DEBUG
  882. scsi_print_command(SCp);
  883. printk(" cmd %p has status %d, requesting sense\n",
  884. SCp, hostdata->status[0]);
  885. #endif
  886. /* we can destroy the command here
  887. * because the contingent allegiance
  888. * condition will cause a retry which
  889. * will re-copy the command from the
  890. * saved data_cmnd. We also unmap any
  891. * data associated with the command
  892. * here */
  893. NCR_700_unmap(hostdata, SCp, slot);
  894. dma_unmap_single(hostdata->dev, slot->pCmd,
  895. MAX_COMMAND_SIZE,
  896. DMA_TO_DEVICE);
  897. cmnd[0] = REQUEST_SENSE;
  898. cmnd[1] = (SCp->device->lun & 0x7) << 5;
  899. cmnd[2] = 0;
  900. cmnd[3] = 0;
  901. cmnd[4] = SCSI_SENSE_BUFFERSIZE;
  902. cmnd[5] = 0;
  903. /* Here's a quiet hack: the
  904. * REQUEST_SENSE command is six bytes,
  905. * so store a flag indicating that
  906. * this was an internal sense request
  907. * and the original status at the end
  908. * of the command */
  909. cmnd[6] = NCR_700_INTERNAL_SENSE_MAGIC;
  910. cmnd[7] = hostdata->status[0];
  911. cmnd[8] = SCp->cmd_len;
  912. SCp->cmd_len = 6; /* command length for
  913. * REQUEST_SENSE */
  914. slot->pCmd = dma_map_single(hostdata->dev, cmnd, MAX_COMMAND_SIZE, DMA_TO_DEVICE);
  915. slot->dma_handle = dma_map_single(hostdata->dev, SCp->sense_buffer, SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  916. slot->SG[0].ins = bS_to_host(SCRIPT_MOVE_DATA_IN | SCSI_SENSE_BUFFERSIZE);
  917. slot->SG[0].pAddr = bS_to_host(slot->dma_handle);
  918. slot->SG[1].ins = bS_to_host(SCRIPT_RETURN);
  919. slot->SG[1].pAddr = 0;
  920. slot->resume_offset = hostdata->pScript;
  921. dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG[0])*2, DMA_TO_DEVICE);
  922. dma_cache_sync(hostdata->dev, SCp->sense_buffer, SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  923. /* queue the command for reissue */
  924. slot->state = NCR_700_SLOT_QUEUED;
  925. slot->flags = NCR_700_FLAG_AUTOSENSE;
  926. hostdata->state = NCR_700_HOST_FREE;
  927. hostdata->cmd = NULL;
  928. }
  929. } else {
  930. // Currently rely on the mid layer evaluation
  931. // of the tag queuing capability
  932. //
  933. //if(status_byte(hostdata->status[0]) == GOOD &&
  934. // SCp->cmnd[0] == INQUIRY && SCp->use_sg == 0) {
  935. // /* Piggy back the tag queueing support
  936. // * on this command */
  937. // dma_sync_single_for_cpu(hostdata->dev,
  938. // slot->dma_handle,
  939. // SCp->request_bufflen,
  940. // DMA_FROM_DEVICE);
  941. // if(((char *)SCp->request_buffer)[7] & 0x02) {
  942. // scmd_printk(KERN_INFO, SCp,
  943. // "Enabling Tag Command Queuing\n");
  944. // hostdata->tag_negotiated |= (1<<scmd_id(SCp));
  945. // NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  946. // } else {
  947. // NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  948. // hostdata->tag_negotiated &= ~(1<<scmd_id(SCp));
  949. // }
  950. //}
  951. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  952. }
  953. } else if((dsps & 0xfffff0f0) == A_UNEXPECTED_PHASE) {
  954. __u8 i = (dsps & 0xf00) >> 8;
  955. scmd_printk(KERN_ERR, SCp, "UNEXPECTED PHASE %s (%s)\n",
  956. NCR_700_phase[i],
  957. sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
  958. scmd_printk(KERN_ERR, SCp, " len = %d, cmd =",
  959. SCp->cmd_len);
  960. scsi_print_command(SCp);
  961. NCR_700_internal_bus_reset(host);
  962. } else if((dsps & 0xfffff000) == A_FATAL) {
  963. int i = (dsps & 0xfff);
  964. printk(KERN_ERR "scsi%d: (%d:%d) FATAL ERROR: %s\n",
  965. host->host_no, pun, lun, NCR_700_fatal_messages[i]);
  966. if(dsps == A_FATAL_ILLEGAL_MSG_LENGTH) {
  967. printk(KERN_ERR " msg begins %02x %02x\n",
  968. hostdata->msgin[0], hostdata->msgin[1]);
  969. }
  970. NCR_700_internal_bus_reset(host);
  971. } else if((dsps & 0xfffff0f0) == A_DISCONNECT) {
  972. #ifdef NCR_700_DEBUG
  973. __u8 i = (dsps & 0xf00) >> 8;
  974. printk("scsi%d: (%d:%d), DISCONNECTED (%d) %s\n",
  975. host->host_no, pun, lun,
  976. i, NCR_700_phase[i]);
  977. #endif
  978. save_for_reselection(hostdata, SCp, dsp);
  979. } else if(dsps == A_RESELECTION_IDENTIFIED) {
  980. __u8 lun;
  981. struct NCR_700_command_slot *slot;
  982. __u8 reselection_id = hostdata->reselection_id;
  983. struct scsi_device *SDp;
  984. lun = hostdata->msgin[0] & 0x1f;
  985. hostdata->reselection_id = 0xff;
  986. DEBUG(("scsi%d: (%d:%d) RESELECTED!\n",
  987. host->host_no, reselection_id, lun));
  988. /* clear the reselection indicator */
  989. SDp = __scsi_device_lookup(host, 0, reselection_id, lun);
  990. if(unlikely(SDp == NULL)) {
  991. printk(KERN_ERR "scsi%d: (%d:%d) HAS NO device\n",
  992. host->host_no, reselection_id, lun);
  993. BUG();
  994. }
  995. if(hostdata->msgin[1] == A_SIMPLE_TAG_MSG) {
  996. struct scsi_cmnd *SCp = scsi_find_tag(SDp, hostdata->msgin[2]);
  997. if(unlikely(SCp == NULL)) {
  998. printk(KERN_ERR "scsi%d: (%d:%d) no saved request for tag %d\n",
  999. host->host_no, reselection_id, lun, hostdata->msgin[2]);
  1000. BUG();
  1001. }
  1002. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1003. DDEBUG(KERN_DEBUG, SDp,
  1004. "reselection is tag %d, slot %p(%d)\n",
  1005. hostdata->msgin[2], slot, slot->tag);
  1006. } else {
  1007. struct scsi_cmnd *SCp = scsi_find_tag(SDp, SCSI_NO_TAG);
  1008. if(unlikely(SCp == NULL)) {
  1009. sdev_printk(KERN_ERR, SDp,
  1010. "no saved request for untagged cmd\n");
  1011. BUG();
  1012. }
  1013. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1014. }
  1015. if(slot == NULL) {
  1016. printk(KERN_ERR "scsi%d: (%d:%d) RESELECTED but no saved command (MSG = %02x %02x %02x)!!\n",
  1017. host->host_no, reselection_id, lun,
  1018. hostdata->msgin[0], hostdata->msgin[1],
  1019. hostdata->msgin[2]);
  1020. } else {
  1021. if(hostdata->state != NCR_700_HOST_BUSY)
  1022. printk(KERN_ERR "scsi%d: FATAL, host not busy during valid reselection!\n",
  1023. host->host_no);
  1024. resume_offset = slot->resume_offset;
  1025. hostdata->cmd = slot->cmnd;
  1026. /* re-patch for this command */
  1027. script_patch_32_abs(hostdata->dev, hostdata->script,
  1028. CommandAddress, slot->pCmd);
  1029. script_patch_16(hostdata->dev, hostdata->script,
  1030. CommandCount, slot->cmnd->cmd_len);
  1031. script_patch_32_abs(hostdata->dev, hostdata->script,
  1032. SGScriptStartAddress,
  1033. to32bit(&slot->pSG[0].ins));
  1034. /* Note: setting SXFER only works if we're
  1035. * still in the MESSAGE phase, so it is vital
  1036. * that ACK is still asserted when we process
  1037. * the reselection message. The resume offset
  1038. * should therefore always clear ACK */
  1039. NCR_700_writeb(NCR_700_get_SXFER(hostdata->cmd->device),
  1040. host, SXFER_REG);
  1041. dma_cache_sync(hostdata->dev, hostdata->msgin,
  1042. MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  1043. dma_cache_sync(hostdata->dev, hostdata->msgout,
  1044. MSG_ARRAY_SIZE, DMA_TO_DEVICE);
  1045. /* I'm just being paranoid here, the command should
  1046. * already have been flushed from the cache */
  1047. dma_cache_sync(hostdata->dev, slot->cmnd->cmnd,
  1048. slot->cmnd->cmd_len, DMA_TO_DEVICE);
  1049. }
  1050. } else if(dsps == A_RESELECTED_DURING_SELECTION) {
  1051. /* This section is full of debugging code because I've
  1052. * never managed to reach it. I think what happens is
  1053. * that, because the 700 runs with selection
  1054. * interrupts enabled the whole time that we take a
  1055. * selection interrupt before we manage to get to the
  1056. * reselected script interrupt */
  1057. __u8 reselection_id = NCR_700_readb(host, SFBR_REG);
  1058. struct NCR_700_command_slot *slot;
  1059. /* Take out our own ID */
  1060. reselection_id &= ~(1<<host->this_id);
  1061. /* I've never seen this happen, so keep this as a printk rather
  1062. * than a debug */
  1063. printk(KERN_INFO "scsi%d: (%d:%d) RESELECTION DURING SELECTION, dsp=%08x[%04x] state=%d, count=%d\n",
  1064. host->host_no, reselection_id, lun, dsp, dsp - hostdata->pScript, hostdata->state, hostdata->command_slot_count);
  1065. {
  1066. /* FIXME: DEBUGGING CODE */
  1067. __u32 SG = (__u32)bS_to_cpu(hostdata->script[A_SGScriptStartAddress_used[0]]);
  1068. int i;
  1069. for(i=0; i< NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1070. if(SG >= to32bit(&hostdata->slots[i].pSG[0])
  1071. && SG <= to32bit(&hostdata->slots[i].pSG[NCR_700_SG_SEGMENTS]))
  1072. break;
  1073. }
  1074. printk(KERN_INFO "IDENTIFIED SG segment as being %08x in slot %p, cmd %p, slot->resume_offset=%08x\n", SG, &hostdata->slots[i], hostdata->slots[i].cmnd, hostdata->slots[i].resume_offset);
  1075. SCp = hostdata->slots[i].cmnd;
  1076. }
  1077. if(SCp != NULL) {
  1078. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1079. /* change slot from busy to queued to redo command */
  1080. slot->state = NCR_700_SLOT_QUEUED;
  1081. }
  1082. hostdata->cmd = NULL;
  1083. if(reselection_id == 0) {
  1084. if(hostdata->reselection_id == 0xff) {
  1085. printk(KERN_ERR "scsi%d: Invalid reselection during selection!!\n", host->host_no);
  1086. return 0;
  1087. } else {
  1088. printk(KERN_ERR "scsi%d: script reselected and we took a selection interrupt\n",
  1089. host->host_no);
  1090. reselection_id = hostdata->reselection_id;
  1091. }
  1092. } else {
  1093. /* convert to real ID */
  1094. reselection_id = bitmap_to_number(reselection_id);
  1095. }
  1096. hostdata->reselection_id = reselection_id;
  1097. /* just in case we have a stale simple tag message, clear it */
  1098. hostdata->msgin[1] = 0;
  1099. dma_cache_sync(hostdata->dev, hostdata->msgin,
  1100. MSG_ARRAY_SIZE, DMA_BIDIRECTIONAL);
  1101. if(hostdata->tag_negotiated & (1<<reselection_id)) {
  1102. resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
  1103. } else {
  1104. resume_offset = hostdata->pScript + Ent_GetReselectionData;
  1105. }
  1106. } else if(dsps == A_COMPLETED_SELECTION_AS_TARGET) {
  1107. /* we've just disconnected from the bus, do nothing since
  1108. * a return here will re-run the queued command slot
  1109. * that may have been interrupted by the initial selection */
  1110. DEBUG((" SELECTION COMPLETED\n"));
  1111. } else if((dsps & 0xfffff0f0) == A_MSG_IN) {
  1112. resume_offset = process_message(host, hostdata, SCp,
  1113. dsp, dsps);
  1114. } else if((dsps & 0xfffff000) == 0) {
  1115. __u8 i = (dsps & 0xf0) >> 4, j = (dsps & 0xf00) >> 8;
  1116. printk(KERN_ERR "scsi%d: (%d:%d), unhandled script condition %s %s at %04x\n",
  1117. host->host_no, pun, lun, NCR_700_condition[i],
  1118. NCR_700_phase[j], dsp - hostdata->pScript);
  1119. if(SCp != NULL) {
  1120. struct scatterlist *sg;
  1121. scsi_print_command(SCp);
  1122. scsi_for_each_sg(SCp, sg, scsi_sg_count(SCp) + 1, i) {
  1123. printk(KERN_INFO " SG[%d].length = %d, move_insn=%08x, addr %08x\n", i, sg->length, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].ins, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].pAddr);
  1124. }
  1125. }
  1126. NCR_700_internal_bus_reset(host);
  1127. } else if((dsps & 0xfffff000) == A_DEBUG_INTERRUPT) {
  1128. printk(KERN_NOTICE "scsi%d (%d:%d) DEBUG INTERRUPT %d AT %08x[%04x], continuing\n",
  1129. host->host_no, pun, lun, dsps & 0xfff, dsp, dsp - hostdata->pScript);
  1130. resume_offset = dsp;
  1131. } else {
  1132. printk(KERN_ERR "scsi%d: (%d:%d), unidentified script interrupt 0x%x at %04x\n",
  1133. host->host_no, pun, lun, dsps, dsp - hostdata->pScript);
  1134. NCR_700_internal_bus_reset(host);
  1135. }
  1136. return resume_offset;
  1137. }
  1138. /* We run the 53c700 with selection interrupts always enabled. This
  1139. * means that the chip may be selected as soon as the bus frees. On a
  1140. * busy bus, this can be before the scripts engine finishes its
  1141. * processing. Therefore, part of the selection processing has to be
  1142. * to find out what the scripts engine is doing and complete the
  1143. * function if necessary (i.e. process the pending disconnect or save
  1144. * the interrupted initial selection */
  1145. STATIC inline __u32
  1146. process_selection(struct Scsi_Host *host, __u32 dsp)
  1147. {
  1148. __u8 id = 0; /* Squash compiler warning */
  1149. int count = 0;
  1150. __u32 resume_offset = 0;
  1151. struct NCR_700_Host_Parameters *hostdata =
  1152. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1153. struct scsi_cmnd *SCp = hostdata->cmd;
  1154. __u8 sbcl;
  1155. for(count = 0; count < 5; count++) {
  1156. id = NCR_700_readb(host, hostdata->chip710 ?
  1157. CTEST9_REG : SFBR_REG);
  1158. /* Take out our own ID */
  1159. id &= ~(1<<host->this_id);
  1160. if(id != 0)
  1161. break;
  1162. udelay(5);
  1163. }
  1164. sbcl = NCR_700_readb(host, SBCL_REG);
  1165. if((sbcl & SBCL_IO) == 0) {
  1166. /* mark as having been selected rather than reselected */
  1167. id = 0xff;
  1168. } else {
  1169. /* convert to real ID */
  1170. hostdata->reselection_id = id = bitmap_to_number(id);
  1171. DEBUG(("scsi%d: Reselected by %d\n",
  1172. host->host_no, id));
  1173. }
  1174. if(hostdata->state == NCR_700_HOST_BUSY && SCp != NULL) {
  1175. struct NCR_700_command_slot *slot =
  1176. (struct NCR_700_command_slot *)SCp->host_scribble;
  1177. DEBUG((" ID %d WARNING: RESELECTION OF BUSY HOST, saving cmd %p, slot %p, addr %x [%04x], resume %x!\n", id, hostdata->cmd, slot, dsp, dsp - hostdata->pScript, resume_offset));
  1178. switch(dsp - hostdata->pScript) {
  1179. case Ent_Disconnect1:
  1180. case Ent_Disconnect2:
  1181. save_for_reselection(hostdata, SCp, Ent_Disconnect2 + hostdata->pScript);
  1182. break;
  1183. case Ent_Disconnect3:
  1184. case Ent_Disconnect4:
  1185. save_for_reselection(hostdata, SCp, Ent_Disconnect4 + hostdata->pScript);
  1186. break;
  1187. case Ent_Disconnect5:
  1188. case Ent_Disconnect6:
  1189. save_for_reselection(hostdata, SCp, Ent_Disconnect6 + hostdata->pScript);
  1190. break;
  1191. case Ent_Disconnect7:
  1192. case Ent_Disconnect8:
  1193. save_for_reselection(hostdata, SCp, Ent_Disconnect8 + hostdata->pScript);
  1194. break;
  1195. case Ent_Finish1:
  1196. case Ent_Finish2:
  1197. process_script_interrupt(A_GOOD_STATUS_AFTER_STATUS, dsp, SCp, host, hostdata);
  1198. break;
  1199. default:
  1200. slot->state = NCR_700_SLOT_QUEUED;
  1201. break;
  1202. }
  1203. }
  1204. hostdata->state = NCR_700_HOST_BUSY;
  1205. hostdata->cmd = NULL;
  1206. /* clear any stale simple tag message */
  1207. hostdata->msgin[1] = 0;
  1208. dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE,
  1209. DMA_BIDIRECTIONAL);
  1210. if(id == 0xff) {
  1211. /* Selected as target, Ignore */
  1212. resume_offset = hostdata->pScript + Ent_SelectedAsTarget;
  1213. } else if(hostdata->tag_negotiated & (1<<id)) {
  1214. resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
  1215. } else {
  1216. resume_offset = hostdata->pScript + Ent_GetReselectionData;
  1217. }
  1218. return resume_offset;
  1219. }
  1220. static inline void
  1221. NCR_700_clear_fifo(struct Scsi_Host *host) {
  1222. const struct NCR_700_Host_Parameters *hostdata
  1223. = (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1224. if(hostdata->chip710) {
  1225. NCR_700_writeb(CLR_FIFO_710, host, CTEST8_REG);
  1226. } else {
  1227. NCR_700_writeb(CLR_FIFO, host, DFIFO_REG);
  1228. }
  1229. }
  1230. static inline void
  1231. NCR_700_flush_fifo(struct Scsi_Host *host) {
  1232. const struct NCR_700_Host_Parameters *hostdata
  1233. = (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1234. if(hostdata->chip710) {
  1235. NCR_700_writeb(FLUSH_DMA_FIFO_710, host, CTEST8_REG);
  1236. udelay(10);
  1237. NCR_700_writeb(0, host, CTEST8_REG);
  1238. } else {
  1239. NCR_700_writeb(FLUSH_DMA_FIFO, host, DFIFO_REG);
  1240. udelay(10);
  1241. NCR_700_writeb(0, host, DFIFO_REG);
  1242. }
  1243. }
  1244. /* The queue lock with interrupts disabled must be held on entry to
  1245. * this function */
  1246. STATIC int
  1247. NCR_700_start_command(struct scsi_cmnd *SCp)
  1248. {
  1249. struct NCR_700_command_slot *slot =
  1250. (struct NCR_700_command_slot *)SCp->host_scribble;
  1251. struct NCR_700_Host_Parameters *hostdata =
  1252. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1253. __u16 count = 1; /* for IDENTIFY message */
  1254. if(hostdata->state != NCR_700_HOST_FREE) {
  1255. /* keep this inside the lock to close the race window where
  1256. * the running command finishes on another CPU while we don't
  1257. * change the state to queued on this one */
  1258. slot->state = NCR_700_SLOT_QUEUED;
  1259. DEBUG(("scsi%d: host busy, queueing command %p, slot %p\n",
  1260. SCp->device->host->host_no, slot->cmnd, slot));
  1261. return 0;
  1262. }
  1263. hostdata->state = NCR_700_HOST_BUSY;
  1264. hostdata->cmd = SCp;
  1265. slot->state = NCR_700_SLOT_BUSY;
  1266. /* keep interrupts disabled until we have the command correctly
  1267. * set up so we cannot take a selection interrupt */
  1268. hostdata->msgout[0] = NCR_700_identify((SCp->cmnd[0] != REQUEST_SENSE &&
  1269. slot->flags != NCR_700_FLAG_AUTOSENSE),
  1270. SCp->device->lun);
  1271. /* for INQUIRY or REQUEST_SENSE commands, we cannot be sure
  1272. * if the negotiated transfer parameters still hold, so
  1273. * always renegotiate them */
  1274. if(SCp->cmnd[0] == INQUIRY || SCp->cmnd[0] == REQUEST_SENSE ||
  1275. slot->flags == NCR_700_FLAG_AUTOSENSE) {
  1276. NCR_700_clear_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  1277. }
  1278. /* REQUEST_SENSE is asking for contingent I_T_L(_Q) status.
  1279. * If a contingent allegiance condition exists, the device
  1280. * will refuse all tags, so send the request sense as untagged
  1281. * */
  1282. if((hostdata->tag_negotiated & (1<<scmd_id(SCp)))
  1283. && (slot->tag != SCSI_NO_TAG && SCp->cmnd[0] != REQUEST_SENSE &&
  1284. slot->flags != NCR_700_FLAG_AUTOSENSE)) {
  1285. count += scsi_populate_tag_msg(SCp, &hostdata->msgout[count]);
  1286. }
  1287. if(hostdata->fast &&
  1288. NCR_700_is_flag_clear(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC)) {
  1289. count += spi_populate_sync_msg(&hostdata->msgout[count],
  1290. spi_period(SCp->device->sdev_target),
  1291. spi_offset(SCp->device->sdev_target));
  1292. NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1293. }
  1294. script_patch_16(hostdata->dev, hostdata->script, MessageCount, count);
  1295. script_patch_ID(hostdata->dev, hostdata->script,
  1296. Device_ID, 1<<scmd_id(SCp));
  1297. script_patch_32_abs(hostdata->dev, hostdata->script, CommandAddress,
  1298. slot->pCmd);
  1299. script_patch_16(hostdata->dev, hostdata->script, CommandCount,
  1300. SCp->cmd_len);
  1301. /* finally plumb the beginning of the SG list into the script
  1302. * */
  1303. script_patch_32_abs(hostdata->dev, hostdata->script,
  1304. SGScriptStartAddress, to32bit(&slot->pSG[0].ins));
  1305. NCR_700_clear_fifo(SCp->device->host);
  1306. if(slot->resume_offset == 0)
  1307. slot->resume_offset = hostdata->pScript;
  1308. /* now perform all the writebacks and invalidates */
  1309. dma_cache_sync(hostdata->dev, hostdata->msgout, count, DMA_TO_DEVICE);
  1310. dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE,
  1311. DMA_FROM_DEVICE);
  1312. dma_cache_sync(hostdata->dev, SCp->cmnd, SCp->cmd_len, DMA_TO_DEVICE);
  1313. dma_cache_sync(hostdata->dev, hostdata->status, 1, DMA_FROM_DEVICE);
  1314. /* set the synchronous period/offset */
  1315. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  1316. SCp->device->host, SXFER_REG);
  1317. NCR_700_writel(slot->temp, SCp->device->host, TEMP_REG);
  1318. NCR_700_writel(slot->resume_offset, SCp->device->host, DSP_REG);
  1319. return 1;
  1320. }
  1321. irqreturn_t
  1322. NCR_700_intr(int irq, void *dev_id)
  1323. {
  1324. struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
  1325. struct NCR_700_Host_Parameters *hostdata =
  1326. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1327. __u8 istat;
  1328. __u32 resume_offset = 0;
  1329. __u8 pun = 0xff, lun = 0xff;
  1330. unsigned long flags;
  1331. int handled = 0;
  1332. /* Use the host lock to serialise access to the 53c700
  1333. * hardware. Note: In future, we may need to take the queue
  1334. * lock to enter the done routines. When that happens, we
  1335. * need to ensure that for this driver, the host lock and the
  1336. * queue lock point to the same thing. */
  1337. spin_lock_irqsave(host->host_lock, flags);
  1338. if((istat = NCR_700_readb(host, ISTAT_REG))
  1339. & (SCSI_INT_PENDING | DMA_INT_PENDING)) {
  1340. __u32 dsps;
  1341. __u8 sstat0 = 0, dstat = 0;
  1342. __u32 dsp;
  1343. struct scsi_cmnd *SCp = hostdata->cmd;
  1344. enum NCR_700_Host_State state;
  1345. handled = 1;
  1346. state = hostdata->state;
  1347. SCp = hostdata->cmd;
  1348. if(istat & SCSI_INT_PENDING) {
  1349. udelay(10);
  1350. sstat0 = NCR_700_readb(host, SSTAT0_REG);
  1351. }
  1352. if(istat & DMA_INT_PENDING) {
  1353. udelay(10);
  1354. dstat = NCR_700_readb(host, DSTAT_REG);
  1355. }
  1356. dsps = NCR_700_readl(host, DSPS_REG);
  1357. dsp = NCR_700_readl(host, DSP_REG);
  1358. DEBUG(("scsi%d: istat %02x sstat0 %02x dstat %02x dsp %04x[%08x] dsps 0x%x\n",
  1359. host->host_no, istat, sstat0, dstat,
  1360. (dsp - (__u32)(hostdata->pScript))/4,
  1361. dsp, dsps));
  1362. if(SCp != NULL) {
  1363. pun = SCp->device->id;
  1364. lun = SCp->device->lun;
  1365. }
  1366. if(sstat0 & SCSI_RESET_DETECTED) {
  1367. struct scsi_device *SDp;
  1368. int i;
  1369. hostdata->state = NCR_700_HOST_BUSY;
  1370. printk(KERN_ERR "scsi%d: Bus Reset detected, executing command %p, slot %p, dsp %08x[%04x]\n",
  1371. host->host_no, SCp, SCp == NULL ? NULL : SCp->host_scribble, dsp, dsp - hostdata->pScript);
  1372. scsi_report_bus_reset(host, 0);
  1373. /* clear all the negotiated parameters */
  1374. __shost_for_each_device(SDp, host)
  1375. NCR_700_clear_flag(SDp, ~0);
  1376. /* clear all the slots and their pending commands */
  1377. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1378. struct scsi_cmnd *SCp;
  1379. struct NCR_700_command_slot *slot =
  1380. &hostdata->slots[i];
  1381. if(slot->state == NCR_700_SLOT_FREE)
  1382. continue;
  1383. SCp = slot->cmnd;
  1384. printk(KERN_ERR " failing command because of reset, slot %p, cmnd %p\n",
  1385. slot, SCp);
  1386. free_slot(slot, hostdata);
  1387. SCp->host_scribble = NULL;
  1388. NCR_700_set_depth(SCp->device, 0);
  1389. /* NOTE: deadlock potential here: we
  1390. * rely on mid-layer guarantees that
  1391. * scsi_done won't try to issue the
  1392. * command again otherwise we'll
  1393. * deadlock on the
  1394. * hostdata->state_lock */
  1395. SCp->result = DID_RESET << 16;
  1396. SCp->scsi_done(SCp);
  1397. }
  1398. mdelay(25);
  1399. NCR_700_chip_setup(host);
  1400. hostdata->state = NCR_700_HOST_FREE;
  1401. hostdata->cmd = NULL;
  1402. /* signal back if this was an eh induced reset */
  1403. if(hostdata->eh_complete != NULL)
  1404. complete(hostdata->eh_complete);
  1405. goto out_unlock;
  1406. } else if(sstat0 & SELECTION_TIMEOUT) {
  1407. DEBUG(("scsi%d: (%d:%d) selection timeout\n",
  1408. host->host_no, pun, lun));
  1409. NCR_700_scsi_done(hostdata, SCp, DID_NO_CONNECT<<16);
  1410. } else if(sstat0 & PHASE_MISMATCH) {
  1411. struct NCR_700_command_slot *slot = (SCp == NULL) ? NULL :
  1412. (struct NCR_700_command_slot *)SCp->host_scribble;
  1413. if(dsp == Ent_SendMessage + 8 + hostdata->pScript) {
  1414. /* It wants to reply to some part of
  1415. * our message */
  1416. #ifdef NCR_700_DEBUG
  1417. __u32 temp = NCR_700_readl(host, TEMP_REG);
  1418. int count = (hostdata->script[Ent_SendMessage/4] & 0xffffff) - ((NCR_700_readl(host, DBC_REG) & 0xffffff) + NCR_700_data_residual(host));
  1419. printk("scsi%d (%d:%d) PHASE MISMATCH IN SEND MESSAGE %d remain, return %p[%04x], phase %s\n", host->host_no, pun, lun, count, (void *)temp, temp - hostdata->pScript, sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
  1420. #endif
  1421. resume_offset = hostdata->pScript + Ent_SendMessagePhaseMismatch;
  1422. } else if(dsp >= to32bit(&slot->pSG[0].ins) &&
  1423. dsp <= to32bit(&slot->pSG[NCR_700_SG_SEGMENTS].ins)) {
  1424. int data_transfer = NCR_700_readl(host, DBC_REG) & 0xffffff;
  1425. int SGcount = (dsp - to32bit(&slot->pSG[0].ins))/sizeof(struct NCR_700_SG_List);
  1426. int residual = NCR_700_data_residual(host);
  1427. int i;
  1428. #ifdef NCR_700_DEBUG
  1429. __u32 naddr = NCR_700_readl(host, DNAD_REG);
  1430. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x\n",
  1431. host->host_no, pun, lun,
  1432. SGcount, data_transfer);
  1433. scsi_print_command(SCp);
  1434. if(residual) {
  1435. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x, residual %d\n",
  1436. host->host_no, pun, lun,
  1437. SGcount, data_transfer, residual);
  1438. }
  1439. #endif
  1440. data_transfer += residual;
  1441. if(data_transfer != 0) {
  1442. int count;
  1443. __u32 pAddr;
  1444. SGcount--;
  1445. count = (bS_to_cpu(slot->SG[SGcount].ins) & 0x00ffffff);
  1446. DEBUG(("DATA TRANSFER MISMATCH, count = %d, transferred %d\n", count, count-data_transfer));
  1447. slot->SG[SGcount].ins &= bS_to_host(0xff000000);
  1448. slot->SG[SGcount].ins |= bS_to_host(data_transfer);
  1449. pAddr = bS_to_cpu(slot->SG[SGcount].pAddr);
  1450. pAddr += (count - data_transfer);
  1451. #ifdef NCR_700_DEBUG
  1452. if(pAddr != naddr) {
  1453. printk("scsi%d (%d:%d) transfer mismatch pAddr=%lx, naddr=%lx, data_transfer=%d, residual=%d\n", host->host_no, pun, lun, (unsigned long)pAddr, (unsigned long)naddr, data_transfer, residual);
  1454. }
  1455. #endif
  1456. slot->SG[SGcount].pAddr = bS_to_host(pAddr);
  1457. }
  1458. /* set the executed moves to nops */
  1459. for(i=0; i<SGcount; i++) {
  1460. slot->SG[i].ins = bS_to_host(SCRIPT_NOP);
  1461. slot->SG[i].pAddr = 0;
  1462. }
  1463. dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
  1464. /* and pretend we disconnected after
  1465. * the command phase */
  1466. resume_offset = hostdata->pScript + Ent_MsgInDuringData;
  1467. /* make sure all the data is flushed */
  1468. NCR_700_flush_fifo(host);
  1469. } else {
  1470. __u8 sbcl = NCR_700_readb(host, SBCL_REG);
  1471. printk(KERN_ERR "scsi%d: (%d:%d) phase mismatch at %04x, phase %s\n",
  1472. host->host_no, pun, lun, dsp - hostdata->pScript, sbcl_to_string(sbcl));
  1473. NCR_700_internal_bus_reset(host);
  1474. }
  1475. } else if(sstat0 & SCSI_GROSS_ERROR) {
  1476. printk(KERN_ERR "scsi%d: (%d:%d) GROSS ERROR\n",
  1477. host->host_no, pun, lun);
  1478. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1479. } else if(sstat0 & PARITY_ERROR) {
  1480. printk(KERN_ERR "scsi%d: (%d:%d) PARITY ERROR\n",
  1481. host->host_no, pun, lun);
  1482. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1483. } else if(dstat & SCRIPT_INT_RECEIVED) {
  1484. DEBUG(("scsi%d: (%d:%d) ====>SCRIPT INTERRUPT<====\n",
  1485. host->host_no, pun, lun));
  1486. resume_offset = process_script_interrupt(dsps, dsp, SCp, host, hostdata);
  1487. } else if(dstat & (ILGL_INST_DETECTED)) {
  1488. printk(KERN_ERR "scsi%d: (%d:%d) Illegal Instruction detected at 0x%08x[0x%x]!!!\n"
  1489. " Please email James.Bottomley@HansenPartnership.com with the details\n",
  1490. host->host_no, pun, lun,
  1491. dsp, dsp - hostdata->pScript);
  1492. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1493. } else if(dstat & (WATCH_DOG_INTERRUPT|ABORTED)) {
  1494. printk(KERN_ERR "scsi%d: (%d:%d) serious DMA problem, dstat=%02x\n",
  1495. host->host_no, pun, lun, dstat);
  1496. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1497. }
  1498. /* NOTE: selection interrupt processing MUST occur
  1499. * after script interrupt processing to correctly cope
  1500. * with the case where we process a disconnect and
  1501. * then get reselected before we process the
  1502. * disconnection */
  1503. if(sstat0 & SELECTED) {
  1504. /* FIXME: It currently takes at least FOUR
  1505. * interrupts to complete a command that
  1506. * disconnects: one for the disconnect, one
  1507. * for the reselection, one to get the
  1508. * reselection data and one to complete the
  1509. * command. If we guess the reselected
  1510. * command here and prepare it, we only need
  1511. * to get a reselection data interrupt if we
  1512. * guessed wrongly. Since the interrupt
  1513. * overhead is much greater than the command
  1514. * setup, this would be an efficient
  1515. * optimisation particularly as we probably
  1516. * only have one outstanding command on a
  1517. * target most of the time */
  1518. resume_offset = process_selection(host, dsp);
  1519. }
  1520. }
  1521. if(resume_offset) {
  1522. if(hostdata->state != NCR_700_HOST_BUSY) {
  1523. printk(KERN_ERR "scsi%d: Driver error: resume at 0x%08x [0x%04x] with non busy host!\n",
  1524. host->host_no, resume_offset, resume_offset - hostdata->pScript);
  1525. hostdata->state = NCR_700_HOST_BUSY;
  1526. }
  1527. DEBUG(("Attempting to resume at %x\n", resume_offset));
  1528. NCR_700_clear_fifo(host);
  1529. NCR_700_writel(resume_offset, host, DSP_REG);
  1530. }
  1531. /* There is probably a technical no-no about this: If we're a
  1532. * shared interrupt and we got this interrupt because the
  1533. * other device needs servicing not us, we're still going to
  1534. * check our queued commands here---of course, there shouldn't
  1535. * be any outstanding.... */
  1536. if(hostdata->state == NCR_700_HOST_FREE) {
  1537. int i;
  1538. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1539. /* fairness: always run the queue from the last
  1540. * position we left off */
  1541. int j = (i + hostdata->saved_slot_position)
  1542. % NCR_700_COMMAND_SLOTS_PER_HOST;
  1543. if(hostdata->slots[j].state != NCR_700_SLOT_QUEUED)
  1544. continue;
  1545. if(NCR_700_start_command(hostdata->slots[j].cmnd)) {
  1546. DEBUG(("scsi%d: Issuing saved command slot %p, cmd %p\t\n",
  1547. host->host_no, &hostdata->slots[j],
  1548. hostdata->slots[j].cmnd));
  1549. hostdata->saved_slot_position = j + 1;
  1550. }
  1551. break;
  1552. }
  1553. }
  1554. out_unlock:
  1555. spin_unlock_irqrestore(host->host_lock, flags);
  1556. return IRQ_RETVAL(handled);
  1557. }
  1558. STATIC int
  1559. NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
  1560. {
  1561. struct NCR_700_Host_Parameters *hostdata =
  1562. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1563. __u32 move_ins;
  1564. enum dma_data_direction direction;
  1565. struct NCR_700_command_slot *slot;
  1566. if(hostdata->command_slot_count >= NCR_700_COMMAND_SLOTS_PER_HOST) {
  1567. /* We're over our allocation, this should never happen
  1568. * since we report the max allocation to the mid layer */
  1569. printk(KERN_WARNING "scsi%d: Command depth has gone over queue depth\n", SCp->device->host->host_no);
  1570. return 1;
  1571. }
  1572. /* check for untagged commands. We cannot have any outstanding
  1573. * commands if we accept them. Commands could be untagged because:
  1574. *
  1575. * - The tag negotiated bitmap is clear
  1576. * - The blk layer sent and untagged command
  1577. */
  1578. if(NCR_700_get_depth(SCp->device) != 0
  1579. && (!(hostdata->tag_negotiated & (1<<scmd_id(SCp)))
  1580. || !blk_rq_tagged(SCp->request))) {
  1581. CDEBUG(KERN_ERR, SCp, "has non zero depth %d\n",
  1582. NCR_700_get_depth(SCp->device));
  1583. return SCSI_MLQUEUE_DEVICE_BUSY;
  1584. }
  1585. if(NCR_700_get_depth(SCp->device) >= SCp->device->queue_depth) {
  1586. CDEBUG(KERN_ERR, SCp, "has max tag depth %d\n",
  1587. NCR_700_get_depth(SCp->device));
  1588. return SCSI_MLQUEUE_DEVICE_BUSY;
  1589. }
  1590. NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) + 1);
  1591. /* begin the command here */
  1592. /* no need to check for NULL, test for command_slot_count above
  1593. * ensures a slot is free */
  1594. slot = find_empty_slot(hostdata);
  1595. slot->cmnd = SCp;
  1596. SCp->scsi_done = done;
  1597. SCp->host_scribble = (unsigned char *)slot;
  1598. SCp->SCp.ptr = NULL;
  1599. SCp->SCp.buffer = NULL;
  1600. #ifdef NCR_700_DEBUG
  1601. printk("53c700: scsi%d, command ", SCp->device->host->host_no);
  1602. scsi_print_command(SCp);
  1603. #endif
  1604. if(blk_rq_tagged(SCp->request)
  1605. && (hostdata->tag_negotiated &(1<<scmd_id(SCp))) == 0
  1606. && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_START_TAG_NEGOTIATION) {
  1607. scmd_printk(KERN_ERR, SCp, "Enabling Tag Command Queuing\n");
  1608. hostdata->tag_negotiated |= (1<<scmd_id(SCp));
  1609. NCR_700_set_tag_neg_state(SCp->device, NCR_700_DURING_TAG_NEGOTIATION);
  1610. }
  1611. /* here we may have to process an untagged command. The gate
  1612. * above ensures that this will be the only one outstanding,
  1613. * so clear the tag negotiated bit.
  1614. *
  1615. * FIXME: This will royally screw up on multiple LUN devices
  1616. * */
  1617. if(!blk_rq_tagged(SCp->request)
  1618. && (hostdata->tag_negotiated &(1<<scmd_id(SCp)))) {
  1619. scmd_printk(KERN_INFO, SCp, "Disabling Tag Command Queuing\n");
  1620. hostdata->tag_negotiated &= ~(1<<scmd_id(SCp));
  1621. }
  1622. if((hostdata->tag_negotiated &(1<<scmd_id(SCp)))
  1623. && scsi_get_tag_type(SCp->device)) {
  1624. slot->tag = SCp->request->tag;
  1625. CDEBUG(KERN_DEBUG, SCp, "sending out tag %d, slot %p\n",
  1626. slot->tag, slot);
  1627. } else {
  1628. slot->tag = SCSI_NO_TAG;
  1629. /* must populate current_cmnd for scsi_find_tag to work */
  1630. SCp->device->current_cmnd = SCp;
  1631. }
  1632. /* sanity check: some of the commands generated by the mid-layer
  1633. * have an eccentric idea of their sc_data_direction */
  1634. if(!scsi_sg_count(SCp) && !scsi_bufflen(SCp) &&
  1635. SCp->sc_data_direction != DMA_NONE) {
  1636. #ifdef NCR_700_DEBUG
  1637. printk("53c700: Command");
  1638. scsi_print_command(SCp);
  1639. printk("Has wrong data direction %d\n", SCp->sc_data_direction);
  1640. #endif
  1641. SCp->sc_data_direction = DMA_NONE;
  1642. }
  1643. switch (SCp->cmnd[0]) {
  1644. case REQUEST_SENSE:
  1645. /* clear the internal sense magic */
  1646. SCp->cmnd[6] = 0;
  1647. /* fall through */
  1648. default:
  1649. /* OK, get it from the command */
  1650. switch(SCp->sc_data_direction) {
  1651. case DMA_BIDIRECTIONAL:
  1652. default:
  1653. printk(KERN_ERR "53c700: Unknown command for data direction ");
  1654. scsi_print_command(SCp);
  1655. move_ins = 0;
  1656. break;
  1657. case DMA_NONE:
  1658. move_ins = 0;
  1659. break;
  1660. case DMA_FROM_DEVICE:
  1661. move_ins = SCRIPT_MOVE_DATA_IN;
  1662. break;
  1663. case DMA_TO_DEVICE:
  1664. move_ins = SCRIPT_MOVE_DATA_OUT;
  1665. break;
  1666. }
  1667. }
  1668. /* now build the scatter gather list */
  1669. direction = SCp->sc_data_direction;
  1670. if(move_ins != 0) {
  1671. int i;
  1672. int sg_count;
  1673. dma_addr_t vPtr = 0;
  1674. struct scatterlist *sg;
  1675. __u32 count = 0;
  1676. sg_count = scsi_dma_map(SCp);
  1677. BUG_ON(sg_count < 0);
  1678. scsi_for_each_sg(SCp, sg, sg_count, i) {
  1679. vPtr = sg_dma_address(sg);
  1680. count = sg_dma_len(sg);
  1681. slot->SG[i].ins = bS_to_host(move_ins | count);
  1682. DEBUG((" scatter block %d: move %d[%08x] from 0x%lx\n",
  1683. i, count, slot->SG[i].ins, (unsigned long)vPtr));
  1684. slot->SG[i].pAddr = bS_to_host(vPtr);
  1685. }
  1686. slot->SG[i].ins = bS_to_host(SCRIPT_RETURN);
  1687. slot->SG[i].pAddr = 0;
  1688. dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
  1689. DEBUG((" SETTING %08lx to %x\n",
  1690. (&slot->pSG[i].ins),
  1691. slot->SG[i].ins));
  1692. }
  1693. slot->resume_offset = 0;
  1694. slot->pCmd = dma_map_single(hostdata->dev, SCp->cmnd,
  1695. MAX_COMMAND_SIZE, DMA_TO_DEVICE);
  1696. NCR_700_start_command(SCp);
  1697. return 0;
  1698. }
  1699. STATIC int
  1700. NCR_700_abort(struct scsi_cmnd * SCp)
  1701. {
  1702. struct NCR_700_command_slot *slot;
  1703. scmd_printk(KERN_INFO, SCp,
  1704. "New error handler wants to abort command\n\t");
  1705. scsi_print_command(SCp);
  1706. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1707. if(slot == NULL)
  1708. /* no outstanding command to abort */
  1709. return SUCCESS;
  1710. if(SCp->cmnd[0] == TEST_UNIT_READY) {
  1711. /* FIXME: This is because of a problem in the new
  1712. * error handler. When it is in error recovery, it
  1713. * will send a TUR to a device it thinks may still be
  1714. * showing a problem. If the TUR isn't responded to,
  1715. * it will abort it and mark the device off line.
  1716. * Unfortunately, it does no other error recovery, so
  1717. * this would leave us with an outstanding command
  1718. * occupying a slot. Rather than allow this to
  1719. * happen, we issue a bus reset to force all
  1720. * outstanding commands to terminate here. */
  1721. NCR_700_internal_bus_reset(SCp->device->host);
  1722. /* still drop through and return failed */
  1723. }
  1724. return FAILED;
  1725. }
  1726. STATIC int
  1727. NCR_700_bus_reset(struct scsi_cmnd * SCp)
  1728. {
  1729. DECLARE_COMPLETION_ONSTACK(complete);
  1730. struct NCR_700_Host_Parameters *hostdata =
  1731. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1732. scmd_printk(KERN_INFO, SCp,
  1733. "New error handler wants BUS reset, cmd %p\n\t", SCp);
  1734. scsi_print_command(SCp);
  1735. /* In theory, eh_complete should always be null because the
  1736. * eh is single threaded, but just in case we're handling a
  1737. * reset via sg or something */
  1738. spin_lock_irq(SCp->device->host->host_lock);
  1739. while (hostdata->eh_complete != NULL) {
  1740. spin_unlock_irq(SCp->device->host->host_lock);
  1741. msleep_interruptible(100);
  1742. spin_lock_irq(SCp->device->host->host_lock);
  1743. }
  1744. hostdata->eh_complete = &complete;
  1745. NCR_700_internal_bus_reset(SCp->device->host);
  1746. spin_unlock_irq(SCp->device->host->host_lock);
  1747. wait_for_completion(&complete);
  1748. spin_lock_irq(SCp->device->host->host_lock);
  1749. hostdata->eh_complete = NULL;
  1750. /* Revalidate the transport parameters of the failing device */
  1751. if(hostdata->fast)
  1752. spi_schedule_dv_device(SCp->device);
  1753. spin_unlock_irq(SCp->device->host->host_lock);
  1754. return SUCCESS;
  1755. }
  1756. STATIC int
  1757. NCR_700_host_reset(struct scsi_cmnd * SCp)
  1758. {
  1759. scmd_printk(KERN_INFO, SCp, "New error handler wants HOST reset\n\t");
  1760. scsi_print_command(SCp);
  1761. spin_lock_irq(SCp->device->host->host_lock);
  1762. NCR_700_internal_bus_reset(SCp->device->host);
  1763. NCR_700_chip_reset(SCp->device->host);
  1764. spin_unlock_irq(SCp->device->host->host_lock);
  1765. return SUCCESS;
  1766. }
  1767. STATIC void
  1768. NCR_700_set_period(struct scsi_target *STp, int period)
  1769. {
  1770. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1771. struct NCR_700_Host_Parameters *hostdata =
  1772. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1773. if(!hostdata->fast)
  1774. return;
  1775. if(period < hostdata->min_period)
  1776. period = hostdata->min_period;
  1777. spi_period(STp) = period;
  1778. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1779. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1780. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1781. }
  1782. STATIC void
  1783. NCR_700_set_offset(struct scsi_target *STp, int offset)
  1784. {
  1785. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1786. struct NCR_700_Host_Parameters *hostdata =
  1787. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1788. int max_offset = hostdata->chip710
  1789. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET;
  1790. if(!hostdata->fast)
  1791. return;
  1792. if(offset > max_offset)
  1793. offset = max_offset;
  1794. /* if we're currently async, make sure the period is reasonable */
  1795. if(spi_offset(STp) == 0 && (spi_period(STp) < hostdata->min_period ||
  1796. spi_period(STp) > 0xff))
  1797. spi_period(STp) = hostdata->min_period;
  1798. spi_offset(STp) = offset;
  1799. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1800. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1801. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1802. }
  1803. STATIC int
  1804. NCR_700_slave_alloc(struct scsi_device *SDp)
  1805. {
  1806. SDp->hostdata = kzalloc(sizeof(struct NCR_700_Device_Parameters),
  1807. GFP_KERNEL);
  1808. if (!SDp->hostdata)
  1809. return -ENOMEM;
  1810. return 0;
  1811. }
  1812. STATIC int
  1813. NCR_700_slave_configure(struct scsi_device *SDp)
  1814. {
  1815. struct NCR_700_Host_Parameters *hostdata =
  1816. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1817. /* to do here: allocate memory; build a queue_full list */
  1818. if(SDp->tagged_supported) {
  1819. scsi_set_tag_type(SDp, MSG_ORDERED_TAG);
  1820. scsi_activate_tcq(SDp, NCR_700_DEFAULT_TAGS);
  1821. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1822. } else {
  1823. /* initialise to default depth */
  1824. scsi_adjust_queue_depth(SDp, 0, SDp->host->cmd_per_lun);
  1825. }
  1826. if(hostdata->fast) {
  1827. /* Find the correct offset and period via domain validation */
  1828. if (!spi_initial_dv(SDp->sdev_target))
  1829. spi_dv_device(SDp);
  1830. } else {
  1831. spi_offset(SDp->sdev_target) = 0;
  1832. spi_period(SDp->sdev_target) = 0;
  1833. }
  1834. return 0;
  1835. }
  1836. STATIC void
  1837. NCR_700_slave_destroy(struct scsi_device *SDp)
  1838. {
  1839. kfree(SDp->hostdata);
  1840. SDp->hostdata = NULL;
  1841. }
  1842. static int
  1843. NCR_700_change_queue_depth(struct scsi_device *SDp, int depth, int reason)
  1844. {
  1845. if (reason != SCSI_QDEPTH_DEFAULT)
  1846. return -EOPNOTSUPP;
  1847. if (depth > NCR_700_MAX_TAGS)
  1848. depth = NCR_700_MAX_TAGS;
  1849. scsi_adjust_queue_depth(SDp, scsi_get_tag_type(SDp), depth);
  1850. return depth;
  1851. }
  1852. static int NCR_700_change_queue_type(struct scsi_device *SDp, int tag_type)
  1853. {
  1854. int change_tag = ((tag_type ==0 && scsi_get_tag_type(SDp) != 0)
  1855. || (tag_type != 0 && scsi_get_tag_type(SDp) == 0));
  1856. struct NCR_700_Host_Parameters *hostdata =
  1857. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1858. scsi_set_tag_type(SDp, tag_type);
  1859. /* We have a global (per target) flag to track whether TCQ is
  1860. * enabled, so we'll be turning it off for the entire target here.
  1861. * our tag algorithm will fail if we mix tagged and untagged commands,
  1862. * so quiesce the device before doing this */
  1863. if (change_tag)
  1864. scsi_target_quiesce(SDp->sdev_target);
  1865. if (!tag_type) {
  1866. /* shift back to the default unqueued number of commands
  1867. * (the user can still raise this) */
  1868. scsi_deactivate_tcq(SDp, SDp->host->cmd_per_lun);
  1869. hostdata->tag_negotiated &= ~(1 << sdev_id(SDp));
  1870. } else {
  1871. /* Here, we cleared the negotiation flag above, so this
  1872. * will force the driver to renegotiate */
  1873. scsi_activate_tcq(SDp, SDp->queue_depth);
  1874. if (change_tag)
  1875. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1876. }
  1877. if (change_tag)
  1878. scsi_target_resume(SDp->sdev_target);
  1879. return tag_type;
  1880. }
  1881. static ssize_t
  1882. NCR_700_show_active_tags(struct device *dev, struct device_attribute *attr, char *buf)
  1883. {
  1884. struct scsi_device *SDp = to_scsi_device(dev);
  1885. return snprintf(buf, 20, "%d\n", NCR_700_get_depth(SDp));
  1886. }
  1887. static struct device_attribute NCR_700_active_tags_attr = {
  1888. .attr = {
  1889. .name = "active_tags",
  1890. .mode = S_IRUGO,
  1891. },
  1892. .show = NCR_700_show_active_tags,
  1893. };
  1894. STATIC struct device_attribute *NCR_700_dev_attrs[] = {
  1895. &NCR_700_active_tags_attr,
  1896. NULL,
  1897. };
  1898. EXPORT_SYMBOL(NCR_700_detect);
  1899. EXPORT_SYMBOL(NCR_700_release);
  1900. EXPORT_SYMBOL(NCR_700_intr);
  1901. static struct spi_function_template NCR_700_transport_functions = {
  1902. .set_period = NCR_700_set_period,
  1903. .show_period = 1,
  1904. .set_offset = NCR_700_set_offset,
  1905. .show_offset = 1,
  1906. };
  1907. static int __init NCR_700_init(void)
  1908. {
  1909. NCR_700_transport_template = spi_attach_transport(&NCR_700_transport_functions);
  1910. if(!NCR_700_transport_template)
  1911. return -ENODEV;
  1912. return 0;
  1913. }
  1914. static void __exit NCR_700_exit(void)
  1915. {
  1916. spi_release_transport(NCR_700_transport_template);
  1917. }
  1918. module_init(NCR_700_init);
  1919. module_exit(NCR_700_exit);