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