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