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)
9 * C.L. Huang <ching@tekram.com.tw>
10 * Erich Chen <erich@tekram.com.tw>
11 * (C) Copyright 1995-1999 Tekram Technology Co., Ltd.
13 * Kurt Garloff <garloff@suse.de>
14 * (C) 1999-2000 Kurt Garloff
16 * Oliver Neukum <oliver@neukum.name>
17 * Ali Akcaagac <aliakc@web.de>
18 * Jamie Lenehan <lenehan@twibble.org>
23 *************************************************************************
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
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.
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.
47 ************************************************************************
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>
58 #include <scsi/scsicam.h> /* needed for scsicam_bios_param */
59 #include <linux/interrupt.h>
60 #include <linux/init.h>
61 #include <linux/spinlock.h>
62 #include <linux/pci.h>
64 /*---------------------------------------------------------------------------
66 ---------------------------------------------------------------------------*/
68 * Set to disable parts of the driver
70 /*#define DC395x_NO_DISCONNECT*/
71 /*#define DC395x_NO_TAGQ*/
72 /*#define DC395x_NO_SYNC*/
73 /*#define DC395x_NO_WIDE*/
75 /*---------------------------------------------------------------------------
77 ---------------------------------------------------------------------------*/
79 * Types of debugging that can be enabled and disabled
84 #define DBG_DCB 0x0008
85 #define DBG_PARSE 0x0010 /* debug command line parsing */
86 #define DBG_SGPARANOIA 0x0020
87 #define DBG_FIFO 0x0040
88 #define DBG_PIO 0x0080
89 #define DBG_RECURSION 0x0100 /* check for excessive recursion */
90 #define DBG_MALLOC 0x0200 /* report on memory allocations */
91 #define DBG_TRACE 0x0400
92 #define DBG_TRACEALL 0x0800
96 * Set set of things to output debugging for.
97 * Undefine to remove all debugging
99 /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_DCB|DBG_PARSE|DBG_SGPARANOIA|DBG_FIFO|DBG_PIO|DBG_TRACE|DBG_TRACEALL)*/
100 /*#define DEBUG_MASK DBG_0*/
104 * Output a kernel mesage at the specified level and append the
105 * driver name and a ": " to the start of the message
107 #define dprintkl(level, format, arg...) \
108 printk(level DC395X_NAME ": " format , ## arg)
113 * print a debug message - this is formated with KERN_DEBUG, then the
114 * driver name followed by a ": " and then the message is output.
115 * This also checks that the specified debug level is enabled before
116 * outputing the message
118 #define dprintkdbg(type, format, arg...) \
120 if ((type) & (DEBUG_MASK)) \
121 dprintkl(KERN_DEBUG , format , ## arg); \
125 * Check if the specified type of debugging is enabled
127 #define debug_enabled(type) ((DEBUG_MASK) & (type))
131 * No debugging. Do nothing
133 #define dprintkdbg(type, format, arg...) \
135 #define debug_enabled(type) (0)
141 * The recursion debugging just counts entries into the driver and
142 * prints out a messge if it exceeds a certain limit. This variable
145 #if debug_enabled(DBG_RECURSION)
146 static int dbg_in_driver = 0;
151 * Memory allocation debugging
152 * Just reports when memory is allocated and/or released.
154 #if debug_enabled(DBG_MALLOC)
155 inline void *dc395x_kmalloc(size_t sz, int fl)
157 void *ptr = kmalloc(sz, fl);
158 dprintkl(KERN_DEBUG, "Alloc %i bytes @ %p w/ fl %08x\n", sz, ptr, fl);
161 inline void dc395x_kfree(const void *adr)
163 dprintkl(KERN_DEBUG, "Free mem @ %p\n", adr);
167 #define dc395x_kmalloc(sz, fl) kmalloc(sz, fl)
168 #define dc395x_kfree(adr) kfree(adr)
175 #if debug_enabled(DBG_TRACEALL)
176 # define TRACEOUTALL(x...) printk ( x)
178 # define TRACEOUTALL(x...) do {} while (0)
181 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
182 # define DEBUGTRACEBUFSZ 512
183 static char tracebuf[64];
184 static char traceoverflow[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
185 # define TRACEPRINTF(x...) \
187 int ln = sprintf(tracebuf, x); \
188 if (srb->debugpos + ln >= DEBUGTRACEBUFSZ) { \
189 srb->debugtrace[srb->debugpos] = 0; \
190 srb->debugpos = DEBUGTRACEBUFSZ/5; \
191 srb->debugtrace[srb->debugpos++] = '>'; \
193 sprintf(srb->debugtrace + srb->debugpos, "%s", tracebuf); \
194 srb->debugpos += ln - 1; \
196 # define TRACEOUT(x...) printk (x)
198 # define TRACEPRINTF(x...) do {} while (0)
199 # define TRACEOUT(x...) do {} while (0)
203 /*---------------------------------------------------------------------------
204 ---------------------------------------------------------------------------*/
206 #ifndef PCI_VENDOR_ID_TEKRAM
207 #define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */
209 #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
210 #define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */
215 #define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
216 #define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
218 #define DC395x_ACB_INITLOCK(acb) spin_lock_init(&acb->smp_lock)
219 #define DC395x_ACB_LOCK(acb,acb_flags) if (!acb->lock_level_count[cpuid]) { spin_lock_irqsave(&acb->smp_lock,acb_flags); acb->lock_level_count[cpuid]++; } else { acb->lock_level_count[cpuid]++; }
220 #define DC395x_ACB_UNLOCK(acb,acb_flags) if (--acb->lock_level_count[cpuid] == 0) { spin_unlock_irqrestore(&acb->smp_lock,acb_flags); }
222 #define DC395x_SMP_IO_LOCK(dev,irq_flags) spin_lock_irqsave(((struct Scsi_Host*)dev)->host_lock,irq_flags)
223 #define DC395x_SMP_IO_UNLOCK(dev,irq_flags) spin_unlock_irqrestore(((struct Scsi_Host*)dev)->host_lock,irq_flags)
226 #define DC395x_read8(acb,address) (u8)(inb(acb->IOPortBase + (address)))
227 #define DC395x_read8_(address, base) (u8)(inb((USHORT)(base) + (address)))
228 #define DC395x_read16(acb,address) (u16)(inw(acb->IOPortBase + (address)))
229 #define DC395x_read32(acb,address) (u32)(inl(acb->IOPortBase + (address)))
230 #define DC395x_write8(acb,address,value) outb((value), acb->IOPortBase + (address))
231 #define DC395x_write8_(address,value,base) outb((value), (USHORT)(base) + (address))
232 #define DC395x_write16(acb,address,value) outw((value), acb->IOPortBase + (address))
233 #define DC395x_write32(acb,address,value) outl((value), acb->IOPortBase + (address))
236 #define BUS_ADDR(sg) sg_dma_address(&(sg))
237 #define CPU_ADDR(sg) (page_address((sg).page)+(sg).offset)
238 #define PAGE_ADDRESS(sg) page_address((sg)->page)
241 #define RES_TARGET 0x000000FF /* Target State */
242 #define RES_TARGET_LNX STATUS_MASK /* Only official ... */
243 #define RES_ENDMSG 0x0000FF00 /* End Message */
244 #define RES_DID 0x00FF0000 /* DID_ codes */
245 #define RES_DRV 0xFF000000 /* DRIVER_ codes */
247 #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
248 #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
250 #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
251 #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
252 #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
253 #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
254 #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
257 **************************************************************************
262 u32 address; /* bus! address */
267 /*-----------------------------------------------------------------------
269 -----------------------------------------------------------------------*/
271 struct ScsiReqBlk *next;
272 struct DeviceCtlBlk *dcb;
274 /* HW scatter list (up to 64 entries) */
275 struct SGentry *segment_x;
278 unsigned char *virt_addr; /* set by update_sg_list */
280 u32 total_xfer_length;
281 u32 xferred; /* Backup for the already xferred len */
283 u32 sg_bus_addr; /* bus address of DC395x scatterlist */
304 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
311 /*-----------------------------------------------------------------------
313 -----------------------------------------------------------------------*/
314 struct DeviceCtlBlk {
315 struct DeviceCtlBlk *next;
316 struct AdapterCtlBlk *acb;
318 struct ScsiReqBlk *going_srb;
319 struct ScsiReqBlk *going_last;
321 struct ScsiReqBlk *waiting_srb;
322 struct ScsiReqBlk *wait_list;
324 struct ScsiReqBlk *active_srb;
330 u16 waiting_srb_count;
331 u8 target_id; /* SCSI Target ID (SCSI Only) */
332 u8 target_lun; /* SCSI Log. Unit (SCSI Only) */
336 u8 inquiry7; /* To store Inquiry flags */
337 u8 sync_mode; /* 0:async mode */
338 u8 min_nego_period; /* for nego. */
339 u8 sync_period; /* for reg. */
341 u8 sync_offset; /* for reg. and nego.(low nibble) */
347 /*-----------------------------------------------------------------------
348 Adapter Control Block
349 -----------------------------------------------------------------------*/
350 struct AdapterCtlBlk {
351 struct Scsi_Host *scsi_host;
352 struct AdapterCtlBlk *next_acb;
356 struct DeviceCtlBlk *link_dcb;
357 struct DeviceCtlBlk *last_dcb;
358 struct DeviceCtlBlk *dcb_run_robin;
360 struct DeviceCtlBlk *active_dcb;
362 struct ScsiReqBlk *free_srb;
363 struct ScsiReqBlk *tmp_srb;
364 struct timer_list waiting_timer;
365 struct timer_list selto_timer;
368 u16 adapter_index; /* nth Adapter this driver */
383 u8 dcb_map[DC395x_MAX_SCSI_ID];
384 struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
390 struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
391 struct ScsiReqBlk srb;
396 * The SEEPROM structure for TRM_S1040
399 u8 cfg0; /* Target configuration byte 0 */
400 u8 period; /* Target period */
401 u8 cfg2; /* Target configuration byte 2 */
402 u8 cfg3; /* Target configuration byte 3 */
407 u8 sub_vendor_id[2]; /* 0,1 Sub Vendor ID */
408 u8 sub_sys_id[2]; /* 2,3 Sub System ID */
409 u8 sub_class; /* 4 Sub Class */
410 u8 vendor_id[2]; /* 5,6 Vendor ID */
411 u8 device_id[2]; /* 7,8 Device ID */
412 u8 reserved; /* 9 Reserved */
413 struct NVRamTarget target[DC395x_MAX_SCSI_ID];
420 u8 scsi_id; /* 74 Host Adapter SCSI ID */
421 u8 channel_cfg; /* 75 Channel configuration */
422 u8 delay_time; /* 76 Power on delay time */
423 u8 max_tag; /* 77 Maximum tags */
424 u8 reserved0; /* 78 */
425 u8 boot_target; /* 79 */
426 u8 boot_lun; /* 80 */
427 u8 reserved1; /* 81 */
428 u16 reserved2[22]; /* 82,..125 */
429 u16 cksum; /* 126,127 */
433 /*---------------------------------------------------------------------------
435 ---------------------------------------------------------------------------*/
436 static void data_out_phase0(struct AdapterCtlBlk *acb,
437 struct ScsiReqBlk *srb,
439 static void data_in_phase0(struct AdapterCtlBlk *acb,
440 struct ScsiReqBlk *srb,
442 static void command_phase0(struct AdapterCtlBlk *acb,
443 struct ScsiReqBlk *srb,
445 static void status_phase0(struct AdapterCtlBlk *acb,
446 struct ScsiReqBlk *srb,
448 static void msgout_phase0(struct AdapterCtlBlk *acb,
449 struct ScsiReqBlk *srb,
451 static void msgin_phase0(struct AdapterCtlBlk *acb,
452 struct ScsiReqBlk *srb,
454 static void data_out_phase1(struct AdapterCtlBlk *acb,
455 struct ScsiReqBlk *srb,
457 static void data_in_phase1(struct AdapterCtlBlk *acb,
458 struct ScsiReqBlk *srb,
460 static void command_phase1(struct AdapterCtlBlk *acb,
461 struct ScsiReqBlk *srb,
463 static void status_phase1(struct AdapterCtlBlk *acb,
464 struct ScsiReqBlk *srb,
466 static void msgout_phase1(struct AdapterCtlBlk *acb,
467 struct ScsiReqBlk *srb,
469 static void msgin_phase1(struct AdapterCtlBlk *acb,
470 struct ScsiReqBlk *srb,
472 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
474 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
476 static void set_basic_config(struct AdapterCtlBlk *acb);
477 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
478 struct ScsiReqBlk *srb);
479 static void reset_scsi_bus(struct AdapterCtlBlk *acb);
480 static void data_io_transfer(struct AdapterCtlBlk *acb,
481 struct ScsiReqBlk *srb, u16 io_dir);
482 static void disconnect(struct AdapterCtlBlk *acb);
483 static void reselect(struct AdapterCtlBlk *acb);
484 static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
485 struct ScsiReqBlk *srb);
486 static void build_srb(Scsi_Cmnd * cmd, struct DeviceCtlBlk *dcb,
487 struct ScsiReqBlk *srb);
488 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
489 Scsi_Cmnd * cmd, u8 force);
490 static void scsi_reset_detect(struct AdapterCtlBlk *acb);
491 static void pci_unmap_srb(struct AdapterCtlBlk *acb,
492 struct ScsiReqBlk *srb);
493 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
494 struct ScsiReqBlk *srb);
495 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
496 struct ScsiReqBlk *srb);
497 static void srb_done(struct AdapterCtlBlk *acb,
498 struct DeviceCtlBlk *dcb,
499 struct ScsiReqBlk *srb);
500 static void request_sense(struct AdapterCtlBlk *acb,
501 struct DeviceCtlBlk *dcb,
502 struct ScsiReqBlk *srb);
503 static inline void set_xfer_rate(struct AdapterCtlBlk *acb,
504 struct DeviceCtlBlk *dcb);
505 static void init_dcb(struct AdapterCtlBlk *acb,
506 struct DeviceCtlBlk **pdcb, u8 target, u8 lun);
507 static void remove_dev(struct AdapterCtlBlk *acb,
508 struct DeviceCtlBlk *dcb);
509 static void waiting_timeout(unsigned long ptr);
512 /*---------------------------------------------------------------------------
514 ---------------------------------------------------------------------------*/
515 static struct AdapterCtlBlk *acb_list_head = NULL;
516 static struct AdapterCtlBlk *acb_list_tail = NULL;
517 static u16 adapter_count = 0;
518 static u16 current_sync_offset = 0;
519 static char monitor_next_irq = 0;
522 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
524 static void *dc395x_scsi_phase0[] = {
525 data_out_phase0,/* phase:0 */
526 data_in_phase0, /* phase:1 */
527 command_phase0, /* phase:2 */
528 status_phase0, /* phase:3 */
529 nop0, /* phase:4 PH_BUS_FREE .. initial phase */
530 nop0, /* phase:5 PH_BUS_FREE .. initial phase */
531 msgout_phase0, /* phase:6 */
532 msgin_phase0, /* phase:7 */
536 * dc395x_statev = (void *)dc395x_scsi_phase1[phase]
538 static void *dc395x_scsi_phase1[] = {
539 data_out_phase1,/* phase:0 */
540 data_in_phase1, /* phase:1 */
541 command_phase1, /* phase:2 */
542 status_phase1, /* phase:3 */
543 nop1, /* phase:4 PH_BUS_FREE .. initial phase */
544 nop1, /* phase:5 PH_BUS_FREE .. initial phase */
545 msgout_phase1, /* phase:6 */
546 msgin_phase1, /* phase:7 */
549 struct NvRamType eeprom_buf[DC395x_MAX_ADAPTER_NUM];
551 *Fast20: 000 50ns, 20.0 MHz
553 * 010 100ns, 10.0 MHz
560 *Fast40(LVDS): 000 25ns, 40.0 MHz
563 * 011 100ns, 10.0 MHz
569 /*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/
571 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
572 static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
573 static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
574 /* real period:48ns,72ns,100ns,124ns,148ns,172ns,200ns,248ns */
578 /*---------------------------------------------------------------------------
580 ---------------------------------------------------------------------------*/
582 * Module/boot parameters currently effect *all* instances of the
583 * card in the system.
587 * Command line parameters are stored in a structure below.
588 * These are the index's into the structure for the various
589 * command line options.
591 #define CFG_ADAPTER_ID 0
592 #define CFG_MAX_SPEED 1
593 #define CFG_DEV_MODE 2
594 #define CFG_ADAPTER_MODE 3
596 #define CFG_RESET_DELAY 5
598 #define CFG_NUM 6 /* number of configuration items */
602 * Value used to indicate that a command line override
603 * hasn't been used to modify the value.
605 #define CFG_PARAM_UNSET -1
609 * Hold command line parameters.
611 struct ParameterData {
612 int value; /* value of this setting */
613 int min; /* minimum value */
614 int max; /* maximum value */
615 int def; /* default value */
616 int safe; /* safe value */
618 static struct ParameterData __initdata cfg_data[] = {
637 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
638 NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
640 NTC_DO_PARITY_CHK | NTC_DO_SEND_START
646 #ifdef CONFIG_SCSI_MULTI_LUN
649 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
650 /*| NAC_ACTIVE_NEG*/,
651 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
657 3, /* 16 tags (??) */
671 * Safe settings. If set to zero the the BIOS/default values with command line
672 * overrides will be used. If set to 1 then safe and slow settings will be used.
674 static int use_safe_settings = 0;
675 module_param_named(safe, use_safe_settings, bool, 0);
676 MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
679 module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
680 MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
682 module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
683 MODULE_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");
685 module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
686 MODULE_PARM_DESC(dev_mode, "Device mode.");
688 module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
689 MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
691 module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
692 MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
694 module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
695 MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
699 * set_safe_settings - if the use_safe_settings option is set then
700 * set all values to the safe and slow values.
703 void __init set_safe_settings(void)
705 if (use_safe_settings)
709 dprintkl(KERN_INFO, "Using safe settings.\n");
710 for (i = 0; i < CFG_NUM; i++)
712 cfg_data[i].value = cfg_data[i].safe;
719 * fix_settings - reset any boot parameters which are out of range
720 * back to the default values.
723 void __init fix_settings(void)
727 dprintkdbg(DBG_PARSE, "setup %08x %08x %08x %08x %08x %08x\n",
728 cfg_data[CFG_ADAPTER_ID].value,
729 cfg_data[CFG_MAX_SPEED].value,
730 cfg_data[CFG_DEV_MODE].value,
731 cfg_data[CFG_ADAPTER_MODE].value,
732 cfg_data[CFG_TAGS].value,
733 cfg_data[CFG_RESET_DELAY].value);
734 for (i = 0; i < CFG_NUM; i++)
736 if (cfg_data[i].value < cfg_data[i].min ||
737 cfg_data[i].value > cfg_data[i].max)
739 cfg_data[i].value = cfg_data[i].def;
747 * Mapping from the eeprom delay index value (index into this array)
748 * to the the number of actual seconds that the delay should be for.
751 char __initdata eeprom_index_to_delay_map[] = { 1, 3, 5, 10, 16, 30, 60, 120 };
755 * eeprom_index_to_delay - Take the eeprom delay setting and convert it
756 * into a number of seconds.
758 * @eeprom: The eeprom structure in which we find the delay index to map.
761 void __init eeprom_index_to_delay(struct NvRamType *eeprom)
763 eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
768 * delay_to_eeprom_index - Take a delay in seconds and return the closest
769 * eeprom index which will delay for at least that amount of seconds.
771 * @delay: The delay, in seconds, to find the eeprom index for.
773 static int __init delay_to_eeprom_index(int delay)
776 while (idx < 7 && eeprom_index_to_delay_map[idx] < delay) {
784 * eeprom_override - Override the eeprom settings, in the provided
785 * eeprom structure, with values that have been set on the command
788 * @eeprom: The eeprom data to override with command line options.
791 void __init eeprom_override(struct NvRamType *eeprom)
795 /* Adapter Settings */
796 if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET) {
798 (u8)cfg_data[CFG_ADAPTER_ID].value;
800 if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET) {
801 eeprom->channel_cfg =
802 (u8)cfg_data[CFG_ADAPTER_MODE].value;
804 if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET) {
806 delay_to_eeprom_index(cfg_data[CFG_RESET_DELAY].value);
808 if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET) {
809 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
812 /* Device Settings */
813 for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
814 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET) {
815 eeprom->target[id].cfg0 =
816 (u8)cfg_data[CFG_DEV_MODE].value;
818 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET) {
819 eeprom->target[id].period =
820 (u8)cfg_data[CFG_MAX_SPEED].value;
826 /*---------------------------------------------------------------------------
827 ---------------------------------------------------------------------------*/
831 * Queueing philosphy:
832 * There are a couple of lists:
833 * - Waiting: Contains a list of SRBs not yet sent (per DCB)
834 * - Free: List of free SRB slots
836 * If there are no waiting commands for the DCB, the new one is sent to the bus
837 * otherwise the oldest one is taken from the Waiting list and the new one is
838 * queued to the Waiting List
840 * Lists are managed using two pointers and eventually a counter
843 /* Nomen est omen ... */
845 void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
847 if (srb->tag_number < 255) {
848 dcb->tag_mask &= ~(1 << srb->tag_number); /* free tag mask */
849 srb->tag_number = 255;
854 /* Find cmd in SRB list */
856 struct ScsiReqBlk *find_cmd(Scsi_Cmnd * cmd,
857 struct ScsiReqBlk *start)
859 struct ScsiReqBlk *psrb = start;
863 if (psrb->cmd == cmd)
866 } while (psrb && psrb != start);
871 /* Return next free SRB */
873 struct ScsiReqBlk *get_srb_free(struct AdapterCtlBlk *acb)
875 struct ScsiReqBlk *srb;
877 /*DC395x_Free_integrity (acb); */
880 dprintkl(KERN_ERR, "Out of Free SRBs :-(\n");
882 acb->free_srb = srb->next;
890 /* Insert SRB oin top of free list */
892 void insert_srb_free(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
894 dprintkdbg(DBG_0, "Free SRB %p\n", srb);
895 srb->next = acb->free_srb;
900 /* Inserts a SRB to the top of the Waiting list */
902 void insert_srb_waiting(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
904 dprintkdbg(DBG_0, "Insert srb %p cmd %li to Waiting\n", srb, srb->cmd->pid);
905 srb->next = dcb->waiting_srb;
906 if (!dcb->waiting_srb)
907 dcb->wait_list = srb;
908 dcb->waiting_srb = srb;
909 dcb->waiting_srb_count++;
913 /* Queue SRB to waiting list */
915 void append_srb_waiting(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
917 dprintkdbg(DBG_0, "Append srb %p cmd %li to Waiting\n", srb, srb->cmd->pid);
918 if (dcb->waiting_srb)
919 dcb->wait_list->next = srb;
921 dcb->waiting_srb = srb;
923 dcb->wait_list = srb;
924 /* No next one in waiting list */
926 dcb->waiting_srb_count++;
931 void append_srb_going(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
933 dprintkdbg(DBG_0, "Append SRB %p to Going\n", srb);
934 /* Append to the list of Going commands */
936 dcb->going_last->next = srb;
938 dcb->going_srb = srb;
940 dcb->going_last = srb;
941 /* No next one in sent list */
943 dcb->going_srb_count++;
947 /* Find predecessor SRB */
949 struct ScsiReqBlk *find_srb_prev(struct ScsiReqBlk *srb,
950 struct ScsiReqBlk *start)
952 struct ScsiReqBlk *p = start;
959 } while (p && p != start);
964 /* Remove SRB from SRB queue */
966 struct ScsiReqBlk *remove_srb(struct ScsiReqBlk *srb,
967 struct ScsiReqBlk *pre)
969 if (pre->next != srb)
970 pre = find_srb_prev(srb, pre);
972 dprintkl(KERN_ERR, "Internal ERROR: SRB to rmv not found in Q!\n");
975 pre->next = srb->next;
976 /*srb->next = NULL; */
981 /* Remove SRB from Going queue */
983 void remove_srb_going(struct DeviceCtlBlk *dcb,
984 struct ScsiReqBlk *srb,
985 struct ScsiReqBlk *hint)
987 struct ScsiReqBlk *pre = NULL;
988 dprintkdbg(DBG_0, "Remove SRB %p from Going\n", srb);
990 dprintkl(KERN_ERR, "Going_remove %p!\n", srb);
991 if (srb == dcb->going_srb)
992 dcb->going_srb = srb->next;
993 else if (hint && hint->next == srb)
994 pre = remove_srb(srb, hint);
996 pre = remove_srb(srb, dcb->going_srb);
997 if (srb == dcb->going_last)
998 dcb->going_last = pre;
999 dcb->going_srb_count--;
1003 /* Remove SRB from Waiting queue */
1005 void remove_srb_waiting(struct DeviceCtlBlk *dcb,
1006 struct ScsiReqBlk *srb,
1007 struct ScsiReqBlk *hint)
1009 struct ScsiReqBlk *pre = NULL;
1010 dprintkdbg(DBG_0, "Remove SRB %p from Waiting\n", srb);
1012 dprintkl(KERN_ERR, "Waiting_remove %p!\n", srb);
1013 if (srb == dcb->waiting_srb)
1014 dcb->waiting_srb = srb->next;
1015 else if (hint && hint->next == srb)
1016 pre = remove_srb(srb, hint);
1018 pre = remove_srb(srb, dcb->waiting_srb);
1019 if (srb == dcb->wait_list)
1020 dcb->wait_list = pre;
1021 dcb->waiting_srb_count--;
1025 /* Moves SRB from Going list to the top of Waiting list */
1027 void move_srb_going_to_waiting(struct DeviceCtlBlk *dcb,
1028 struct ScsiReqBlk *srb)
1030 dprintkdbg(DBG_0, "Going_to_Waiting (SRB %p) pid = %li\n", srb, srb->cmd->pid);
1031 /* Remove SRB from Going */
1032 remove_srb_going(dcb, srb, 0);
1033 TRACEPRINTF("GtW *");
1034 /* Insert on top of Waiting */
1035 insert_srb_waiting(dcb, srb);
1036 /* Tag Mask must be freed elsewhere ! (KG, 99/06/18) */
1040 /* Moves first SRB from Waiting list to Going list */
1042 void move_srb_waiting_to_going(struct DeviceCtlBlk *dcb,
1043 struct ScsiReqBlk *srb)
1045 /* Remove from waiting list */
1046 dprintkdbg(DBG_0, "Remove SRB %p from head of Waiting\n", srb);
1047 remove_srb_waiting(dcb, srb, 0);
1048 TRACEPRINTF("WtG *");
1049 append_srb_going(dcb, srb);
1053 /* Sets the timer to wake us up */
1055 void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
1057 if (timer_pending(&acb->waiting_timer))
1059 init_timer(&acb->waiting_timer);
1060 acb->waiting_timer.function = waiting_timeout;
1061 acb->waiting_timer.data = (unsigned long) acb;
1062 if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
1063 acb->waiting_timer.expires =
1064 acb->scsi_host->last_reset - HZ / 2 + 1;
1066 acb->waiting_timer.expires = jiffies + to + 1;
1067 add_timer(&acb->waiting_timer);
1071 /* Send the next command from the waiting list to the bus */
1073 void waiting_process_next(struct AdapterCtlBlk *acb)
1075 struct DeviceCtlBlk *ptr;
1076 struct DeviceCtlBlk *ptr1;
1077 struct ScsiReqBlk *srb;
1079 if ((acb->active_dcb)
1080 || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
1082 if (timer_pending(&acb->waiting_timer))
1083 del_timer(&acb->waiting_timer);
1084 ptr = acb->dcb_run_robin;
1085 if (!ptr) { /* This can happen! */
1086 ptr = acb->link_dcb;
1087 acb->dcb_run_robin = ptr;
1093 /* Make sure, the next another device gets scheduled ... */
1094 acb->dcb_run_robin = ptr1->next;
1095 if (!(srb = ptr1->waiting_srb)
1096 || (ptr1->max_command <= ptr1->going_srb_count))
1099 /* Try to send to the bus */
1100 if (!start_scsi(acb, ptr1, srb))
1101 move_srb_waiting_to_going(ptr1, srb);
1103 waiting_set_timer(acb, HZ / 50);
1106 } while (ptr1 != ptr);
1111 /* Wake up waiting queue */
1112 static void waiting_timeout(unsigned long ptr)
1114 unsigned long flags;
1115 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *) ptr;
1116 dprintkdbg(DBG_KG, "Debug: Waiting queue woken up by timer.\n");
1117 DC395x_LOCK_IO(acb->scsi_host, flags);
1118 waiting_process_next(acb);
1119 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1123 /* Get the DCB for a given ID/LUN combination */
1125 struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
1127 return acb->children[id][lun];
1131 /***********************************************************************
1132 * Function: static void send_srb (struct AdapterCtlBlk* acb, struct ScsiReqBlk* srb)
1134 * Purpose: Send SCSI Request Block (srb) to adapter (acb)
1136 * dc395x_queue_command
1137 * waiting_process_next
1139 ***********************************************************************/
1141 void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
1143 struct DeviceCtlBlk *dcb;
1146 if ((dcb->max_command <= dcb->going_srb_count) ||
1147 (acb->active_dcb) ||
1148 (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
1149 append_srb_waiting(dcb, srb);
1150 waiting_process_next(acb);
1154 if (dcb->waiting_srb) {
1155 append_srb_waiting(dcb, srb);
1156 /* srb = waiting_srb_get(dcb); *//* non-existent */
1157 srb = dcb->waiting_srb;
1158 /* Remove from waiting list */
1159 dcb->waiting_srb = srb->next;
1161 if (!dcb->waiting_srb)
1162 dcb->wait_list = NULL;
1166 if (!start_scsi(acb, dcb, srb))
1167 append_srb_going(dcb, srb);
1169 insert_srb_waiting(dcb, srb);
1170 waiting_set_timer(acb, HZ / 50);
1176 *********************************************************************
1178 * Function: static void build_srb (Scsi_Cmd *cmd, struct DeviceCtlBlk* dcb, struct ScsiReqBlk* srb)
1180 * Purpose: Prepare SRB for being sent to Device DCB w/ command *cmd
1182 *********************************************************************
1185 void build_srb(Scsi_Cmnd * cmd, struct DeviceCtlBlk *dcb,
1186 struct ScsiReqBlk *srb)
1189 struct SGentry *sgp;
1190 struct scatterlist *sl;
1194 dprintkdbg(DBG_0, "build_srb..............\n");
1195 /*memset (srb, 0, sizeof (struct ScsiReqBlk)); */
1198 /* Find out about direction */
1199 dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1201 if (cmd->use_sg && dir != PCI_DMA_NONE) {
1202 unsigned int len = 0;
1203 /* TODO: In case usg_sg and the no of segments differ, things
1204 * will probably go wrong. */
1205 max = srb->sg_count =
1206 pci_map_sg(dcb->acb->dev,
1207 (struct scatterlist *) cmd->request_buffer,
1209 sgp = srb->segment_x;
1210 request_size = cmd->request_bufflen;
1211 dprintkdbg(DBG_SGPARANOIA,
1212 "BuildSRB: Bufflen = %d, buffer = %p, use_sg = %d\n",
1213 cmd->request_bufflen, cmd->request_buffer,
1215 dprintkdbg(DBG_SGPARANOIA,
1216 "Mapped %i Segments to %i\n", cmd->use_sg,
1218 sl = (struct scatterlist *) cmd->request_buffer;
1220 srb->virt_addr = page_address(sl->page);
1221 for (i = 0; i < max; i++) {
1222 u32 busaddr = (u32) sg_dma_address(&sl[i]);
1223 u32 seglen = (u32) sl[i].length;
1224 sgp[i].address = busaddr;
1225 sgp[i].length = seglen;
1227 dprintkdbg(DBG_SGPARANOIA,
1228 "Setting up sgp %d, address = 0x%08x, length = %d, tot len = %d\n",
1229 i, busaddr, seglen, len);
1232 /* Fixup for last buffer too big as it is allocated on even page boundaries */
1233 if (len > request_size) {
1234 #if debug_enabled(DBG_KG) || debug_enabled(DBG_SGPARANOIA)
1235 dprintkdbg(DBG_KG|DBG_SGPARANOIA,
1236 "Fixup SG total length: %d->%d, last seg %d->%d\n",
1237 len, request_size, sgp->length,
1238 sgp->length - (len - request_size));
1240 sgp->length -= (len - request_size);
1244 if (dcb->sync_period & WIDE_SYNC && len % 2) {
1248 srb->total_xfer_length = len; /*? */
1249 /* Hopefully this does not cross a page boundary ... */
1251 pci_map_single(dcb->acb->dev, srb->segment_x,
1252 sizeof(struct SGentry) *
1253 DC395x_MAX_SG_LISTENTRY,
1255 dprintkdbg(DBG_SGPARANOIA,
1256 "Map SG descriptor list %p (%05x) to %08x\n",
1258 sizeof(struct SGentry) * DC395x_MAX_SG_LISTENTRY,
1261 if (cmd->request_buffer && dir != PCI_DMA_NONE) {
1262 u32 len = cmd->request_bufflen; /* Actual request size */
1264 srb->segment_x[0].address =
1265 pci_map_single(dcb->acb->dev,
1266 cmd->request_buffer, len, dir);
1268 if (dcb->sync_period & WIDE_SYNC && len % 2)
1270 srb->segment_x[0].length = len;
1271 srb->total_xfer_length = len;
1272 srb->virt_addr = cmd->request_buffer;
1273 srb->sg_bus_addr = 0;
1274 dprintkdbg(DBG_SGPARANOIA,
1275 "BuildSRB: len = %d, buffer = %p, use_sg = %d, map %08x\n",
1276 len, cmd->request_buffer, cmd->use_sg,
1277 srb->segment_x[0].address);
1280 srb->total_xfer_length = 0;
1281 srb->sg_bus_addr = 0;
1283 dprintkdbg(DBG_SGPARANOIA,
1284 "BuildSRB: buflen = %d, buffer = %p, use_sg = %d, NOMAP %08x\n",
1285 cmd->bufflen, cmd->request_buffer,
1286 cmd->use_sg, srb->segment_x[0].address);
1291 srb->adapter_status = 0;
1292 srb->target_status = 0;
1297 srb->retry_count = 0;
1299 #if debug_enabled(DBG_TRACE|DBG_TRACEALL) && debug_enabled(DBG_SGPARANOIA)
1300 if ((unsigned long)srb->debugtrace & (DEBUGTRACEBUFSZ - 1)) {
1301 dprintkdbg(DBG_SGPARANOIA,
1302 "SRB %i (%p): debugtrace %p corrupt!\n",
1303 (srb - dcb->acb->srb_array) /
1304 sizeof(struct ScsiReqBlk), srb, srb->debugtrace);
1307 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
1309 srb->debugtrace = 0;
1311 TRACEPRINTF("pid %li(%li):%02x %02x..(%i-%i) *", cmd->pid,
1312 jiffies, cmd->cmnd[0], cmd->cmnd[1],
1313 cmd->device->id, cmd->device->lun);
1314 srb->tag_number = TAG_NONE;
1316 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1317 srb->end_message = 0;
1324 * dc395x_queue_command - queue scsi command passed from the mid
1325 * layer, invoke 'done' on completion
1327 * @cmd: pointer to scsi command object
1328 * @done: function pointer to be invoked on completion
1330 * Returns 1 if the adapter (host) is busy, else returns 0. One
1331 * reason for an adapter to be busy is that the number
1332 * of outstanding queued commands is already equal to
1333 * struct Scsi_Host::can_queue .
1335 * Required: if struct Scsi_Host::can_queue is ever non-zero
1336 * then this function is required.
1338 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1339 * and is expected to be held on return.
1343 dc395x_queue_command(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
1345 struct DeviceCtlBlk *dcb;
1346 struct ScsiReqBlk *srb;
1347 struct AdapterCtlBlk *acb =
1348 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1350 dprintkdbg(DBG_0, "Queue Cmd=%02x,Tgt=%d,LUN=%d (pid=%li)\n",
1356 #if debug_enabled(DBG_RECURSION)
1357 if (dbg_in_driver++ > NORM_REC_LVL) {
1358 dprintkl(KERN_DEBUG,
1359 "%i queue_command () recursion? (pid=%li)\n",
1360 dbg_in_driver, cmd->pid);
1364 /* Assume BAD_TARGET; will be cleared later */
1365 cmd->result = DID_BAD_TARGET << 16;
1367 /* ignore invalid targets */
1368 if (cmd->device->id >= acb->scsi_host->max_id ||
1369 cmd->device->lun >= acb->scsi_host->max_lun ||
1370 cmd->device->lun >31) {
1374 /* does the specified lun on the specified device exist */
1375 if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
1376 dprintkl(KERN_INFO, "Ignore target %02x lun %02x\n", cmd->device->id,
1381 /* do we have a DCB for the device */
1382 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1384 /* should never happen */
1385 dprintkl(KERN_ERR, "no DCB failed, target %02x lun %02x\n",
1386 cmd->device->id, cmd->device->lun);
1387 dprintkl(KERN_ERR, "No DCB in queuecommand (2)!\n");
1391 /* set callback and clear result in the command */
1392 cmd->scsi_done = done;
1395 /* get a free SRB */
1396 srb = get_srb_free(acb);
1400 * Return 1 since we are unable to queue this command at this
1403 dprintkdbg(DBG_0, "No free SRB's in queuecommand\n");
1407 /* build srb for the command */
1408 build_srb(cmd, dcb, srb);
1410 if (dcb->waiting_srb) {
1411 /* append to waiting queue */
1412 append_srb_waiting(dcb, srb);
1413 waiting_process_next(acb);
1415 /* process immediately */
1418 dprintkdbg(DBG_1, "... command (pid %li) queued successfully.\n", cmd->pid);
1420 #if debug_enabled(DBG_RECURSION)
1427 * Complete the command immediatey, and then return 0 to
1428 * indicate that we have handled the command. This is usually
1429 * done when the commad is for things like non existent
1432 #if debug_enabled(DBG_RECURSION)
1440 /***********************************************************************
1441 * Function static int dc395x_slave_alloc()
1443 * Purpose: Allocate DCB
1444 ***********************************************************************/
1445 static int dc395x_slave_alloc(struct scsi_device *sdp)
1447 struct AdapterCtlBlk *acb;
1448 struct DeviceCtlBlk *dummy;
1450 acb = (struct AdapterCtlBlk *) sdp->host->hostdata;
1452 init_dcb(acb, &dummy, sdp->id, sdp->lun);
1454 return dummy ? 0 : -ENOMEM;
1458 static void dc395x_slave_destroy(struct scsi_device *sdp)
1460 struct AdapterCtlBlk *acb;
1461 struct DeviceCtlBlk *dcb;
1463 acb = (struct AdapterCtlBlk *) sdp->host->hostdata;
1464 dcb = find_dcb(acb, sdp->id, sdp->lun);
1466 remove_dev(acb, dcb);
1471 *********************************************************************
1473 * Function : dc395x_bios_param
1474 * Description: Return the disk geometry for the given SCSI device.
1475 *********************************************************************
1478 int dc395x_bios_param(struct scsi_device *sdev,
1479 struct block_device *bdev,
1483 #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
1484 int heads, sectors, cylinders;
1485 struct AdapterCtlBlk *acb;
1486 int size = capacity;
1488 dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
1489 acb = (struct AdapterCtlBlk *) sdev->host->hostdata;
1492 cylinders = size / (heads * sectors);
1494 if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
1497 cylinders = size / (heads * sectors);
1501 geom[2] = cylinders;
1504 return scsicam_bios_param(bdev, capacity, info);
1510 * DC395x register dump
1513 void dump_register_info(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1514 struct ScsiReqBlk *srb)
1517 struct pci_dev *dev = acb->dev;
1518 pci_read_config_word(dev, PCI_STATUS, &pstat);
1520 dcb = acb->active_dcb;
1522 srb = dcb->active_srb;
1525 dprintkl(KERN_INFO, "dump: SRB %p: cmd %p OOOPS!\n", srb,
1528 dprintkl(KERN_INFO, "dump: SRB %p: cmd %p pid %li: %02x (%02i-%i)\n",
1529 srb, srb->cmd, srb->cmd->pid,
1530 srb->cmd->cmnd[0], srb->cmd->device->id,
1531 srb->cmd->device->lun);
1532 printk(" SGList %p Cnt %i Idx %i Len %i\n",
1533 srb->segment_x, srb->sg_count, srb->sg_index,
1534 srb->total_xfer_length);
1536 (" State %04x Status %02x Phase %02x (%sconn.)\n",
1537 srb->state, srb->status, srb->scsi_phase,
1538 (acb->active_dcb) ? "" : "not");
1539 TRACEOUT(" %s\n", srb->debugtrace);
1541 dprintkl(KERN_INFO, "dump: SCSI block\n");
1543 (" Status %04x FIFOCnt %02x Signals %02x IRQStat %02x\n",
1544 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1545 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1546 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1547 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS));
1549 (" Sync %02x Target %02x RSelID %02x SCSICtr %08x\n",
1550 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1551 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1552 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1553 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER));
1555 (" IRQEn %02x Config %04x Cfg2 %02x Cmd %02x SelTO %02x\n",
1556 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1557 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1558 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1559 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1560 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1561 dprintkl(KERN_INFO, "dump: DMA block\n");
1563 (" Cmd %04x FIFOCnt %02x FStat %02x IRQStat %02x IRQEn %02x Cfg %04x\n",
1564 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1565 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1566 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1567 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1568 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1569 DC395x_read16(acb, TRM_S1040_DMA_CONFIG));
1570 printk(" TCtr %08x CTCtr %08x Addr %08x%08x\n",
1571 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1572 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1573 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1574 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1575 dprintkl(KERN_INFO, "dump: Misc: GCtrl %02x GStat %02x GTmr %02x\n",
1576 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1577 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1578 DC395x_read8(acb, TRM_S1040_GEN_TIMER));
1579 dprintkl(KERN_INFO, "dump: PCI Status %04x\n", pstat);
1585 static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1587 #if debug_enabled(DBG_FIFO)
1588 u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1589 u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1590 if (!(fifocnt & 0x40))
1591 dprintkdbg(DBG_FIFO,
1592 "Clr FIFO (%i bytes) on phase %02x in %s\n",
1593 fifocnt & 0x3f, lines, txt);
1595 #if debug_enabled(DBG_TRACE)
1596 if (acb->active_dcb && acb->active_dcb->active_srb) {
1597 struct ScsiReqBlk *srb = acb->active_dcb->active_srb;
1601 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1606 ********************************************************************
1608 * DC395x_reset scsi_reset_detect
1610 ********************************************************************
1612 static void reset_dev_param(struct AdapterCtlBlk *acb)
1614 struct DeviceCtlBlk *dcb;
1615 struct DeviceCtlBlk *dcb_temp;
1616 struct NvRamType *eeprom;
1620 dprintkdbg(DBG_0, "reset_dev_param..............\n");
1621 dcb = acb->link_dcb;
1627 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1628 dcb->sync_period = 0;
1629 dcb->sync_offset = 0;
1630 index = acb->adapter_index;
1631 eeprom = &eeprom_buf[index];
1633 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1634 /*dcb->AdpMode = eeprom->channel_cfg; */
1635 period_index = eeprom->target[dcb->target_id].period & 0x07;
1636 dcb->min_nego_period = clock_period[period_index];
1637 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1638 || !(acb->config & HCC_WIDE_CARD))
1639 dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1643 while (dcb_temp != dcb && dcb != NULL);
1648 *********************************************************************
1649 * Function : int dc395x_eh_bus_reset(Scsi_Cmnd *cmd)
1650 * Purpose : perform a hard reset on the SCSI bus
1651 * Inputs : cmd - some command for this host (for fetching hooks)
1652 * Returns : SUCCESS (0x2002) on success, else FAILED (0x2003).
1653 *********************************************************************
1655 static int dc395x_eh_bus_reset(Scsi_Cmnd * cmd)
1657 struct AdapterCtlBlk *acb;
1658 /*u32 acb_flags=0; */
1660 dprintkl(KERN_INFO, "reset requested!\n");
1661 acb = (struct AdapterCtlBlk *) cmd->device->host->hostdata;
1662 /* mid level guarantees no recursion */
1663 /*DC395x_ACB_LOCK(acb,acb_flags); */
1665 if (timer_pending(&acb->waiting_timer))
1666 del_timer(&acb->waiting_timer);
1671 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1672 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1673 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1674 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1676 reset_scsi_bus(acb);
1679 /* We may be in serious trouble. Wait some seconds */
1680 acb->scsi_host->last_reset =
1681 jiffies + 3 * HZ / 2 +
1682 HZ * eeprom_buf[acb->adapter_index].delay_time;
1685 * re-enable interrupt
1687 /* Clear SCSI FIFO */
1688 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1689 clear_fifo(acb, "reset");
1690 /* Delete pending IRQ */
1691 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1692 set_basic_config(acb);
1694 reset_dev_param(acb);
1695 doing_srb_done(acb, DID_RESET, cmd, 0);
1697 acb->active_dcb = NULL;
1699 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1700 waiting_process_next(acb);
1702 /*DC395x_ACB_LOCK(acb,acb_flags); */
1708 *********************************************************************
1709 * Function : int dc395x_eh_abort(Scsi_Cmnd *cmd)
1710 * Purpose : abort an errant SCSI command
1711 * Inputs : cmd - command to be aborted
1712 * Returns : SUCCESS (0x2002) on success, else FAILED (0x2003).
1713 *********************************************************************
1715 static int dc395x_eh_abort(Scsi_Cmnd * cmd)
1718 * Look into our command queues: If it has not been sent already,
1719 * we remove it and return success. Otherwise fail.
1721 struct AdapterCtlBlk *acb =
1722 (struct AdapterCtlBlk *) cmd->device->host->hostdata;
1723 struct DeviceCtlBlk *dcb;
1724 struct ScsiReqBlk *srb;
1726 dprintkl(KERN_INFO, "eh abort: cmd %p (pid %li, %02i-%i) ",
1732 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1734 dprintkl(KERN_DEBUG, "abort - no DCB found");
1738 srb = find_cmd(cmd, dcb->waiting_srb);
1740 remove_srb_waiting(dcb, srb, 0);
1741 pci_unmap_srb_sense(acb, srb);
1742 pci_unmap_srb(acb, srb);
1744 insert_srb_free(acb, srb);
1745 dprintkl(KERN_DEBUG, "abort - command found in waiting commands queue");
1746 cmd->result = DID_ABORT << 16;
1749 srb = find_cmd(cmd, dcb->going_srb);
1751 dprintkl(KERN_DEBUG, "abort - command currently in progress");
1752 /* XXX: Should abort the command here */
1754 dprintkl(KERN_DEBUG, "abort - command not found");
1762 void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1763 struct ScsiReqBlk *srb)
1765 u8 *ptr = srb->msgout_buf + srb->msg_count;
1766 if (srb->msg_count > 1) {
1768 "Build_SDTR: msgout_buf BUSY (%i: %02x %02x)\n",
1769 srb->msg_count, srb->msgout_buf[0],
1770 srb->msgout_buf[1]);
1773 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1774 dcb->sync_offset = 0;
1775 dcb->min_nego_period = 200 >> 2;
1776 } else if (dcb->sync_offset == 0)
1777 dcb->sync_offset = SYNC_NEGO_OFFSET;
1779 *ptr++ = MSG_EXTENDED; /* (01h) */
1780 *ptr++ = 3; /* length */
1781 *ptr++ = EXTENDED_SDTR; /* (01h) */
1782 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */
1783 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */
1784 srb->msg_count += 5;
1785 srb->state |= SRB_DO_SYNC_NEGO;
1792 void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1793 struct ScsiReqBlk *srb)
1796 ((dcb->dev_mode & NTC_DO_WIDE_NEGO) & (acb->
1797 config & HCC_WIDE_CARD))
1799 u8 *ptr = srb->msgout_buf + srb->msg_count;
1800 if (srb->msg_count > 1) {
1802 "Build_WDTR: msgout_buf BUSY (%i: %02x %02x)\n",
1803 srb->msg_count, srb->msgout_buf[0],
1804 srb->msgout_buf[1]);
1807 *ptr++ = MSG_EXTENDED; /* (01h) */
1808 *ptr++ = 2; /* length */
1809 *ptr++ = EXTENDED_WDTR; /* (03h) */
1811 srb->msg_count += 4;
1812 srb->state |= SRB_DO_WIDE_NEGO;
1818 /* Timer to work around chip flaw: When selecting and the bus is
1819 * busy, we sometimes miss a Selection timeout IRQ */
1820 void selection_timeout_missed(unsigned long ptr);
1821 /* Sets the timer to wake us up */
1822 static void selto_timer(struct AdapterCtlBlk *acb)
1824 if (timer_pending(&acb->selto_timer))
1826 init_timer(&acb->selto_timer);
1827 acb->selto_timer.function = selection_timeout_missed;
1828 acb->selto_timer.data = (unsigned long) acb;
1830 (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
1831 acb->selto_timer.expires =
1832 acb->scsi_host->last_reset + HZ / 2 + 1;
1834 acb->selto_timer.expires = jiffies + HZ + 1;
1835 add_timer(&acb->selto_timer);
1839 void selection_timeout_missed(unsigned long ptr)
1841 unsigned long flags;
1842 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *) ptr;
1843 struct ScsiReqBlk *srb;
1844 dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1845 if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1846 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1849 DC395x_LOCK_IO(acb->scsi_host, flags);
1850 srb = acb->active_dcb->active_srb;
1851 TRACEPRINTF("N/TO *");
1853 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1860 * DC395x_DoWaitingSRB srb_done
1861 * send_srb request_sense
1864 u8 start_scsi(struct AdapterCtlBlk * acb, struct DeviceCtlBlk * dcb,
1865 struct ScsiReqBlk * srb)
1867 u16 s_stat2, return_code;
1868 u8 s_stat, scsicommand, i, identify_message;
1871 dprintkdbg(DBG_0, "start_scsi..............\n");
1872 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
1874 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1876 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1877 TRACEPRINTF("Start %02x *", s_stat);
1879 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1881 "StartSCSI: pid %li(%02i-%i): BUSY %02x %04x\n",
1882 srb->cmd->pid, dcb->target_id, dcb->target_lun,
1887 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1888 * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed!
1889 * (This is likely to be a bug in the hardware. Obviously, most people
1890 * only have one initiator per SCSI bus.)
1891 * Instead let this fail and have the timer make sure the command is
1892 * tried again after a short time
1895 /*selto_timer (acb); */
1896 /*monitor_next_irq = 1; */
1900 if (acb->active_dcb) {
1901 dprintkl(KERN_DEBUG, "We try to start a SCSI command (%li)!\n",
1903 dprintkl(KERN_DEBUG, "While another one (%li) is active!!\n",
1904 (acb->active_dcb->active_srb ? acb->active_dcb->
1905 active_srb->cmd->pid : 0));
1906 TRACEOUT(" %s\n", srb->debugtrace);
1907 if (acb->active_dcb->active_srb)
1909 acb->active_dcb->active_srb->debugtrace);
1912 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1914 "StartSCSI failed (busy) for pid %li(%02i-%i)\n",
1915 srb->cmd->pid, dcb->target_id, dcb->target_lun);
1919 /* Allow starting of SCSI commands half a second before we allow the mid-level
1920 * to queue them again after a reset */
1921 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1923 "We were just reset and don't accept commands yet!\n");
1928 clear_fifo(acb, "Start");
1929 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1930 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1931 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1932 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1933 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1935 identify_message = dcb->identify_msg;
1936 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1937 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1938 if (srb->flag & AUTO_REQSENSE)
1939 identify_message &= 0xBF;
1941 if (((srb->cmd->cmnd[0] == INQUIRY)
1942 || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1943 || (srb->flag & AUTO_REQSENSE))
1944 && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1945 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1946 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1947 && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1948 && (dcb->target_lun == 0)) {
1949 srb->msgout_buf[0] = identify_message;
1951 scsicommand = SCMD_SEL_ATNSTOP;
1952 srb->state = SRB_MSGOUT;
1954 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1955 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1956 build_wdtr(acb, dcb, srb);
1960 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1961 && dcb->inquiry7 & SCSI_INQ_SYNC) {
1962 build_sdtr(acb, dcb, srb);
1965 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1966 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1967 build_wdtr(acb, dcb, srb);
1973 ** Send identify message
1975 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1977 scsicommand = SCMD_SEL_ATN;
1978 srb->state = SRB_START_;
1979 #ifndef DC395x_NO_TAGQ
1980 if ((dcb->sync_mode & EN_TAG_QUEUEING)
1981 && (identify_message & 0xC0)) {
1982 /* Send Tag message */
1985 while (tag_mask & dcb->tag_mask
1986 && tag_number <= dcb->max_command) {
1987 tag_mask = tag_mask << 1;
1990 if (tag_number >= dcb->max_command) {
1991 dprintkl(KERN_WARNING,
1992 "Start_SCSI: Out of tags for pid %li (%i-%i)\n",
1993 srb->cmd->pid, srb->cmd->device->id,
1994 srb->cmd->device->lun);
1995 srb->state = SRB_READY;
1996 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2003 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
2004 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
2005 dcb->tag_mask |= tag_mask;
2006 srb->tag_number = tag_number;
2007 TRACEPRINTF("Tag %i *", tag_number);
2009 scsicommand = SCMD_SEL_ATN3;
2010 srb->state = SRB_START_;
2015 * Send CDB ..command block .........
2018 "StartSCSI (pid %li) %02x (%i-%i): Tag %i\n",
2019 srb->cmd->pid, srb->cmd->cmnd[0],
2020 srb->cmd->device->id, srb->cmd->device->lun,
2022 if (srb->flag & AUTO_REQSENSE) {
2023 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
2024 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
2025 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2026 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2027 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
2028 sizeof(srb->cmd->sense_buffer));
2029 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2031 ptr = (u8 *) srb->cmd->cmnd;
2032 for (i = 0; i < srb->cmd->cmd_len; i++)
2033 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
2036 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2037 DO_HWRESELECT | DO_DATALATCH);
2038 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
2040 * If start_scsi return 1:
2041 * we caught an interrupt (must be reset or reselection ... )
2042 * : Let's process it first!
2044 dprintkdbg(DBG_0, "Debug: StartSCSI failed (busy) for pid %li(%02i-%i)!\n",
2045 srb->cmd->pid, dcb->target_id, dcb->target_lun);
2046 /*clear_fifo (acb, "Start2"); */
2047 /*DC395x_write16 (TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
2048 srb->state = SRB_READY;
2052 /* This IRQ should NOT get lost, as we did not acknowledge it */
2055 * If start_scsi returns 0:
2056 * we know that the SCSI processor is free
2058 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
2059 dcb->active_srb = srb;
2060 acb->active_dcb = dcb;
2062 /* it's important for atn stop */
2063 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2064 DO_DATALATCH | DO_HWRESELECT);
2068 TRACEPRINTF("%02x *", scsicommand);
2069 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
2076 ********************************************************************
2079 ********************************************************************
2083 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
2084 * have been triggered for this card.
2086 * @acb: a pointer to the adpter control block
2087 * @scsi_status: the status return when we checked the card
2089 static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb, u16 scsi_status)
2091 struct DeviceCtlBlk *dcb;
2092 struct ScsiReqBlk *srb;
2095 unsigned long flags;
2096 void (*dc395x_statev) (struct AdapterCtlBlk *, struct ScsiReqBlk *,
2099 DC395x_LOCK_IO(acb->scsi_host, flags);
2101 /* This acknowledges the IRQ */
2102 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
2103 if ((scsi_status & 0x2007) == 0x2002)
2104 dprintkl(KERN_DEBUG, "COP after COP completed? %04x\n",
2106 #if 1 /*def DBG_0 */
2107 if (monitor_next_irq) {
2109 "status=%04x intstatus=%02x\n", scsi_status,
2114 /*DC395x_ACB_LOCK(acb,acb_flags); */
2115 if (debug_enabled(DBG_KG)) {
2116 if (scsi_intstatus & INT_SELTIMEOUT)
2117 dprintkdbg(DBG_KG, "Sel Timeout IRQ\n");
2119 /*dprintkl(KERN_DEBUG, "DC395x_IRQ: intstatus = %02x ", scsi_intstatus); */
2121 if (timer_pending(&acb->selto_timer))
2122 del_timer(&acb->selto_timer);
2124 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
2125 disconnect(acb); /* bus free interrupt */
2128 if (scsi_intstatus & INT_RESELECTED) {
2132 if (scsi_intstatus & INT_SELECT) {
2133 dprintkl(KERN_INFO, "Host does not support target mode!\n");
2136 if (scsi_intstatus & INT_SCSIRESET) {
2137 scsi_reset_detect(acb);
2140 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
2141 dcb = acb->active_dcb;
2143 dprintkl(KERN_DEBUG,
2144 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
2145 scsi_status, scsi_intstatus);
2148 srb = dcb->active_srb;
2149 if (dcb->flag & ABORT_DEV_) {
2150 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
2151 enable_msgout_abort(acb, srb);
2154 ************************************************************
2155 * software sequential machine
2156 ************************************************************
2158 phase = (u16) srb->scsi_phase;
2161 * call dc395x_scsi_phase0[]... "phase entry"
2162 * handle every phase before start transfer
2164 /* data_out_phase0, phase:0 */
2165 /* data_in_phase0, phase:1 */
2166 /* command_phase0, phase:2 */
2167 /* status_phase0, phase:3 */
2168 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
2169 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
2170 /* msgout_phase0, phase:6 */
2171 /* msgin_phase0, phase:7 */
2172 dc395x_statev = (void *) dc395x_scsi_phase0[phase];
2173 dc395x_statev(acb, srb, &scsi_status);
2175 *$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2177 * if there were any exception occured
2178 * scsi_status will be modify to bus free phase
2179 * new scsi_status transfer out from ... previous dc395x_statev
2181 *$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2183 srb->scsi_phase = scsi_status & PHASEMASK;
2184 phase = (u16) scsi_status & PHASEMASK;
2186 * call dc395x_scsi_phase1[]... "phase entry"
2187 * handle every phase do transfer
2189 /* data_out_phase1, phase:0 */
2190 /* data_in_phase1, phase:1 */
2191 /* command_phase1, phase:2 */
2192 /* status_phase1, phase:3 */
2193 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
2194 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
2195 /* msgout_phase1, phase:6 */
2196 /* msgin_phase1, phase:7 */
2197 dc395x_statev = (void *) dc395x_scsi_phase1[phase];
2198 dc395x_statev(acb, srb, &scsi_status);
2201 DC395x_UNLOCK_IO(acb->scsi_host, flags);
2207 irqreturn_t dc395x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2209 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)dev_id;
2212 irqreturn_t handled = IRQ_NONE;
2214 dprintkdbg(DBG_0, "dc395x_interrupt..............\n");
2215 #if debug_enabled(DBG_RECURSION)
2216 if (dbg_in_driver++ > NORM_REC_LVL) {
2217 dprintkl(KERN_DEBUG, "%i interrupt recursion?\n", dbg_in_driver);
2222 * Check for pending interupt
2224 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
2225 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2226 if (scsi_status & SCSIINTERRUPT) {
2227 /* interupt pending - let's process it! */
2228 dc395x_handle_interrupt(acb, scsi_status);
2229 handled = IRQ_HANDLED;
2231 else if (dma_status & 0x20) {
2232 /* Error from the DMA engine */
2233 dprintkl(KERN_INFO, "Interrupt from DMA engine: %02x!\n", dma_status);
2235 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
2236 if (acb->active_dcb) {
2237 acb->active_dcb-> flag |= ABORT_DEV_;
2238 if (acb->active_dcb->active_srb)
2239 enable_msgout_abort(acb, acb->active_dcb->active_srb);
2241 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
2243 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
2246 handled = IRQ_HANDLED;
2249 #if debug_enabled(DBG_RECURSION)
2257 ********************************************************************
2259 * msgout_phase0: one of dc395x_scsi_phase0[] vectors
2260 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
2262 ********************************************************************
2265 void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2268 dprintkdbg(DBG_0, "msgout_phase0.....\n");
2269 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT)) {
2270 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
2272 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2273 srb->state &= ~SRB_MSGOUT;
2274 TRACEPRINTF("MOP0 *");
2279 ********************************************************************
2281 * msgout_phase1: one of dc395x_scsi_phase0[] vectors
2282 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
2284 ********************************************************************
2287 void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2292 struct DeviceCtlBlk *dcb;
2294 dprintkdbg(DBG_0, "msgout_phase1..............\n");
2295 TRACEPRINTF("MOP1*");
2296 dcb = acb->active_dcb;
2297 clear_fifo(acb, "MOP1");
2298 if (!(srb->state & SRB_MSGOUT)) {
2299 srb->state |= SRB_MSGOUT;
2300 dprintkl(KERN_DEBUG, "Debug: pid %li: MsgOut Phase unexpected.\n", srb->cmd->pid); /* So what ? */
2302 if (!srb->msg_count) {
2303 dprintkdbg(DBG_0, "Debug: pid %li: NOP Msg (no output message there).\n",
2305 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
2306 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2307 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
2309 TRACEOUT(" %s\n", srb->debugtrace);
2312 ptr = (u8 *) srb->msgout_buf;
2314 /*dprintkl(KERN_DEBUG, "Send msg: "); print_msg (ptr, srb->msg_count); */
2315 /*dprintkl(KERN_DEBUG, "MsgOut: "); */
2316 for (i = 0; i < srb->msg_count; i++) {
2317 TRACEPRINTF("%02x *", *ptr);
2318 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
2323 if (/*(dcb->flag & ABORT_DEV_) && */
2324 (srb->msgout_buf[0] == MSG_ABORT))
2325 srb->state = SRB_ABORT_SENT;
2328 /*DC395x_write16 (TRM_S1040_SCSI_CONTROL, DO_DATALATCH); *//* it's important for atn stop */
2332 /*TRACEPRINTF (".*"); */
2333 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
2338 ********************************************************************
2340 * command_phase0: one of dc395x_scsi_phase0[] vectors
2341 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
2343 ********************************************************************
2346 void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2349 TRACEPRINTF("COP0 *");
2351 /*clear_fifo (acb, COP0); */
2352 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2357 ********************************************************************
2359 * command_phase1: one of dc395x_scsi_phase1[] vectors
2360 * dc395x_statev = (void *)dc395x_scsi_phase1[phase]
2362 ********************************************************************
2365 void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2368 struct DeviceCtlBlk *dcb;
2372 dprintkdbg(DBG_0, "command_phase1..............\n");
2373 TRACEPRINTF("COP1*");
2374 clear_fifo(acb, "COP1");
2375 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
2376 if (!(srb->flag & AUTO_REQSENSE)) {
2377 ptr = (u8 *) srb->cmd->cmnd;
2378 for (i = 0; i < srb->cmd->cmd_len; i++) {
2379 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
2383 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
2384 dcb = acb->active_dcb;
2386 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
2387 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2388 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2389 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
2390 sizeof(srb->cmd->sense_buffer));
2391 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2393 srb->state |= SRB_COMMAND;
2394 /* it's important for atn stop */
2395 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2398 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
2402 /* Do sanity checks for S/G list */
2403 static inline void check_sg_list(struct ScsiReqBlk *srb)
2405 if (debug_enabled(DBG_SGPARANOIA)) {
2407 unsigned idx = srb->sg_index;
2408 struct SGentry *psge = srb->segment_x + idx;
2409 for (; idx < srb->sg_count; psge++, idx++)
2410 len += psge->length;
2411 if (len != srb->total_xfer_length)
2412 dprintkdbg(DBG_SGPARANOIA,
2413 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
2414 srb->total_xfer_length, len);
2420 * Compute the next Scatter Gather list index and adjust its length
2421 * and address if necessary; also compute virt_addr
2423 static void update_sg_list(struct ScsiReqBlk *srb, u32 left)
2425 struct SGentry *psge;
2428 Scsi_Cmnd *cmd = srb->cmd;
2429 struct scatterlist *sg;
2430 int segment = cmd->use_sg;
2432 dprintkdbg(DBG_KG, "Update SG: Total %i, Left %i\n",
2433 srb->total_xfer_length, left);
2435 psge = srb->segment_x + srb->sg_index;
2436 /* data that has already been transferred */
2437 xferred = srb->total_xfer_length - left;
2438 if (srb->total_xfer_length != left) {
2439 /*check_sg_list_TX (srb, xferred); */
2441 srb->total_xfer_length = left;
2442 /* parsing from last time disconnect SGIndex */
2443 for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
2444 /* Complete SG entries done */
2445 if (xferred >= psge->length)
2446 xferred -= psge->length;
2447 /* Partial SG entries done */
2449 psge->length -= xferred; /* residue data length */
2450 psge->address += xferred; /* residue data pointer */
2451 srb->sg_index = idx;
2452 pci_dma_sync_single(srb->dcb->
2455 sizeof(struct SGentry)
2457 DC395x_MAX_SG_LISTENTRY,
2465 /* We need the corresponding virtual address sg_to_virt */
2466 /*dprintkl(KERN_DEBUG, "sg_to_virt: bus %08x -> virt ", psge->address); */
2468 srb->virt_addr += xferred;
2469 /*printk("%p\n", srb->virt_addr); */
2472 /* We have to walk the scatterlist to find it */
2473 sg = (struct scatterlist *) cmd->request_buffer;
2475 /*printk("(%08x)%p ", BUS_ADDR(*sg), PAGE_ADDRESS(sg)); */
2476 unsigned long mask =
2477 ~((unsigned long) sg->length - 1) & PAGE_MASK;
2478 if ((BUS_ADDR(*sg) & mask) == (psge->address & mask)) {
2479 srb->virt_addr = (PAGE_ADDRESS(sg)
2481 (psge->address & PAGE_MASK));
2482 /*printk("%p\n", srb->virt_addr); */
2487 dprintkl(KERN_ERR, "sg_to_virt failed!\n");
2493 * cleanup_after_transfer
2495 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
2496 * KG: Currently called from StatusPhase1 ()
2497 * Should probably also be called from other places
2498 * Best might be to call it in DataXXPhase0, if new phase will differ
2501 void cleanup_after_transfer(struct AdapterCtlBlk *acb,
2502 struct ScsiReqBlk *srb)
2504 TRACEPRINTF(" Cln*");
2505 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
2506 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
2507 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2508 clear_fifo(acb, "ClnIn");
2510 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2511 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2512 } else { /* write */
2513 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2514 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2516 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2517 clear_fifo(acb, "ClnOut");
2521 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2526 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
2527 * Seems to be needed for unknown reasons; could be a hardware bug :-(
2529 #define DC395x_LASTPIO 4
2531 ********************************************************************
2533 * data_out_phase0: one of dc395x_scsi_phase0[] vectors
2534 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
2536 ********************************************************************
2539 void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2543 u32 d_left_counter = 0;
2544 struct DeviceCtlBlk *dcb = srb->dcb;
2546 dprintkdbg(DBG_0, "data_out_phase0.....\n");
2547 TRACEPRINTF("DOP0*");
2549 scsi_status = *pscsi_status;
2552 * KG: We need to drain the buffers before we draw any conclusions!
2553 * This means telling the DMA to push the rest into SCSI, telling
2554 * SCSI to push the rest to the bus.
2555 * However, the device might have been the one to stop us (phase
2556 * change), and the data in transit just needs to be accounted so
2557 * it can be retransmitted.)
2560 * KG: Stop DMA engine pushing more data into the SCSI FIFO
2561 * If we need more data, the DMA SG list will be freshly set up, anyway
2563 dprintkdbg(DBG_PIO, "DOP0: DMA_FCNT: %02x, DMA_FSTAT: %02x, SCSI_FCNT: %02x, CTR %06x, stat %04x, Tot: %06x\n",
2564 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2565 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2566 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2567 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2568 srb->total_xfer_length);
2569 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2571 if (!(srb->state & SRB_XFERPAD)) {
2572 if (scsi_status & PARITYERROR)
2573 srb->status |= PARITY_ERROR;
2576 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2577 * is the no of bytes it got from the DMA engine not the no it
2578 * transferred successfully to the device. (And the difference could
2579 * be as much as the FIFO size, I guess ...)
2581 if (!(scsi_status & SCSIXFERDONE)) {
2583 * when data transfer from DMA FIFO to SCSI FIFO
2584 * if there was some data left in SCSI FIFO
2587 (u32) (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2589 if (dcb->sync_period & WIDE_SYNC)
2590 d_left_counter <<= 1;
2593 "Debug: SCSI FIFO contains %i %s in DOP0\n",
2594 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2596 sync_period & WIDE_SYNC) ? "words" :
2599 "SCSI FIFOCNT %02x, SCSI CTR %08x\n",
2600 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2601 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER));
2603 "DMA FIFOCNT %04x, FIFOSTAT %02x, DMA CTR %08x\n",
2604 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2605 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2606 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2609 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2614 * calculate all the residue data that not yet tranfered
2615 * SCSI transfer counter + left in SCSI FIFO data
2617 * .....TRM_S1040_SCSI_COUNTER (24bits)
2618 * The counter always decrement by one for every SCSI byte transfer.
2619 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2620 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2622 if (srb->total_xfer_length > DC395x_LASTPIO)
2624 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2625 TRACEPRINTF("%06x *", d_left_counter);
2627 /* Is this a good idea? */
2628 /*clear_fifo (acb, "DOP1"); */
2629 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2630 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2631 && srb->cmd->request_bufflen % 2) {
2633 dprintkl(KERN_INFO, "DOP0: Discard 1 byte. (%02x)\n",
2637 * KG: Oops again. Same thinko as above: The SCSI might have been
2638 * faster than the DMA engine, so that it ran out of data.
2639 * In that case, we have to do just nothing!
2640 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2643 * KG: This is nonsense: We have been WRITING data to the bus
2644 * If the SCSI engine has no bytes left, how should the DMA engine?
2646 if ((d_left_counter ==
2647 0) /*|| (scsi_status & SCSIXFERCNT_2_ZERO) ) */ ) {
2649 * int ctr = 6000000; u8 TempDMAstatus;
2652 * TempDMAstatus = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2653 * } while( !(TempDMAstatus & DMAXFERCOMP) && --ctr);
2654 * if (ctr < 6000000-1) dprintkl(KERN_DEBUG, "DMA should be complete ... in DOP1\n");
2655 * if (!ctr) dprintkl(KERN_ERR, "Deadlock in DataOutPhase0 !!\n");
2657 srb->total_xfer_length = 0;
2658 } else { /* Update SG list */
2660 * if transfer not yet complete
2661 * there were some data residue in SCSI FIFO or
2662 * SCSI transfer counter not empty
2665 srb->total_xfer_length - d_left_counter;
2667 (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2668 update_sg_list(srb, d_left_counter);
2669 /* KG: Most ugly hack! Apparently, this works around a chip bug */
2670 if ((srb->segment_x[srb->sg_index].length ==
2671 diff && srb->cmd->use_sg)
2672 || ((oldxferred & ~PAGE_MASK) ==
2676 "Work around chip bug (%i)?\n", diff);
2678 srb->total_xfer_length - diff;
2679 update_sg_list(srb, d_left_counter);
2680 /*srb->total_xfer_length -= diff; */
2681 /*srb->virt_addr += diff; */
2682 /*if (srb->cmd->use_sg) */
2683 /* srb->sg_index++; */
2688 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2689 dprintkl(KERN_DEBUG,
2690 "DOP0(%li): %i bytes in SCSI FIFO! (Clear!)\n",
2692 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f);
2694 /*clear_fifo (acb, "DOP0"); */
2695 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, CLRXFIFO | ABORTXFER); */
2697 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2698 /*dprintkl(KERN_DEBUG, "Debug: Clean up after Data Out ...\n"); */
2699 cleanup_after_transfer(acb, srb);
2707 ********************************************************************
2709 * data_out_phase1: one of dc395x_scsi_phase0[] vectors
2710 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
2713 ********************************************************************
2716 void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2720 dprintkdbg(DBG_0, "data_out_phase1.....\n");
2722 TRACEPRINTF("DOP1*");
2723 clear_fifo(acb, "DOP1");
2725 ** do prepare befor transfer when data out phase
2727 data_io_transfer(acb, srb, XFERDATAOUT);
2733 ********************************************************************
2735 * data_in_phase0: one of dc395x_scsi_phase1[] vectors
2736 * dc395x_statev = (void *)dc395x_scsi_phase1[phase]
2738 ********************************************************************
2741 void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2745 u32 d_left_counter = 0;
2746 /*struct DeviceCtlBlk* dcb = srb->dcb; */
2749 dprintkdbg(DBG_0, "data_in_phase0..............\n");
2750 TRACEPRINTF("DIP0*");
2751 scsi_status = *pscsi_status;
2754 * KG: DataIn is much more tricky than DataOut. When the device is finished
2755 * and switches to another phase, the SCSI engine should be finished too.
2756 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2757 * engine and transferred to memory.
2758 * We should wait for the FIFOs to be emptied by that (is there any way to
2759 * enforce this?) and then stop the DMA engine, because it might think, that
2760 * there are more bytes to follow. Yes, the device might disconnect prior to
2761 * having all bytes transferred!
2762 * Also we should make sure that all data from the DMA engine buffer's really
2763 * made its way to the system memory! Some documentation on this would not
2764 * seem to be a bad idea, actually.
2766 if (!(srb->state & SRB_XFERPAD)) {
2767 if (scsi_status & PARITYERROR) {
2769 "Parity Error (pid %li, target %02i-%i)\n",
2770 srb->cmd->pid, srb->cmd->device->id,
2771 srb->cmd->device->lun);
2772 srb->status |= PARITY_ERROR;
2775 * KG: We should wait for the DMA FIFO to be empty ...
2776 * but: it would be better to wait first for the SCSI FIFO and then the
2777 * the DMA FIFO to become empty? How do we know, that the device not already
2778 * sent data to the FIFO in a MsgIn phase, eg.?
2780 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2783 dprintkl(KERN_DEBUG,
2784 "DIP0: Wait for DMA FIFO to flush ...\n");
2785 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2786 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2787 /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2789 (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
2791 if (ctr < 6000000 - 1)
2793 "DIP0: Had to wait for DMA ...\n");
2796 "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2797 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2799 dprintkdbg(DBG_KG, "DIP0: DMA_FIFO: %02x %02x\n",
2800 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2801 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2803 /* Now: Check remainig data: The SCSI counters should tell us ... */
2804 d_left_counter = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER)
2805 + ((DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f)
2806 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2809 dprintkdbg(DBG_KG, "SCSI FIFO contains %i %s in DIP0\n",
2810 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f,
2812 sync_period & WIDE_SYNC) ? "words" : "bytes");
2813 dprintkdbg(DBG_KG, "SCSI FIFOCNT %02x, SCSI CTR %08x\n",
2814 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2815 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER));
2816 dprintkdbg(DBG_KG, "DMA FIFOCNT %02x,%02x DMA CTR %08x\n",
2817 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2818 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2819 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2820 dprintkdbg(DBG_KG, "Remaining: TotXfer: %i, SCSI FIFO+Ctr: %i\n",
2821 srb->total_xfer_length, d_left_counter);
2823 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2825 && srb->total_xfer_length <= DC395x_LASTPIO) {
2826 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2827 /*update_sg_list (srb, d_left_counter); */
2828 dprintkdbg(DBG_PIO, "DIP0: PIO (%i %s) to %p for remaining %i bytes:",
2829 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2832 sync_period & WIDE_SYNC) ? "words" :
2833 "bytes", srb->virt_addr,
2834 srb->total_xfer_length);
2836 if (srb->dcb->sync_period & WIDE_SYNC)
2837 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2840 while (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) !=
2843 DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2844 *(srb->virt_addr)++ = byte;
2845 if (debug_enabled(DBG_PIO))
2846 printk(" %02x", byte);
2847 srb->total_xfer_length--;
2849 srb->segment_x[srb->sg_index].length--;
2850 if (srb->total_xfer_length
2851 && !srb->segment_x[srb->sg_index].
2853 if (debug_enabled(DBG_PIO))
2854 printk(" (next segment)");
2860 if (srb->dcb->sync_period & WIDE_SYNC) {
2861 #if 1 /* Read the last byte ... */
2862 if (srb->total_xfer_length > 0) {
2865 (acb, TRM_S1040_SCSI_FIFO);
2866 *(srb->virt_addr)++ = byte;
2867 srb->total_xfer_length--;
2868 if (debug_enabled(DBG_PIO))
2869 printk(" %02x", byte);
2872 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2874 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2875 /*srb->total_xfer_length = 0; */
2876 if (debug_enabled(DBG_PIO))
2879 #endif /* DC395x_LASTPIO */
2883 * KG: This was in DATAOUT. Does it also belong here?
2884 * Nobody seems to know what counter and fifo_cnt count exactly ...
2886 if (!(scsi_status & SCSIXFERDONE)) {
2888 * when data transfer from DMA FIFO to SCSI FIFO
2889 * if there was some data left in SCSI FIFO
2892 (u32) (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2894 if (srb->dcb->sync_period & WIDE_SYNC)
2895 d_left_counter <<= 1;
2897 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2899 * KG: Seems to be correct ...
2903 /*d_left_counter += DC395x_read32(acb, TRM_S1040_SCSI_COUNTER); */
2905 dprintkl(KERN_DEBUG,
2906 "DIP0: ctr=%08x, DMA_FIFO=%02x,%02x SCSI_FIFO=%02x\n",
2907 d_left_counter, DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2908 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2909 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT));
2910 dprintkl(KERN_DEBUG, "DIP0: DMAStat %02x\n",
2911 DC395x_read8(acb, TRM_S1040_DMA_STATUS));
2914 /* KG: This should not be needed any more! */
2915 if ((d_left_counter == 0)
2916 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2922 DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2923 } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
2926 "Deadlock in DataInPhase0 waiting for DMA!!\n");
2927 srb->total_xfer_length = 0;
2929 #if 0 /*def DBG_KG */
2930 dprintkl(KERN_DEBUG,
2931 "DIP0: DMA not yet ready: %02x: %i -> %i bytes\n",
2932 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
2933 srb->total_xfer_length, d_left_counter);
2935 srb->total_xfer_length = d_left_counter;
2936 } else { /* phase changed */
2939 * when a transfer not yet complete
2940 * but be disconnected by target
2941 * if transfer not yet complete
2942 * there were some data residue in SCSI FIFO or
2943 * SCSI transfer counter not empty
2945 update_sg_list(srb, d_left_counter);
2948 /* KG: The target may decide to disconnect: Empty FIFO before! */
2949 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2950 /*dprintkl(KERN_DEBUG, "Debug: Clean up after Data In ...\n"); */
2951 cleanup_after_transfer(acb, srb);
2954 /* KG: Make sure, no previous transfers are pending! */
2955 bval = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2956 if (!(bval & 0x40)) {
2958 dprintkl(KERN_DEBUG,
2959 "DIP0(%li): %i bytes in SCSI FIFO (stat %04x) (left %08x)!!\n",
2960 srb->cmd->pid, bval & 0x1f, scsi_status,
2962 if ((d_left_counter == 0)
2963 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2964 dprintkl(KERN_DEBUG, "Clear FIFO!\n");
2965 clear_fifo(acb, "DIP0");
2969 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, CLRXFIFO | ABORTXFER); */
2971 /*clear_fifo (acb, "DIP0"); */
2972 /*DC395x_write16 (TRM_S1040_SCSI_CONTROL, DO_DATALATCH); */
2978 ********************************************************************
2980 * data_in_phase1: one of dc395x_scsi_phase0[] vectors
2981 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
2983 ********************************************************************
2986 void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2989 dprintkdbg(DBG_0, "data_in_phase1.....\n");
2990 /* FIFO should be cleared, if previous phase was not DataPhase */
2991 /*clear_fifo (acb, "DIP1"); */
2992 /* Allow data in! */
2993 /*DC395x_write16 (TRM_S1040_SCSI_CONTROL, DO_DATALATCH); */
2994 TRACEPRINTF("DIP1:*");
2996 ** do prepare before transfer when data in phase
2998 data_io_transfer(acb, srb, XFERDATAIN);
3004 ********************************************************************
3008 ********************************************************************
3011 void data_io_transfer(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3015 struct DeviceCtlBlk *dcb;
3017 dprintkdbg(DBG_0, "DataIO_transfer %c (pid %li): len = %i, SG: %i/%i\n",
3018 ((io_dir & DMACMD_DIR) ? 'r' : 'w'), srb->cmd->pid,
3019 srb->total_xfer_length, srb->sg_index,
3021 TRACEPRINTF("%05x(%i/%i)*", srb->total_xfer_length,
3022 srb->sg_index, srb->sg_count);
3024 if (srb == acb->tmp_srb) {
3025 dprintkl(KERN_ERR, "Using tmp_srb in DataPhase!\n");
3027 if (srb->sg_index < srb->sg_count) {
3028 if (srb->total_xfer_length > DC395x_LASTPIO) {
3029 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
3031 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
3032 * Maybe, even ABORTXFER would be appropriate
3034 if (dma_status & XFERPENDING) {
3035 dprintkl(KERN_DEBUG, "Xfer pending! Expect trouble!!\n");
3036 dump_register_info(acb, dcb, srb);
3037 DC395x_write8(acb, TRM_S1040_DMA_CONTROL,
3040 /*clear_fifo (acb, "IO"); */
3042 * load what physical address of Scatter/Gather list table want to be
3045 srb->state |= SRB_DATA_XFER;
3046 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
3047 if (srb->cmd->use_sg) { /* with S/G */
3048 io_dir |= DMACMD_SG;
3049 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
3051 sizeof(struct SGentry) *
3053 /* load how many bytes in the Scatter/Gather list table */
3054 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
3057 srb->sg_index) << 3));
3058 } else { /* without S/G */
3059 io_dir &= ~DMACMD_SG;
3060 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
3061 srb->segment_x[0].address);
3062 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
3063 srb->segment_x[0].length);
3065 /* load total transfer length (24bits) max value 16Mbyte */
3066 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
3067 srb->total_xfer_length);
3068 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3069 if (io_dir & DMACMD_DIR) { /* read */
3070 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
3072 DC395x_write16(acb, TRM_S1040_DMA_COMMAND,
3075 DC395x_write16(acb, TRM_S1040_DMA_COMMAND,
3077 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
3083 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
3084 /*clear_fifo (acb, "IO"); */
3086 * load what physical address of Scatter/Gather list table want to be
3089 srb->state |= SRB_DATA_XFER;
3090 /* load total transfer length (24bits) max value 16Mbyte */
3091 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
3092 srb->total_xfer_length);
3093 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3094 if (io_dir & DMACMD_DIR) { /* read */
3095 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
3097 } else { /* write */
3098 int ln = srb->total_xfer_length;
3099 if (srb->dcb->sync_period & WIDE_SYNC)
3101 (acb, TRM_S1040_SCSI_CONFIG2,
3103 dprintkdbg(DBG_PIO, "DOP1: PIO %i bytes from %p:",
3104 srb->total_xfer_length,
3106 while (srb->total_xfer_length) {
3107 if (debug_enabled(DBG_PIO))
3108 printk(" %02x", (unsigned char) *(srb->virt_addr));
3110 (acb, TRM_S1040_SCSI_FIFO,
3111 *(srb->virt_addr)++);
3112 srb->total_xfer_length--;
3113 srb->segment_x[srb->sg_index].
3115 if (srb->total_xfer_length
3116 && !srb->segment_x[srb->
3119 if (debug_enabled(DBG_PIO))
3120 printk(" (next segment)");
3123 srb->total_xfer_length);
3126 if (srb->dcb->sync_period & WIDE_SYNC) {
3128 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
3129 if (debug_enabled(DBG_PIO))
3133 (acb, TRM_S1040_SCSI_CONFIG2, 0);
3135 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
3136 if (debug_enabled(DBG_PIO))
3138 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
3142 #endif /* DC395x_LASTPIO */
3143 else { /* xfer pad */
3145 u8 data = 0, data2 = 0;
3146 if (srb->sg_count) {
3147 srb->adapter_status = H_OVER_UNDER_RUN;
3148 srb->status |= OVER_RUN;
3151 * KG: despite the fact that we are using 16 bits I/O ops
3152 * the SCSI FIFO is only 8 bits according to the docs
3153 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
3155 if (dcb->sync_period & WIDE_SYNC) {
3156 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
3157 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
3159 if (io_dir & DMACMD_DIR) { /* read */
3162 (acb, TRM_S1040_SCSI_FIFO);
3165 (acb, TRM_S1040_SCSI_FIFO);
3166 /*dprintkl(KERN_DEBUG, "DataIO: Xfer pad: %02x %02x\n", data, data2); */
3168 /* Danger, Robinson: If you find KGs scattered over the wide
3169 * disk, the driver or chip is to blame :-( */
3170 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
3172 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
3175 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
3177 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
3178 /* Danger, Robinson: If you find a collection of Ks on your disk
3179 * something broke :-( */
3180 if (io_dir & DMACMD_DIR) { /* read */
3183 (acb, TRM_S1040_SCSI_FIFO);
3184 /*dprintkl(KERN_DEBUG, "DataIO: Xfer pad: %02x\n", data); */
3186 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
3190 srb->state |= SRB_XFERPAD;
3191 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3196 (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN :
3198 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
3201 /*monitor_next_irq = 2; */
3202 /*printk(" done\n"); */
3207 ********************************************************************
3209 * status_phase0: one of dc395x_scsi_phase0[] vectors
3210 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
3212 ********************************************************************
3215 void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3218 dprintkdbg(DBG_0, "StatusPhase0 (pid %li)\n", srb->cmd->pid);
3219 TRACEPRINTF("STP0 *");
3220 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
3221 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
3222 srb->state = SRB_COMPLETED;
3223 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
3225 /*clear_fifo (acb, "STP0"); */
3226 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3230 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3235 ********************************************************************
3237 * status_phase1: one of dc395x_scsi_phase1[] vectors
3238 * dc395x_statev = (void *)dc395x_scsi_phase1[phase]
3240 ********************************************************************
3243 void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3246 dprintkdbg(DBG_0, "StatusPhase1 (pid=%li)\n", srb->cmd->pid);
3247 TRACEPRINTF("STP1 *");
3248 /* Cleanup is now done at the end of DataXXPhase0 */
3249 /*cleanup_after_transfer (acb, srb); */
3251 srb->state = SRB_STATUS;
3252 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3256 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
3259 /* Message handling */
3262 /* Print received message */
3263 static void print_msg(u8 * msg_buf, u32 len)
3266 printk(" %02x", msg_buf[0]);
3267 for (i = 1; i < len; i++)
3268 printk(" %02x", msg_buf[i]);
3273 /* Check if the message is complete */
3274 static inline u8 msgin_completed(u8 * msgbuf, u32 len)
3276 if (*msgbuf == EXTENDED_MESSAGE) {
3279 if (len < msgbuf[1] + 2)
3281 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
3287 #define DC395x_ENABLE_MSGOUT \
3288 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
3289 srb->state |= SRB_MSGOUT
3294 void msgin_reject(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3296 srb->msgout_buf[0] = MESSAGE_REJECT;
3298 DC395x_ENABLE_MSGOUT;
3299 srb->state &= ~SRB_MSGIN;
3300 srb->state |= SRB_MSGOUT;
3302 "Reject message %02x from %02i-%i\n", srb->msgin_buf[0],
3303 srb->dcb->target_id, srb->dcb->target_lun);
3310 void enable_msgout_abort(struct AdapterCtlBlk *acb,
3311 struct ScsiReqBlk *srb)
3313 srb->msgout_buf[0] = ABORT;
3315 DC395x_ENABLE_MSGOUT;
3316 srb->state &= ~SRB_MSGIN;
3317 srb->state |= SRB_MSGOUT;
3320 srb->dcb->flag &= ~ABORT_DEV_;
3327 struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
3328 struct DeviceCtlBlk *dcb,
3331 struct ScsiReqBlk *last_srb = dcb->going_last;
3332 struct ScsiReqBlk *srb = dcb->going_srb;
3333 dprintkdbg(DBG_0, "QTag Msg (SRB %p): %i\n", srb, tag);
3334 if (!(dcb->tag_mask & (1 << tag)))
3335 dprintkl(KERN_DEBUG,
3336 "MsgIn_QTag: tag_mask (%08x) does not reserve tag %i!\n",
3337 dcb->tag_mask, tag);
3342 if (srb->tag_number == tag)
3344 if (srb == last_srb)
3348 dprintkdbg(DBG_0, "pid %li (%i-%i)\n", srb->cmd->pid,
3349 srb->dcb->target_id, srb->dcb->target_lun);
3350 if (dcb->flag & ABORT_DEV_) {
3351 /*srb->state = SRB_ABORT_SENT; */
3352 enable_msgout_abort(acb, srb);
3355 if (!(srb->state & SRB_DISCONNECT))
3359 TRACEPRINTF("[%s]*", dcb->active_srb->debugtrace);
3360 TRACEPRINTF("RTag*");
3361 /* Just for debugging ... */
3363 srb = dcb->active_srb;
3364 TRACEPRINTF("Found.*");
3367 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
3368 srb->state |= dcb->active_srb->state;
3369 srb->state |= SRB_DATA_XFER;
3370 dcb->active_srb = srb;
3371 /* How can we make the DORS happy? */
3376 srb->state = SRB_UNEXPECT_RESEL;
3377 dcb->active_srb = srb;
3378 srb->msgout_buf[0] = MSG_ABORT_TAG;
3380 DC395x_ENABLE_MSGOUT;
3382 dprintkl(KERN_DEBUG, "Unknown tag received: %i: abort !!\n", tag);
3387 /* Reprogram registers */
3389 reprogram_regs(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
3391 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
3392 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
3393 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
3394 set_xfer_rate(acb, dcb);
3398 /* set async transfer mode */
3400 void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3402 struct DeviceCtlBlk *dcb = srb->dcb;
3403 dprintkl(KERN_DEBUG, "Target %02i: No sync transfers\n", dcb->target_id);
3404 TRACEPRINTF("!S *");
3405 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
3406 dcb->sync_mode |= SYNC_NEGO_DONE;
3407 /*dcb->sync_period &= 0; */
3408 dcb->sync_offset = 0;
3409 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
3410 srb->state &= ~SRB_DO_SYNC_NEGO;
3411 reprogram_regs(acb, dcb);
3412 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
3413 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
3414 build_wdtr(acb, dcb, srb);
3415 DC395x_ENABLE_MSGOUT;
3416 dprintkdbg(DBG_0, "SDTR(rej): Try WDTR anyway ...\n");
3421 /* set sync transfer mode */
3423 void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3427 struct DeviceCtlBlk *dcb = srb->dcb;
3428 /*u8 oldsyncperiod = dcb->sync_period; */
3429 /*u8 oldsyncoffset = dcb->sync_offset; */
3431 dprintkdbg(DBG_1, "Target %02i: Sync: %ins (%02i.%01i MHz) Offset %i\n",
3432 dcb->target_id, srb->msgin_buf[3] << 2,
3433 (250 / srb->msgin_buf[3]),
3434 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
3437 if (srb->msgin_buf[4] > 15)
3438 srb->msgin_buf[4] = 15;
3439 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
3440 dcb->sync_offset = 0;
3441 else if (dcb->sync_offset == 0)
3442 dcb->sync_offset = srb->msgin_buf[4];
3443 if (srb->msgin_buf[4] > dcb->sync_offset)
3444 srb->msgin_buf[4] = dcb->sync_offset;
3446 dcb->sync_offset = srb->msgin_buf[4];
3448 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
3449 || dcb->min_nego_period >
3450 clock_period[bval]))
3452 if (srb->msgin_buf[3] < clock_period[bval])
3454 "Increase sync nego period to %ins\n",
3455 clock_period[bval] << 2);
3456 srb->msgin_buf[3] = clock_period[bval];
3457 dcb->sync_period &= 0xf0;
3458 dcb->sync_period |= ALT_SYNC | bval;
3459 dcb->min_nego_period = srb->msgin_buf[3];
3461 if (dcb->sync_period & WIDE_SYNC)
3467 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
3468 dcb->target_id, (fact == 500) ? "Wide16" : "",
3469 dcb->min_nego_period << 2, dcb->sync_offset,
3470 (fact / dcb->min_nego_period),
3471 ((fact % dcb->min_nego_period) * 10 +
3472 dcb->min_nego_period / 2) / dcb->min_nego_period);
3474 TRACEPRINTF("S%i *", dcb->min_nego_period << 2);
3475 if (!(srb->state & SRB_DO_SYNC_NEGO)) {
3476 /* Reply with corrected SDTR Message */
3477 dprintkl(KERN_DEBUG, " .. answer w/ %ins %i\n",
3478 srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
3480 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
3482 DC395x_ENABLE_MSGOUT;
3483 dcb->sync_mode |= SYNC_NEGO_DONE;
3485 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
3486 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
3487 build_wdtr(acb, dcb, srb);
3488 DC395x_ENABLE_MSGOUT;
3489 dprintkdbg(DBG_0, "SDTR: Also try WDTR ...\n");
3492 srb->state &= ~SRB_DO_SYNC_NEGO;
3493 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
3495 reprogram_regs(acb, dcb);
3500 void msgin_set_nowide(struct AdapterCtlBlk *acb,
3501 struct ScsiReqBlk *srb)
3503 struct DeviceCtlBlk *dcb = srb->dcb;
3504 dprintkdbg(DBG_KG, "WDTR got rejected from target %02i\n",
3506 TRACEPRINTF("!W *");
3507 dcb->sync_period &= ~WIDE_SYNC;
3508 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
3509 dcb->sync_mode |= WIDE_NEGO_DONE;
3510 srb->state &= ~SRB_DO_WIDE_NEGO;
3511 reprogram_regs(acb, dcb);
3512 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
3513 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
3514 build_sdtr(acb, dcb, srb);
3515 DC395x_ENABLE_MSGOUT;
3516 dprintkdbg(DBG_0, "WDTR(rej): Try SDTR anyway ...\n");
3521 void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3523 struct DeviceCtlBlk *dcb = srb->dcb;
3524 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
3525 && acb->config & HCC_WIDE_CARD) ? 1 : 0;
3526 if (srb->msgin_buf[3] > wide)
3527 srb->msgin_buf[3] = wide;
3529 if (!(srb->state & SRB_DO_WIDE_NEGO)) {
3530 dprintkl(KERN_DEBUG,
3531 "Target %02i initiates Wide Nego ...\n",
3533 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
3535 srb->state |= SRB_DO_WIDE_NEGO;
3536 DC395x_ENABLE_MSGOUT;
3539 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
3540 if (srb->msgin_buf[3] > 0)
3541 dcb->sync_period |= WIDE_SYNC;
3543 dcb->sync_period &= ~WIDE_SYNC;
3544 srb->state &= ~SRB_DO_WIDE_NEGO;
3545 TRACEPRINTF("W%i *", (dcb->sync_period & WIDE_SYNC ? 1 : 0));
3546 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
3548 "Wide transfers (%i bit) negotiated with target %02i\n",
3549 (8 << srb->msgin_buf[3]), dcb->target_id);
3550 reprogram_regs(acb, dcb);
3551 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
3552 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
3553 build_sdtr(acb, dcb, srb);
3554 DC395x_ENABLE_MSGOUT;
3555 dprintkdbg(DBG_0, "WDTR: Also try SDTR ...\n");
3561 ********************************************************************
3563 * msgin_phase0: one of dc395x_scsi_phase0[] vectors
3564 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
3567 * extended message codes:
3572 * 00h MODIFY DATA POINTER
3573 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
3574 * 03h WIDE DATA TRANSFER REQUEST
3575 * 04h - 7Fh Reserved
3576 * 80h - FFh Vendor specific
3578 ********************************************************************
3581 void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3584 struct DeviceCtlBlk *dcb;
3586 dprintkdbg(DBG_0, "msgin_phase0..............\n");
3587 TRACEPRINTF("MIP0*");
3588 dcb = acb->active_dcb;
3590 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
3591 if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
3592 TRACEPRINTF("(%02x)*", srb->msgin_buf[0]);
3593 /*dprintkl(KERN_INFO, "MsgIn:"); */
3594 /*print_msg (srb->msgin_buf, acb->msg_len); */
3596 /* Now eval the msg */
3597 switch (srb->msgin_buf[0]) {
3599 srb->state = SRB_DISCONNECT;
3602 case SIMPLE_QUEUE_TAG:
3603 case HEAD_OF_QUEUE_TAG:
3604 case ORDERED_QUEUE_TAG:
3605 TRACEPRINTF("(%02x)*", srb->msgin_buf[1]);
3607 msgin_qtag(acb, dcb,
3611 case MESSAGE_REJECT:
3612 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
3613 DO_CLRATN | DO_DATALATCH);
3614 /* A sync nego message was rejected ! */
3615 if (srb->state & SRB_DO_SYNC_NEGO) {
3616 msgin_set_async(acb, srb);
3619 /* A wide nego message was rejected ! */
3620 if (srb->state & SRB_DO_WIDE_NEGO) {
3621 msgin_set_nowide(acb, srb);
3624 enable_msgout_abort(acb, srb);
3625 /*srb->state |= SRB_ABORT_SENT */
3628 case EXTENDED_MESSAGE:
3629 TRACEPRINTF("(%02x)*", srb->msgin_buf[2]);
3631 if (srb->msgin_buf[1] == 3
3632 && srb->msgin_buf[2] == EXTENDED_SDTR) {
3633 msgin_set_sync(acb, srb);
3637 if (srb->msgin_buf[1] == 2 && srb->msgin_buf[2] == EXTENDED_WDTR && srb->msgin_buf[3] <= 2) { /* sanity check ... */
3638 msgin_set_wide(acb, srb);
3641 msgin_reject(acb, srb);
3644 /* Discard wide residual */
3645 case MSG_IGNOREWIDE:
3646 dprintkdbg(DBG_0, "Ignore Wide Residual!\n");
3647 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 1); */
3648 /*DC395x_read8 (TRM_S1040_SCSI_FIFO); */
3651 /* nothing has to be done */
3652 case COMMAND_COMPLETE:
3656 * SAVE POINTER may be ignored as we have the struct ScsiReqBlk* associated with the
3657 * scsi command. Thanks, Gérard, for pointing it out.
3660 dprintkdbg(DBG_0, "SAVE POINTER message received (pid %li: rem.%i) ... ignore :-(\n",
3661 srb->cmd->pid, srb->total_xfer_length);
3662 /*srb->Saved_Ptr = srb->TotalxferredLen; */
3664 /* The device might want to restart transfer with a RESTORE */
3665 case RESTORE_POINTERS:
3666 dprintkl(KERN_DEBUG,
3667 "RESTORE POINTER message received ... ignore :-(\n");
3668 /*dc395x_restore_ptr (acb, srb); */
3671 dprintkl(KERN_DEBUG,
3672 "ABORT msg received (pid %li %02i-%i)\n",
3673 srb->cmd->pid, dcb->target_id,
3675 dcb->flag |= ABORT_DEV_;
3676 enable_msgout_abort(acb, srb);
3678 /* reject unknown messages */
3680 if (srb->msgin_buf[0] & IDENTIFY_BASE) {
3681 dprintkl(KERN_DEBUG, "Identify Message received?\n");
3682 /*TRACEOUT (" %s\n", srb->debugtrace); */
3684 srb->msgout_buf[0] = dcb->identify_msg;
3685 DC395x_ENABLE_MSGOUT;
3686 srb->state |= SRB_MSGOUT;
3689 msgin_reject(acb, srb);
3690 TRACEOUT(" %s\n", srb->debugtrace);
3694 /* Clear counter and MsgIn state */
3695 srb->state &= ~SRB_MSGIN;
3700 if ((*pscsi_status & PHASEMASK) != PH_MSG_IN)
3702 clear_fifo(acb, "MIP0_");
3704 TRACEPRINTF("N/Cln *");
3706 *pscsi_status = PH_BUS_FREE;
3707 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
3708 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3713 ********************************************************************
3715 * msgin_phase1: one of dc395x_scsi_phase1[] vectors
3716 * dc395x_statev = (void *)dc395x_scsi_phase1[phase]
3718 ********************************************************************
3721 void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3724 dprintkdbg(DBG_0, "msgin_phase1..............\n");
3725 TRACEPRINTF("MIP1 *");
3726 clear_fifo(acb, "MIP1");
3727 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
3728 if (!(srb->state & SRB_MSGIN)) {
3729 srb->state &= ~SRB_DISCONNECT;
3730 srb->state |= SRB_MSGIN;
3732 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3736 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
3741 ********************************************************************
3743 * nop0: one of dc395x_scsi_phase1[] ,dc395x_scsi_phase0[] vectors
3744 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
3745 * dc395x_statev = (void *)dc395x_scsi_phase1[phase]
3746 * if phase =4 ..PH_BUS_FREE
3747 ********************************************************************
3750 void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3753 /*TRACEPRINTF("NOP0 *"); */
3758 ********************************************************************
3760 * nop1: one of dc395x_scsi_phase0[] ,dc395x_scsi_phase1[] vectors
3761 * dc395x_statev = (void *)dc395x_scsi_phase0[phase]
3762 * dc395x_statev = (void *)dc395x_scsi_phase1[phase]
3764 ********************************************************************
3767 void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3770 /*TRACEPRINTF("NOP1 *"); */
3775 ********************************************************************
3778 ********************************************************************
3781 void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
3785 struct DeviceCtlBlk *dcb_temp;
3788 ** set all lun device's period , offset
3790 if (!(dcb->identify_msg & 0x07)) {
3791 if (acb->scan_devices)
3792 current_sync_offset = dcb->sync_offset;
3794 dcb_temp = acb->link_dcb;
3795 cnt = acb->dcb_count;
3796 bval = dcb->target_id;
3797 for (i = 0; i < cnt; i++) {
3798 if (dcb_temp->target_id == bval) {
3799 dcb_temp->sync_period =
3801 dcb_temp->sync_offset =
3803 dcb_temp->sync_mode =
3805 dcb_temp->min_nego_period =
3806 dcb->min_nego_period;
3808 dcb_temp = dcb_temp->next;
3817 ********************************************************************
3820 ********************************************************************
3822 static void disconnect(struct AdapterCtlBlk *acb)
3824 struct DeviceCtlBlk *dcb;
3825 struct ScsiReqBlk *srb;
3827 dprintkdbg(DBG_0, "Disconnect (pid=%li)\n", acb->active_dcb->active_srb->cmd->pid);
3828 dcb = acb->active_dcb;
3830 dprintkl(KERN_ERR, "Disc: Exception Disconnect dcb=NULL !!\n ");
3832 /* Suspend queue for a while */
3833 acb->scsi_host->last_reset =
3836 eeprom_buf[acb->adapter_index].
3838 clear_fifo(acb, "DiscEx");
3839 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3842 srb = dcb->active_srb;
3843 acb->active_dcb = NULL;
3844 TRACEPRINTF("DISC *");
3846 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3847 clear_fifo(acb, "Disc");
3848 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3849 if (srb->state & SRB_UNEXPECT_RESEL) {
3850 dprintkl(KERN_ERR, "Disc: Unexpected Reselection (%i-%i)\n",
3851 dcb->target_id, dcb->target_lun);
3853 waiting_process_next(acb);
3854 } else if (srb->state & SRB_ABORT_SENT) {
3855 /*Scsi_Cmnd* cmd = srb->cmd; */
3856 dcb->flag &= ~ABORT_DEV_;
3857 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
3858 dprintkl(KERN_ERR, "Disc: SRB_ABORT_SENT!\n");
3859 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
3860 waiting_process_next(acb);
3862 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
3864 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
3866 * Selection time out
3867 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
3869 /* Unexp. Disc / Sel Timeout */
3870 if (srb->state != SRB_START_
3871 && srb->state != SRB_MSGOUT) {
3872 srb->state = SRB_READY;
3873 dprintkl(KERN_DEBUG, "Unexpected Disconnection (pid %li)!\n",
3875 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
3876 TRACEPRINTF("UnExpD *");
3877 TRACEOUT("%s\n", srb->debugtrace);
3880 /* Normal selection timeout */
3881 TRACEPRINTF("SlTO *");
3883 "Disc: SelTO (pid=%li) for dev %02i-%i\n",
3884 srb->cmd->pid, dcb->target_id,
3886 if (srb->retry_count++ > DC395x_MAX_RETRIES
3887 || acb->scan_devices) {
3888 srb->target_status =
3889 SCSI_STAT_SEL_TIMEOUT;
3893 move_srb_going_to_waiting(dcb, srb);
3894 dprintkdbg(DBG_KG, "Retry pid %li ...\n",
3896 waiting_set_timer(acb, HZ / 20);
3898 } else if (srb->state & SRB_DISCONNECT) {
3899 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
3901 * SRB_DISCONNECT (This is what we expect!)
3903 /* dprintkl(KERN_DEBUG, "DoWaitingSRB (pid=%li)\n", srb->cmd->pid); */
3906 dprintkdbg(DBG_0, "Debug: DISC: SCSI bus stat %02x: ACK set! Other controllers?\n",
3908 /* It could come from another initiator, therefore don't do much ! */
3909 TRACEPRINTF("ACK(%02x) *", bval);
3910 /*dump_register_info (acb, dcb, srb); */
3911 /*TRACEOUT (" %s\n", srb->debugtrace); */
3912 /*dcb->flag |= ABORT_DEV_; */
3913 /*enable_msgout_abort (acb, srb); */
3914 /*DC395x_write16 (TRM_S1040_SCSI_CONTROL, DO_CLRFIFO | DO_CLRATN | DO_HWRESELECT); */
3916 waiting_process_next(acb);
3917 } else if (srb->state & SRB_COMPLETED) {
3923 dcb->active_srb = NULL;
3924 srb->state = SRB_FREE;
3925 /*dprintkl(KERN_DEBUG, "done (pid=%li)\n", srb->cmd->pid); */
3926 srb_done(acb, dcb, srb);
3934 ********************************************************************
3937 ********************************************************************
3939 static void reselect(struct AdapterCtlBlk *acb)
3941 struct DeviceCtlBlk *dcb;
3942 struct ScsiReqBlk *srb = NULL;
3943 u16 rsel_tar_lun_id;
3947 dprintkdbg(DBG_0, "reselect..............\n");
3949 clear_fifo(acb, "Resel");
3950 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3951 /* Read Reselected Target ID and LUN */
3952 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3953 dcb = acb->active_dcb;
3954 if (dcb) { /* Arbitration lost but Reselection win */
3955 srb = dcb->active_srb;
3957 dprintkl(KERN_DEBUG, "Arb lost Resel won, but active_srb == NULL!\n");
3958 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3962 if (!(acb->scan_devices)) {
3964 "Arb lost but Resel win pid %li (%02i-%i) Rsel %04x Stat %04x\n",
3965 srb->cmd->pid, dcb->target_id,
3966 dcb->target_lun, rsel_tar_lun_id,
3967 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3968 TRACEPRINTF("ArbLResel!*");
3969 /*TRACEOUT (" %s\n", srb->debugtrace); */
3971 /*srb->state |= SRB_DISCONNECT; */
3973 srb->state = SRB_READY;
3975 move_srb_going_to_waiting(dcb, srb);
3976 waiting_set_timer(acb, HZ / 20);
3981 /* Read Reselected Target Id and LUN */
3982 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3983 dprintkl(KERN_DEBUG, "Resel expects identify msg! Got %04x!\n",
3985 id = rsel_tar_lun_id & 0xff;
3986 lun = (rsel_tar_lun_id >> 8) & 7;
3987 dcb = find_dcb(acb, id, lun);
3989 dprintkl(KERN_ERR, "Reselect from non existing device (%02i-%i)\n",
3991 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3995 acb->active_dcb = dcb;
3997 if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3998 dprintkl(KERN_DEBUG, "Reselection in spite of forbidden disconnection? (%02i-%i)\n",
3999 dcb->target_id, dcb->target_lun);
4001 if ((dcb->sync_mode & EN_TAG_QUEUEING) /*&& !arblostflag */ ) {
4002 struct ScsiReqBlk *oldSRB = srb;
4004 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
4006 srb->debugtrace[0] = 0;
4008 dcb->active_srb = srb;
4010 TRACEPRINTF("ArbLResel(%li):*", oldSRB->cmd->pid);
4011 /*if (arblostflag) dprintkl(KERN_DEBUG, "Reselect: Wait for Tag ... \n"); */
4013 /* There can be only one! */
4014 srb = dcb->active_srb;
4016 TRACEPRINTF("RSel *");
4017 if (!srb || !(srb->state & SRB_DISCONNECT)) {
4021 dprintkl(KERN_DEBUG,
4022 "Reselected w/o disconnected cmds from %02i-%i?\n",
4023 dcb->target_id, dcb->target_lun);
4025 srb->state = SRB_UNEXPECT_RESEL;
4026 dcb->active_srb = srb;
4027 enable_msgout_abort(acb, srb);
4029 if (dcb->flag & ABORT_DEV_) {
4030 /*srb->state = SRB_ABORT_SENT; */
4031 enable_msgout_abort(acb, srb);
4033 srb->state = SRB_DATA_XFER;
4036 /*if (arblostflag) TRACEOUT (" %s\n", srb->debugtrace); */
4038 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
4040 ***********************************************
4041 ** Program HA ID, target ID, period and offset
4042 ***********************************************
4044 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
4045 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
4046 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
4047 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
4048 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
4050 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
4054 /* Dynamic device handling */
4056 /* Remove dev (and DCB) */
4058 void remove_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
4060 struct DeviceCtlBlk *pPrevDCB = acb->link_dcb;
4062 dprintkdbg(DBG_0, "remove_dev\n");
4063 if (dcb->going_srb_count > 1) {
4064 dprintkdbg(DBG_DCB, "Driver won't free DCB (ID %i, LUN %i): 0x%08x because of SRBCnt %i\n",
4065 dcb->target_id, dcb->target_lun, (int) dcb,
4066 dcb->going_srb_count);
4069 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
4070 acb->children[dcb->target_id][dcb->target_lun] = NULL;
4073 if (dcb == acb->link_dcb) {
4075 if (acb->last_dcb == dcb) {
4077 acb->last_dcb = NULL;
4079 acb->link_dcb = dcb->next;
4081 while (pPrevDCB->next != dcb)
4082 pPrevDCB = pPrevDCB->next;
4083 pPrevDCB->next = dcb->next;
4084 if (dcb == acb->last_dcb)
4085 acb->last_dcb = pPrevDCB;
4088 dprintkdbg(DBG_DCB, "Driver about to free DCB (ID %i, LUN %i): %p\n",
4089 dcb->target_id, dcb->target_lun, dcb);
4090 if (dcb == acb->active_dcb)
4091 acb->active_dcb = NULL;
4092 if (dcb == acb->link_dcb)
4093 acb->link_dcb = dcb->next;
4094 if (dcb == acb->dcb_run_robin)
4095 acb->dcb_run_robin = dcb->next;
4101 static inline u8 tagq_blacklist(char *name)
4103 #ifndef DC395x_NO_TAGQ
4106 for (i = 0; i < BADDEVCNT; i++)
4107 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
4118 void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
4120 /* Check for SCSI format (ANSI and Response data format) */
4121 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
4122 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
4123 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
4124 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
4125 /* ((dcb->dev_type == TYPE_DISK)
4126 || (dcb->dev_type == TYPE_MOD)) && */
4127 !tagq_blacklist(((char *) ptr) + 8)) {
4128 if (dcb->max_command == 1)
4130 dcb->acb->tag_max_num;
4131 dcb->sync_mode |= EN_TAG_QUEUEING;
4132 /*dcb->tag_mask = 0; */
4134 dcb->max_command = 1;
4140 void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
4141 struct ScsiInqData *ptr)
4143 u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
4144 dcb->dev_type = bval1;
4145 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
4146 disc_tagq_set(dcb, ptr);
4151 ********************************************************************
4152 * unmap mapped pci regions from SRB
4153 ********************************************************************
4156 void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
4159 Scsi_Cmnd *cmd = srb->cmd;
4160 dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
4161 if (cmd->use_sg && dir != PCI_DMA_NONE) {
4162 /* unmap DC395x SG list */
4163 dprintkdbg(DBG_SGPARANOIA,
4164 "Unmap SG descriptor list %08x (%05x)\n",
4166 sizeof(struct SGentry) * DC395x_MAX_SG_LISTENTRY);
4167 pci_unmap_single(acb->dev, srb->sg_bus_addr,
4168 sizeof(struct SGentry) *
4169 DC395x_MAX_SG_LISTENTRY,
4171 dprintkdbg(DBG_SGPARANOIA, "Unmap %i SG segments from %p\n",
4172 cmd->use_sg, cmd->request_buffer);
4173 /* unmap the sg segments */
4174 pci_unmap_sg(acb->dev,
4175 (struct scatterlist *) cmd->request_buffer,
4177 } else if (cmd->request_buffer && dir != PCI_DMA_NONE) {
4178 dprintkdbg(DBG_SGPARANOIA, "Unmap buffer at %08x (%05x)\n",
4179 srb->segment_x[0].address, cmd->request_bufflen);
4180 pci_unmap_single(acb->dev, srb->segment_x[0].address,
4181 cmd->request_bufflen, dir);
4187 ********************************************************************
4188 * unmap mapped pci sense buffer from SRB
4189 ********************************************************************
4192 void pci_unmap_srb_sense(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
4194 if (!(srb->flag & AUTO_REQSENSE))
4196 /* Unmap sense buffer */
4197 dprintkdbg(DBG_SGPARANOIA, "Unmap sense buffer from %08x\n",
4198 srb->segment_x[0].address);
4199 pci_unmap_single(acb->dev, srb->segment_x[0].address,
4200 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
4201 /* Restore SG stuff */
4202 /*printk ("Auto_ReqSense finished: Restore Counters ...\n"); */
4203 srb->total_xfer_length = srb->xferred;
4204 srb->segment_x[0].address =
4205 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
4206 srb->segment_x[0].length =
4207 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
4212 ********************************************************************
4215 * Complete execution of a SCSI command
4216 * Signal completion to the generic SCSI driver
4217 ********************************************************************
4220 void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
4221 struct ScsiReqBlk *srb)
4225 struct ScsiInqData *ptr;
4226 /*u32 drv_flags=0; */
4230 TRACEPRINTF("DONE *");
4232 dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
4233 ptr = (struct ScsiInqData *) (cmd->request_buffer);
4236 (struct ScsiInqData *) CPU_ADDR(*(struct scatterlist *)
4238 dprintkdbg(DBG_SGPARANOIA,
4239 "SRBdone SG=%i (%i/%i), req_buf = %p, adr = %p\n",
4240 cmd->use_sg, srb->sg_index, srb->sg_count,
4241 cmd->request_buffer, ptr);
4243 "SRBdone (pid %li, target %02i-%i): ", srb->cmd->pid,
4244 srb->cmd->device->id, srb->cmd->device->lun);
4245 status = srb->target_status;
4246 if (srb->flag & AUTO_REQSENSE) {
4247 dprintkdbg(DBG_0, "AUTO_REQSENSE1..............\n");
4248 pci_unmap_srb_sense(acb, srb);
4250 ** target status..........................
4252 srb->flag &= ~AUTO_REQSENSE;
4253 srb->adapter_status = 0;
4254 srb->target_status = CHECK_CONDITION << 1;
4255 if (debug_enabled(DBG_KG)) {
4256 switch (cmd->sense_buffer[2] & 0x0f) {
4258 dprintkl(KERN_DEBUG,
4259 "ReqSense: NOT_READY (Cmnd = 0x%02x, Dev = %i-%i, Stat = %i, Scan = %i) ",
4260 cmd->cmnd[0], dcb->target_id,
4261 dcb->target_lun, status, acb->scan_devices);
4263 case UNIT_ATTENTION:
4264 dprintkl(KERN_DEBUG,
4265 "ReqSense: UNIT_ATTENTION (Cmnd = 0x%02x, Dev = %i-%i, Stat = %i, Scan = %i) ",
4266 cmd->cmnd[0], dcb->target_id,
4267 dcb->target_lun, status, acb->scan_devices);
4269 case ILLEGAL_REQUEST:
4270 dprintkl(KERN_DEBUG,
4271 "ReqSense: ILLEGAL_REQUEST (Cmnd = 0x%02x, Dev = %i-%i, Stat = %i, Scan = %i) ",
4272 cmd->cmnd[0], dcb->target_id,
4273 dcb->target_lun, status, acb->scan_devices);
4276 dprintkl(KERN_DEBUG,
4277 "ReqSense: MEDIUM_ERROR (Cmnd = 0x%02x, Dev = %i-%i, Stat = %i, Scan = %i) ",
4278 cmd->cmnd[0], dcb->target_id,
4279 dcb->target_lun, status, acb->scan_devices);
4281 case HARDWARE_ERROR:
4282 dprintkl(KERN_DEBUG,
4283 "ReqSense: HARDWARE_ERROR (Cmnd = 0x%02x, Dev = %i-%i, Stat = %i, Scan = %i) ",
4284 cmd->cmnd[0], dcb->target_id,
4285 dcb->target_lun, status, acb->scan_devices);
4288 if (cmd->sense_buffer[7] >= 6)
4289 dprintkl(KERN_DEBUG,
4290 "Sense=%02x, ASC=%02x, ASCQ=%02x (%08x %08x) ",
4291 cmd->sense_buffer[2], cmd->sense_buffer[12],
4292 cmd->sense_buffer[13],
4293 *((unsigned int *) (cmd->sense_buffer + 3)),
4294 *((unsigned int *) (cmd->sense_buffer + 8)));
4296 dprintkl(KERN_DEBUG,
4297 "Sense=%02x, No ASC/ASCQ (%08x) ",
4298 cmd->sense_buffer[2],
4299 *((unsigned int *) (cmd->sense_buffer + 3)));
4302 if (status == (CHECK_CONDITION << 1)) {
4303 cmd->result = DID_BAD_TARGET << 16;
4306 dprintkdbg(DBG_0, "AUTO_REQSENSE2..............\n");
4308 if ((srb->total_xfer_length)
4309 && (srb->total_xfer_length >= cmd->underflow))
4311 MK_RES_LNX(DRIVER_SENSE, DID_OK,
4312 srb->end_message, CHECK_CONDITION);
4313 /*SET_RES_DID(cmd->result,DID_OK) */
4316 MK_RES_LNX(DRIVER_SENSE, DID_OK,
4317 srb->end_message, CHECK_CONDITION);
4322 /*************************************************************/
4325 * target status..........................
4327 if (status_byte(status) == CHECK_CONDITION) {
4328 request_sense(acb, dcb, srb);
4330 } else if (status_byte(status) == QUEUE_FULL) {
4331 tempcnt = (u8) dcb->going_srb_count;
4333 ("\nDC395x: QUEUE_FULL for dev %02i-%i with %i cmnds\n",
4334 dcb->target_id, dcb->target_lun, tempcnt);
4337 dcb->max_command = tempcnt;
4339 move_srb_going_to_waiting(dcb, srb);
4340 waiting_set_timer(acb, HZ / 20);
4341 srb->adapter_status = 0;
4342 srb->target_status = 0;
4344 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
4345 srb->adapter_status = H_SEL_TIMEOUT;
4346 srb->target_status = 0;
4347 cmd->result = DID_NO_CONNECT << 16;
4349 srb->adapter_status = 0;
4350 SET_RES_DID(cmd->result, DID_ERROR);
4351 SET_RES_MSG(cmd->result, srb->end_message);
4352 SET_RES_TARGET(cmd->result, status);
4357 ** process initiator status..........................
4359 status = srb->adapter_status;
4360 if (status & H_OVER_UNDER_RUN) {
4361 srb->target_status = 0;
4362 SET_RES_DID(cmd->result, DID_OK);
4363 SET_RES_MSG(cmd->result, srb->end_message);
4364 } else if (srb->status & PARITY_ERROR) {
4365 SET_RES_DID(cmd->result, DID_PARITY);
4366 SET_RES_MSG(cmd->result, srb->end_message);
4367 } else { /* No error */
4369 srb->adapter_status = 0;
4370 srb->target_status = 0;
4371 SET_RES_DID(cmd->result, DID_OK);
4375 if (dir != PCI_DMA_NONE) {
4377 pci_dma_sync_sg(acb->dev,
4378 (struct scatterlist *) cmd->
4379 request_buffer, cmd->use_sg, dir);
4380 else if (cmd->request_buffer)
4381 pci_dma_sync_single(acb->dev,
4382 srb->segment_x[0].address,
4383 cmd->request_bufflen, dir);
4386 if ((cmd->result & RES_DID) == 0 && cmd->cmnd[0] == INQUIRY
4387 && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8
4388 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
4389 dcb->inquiry7 = ptr->Flags;
4390 /* Check Error Conditions */
4393 /*if( srb->cmd->cmnd[0] == INQUIRY && */
4394 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
4395 if (cmd->cmnd[0] == INQUIRY && (cmd->result == (DID_OK << 16)
4396 || status_byte(cmd->
4400 if (!dcb->init_tcq_flag) {
4401 add_dev(acb, dcb, ptr);
4402 dcb->init_tcq_flag = 1;
4408 /* Here is the info for Doug Gilbert's sg3 ... */
4409 cmd->resid = srb->total_xfer_length;
4410 /* This may be interpreted by sb. or not ... */
4411 cmd->SCp.this_residual = srb->total_xfer_length;
4412 cmd->SCp.buffers_residual = 0;
4413 if (debug_enabled(DBG_KG)) {
4414 if (srb->total_xfer_length)
4415 dprintkdbg(DBG_KG, "pid %li: %02x (%02i-%i): Missed %i bytes\n",
4416 cmd->pid, cmd->cmnd[0], cmd->device->id,
4417 cmd->device->lun, srb->total_xfer_length);
4420 remove_srb_going(dcb, srb, 0);
4421 /* Add to free list */
4422 if (srb == acb->tmp_srb)
4423 dprintkl(KERN_ERR, "ERROR! Completed Cmnd with tmp_srb!\n");
4425 insert_srb_free(acb, srb);
4427 dprintkdbg(DBG_0, "SRBdone: done pid %li\n", cmd->pid);
4428 if (debug_enabled(DBG_KG)) {
4429 printk(" 0x%08x\n", cmd->result);
4431 TRACEPRINTF("%08x(%li)*", cmd->result, jiffies);
4432 pci_unmap_srb(acb, srb);
4433 /*DC395x_UNLOCK_ACB_NI; */
4434 cmd->scsi_done(cmd);
4435 /*DC395x_LOCK_ACB_NI; */
4436 TRACEOUTALL(KERN_INFO " %s\n", srb->debugtrace);
4438 waiting_process_next(acb);
4444 ********************************************************************
4447 * abort all cmds in our queues
4448 ********************************************************************
4451 void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
4452 Scsi_Cmnd * cmd, u8 force)
4454 struct DeviceCtlBlk *dcb;
4455 struct ScsiReqBlk *srb;
4456 struct ScsiReqBlk *srb_temp;
4460 dcb = acb->link_dcb;
4463 dprintkl(KERN_INFO, "doing_srb_done: pids ");
4465 /* As the ML may queue cmnds again, cache old values */
4466 struct ScsiReqBlk *waiting_srb = dcb->waiting_srb;
4467 /*struct ScsiReqBlk* wait_list = dcb->wait_list; */
4468 u16 waiting_srb_count = dcb->waiting_srb_count;
4470 cnt = dcb->going_srb_count;
4471 srb = dcb->going_srb;
4475 srb_temp = srb->next;
4477 dir = scsi_to_pci_dma_dir(p->sc_data_direction);
4478 result = MK_RES(0, did_flag, 0, 0);
4479 /*result = MK_RES(0,DID_RESET,0,0); */
4480 TRACEPRINTF("Reset(%li):%08x*", jiffies, result);
4482 #if 1 /*ndef DC395x_DEBUGTRACE */
4483 printk("%li(%02i-%i) ", p->pid,
4484 p->device->id, p->device->lun);
4486 TRACEOUT("%s\n", srb->debugtrace);
4487 dcb->going_srb = srb_temp;
4488 dcb->going_srb_count--;
4490 dcb->going_last = NULL;
4492 insert_srb_free(acb, srb);
4494 pci_unmap_srb_sense(acb, srb);
4495 pci_unmap_srb(acb, srb);
4497 /* For new EH, we normally don't need to give commands back,
4498 * as they all complete or all time out */
4504 dprintkl(KERN_DEBUG,
4505 "How could the ML send cmnds to the Going queue? (%02i-%i)!!\n",
4506 dcb->target_id, dcb->target_lun);
4508 dprintkl(KERN_DEBUG,
4509 "tag_mask for %02i-%i should be empty, is %08x!\n",
4510 dcb->target_id, dcb->target_lun,
4512 /*dcb->going_srb_count = 0;; */
4513 /*dcb->going_srb = NULL; dcb->going_last = NULL; */
4516 cnt = waiting_srb_count;
4520 srb_temp = srb->next;
4522 result = MK_RES(0, did_flag, 0, 0);
4523 TRACEPRINTF("Reset(%li):%08x*", jiffies, result);
4525 #if 1 /*ndef DC395x_DEBUGTRACE */
4526 printk("%li(%i-%i)", p->pid, p->device->id,
4529 TRACEOUT("%s\n", srb->debugtrace);
4530 dcb->waiting_srb = srb_temp;
4531 dcb->waiting_srb_count--;
4533 dcb->wait_list = NULL;
4534 insert_srb_free(acb, srb);
4537 pci_unmap_srb_sense(acb, srb);
4538 pci_unmap_srb(acb, srb);
4540 /* For new EH, we normally don't need to give commands back,
4541 * as they all complete or all time out */
4542 cmd->scsi_done(cmd);
4546 if (dcb->waiting_srb_count)
4548 ("\nDC395x: Debug: ML queued %i cmnds again to %02i-%i\n",
4549 dcb->waiting_srb_count, dcb->target_id,
4551 /* The ML could have queued the cmnds again! */
4552 /*dcb->waiting_srb_count = 0;; */
4553 /*dcb->waiting_srb = NULL; dcb->wait_list = NULL; */
4554 dcb->flag &= ~ABORT_DEV_;
4557 while (dcb != acb->link_dcb && dcb);
4563 ********************************************************************
4565 * DC395x_shutdown DC395x_reset
4566 ********************************************************************
4568 static void reset_scsi_bus(struct AdapterCtlBlk *acb)
4570 /*u32 drv_flags=0; */
4572 dprintkdbg(DBG_0, "reset_scsi_bus..............\n");
4574 /*DC395x_DRV_LOCK(drv_flags); */
4575 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4577 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4578 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET));
4580 /*DC395x_DRV_UNLOCK(drv_flags); */
4585 /* Set basic config */
4586 static void set_basic_config(struct AdapterCtlBlk *acb)
4590 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
4591 if (acb->config & HCC_PARITY)
4592 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
4594 bval = PHASELATCH | INITIATOR | BLOCKRST;
4596 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
4598 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
4599 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
4600 /* program Host ID */
4601 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
4602 /* set ansynchronous transfer */
4603 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
4604 /* Turn LED control off */
4605 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
4606 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
4608 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
4610 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
4611 /*dprintkl(KERN_INFO, "DMA_Config: %04x\n", wval); */
4612 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
4613 /* Clear pending interrupt status */
4614 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4615 /* Enable SCSI interrupt */
4616 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
4617 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
4618 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
4624 ********************************************************************
4627 ********************************************************************
4629 static void scsi_reset_detect(struct AdapterCtlBlk *acb)
4631 dprintkl(KERN_INFO, "scsi_reset_detect\n");
4632 /* delay half a second */
4633 if (timer_pending(&acb->waiting_timer))
4634 del_timer(&acb->waiting_timer);
4636 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4637 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4638 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
4640 /* Maybe we locked up the bus? Then lets wait even longer ... */
4641 acb->scsi_host->last_reset =
4642 jiffies + 5 * HZ / 2 +
4643 HZ * eeprom_buf[acb->adapter_index].delay_time;
4645 clear_fifo(acb, "RstDet");
4646 set_basic_config(acb);
4648 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
4650 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
4651 acb->acb_flag |= RESET_DONE;
4653 acb->acb_flag |= RESET_DETECT;
4654 reset_dev_param(acb);
4655 doing_srb_done(acb, DID_RESET, 0, 1);
4656 /*DC395x_RecoverSRB( acb ); */
4657 acb->active_dcb = NULL;
4659 waiting_process_next(acb);
4667 ********************************************************************
4670 ********************************************************************
4673 void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
4674 struct ScsiReqBlk *srb)
4680 "request_sense for pid %li, target %02i-%i\n",
4681 cmd->pid, cmd->device->id, cmd->device->lun);
4682 TRACEPRINTF("RqSn*");
4683 srb->flag |= AUTO_REQSENSE;
4684 srb->adapter_status = 0;
4685 srb->target_status = 0;
4687 /* KG: Can this prevent crap sense data ? */
4688 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
4690 /* Save some data */
4691 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
4692 srb->segment_x[0].address;
4693 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
4694 srb->segment_x[0].length;
4695 srb->xferred = srb->total_xfer_length;
4696 /* srb->segment_x : a one entry of S/G list table */
4697 srb->total_xfer_length = sizeof(cmd->sense_buffer);
4698 srb->segment_x[0].length = sizeof(cmd->sense_buffer);
4699 /* Map sense buffer */
4700 srb->segment_x[0].address =
4701 pci_map_single(acb->dev, cmd->sense_buffer,
4702 sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE);
4703 dprintkdbg(DBG_SGPARANOIA, "Map sense buffer at %p (%05x) to %08x\n",
4704 cmd->sense_buffer, sizeof(cmd->sense_buffer),
4705 srb->segment_x[0].address);
4709 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
4710 dprintkl(KERN_DEBUG,
4711 "Request Sense failed for pid %li (%02i-%i)!\n",
4712 srb->cmd->pid, dcb->target_id, dcb->target_lun);
4714 move_srb_going_to_waiting(dcb, srb);
4715 waiting_set_timer(acb, HZ / 100);
4722 *********************************************************************
4723 * dc395x_queue_command
4725 * Function : void init_dcb
4726 * Purpose : initialize the internal structures for a given DCB
4727 * Inputs : cmd - pointer to this scsi cmd request block structure
4728 *********************************************************************
4731 void init_dcb(struct AdapterCtlBlk *acb, struct DeviceCtlBlk **pdcb,
4734 struct NvRamType *eeprom;
4737 struct DeviceCtlBlk *dcb;
4738 struct DeviceCtlBlk *dcb2;
4740 dprintkdbg(DBG_0, "init_dcb..............\n");
4741 dcb = dc395x_kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
4742 /*dcb = find_dcb (acb, target, lun); */
4748 if (acb->dcb_count == 0) {
4749 acb->link_dcb = dcb;
4750 acb->dcb_run_robin = dcb;
4752 acb->last_dcb->next = dcb;
4756 dcb->next = acb->link_dcb;
4757 acb->last_dcb = dcb;
4761 dcb->target_id = target;
4762 dcb->target_lun = lun;
4764 dcb->waiting_srb = NULL;
4765 dcb->going_srb = NULL;
4766 dcb->going_srb_count = 0;
4767 dcb->waiting_srb_count = 0;
4768 dcb->active_srb = NULL;
4772 dcb->max_command = 1;
4774 index = acb->adapter_index;
4775 eeprom = &eeprom_buf[index];
4776 dcb->dev_mode = eeprom->target[target].cfg0;
4777 /*dcb->AdpMode = eeprom->channel_cfg; */
4781 dcb->sync_period = 0;
4782 dcb->sync_offset = 0;
4783 period_index = eeprom->target[target].period & 0x07;
4784 dcb->min_nego_period = clock_period[period_index];
4786 #ifndef DC395x_NO_WIDE
4787 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
4788 && (acb->config & HCC_WIDE_CARD))
4789 dcb->sync_mode |= WIDE_NEGO_ENABLE;
4791 #ifndef DC395x_NO_SYNC
4792 if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
4793 if (!(lun) || current_sync_offset)
4794 dcb->sync_mode |= SYNC_NEGO_ENABLE;
4797 #ifndef DC395x_NO_DISCONNECT
4799 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
4801 dcb->identify_msg = IDENTIFY(0, lun);
4804 if (dcb->target_lun != 0) {
4806 struct DeviceCtlBlk *prevDCB = acb->link_dcb;
4807 while (prevDCB->target_id != dcb->target_id)
4808 prevDCB = prevDCB->next;
4810 "Copy settings from %02i-%02i to %02i-%02i\n",
4811 prevDCB->target_id, prevDCB->target_lun,
4812 dcb->target_id, dcb->target_lun);
4813 dcb->sync_mode = prevDCB->sync_mode;
4814 dcb->sync_period = prevDCB->sync_period;
4815 dcb->min_nego_period = prevDCB->min_nego_period;
4816 dcb->sync_offset = prevDCB->sync_offset;
4817 dcb->inquiry7 = prevDCB->inquiry7;
4820 acb->dcb_map[target] |= (1 << lun);
4821 acb->children[target][lun] = dcb;
4825 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
4827 * Memory for trace buffers
4830 void free_tracebufs(struct AdapterCtlBlk *acb, int srb_idx)
4833 const unsigned bufs_per_page = PAGE_SIZE / DEBUGTRACEBUFSZ;
4834 for (i = 0; i < srb_idx; i += bufs_per_page) {
4835 /*dprintkl(KERN_DEBUG, "Free tracebuf %p (for %i)\n", */
4836 /* acb->srb_array[i].debugtrace, i); */
4837 dc395x_kfree(acb->srb_array[i].debugtrace);
4843 int alloc_tracebufs(struct AdapterCtlBlk *acb)
4845 const unsigned mem_needed =
4846 (DC395x_MAX_SRB_CNT + 1) * DEBUGTRACEBUFSZ;
4847 int pages = (mem_needed + (PAGE_SIZE - 1)) / PAGE_SIZE;
4848 const unsigned bufs_per_page = PAGE_SIZE / DEBUGTRACEBUFSZ;
4852 /*dprintkl(KERN_DEBUG, "Alloc %i pages for tracebufs\n", pages); */
4854 ptr = dc395x_kmalloc(PAGE_SIZE, GFP_KERNEL);
4856 free_tracebufs(acb, srb_idx);
4859 /*dprintkl(KERN_DEBUG, "Alloc %li bytes at %p for tracebuf %i\n", */
4860 /* PAGE_SIZE, ptr, srb_idx); */
4862 while (i < bufs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4863 acb->srb_array[srb_idx++].debugtrace =
4864 ptr + (i++ * DEBUGTRACEBUFSZ);
4866 if (i < bufs_per_page) {
4867 acb->srb.debugtrace = ptr + (i * DEBUGTRACEBUFSZ);
4868 acb->srb.debugtrace[0] = 0;
4870 dprintkl(KERN_DEBUG, "No space for tmsrb tracebuf reserved?!\n");
4876 /* Free SG tables */
4877 static void free_sg_tables(struct AdapterCtlBlk *acb, int srb_idx)
4880 const unsigned srbs_per_page =
4881 PAGE_SIZE / (DC395x_MAX_SG_LISTENTRY * sizeof(struct SGentry));
4882 for (i = 0; i < srb_idx; i += srbs_per_page) {
4883 /*dprintkl(KERN_DEBUG, "Free SG segs %p (for %i)\n", */
4884 /* acb->srb_array[i].segment_x, i); */
4885 dc395x_kfree(acb->srb_array[i].segment_x);
4891 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4892 * should never cross a page boundary */
4893 static int alloc_sg_tables(struct AdapterCtlBlk *acb)
4895 const unsigned mem_needed =
4896 (DC395x_MAX_SRB_CNT +
4897 1) * DC395x_MAX_SG_LISTENTRY * sizeof(struct SGentry);
4898 int pages = (mem_needed + (PAGE_SIZE - 1)) / PAGE_SIZE;
4899 const unsigned srbs_per_page =
4900 PAGE_SIZE / (DC395x_MAX_SG_LISTENTRY * sizeof(struct SGentry));
4903 struct SGentry *ptr;
4904 /*dprintkl(KERN_DEBUG, "Alloc %i pages for SG tables\n", pages); */
4906 ptr = (struct SGentry *) dc395x_kmalloc(PAGE_SIZE, GFP_KERNEL);
4908 free_sg_tables(acb, srb_idx);
4911 /*dprintkl(KERN_DEBUG, "Alloc %li bytes at %p for SG segments %i\n", */
4912 /* PAGE_SIZE, ptr, srb_idx); */
4914 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4915 acb->srb_array[srb_idx++].segment_x =
4916 ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4918 if (i < srbs_per_page)
4919 acb->srb.segment_x =
4920 ptr + (i * DC395x_MAX_SG_LISTENTRY);
4922 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
4928 ********************************************************************
4931 ********************************************************************
4933 static void __init link_srb(struct AdapterCtlBlk *acb)
4937 for (i = 0; i < acb->srb_count - 1; i++)
4938 acb->srb_array[i].next = &acb->srb_array[i + 1];
4939 acb->srb_array[i].next = NULL;
4940 /*DC395x_Free_integrity (acb); */
4945 ***********************************************************************
4948 * Function : static void init_acb
4949 * Purpose : initialize the internal structures for a given SCSI host
4950 * Inputs : host - pointer to this host adapter's structure
4951 ***********************************************************************
4954 int __init init_acb(struct Scsi_Host *host, u32 io_port, u8 irq, u16 index)
4956 struct NvRamType *eeprom;
4957 struct AdapterCtlBlk *acb;
4960 eeprom = &eeprom_buf[index];
4961 host->max_cmd_len = 24;
4962 host->can_queue = DC395x_MAX_CMD_QUEUE;
4963 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4964 host->this_id = (int) eeprom->scsi_id;
4965 host->io_port = io_port;
4966 host->n_io_port = 0x80;
4967 host->dma_channel = -1;
4968 host->unique_id = io_port;
4970 host->last_reset = jiffies;
4972 acb = (struct AdapterCtlBlk *) host->hostdata;
4975 if (host->max_id - 1 == eeprom->scsi_id)
4977 #ifdef CONFIG_SCSI_MULTI_LUN
4978 if (eeprom->channel_cfg & NAC_SCANLUN)
4986 ********************************
4988 acb->scsi_host = host;
4989 acb->IOPortBase = (u16) io_port;
4990 acb->link_dcb = NULL;
4991 acb->dcb_run_robin = NULL;
4992 acb->active_dcb = NULL;
4993 acb->srb_count = DC395x_MAX_SRB_CNT;
4994 acb->adapter_index = index;
4995 acb->scsi_host->this_id = eeprom->scsi_id;
4996 acb->hostid_bit = (1 << acb->scsi_host->this_id);
4997 /*acb->scsi_host->this_lun = 0; */
4999 acb->irq_level = irq;
5000 acb->tag_max_num = 1 << eeprom->max_tag;
5001 if (acb->tag_max_num > 30)
5002 acb->tag_max_num = 30;
5003 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
5004 acb->scan_devices = 1;
5006 acb->gmode2 = eeprom->channel_cfg;
5007 if (eeprom->channel_cfg & NAC_SCANLUN)
5010 * link all device's SRB Q of this adapter
5012 if (alloc_sg_tables(acb)) {
5013 dprintkl(KERN_DEBUG, "SG table allocation failed!\n");
5016 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
5017 if (alloc_tracebufs(acb)) {
5018 dprintkl(KERN_DEBUG, "SG trace buffer allocation failed!\n");
5019 free_sg_tables(acb, DC395x_MAX_SRB_CNT);
5024 acb->free_srb = acb->srb_array;
5026 * temp SRB for Q tag used or abort command used
5028 acb->tmp_srb = &acb->srb;
5029 acb->srb.dcb = NULL;
5030 acb->srb.next = NULL;
5031 init_timer(&acb->waiting_timer);
5033 for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
5034 acb->dcb_map[i] = 0;
5035 dprintkdbg(DBG_0, "acb = %p, pdcb_map = %p, psrb_array = %p\n", acb,
5036 acb->dcb_map, acb->srb_array);
5037 dprintkdbg(DBG_0, "ACB size= %04x, DCB size= %04x, SRB size= %04x\n",
5038 sizeof(struct AdapterCtlBlk), sizeof(struct DeviceCtlBlk),
5039 sizeof(struct ScsiReqBlk));
5044 /*===========================================================================
5046 ===========================================================================*/
5048 * init_adapter - Initialize the SCSI chip control registers
5050 * @host: This hosts adapter strcuture
5051 * @io_port: The base I/O port
5053 * @index: Card instance number
5055 * Returns 0 if the initialization succeeds, any other value on failure.
5058 int __init init_adapter(struct Scsi_Host *host, u32 io_port,
5061 struct NvRamType *eeprom = &eeprom_buf[index];
5062 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
5064 if (!request_region(io_port, host->n_io_port, DC395X_NAME)) {
5065 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%x\n", io_port);
5068 if (request_irq(irq, dc395x_interrupt, SA_SHIRQ, DC395X_NAME, acb)) {
5069 /* release the region we just claimed */
5070 release_region(io_port, host->n_io_port);
5071 dprintkl(KERN_INFO, "Failed to register IRQ!\n");
5075 acb->IOPortBase = io_port;
5077 /* selection timeout = 250 ms */
5078 acb->sel_timeout = DC395x_SEL_TIMEOUT;
5080 /* Mask all the interrupt */
5081 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
5082 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
5084 /* Reset SCSI module */
5085 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
5087 /* Reset PCI/DMA module */
5088 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
5091 /* program configuration 0 */
5092 acb->config = HCC_AUTOTERM | HCC_PARITY;
5093 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
5094 acb->config |= HCC_WIDE_CARD;
5096 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
5097 acb->config |= HCC_SCSI_RESET;
5099 if (acb->config & HCC_SCSI_RESET) {
5100 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
5101 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
5103 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
5104 /*spin_unlock_irq (&io_request_lock); */
5107 acb->scsi_host->last_reset =
5110 eeprom_buf[acb->adapter_index].
5113 /*spin_lock_irq (&io_request_lock); */
5115 set_basic_config(acb);
5121 * trms1040_wait_30us: wait for 30 us
5123 * Waits for 30us (using the chip by the looks of it..)
5125 * @io_port: base I/O address
5128 void __init trms1040_wait_30us(u16 io_port)
5130 /* ScsiPortStallExecution(30); wait 30 us */
5131 outb(5, io_port + TRM_S1040_GEN_TIMER);
5132 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
5139 * trms1040_write_cmd - write the secified command and address to
5142 * @io_port: base I/O address
5143 * @cmd: SB + op code (command) to send
5144 * @addr: address to send
5147 void __init trms1040_write_cmd(u16 io_port, u8 cmd, u8 addr)
5152 /* program SB + OP code */
5153 for (i = 0; i < 3; i++, cmd <<= 1) {
5154 send_data = NVR_SELECT;
5155 if (cmd & 0x04) /* Start from bit 2 */
5156 send_data |= NVR_BITOUT;
5158 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
5159 trms1040_wait_30us(io_port);
5160 outb((send_data | NVR_CLOCK),
5161 io_port + TRM_S1040_GEN_NVRAM);
5162 trms1040_wait_30us(io_port);
5166 for (i = 0; i < 7; i++, addr <<= 1) {
5167 send_data = NVR_SELECT;
5168 if (addr & 0x40) /* Start from bit 6 */
5169 send_data |= NVR_BITOUT;
5171 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
5172 trms1040_wait_30us(io_port);
5173 outb((send_data | NVR_CLOCK),
5174 io_port + TRM_S1040_GEN_NVRAM);
5175 trms1040_wait_30us(io_port);
5177 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
5178 trms1040_wait_30us(io_port);
5183 * trms1040_set_data - store a single byte in the eeprom
5185 * Called from write all to write a single byte into the SSEEPROM
5186 * Which is done one bit at a time.
5188 * @io_port: base I/O address
5189 * @addr: offset into EEPROM
5190 * @byte: bytes to write
5193 void __init trms1040_set_data(u16 io_port, u8 addr, u8 byte)
5198 /* Send write command & address */
5199 trms1040_write_cmd(io_port, 0x05, addr);
5202 for (i = 0; i < 8; i++, byte <<= 1) {
5203 send_data = NVR_SELECT;
5204 if (byte & 0x80) /* Start from bit 7 */
5205 send_data |= NVR_BITOUT;
5207 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
5208 trms1040_wait_30us(io_port);
5209 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
5210 trms1040_wait_30us(io_port);
5212 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
5213 trms1040_wait_30us(io_port);
5215 /* Disable chip select */
5216 outb(0, io_port + TRM_S1040_GEN_NVRAM);
5217 trms1040_wait_30us(io_port);
5219 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
5220 trms1040_wait_30us(io_port);
5222 /* Wait for write ready */
5224 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
5225 trms1040_wait_30us(io_port);
5227 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
5228 trms1040_wait_30us(io_port);
5230 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
5234 /* Disable chip select */
5235 outb(0, io_port + TRM_S1040_GEN_NVRAM);
5240 * trms1040_write_all - write 128 bytes to the eeprom
5242 * Write the supplied 128 bytes to the chips SEEPROM
5244 * @eeprom: the data to write
5245 * @io_port: the base io port
5248 void __init trms1040_write_all(struct NvRamType *eeprom, u16 io_port)
5250 u8 *b_eeprom = (u8 *) eeprom;
5253 /* Enable SEEPROM */
5254 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
5255 io_port + TRM_S1040_GEN_CONTROL);
5258 trms1040_write_cmd(io_port, 0x04, 0xFF);
5259 outb(0, io_port + TRM_S1040_GEN_NVRAM);
5260 trms1040_wait_30us(io_port);
5263 for (addr = 0; addr < 128; addr++, b_eeprom++) {
5264 trms1040_set_data(io_port, addr, *b_eeprom);
5268 trms1040_write_cmd(io_port, 0x04, 0x00);
5269 outb(0, io_port + TRM_S1040_GEN_NVRAM);
5270 trms1040_wait_30us(io_port);
5272 /* Disable SEEPROM */
5273 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
5274 io_port + TRM_S1040_GEN_CONTROL);
5279 * trms1040_get_data - get a single byte from the eeprom
5281 * Called from read all to read a single byte into the SSEEPROM
5282 * Which is done one bit at a time.
5284 * @io_port: base I/O address
5285 * @addr: offset into SEEPROM
5287 * Returns the the byte read.
5290 u8 __init trms1040_get_data(u16 io_port, u8 addr)
5296 /* Send read command & address */
5297 trms1040_write_cmd(io_port, 0x06, addr);
5300 for (i = 0; i < 8; i++) {
5301 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
5302 trms1040_wait_30us(io_port);
5303 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
5305 /* Get data bit while falling edge */
5306 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
5308 if (read_byte & NVR_BITIN)
5311 trms1040_wait_30us(io_port);
5314 /* Disable chip select */
5315 outb(0, io_port + TRM_S1040_GEN_NVRAM);
5321 * trms1040_read_all - read all bytes from the eeprom
5323 * Read the 128 bytes from the SEEPROM.
5325 * @eeprom: where to store the data
5326 * @io_port: the base io port
5329 void __init trms1040_read_all(struct NvRamType *eeprom, u16 io_port)
5331 u8 *b_eeprom = (u8 *) eeprom;
5334 /* Enable SEEPROM */
5335 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
5336 io_port + TRM_S1040_GEN_CONTROL);
5339 for (addr = 0; addr < 128; addr++, b_eeprom++) {
5340 *b_eeprom = trms1040_get_data(io_port, addr);
5343 /* Disable SEEPROM */
5344 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
5345 io_port + TRM_S1040_GEN_CONTROL);
5351 * check_eeprom - get and check contents of the eeprom
5353 * Read seeprom 128 bytes into the memory provider in eeprom.
5354 * Checks the checksum and if it's not correct it uses a set of default
5357 * @eeprom: caller allocated strcuture to read the eeprom data into
5358 * @io_port: io port to read from
5361 void __init check_eeprom(struct NvRamType *eeprom, u16 io_port)
5363 u16 *w_eeprom = (u16 *) eeprom;
5369 trms1040_read_all(eeprom, io_port); /* read eeprom */
5372 for (w_addr = 0, w_eeprom = (u16 *) eeprom; w_addr < 64;
5373 w_addr++, w_eeprom++)
5375 if (cksum != 0x1234) {
5377 * Checksum is wrong.
5378 * Load a set of defaults into the eeprom buffer
5380 dprintkl(KERN_WARNING,
5381 "EEProm checksum error: using default values and options.\n");
5382 eeprom->sub_vendor_id[0] = (u8) PCI_VENDOR_ID_TEKRAM;
5383 eeprom->sub_vendor_id[1] = (u8) (PCI_VENDOR_ID_TEKRAM >> 8);
5384 eeprom->sub_sys_id[0] = (u8) PCI_DEVICE_ID_TEKRAM_TRMS1040;
5385 eeprom->sub_sys_id[1] =
5386 (u8) (PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
5387 eeprom->sub_class = 0x00;
5388 eeprom->vendor_id[0] = (u8) PCI_VENDOR_ID_TEKRAM;
5389 eeprom->vendor_id[1] = (u8) (PCI_VENDOR_ID_TEKRAM >> 8);
5390 eeprom->device_id[0] = (u8) PCI_DEVICE_ID_TEKRAM_TRMS1040;
5391 eeprom->device_id[1] =
5392 (u8) (PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
5393 eeprom->reserved = 0x00;
5395 for (d_addr = 0, d_eeprom = (u32 *) eeprom->target;
5396 d_addr < 16; d_addr++, d_eeprom++)
5397 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
5399 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
5400 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
5401 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
5404 /* Now load defaults (maybe set by boot/module params) */
5405 set_safe_settings();
5407 eeprom_override(eeprom);
5409 eeprom->cksum = 0x00;
5410 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *) eeprom;
5411 w_addr < 63; w_addr++, w_eeprom++)
5414 *w_eeprom = 0x1234 - cksum;
5415 trms1040_write_all(eeprom, io_port);
5416 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
5418 set_safe_settings();
5419 eeprom_index_to_delay(eeprom);
5420 eeprom_override(eeprom);
5426 * print_config - print adapter connection and termination
5429 * @acb: adapter control block
5432 void __init print_config(struct AdapterCtlBlk *acb)
5436 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
5437 dprintkl(KERN_INFO, "%c: Connectors: ",
5438 ((bval & WIDESCSI) ? 'W' : ' '));
5439 if (!(bval & CON5068))
5440 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
5441 if (!(bval & CON68))
5442 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
5443 if (!(bval & CON50))
5445 if ((bval & (CON5068 | CON50 | CON68)) ==
5446 0 /*(CON5068 | CON50 | CON68) */ )
5447 printk(" Oops! (All 3?) ");
5448 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
5449 printk(" Termination: ");
5450 if (bval & DIS_TERM)
5451 printk("Disabled\n");
5453 if (bval & AUTOTERM)
5455 if (bval & LOW8TERM)
5465 * print_eeprom_settings - output the eeprom settings
5466 * to the kernel log so people can see what they were.
5468 * @index: Adapter number
5471 void __init print_eeprom_settings(u16 index)
5473 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
5474 eeprom_buf[index].scsi_id,
5475 eeprom_buf[index].target[0].period,
5476 clock_speed[eeprom_buf[index].target[0].period] / 10,
5477 clock_speed[eeprom_buf[index].target[0].period] % 10,
5478 eeprom_buf[index].target[0].cfg0);
5479 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
5480 eeprom_buf[index].channel_cfg,
5481 eeprom_buf[index].max_tag,
5482 1 << eeprom_buf[index].max_tag,
5483 eeprom_buf[index].delay_time);
5488 *********************************************************************
5491 * Function : static int host_init (struct Scsi_Host *host)
5492 * Purpose : initialize the internal structures for a given SCSI host
5493 * Inputs : host - pointer to this host adapter's structure/
5494 * Preconditions : when this function is called, the chip_type
5495 * field of the acb structure MUST have been set.
5496 *********************************************************************
5499 struct Scsi_Host *__init host_init(Scsi_Host_Template * host_template,
5500 u32 io_port, u8 irq,
5503 struct Scsi_Host *host;
5504 struct AdapterCtlBlk *acb;
5507 * Read the eeprom contents info the buffer we supply. Use
5508 * defaults is eeprom checksum is wrong.
5510 check_eeprom(&eeprom_buf[index], (u16) io_port);
5513 *$$$$$$$$$$$ MEMORY ALLOCATE FOR ADAPTER CONTROL BLOCK $$$$$$$$$$$$
5515 host = scsi_host_alloc(host_template, sizeof(struct AdapterCtlBlk));
5517 dprintkl(KERN_INFO, "pSH scsi_host_alloc ERROR\n");
5520 print_eeprom_settings(index);
5522 acb = (struct AdapterCtlBlk *) host->hostdata;
5523 if (init_acb(host, io_port, irq, index)) {
5524 scsi_host_put(host);
5530 *$$$$$$$$$$$$$$$$$ INITIAL ADAPTER $$$$$$$$$$$$$$$$$
5532 if (!init_adapter(host, io_port, irq, index)) {
5533 if (!acb_list_head) {
5534 acb_list_head = acb;
5536 acb_list_tail->next_acb = acb;
5538 acb_list_tail = acb;
5539 acb->next_acb = NULL;
5541 dprintkl(KERN_INFO, "DC395x_initAdapter initial ERROR\n");
5542 scsi_host_put(host);
5554 ******************************************************************
5555 * Function: dc395x_proc_info(char* buffer, char **start,
5556 * off_t offset, int length, int hostno, int inout)
5557 * Purpose: return SCSI Adapter/Device Info
5559 * buffer: Pointer to a buffer where to write info
5562 * hostno: Host adapter index
5563 * inout : Read (=0) or set(!=0) info
5565 * buffer: contains info length
5567 * return value: length of info in buffer
5569 ******************************************************************
5572 /* KG: dc395x_proc_info taken from driver aha152x.c */
5575 #define SPRINTF(args...) pos += sprintf(pos, args)
5578 if (YN) SPRINTF(" Yes ");\
5579 else SPRINTF(" No ")
5582 int dc395x_proc_info(struct Scsi_Host *shpnt, char *buffer, char **start, off_t offset, int length,
5587 struct AdapterCtlBlk *acb;
5588 struct DeviceCtlBlk *dcb;
5589 unsigned long flags;
5591 acb = acb_list_head;
5594 if (acb->scsi_host == shpnt)
5596 acb = acb->next_acb;
5601 if (inout) /* Has data been written to the file ? */
5604 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
5605 SPRINTF(" Driver Version " DC395X_VERSION "\n");
5607 DC395x_LOCK_IO(acb->scsi_host, flags);
5609 SPRINTF("SCSI Host Nr %i, ", shpnt->host_no);
5610 SPRINTF("DC395U/UW/F DC315/U %s Adapter Nr %i\n",
5611 (acb->config & HCC_WIDE_CARD) ? "Wide" : "",
5612 acb->adapter_index);
5613 SPRINTF("IOPortBase 0x%04x, ", acb->IOPortBase);
5614 SPRINTF("irq_level 0x%02x, ", acb->irq_level);
5615 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
5617 SPRINTF("MaxID %i, MaxLUN %i, ", shpnt->max_id, shpnt->max_lun);
5618 SPRINTF("AdapterID %i\n", shpnt->this_id);
5620 SPRINTF("tag_max_num %i", acb->tag_max_num);
5621 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
5622 SPRINTF(", FilterCfg 0x%02x",
5623 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
5624 SPRINTF(", DelayReset %is\n",
5625 eeprom_buf[acb->adapter_index].delay_time);
5628 SPRINTF("Nr of DCBs: %i\n", acb->dcb_count);
5630 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
5631 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
5632 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
5633 acb->dcb_map[6], acb->dcb_map[7]);
5635 (" %02x %02x %02x %02x %02x %02x %02x %02x\n",
5636 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
5637 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
5638 acb->dcb_map[14], acb->dcb_map[15]);
5641 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
5643 dcb = acb->link_dcb;
5644 for (dev = 0; dev < acb->dcb_count; dev++) {
5646 SPRINTF("%02i %02i %02i ", dev, dcb->target_id,
5648 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
5649 YESNO(dcb->sync_offset);
5650 YESNO(dcb->sync_period & WIDE_SYNC);
5651 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
5652 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
5653 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
5655 clock_period[dcb->sync_period & 0x07] << 2;
5656 if (dcb->sync_offset)
5657 SPRINTF(" %03i ns ", nego_period);
5659 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
5661 if (dcb->sync_offset & 0x0f) {
5662 spd = 1000 / (nego_period);
5663 spd1 = 1000 % (nego_period);
5664 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
5665 SPRINTF(" %2i.%1i M %02i ", spd, spd1,
5666 (dcb->sync_offset & 0x0f));
5670 /* Add more info ... */
5671 SPRINTF(" %02i\n", dcb->max_command);
5675 if (timer_pending(&acb->waiting_timer))
5676 SPRINTF("Waiting queue timer running\n");
5679 dcb = acb->link_dcb;
5681 for (dev = 0; dev < acb->dcb_count; dev++) {
5682 struct ScsiReqBlk *srb;
5683 if (dcb->waiting_srb_count)
5684 SPRINTF("DCB (%02i-%i): Waiting: %i:",
5685 dcb->target_id, dcb->target_lun,
5686 dcb->waiting_srb_count);
5687 for (srb = dcb->waiting_srb; srb; srb = srb->next)
5688 SPRINTF(" %li", srb->cmd->pid);
5689 if (dcb->going_srb_count)
5690 SPRINTF("\nDCB (%02i-%i): Going : %i:",
5691 dcb->target_id, dcb->target_lun,
5692 dcb->going_srb_count);
5693 for (srb = dcb->going_srb; srb; srb = srb->next)
5694 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
5695 SPRINTF("\n %s", srb->debugtrace);
5697 SPRINTF(" %li", srb->cmd->pid);
5699 if (dcb->waiting_srb_count || dcb->going_srb_count)
5704 if (debug_enabled(DBG_DCB)) {
5705 SPRINTF("DCB list for ACB %p:\n", acb);
5706 dcb = acb->link_dcb;
5708 for (dev = 0; dev < acb->dcb_count; dev++, dcb = dcb->next)
5709 SPRINTF("->%p", dcb->next);
5713 *start = buffer + offset;
5714 DC395x_UNLOCK_IO(acb->scsi_host, flags);
5716 if (pos - buffer < offset)
5718 else if (pos - buffer - offset < length)
5719 return pos - buffer - offset;
5726 * chip_shutdown - cleanly shut down the scsi controller chip,
5727 * stopping all operations and disablig interrupt generation on the
5730 * @acb: The scsi adapter control block of the adapter to shut down.
5733 void chip_shutdown(struct AdapterCtlBlk *acb)
5735 /* disable interrupt */
5736 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
5737 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
5740 if (timer_pending(&acb->waiting_timer))
5741 del_timer(&acb->waiting_timer);
5742 if (timer_pending(&acb->selto_timer))
5743 del_timer(&acb->selto_timer);
5745 /* reset the scsi bus */
5746 if (acb->config & HCC_SCSI_RESET)
5747 reset_scsi_bus(acb);
5749 /* clear any pending interupt state */
5750 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
5752 /* release chip resources */
5753 #if debug_enabled(DBG_TRACE|DBG_TRACEALL)
5754 free_tracebufs(acb, DC395x_MAX_SRB_CNT);
5756 free_sg_tables(acb, DC395x_MAX_SRB_CNT);
5761 * free_dcbs - Free all of the DCBs.
5763 * @acb: Adapter to remove the DCBs for.
5766 void free_dcbs(struct AdapterCtlBlk* acb)
5768 struct DeviceCtlBlk *dcb;
5769 struct DeviceCtlBlk *dcb_next;
5771 dprintkdbg(DBG_DCB, "Free %i DCBs\n", acb->dcb_count);
5773 for (dcb = acb->link_dcb; dcb != NULL; dcb = dcb_next)
5775 dcb_next = dcb->next;
5776 dprintkdbg(DBG_DCB, "Free DCB (ID %i, LUN %i): %p\n",
5777 dcb->target_id, dcb->target_lun, dcb);
5779 * Free the DCB. This removes the entry from the
5780 * link_dcb list and decrements the count in dcb_count
5782 remove_dev(acb, dcb);
5787 * host_release - shutdown device and release resources that were
5788 * allocate for it. Called once for each card as it is shutdown.
5790 * @host: The adapter instance to shutdown.
5793 void host_release(struct Scsi_Host *host)
5795 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(host->hostdata);
5796 unsigned long flags;
5798 dprintkl(KERN_DEBUG, "DC395x release\n");
5800 DC395x_LOCK_IO(acb->scsi_host, flags);
5804 free_irq(host->irq, acb);
5805 release_region(host->io_port, host->n_io_port);
5807 DC395x_UNLOCK_IO(acb->scsi_host, flags);
5812 * SCSI host template
5814 static Scsi_Host_Template dc395x_driver_template = {
5815 .module = THIS_MODULE,
5816 .proc_name = DC395X_NAME,
5817 .proc_info = dc395x_proc_info,
5818 .name = DC395X_BANNER " " DC395X_VERSION,
5819 .queuecommand = dc395x_queue_command,
5820 .bios_param = dc395x_bios_param,
5821 .slave_alloc = dc395x_slave_alloc,
5822 .slave_destroy = dc395x_slave_destroy,
5823 .can_queue = DC395x_MAX_CAN_QUEUE,
5825 .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
5826 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
5827 .eh_abort_handler = dc395x_eh_abort,
5828 .eh_bus_reset_handler = dc395x_eh_bus_reset,
5829 .unchecked_isa_dma = 0,
5830 .use_clustering = DISABLE_CLUSTERING,
5835 * dc395x_init_one - Initialise a single instance of the adapter.
5837 * The PCI layer will call this once for each instance of the adapter
5838 * that it finds in the system. The pci_dev strcuture indicates which
5839 * instance we are being called from.
5841 * @dev: The PCI device to intialize.
5842 * @id: Looks like a pointer to the entry in our pci device table
5843 * that was actually matched by the PCI subsystem.
5845 * Returns 0 on success, or an error code (-ve) on failure.
5848 int __devinit dc395x_init_one(struct pci_dev *dev,
5849 const struct pci_device_id *id)
5851 unsigned int io_port;
5853 struct Scsi_Host *scsi_host;
5854 static int banner_done = 0;
5856 dprintkdbg(DBG_0, "Init one instance of the dc395x\n");
5859 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
5863 if (pci_enable_device(dev))
5865 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
5869 dprintkdbg(DBG_0, "Get resources...\n");
5870 io_port = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
5872 dprintkdbg(DBG_0, "IO_PORT=%04x,IRQ=%x\n", (unsigned int) io_port, irq);
5874 scsi_host = host_init(&dc395x_driver_template, io_port, irq, adapter_count);
5877 dprintkdbg(DBG_0, "host_init failed\n");
5881 pci_set_master(dev);
5883 /* store pci devices in out host data object. */
5884 ((struct AdapterCtlBlk *)(scsi_host->hostdata))->dev = dev;
5886 /* increment adaptor count */
5889 /* store ptr to scsi host in the PCI device structure */
5890 pci_set_drvdata(dev, scsi_host);
5892 /* get the scsi mid level to scan for new devices on the bus */
5893 scsi_add_host(scsi_host, &dev->dev); /* XXX handle failure */
5894 scsi_scan_host(scsi_host);
5901 * dc395x_remove_one - Called to remove a single instance of the
5904 * @dev: The PCI device to intialize.
5906 static void __devexit dc395x_remove_one(struct pci_dev *dev)
5908 struct Scsi_Host *host = pci_get_drvdata(dev);
5909 dprintkdbg(DBG_0, "Removing instance\n");
5910 scsi_remove_host(host);
5912 pci_set_drvdata(dev, NULL);
5917 * Table which identifies the PCI devices which
5918 * are handled by this device driver.
5920 static struct pci_device_id dc395x_pci_table[] = {
5922 .vendor = PCI_VENDOR_ID_TEKRAM,
5923 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
5924 .subvendor = PCI_ANY_ID,
5925 .subdevice = PCI_ANY_ID,
5927 {} /* Terminating entry */
5929 MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
5933 * PCI driver operations.
5934 * Tells the PCI sub system what can be done with the card.
5936 static struct pci_driver dc395x_driver = {
5937 .name = DC395X_NAME,
5938 .id_table = dc395x_pci_table,
5939 .probe = dc395x_init_one,
5940 .remove = __devexit_p(dc395x_remove_one),
5945 * dc395x_module_init - Module initialization function
5947 * Used by both module and built-in driver to initialise this driver.
5950 int __init dc395x_module_init(void)
5952 return pci_module_init(&dc395x_driver);
5957 * dc395x_module_exit - Module cleanup function.
5960 void __exit dc395x_module_exit(void)
5962 pci_unregister_driver(&dc395x_driver);
5966 module_init(dc395x_module_init);
5967 module_exit(dc395x_module_exit);
5969 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
5970 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
5971 MODULE_LICENSE("GPL");