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