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