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)
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 dma_addr_t 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 size_t total_xfer_length; /* Total number of bytes remaining to be transfered */
238 size_t request_length; /* Total number of bytes in this request */
240 * The sense buffer handling function, request_sense, uses
241 * the first hw sg entry (segment_x[0]) and the transfer
242 * length (total_xfer_length). While doing this it stores the
243 * original values into the last sg hw list
244 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
245 * total_xfer_length in xferred. These values are restored in
246 * pci_unmap_srb_sense. This is the only place xferred is used.
248 size_t xferred; /* Saved copy of total_xfer_length */
268 struct DeviceCtlBlk {
269 struct list_head list; /* next/prev ptrs for the dcb list */
270 struct AdapterCtlBlk *acb;
271 struct list_head srb_going_list; /* head of going srb list */
272 struct list_head srb_waiting_list; /* head of waiting srb list */
274 struct ScsiReqBlk *active_srb;
279 u8 target_id; /* SCSI Target ID (SCSI Only) */
280 u8 target_lun; /* SCSI Log. Unit (SCSI Only) */
284 u8 inquiry7; /* To store Inquiry flags */
285 u8 sync_mode; /* 0:async mode */
286 u8 min_nego_period; /* for nego. */
287 u8 sync_period; /* for reg. */
289 u8 sync_offset; /* for reg. and nego.(low nibble) */
295 struct AdapterCtlBlk {
296 struct Scsi_Host *scsi_host;
298 unsigned long io_port_base;
299 unsigned long io_port_len;
301 struct list_head dcb_list; /* head of going dcb list */
302 struct DeviceCtlBlk *dcb_run_robin;
303 struct DeviceCtlBlk *active_dcb;
305 struct list_head srb_free_list; /* head of free srb list */
306 struct ScsiReqBlk *tmp_srb;
307 struct timer_list waiting_timer;
308 struct timer_list selto_timer;
314 unsigned int irq_level;
324 u8 dcb_map[DC395x_MAX_SCSI_ID];
325 struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
331 struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
332 struct ScsiReqBlk srb;
334 struct NvRamType eeprom; /* eeprom settings for this adapter */
338 /*---------------------------------------------------------------------------
340 ---------------------------------------------------------------------------*/
341 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
343 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
345 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
347 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
349 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
351 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
353 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
355 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
357 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
359 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
361 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
363 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
365 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
367 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
369 static void set_basic_config(struct AdapterCtlBlk *acb);
370 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
371 struct ScsiReqBlk *srb);
372 static void reset_scsi_bus(struct AdapterCtlBlk *acb);
373 static void data_io_transfer(struct AdapterCtlBlk *acb,
374 struct ScsiReqBlk *srb, u16 io_dir);
375 static void disconnect(struct AdapterCtlBlk *acb);
376 static void reselect(struct AdapterCtlBlk *acb);
377 static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
378 struct ScsiReqBlk *srb);
379 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
380 struct ScsiReqBlk *srb);
381 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
382 struct ScsiReqBlk *srb);
383 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
384 struct scsi_cmnd *cmd, u8 force);
385 static void scsi_reset_detect(struct AdapterCtlBlk *acb);
386 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
387 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
388 struct ScsiReqBlk *srb);
389 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
390 struct ScsiReqBlk *srb);
391 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
392 struct ScsiReqBlk *srb);
393 static void set_xfer_rate(struct AdapterCtlBlk *acb,
394 struct DeviceCtlBlk *dcb);
395 static void waiting_timeout(unsigned long ptr);
398 /*---------------------------------------------------------------------------
400 ---------------------------------------------------------------------------*/
401 static u16 current_sync_offset = 0;
403 static void *dc395x_scsi_phase0[] = {
404 data_out_phase0,/* phase:0 */
405 data_in_phase0, /* phase:1 */
406 command_phase0, /* phase:2 */
407 status_phase0, /* phase:3 */
408 nop0, /* phase:4 PH_BUS_FREE .. initial phase */
409 nop0, /* phase:5 PH_BUS_FREE .. initial phase */
410 msgout_phase0, /* phase:6 */
411 msgin_phase0, /* phase:7 */
414 static void *dc395x_scsi_phase1[] = {
415 data_out_phase1,/* phase:0 */
416 data_in_phase1, /* phase:1 */
417 command_phase1, /* phase:2 */
418 status_phase1, /* phase:3 */
419 nop1, /* phase:4 PH_BUS_FREE .. initial phase */
420 nop1, /* phase:5 PH_BUS_FREE .. initial phase */
421 msgout_phase1, /* phase:6 */
422 msgin_phase1, /* phase:7 */
426 *Fast20: 000 50ns, 20.0 MHz
428 * 010 100ns, 10.0 MHz
435 *Fast40(LVDS): 000 25ns, 40.0 MHz
438 * 011 100ns, 10.0 MHz
444 /*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/
446 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
447 static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
448 static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
451 /*---------------------------------------------------------------------------
453 ---------------------------------------------------------------------------*/
455 * Module/boot parameters currently effect *all* instances of the
456 * card in the system.
460 * Command line parameters are stored in a structure below.
461 * These are the index's into the structure for the various
462 * command line options.
464 #define CFG_ADAPTER_ID 0
465 #define CFG_MAX_SPEED 1
466 #define CFG_DEV_MODE 2
467 #define CFG_ADAPTER_MODE 3
469 #define CFG_RESET_DELAY 5
471 #define CFG_NUM 6 /* number of configuration items */
475 * Value used to indicate that a command line override
476 * hasn't been used to modify the value.
478 #define CFG_PARAM_UNSET -1
482 * Hold command line parameters.
484 struct ParameterData {
485 int value; /* value of this setting */
486 int min; /* minimum value */
487 int max; /* maximum value */
488 int def; /* default value */
489 int safe; /* safe value */
491 static struct ParameterData __devinitdata cfg_data[] = {
510 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
511 NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
513 NTC_DO_PARITY_CHK | NTC_DO_SEND_START
519 #ifdef CONFIG_SCSI_MULTI_LUN
522 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
523 /*| NAC_ACTIVE_NEG*/,
524 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
530 3, /* 16 tags (??) */
544 * Safe settings. If set to zero the the BIOS/default values with
545 * command line overrides will be used. If set to 1 then safe and
546 * slow settings will be used.
548 static int use_safe_settings = 0;
549 module_param_named(safe, use_safe_settings, bool, 0);
550 MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
553 module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
554 MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
556 module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
557 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");
559 module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
560 MODULE_PARM_DESC(dev_mode, "Device mode.");
562 module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
563 MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
565 module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
566 MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
568 module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
569 MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
573 * set_safe_settings - if the use_safe_settings option is set then
574 * set all values to the safe and slow values.
576 static void __devinit set_safe_settings(void)
578 if (use_safe_settings)
582 dprintkl(KERN_INFO, "Using safe settings.\n");
583 for (i = 0; i < CFG_NUM; i++)
585 cfg_data[i].value = cfg_data[i].safe;
592 * fix_settings - reset any boot parameters which are out of range
593 * back to the default values.
595 static void __devinit fix_settings(void)
600 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
601 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
602 cfg_data[CFG_ADAPTER_ID].value,
603 cfg_data[CFG_MAX_SPEED].value,
604 cfg_data[CFG_DEV_MODE].value,
605 cfg_data[CFG_ADAPTER_MODE].value,
606 cfg_data[CFG_TAGS].value,
607 cfg_data[CFG_RESET_DELAY].value);
608 for (i = 0; i < CFG_NUM; i++)
610 if (cfg_data[i].value < cfg_data[i].min
611 || cfg_data[i].value > cfg_data[i].max)
612 cfg_data[i].value = cfg_data[i].def;
619 * Mapping from the eeprom delay index value (index into this array)
620 * to the the number of actual seconds that the delay should be for.
622 static char __devinitdata eeprom_index_to_delay_map[] =
623 { 1, 3, 5, 10, 16, 30, 60, 120 };
627 * eeprom_index_to_delay - Take the eeprom delay setting and convert it
628 * into a number of seconds.
630 * @eeprom: The eeprom structure in which we find the delay index to map.
632 static void __devinit eeprom_index_to_delay(struct NvRamType *eeprom)
634 eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
639 * delay_to_eeprom_index - Take a delay in seconds and return the
640 * closest eeprom index which will delay for at least that amount of
643 * @delay: The delay, in seconds, to find the eeprom index for.
645 static int __devinit delay_to_eeprom_index(int delay)
648 while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
655 * eeprom_override - Override the eeprom settings, in the provided
656 * eeprom structure, with values that have been set on the command
659 * @eeprom: The eeprom data to override with command line options.
661 static void __devinit eeprom_override(struct NvRamType *eeprom)
665 /* Adapter Settings */
666 if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
667 eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
669 if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
670 eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
672 if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
673 eeprom->delay_time = delay_to_eeprom_index(
674 cfg_data[CFG_RESET_DELAY].value);
676 if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
677 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
679 /* Device Settings */
680 for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
681 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
682 eeprom->target[id].cfg0 =
683 (u8)cfg_data[CFG_DEV_MODE].value;
685 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
686 eeprom->target[id].period =
687 (u8)cfg_data[CFG_MAX_SPEED].value;
693 /*---------------------------------------------------------------------------
694 ---------------------------------------------------------------------------*/
696 static unsigned int list_size(struct list_head *head)
698 unsigned int count = 0;
699 struct list_head *pos;
700 list_for_each(pos, head)
706 static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
707 struct DeviceCtlBlk *pos)
710 struct DeviceCtlBlk* next = NULL;
711 struct DeviceCtlBlk* i;
713 if (list_empty(head))
716 /* find supplied dcb and then select the next one */
717 list_for_each_entry(i, head, list)
721 } else if (i == pos) {
724 /* if no next one take the head one (ie, wraparound) */
726 list_for_each_entry(i, head, list) {
735 static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
737 if (srb->tag_number < 255) {
738 dcb->tag_mask &= ~(1 << srb->tag_number); /* free tag mask */
739 srb->tag_number = 255;
744 /* Find cmd in SRB list */
745 static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
746 struct list_head *head)
748 struct ScsiReqBlk *i;
749 list_for_each_entry(i, head, list)
756 static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb)
758 struct list_head *head = &acb->srb_free_list;
759 struct ScsiReqBlk *srb = NULL;
761 if (!list_empty(head)) {
762 srb = list_entry(head->next, struct ScsiReqBlk, list);
763 list_del(head->next);
764 dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb);
770 static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
772 dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb);
773 list_add_tail(&srb->list, &acb->srb_free_list);
777 static void srb_waiting_insert(struct DeviceCtlBlk *dcb,
778 struct ScsiReqBlk *srb)
780 dprintkdbg(DBG_0, "srb_waiting_insert: (pid#%li) <%02i-%i> srb=%p\n",
781 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
782 list_add(&srb->list, &dcb->srb_waiting_list);
786 static void srb_waiting_append(struct DeviceCtlBlk *dcb,
787 struct ScsiReqBlk *srb)
789 dprintkdbg(DBG_0, "srb_waiting_append: (pid#%li) <%02i-%i> srb=%p\n",
790 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
791 list_add_tail(&srb->list, &dcb->srb_waiting_list);
795 static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
797 dprintkdbg(DBG_0, "srb_going_append: (pid#%li) <%02i-%i> srb=%p\n",
798 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
799 list_add_tail(&srb->list, &dcb->srb_going_list);
803 static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
805 struct ScsiReqBlk *i;
806 struct ScsiReqBlk *tmp;
807 dprintkdbg(DBG_0, "srb_going_remove: (pid#%li) <%02i-%i> srb=%p\n",
808 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
810 list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list)
812 list_del(&srb->list);
818 static void srb_waiting_remove(struct DeviceCtlBlk *dcb,
819 struct ScsiReqBlk *srb)
821 struct ScsiReqBlk *i;
822 struct ScsiReqBlk *tmp;
823 dprintkdbg(DBG_0, "srb_waiting_remove: (pid#%li) <%02i-%i> srb=%p\n",
824 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
826 list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list)
828 list_del(&srb->list);
834 static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb,
835 struct ScsiReqBlk *srb)
838 "srb_going_to_waiting_move: (pid#%li) <%02i-%i> srb=%p\n",
839 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
840 list_move(&srb->list, &dcb->srb_waiting_list);
844 static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb,
845 struct ScsiReqBlk *srb)
848 "srb_waiting_to_going_move: (pid#%li) <%02i-%i> srb=%p\n",
849 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
850 list_move(&srb->list, &dcb->srb_going_list);
854 /* Sets the timer to wake us up */
855 static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
857 if (timer_pending(&acb->waiting_timer))
859 init_timer(&acb->waiting_timer);
860 acb->waiting_timer.function = waiting_timeout;
861 acb->waiting_timer.data = (unsigned long) acb;
862 if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
863 acb->waiting_timer.expires =
864 acb->scsi_host->last_reset - HZ / 2 + 1;
866 acb->waiting_timer.expires = jiffies + to + 1;
867 add_timer(&acb->waiting_timer);
871 /* Send the next command from the waiting list to the bus */
872 static void waiting_process_next(struct AdapterCtlBlk *acb)
874 struct DeviceCtlBlk *start = NULL;
875 struct DeviceCtlBlk *pos;
876 struct DeviceCtlBlk *dcb;
877 struct ScsiReqBlk *srb;
878 struct list_head *dcb_list_head = &acb->dcb_list;
881 || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
884 if (timer_pending(&acb->waiting_timer))
885 del_timer(&acb->waiting_timer);
887 if (list_empty(dcb_list_head))
891 * Find the starting dcb. Need to find it again in the list
892 * since the list may have changed since we set the ptr to it
894 list_for_each_entry(dcb, dcb_list_head, list)
895 if (dcb == acb->dcb_run_robin) {
900 /* This can happen! */
901 start = list_entry(dcb_list_head->next, typeof(*start), list);
902 acb->dcb_run_robin = start;
907 * Loop over the dcb, but we start somewhere (potentially) in
908 * the middle of the loop so we need to manully do this.
912 struct list_head *waiting_list_head = &pos->srb_waiting_list;
914 /* Make sure, the next another device gets scheduled ... */
915 acb->dcb_run_robin = dcb_get_next(dcb_list_head,
918 if (list_empty(waiting_list_head) ||
919 pos->max_command <= list_size(&pos->srb_going_list)) {
920 /* move to next dcb */
921 pos = dcb_get_next(dcb_list_head, pos);
923 srb = list_entry(waiting_list_head->next,
924 struct ScsiReqBlk, list);
926 /* Try to send to the bus */
927 if (!start_scsi(acb, pos, srb))
928 srb_waiting_to_going_move(pos, srb);
930 waiting_set_timer(acb, HZ/50);
933 } while (pos != start);
937 /* Wake up waiting queue */
938 static void waiting_timeout(unsigned long ptr)
941 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
943 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
944 DC395x_LOCK_IO(acb->scsi_host, flags);
945 waiting_process_next(acb);
946 DC395x_UNLOCK_IO(acb->scsi_host, flags);
950 /* Get the DCB for a given ID/LUN combination */
951 static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
953 return acb->children[id][lun];
957 /* Send SCSI Request Block (srb) to adapter (acb) */
958 static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
960 struct DeviceCtlBlk *dcb = srb->dcb;
962 if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
964 (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
965 srb_waiting_append(dcb, srb);
966 waiting_process_next(acb);
970 if (!start_scsi(acb, dcb, srb))
971 srb_going_append(dcb, srb);
973 srb_waiting_insert(dcb, srb);
974 waiting_set_timer(acb, HZ / 50);
978 /* Prepare SRB for being sent to Device DCB w/ command *cmd */
979 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
980 struct ScsiReqBlk *srb)
982 enum dma_data_direction dir = cmd->sc_data_direction;
983 dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n",
984 cmd->pid, dcb->target_id, dcb->target_lun);
989 srb->total_xfer_length = 0;
990 srb->sg_bus_addr = 0;
992 srb->adapter_status = 0;
993 srb->target_status = 0;
998 srb->retry_count = 0;
999 srb->tag_number = TAG_NONE;
1000 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1001 srb->end_message = 0;
1003 if (dir == PCI_DMA_NONE || !cmd->request_buffer) {
1005 "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
1006 cmd->bufflen, cmd->request_buffer,
1007 cmd->use_sg, srb->segment_x[0].address);
1008 } else if (cmd->use_sg) {
1010 u32 reqlen = cmd->request_bufflen;
1011 struct scatterlist *sl = (struct scatterlist *)
1012 cmd->request_buffer;
1013 struct SGentry *sgp = srb->segment_x;
1014 srb->sg_count = pci_map_sg(dcb->acb->dev, sl, cmd->use_sg,
1017 "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
1018 reqlen, cmd->request_buffer, cmd->use_sg,
1021 for (i = 0; i < srb->sg_count; i++) {
1022 u32 busaddr = (u32)sg_dma_address(&sl[i]);
1023 u32 seglen = (u32)sl[i].length;
1024 sgp[i].address = busaddr;
1025 sgp[i].length = seglen;
1026 srb->total_xfer_length += seglen;
1028 sgp += srb->sg_count - 1;
1031 * adjust last page if too big as it is allocated
1032 * on even page boundaries
1034 if (srb->total_xfer_length > reqlen) {
1035 sgp->length -= (srb->total_xfer_length - reqlen);
1036 srb->total_xfer_length = reqlen;
1039 /* Fixup for WIDE padding - make sure length is even */
1040 if (dcb->sync_period & WIDE_SYNC &&
1041 srb->total_xfer_length % 2) {
1042 srb->total_xfer_length++;
1046 srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
1051 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
1052 srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
1054 srb->total_xfer_length = cmd->request_bufflen;
1056 srb->segment_x[0].address =
1057 pci_map_single(dcb->acb->dev, cmd->request_buffer,
1058 srb->total_xfer_length, dir);
1060 /* Fixup for WIDE padding - make sure length is even */
1061 if (dcb->sync_period & WIDE_SYNC && srb->total_xfer_length % 2)
1062 srb->total_xfer_length++;
1064 srb->segment_x[0].length = srb->total_xfer_length;
1067 "build_srb: [1] len=%d buf=%p use_sg=%d map=%08x\n",
1068 srb->total_xfer_length, cmd->request_buffer,
1069 cmd->use_sg, srb->segment_x[0].address);
1072 srb->request_length = srb->total_xfer_length;
1077 * dc395x_queue_command - queue scsi command passed from the mid
1078 * layer, invoke 'done' on completion
1080 * @cmd: pointer to scsi command object
1081 * @done: function pointer to be invoked on completion
1083 * Returns 1 if the adapter (host) is busy, else returns 0. One
1084 * reason for an adapter to be busy is that the number
1085 * of outstanding queued commands is already equal to
1086 * struct Scsi_Host::can_queue .
1088 * Required: if struct Scsi_Host::can_queue is ever non-zero
1089 * then this function is required.
1091 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1092 * and is expected to be held on return.
1095 static int dc395x_queue_command(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1097 struct DeviceCtlBlk *dcb;
1098 struct ScsiReqBlk *srb;
1099 struct AdapterCtlBlk *acb =
1100 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1101 dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n",
1102 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
1104 /* Assume BAD_TARGET; will be cleared later */
1105 cmd->result = DID_BAD_TARGET << 16;
1107 /* ignore invalid targets */
1108 if (cmd->device->id >= acb->scsi_host->max_id ||
1109 cmd->device->lun >= acb->scsi_host->max_lun ||
1110 cmd->device->lun >31) {
1114 /* does the specified lun on the specified device exist */
1115 if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
1116 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
1117 cmd->device->id, cmd->device->lun);
1121 /* do we have a DCB for the device */
1122 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1124 /* should never happen */
1125 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
1126 cmd->device->id, cmd->device->lun);
1130 /* set callback and clear result in the command */
1131 cmd->scsi_done = done;
1134 srb = srb_get_free(acb);
1138 * Return 1 since we are unable to queue this command at this
1141 dprintkdbg(DBG_0, "queue_command: No free srb's\n");
1145 build_srb(cmd, dcb, srb);
1147 if (!list_empty(&dcb->srb_waiting_list)) {
1148 /* append to waiting queue */
1149 srb_waiting_append(dcb, srb);
1150 waiting_process_next(acb);
1152 /* process immediately */
1155 dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->pid);
1160 * Complete the command immediatey, and then return 0 to
1161 * indicate that we have handled the command. This is usually
1162 * done when the commad is for things like non existent
1171 * Return the disk geometry for the given SCSI device.
1173 static int dc395x_bios_param(struct scsi_device *sdev,
1174 struct block_device *bdev, sector_t capacity, int *info)
1176 #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
1177 int heads, sectors, cylinders;
1178 struct AdapterCtlBlk *acb;
1179 int size = capacity;
1181 dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
1182 acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
1185 cylinders = size / (heads * sectors);
1187 if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
1190 cylinders = size / (heads * sectors);
1194 geom[2] = cylinders;
1197 return scsicam_bios_param(bdev, capacity, info);
1202 static void dump_register_info(struct AdapterCtlBlk *acb,
1203 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
1206 struct pci_dev *dev = acb->dev;
1207 pci_read_config_word(dev, PCI_STATUS, &pstat);
1209 dcb = acb->active_dcb;
1211 srb = dcb->active_srb;
1214 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
1217 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) "
1218 "cmnd=0x%02x <%02i-%i>\n",
1219 srb, srb->cmd, srb->cmd->pid,
1220 srb->cmd->cmnd[0], srb->cmd->device->id,
1221 srb->cmd->device->lun);
1222 printk(" sglist=%p cnt=%i idx=%i len=%i\n",
1223 srb->segment_x, srb->sg_count, srb->sg_index,
1224 srb->total_xfer_length);
1225 printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1226 srb->state, srb->status, srb->scsi_phase,
1227 (acb->active_dcb) ? "" : "not");
1229 dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1230 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1231 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1232 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1233 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1234 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1235 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1236 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
1237 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1238 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1239 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1240 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
1241 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1242 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1243 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1244 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1245 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1246 dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1247 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1248 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1249 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1250 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1251 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1252 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1253 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1254 DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
1255 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1256 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1257 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1258 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1259 dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1260 "pci{status=0x%04x}\n",
1261 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1262 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1263 DC395x_read8(acb, TRM_S1040_GEN_TIMER),
1268 static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1270 #if debug_enabled(DBG_FIFO)
1271 u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1272 u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1273 if (!(fifocnt & 0x40))
1274 dprintkdbg(DBG_FIFO,
1275 "clear_fifo: (%i bytes) on phase %02x in %s\n",
1276 fifocnt & 0x3f, lines, txt);
1278 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1282 static void reset_dev_param(struct AdapterCtlBlk *acb)
1284 struct DeviceCtlBlk *dcb;
1285 struct NvRamType *eeprom = &acb->eeprom;
1286 dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
1288 list_for_each_entry(dcb, &acb->dcb_list, list) {
1291 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1292 dcb->sync_period = 0;
1293 dcb->sync_offset = 0;
1295 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1296 period_index = eeprom->target[dcb->target_id].period & 0x07;
1297 dcb->min_nego_period = clock_period[period_index];
1298 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1299 || !(acb->config & HCC_WIDE_CARD))
1300 dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1306 * perform a hard reset on the SCSI bus
1307 * @cmd - some command for this host (for fetching hooks)
1308 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1310 static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1312 struct AdapterCtlBlk *acb =
1313 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1315 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n",
1316 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1318 if (timer_pending(&acb->waiting_timer))
1319 del_timer(&acb->waiting_timer);
1324 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1325 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1326 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1327 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1329 reset_scsi_bus(acb);
1332 /* We may be in serious trouble. Wait some seconds */
1333 acb->scsi_host->last_reset =
1334 jiffies + 3 * HZ / 2 +
1335 HZ * acb->eeprom.delay_time;
1338 * re-enable interrupt
1340 /* Clear SCSI FIFO */
1341 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1342 clear_fifo(acb, "eh_bus_reset");
1343 /* Delete pending IRQ */
1344 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1345 set_basic_config(acb);
1347 reset_dev_param(acb);
1348 doing_srb_done(acb, DID_RESET, cmd, 0);
1349 acb->active_dcb = NULL;
1350 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1351 waiting_process_next(acb);
1356 static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1360 spin_lock_irq(cmd->device->host->host_lock);
1361 rc = __dc395x_eh_bus_reset(cmd);
1362 spin_unlock_irq(cmd->device->host->host_lock);
1368 * abort an errant SCSI command
1369 * @cmd - command to be aborted
1370 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1372 static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1375 * Look into our command queues: If it has not been sent already,
1376 * we remove it and return success. Otherwise fail.
1378 struct AdapterCtlBlk *acb =
1379 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1380 struct DeviceCtlBlk *dcb;
1381 struct ScsiReqBlk *srb;
1382 dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
1383 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1385 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1387 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1391 srb = find_cmd(cmd, &dcb->srb_waiting_list);
1393 srb_waiting_remove(dcb, srb);
1394 pci_unmap_srb_sense(acb, srb);
1395 pci_unmap_srb(acb, srb);
1397 srb_free_insert(acb, srb);
1398 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1399 cmd->result = DID_ABORT << 16;
1402 srb = find_cmd(cmd, &dcb->srb_going_list);
1404 dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
1405 /* XXX: Should abort the command here */
1407 dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
1414 static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1415 struct ScsiReqBlk *srb)
1417 u8 *ptr = srb->msgout_buf + srb->msg_count;
1418 if (srb->msg_count > 1) {
1420 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1421 srb->msg_count, srb->msgout_buf[0],
1422 srb->msgout_buf[1]);
1425 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1426 dcb->sync_offset = 0;
1427 dcb->min_nego_period = 200 >> 2;
1428 } else if (dcb->sync_offset == 0)
1429 dcb->sync_offset = SYNC_NEGO_OFFSET;
1431 *ptr++ = MSG_EXTENDED; /* (01h) */
1432 *ptr++ = 3; /* length */
1433 *ptr++ = EXTENDED_SDTR; /* (01h) */
1434 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */
1435 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */
1436 srb->msg_count += 5;
1437 srb->state |= SRB_DO_SYNC_NEGO;
1442 static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1443 struct ScsiReqBlk *srb)
1445 u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1446 (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1447 u8 *ptr = srb->msgout_buf + srb->msg_count;
1448 if (srb->msg_count > 1) {
1450 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1451 srb->msg_count, srb->msgout_buf[0],
1452 srb->msgout_buf[1]);
1455 *ptr++ = MSG_EXTENDED; /* (01h) */
1456 *ptr++ = 2; /* length */
1457 *ptr++ = EXTENDED_WDTR; /* (03h) */
1459 srb->msg_count += 4;
1460 srb->state |= SRB_DO_WIDE_NEGO;
1465 /* Timer to work around chip flaw: When selecting and the bus is
1466 * busy, we sometimes miss a Selection timeout IRQ */
1467 void selection_timeout_missed(unsigned long ptr);
1468 /* Sets the timer to wake us up */
1469 static void selto_timer(struct AdapterCtlBlk *acb)
1471 if (timer_pending(&acb->selto_timer))
1473 acb->selto_timer.function = selection_timeout_missed;
1474 acb->selto_timer.data = (unsigned long) acb;
1476 (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
1477 acb->selto_timer.expires =
1478 acb->scsi_host->last_reset + HZ / 2 + 1;
1480 acb->selto_timer.expires = jiffies + HZ + 1;
1481 add_timer(&acb->selto_timer);
1485 void selection_timeout_missed(unsigned long ptr)
1487 unsigned long flags;
1488 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
1489 struct ScsiReqBlk *srb;
1490 dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1491 if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1492 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1495 DC395x_LOCK_IO(acb->scsi_host, flags);
1496 srb = acb->active_dcb->active_srb;
1498 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1503 static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1504 struct ScsiReqBlk* srb)
1506 u16 s_stat2, return_code;
1507 u8 s_stat, scsicommand, i, identify_message;
1509 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
1510 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
1512 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
1514 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1516 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1518 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1519 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
1520 srb->cmd->pid, s_stat, s_stat2);
1524 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1525 * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed!
1526 * (This is likely to be a bug in the hardware. Obviously, most people
1527 * only have one initiator per SCSI bus.)
1528 * Instead let this fail and have the timer make sure the command is
1529 * tried again after a short time
1531 /*selto_timer (acb); */
1535 if (acb->active_dcb) {
1536 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
1537 "command while another command (pid#%li) is active.",
1539 acb->active_dcb->active_srb ?
1540 acb->active_dcb->active_srb->cmd->pid : 0);
1543 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1544 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
1548 /* Allow starting of SCSI commands half a second before we allow the mid-level
1549 * to queue them again after a reset */
1550 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1551 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1556 clear_fifo(acb, "start_scsi");
1557 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1558 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1559 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1560 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1561 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1563 identify_message = dcb->identify_msg;
1564 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1565 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1566 if (srb->flag & AUTO_REQSENSE)
1567 identify_message &= 0xBF;
1569 if (((srb->cmd->cmnd[0] == INQUIRY)
1570 || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1571 || (srb->flag & AUTO_REQSENSE))
1572 && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1573 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1574 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1575 && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1576 && (dcb->target_lun == 0)) {
1577 srb->msgout_buf[0] = identify_message;
1579 scsicommand = SCMD_SEL_ATNSTOP;
1580 srb->state = SRB_MSGOUT;
1582 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1583 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1584 build_wdtr(acb, dcb, srb);
1588 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1589 && dcb->inquiry7 & SCSI_INQ_SYNC) {
1590 build_sdtr(acb, dcb, srb);
1593 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1594 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1595 build_wdtr(acb, dcb, srb);
1600 /* Send identify message */
1601 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1603 scsicommand = SCMD_SEL_ATN;
1604 srb->state = SRB_START_;
1605 #ifndef DC395x_NO_TAGQ
1606 if ((dcb->sync_mode & EN_TAG_QUEUEING)
1607 && (identify_message & 0xC0)) {
1608 /* Send Tag message */
1611 while (tag_mask & dcb->tag_mask
1612 && tag_number <= dcb->max_command) {
1613 tag_mask = tag_mask << 1;
1616 if (tag_number >= dcb->max_command) {
1617 dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
1618 "Out of tags target=<%02i-%i>)\n",
1619 srb->cmd->pid, srb->cmd->device->id,
1620 srb->cmd->device->lun);
1621 srb->state = SRB_READY;
1622 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1627 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1628 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1629 dcb->tag_mask |= tag_mask;
1630 srb->tag_number = tag_number;
1631 scsicommand = SCMD_SEL_ATN3;
1632 srb->state = SRB_START_;
1636 /* Send CDB ..command block ......... */
1637 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
1638 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
1639 srb->cmd->cmnd[0], srb->tag_number);
1640 if (srb->flag & AUTO_REQSENSE) {
1641 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1642 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1643 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1644 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1645 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1646 sizeof(srb->cmd->sense_buffer));
1647 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1649 ptr = (u8 *)srb->cmd->cmnd;
1650 for (i = 0; i < srb->cmd->cmd_len; i++)
1651 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1654 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1655 DO_HWRESELECT | DO_DATALATCH);
1656 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1658 * If start_scsi return 1:
1659 * we caught an interrupt (must be reset or reselection ... )
1660 * : Let's process it first!
1662 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
1663 srb->cmd->pid, dcb->target_id, dcb->target_lun);
1664 srb->state = SRB_READY;
1668 /* This IRQ should NOT get lost, as we did not acknowledge it */
1671 * If start_scsi returns 0:
1672 * we know that the SCSI processor is free
1674 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1675 dcb->active_srb = srb;
1676 acb->active_dcb = dcb;
1678 /* it's important for atn stop */
1679 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1680 DO_DATALATCH | DO_HWRESELECT);
1682 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1688 #define DC395x_ENABLE_MSGOUT \
1689 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1690 srb->state |= SRB_MSGOUT
1694 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1695 struct ScsiReqBlk *srb)
1697 srb->msgout_buf[0] = ABORT;
1699 DC395x_ENABLE_MSGOUT;
1700 srb->state &= ~SRB_MSGIN;
1701 srb->state |= SRB_MSGOUT;
1706 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1707 * have been triggered for this card.
1709 * @acb: a pointer to the adpter control block
1710 * @scsi_status: the status return when we checked the card
1712 static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1715 struct DeviceCtlBlk *dcb;
1716 struct ScsiReqBlk *srb;
1719 unsigned long flags;
1720 void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
1723 DC395x_LOCK_IO(acb->scsi_host, flags);
1725 /* This acknowledges the IRQ */
1726 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1727 if ((scsi_status & 0x2007) == 0x2002)
1728 dprintkl(KERN_DEBUG,
1729 "COP after COP completed? %04x\n", scsi_status);
1730 if (debug_enabled(DBG_KG)) {
1731 if (scsi_intstatus & INT_SELTIMEOUT)
1732 dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1734 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1736 if (timer_pending(&acb->selto_timer))
1737 del_timer(&acb->selto_timer);
1739 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1740 disconnect(acb); /* bus free interrupt */
1743 if (scsi_intstatus & INT_RESELECTED) {
1747 if (scsi_intstatus & INT_SELECT) {
1748 dprintkl(KERN_INFO, "Host does not support target mode!\n");
1751 if (scsi_intstatus & INT_SCSIRESET) {
1752 scsi_reset_detect(acb);
1755 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1756 dcb = acb->active_dcb;
1758 dprintkl(KERN_DEBUG,
1759 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1760 scsi_status, scsi_intstatus);
1763 srb = dcb->active_srb;
1764 if (dcb->flag & ABORT_DEV_) {
1765 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1766 enable_msgout_abort(acb, srb);
1769 /* software sequential machine */
1770 phase = (u16)srb->scsi_phase;
1774 * call dc395x_scsi_phase0[]... "phase entry"
1775 * handle every phase before start transfer
1777 /* data_out_phase0, phase:0 */
1778 /* data_in_phase0, phase:1 */
1779 /* command_phase0, phase:2 */
1780 /* status_phase0, phase:3 */
1781 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
1782 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
1783 /* msgout_phase0, phase:6 */
1784 /* msgin_phase0, phase:7 */
1785 dc395x_statev = dc395x_scsi_phase0[phase];
1786 dc395x_statev(acb, srb, &scsi_status);
1789 * if there were any exception occured scsi_status
1790 * will be modify to bus free phase new scsi_status
1791 * transfer out from ... previous dc395x_statev
1793 srb->scsi_phase = scsi_status & PHASEMASK;
1794 phase = (u16)scsi_status & PHASEMASK;
1797 * call dc395x_scsi_phase1[]... "phase entry" handle
1798 * every phase to do transfer
1800 /* data_out_phase1, phase:0 */
1801 /* data_in_phase1, phase:1 */
1802 /* command_phase1, phase:2 */
1803 /* status_phase1, phase:3 */
1804 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
1805 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
1806 /* msgout_phase1, phase:6 */
1807 /* msgin_phase1, phase:7 */
1808 dc395x_statev = dc395x_scsi_phase1[phase];
1809 dc395x_statev(acb, srb, &scsi_status);
1812 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1816 static irqreturn_t dc395x_interrupt(int irq, void *dev_id,
1817 struct pt_regs *regs)
1819 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)dev_id;
1822 irqreturn_t handled = IRQ_NONE;
1825 * Check for pending interupt
1827 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1828 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1829 if (scsi_status & SCSIINTERRUPT) {
1830 /* interupt pending - let's process it! */
1831 dc395x_handle_interrupt(acb, scsi_status);
1832 handled = IRQ_HANDLED;
1834 else if (dma_status & 0x20) {
1835 /* Error from the DMA engine */
1836 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1838 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
1839 if (acb->active_dcb) {
1840 acb->active_dcb-> flag |= ABORT_DEV_;
1841 if (acb->active_dcb->active_srb)
1842 enable_msgout_abort(acb, acb->active_dcb->active_srb);
1844 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
1846 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
1849 handled = IRQ_HANDLED;
1856 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1859 dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->pid);
1860 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
1861 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
1863 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1864 srb->state &= ~SRB_MSGOUT;
1868 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1873 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->pid);
1875 clear_fifo(acb, "msgout_phase1");
1876 if (!(srb->state & SRB_MSGOUT)) {
1877 srb->state |= SRB_MSGOUT;
1878 dprintkl(KERN_DEBUG,
1879 "msgout_phase1: (pid#%li) Phase unexpected\n",
1880 srb->cmd->pid); /* So what ? */
1882 if (!srb->msg_count) {
1883 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
1885 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
1886 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1887 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1890 ptr = (u8 *)srb->msgout_buf;
1891 for (i = 0; i < srb->msg_count; i++)
1892 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1894 if (srb->msgout_buf[0] == MSG_ABORT)
1895 srb->state = SRB_ABORT_SENT;
1897 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1901 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1904 dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->pid);
1905 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1909 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1912 struct DeviceCtlBlk *dcb;
1915 dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->pid);
1917 clear_fifo(acb, "command_phase1");
1918 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
1919 if (!(srb->flag & AUTO_REQSENSE)) {
1920 ptr = (u8 *)srb->cmd->cmnd;
1921 for (i = 0; i < srb->cmd->cmd_len; i++) {
1922 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
1926 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1927 dcb = acb->active_dcb;
1929 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1930 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1931 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1932 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1933 sizeof(srb->cmd->sense_buffer));
1934 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1936 srb->state |= SRB_COMMAND;
1937 /* it's important for atn stop */
1938 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1940 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1945 * Verify that the remaining space in the hw sg lists is the same as
1946 * the count of remaining bytes in srb->total_xfer_length
1948 static void sg_verify_length(struct ScsiReqBlk *srb)
1950 if (debug_enabled(DBG_SG)) {
1952 unsigned idx = srb->sg_index;
1953 struct SGentry *psge = srb->segment_x + idx;
1954 for (; idx < srb->sg_count; psge++, idx++)
1955 len += psge->length;
1956 if (len != srb->total_xfer_length)
1958 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1959 srb->total_xfer_length, len);
1965 * Compute the next Scatter Gather list index and adjust its length
1966 * and address if necessary
1968 static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
1971 u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
1972 struct SGentry *psge = srb->segment_x + srb->sg_index;
1975 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
1976 xferred, srb->total_xfer_length, left);
1978 /* nothing to update since we did not transfer any data */
1982 sg_verify_length(srb);
1983 srb->total_xfer_length = left; /* update remaining count */
1984 for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
1985 if (xferred >= psge->length) {
1986 /* Complete SG entries done */
1987 xferred -= psge->length;
1989 /* Partial SG entry done */
1990 psge->length -= xferred;
1991 psge->address += xferred;
1992 srb->sg_index = idx;
1993 pci_dma_sync_single_for_device(srb->dcb->
2002 sg_verify_length(srb);
2007 * We have transfered a single byte (PIO mode?) and need to update
2008 * the count of bytes remaining (total_xfer_length) and update the sg
2009 * entry to either point to next byte in the current sg entry, or of
2010 * already at the end to point to the start of the next sg entry
2012 static void sg_subtract_one(struct ScsiReqBlk *srb)
2014 sg_update_list(srb, srb->total_xfer_length - 1);
2019 * cleanup_after_transfer
2021 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
2022 * KG: Currently called from StatusPhase1 ()
2023 * Should probably also be called from other places
2024 * Best might be to call it in DataXXPhase0, if new phase will differ
2026 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
2027 struct ScsiReqBlk *srb)
2029 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
2030 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
2031 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2032 clear_fifo(acb, "cleanup/in");
2033 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2034 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2035 } else { /* write */
2036 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2037 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2038 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2039 clear_fifo(acb, "cleanup/out");
2041 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2046 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
2047 * Seems to be needed for unknown reasons; could be a hardware bug :-(
2049 #define DC395x_LASTPIO 4
2052 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2055 struct DeviceCtlBlk *dcb = srb->dcb;
2056 u16 scsi_status = *pscsi_status;
2057 u32 d_left_counter = 0;
2058 dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
2059 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2062 * KG: We need to drain the buffers before we draw any conclusions!
2063 * This means telling the DMA to push the rest into SCSI, telling
2064 * SCSI to push the rest to the bus.
2065 * However, the device might have been the one to stop us (phase
2066 * change), and the data in transit just needs to be accounted so
2067 * it can be retransmitted.)
2070 * KG: Stop DMA engine pushing more data into the SCSI FIFO
2071 * If we need more data, the DMA SG list will be freshly set up, anyway
2073 dprintkdbg(DBG_PIO, "data_out_phase0: "
2074 "DMA{fifocnt=0x%02x fifostat=0x%02x} "
2075 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
2076 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2077 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2078 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2079 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2080 srb->total_xfer_length);
2081 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2083 if (!(srb->state & SRB_XFERPAD)) {
2084 if (scsi_status & PARITYERROR)
2085 srb->status |= PARITY_ERROR;
2088 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2089 * is the no of bytes it got from the DMA engine not the no it
2090 * transferred successfully to the device. (And the difference could
2091 * be as much as the FIFO size, I guess ...)
2093 if (!(scsi_status & SCSIXFERDONE)) {
2095 * when data transfer from DMA FIFO to SCSI FIFO
2096 * if there was some data left in SCSI FIFO
2099 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2101 if (dcb->sync_period & WIDE_SYNC)
2102 d_left_counter <<= 1;
2104 dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
2105 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
2106 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
2107 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2108 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2109 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2110 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2111 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2112 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2113 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2116 * calculate all the residue data that not yet tranfered
2117 * SCSI transfer counter + left in SCSI FIFO data
2119 * .....TRM_S1040_SCSI_COUNTER (24bits)
2120 * The counter always decrement by one for every SCSI byte transfer.
2121 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2122 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2124 if (srb->total_xfer_length > DC395x_LASTPIO)
2126 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2128 /* Is this a good idea? */
2129 /*clear_fifo(acb, "DOP1"); */
2130 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2131 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2132 && srb->cmd->request_bufflen % 2) {
2135 "data_out_phase0: Discard 1 byte (0x%02x)\n",
2139 * KG: Oops again. Same thinko as above: The SCSI might have been
2140 * faster than the DMA engine, so that it ran out of data.
2141 * In that case, we have to do just nothing!
2142 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2145 * KG: This is nonsense: We have been WRITING data to the bus
2146 * If the SCSI engine has no bytes left, how should the DMA engine?
2148 if (d_left_counter == 0) {
2149 srb->total_xfer_length = 0;
2152 * if transfer not yet complete
2153 * there were some data residue in SCSI FIFO or
2154 * SCSI transfer counter not empty
2157 srb->total_xfer_length - d_left_counter;
2159 (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2160 sg_update_list(srb, d_left_counter);
2161 /* KG: Most ugly hack! Apparently, this works around a chip bug */
2162 if ((srb->segment_x[srb->sg_index].length ==
2163 diff && srb->cmd->use_sg)
2164 || ((oldxferred & ~PAGE_MASK) ==
2167 dprintkl(KERN_INFO, "data_out_phase0: "
2168 "Work around chip bug (%i)?\n", diff);
2170 srb->total_xfer_length - diff;
2171 sg_update_list(srb, d_left_counter);
2172 /*srb->total_xfer_length -= diff; */
2173 /*srb->virt_addr += diff; */
2174 /*if (srb->cmd->use_sg) */
2175 /* srb->sg_index++; */
2179 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2180 cleanup_after_transfer(acb, srb);
2185 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2188 dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
2189 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2190 clear_fifo(acb, "data_out_phase1");
2191 /* do prepare before transfer when data out phase */
2192 data_io_transfer(acb, srb, XFERDATAOUT);
2195 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2198 u16 scsi_status = *pscsi_status;
2200 dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
2201 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2204 * KG: DataIn is much more tricky than DataOut. When the device is finished
2205 * and switches to another phase, the SCSI engine should be finished too.
2206 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2207 * engine and transferred to memory.
2208 * We should wait for the FIFOs to be emptied by that (is there any way to
2209 * enforce this?) and then stop the DMA engine, because it might think, that
2210 * there are more bytes to follow. Yes, the device might disconnect prior to
2211 * having all bytes transferred!
2212 * Also we should make sure that all data from the DMA engine buffer's really
2213 * made its way to the system memory! Some documentation on this would not
2214 * seem to be a bad idea, actually.
2216 if (!(srb->state & SRB_XFERPAD)) {
2218 unsigned int sc, fc;
2220 if (scsi_status & PARITYERROR) {
2221 dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
2222 "Parity Error\n", srb->cmd->pid);
2223 srb->status |= PARITY_ERROR;
2226 * KG: We should wait for the DMA FIFO to be empty ...
2227 * but: it would be better to wait first for the SCSI FIFO and then the
2228 * the DMA FIFO to become empty? How do we know, that the device not already
2229 * sent data to the FIFO in a MsgIn phase, eg.?
2231 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2234 dprintkl(KERN_DEBUG,
2235 "DIP0: Wait for DMA FIFO to flush ...\n");
2236 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2237 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2238 /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2240 (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
2242 if (ctr < 6000000 - 1)
2244 "DIP0: Had to wait for DMA ...\n");
2247 "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2248 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2250 dprintkdbg(DBG_KG, "data_in_phase0: "
2251 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2252 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2253 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2255 /* Now: Check remainig data: The SCSI counters should tell us ... */
2256 sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2257 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2258 d_left_counter = sc + ((fc & 0x1f)
2259 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2261 dprintkdbg(DBG_KG, "data_in_phase0: "
2262 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2263 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2264 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2266 (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2269 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2270 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
2271 srb->total_xfer_length, d_left_counter);
2273 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2275 && srb->total_xfer_length <= DC395x_LASTPIO) {
2276 size_t left_io = srb->total_xfer_length;
2278 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2279 /*sg_update_list (srb, d_left_counter); */
2280 dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
2281 "for remaining %i bytes:",
2283 (srb->dcb->sync_period & WIDE_SYNC) ?
2285 srb->total_xfer_length);
2286 if (srb->dcb->sync_period & WIDE_SYNC)
2287 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2290 unsigned char *virt, *base = NULL;
2291 unsigned long flags = 0;
2292 size_t len = left_io;
2294 if (srb->cmd->use_sg) {
2295 size_t offset = srb->request_length - left_io;
2296 local_irq_save(flags);
2297 /* Assumption: it's inside one page as it's at most 4 bytes and
2298 I just assume it's on a 4-byte boundary */
2299 base = scsi_kmap_atomic_sg((struct scatterlist *)srb->cmd->request_buffer,
2300 srb->sg_count, &offset, &len);
2301 virt = base + offset;
2303 virt = srb->cmd->request_buffer + srb->cmd->request_bufflen - left_io;
2310 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2313 if (debug_enabled(DBG_PIO))
2314 printk(" %02x", byte);
2317 sg_subtract_one(srb);
2321 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2329 WARN_ON((fc != 0x40) == !d_left_counter);
2331 if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
2332 /* Read the last byte ... */
2333 if (srb->total_xfer_length > 0) {
2334 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2337 srb->total_xfer_length--;
2338 if (debug_enabled(DBG_PIO))
2339 printk(" %02x", byte);
2342 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2345 if (srb->cmd->use_sg) {
2346 scsi_kunmap_atomic_sg(base);
2347 local_irq_restore(flags);
2350 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2351 /*srb->total_xfer_length = 0; */
2352 if (debug_enabled(DBG_PIO))
2355 #endif /* DC395x_LASTPIO */
2359 * KG: This was in DATAOUT. Does it also belong here?
2360 * Nobody seems to know what counter and fifo_cnt count exactly ...
2362 if (!(scsi_status & SCSIXFERDONE)) {
2364 * when data transfer from DMA FIFO to SCSI FIFO
2365 * if there was some data left in SCSI FIFO
2368 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2370 if (srb->dcb->sync_period & WIDE_SYNC)
2371 d_left_counter <<= 1;
2373 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2375 * KG: Seems to be correct ...
2379 /* KG: This should not be needed any more! */
2380 if (d_left_counter == 0
2381 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2387 DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2388 } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
2391 "Deadlock in DataInPhase0 waiting for DMA!!\n");
2392 srb->total_xfer_length = 0;
2394 srb->total_xfer_length = d_left_counter;
2395 } else { /* phase changed */
2398 * when a transfer not yet complete
2399 * but be disconnected by target
2400 * if transfer not yet complete
2401 * there were some data residue in SCSI FIFO or
2402 * SCSI transfer counter not empty
2404 sg_update_list(srb, d_left_counter);
2407 /* KG: The target may decide to disconnect: Empty FIFO before! */
2408 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2409 cleanup_after_transfer(acb, srb);
2414 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2417 dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
2418 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2419 data_io_transfer(acb, srb, XFERDATAIN);
2423 static void data_io_transfer(struct AdapterCtlBlk *acb,
2424 struct ScsiReqBlk *srb, u16 io_dir)
2426 struct DeviceCtlBlk *dcb = srb->dcb;
2429 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
2430 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
2431 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
2432 srb->total_xfer_length, srb->sg_index, srb->sg_count);
2433 if (srb == acb->tmp_srb)
2434 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
2435 if (srb->sg_index >= srb->sg_count) {
2436 /* can't happen? out of bounds error */
2440 if (srb->total_xfer_length > DC395x_LASTPIO) {
2441 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2443 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2444 * Maybe, even ABORTXFER would be appropriate
2446 if (dma_status & XFERPENDING) {
2447 dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
2448 "Expect trouble!\n");
2449 dump_register_info(acb, dcb, srb);
2450 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2452 /* clear_fifo(acb, "IO"); */
2454 * load what physical address of Scatter/Gather list table
2455 * want to be transfer
2457 srb->state |= SRB_DATA_XFER;
2458 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2459 if (srb->cmd->use_sg) { /* with S/G */
2460 io_dir |= DMACMD_SG;
2461 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2463 sizeof(struct SGentry) *
2465 /* load how many bytes in the sg list table */
2466 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2467 ((u32)(srb->sg_count -
2468 srb->sg_index) << 3));
2469 } else { /* without S/G */
2470 io_dir &= ~DMACMD_SG;
2471 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2472 srb->segment_x[0].address);
2473 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2474 srb->segment_x[0].length);
2476 /* load total transfer length (24bits) max value 16Mbyte */
2477 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2478 srb->total_xfer_length);
2479 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2480 if (io_dir & DMACMD_DIR) { /* read */
2481 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2483 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2485 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2486 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2492 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
2494 * load what physical address of Scatter/Gather list table
2495 * want to be transfer
2497 srb->state |= SRB_DATA_XFER;
2498 /* load total transfer length (24bits) max value 16Mbyte */
2499 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2500 srb->total_xfer_length);
2501 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2502 if (io_dir & DMACMD_DIR) { /* read */
2503 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2505 } else { /* write */
2506 int ln = srb->total_xfer_length;
2507 size_t left_io = srb->total_xfer_length;
2509 if (srb->dcb->sync_period & WIDE_SYNC)
2510 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2514 unsigned char *virt, *base = NULL;
2515 unsigned long flags = 0;
2516 size_t len = left_io;
2518 if (srb->cmd->use_sg) {
2519 size_t offset = srb->request_length - left_io;
2520 local_irq_save(flags);
2521 /* Again, max 4 bytes */
2522 base = scsi_kmap_atomic_sg((struct scatterlist *)srb->cmd->request_buffer,
2523 srb->sg_count, &offset, &len);
2524 virt = base + offset;
2526 virt = srb->cmd->request_buffer + srb->cmd->request_bufflen - left_io;
2532 if (debug_enabled(DBG_PIO))
2533 printk(" %02x", *virt);
2535 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
2537 sg_subtract_one(srb);
2540 if (srb->cmd->use_sg) {
2541 scsi_kunmap_atomic_sg(base);
2542 local_irq_restore(flags);
2545 if (srb->dcb->sync_period & WIDE_SYNC) {
2547 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2548 if (debug_enabled(DBG_PIO))
2551 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2553 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2554 if (debug_enabled(DBG_PIO))
2556 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2560 #endif /* DC395x_LASTPIO */
2561 else { /* xfer pad */
2562 u8 data = 0, data2 = 0;
2563 if (srb->sg_count) {
2564 srb->adapter_status = H_OVER_UNDER_RUN;
2565 srb->status |= OVER_RUN;
2568 * KG: despite the fact that we are using 16 bits I/O ops
2569 * the SCSI FIFO is only 8 bits according to the docs
2570 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2572 if (dcb->sync_period & WIDE_SYNC) {
2573 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
2574 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2576 if (io_dir & DMACMD_DIR) {
2577 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2578 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2580 /* Danger, Robinson: If you find KGs
2581 * scattered over the wide disk, the driver
2582 * or chip is to blame :-( */
2583 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2584 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
2586 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2588 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2589 /* Danger, Robinson: If you find a collection of Ks on your disk
2590 * something broke :-( */
2591 if (io_dir & DMACMD_DIR)
2592 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2594 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2596 srb->state |= SRB_XFERPAD;
2597 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2599 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
2600 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
2605 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2608 dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
2609 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2610 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2611 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
2612 srb->state = SRB_COMPLETED;
2613 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
2614 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2615 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2619 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2622 dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
2623 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2624 srb->state = SRB_STATUS;
2625 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2626 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
2630 /* Check if the message is complete */
2631 static inline u8 msgin_completed(u8 * msgbuf, u32 len)
2633 if (*msgbuf == EXTENDED_MESSAGE) {
2636 if (len < msgbuf[1] + 2)
2638 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
2645 static inline void msgin_reject(struct AdapterCtlBlk *acb,
2646 struct ScsiReqBlk *srb)
2648 srb->msgout_buf[0] = MESSAGE_REJECT;
2650 DC395x_ENABLE_MSGOUT;
2651 srb->state &= ~SRB_MSGIN;
2652 srb->state |= SRB_MSGOUT;
2653 dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
2655 srb->dcb->target_id, srb->dcb->target_lun);
2659 static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
2660 struct DeviceCtlBlk *dcb, u8 tag)
2662 struct ScsiReqBlk *srb = NULL;
2663 struct ScsiReqBlk *i;
2664 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
2665 srb->cmd->pid, tag, srb);
2667 if (!(dcb->tag_mask & (1 << tag)))
2668 dprintkl(KERN_DEBUG,
2669 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2670 dcb->tag_mask, tag);
2672 if (list_empty(&dcb->srb_going_list))
2674 list_for_each_entry(i, &dcb->srb_going_list, list) {
2675 if (i->tag_number == tag) {
2683 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
2684 srb->cmd->pid, srb->dcb->target_id, srb->dcb->target_lun);
2685 if (dcb->flag & ABORT_DEV_) {
2686 /*srb->state = SRB_ABORT_SENT; */
2687 enable_msgout_abort(acb, srb);
2690 if (!(srb->state & SRB_DISCONNECT))
2693 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
2694 srb->state |= dcb->active_srb->state;
2695 srb->state |= SRB_DATA_XFER;
2696 dcb->active_srb = srb;
2697 /* How can we make the DORS happy? */
2702 srb->state = SRB_UNEXPECT_RESEL;
2703 dcb->active_srb = srb;
2704 srb->msgout_buf[0] = MSG_ABORT_TAG;
2706 DC395x_ENABLE_MSGOUT;
2707 dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
2712 static inline void reprogram_regs(struct AdapterCtlBlk *acb,
2713 struct DeviceCtlBlk *dcb)
2715 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
2716 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
2717 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
2718 set_xfer_rate(acb, dcb);
2722 /* set async transfer mode */
2723 static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2725 struct DeviceCtlBlk *dcb = srb->dcb;
2726 dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
2727 dcb->target_id, dcb->target_lun);
2729 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
2730 dcb->sync_mode |= SYNC_NEGO_DONE;
2731 /*dcb->sync_period &= 0; */
2732 dcb->sync_offset = 0;
2733 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
2734 srb->state &= ~SRB_DO_SYNC_NEGO;
2735 reprogram_regs(acb, dcb);
2736 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2737 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2738 build_wdtr(acb, dcb, srb);
2739 DC395x_ENABLE_MSGOUT;
2740 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
2745 /* set sync transfer mode */
2746 static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2748 struct DeviceCtlBlk *dcb = srb->dcb;
2751 dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
2752 "(%02i.%01i MHz) Offset %i\n",
2753 dcb->target_id, srb->msgin_buf[3] << 2,
2754 (250 / srb->msgin_buf[3]),
2755 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
2758 if (srb->msgin_buf[4] > 15)
2759 srb->msgin_buf[4] = 15;
2760 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
2761 dcb->sync_offset = 0;
2762 else if (dcb->sync_offset == 0)
2763 dcb->sync_offset = srb->msgin_buf[4];
2764 if (srb->msgin_buf[4] > dcb->sync_offset)
2765 srb->msgin_buf[4] = dcb->sync_offset;
2767 dcb->sync_offset = srb->msgin_buf[4];
2769 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
2770 || dcb->min_nego_period >
2771 clock_period[bval]))
2773 if (srb->msgin_buf[3] < clock_period[bval])
2775 "msgin_set_sync: Increase sync nego period to %ins\n",
2776 clock_period[bval] << 2);
2777 srb->msgin_buf[3] = clock_period[bval];
2778 dcb->sync_period &= 0xf0;
2779 dcb->sync_period |= ALT_SYNC | bval;
2780 dcb->min_nego_period = srb->msgin_buf[3];
2782 if (dcb->sync_period & WIDE_SYNC)
2788 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2789 dcb->target_id, (fact == 500) ? "Wide16" : "",
2790 dcb->min_nego_period << 2, dcb->sync_offset,
2791 (fact / dcb->min_nego_period),
2792 ((fact % dcb->min_nego_period) * 10 +
2793 dcb->min_nego_period / 2) / dcb->min_nego_period);
2795 if (!(srb->state & SRB_DO_SYNC_NEGO)) {
2796 /* Reply with corrected SDTR Message */
2797 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
2798 srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
2800 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
2802 DC395x_ENABLE_MSGOUT;
2803 dcb->sync_mode |= SYNC_NEGO_DONE;
2805 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2806 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2807 build_wdtr(acb, dcb, srb);
2808 DC395x_ENABLE_MSGOUT;
2809 dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
2812 srb->state &= ~SRB_DO_SYNC_NEGO;
2813 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
2815 reprogram_regs(acb, dcb);
2819 static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
2820 struct ScsiReqBlk *srb)
2822 struct DeviceCtlBlk *dcb = srb->dcb;
2823 dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
2825 dcb->sync_period &= ~WIDE_SYNC;
2826 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
2827 dcb->sync_mode |= WIDE_NEGO_DONE;
2828 srb->state &= ~SRB_DO_WIDE_NEGO;
2829 reprogram_regs(acb, dcb);
2830 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2831 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2832 build_sdtr(acb, dcb, srb);
2833 DC395x_ENABLE_MSGOUT;
2834 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2838 static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2840 struct DeviceCtlBlk *dcb = srb->dcb;
2841 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
2842 && acb->config & HCC_WIDE_CARD) ? 1 : 0;
2843 dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
2845 if (srb->msgin_buf[3] > wide)
2846 srb->msgin_buf[3] = wide;
2848 if (!(srb->state & SRB_DO_WIDE_NEGO)) {
2849 dprintkl(KERN_DEBUG,
2850 "msgin_set_wide: Wide nego initiated <%02i>\n",
2852 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
2854 srb->state |= SRB_DO_WIDE_NEGO;
2855 DC395x_ENABLE_MSGOUT;
2858 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
2859 if (srb->msgin_buf[3] > 0)
2860 dcb->sync_period |= WIDE_SYNC;
2862 dcb->sync_period &= ~WIDE_SYNC;
2863 srb->state &= ~SRB_DO_WIDE_NEGO;
2864 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2866 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2867 (8 << srb->msgin_buf[3]), dcb->target_id);
2868 reprogram_regs(acb, dcb);
2869 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2870 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2871 build_sdtr(acb, dcb, srb);
2872 DC395x_ENABLE_MSGOUT;
2873 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
2879 * extended message codes:
2884 * 00h MODIFY DATA POINTER
2885 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
2886 * 03h WIDE DATA TRANSFER REQUEST
2887 * 04h - 7Fh Reserved
2888 * 80h - FFh Vendor specific
2890 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2893 struct DeviceCtlBlk *dcb = acb->active_dcb;
2894 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->pid);
2896 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2897 if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
2898 /* Now eval the msg */
2899 switch (srb->msgin_buf[0]) {
2901 srb->state = SRB_DISCONNECT;
2904 case SIMPLE_QUEUE_TAG:
2905 case HEAD_OF_QUEUE_TAG:
2906 case ORDERED_QUEUE_TAG:
2908 msgin_qtag(acb, dcb,
2912 case MESSAGE_REJECT:
2913 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2914 DO_CLRATN | DO_DATALATCH);
2915 /* A sync nego message was rejected ! */
2916 if (srb->state & SRB_DO_SYNC_NEGO) {
2917 msgin_set_async(acb, srb);
2920 /* A wide nego message was rejected ! */
2921 if (srb->state & SRB_DO_WIDE_NEGO) {
2922 msgin_set_nowide(acb, srb);
2925 enable_msgout_abort(acb, srb);
2926 /*srb->state |= SRB_ABORT_SENT */
2929 case EXTENDED_MESSAGE:
2931 if (srb->msgin_buf[1] == 3
2932 && srb->msgin_buf[2] == EXTENDED_SDTR) {
2933 msgin_set_sync(acb, srb);
2937 if (srb->msgin_buf[1] == 2
2938 && srb->msgin_buf[2] == EXTENDED_WDTR
2939 && srb->msgin_buf[3] <= 2) { /* sanity check ... */
2940 msgin_set_wide(acb, srb);
2943 msgin_reject(acb, srb);
2946 case MSG_IGNOREWIDE:
2947 /* Discard wide residual */
2948 dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
2951 case COMMAND_COMPLETE:
2952 /* nothing has to be done */
2957 * SAVE POINTER may be ignored as we have the struct
2958 * ScsiReqBlk* associated with the scsi command.
2960 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2961 "SAVE POINTER rem=%i Ignore\n",
2962 srb->cmd->pid, srb->total_xfer_length);
2965 case RESTORE_POINTERS:
2966 dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
2970 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2971 "<%02i-%i> ABORT msg\n",
2972 srb->cmd->pid, dcb->target_id,
2974 dcb->flag |= ABORT_DEV_;
2975 enable_msgout_abort(acb, srb);
2979 /* reject unknown messages */
2980 if (srb->msgin_buf[0] & IDENTIFY_BASE) {
2981 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
2983 srb->msgout_buf[0] = dcb->identify_msg;
2984 DC395x_ENABLE_MSGOUT;
2985 srb->state |= SRB_MSGOUT;
2988 msgin_reject(acb, srb);
2991 /* Clear counter and MsgIn state */
2992 srb->state &= ~SRB_MSGIN;
2995 *pscsi_status = PH_BUS_FREE;
2996 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
2997 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3001 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3004 dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->pid);
3005 clear_fifo(acb, "msgin_phase1");
3006 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
3007 if (!(srb->state & SRB_MSGIN)) {
3008 srb->state &= ~SRB_DISCONNECT;
3009 srb->state |= SRB_MSGIN;
3011 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3013 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
3017 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3023 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3029 static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
3031 struct DeviceCtlBlk *i;
3033 /* set all lun device's period, offset */
3034 if (dcb->identify_msg & 0x07)
3037 if (acb->scan_devices) {
3038 current_sync_offset = dcb->sync_offset;
3042 list_for_each_entry(i, &acb->dcb_list, list)
3043 if (i->target_id == dcb->target_id) {
3044 i->sync_period = dcb->sync_period;
3045 i->sync_offset = dcb->sync_offset;
3046 i->sync_mode = dcb->sync_mode;
3047 i->min_nego_period = dcb->min_nego_period;
3052 static void disconnect(struct AdapterCtlBlk *acb)
3054 struct DeviceCtlBlk *dcb = acb->active_dcb;
3055 struct ScsiReqBlk *srb;
3058 dprintkl(KERN_ERR, "disconnect: No such device\n");
3060 /* Suspend queue for a while */
3061 acb->scsi_host->last_reset =
3063 HZ * acb->eeprom.delay_time;
3064 clear_fifo(acb, "disconnectEx");
3065 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3068 srb = dcb->active_srb;
3069 acb->active_dcb = NULL;
3070 dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->pid);
3072 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3073 clear_fifo(acb, "disconnect");
3074 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3075 if (srb->state & SRB_UNEXPECT_RESEL) {
3077 "disconnect: Unexpected reselection <%02i-%i>\n",
3078 dcb->target_id, dcb->target_lun);
3080 waiting_process_next(acb);
3081 } else if (srb->state & SRB_ABORT_SENT) {
3082 dcb->flag &= ~ABORT_DEV_;
3083 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
3084 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
3085 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
3086 waiting_process_next(acb);
3088 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
3090 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
3092 * Selection time out
3093 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
3095 /* Unexp. Disc / Sel Timeout */
3096 if (srb->state != SRB_START_
3097 && srb->state != SRB_MSGOUT) {
3098 srb->state = SRB_READY;
3099 dprintkl(KERN_DEBUG,
3100 "disconnect: (pid#%li) Unexpected\n",
3102 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
3105 /* Normal selection timeout */
3106 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
3107 "<%02i-%i> SelTO\n", srb->cmd->pid,
3108 dcb->target_id, dcb->target_lun);
3109 if (srb->retry_count++ > DC395x_MAX_RETRIES
3110 || acb->scan_devices) {
3111 srb->target_status =
3112 SCSI_STAT_SEL_TIMEOUT;
3116 srb_going_to_waiting_move(dcb, srb);
3118 "disconnect: (pid#%li) Retry\n",
3120 waiting_set_timer(acb, HZ / 20);
3122 } else if (srb->state & SRB_DISCONNECT) {
3123 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
3125 * SRB_DISCONNECT (This is what we expect!)
3128 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
3129 " 0x%02x: ACK set! Other controllers?\n",
3131 /* It could come from another initiator, therefore don't do much ! */
3133 waiting_process_next(acb);
3134 } else if (srb->state & SRB_COMPLETED) {
3140 dcb->active_srb = NULL;
3141 srb->state = SRB_FREE;
3142 srb_done(acb, dcb, srb);
3148 static void reselect(struct AdapterCtlBlk *acb)
3150 struct DeviceCtlBlk *dcb = acb->active_dcb;
3151 struct ScsiReqBlk *srb = NULL;
3152 u16 rsel_tar_lun_id;
3155 dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
3157 clear_fifo(acb, "reselect");
3158 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3159 /* Read Reselected Target ID and LUN */
3160 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3161 if (dcb) { /* Arbitration lost but Reselection win */
3162 srb = dcb->active_srb;
3164 dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
3165 "but active_srb == NULL\n");
3166 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3170 if (!acb->scan_devices) {
3171 dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
3172 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
3173 srb->cmd->pid, dcb->target_id,
3174 dcb->target_lun, rsel_tar_lun_id,
3175 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3177 /*srb->state |= SRB_DISCONNECT; */
3179 srb->state = SRB_READY;
3181 srb_going_to_waiting_move(dcb, srb);
3182 waiting_set_timer(acb, HZ / 20);
3187 /* Read Reselected Target Id and LUN */
3188 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3189 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
3190 "Got %i!\n", rsel_tar_lun_id);
3191 id = rsel_tar_lun_id & 0xff;
3192 lun = (rsel_tar_lun_id >> 8) & 7;
3193 dcb = find_dcb(acb, id, lun);
3195 dprintkl(KERN_ERR, "reselect: From non existent device "
3196 "<%02i-%i>\n", id, lun);
3197 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3200 acb->active_dcb = dcb;
3202 if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3203 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
3204 "disconnection? <%02i-%i>\n",
3205 dcb->target_id, dcb->target_lun);
3207 if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
3209 dcb->active_srb = srb;
3211 /* There can be only one! */
3212 srb = dcb->active_srb;
3213 if (!srb || !(srb->state & SRB_DISCONNECT)) {
3217 dprintkl(KERN_DEBUG,
3218 "reselect: w/o disconnected cmds <%02i-%i>\n",
3219 dcb->target_id, dcb->target_lun);
3221 srb->state = SRB_UNEXPECT_RESEL;
3222 dcb->active_srb = srb;
3223 enable_msgout_abort(acb, srb);
3225 if (dcb->flag & ABORT_DEV_) {
3226 /*srb->state = SRB_ABORT_SENT; */
3227 enable_msgout_abort(acb, srb);
3229 srb->state = SRB_DATA_XFER;
3233 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3235 /* Program HA ID, target ID, period and offset */
3236 dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
3237 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
3238 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
3239 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
3240 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
3241 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3243 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3247 static inline u8 tagq_blacklist(char *name)
3249 #ifndef DC395x_NO_TAGQ
3252 for (i = 0; i < BADDEVCNT; i++)
3253 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
3263 static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
3265 /* Check for SCSI format (ANSI and Response data format) */
3266 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
3267 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
3268 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
3269 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3270 /* ((dcb->dev_type == TYPE_DISK)
3271 || (dcb->dev_type == TYPE_MOD)) && */
3272 !tagq_blacklist(((char *)ptr) + 8)) {
3273 if (dcb->max_command == 1)
3275 dcb->acb->tag_max_num;
3276 dcb->sync_mode |= EN_TAG_QUEUEING;
3277 /*dcb->tag_mask = 0; */
3279 dcb->max_command = 1;
3284 static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3285 struct ScsiInqData *ptr)
3287 u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
3288 dcb->dev_type = bval1;
3289 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3290 disc_tagq_set(dcb, ptr);
3294 /* unmap mapped pci regions from SRB */
3295 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3297 struct scsi_cmnd *cmd = srb->cmd;
3298 enum dma_data_direction dir = cmd->sc_data_direction;
3299 if (cmd->use_sg && dir != PCI_DMA_NONE) {
3300 /* unmap DC395x SG list */
3301 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
3302 srb->sg_bus_addr, SEGMENTX_LEN);
3303 pci_unmap_single(acb->dev, srb->sg_bus_addr,
3306 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3307 cmd->use_sg, cmd->request_buffer);
3308 /* unmap the sg segments */
3309 pci_unmap_sg(acb->dev,
3310 (struct scatterlist *)cmd->request_buffer,
3312 } else if (cmd->request_buffer && dir != PCI_DMA_NONE) {
3313 dprintkdbg(DBG_SG, "pci_unmap_srb: buffer=%08x(%05x)\n",
3314 srb->segment_x[0].address, cmd->request_bufflen);
3315 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3316 cmd->request_bufflen, dir);
3321 /* unmap mapped pci sense buffer from SRB */
3322 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3323 struct ScsiReqBlk *srb)
3325 if (!(srb->flag & AUTO_REQSENSE))
3327 /* Unmap sense buffer */
3328 dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
3329 srb->segment_x[0].address);
3330 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3331 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
3332 /* Restore SG stuff */
3333 srb->total_xfer_length = srb->xferred;
3334 srb->segment_x[0].address =
3335 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
3336 srb->segment_x[0].length =
3337 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
3342 * Complete execution of a SCSI command
3343 * Signal completion to the generic SCSI driver
3345 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3346 struct ScsiReqBlk *srb)
3349 struct scsi_cmnd *cmd = srb->cmd;
3350 enum dma_data_direction dir = cmd->sc_data_direction;
3353 dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->pid,
3354 srb->cmd->device->id, srb->cmd->device->lun);
3355 dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
3356 srb, cmd->use_sg, srb->sg_index, srb->sg_count,
3357 cmd->request_buffer);
3358 status = srb->target_status;
3359 if (srb->flag & AUTO_REQSENSE) {
3360 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
3361 pci_unmap_srb_sense(acb, srb);
3363 ** target status..........................
3365 srb->flag &= ~AUTO_REQSENSE;
3366 srb->adapter_status = 0;
3367 srb->target_status = CHECK_CONDITION << 1;
3368 if (debug_enabled(DBG_1)) {
3369 switch (cmd->sense_buffer[2] & 0x0f) {
3371 dprintkl(KERN_DEBUG,
3372 "ReqSense: NOT_READY 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 UNIT_ATTENTION:
3377 dprintkl(KERN_DEBUG,
3378 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3379 cmd->cmnd[0], dcb->target_id,
3380 dcb->target_lun, status, acb->scan_devices);
3382 case ILLEGAL_REQUEST:
3383 dprintkl(KERN_DEBUG,
3384 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3385 cmd->cmnd[0], dcb->target_id,
3386 dcb->target_lun, status, acb->scan_devices);
3389 dprintkl(KERN_DEBUG,
3390 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3391 cmd->cmnd[0], dcb->target_id,
3392 dcb->target_lun, status, acb->scan_devices);
3394 case HARDWARE_ERROR:
3395 dprintkl(KERN_DEBUG,
3396 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3397 cmd->cmnd[0], dcb->target_id,
3398 dcb->target_lun, status, acb->scan_devices);
3401 if (cmd->sense_buffer[7] >= 6)
3402 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3403 "(0x%08x 0x%08x)\n",
3404 cmd->sense_buffer[2], cmd->sense_buffer[12],
3405 cmd->sense_buffer[13],
3406 *((unsigned int *)(cmd->sense_buffer + 3)),
3407 *((unsigned int *)(cmd->sense_buffer + 8)));
3409 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3410 cmd->sense_buffer[2],
3411 *((unsigned int *)(cmd->sense_buffer + 3)));
3414 if (status == (CHECK_CONDITION << 1)) {
3415 cmd->result = DID_BAD_TARGET << 16;
3418 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
3420 if (srb->total_xfer_length
3421 && srb->total_xfer_length >= cmd->underflow)
3423 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3424 srb->end_message, CHECK_CONDITION);
3425 /*SET_RES_DID(cmd->result,DID_OK) */
3428 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3429 srb->end_message, CHECK_CONDITION);
3434 /*************************************************************/
3437 * target status..........................
3439 if (status_byte(status) == CHECK_CONDITION) {
3440 request_sense(acb, dcb, srb);
3442 } else if (status_byte(status) == QUEUE_FULL) {
3443 tempcnt = (u8)list_size(&dcb->srb_going_list);
3444 dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3445 dcb->target_id, dcb->target_lun, tempcnt);
3448 dcb->max_command = tempcnt;
3450 srb_going_to_waiting_move(dcb, srb);
3451 waiting_set_timer(acb, HZ / 20);
3452 srb->adapter_status = 0;
3453 srb->target_status = 0;
3455 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
3456 srb->adapter_status = H_SEL_TIMEOUT;
3457 srb->target_status = 0;
3458 cmd->result = DID_NO_CONNECT << 16;
3460 srb->adapter_status = 0;
3461 SET_RES_DID(cmd->result, DID_ERROR);
3462 SET_RES_MSG(cmd->result, srb->end_message);
3463 SET_RES_TARGET(cmd->result, status);
3468 ** process initiator status..........................
3470 status = srb->adapter_status;
3471 if (status & H_OVER_UNDER_RUN) {
3472 srb->target_status = 0;
3473 SET_RES_DID(cmd->result, DID_OK);
3474 SET_RES_MSG(cmd->result, srb->end_message);
3475 } else if (srb->status & PARITY_ERROR) {
3476 SET_RES_DID(cmd->result, DID_PARITY);
3477 SET_RES_MSG(cmd->result, srb->end_message);
3478 } else { /* No error */
3480 srb->adapter_status = 0;
3481 srb->target_status = 0;
3482 SET_RES_DID(cmd->result, DID_OK);
3486 if (dir != PCI_DMA_NONE) {
3488 pci_dma_sync_sg_for_cpu(acb->dev,
3489 (struct scatterlist *)cmd->
3490 request_buffer, cmd->use_sg, dir);
3491 else if (cmd->request_buffer)
3492 pci_dma_sync_single_for_cpu(acb->dev,
3493 srb->segment_x[0].address,
3494 cmd->request_bufflen, dir);
3497 /* Check Error Conditions */
3500 if (cmd->cmnd[0] == INQUIRY) {
3501 unsigned char *base = NULL;
3502 struct ScsiInqData *ptr;
3503 unsigned long flags = 0;
3506 struct scatterlist* sg = (struct scatterlist *)cmd->request_buffer;
3507 size_t offset = 0, len = sizeof(struct ScsiInqData);
3509 local_irq_save(flags);
3510 base = scsi_kmap_atomic_sg(sg, cmd->use_sg, &offset, &len);
3511 ptr = (struct ScsiInqData *)(base + offset);
3513 ptr = (struct ScsiInqData *)(cmd->request_buffer);
3515 if (!ckc_only && (cmd->result & RES_DID) == 0
3516 && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8
3517 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
3518 dcb->inquiry7 = ptr->Flags;
3520 /*if( srb->cmd->cmnd[0] == INQUIRY && */
3521 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3522 if ((cmd->result == (DID_OK << 16)
3523 || status_byte(cmd->result) &
3525 if (!dcb->init_tcq_flag) {
3526 add_dev(acb, dcb, ptr);
3527 dcb->init_tcq_flag = 1;
3532 scsi_kunmap_atomic_sg(base);
3533 local_irq_restore(flags);
3537 /* Here is the info for Doug Gilbert's sg3 ... */
3538 cmd->resid = srb->total_xfer_length;
3539 /* This may be interpreted by sb. or not ... */
3540 cmd->SCp.this_residual = srb->total_xfer_length;
3541 cmd->SCp.buffers_residual = 0;
3542 if (debug_enabled(DBG_KG)) {
3543 if (srb->total_xfer_length)
3544 dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
3545 "cmnd=0x%02x Missed %i bytes\n",
3546 cmd->pid, cmd->device->id, cmd->device->lun,
3547 cmd->cmnd[0], srb->total_xfer_length);
3550 srb_going_remove(dcb, srb);
3551 /* Add to free list */
3552 if (srb == acb->tmp_srb)
3553 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3555 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
3556 cmd->pid, cmd->result);
3557 srb_free_insert(acb, srb);
3559 pci_unmap_srb(acb, srb);
3561 cmd->scsi_done(cmd);
3562 waiting_process_next(acb);
3566 /* abort all cmds in our queues */
3567 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
3568 struct scsi_cmnd *cmd, u8 force)
3570 struct DeviceCtlBlk *dcb;
3571 dprintkl(KERN_INFO, "doing_srb_done: pids ");
3573 list_for_each_entry(dcb, &acb->dcb_list, list) {
3574 struct ScsiReqBlk *srb;
3575 struct ScsiReqBlk *tmp;
3576 struct scsi_cmnd *p;
3578 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
3579 enum dma_data_direction dir;
3583 dir = p->sc_data_direction;
3584 result = MK_RES(0, did_flag, 0, 0);
3585 printk("G:%li(%02i-%i) ", p->pid,
3586 p->device->id, p->device->lun);
3587 srb_going_remove(dcb, srb);
3589 srb_free_insert(acb, srb);
3591 pci_unmap_srb_sense(acb, srb);
3592 pci_unmap_srb(acb, srb);
3594 /* For new EH, we normally don't need to give commands back,
3595 * as they all complete or all time out */
3599 if (!list_empty(&dcb->srb_going_list))
3600 dprintkl(KERN_DEBUG,
3601 "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3602 dcb->target_id, dcb->target_lun);
3604 dprintkl(KERN_DEBUG,
3605 "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3606 dcb->target_id, dcb->target_lun,
3610 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
3614 result = MK_RES(0, did_flag, 0, 0);
3615 printk("W:%li<%02i-%i>", p->pid, p->device->id,
3617 srb_waiting_remove(dcb, srb);
3618 srb_free_insert(acb, srb);
3620 pci_unmap_srb_sense(acb, srb);
3621 pci_unmap_srb(acb, srb);
3623 /* For new EH, we normally don't need to give commands back,
3624 * as they all complete or all time out */
3625 cmd->scsi_done(cmd);
3628 if (!list_empty(&dcb->srb_waiting_list))
3629 dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
3630 list_size(&dcb->srb_waiting_list), dcb->target_id,
3632 dcb->flag &= ~ABORT_DEV_;
3638 static void reset_scsi_bus(struct AdapterCtlBlk *acb)
3640 dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
3641 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
3642 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
3644 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
3649 static void set_basic_config(struct AdapterCtlBlk *acb)
3653 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
3654 if (acb->config & HCC_PARITY)
3655 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
3657 bval = PHASELATCH | INITIATOR | BLOCKRST;
3659 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
3661 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3662 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
3663 /* program Host ID */
3664 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
3665 /* set ansynchronous transfer */
3666 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
3667 /* Turn LED control off */
3668 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
3669 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
3671 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
3673 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
3674 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
3675 /* Clear pending interrupt status */
3676 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
3677 /* Enable SCSI interrupt */
3678 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
3679 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
3680 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3685 static void scsi_reset_detect(struct AdapterCtlBlk *acb)
3687 dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
3688 /* delay half a second */
3689 if (timer_pending(&acb->waiting_timer))
3690 del_timer(&acb->waiting_timer);
3692 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
3693 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
3694 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3696 /* Maybe we locked up the bus? Then lets wait even longer ... */
3697 acb->scsi_host->last_reset =
3698 jiffies + 5 * HZ / 2 +
3699 HZ * acb->eeprom.delay_time;
3701 clear_fifo(acb, "scsi_reset_detect");
3702 set_basic_config(acb);
3704 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3706 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
3707 acb->acb_flag |= RESET_DONE;
3709 acb->acb_flag |= RESET_DETECT;
3710 reset_dev_param(acb);
3711 doing_srb_done(acb, DID_RESET, NULL, 1);
3712 /*DC395x_RecoverSRB( acb ); */
3713 acb->active_dcb = NULL;
3715 waiting_process_next(acb);
3720 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3721 struct ScsiReqBlk *srb)
3723 struct scsi_cmnd *cmd = srb->cmd;
3724 dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
3725 cmd->pid, cmd->device->id, cmd->device->lun);
3727 srb->flag |= AUTO_REQSENSE;
3728 srb->adapter_status = 0;
3729 srb->target_status = 0;
3731 /* KG: Can this prevent crap sense data ? */
3732 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3734 /* Save some data */
3735 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
3736 srb->segment_x[0].address;
3737 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
3738 srb->segment_x[0].length;
3739 srb->xferred = srb->total_xfer_length;
3740 /* srb->segment_x : a one entry of S/G list table */
3741 srb->total_xfer_length = sizeof(cmd->sense_buffer);
3742 srb->segment_x[0].length = sizeof(cmd->sense_buffer);
3743 /* Map sense buffer */
3744 srb->segment_x[0].address =
3745 pci_map_single(acb->dev, cmd->sense_buffer,
3746 sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE);
3747 dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
3748 cmd->sense_buffer, srb->segment_x[0].address,
3749 sizeof(cmd->sense_buffer));
3753 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
3754 dprintkl(KERN_DEBUG,
3755 "request_sense: (pid#%li) failed <%02i-%i>\n",
3756 srb->cmd->pid, dcb->target_id, dcb->target_lun);
3757 srb_going_to_waiting_move(dcb, srb);
3758 waiting_set_timer(acb, HZ / 100);
3764 * device_alloc - Allocate a new device instance. This create the
3765 * devices instance and sets up all the data items. The adapter
3766 * instance is required to obtain confiuration information for this
3767 * device. This does *not* add this device to the adapters device
3770 * @acb: The adapter to obtain configuration information from.
3771 * @target: The target for the new device.
3772 * @lun: The lun for the new device.
3774 * Return the new device if successful or NULL on failure.
3776 static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
3779 struct NvRamType *eeprom = &acb->eeprom;
3780 u8 period_index = eeprom->target[target].period & 0x07;
3781 struct DeviceCtlBlk *dcb;
3783 dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
3784 dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
3788 INIT_LIST_HEAD(&dcb->srb_going_list);
3789 INIT_LIST_HEAD(&dcb->srb_waiting_list);
3790 dcb->active_srb = NULL;
3792 dcb->max_command = 1;
3793 dcb->target_id = target;
3794 dcb->target_lun = lun;
3795 #ifndef DC395x_NO_DISCONNECT
3797 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
3799 dcb->identify_msg = IDENTIFY(0, lun);
3801 dcb->dev_mode = eeprom->target[target].cfg0;
3804 dcb->min_nego_period = clock_period[period_index];
3805 dcb->sync_period = 0;
3806 dcb->sync_offset = 0;
3809 #ifndef DC395x_NO_WIDE
3810 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
3811 && (acb->config & HCC_WIDE_CARD))
3812 dcb->sync_mode |= WIDE_NEGO_ENABLE;
3814 #ifndef DC395x_NO_SYNC
3815 if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
3816 if (!(lun) || current_sync_offset)
3817 dcb->sync_mode |= SYNC_NEGO_ENABLE;
3819 if (dcb->target_lun != 0) {
3821 struct DeviceCtlBlk *p;
3822 list_for_each_entry(p, &acb->dcb_list, list)
3823 if (p->target_id == dcb->target_id)
3826 "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3827 dcb->target_id, dcb->target_lun,
3828 p->target_id, p->target_lun);
3829 dcb->sync_mode = p->sync_mode;
3830 dcb->sync_period = p->sync_period;
3831 dcb->min_nego_period = p->min_nego_period;
3832 dcb->sync_offset = p->sync_offset;
3833 dcb->inquiry7 = p->inquiry7;
3840 * adapter_add_device - Adds the device instance to the adaptor instance.
3842 * @acb: The adapter device to be updated
3843 * @dcb: A newly created and intialised device instance to add.
3845 static void adapter_add_device(struct AdapterCtlBlk *acb,
3846 struct DeviceCtlBlk *dcb)
3848 /* backpointer to adapter */
3851 /* set run_robin to this device if it is currently empty */
3852 if (list_empty(&acb->dcb_list))
3853 acb->dcb_run_robin = dcb;
3855 /* add device to list */
3856 list_add_tail(&dcb->list, &acb->dcb_list);
3858 /* update device maps */
3859 acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
3860 acb->children[dcb->target_id][dcb->target_lun] = dcb;
3865 * adapter_remove_device - Removes the device instance from the adaptor
3866 * instance. The device instance is not check in any way or freed by this.
3867 * The caller is expected to take care of that. This will simply remove the
3868 * device from the adapters data strcutures.
3870 * @acb: The adapter device to be updated
3871 * @dcb: A device that has previously been added to the adapter.
3873 static void adapter_remove_device(struct AdapterCtlBlk *acb,
3874 struct DeviceCtlBlk *dcb)
3876 struct DeviceCtlBlk *i;
3877 struct DeviceCtlBlk *tmp;
3878 dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
3879 dcb->target_id, dcb->target_lun);
3881 /* fix up any pointers to this device that we have in the adapter */
3882 if (acb->active_dcb == dcb)
3883 acb->active_dcb = NULL;
3884 if (acb->dcb_run_robin == dcb)
3885 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
3887 /* unlink from list */
3888 list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
3894 /* clear map and children */
3895 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
3896 acb->children[dcb->target_id][dcb->target_lun] = NULL;
3902 * adapter_remove_and_free_device - Removes a single device from the adapter
3903 * and then frees the device information.
3905 * @acb: The adapter device to be updated
3906 * @dcb: A device that has previously been added to the adapter.
3908 static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
3909 struct DeviceCtlBlk *dcb)
3911 if (list_size(&dcb->srb_going_list) > 1) {
3912 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
3913 "Won't remove because of %i active requests.\n",
3914 dcb->target_id, dcb->target_lun,
3915 list_size(&dcb->srb_going_list));
3918 adapter_remove_device(acb, dcb);
3924 * adapter_remove_and_free_all_devices - Removes and frees all of the
3925 * devices associated with the specified adapter.
3927 * @acb: The adapter from which all devices should be removed.
3929 static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
3931 struct DeviceCtlBlk *dcb;
3932 struct DeviceCtlBlk *tmp;
3933 dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
3934 list_size(&acb->dcb_list));
3936 list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
3937 adapter_remove_and_free_device(acb, dcb);
3942 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3943 * scsi device that we need to deal with. We allocate a new device and then
3944 * insert that device into the adapters device list.
3946 * @scsi_device: The new scsi device that we need to handle.
3948 static int dc395x_slave_alloc(struct scsi_device *scsi_device)
3950 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3951 struct DeviceCtlBlk *dcb;
3953 dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
3956 adapter_add_device(acb, dcb);
3963 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3964 * device that is going away.
3966 * @scsi_device: The new scsi device that we need to handle.
3968 static void dc395x_slave_destroy(struct scsi_device *scsi_device)
3970 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3971 struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
3973 adapter_remove_and_free_device(acb, dcb);
3980 * trms1040_wait_30us: wait for 30 us
3982 * Waits for 30us (using the chip by the looks of it..)
3984 * @io_port: base I/O address
3986 static void __devinit trms1040_wait_30us(unsigned long io_port)
3988 /* ScsiPortStallExecution(30); wait 30 us */
3989 outb(5, io_port + TRM_S1040_GEN_TIMER);
3990 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
3996 * trms1040_write_cmd - write the secified command and address to
3999 * @io_port: base I/O address
4000 * @cmd: SB + op code (command) to send
4001 * @addr: address to send
4003 static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
4008 /* program SB + OP code */
4009 for (i = 0; i < 3; i++, cmd <<= 1) {
4010 send_data = NVR_SELECT;
4011 if (cmd & 0x04) /* Start from bit 2 */
4012 send_data |= NVR_BITOUT;
4014 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4015 trms1040_wait_30us(io_port);
4016 outb((send_data | NVR_CLOCK),
4017 io_port + TRM_S1040_GEN_NVRAM);
4018 trms1040_wait_30us(io_port);
4022 for (i = 0; i < 7; i++, addr <<= 1) {
4023 send_data = NVR_SELECT;
4024 if (addr & 0x40) /* Start from bit 6 */
4025 send_data |= NVR_BITOUT;
4027 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4028 trms1040_wait_30us(io_port);
4029 outb((send_data | NVR_CLOCK),
4030 io_port + TRM_S1040_GEN_NVRAM);
4031 trms1040_wait_30us(io_port);
4033 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4034 trms1040_wait_30us(io_port);
4039 * trms1040_set_data - store a single byte in the eeprom
4041 * Called from write all to write a single byte into the SSEEPROM
4042 * Which is done one bit at a time.
4044 * @io_port: base I/O address
4045 * @addr: offset into EEPROM
4046 * @byte: bytes to write
4048 static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
4053 /* Send write command & address */
4054 trms1040_write_cmd(io_port, 0x05, addr);
4057 for (i = 0; i < 8; i++, byte <<= 1) {
4058 send_data = NVR_SELECT;
4059 if (byte & 0x80) /* Start from bit 7 */
4060 send_data |= NVR_BITOUT;
4062 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4063 trms1040_wait_30us(io_port);
4064 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4065 trms1040_wait_30us(io_port);
4067 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4068 trms1040_wait_30us(io_port);
4070 /* Disable chip select */
4071 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4072 trms1040_wait_30us(io_port);
4074 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4075 trms1040_wait_30us(io_port);
4077 /* Wait for write ready */
4079 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4080 trms1040_wait_30us(io_port);
4082 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4083 trms1040_wait_30us(io_port);
4085 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
4089 /* Disable chip select */
4090 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4095 * trms1040_write_all - write 128 bytes to the eeprom
4097 * Write the supplied 128 bytes to the chips SEEPROM
4099 * @eeprom: the data to write
4100 * @io_port: the base io port
4102 static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
4104 u8 *b_eeprom = (u8 *)eeprom;
4107 /* Enable SEEPROM */
4108 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4109 io_port + TRM_S1040_GEN_CONTROL);
4112 trms1040_write_cmd(io_port, 0x04, 0xFF);
4113 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4114 trms1040_wait_30us(io_port);
4117 for (addr = 0; addr < 128; addr++, b_eeprom++)
4118 trms1040_set_data(io_port, addr, *b_eeprom);
4121 trms1040_write_cmd(io_port, 0x04, 0x00);
4122 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4123 trms1040_wait_30us(io_port);
4125 /* Disable SEEPROM */
4126 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4127 io_port + TRM_S1040_GEN_CONTROL);
4132 * trms1040_get_data - get a single byte from the eeprom
4134 * Called from read all to read a single byte into the SSEEPROM
4135 * Which is done one bit at a time.
4137 * @io_port: base I/O address
4138 * @addr: offset into SEEPROM
4140 * Returns the the byte read.
4142 static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
4148 /* Send read command & address */
4149 trms1040_write_cmd(io_port, 0x06, addr);
4152 for (i = 0; i < 8; i++) {
4153 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4154 trms1040_wait_30us(io_port);
4155 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4157 /* Get data bit while falling edge */
4158 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
4160 if (read_byte & NVR_BITIN)
4163 trms1040_wait_30us(io_port);
4166 /* Disable chip select */
4167 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4173 * trms1040_read_all - read all bytes from the eeprom
4175 * Read the 128 bytes from the SEEPROM.
4177 * @eeprom: where to store the data
4178 * @io_port: the base io port
4180 static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
4182 u8 *b_eeprom = (u8 *)eeprom;
4185 /* Enable SEEPROM */
4186 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4187 io_port + TRM_S1040_GEN_CONTROL);
4190 for (addr = 0; addr < 128; addr++, b_eeprom++)
4191 *b_eeprom = trms1040_get_data(io_port, addr);
4193 /* Disable SEEPROM */
4194 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4195 io_port + TRM_S1040_GEN_CONTROL);
4201 * check_eeprom - get and check contents of the eeprom
4203 * Read seeprom 128 bytes into the memory provider in eeprom.
4204 * Checks the checksum and if it's not correct it uses a set of default
4207 * @eeprom: caller allocated strcuture to read the eeprom data into
4208 * @io_port: io port to read from
4210 static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
4212 u16 *w_eeprom = (u16 *)eeprom;
4218 trms1040_read_all(eeprom, io_port); /* read eeprom */
4221 for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
4222 w_addr++, w_eeprom++)
4224 if (cksum != 0x1234) {
4226 * Checksum is wrong.
4227 * Load a set of defaults into the eeprom buffer
4229 dprintkl(KERN_WARNING,
4230 "EEProm checksum error: using default values and options.\n");
4231 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4232 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4233 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4234 eeprom->sub_sys_id[1] =
4235 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4236 eeprom->sub_class = 0x00;
4237 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4238 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4239 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4240 eeprom->device_id[1] =
4241 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4242 eeprom->reserved = 0x00;
4244 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
4245 d_addr < 16; d_addr++, d_eeprom++)
4246 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
4248 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
4249 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
4250 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
4253 /* Now load defaults (maybe set by boot/module params) */
4254 set_safe_settings();
4256 eeprom_override(eeprom);
4258 eeprom->cksum = 0x00;
4259 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
4260 w_addr < 63; w_addr++, w_eeprom++)
4263 *w_eeprom = 0x1234 - cksum;
4264 trms1040_write_all(eeprom, io_port);
4265 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
4267 set_safe_settings();
4268 eeprom_index_to_delay(eeprom);
4269 eeprom_override(eeprom);
4275 * print_eeprom_settings - output the eeprom settings
4276 * to the kernel log so people can see what they were.
4278 * @eeprom: The eeprom data strucutre to show details for.
4280 static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
4282 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4284 eeprom->target[0].period,
4285 clock_speed[eeprom->target[0].period] / 10,
4286 clock_speed[eeprom->target[0].period] % 10,
4287 eeprom->target[0].cfg0);
4288 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4289 eeprom->channel_cfg, eeprom->max_tag,
4290 1 << eeprom->max_tag, eeprom->delay_time);
4294 /* Free SG tables */
4295 static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
4298 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4300 for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
4301 kfree(acb->srb_array[i].segment_x);
4306 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4307 * should never cross a page boundary */
4308 static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
4310 const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
4312 int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
4313 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4316 struct SGentry *ptr;
4318 for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
4319 acb->srb_array[i].segment_x = NULL;
4321 dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
4323 ptr = (struct SGentry *)kmalloc(PAGE_SIZE, GFP_KERNEL);
4325 adapter_sg_tables_free(acb);
4328 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
4329 PAGE_SIZE, ptr, srb_idx);
4331 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4332 acb->srb_array[srb_idx++].segment_x =
4333 ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4335 if (i < srbs_per_page)
4336 acb->srb.segment_x =
4337 ptr + (i * DC395x_MAX_SG_LISTENTRY);
4339 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
4346 * adapter_print_config - print adapter connection and termination
4349 * The io port in the adapter needs to have been set before calling
4352 * @acb: The adapter to print the information for.
4354 static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
4358 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
4359 dprintkl(KERN_INFO, "%sConnectors: ",
4360 ((bval & WIDESCSI) ? "(Wide) " : ""));
4361 if (!(bval & CON5068))
4362 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
4363 if (!(bval & CON68))
4364 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
4365 if (!(bval & CON50))
4367 if ((bval & (CON5068 | CON50 | CON68)) ==
4368 0 /*(CON5068 | CON50 | CON68) */ )
4369 printk(" Oops! (All 3?) ");
4370 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
4371 printk(" Termination: ");
4372 if (bval & DIS_TERM)
4373 printk("Disabled\n");
4375 if (bval & AUTOTERM)
4377 if (bval & LOW8TERM)
4387 * adapter_init_params - Initialize the various parameters in the
4388 * adapter structure. Note that the pointer to the scsi_host is set
4389 * early (when this instance is created) and the io_port and irq
4390 * values are set later after they have been reserved. This just gets
4391 * everything set to a good starting position.
4393 * The eeprom structure in the adapter needs to have been set before
4394 * calling this function.
4396 * @acb: The adapter to initialize.
4398 static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
4400 struct NvRamType *eeprom = &acb->eeprom;
4403 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4404 /* NOTE: acb->io_port_base is set at port registration time */
4405 /* NOTE: acb->io_port_len is set at port registration time */
4407 INIT_LIST_HEAD(&acb->dcb_list);
4408 acb->dcb_run_robin = NULL;
4409 acb->active_dcb = NULL;
4411 INIT_LIST_HEAD(&acb->srb_free_list);
4412 /* temp SRB for Q tag used or abort command used */
4413 acb->tmp_srb = &acb->srb;
4414 init_timer(&acb->waiting_timer);
4415 init_timer(&acb->selto_timer);
4417 acb->srb_count = DC395x_MAX_SRB_CNT;
4419 acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */
4420 /* NOTE: acb->irq_level is set at IRQ registration time */
4422 acb->tag_max_num = 1 << eeprom->max_tag;
4423 if (acb->tag_max_num > 30)
4424 acb->tag_max_num = 30;
4426 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4427 acb->gmode2 = eeprom->channel_cfg;
4428 acb->config = 0; /* NOTE: actually set in adapter_init_chip */
4430 if (eeprom->channel_cfg & NAC_SCANLUN)
4432 acb->scan_devices = 1;
4434 acb->scsi_host->this_id = eeprom->scsi_id;
4435 acb->hostid_bit = (1 << acb->scsi_host->this_id);
4437 for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
4438 acb->dcb_map[i] = 0;
4442 /* link static array of srbs into the srb free list */
4443 for (i = 0; i < acb->srb_count - 1; i++)
4444 srb_free_insert(acb, &acb->srb_array[i]);
4449 * adapter_init_host - Initialize the scsi host instance based on
4450 * values that we have already stored in the adapter instance. There's
4451 * some mention that a lot of these are deprecated, so we won't use
4452 * them (we'll use the ones in the adapter instance) but we'll fill
4453 * them in in case something else needs them.
4455 * The eeprom structure, irq and io ports in the adapter need to have
4456 * been set before calling this function.
4458 * @host: The scsi host instance to fill in the values for.
4460 static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
4462 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4463 struct NvRamType *eeprom = &acb->eeprom;
4465 host->max_cmd_len = 24;
4466 host->can_queue = DC395x_MAX_CMD_QUEUE;
4467 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4468 host->this_id = (int)eeprom->scsi_id;
4469 host->io_port = acb->io_port_base;
4470 host->n_io_port = acb->io_port_len;
4471 host->dma_channel = -1;
4472 host->unique_id = acb->io_port_base;
4473 host->irq = acb->irq_level;
4474 host->last_reset = jiffies;
4477 if (host->max_id - 1 == eeprom->scsi_id)
4480 #ifdef CONFIG_SCSI_MULTI_LUN
4481 if (eeprom->channel_cfg & NAC_SCANLUN)
4493 * adapter_init_chip - Get the chip into a know state and figure out
4494 * some of the settings that apply to this adapter.
4496 * The io port in the adapter needs to have been set before calling
4497 * this function. The config will be configured correctly on return.
4499 * @acb: The adapter which we are to init.
4501 static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
4503 struct NvRamType *eeprom = &acb->eeprom;
4505 /* Mask all the interrupt */
4506 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
4507 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
4509 /* Reset SCSI module */
4510 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4512 /* Reset PCI/DMA module */
4513 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4516 /* program configuration 0 */
4517 acb->config = HCC_AUTOTERM | HCC_PARITY;
4518 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
4519 acb->config |= HCC_WIDE_CARD;
4521 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
4522 acb->config |= HCC_SCSI_RESET;
4524 if (acb->config & HCC_SCSI_RESET) {
4525 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
4526 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4528 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4529 /*spin_unlock_irq (&io_request_lock); */
4532 acb->scsi_host->last_reset =
4534 HZ * acb->eeprom.delay_time;
4536 /*spin_lock_irq (&io_request_lock); */
4542 * init_adapter - Grab the resource for the card, setup the adapter
4543 * information, set the card into a known state, create the various
4544 * tables etc etc. This basically gets all adapter information all up
4545 * to date, intialised and gets the chip in sync with it.
4547 * @host: This hosts adapter structure
4548 * @io_port: The base I/O port
4551 * Returns 0 if the initialization succeeds, any other value on
4554 static int __devinit adapter_init(struct AdapterCtlBlk *acb,
4555 unsigned long io_port, u32 io_port_len, unsigned int irq)
4557 if (!request_region(io_port, io_port_len, DC395X_NAME)) {
4558 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
4561 /* store port base to indicate we have registered it */
4562 acb->io_port_base = io_port;
4563 acb->io_port_len = io_port_len;
4565 if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
4566 /* release the region we just claimed */
4567 dprintkl(KERN_INFO, "Failed to register IRQ\n");
4570 /* store irq to indicate we have registered it */
4571 acb->irq_level = irq;
4573 /* get eeprom configuration information and command line settings etc */
4574 check_eeprom(&acb->eeprom, io_port);
4575 print_eeprom_settings(&acb->eeprom);
4577 /* setup adapter control block */
4578 adapter_init_params(acb);
4580 /* display card connectors/termination settings */
4581 adapter_print_config(acb);
4583 if (adapter_sg_tables_alloc(acb)) {
4584 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
4587 adapter_init_scsi_host(acb->scsi_host);
4588 adapter_init_chip(acb);
4589 set_basic_config(acb);
4592 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4593 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4594 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
4595 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
4600 free_irq(acb->irq_level, acb);
4601 if (acb->io_port_base)
4602 release_region(acb->io_port_base, acb->io_port_len);
4603 adapter_sg_tables_free(acb);
4610 * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4611 * stopping all operations and disabling interrupt generation on the
4614 * @acb: The adapter which we are to shutdown.
4616 static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
4618 /* disable interrupts */
4619 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
4620 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
4622 /* reset the scsi bus */
4623 if (acb->config & HCC_SCSI_RESET)
4624 reset_scsi_bus(acb);
4626 /* clear any pending interupt state */
4627 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4633 * adapter_uninit - Shut down the chip and release any resources that
4634 * we had allocated. Once this returns the adapter should not be used
4637 * @acb: The adapter which we are to un-initialize.
4639 static void adapter_uninit(struct AdapterCtlBlk *acb)
4641 unsigned long flags;
4642 DC395x_LOCK_IO(acb->scsi_host, flags);
4645 if (timer_pending(&acb->waiting_timer))
4646 del_timer(&acb->waiting_timer);
4647 if (timer_pending(&acb->selto_timer))
4648 del_timer(&acb->selto_timer);
4650 adapter_uninit_chip(acb);
4651 adapter_remove_and_free_all_devices(acb);
4652 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4655 free_irq(acb->irq_level, acb);
4656 if (acb->io_port_base)
4657 release_region(acb->io_port_base, acb->io_port_len);
4659 adapter_sg_tables_free(acb);
4664 #define SPRINTF(args...) pos += sprintf(pos, args)
4668 if (YN) SPRINTF(" Yes ");\
4669 else SPRINTF(" No ")
4671 static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
4672 char **start, off_t offset, int length, int inout)
4674 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4677 struct DeviceCtlBlk *dcb;
4678 unsigned long flags;
4681 if (inout) /* Has data been written to the file ? */
4684 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
4685 SPRINTF(" Driver Version " DC395X_VERSION "\n");
4687 DC395x_LOCK_IO(acb->scsi_host, flags);
4689 SPRINTF("SCSI Host Nr %i, ", host->host_no);
4690 SPRINTF("DC395U/UW/F DC315/U %s\n",
4691 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
4692 SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
4693 SPRINTF("irq_level 0x%04x, ", acb->irq_level);
4694 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
4696 SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
4697 SPRINTF("AdapterID %i\n", host->this_id);
4699 SPRINTF("tag_max_num %i", acb->tag_max_num);
4700 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4701 SPRINTF(", FilterCfg 0x%02x",
4702 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
4703 SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
4706 SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4708 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
4709 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
4710 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
4711 acb->dcb_map[6], acb->dcb_map[7]);
4713 (" %02x %02x %02x %02x %02x %02x %02x %02x\n",
4714 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
4715 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
4716 acb->dcb_map[14], acb->dcb_map[15]);
4719 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4722 list_for_each_entry(dcb, &acb->dcb_list, list) {
4724 SPRINTF("%02i %02i %02i ", dev, dcb->target_id,
4726 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
4727 YESNO(dcb->sync_offset);
4728 YESNO(dcb->sync_period & WIDE_SYNC);
4729 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
4730 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
4731 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
4732 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
4733 if (dcb->sync_offset)
4734 SPRINTF(" %03i ns ", nego_period);
4736 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
4738 if (dcb->sync_offset & 0x0f) {
4739 spd = 1000 / (nego_period);
4740 spd1 = 1000 % (nego_period);
4741 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
4742 SPRINTF(" %2i.%1i M %02i ", spd, spd1,
4743 (dcb->sync_offset & 0x0f));
4747 /* Add more info ... */
4748 SPRINTF(" %02i\n", dcb->max_command);
4752 if (timer_pending(&acb->waiting_timer))
4753 SPRINTF("Waiting queue timer running\n");
4757 list_for_each_entry(dcb, &acb->dcb_list, list) {
4758 struct ScsiReqBlk *srb;
4759 if (!list_empty(&dcb->srb_waiting_list))
4760 SPRINTF("DCB (%02i-%i): Waiting: %i:",
4761 dcb->target_id, dcb->target_lun,
4762 list_size(&dcb->srb_waiting_list));
4763 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
4764 SPRINTF(" %li", srb->cmd->pid);
4765 if (!list_empty(&dcb->srb_going_list))
4766 SPRINTF("\nDCB (%02i-%i): Going : %i:",
4767 dcb->target_id, dcb->target_lun,
4768 list_size(&dcb->srb_going_list));
4769 list_for_each_entry(srb, &dcb->srb_going_list, list)
4770 SPRINTF(" %li", srb->cmd->pid);
4771 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4775 if (debug_enabled(DBG_1)) {
4776 SPRINTF("DCB list for ACB %p:\n", acb);
4777 list_for_each_entry(dcb, &acb->dcb_list, list) {
4778 SPRINTF("%p -> ", dcb);
4783 *start = buffer + offset;
4784 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4786 if (pos - buffer < offset)
4788 else if (pos - buffer - offset < length)
4789 return pos - buffer - offset;
4795 static struct scsi_host_template dc395x_driver_template = {
4796 .module = THIS_MODULE,
4797 .proc_name = DC395X_NAME,
4798 .proc_info = dc395x_proc_info,
4799 .name = DC395X_BANNER " " DC395X_VERSION,
4800 .queuecommand = dc395x_queue_command,
4801 .bios_param = dc395x_bios_param,
4802 .slave_alloc = dc395x_slave_alloc,
4803 .slave_destroy = dc395x_slave_destroy,
4804 .can_queue = DC395x_MAX_CAN_QUEUE,
4806 .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
4807 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
4808 .eh_abort_handler = dc395x_eh_abort,
4809 .eh_bus_reset_handler = dc395x_eh_bus_reset,
4810 .unchecked_isa_dma = 0,
4811 .use_clustering = DISABLE_CLUSTERING,
4816 * banner_display - Display banner on first instance of driver
4819 static void banner_display(void)
4821 static int banner_done = 0;
4824 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
4831 * dc395x_init_one - Initialise a single instance of the adapter.
4833 * The PCI layer will call this once for each instance of the adapter
4834 * that it finds in the system. The pci_dev strcuture indicates which
4835 * instance we are being called from.
4837 * @dev: The PCI device to intialize.
4838 * @id: Looks like a pointer to the entry in our pci device table
4839 * that was actually matched by the PCI subsystem.
4841 * Returns 0 on success, or an error code (-ve) on failure.
4843 static int __devinit dc395x_init_one(struct pci_dev *dev,
4844 const struct pci_device_id *id)
4846 struct Scsi_Host *scsi_host = NULL;
4847 struct AdapterCtlBlk *acb = NULL;
4848 unsigned long io_port_base;
4849 unsigned int io_port_len;
4852 dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
4855 if (pci_enable_device(dev))
4857 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
4860 io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
4861 io_port_len = pci_resource_len(dev, 0);
4863 dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
4865 /* allocate scsi host information (includes out adapter) */
4866 scsi_host = scsi_host_alloc(&dc395x_driver_template,
4867 sizeof(struct AdapterCtlBlk));
4869 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
4872 acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
4873 acb->scsi_host = scsi_host;
4876 /* initialise the adapter and everything we need */
4877 if (adapter_init(acb, io_port_base, io_port_len, irq)) {
4878 dprintkl(KERN_INFO, "adapter init failed\n");
4882 pci_set_master(dev);
4884 /* get the scsi mid level to scan for new devices on the bus */
4885 if (scsi_add_host(scsi_host, &dev->dev)) {
4886 dprintkl(KERN_ERR, "scsi_add_host failed\n");
4889 pci_set_drvdata(dev, scsi_host);
4890 scsi_scan_host(scsi_host);
4896 adapter_uninit(acb);
4897 if (scsi_host != NULL)
4898 scsi_host_put(scsi_host);
4899 pci_disable_device(dev);
4905 * dc395x_remove_one - Called to remove a single instance of the
4908 * @dev: The PCI device to intialize.
4910 static void __devexit dc395x_remove_one(struct pci_dev *dev)
4912 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
4913 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
4915 dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
4917 scsi_remove_host(scsi_host);
4918 adapter_uninit(acb);
4919 pci_disable_device(dev);
4920 scsi_host_put(scsi_host);
4921 pci_set_drvdata(dev, NULL);
4925 static struct pci_device_id dc395x_pci_table[] = {
4927 .vendor = PCI_VENDOR_ID_TEKRAM,
4928 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
4929 .subvendor = PCI_ANY_ID,
4930 .subdevice = PCI_ANY_ID,
4932 {} /* Terminating entry */
4934 MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
4937 static struct pci_driver dc395x_driver = {
4938 .name = DC395X_NAME,
4939 .id_table = dc395x_pci_table,
4940 .probe = dc395x_init_one,
4941 .remove = __devexit_p(dc395x_remove_one),
4946 * dc395x_module_init - Module initialization function
4948 * Used by both module and built-in driver to initialise this driver.
4950 static int __init dc395x_module_init(void)
4952 return pci_module_init(&dc395x_driver);
4957 * dc395x_module_exit - Module cleanup function.
4959 static void __exit dc395x_module_exit(void)
4961 pci_unregister_driver(&dc395x_driver);
4965 module_init(dc395x_module_init);
4966 module_exit(dc395x_module_exit);
4968 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4969 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4970 MODULE_LICENSE("GPL");