1 /* -*- mode: c; c-basic-offset: 8 -*- */
3 /* NCR (or Symbios) 53c700 and 53c700-66 Driver
5 * Copyright (C) 2001 by James.Bottomley@HansenPartnership.com
6 **-----------------------------------------------------------------------------
8 ** This program is free software; you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation; either version 2 of the License, or
11 ** (at your option) any later version.
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **-----------------------------------------------------------------------------
27 * This driver is designed exclusively for these chips (virtually the
28 * earliest of the scripts engine chips). They need their own drivers
29 * because they are missing so many of the scripts and snazzy register
30 * features of their elder brothers (the 710, 720 and 770).
32 * The 700 is the lowliest of the line, it can only do async SCSI.
33 * The 700-66 can at least do synchronous SCSI up to 10MHz.
35 * The 700 chip has no host bus interface logic of its own. However,
36 * it is usually mapped to a location with well defined register
37 * offsets. Therefore, if you can determine the base address and the
38 * irq your board incorporating this chip uses, you can probably use
39 * this driver to run it (although you'll probably have to write a
40 * minimal wrapper for the purpose---see the NCR_D700 driver for
41 * details about how to do this).
46 * 1. Better statistics in the proc fs
48 * 2. Implement message queue (queues SCSI messages like commands) and make
49 * the abort and device reset functions use them.
56 * Fixed bad bug affecting tag starvation processing (previously the
57 * driver would hang the system if too many tags starved. Also fixed
58 * bad bug having to do with 10 byte command processing and REQUEST
59 * SENSE (the command would loop forever getting a transfer length
60 * mismatch in the CMD phase).
64 * Fixed scripts problem which caused certain devices (notably CDRWs)
65 * to hang on initial INQUIRY. Updated NCR_700_readl/writel to use
66 * __raw_readl/writel for parisc compatibility (Thomas
67 * Bogendoerfer). Added missing SCp->request_bufflen initialisation
68 * for sense requests (Ryan Bradetich).
72 * Following test of the 64 bit parisc kernel by Richard Hirst,
73 * several problems have now been corrected. Also adds support for
74 * consistent memory allocation.
78 * More Compatibility changes for 710 (now actually works). Enhanced
79 * support for odd clock speeds which constrain SDTR negotiations.
80 * correct cacheline separation for scsi messages and status for
81 * incoherent architectures. Use of the pci mapping functions on
82 * buffers to begin support for 64 bit drivers.
86 * Added support for the 53c710 chip (in 53c700 emulation mode only---no
87 * special 53c710 instructions or registers are used).
91 * More endianness/cache coherency changes.
93 * Better bad device handling (handles devices lying about tag
94 * queueing support and devices which fail to provide sense data on
95 * contingent allegiance conditions)
97 * Many thanks to Richard Hirst <rhirst@linuxcare.com> for patiently
98 * debugging this driver on the parisc architecture and suggesting
99 * many improvements and bug fixes.
101 * Thanks also go to Linuxcare Inc. for providing several PARISC
102 * machines for me to debug the driver on.
106 * Made the driver mem or io mapped; added endian invariance; added
107 * dma cache flushing operations for architectures which need it;
108 * added support for more varied clocking speeds.
112 * Initial modularisation from the D700. See NCR_D700.c for the rest of
115 #define NCR_700_VERSION "2.8"
117 #include <linux/config.h>
118 #include <linux/kernel.h>
119 #include <linux/types.h>
120 #include <linux/string.h>
121 #include <linux/ioport.h>
122 #include <linux/delay.h>
123 #include <linux/spinlock.h>
124 #include <linux/completion.h>
125 #include <linux/sched.h>
126 #include <linux/init.h>
127 #include <linux/proc_fs.h>
129 #include <asm/system.h>
131 #include <asm/pgtable.h>
132 #include <asm/byteorder.h>
133 #include <linux/blkdev.h>
134 #include <linux/module.h>
135 #include <linux/interrupt.h>
140 #include <scsi/scsi_transport.h>
141 #include <scsi/scsi_transport_spi.h>
145 /* NOTE: For 64 bit drivers there are points in the code where we use
146 * a non dereferenceable pointer to point to a structure in dma-able
147 * memory (which is 32 bits) so that we can use all of the structure
148 * operations but take the address at the end. This macro allows us
149 * to truncate the 64 bit pointer down to 32 bits without the compiler
151 #define to32bit(x) ((__u32)((unsigned long)(x)))
156 #define STATIC static
159 MODULE_AUTHOR("James Bottomley");
160 MODULE_DESCRIPTION("53c700 and 53c700-66 Driver");
161 MODULE_LICENSE("GPL");
163 /* This is the script */
164 #include "53c700_d.h"
167 STATIC int NCR_700_queuecommand(Scsi_Cmnd *, void (*done)(Scsi_Cmnd *));
168 STATIC int NCR_700_abort(Scsi_Cmnd * SCpnt);
169 STATIC int NCR_700_bus_reset(Scsi_Cmnd * SCpnt);
170 STATIC int NCR_700_dev_reset(Scsi_Cmnd * SCpnt);
171 STATIC int NCR_700_host_reset(Scsi_Cmnd * SCpnt);
172 STATIC void NCR_700_chip_setup(struct Scsi_Host *host);
173 STATIC void NCR_700_chip_reset(struct Scsi_Host *host);
174 STATIC int NCR_700_slave_configure(Scsi_Device *SDpnt);
175 STATIC void NCR_700_slave_destroy(Scsi_Device *SDpnt);
177 STATIC struct device_attribute *NCR_700_dev_attrs[];
179 STATIC struct scsi_transport_template *NCR_700_transport_template = NULL;
181 static char *NCR_700_phase[] = {
184 "before command phase",
185 "after command phase",
186 "after status phase",
187 "after data in phase",
188 "after data out phase",
192 static char *NCR_700_condition[] = {
200 "REJECT_MSG RECEIVED",
201 "DISCONNECT_MSG RECEIVED",
207 static char *NCR_700_fatal_messages[] = {
208 "unexpected message after reselection",
209 "still MSG_OUT after message injection",
210 "not MSG_IN after selection",
211 "Illegal message length received",
214 static char *NCR_700_SBCL_bits[] = {
225 static char *NCR_700_SBCL_to_phase[] = {
236 static __u8 NCR_700_SDTR_msg[] = {
237 0x01, /* Extended message */
238 0x03, /* Extended message Length */
239 0x01, /* SDTR Extended message */
244 /* This translates the SDTR message offset and period to a value
245 * which can be loaded into the SXFER_REG.
247 * NOTE: According to SCSI-2, the true transfer period (in ns) is
248 * actually four times this period value */
250 NCR_700_offset_period_to_sxfer(struct NCR_700_Host_Parameters *hostdata,
251 __u8 offset, __u8 period)
255 __u8 min_xferp = (hostdata->chip710
256 ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
257 __u8 max_offset = (hostdata->chip710
258 ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET);
263 if(period < hostdata->min_period) {
264 printk(KERN_WARNING "53c700: Period %dns is less than this chip's minimum, setting to %d\n", period*4, NCR_700_SDTR_msg[3]*4);
265 period = hostdata->min_period;
267 XFERP = (period*4 * hostdata->sync_clock)/1000 - 4;
268 if(offset > max_offset) {
269 printk(KERN_WARNING "53c700: Offset %d exceeds chip maximum, setting to %d\n",
273 if(XFERP < min_xferp) {
274 printk(KERN_WARNING "53c700: XFERP %d is less than minium, setting to %d\n",
278 return (offset & 0x0f) | (XFERP & 0x07)<<4;
282 NCR_700_get_SXFER(Scsi_Device *SDp)
284 struct NCR_700_Host_Parameters *hostdata =
285 (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
287 return NCR_700_offset_period_to_sxfer(hostdata, spi_offset(SDp),
292 NCR_700_detect(Scsi_Host_Template *tpnt,
293 struct NCR_700_Host_Parameters *hostdata)
295 dma_addr_t pScript, pSlots;
298 struct Scsi_Host *host;
299 static int banner = 0;
302 if(tpnt->sdev_attrs == NULL)
303 tpnt->sdev_attrs = NCR_700_dev_attrs;
305 memory = dma_alloc_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
306 &pScript, GFP_KERNEL);
308 printk(KERN_ERR "53c700: Failed to allocate memory for driver, detatching\n");
312 script = (__u32 *)memory;
313 hostdata->msgin = memory + MSGIN_OFFSET;
314 hostdata->msgout = memory + MSGOUT_OFFSET;
315 hostdata->status = memory + STATUS_OFFSET;
316 /* all of these offsets are L1_CACHE_BYTES separated. It is fatal
317 * if this isn't sufficient separation to avoid dma flushing issues */
318 BUG_ON(!dma_is_consistent(pScript) && L1_CACHE_BYTES < dma_get_cache_alignment());
319 hostdata->slots = (struct NCR_700_command_slot *)(memory + SLOTS_OFFSET);
321 pSlots = pScript + SLOTS_OFFSET;
323 /* Fill in the missing routines from the host template */
324 tpnt->queuecommand = NCR_700_queuecommand;
325 tpnt->eh_abort_handler = NCR_700_abort;
326 tpnt->eh_device_reset_handler = NCR_700_dev_reset;
327 tpnt->eh_bus_reset_handler = NCR_700_bus_reset;
328 tpnt->eh_host_reset_handler = NCR_700_host_reset;
329 tpnt->can_queue = NCR_700_COMMAND_SLOTS_PER_HOST;
330 tpnt->sg_tablesize = NCR_700_SG_SEGMENTS;
331 tpnt->cmd_per_lun = NCR_700_CMD_PER_LUN;
332 tpnt->use_clustering = DISABLE_CLUSTERING;
333 tpnt->slave_configure = NCR_700_slave_configure;
334 tpnt->slave_destroy = NCR_700_slave_destroy;
336 if(tpnt->name == NULL)
337 tpnt->name = "53c700";
338 if(tpnt->proc_name == NULL)
339 tpnt->proc_name = "53c700";
342 host = scsi_host_alloc(tpnt, 4);
345 memset(hostdata->slots, 0, sizeof(struct NCR_700_command_slot)
346 * NCR_700_COMMAND_SLOTS_PER_HOST);
347 for(j = 0; j < NCR_700_COMMAND_SLOTS_PER_HOST; j++) {
348 dma_addr_t offset = (dma_addr_t)((unsigned long)&hostdata->slots[j].SG[0]
349 - (unsigned long)&hostdata->slots[0].SG[0]);
350 hostdata->slots[j].pSG = (struct NCR_700_SG_List *)((unsigned long)(pSlots + offset));
352 hostdata->free_list = &hostdata->slots[j];
354 hostdata->slots[j-1].ITL_forw = &hostdata->slots[j];
355 hostdata->slots[j].state = NCR_700_SLOT_FREE;
358 for(j = 0; j < sizeof(SCRIPT)/sizeof(SCRIPT[0]); j++) {
359 script[j] = bS_to_host(SCRIPT[j]);
362 /* adjust all labels to be bus physical */
363 for(j = 0; j < PATCHES; j++) {
364 script[LABELPATCHES[j]] = bS_to_host(pScript + SCRIPT[LABELPATCHES[j]]);
366 /* now patch up fixed addresses. */
367 script_patch_32(script, MessageLocation,
368 pScript + MSGOUT_OFFSET);
369 script_patch_32(script, StatusAddress,
370 pScript + STATUS_OFFSET);
371 script_patch_32(script, ReceiveMsgAddress,
372 pScript + MSGIN_OFFSET);
374 hostdata->script = script;
375 hostdata->pScript = pScript;
376 dma_sync_single_for_device(hostdata->dev, pScript, sizeof(SCRIPT), DMA_TO_DEVICE);
377 hostdata->state = NCR_700_HOST_FREE;
378 hostdata->cmd = NULL;
380 host->max_lun = NCR_700_MAX_LUNS;
381 BUG_ON(NCR_700_transport_template == NULL);
382 host->transportt = NCR_700_transport_template;
383 host->unique_id = hostdata->base;
384 host->base = hostdata->base;
385 hostdata->eh_complete = NULL;
386 host->hostdata[0] = (unsigned long)hostdata;
388 NCR_700_writeb(0xff, host, CTEST9_REG);
389 if(hostdata->chip710)
390 hostdata->rev = (NCR_700_readb(host, CTEST8_REG)>>4) & 0x0f;
392 hostdata->rev = (NCR_700_readb(host, CTEST7_REG)>>4) & 0x0f;
393 hostdata->fast = (NCR_700_readb(host, CTEST9_REG) == 0);
395 printk(KERN_NOTICE "53c700: Version " NCR_700_VERSION " By James.Bottomley@HansenPartnership.com\n");
398 printk(KERN_NOTICE "scsi%d: %s rev %d %s\n", host->host_no,
399 hostdata->chip710 ? "53c710" :
400 (hostdata->fast ? "53c700-66" : "53c700"),
401 hostdata->rev, hostdata->differential ?
402 "(Differential)" : "");
404 NCR_700_chip_reset(host);
410 NCR_700_release(struct Scsi_Host *host)
412 struct NCR_700_Host_Parameters *hostdata =
413 (struct NCR_700_Host_Parameters *)host->hostdata[0];
415 dma_free_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
416 hostdata->script, hostdata->pScript);
421 NCR_700_identify(int can_disconnect, __u8 lun)
423 return IDENTIFY_BASE |
424 ((can_disconnect) ? 0x40 : 0) |
425 (lun & NCR_700_LUN_MASK);
429 * Function : static int data_residual (Scsi_Host *host)
431 * Purpose : return residual data count of what's in the chip. If you
432 * really want to know what this function is doing, it's almost a
433 * direct transcription of the algorithm described in the 53c710
434 * guide, except that the DBC and DFIFO registers are only 6 bits
437 * Inputs : host - SCSI host */
439 NCR_700_data_residual (struct Scsi_Host *host) {
440 struct NCR_700_Host_Parameters *hostdata =
441 (struct NCR_700_Host_Parameters *)host->hostdata[0];
442 int count, synchronous = 0;
445 if(hostdata->chip710) {
446 count = ((NCR_700_readb(host, DFIFO_REG) & 0x7f) -
447 (NCR_700_readl(host, DBC_REG) & 0x7f)) & 0x7f;
449 count = ((NCR_700_readb(host, DFIFO_REG) & 0x3f) -
450 (NCR_700_readl(host, DBC_REG) & 0x3f)) & 0x3f;
454 synchronous = NCR_700_readb(host, SXFER_REG) & 0x0f;
456 /* get the data direction */
457 ddir = NCR_700_readb(host, CTEST0_REG) & 0x01;
462 count += (NCR_700_readb(host, SSTAT2_REG) & 0xf0) >> 4;
464 if (NCR_700_readb(host, SSTAT1_REG) & SIDL_REG_FULL)
468 __u8 sstat = NCR_700_readb(host, SSTAT1_REG);
469 if (sstat & SODL_REG_FULL)
471 if (synchronous && (sstat & SODR_REG_FULL))
476 printk("RESIDUAL IS %d (ddir %d)\n", count, ddir);
481 /* print out the SCSI wires and corresponding phase from the SBCL register
484 sbcl_to_string(__u8 sbcl)
487 static char ret[256];
492 strcat(ret, NCR_700_SBCL_bits[i]);
494 strcat(ret, NCR_700_SBCL_to_phase[sbcl & 0x07]);
499 bitmap_to_number(__u8 bitmap)
503 for(i=0; i<8 && !(bitmap &(1<<i)); i++)
508 /* Pull a slot off the free list */
509 STATIC struct NCR_700_command_slot *
510 find_empty_slot(struct NCR_700_Host_Parameters *hostdata)
512 struct NCR_700_command_slot *slot = hostdata->free_list;
516 if(hostdata->command_slot_count != NCR_700_COMMAND_SLOTS_PER_HOST)
517 printk(KERN_ERR "SLOTS FULL, but count is %d, should be %d\n", hostdata->command_slot_count, NCR_700_COMMAND_SLOTS_PER_HOST);
521 if(slot->state != NCR_700_SLOT_FREE)
523 printk(KERN_ERR "BUSY SLOT ON FREE LIST!!!\n");
526 hostdata->free_list = slot->ITL_forw;
527 slot->ITL_forw = NULL;
530 /* NOTE: set the state to busy here, not queued, since this
531 * indicates the slot is in use and cannot be run by the IRQ
532 * finish routine. If we cannot queue the command when it
533 * is properly build, we then change to NCR_700_SLOT_QUEUED */
534 slot->state = NCR_700_SLOT_BUSY;
535 hostdata->command_slot_count++;
541 free_slot(struct NCR_700_command_slot *slot,
542 struct NCR_700_Host_Parameters *hostdata)
544 if((slot->state & NCR_700_SLOT_MASK) != NCR_700_SLOT_MAGIC) {
545 printk(KERN_ERR "53c700: SLOT %p is not MAGIC!!!\n", slot);
547 if(slot->state == NCR_700_SLOT_FREE) {
548 printk(KERN_ERR "53c700: SLOT %p is FREE!!!\n", slot);
551 slot->resume_offset = 0;
553 slot->state = NCR_700_SLOT_FREE;
554 slot->ITL_forw = hostdata->free_list;
555 hostdata->free_list = slot;
556 hostdata->command_slot_count--;
560 /* This routine really does very little. The command is indexed on
561 the ITL and (if tagged) the ITLQ lists in _queuecommand */
563 save_for_reselection(struct NCR_700_Host_Parameters *hostdata,
564 Scsi_Cmnd *SCp, __u32 dsp)
566 /* Its just possible that this gets executed twice */
568 struct NCR_700_command_slot *slot =
569 (struct NCR_700_command_slot *)SCp->host_scribble;
571 slot->resume_offset = dsp;
573 hostdata->state = NCR_700_HOST_FREE;
574 hostdata->cmd = NULL;
578 NCR_700_unmap(struct NCR_700_Host_Parameters *hostdata, Scsi_Cmnd *SCp,
579 struct NCR_700_command_slot *slot)
581 if(SCp->sc_data_direction != SCSI_DATA_NONE &&
582 SCp->sc_data_direction != SCSI_DATA_UNKNOWN) {
583 enum dma_data_direction direction = SCp->sc_data_direction;
585 dma_unmap_sg(hostdata->dev, SCp->buffer,
586 SCp->use_sg, direction);
588 dma_unmap_single(hostdata->dev,
590 SCp->request_bufflen,
597 NCR_700_scsi_done(struct NCR_700_Host_Parameters *hostdata,
598 Scsi_Cmnd *SCp, int result)
600 hostdata->state = NCR_700_HOST_FREE;
601 hostdata->cmd = NULL;
604 struct NCR_700_command_slot *slot =
605 (struct NCR_700_command_slot *)SCp->host_scribble;
607 NCR_700_unmap(hostdata, SCp, slot);
608 dma_unmap_single(hostdata->dev, slot->pCmd,
609 sizeof(SCp->cmnd), DMA_TO_DEVICE);
610 if(SCp->cmnd[0] == REQUEST_SENSE && SCp->cmnd[6] == NCR_700_INTERNAL_SENSE_MAGIC) {
612 printk(" ORIGINAL CMD %p RETURNED %d, new return is %d sense is\n",
613 SCp, SCp->cmnd[7], result);
614 print_sense("53c700", SCp);
617 /* restore the old result if the request sense was
620 result = SCp->cmnd[7];
621 /* now restore the original command */
622 memcpy((void *) SCp->cmnd, (void *) SCp->data_cmnd,
623 sizeof(SCp->data_cmnd));
624 SCp->request_buffer = SCp->buffer;
625 SCp->request_bufflen = SCp->bufflen;
626 SCp->use_sg = SCp->old_use_sg;
627 SCp->cmd_len = SCp->old_cmd_len;
628 SCp->sc_data_direction = SCp->sc_old_data_direction;
629 SCp->underflow = SCp->old_underflow;
632 free_slot(slot, hostdata);
634 if(NCR_700_get_depth(SCp->device) == 0 ||
635 NCR_700_get_depth(SCp->device) > SCp->device->queue_depth)
636 printk(KERN_ERR "Invalid depth in NCR_700_scsi_done(): %d\n",
637 NCR_700_get_depth(SCp->device));
638 #endif /* NCR_700_DEBUG */
639 NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) - 1);
641 SCp->host_scribble = NULL;
642 SCp->result = result;
645 printk(KERN_ERR "53c700: SCSI DONE HAS NULL SCp\n");
651 NCR_700_internal_bus_reset(struct Scsi_Host *host)
654 NCR_700_writeb(ASSERT_RST, host, SCNTL1_REG);
656 NCR_700_writeb(0, host, SCNTL1_REG);
661 NCR_700_chip_setup(struct Scsi_Host *host)
663 struct NCR_700_Host_Parameters *hostdata =
664 (struct NCR_700_Host_Parameters *)host->hostdata[0];
665 __u32 dcntl_extra = 0;
667 __u8 min_xferp = (hostdata->chip710 ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
669 if(hostdata->chip710) {
670 __u8 burst_disable = hostdata->burst_disable
672 dcntl_extra = COMPAT_700_MODE;
674 NCR_700_writeb(dcntl_extra, host, DCNTL_REG);
675 NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
676 host, DMODE_710_REG);
677 NCR_700_writeb(burst_disable | (hostdata->differential ?
678 DIFF : 0), host, CTEST7_REG);
679 NCR_700_writeb(BTB_TIMER_DISABLE, host, CTEST0_REG);
680 NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY | PARITY
681 | AUTO_ATN, host, SCNTL0_REG);
683 NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
684 host, DMODE_700_REG);
685 NCR_700_writeb(hostdata->differential ?
686 DIFF : 0, host, CTEST7_REG);
688 /* this is for 700-66, does nothing on 700 */
689 NCR_700_writeb(LAST_DIS_ENBL | ENABLE_ACTIVE_NEGATION
690 | GENERATE_RECEIVE_PARITY, host,
693 NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY
694 | PARITY | AUTO_ATN, host, SCNTL0_REG);
698 NCR_700_writeb(1 << host->this_id, host, SCID_REG);
699 NCR_700_writeb(0, host, SBCL_REG);
700 NCR_700_writeb(ASYNC_OPERATION, host, SXFER_REG);
702 NCR_700_writeb(PHASE_MM_INT | SEL_TIMEOUT_INT | GROSS_ERR_INT | UX_DISC_INT
703 | RST_INT | PAR_ERR_INT | SELECT_INT, host, SIEN_REG);
705 NCR_700_writeb(ABORT_INT | INT_INST_INT | ILGL_INST_INT, host, DIEN_REG);
706 NCR_700_writeb(ENABLE_SELECT, host, SCNTL1_REG);
707 if(hostdata->clock > 75) {
708 printk(KERN_ERR "53c700: Clock speed %dMHz is too high: 75Mhz is the maximum this chip can be driven at\n", hostdata->clock);
709 /* do the best we can, but the async clock will be out
710 * of spec: sync divider 2, async divider 3 */
711 DEBUG(("53c700: sync 2 async 3\n"));
712 NCR_700_writeb(SYNC_DIV_2_0, host, SBCL_REG);
713 NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
714 hostdata->sync_clock = hostdata->clock/2;
715 } else if(hostdata->clock > 50 && hostdata->clock <= 75) {
716 /* sync divider 1.5, async divider 3 */
717 DEBUG(("53c700: sync 1.5 async 3\n"));
718 NCR_700_writeb(SYNC_DIV_1_5, host, SBCL_REG);
719 NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
720 hostdata->sync_clock = hostdata->clock*2;
721 hostdata->sync_clock /= 3;
723 } else if(hostdata->clock > 37 && hostdata->clock <= 50) {
724 /* sync divider 1, async divider 2 */
725 DEBUG(("53c700: sync 1 async 2\n"));
726 NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
727 NCR_700_writeb(ASYNC_DIV_2_0 | dcntl_extra, host, DCNTL_REG);
728 hostdata->sync_clock = hostdata->clock;
729 } else if(hostdata->clock > 25 && hostdata->clock <=37) {
730 /* sync divider 1, async divider 1.5 */
731 DEBUG(("53c700: sync 1 async 1.5\n"));
732 NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
733 NCR_700_writeb(ASYNC_DIV_1_5 | dcntl_extra, host, DCNTL_REG);
734 hostdata->sync_clock = hostdata->clock;
736 DEBUG(("53c700: sync 1 async 1\n"));
737 NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
738 NCR_700_writeb(ASYNC_DIV_1_0 | dcntl_extra, host, DCNTL_REG);
739 /* sync divider 1, async divider 1 */
740 hostdata->sync_clock = hostdata->clock;
742 /* Calculate the actual minimum period that can be supported
743 * by our synchronous clock speed. See the 710 manual for
744 * exact details of this calculation which is based on a
745 * setting of the SXFER register */
746 min_period = 1000*(4+min_xferp)/(4*hostdata->sync_clock);
747 hostdata->min_period = NCR_700_MIN_PERIOD;
748 if(min_period > NCR_700_MIN_PERIOD)
749 hostdata->min_period = min_period;
753 NCR_700_chip_reset(struct Scsi_Host *host)
755 struct NCR_700_Host_Parameters *hostdata =
756 (struct NCR_700_Host_Parameters *)host->hostdata[0];
757 if(hostdata->chip710) {
758 NCR_700_writeb(SOFTWARE_RESET_710, host, ISTAT_REG);
761 NCR_700_writeb(0, host, ISTAT_REG);
763 NCR_700_writeb(SOFTWARE_RESET, host, DCNTL_REG);
766 NCR_700_writeb(0, host, DCNTL_REG);
771 NCR_700_chip_setup(host);
774 /* The heart of the message processing engine is that the instruction
775 * immediately after the INT is the normal case (and so must be CLEAR
776 * ACK). If we want to do something else, we call that routine in
777 * scripts and set temp to be the normal case + 8 (skipping the CLEAR
778 * ACK) so that the routine returns correctly to resume its activity
781 process_extended_message(struct Scsi_Host *host,
782 struct NCR_700_Host_Parameters *hostdata,
783 Scsi_Cmnd *SCp, __u32 dsp, __u32 dsps)
785 __u32 resume_offset = dsp, temp = dsp + 8;
786 __u8 pun = 0xff, lun = 0xff;
789 pun = SCp->device->id;
790 lun = SCp->device->lun;
793 switch(hostdata->msgin[2]) {
795 if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
796 __u8 period = hostdata->msgin[3];
797 __u8 offset = hostdata->msgin[4];
799 if(offset == 0 || period == 0) {
804 if(NCR_700_is_flag_set(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION)) {
805 if(spi_offset(SCp->device) != 0)
806 printk(KERN_INFO "scsi%d: (%d:%d) Synchronous at offset %d, period %dns\n",
807 host->host_no, pun, lun,
810 printk(KERN_INFO "scsi%d: (%d:%d) Asynchronous\n",
811 host->host_no, pun, lun);
812 NCR_700_clear_flag(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION);
815 spi_offset(SCp->device) = offset;
816 spi_period(SCp->device) = period;
819 NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
820 NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
822 NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
826 /* SDTR message out of the blue, reject it */
827 printk(KERN_WARNING "scsi%d Unexpected SDTR msg\n",
829 hostdata->msgout[0] = A_REJECT_MSG;
830 dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
831 script_patch_16(hostdata->script, MessageCount, 1);
832 /* SendMsgOut returns, so set up the return
834 resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
839 printk(KERN_INFO "scsi%d: (%d:%d), Unsolicited WDTR after CMD, Rejecting\n",
840 host->host_no, pun, lun);
841 hostdata->msgout[0] = A_REJECT_MSG;
842 dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
843 script_patch_16(hostdata->script, MessageCount, 1);
844 resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
849 printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
850 host->host_no, pun, lun,
851 NCR_700_phase[(dsps & 0xf00) >> 8]);
852 print_msg(hostdata->msgin);
855 hostdata->msgout[0] = A_REJECT_MSG;
856 dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
857 script_patch_16(hostdata->script, MessageCount, 1);
858 /* SendMsgOut returns, so set up the return
860 resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
862 NCR_700_writel(temp, host, TEMP_REG);
863 return resume_offset;
867 process_message(struct Scsi_Host *host, struct NCR_700_Host_Parameters *hostdata,
868 Scsi_Cmnd *SCp, __u32 dsp, __u32 dsps)
870 /* work out where to return to */
871 __u32 temp = dsp + 8, resume_offset = dsp;
872 __u8 pun = 0xff, lun = 0xff;
875 pun = SCp->device->id;
876 lun = SCp->device->lun;
880 printk("scsi%d (%d:%d): message %s: ", host->host_no, pun, lun,
881 NCR_700_phase[(dsps & 0xf00) >> 8]);
882 print_msg(hostdata->msgin);
886 switch(hostdata->msgin[0]) {
889 resume_offset = process_extended_message(host, hostdata, SCp,
894 if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
895 /* Rejected our sync negotiation attempt */
896 spi_period(SCp->device) = spi_offset(SCp->device) = 0;
897 NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
898 NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
899 } else if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING)) {
900 /* rejected our first simple tag message */
901 printk(KERN_WARNING "scsi%d (%d:%d) Rejected first tag queue attempt, turning off tag queueing\n", host->host_no, pun, lun);
902 NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
903 hostdata->tag_negotiated &= ~(1<<SCp->device->id);
904 SCp->device->tagged_supported = 0;
905 scsi_deactivate_tcq(SCp->device, host->cmd_per_lun);
907 printk(KERN_WARNING "scsi%d (%d:%d) Unexpected REJECT Message %s\n",
908 host->host_no, pun, lun,
909 NCR_700_phase[(dsps & 0xf00) >> 8]);
910 /* however, just ignore it */
914 case A_PARITY_ERROR_MSG:
915 printk(KERN_ERR "scsi%d (%d:%d) Parity Error!\n", host->host_no,
917 NCR_700_internal_bus_reset(host);
919 case A_SIMPLE_TAG_MSG:
920 printk(KERN_INFO "scsi%d (%d:%d) SIMPLE TAG %d %s\n", host->host_no,
921 pun, lun, hostdata->msgin[1],
922 NCR_700_phase[(dsps & 0xf00) >> 8]);
926 printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
927 host->host_no, pun, lun,
928 NCR_700_phase[(dsps & 0xf00) >> 8]);
930 print_msg(hostdata->msgin);
933 hostdata->msgout[0] = A_REJECT_MSG;
934 dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
935 script_patch_16(hostdata->script, MessageCount, 1);
936 /* SendMsgOut returns, so set up the return
938 resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
942 NCR_700_writel(temp, host, TEMP_REG);
943 /* set us up to receive another message */
944 dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
945 return resume_offset;
949 process_script_interrupt(__u32 dsps, __u32 dsp, Scsi_Cmnd *SCp,
950 struct Scsi_Host *host,
951 struct NCR_700_Host_Parameters *hostdata)
953 __u32 resume_offset = 0;
954 __u8 pun = 0xff, lun=0xff;
957 pun = SCp->device->id;
958 lun = SCp->device->lun;
961 if(dsps == A_GOOD_STATUS_AFTER_STATUS) {
962 DEBUG((" COMMAND COMPLETE, status=%02x\n",
963 hostdata->status[0]));
964 /* OK, if TCQ still on, we know it works */
965 NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
966 /* check for contingent allegiance contitions */
967 if(status_byte(hostdata->status[0]) == CHECK_CONDITION ||
968 status_byte(hostdata->status[0]) == COMMAND_TERMINATED) {
969 struct NCR_700_command_slot *slot =
970 (struct NCR_700_command_slot *)SCp->host_scribble;
971 if(SCp->cmnd[0] == REQUEST_SENSE) {
972 /* OOPS: bad device, returning another
973 * contingent allegiance condition */
974 printk(KERN_ERR "scsi%d (%d:%d) broken device is looping in contingent allegiance: ignoring\n", host->host_no, pun, lun);
975 NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
978 print_command(SCp->cmnd);
979 printk(" cmd %p has status %d, requesting sense\n",
980 SCp, hostdata->status[0]);
982 /* we can destroy the command here
983 * because the contingent allegiance
984 * condition will cause a retry which
985 * will re-copy the command from the
986 * saved data_cmnd. We also unmap any
987 * data associated with the command
989 NCR_700_unmap(hostdata, SCp, slot);
991 SCp->cmnd[0] = REQUEST_SENSE;
992 SCp->cmnd[1] = (SCp->device->lun & 0x7) << 5;
995 SCp->cmnd[4] = sizeof(SCp->sense_buffer);
998 /* Here's a quiet hack: the
999 * REQUEST_SENSE command is six bytes,
1000 * so store a flag indicating that
1001 * this was an internal sense request
1002 * and the original status at the end
1004 SCp->cmnd[6] = NCR_700_INTERNAL_SENSE_MAGIC;
1005 SCp->cmnd[7] = hostdata->status[0];
1007 SCp->sc_data_direction = SCSI_DATA_READ;
1008 dma_sync_single_for_device(hostdata->dev, slot->pCmd,
1009 SCp->cmd_len, DMA_TO_DEVICE);
1010 SCp->request_bufflen = sizeof(SCp->sense_buffer);
1011 slot->dma_handle = dma_map_single(hostdata->dev, SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
1012 slot->SG[0].ins = bS_to_host(SCRIPT_MOVE_DATA_IN | sizeof(SCp->sense_buffer));
1013 slot->SG[0].pAddr = bS_to_host(slot->dma_handle);
1014 slot->SG[1].ins = bS_to_host(SCRIPT_RETURN);
1015 slot->SG[1].pAddr = 0;
1016 slot->resume_offset = hostdata->pScript;
1017 dma_cache_sync(slot->SG, sizeof(slot->SG[0])*2, DMA_TO_DEVICE);
1018 dma_cache_sync(SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
1020 /* queue the command for reissue */
1021 slot->state = NCR_700_SLOT_QUEUED;
1022 hostdata->state = NCR_700_HOST_FREE;
1023 hostdata->cmd = NULL;
1026 // Currently rely on the mid layer evaluation
1027 // of the tag queuing capability
1029 //if(status_byte(hostdata->status[0]) == GOOD &&
1030 // SCp->cmnd[0] == INQUIRY && SCp->use_sg == 0) {
1031 // /* Piggy back the tag queueing support
1032 // * on this command */
1033 // dma_sync_single_for_cpu(hostdata->dev,
1034 // slot->dma_handle,
1035 // SCp->request_bufflen,
1036 // DMA_FROM_DEVICE);
1037 // if(((char *)SCp->request_buffer)[7] & 0x02) {
1038 // printk(KERN_INFO "scsi%d: (%d:%d) Enabling Tag Command Queuing\n", host->host_no, pun, lun);
1039 // hostdata->tag_negotiated |= (1<<SCp->device->id);
1040 // NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
1042 // NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
1043 // hostdata->tag_negotiated &= ~(1<<SCp->device->id);
1046 NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
1048 } else if((dsps & 0xfffff0f0) == A_UNEXPECTED_PHASE) {
1049 __u8 i = (dsps & 0xf00) >> 8;
1051 printk(KERN_ERR "scsi%d: (%d:%d), UNEXPECTED PHASE %s (%s)\n",
1052 host->host_no, pun, lun,
1054 sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
1055 printk(KERN_ERR " len = %d, cmd =", SCp->cmd_len);
1056 print_command(SCp->cmnd);
1058 NCR_700_internal_bus_reset(host);
1059 } else if((dsps & 0xfffff000) == A_FATAL) {
1060 int i = (dsps & 0xfff);
1062 printk(KERN_ERR "scsi%d: (%d:%d) FATAL ERROR: %s\n",
1063 host->host_no, pun, lun, NCR_700_fatal_messages[i]);
1064 if(dsps == A_FATAL_ILLEGAL_MSG_LENGTH) {
1065 printk(KERN_ERR " msg begins %02x %02x\n",
1066 hostdata->msgin[0], hostdata->msgin[1]);
1068 NCR_700_internal_bus_reset(host);
1069 } else if((dsps & 0xfffff0f0) == A_DISCONNECT) {
1070 #ifdef NCR_700_DEBUG
1071 __u8 i = (dsps & 0xf00) >> 8;
1073 printk("scsi%d: (%d:%d), DISCONNECTED (%d) %s\n",
1074 host->host_no, pun, lun,
1075 i, NCR_700_phase[i]);
1077 save_for_reselection(hostdata, SCp, dsp);
1079 } else if(dsps == A_RESELECTION_IDENTIFIED) {
1081 struct NCR_700_command_slot *slot;
1082 __u8 reselection_id = hostdata->reselection_id;
1085 lun = hostdata->msgin[0] & 0x1f;
1087 hostdata->reselection_id = 0xff;
1088 DEBUG(("scsi%d: (%d:%d) RESELECTED!\n",
1089 host->host_no, reselection_id, lun));
1090 /* clear the reselection indicator */
1091 SDp = __scsi_device_lookup(host, 0, reselection_id, lun);
1092 if(unlikely(SDp == NULL)) {
1093 printk(KERN_ERR "scsi%d: (%d:%d) HAS NO device\n",
1094 host->host_no, reselection_id, lun);
1097 if(hostdata->msgin[1] == A_SIMPLE_TAG_MSG) {
1098 Scsi_Cmnd *SCp = scsi_find_tag(SDp, hostdata->msgin[2]);
1099 if(unlikely(SCp == NULL)) {
1100 printk(KERN_ERR "scsi%d: (%d:%d) no saved request for tag %d\n",
1101 host->host_no, reselection_id, lun, hostdata->msgin[2]);
1105 slot = (struct NCR_700_command_slot *)SCp->host_scribble;
1106 DEBUG(("53c700: %d:%d:%d, reselection is tag %d, slot %p(%d)\n",
1107 host->host_no, SDp->id, SDp->lun,
1108 hostdata->msgin[2], slot, slot->tag));
1110 Scsi_Cmnd *SCp = scsi_find_tag(SDp, SCSI_NO_TAG);
1111 if(unlikely(SCp == NULL)) {
1112 printk(KERN_ERR "scsi%d: (%d:%d) no saved request for untagged cmd\n",
1113 host->host_no, reselection_id, lun);
1116 slot = (struct NCR_700_command_slot *)SCp->host_scribble;
1120 printk(KERN_ERR "scsi%d: (%d:%d) RESELECTED but no saved command (MSG = %02x %02x %02x)!!\n",
1121 host->host_no, reselection_id, lun,
1122 hostdata->msgin[0], hostdata->msgin[1],
1123 hostdata->msgin[2]);
1125 if(hostdata->state != NCR_700_HOST_BUSY)
1126 printk(KERN_ERR "scsi%d: FATAL, host not busy during valid reselection!\n",
1128 resume_offset = slot->resume_offset;
1129 hostdata->cmd = slot->cmnd;
1131 /* re-patch for this command */
1132 script_patch_32_abs(hostdata->script, CommandAddress,
1134 script_patch_16(hostdata->script,
1135 CommandCount, slot->cmnd->cmd_len);
1136 script_patch_32_abs(hostdata->script, SGScriptStartAddress,
1137 to32bit(&slot->pSG[0].ins));
1139 /* Note: setting SXFER only works if we're
1140 * still in the MESSAGE phase, so it is vital
1141 * that ACK is still asserted when we process
1142 * the reselection message. The resume offset
1143 * should therefore always clear ACK */
1144 NCR_700_writeb(NCR_700_get_SXFER(hostdata->cmd->device),
1146 dma_cache_sync(hostdata->msgin,
1147 MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
1148 dma_cache_sync(hostdata->msgout,
1149 MSG_ARRAY_SIZE, DMA_TO_DEVICE);
1150 /* I'm just being paranoid here, the command should
1151 * already have been flushed from the cache */
1152 dma_cache_sync(slot->cmnd->cmnd,
1153 slot->cmnd->cmd_len, DMA_TO_DEVICE);
1158 } else if(dsps == A_RESELECTED_DURING_SELECTION) {
1160 /* This section is full of debugging code because I've
1161 * never managed to reach it. I think what happens is
1162 * that, because the 700 runs with selection
1163 * interrupts enabled the whole time that we take a
1164 * selection interrupt before we manage to get to the
1165 * reselected script interrupt */
1167 __u8 reselection_id = NCR_700_readb(host, SFBR_REG);
1168 struct NCR_700_command_slot *slot;
1170 /* Take out our own ID */
1171 reselection_id &= ~(1<<host->this_id);
1173 /* I've never seen this happen, so keep this as a printk rather
1175 printk(KERN_INFO "scsi%d: (%d:%d) RESELECTION DURING SELECTION, dsp=%08x[%04x] state=%d, count=%d\n",
1176 host->host_no, reselection_id, lun, dsp, dsp - hostdata->pScript, hostdata->state, hostdata->command_slot_count);
1179 /* FIXME: DEBUGGING CODE */
1180 __u32 SG = (__u32)bS_to_cpu(hostdata->script[A_SGScriptStartAddress_used[0]]);
1183 for(i=0; i< NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
1184 if(SG >= to32bit(&hostdata->slots[i].pSG[0])
1185 && SG <= to32bit(&hostdata->slots[i].pSG[NCR_700_SG_SEGMENTS]))
1188 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);
1189 SCp = hostdata->slots[i].cmnd;
1193 slot = (struct NCR_700_command_slot *)SCp->host_scribble;
1194 /* change slot from busy to queued to redo command */
1195 slot->state = NCR_700_SLOT_QUEUED;
1197 hostdata->cmd = NULL;
1199 if(reselection_id == 0) {
1200 if(hostdata->reselection_id == 0xff) {
1201 printk(KERN_ERR "scsi%d: Invalid reselection during selection!!\n", host->host_no);
1204 printk(KERN_ERR "scsi%d: script reselected and we took a selection interrupt\n",
1206 reselection_id = hostdata->reselection_id;
1210 /* convert to real ID */
1211 reselection_id = bitmap_to_number(reselection_id);
1213 hostdata->reselection_id = reselection_id;
1214 /* just in case we have a stale simple tag message, clear it */
1215 hostdata->msgin[1] = 0;
1216 dma_cache_sync(hostdata->msgin,
1217 MSG_ARRAY_SIZE, DMA_BIDIRECTIONAL);
1218 if(hostdata->tag_negotiated & (1<<reselection_id)) {
1219 resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
1221 resume_offset = hostdata->pScript + Ent_GetReselectionData;
1223 } else if(dsps == A_COMPLETED_SELECTION_AS_TARGET) {
1224 /* we've just disconnected from the bus, do nothing since
1225 * a return here will re-run the queued command slot
1226 * that may have been interrupted by the initial selection */
1227 DEBUG((" SELECTION COMPLETED\n"));
1228 } else if((dsps & 0xfffff0f0) == A_MSG_IN) {
1229 resume_offset = process_message(host, hostdata, SCp,
1231 } else if((dsps & 0xfffff000) == 0) {
1232 __u8 i = (dsps & 0xf0) >> 4, j = (dsps & 0xf00) >> 8;
1233 printk(KERN_ERR "scsi%d: (%d:%d), unhandled script condition %s %s at %04x\n",
1234 host->host_no, pun, lun, NCR_700_condition[i],
1235 NCR_700_phase[j], dsp - hostdata->pScript);
1237 print_command(SCp->cmnd);
1240 for(i = 0; i < SCp->use_sg + 1; i++) {
1241 printk(KERN_INFO " SG[%d].length = %d, move_insn=%08x, addr %08x\n", i, ((struct scatterlist *)SCp->buffer)[i].length, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].ins, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].pAddr);
1245 NCR_700_internal_bus_reset(host);
1246 } else if((dsps & 0xfffff000) == A_DEBUG_INTERRUPT) {
1247 printk(KERN_NOTICE "scsi%d (%d:%d) DEBUG INTERRUPT %d AT %08x[%04x], continuing\n",
1248 host->host_no, pun, lun, dsps & 0xfff, dsp, dsp - hostdata->pScript);
1249 resume_offset = dsp;
1251 printk(KERN_ERR "scsi%d: (%d:%d), unidentified script interrupt 0x%x at %04x\n",
1252 host->host_no, pun, lun, dsps, dsp - hostdata->pScript);
1253 NCR_700_internal_bus_reset(host);
1255 return resume_offset;
1258 /* We run the 53c700 with selection interrupts always enabled. This
1259 * means that the chip may be selected as soon as the bus frees. On a
1260 * busy bus, this can be before the scripts engine finishes its
1261 * processing. Therefore, part of the selection processing has to be
1262 * to find out what the scripts engine is doing and complete the
1263 * function if necessary (i.e. process the pending disconnect or save
1264 * the interrupted initial selection */
1266 process_selection(struct Scsi_Host *host, __u32 dsp)
1268 __u8 id = 0; /* Squash compiler warning */
1270 __u32 resume_offset = 0;
1271 struct NCR_700_Host_Parameters *hostdata =
1272 (struct NCR_700_Host_Parameters *)host->hostdata[0];
1273 Scsi_Cmnd *SCp = hostdata->cmd;
1276 for(count = 0; count < 5; count++) {
1277 id = NCR_700_readb(host, hostdata->chip710 ?
1278 CTEST9_REG : SFBR_REG);
1280 /* Take out our own ID */
1281 id &= ~(1<<host->this_id);
1286 sbcl = NCR_700_readb(host, SBCL_REG);
1287 if((sbcl & SBCL_IO) == 0) {
1288 /* mark as having been selected rather than reselected */
1291 /* convert to real ID */
1292 hostdata->reselection_id = id = bitmap_to_number(id);
1293 DEBUG(("scsi%d: Reselected by %d\n",
1294 host->host_no, id));
1296 if(hostdata->state == NCR_700_HOST_BUSY && SCp != NULL) {
1297 struct NCR_700_command_slot *slot =
1298 (struct NCR_700_command_slot *)SCp->host_scribble;
1299 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));
1301 switch(dsp - hostdata->pScript) {
1302 case Ent_Disconnect1:
1303 case Ent_Disconnect2:
1304 save_for_reselection(hostdata, SCp, Ent_Disconnect2 + hostdata->pScript);
1306 case Ent_Disconnect3:
1307 case Ent_Disconnect4:
1308 save_for_reselection(hostdata, SCp, Ent_Disconnect4 + hostdata->pScript);
1310 case Ent_Disconnect5:
1311 case Ent_Disconnect6:
1312 save_for_reselection(hostdata, SCp, Ent_Disconnect6 + hostdata->pScript);
1314 case Ent_Disconnect7:
1315 case Ent_Disconnect8:
1316 save_for_reselection(hostdata, SCp, Ent_Disconnect8 + hostdata->pScript);
1320 process_script_interrupt(A_GOOD_STATUS_AFTER_STATUS, dsp, SCp, host, hostdata);
1324 slot->state = NCR_700_SLOT_QUEUED;
1328 hostdata->state = NCR_700_HOST_BUSY;
1329 hostdata->cmd = NULL;
1330 /* clear any stale simple tag message */
1331 hostdata->msgin[1] = 0;
1332 dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
1336 /* Selected as target, Ignore */
1337 resume_offset = hostdata->pScript + Ent_SelectedAsTarget;
1338 } else if(hostdata->tag_negotiated & (1<<id)) {
1339 resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
1341 resume_offset = hostdata->pScript + Ent_GetReselectionData;
1343 return resume_offset;
1347 NCR_700_clear_fifo(struct Scsi_Host *host) {
1348 const struct NCR_700_Host_Parameters *hostdata
1349 = (struct NCR_700_Host_Parameters *)host->hostdata[0];
1350 if(hostdata->chip710) {
1351 NCR_700_writeb(CLR_FIFO_710, host, CTEST8_REG);
1353 NCR_700_writeb(CLR_FIFO, host, DFIFO_REG);
1358 NCR_700_flush_fifo(struct Scsi_Host *host) {
1359 const struct NCR_700_Host_Parameters *hostdata
1360 = (struct NCR_700_Host_Parameters *)host->hostdata[0];
1361 if(hostdata->chip710) {
1362 NCR_700_writeb(FLUSH_DMA_FIFO_710, host, CTEST8_REG);
1364 NCR_700_writeb(0, host, CTEST8_REG);
1366 NCR_700_writeb(FLUSH_DMA_FIFO, host, DFIFO_REG);
1368 NCR_700_writeb(0, host, DFIFO_REG);
1373 /* The queue lock with interrupts disabled must be held on entry to
1376 NCR_700_start_command(Scsi_Cmnd *SCp)
1378 struct NCR_700_command_slot *slot =
1379 (struct NCR_700_command_slot *)SCp->host_scribble;
1380 struct NCR_700_Host_Parameters *hostdata =
1381 (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
1382 __u16 count = 1; /* for IDENTIFY message */
1384 if(hostdata->state != NCR_700_HOST_FREE) {
1385 /* keep this inside the lock to close the race window where
1386 * the running command finishes on another CPU while we don't
1387 * change the state to queued on this one */
1388 slot->state = NCR_700_SLOT_QUEUED;
1390 DEBUG(("scsi%d: host busy, queueing command %p, slot %p\n",
1391 SCp->device->host->host_no, slot->cmnd, slot));
1394 hostdata->state = NCR_700_HOST_BUSY;
1395 hostdata->cmd = SCp;
1396 slot->state = NCR_700_SLOT_BUSY;
1397 /* keep interrupts disabled until we have the command correctly
1398 * set up so we cannot take a selection interrupt */
1400 hostdata->msgout[0] = NCR_700_identify(SCp->cmnd[0] != REQUEST_SENSE,
1402 /* for INQUIRY or REQUEST_SENSE commands, we cannot be sure
1403 * if the negotiated transfer parameters still hold, so
1404 * always renegotiate them */
1405 if(SCp->cmnd[0] == INQUIRY || SCp->cmnd[0] == REQUEST_SENSE) {
1406 NCR_700_clear_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
1409 /* REQUEST_SENSE is asking for contingent I_T_L(_Q) status.
1410 * If a contingent allegiance condition exists, the device
1411 * will refuse all tags, so send the request sense as untagged
1413 if((hostdata->tag_negotiated & (1<<SCp->device->id))
1414 && (slot->tag != SCSI_NO_TAG && SCp->cmnd[0] != REQUEST_SENSE)) {
1415 count += scsi_populate_tag_msg(SCp, &hostdata->msgout[count]);
1418 if(hostdata->fast &&
1419 NCR_700_is_flag_clear(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC)) {
1420 memcpy(&hostdata->msgout[count], NCR_700_SDTR_msg,
1421 sizeof(NCR_700_SDTR_msg));
1422 hostdata->msgout[count+3] = spi_period(SCp->device);
1423 hostdata->msgout[count+4] = spi_offset(SCp->device);
1424 count += sizeof(NCR_700_SDTR_msg);
1425 NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
1428 script_patch_16(hostdata->script, MessageCount, count);
1431 script_patch_ID(hostdata->script,
1432 Device_ID, 1<<SCp->device->id);
1434 script_patch_32_abs(hostdata->script, CommandAddress,
1436 script_patch_16(hostdata->script, CommandCount, SCp->cmd_len);
1437 /* finally plumb the beginning of the SG list into the script
1439 script_patch_32_abs(hostdata->script, SGScriptStartAddress,
1440 to32bit(&slot->pSG[0].ins));
1441 NCR_700_clear_fifo(SCp->device->host);
1443 if(slot->resume_offset == 0)
1444 slot->resume_offset = hostdata->pScript;
1445 /* now perform all the writebacks and invalidates */
1446 dma_cache_sync(hostdata->msgout, count, DMA_TO_DEVICE);
1447 dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
1449 dma_cache_sync(SCp->cmnd, SCp->cmd_len, DMA_TO_DEVICE);
1450 dma_cache_sync(hostdata->status, 1, DMA_FROM_DEVICE);
1452 /* set the synchronous period/offset */
1453 NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
1454 SCp->device->host, SXFER_REG);
1455 NCR_700_writel(slot->temp, SCp->device->host, TEMP_REG);
1456 NCR_700_writel(slot->resume_offset, SCp->device->host, DSP_REG);
1462 NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
1464 struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
1465 struct NCR_700_Host_Parameters *hostdata =
1466 (struct NCR_700_Host_Parameters *)host->hostdata[0];
1468 __u32 resume_offset = 0;
1469 __u8 pun = 0xff, lun = 0xff;
1470 unsigned long flags;
1473 /* Use the host lock to serialise acess to the 53c700
1474 * hardware. Note: In future, we may need to take the queue
1475 * lock to enter the done routines. When that happens, we
1476 * need to ensure that for this driver, the host lock and the
1477 * queue lock point to the same thing. */
1478 spin_lock_irqsave(host->host_lock, flags);
1479 if((istat = NCR_700_readb(host, ISTAT_REG))
1480 & (SCSI_INT_PENDING | DMA_INT_PENDING)) {
1482 __u8 sstat0 = 0, dstat = 0;
1484 Scsi_Cmnd *SCp = hostdata->cmd;
1485 enum NCR_700_Host_State state;
1488 state = hostdata->state;
1489 SCp = hostdata->cmd;
1491 if(istat & SCSI_INT_PENDING) {
1494 sstat0 = NCR_700_readb(host, SSTAT0_REG);
1497 if(istat & DMA_INT_PENDING) {
1500 dstat = NCR_700_readb(host, DSTAT_REG);
1503 dsps = NCR_700_readl(host, DSPS_REG);
1504 dsp = NCR_700_readl(host, DSP_REG);
1506 DEBUG(("scsi%d: istat %02x sstat0 %02x dstat %02x dsp %04x[%08x] dsps 0x%x\n",
1507 host->host_no, istat, sstat0, dstat,
1508 (dsp - (__u32)(hostdata->pScript))/4,
1512 pun = SCp->device->id;
1513 lun = SCp->device->lun;
1516 if(sstat0 & SCSI_RESET_DETECTED) {
1520 hostdata->state = NCR_700_HOST_BUSY;
1522 printk(KERN_ERR "scsi%d: Bus Reset detected, executing command %p, slot %p, dsp %08x[%04x]\n",
1523 host->host_no, SCp, SCp == NULL ? NULL : SCp->host_scribble, dsp, dsp - hostdata->pScript);
1525 scsi_report_bus_reset(host, 0);
1527 /* clear all the negotiated parameters */
1528 __shost_for_each_device(SDp, host)
1531 /* clear all the slots and their pending commands */
1532 for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
1534 struct NCR_700_command_slot *slot =
1535 &hostdata->slots[i];
1537 if(slot->state == NCR_700_SLOT_FREE)
1541 printk(KERN_ERR " failing command because of reset, slot %p, cmnd %p\n",
1543 free_slot(slot, hostdata);
1544 SCp->host_scribble = NULL;
1545 NCR_700_set_depth(SCp->device, 0);
1546 /* NOTE: deadlock potential here: we
1547 * rely on mid-layer guarantees that
1548 * scsi_done won't try to issue the
1549 * command again otherwise we'll
1551 * hostdata->state_lock */
1552 SCp->result = DID_RESET << 16;
1553 SCp->scsi_done(SCp);
1556 NCR_700_chip_setup(host);
1558 hostdata->state = NCR_700_HOST_FREE;
1559 hostdata->cmd = NULL;
1560 /* signal back if this was an eh induced reset */
1561 if(hostdata->eh_complete != NULL)
1562 complete(hostdata->eh_complete);
1564 } else if(sstat0 & SELECTION_TIMEOUT) {
1565 DEBUG(("scsi%d: (%d:%d) selection timeout\n",
1566 host->host_no, pun, lun));
1567 NCR_700_scsi_done(hostdata, SCp, DID_NO_CONNECT<<16);
1568 } else if(sstat0 & PHASE_MISMATCH) {
1569 struct NCR_700_command_slot *slot = (SCp == NULL) ? NULL :
1570 (struct NCR_700_command_slot *)SCp->host_scribble;
1572 if(dsp == Ent_SendMessage + 8 + hostdata->pScript) {
1573 /* It wants to reply to some part of
1575 #ifdef NCR_700_DEBUG
1576 __u32 temp = NCR_700_readl(host, TEMP_REG);
1577 int count = (hostdata->script[Ent_SendMessage/4] & 0xffffff) - ((NCR_700_readl(host, DBC_REG) & 0xffffff) + NCR_700_data_residual(host));
1578 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)));
1580 resume_offset = hostdata->pScript + Ent_SendMessagePhaseMismatch;
1581 } else if(dsp >= to32bit(&slot->pSG[0].ins) &&
1582 dsp <= to32bit(&slot->pSG[NCR_700_SG_SEGMENTS].ins)) {
1583 int data_transfer = NCR_700_readl(host, DBC_REG) & 0xffffff;
1584 int SGcount = (dsp - to32bit(&slot->pSG[0].ins))/sizeof(struct NCR_700_SG_List);
1585 int residual = NCR_700_data_residual(host);
1587 #ifdef NCR_700_DEBUG
1588 __u32 naddr = NCR_700_readl(host, DNAD_REG);
1590 printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x\n",
1591 host->host_no, pun, lun,
1592 SGcount, data_transfer);
1593 print_command(SCp->cmnd);
1595 printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x, residual %d\n",
1596 host->host_no, pun, lun,
1597 SGcount, data_transfer, residual);
1600 data_transfer += residual;
1602 if(data_transfer != 0) {
1608 count = (bS_to_cpu(slot->SG[SGcount].ins) & 0x00ffffff);
1609 DEBUG(("DATA TRANSFER MISMATCH, count = %d, transferred %d\n", count, count-data_transfer));
1610 slot->SG[SGcount].ins &= bS_to_host(0xff000000);
1611 slot->SG[SGcount].ins |= bS_to_host(data_transfer);
1612 pAddr = bS_to_cpu(slot->SG[SGcount].pAddr);
1613 pAddr += (count - data_transfer);
1614 #ifdef NCR_700_DEBUG
1615 if(pAddr != naddr) {
1616 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);
1619 slot->SG[SGcount].pAddr = bS_to_host(pAddr);
1621 /* set the executed moves to nops */
1622 for(i=0; i<SGcount; i++) {
1623 slot->SG[i].ins = bS_to_host(SCRIPT_NOP);
1624 slot->SG[i].pAddr = 0;
1626 dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
1627 /* and pretend we disconnected after
1628 * the command phase */
1629 resume_offset = hostdata->pScript + Ent_MsgInDuringData;
1630 /* make sure all the data is flushed */
1631 NCR_700_flush_fifo(host);
1633 __u8 sbcl = NCR_700_readb(host, SBCL_REG);
1634 printk(KERN_ERR "scsi%d: (%d:%d) phase mismatch at %04x, phase %s\n",
1635 host->host_no, pun, lun, dsp - hostdata->pScript, sbcl_to_string(sbcl));
1636 NCR_700_internal_bus_reset(host);
1639 } else if(sstat0 & SCSI_GROSS_ERROR) {
1640 printk(KERN_ERR "scsi%d: (%d:%d) GROSS ERROR\n",
1641 host->host_no, pun, lun);
1642 NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
1643 } else if(sstat0 & PARITY_ERROR) {
1644 printk(KERN_ERR "scsi%d: (%d:%d) PARITY ERROR\n",
1645 host->host_no, pun, lun);
1646 NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
1647 } else if(dstat & SCRIPT_INT_RECEIVED) {
1648 DEBUG(("scsi%d: (%d:%d) ====>SCRIPT INTERRUPT<====\n",
1649 host->host_no, pun, lun));
1650 resume_offset = process_script_interrupt(dsps, dsp, SCp, host, hostdata);
1651 } else if(dstat & (ILGL_INST_DETECTED)) {
1652 printk(KERN_ERR "scsi%d: (%d:%d) Illegal Instruction detected at 0x%08x[0x%x]!!!\n"
1653 " Please email James.Bottomley@HansenPartnership.com with the details\n",
1654 host->host_no, pun, lun,
1655 dsp, dsp - hostdata->pScript);
1656 NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
1657 } else if(dstat & (WATCH_DOG_INTERRUPT|ABORTED)) {
1658 printk(KERN_ERR "scsi%d: (%d:%d) serious DMA problem, dstat=%02x\n",
1659 host->host_no, pun, lun, dstat);
1660 NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
1664 /* NOTE: selection interrupt processing MUST occur
1665 * after script interrupt processing to correctly cope
1666 * with the case where we process a disconnect and
1667 * then get reselected before we process the
1669 if(sstat0 & SELECTED) {
1670 /* FIXME: It currently takes at least FOUR
1671 * interrupts to complete a command that
1672 * disconnects: one for the disconnect, one
1673 * for the reselection, one to get the
1674 * reselection data and one to complete the
1675 * command. If we guess the reselected
1676 * command here and prepare it, we only need
1677 * to get a reselection data interrupt if we
1678 * guessed wrongly. Since the interrupt
1679 * overhead is much greater than the command
1680 * setup, this would be an efficient
1681 * optimisation particularly as we probably
1682 * only have one outstanding command on a
1683 * target most of the time */
1685 resume_offset = process_selection(host, dsp);
1692 if(hostdata->state != NCR_700_HOST_BUSY) {
1693 printk(KERN_ERR "scsi%d: Driver error: resume at 0x%08x [0x%04x] with non busy host!\n",
1694 host->host_no, resume_offset, resume_offset - hostdata->pScript);
1695 hostdata->state = NCR_700_HOST_BUSY;
1698 DEBUG(("Attempting to resume at %x\n", resume_offset));
1699 NCR_700_clear_fifo(host);
1700 NCR_700_writel(resume_offset, host, DSP_REG);
1702 /* There is probably a technical no-no about this: If we're a
1703 * shared interrupt and we got this interrupt because the
1704 * other device needs servicing not us, we're still going to
1705 * check our queued commands here---of course, there shouldn't
1706 * be any outstanding.... */
1707 if(hostdata->state == NCR_700_HOST_FREE) {
1710 for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
1711 /* fairness: always run the queue from the last
1712 * position we left off */
1713 int j = (i + hostdata->saved_slot_position)
1714 % NCR_700_COMMAND_SLOTS_PER_HOST;
1716 if(hostdata->slots[j].state != NCR_700_SLOT_QUEUED)
1718 if(NCR_700_start_command(hostdata->slots[j].cmnd)) {
1719 DEBUG(("scsi%d: Issuing saved command slot %p, cmd %p\t\n",
1720 host->host_no, &hostdata->slots[j],
1721 hostdata->slots[j].cmnd));
1722 hostdata->saved_slot_position = j + 1;
1729 spin_unlock_irqrestore(host->host_lock, flags);
1730 return IRQ_RETVAL(handled);
1734 NCR_700_queuecommand(Scsi_Cmnd *SCp, void (*done)(Scsi_Cmnd *))
1736 struct NCR_700_Host_Parameters *hostdata =
1737 (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
1739 enum dma_data_direction direction;
1740 struct NCR_700_command_slot *slot;
1742 if(hostdata->command_slot_count >= NCR_700_COMMAND_SLOTS_PER_HOST) {
1743 /* We're over our allocation, this should never happen
1744 * since we report the max allocation to the mid layer */
1745 printk(KERN_WARNING "scsi%d: Command depth has gone over queue depth\n", SCp->device->host->host_no);
1748 /* check for untagged commands. We cannot have any outstanding
1749 * commands if we accept them. Commands could be untagged because:
1751 * - The tag negotiated bitmap is clear
1752 * - The blk layer sent and untagged command
1754 if(NCR_700_get_depth(SCp->device) != 0
1755 && (!(hostdata->tag_negotiated & (1<<SCp->device->id))
1756 || !blk_rq_tagged(SCp->request))) {
1757 DEBUG((KERN_ERR "scsi%d (%d:%d) has non zero depth %d\n",
1758 SCp->device->host->host_no, SCp->device->id, SCp->device->lun,
1759 NCR_700_get_depth(SCp->device)));
1760 return SCSI_MLQUEUE_DEVICE_BUSY;
1762 if(NCR_700_get_depth(SCp->device) >= SCp->device->queue_depth) {
1763 DEBUG((KERN_ERR "scsi%d (%d:%d) has max tag depth %d\n",
1764 SCp->device->host->host_no, SCp->device->id, SCp->device->lun,
1765 NCR_700_get_depth(SCp->device)));
1766 return SCSI_MLQUEUE_DEVICE_BUSY;
1768 NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) + 1);
1770 /* begin the command here */
1771 /* no need to check for NULL, test for command_slot_count above
1772 * ensures a slot is free */
1773 slot = find_empty_slot(hostdata);
1777 SCp->scsi_done = done;
1778 SCp->host_scribble = (unsigned char *)slot;
1779 SCp->SCp.ptr = NULL;
1780 SCp->SCp.buffer = NULL;
1782 #ifdef NCR_700_DEBUG
1783 printk("53c700: scsi%d, command ", SCp->device->host->host_no);
1784 print_command(SCp->cmnd);
1786 if(SCp->device->tagged_supported && !SCp->device->simple_tags
1787 && (hostdata->tag_negotiated &(1<<SCp->device->id)) == 0
1788 && NCR_700_is_flag_clear(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING)) {
1789 /* upper layer has indicated tags are supported. We don't
1790 * necessarily believe it yet.
1792 * NOTE: There is a danger here: the mid layer supports
1793 * tag queuing per LUN. We only support it per PUN because
1794 * of potential reselection issues */
1795 scsi_activate_tcq(SCp->device, NCR_700_DEFAULT_TAGS);
1798 if(blk_rq_tagged(SCp->request)
1799 && (hostdata->tag_negotiated &(1<<SCp->device->id)) == 0) {
1800 printk(KERN_INFO "scsi%d: (%d:%d) Enabling Tag Command Queuing\n", SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
1801 hostdata->tag_negotiated |= (1<<SCp->device->id);
1802 NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
1805 /* here we may have to process an untagged command. The gate
1806 * above ensures that this will be the only one outstanding,
1807 * so clear the tag negotiated bit.
1809 * FIXME: This will royally screw up on multiple LUN devices
1811 if(!blk_rq_tagged(SCp->request)
1812 && (hostdata->tag_negotiated &(1<<SCp->device->id))) {
1813 printk(KERN_INFO "scsi%d: (%d:%d) Disabling Tag Command Queuing\n", SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
1814 hostdata->tag_negotiated &= ~(1<<SCp->device->id);
1817 if((hostdata->tag_negotiated &(1<<SCp->device->id))) {
1818 slot->tag = SCp->request->tag;
1819 DEBUG(("53c700 %d:%d:%d, sending out tag %d, slot %p\n",
1820 SCp->device->host->host_no, SCp->device->id, SCp->device->lun, slot->tag,
1823 slot->tag = SCSI_NO_TAG;
1824 /* must populate current_cmnd for scsi_find_tag to work */
1825 SCp->device->current_cmnd = SCp;
1827 /* sanity check: some of the commands generated by the mid-layer
1828 * have an eccentric idea of their sc_data_direction */
1829 if(!SCp->use_sg && !SCp->request_bufflen
1830 && SCp->sc_data_direction != SCSI_DATA_NONE) {
1831 #ifdef NCR_700_DEBUG
1832 printk("53c700: Command");
1833 print_command(SCp->cmnd);
1834 printk("Has wrong data direction %d\n", SCp->sc_data_direction);
1836 SCp->sc_data_direction = SCSI_DATA_NONE;
1839 switch (SCp->cmnd[0]) {
1841 /* clear the internal sense magic */
1845 /* OK, get it from the command */
1846 switch(SCp->sc_data_direction) {
1847 case SCSI_DATA_UNKNOWN:
1849 printk(KERN_ERR "53c700: Unknown command for data direction ");
1850 print_command(SCp->cmnd);
1854 case SCSI_DATA_NONE:
1857 case SCSI_DATA_READ:
1858 move_ins = SCRIPT_MOVE_DATA_IN;
1860 case SCSI_DATA_WRITE:
1861 move_ins = SCRIPT_MOVE_DATA_OUT;
1866 /* now build the scatter gather list */
1867 direction = SCp->sc_data_direction;
1871 dma_addr_t vPtr = 0;
1875 sg_count = dma_map_sg(hostdata->dev, SCp->buffer,
1876 SCp->use_sg, direction);
1878 vPtr = dma_map_single(hostdata->dev,
1879 SCp->request_buffer,
1880 SCp->request_bufflen,
1882 count = SCp->request_bufflen;
1883 slot->dma_handle = vPtr;
1888 for(i = 0; i < sg_count; i++) {
1891 struct scatterlist *sg = SCp->buffer;
1893 vPtr = sg_dma_address(&sg[i]);
1894 count = sg_dma_len(&sg[i]);
1897 slot->SG[i].ins = bS_to_host(move_ins | count);
1898 DEBUG((" scatter block %d: move %d[%08x] from 0x%lx\n",
1899 i, count, slot->SG[i].ins, (unsigned long)vPtr));
1900 slot->SG[i].pAddr = bS_to_host(vPtr);
1902 slot->SG[i].ins = bS_to_host(SCRIPT_RETURN);
1903 slot->SG[i].pAddr = 0;
1904 dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
1905 DEBUG((" SETTING %08lx to %x\n",
1906 (&slot->pSG[i].ins),
1909 slot->resume_offset = 0;
1910 slot->pCmd = dma_map_single(hostdata->dev, SCp->cmnd,
1911 sizeof(SCp->cmnd), DMA_TO_DEVICE);
1912 NCR_700_start_command(SCp);
1917 NCR_700_abort(Scsi_Cmnd * SCp)
1919 struct NCR_700_command_slot *slot;
1921 printk(KERN_INFO "scsi%d (%d:%d) New error handler wants to abort command\n\t",
1922 SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
1923 print_command(SCp->cmnd);
1925 slot = (struct NCR_700_command_slot *)SCp->host_scribble;
1928 /* no outstanding command to abort */
1930 if(SCp->cmnd[0] == TEST_UNIT_READY) {
1931 /* FIXME: This is because of a problem in the new
1932 * error handler. When it is in error recovery, it
1933 * will send a TUR to a device it thinks may still be
1934 * showing a problem. If the TUR isn't responded to,
1935 * it will abort it and mark the device off line.
1936 * Unfortunately, it does no other error recovery, so
1937 * this would leave us with an outstanding command
1938 * occupying a slot. Rather than allow this to
1939 * happen, we issue a bus reset to force all
1940 * outstanding commands to terminate here. */
1941 NCR_700_internal_bus_reset(SCp->device->host);
1942 /* still drop through and return failed */
1949 NCR_700_bus_reset(Scsi_Cmnd * SCp)
1951 DECLARE_COMPLETION(complete);
1952 struct NCR_700_Host_Parameters *hostdata =
1953 (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
1955 printk(KERN_INFO "scsi%d (%d:%d) New error handler wants BUS reset, cmd %p\n\t",
1956 SCp->device->host->host_no, SCp->device->id, SCp->device->lun, SCp);
1957 print_command(SCp->cmnd);
1958 /* In theory, eh_complete should always be null because the
1959 * eh is single threaded, but just in case we're handling a
1960 * reset via sg or something */
1961 while(hostdata->eh_complete != NULL) {
1962 spin_unlock_irq(SCp->device->host->host_lock);
1963 schedule_timeout(HZ/10);
1964 spin_lock_irq(SCp->device->host->host_lock);
1966 hostdata->eh_complete = &complete;
1967 NCR_700_internal_bus_reset(SCp->device->host);
1968 spin_unlock_irq(SCp->device->host->host_lock);
1969 wait_for_completion(&complete);
1970 spin_lock_irq(SCp->device->host->host_lock);
1971 hostdata->eh_complete = NULL;
1972 /* Revalidate the transport parameters of the failing device */
1974 spi_schedule_dv_device(SCp->device);
1979 NCR_700_dev_reset(Scsi_Cmnd * SCp)
1981 printk(KERN_INFO "scsi%d (%d:%d) New error handler wants device reset\n\t",
1982 SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
1983 print_command(SCp->cmnd);
1989 NCR_700_host_reset(Scsi_Cmnd * SCp)
1991 printk(KERN_INFO "scsi%d (%d:%d) New error handler wants HOST reset\n\t",
1992 SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
1993 print_command(SCp->cmnd);
1995 NCR_700_internal_bus_reset(SCp->device->host);
1996 NCR_700_chip_reset(SCp->device->host);
2001 NCR_700_set_period(struct scsi_device *SDp, int period)
2003 struct NCR_700_Host_Parameters *hostdata =
2004 (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
2009 if(period < hostdata->min_period)
2010 period = hostdata->min_period;
2012 spi_period(SDp) = period;
2013 NCR_700_clear_flag(SDp, NCR_700_DEV_NEGOTIATED_SYNC);
2014 NCR_700_clear_flag(SDp, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
2015 NCR_700_set_flag(SDp, NCR_700_DEV_PRINT_SYNC_NEGOTIATION);
2019 NCR_700_set_offset(struct scsi_device *SDp, int offset)
2021 struct NCR_700_Host_Parameters *hostdata =
2022 (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
2023 int max_offset = hostdata->chip710
2024 ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET;
2029 if(offset > max_offset)
2030 offset = max_offset;
2032 /* if we're currently async, make sure the period is reasonable */
2033 if(spi_offset(SDp) == 0 && (spi_period(SDp) < hostdata->min_period ||
2034 spi_period(SDp) > 0xff))
2035 spi_period(SDp) = hostdata->min_period;
2037 spi_offset(SDp) = offset;
2038 NCR_700_clear_flag(SDp, NCR_700_DEV_NEGOTIATED_SYNC);
2039 NCR_700_clear_flag(SDp, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
2040 NCR_700_set_flag(SDp, NCR_700_DEV_PRINT_SYNC_NEGOTIATION);
2046 NCR_700_slave_configure(Scsi_Device *SDp)
2048 struct NCR_700_Host_Parameters *hostdata =
2049 (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
2051 /* to do here: allocate memory; build a queue_full list */
2052 if(SDp->tagged_supported) {
2053 /* do TCQ stuff here */
2055 /* initialise to default depth */
2056 scsi_adjust_queue_depth(SDp, 0, SDp->host->cmd_per_lun);
2058 if(hostdata->fast) {
2059 /* Find the correct offset and period via domain validation */
2062 spi_offset(SDp) = 0;
2063 spi_period(SDp) = 0;
2069 NCR_700_slave_destroy(Scsi_Device *SDp)
2071 /* to do here: deallocate memory */
2075 NCR_700_store_queue_depth(struct device *dev, const char *buf, size_t count)
2079 struct scsi_device *SDp = to_scsi_device(dev);
2080 depth = simple_strtoul(buf, NULL, 0);
2081 if(depth > NCR_700_MAX_TAGS)
2083 scsi_adjust_queue_depth(SDp, MSG_ORDERED_TAG, depth);
2089 NCR_700_show_active_tags(struct device *dev, char *buf)
2091 struct scsi_device *SDp = to_scsi_device(dev);
2093 return snprintf(buf, 20, "%d\n", NCR_700_get_depth(SDp));
2096 static struct device_attribute NCR_700_queue_depth_attr = {
2098 .name = "queue_depth",
2101 .store = NCR_700_store_queue_depth,
2104 static struct device_attribute NCR_700_active_tags_attr = {
2106 .name = "active_tags",
2109 .show = NCR_700_show_active_tags,
2112 STATIC struct device_attribute *NCR_700_dev_attrs[] = {
2113 &NCR_700_queue_depth_attr,
2114 &NCR_700_active_tags_attr,
2118 EXPORT_SYMBOL(NCR_700_detect);
2119 EXPORT_SYMBOL(NCR_700_release);
2120 EXPORT_SYMBOL(NCR_700_intr);
2122 static struct spi_function_template NCR_700_transport_functions = {
2123 .set_period = NCR_700_set_period,
2125 .set_offset = NCR_700_set_offset,
2129 static int __init NCR_700_init(void)
2131 NCR_700_transport_template = spi_attach_transport(&NCR_700_transport_functions);
2132 if(!NCR_700_transport_template)
2137 static void __exit NCR_700_exit(void)
2139 spi_release_transport(NCR_700_transport_template);
2142 module_init(NCR_700_init);
2143 module_exit(NCR_700_exit);