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