1/* 2 * dc395x.c 3 * 4 * Device Driver for Tekram DC395(U/UW/F), DC315(U) 5 * PCI SCSI Bus Master Host Adapter 6 * (SCSI chip set used Tekram ASIC TRM-S1040) 7 * 8 * Authors: 9 * C.L. Huang <ching@tekram.com.tw> 10 * Erich Chen <erich@tekram.com.tw> 11 * (C) Copyright 1995-1999 Tekram Technology Co., Ltd. 12 * 13 * Kurt Garloff <garloff@suse.de> 14 * (C) 1999-2000 Kurt Garloff 15 * 16 * Oliver Neukum <oliver@neukum.name> 17 * Ali Akcaagac <aliakc@web.de> 18 * Jamie Lenehan <lenehan@twibble.org> 19 * (C) 2003 20 * 21 * License: GNU GPL 22 * 23 ************************************************************************* 24 * 25 * Redistribution and use in source and binary forms, with or without 26 * modification, are permitted provided that the following conditions 27 * are met: 28 * 1. Redistributions of source code must retain the above copyright 29 * notice, this list of conditions and the following disclaimer. 30 * 2. Redistributions in binary form must reproduce the above copyright 31 * notice, this list of conditions and the following disclaimer in the 32 * documentation and/or other materials provided with the distribution. 33 * 3. The name of the author may not be used to endorse or promote products 34 * derived from this software without specific prior written permission. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 37 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 39 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 42 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 43 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 45 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 46 * 47 ************************************************************************ 48 */ 49#include <linux/module.h> 50#include <linux/moduleparam.h> 51#include <linux/delay.h> 52#include <linux/ctype.h> 53#include <linux/blkdev.h> 54#include <linux/interrupt.h> 55#include <linux/init.h> 56#include <linux/spinlock.h> 57#include <linux/pci.h> 58#include <linux/list.h> 59#include <linux/vmalloc.h> 60#include <asm/io.h> 61 62#include <scsi/scsi.h> 63#include <scsi/scsicam.h> /* needed for scsicam_bios_param */ 64#include <scsi/scsi_cmnd.h> 65#include <scsi/scsi_device.h> 66#include <scsi/scsi_host.h> 67 68#include "dc395x.h" 69 70#define DC395X_NAME "dc395x" 71#define DC395X_BANNER "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040" 72#define DC395X_VERSION "v2.05, 2004/03/08" 73 74/*--------------------------------------------------------------------------- 75 Features 76 ---------------------------------------------------------------------------*/ 77/* 78 * Set to disable parts of the driver 79 */ 80/*#define DC395x_NO_DISCONNECT*/ 81/*#define DC395x_NO_TAGQ*/ 82/*#define DC395x_NO_SYNC*/ 83/*#define DC395x_NO_WIDE*/ 84 85/*--------------------------------------------------------------------------- 86 Debugging 87 ---------------------------------------------------------------------------*/ 88/* 89 * Types of debugging that can be enabled and disabled 90 */ 91#define DBG_KG 0x0001 92#define DBG_0 0x0002 93#define DBG_1 0x0004 94#define DBG_SG 0x0020 95#define DBG_FIFO 0x0040 96#define DBG_PIO 0x0080 97 98 99/* 100 * Set set of things to output debugging for. 101 * Undefine to remove all debugging 102 */ 103/*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/ 104/*#define DEBUG_MASK DBG_0*/ 105 106 107/* 108 * Output a kernel mesage at the specified level and append the 109 * driver name and a ": " to the start of the message 110 */ 111#define dprintkl(level, format, arg...) \ 112 printk(level DC395X_NAME ": " format , ## arg) 113 114 115#ifdef DEBUG_MASK 116/* 117 * print a debug message - this is formated with KERN_DEBUG, then the 118 * driver name followed by a ": " and then the message is output. 119 * This also checks that the specified debug level is enabled before 120 * outputing the message 121 */ 122#define dprintkdbg(type, format, arg...) \ 123 do { \ 124 if ((type) & (DEBUG_MASK)) \ 125 dprintkl(KERN_DEBUG , format , ## arg); \ 126 } while (0) 127 128/* 129 * Check if the specified type of debugging is enabled 130 */ 131#define debug_enabled(type) ((DEBUG_MASK) & (type)) 132 133#else 134/* 135 * No debugging. Do nothing 136 */ 137#define dprintkdbg(type, format, arg...) \ 138 do {} while (0) 139#define debug_enabled(type) (0) 140 141#endif 142 143 144#ifndef PCI_VENDOR_ID_TEKRAM 145#define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */ 146#endif 147#ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040 148#define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */ 149#endif 150 151 152#define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags) 153#define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags) 154 155#define DC395x_read8(acb,address) (u8)(inb(acb->io_port_base + (address))) 156#define DC395x_read16(acb,address) (u16)(inw(acb->io_port_base + (address))) 157#define DC395x_read32(acb,address) (u32)(inl(acb->io_port_base + (address))) 158#define DC395x_write8(acb,address,value) outb((value), acb->io_port_base + (address)) 159#define DC395x_write16(acb,address,value) outw((value), acb->io_port_base + (address)) 160#define DC395x_write32(acb,address,value) outl((value), acb->io_port_base + (address)) 161 162/* cmd->result */ 163#define RES_TARGET 0x000000FF /* Target State */ 164#define RES_TARGET_LNX STATUS_MASK /* Only official ... */ 165#define RES_ENDMSG 0x0000FF00 /* End Message */ 166#define RES_DID 0x00FF0000 /* DID_ codes */ 167#define RES_DRV 0xFF000000 /* DRIVER_ codes */ 168 169#define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)) 170#define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1) 171 172#define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); } 173#define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; } 174#define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; } 175#define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; } 176#define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; } 177 178#define TAG_NONE 255 179 180/* 181 * srb->segement_x is the hw sg list. It is always allocated as a 182 * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not 183 * cross a page boundy. 184 */ 185#define SEGMENTX_LEN (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY) 186 187 188struct SGentry { 189 u32 address; /* bus! address */ 190 u32 length; 191}; 192 193/* The SEEPROM structure for TRM_S1040 */ 194struct NVRamTarget { 195 u8 cfg0; /* Target configuration byte 0 */ 196 u8 period; /* Target period */ 197 u8 cfg2; /* Target configuration byte 2 */ 198 u8 cfg3; /* Target configuration byte 3 */ 199}; 200 201struct NvRamType { 202 u8 sub_vendor_id[2]; /* 0,1 Sub Vendor ID */ 203 u8 sub_sys_id[2]; /* 2,3 Sub System ID */ 204 u8 sub_class; /* 4 Sub Class */ 205 u8 vendor_id[2]; /* 5,6 Vendor ID */ 206 u8 device_id[2]; /* 7,8 Device ID */ 207 u8 reserved; /* 9 Reserved */ 208 struct NVRamTarget target[DC395x_MAX_SCSI_ID]; 209 /** 10,11,12,13 210 ** 14,15,16,17 211 ** .... 212 ** .... 213 ** 70,71,72,73 214 */ 215 u8 scsi_id; /* 74 Host Adapter SCSI ID */ 216 u8 channel_cfg; /* 75 Channel configuration */ 217 u8 delay_time; /* 76 Power on delay time */ 218 u8 max_tag; /* 77 Maximum tags */ 219 u8 reserved0; /* 78 */ 220 u8 boot_target; /* 79 */ 221 u8 boot_lun; /* 80 */ 222 u8 reserved1; /* 81 */ 223 u16 reserved2[22]; /* 82,..125 */ 224 u16 cksum; /* 126,127 */ 225}; 226 227struct ScsiReqBlk { 228 struct list_head list; /* next/prev ptrs for srb lists */ 229 struct DeviceCtlBlk *dcb; 230 struct scsi_cmnd *cmd; 231 232 struct SGentry *segment_x; /* Linear array of hw sg entries (up to 64 entries) */ 233 dma_addr_t sg_bus_addr; /* Bus address of sg list (ie, of segment_x) */ 234 235 u8 sg_count; /* No of HW sg entries for this request */ 236 u8 sg_index; /* Index of HW sg entry for this request */ 237 size_t total_xfer_length; /* Total number of bytes remaining to be transfered */ 238 size_t request_length; /* Total number of bytes in this request */ 239 /* 240 * The sense buffer handling function, request_sense, uses 241 * the first hw sg entry (segment_x[0]) and the transfer 242 * length (total_xfer_length). While doing this it stores the 243 * original values into the last sg hw list 244 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the 245 * total_xfer_length in xferred. These values are restored in 246 * pci_unmap_srb_sense. This is the only place xferred is used. 247 */ 248 size_t xferred; /* Saved copy of total_xfer_length */ 249 250 u16 state; 251 252 u8 msgin_buf[6]; 253 u8 msgout_buf[6]; 254 255 u8 adapter_status; 256 u8 target_status; 257 u8 msg_count; 258 u8 end_message; 259 260 u8 tag_number; 261 u8 status; 262 u8 retry_count; 263 u8 flag; 264 265 u8 scsi_phase; 266}; 267 268struct DeviceCtlBlk { 269 struct list_head list; /* next/prev ptrs for the dcb list */ 270 struct AdapterCtlBlk *acb; 271 struct list_head srb_going_list; /* head of going srb list */ 272 struct list_head srb_waiting_list; /* head of waiting srb list */ 273 274 struct ScsiReqBlk *active_srb; 275 u32 tag_mask; 276 277 u16 max_command; 278 279 u8 target_id; /* SCSI Target ID (SCSI Only) */ 280 u8 target_lun; /* SCSI Log. Unit (SCSI Only) */ 281 u8 identify_msg; 282 u8 dev_mode; 283 284 u8 inquiry7; /* To store Inquiry flags */ 285 u8 sync_mode; /* 0:async mode */ 286 u8 min_nego_period; /* for nego. */ 287 u8 sync_period; /* for reg. */ 288 289 u8 sync_offset; /* for reg. and nego.(low nibble) */ 290 u8 flag; 291 u8 dev_type; 292 u8 init_tcq_flag; 293}; 294 295struct AdapterCtlBlk { 296 struct Scsi_Host *scsi_host; 297 298 unsigned long io_port_base; 299 unsigned long io_port_len; 300 301 struct list_head dcb_list; /* head of going dcb list */ 302 struct DeviceCtlBlk *dcb_run_robin; 303 struct DeviceCtlBlk *active_dcb; 304 305 struct list_head srb_free_list; /* head of free srb list */ 306 struct ScsiReqBlk *tmp_srb; 307 struct timer_list waiting_timer; 308 struct timer_list selto_timer; 309 310 u16 srb_count; 311 312 u8 sel_timeout; 313 314 unsigned int irq_level; 315 u8 tag_max_num; 316 u8 acb_flag; 317 u8 gmode2; 318 319 u8 config; 320 u8 lun_chk; 321 u8 scan_devices; 322 u8 hostid_bit; 323 324 u8 dcb_map[DC395x_MAX_SCSI_ID]; 325 struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32]; 326 327 struct pci_dev *dev; 328 329 u8 msg_len; 330 331 struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT]; 332 struct ScsiReqBlk srb; 333 334 struct NvRamType eeprom; /* eeprom settings for this adapter */ 335}; 336 337 338/*--------------------------------------------------------------------------- 339 Forward declarations 340 ---------------------------------------------------------------------------*/ 341static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 342 u16 *pscsi_status); 343static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 344 u16 *pscsi_status); 345static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 346 u16 *pscsi_status); 347static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 348 u16 *pscsi_status); 349static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 350 u16 *pscsi_status); 351static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 352 u16 *pscsi_status); 353static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 354 u16 *pscsi_status); 355static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 356 u16 *pscsi_status); 357static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 358 u16 *pscsi_status); 359static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 360 u16 *pscsi_status); 361static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 362 u16 *pscsi_status); 363static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 364 u16 *pscsi_status); 365static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 366 u16 *pscsi_status); 367static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 368 u16 *pscsi_status); 369static void set_basic_config(struct AdapterCtlBlk *acb); 370static void cleanup_after_transfer(struct AdapterCtlBlk *acb, 371 struct ScsiReqBlk *srb); 372static void reset_scsi_bus(struct AdapterCtlBlk *acb); 373static void data_io_transfer(struct AdapterCtlBlk *acb, 374 struct ScsiReqBlk *srb, u16 io_dir); 375static void disconnect(struct AdapterCtlBlk *acb); 376static void reselect(struct AdapterCtlBlk *acb); 377static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 378 struct ScsiReqBlk *srb); 379static inline void enable_msgout_abort(struct AdapterCtlBlk *acb, 380 struct ScsiReqBlk *srb); 381static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb, 382 struct ScsiReqBlk *srb); 383static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code, 384 struct scsi_cmnd *cmd, u8 force); 385static void scsi_reset_detect(struct AdapterCtlBlk *acb); 386static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb); 387static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb, 388 struct ScsiReqBlk *srb); 389static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 390 struct ScsiReqBlk *srb); 391static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 392 struct ScsiReqBlk *srb); 393static void set_xfer_rate(struct AdapterCtlBlk *acb, 394 struct DeviceCtlBlk *dcb); 395static void waiting_timeout(unsigned long ptr); 396 397 398/*--------------------------------------------------------------------------- 399 Static Data 400 ---------------------------------------------------------------------------*/ 401static u16 current_sync_offset = 0; 402 403static void *dc395x_scsi_phase0[] = { 404 data_out_phase0,/* phase:0 */ 405 data_in_phase0, /* phase:1 */ 406 command_phase0, /* phase:2 */ 407 status_phase0, /* phase:3 */ 408 nop0, /* phase:4 PH_BUS_FREE .. initial phase */ 409 nop0, /* phase:5 PH_BUS_FREE .. initial phase */ 410 msgout_phase0, /* phase:6 */ 411 msgin_phase0, /* phase:7 */ 412}; 413 414static void *dc395x_scsi_phase1[] = { 415 data_out_phase1,/* phase:0 */ 416 data_in_phase1, /* phase:1 */ 417 command_phase1, /* phase:2 */ 418 status_phase1, /* phase:3 */ 419 nop1, /* phase:4 PH_BUS_FREE .. initial phase */ 420 nop1, /* phase:5 PH_BUS_FREE .. initial phase */ 421 msgout_phase1, /* phase:6 */ 422 msgin_phase1, /* phase:7 */ 423}; 424 425/* 426 *Fast20: 000 50ns, 20.0 MHz 427 * 001 75ns, 13.3 MHz 428 * 010 100ns, 10.0 MHz 429 * 011 125ns, 8.0 MHz 430 * 100 150ns, 6.6 MHz 431 * 101 175ns, 5.7 MHz 432 * 110 200ns, 5.0 MHz 433 * 111 250ns, 4.0 MHz 434 * 435 *Fast40(LVDS): 000 25ns, 40.0 MHz 436 * 001 50ns, 20.0 MHz 437 * 010 75ns, 13.3 MHz 438 * 011 100ns, 10.0 MHz 439 * 100 125ns, 8.0 MHz 440 * 101 150ns, 6.6 MHz 441 * 110 175ns, 5.7 MHz 442 * 111 200ns, 5.0 MHz 443 */ 444/*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/ 445 446/* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */ 447static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 }; 448static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 }; 449 450 451/*--------------------------------------------------------------------------- 452 Configuration 453 ---------------------------------------------------------------------------*/ 454/* 455 * Module/boot parameters currently effect *all* instances of the 456 * card in the system. 457 */ 458 459/* 460 * Command line parameters are stored in a structure below. 461 * These are the index's into the structure for the various 462 * command line options. 463 */ 464#define CFG_ADAPTER_ID 0 465#define CFG_MAX_SPEED 1 466#define CFG_DEV_MODE 2 467#define CFG_ADAPTER_MODE 3 468#define CFG_TAGS 4 469#define CFG_RESET_DELAY 5 470 471#define CFG_NUM 6 /* number of configuration items */ 472 473 474/* 475 * Value used to indicate that a command line override 476 * hasn't been used to modify the value. 477 */ 478#define CFG_PARAM_UNSET -1 479 480 481/* 482 * Hold command line parameters. 483 */ 484struct ParameterData { 485 int value; /* value of this setting */ 486 int min; /* minimum value */ 487 int max; /* maximum value */ 488 int def; /* default value */ 489 int safe; /* safe value */ 490}; 491static struct ParameterData __devinitdata cfg_data[] = { 492 { /* adapter id */ 493 CFG_PARAM_UNSET, 494 0, 495 15, 496 7, 497 7 498 }, 499 { /* max speed */ 500 CFG_PARAM_UNSET, 501 0, 502 7, 503 1, /* 13.3Mhz */ 504 4, /* 6.7Hmz */ 505 }, 506 { /* dev mode */ 507 CFG_PARAM_UNSET, 508 0, 509 0x3f, 510 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO | 511 NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING | 512 NTC_DO_SEND_START, 513 NTC_DO_PARITY_CHK | NTC_DO_SEND_START 514 }, 515 { /* adapter mode */ 516 CFG_PARAM_UNSET, 517 0, 518 0x2f, 519#ifdef CONFIG_SCSI_MULTI_LUN 520 NAC_SCANLUN | 521#endif 522 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET 523 /*| NAC_ACTIVE_NEG*/, 524 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08 525 }, 526 { /* tags */ 527 CFG_PARAM_UNSET, 528 0, 529 5, 530 3, /* 16 tags (??) */ 531 2, 532 }, 533 { /* reset delay */ 534 CFG_PARAM_UNSET, 535 0, 536 180, 537 1, /* 1 second */ 538 10, /* 10 seconds */ 539 } 540}; 541 542 543/* 544 * Safe settings. If set to zero the BIOS/default values with 545 * command line overrides will be used. If set to 1 then safe and 546 * slow settings will be used. 547 */ 548static int use_safe_settings = 0; 549module_param_named(safe, use_safe_settings, bool, 0); 550MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false"); 551 552 553module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0); 554MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)"); 555 556module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0); 557MODULE_PARM_DESC(max_speed, "Maximum bus speed. Default 1 (0-7) Speeds: 0=20, 1=13.3, 2=10, 3=8, 4=6.7, 5=5.8, 6=5, 7=4 Mhz"); 558 559module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0); 560MODULE_PARM_DESC(dev_mode, "Device mode."); 561 562module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0); 563MODULE_PARM_DESC(adapter_mode, "Adapter mode."); 564 565module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0); 566MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)"); 567 568module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0); 569MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)"); 570 571 572/** 573 * set_safe_settings - if the use_safe_settings option is set then 574 * set all values to the safe and slow values. 575 **/ 576static void __devinit set_safe_settings(void) 577{ 578 if (use_safe_settings) 579 { 580 int i; 581 582 dprintkl(KERN_INFO, "Using safe settings.\n"); 583 for (i = 0; i < CFG_NUM; i++) 584 { 585 cfg_data[i].value = cfg_data[i].safe; 586 } 587 } 588} 589 590 591/** 592 * fix_settings - reset any boot parameters which are out of range 593 * back to the default values. 594 **/ 595static void __devinit fix_settings(void) 596{ 597 int i; 598 599 dprintkdbg(DBG_1, 600 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x " 601 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n", 602 cfg_data[CFG_ADAPTER_ID].value, 603 cfg_data[CFG_MAX_SPEED].value, 604 cfg_data[CFG_DEV_MODE].value, 605 cfg_data[CFG_ADAPTER_MODE].value, 606 cfg_data[CFG_TAGS].value, 607 cfg_data[CFG_RESET_DELAY].value); 608 for (i = 0; i < CFG_NUM; i++) 609 { 610 if (cfg_data[i].value < cfg_data[i].min 611 || cfg_data[i].value > cfg_data[i].max) 612 cfg_data[i].value = cfg_data[i].def; 613 } 614} 615 616 617 618/* 619 * Mapping from the eeprom delay index value (index into this array) 620 * to the number of actual seconds that the delay should be for. 621 */ 622static char __devinitdata eeprom_index_to_delay_map[] = 623 { 1, 3, 5, 10, 16, 30, 60, 120 }; 624 625 626/** 627 * eeprom_index_to_delay - Take the eeprom delay setting and convert it 628 * into a number of seconds. 629 * 630 * @eeprom: The eeprom structure in which we find the delay index to map. 631 **/ 632static void __devinit eeprom_index_to_delay(struct NvRamType *eeprom) 633{ 634 eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time]; 635} 636 637 638/** 639 * delay_to_eeprom_index - Take a delay in seconds and return the 640 * closest eeprom index which will delay for at least that amount of 641 * seconds. 642 * 643 * @delay: The delay, in seconds, to find the eeprom index for. 644 **/ 645static int __devinit delay_to_eeprom_index(int delay) 646{ 647 u8 idx = 0; 648 while (idx < 7 && eeprom_index_to_delay_map[idx] < delay) 649 idx++; 650 return idx; 651} 652 653 654/** 655 * eeprom_override - Override the eeprom settings, in the provided 656 * eeprom structure, with values that have been set on the command 657 * line. 658 * 659 * @eeprom: The eeprom data to override with command line options. 660 **/ 661static void __devinit eeprom_override(struct NvRamType *eeprom) 662{ 663 u8 id; 664 665 /* Adapter Settings */ 666 if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET) 667 eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value; 668 669 if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET) 670 eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value; 671 672 if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET) 673 eeprom->delay_time = delay_to_eeprom_index( 674 cfg_data[CFG_RESET_DELAY].value); 675 676 if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET) 677 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value; 678 679 /* Device Settings */ 680 for (id = 0; id < DC395x_MAX_SCSI_ID; id++) { 681 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET) 682 eeprom->target[id].cfg0 = 683 (u8)cfg_data[CFG_DEV_MODE].value; 684 685 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET) 686 eeprom->target[id].period = 687 (u8)cfg_data[CFG_MAX_SPEED].value; 688 689 } 690} 691 692 693/*--------------------------------------------------------------------------- 694 ---------------------------------------------------------------------------*/ 695 696static unsigned int list_size(struct list_head *head) 697{ 698 unsigned int count = 0; 699 struct list_head *pos; 700 list_for_each(pos, head) 701 count++; 702 return count; 703} 704 705 706static struct DeviceCtlBlk *dcb_get_next(struct list_head *head, 707 struct DeviceCtlBlk *pos) 708{ 709 int use_next = 0; 710 struct DeviceCtlBlk* next = NULL; 711 struct DeviceCtlBlk* i; 712 713 if (list_empty(head)) 714 return NULL; 715 716 /* find supplied dcb and then select the next one */ 717 list_for_each_entry(i, head, list) 718 if (use_next) { 719 next = i; 720 break; 721 } else if (i == pos) { 722 use_next = 1; 723 } 724 /* if no next one take the head one (ie, wraparound) */ 725 if (!next) 726 list_for_each_entry(i, head, list) { 727 next = i; 728 break; 729 } 730 731 return next; 732} 733 734 735static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb) 736{ 737 if (srb->tag_number < 255) { 738 dcb->tag_mask &= ~(1 << srb->tag_number); /* free tag mask */ 739 srb->tag_number = 255; 740 } 741} 742 743 744/* Find cmd in SRB list */ 745static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd, 746 struct list_head *head) 747{ 748 struct ScsiReqBlk *i; 749 list_for_each_entry(i, head, list) 750 if (i->cmd == cmd) 751 return i; 752 return NULL; 753} 754 755 756static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb) 757{ 758 struct list_head *head = &acb->srb_free_list; 759 struct ScsiReqBlk *srb = NULL; 760 761 if (!list_empty(head)) { 762 srb = list_entry(head->next, struct ScsiReqBlk, list); 763 list_del(head->next); 764 dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb); 765 } 766 return srb; 767} 768 769 770static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb) 771{ 772 dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb); 773 list_add_tail(&srb->list, &acb->srb_free_list); 774} 775 776 777static void srb_waiting_insert(struct DeviceCtlBlk *dcb, 778 struct ScsiReqBlk *srb) 779{ 780 dprintkdbg(DBG_0, "srb_waiting_insert: (pid#%li) <%02i-%i> srb=%p\n", 781 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 782 list_add(&srb->list, &dcb->srb_waiting_list); 783} 784 785 786static void srb_waiting_append(struct DeviceCtlBlk *dcb, 787 struct ScsiReqBlk *srb) 788{ 789 dprintkdbg(DBG_0, "srb_waiting_append: (pid#%li) <%02i-%i> srb=%p\n", 790 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 791 list_add_tail(&srb->list, &dcb->srb_waiting_list); 792} 793 794 795static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb) 796{ 797 dprintkdbg(DBG_0, "srb_going_append: (pid#%li) <%02i-%i> srb=%p\n", 798 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 799 list_add_tail(&srb->list, &dcb->srb_going_list); 800} 801 802 803static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb) 804{ 805 struct ScsiReqBlk *i; 806 struct ScsiReqBlk *tmp; 807 dprintkdbg(DBG_0, "srb_going_remove: (pid#%li) <%02i-%i> srb=%p\n", 808 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 809 810 list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list) 811 if (i == srb) { 812 list_del(&srb->list); 813 break; 814 } 815} 816 817 818static void srb_waiting_remove(struct DeviceCtlBlk *dcb, 819 struct ScsiReqBlk *srb) 820{ 821 struct ScsiReqBlk *i; 822 struct ScsiReqBlk *tmp; 823 dprintkdbg(DBG_0, "srb_waiting_remove: (pid#%li) <%02i-%i> srb=%p\n", 824 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 825 826 list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list) 827 if (i == srb) { 828 list_del(&srb->list); 829 break; 830 } 831} 832 833 834static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb, 835 struct ScsiReqBlk *srb) 836{ 837 dprintkdbg(DBG_0, 838 "srb_going_to_waiting_move: (pid#%li) <%02i-%i> srb=%p\n", 839 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 840 list_move(&srb->list, &dcb->srb_waiting_list); 841} 842 843 844static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb, 845 struct ScsiReqBlk *srb) 846{ 847 dprintkdbg(DBG_0, 848 "srb_waiting_to_going_move: (pid#%li) <%02i-%i> srb=%p\n", 849 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 850 list_move(&srb->list, &dcb->srb_going_list); 851} 852 853 854/* Sets the timer to wake us up */ 855static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to) 856{ 857 if (timer_pending(&acb->waiting_timer)) 858 return; 859 init_timer(&acb->waiting_timer); 860 acb->waiting_timer.function = waiting_timeout; 861 acb->waiting_timer.data = (unsigned long) acb; 862 if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2)) 863 acb->waiting_timer.expires = 864 acb->scsi_host->last_reset - HZ / 2 + 1; 865 else 866 acb->waiting_timer.expires = jiffies + to + 1; 867 add_timer(&acb->waiting_timer); 868} 869 870 871/* Send the next command from the waiting list to the bus */ 872static void waiting_process_next(struct AdapterCtlBlk *acb) 873{ 874 struct DeviceCtlBlk *start = NULL; 875 struct DeviceCtlBlk *pos; 876 struct DeviceCtlBlk *dcb; 877 struct ScsiReqBlk *srb; 878 struct list_head *dcb_list_head = &acb->dcb_list; 879 880 if (acb->active_dcb 881 || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) 882 return; 883 884 if (timer_pending(&acb->waiting_timer)) 885 del_timer(&acb->waiting_timer); 886 887 if (list_empty(dcb_list_head)) 888 return; 889 890 /* 891 * Find the starting dcb. Need to find it again in the list 892 * since the list may have changed since we set the ptr to it 893 */ 894 list_for_each_entry(dcb, dcb_list_head, list) 895 if (dcb == acb->dcb_run_robin) { 896 start = dcb; 897 break; 898 } 899 if (!start) { 900 /* This can happen! */ 901 start = list_entry(dcb_list_head->next, typeof(*start), list); 902 acb->dcb_run_robin = start; 903 } 904 905 906 /* 907 * Loop over the dcb, but we start somewhere (potentially) in 908 * the middle of the loop so we need to manully do this. 909 */ 910 pos = start; 911 do { 912 struct list_head *waiting_list_head = &pos->srb_waiting_list; 913 914 /* Make sure, the next another device gets scheduled ... */ 915 acb->dcb_run_robin = dcb_get_next(dcb_list_head, 916 acb->dcb_run_robin); 917 918 if (list_empty(waiting_list_head) || 919 pos->max_command <= list_size(&pos->srb_going_list)) { 920 /* move to next dcb */ 921 pos = dcb_get_next(dcb_list_head, pos); 922 } else { 923 srb = list_entry(waiting_list_head->next, 924 struct ScsiReqBlk, list); 925 926 /* Try to send to the bus */ 927 if (!start_scsi(acb, pos, srb)) 928 srb_waiting_to_going_move(pos, srb); 929 else 930 waiting_set_timer(acb, HZ/50); 931 break; 932 } 933 } while (pos != start); 934} 935 936 937/* Wake up waiting queue */ 938static void waiting_timeout(unsigned long ptr) 939{ 940 unsigned long flags; 941 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr; 942 dprintkdbg(DBG_1, 943 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb); 944 DC395x_LOCK_IO(acb->scsi_host, flags); 945 waiting_process_next(acb); 946 DC395x_UNLOCK_IO(acb->scsi_host, flags); 947} 948 949 950/* Get the DCB for a given ID/LUN combination */ 951static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun) 952{ 953 return acb->children[id][lun]; 954} 955 956 957/* Send SCSI Request Block (srb) to adapter (acb) */ 958static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb) 959{ 960 struct DeviceCtlBlk *dcb = srb->dcb; 961 962 if (dcb->max_command <= list_size(&dcb->srb_going_list) || 963 acb->active_dcb || 964 (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) { 965 srb_waiting_append(dcb, srb); 966 waiting_process_next(acb); 967 return; 968 } 969 970 if (!start_scsi(acb, dcb, srb)) 971 srb_going_append(dcb, srb); 972 else { 973 srb_waiting_insert(dcb, srb); 974 waiting_set_timer(acb, HZ / 50); 975 } 976} 977 978/* Prepare SRB for being sent to Device DCB w/ command *cmd */ 979static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb, 980 struct ScsiReqBlk *srb) 981{ 982 enum dma_data_direction dir = cmd->sc_data_direction; 983 dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n", 984 cmd->pid, dcb->target_id, dcb->target_lun); 985 986 srb->dcb = dcb; 987 srb->cmd = cmd; 988 srb->sg_count = 0; 989 srb->total_xfer_length = 0; 990 srb->sg_bus_addr = 0; 991 srb->sg_index = 0; 992 srb->adapter_status = 0; 993 srb->target_status = 0; 994 srb->msg_count = 0; 995 srb->status = 0; 996 srb->flag = 0; 997 srb->state = 0; 998 srb->retry_count = 0; 999 srb->tag_number = TAG_NONE; 1000 srb->scsi_phase = PH_BUS_FREE; /* initial phase */ 1001 srb->end_message = 0; 1002 1003 if (dir == PCI_DMA_NONE || !cmd->request_buffer) { 1004 dprintkdbg(DBG_0, 1005 "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n", 1006 cmd->bufflen, cmd->request_buffer, 1007 cmd->use_sg, srb->segment_x[0].address); 1008 } else if (cmd->use_sg) { 1009 int i; 1010 u32 reqlen = cmd->request_bufflen; 1011 struct scatterlist *sl = (struct scatterlist *) 1012 cmd->request_buffer; 1013 struct SGentry *sgp = srb->segment_x; 1014 srb->sg_count = pci_map_sg(dcb->acb->dev, sl, cmd->use_sg, 1015 dir); 1016 dprintkdbg(DBG_0, 1017 "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n", 1018 reqlen, cmd->request_buffer, cmd->use_sg, 1019 srb->sg_count); 1020 1021 for (i = 0; i < srb->sg_count; i++) { 1022 u32 busaddr = (u32)sg_dma_address(&sl[i]); 1023 u32 seglen = (u32)sl[i].length; 1024 sgp[i].address = busaddr; 1025 sgp[i].length = seglen; 1026 srb->total_xfer_length += seglen; 1027 } 1028 sgp += srb->sg_count - 1; 1029 1030 /* 1031 * adjust last page if too big as it is allocated 1032 * on even page boundaries 1033 */ 1034 if (srb->total_xfer_length > reqlen) { 1035 sgp->length -= (srb->total_xfer_length - reqlen); 1036 srb->total_xfer_length = reqlen; 1037 } 1038 1039 /* Fixup for WIDE padding - make sure length is even */ 1040 if (dcb->sync_period & WIDE_SYNC && 1041 srb->total_xfer_length % 2) { 1042 srb->total_xfer_length++; 1043 sgp->length++; 1044 } 1045 1046 srb->sg_bus_addr = pci_map_single(dcb->acb->dev, 1047 srb->segment_x, 1048 SEGMENTX_LEN, 1049 PCI_DMA_TODEVICE); 1050 1051 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n", 1052 srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN); 1053 } else { 1054 srb->total_xfer_length = cmd->request_bufflen; 1055 srb->sg_count = 1; 1056 srb->segment_x[0].address = 1057 pci_map_single(dcb->acb->dev, cmd->request_buffer, 1058 srb->total_xfer_length, dir); 1059 1060 /* Fixup for WIDE padding - make sure length is even */ 1061 if (dcb->sync_period & WIDE_SYNC && srb->total_xfer_length % 2) 1062 srb->total_xfer_length++; 1063 1064 srb->segment_x[0].length = srb->total_xfer_length; 1065 1066 dprintkdbg(DBG_0, 1067 "build_srb: [1] len=%d buf=%p use_sg=%d map=%08x\n", 1068 srb->total_xfer_length, cmd->request_buffer, 1069 cmd->use_sg, srb->segment_x[0].address); 1070 } 1071 1072 srb->request_length = srb->total_xfer_length; 1073} 1074 1075 1076/** 1077 * dc395x_queue_command - queue scsi command passed from the mid 1078 * layer, invoke 'done' on completion 1079 * 1080 * @cmd: pointer to scsi command object 1081 * @done: function pointer to be invoked on completion 1082 * 1083 * Returns 1 if the adapter (host) is busy, else returns 0. One 1084 * reason for an adapter to be busy is that the number 1085 * of outstanding queued commands is already equal to 1086 * struct Scsi_Host::can_queue . 1087 * 1088 * Required: if struct Scsi_Host::can_queue is ever non-zero 1089 * then this function is required. 1090 * 1091 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave") 1092 * and is expected to be held on return. 1093 * 1094 **/ 1095static int dc395x_queue_command(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) 1096{ 1097 struct DeviceCtlBlk *dcb; 1098 struct ScsiReqBlk *srb; 1099 struct AdapterCtlBlk *acb = 1100 (struct AdapterCtlBlk *)cmd->device->host->hostdata; 1101 dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n", 1102 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]); 1103 1104 /* Assume BAD_TARGET; will be cleared later */ 1105 cmd->result = DID_BAD_TARGET << 16; 1106 1107 /* ignore invalid targets */ 1108 if (cmd->device->id >= acb->scsi_host->max_id || 1109 cmd->device->lun >= acb->scsi_host->max_lun || 1110 cmd->device->lun >31) { 1111 goto complete; 1112 } 1113 1114 /* does the specified lun on the specified device exist */ 1115 if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) { 1116 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n", 1117 cmd->device->id, cmd->device->lun); 1118 goto complete; 1119 } 1120 1121 /* do we have a DCB for the device */ 1122 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun); 1123 if (!dcb) { 1124 /* should never happen */ 1125 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>", 1126 cmd->device->id, cmd->device->lun); 1127 goto complete; 1128 } 1129 1130 /* set callback and clear result in the command */ 1131 cmd->scsi_done = done; 1132 cmd->result = 0; 1133 1134 srb = srb_get_free(acb); 1135 if (!srb) 1136 { 1137 /* 1138 * Return 1 since we are unable to queue this command at this 1139 * point in time. 1140 */ 1141 dprintkdbg(DBG_0, "queue_command: No free srb's\n"); 1142 return 1; 1143 } 1144 1145 build_srb(cmd, dcb, srb); 1146 1147 if (!list_empty(&dcb->srb_waiting_list)) { 1148 /* append to waiting queue */ 1149 srb_waiting_append(dcb, srb); 1150 waiting_process_next(acb); 1151 } else { 1152 /* process immediately */ 1153 send_srb(acb, srb); 1154 } 1155 dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->pid); 1156 return 0; 1157 1158complete: 1159 /* 1160 * Complete the command immediatey, and then return 0 to 1161 * indicate that we have handled the command. This is usually 1162 * done when the commad is for things like non existent 1163 * devices. 1164 */ 1165 done(cmd); 1166 return 0; 1167} 1168 1169 1170/* 1171 * Return the disk geometry for the given SCSI device. 1172 */ 1173static int dc395x_bios_param(struct scsi_device *sdev, 1174 struct block_device *bdev, sector_t capacity, int *info) 1175{ 1176#ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP 1177 int heads, sectors, cylinders; 1178 struct AdapterCtlBlk *acb; 1179 int size = capacity; 1180 1181 dprintkdbg(DBG_0, "dc395x_bios_param..............\n"); 1182 acb = (struct AdapterCtlBlk *)sdev->host->hostdata; 1183 heads = 64; 1184 sectors = 32; 1185 cylinders = size / (heads * sectors); 1186 1187 if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) { 1188 heads = 255; 1189 sectors = 63; 1190 cylinders = size / (heads * sectors); 1191 } 1192 geom[0] = heads; 1193 geom[1] = sectors; 1194 geom[2] = cylinders; 1195 return 0; 1196#else 1197 return scsicam_bios_param(bdev, capacity, info); 1198#endif 1199} 1200 1201 1202static void dump_register_info(struct AdapterCtlBlk *acb, 1203 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb) 1204{ 1205 u16 pstat; 1206 struct pci_dev *dev = acb->dev; 1207 pci_read_config_word(dev, PCI_STATUS, &pstat); 1208 if (!dcb) 1209 dcb = acb->active_dcb; 1210 if (!srb && dcb) 1211 srb = dcb->active_srb; 1212 if (srb) { 1213 if (!srb->cmd) 1214 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n", 1215 srb, srb->cmd); 1216 else 1217 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) " 1218 "cmnd=0x%02x <%02i-%i>\n", 1219 srb, srb->cmd, srb->cmd->pid, 1220 srb->cmd->cmnd[0], srb->cmd->device->id, 1221 srb->cmd->device->lun); 1222 printk(" sglist=%p cnt=%i idx=%i len=%zu\n", 1223 srb->segment_x, srb->sg_count, srb->sg_index, 1224 srb->total_xfer_length); 1225 printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n", 1226 srb->state, srb->status, srb->scsi_phase, 1227 (acb->active_dcb) ? "" : "not"); 1228 } 1229 dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x " 1230 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x " 1231 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x " 1232 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n", 1233 DC395x_read16(acb, TRM_S1040_SCSI_STATUS), 1234 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT), 1235 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL), 1236 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS), 1237 DC395x_read8(acb, TRM_S1040_SCSI_SYNC), 1238 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID), 1239 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG), 1240 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), 1241 DC395x_read8(acb, TRM_S1040_SCSI_INTEN), 1242 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0), 1243 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2), 1244 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND), 1245 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT)); 1246 dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x " 1247 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x " 1248 "ctctr=0x%08x addr=0x%08x:0x%08x}\n", 1249 DC395x_read16(acb, TRM_S1040_DMA_COMMAND), 1250 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT), 1251 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT), 1252 DC395x_read8(acb, TRM_S1040_DMA_STATUS), 1253 DC395x_read8(acb, TRM_S1040_DMA_INTEN), 1254 DC395x_read16(acb, TRM_S1040_DMA_CONFIG), 1255 DC395x_read32(acb, TRM_S1040_DMA_XCNT), 1256 DC395x_read32(acb, TRM_S1040_DMA_CXCNT), 1257 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR), 1258 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR)); 1259 dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} " 1260 "pci{status=0x%04x}\n", 1261 DC395x_read8(acb, TRM_S1040_GEN_CONTROL), 1262 DC395x_read8(acb, TRM_S1040_GEN_STATUS), 1263 DC395x_read8(acb, TRM_S1040_GEN_TIMER), 1264 pstat); 1265} 1266 1267 1268static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt) 1269{ 1270#if debug_enabled(DBG_FIFO) 1271 u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL); 1272 u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT); 1273 if (!(fifocnt & 0x40)) 1274 dprintkdbg(DBG_FIFO, 1275 "clear_fifo: (%i bytes) on phase %02x in %s\n", 1276 fifocnt & 0x3f, lines, txt); 1277#endif 1278 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO); 1279} 1280 1281 1282static void reset_dev_param(struct AdapterCtlBlk *acb) 1283{ 1284 struct DeviceCtlBlk *dcb; 1285 struct NvRamType *eeprom = &acb->eeprom; 1286 dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb); 1287 1288 list_for_each_entry(dcb, &acb->dcb_list, list) { 1289 u8 period_index; 1290 1291 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE); 1292 dcb->sync_period = 0; 1293 dcb->sync_offset = 0; 1294 1295 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0; 1296 period_index = eeprom->target[dcb->target_id].period & 0x07; 1297 dcb->min_nego_period = clock_period[period_index]; 1298 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO) 1299 || !(acb->config & HCC_WIDE_CARD)) 1300 dcb->sync_mode &= ~WIDE_NEGO_ENABLE; 1301 } 1302} 1303 1304 1305/* 1306 * perform a hard reset on the SCSI bus 1307 * @cmd - some command for this host (for fetching hooks) 1308 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003). 1309 */ 1310static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd) 1311{ 1312 struct AdapterCtlBlk *acb = 1313 (struct AdapterCtlBlk *)cmd->device->host->hostdata; 1314 dprintkl(KERN_INFO, 1315 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n", 1316 cmd->pid, cmd->device->id, cmd->device->lun, cmd); 1317 1318 if (timer_pending(&acb->waiting_timer)) 1319 del_timer(&acb->waiting_timer); 1320 1321 /* 1322 * disable interrupt 1323 */ 1324 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00); 1325 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00); 1326 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE); 1327 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE); 1328 1329 reset_scsi_bus(acb); 1330 udelay(500); 1331 1332 /* We may be in serious trouble. Wait some seconds */ 1333 acb->scsi_host->last_reset = 1334 jiffies + 3 * HZ / 2 + 1335 HZ * acb->eeprom.delay_time; 1336 1337 /* 1338 * re-enable interrupt 1339 */ 1340 /* Clear SCSI FIFO */ 1341 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO); 1342 clear_fifo(acb, "eh_bus_reset"); 1343 /* Delete pending IRQ */ 1344 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS); 1345 set_basic_config(acb); 1346 1347 reset_dev_param(acb); 1348 doing_srb_done(acb, DID_RESET, cmd, 0); 1349 acb->active_dcb = NULL; 1350 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */ 1351 waiting_process_next(acb); 1352 1353 return SUCCESS; 1354} 1355 1356static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd) 1357{ 1358 int rc; 1359 1360 spin_lock_irq(cmd->device->host->host_lock); 1361 rc = __dc395x_eh_bus_reset(cmd); 1362 spin_unlock_irq(cmd->device->host->host_lock); 1363 1364 return rc; 1365} 1366 1367/* 1368 * abort an errant SCSI command 1369 * @cmd - command to be aborted 1370 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003). 1371 */ 1372static int dc395x_eh_abort(struct scsi_cmnd *cmd) 1373{ 1374 /* 1375 * Look into our command queues: If it has not been sent already, 1376 * we remove it and return success. Otherwise fail. 1377 */ 1378 struct AdapterCtlBlk *acb = 1379 (struct AdapterCtlBlk *)cmd->device->host->hostdata; 1380 struct DeviceCtlBlk *dcb; 1381 struct ScsiReqBlk *srb; 1382 dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n", 1383 cmd->pid, cmd->device->id, cmd->device->lun, cmd); 1384 1385 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun); 1386 if (!dcb) { 1387 dprintkl(KERN_DEBUG, "eh_abort: No such device\n"); 1388 return FAILED; 1389 } 1390 1391 srb = find_cmd(cmd, &dcb->srb_waiting_list); 1392 if (srb) { 1393 srb_waiting_remove(dcb, srb); 1394 pci_unmap_srb_sense(acb, srb); 1395 pci_unmap_srb(acb, srb); 1396 free_tag(dcb, srb); 1397 srb_free_insert(acb, srb); 1398 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n"); 1399 cmd->result = DID_ABORT << 16; 1400 return SUCCESS; 1401 } 1402 srb = find_cmd(cmd, &dcb->srb_going_list); 1403 if (srb) { 1404 dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n"); 1405 } else { 1406 dprintkl(KERN_DEBUG, "eh_abort: Command not found\n"); 1407 } 1408 return FAILED; 1409} 1410 1411 1412/* SDTR */ 1413static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 1414 struct ScsiReqBlk *srb) 1415{ 1416 u8 *ptr = srb->msgout_buf + srb->msg_count; 1417 if (srb->msg_count > 1) { 1418 dprintkl(KERN_INFO, 1419 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n", 1420 srb->msg_count, srb->msgout_buf[0], 1421 srb->msgout_buf[1]); 1422 return; 1423 } 1424 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) { 1425 dcb->sync_offset = 0; 1426 dcb->min_nego_period = 200 >> 2; 1427 } else if (dcb->sync_offset == 0) 1428 dcb->sync_offset = SYNC_NEGO_OFFSET; 1429 1430 *ptr++ = MSG_EXTENDED; /* (01h) */ 1431 *ptr++ = 3; /* length */ 1432 *ptr++ = EXTENDED_SDTR; /* (01h) */ 1433 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */ 1434 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */ 1435 srb->msg_count += 5; 1436 srb->state |= SRB_DO_SYNC_NEGO; 1437} 1438 1439 1440/* WDTR */ 1441static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 1442 struct ScsiReqBlk *srb) 1443{ 1444 u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) & 1445 (acb->config & HCC_WIDE_CARD)) ? 1 : 0; 1446 u8 *ptr = srb->msgout_buf + srb->msg_count; 1447 if (srb->msg_count > 1) { 1448 dprintkl(KERN_INFO, 1449 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n", 1450 srb->msg_count, srb->msgout_buf[0], 1451 srb->msgout_buf[1]); 1452 return; 1453 } 1454 *ptr++ = MSG_EXTENDED; /* (01h) */ 1455 *ptr++ = 2; /* length */ 1456 *ptr++ = EXTENDED_WDTR; /* (03h) */ 1457 *ptr++ = wide; 1458 srb->msg_count += 4; 1459 srb->state |= SRB_DO_WIDE_NEGO; 1460} 1461 1462 1463 1464 1465static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb, 1466 struct ScsiReqBlk* srb) 1467{ 1468 u16 s_stat2, return_code; 1469 u8 s_stat, scsicommand, i, identify_message; 1470 u8 *ptr; 1471 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n", 1472 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb); 1473 1474 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */ 1475 1476 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL); 1477 s_stat2 = 0; 1478 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS); 1479 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) { 1480 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n", 1481 srb->cmd->pid, s_stat, s_stat2); 1482 /* 1483 * Try anyway? 1484 * 1485 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection 1486 * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed! 1487 * (This is likely to be a bug in the hardware. Obviously, most people 1488 * only have one initiator per SCSI bus.) 1489 * Instead let this fail and have the timer make sure the command is 1490 * tried again after a short time 1491 */ 1492 /*selto_timer (acb); */ 1493 return 1; 1494 } 1495 if (acb->active_dcb) { 1496 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a" 1497 "command while another command (pid#%li) is active.", 1498 srb->cmd->pid, 1499 acb->active_dcb->active_srb ? 1500 acb->active_dcb->active_srb->cmd->pid : 0); 1501 return 1; 1502 } 1503 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) { 1504 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n", 1505 srb->cmd->pid); 1506 return 1; 1507 } 1508 /* Allow starting of SCSI commands half a second before we allow the mid-level 1509 * to queue them again after a reset */ 1510 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) { 1511 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n"); 1512 return 1; 1513 } 1514 1515 /* Flush FIFO */ 1516 clear_fifo(acb, "start_scsi"); 1517 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); 1518 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); 1519 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); 1520 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); 1521 srb->scsi_phase = PH_BUS_FREE; /* initial phase */ 1522 1523 identify_message = dcb->identify_msg; 1524 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */ 1525 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */ 1526 if (srb->flag & AUTO_REQSENSE) 1527 identify_message &= 0xBF; 1528 1529 if (((srb->cmd->cmnd[0] == INQUIRY) 1530 || (srb->cmd->cmnd[0] == REQUEST_SENSE) 1531 || (srb->flag & AUTO_REQSENSE)) 1532 && (((dcb->sync_mode & WIDE_NEGO_ENABLE) 1533 && !(dcb->sync_mode & WIDE_NEGO_DONE)) 1534 || ((dcb->sync_mode & SYNC_NEGO_ENABLE) 1535 && !(dcb->sync_mode & SYNC_NEGO_DONE))) 1536 && (dcb->target_lun == 0)) { 1537 srb->msgout_buf[0] = identify_message; 1538 srb->msg_count = 1; 1539 scsicommand = SCMD_SEL_ATNSTOP; 1540 srb->state = SRB_MSGOUT; 1541#ifndef SYNC_FIRST 1542 if (dcb->sync_mode & WIDE_NEGO_ENABLE 1543 && dcb->inquiry7 & SCSI_INQ_WBUS16) { 1544 build_wdtr(acb, dcb, srb); 1545 goto no_cmd; 1546 } 1547#endif 1548 if (dcb->sync_mode & SYNC_NEGO_ENABLE 1549 && dcb->inquiry7 & SCSI_INQ_SYNC) { 1550 build_sdtr(acb, dcb, srb); 1551 goto no_cmd; 1552 } 1553 if (dcb->sync_mode & WIDE_NEGO_ENABLE 1554 && dcb->inquiry7 & SCSI_INQ_WBUS16) { 1555 build_wdtr(acb, dcb, srb); 1556 goto no_cmd; 1557 } 1558 srb->msg_count = 0; 1559 } 1560 /* Send identify message */ 1561 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message); 1562 1563 scsicommand = SCMD_SEL_ATN; 1564 srb->state = SRB_START_; 1565#ifndef DC395x_NO_TAGQ 1566 if ((dcb->sync_mode & EN_TAG_QUEUEING) 1567 && (identify_message & 0xC0)) { 1568 /* Send Tag message */ 1569 u32 tag_mask = 1; 1570 u8 tag_number = 0; 1571 while (tag_mask & dcb->tag_mask 1572 && tag_number <= dcb->max_command) { 1573 tag_mask = tag_mask << 1; 1574 tag_number++; 1575 } 1576 if (tag_number >= dcb->max_command) { 1577 dprintkl(KERN_WARNING, "start_scsi: (pid#%li) " 1578 "Out of tags target=<%02i-%i>)\n", 1579 srb->cmd->pid, srb->cmd->device->id, 1580 srb->cmd->device->lun); 1581 srb->state = SRB_READY; 1582 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, 1583 DO_HWRESELECT); 1584 return 1; 1585 } 1586 /* Send Tag id */ 1587 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG); 1588 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number); 1589 dcb->tag_mask |= tag_mask; 1590 srb->tag_number = tag_number; 1591 scsicommand = SCMD_SEL_ATN3; 1592 srb->state = SRB_START_; 1593 } 1594#endif 1595/*polling:*/ 1596 /* Send CDB ..command block ......... */ 1597 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n", 1598 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun, 1599 srb->cmd->cmnd[0], srb->tag_number); 1600 if (srb->flag & AUTO_REQSENSE) { 1601 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE); 1602 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5)); 1603 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0); 1604 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0); 1605 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 1606 sizeof(srb->cmd->sense_buffer)); 1607 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0); 1608 } else { 1609 ptr = (u8 *)srb->cmd->cmnd; 1610 for (i = 0; i < srb->cmd->cmd_len; i++) 1611 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++); 1612 } 1613 no_cmd: 1614 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, 1615 DO_HWRESELECT | DO_DATALATCH); 1616 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) { 1617 /* 1618 * If start_scsi return 1: 1619 * we caught an interrupt (must be reset or reselection ... ) 1620 * : Let's process it first! 1621 */ 1622 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n", 1623 srb->cmd->pid, dcb->target_id, dcb->target_lun); 1624 srb->state = SRB_READY; 1625 free_tag(dcb, srb); 1626 srb->msg_count = 0; 1627 return_code = 1; 1628 /* This IRQ should NOT get lost, as we did not acknowledge it */ 1629 } else { 1630 /* 1631 * If start_scsi returns 0: 1632 * we know that the SCSI processor is free 1633 */ 1634 srb->scsi_phase = PH_BUS_FREE; /* initial phase */ 1635 dcb->active_srb = srb; 1636 acb->active_dcb = dcb; 1637 return_code = 0; 1638 /* it's important for atn stop */ 1639 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, 1640 DO_DATALATCH | DO_HWRESELECT); 1641 /* SCSI command */ 1642 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand); 1643 } 1644 return return_code; 1645} 1646 1647 1648#define DC395x_ENABLE_MSGOUT \ 1649 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \ 1650 srb->state |= SRB_MSGOUT 1651 1652 1653/* abort command */ 1654static inline void enable_msgout_abort(struct AdapterCtlBlk *acb, 1655 struct ScsiReqBlk *srb) 1656{ 1657 srb->msgout_buf[0] = ABORT; 1658 srb->msg_count = 1; 1659 DC395x_ENABLE_MSGOUT; 1660 srb->state &= ~SRB_MSGIN; 1661 srb->state |= SRB_MSGOUT; 1662} 1663 1664 1665/** 1666 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to 1667 * have been triggered for this card. 1668 * 1669 * @acb: a pointer to the adpter control block 1670 * @scsi_status: the status return when we checked the card 1671 **/ 1672static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb, 1673 u16 scsi_status) 1674{ 1675 struct DeviceCtlBlk *dcb; 1676 struct ScsiReqBlk *srb; 1677 u16 phase; 1678 u8 scsi_intstatus; 1679 unsigned long flags; 1680 void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *, 1681 u16 *); 1682 1683 DC395x_LOCK_IO(acb->scsi_host, flags); 1684 1685 /* This acknowledges the IRQ */ 1686 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS); 1687 if ((scsi_status & 0x2007) == 0x2002) 1688 dprintkl(KERN_DEBUG, 1689 "COP after COP completed? %04x\n", scsi_status); 1690 if (debug_enabled(DBG_KG)) { 1691 if (scsi_intstatus & INT_SELTIMEOUT) 1692 dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n"); 1693 } 1694 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */ 1695 1696 if (timer_pending(&acb->selto_timer)) 1697 del_timer(&acb->selto_timer); 1698 1699 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) { 1700 disconnect(acb); /* bus free interrupt */ 1701 goto out_unlock; 1702 } 1703 if (scsi_intstatus & INT_RESELECTED) { 1704 reselect(acb); 1705 goto out_unlock; 1706 } 1707 if (scsi_intstatus & INT_SELECT) { 1708 dprintkl(KERN_INFO, "Host does not support target mode!\n"); 1709 goto out_unlock; 1710 } 1711 if (scsi_intstatus & INT_SCSIRESET) { 1712 scsi_reset_detect(acb); 1713 goto out_unlock; 1714 } 1715 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) { 1716 dcb = acb->active_dcb; 1717 if (!dcb) { 1718 dprintkl(KERN_DEBUG, 1719 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n", 1720 scsi_status, scsi_intstatus); 1721 goto out_unlock; 1722 } 1723 srb = dcb->active_srb; 1724 if (dcb->flag & ABORT_DEV_) { 1725 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n"); 1726 enable_msgout_abort(acb, srb); 1727 } 1728 1729 /* software sequential machine */ 1730 phase = (u16)srb->scsi_phase; 1731 1732 /* 1733 * 62037 or 62137 1734 * call dc395x_scsi_phase0[]... "phase entry" 1735 * handle every phase before start transfer 1736 */ 1737 /* data_out_phase0, phase:0 */ 1738 /* data_in_phase0, phase:1 */ 1739 /* command_phase0, phase:2 */ 1740 /* status_phase0, phase:3 */ 1741 /* nop0, phase:4 PH_BUS_FREE .. initial phase */ 1742 /* nop0, phase:5 PH_BUS_FREE .. initial phase */ 1743 /* msgout_phase0, phase:6 */ 1744 /* msgin_phase0, phase:7 */ 1745 dc395x_statev = dc395x_scsi_phase0[phase]; 1746 dc395x_statev(acb, srb, &scsi_status); 1747 1748 /* 1749 * if there were any exception occured scsi_status 1750 * will be modify to bus free phase new scsi_status 1751 * transfer out from ... previous dc395x_statev 1752 */ 1753 srb->scsi_phase = scsi_status & PHASEMASK; 1754 phase = (u16)scsi_status & PHASEMASK; 1755 1756 /* 1757 * call dc395x_scsi_phase1[]... "phase entry" handle 1758 * every phase to do transfer 1759 */ 1760 /* data_out_phase1, phase:0 */ 1761 /* data_in_phase1, phase:1 */ 1762 /* command_phase1, phase:2 */ 1763 /* status_phase1, phase:3 */ 1764 /* nop1, phase:4 PH_BUS_FREE .. initial phase */ 1765 /* nop1, phase:5 PH_BUS_FREE .. initial phase */ 1766 /* msgout_phase1, phase:6 */ 1767 /* msgin_phase1, phase:7 */ 1768 dc395x_statev = dc395x_scsi_phase1[phase]; 1769 dc395x_statev(acb, srb, &scsi_status); 1770 } 1771 out_unlock: 1772 DC395x_UNLOCK_IO(acb->scsi_host, flags); 1773} 1774 1775 1776static irqreturn_t dc395x_interrupt(int irq, void *dev_id) 1777{ 1778 struct AdapterCtlBlk *acb = dev_id; 1779 u16 scsi_status; 1780 u8 dma_status; 1781 irqreturn_t handled = IRQ_NONE; 1782 1783 /* 1784 * Check for pending interupt 1785 */ 1786 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS); 1787 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS); 1788 if (scsi_status & SCSIINTERRUPT) { 1789 /* interupt pending - let's process it! */ 1790 dc395x_handle_interrupt(acb, scsi_status); 1791 handled = IRQ_HANDLED; 1792 } 1793 else if (dma_status & 0x20) { 1794 /* Error from the DMA engine */ 1795 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status); 1796 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n"); 1797 acb = NULL; 1798 handled = IRQ_HANDLED; 1799 } 1800 1801 return handled; 1802} 1803 1804 1805static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 1806 u16 *pscsi_status) 1807{ 1808 dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->pid); 1809 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT)) 1810 *pscsi_status = PH_BUS_FREE; /*.. initial phase */ 1811 1812 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 1813 srb->state &= ~SRB_MSGOUT; 1814} 1815 1816 1817static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 1818 u16 *pscsi_status) 1819{ 1820 u16 i; 1821 u8 *ptr; 1822 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->pid); 1823 1824 clear_fifo(acb, "msgout_phase1"); 1825 if (!(srb->state & SRB_MSGOUT)) { 1826 srb->state |= SRB_MSGOUT; 1827 dprintkl(KERN_DEBUG, 1828 "msgout_phase1: (pid#%li) Phase unexpected\n", 1829 srb->cmd->pid); /* So what ? */ 1830 } 1831 if (!srb->msg_count) { 1832 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n", 1833 srb->cmd->pid); 1834 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP); 1835 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 1836 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT); 1837 return; 1838 } 1839 ptr = (u8 *)srb->msgout_buf; 1840 for (i = 0; i < srb->msg_count; i++) 1841 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++); 1842 srb->msg_count = 0; 1843 if (srb->msgout_buf[0] == MSG_ABORT) 1844 srb->state = SRB_ABORT_SENT; 1845 1846 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT); 1847} 1848 1849 1850static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 1851 u16 *pscsi_status) 1852{ 1853 dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->pid); 1854 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); 1855} 1856 1857 1858static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 1859 u16 *pscsi_status) 1860{ 1861 struct DeviceCtlBlk *dcb; 1862 u8 *ptr; 1863 u16 i; 1864 dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->pid); 1865 1866 clear_fifo(acb, "command_phase1"); 1867 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN); 1868 if (!(srb->flag & AUTO_REQSENSE)) { 1869 ptr = (u8 *)srb->cmd->cmnd; 1870 for (i = 0; i < srb->cmd->cmd_len; i++) { 1871 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr); 1872 ptr++; 1873 } 1874 } else { 1875 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE); 1876 dcb = acb->active_dcb; 1877 /* target id */ 1878 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5)); 1879 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0); 1880 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0); 1881 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 1882 sizeof(srb->cmd->sense_buffer)); 1883 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0); 1884 } 1885 srb->state |= SRB_COMMAND; 1886 /* it's important for atn stop */ 1887 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); 1888 /* SCSI command */ 1889 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT); 1890} 1891 1892 1893/* 1894 * Verify that the remaining space in the hw sg lists is the same as 1895 * the count of remaining bytes in srb->total_xfer_length 1896 */ 1897static void sg_verify_length(struct ScsiReqBlk *srb) 1898{ 1899 if (debug_enabled(DBG_SG)) { 1900 unsigned len = 0; 1901 unsigned idx = srb->sg_index; 1902 struct SGentry *psge = srb->segment_x + idx; 1903 for (; idx < srb->sg_count; psge++, idx++) 1904 len += psge->length; 1905 if (len != srb->total_xfer_length) 1906 dprintkdbg(DBG_SG, 1907 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n", 1908 srb->total_xfer_length, len); 1909 } 1910} 1911 1912 1913/* 1914 * Compute the next Scatter Gather list index and adjust its length 1915 * and address if necessary 1916 */ 1917static void sg_update_list(struct ScsiReqBlk *srb, u32 left) 1918{ 1919 u8 idx; 1920 u32 xferred = srb->total_xfer_length - left; /* bytes transfered */ 1921 struct SGentry *psge = srb->segment_x + srb->sg_index; 1922 1923 dprintkdbg(DBG_0, 1924 "sg_update_list: Transfered %i of %i bytes, %i remain\n", 1925 xferred, srb->total_xfer_length, left); 1926 if (xferred == 0) { 1927 /* nothing to update since we did not transfer any data */ 1928 return; 1929 } 1930 1931 sg_verify_length(srb); 1932 srb->total_xfer_length = left; /* update remaining count */ 1933 for (idx = srb->sg_index; idx < srb->sg_count; idx++) { 1934 if (xferred >= psge->length) { 1935 /* Complete SG entries done */ 1936 xferred -= psge->length; 1937 } else { 1938 /* Partial SG entry done */ 1939 psge->length -= xferred; 1940 psge->address += xferred; 1941 srb->sg_index = idx; 1942 pci_dma_sync_single_for_device(srb->dcb-> 1943 acb->dev, 1944 srb->sg_bus_addr, 1945 SEGMENTX_LEN, 1946 PCI_DMA_TODEVICE); 1947 break; 1948 } 1949 psge++; 1950 } 1951 sg_verify_length(srb); 1952} 1953 1954 1955/* 1956 * We have transfered a single byte (PIO mode?) and need to update 1957 * the count of bytes remaining (total_xfer_length) and update the sg 1958 * entry to either point to next byte in the current sg entry, or of 1959 * already at the end to point to the start of the next sg entry 1960 */ 1961static void sg_subtract_one(struct ScsiReqBlk *srb) 1962{ 1963 sg_update_list(srb, srb->total_xfer_length - 1); 1964} 1965 1966 1967/* 1968 * cleanup_after_transfer 1969 * 1970 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished 1971 * KG: Currently called from StatusPhase1 () 1972 * Should probably also be called from other places 1973 * Best might be to call it in DataXXPhase0, if new phase will differ 1974 */ 1975static void cleanup_after_transfer(struct AdapterCtlBlk *acb, 1976 struct ScsiReqBlk *srb) 1977{ 1978 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */ 1979 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */ 1980 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40)) 1981 clear_fifo(acb, "cleanup/in"); 1982 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) 1983 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO); 1984 } else { /* write */ 1985 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) 1986 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO); 1987 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40)) 1988 clear_fifo(acb, "cleanup/out"); 1989 } 1990 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); 1991} 1992 1993 1994/* 1995 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO 1996 * Seems to be needed for unknown reasons; could be a hardware bug :-( 1997 */ 1998#define DC395x_LASTPIO 4 1999 2000 2001static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2002 u16 *pscsi_status) 2003{ 2004 struct DeviceCtlBlk *dcb = srb->dcb; 2005 u16 scsi_status = *pscsi_status; 2006 u32 d_left_counter = 0; 2007 dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n", 2008 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun); 2009 2010 /* 2011 * KG: We need to drain the buffers before we draw any conclusions! 2012 * This means telling the DMA to push the rest into SCSI, telling 2013 * SCSI to push the rest to the bus. 2014 * However, the device might have been the one to stop us (phase 2015 * change), and the data in transit just needs to be accounted so 2016 * it can be retransmitted.) 2017 */ 2018 /* 2019 * KG: Stop DMA engine pushing more data into the SCSI FIFO 2020 * If we need more data, the DMA SG list will be freshly set up, anyway 2021 */ 2022 dprintkdbg(DBG_PIO, "data_out_phase0: " 2023 "DMA{fifocnt=0x%02x fifostat=0x%02x} " 2024 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n", 2025 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT), 2026 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT), 2027 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT), 2028 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status, 2029 srb->total_xfer_length); 2030 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO); 2031 2032 if (!(srb->state & SRB_XFERPAD)) { 2033 if (scsi_status & PARITYERROR) 2034 srb->status |= PARITY_ERROR; 2035 2036 /* 2037 * KG: Right, we can't just rely on the SCSI_COUNTER, because this 2038 * is the no of bytes it got from the DMA engine not the no it 2039 * transferred successfully to the device. (And the difference could 2040 * be as much as the FIFO size, I guess ...) 2041 */ 2042 if (!(scsi_status & SCSIXFERDONE)) { 2043 /* 2044 * when data transfer from DMA FIFO to SCSI FIFO 2045 * if there was some data left in SCSI FIFO 2046 */ 2047 d_left_counter = 2048 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 2049 0x1F); 2050 if (dcb->sync_period & WIDE_SYNC) 2051 d_left_counter <<= 1; 2052 2053 dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n" 2054 "SCSI{fifocnt=0x%02x cnt=0x%08x} " 2055 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n", 2056 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT), 2057 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes", 2058 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT), 2059 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), 2060 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT), 2061 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT), 2062 DC395x_read32(acb, TRM_S1040_DMA_CXCNT)); 2063 } 2064 /* 2065 * calculate all the residue data that not yet tranfered 2066 * SCSI transfer counter + left in SCSI FIFO data 2067 * 2068 * .....TRM_S1040_SCSI_COUNTER (24bits) 2069 * The counter always decrement by one for every SCSI byte transfer. 2070 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits) 2071 * The counter is SCSI FIFO offset counter (in units of bytes or! words) 2072 */ 2073 if (srb->total_xfer_length > DC395x_LASTPIO) 2074 d_left_counter += 2075 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER); 2076 2077 /* Is this a good idea? */ 2078 /*clear_fifo(acb, "DOP1"); */ 2079 /* KG: What is this supposed to be useful for? WIDE padding stuff? */ 2080 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC 2081 && srb->cmd->request_bufflen % 2) { 2082 d_left_counter = 0; 2083 dprintkl(KERN_INFO, 2084 "data_out_phase0: Discard 1 byte (0x%02x)\n", 2085 scsi_status); 2086 } 2087 /* 2088 * KG: Oops again. Same thinko as above: The SCSI might have been 2089 * faster than the DMA engine, so that it ran out of data. 2090 * In that case, we have to do just nothing! 2091 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or? 2092 */ 2093 /* 2094 * KG: This is nonsense: We have been WRITING data to the bus 2095 * If the SCSI engine has no bytes left, how should the DMA engine? 2096 */ 2097 if (d_left_counter == 0) { 2098 srb->total_xfer_length = 0; 2099 } else { 2100 /* 2101 * if transfer not yet complete 2102 * there were some data residue in SCSI FIFO or 2103 * SCSI transfer counter not empty 2104 */ 2105 long oldxferred = 2106 srb->total_xfer_length - d_left_counter; 2107 const int diff = 2108 (dcb->sync_period & WIDE_SYNC) ? 2 : 1; 2109 sg_update_list(srb, d_left_counter); 2110 /* KG: Most ugly hack! Apparently, this works around a chip bug */ 2111 if ((srb->segment_x[srb->sg_index].length == 2112 diff && srb->cmd->use_sg) 2113 || ((oldxferred & ~PAGE_MASK) == 2114 (PAGE_SIZE - diff)) 2115 ) { 2116 dprintkl(KERN_INFO, "data_out_phase0: " 2117 "Work around chip bug (%i)?\n", diff); 2118 d_left_counter = 2119 srb->total_xfer_length - diff; 2120 sg_update_list(srb, d_left_counter); 2121 /*srb->total_xfer_length -= diff; */ 2122 /*srb->virt_addr += diff; */ 2123 /*if (srb->cmd->use_sg) */ 2124 /* srb->sg_index++; */ 2125 } 2126 } 2127 } 2128 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) { 2129 cleanup_after_transfer(acb, srb); 2130 } 2131} 2132 2133 2134static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2135 u16 *pscsi_status) 2136{ 2137 dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n", 2138 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun); 2139 clear_fifo(acb, "data_out_phase1"); 2140 /* do prepare before transfer when data out phase */ 2141 data_io_transfer(acb, srb, XFERDATAOUT); 2142} 2143 2144static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2145 u16 *pscsi_status) 2146{ 2147 u16 scsi_status = *pscsi_status; 2148 2149 dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n", 2150 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun); 2151 2152 /* 2153 * KG: DataIn is much more tricky than DataOut. When the device is finished 2154 * and switches to another phase, the SCSI engine should be finished too. 2155 * But: There might still be bytes left in its FIFO to be fetched by the DMA 2156 * engine and transferred to memory. 2157 * We should wait for the FIFOs to be emptied by that (is there any way to 2158 * enforce this?) and then stop the DMA engine, because it might think, that 2159 * there are more bytes to follow. Yes, the device might disconnect prior to 2160 * having all bytes transferred! 2161 * Also we should make sure that all data from the DMA engine buffer's really 2162 * made its way to the system memory! Some documentation on this would not 2163 * seem to be a bad idea, actually. 2164 */ 2165 if (!(srb->state & SRB_XFERPAD)) { 2166 u32 d_left_counter; 2167 unsigned int sc, fc; 2168 2169 if (scsi_status & PARITYERROR) { 2170 dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) " 2171 "Parity Error\n", srb->cmd->pid); 2172 srb->status |= PARITY_ERROR; 2173 } 2174 /* 2175 * KG: We should wait for the DMA FIFO to be empty ... 2176 * but: it would be better to wait first for the SCSI FIFO and then the 2177 * the DMA FIFO to become empty? How do we know, that the device not already 2178 * sent data to the FIFO in a MsgIn phase, eg.? 2179 */ 2180 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) { 2181 dprintkdbg(DBG_KG, "data_in_phase0: " 2182 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n", 2183 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT), 2184 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT)); 2185 } 2186 /* Now: Check remainig data: The SCSI counters should tell us ... */ 2187 sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER); 2188 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT); 2189 d_left_counter = sc + ((fc & 0x1f) 2190 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 : 2191 0)); 2192 dprintkdbg(DBG_KG, "data_in_phase0: " 2193 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} " 2194 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} " 2195 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n", 2196 fc, 2197 (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes", 2198 sc, 2199 fc, 2200 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT), 2201 DC395x_read32(acb, TRM_S1040_DMA_CXCNT), 2202 srb->total_xfer_length, d_left_counter); 2203#if DC395x_LASTPIO 2204 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */ 2205 if (d_left_counter 2206 && srb->total_xfer_length <= DC395x_LASTPIO) { 2207 size_t left_io = srb->total_xfer_length; 2208 2209 /*u32 addr = (srb->segment_x[srb->sg_index].address); */ 2210 /*sg_update_list (srb, d_left_counter); */ 2211 dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) " 2212 "for remaining %i bytes:", 2213 fc & 0x1f, 2214 (srb->dcb->sync_period & WIDE_SYNC) ? 2215 "words" : "bytes", 2216 srb->total_xfer_length); 2217 if (srb->dcb->sync_period & WIDE_SYNC) 2218 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 2219 CFG2_WIDEFIFO); 2220 while (left_io) { 2221 unsigned char *virt, *base = NULL; 2222 unsigned long flags = 0; 2223 size_t len = left_io; 2224 2225 if (srb->cmd->use_sg) { 2226 size_t offset = srb->request_length - left_io; 2227 local_irq_save(flags); 2228 /* Assumption: it's inside one page as it's at most 4 bytes and 2229 I just assume it's on a 4-byte boundary */ 2230 base = scsi_kmap_atomic_sg((struct scatterlist *)srb->cmd->request_buffer, 2231 srb->sg_count, &offset, &len); 2232 virt = base + offset; 2233 } else { 2234 virt = srb->cmd->request_buffer + srb->cmd->request_bufflen - left_io; 2235 len = left_io; 2236 } 2237 left_io -= len; 2238 2239 while (len) { 2240 u8 byte; 2241 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2242 *virt++ = byte; 2243 2244 if (debug_enabled(DBG_PIO)) 2245 printk(" %02x", byte); 2246 2247 d_left_counter--; 2248 sg_subtract_one(srb); 2249 2250 len--; 2251 2252 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT); 2253 2254 if (fc == 0x40) { 2255 left_io = 0; 2256 break; 2257 } 2258 } 2259 2260 WARN_ON((fc != 0x40) == !d_left_counter); 2261 2262 if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) { 2263 /* Read the last byte ... */ 2264 if (srb->total_xfer_length > 0) { 2265 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2266 2267 *virt++ = byte; 2268 srb->total_xfer_length--; 2269 if (debug_enabled(DBG_PIO)) 2270 printk(" %02x", byte); 2271 } 2272 2273 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0); 2274 } 2275 2276 if (srb->cmd->use_sg) { 2277 scsi_kunmap_atomic_sg(base); 2278 local_irq_restore(flags); 2279 } 2280 } 2281 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */ 2282 /*srb->total_xfer_length = 0; */ 2283 if (debug_enabled(DBG_PIO)) 2284 printk("\n"); 2285 } 2286#endif /* DC395x_LASTPIO */ 2287 2288 /* KG: This should not be needed any more! */ 2289 if (d_left_counter == 0 2290 || (scsi_status & SCSIXFERCNT_2_ZERO)) { 2291 srb->total_xfer_length = d_left_counter; 2292 } else { /* phase changed */ 2293 /* 2294 * parsing the case: 2295 * when a transfer not yet complete 2296 * but be disconnected by target 2297 * if transfer not yet complete 2298 * there were some data residue in SCSI FIFO or 2299 * SCSI transfer counter not empty 2300 */ 2301 sg_update_list(srb, d_left_counter); 2302 } 2303 } 2304 /* KG: The target may decide to disconnect: Empty FIFO before! */ 2305 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) { 2306 cleanup_after_transfer(acb, srb); 2307 } 2308} 2309 2310 2311static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2312 u16 *pscsi_status) 2313{ 2314 dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n", 2315 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun); 2316 data_io_transfer(acb, srb, XFERDATAIN); 2317} 2318 2319 2320static void data_io_transfer(struct AdapterCtlBlk *acb, 2321 struct ScsiReqBlk *srb, u16 io_dir) 2322{ 2323 struct DeviceCtlBlk *dcb = srb->dcb; 2324 u8 bval; 2325 dprintkdbg(DBG_0, 2326 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n", 2327 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun, 2328 ((io_dir & DMACMD_DIR) ? 'r' : 'w'), 2329 srb->total_xfer_length, srb->sg_index, srb->sg_count); 2330 if (srb == acb->tmp_srb) 2331 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n"); 2332 if (srb->sg_index >= srb->sg_count) { 2333 /* can't happen? out of bounds error */ 2334 return; 2335 } 2336 2337 if (srb->total_xfer_length > DC395x_LASTPIO) { 2338 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS); 2339 /* 2340 * KG: What should we do: Use SCSI Cmd 0x90/0x92? 2341 * Maybe, even ABORTXFER would be appropriate 2342 */ 2343 if (dma_status & XFERPENDING) { 2344 dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! " 2345 "Expect trouble!\n"); 2346 dump_register_info(acb, dcb, srb); 2347 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO); 2348 } 2349 /* clear_fifo(acb, "IO"); */ 2350 /* 2351 * load what physical address of Scatter/Gather list table 2352 * want to be transfer 2353 */ 2354 srb->state |= SRB_DATA_XFER; 2355 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0); 2356 if (srb->cmd->use_sg) { /* with S/G */ 2357 io_dir |= DMACMD_SG; 2358 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR, 2359 srb->sg_bus_addr + 2360 sizeof(struct SGentry) * 2361 srb->sg_index); 2362 /* load how many bytes in the sg list table */ 2363 DC395x_write32(acb, TRM_S1040_DMA_XCNT, 2364 ((u32)(srb->sg_count - 2365 srb->sg_index) << 3)); 2366 } else { /* without S/G */ 2367 io_dir &= ~DMACMD_SG; 2368 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR, 2369 srb->segment_x[0].address); 2370 DC395x_write32(acb, TRM_S1040_DMA_XCNT, 2371 srb->segment_x[0].length); 2372 } 2373 /* load total transfer length (24bits) max value 16Mbyte */ 2374 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2375 srb->total_xfer_length); 2376 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 2377 if (io_dir & DMACMD_DIR) { /* read */ 2378 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, 2379 SCMD_DMA_IN); 2380 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir); 2381 } else { 2382 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir); 2383 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, 2384 SCMD_DMA_OUT); 2385 } 2386 2387 } 2388#if DC395x_LASTPIO 2389 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */ 2390 /* 2391 * load what physical address of Scatter/Gather list table 2392 * want to be transfer 2393 */ 2394 srb->state |= SRB_DATA_XFER; 2395 /* load total transfer length (24bits) max value 16Mbyte */ 2396 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2397 srb->total_xfer_length); 2398 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 2399 if (io_dir & DMACMD_DIR) { /* read */ 2400 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, 2401 SCMD_FIFO_IN); 2402 } else { /* write */ 2403 int ln = srb->total_xfer_length; 2404 size_t left_io = srb->total_xfer_length; 2405 2406 if (srb->dcb->sync_period & WIDE_SYNC) 2407 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 2408 CFG2_WIDEFIFO); 2409 2410 while (left_io) { 2411 unsigned char *virt, *base = NULL; 2412 unsigned long flags = 0; 2413 size_t len = left_io; 2414 2415 if (srb->cmd->use_sg) { 2416 size_t offset = srb->request_length - left_io; 2417 local_irq_save(flags); 2418 /* Again, max 4 bytes */ 2419 base = scsi_kmap_atomic_sg((struct scatterlist *)srb->cmd->request_buffer, 2420 srb->sg_count, &offset, &len); 2421 virt = base + offset; 2422 } else { 2423 virt = srb->cmd->request_buffer + srb->cmd->request_bufflen - left_io; 2424 len = left_io; 2425 } 2426 left_io -= len; 2427 2428 while (len--) { 2429 if (debug_enabled(DBG_PIO)) 2430 printk(" %02x", *virt); 2431 2432 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++); 2433 2434 sg_subtract_one(srb); 2435 } 2436 2437 if (srb->cmd->use_sg) { 2438 scsi_kunmap_atomic_sg(base); 2439 local_irq_restore(flags); 2440 } 2441 } 2442 if (srb->dcb->sync_period & WIDE_SYNC) { 2443 if (ln % 2) { 2444 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0); 2445 if (debug_enabled(DBG_PIO)) 2446 printk(" |00"); 2447 } 2448 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0); 2449 } 2450 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */ 2451 if (debug_enabled(DBG_PIO)) 2452 printk("\n"); 2453 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, 2454 SCMD_FIFO_OUT); 2455 } 2456 } 2457#endif /* DC395x_LASTPIO */ 2458 else { /* xfer pad */ 2459 u8 data = 0, data2 = 0; 2460 if (srb->sg_count) { 2461 srb->adapter_status = H_OVER_UNDER_RUN; 2462 srb->status |= OVER_RUN; 2463 } 2464 /* 2465 * KG: despite the fact that we are using 16 bits I/O ops 2466 * the SCSI FIFO is only 8 bits according to the docs 2467 * (we can set bit 1 in 0x8f to serialize FIFO access ...) 2468 */ 2469 if (dcb->sync_period & WIDE_SYNC) { 2470 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2); 2471 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 2472 CFG2_WIDEFIFO); 2473 if (io_dir & DMACMD_DIR) { 2474 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2475 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2476 } else { 2477 /* Danger, Robinson: If you find KGs 2478 * scattered over the wide disk, the driver 2479 * or chip is to blame :-( */ 2480 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K'); 2481 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G'); 2482 } 2483 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0); 2484 } else { 2485 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1); 2486 /* Danger, Robinson: If you find a collection of Ks on your disk 2487 * something broke :-( */ 2488 if (io_dir & DMACMD_DIR) 2489 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2490 else 2491 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K'); 2492 } 2493 srb->state |= SRB_XFERPAD; 2494 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 2495 /* SCSI command */ 2496 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT; 2497 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval); 2498 } 2499} 2500 2501 2502static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2503 u16 *pscsi_status) 2504{ 2505 dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n", 2506 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun); 2507 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2508 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */ 2509 srb->state = SRB_COMPLETED; 2510 *pscsi_status = PH_BUS_FREE; /*.. initial phase */ 2511 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 2512 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT); 2513} 2514 2515 2516static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2517 u16 *pscsi_status) 2518{ 2519 dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n", 2520 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun); 2521 srb->state = SRB_STATUS; 2522 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 2523 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP); 2524} 2525 2526 2527/* Check if the message is complete */ 2528static inline u8 msgin_completed(u8 * msgbuf, u32 len) 2529{ 2530 if (*msgbuf == EXTENDED_MESSAGE) { 2531 if (len < 2) 2532 return 0; 2533 if (len < msgbuf[1] + 2) 2534 return 0; 2535 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */ 2536 if (len < 2) 2537 return 0; 2538 return 1; 2539} 2540 2541/* reject_msg */ 2542static inline void msgin_reject(struct AdapterCtlBlk *acb, 2543 struct ScsiReqBlk *srb) 2544{ 2545 srb->msgout_buf[0] = MESSAGE_REJECT; 2546 srb->msg_count = 1; 2547 DC395x_ENABLE_MSGOUT; 2548 srb->state &= ~SRB_MSGIN; 2549 srb->state |= SRB_MSGOUT; 2550 dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n", 2551 srb->msgin_buf[0], 2552 srb->dcb->target_id, srb->dcb->target_lun); 2553} 2554 2555 2556static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb, 2557 struct DeviceCtlBlk *dcb, u8 tag) 2558{ 2559 struct ScsiReqBlk *srb = NULL; 2560 struct ScsiReqBlk *i; 2561 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n", 2562 srb->cmd->pid, tag, srb); 2563 2564 if (!(dcb->tag_mask & (1 << tag))) 2565 dprintkl(KERN_DEBUG, 2566 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n", 2567 dcb->tag_mask, tag); 2568 2569 if (list_empty(&dcb->srb_going_list)) 2570 goto mingx0; 2571 list_for_each_entry(i, &dcb->srb_going_list, list) { 2572 if (i->tag_number == tag) { 2573 srb = i; 2574 break; 2575 } 2576 } 2577 if (!srb) 2578 goto mingx0; 2579 2580 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n", 2581 srb->cmd->pid, srb->dcb->target_id, srb->dcb->target_lun); 2582 if (dcb->flag & ABORT_DEV_) { 2583 /*srb->state = SRB_ABORT_SENT; */ 2584 enable_msgout_abort(acb, srb); 2585 } 2586 2587 if (!(srb->state & SRB_DISCONNECT)) 2588 goto mingx0; 2589 2590 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len); 2591 srb->state |= dcb->active_srb->state; 2592 srb->state |= SRB_DATA_XFER; 2593 dcb->active_srb = srb; 2594 /* How can we make the DORS happy? */ 2595 return srb; 2596 2597 mingx0: 2598 srb = acb->tmp_srb; 2599 srb->state = SRB_UNEXPECT_RESEL; 2600 dcb->active_srb = srb; 2601 srb->msgout_buf[0] = MSG_ABORT_TAG; 2602 srb->msg_count = 1; 2603 DC395x_ENABLE_MSGOUT; 2604 dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag); 2605 return srb; 2606} 2607 2608 2609static inline void reprogram_regs(struct AdapterCtlBlk *acb, 2610 struct DeviceCtlBlk *dcb) 2611{ 2612 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); 2613 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); 2614 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); 2615 set_xfer_rate(acb, dcb); 2616} 2617 2618 2619/* set async transfer mode */ 2620static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb) 2621{ 2622 struct DeviceCtlBlk *dcb = srb->dcb; 2623 dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n", 2624 dcb->target_id, dcb->target_lun); 2625 2626 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE); 2627 dcb->sync_mode |= SYNC_NEGO_DONE; 2628 /*dcb->sync_period &= 0; */ 2629 dcb->sync_offset = 0; 2630 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */ 2631 srb->state &= ~SRB_DO_SYNC_NEGO; 2632 reprogram_regs(acb, dcb); 2633 if ((dcb->sync_mode & WIDE_NEGO_ENABLE) 2634 && !(dcb->sync_mode & WIDE_NEGO_DONE)) { 2635 build_wdtr(acb, dcb, srb); 2636 DC395x_ENABLE_MSGOUT; 2637 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n"); 2638 } 2639} 2640 2641 2642/* set sync transfer mode */ 2643static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb) 2644{ 2645 struct DeviceCtlBlk *dcb = srb->dcb; 2646 u8 bval; 2647 int fact; 2648 dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins " 2649 "(%02i.%01i MHz) Offset %i\n", 2650 dcb->target_id, srb->msgin_buf[3] << 2, 2651 (250 / srb->msgin_buf[3]), 2652 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3], 2653 srb->msgin_buf[4]); 2654 2655 if (srb->msgin_buf[4] > 15) 2656 srb->msgin_buf[4] = 15; 2657 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) 2658 dcb->sync_offset = 0; 2659 else if (dcb->sync_offset == 0) 2660 dcb->sync_offset = srb->msgin_buf[4]; 2661 if (srb->msgin_buf[4] > dcb->sync_offset) 2662 srb->msgin_buf[4] = dcb->sync_offset; 2663 else 2664 dcb->sync_offset = srb->msgin_buf[4]; 2665 bval = 0; 2666 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval] 2667 || dcb->min_nego_period > 2668 clock_period[bval])) 2669 bval++; 2670 if (srb->msgin_buf[3] < clock_period[bval]) 2671 dprintkl(KERN_INFO, 2672 "msgin_set_sync: Increase sync nego period to %ins\n", 2673 clock_period[bval] << 2); 2674 srb->msgin_buf[3] = clock_period[bval]; 2675 dcb->sync_period &= 0xf0; 2676 dcb->sync_period |= ALT_SYNC | bval; 2677 dcb->min_nego_period = srb->msgin_buf[3]; 2678 2679 if (dcb->sync_period & WIDE_SYNC) 2680 fact = 500; 2681 else 2682 fact = 250; 2683 2684 dprintkl(KERN_INFO, 2685 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n", 2686 dcb->target_id, (fact == 500) ? "Wide16" : "", 2687 dcb->min_nego_period << 2, dcb->sync_offset, 2688 (fact / dcb->min_nego_period), 2689 ((fact % dcb->min_nego_period) * 10 + 2690 dcb->min_nego_period / 2) / dcb->min_nego_period); 2691 2692 if (!(srb->state & SRB_DO_SYNC_NEGO)) { 2693 /* Reply with corrected SDTR Message */ 2694 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n", 2695 srb->msgin_buf[3] << 2, srb->msgin_buf[4]); 2696 2697 memcpy(srb->msgout_buf, srb->msgin_buf, 5); 2698 srb->msg_count = 5; 2699 DC395x_ENABLE_MSGOUT; 2700 dcb->sync_mode |= SYNC_NEGO_DONE; 2701 } else { 2702 if ((dcb->sync_mode & WIDE_NEGO_ENABLE) 2703 && !(dcb->sync_mode & WIDE_NEGO_DONE)) { 2704 build_wdtr(acb, dcb, srb); 2705 DC395x_ENABLE_MSGOUT; 2706 dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n"); 2707 } 2708 } 2709 srb->state &= ~SRB_DO_SYNC_NEGO; 2710 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE; 2711 2712 reprogram_regs(acb, dcb); 2713} 2714 2715 2716static inline void msgin_set_nowide(struct AdapterCtlBlk *acb, 2717 struct ScsiReqBlk *srb) 2718{ 2719 struct DeviceCtlBlk *dcb = srb->dcb; 2720 dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id); 2721 2722 dcb->sync_period &= ~WIDE_SYNC; 2723 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE); 2724 dcb->sync_mode |= WIDE_NEGO_DONE; 2725 srb->state &= ~SRB_DO_WIDE_NEGO; 2726 reprogram_regs(acb, dcb); 2727 if ((dcb->sync_mode & SYNC_NEGO_ENABLE) 2728 && !(dcb->sync_mode & SYNC_NEGO_DONE)) { 2729 build_sdtr(acb, dcb, srb); 2730 DC395x_ENABLE_MSGOUT; 2731 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n"); 2732 } 2733} 2734 2735static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb) 2736{ 2737 struct DeviceCtlBlk *dcb = srb->dcb; 2738 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO 2739 && acb->config & HCC_WIDE_CARD) ? 1 : 0; 2740 dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id); 2741 2742 if (srb->msgin_buf[3] > wide) 2743 srb->msgin_buf[3] = wide; 2744 /* Completed */ 2745 if (!(srb->state & SRB_DO_WIDE_NEGO)) { 2746 dprintkl(KERN_DEBUG, 2747 "msgin_set_wide: Wide nego initiated <%02i>\n", 2748 dcb->target_id); 2749 memcpy(srb->msgout_buf, srb->msgin_buf, 4); 2750 srb->msg_count = 4; 2751 srb->state |= SRB_DO_WIDE_NEGO; 2752 DC395x_ENABLE_MSGOUT; 2753 } 2754 2755 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE); 2756 if (srb->msgin_buf[3] > 0) 2757 dcb->sync_period |= WIDE_SYNC; 2758 else 2759 dcb->sync_period &= ~WIDE_SYNC; 2760 srb->state &= ~SRB_DO_WIDE_NEGO; 2761 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */ 2762 dprintkdbg(DBG_1, 2763 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n", 2764 (8 << srb->msgin_buf[3]), dcb->target_id); 2765 reprogram_regs(acb, dcb); 2766 if ((dcb->sync_mode & SYNC_NEGO_ENABLE) 2767 && !(dcb->sync_mode & SYNC_NEGO_DONE)) { 2768 build_sdtr(acb, dcb, srb); 2769 DC395x_ENABLE_MSGOUT; 2770 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n"); 2771 } 2772} 2773 2774 2775/* 2776 * extended message codes: 2777 * 2778 * code description 2779 * 2780 * 02h Reserved 2781 * 00h MODIFY DATA POINTER 2782 * 01h SYNCHRONOUS DATA TRANSFER REQUEST 2783 * 03h WIDE DATA TRANSFER REQUEST 2784 * 04h - 7Fh Reserved 2785 * 80h - FFh Vendor specific 2786 */ 2787static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2788 u16 *pscsi_status) 2789{ 2790 struct DeviceCtlBlk *dcb = acb->active_dcb; 2791 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->pid); 2792 2793 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2794 if (msgin_completed(srb->msgin_buf, acb->msg_len)) { 2795 /* Now eval the msg */ 2796 switch (srb->msgin_buf[0]) { 2797 case DISCONNECT: 2798 srb->state = SRB_DISCONNECT; 2799 break; 2800 2801 case SIMPLE_QUEUE_TAG: 2802 case HEAD_OF_QUEUE_TAG: 2803 case ORDERED_QUEUE_TAG: 2804 srb = 2805 msgin_qtag(acb, dcb, 2806 srb->msgin_buf[1]); 2807 break; 2808 2809 case MESSAGE_REJECT: 2810 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, 2811 DO_CLRATN | DO_DATALATCH); 2812 /* A sync nego message was rejected ! */ 2813 if (srb->state & SRB_DO_SYNC_NEGO) { 2814 msgin_set_async(acb, srb); 2815 break; 2816 } 2817 /* A wide nego message was rejected ! */ 2818 if (srb->state & SRB_DO_WIDE_NEGO) { 2819 msgin_set_nowide(acb, srb); 2820 break; 2821 } 2822 enable_msgout_abort(acb, srb); 2823 /*srb->state |= SRB_ABORT_SENT */ 2824 break; 2825 2826 case EXTENDED_MESSAGE: 2827 /* SDTR */ 2828 if (srb->msgin_buf[1] == 3 2829 && srb->msgin_buf[2] == EXTENDED_SDTR) { 2830 msgin_set_sync(acb, srb); 2831 break; 2832 } 2833 /* WDTR */ 2834 if (srb->msgin_buf[1] == 2 2835 && srb->msgin_buf[2] == EXTENDED_WDTR 2836 && srb->msgin_buf[3] <= 2) { /* sanity check ... */ 2837 msgin_set_wide(acb, srb); 2838 break; 2839 } 2840 msgin_reject(acb, srb); 2841 break; 2842 2843 case MSG_IGNOREWIDE: 2844 /* Discard wide residual */ 2845 dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n"); 2846 break; 2847 2848 case COMMAND_COMPLETE: 2849 /* nothing has to be done */ 2850 break; 2851 2852 case SAVE_POINTERS: 2853 /* 2854 * SAVE POINTER may be ignored as we have the struct 2855 * ScsiReqBlk* associated with the scsi command. 2856 */ 2857 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) " 2858 "SAVE POINTER rem=%i Ignore\n", 2859 srb->cmd->pid, srb->total_xfer_length); 2860 break; 2861 2862 case RESTORE_POINTERS: 2863 dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n"); 2864 break; 2865 2866 case ABORT: 2867 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) " 2868 "<%02i-%i> ABORT msg\n", 2869 srb->cmd->pid, dcb->target_id, 2870 dcb->target_lun); 2871 dcb->flag |= ABORT_DEV_; 2872 enable_msgout_abort(acb, srb); 2873 break; 2874 2875 default: 2876 /* reject unknown messages */ 2877 if (srb->msgin_buf[0] & IDENTIFY_BASE) { 2878 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n"); 2879 srb->msg_count = 1; 2880 srb->msgout_buf[0] = dcb->identify_msg; 2881 DC395x_ENABLE_MSGOUT; 2882 srb->state |= SRB_MSGOUT; 2883 /*break; */ 2884 } 2885 msgin_reject(acb, srb); 2886 } 2887 2888 /* Clear counter and MsgIn state */ 2889 srb->state &= ~SRB_MSGIN; 2890 acb->msg_len = 0; 2891 } 2892 *pscsi_status = PH_BUS_FREE; 2893 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */ 2894 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT); 2895} 2896 2897 2898static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2899 u16 *pscsi_status) 2900{ 2901 dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->pid); 2902 clear_fifo(acb, "msgin_phase1"); 2903 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1); 2904 if (!(srb->state & SRB_MSGIN)) { 2905 srb->state &= ~SRB_DISCONNECT; 2906 srb->state |= SRB_MSGIN; 2907 } 2908 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 2909 /* SCSI command */ 2910 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN); 2911} 2912 2913 2914static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2915 u16 *pscsi_status) 2916{ 2917} 2918 2919 2920static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 2921 u16 *pscsi_status) 2922{ 2923} 2924 2925 2926static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb) 2927{ 2928 struct DeviceCtlBlk *i; 2929 2930 /* set all lun device's period, offset */ 2931 if (dcb->identify_msg & 0x07) 2932 return; 2933 2934 if (acb->scan_devices) { 2935 current_sync_offset = dcb->sync_offset; 2936 return; 2937 } 2938 2939 list_for_each_entry(i, &acb->dcb_list, list) 2940 if (i->target_id == dcb->target_id) { 2941 i->sync_period = dcb->sync_period; 2942 i->sync_offset = dcb->sync_offset; 2943 i->sync_mode = dcb->sync_mode; 2944 i->min_nego_period = dcb->min_nego_period; 2945 } 2946} 2947 2948 2949static void disconnect(struct AdapterCtlBlk *acb) 2950{ 2951 struct DeviceCtlBlk *dcb = acb->active_dcb; 2952 struct ScsiReqBlk *srb; 2953 2954 if (!dcb) { 2955 dprintkl(KERN_ERR, "disconnect: No such device\n"); 2956 udelay(500); 2957 /* Suspend queue for a while */ 2958 acb->scsi_host->last_reset = 2959 jiffies + HZ / 2 + 2960 HZ * acb->eeprom.delay_time; 2961 clear_fifo(acb, "disconnectEx"); 2962 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); 2963 return; 2964 } 2965 srb = dcb->active_srb; 2966 acb->active_dcb = NULL; 2967 dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->pid); 2968 2969 srb->scsi_phase = PH_BUS_FREE; /* initial phase */ 2970 clear_fifo(acb, "disconnect"); 2971 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); 2972 if (srb->state & SRB_UNEXPECT_RESEL) { 2973 dprintkl(KERN_ERR, 2974 "disconnect: Unexpected reselection <%02i-%i>\n", 2975 dcb->target_id, dcb->target_lun); 2976 srb->state = 0; 2977 waiting_process_next(acb); 2978 } else if (srb->state & SRB_ABORT_SENT) { 2979 dcb->flag &= ~ABORT_DEV_; 2980 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1; 2981 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n"); 2982 doing_srb_done(acb, DID_ABORT, srb->cmd, 1); 2983 waiting_process_next(acb); 2984 } else { 2985 if ((srb->state & (SRB_START_ + SRB_MSGOUT)) 2986 || !(srb-> 2987 state & (SRB_DISCONNECT + SRB_COMPLETED))) { 2988 /* 2989 * Selection time out 2990 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED) 2991 */ 2992 /* Unexp. Disc / Sel Timeout */ 2993 if (srb->state != SRB_START_ 2994 && srb->state != SRB_MSGOUT) { 2995 srb->state = SRB_READY; 2996 dprintkl(KERN_DEBUG, 2997 "disconnect: (pid#%li) Unexpected\n", 2998 srb->cmd->pid); 2999 srb->target_status = SCSI_STAT_SEL_TIMEOUT; 3000 goto disc1; 3001 } else { 3002 /* Normal selection timeout */ 3003 dprintkdbg(DBG_KG, "disconnect: (pid#%li) " 3004 "<%02i-%i> SelTO\n", srb->cmd->pid, 3005 dcb->target_id, dcb->target_lun); 3006 if (srb->retry_count++ > DC395x_MAX_RETRIES 3007 || acb->scan_devices) { 3008 srb->target_status = 3009 SCSI_STAT_SEL_TIMEOUT; 3010 goto disc1; 3011 } 3012 free_tag(dcb, srb); 3013 srb_going_to_waiting_move(dcb, srb); 3014 dprintkdbg(DBG_KG, 3015 "disconnect: (pid#%li) Retry\n", 3016 srb->cmd->pid); 3017 waiting_set_timer(acb, HZ / 20); 3018 } 3019 } else if (srb->state & SRB_DISCONNECT) { 3020 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL); 3021 /* 3022 * SRB_DISCONNECT (This is what we expect!) 3023 */ 3024 if (bval & 0x40) { 3025 dprintkdbg(DBG_0, "disconnect: SCSI bus stat " 3026 " 0x%02x: ACK set! Other controllers?\n", 3027 bval); 3028 /* It could come from another initiator, therefore don't do much ! */ 3029 } else 3030 waiting_process_next(acb); 3031 } else if (srb->state & SRB_COMPLETED) { 3032 disc1: 3033 /* 3034 ** SRB_COMPLETED 3035 */ 3036 free_tag(dcb, srb); 3037 dcb->active_srb = NULL; 3038 srb->state = SRB_FREE; 3039 srb_done(acb, dcb, srb); 3040 } 3041 } 3042} 3043 3044 3045static void reselect(struct AdapterCtlBlk *acb) 3046{ 3047 struct DeviceCtlBlk *dcb = acb->active_dcb; 3048 struct ScsiReqBlk *srb = NULL; 3049 u16 rsel_tar_lun_id; 3050 u8 id, lun; 3051 u8 arblostflag = 0; 3052 dprintkdbg(DBG_0, "reselect: acb=%p\n", acb); 3053 3054 clear_fifo(acb, "reselect"); 3055 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */ 3056 /* Read Reselected Target ID and LUN */ 3057 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID); 3058 if (dcb) { /* Arbitration lost but Reselection win */ 3059 srb = dcb->active_srb; 3060 if (!srb) { 3061 dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, " 3062 "but active_srb == NULL\n"); 3063 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 3064 return; 3065 } 3066 /* Why the if ? */ 3067 if (!acb->scan_devices) { 3068 dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> " 3069 "Arb lost but Resel win rsel=%i stat=0x%04x\n", 3070 srb->cmd->pid, dcb->target_id, 3071 dcb->target_lun, rsel_tar_lun_id, 3072 DC395x_read16(acb, TRM_S1040_SCSI_STATUS)); 3073 arblostflag = 1; 3074 /*srb->state |= SRB_DISCONNECT; */ 3075 3076 srb->state = SRB_READY; 3077 free_tag(dcb, srb); 3078 srb_going_to_waiting_move(dcb, srb); 3079 waiting_set_timer(acb, HZ / 20); 3080 3081 /* return; */ 3082 } 3083 } 3084 /* Read Reselected Target Id and LUN */ 3085 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8))) 3086 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. " 3087 "Got %i!\n", rsel_tar_lun_id); 3088 id = rsel_tar_lun_id & 0xff; 3089 lun = (rsel_tar_lun_id >> 8) & 7; 3090 dcb = find_dcb(acb, id, lun); 3091 if (!dcb) { 3092 dprintkl(KERN_ERR, "reselect: From non existent device " 3093 "<%02i-%i>\n", id, lun); 3094 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 3095 return; 3096 } 3097 acb->active_dcb = dcb; 3098 3099 if (!(dcb->dev_mode & NTC_DO_DISCONNECT)) 3100 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden " 3101 "disconnection? <%02i-%i>\n", 3102 dcb->target_id, dcb->target_lun); 3103 3104 if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) { 3105 srb = acb->tmp_srb; 3106 dcb->active_srb = srb; 3107 } else { 3108 /* There can be only one! */ 3109 srb = dcb->active_srb; 3110 if (!srb || !(srb->state & SRB_DISCONNECT)) { 3111 /* 3112 * abort command 3113 */ 3114 dprintkl(KERN_DEBUG, 3115 "reselect: w/o disconnected cmds <%02i-%i>\n", 3116 dcb->target_id, dcb->target_lun); 3117 srb = acb->tmp_srb; 3118 srb->state = SRB_UNEXPECT_RESEL; 3119 dcb->active_srb = srb; 3120 enable_msgout_abort(acb, srb); 3121 } else { 3122 if (dcb->flag & ABORT_DEV_) { 3123 /*srb->state = SRB_ABORT_SENT; */ 3124 enable_msgout_abort(acb, srb); 3125 } else 3126 srb->state = SRB_DATA_XFER; 3127 3128 } 3129 } 3130 srb->scsi_phase = PH_BUS_FREE; /* initial phase */ 3131 3132 /* Program HA ID, target ID, period and offset */ 3133 dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id); 3134 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */ 3135 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */ 3136 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */ 3137 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */ 3138 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */ 3139 /* SCSI command */ 3140 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT); 3141} 3142 3143 3144static inline u8 tagq_blacklist(char *name) 3145{ 3146#ifndef DC395x_NO_TAGQ 3147 return 0; 3148#else 3149 return 1; 3150#endif 3151} 3152 3153 3154static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr) 3155{ 3156 /* Check for SCSI format (ANSI and Response data format) */ 3157 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) { 3158 if ((ptr->Flags & SCSI_INQ_CMDQUEUE) 3159 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) && 3160 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */ 3161 /* ((dcb->dev_type == TYPE_DISK) 3162 || (dcb->dev_type == TYPE_MOD)) && */ 3163 !tagq_blacklist(((char *)ptr) + 8)) { 3164 if (dcb->max_command == 1) 3165 dcb->max_command = 3166 dcb->acb->tag_max_num; 3167 dcb->sync_mode |= EN_TAG_QUEUEING; 3168 /*dcb->tag_mask = 0; */ 3169 } else 3170 dcb->max_command = 1; 3171 } 3172} 3173 3174 3175static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 3176 struct ScsiInqData *ptr) 3177{ 3178 u8 bval1 = ptr->DevType & SCSI_DEVTYPE; 3179 dcb->dev_type = bval1; 3180 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */ 3181 disc_tagq_set(dcb, ptr); 3182} 3183 3184 3185/* unmap mapped pci regions from SRB */ 3186static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb) 3187{ 3188 struct scsi_cmnd *cmd = srb->cmd; 3189 enum dma_data_direction dir = cmd->sc_data_direction; 3190 if (cmd->use_sg && dir != PCI_DMA_NONE) { 3191 /* unmap DC395x SG list */ 3192 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n", 3193 srb->sg_bus_addr, SEGMENTX_LEN); 3194 pci_unmap_single(acb->dev, srb->sg_bus_addr, 3195 SEGMENTX_LEN, 3196 PCI_DMA_TODEVICE); 3197 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n", 3198 cmd->use_sg, cmd->request_buffer); 3199 /* unmap the sg segments */ 3200 pci_unmap_sg(acb->dev, 3201 (struct scatterlist *)cmd->request_buffer, 3202 cmd->use_sg, dir); 3203 } else if (cmd->request_buffer && dir != PCI_DMA_NONE) { 3204 dprintkdbg(DBG_SG, "pci_unmap_srb: buffer=%08x(%05x)\n", 3205 srb->segment_x[0].address, cmd->request_bufflen); 3206 pci_unmap_single(acb->dev, srb->segment_x[0].address, 3207 cmd->request_bufflen, dir); 3208 } 3209} 3210 3211 3212/* unmap mapped pci sense buffer from SRB */ 3213static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb, 3214 struct ScsiReqBlk *srb) 3215{ 3216 if (!(srb->flag & AUTO_REQSENSE)) 3217 return; 3218 /* Unmap sense buffer */ 3219 dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n", 3220 srb->segment_x[0].address); 3221 pci_unmap_single(acb->dev, srb->segment_x[0].address, 3222 srb->segment_x[0].length, PCI_DMA_FROMDEVICE); 3223 /* Restore SG stuff */ 3224 srb->total_xfer_length = srb->xferred; 3225 srb->segment_x[0].address = 3226 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address; 3227 srb->segment_x[0].length = 3228 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length; 3229} 3230 3231 3232/* 3233 * Complete execution of a SCSI command 3234 * Signal completion to the generic SCSI driver 3235 */ 3236static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 3237 struct ScsiReqBlk *srb) 3238{ 3239 u8 tempcnt, status; 3240 struct scsi_cmnd *cmd = srb->cmd; 3241 enum dma_data_direction dir = cmd->sc_data_direction; 3242 int ckc_only = 1; 3243 3244 dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->pid, 3245 srb->cmd->device->id, srb->cmd->device->lun); 3246 dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n", 3247 srb, cmd->use_sg, srb->sg_index, srb->sg_count, 3248 cmd->request_buffer); 3249 status = srb->target_status; 3250 if (srb->flag & AUTO_REQSENSE) { 3251 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n"); 3252 pci_unmap_srb_sense(acb, srb); 3253 /* 3254 ** target status.......................... 3255 */ 3256 srb->flag &= ~AUTO_REQSENSE; 3257 srb->adapter_status = 0; 3258 srb->target_status = CHECK_CONDITION << 1; 3259 if (debug_enabled(DBG_1)) { 3260 switch (cmd->sense_buffer[2] & 0x0f) { 3261 case NOT_READY: 3262 dprintkl(KERN_DEBUG, 3263 "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ", 3264 cmd->cmnd[0], dcb->target_id, 3265 dcb->target_lun, status, acb->scan_devices); 3266 break; 3267 case UNIT_ATTENTION: 3268 dprintkl(KERN_DEBUG, 3269 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ", 3270 cmd->cmnd[0], dcb->target_id, 3271 dcb->target_lun, status, acb->scan_devices); 3272 break; 3273 case ILLEGAL_REQUEST: 3274 dprintkl(KERN_DEBUG, 3275 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ", 3276 cmd->cmnd[0], dcb->target_id, 3277 dcb->target_lun, status, acb->scan_devices); 3278 break; 3279 case MEDIUM_ERROR: 3280 dprintkl(KERN_DEBUG, 3281 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ", 3282 cmd->cmnd[0], dcb->target_id, 3283 dcb->target_lun, status, acb->scan_devices); 3284 break; 3285 case HARDWARE_ERROR: 3286 dprintkl(KERN_DEBUG, 3287 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ", 3288 cmd->cmnd[0], dcb->target_id, 3289 dcb->target_lun, status, acb->scan_devices); 3290 break; 3291 } 3292 if (cmd->sense_buffer[7] >= 6) 3293 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x " 3294 "(0x%08x 0x%08x)\n", 3295 cmd->sense_buffer[2], cmd->sense_buffer[12], 3296 cmd->sense_buffer[13], 3297 *((unsigned int *)(cmd->sense_buffer + 3)), 3298 *((unsigned int *)(cmd->sense_buffer + 8))); 3299 else 3300 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n", 3301 cmd->sense_buffer[2], 3302 *((unsigned int *)(cmd->sense_buffer + 3))); 3303 } 3304 3305 if (status == (CHECK_CONDITION << 1)) { 3306 cmd->result = DID_BAD_TARGET << 16; 3307 goto ckc_e; 3308 } 3309 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n"); 3310 3311 if (srb->total_xfer_length 3312 && srb->total_xfer_length >= cmd->underflow) 3313 cmd->result = 3314 MK_RES_LNX(DRIVER_SENSE, DID_OK, 3315 srb->end_message, CHECK_CONDITION); 3316 /*SET_RES_DID(cmd->result,DID_OK) */ 3317 else 3318 cmd->result = 3319 MK_RES_LNX(DRIVER_SENSE, DID_OK, 3320 srb->end_message, CHECK_CONDITION); 3321 3322 goto ckc_e; 3323 } 3324 3325/*************************************************************/ 3326 if (status) { 3327 /* 3328 * target status.......................... 3329 */ 3330 if (status_byte(status) == CHECK_CONDITION) { 3331 request_sense(acb, dcb, srb); 3332 return; 3333 } else if (status_byte(status) == QUEUE_FULL) { 3334 tempcnt = (u8)list_size(&dcb->srb_going_list); 3335 dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n", 3336 dcb->target_id, dcb->target_lun, tempcnt); 3337 if (tempcnt > 1) 3338 tempcnt--; 3339 dcb->max_command = tempcnt; 3340 free_tag(dcb, srb); 3341 srb_going_to_waiting_move(dcb, srb); 3342 waiting_set_timer(acb, HZ / 20); 3343 srb->adapter_status = 0; 3344 srb->target_status = 0; 3345 return; 3346 } else if (status == SCSI_STAT_SEL_TIMEOUT) { 3347 srb->adapter_status = H_SEL_TIMEOUT; 3348 srb->target_status = 0; 3349 cmd->result = DID_NO_CONNECT << 16; 3350 } else { 3351 srb->adapter_status = 0; 3352 SET_RES_DID(cmd->result, DID_ERROR); 3353 SET_RES_MSG(cmd->result, srb->end_message); 3354 SET_RES_TARGET(cmd->result, status); 3355 3356 } 3357 } else { 3358 /* 3359 ** process initiator status.......................... 3360 */ 3361 status = srb->adapter_status; 3362 if (status & H_OVER_UNDER_RUN) { 3363 srb->target_status = 0; 3364 SET_RES_DID(cmd->result, DID_OK); 3365 SET_RES_MSG(cmd->result, srb->end_message); 3366 } else if (srb->status & PARITY_ERROR) { 3367 SET_RES_DID(cmd->result, DID_PARITY); 3368 SET_RES_MSG(cmd->result, srb->end_message); 3369 } else { /* No error */ 3370 3371 srb->adapter_status = 0; 3372 srb->target_status = 0; 3373 SET_RES_DID(cmd->result, DID_OK); 3374 } 3375 } 3376 3377 if (dir != PCI_DMA_NONE) { 3378 if (cmd->use_sg) 3379 pci_dma_sync_sg_for_cpu(acb->dev, 3380 (struct scatterlist *)cmd-> 3381 request_buffer, cmd->use_sg, dir); 3382 else if (cmd->request_buffer) 3383 pci_dma_sync_single_for_cpu(acb->dev, 3384 srb->segment_x[0].address, 3385 cmd->request_bufflen, dir); 3386 } 3387 ckc_only = 0; 3388/* Check Error Conditions */ 3389 ckc_e: 3390 3391 if (cmd->cmnd[0] == INQUIRY) { 3392 unsigned char *base = NULL; 3393 struct ScsiInqData *ptr; 3394 unsigned long flags = 0; 3395 3396 if (cmd->use_sg) { 3397 struct scatterlist* sg = (struct scatterlist *)cmd->request_buffer; 3398 size_t offset = 0, len = sizeof(struct ScsiInqData); 3399 3400 local_irq_save(flags); 3401 base = scsi_kmap_atomic_sg(sg, cmd->use_sg, &offset, &len); 3402 ptr = (struct ScsiInqData *)(base + offset); 3403 } else 3404 ptr = (struct ScsiInqData *)(cmd->request_buffer); 3405 3406 if (!ckc_only && (cmd->result & RES_DID) == 0 3407 && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8 3408 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2) 3409 dcb->inquiry7 = ptr->Flags; 3410 3411 /*if( srb->cmd->cmnd[0] == INQUIRY && */ 3412 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */ 3413 if ((cmd->result == (DID_OK << 16) 3414 || status_byte(cmd->result) & 3415 CHECK_CONDITION)) { 3416 if (!dcb->init_tcq_flag) { 3417 add_dev(acb, dcb, ptr); 3418 dcb->init_tcq_flag = 1; 3419 } 3420 } 3421 3422 if (cmd->use_sg) { 3423 scsi_kunmap_atomic_sg(base); 3424 local_irq_restore(flags); 3425 } 3426 } 3427 3428 /* Here is the info for Doug Gilbert's sg3 ... */ 3429 cmd->resid = srb->total_xfer_length; 3430 /* This may be interpreted by sb. or not ... */ 3431 cmd->SCp.this_residual = srb->total_xfer_length; 3432 cmd->SCp.buffers_residual = 0; 3433 if (debug_enabled(DBG_KG)) { 3434 if (srb->total_xfer_length) 3435 dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> " 3436 "cmnd=0x%02x Missed %i bytes\n", 3437 cmd->pid, cmd->device->id, cmd->device->lun, 3438 cmd->cmnd[0], srb->total_xfer_length); 3439 } 3440 3441 srb_going_remove(dcb, srb); 3442 /* Add to free list */ 3443 if (srb == acb->tmp_srb) 3444 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n"); 3445 else { 3446 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n", 3447 cmd->pid, cmd->result); 3448 srb_free_insert(acb, srb); 3449 } 3450 pci_unmap_srb(acb, srb); 3451 3452 cmd->scsi_done(cmd); 3453 waiting_process_next(acb); 3454} 3455 3456 3457/* abort all cmds in our queues */ 3458static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag, 3459 struct scsi_cmnd *cmd, u8 force) 3460{ 3461 struct DeviceCtlBlk *dcb; 3462 dprintkl(KERN_INFO, "doing_srb_done: pids "); 3463 3464 list_for_each_entry(dcb, &acb->dcb_list, list) { 3465 struct ScsiReqBlk *srb; 3466 struct ScsiReqBlk *tmp; 3467 struct scsi_cmnd *p; 3468 3469 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) { 3470 enum dma_data_direction dir; 3471 int result; 3472 3473 p = srb->cmd; 3474 dir = p->sc_data_direction; 3475 result = MK_RES(0, did_flag, 0, 0); 3476 printk("G:%li(%02i-%i) ", p->pid, 3477 p->device->id, p->device->lun); 3478 srb_going_remove(dcb, srb); 3479 free_tag(dcb, srb); 3480 srb_free_insert(acb, srb); 3481 p->result = result; 3482 pci_unmap_srb_sense(acb, srb); 3483 pci_unmap_srb(acb, srb); 3484 if (force) { 3485 /* For new EH, we normally don't need to give commands back, 3486 * as they all complete or all time out */ 3487 p->scsi_done(p); 3488 } 3489 } 3490 if (!list_empty(&dcb->srb_going_list)) 3491 dprintkl(KERN_DEBUG, 3492 "How could the ML send cmnds to the Going queue? <%02i-%i>\n", 3493 dcb->target_id, dcb->target_lun); 3494 if (dcb->tag_mask) 3495 dprintkl(KERN_DEBUG, 3496 "tag_mask for <%02i-%i> should be empty, is %08x!\n", 3497 dcb->target_id, dcb->target_lun, 3498 dcb->tag_mask); 3499 3500 /* Waiting queue */ 3501 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) { 3502 int result; 3503 p = srb->cmd; 3504 3505 result = MK_RES(0, did_flag, 0, 0); 3506 printk("W:%li<%02i-%i>", p->pid, p->device->id, 3507 p->device->lun); 3508 srb_waiting_remove(dcb, srb); 3509 srb_free_insert(acb, srb); 3510 p->result = result; 3511 pci_unmap_srb_sense(acb, srb); 3512 pci_unmap_srb(acb, srb); 3513 if (force) { 3514 /* For new EH, we normally don't need to give commands back, 3515 * as they all complete or all time out */ 3516 cmd->scsi_done(cmd); 3517 } 3518 } 3519 if (!list_empty(&dcb->srb_waiting_list)) 3520 dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n", 3521 list_size(&dcb->srb_waiting_list), dcb->target_id, 3522 dcb->target_lun); 3523 dcb->flag &= ~ABORT_DEV_; 3524 } 3525 printk("\n"); 3526} 3527 3528 3529static void reset_scsi_bus(struct AdapterCtlBlk *acb) 3530{ 3531 dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb); 3532 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */ 3533 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI); 3534 3535 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET)) 3536 /* nothing */; 3537} 3538 3539 3540static void set_basic_config(struct AdapterCtlBlk *acb) 3541{ 3542 u8 bval; 3543 u16 wval; 3544 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout); 3545 if (acb->config & HCC_PARITY) 3546 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK; 3547 else 3548 bval = PHASELATCH | INITIATOR | BLOCKRST; 3549 3550 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval); 3551 3552 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */ 3553 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */ 3554 /* program Host ID */ 3555 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); 3556 /* set ansynchronous transfer */ 3557 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00); 3558 /* Turn LED control off */ 3559 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F; 3560 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval); 3561 /* DMA config */ 3562 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL; 3563 wval |= 3564 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ; 3565 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval); 3566 /* Clear pending interrupt status */ 3567 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS); 3568 /* Enable SCSI interrupt */ 3569 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F); 3570 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR 3571 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */ 3572 ); 3573} 3574 3575 3576static void scsi_reset_detect(struct AdapterCtlBlk *acb) 3577{ 3578 dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb); 3579 /* delay half a second */ 3580 if (timer_pending(&acb->waiting_timer)) 3581 del_timer(&acb->waiting_timer); 3582 3583 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE); 3584 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE); 3585 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */ 3586 udelay(500); 3587 /* Maybe we locked up the bus? Then lets wait even longer ... */ 3588 acb->scsi_host->last_reset = 3589 jiffies + 5 * HZ / 2 + 3590 HZ * acb->eeprom.delay_time; 3591 3592 clear_fifo(acb, "scsi_reset_detect"); 3593 set_basic_config(acb); 3594 /*1.25 */ 3595 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */ 3596 3597 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */ 3598 acb->acb_flag |= RESET_DONE; 3599 } else { 3600 acb->acb_flag |= RESET_DETECT; 3601 reset_dev_param(acb); 3602 doing_srb_done(acb, DID_RESET, NULL, 1); 3603 /*DC395x_RecoverSRB( acb ); */ 3604 acb->active_dcb = NULL; 3605 acb->acb_flag = 0; 3606 waiting_process_next(acb); 3607 } 3608} 3609 3610 3611static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb, 3612 struct ScsiReqBlk *srb) 3613{ 3614 struct scsi_cmnd *cmd = srb->cmd; 3615 dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n", 3616 cmd->pid, cmd->device->id, cmd->device->lun); 3617 3618 srb->flag |= AUTO_REQSENSE; 3619 srb->adapter_status = 0; 3620 srb->target_status = 0; 3621 3622 /* KG: Can this prevent crap sense data ? */ 3623 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer)); 3624 3625 /* Save some data */ 3626 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address = 3627 srb->segment_x[0].address; 3628 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length = 3629 srb->segment_x[0].length; 3630 srb->xferred = srb->total_xfer_length; 3631 /* srb->segment_x : a one entry of S/G list table */ 3632 srb->total_xfer_length = sizeof(cmd->sense_buffer); 3633 srb->segment_x[0].length = sizeof(cmd->sense_buffer); 3634 /* Map sense buffer */ 3635 srb->segment_x[0].address = 3636 pci_map_single(acb->dev, cmd->sense_buffer, 3637 sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE); 3638 dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n", 3639 cmd->sense_buffer, srb->segment_x[0].address, 3640 sizeof(cmd->sense_buffer)); 3641 srb->sg_count = 1; 3642 srb->sg_index = 0; 3643 3644 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */ 3645 dprintkl(KERN_DEBUG, 3646 "request_sense: (pid#%li) failed <%02i-%i>\n", 3647 srb->cmd->pid, dcb->target_id, dcb->target_lun); 3648 srb_going_to_waiting_move(dcb, srb); 3649 waiting_set_timer(acb, HZ / 100); 3650 } 3651} 3652 3653 3654/** 3655 * device_alloc - Allocate a new device instance. This create the 3656 * devices instance and sets up all the data items. The adapter 3657 * instance is required to obtain confiuration information for this 3658 * device. This does *not* add this device to the adapters device 3659 * list. 3660 * 3661 * @acb: The adapter to obtain configuration information from. 3662 * @target: The target for the new device. 3663 * @lun: The lun for the new device. 3664 * 3665 * Return the new device if successful or NULL on failure. 3666 **/ 3667static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb, 3668 u8 target, u8 lun) 3669{ 3670 struct NvRamType *eeprom = &acb->eeprom; 3671 u8 period_index = eeprom->target[target].period & 0x07; 3672 struct DeviceCtlBlk *dcb; 3673 3674 dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC); 3675 dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun); 3676 if (!dcb) 3677 return NULL; 3678 dcb->acb = NULL; 3679 INIT_LIST_HEAD(&dcb->srb_going_list); 3680 INIT_LIST_HEAD(&dcb->srb_waiting_list); 3681 dcb->active_srb = NULL; 3682 dcb->tag_mask = 0; 3683 dcb->max_command = 1; 3684 dcb->target_id = target; 3685 dcb->target_lun = lun; 3686#ifndef DC395x_NO_DISCONNECT 3687 dcb->identify_msg = 3688 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun); 3689#else 3690 dcb->identify_msg = IDENTIFY(0, lun); 3691#endif 3692 dcb->dev_mode = eeprom->target[target].cfg0; 3693 dcb->inquiry7 = 0; 3694 dcb->sync_mode = 0; 3695 dcb->min_nego_period = clock_period[period_index]; 3696 dcb->sync_period = 0; 3697 dcb->sync_offset = 0; 3698 dcb->flag = 0; 3699 3700#ifndef DC395x_NO_WIDE 3701 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO) 3702 && (acb->config & HCC_WIDE_CARD)) 3703 dcb->sync_mode |= WIDE_NEGO_ENABLE; 3704#endif 3705#ifndef DC395x_NO_SYNC 3706 if (dcb->dev_mode & NTC_DO_SYNC_NEGO) 3707 if (!(lun) || current_sync_offset) 3708 dcb->sync_mode |= SYNC_NEGO_ENABLE; 3709#endif 3710 if (dcb->target_lun != 0) { 3711 /* Copy settings */ 3712 struct DeviceCtlBlk *p; 3713 list_for_each_entry(p, &acb->dcb_list, list) 3714 if (p->target_id == dcb->target_id) 3715 break; 3716 dprintkdbg(DBG_1, 3717 "device_alloc: <%02i-%i> copy from <%02i-%i>\n", 3718 dcb->target_id, dcb->target_lun, 3719 p->target_id, p->target_lun); 3720 dcb->sync_mode = p->sync_mode; 3721 dcb->sync_period = p->sync_period; 3722 dcb->min_nego_period = p->min_nego_period; 3723 dcb->sync_offset = p->sync_offset; 3724 dcb->inquiry7 = p->inquiry7; 3725 } 3726 return dcb; 3727} 3728 3729 3730/** 3731 * adapter_add_device - Adds the device instance to the adaptor instance. 3732 * 3733 * @acb: The adapter device to be updated 3734 * @dcb: A newly created and intialised device instance to add. 3735 **/ 3736static void adapter_add_device(struct AdapterCtlBlk *acb, 3737 struct DeviceCtlBlk *dcb) 3738{ 3739 /* backpointer to adapter */ 3740 dcb->acb = acb; 3741 3742 /* set run_robin to this device if it is currently empty */ 3743 if (list_empty(&acb->dcb_list)) 3744 acb->dcb_run_robin = dcb; 3745 3746 /* add device to list */ 3747 list_add_tail(&dcb->list, &acb->dcb_list); 3748 3749 /* update device maps */ 3750 acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun); 3751 acb->children[dcb->target_id][dcb->target_lun] = dcb; 3752} 3753 3754 3755/** 3756 * adapter_remove_device - Removes the device instance from the adaptor 3757 * instance. The device instance is not check in any way or freed by this. 3758 * The caller is expected to take care of that. This will simply remove the 3759 * device from the adapters data strcutures. 3760 * 3761 * @acb: The adapter device to be updated 3762 * @dcb: A device that has previously been added to the adapter. 3763 **/ 3764static void adapter_remove_device(struct AdapterCtlBlk *acb, 3765 struct DeviceCtlBlk *dcb) 3766{ 3767 struct DeviceCtlBlk *i; 3768 struct DeviceCtlBlk *tmp; 3769 dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n", 3770 dcb->target_id, dcb->target_lun); 3771 3772 /* fix up any pointers to this device that we have in the adapter */ 3773 if (acb->active_dcb == dcb) 3774 acb->active_dcb = NULL; 3775 if (acb->dcb_run_robin == dcb) 3776 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb); 3777 3778 /* unlink from list */ 3779 list_for_each_entry_safe(i, tmp, &acb->dcb_list, list) 3780 if (dcb == i) { 3781 list_del(&i->list); 3782 break; 3783 } 3784 3785 /* clear map and children */ 3786 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun); 3787 acb->children[dcb->target_id][dcb->target_lun] = NULL; 3788 dcb->acb = NULL; 3789} 3790 3791 3792/** 3793 * adapter_remove_and_free_device - Removes a single device from the adapter 3794 * and then frees the device information. 3795 * 3796 * @acb: The adapter device to be updated 3797 * @dcb: A device that has previously been added to the adapter. 3798 */ 3799static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb, 3800 struct DeviceCtlBlk *dcb) 3801{ 3802 if (list_size(&dcb->srb_going_list) > 1) { 3803 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> " 3804 "Won't remove because of %i active requests.\n", 3805 dcb->target_id, dcb->target_lun, 3806 list_size(&dcb->srb_going_list)); 3807 return; 3808 } 3809 adapter_remove_device(acb, dcb); 3810 kfree(dcb); 3811} 3812 3813 3814/** 3815 * adapter_remove_and_free_all_devices - Removes and frees all of the 3816 * devices associated with the specified adapter. 3817 * 3818 * @acb: The adapter from which all devices should be removed. 3819 **/ 3820static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb) 3821{ 3822 struct DeviceCtlBlk *dcb; 3823 struct DeviceCtlBlk *tmp; 3824 dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n", 3825 list_size(&acb->dcb_list)); 3826 3827 list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list) 3828 adapter_remove_and_free_device(acb, dcb); 3829} 3830 3831 3832/** 3833 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new 3834 * scsi device that we need to deal with. We allocate a new device and then 3835 * insert that device into the adapters device list. 3836 * 3837 * @scsi_device: The new scsi device that we need to handle. 3838 **/ 3839static int dc395x_slave_alloc(struct scsi_device *scsi_device) 3840{ 3841 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata; 3842 struct DeviceCtlBlk *dcb; 3843 3844 dcb = device_alloc(acb, scsi_device->id, scsi_device->lun); 3845 if (!dcb) 3846 return -ENOMEM; 3847 adapter_add_device(acb, dcb); 3848 3849 return 0; 3850} 3851 3852 3853/** 3854 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a 3855 * device that is going away. 3856 * 3857 * @scsi_device: The new scsi device that we need to handle. 3858 **/ 3859static void dc395x_slave_destroy(struct scsi_device *scsi_device) 3860{ 3861 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata; 3862 struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun); 3863 if (dcb) 3864 adapter_remove_and_free_device(acb, dcb); 3865} 3866 3867 3868 3869 3870/** 3871 * trms1040_wait_30us: wait for 30 us 3872 * 3873 * Waits for 30us (using the chip by the looks of it..) 3874 * 3875 * @io_port: base I/O address 3876 **/ 3877static void __devinit trms1040_wait_30us(unsigned long io_port) 3878{ 3879 /* ScsiPortStallExecution(30); wait 30 us */ 3880 outb(5, io_port + TRM_S1040_GEN_TIMER); 3881 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT)) 3882 /* nothing */ ; 3883} 3884 3885 3886/** 3887 * trms1040_write_cmd - write the secified command and address to 3888 * chip 3889 * 3890 * @io_port: base I/O address 3891 * @cmd: SB + op code (command) to send 3892 * @addr: address to send 3893 **/ 3894static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr) 3895{ 3896 int i; 3897 u8 send_data; 3898 3899 /* program SB + OP code */ 3900 for (i = 0; i < 3; i++, cmd <<= 1) { 3901 send_data = NVR_SELECT; 3902 if (cmd & 0x04) /* Start from bit 2 */ 3903 send_data |= NVR_BITOUT; 3904 3905 outb(send_data, io_port + TRM_S1040_GEN_NVRAM); 3906 trms1040_wait_30us(io_port); 3907 outb((send_data | NVR_CLOCK), 3908 io_port + TRM_S1040_GEN_NVRAM); 3909 trms1040_wait_30us(io_port); 3910 } 3911 3912 /* send address */ 3913 for (i = 0; i < 7; i++, addr <<= 1) { 3914 send_data = NVR_SELECT; 3915 if (addr & 0x40) /* Start from bit 6 */ 3916 send_data |= NVR_BITOUT; 3917 3918 outb(send_data, io_port + TRM_S1040_GEN_NVRAM); 3919 trms1040_wait_30us(io_port); 3920 outb((send_data | NVR_CLOCK), 3921 io_port + TRM_S1040_GEN_NVRAM); 3922 trms1040_wait_30us(io_port); 3923 } 3924 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM); 3925 trms1040_wait_30us(io_port); 3926} 3927 3928 3929/** 3930 * trms1040_set_data - store a single byte in the eeprom 3931 * 3932 * Called from write all to write a single byte into the SSEEPROM 3933 * Which is done one bit at a time. 3934 * 3935 * @io_port: base I/O address 3936 * @addr: offset into EEPROM 3937 * @byte: bytes to write 3938 **/ 3939static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte) 3940{ 3941 int i; 3942 u8 send_data; 3943 3944 /* Send write command & address */ 3945 trms1040_write_cmd(io_port, 0x05, addr); 3946 3947 /* Write data */ 3948 for (i = 0; i < 8; i++, byte <<= 1) { 3949 send_data = NVR_SELECT; 3950 if (byte & 0x80) /* Start from bit 7 */ 3951 send_data |= NVR_BITOUT; 3952 3953 outb(send_data, io_port + TRM_S1040_GEN_NVRAM); 3954 trms1040_wait_30us(io_port); 3955 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM); 3956 trms1040_wait_30us(io_port); 3957 } 3958 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM); 3959 trms1040_wait_30us(io_port); 3960 3961 /* Disable chip select */ 3962 outb(0, io_port + TRM_S1040_GEN_NVRAM); 3963 trms1040_wait_30us(io_port); 3964 3965 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM); 3966 trms1040_wait_30us(io_port); 3967 3968 /* Wait for write ready */ 3969 while (1) { 3970 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM); 3971 trms1040_wait_30us(io_port); 3972 3973 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM); 3974 trms1040_wait_30us(io_port); 3975 3976 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN) 3977 break; 3978 } 3979 3980 /* Disable chip select */ 3981 outb(0, io_port + TRM_S1040_GEN_NVRAM); 3982} 3983 3984 3985/** 3986 * trms1040_write_all - write 128 bytes to the eeprom 3987 * 3988 * Write the supplied 128 bytes to the chips SEEPROM 3989 * 3990 * @eeprom: the data to write 3991 * @io_port: the base io port 3992 **/ 3993static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port) 3994{ 3995 u8 *b_eeprom = (u8 *)eeprom; 3996 u8 addr; 3997 3998 /* Enable SEEPROM */ 3999 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM), 4000 io_port + TRM_S1040_GEN_CONTROL); 4001 4002 /* write enable */ 4003 trms1040_write_cmd(io_port, 0x04, 0xFF); 4004 outb(0, io_port + TRM_S1040_GEN_NVRAM); 4005 trms1040_wait_30us(io_port); 4006 4007 /* write */ 4008 for (addr = 0; addr < 128; addr++, b_eeprom++) 4009 trms1040_set_data(io_port, addr, *b_eeprom); 4010 4011 /* write disable */ 4012 trms1040_write_cmd(io_port, 0x04, 0x00); 4013 outb(0, io_port + TRM_S1040_GEN_NVRAM); 4014 trms1040_wait_30us(io_port); 4015 4016 /* Disable SEEPROM */ 4017 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM), 4018 io_port + TRM_S1040_GEN_CONTROL); 4019} 4020 4021 4022/** 4023 * trms1040_get_data - get a single byte from the eeprom 4024 * 4025 * Called from read all to read a single byte into the SSEEPROM 4026 * Which is done one bit at a time. 4027 * 4028 * @io_port: base I/O address 4029 * @addr: offset into SEEPROM 4030 * 4031 * Returns the byte read. 4032 **/ 4033static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr) 4034{ 4035 int i; 4036 u8 read_byte; 4037 u8 result = 0; 4038 4039 /* Send read command & address */ 4040 trms1040_write_cmd(io_port, 0x06, addr); 4041 4042 /* read data */ 4043 for (i = 0; i < 8; i++) { 4044 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM); 4045 trms1040_wait_30us(io_port); 4046 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM); 4047 4048 /* Get data bit while falling edge */ 4049 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM); 4050 result <<= 1; 4051 if (read_byte & NVR_BITIN) 4052 result |= 1; 4053 4054 trms1040_wait_30us(io_port); 4055 } 4056 4057 /* Disable chip select */ 4058 outb(0, io_port + TRM_S1040_GEN_NVRAM); 4059 return result; 4060} 4061 4062 4063/** 4064 * trms1040_read_all - read all bytes from the eeprom 4065 * 4066 * Read the 128 bytes from the SEEPROM. 4067 * 4068 * @eeprom: where to store the data 4069 * @io_port: the base io port 4070 **/ 4071static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port) 4072{ 4073 u8 *b_eeprom = (u8 *)eeprom; 4074 u8 addr; 4075 4076 /* Enable SEEPROM */ 4077 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM), 4078 io_port + TRM_S1040_GEN_CONTROL); 4079 4080 /* read details */ 4081 for (addr = 0; addr < 128; addr++, b_eeprom++) 4082 *b_eeprom = trms1040_get_data(io_port, addr); 4083 4084 /* Disable SEEPROM */ 4085 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM), 4086 io_port + TRM_S1040_GEN_CONTROL); 4087} 4088 4089 4090 4091/** 4092 * check_eeprom - get and check contents of the eeprom 4093 * 4094 * Read seeprom 128 bytes into the memory provider in eeprom. 4095 * Checks the checksum and if it's not correct it uses a set of default 4096 * values. 4097 * 4098 * @eeprom: caller allocated strcuture to read the eeprom data into 4099 * @io_port: io port to read from 4100 **/ 4101static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port) 4102{ 4103 u16 *w_eeprom = (u16 *)eeprom; 4104 u16 w_addr; 4105 u16 cksum; 4106 u32 d_addr; 4107 u32 *d_eeprom; 4108 4109 trms1040_read_all(eeprom, io_port); /* read eeprom */ 4110 4111 cksum = 0; 4112 for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64; 4113 w_addr++, w_eeprom++) 4114 cksum += *w_eeprom; 4115 if (cksum != 0x1234) { 4116 /* 4117 * Checksum is wrong. 4118 * Load a set of defaults into the eeprom buffer 4119 */ 4120 dprintkl(KERN_WARNING, 4121 "EEProm checksum error: using default values and options.\n"); 4122 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM; 4123 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8); 4124 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040; 4125 eeprom->sub_sys_id[1] = 4126 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8); 4127 eeprom->sub_class = 0x00; 4128 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM; 4129 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8); 4130 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040; 4131 eeprom->device_id[1] = 4132 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8); 4133 eeprom->reserved = 0x00; 4134 4135 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target; 4136 d_addr < 16; d_addr++, d_eeprom++) 4137 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */ 4138 4139 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */ 4140 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */ 4141 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++) 4142 *d_eeprom = 0x00; 4143 4144 /* Now load defaults (maybe set by boot/module params) */ 4145 set_safe_settings(); 4146 fix_settings(); 4147 eeprom_override(eeprom); 4148 4149 eeprom->cksum = 0x00; 4150 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom; 4151 w_addr < 63; w_addr++, w_eeprom++) 4152 cksum += *w_eeprom; 4153 4154 *w_eeprom = 0x1234 - cksum; 4155 trms1040_write_all(eeprom, io_port); 4156 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value; 4157 } else { 4158 set_safe_settings(); 4159 eeprom_index_to_delay(eeprom); 4160 eeprom_override(eeprom); 4161 } 4162} 4163 4164 4165/** 4166 * print_eeprom_settings - output the eeprom settings 4167 * to the kernel log so people can see what they were. 4168 * 4169 * @eeprom: The eeprom data strucutre to show details for. 4170 **/ 4171static void __devinit print_eeprom_settings(struct NvRamType *eeprom) 4172{ 4173 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n", 4174 eeprom->scsi_id, 4175 eeprom->target[0].period, 4176 clock_speed[eeprom->target[0].period] / 10, 4177 clock_speed[eeprom->target[0].period] % 10, 4178 eeprom->target[0].cfg0); 4179 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n", 4180 eeprom->channel_cfg, eeprom->max_tag, 4181 1 << eeprom->max_tag, eeprom->delay_time); 4182} 4183 4184 4185/* Free SG tables */ 4186static void adapter_sg_tables_free(struct AdapterCtlBlk *acb) 4187{ 4188 int i; 4189 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN; 4190 4191 for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page) 4192 kfree(acb->srb_array[i].segment_x); 4193} 4194 4195 4196/* 4197 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*) 4198 * should never cross a page boundary */ 4199static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb) 4200{ 4201 const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1) 4202 *SEGMENTX_LEN; 4203 int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE; 4204 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN; 4205 int srb_idx = 0; 4206 unsigned i = 0; 4207 struct SGentry *ptr; 4208 4209 for (i = 0; i < DC395x_MAX_SRB_CNT; i++) 4210 acb->srb_array[i].segment_x = NULL; 4211 4212 dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages); 4213 while (pages--) { 4214 ptr = kmalloc(PAGE_SIZE, GFP_KERNEL); 4215 if (!ptr) { 4216 adapter_sg_tables_free(acb); 4217 return 1; 4218 } 4219 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n", 4220 PAGE_SIZE, ptr, srb_idx); 4221 i = 0; 4222 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT) 4223 acb->srb_array[srb_idx++].segment_x = 4224 ptr + (i++ * DC395x_MAX_SG_LISTENTRY); 4225 } 4226 if (i < srbs_per_page) 4227 acb->srb.segment_x = 4228 ptr + (i * DC395x_MAX_SG_LISTENTRY); 4229 else 4230 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n"); 4231 return 0; 4232} 4233 4234 4235 4236/** 4237 * adapter_print_config - print adapter connection and termination 4238 * config 4239 * 4240 * The io port in the adapter needs to have been set before calling 4241 * this function. 4242 * 4243 * @acb: The adapter to print the information for. 4244 **/ 4245static void __devinit adapter_print_config(struct AdapterCtlBlk *acb) 4246{ 4247 u8 bval; 4248 4249 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS); 4250 dprintkl(KERN_INFO, "%sConnectors: ", 4251 ((bval & WIDESCSI) ? "(Wide) " : "")); 4252 if (!(bval & CON5068)) 4253 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50"); 4254 if (!(bval & CON68)) 4255 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)"); 4256 if (!(bval & CON50)) 4257 printk("int50 "); 4258 if ((bval & (CON5068 | CON50 | CON68)) == 4259 0 /*(CON5068 | CON50 | CON68) */ ) 4260 printk(" Oops! (All 3?) "); 4261 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL); 4262 printk(" Termination: "); 4263 if (bval & DIS_TERM) 4264 printk("Disabled\n"); 4265 else { 4266 if (bval & AUTOTERM) 4267 printk("Auto "); 4268 if (bval & LOW8TERM) 4269 printk("Low "); 4270 if (bval & UP8TERM) 4271 printk("High "); 4272 printk("\n"); 4273 } 4274} 4275 4276 4277/** 4278 * adapter_init_params - Initialize the various parameters in the 4279 * adapter structure. Note that the pointer to the scsi_host is set 4280 * early (when this instance is created) and the io_port and irq 4281 * values are set later after they have been reserved. This just gets 4282 * everything set to a good starting position. 4283 * 4284 * The eeprom structure in the adapter needs to have been set before 4285 * calling this function. 4286 * 4287 * @acb: The adapter to initialize. 4288 **/ 4289static void __devinit adapter_init_params(struct AdapterCtlBlk *acb) 4290{ 4291 struct NvRamType *eeprom = &acb->eeprom; 4292 int i; 4293 4294 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */ 4295 /* NOTE: acb->io_port_base is set at port registration time */ 4296 /* NOTE: acb->io_port_len is set at port registration time */ 4297 4298 INIT_LIST_HEAD(&acb->dcb_list); 4299 acb->dcb_run_robin = NULL; 4300 acb->active_dcb = NULL; 4301 4302 INIT_LIST_HEAD(&acb->srb_free_list); 4303 /* temp SRB for Q tag used or abort command used */ 4304 acb->tmp_srb = &acb->srb; 4305 init_timer(&acb->waiting_timer); 4306 init_timer(&acb->selto_timer); 4307 4308 acb->srb_count = DC395x_MAX_SRB_CNT; 4309 4310 acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */ 4311 /* NOTE: acb->irq_level is set at IRQ registration time */ 4312 4313 acb->tag_max_num = 1 << eeprom->max_tag; 4314 if (acb->tag_max_num > 30) 4315 acb->tag_max_num = 30; 4316 4317 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */ 4318 acb->gmode2 = eeprom->channel_cfg; 4319 acb->config = 0; /* NOTE: actually set in adapter_init_chip */ 4320 4321 if (eeprom->channel_cfg & NAC_SCANLUN) 4322 acb->lun_chk = 1; 4323 acb->scan_devices = 1; 4324 4325 acb->scsi_host->this_id = eeprom->scsi_id; 4326 acb->hostid_bit = (1 << acb->scsi_host->this_id); 4327 4328 for (i = 0; i < DC395x_MAX_SCSI_ID; i++) 4329 acb->dcb_map[i] = 0; 4330 4331 acb->msg_len = 0; 4332 4333 /* link static array of srbs into the srb free list */ 4334 for (i = 0; i < acb->srb_count - 1; i++) 4335 srb_free_insert(acb, &acb->srb_array[i]); 4336} 4337 4338 4339/** 4340 * adapter_init_host - Initialize the scsi host instance based on 4341 * values that we have already stored in the adapter instance. There's 4342 * some mention that a lot of these are deprecated, so we won't use 4343 * them (we'll use the ones in the adapter instance) but we'll fill 4344 * them in in case something else needs them. 4345 * 4346 * The eeprom structure, irq and io ports in the adapter need to have 4347 * been set before calling this function. 4348 * 4349 * @host: The scsi host instance to fill in the values for. 4350 **/ 4351static void __devinit adapter_init_scsi_host(struct Scsi_Host *host) 4352{ 4353 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata; 4354 struct NvRamType *eeprom = &acb->eeprom; 4355 4356 host->max_cmd_len = 24; 4357 host->can_queue = DC395x_MAX_CMD_QUEUE; 4358 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN; 4359 host->this_id = (int)eeprom->scsi_id; 4360 host->io_port = acb->io_port_base; 4361 host->n_io_port = acb->io_port_len; 4362 host->dma_channel = -1; 4363 host->unique_id = acb->io_port_base; 4364 host->irq = acb->irq_level; 4365 host->last_reset = jiffies; 4366 4367 host->max_id = 16; 4368 if (host->max_id - 1 == eeprom->scsi_id) 4369 host->max_id--; 4370 4371#ifdef CONFIG_SCSI_MULTI_LUN 4372 if (eeprom->channel_cfg & NAC_SCANLUN) 4373 host->max_lun = 8; 4374 else 4375 host->max_lun = 1; 4376#else 4377 host->max_lun = 1; 4378#endif 4379 4380} 4381 4382 4383/** 4384 * adapter_init_chip - Get the chip into a know state and figure out 4385 * some of the settings that apply to this adapter. 4386 * 4387 * The io port in the adapter needs to have been set before calling 4388 * this function. The config will be configured correctly on return. 4389 * 4390 * @acb: The adapter which we are to init. 4391 **/ 4392static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb) 4393{ 4394 struct NvRamType *eeprom = &acb->eeprom; 4395 4396 /* Mask all the interrupt */ 4397 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00); 4398 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00); 4399 4400 /* Reset SCSI module */ 4401 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE); 4402 4403 /* Reset PCI/DMA module */ 4404 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE); 4405 udelay(20); 4406 4407 /* program configuration 0 */ 4408 acb->config = HCC_AUTOTERM | HCC_PARITY; 4409 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI) 4410 acb->config |= HCC_WIDE_CARD; 4411 4412 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET) 4413 acb->config |= HCC_SCSI_RESET; 4414 4415 if (acb->config & HCC_SCSI_RESET) { 4416 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n"); 4417 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI); 4418 4419 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */ 4420 /*spin_unlock_irq (&io_request_lock); */ 4421 udelay(500); 4422 4423 acb->scsi_host->last_reset = 4424 jiffies + HZ / 2 + 4425 HZ * acb->eeprom.delay_time; 4426 4427 /*spin_lock_irq (&io_request_lock); */ 4428 } 4429} 4430 4431 4432/** 4433 * init_adapter - Grab the resource for the card, setup the adapter 4434 * information, set the card into a known state, create the various 4435 * tables etc etc. This basically gets all adapter information all up 4436 * to date, intialised and gets the chip in sync with it. 4437 * 4438 * @host: This hosts adapter structure 4439 * @io_port: The base I/O port 4440 * @irq: IRQ 4441 * 4442 * Returns 0 if the initialization succeeds, any other value on 4443 * failure. 4444 **/ 4445static int __devinit adapter_init(struct AdapterCtlBlk *acb, 4446 unsigned long io_port, u32 io_port_len, unsigned int irq) 4447{ 4448 if (!request_region(io_port, io_port_len, DC395X_NAME)) { 4449 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port); 4450 goto failed; 4451 } 4452 /* store port base to indicate we have registered it */ 4453 acb->io_port_base = io_port; 4454 acb->io_port_len = io_port_len; 4455 4456 if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) { 4457 /* release the region we just claimed */ 4458 dprintkl(KERN_INFO, "Failed to register IRQ\n"); 4459 goto failed; 4460 } 4461 /* store irq to indicate we have registered it */ 4462 acb->irq_level = irq; 4463 4464 /* get eeprom configuration information and command line settings etc */ 4465 check_eeprom(&acb->eeprom, io_port); 4466 print_eeprom_settings(&acb->eeprom); 4467 4468 /* setup adapter control block */ 4469 adapter_init_params(acb); 4470 4471 /* display card connectors/termination settings */ 4472 adapter_print_config(acb); 4473 4474 if (adapter_sg_tables_alloc(acb)) { 4475 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n"); 4476 goto failed; 4477 } 4478 adapter_init_scsi_host(acb->scsi_host); 4479 adapter_init_chip(acb); 4480 set_basic_config(acb); 4481 4482 dprintkdbg(DBG_0, 4483 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p " 4484 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n", 4485 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk), 4486 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk)); 4487 return 0; 4488 4489failed: 4490 if (acb->irq_level) 4491 free_irq(acb->irq_level, acb); 4492 if (acb->io_port_base) 4493 release_region(acb->io_port_base, acb->io_port_len); 4494 adapter_sg_tables_free(acb); 4495 4496 return 1; 4497} 4498 4499 4500/** 4501 * adapter_uninit_chip - cleanly shut down the scsi controller chip, 4502 * stopping all operations and disabling interrupt generation on the 4503 * card. 4504 * 4505 * @acb: The adapter which we are to shutdown. 4506 **/ 4507static void adapter_uninit_chip(struct AdapterCtlBlk *acb) 4508{ 4509 /* disable interrupts */ 4510 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0); 4511 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0); 4512 4513 /* reset the scsi bus */ 4514 if (acb->config & HCC_SCSI_RESET) 4515 reset_scsi_bus(acb); 4516 4517 /* clear any pending interupt state */ 4518 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS); 4519} 4520 4521 4522 4523/** 4524 * adapter_uninit - Shut down the chip and release any resources that 4525 * we had allocated. Once this returns the adapter should not be used 4526 * anymore. 4527 * 4528 * @acb: The adapter which we are to un-initialize. 4529 **/ 4530static void adapter_uninit(struct AdapterCtlBlk *acb) 4531{ 4532 unsigned long flags; 4533 DC395x_LOCK_IO(acb->scsi_host, flags); 4534 4535 /* remove timers */ 4536 if (timer_pending(&acb->waiting_timer)) 4537 del_timer(&acb->waiting_timer); 4538 if (timer_pending(&acb->selto_timer)) 4539 del_timer(&acb->selto_timer); 4540 4541 adapter_uninit_chip(acb); 4542 adapter_remove_and_free_all_devices(acb); 4543 DC395x_UNLOCK_IO(acb->scsi_host, flags); 4544 4545 if (acb->irq_level) 4546 free_irq(acb->irq_level, acb); 4547 if (acb->io_port_base) 4548 release_region(acb->io_port_base, acb->io_port_len); 4549 4550 adapter_sg_tables_free(acb); 4551} 4552 4553 4554#undef SPRINTF 4555#define SPRINTF(args...) pos += sprintf(pos, args) 4556 4557#undef YESNO 4558#define YESNO(YN) \ 4559 if (YN) SPRINTF(" Yes ");\ 4560 else SPRINTF(" No ") 4561 4562static int dc395x_proc_info(struct Scsi_Host *host, char *buffer, 4563 char **start, off_t offset, int length, int inout) 4564{ 4565 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata; 4566 int spd, spd1; 4567 char *pos = buffer; 4568 struct DeviceCtlBlk *dcb; 4569 unsigned long flags; 4570 int dev; 4571 4572 if (inout) /* Has data been written to the file ? */ 4573 return -EPERM; 4574 4575 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n"); 4576 SPRINTF(" Driver Version " DC395X_VERSION "\n"); 4577 4578 DC395x_LOCK_IO(acb->scsi_host, flags); 4579 4580 SPRINTF("SCSI Host Nr %i, ", host->host_no); 4581 SPRINTF("DC395U/UW/F DC315/U %s\n", 4582 (acb->config & HCC_WIDE_CARD) ? "Wide" : ""); 4583 SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base); 4584 SPRINTF("irq_level 0x%04x, ", acb->irq_level); 4585 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000); 4586 4587 SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun); 4588 SPRINTF("AdapterID %i\n", host->this_id); 4589 4590 SPRINTF("tag_max_num %i", acb->tag_max_num); 4591 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */ 4592 SPRINTF(", FilterCfg 0x%02x", 4593 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1)); 4594 SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time); 4595 /*SPRINTF("\n"); */ 4596 4597 SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list)); 4598 SPRINTF 4599 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n", 4600 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2], 4601 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5], 4602 acb->dcb_map[6], acb->dcb_map[7]); 4603 SPRINTF 4604 (" %02x %02x %02x %02x %02x %02x %02x %02x\n", 4605 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10], 4606 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13], 4607 acb->dcb_map[14], acb->dcb_map[15]); 4608 4609 SPRINTF 4610 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n"); 4611 4612 dev = 0; 4613 list_for_each_entry(dcb, &acb->dcb_list, list) { 4614 int nego_period; 4615 SPRINTF("%02i %02i %02i ", dev, dcb->target_id, 4616 dcb->target_lun); 4617 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK); 4618 YESNO(dcb->sync_offset); 4619 YESNO(dcb->sync_period & WIDE_SYNC); 4620 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT); 4621 YESNO(dcb->dev_mode & NTC_DO_SEND_START); 4622 YESNO(dcb->sync_mode & EN_TAG_QUEUEING); 4623 nego_period = clock_period[dcb->sync_period & 0x07] << 2; 4624 if (dcb->sync_offset) 4625 SPRINTF(" %03i ns ", nego_period); 4626 else 4627 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2)); 4628 4629 if (dcb->sync_offset & 0x0f) { 4630 spd = 1000 / (nego_period); 4631 spd1 = 1000 % (nego_period); 4632 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period); 4633 SPRINTF(" %2i.%1i M %02i ", spd, spd1, 4634 (dcb->sync_offset & 0x0f)); 4635 } else 4636 SPRINTF(" "); 4637 4638 /* Add more info ... */ 4639 SPRINTF(" %02i\n", dcb->max_command); 4640 dev++; 4641 } 4642 4643 if (timer_pending(&acb->waiting_timer)) 4644 SPRINTF("Waiting queue timer running\n"); 4645 else 4646 SPRINTF("\n"); 4647 4648 list_for_each_entry(dcb, &acb->dcb_list, list) { 4649 struct ScsiReqBlk *srb; 4650 if (!list_empty(&dcb->srb_waiting_list)) 4651 SPRINTF("DCB (%02i-%i): Waiting: %i:", 4652 dcb->target_id, dcb->target_lun, 4653 list_size(&dcb->srb_waiting_list)); 4654 list_for_each_entry(srb, &dcb->srb_waiting_list, list) 4655 SPRINTF(" %li", srb->cmd->pid); 4656 if (!list_empty(&dcb->srb_going_list)) 4657 SPRINTF("\nDCB (%02i-%i): Going : %i:", 4658 dcb->target_id, dcb->target_lun, 4659 list_size(&dcb->srb_going_list)); 4660 list_for_each_entry(srb, &dcb->srb_going_list, list) 4661 SPRINTF(" %li", srb->cmd->pid); 4662 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list)) 4663 SPRINTF("\n"); 4664 } 4665 4666 if (debug_enabled(DBG_1)) { 4667 SPRINTF("DCB list for ACB %p:\n", acb); 4668 list_for_each_entry(dcb, &acb->dcb_list, list) { 4669 SPRINTF("%p -> ", dcb); 4670 } 4671 SPRINTF("END\n"); 4672 } 4673 4674 *start = buffer + offset; 4675 DC395x_UNLOCK_IO(acb->scsi_host, flags); 4676 4677 if (pos - buffer < offset) 4678 return 0; 4679 else if (pos - buffer - offset < length) 4680 return pos - buffer - offset; 4681 else 4682 return length; 4683} 4684 4685 4686static struct scsi_host_template dc395x_driver_template = { 4687 .module = THIS_MODULE, 4688 .proc_name = DC395X_NAME, 4689 .proc_info = dc395x_proc_info, 4690 .name = DC395X_BANNER " " DC395X_VERSION, 4691 .queuecommand = dc395x_queue_command, 4692 .bios_param = dc395x_bios_param, 4693 .slave_alloc = dc395x_slave_alloc, 4694 .slave_destroy = dc395x_slave_destroy, 4695 .can_queue = DC395x_MAX_CAN_QUEUE, 4696 .this_id = 7, 4697 .sg_tablesize = DC395x_MAX_SG_TABLESIZE, 4698 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN, 4699 .eh_abort_handler = dc395x_eh_abort, 4700 .eh_bus_reset_handler = dc395x_eh_bus_reset, 4701 .unchecked_isa_dma = 0, 4702 .use_clustering = DISABLE_CLUSTERING, 4703}; 4704 4705 4706/** 4707 * banner_display - Display banner on first instance of driver 4708 * initialized. 4709 **/ 4710static void banner_display(void) 4711{ 4712 static int banner_done = 0; 4713 if (!banner_done) 4714 { 4715 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION); 4716 banner_done = 1; 4717 } 4718} 4719 4720 4721/** 4722 * dc395x_init_one - Initialise a single instance of the adapter. 4723 * 4724 * The PCI layer will call this once for each instance of the adapter 4725 * that it finds in the system. The pci_dev strcuture indicates which 4726 * instance we are being called from. 4727 * 4728 * @dev: The PCI device to intialize. 4729 * @id: Looks like a pointer to the entry in our pci device table 4730 * that was actually matched by the PCI subsystem. 4731 * 4732 * Returns 0 on success, or an error code (-ve) on failure. 4733 **/ 4734static int __devinit dc395x_init_one(struct pci_dev *dev, 4735 const struct pci_device_id *id) 4736{ 4737 struct Scsi_Host *scsi_host = NULL; 4738 struct AdapterCtlBlk *acb = NULL; 4739 unsigned long io_port_base; 4740 unsigned int io_port_len; 4741 unsigned int irq; 4742 4743 dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev)); 4744 banner_display(); 4745 4746 if (pci_enable_device(dev)) 4747 { 4748 dprintkl(KERN_INFO, "PCI Enable device failed.\n"); 4749 return -ENODEV; 4750 } 4751 io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK; 4752 io_port_len = pci_resource_len(dev, 0); 4753 irq = dev->irq; 4754 dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq); 4755 4756 /* allocate scsi host information (includes out adapter) */ 4757 scsi_host = scsi_host_alloc(&dc395x_driver_template, 4758 sizeof(struct AdapterCtlBlk)); 4759 if (!scsi_host) { 4760 dprintkl(KERN_INFO, "scsi_host_alloc failed\n"); 4761 goto fail; 4762 } 4763 acb = (struct AdapterCtlBlk*)scsi_host->hostdata; 4764 acb->scsi_host = scsi_host; 4765 acb->dev = dev; 4766 4767 /* initialise the adapter and everything we need */ 4768 if (adapter_init(acb, io_port_base, io_port_len, irq)) { 4769 dprintkl(KERN_INFO, "adapter init failed\n"); 4770 goto fail; 4771 } 4772 4773 pci_set_master(dev); 4774 4775 /* get the scsi mid level to scan for new devices on the bus */ 4776 if (scsi_add_host(scsi_host, &dev->dev)) { 4777 dprintkl(KERN_ERR, "scsi_add_host failed\n"); 4778 goto fail; 4779 } 4780 pci_set_drvdata(dev, scsi_host); 4781 scsi_scan_host(scsi_host); 4782 4783 return 0; 4784 4785fail: 4786 if (acb != NULL) 4787 adapter_uninit(acb); 4788 if (scsi_host != NULL) 4789 scsi_host_put(scsi_host); 4790 pci_disable_device(dev); 4791 return -ENODEV; 4792} 4793 4794 4795/** 4796 * dc395x_remove_one - Called to remove a single instance of the 4797 * adapter. 4798 * 4799 * @dev: The PCI device to intialize. 4800 **/ 4801static void __devexit dc395x_remove_one(struct pci_dev *dev) 4802{ 4803 struct Scsi_Host *scsi_host = pci_get_drvdata(dev); 4804 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata); 4805 4806 dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb); 4807 4808 scsi_remove_host(scsi_host); 4809 adapter_uninit(acb); 4810 pci_disable_device(dev); 4811 scsi_host_put(scsi_host); 4812 pci_set_drvdata(dev, NULL); 4813} 4814 4815 4816static struct pci_device_id dc395x_pci_table[] = { 4817 { 4818 .vendor = PCI_VENDOR_ID_TEKRAM, 4819 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040, 4820 .subvendor = PCI_ANY_ID, 4821 .subdevice = PCI_ANY_ID, 4822 }, 4823 {} /* Terminating entry */ 4824}; 4825MODULE_DEVICE_TABLE(pci, dc395x_pci_table); 4826 4827 4828static struct pci_driver dc395x_driver = { 4829 .name = DC395X_NAME, 4830 .id_table = dc395x_pci_table, 4831 .probe = dc395x_init_one, 4832 .remove = __devexit_p(dc395x_remove_one), 4833}; 4834 4835 4836/** 4837 * dc395x_module_init - Module initialization function 4838 * 4839 * Used by both module and built-in driver to initialise this driver. 4840 **/ 4841static int __init dc395x_module_init(void) 4842{ 4843 return pci_register_driver(&dc395x_driver); 4844} 4845 4846 4847/** 4848 * dc395x_module_exit - Module cleanup function. 4849 **/ 4850static void __exit dc395x_module_exit(void) 4851{ 4852 pci_unregister_driver(&dc395x_driver); 4853} 4854 4855 4856module_init(dc395x_module_init); 4857module_exit(dc395x_module_exit); 4858 4859MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff"); 4860MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series"); 4861MODULE_LICENSE("GPL"); 4862