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>
54 #include <linux/interrupt.h>
55 #include <linux/init.h>
56 #include <linux/spinlock.h>
57 #include <linux/pci.h>
58 #include <linux/list.h>
59 #include <linux/vmalloc.h>
62 #include <scsi/scsi.h>
63 #include <scsi/scsicam.h> /* needed for scsicam_bios_param */
64 #include <scsi/scsi_cmnd.h>
65 #include <scsi/scsi_device.h>
66 #include <scsi/scsi_host.h>
70 #define DC395X_NAME "dc395x"
71 #define DC395X_BANNER "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
72 #define DC395X_VERSION "v2.05, 2004/03/08"
74 /*---------------------------------------------------------------------------
76 ---------------------------------------------------------------------------*/
78 * Set to disable parts of the driver
80 /*#define DC395x_NO_DISCONNECT*/
81 /*#define DC395x_NO_TAGQ*/
82 /*#define DC395x_NO_SYNC*/
83 /*#define DC395x_NO_WIDE*/
85 /*---------------------------------------------------------------------------
87 ---------------------------------------------------------------------------*/
89 * Types of debugging that can be enabled and disabled
95 #define DBG_FIFO 0x0040
96 #define DBG_PIO 0x0080
100 * Set set of things to output debugging for.
101 * Undefine to remove all debugging
103 /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
104 /*#define DEBUG_MASK DBG_0*/
108 * Output a kernel mesage at the specified level and append the
109 * driver name and a ": " to the start of the message
111 #define dprintkl(level, format, arg...) \
112 printk(level DC395X_NAME ": " format , ## arg)
117 * print a debug message - this is formated with KERN_DEBUG, then the
118 * driver name followed by a ": " and then the message is output.
119 * This also checks that the specified debug level is enabled before
120 * outputing the message
122 #define dprintkdbg(type, format, arg...) \
124 if ((type) & (DEBUG_MASK)) \
125 dprintkl(KERN_DEBUG , format , ## arg); \
129 * Check if the specified type of debugging is enabled
131 #define debug_enabled(type) ((DEBUG_MASK) & (type))
135 * No debugging. Do nothing
137 #define dprintkdbg(type, format, arg...) \
139 #define debug_enabled(type) (0)
144 #ifndef PCI_VENDOR_ID_TEKRAM
145 #define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */
147 #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
148 #define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */
152 #define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
153 #define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
155 #define DC395x_read8(acb,address) (u8)(inb(acb->io_port_base + (address)))
156 #define DC395x_read16(acb,address) (u16)(inw(acb->io_port_base + (address)))
157 #define DC395x_read32(acb,address) (u32)(inl(acb->io_port_base + (address)))
158 #define DC395x_write8(acb,address,value) outb((value), acb->io_port_base + (address))
159 #define DC395x_write16(acb,address,value) outw((value), acb->io_port_base + (address))
160 #define DC395x_write32(acb,address,value) outl((value), acb->io_port_base + (address))
163 #define RES_TARGET 0x000000FF /* Target State */
164 #define RES_TARGET_LNX STATUS_MASK /* Only official ... */
165 #define RES_ENDMSG 0x0000FF00 /* End Message */
166 #define RES_DID 0x00FF0000 /* DID_ codes */
167 #define RES_DRV 0xFF000000 /* DRIVER_ codes */
169 #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
170 #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
172 #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
173 #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
174 #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
175 #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
176 #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
181 * srb->segement_x is the hw sg list. It is always allocated as a
182 * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
183 * cross a page boundy.
185 #define SEGMENTX_LEN (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
186 #define VIRTX_LEN (sizeof(void *) * DC395x_MAX_SG_LISTENTRY)
189 u32 address; /* bus! address */
193 /* The SEEPROM structure for TRM_S1040 */
195 u8 cfg0; /* Target configuration byte 0 */
196 u8 period; /* Target period */
197 u8 cfg2; /* Target configuration byte 2 */
198 u8 cfg3; /* Target configuration byte 3 */
202 u8 sub_vendor_id[2]; /* 0,1 Sub Vendor ID */
203 u8 sub_sys_id[2]; /* 2,3 Sub System ID */
204 u8 sub_class; /* 4 Sub Class */
205 u8 vendor_id[2]; /* 5,6 Vendor ID */
206 u8 device_id[2]; /* 7,8 Device ID */
207 u8 reserved; /* 9 Reserved */
208 struct NVRamTarget target[DC395x_MAX_SCSI_ID];
215 u8 scsi_id; /* 74 Host Adapter SCSI ID */
216 u8 channel_cfg; /* 75 Channel configuration */
217 u8 delay_time; /* 76 Power on delay time */
218 u8 max_tag; /* 77 Maximum tags */
219 u8 reserved0; /* 78 */
220 u8 boot_target; /* 79 */
221 u8 boot_lun; /* 80 */
222 u8 reserved1; /* 81 */
223 u16 reserved2[22]; /* 82,..125 */
224 u16 cksum; /* 126,127 */
228 struct list_head list; /* next/prev ptrs for srb lists */
229 struct DeviceCtlBlk *dcb;
230 struct scsi_cmnd *cmd;
232 struct SGentry *segment_x; /* Linear array of hw sg entries (up to 64 entries) */
233 u32 sg_bus_addr; /* Bus address of sg list (ie, of segment_x) */
235 u8 sg_count; /* No of HW sg entries for this request */
236 u8 sg_index; /* Index of HW sg entry for this request */
237 u32 total_xfer_length; /* Total number of bytes remaining to be transfered */
239 unsigned char *virt_addr; /* Virtual address of current transfer position */
242 * The sense buffer handling function, request_sense, uses
243 * the first hw sg entry (segment_x[0]) and the transfer
244 * length (total_xfer_length). While doing this it stores the
245 * original values into the last sg hw list
246 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
247 * total_xfer_length in xferred. These values are restored in
248 * pci_unmap_srb_sense. This is the only place xferred is used.
250 u32 xferred; /* Saved copy of total_xfer_length */
270 struct DeviceCtlBlk {
271 struct list_head list; /* next/prev ptrs for the dcb list */
272 struct AdapterCtlBlk *acb;
273 struct list_head srb_going_list; /* head of going srb list */
274 struct list_head srb_waiting_list; /* head of waiting srb list */
276 struct ScsiReqBlk *active_srb;
281 u8 target_id; /* SCSI Target ID (SCSI Only) */
282 u8 target_lun; /* SCSI Log. Unit (SCSI Only) */
286 u8 inquiry7; /* To store Inquiry flags */
287 u8 sync_mode; /* 0:async mode */
288 u8 min_nego_period; /* for nego. */
289 u8 sync_period; /* for reg. */
291 u8 sync_offset; /* for reg. and nego.(low nibble) */
297 struct AdapterCtlBlk {
298 struct Scsi_Host *scsi_host;
300 unsigned long io_port_base;
301 unsigned long io_port_len;
303 struct list_head dcb_list; /* head of going dcb list */
304 struct DeviceCtlBlk *dcb_run_robin;
305 struct DeviceCtlBlk *active_dcb;
307 struct list_head srb_free_list; /* head of free srb list */
308 struct ScsiReqBlk *tmp_srb;
309 struct timer_list waiting_timer;
310 struct timer_list selto_timer;
316 unsigned int irq_level;
326 u8 dcb_map[DC395x_MAX_SCSI_ID];
327 struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
333 struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
334 struct ScsiReqBlk srb;
336 struct NvRamType eeprom; /* eeprom settings for this adapter */
340 /*---------------------------------------------------------------------------
342 ---------------------------------------------------------------------------*/
343 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
345 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
347 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
349 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
351 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
353 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
355 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
357 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
359 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
361 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
363 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
365 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
367 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
369 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
371 static void set_basic_config(struct AdapterCtlBlk *acb);
372 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
373 struct ScsiReqBlk *srb);
374 static void reset_scsi_bus(struct AdapterCtlBlk *acb);
375 static void data_io_transfer(struct AdapterCtlBlk *acb,
376 struct ScsiReqBlk *srb, u16 io_dir);
377 static void disconnect(struct AdapterCtlBlk *acb);
378 static void reselect(struct AdapterCtlBlk *acb);
379 static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
380 struct ScsiReqBlk *srb);
381 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
382 struct ScsiReqBlk *srb);
383 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
384 struct ScsiReqBlk *srb);
385 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
386 struct scsi_cmnd *cmd, u8 force);
387 static void scsi_reset_detect(struct AdapterCtlBlk *acb);
388 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
389 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
390 struct ScsiReqBlk *srb);
391 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
392 struct ScsiReqBlk *srb);
393 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
394 struct ScsiReqBlk *srb);
395 static void set_xfer_rate(struct AdapterCtlBlk *acb,
396 struct DeviceCtlBlk *dcb);
397 static void waiting_timeout(unsigned long ptr);
400 /*---------------------------------------------------------------------------
402 ---------------------------------------------------------------------------*/
403 static u16 current_sync_offset = 0;
405 static void *dc395x_scsi_phase0[] = {
406 data_out_phase0,/* phase:0 */
407 data_in_phase0, /* phase:1 */
408 command_phase0, /* phase:2 */
409 status_phase0, /* phase:3 */
410 nop0, /* phase:4 PH_BUS_FREE .. initial phase */
411 nop0, /* phase:5 PH_BUS_FREE .. initial phase */
412 msgout_phase0, /* phase:6 */
413 msgin_phase0, /* phase:7 */
416 static void *dc395x_scsi_phase1[] = {
417 data_out_phase1,/* phase:0 */
418 data_in_phase1, /* phase:1 */
419 command_phase1, /* phase:2 */
420 status_phase1, /* phase:3 */
421 nop1, /* phase:4 PH_BUS_FREE .. initial phase */
422 nop1, /* phase:5 PH_BUS_FREE .. initial phase */
423 msgout_phase1, /* phase:6 */
424 msgin_phase1, /* phase:7 */
428 *Fast20: 000 50ns, 20.0 MHz
430 * 010 100ns, 10.0 MHz
437 *Fast40(LVDS): 000 25ns, 40.0 MHz
440 * 011 100ns, 10.0 MHz
446 /*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/
448 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
449 static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
450 static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
453 /*---------------------------------------------------------------------------
455 ---------------------------------------------------------------------------*/
457 * Module/boot parameters currently effect *all* instances of the
458 * card in the system.
462 * Command line parameters are stored in a structure below.
463 * These are the index's into the structure for the various
464 * command line options.
466 #define CFG_ADAPTER_ID 0
467 #define CFG_MAX_SPEED 1
468 #define CFG_DEV_MODE 2
469 #define CFG_ADAPTER_MODE 3
471 #define CFG_RESET_DELAY 5
473 #define CFG_NUM 6 /* number of configuration items */
477 * Value used to indicate that a command line override
478 * hasn't been used to modify the value.
480 #define CFG_PARAM_UNSET -1
484 * Hold command line parameters.
486 struct ParameterData {
487 int value; /* value of this setting */
488 int min; /* minimum value */
489 int max; /* maximum value */
490 int def; /* default value */
491 int safe; /* safe value */
493 static struct ParameterData __devinitdata cfg_data[] = {
512 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
513 NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
515 NTC_DO_PARITY_CHK | NTC_DO_SEND_START
521 #ifdef CONFIG_SCSI_MULTI_LUN
524 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
525 /*| NAC_ACTIVE_NEG*/,
526 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
532 3, /* 16 tags (??) */
546 * Safe settings. If set to zero the the BIOS/default values with
547 * command line overrides will be used. If set to 1 then safe and
548 * slow settings will be used.
550 static int use_safe_settings = 0;
551 module_param_named(safe, use_safe_settings, bool, 0);
552 MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
555 module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
556 MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
558 module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
559 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");
561 module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
562 MODULE_PARM_DESC(dev_mode, "Device mode.");
564 module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
565 MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
567 module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
568 MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
570 module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
571 MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
575 * set_safe_settings - if the use_safe_settings option is set then
576 * set all values to the safe and slow values.
578 static void __devinit set_safe_settings(void)
580 if (use_safe_settings)
584 dprintkl(KERN_INFO, "Using safe settings.\n");
585 for (i = 0; i < CFG_NUM; i++)
587 cfg_data[i].value = cfg_data[i].safe;
594 * fix_settings - reset any boot parameters which are out of range
595 * back to the default values.
597 static void __devinit fix_settings(void)
602 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
603 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
604 cfg_data[CFG_ADAPTER_ID].value,
605 cfg_data[CFG_MAX_SPEED].value,
606 cfg_data[CFG_DEV_MODE].value,
607 cfg_data[CFG_ADAPTER_MODE].value,
608 cfg_data[CFG_TAGS].value,
609 cfg_data[CFG_RESET_DELAY].value);
610 for (i = 0; i < CFG_NUM; i++)
612 if (cfg_data[i].value < cfg_data[i].min
613 || cfg_data[i].value > cfg_data[i].max)
614 cfg_data[i].value = cfg_data[i].def;
621 * Mapping from the eeprom delay index value (index into this array)
622 * to the the number of actual seconds that the delay should be for.
624 static char __devinitdata eeprom_index_to_delay_map[] =
625 { 1, 3, 5, 10, 16, 30, 60, 120 };
629 * eeprom_index_to_delay - Take the eeprom delay setting and convert it
630 * into a number of seconds.
632 * @eeprom: The eeprom structure in which we find the delay index to map.
634 static void __devinit eeprom_index_to_delay(struct NvRamType *eeprom)
636 eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
641 * delay_to_eeprom_index - Take a delay in seconds and return the
642 * closest eeprom index which will delay for at least that amount of
645 * @delay: The delay, in seconds, to find the eeprom index for.
647 static int __devinit delay_to_eeprom_index(int delay)
650 while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
657 * eeprom_override - Override the eeprom settings, in the provided
658 * eeprom structure, with values that have been set on the command
661 * @eeprom: The eeprom data to override with command line options.
663 static void __devinit eeprom_override(struct NvRamType *eeprom)
667 /* Adapter Settings */
668 if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
669 eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
671 if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
672 eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
674 if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
675 eeprom->delay_time = delay_to_eeprom_index(
676 cfg_data[CFG_RESET_DELAY].value);
678 if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
679 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
681 /* Device Settings */
682 for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
683 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
684 eeprom->target[id].cfg0 =
685 (u8)cfg_data[CFG_DEV_MODE].value;
687 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
688 eeprom->target[id].period =
689 (u8)cfg_data[CFG_MAX_SPEED].value;
695 /*---------------------------------------------------------------------------
696 ---------------------------------------------------------------------------*/
698 static unsigned int list_size(struct list_head *head)
700 unsigned int count = 0;
701 struct list_head *pos;
702 list_for_each(pos, head)
708 static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
709 struct DeviceCtlBlk *pos)
712 struct DeviceCtlBlk* next = NULL;
713 struct DeviceCtlBlk* i;
715 if (list_empty(head))
718 /* find supplied dcb and then select the next one */
719 list_for_each_entry(i, head, list)
723 } else if (i == pos) {
726 /* if no next one take the head one (ie, wraparound) */
728 list_for_each_entry(i, head, list) {
737 static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
739 if (srb->tag_number < 255) {
740 dcb->tag_mask &= ~(1 << srb->tag_number); /* free tag mask */
741 srb->tag_number = 255;
746 /* Find cmd in SRB list */
747 inline static struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
748 struct list_head *head)
750 struct ScsiReqBlk *i;
751 list_for_each_entry(i, head, list)
758 static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb)
760 struct list_head *head = &acb->srb_free_list;
761 struct ScsiReqBlk *srb = NULL;
763 if (!list_empty(head)) {
764 srb = list_entry(head->next, struct ScsiReqBlk, list);
765 list_del(head->next);
766 dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb);
772 static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
774 dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb);
775 list_add_tail(&srb->list, &acb->srb_free_list);
779 static void srb_waiting_insert(struct DeviceCtlBlk *dcb,
780 struct ScsiReqBlk *srb)
782 dprintkdbg(DBG_0, "srb_waiting_insert: (pid#%li) <%02i-%i> srb=%p\n",
783 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
784 list_add(&srb->list, &dcb->srb_waiting_list);
788 static void srb_waiting_append(struct DeviceCtlBlk *dcb,
789 struct ScsiReqBlk *srb)
791 dprintkdbg(DBG_0, "srb_waiting_append: (pid#%li) <%02i-%i> srb=%p\n",
792 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
793 list_add_tail(&srb->list, &dcb->srb_waiting_list);
797 static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
799 dprintkdbg(DBG_0, "srb_going_append: (pid#%li) <%02i-%i> srb=%p\n",
800 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
801 list_add_tail(&srb->list, &dcb->srb_going_list);
805 static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
807 struct ScsiReqBlk *i;
808 struct ScsiReqBlk *tmp;
809 dprintkdbg(DBG_0, "srb_going_remove: (pid#%li) <%02i-%i> srb=%p\n",
810 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
812 list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list)
814 list_del(&srb->list);
820 static void srb_waiting_remove(struct DeviceCtlBlk *dcb,
821 struct ScsiReqBlk *srb)
823 struct ScsiReqBlk *i;
824 struct ScsiReqBlk *tmp;
825 dprintkdbg(DBG_0, "srb_waiting_remove: (pid#%li) <%02i-%i> srb=%p\n",
826 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
828 list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list)
830 list_del(&srb->list);
836 static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb,
837 struct ScsiReqBlk *srb)
840 "srb_going_to_waiting_move: (pid#%li) <%02i-%i> srb=%p\n",
841 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
842 list_move(&srb->list, &dcb->srb_waiting_list);
846 static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb,
847 struct ScsiReqBlk *srb)
850 "srb_waiting_to_going_move: (pid#%li) <%02i-%i> srb=%p\n",
851 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
852 list_move(&srb->list, &dcb->srb_going_list);
856 /* Sets the timer to wake us up */
857 static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
859 if (timer_pending(&acb->waiting_timer))
861 init_timer(&acb->waiting_timer);
862 acb->waiting_timer.function = waiting_timeout;
863 acb->waiting_timer.data = (unsigned long) acb;
864 if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
865 acb->waiting_timer.expires =
866 acb->scsi_host->last_reset - HZ / 2 + 1;
868 acb->waiting_timer.expires = jiffies + to + 1;
869 add_timer(&acb->waiting_timer);
873 /* Send the next command from the waiting list to the bus */
874 static void waiting_process_next(struct AdapterCtlBlk *acb)
876 struct DeviceCtlBlk *start = NULL;
877 struct DeviceCtlBlk *pos;
878 struct DeviceCtlBlk *dcb;
879 struct ScsiReqBlk *srb;
880 struct list_head *dcb_list_head = &acb->dcb_list;
883 || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
886 if (timer_pending(&acb->waiting_timer))
887 del_timer(&acb->waiting_timer);
889 if (list_empty(dcb_list_head))
893 * Find the starting dcb. Need to find it again in the list
894 * since the list may have changed since we set the ptr to it
896 list_for_each_entry(dcb, dcb_list_head, list)
897 if (dcb == acb->dcb_run_robin) {
902 /* This can happen! */
903 start = list_entry(dcb_list_head->next, typeof(*start), list);
904 acb->dcb_run_robin = start;
909 * Loop over the dcb, but we start somewhere (potentially) in
910 * the middle of the loop so we need to manully do this.
914 struct list_head *waiting_list_head = &pos->srb_waiting_list;
916 /* Make sure, the next another device gets scheduled ... */
917 acb->dcb_run_robin = dcb_get_next(dcb_list_head,
920 if (list_empty(waiting_list_head) ||
921 pos->max_command <= list_size(&pos->srb_going_list)) {
922 /* move to next dcb */
923 pos = dcb_get_next(dcb_list_head, pos);
925 srb = list_entry(waiting_list_head->next,
926 struct ScsiReqBlk, list);
928 /* Try to send to the bus */
929 if (!start_scsi(acb, pos, srb))
930 srb_waiting_to_going_move(pos, srb);
932 waiting_set_timer(acb, HZ/50);
935 } while (pos != start);
939 /* Wake up waiting queue */
940 static void waiting_timeout(unsigned long ptr)
943 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
945 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
946 DC395x_LOCK_IO(acb->scsi_host, flags);
947 waiting_process_next(acb);
948 DC395x_UNLOCK_IO(acb->scsi_host, flags);
952 /* Get the DCB for a given ID/LUN combination */
953 static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
955 return acb->children[id][lun];
959 /* Send SCSI Request Block (srb) to adapter (acb) */
960 static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
962 struct DeviceCtlBlk *dcb = srb->dcb;
964 if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
966 (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
967 srb_waiting_append(dcb, srb);
968 waiting_process_next(acb);
972 if (!start_scsi(acb, dcb, srb))
973 srb_going_append(dcb, srb);
975 srb_waiting_insert(dcb, srb);
976 waiting_set_timer(acb, HZ / 50);
981 /* Prepare SRB for being sent to Device DCB w/ command *cmd */
982 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
983 struct ScsiReqBlk *srb)
985 enum dma_data_direction dir = cmd->sc_data_direction;
986 dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n",
987 cmd->pid, dcb->target_id, dcb->target_lun);
992 srb->total_xfer_length = 0;
993 srb->sg_bus_addr = 0;
994 srb->virt_addr = NULL;
996 srb->adapter_status = 0;
997 srb->target_status = 0;
1002 srb->retry_count = 0;
1003 srb->tag_number = TAG_NONE;
1004 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1005 srb->end_message = 0;
1007 if (dir == PCI_DMA_NONE || !cmd->request_buffer) {
1009 "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
1010 cmd->bufflen, cmd->request_buffer,
1011 cmd->use_sg, srb->segment_x[0].address);
1012 } else if (cmd->use_sg) {
1014 u32 reqlen = cmd->request_bufflen;
1015 struct scatterlist *sl = (struct scatterlist *)
1016 cmd->request_buffer;
1017 struct SGentry *sgp = srb->segment_x;
1018 srb->sg_count = pci_map_sg(dcb->acb->dev, sl, cmd->use_sg,
1021 "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
1022 reqlen, cmd->request_buffer, cmd->use_sg,
1025 for (i = 0; i < srb->sg_count; i++) {
1026 u32 seglen = (u32)sg_dma_len(sl + i);
1027 sgp[i].address = (u32)sg_dma_address(sl + i);
1028 sgp[i].length = seglen;
1029 srb->total_xfer_length += seglen;
1030 srb->virt_map[i] = kmap(sl[i].page);
1032 srb->virt_addr = srb->virt_map[0];
1033 sgp += srb->sg_count - 1;
1036 * adjust last page if too big as it is allocated
1037 * on even page boundaries
1039 if (srb->total_xfer_length > reqlen) {
1040 sgp->length -= (srb->total_xfer_length - reqlen);
1041 srb->total_xfer_length = reqlen;
1044 /* Fixup for WIDE padding - make sure length is even */
1045 if (dcb->sync_period & WIDE_SYNC &&
1046 srb->total_xfer_length % 2) {
1047 srb->total_xfer_length++;
1051 srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
1056 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
1057 srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
1059 srb->total_xfer_length = cmd->request_bufflen;
1061 srb->segment_x[0].address =
1062 pci_map_single(dcb->acb->dev, cmd->request_buffer,
1063 srb->total_xfer_length, dir);
1065 /* Fixup for WIDE padding - make sure length is even */
1066 if (dcb->sync_period & WIDE_SYNC && srb->total_xfer_length % 2)
1067 srb->total_xfer_length++;
1069 srb->segment_x[0].length = srb->total_xfer_length;
1070 srb->virt_addr = cmd->request_buffer;
1072 "build_srb: [1] len=%d buf=%p use_sg=%d map=%08x\n",
1073 srb->total_xfer_length, cmd->request_buffer,
1074 cmd->use_sg, srb->segment_x[0].address);
1080 * dc395x_queue_command - queue scsi command passed from the mid
1081 * layer, invoke 'done' on completion
1083 * @cmd: pointer to scsi command object
1084 * @done: function pointer to be invoked on completion
1086 * Returns 1 if the adapter (host) is busy, else returns 0. One
1087 * reason for an adapter to be busy is that the number
1088 * of outstanding queued commands is already equal to
1089 * struct Scsi_Host::can_queue .
1091 * Required: if struct Scsi_Host::can_queue is ever non-zero
1092 * then this function is required.
1094 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1095 * and is expected to be held on return.
1098 static int dc395x_queue_command(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1100 struct DeviceCtlBlk *dcb;
1101 struct ScsiReqBlk *srb;
1102 struct AdapterCtlBlk *acb =
1103 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1104 dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n",
1105 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
1107 /* Assume BAD_TARGET; will be cleared later */
1108 cmd->result = DID_BAD_TARGET << 16;
1110 /* ignore invalid targets */
1111 if (cmd->device->id >= acb->scsi_host->max_id ||
1112 cmd->device->lun >= acb->scsi_host->max_lun ||
1113 cmd->device->lun >31) {
1117 /* does the specified lun on the specified device exist */
1118 if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
1119 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
1120 cmd->device->id, cmd->device->lun);
1124 /* do we have a DCB for the device */
1125 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1127 /* should never happen */
1128 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
1129 cmd->device->id, cmd->device->lun);
1133 /* set callback and clear result in the command */
1134 cmd->scsi_done = done;
1137 srb = srb_get_free(acb);
1141 * Return 1 since we are unable to queue this command at this
1144 dprintkdbg(DBG_0, "queue_command: No free srb's\n");
1148 build_srb(cmd, dcb, srb);
1150 if (!list_empty(&dcb->srb_waiting_list)) {
1151 /* append to waiting queue */
1152 srb_waiting_append(dcb, srb);
1153 waiting_process_next(acb);
1155 /* process immediately */
1158 dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->pid);
1163 * Complete the command immediatey, and then return 0 to
1164 * indicate that we have handled the command. This is usually
1165 * done when the commad is for things like non existent
1174 * Return the disk geometry for the given SCSI device.
1176 static int dc395x_bios_param(struct scsi_device *sdev,
1177 struct block_device *bdev, sector_t capacity, int *info)
1179 #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
1180 int heads, sectors, cylinders;
1181 struct AdapterCtlBlk *acb;
1182 int size = capacity;
1184 dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
1185 acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
1188 cylinders = size / (heads * sectors);
1190 if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
1193 cylinders = size / (heads * sectors);
1197 geom[2] = cylinders;
1200 return scsicam_bios_param(bdev, capacity, info);
1205 static void dump_register_info(struct AdapterCtlBlk *acb,
1206 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
1209 struct pci_dev *dev = acb->dev;
1210 pci_read_config_word(dev, PCI_STATUS, &pstat);
1212 dcb = acb->active_dcb;
1214 srb = dcb->active_srb;
1217 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
1220 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) "
1221 "cmnd=0x%02x <%02i-%i>\n",
1222 srb, srb->cmd, srb->cmd->pid,
1223 srb->cmd->cmnd[0], srb->cmd->device->id,
1224 srb->cmd->device->lun);
1225 printk(" sglist=%p cnt=%i idx=%i len=%i\n",
1226 srb->segment_x, srb->sg_count, srb->sg_index,
1227 srb->total_xfer_length);
1228 printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1229 srb->state, srb->status, srb->scsi_phase,
1230 (acb->active_dcb) ? "" : "not");
1232 dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1233 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1234 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1235 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1236 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1237 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1238 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1239 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
1240 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1241 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1242 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1243 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
1244 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1245 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1246 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1247 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1248 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1249 dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1250 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1251 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1252 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1253 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1254 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1255 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1256 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1257 DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
1258 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1259 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1260 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1261 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1262 dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1263 "pci{status=0x%04x}\n",
1264 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1265 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1266 DC395x_read8(acb, TRM_S1040_GEN_TIMER),
1271 static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1273 #if debug_enabled(DBG_FIFO)
1274 u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1275 u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1276 if (!(fifocnt & 0x40))
1277 dprintkdbg(DBG_FIFO,
1278 "clear_fifo: (%i bytes) on phase %02x in %s\n",
1279 fifocnt & 0x3f, lines, txt);
1281 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1285 static void reset_dev_param(struct AdapterCtlBlk *acb)
1287 struct DeviceCtlBlk *dcb;
1288 struct NvRamType *eeprom = &acb->eeprom;
1289 dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
1291 list_for_each_entry(dcb, &acb->dcb_list, list) {
1294 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1295 dcb->sync_period = 0;
1296 dcb->sync_offset = 0;
1298 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1299 period_index = eeprom->target[dcb->target_id].period & 0x07;
1300 dcb->min_nego_period = clock_period[period_index];
1301 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1302 || !(acb->config & HCC_WIDE_CARD))
1303 dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1309 * perform a hard reset on the SCSI bus
1310 * @cmd - some command for this host (for fetching hooks)
1311 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1313 static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1315 struct AdapterCtlBlk *acb =
1316 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1318 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n",
1319 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1321 if (timer_pending(&acb->waiting_timer))
1322 del_timer(&acb->waiting_timer);
1327 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1328 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1329 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1330 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1332 reset_scsi_bus(acb);
1335 /* We may be in serious trouble. Wait some seconds */
1336 acb->scsi_host->last_reset =
1337 jiffies + 3 * HZ / 2 +
1338 HZ * acb->eeprom.delay_time;
1341 * re-enable interrupt
1343 /* Clear SCSI FIFO */
1344 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1345 clear_fifo(acb, "eh_bus_reset");
1346 /* Delete pending IRQ */
1347 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1348 set_basic_config(acb);
1350 reset_dev_param(acb);
1351 doing_srb_done(acb, DID_RESET, cmd, 0);
1352 acb->active_dcb = NULL;
1353 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1354 waiting_process_next(acb);
1359 static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1363 spin_lock_irq(cmd->device->host->host_lock);
1364 rc = __dc395x_eh_bus_reset(cmd);
1365 spin_unlock_irq(cmd->device->host->host_lock);
1371 * abort an errant SCSI command
1372 * @cmd - command to be aborted
1373 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1375 static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1378 * Look into our command queues: If it has not been sent already,
1379 * we remove it and return success. Otherwise fail.
1381 struct AdapterCtlBlk *acb =
1382 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1383 struct DeviceCtlBlk *dcb;
1384 struct ScsiReqBlk *srb;
1385 dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
1386 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1388 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1390 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1394 srb = find_cmd(cmd, &dcb->srb_waiting_list);
1396 srb_waiting_remove(dcb, srb);
1397 pci_unmap_srb_sense(acb, srb);
1398 pci_unmap_srb(acb, srb);
1400 srb_free_insert(acb, srb);
1401 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1402 cmd->result = DID_ABORT << 16;
1405 srb = find_cmd(cmd, &dcb->srb_going_list);
1407 dprintkl(KERN_DEBUG, "eh_abort: Command in progress");
1408 /* XXX: Should abort the command here */
1410 dprintkl(KERN_DEBUG, "eh_abort: Command not found");
1417 static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1418 struct ScsiReqBlk *srb)
1420 u8 *ptr = srb->msgout_buf + srb->msg_count;
1421 if (srb->msg_count > 1) {
1423 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1424 srb->msg_count, srb->msgout_buf[0],
1425 srb->msgout_buf[1]);
1428 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1429 dcb->sync_offset = 0;
1430 dcb->min_nego_period = 200 >> 2;
1431 } else if (dcb->sync_offset == 0)
1432 dcb->sync_offset = SYNC_NEGO_OFFSET;
1434 *ptr++ = MSG_EXTENDED; /* (01h) */
1435 *ptr++ = 3; /* length */
1436 *ptr++ = EXTENDED_SDTR; /* (01h) */
1437 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */
1438 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */
1439 srb->msg_count += 5;
1440 srb->state |= SRB_DO_SYNC_NEGO;
1445 static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1446 struct ScsiReqBlk *srb)
1448 u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1449 (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1450 u8 *ptr = srb->msgout_buf + srb->msg_count;
1451 if (srb->msg_count > 1) {
1453 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1454 srb->msg_count, srb->msgout_buf[0],
1455 srb->msgout_buf[1]);
1458 *ptr++ = MSG_EXTENDED; /* (01h) */
1459 *ptr++ = 2; /* length */
1460 *ptr++ = EXTENDED_WDTR; /* (03h) */
1462 srb->msg_count += 4;
1463 srb->state |= SRB_DO_WIDE_NEGO;
1468 /* Timer to work around chip flaw: When selecting and the bus is
1469 * busy, we sometimes miss a Selection timeout IRQ */
1470 void selection_timeout_missed(unsigned long ptr);
1471 /* Sets the timer to wake us up */
1472 static void selto_timer(struct AdapterCtlBlk *acb)
1474 if (timer_pending(&acb->selto_timer))
1476 acb->selto_timer.function = selection_timeout_missed;
1477 acb->selto_timer.data = (unsigned long) acb;
1479 (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
1480 acb->selto_timer.expires =
1481 acb->scsi_host->last_reset + HZ / 2 + 1;
1483 acb->selto_timer.expires = jiffies + HZ + 1;
1484 add_timer(&acb->selto_timer);
1488 void selection_timeout_missed(unsigned long ptr)
1490 unsigned long flags;
1491 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
1492 struct ScsiReqBlk *srb;
1493 dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1494 if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1495 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1498 DC395x_LOCK_IO(acb->scsi_host, flags);
1499 srb = acb->active_dcb->active_srb;
1501 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1506 static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1507 struct ScsiReqBlk* srb)
1509 u16 s_stat2, return_code;
1510 u8 s_stat, scsicommand, i, identify_message;
1512 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
1513 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
1515 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
1517 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1519 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1521 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1522 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
1523 srb->cmd->pid, s_stat, s_stat2);
1527 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1528 * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed!
1529 * (This is likely to be a bug in the hardware. Obviously, most people
1530 * only have one initiator per SCSI bus.)
1531 * Instead let this fail and have the timer make sure the command is
1532 * tried again after a short time
1534 /*selto_timer (acb); */
1538 if (acb->active_dcb) {
1539 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
1540 "command while another command (pid#%li) is active.",
1542 acb->active_dcb->active_srb ?
1543 acb->active_dcb->active_srb->cmd->pid : 0);
1546 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1547 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
1551 /* Allow starting of SCSI commands half a second before we allow the mid-level
1552 * to queue them again after a reset */
1553 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1554 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1559 clear_fifo(acb, "start_scsi");
1560 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1561 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1562 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1563 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1564 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1566 identify_message = dcb->identify_msg;
1567 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1568 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1569 if (srb->flag & AUTO_REQSENSE)
1570 identify_message &= 0xBF;
1572 if (((srb->cmd->cmnd[0] == INQUIRY)
1573 || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1574 || (srb->flag & AUTO_REQSENSE))
1575 && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1576 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1577 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1578 && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1579 && (dcb->target_lun == 0)) {
1580 srb->msgout_buf[0] = identify_message;
1582 scsicommand = SCMD_SEL_ATNSTOP;
1583 srb->state = SRB_MSGOUT;
1585 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1586 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1587 build_wdtr(acb, dcb, srb);
1591 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1592 && dcb->inquiry7 & SCSI_INQ_SYNC) {
1593 build_sdtr(acb, dcb, srb);
1596 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1597 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1598 build_wdtr(acb, dcb, srb);
1603 /* Send identify message */
1604 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1606 scsicommand = SCMD_SEL_ATN;
1607 srb->state = SRB_START_;
1608 #ifndef DC395x_NO_TAGQ
1609 if ((dcb->sync_mode & EN_TAG_QUEUEING)
1610 && (identify_message & 0xC0)) {
1611 /* Send Tag message */
1614 while (tag_mask & dcb->tag_mask
1615 && tag_number <= dcb->max_command) {
1616 tag_mask = tag_mask << 1;
1619 if (tag_number >= dcb->max_command) {
1620 dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
1621 "Out of tags target=<%02i-%i>)\n",
1622 srb->cmd->pid, srb->cmd->device->id,
1623 srb->cmd->device->lun);
1624 srb->state = SRB_READY;
1625 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1630 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1631 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1632 dcb->tag_mask |= tag_mask;
1633 srb->tag_number = tag_number;
1634 scsicommand = SCMD_SEL_ATN3;
1635 srb->state = SRB_START_;
1639 /* Send CDB ..command block ......... */
1640 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
1641 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
1642 srb->cmd->cmnd[0], srb->tag_number);
1643 if (srb->flag & AUTO_REQSENSE) {
1644 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1645 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1646 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1647 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1648 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1649 sizeof(srb->cmd->sense_buffer));
1650 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1652 ptr = (u8 *)srb->cmd->cmnd;
1653 for (i = 0; i < srb->cmd->cmd_len; i++)
1654 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1657 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1658 DO_HWRESELECT | DO_DATALATCH);
1659 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1661 * If start_scsi return 1:
1662 * we caught an interrupt (must be reset or reselection ... )
1663 * : Let's process it first!
1665 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
1666 srb->cmd->pid, dcb->target_id, dcb->target_lun);
1667 srb->state = SRB_READY;
1671 /* This IRQ should NOT get lost, as we did not acknowledge it */
1674 * If start_scsi returns 0:
1675 * we know that the SCSI processor is free
1677 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1678 dcb->active_srb = srb;
1679 acb->active_dcb = dcb;
1681 /* it's important for atn stop */
1682 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1683 DO_DATALATCH | DO_HWRESELECT);
1685 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1691 #define DC395x_ENABLE_MSGOUT \
1692 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1693 srb->state |= SRB_MSGOUT
1697 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1698 struct ScsiReqBlk *srb)
1700 srb->msgout_buf[0] = ABORT;
1702 DC395x_ENABLE_MSGOUT;
1703 srb->state &= ~SRB_MSGIN;
1704 srb->state |= SRB_MSGOUT;
1709 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1710 * have been triggered for this card.
1712 * @acb: a pointer to the adpter control block
1713 * @scsi_status: the status return when we checked the card
1715 static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1718 struct DeviceCtlBlk *dcb;
1719 struct ScsiReqBlk *srb;
1722 unsigned long flags;
1723 void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
1726 DC395x_LOCK_IO(acb->scsi_host, flags);
1728 /* This acknowledges the IRQ */
1729 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1730 if ((scsi_status & 0x2007) == 0x2002)
1731 dprintkl(KERN_DEBUG,
1732 "COP after COP completed? %04x\n", scsi_status);
1733 if (debug_enabled(DBG_KG)) {
1734 if (scsi_intstatus & INT_SELTIMEOUT)
1735 dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1737 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1739 if (timer_pending(&acb->selto_timer))
1740 del_timer(&acb->selto_timer);
1742 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1743 disconnect(acb); /* bus free interrupt */
1746 if (scsi_intstatus & INT_RESELECTED) {
1750 if (scsi_intstatus & INT_SELECT) {
1751 dprintkl(KERN_INFO, "Host does not support target mode!\n");
1754 if (scsi_intstatus & INT_SCSIRESET) {
1755 scsi_reset_detect(acb);
1758 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1759 dcb = acb->active_dcb;
1761 dprintkl(KERN_DEBUG,
1762 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1763 scsi_status, scsi_intstatus);
1766 srb = dcb->active_srb;
1767 if (dcb->flag & ABORT_DEV_) {
1768 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1769 enable_msgout_abort(acb, srb);
1772 /* software sequential machine */
1773 phase = (u16)srb->scsi_phase;
1777 * call dc395x_scsi_phase0[]... "phase entry"
1778 * handle every phase before start transfer
1780 /* data_out_phase0, phase:0 */
1781 /* data_in_phase0, phase:1 */
1782 /* command_phase0, phase:2 */
1783 /* status_phase0, phase:3 */
1784 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
1785 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
1786 /* msgout_phase0, phase:6 */
1787 /* msgin_phase0, phase:7 */
1788 dc395x_statev = dc395x_scsi_phase0[phase];
1789 dc395x_statev(acb, srb, &scsi_status);
1792 * if there were any exception occured scsi_status
1793 * will be modify to bus free phase new scsi_status
1794 * transfer out from ... previous dc395x_statev
1796 srb->scsi_phase = scsi_status & PHASEMASK;
1797 phase = (u16)scsi_status & PHASEMASK;
1800 * call dc395x_scsi_phase1[]... "phase entry" handle
1801 * every phase to do transfer
1803 /* data_out_phase1, phase:0 */
1804 /* data_in_phase1, phase:1 */
1805 /* command_phase1, phase:2 */
1806 /* status_phase1, phase:3 */
1807 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
1808 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
1809 /* msgout_phase1, phase:6 */
1810 /* msgin_phase1, phase:7 */
1811 dc395x_statev = dc395x_scsi_phase1[phase];
1812 dc395x_statev(acb, srb, &scsi_status);
1815 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1819 static irqreturn_t dc395x_interrupt(int irq, void *dev_id,
1820 struct pt_regs *regs)
1822 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)dev_id;
1825 irqreturn_t handled = IRQ_NONE;
1828 * Check for pending interupt
1830 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1831 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1832 if (scsi_status & SCSIINTERRUPT) {
1833 /* interupt pending - let's process it! */
1834 dc395x_handle_interrupt(acb, scsi_status);
1835 handled = IRQ_HANDLED;
1837 else if (dma_status & 0x20) {
1838 /* Error from the DMA engine */
1839 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1841 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
1842 if (acb->active_dcb) {
1843 acb->active_dcb-> flag |= ABORT_DEV_;
1844 if (acb->active_dcb->active_srb)
1845 enable_msgout_abort(acb, acb->active_dcb->active_srb);
1847 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
1849 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
1852 handled = IRQ_HANDLED;
1859 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1862 dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->pid);
1863 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
1864 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
1866 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1867 srb->state &= ~SRB_MSGOUT;
1871 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1876 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->pid);
1878 clear_fifo(acb, "msgout_phase1");
1879 if (!(srb->state & SRB_MSGOUT)) {
1880 srb->state |= SRB_MSGOUT;
1881 dprintkl(KERN_DEBUG,
1882 "msgout_phase1: (pid#%li) Phase unexpected\n",
1883 srb->cmd->pid); /* So what ? */
1885 if (!srb->msg_count) {
1886 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
1888 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
1889 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1890 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1893 ptr = (u8 *)srb->msgout_buf;
1894 for (i = 0; i < srb->msg_count; i++)
1895 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1897 if (srb->msgout_buf[0] == MSG_ABORT)
1898 srb->state = SRB_ABORT_SENT;
1900 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1904 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1907 dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->pid);
1908 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1912 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1915 struct DeviceCtlBlk *dcb;
1918 dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->pid);
1920 clear_fifo(acb, "command_phase1");
1921 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
1922 if (!(srb->flag & AUTO_REQSENSE)) {
1923 ptr = (u8 *)srb->cmd->cmnd;
1924 for (i = 0; i < srb->cmd->cmd_len; i++) {
1925 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
1929 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1930 dcb = acb->active_dcb;
1932 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1933 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1934 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1935 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1936 sizeof(srb->cmd->sense_buffer));
1937 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1939 srb->state |= SRB_COMMAND;
1940 /* it's important for atn stop */
1941 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1943 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1948 * Verify that the remaining space in the hw sg lists is the same as
1949 * the count of remaining bytes in srb->total_xfer_length
1951 static void sg_verify_length(struct ScsiReqBlk *srb)
1953 if (debug_enabled(DBG_SG)) {
1955 unsigned idx = srb->sg_index;
1956 struct SGentry *psge = srb->segment_x + idx;
1957 for (; idx < srb->sg_count; psge++, idx++)
1958 len += psge->length;
1959 if (len != srb->total_xfer_length)
1961 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1962 srb->total_xfer_length, len);
1968 * Compute the next Scatter Gather list index and adjust its length
1969 * and address if necessary; also compute virt_addr
1971 static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
1974 struct scatterlist *sg;
1975 struct scsi_cmnd *cmd = srb->cmd;
1976 int segment = cmd->use_sg;
1977 u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
1978 struct SGentry *psge = srb->segment_x + srb->sg_index;
1979 void **virt = srb->virt_map;
1982 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
1983 xferred, srb->total_xfer_length, left);
1985 /* nothing to update since we did not transfer any data */
1989 sg_verify_length(srb);
1990 srb->total_xfer_length = left; /* update remaining count */
1991 for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
1992 if (xferred >= psge->length) {
1993 /* Complete SG entries done */
1994 xferred -= psge->length;
1996 /* Partial SG entry done */
1997 psge->length -= xferred;
1998 psge->address += xferred;
1999 srb->sg_index = idx;
2000 pci_dma_sync_single_for_device(srb->dcb->
2009 sg_verify_length(srb);
2011 /* we need the corresponding virtual address */
2013 srb->virt_addr += xferred;
2017 /* We have to walk the scatterlist to find it */
2018 sg = (struct scatterlist *)cmd->request_buffer;
2021 unsigned long mask =
2022 ~((unsigned long)sg->length - 1) & PAGE_MASK;
2023 if ((sg_dma_address(sg) & mask) == (psge->address & mask)) {
2024 srb->virt_addr = virt[idx] + (psge->address & ~PAGE_MASK);
2031 dprintkl(KERN_ERR, "sg_update_list: sg_to_virt failed\n");
2032 srb->virt_addr = NULL;
2037 * We have transfered a single byte (PIO mode?) and need to update
2038 * the count of bytes remaining (total_xfer_length) and update the sg
2039 * entry to either point to next byte in the current sg entry, or of
2040 * already at the end to point to the start of the next sg entry
2042 static void sg_subtract_one(struct ScsiReqBlk *srb)
2044 srb->total_xfer_length--;
2045 srb->segment_x[srb->sg_index].length--;
2046 if (srb->total_xfer_length &&
2047 !srb->segment_x[srb->sg_index].length) {
2048 if (debug_enabled(DBG_PIO))
2049 printk(" (next segment)");
2051 sg_update_list(srb, srb->total_xfer_length);
2057 * cleanup_after_transfer
2059 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
2060 * KG: Currently called from StatusPhase1 ()
2061 * Should probably also be called from other places
2062 * Best might be to call it in DataXXPhase0, if new phase will differ
2064 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
2065 struct ScsiReqBlk *srb)
2067 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
2068 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
2069 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2070 clear_fifo(acb, "cleanup/in");
2071 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2072 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2073 } else { /* write */
2074 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2075 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2076 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2077 clear_fifo(acb, "cleanup/out");
2079 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2084 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
2085 * Seems to be needed for unknown reasons; could be a hardware bug :-(
2087 #define DC395x_LASTPIO 4
2090 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2093 struct DeviceCtlBlk *dcb = srb->dcb;
2094 u16 scsi_status = *pscsi_status;
2095 u32 d_left_counter = 0;
2096 dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
2097 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2100 * KG: We need to drain the buffers before we draw any conclusions!
2101 * This means telling the DMA to push the rest into SCSI, telling
2102 * SCSI to push the rest to the bus.
2103 * However, the device might have been the one to stop us (phase
2104 * change), and the data in transit just needs to be accounted so
2105 * it can be retransmitted.)
2108 * KG: Stop DMA engine pushing more data into the SCSI FIFO
2109 * If we need more data, the DMA SG list will be freshly set up, anyway
2111 dprintkdbg(DBG_PIO, "data_out_phase0: "
2112 "DMA{fifcnt=0x%02x fifostat=0x%02x} "
2113 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
2114 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2115 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2116 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2117 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2118 srb->total_xfer_length);
2119 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2121 if (!(srb->state & SRB_XFERPAD)) {
2122 if (scsi_status & PARITYERROR)
2123 srb->status |= PARITY_ERROR;
2126 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2127 * is the no of bytes it got from the DMA engine not the no it
2128 * transferred successfully to the device. (And the difference could
2129 * be as much as the FIFO size, I guess ...)
2131 if (!(scsi_status & SCSIXFERDONE)) {
2133 * when data transfer from DMA FIFO to SCSI FIFO
2134 * if there was some data left in SCSI FIFO
2137 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2139 if (dcb->sync_period & WIDE_SYNC)
2140 d_left_counter <<= 1;
2142 dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
2143 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
2144 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
2145 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2146 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2147 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2148 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2149 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2150 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2151 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2154 * calculate all the residue data that not yet transfered
2155 * SCSI transfer counter + left in SCSI FIFO data
2157 * .....TRM_S1040_SCSI_COUNTER (24bits)
2158 * The counter always decrement by one for every SCSI byte transfer.
2159 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2160 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2162 if (srb->total_xfer_length > DC395x_LASTPIO)
2164 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2166 /* Is this a good idea? */
2167 /*clear_fifo(acb, "DOP1"); */
2168 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2169 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2170 && srb->cmd->request_bufflen % 2) {
2173 "data_out_phase0: Discard 1 byte (0x%02x)\n",
2177 * KG: Oops again. Same thinko as above: The SCSI might have been
2178 * faster than the DMA engine, so that it ran out of data.
2179 * In that case, we have to do just nothing!
2180 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2183 * KG: This is nonsense: We have been WRITING data to the bus
2184 * If the SCSI engine has no bytes left, how should the DMA engine?
2186 if (d_left_counter == 0) {
2187 srb->total_xfer_length = 0;
2190 * if transfer not yet complete
2191 * there were some data residue in SCSI FIFO or
2192 * SCSI transfer counter not empty
2195 srb->total_xfer_length - d_left_counter;
2197 (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2198 sg_update_list(srb, d_left_counter);
2199 /* KG: Most ugly hack! Apparently, this works around a chip bug */
2200 if ((srb->segment_x[srb->sg_index].length ==
2201 diff && srb->cmd->use_sg)
2202 || ((oldxferred & ~PAGE_MASK) ==
2205 dprintkl(KERN_INFO, "data_out_phase0: "
2206 "Work around chip bug (%i)?\n", diff);
2208 srb->total_xfer_length - diff;
2209 sg_update_list(srb, d_left_counter);
2210 /*srb->total_xfer_length -= diff; */
2211 /*srb->virt_addr += diff; */
2212 /*if (srb->cmd->use_sg) */
2213 /* srb->sg_index++; */
2217 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2218 cleanup_after_transfer(acb, srb);
2223 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2226 dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
2227 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2228 clear_fifo(acb, "data_out_phase1");
2229 /* do prepare before transfer when data out phase */
2230 data_io_transfer(acb, srb, XFERDATAOUT);
2234 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2237 u16 scsi_status = *pscsi_status;
2238 u32 d_left_counter = 0;
2239 dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
2240 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2243 * KG: DataIn is much more tricky than DataOut. When the device is finished
2244 * and switches to another phase, the SCSI engine should be finished too.
2245 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2246 * engine and transferred to memory.
2247 * We should wait for the FIFOs to be emptied by that (is there any way to
2248 * enforce this?) and then stop the DMA engine, because it might think, that
2249 * there are more bytes to follow. Yes, the device might disconnect prior to
2250 * having all bytes transferred!
2251 * Also we should make sure that all data from the DMA engine buffer's really
2252 * made its way to the system memory! Some documentation on this would not
2253 * seem to be a bad idea, actually.
2255 if (!(srb->state & SRB_XFERPAD)) {
2256 if (scsi_status & PARITYERROR) {
2257 dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
2258 "Parity Error\n", srb->cmd->pid);
2259 srb->status |= PARITY_ERROR;
2262 * KG: We should wait for the DMA FIFO to be empty ...
2263 * but: it would be better to wait first for the SCSI FIFO and then the
2264 * the DMA FIFO to become empty? How do we know, that the device not already
2265 * sent data to the FIFO in a MsgIn phase, eg.?
2267 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2270 dprintkl(KERN_DEBUG,
2271 "DIP0: Wait for DMA FIFO to flush ...\n");
2272 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2273 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2274 /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2276 (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
2278 if (ctr < 6000000 - 1)
2280 "DIP0: Had to wait for DMA ...\n");
2283 "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2284 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2286 dprintkdbg(DBG_KG, "data_in_phase0: "
2287 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2288 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2289 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2291 /* Now: Check remainig data: The SCSI counters should tell us ... */
2292 d_left_counter = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER)
2293 + ((DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f)
2294 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2296 dprintkdbg(DBG_KG, "data_in_phase0: "
2297 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2298 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2299 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2300 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2301 (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2302 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2303 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2304 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2305 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
2306 srb->total_xfer_length, d_left_counter);
2308 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2310 && srb->total_xfer_length <= DC395x_LASTPIO) {
2311 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2312 /*sg_update_list (srb, d_left_counter); */
2313 dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) to "
2314 "%p for remaining %i bytes:",
2315 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f,
2316 (srb->dcb->sync_period & WIDE_SYNC) ?
2319 srb->total_xfer_length);
2320 if (srb->dcb->sync_period & WIDE_SYNC)
2321 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2323 while (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) != 0x40) {
2324 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2325 *(srb->virt_addr)++ = byte;
2326 if (debug_enabled(DBG_PIO))
2327 printk(" %02x", byte);
2329 sg_subtract_one(srb);
2331 if (srb->dcb->sync_period & WIDE_SYNC) {
2333 /* Read the last byte ... */
2334 if (srb->total_xfer_length > 0) {
2335 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2336 *(srb->virt_addr)++ = byte;
2337 srb->total_xfer_length--;
2338 if (debug_enabled(DBG_PIO))
2339 printk(" %02x", byte);
2342 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2344 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2345 /*srb->total_xfer_length = 0; */
2346 if (debug_enabled(DBG_PIO))
2349 #endif /* DC395x_LASTPIO */
2353 * KG: This was in DATAOUT. Does it also belong here?
2354 * Nobody seems to know what counter and fifo_cnt count exactly ...
2356 if (!(scsi_status & SCSIXFERDONE)) {
2358 * when data transfer from DMA FIFO to SCSI FIFO
2359 * if there was some data left in SCSI FIFO
2362 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2364 if (srb->dcb->sync_period & WIDE_SYNC)
2365 d_left_counter <<= 1;
2367 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2369 * KG: Seems to be correct ...
2373 /* KG: This should not be needed any more! */
2374 if (d_left_counter == 0
2375 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2381 DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2382 } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
2385 "Deadlock in DataInPhase0 waiting for DMA!!\n");
2386 srb->total_xfer_length = 0;
2388 srb->total_xfer_length = d_left_counter;
2389 } else { /* phase changed */
2392 * when a transfer not yet complete
2393 * but be disconnected by target
2394 * if transfer not yet complete
2395 * there were some data residue in SCSI FIFO or
2396 * SCSI transfer counter not empty
2398 sg_update_list(srb, d_left_counter);
2401 /* KG: The target may decide to disconnect: Empty FIFO before! */
2402 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2403 cleanup_after_transfer(acb, srb);
2408 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2411 dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
2412 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2413 data_io_transfer(acb, srb, XFERDATAIN);
2417 static void data_io_transfer(struct AdapterCtlBlk *acb,
2418 struct ScsiReqBlk *srb, u16 io_dir)
2420 struct DeviceCtlBlk *dcb = srb->dcb;
2423 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
2424 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
2425 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
2426 srb->total_xfer_length, srb->sg_index, srb->sg_count);
2427 if (srb == acb->tmp_srb)
2428 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
2429 if (srb->sg_index >= srb->sg_count) {
2430 /* can't happen? out of bounds error */
2434 if (srb->total_xfer_length > DC395x_LASTPIO) {
2435 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2437 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2438 * Maybe, even ABORTXFER would be appropriate
2440 if (dma_status & XFERPENDING) {
2441 dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
2442 "Expect trouble!\n");
2443 dump_register_info(acb, dcb, srb);
2444 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2446 /* clear_fifo(acb, "IO"); */
2448 * load what physical address of Scatter/Gather list table
2449 * want to be transfer
2451 srb->state |= SRB_DATA_XFER;
2452 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2453 if (srb->cmd->use_sg) { /* with S/G */
2454 io_dir |= DMACMD_SG;
2455 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2457 sizeof(struct SGentry) *
2459 /* load how many bytes in the sg list table */
2460 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2461 ((u32)(srb->sg_count -
2462 srb->sg_index) << 3));
2463 } else { /* without S/G */
2464 io_dir &= ~DMACMD_SG;
2465 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2466 srb->segment_x[0].address);
2467 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2468 srb->segment_x[0].length);
2470 /* load total transfer length (24bits) max value 16Mbyte */
2471 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2472 srb->total_xfer_length);
2473 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2474 if (io_dir & DMACMD_DIR) { /* read */
2475 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2477 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2479 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2480 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2486 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
2488 * load what physical address of Scatter/Gather list table
2489 * want to be transfer
2491 srb->state |= SRB_DATA_XFER;
2492 /* load total transfer length (24bits) max value 16Mbyte */
2493 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2494 srb->total_xfer_length);
2495 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2496 if (io_dir & DMACMD_DIR) { /* read */
2497 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2499 } else { /* write */
2500 int ln = srb->total_xfer_length;
2501 if (srb->dcb->sync_period & WIDE_SYNC)
2502 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2505 "data_io_transfer: PIO %i bytes from %p:",
2506 srb->total_xfer_length, srb->virt_addr);
2508 while (srb->total_xfer_length) {
2509 if (debug_enabled(DBG_PIO))
2510 printk(" %02x", (unsigned char) *(srb->virt_addr));
2512 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
2513 *(srb->virt_addr)++);
2515 sg_subtract_one(srb);
2517 if (srb->dcb->sync_period & WIDE_SYNC) {
2519 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2520 if (debug_enabled(DBG_PIO))
2523 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2525 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2526 if (debug_enabled(DBG_PIO))
2528 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2532 #endif /* DC395x_LASTPIO */
2533 else { /* xfer pad */
2534 u8 data = 0, data2 = 0;
2535 if (srb->sg_count) {
2536 srb->adapter_status = H_OVER_UNDER_RUN;
2537 srb->status |= OVER_RUN;
2540 * KG: despite the fact that we are using 16 bits I/O ops
2541 * the SCSI FIFO is only 8 bits according to the docs
2542 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2544 if (dcb->sync_period & WIDE_SYNC) {
2545 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
2546 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2548 if (io_dir & DMACMD_DIR) {
2549 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2550 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2552 /* Danger, Robinson: If you find KGs
2553 * scattered over the wide disk, the driver
2554 * or chip is to blame :-( */
2555 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2556 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
2558 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2560 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2561 /* Danger, Robinson: If you find a collection of Ks on your disk
2562 * something broke :-( */
2563 if (io_dir & DMACMD_DIR)
2564 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2566 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2568 srb->state |= SRB_XFERPAD;
2569 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2571 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
2572 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
2577 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2580 dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
2581 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2582 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2583 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
2584 srb->state = SRB_COMPLETED;
2585 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
2586 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2587 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2591 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2594 dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
2595 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2596 srb->state = SRB_STATUS;
2597 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2598 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
2602 /* Check if the message is complete */
2603 static inline u8 msgin_completed(u8 * msgbuf, u32 len)
2605 if (*msgbuf == EXTENDED_MESSAGE) {
2608 if (len < msgbuf[1] + 2)
2610 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
2617 static inline void msgin_reject(struct AdapterCtlBlk *acb,
2618 struct ScsiReqBlk *srb)
2620 srb->msgout_buf[0] = MESSAGE_REJECT;
2622 DC395x_ENABLE_MSGOUT;
2623 srb->state &= ~SRB_MSGIN;
2624 srb->state |= SRB_MSGOUT;
2625 dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
2627 srb->dcb->target_id, srb->dcb->target_lun);
2631 static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
2632 struct DeviceCtlBlk *dcb, u8 tag)
2634 struct ScsiReqBlk *srb = NULL;
2635 struct ScsiReqBlk *i;
2636 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
2637 srb->cmd->pid, tag, srb);
2639 if (!(dcb->tag_mask & (1 << tag)))
2640 dprintkl(KERN_DEBUG,
2641 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2642 dcb->tag_mask, tag);
2644 if (list_empty(&dcb->srb_going_list))
2646 list_for_each_entry(i, &dcb->srb_going_list, list) {
2647 if (i->tag_number == tag) {
2655 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
2656 srb->cmd->pid, srb->dcb->target_id, srb->dcb->target_lun);
2657 if (dcb->flag & ABORT_DEV_) {
2658 /*srb->state = SRB_ABORT_SENT; */
2659 enable_msgout_abort(acb, srb);
2662 if (!(srb->state & SRB_DISCONNECT))
2665 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
2666 srb->state |= dcb->active_srb->state;
2667 srb->state |= SRB_DATA_XFER;
2668 dcb->active_srb = srb;
2669 /* How can we make the DORS happy? */
2674 srb->state = SRB_UNEXPECT_RESEL;
2675 dcb->active_srb = srb;
2676 srb->msgout_buf[0] = MSG_ABORT_TAG;
2678 DC395x_ENABLE_MSGOUT;
2679 dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
2684 static inline void reprogram_regs(struct AdapterCtlBlk *acb,
2685 struct DeviceCtlBlk *dcb)
2687 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
2688 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
2689 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
2690 set_xfer_rate(acb, dcb);
2694 /* set async transfer mode */
2695 static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2697 struct DeviceCtlBlk *dcb = srb->dcb;
2698 dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
2699 dcb->target_id, dcb->target_lun);
2701 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
2702 dcb->sync_mode |= SYNC_NEGO_DONE;
2703 /*dcb->sync_period &= 0; */
2704 dcb->sync_offset = 0;
2705 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
2706 srb->state &= ~SRB_DO_SYNC_NEGO;
2707 reprogram_regs(acb, dcb);
2708 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2709 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2710 build_wdtr(acb, dcb, srb);
2711 DC395x_ENABLE_MSGOUT;
2712 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
2717 /* set sync transfer mode */
2718 static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2720 struct DeviceCtlBlk *dcb = srb->dcb;
2723 dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
2724 "(%02i.%01i MHz) Offset %i\n",
2725 dcb->target_id, srb->msgin_buf[3] << 2,
2726 (250 / srb->msgin_buf[3]),
2727 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
2730 if (srb->msgin_buf[4] > 15)
2731 srb->msgin_buf[4] = 15;
2732 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
2733 dcb->sync_offset = 0;
2734 else if (dcb->sync_offset == 0)
2735 dcb->sync_offset = srb->msgin_buf[4];
2736 if (srb->msgin_buf[4] > dcb->sync_offset)
2737 srb->msgin_buf[4] = dcb->sync_offset;
2739 dcb->sync_offset = srb->msgin_buf[4];
2741 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
2742 || dcb->min_nego_period >
2743 clock_period[bval]))
2745 if (srb->msgin_buf[3] < clock_period[bval])
2747 "msgin_set_sync: Increase sync nego period to %ins\n",
2748 clock_period[bval] << 2);
2749 srb->msgin_buf[3] = clock_period[bval];
2750 dcb->sync_period &= 0xf0;
2751 dcb->sync_period |= ALT_SYNC | bval;
2752 dcb->min_nego_period = srb->msgin_buf[3];
2754 if (dcb->sync_period & WIDE_SYNC)
2760 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2761 dcb->target_id, (fact == 500) ? "Wide16" : "",
2762 dcb->min_nego_period << 2, dcb->sync_offset,
2763 (fact / dcb->min_nego_period),
2764 ((fact % dcb->min_nego_period) * 10 +
2765 dcb->min_nego_period / 2) / dcb->min_nego_period);
2767 if (!(srb->state & SRB_DO_SYNC_NEGO)) {
2768 /* Reply with corrected SDTR Message */
2769 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
2770 srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
2772 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
2774 DC395x_ENABLE_MSGOUT;
2775 dcb->sync_mode |= SYNC_NEGO_DONE;
2777 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2778 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2779 build_wdtr(acb, dcb, srb);
2780 DC395x_ENABLE_MSGOUT;
2781 dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
2784 srb->state &= ~SRB_DO_SYNC_NEGO;
2785 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
2787 reprogram_regs(acb, dcb);
2791 static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
2792 struct ScsiReqBlk *srb)
2794 struct DeviceCtlBlk *dcb = srb->dcb;
2795 dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
2797 dcb->sync_period &= ~WIDE_SYNC;
2798 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
2799 dcb->sync_mode |= WIDE_NEGO_DONE;
2800 srb->state &= ~SRB_DO_WIDE_NEGO;
2801 reprogram_regs(acb, dcb);
2802 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2803 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2804 build_sdtr(acb, dcb, srb);
2805 DC395x_ENABLE_MSGOUT;
2806 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2810 static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2812 struct DeviceCtlBlk *dcb = srb->dcb;
2813 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
2814 && acb->config & HCC_WIDE_CARD) ? 1 : 0;
2815 dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
2817 if (srb->msgin_buf[3] > wide)
2818 srb->msgin_buf[3] = wide;
2820 if (!(srb->state & SRB_DO_WIDE_NEGO)) {
2821 dprintkl(KERN_DEBUG,
2822 "msgin_set_wide: Wide nego initiated <%02i>\n",
2824 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
2826 srb->state |= SRB_DO_WIDE_NEGO;
2827 DC395x_ENABLE_MSGOUT;
2830 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
2831 if (srb->msgin_buf[3] > 0)
2832 dcb->sync_period |= WIDE_SYNC;
2834 dcb->sync_period &= ~WIDE_SYNC;
2835 srb->state &= ~SRB_DO_WIDE_NEGO;
2836 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2838 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2839 (8 << srb->msgin_buf[3]), dcb->target_id);
2840 reprogram_regs(acb, dcb);
2841 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2842 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2843 build_sdtr(acb, dcb, srb);
2844 DC395x_ENABLE_MSGOUT;
2845 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
2851 * extended message codes:
2856 * 00h MODIFY DATA POINTER
2857 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
2858 * 03h WIDE DATA TRANSFER REQUEST
2859 * 04h - 7Fh Reserved
2860 * 80h - FFh Vendor specific
2862 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2865 struct DeviceCtlBlk *dcb = acb->active_dcb;
2866 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->pid);
2868 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2869 if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
2870 /* Now eval the msg */
2871 switch (srb->msgin_buf[0]) {
2873 srb->state = SRB_DISCONNECT;
2876 case SIMPLE_QUEUE_TAG:
2877 case HEAD_OF_QUEUE_TAG:
2878 case ORDERED_QUEUE_TAG:
2880 msgin_qtag(acb, dcb,
2884 case MESSAGE_REJECT:
2885 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2886 DO_CLRATN | DO_DATALATCH);
2887 /* A sync nego message was rejected ! */
2888 if (srb->state & SRB_DO_SYNC_NEGO) {
2889 msgin_set_async(acb, srb);
2892 /* A wide nego message was rejected ! */
2893 if (srb->state & SRB_DO_WIDE_NEGO) {
2894 msgin_set_nowide(acb, srb);
2897 enable_msgout_abort(acb, srb);
2898 /*srb->state |= SRB_ABORT_SENT */
2901 case EXTENDED_MESSAGE:
2903 if (srb->msgin_buf[1] == 3
2904 && srb->msgin_buf[2] == EXTENDED_SDTR) {
2905 msgin_set_sync(acb, srb);
2909 if (srb->msgin_buf[1] == 2
2910 && srb->msgin_buf[2] == EXTENDED_WDTR
2911 && srb->msgin_buf[3] <= 2) { /* sanity check ... */
2912 msgin_set_wide(acb, srb);
2915 msgin_reject(acb, srb);
2918 case MSG_IGNOREWIDE:
2919 /* Discard wide residual */
2920 dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
2923 case COMMAND_COMPLETE:
2924 /* nothing has to be done */
2929 * SAVE POINTER may be ignored as we have the struct
2930 * ScsiReqBlk* associated with the scsi command.
2932 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2933 "SAVE POINTER rem=%i Ignore\n",
2934 srb->cmd->pid, srb->total_xfer_length);
2937 case RESTORE_POINTERS:
2938 dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
2942 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2943 "<%02i-%i> ABORT msg\n",
2944 srb->cmd->pid, dcb->target_id,
2946 dcb->flag |= ABORT_DEV_;
2947 enable_msgout_abort(acb, srb);
2951 /* reject unknown messages */
2952 if (srb->msgin_buf[0] & IDENTIFY_BASE) {
2953 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
2955 srb->msgout_buf[0] = dcb->identify_msg;
2956 DC395x_ENABLE_MSGOUT;
2957 srb->state |= SRB_MSGOUT;
2960 msgin_reject(acb, srb);
2963 /* Clear counter and MsgIn state */
2964 srb->state &= ~SRB_MSGIN;
2967 *pscsi_status = PH_BUS_FREE;
2968 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
2969 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2973 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2976 dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->pid);
2977 clear_fifo(acb, "msgin_phase1");
2978 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2979 if (!(srb->state & SRB_MSGIN)) {
2980 srb->state &= ~SRB_DISCONNECT;
2981 srb->state |= SRB_MSGIN;
2983 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2985 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
2989 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2995 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3001 static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
3003 struct DeviceCtlBlk *i;
3005 /* set all lun device's period, offset */
3006 if (dcb->identify_msg & 0x07)
3009 if (acb->scan_devices) {
3010 current_sync_offset = dcb->sync_offset;
3014 list_for_each_entry(i, &acb->dcb_list, list)
3015 if (i->target_id == dcb->target_id) {
3016 i->sync_period = dcb->sync_period;
3017 i->sync_offset = dcb->sync_offset;
3018 i->sync_mode = dcb->sync_mode;
3019 i->min_nego_period = dcb->min_nego_period;
3024 static void disconnect(struct AdapterCtlBlk *acb)
3026 struct DeviceCtlBlk *dcb = acb->active_dcb;
3027 struct ScsiReqBlk *srb;
3030 dprintkl(KERN_ERR, "disconnect: No such device\n");
3032 /* Suspend queue for a while */
3033 acb->scsi_host->last_reset =
3035 HZ * acb->eeprom.delay_time;
3036 clear_fifo(acb, "disconnectEx");
3037 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3040 srb = dcb->active_srb;
3041 acb->active_dcb = NULL;
3042 dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->pid);
3044 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3045 clear_fifo(acb, "disconnect");
3046 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3047 if (srb->state & SRB_UNEXPECT_RESEL) {
3049 "disconnect: Unexpected reselection <%02i-%i>\n",
3050 dcb->target_id, dcb->target_lun);
3052 waiting_process_next(acb);
3053 } else if (srb->state & SRB_ABORT_SENT) {
3054 dcb->flag &= ~ABORT_DEV_;
3055 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
3056 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
3057 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
3058 waiting_process_next(acb);
3060 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
3062 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
3064 * Selection time out
3065 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
3067 /* Unexp. Disc / Sel Timeout */
3068 if (srb->state != SRB_START_
3069 && srb->state != SRB_MSGOUT) {
3070 srb->state = SRB_READY;
3071 dprintkl(KERN_DEBUG,
3072 "disconnect: (pid#%li) Unexpected\n",
3074 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
3077 /* Normal selection timeout */
3078 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
3079 "<%02i-%i> SelTO\n", srb->cmd->pid,
3080 dcb->target_id, dcb->target_lun);
3081 if (srb->retry_count++ > DC395x_MAX_RETRIES
3082 || acb->scan_devices) {
3083 srb->target_status =
3084 SCSI_STAT_SEL_TIMEOUT;
3088 srb_going_to_waiting_move(dcb, srb);
3090 "disconnect: (pid#%li) Retry\n",
3092 waiting_set_timer(acb, HZ / 20);
3094 } else if (srb->state & SRB_DISCONNECT) {
3095 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
3097 * SRB_DISCONNECT (This is what we expect!)
3100 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
3101 " 0x%02x: ACK set! Other controllers?\n",
3103 /* It could come from another initiator, therefore don't do much ! */
3105 waiting_process_next(acb);
3106 } else if (srb->state & SRB_COMPLETED) {
3112 dcb->active_srb = NULL;
3113 srb->state = SRB_FREE;
3114 srb_done(acb, dcb, srb);
3120 static void reselect(struct AdapterCtlBlk *acb)
3122 struct DeviceCtlBlk *dcb = acb->active_dcb;
3123 struct ScsiReqBlk *srb = NULL;
3124 u16 rsel_tar_lun_id;
3127 dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
3129 clear_fifo(acb, "reselect");
3130 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3131 /* Read Reselected Target ID and LUN */
3132 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3133 if (dcb) { /* Arbitration lost but Reselection win */
3134 srb = dcb->active_srb;
3136 dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
3137 "but active_srb == NULL\n");
3138 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3142 if (!acb->scan_devices) {
3143 dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
3144 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
3145 srb->cmd->pid, dcb->target_id,
3146 dcb->target_lun, rsel_tar_lun_id,
3147 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3149 /*srb->state |= SRB_DISCONNECT; */
3151 srb->state = SRB_READY;
3153 srb_going_to_waiting_move(dcb, srb);
3154 waiting_set_timer(acb, HZ / 20);
3159 /* Read Reselected Target Id and LUN */
3160 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3161 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
3162 "Got %i!\n", rsel_tar_lun_id);
3163 id = rsel_tar_lun_id & 0xff;
3164 lun = (rsel_tar_lun_id >> 8) & 7;
3165 dcb = find_dcb(acb, id, lun);
3167 dprintkl(KERN_ERR, "reselect: From non existent device "
3168 "<%02i-%i>\n", id, lun);
3169 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3172 acb->active_dcb = dcb;
3174 if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3175 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
3176 "disconnection? <%02i-%i>\n",
3177 dcb->target_id, dcb->target_lun);
3179 if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
3181 dcb->active_srb = srb;
3183 /* There can be only one! */
3184 srb = dcb->active_srb;
3185 if (!srb || !(srb->state & SRB_DISCONNECT)) {
3189 dprintkl(KERN_DEBUG,
3190 "reselect: w/o disconnected cmds <%02i-%i>\n",
3191 dcb->target_id, dcb->target_lun);
3193 srb->state = SRB_UNEXPECT_RESEL;
3194 dcb->active_srb = srb;
3195 enable_msgout_abort(acb, srb);
3197 if (dcb->flag & ABORT_DEV_) {
3198 /*srb->state = SRB_ABORT_SENT; */
3199 enable_msgout_abort(acb, srb);
3201 srb->state = SRB_DATA_XFER;
3205 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3207 /* Program HA ID, target ID, period and offset */
3208 dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
3209 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
3210 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
3211 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
3212 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
3213 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3215 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3219 static inline u8 tagq_blacklist(char *name)
3221 #ifndef DC395x_NO_TAGQ
3224 for (i = 0; i < BADDEVCNT; i++)
3225 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
3235 static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
3237 /* Check for SCSI format (ANSI and Response data format) */
3238 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
3239 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
3240 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
3241 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3242 /* ((dcb->dev_type == TYPE_DISK)
3243 || (dcb->dev_type == TYPE_MOD)) && */
3244 !tagq_blacklist(((char *)ptr) + 8)) {
3245 if (dcb->max_command == 1)
3247 dcb->acb->tag_max_num;
3248 dcb->sync_mode |= EN_TAG_QUEUEING;
3249 /*dcb->tag_mask = 0; */
3251 dcb->max_command = 1;
3256 static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3257 struct ScsiInqData *ptr)
3259 u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
3260 dcb->dev_type = bval1;
3261 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3262 disc_tagq_set(dcb, ptr);
3266 /* unmap mapped pci regions from SRB */
3267 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3269 struct scsi_cmnd *cmd = srb->cmd;
3270 enum dma_data_direction dir = cmd->sc_data_direction;
3271 if (cmd->use_sg && dir != PCI_DMA_NONE) {
3273 /* unmap DC395x SG list */
3274 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
3275 srb->sg_bus_addr, SEGMENTX_LEN);
3276 pci_unmap_single(acb->dev, srb->sg_bus_addr,
3279 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3280 cmd->use_sg, cmd->request_buffer);
3281 /* unmap the sg segments */
3282 for (i = 0; i < srb->sg_count; i++)
3283 kunmap(virt_to_page(srb->virt_map[i]));
3284 pci_unmap_sg(acb->dev,
3285 (struct scatterlist *)cmd->request_buffer,
3287 } else if (cmd->request_buffer && dir != PCI_DMA_NONE) {
3288 dprintkdbg(DBG_SG, "pci_unmap_srb: buffer=%08x(%05x)\n",
3289 srb->segment_x[0].address, cmd->request_bufflen);
3290 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3291 cmd->request_bufflen, dir);
3296 /* unmap mapped pci sense buffer from SRB */
3297 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3298 struct ScsiReqBlk *srb)
3300 if (!(srb->flag & AUTO_REQSENSE))
3302 /* Unmap sense buffer */
3303 dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
3304 srb->segment_x[0].address);
3305 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3306 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
3307 /* Restore SG stuff */
3308 srb->total_xfer_length = srb->xferred;
3309 srb->segment_x[0].address =
3310 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
3311 srb->segment_x[0].length =
3312 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
3317 * Complete execution of a SCSI command
3318 * Signal completion to the generic SCSI driver
3320 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3321 struct ScsiReqBlk *srb)
3324 struct scsi_cmnd *cmd = srb->cmd;
3325 struct ScsiInqData *ptr;
3326 enum dma_data_direction dir = cmd->sc_data_direction;
3329 struct scatterlist* sg = (struct scatterlist *)cmd->request_buffer;
3330 ptr = (struct ScsiInqData *)(srb->virt_map[0] + sg->offset);
3332 ptr = (struct ScsiInqData *)(cmd->request_buffer);
3335 dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->pid,
3336 srb->cmd->device->id, srb->cmd->device->lun);
3337 dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p addr=%p\n",
3338 srb, cmd->use_sg, srb->sg_index, srb->sg_count,
3339 cmd->request_buffer, ptr);
3340 status = srb->target_status;
3341 if (srb->flag & AUTO_REQSENSE) {
3342 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
3343 pci_unmap_srb_sense(acb, srb);
3345 ** target status..........................
3347 srb->flag &= ~AUTO_REQSENSE;
3348 srb->adapter_status = 0;
3349 srb->target_status = CHECK_CONDITION << 1;
3350 if (debug_enabled(DBG_1)) {
3351 switch (cmd->sense_buffer[2] & 0x0f) {
3353 dprintkl(KERN_DEBUG,
3354 "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3355 cmd->cmnd[0], dcb->target_id,
3356 dcb->target_lun, status, acb->scan_devices);
3358 case UNIT_ATTENTION:
3359 dprintkl(KERN_DEBUG,
3360 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3361 cmd->cmnd[0], dcb->target_id,
3362 dcb->target_lun, status, acb->scan_devices);
3364 case ILLEGAL_REQUEST:
3365 dprintkl(KERN_DEBUG,
3366 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3367 cmd->cmnd[0], dcb->target_id,
3368 dcb->target_lun, status, acb->scan_devices);
3371 dprintkl(KERN_DEBUG,
3372 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3373 cmd->cmnd[0], dcb->target_id,
3374 dcb->target_lun, status, acb->scan_devices);
3376 case HARDWARE_ERROR:
3377 dprintkl(KERN_DEBUG,
3378 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3379 cmd->cmnd[0], dcb->target_id,
3380 dcb->target_lun, status, acb->scan_devices);
3383 if (cmd->sense_buffer[7] >= 6)
3384 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3385 "(0x%08x 0x%08x)\n",
3386 cmd->sense_buffer[2], cmd->sense_buffer[12],
3387 cmd->sense_buffer[13],
3388 *((unsigned int *)(cmd->sense_buffer + 3)),
3389 *((unsigned int *)(cmd->sense_buffer + 8)));
3391 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3392 cmd->sense_buffer[2],
3393 *((unsigned int *)(cmd->sense_buffer + 3)));
3396 if (status == (CHECK_CONDITION << 1)) {
3397 cmd->result = DID_BAD_TARGET << 16;
3400 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
3402 if (srb->total_xfer_length
3403 && srb->total_xfer_length >= cmd->underflow)
3405 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3406 srb->end_message, CHECK_CONDITION);
3407 /*SET_RES_DID(cmd->result,DID_OK) */
3410 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3411 srb->end_message, CHECK_CONDITION);
3416 /*************************************************************/
3419 * target status..........................
3421 if (status_byte(status) == CHECK_CONDITION) {
3422 request_sense(acb, dcb, srb);
3424 } else if (status_byte(status) == QUEUE_FULL) {
3425 tempcnt = (u8)list_size(&dcb->srb_going_list);
3426 dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3427 dcb->target_id, dcb->target_lun, tempcnt);
3430 dcb->max_command = tempcnt;
3432 srb_going_to_waiting_move(dcb, srb);
3433 waiting_set_timer(acb, HZ / 20);
3434 srb->adapter_status = 0;
3435 srb->target_status = 0;
3437 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
3438 srb->adapter_status = H_SEL_TIMEOUT;
3439 srb->target_status = 0;
3440 cmd->result = DID_NO_CONNECT << 16;
3442 srb->adapter_status = 0;
3443 SET_RES_DID(cmd->result, DID_ERROR);
3444 SET_RES_MSG(cmd->result, srb->end_message);
3445 SET_RES_TARGET(cmd->result, status);
3450 ** process initiator status..........................
3452 status = srb->adapter_status;
3453 if (status & H_OVER_UNDER_RUN) {
3454 srb->target_status = 0;
3455 SET_RES_DID(cmd->result, DID_OK);
3456 SET_RES_MSG(cmd->result, srb->end_message);
3457 } else if (srb->status & PARITY_ERROR) {
3458 SET_RES_DID(cmd->result, DID_PARITY);
3459 SET_RES_MSG(cmd->result, srb->end_message);
3460 } else { /* No error */
3462 srb->adapter_status = 0;
3463 srb->target_status = 0;
3464 SET_RES_DID(cmd->result, DID_OK);
3468 if (dir != PCI_DMA_NONE) {
3470 pci_dma_sync_sg_for_cpu(acb->dev,
3471 (struct scatterlist *)cmd->
3472 request_buffer, cmd->use_sg, dir);
3473 else if (cmd->request_buffer)
3474 pci_dma_sync_single_for_cpu(acb->dev,
3475 srb->segment_x[0].address,
3476 cmd->request_bufflen, dir);
3479 if ((cmd->result & RES_DID) == 0 && cmd->cmnd[0] == INQUIRY
3480 && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8
3481 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
3482 dcb->inquiry7 = ptr->Flags;
3483 /* Check Error Conditions */
3486 /*if( srb->cmd->cmnd[0] == INQUIRY && */
3487 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3488 if (cmd->cmnd[0] == INQUIRY && (cmd->result == (DID_OK << 16)
3489 || status_byte(cmd->
3493 if (!dcb->init_tcq_flag) {
3494 add_dev(acb, dcb, ptr);
3495 dcb->init_tcq_flag = 1;
3501 /* Here is the info for Doug Gilbert's sg3 ... */
3502 cmd->resid = srb->total_xfer_length;
3503 /* This may be interpreted by sb. or not ... */
3504 cmd->SCp.this_residual = srb->total_xfer_length;
3505 cmd->SCp.buffers_residual = 0;
3506 if (debug_enabled(DBG_KG)) {
3507 if (srb->total_xfer_length)
3508 dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
3509 "cmnd=0x%02x Missed %i bytes\n",
3510 cmd->pid, cmd->device->id, cmd->device->lun,
3511 cmd->cmnd[0], srb->total_xfer_length);
3514 srb_going_remove(dcb, srb);
3515 /* Add to free list */
3516 if (srb == acb->tmp_srb)
3517 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3519 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
3520 cmd->pid, cmd->result);
3521 srb_free_insert(acb, srb);
3523 pci_unmap_srb(acb, srb);
3525 cmd->scsi_done(cmd);
3526 waiting_process_next(acb);
3530 /* abort all cmds in our queues */
3531 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
3532 struct scsi_cmnd *cmd, u8 force)
3534 struct DeviceCtlBlk *dcb;
3535 dprintkl(KERN_INFO, "doing_srb_done: pids ");
3537 list_for_each_entry(dcb, &acb->dcb_list, list) {
3538 struct ScsiReqBlk *srb;
3539 struct ScsiReqBlk *tmp;
3540 struct scsi_cmnd *p;
3542 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
3543 enum dma_data_direction dir;
3547 dir = p->sc_data_direction;
3548 result = MK_RES(0, did_flag, 0, 0);
3549 printk("G:%li(%02i-%i) ", p->pid,
3550 p->device->id, p->device->lun);
3551 srb_going_remove(dcb, srb);
3553 srb_free_insert(acb, srb);
3555 pci_unmap_srb_sense(acb, srb);
3556 pci_unmap_srb(acb, srb);
3558 /* For new EH, we normally don't need to give commands back,
3559 * as they all complete or all time out */
3563 if (!list_empty(&dcb->srb_going_list))
3564 dprintkl(KERN_DEBUG,
3565 "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3566 dcb->target_id, dcb->target_lun);
3568 dprintkl(KERN_DEBUG,
3569 "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3570 dcb->target_id, dcb->target_lun,
3574 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
3578 result = MK_RES(0, did_flag, 0, 0);
3579 printk("W:%li<%02i-%i>", p->pid, p->device->id,
3581 srb_waiting_remove(dcb, srb);
3582 srb_free_insert(acb, srb);
3584 pci_unmap_srb_sense(acb, srb);
3585 pci_unmap_srb(acb, srb);
3587 /* For new EH, we normally don't need to give commands back,
3588 * as they all complete or all time out */
3589 cmd->scsi_done(cmd);
3592 if (!list_empty(&dcb->srb_waiting_list))
3593 dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
3594 list_size(&dcb->srb_waiting_list), dcb->target_id,
3596 dcb->flag &= ~ABORT_DEV_;
3602 static void reset_scsi_bus(struct AdapterCtlBlk *acb)
3604 dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
3605 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
3606 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
3608 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
3613 static void set_basic_config(struct AdapterCtlBlk *acb)
3617 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
3618 if (acb->config & HCC_PARITY)
3619 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
3621 bval = PHASELATCH | INITIATOR | BLOCKRST;
3623 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
3625 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3626 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
3627 /* program Host ID */
3628 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
3629 /* set ansynchronous transfer */
3630 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
3631 /* Turn LED control off */
3632 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
3633 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
3635 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
3637 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
3638 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
3639 /* Clear pending interrupt status */
3640 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
3641 /* Enable SCSI interrupt */
3642 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
3643 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
3644 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3649 static void scsi_reset_detect(struct AdapterCtlBlk *acb)
3651 dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
3652 /* delay half a second */
3653 if (timer_pending(&acb->waiting_timer))
3654 del_timer(&acb->waiting_timer);
3656 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
3657 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
3658 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3660 /* Maybe we locked up the bus? Then lets wait even longer ... */
3661 acb->scsi_host->last_reset =
3662 jiffies + 5 * HZ / 2 +
3663 HZ * acb->eeprom.delay_time;
3665 clear_fifo(acb, "scsi_reset_detect");
3666 set_basic_config(acb);
3668 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3670 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
3671 acb->acb_flag |= RESET_DONE;
3673 acb->acb_flag |= RESET_DETECT;
3674 reset_dev_param(acb);
3675 doing_srb_done(acb, DID_RESET, NULL, 1);
3676 /*DC395x_RecoverSRB( acb ); */
3677 acb->active_dcb = NULL;
3679 waiting_process_next(acb);
3684 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3685 struct ScsiReqBlk *srb)
3687 struct scsi_cmnd *cmd = srb->cmd;
3688 dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
3689 cmd->pid, cmd->device->id, cmd->device->lun);
3691 srb->flag |= AUTO_REQSENSE;
3692 srb->adapter_status = 0;
3693 srb->target_status = 0;
3695 /* KG: Can this prevent crap sense data ? */
3696 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3698 /* Save some data */
3699 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
3700 srb->segment_x[0].address;
3701 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
3702 srb->segment_x[0].length;
3703 srb->xferred = srb->total_xfer_length;
3704 /* srb->segment_x : a one entry of S/G list table */
3705 srb->total_xfer_length = sizeof(cmd->sense_buffer);
3706 srb->segment_x[0].length = sizeof(cmd->sense_buffer);
3707 /* Map sense buffer */
3708 srb->segment_x[0].address =
3709 pci_map_single(acb->dev, cmd->sense_buffer,
3710 sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE);
3711 dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
3712 cmd->sense_buffer, srb->segment_x[0].address,
3713 sizeof(cmd->sense_buffer));
3717 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
3718 dprintkl(KERN_DEBUG,
3719 "request_sense: (pid#%li) failed <%02i-%i>\n",
3720 srb->cmd->pid, dcb->target_id, dcb->target_lun);
3721 srb_going_to_waiting_move(dcb, srb);
3722 waiting_set_timer(acb, HZ / 100);
3728 * device_alloc - Allocate a new device instance. This create the
3729 * devices instance and sets up all the data items. The adapter
3730 * instance is required to obtain confiuration information for this
3731 * device. This does *not* add this device to the adapters device
3734 * @acb: The adapter to obtain configuration information from.
3735 * @target: The target for the new device.
3736 * @lun: The lun for the new device.
3738 * Return the new device if succesfull or NULL on failure.
3740 static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
3743 struct NvRamType *eeprom = &acb->eeprom;
3744 u8 period_index = eeprom->target[target].period & 0x07;
3745 struct DeviceCtlBlk *dcb;
3747 dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
3748 dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
3752 INIT_LIST_HEAD(&dcb->srb_going_list);
3753 INIT_LIST_HEAD(&dcb->srb_waiting_list);
3754 dcb->active_srb = NULL;
3756 dcb->max_command = 1;
3757 dcb->target_id = target;
3758 dcb->target_lun = lun;
3759 #ifndef DC395x_NO_DISCONNECT
3761 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
3763 dcb->identify_msg = IDENTIFY(0, lun);
3765 dcb->dev_mode = eeprom->target[target].cfg0;
3768 dcb->min_nego_period = clock_period[period_index];
3769 dcb->sync_period = 0;
3770 dcb->sync_offset = 0;
3773 #ifndef DC395x_NO_WIDE
3774 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
3775 && (acb->config & HCC_WIDE_CARD))
3776 dcb->sync_mode |= WIDE_NEGO_ENABLE;
3778 #ifndef DC395x_NO_SYNC
3779 if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
3780 if (!(lun) || current_sync_offset)
3781 dcb->sync_mode |= SYNC_NEGO_ENABLE;
3783 if (dcb->target_lun != 0) {
3785 struct DeviceCtlBlk *p;
3786 list_for_each_entry(p, &acb->dcb_list, list)
3787 if (p->target_id == dcb->target_id)
3790 "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3791 dcb->target_id, dcb->target_lun,
3792 p->target_id, p->target_lun);
3793 dcb->sync_mode = p->sync_mode;
3794 dcb->sync_period = p->sync_period;
3795 dcb->min_nego_period = p->min_nego_period;
3796 dcb->sync_offset = p->sync_offset;
3797 dcb->inquiry7 = p->inquiry7;
3804 * adapter_add_device - Adds the device instance to the adaptor instance.
3806 * @acb: The adapter device to be updated
3807 * @dcb: A newly created and intialised device instance to add.
3809 static void adapter_add_device(struct AdapterCtlBlk *acb,
3810 struct DeviceCtlBlk *dcb)
3812 /* backpointer to adapter */
3815 /* set run_robin to this device if it is currently empty */
3816 if (list_empty(&acb->dcb_list))
3817 acb->dcb_run_robin = dcb;
3819 /* add device to list */
3820 list_add_tail(&dcb->list, &acb->dcb_list);
3822 /* update device maps */
3823 acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
3824 acb->children[dcb->target_id][dcb->target_lun] = dcb;
3829 * adapter_remove_device - Removes the device instance from the adaptor
3830 * instance. The device instance is not check in any way or freed by this.
3831 * The caller is expected to take care of that. This will simply remove the
3832 * device from the adapters data strcutures.
3834 * @acb: The adapter device to be updated
3835 * @dcb: A device that has previously been added to the adapter.
3837 static void adapter_remove_device(struct AdapterCtlBlk *acb,
3838 struct DeviceCtlBlk *dcb)
3840 struct DeviceCtlBlk *i;
3841 struct DeviceCtlBlk *tmp;
3842 dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
3843 dcb->target_id, dcb->target_lun);
3845 /* fix up any pointers to this device that we have in the adapter */
3846 if (acb->active_dcb == dcb)
3847 acb->active_dcb = NULL;
3848 if (acb->dcb_run_robin == dcb)
3849 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
3851 /* unlink from list */
3852 list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
3858 /* clear map and children */
3859 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
3860 acb->children[dcb->target_id][dcb->target_lun] = NULL;
3866 * adapter_remove_and_free_device - Removes a single device from the adapter
3867 * and then frees the device information.
3869 * @acb: The adapter device to be updated
3870 * @dcb: A device that has previously been added to the adapter.
3872 static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
3873 struct DeviceCtlBlk *dcb)
3875 if (list_size(&dcb->srb_going_list) > 1) {
3876 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
3877 "Won't remove because of %i active requests.\n",
3878 dcb->target_id, dcb->target_lun,
3879 list_size(&dcb->srb_going_list));
3882 adapter_remove_device(acb, dcb);
3888 * adapter_remove_and_free_all_devices - Removes and frees all of the
3889 * devices associated with the specified adapter.
3891 * @acb: The adapter from which all devices should be removed.
3893 static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
3895 struct DeviceCtlBlk *dcb;
3896 struct DeviceCtlBlk *tmp;
3897 dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
3898 list_size(&acb->dcb_list));
3900 list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
3901 adapter_remove_and_free_device(acb, dcb);
3906 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3907 * scsi device that we need to deal with. We allocate a new device and then
3908 * insert that device into the adapters device list.
3910 * @scsi_device: The new scsi device that we need to handle.
3912 static int dc395x_slave_alloc(struct scsi_device *scsi_device)
3914 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3915 struct DeviceCtlBlk *dcb;
3917 dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
3920 adapter_add_device(acb, dcb);
3927 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3928 * device that is going away.
3930 * @scsi_device: The new scsi device that we need to handle.
3932 static void dc395x_slave_destroy(struct scsi_device *scsi_device)
3934 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3935 struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
3937 adapter_remove_and_free_device(acb, dcb);
3944 * trms1040_wait_30us: wait for 30 us
3946 * Waits for 30us (using the chip by the looks of it..)
3948 * @io_port: base I/O address
3950 static void __devinit trms1040_wait_30us(unsigned long io_port)
3952 /* ScsiPortStallExecution(30); wait 30 us */
3953 outb(5, io_port + TRM_S1040_GEN_TIMER);
3954 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
3960 * trms1040_write_cmd - write the secified command and address to
3963 * @io_port: base I/O address
3964 * @cmd: SB + op code (command) to send
3965 * @addr: address to send
3967 static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
3972 /* program SB + OP code */
3973 for (i = 0; i < 3; i++, cmd <<= 1) {
3974 send_data = NVR_SELECT;
3975 if (cmd & 0x04) /* Start from bit 2 */
3976 send_data |= NVR_BITOUT;
3978 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3979 trms1040_wait_30us(io_port);
3980 outb((send_data | NVR_CLOCK),
3981 io_port + TRM_S1040_GEN_NVRAM);
3982 trms1040_wait_30us(io_port);
3986 for (i = 0; i < 7; i++, addr <<= 1) {
3987 send_data = NVR_SELECT;
3988 if (addr & 0x40) /* Start from bit 6 */
3989 send_data |= NVR_BITOUT;
3991 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3992 trms1040_wait_30us(io_port);
3993 outb((send_data | NVR_CLOCK),
3994 io_port + TRM_S1040_GEN_NVRAM);
3995 trms1040_wait_30us(io_port);
3997 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
3998 trms1040_wait_30us(io_port);
4003 * trms1040_set_data - store a single byte in the eeprom
4005 * Called from write all to write a single byte into the SSEEPROM
4006 * Which is done one bit at a time.
4008 * @io_port: base I/O address
4009 * @addr: offset into EEPROM
4010 * @byte: bytes to write
4012 static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
4017 /* Send write command & address */
4018 trms1040_write_cmd(io_port, 0x05, addr);
4021 for (i = 0; i < 8; i++, byte <<= 1) {
4022 send_data = NVR_SELECT;
4023 if (byte & 0x80) /* Start from bit 7 */
4024 send_data |= NVR_BITOUT;
4026 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4027 trms1040_wait_30us(io_port);
4028 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4029 trms1040_wait_30us(io_port);
4031 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4032 trms1040_wait_30us(io_port);
4034 /* Disable chip select */
4035 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4036 trms1040_wait_30us(io_port);
4038 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4039 trms1040_wait_30us(io_port);
4041 /* Wait for write ready */
4043 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4044 trms1040_wait_30us(io_port);
4046 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4047 trms1040_wait_30us(io_port);
4049 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
4053 /* Disable chip select */
4054 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4059 * trms1040_write_all - write 128 bytes to the eeprom
4061 * Write the supplied 128 bytes to the chips SEEPROM
4063 * @eeprom: the data to write
4064 * @io_port: the base io port
4066 static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
4068 u8 *b_eeprom = (u8 *)eeprom;
4071 /* Enable SEEPROM */
4072 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4073 io_port + TRM_S1040_GEN_CONTROL);
4076 trms1040_write_cmd(io_port, 0x04, 0xFF);
4077 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4078 trms1040_wait_30us(io_port);
4081 for (addr = 0; addr < 128; addr++, b_eeprom++)
4082 trms1040_set_data(io_port, addr, *b_eeprom);
4085 trms1040_write_cmd(io_port, 0x04, 0x00);
4086 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4087 trms1040_wait_30us(io_port);
4089 /* Disable SEEPROM */
4090 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4091 io_port + TRM_S1040_GEN_CONTROL);
4096 * trms1040_get_data - get a single byte from the eeprom
4098 * Called from read all to read a single byte into the SSEEPROM
4099 * Which is done one bit at a time.
4101 * @io_port: base I/O address
4102 * @addr: offset into SEEPROM
4104 * Returns the the byte read.
4106 static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
4112 /* Send read command & address */
4113 trms1040_write_cmd(io_port, 0x06, addr);
4116 for (i = 0; i < 8; i++) {
4117 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4118 trms1040_wait_30us(io_port);
4119 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4121 /* Get data bit while falling edge */
4122 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
4124 if (read_byte & NVR_BITIN)
4127 trms1040_wait_30us(io_port);
4130 /* Disable chip select */
4131 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4137 * trms1040_read_all - read all bytes from the eeprom
4139 * Read the 128 bytes from the SEEPROM.
4141 * @eeprom: where to store the data
4142 * @io_port: the base io port
4144 static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
4146 u8 *b_eeprom = (u8 *)eeprom;
4149 /* Enable SEEPROM */
4150 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4151 io_port + TRM_S1040_GEN_CONTROL);
4154 for (addr = 0; addr < 128; addr++, b_eeprom++)
4155 *b_eeprom = trms1040_get_data(io_port, addr);
4157 /* Disable SEEPROM */
4158 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4159 io_port + TRM_S1040_GEN_CONTROL);
4165 * check_eeprom - get and check contents of the eeprom
4167 * Read seeprom 128 bytes into the memory provider in eeprom.
4168 * Checks the checksum and if it's not correct it uses a set of default
4171 * @eeprom: caller allocated strcuture to read the eeprom data into
4172 * @io_port: io port to read from
4174 static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
4176 u16 *w_eeprom = (u16 *)eeprom;
4182 trms1040_read_all(eeprom, io_port); /* read eeprom */
4185 for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
4186 w_addr++, w_eeprom++)
4188 if (cksum != 0x1234) {
4190 * Checksum is wrong.
4191 * Load a set of defaults into the eeprom buffer
4193 dprintkl(KERN_WARNING,
4194 "EEProm checksum error: using default values and options.\n");
4195 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4196 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4197 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4198 eeprom->sub_sys_id[1] =
4199 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4200 eeprom->sub_class = 0x00;
4201 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4202 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4203 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4204 eeprom->device_id[1] =
4205 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4206 eeprom->reserved = 0x00;
4208 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
4209 d_addr < 16; d_addr++, d_eeprom++)
4210 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
4212 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
4213 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
4214 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
4217 /* Now load defaults (maybe set by boot/module params) */
4218 set_safe_settings();
4220 eeprom_override(eeprom);
4222 eeprom->cksum = 0x00;
4223 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
4224 w_addr < 63; w_addr++, w_eeprom++)
4227 *w_eeprom = 0x1234 - cksum;
4228 trms1040_write_all(eeprom, io_port);
4229 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
4231 set_safe_settings();
4232 eeprom_index_to_delay(eeprom);
4233 eeprom_override(eeprom);
4239 * print_eeprom_settings - output the eeprom settings
4240 * to the kernel log so people can see what they were.
4242 * @eeprom: The eeprom data strucutre to show details for.
4244 static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
4246 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4248 eeprom->target[0].period,
4249 clock_speed[eeprom->target[0].period] / 10,
4250 clock_speed[eeprom->target[0].period] % 10,
4251 eeprom->target[0].cfg0);
4252 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4253 eeprom->channel_cfg, eeprom->max_tag,
4254 1 << eeprom->max_tag, eeprom->delay_time);
4258 /* Free SG tables */
4259 static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
4262 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4264 for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
4265 kfree(acb->srb_array[i].segment_x);
4267 vfree(acb->srb_array[0].virt_map);
4272 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4273 * should never cross a page boundary */
4274 static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
4276 const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
4278 int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
4279 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4282 struct SGentry *ptr;
4285 for (i = 0; i < DC395x_MAX_SRB_CNT; i++) {
4286 acb->srb_array[i].segment_x = NULL;
4287 acb->srb_array[i].virt_map = NULL;
4290 dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
4292 ptr = (struct SGentry *)kmalloc(PAGE_SIZE, GFP_KERNEL);
4294 adapter_sg_tables_free(acb);
4297 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
4298 PAGE_SIZE, ptr, srb_idx);
4300 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4301 acb->srb_array[srb_idx++].segment_x =
4302 ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4304 if (i < srbs_per_page)
4305 acb->srb.segment_x =
4306 ptr + (i * DC395x_MAX_SG_LISTENTRY);
4308 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
4310 virt_array = vmalloc((DC395x_MAX_SRB_CNT + 1) * DC395x_MAX_SG_LISTENTRY * sizeof(void*));
4313 adapter_sg_tables_free(acb);
4317 for (i = 0; i < DC395x_MAX_SRB_CNT + 1; i++) {
4318 acb->srb_array[i].virt_map = virt_array;
4319 virt_array += DC395x_MAX_SG_LISTENTRY;
4328 * adapter_print_config - print adapter connection and termination
4331 * The io port in the adapter needs to have been set before calling
4334 * @acb: The adapter to print the information for.
4336 static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
4340 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
4341 dprintkl(KERN_INFO, "%sConnectors: ",
4342 ((bval & WIDESCSI) ? "(Wide) " : ""));
4343 if (!(bval & CON5068))
4344 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
4345 if (!(bval & CON68))
4346 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
4347 if (!(bval & CON50))
4349 if ((bval & (CON5068 | CON50 | CON68)) ==
4350 0 /*(CON5068 | CON50 | CON68) */ )
4351 printk(" Oops! (All 3?) ");
4352 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
4353 printk(" Termination: ");
4354 if (bval & DIS_TERM)
4355 printk("Disabled\n");
4357 if (bval & AUTOTERM)
4359 if (bval & LOW8TERM)
4369 * adapter_init_params - Initialize the various parameters in the
4370 * adapter structure. Note that the pointer to the scsi_host is set
4371 * early (when this instance is created) and the io_port and irq
4372 * values are set later after they have been reserved. This just gets
4373 * everything set to a good starting position.
4375 * The eeprom structure in the adapter needs to have been set before
4376 * calling this function.
4378 * @acb: The adapter to initialize.
4380 static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
4382 struct NvRamType *eeprom = &acb->eeprom;
4385 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4386 /* NOTE: acb->io_port_base is set at port registration time */
4387 /* NOTE: acb->io_port_len is set at port registration time */
4389 INIT_LIST_HEAD(&acb->dcb_list);
4390 acb->dcb_run_robin = NULL;
4391 acb->active_dcb = NULL;
4393 INIT_LIST_HEAD(&acb->srb_free_list);
4394 /* temp SRB for Q tag used or abort command used */
4395 acb->tmp_srb = &acb->srb;
4396 init_timer(&acb->waiting_timer);
4397 init_timer(&acb->selto_timer);
4399 acb->srb_count = DC395x_MAX_SRB_CNT;
4401 acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */
4402 /* NOTE: acb->irq_level is set at IRQ registration time */
4404 acb->tag_max_num = 1 << eeprom->max_tag;
4405 if (acb->tag_max_num > 30)
4406 acb->tag_max_num = 30;
4408 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4409 acb->gmode2 = eeprom->channel_cfg;
4410 acb->config = 0; /* NOTE: actually set in adapter_init_chip */
4412 if (eeprom->channel_cfg & NAC_SCANLUN)
4414 acb->scan_devices = 1;
4416 acb->scsi_host->this_id = eeprom->scsi_id;
4417 acb->hostid_bit = (1 << acb->scsi_host->this_id);
4419 for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
4420 acb->dcb_map[i] = 0;
4424 /* link static array of srbs into the srb free list */
4425 for (i = 0; i < acb->srb_count - 1; i++)
4426 srb_free_insert(acb, &acb->srb_array[i]);
4431 * adapter_init_host - Initialize the scsi host instance based on
4432 * values that we have already stored in the adapter instance. There's
4433 * some mention that a lot of these are deprecated, so we won't use
4434 * them (we'll use the ones in the adapter instance) but we'll fill
4435 * them in in case something else needs them.
4437 * The eeprom structure, irq and io ports in the adapter need to have
4438 * been set before calling this function.
4440 * @host: The scsi host instance to fill in the values for.
4442 static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
4444 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4445 struct NvRamType *eeprom = &acb->eeprom;
4447 host->max_cmd_len = 24;
4448 host->can_queue = DC395x_MAX_CMD_QUEUE;
4449 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4450 host->this_id = (int)eeprom->scsi_id;
4451 host->io_port = acb->io_port_base;
4452 host->n_io_port = acb->io_port_len;
4453 host->dma_channel = -1;
4454 host->unique_id = acb->io_port_base;
4455 host->irq = acb->irq_level;
4456 host->last_reset = jiffies;
4459 if (host->max_id - 1 == eeprom->scsi_id)
4462 #ifdef CONFIG_SCSI_MULTI_LUN
4463 if (eeprom->channel_cfg & NAC_SCANLUN)
4475 * adapter_init_chip - Get the chip into a know state and figure out
4476 * some of the settings that apply to this adapter.
4478 * The io port in the adapter needs to have been set before calling
4479 * this function. The config will be configured correctly on return.
4481 * @acb: The adapter which we are to init.
4483 static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
4485 struct NvRamType *eeprom = &acb->eeprom;
4487 /* Mask all the interrupt */
4488 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
4489 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
4491 /* Reset SCSI module */
4492 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4494 /* Reset PCI/DMA module */
4495 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4498 /* program configuration 0 */
4499 acb->config = HCC_AUTOTERM | HCC_PARITY;
4500 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
4501 acb->config |= HCC_WIDE_CARD;
4503 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
4504 acb->config |= HCC_SCSI_RESET;
4506 if (acb->config & HCC_SCSI_RESET) {
4507 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
4508 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4510 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4511 /*spin_unlock_irq (&io_request_lock); */
4514 acb->scsi_host->last_reset =
4516 HZ * acb->eeprom.delay_time;
4518 /*spin_lock_irq (&io_request_lock); */
4524 * init_adapter - Grab the resource for the card, setup the adapter
4525 * information, set the card into a known state, create the various
4526 * tables etc etc. This basically gets all adapter information all up
4527 * to date, intialised and gets the chip in sync with it.
4529 * @host: This hosts adapter structure
4530 * @io_port: The base I/O port
4533 * Returns 0 if the initialization succeeds, any other value on
4536 static int __devinit adapter_init(struct AdapterCtlBlk *acb,
4537 unsigned long io_port, u32 io_port_len, unsigned int irq)
4539 if (!request_region(io_port, io_port_len, DC395X_NAME)) {
4540 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
4543 /* store port base to indicate we have registered it */
4544 acb->io_port_base = io_port;
4545 acb->io_port_len = io_port_len;
4547 if (request_irq(irq, dc395x_interrupt, SA_SHIRQ, DC395X_NAME, acb)) {
4548 /* release the region we just claimed */
4549 dprintkl(KERN_INFO, "Failed to register IRQ\n");
4552 /* store irq to indicate we have registered it */
4553 acb->irq_level = irq;
4555 /* get eeprom configuration information and command line settings etc */
4556 check_eeprom(&acb->eeprom, io_port);
4557 print_eeprom_settings(&acb->eeprom);
4559 /* setup adapter control block */
4560 adapter_init_params(acb);
4562 /* display card connectors/termination settings */
4563 adapter_print_config(acb);
4565 if (adapter_sg_tables_alloc(acb)) {
4566 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
4569 adapter_init_scsi_host(acb->scsi_host);
4570 adapter_init_chip(acb);
4571 set_basic_config(acb);
4574 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4575 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4576 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
4577 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
4582 free_irq(acb->irq_level, acb);
4583 if (acb->io_port_base)
4584 release_region(acb->io_port_base, acb->io_port_len);
4585 adapter_sg_tables_free(acb);
4592 * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4593 * stopping all operations and disabling interrupt generation on the
4596 * @acb: The adapter which we are to shutdown.
4598 static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
4600 /* disable interrupts */
4601 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
4602 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
4604 /* reset the scsi bus */
4605 if (acb->config & HCC_SCSI_RESET)
4606 reset_scsi_bus(acb);
4608 /* clear any pending interupt state */
4609 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4615 * adapter_uninit - Shut down the chip and release any resources that
4616 * we had allocated. Once this returns the adapter should not be used
4619 * @acb: The adapter which we are to un-initialize.
4621 static void adapter_uninit(struct AdapterCtlBlk *acb)
4623 unsigned long flags;
4624 DC395x_LOCK_IO(acb->scsi_host, flags);
4627 if (timer_pending(&acb->waiting_timer))
4628 del_timer(&acb->waiting_timer);
4629 if (timer_pending(&acb->selto_timer))
4630 del_timer(&acb->selto_timer);
4632 adapter_uninit_chip(acb);
4633 adapter_remove_and_free_all_devices(acb);
4634 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4637 free_irq(acb->irq_level, acb);
4638 if (acb->io_port_base)
4639 release_region(acb->io_port_base, acb->io_port_len);
4641 adapter_sg_tables_free(acb);
4646 #define SPRINTF(args...) pos += sprintf(pos, args)
4650 if (YN) SPRINTF(" Yes ");\
4651 else SPRINTF(" No ")
4653 static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
4654 char **start, off_t offset, int length, int inout)
4656 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4659 struct DeviceCtlBlk *dcb;
4660 unsigned long flags;
4663 if (inout) /* Has data been written to the file ? */
4666 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
4667 SPRINTF(" Driver Version " DC395X_VERSION "\n");
4669 DC395x_LOCK_IO(acb->scsi_host, flags);
4671 SPRINTF("SCSI Host Nr %i, ", host->host_no);
4672 SPRINTF("DC395U/UW/F DC315/U %s\n",
4673 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
4674 SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
4675 SPRINTF("irq_level 0x%04x, ", acb->irq_level);
4676 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
4678 SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
4679 SPRINTF("AdapterID %i\n", host->this_id);
4681 SPRINTF("tag_max_num %i", acb->tag_max_num);
4682 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4683 SPRINTF(", FilterCfg 0x%02x",
4684 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
4685 SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
4688 SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4690 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
4691 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
4692 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
4693 acb->dcb_map[6], acb->dcb_map[7]);
4695 (" %02x %02x %02x %02x %02x %02x %02x %02x\n",
4696 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
4697 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
4698 acb->dcb_map[14], acb->dcb_map[15]);
4701 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4704 list_for_each_entry(dcb, &acb->dcb_list, list) {
4706 SPRINTF("%02i %02i %02i ", dev, dcb->target_id,
4708 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
4709 YESNO(dcb->sync_offset);
4710 YESNO(dcb->sync_period & WIDE_SYNC);
4711 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
4712 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
4713 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
4714 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
4715 if (dcb->sync_offset)
4716 SPRINTF(" %03i ns ", nego_period);
4718 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
4720 if (dcb->sync_offset & 0x0f) {
4721 spd = 1000 / (nego_period);
4722 spd1 = 1000 % (nego_period);
4723 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
4724 SPRINTF(" %2i.%1i M %02i ", spd, spd1,
4725 (dcb->sync_offset & 0x0f));
4729 /* Add more info ... */
4730 SPRINTF(" %02i\n", dcb->max_command);
4734 if (timer_pending(&acb->waiting_timer))
4735 SPRINTF("Waiting queue timer running\n");
4739 list_for_each_entry(dcb, &acb->dcb_list, list) {
4740 struct ScsiReqBlk *srb;
4741 if (!list_empty(&dcb->srb_waiting_list))
4742 SPRINTF("DCB (%02i-%i): Waiting: %i:",
4743 dcb->target_id, dcb->target_lun,
4744 list_size(&dcb->srb_waiting_list));
4745 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
4746 SPRINTF(" %li", srb->cmd->pid);
4747 if (!list_empty(&dcb->srb_going_list))
4748 SPRINTF("\nDCB (%02i-%i): Going : %i:",
4749 dcb->target_id, dcb->target_lun,
4750 list_size(&dcb->srb_going_list));
4751 list_for_each_entry(srb, &dcb->srb_going_list, list)
4752 SPRINTF(" %li", srb->cmd->pid);
4753 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4757 if (debug_enabled(DBG_1)) {
4758 SPRINTF("DCB list for ACB %p:\n", acb);
4759 list_for_each_entry(dcb, &acb->dcb_list, list) {
4760 SPRINTF("%p -> ", dcb);
4765 *start = buffer + offset;
4766 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4768 if (pos - buffer < offset)
4770 else if (pos - buffer - offset < length)
4771 return pos - buffer - offset;
4777 static struct scsi_host_template dc395x_driver_template = {
4778 .module = THIS_MODULE,
4779 .proc_name = DC395X_NAME,
4780 .proc_info = dc395x_proc_info,
4781 .name = DC395X_BANNER " " DC395X_VERSION,
4782 .queuecommand = dc395x_queue_command,
4783 .bios_param = dc395x_bios_param,
4784 .slave_alloc = dc395x_slave_alloc,
4785 .slave_destroy = dc395x_slave_destroy,
4786 .can_queue = DC395x_MAX_CAN_QUEUE,
4788 .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
4789 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
4790 .eh_abort_handler = dc395x_eh_abort,
4791 .eh_bus_reset_handler = dc395x_eh_bus_reset,
4792 .unchecked_isa_dma = 0,
4793 .use_clustering = DISABLE_CLUSTERING,
4798 * banner_display - Display banner on first instance of driver
4801 static void banner_display(void)
4803 static int banner_done = 0;
4806 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
4813 * dc395x_init_one - Initialise a single instance of the adapter.
4815 * The PCI layer will call this once for each instance of the adapter
4816 * that it finds in the system. The pci_dev strcuture indicates which
4817 * instance we are being called from.
4819 * @dev: The PCI device to intialize.
4820 * @id: Looks like a pointer to the entry in our pci device table
4821 * that was actually matched by the PCI subsystem.
4823 * Returns 0 on success, or an error code (-ve) on failure.
4825 static int __devinit dc395x_init_one(struct pci_dev *dev,
4826 const struct pci_device_id *id)
4828 struct Scsi_Host *scsi_host = NULL;
4829 struct AdapterCtlBlk *acb = NULL;
4830 unsigned long io_port_base;
4831 unsigned int io_port_len;
4834 dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
4837 if (pci_enable_device(dev))
4839 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
4842 io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
4843 io_port_len = pci_resource_len(dev, 0);
4845 dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
4847 /* allocate scsi host information (includes out adapter) */
4848 scsi_host = scsi_host_alloc(&dc395x_driver_template,
4849 sizeof(struct AdapterCtlBlk));
4851 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
4854 acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
4855 acb->scsi_host = scsi_host;
4858 /* initialise the adapter and everything we need */
4859 if (adapter_init(acb, io_port_base, io_port_len, irq)) {
4860 dprintkl(KERN_INFO, "adapter init failed\n");
4864 pci_set_master(dev);
4866 /* get the scsi mid level to scan for new devices on the bus */
4867 if (scsi_add_host(scsi_host, &dev->dev)) {
4868 dprintkl(KERN_ERR, "scsi_add_host failed\n");
4871 pci_set_drvdata(dev, scsi_host);
4872 scsi_scan_host(scsi_host);
4878 adapter_uninit(acb);
4879 if (scsi_host != NULL)
4880 scsi_host_put(scsi_host);
4881 pci_disable_device(dev);
4887 * dc395x_remove_one - Called to remove a single instance of the
4890 * @dev: The PCI device to intialize.
4892 static void __devexit dc395x_remove_one(struct pci_dev *dev)
4894 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
4895 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
4897 dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
4899 scsi_remove_host(scsi_host);
4900 adapter_uninit(acb);
4901 pci_disable_device(dev);
4902 scsi_host_put(scsi_host);
4903 pci_set_drvdata(dev, NULL);
4907 static struct pci_device_id dc395x_pci_table[] = {
4909 .vendor = PCI_VENDOR_ID_TEKRAM,
4910 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
4911 .subvendor = PCI_ANY_ID,
4912 .subdevice = PCI_ANY_ID,
4914 {} /* Terminating entry */
4916 MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
4919 static struct pci_driver dc395x_driver = {
4920 .name = DC395X_NAME,
4921 .id_table = dc395x_pci_table,
4922 .probe = dc395x_init_one,
4923 .remove = __devexit_p(dc395x_remove_one),
4928 * dc395x_module_init - Module initialization function
4930 * Used by both module and built-in driver to initialise this driver.
4932 static int __init dc395x_module_init(void)
4934 return pci_module_init(&dc395x_driver);
4939 * dc395x_module_exit - Module cleanup function.
4941 static void __exit dc395x_module_exit(void)
4943 pci_unregister_driver(&dc395x_driver);
4947 module_init(dc395x_module_init);
4948 module_exit(dc395x_module_exit);
4950 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4951 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4952 MODULE_LICENSE("GPL");