Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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