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>
60 #include <linux/slab.h>
63 #include <scsi/scsi.h>
64 #include <scsi/scsi_cmnd.h>
65 #include <scsi/scsi_device.h>
66 #include <scsi/scsi_host.h>
67 #include <scsi/scsi_transport_spi.h>
71 #define DC395X_NAME "dc395x"
72 #define DC395X_BANNER "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
73 #define DC395X_VERSION "v2.05, 2004/03/08"
75 /*---------------------------------------------------------------------------
77 ---------------------------------------------------------------------------*/
79 * Set to disable parts of the driver
81 /*#define DC395x_NO_DISCONNECT*/
82 /*#define DC395x_NO_TAGQ*/
83 /*#define DC395x_NO_SYNC*/
84 /*#define DC395x_NO_WIDE*/
86 /*---------------------------------------------------------------------------
88 ---------------------------------------------------------------------------*/
90 * Types of debugging that can be enabled and disabled
96 #define DBG_FIFO 0x0040
97 #define DBG_PIO 0x0080
101 * Set set of things to output debugging for.
102 * Undefine to remove all debugging
104 /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
105 /*#define DEBUG_MASK DBG_0*/
109 * Output a kernel mesage at the specified level and append the
110 * driver name and a ": " to the start of the message
112 #define dprintkl(level, format, arg...) \
113 printk(level DC395X_NAME ": " format , ## arg)
118 * print a debug message - this is formated with KERN_DEBUG, then the
119 * driver name followed by a ": " and then the message is output.
120 * This also checks that the specified debug level is enabled before
121 * outputing the message
123 #define dprintkdbg(type, format, arg...) \
125 if ((type) & (DEBUG_MASK)) \
126 dprintkl(KERN_DEBUG , format , ## arg); \
130 * Check if the specified type of debugging is enabled
132 #define debug_enabled(type) ((DEBUG_MASK) & (type))
136 * No debugging. Do nothing
138 #define dprintkdbg(type, format, arg...) \
140 #define debug_enabled(type) (0)
145 #ifndef PCI_VENDOR_ID_TEKRAM
146 #define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */
148 #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
149 #define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */
153 #define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
154 #define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
156 #define DC395x_read8(acb,address) (u8)(inb(acb->io_port_base + (address)))
157 #define DC395x_read16(acb,address) (u16)(inw(acb->io_port_base + (address)))
158 #define DC395x_read32(acb,address) (u32)(inl(acb->io_port_base + (address)))
159 #define DC395x_write8(acb,address,value) outb((value), acb->io_port_base + (address))
160 #define DC395x_write16(acb,address,value) outw((value), acb->io_port_base + (address))
161 #define DC395x_write32(acb,address,value) outl((value), acb->io_port_base + (address))
166 * srb->segement_x is the hw sg list. It is always allocated as a
167 * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
168 * cross a page boundy.
170 #define SEGMENTX_LEN (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
174 u32 address
; /* bus! address */
178 /* The SEEPROM structure for TRM_S1040 */
180 u8 cfg0
; /* Target configuration byte 0 */
181 u8 period
; /* Target period */
182 u8 cfg2
; /* Target configuration byte 2 */
183 u8 cfg3
; /* Target configuration byte 3 */
187 u8 sub_vendor_id
[2]; /* 0,1 Sub Vendor ID */
188 u8 sub_sys_id
[2]; /* 2,3 Sub System ID */
189 u8 sub_class
; /* 4 Sub Class */
190 u8 vendor_id
[2]; /* 5,6 Vendor ID */
191 u8 device_id
[2]; /* 7,8 Device ID */
192 u8 reserved
; /* 9 Reserved */
193 struct NVRamTarget target
[DC395x_MAX_SCSI_ID
];
200 u8 scsi_id
; /* 74 Host Adapter SCSI ID */
201 u8 channel_cfg
; /* 75 Channel configuration */
202 u8 delay_time
; /* 76 Power on delay time */
203 u8 max_tag
; /* 77 Maximum tags */
204 u8 reserved0
; /* 78 */
205 u8 boot_target
; /* 79 */
206 u8 boot_lun
; /* 80 */
207 u8 reserved1
; /* 81 */
208 u16 reserved2
[22]; /* 82,..125 */
209 u16 cksum
; /* 126,127 */
213 struct list_head list
; /* next/prev ptrs for srb lists */
214 struct DeviceCtlBlk
*dcb
;
215 struct scsi_cmnd
*cmd
;
217 struct SGentry
*segment_x
; /* Linear array of hw sg entries (up to 64 entries) */
218 dma_addr_t sg_bus_addr
; /* Bus address of sg list (ie, of segment_x) */
220 u8 sg_count
; /* No of HW sg entries for this request */
221 u8 sg_index
; /* Index of HW sg entry for this request */
222 size_t total_xfer_length
; /* Total number of bytes remaining to be transferred */
223 size_t request_length
; /* Total number of bytes in this request */
225 * The sense buffer handling function, request_sense, uses
226 * the first hw sg entry (segment_x[0]) and the transfer
227 * length (total_xfer_length). While doing this it stores the
228 * original values into the last sg hw list
229 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
230 * total_xfer_length in xferred. These values are restored in
231 * pci_unmap_srb_sense. This is the only place xferred is used.
233 size_t xferred
; /* Saved copy of total_xfer_length */
253 struct DeviceCtlBlk
{
254 struct list_head list
; /* next/prev ptrs for the dcb list */
255 struct AdapterCtlBlk
*acb
;
256 struct list_head srb_going_list
; /* head of going srb list */
257 struct list_head srb_waiting_list
; /* head of waiting srb list */
259 struct ScsiReqBlk
*active_srb
;
264 u8 target_id
; /* SCSI Target ID (SCSI Only) */
265 u8 target_lun
; /* SCSI Log. Unit (SCSI Only) */
269 u8 inquiry7
; /* To store Inquiry flags */
270 u8 sync_mode
; /* 0:async mode */
271 u8 min_nego_period
; /* for nego. */
272 u8 sync_period
; /* for reg. */
274 u8 sync_offset
; /* for reg. and nego.(low nibble) */
280 struct AdapterCtlBlk
{
281 struct Scsi_Host
*scsi_host
;
283 unsigned long io_port_base
;
284 unsigned long io_port_len
;
286 struct list_head dcb_list
; /* head of going dcb list */
287 struct DeviceCtlBlk
*dcb_run_robin
;
288 struct DeviceCtlBlk
*active_dcb
;
290 struct list_head srb_free_list
; /* head of free srb list */
291 struct ScsiReqBlk
*tmp_srb
;
292 struct timer_list waiting_timer
;
293 struct timer_list selto_timer
;
295 unsigned long last_reset
;
301 unsigned int irq_level
;
311 u8 dcb_map
[DC395x_MAX_SCSI_ID
];
312 struct DeviceCtlBlk
*children
[DC395x_MAX_SCSI_ID
][32];
318 struct ScsiReqBlk srb_array
[DC395x_MAX_SRB_CNT
];
319 struct ScsiReqBlk srb
;
321 struct NvRamType eeprom
; /* eeprom settings for this adapter */
325 /*---------------------------------------------------------------------------
327 ---------------------------------------------------------------------------*/
328 static void data_out_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
330 static void data_in_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
332 static void command_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
334 static void status_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
336 static void msgout_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
338 static void msgin_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
340 static void data_out_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
342 static void data_in_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
344 static void command_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
346 static void status_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
348 static void msgout_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
350 static void msgin_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
352 static void nop0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
354 static void nop1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
356 static void set_basic_config(struct AdapterCtlBlk
*acb
);
357 static void cleanup_after_transfer(struct AdapterCtlBlk
*acb
,
358 struct ScsiReqBlk
*srb
);
359 static void reset_scsi_bus(struct AdapterCtlBlk
*acb
);
360 static void data_io_transfer(struct AdapterCtlBlk
*acb
,
361 struct ScsiReqBlk
*srb
, u16 io_dir
);
362 static void disconnect(struct AdapterCtlBlk
*acb
);
363 static void reselect(struct AdapterCtlBlk
*acb
);
364 static u8
start_scsi(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
365 struct ScsiReqBlk
*srb
);
366 static inline void enable_msgout_abort(struct AdapterCtlBlk
*acb
,
367 struct ScsiReqBlk
*srb
);
368 static void build_srb(struct scsi_cmnd
*cmd
, struct DeviceCtlBlk
*dcb
,
369 struct ScsiReqBlk
*srb
);
370 static void doing_srb_done(struct AdapterCtlBlk
*acb
, u8 did_code
,
371 struct scsi_cmnd
*cmd
, u8 force
);
372 static void scsi_reset_detect(struct AdapterCtlBlk
*acb
);
373 static void pci_unmap_srb(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
);
374 static void pci_unmap_srb_sense(struct AdapterCtlBlk
*acb
,
375 struct ScsiReqBlk
*srb
);
376 static void srb_done(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
377 struct ScsiReqBlk
*srb
);
378 static void request_sense(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
379 struct ScsiReqBlk
*srb
);
380 static void set_xfer_rate(struct AdapterCtlBlk
*acb
,
381 struct DeviceCtlBlk
*dcb
);
382 static void waiting_timeout(struct timer_list
*t
);
385 /*---------------------------------------------------------------------------
387 ---------------------------------------------------------------------------*/
388 static u16 current_sync_offset
= 0;
390 static void *dc395x_scsi_phase0
[] = {
391 data_out_phase0
,/* phase:0 */
392 data_in_phase0
, /* phase:1 */
393 command_phase0
, /* phase:2 */
394 status_phase0
, /* phase:3 */
395 nop0
, /* phase:4 PH_BUS_FREE .. initial phase */
396 nop0
, /* phase:5 PH_BUS_FREE .. initial phase */
397 msgout_phase0
, /* phase:6 */
398 msgin_phase0
, /* phase:7 */
401 static void *dc395x_scsi_phase1
[] = {
402 data_out_phase1
,/* phase:0 */
403 data_in_phase1
, /* phase:1 */
404 command_phase1
, /* phase:2 */
405 status_phase1
, /* phase:3 */
406 nop1
, /* phase:4 PH_BUS_FREE .. initial phase */
407 nop1
, /* phase:5 PH_BUS_FREE .. initial phase */
408 msgout_phase1
, /* phase:6 */
409 msgin_phase1
, /* phase:7 */
413 *Fast20: 000 50ns, 20.0 MHz
415 * 010 100ns, 10.0 MHz
422 *Fast40(LVDS): 000 25ns, 40.0 MHz
425 * 011 100ns, 10.0 MHz
431 /*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/
433 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
434 static u8 clock_period
[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
435 static u16 clock_speed
[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
438 /*---------------------------------------------------------------------------
440 ---------------------------------------------------------------------------*/
442 * Module/boot parameters currently effect *all* instances of the
443 * card in the system.
447 * Command line parameters are stored in a structure below.
448 * These are the index's into the structure for the various
449 * command line options.
451 #define CFG_ADAPTER_ID 0
452 #define CFG_MAX_SPEED 1
453 #define CFG_DEV_MODE 2
454 #define CFG_ADAPTER_MODE 3
456 #define CFG_RESET_DELAY 5
458 #define CFG_NUM 6 /* number of configuration items */
462 * Value used to indicate that a command line override
463 * hasn't been used to modify the value.
465 #define CFG_PARAM_UNSET -1
469 * Hold command line parameters.
471 struct ParameterData
{
472 int value
; /* value of this setting */
473 int min
; /* minimum value */
474 int max
; /* maximum value */
475 int def
; /* default value */
476 int safe
; /* safe value */
478 static struct ParameterData cfg_data
[] = {
497 NTC_DO_PARITY_CHK
| NTC_DO_DISCONNECT
| NTC_DO_SYNC_NEGO
|
498 NTC_DO_WIDE_NEGO
| NTC_DO_TAG_QUEUEING
|
500 NTC_DO_PARITY_CHK
| NTC_DO_SEND_START
507 NAC_GT2DRIVES
| NAC_GREATER_1G
| NAC_POWERON_SCSI_RESET
508 /*| NAC_ACTIVE_NEG*/,
509 NAC_GT2DRIVES
| NAC_GREATER_1G
| NAC_POWERON_SCSI_RESET
| 0x08
515 3, /* 16 tags (??) */
529 * Safe settings. If set to zero the BIOS/default values with
530 * command line overrides will be used. If set to 1 then safe and
531 * slow settings will be used.
533 static bool use_safe_settings
= 0;
534 module_param_named(safe
, use_safe_settings
, bool, 0);
535 MODULE_PARM_DESC(safe
, "Use safe and slow settings only. Default: false");
538 module_param_named(adapter_id
, cfg_data
[CFG_ADAPTER_ID
].value
, int, 0);
539 MODULE_PARM_DESC(adapter_id
, "Adapter SCSI ID. Default 7 (0-15)");
541 module_param_named(max_speed
, cfg_data
[CFG_MAX_SPEED
].value
, int, 0);
542 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");
544 module_param_named(dev_mode
, cfg_data
[CFG_DEV_MODE
].value
, int, 0);
545 MODULE_PARM_DESC(dev_mode
, "Device mode.");
547 module_param_named(adapter_mode
, cfg_data
[CFG_ADAPTER_MODE
].value
, int, 0);
548 MODULE_PARM_DESC(adapter_mode
, "Adapter mode.");
550 module_param_named(tags
, cfg_data
[CFG_TAGS
].value
, int, 0);
551 MODULE_PARM_DESC(tags
, "Number of tags (1<<x). Default 3 (0-5)");
553 module_param_named(reset_delay
, cfg_data
[CFG_RESET_DELAY
].value
, int, 0);
554 MODULE_PARM_DESC(reset_delay
, "Reset delay in seconds. Default 1 (0-180)");
558 * set_safe_settings - if the use_safe_settings option is set then
559 * set all values to the safe and slow values.
561 static void set_safe_settings(void)
563 if (use_safe_settings
)
567 dprintkl(KERN_INFO
, "Using safe settings.\n");
568 for (i
= 0; i
< CFG_NUM
; i
++)
570 cfg_data
[i
].value
= cfg_data
[i
].safe
;
577 * fix_settings - reset any boot parameters which are out of range
578 * back to the default values.
580 static void fix_settings(void)
585 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
586 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
587 cfg_data
[CFG_ADAPTER_ID
].value
,
588 cfg_data
[CFG_MAX_SPEED
].value
,
589 cfg_data
[CFG_DEV_MODE
].value
,
590 cfg_data
[CFG_ADAPTER_MODE
].value
,
591 cfg_data
[CFG_TAGS
].value
,
592 cfg_data
[CFG_RESET_DELAY
].value
);
593 for (i
= 0; i
< CFG_NUM
; i
++)
595 if (cfg_data
[i
].value
< cfg_data
[i
].min
596 || cfg_data
[i
].value
> cfg_data
[i
].max
)
597 cfg_data
[i
].value
= cfg_data
[i
].def
;
604 * Mapping from the eeprom delay index value (index into this array)
605 * to the number of actual seconds that the delay should be for.
607 static char eeprom_index_to_delay_map
[] =
608 { 1, 3, 5, 10, 16, 30, 60, 120 };
612 * eeprom_index_to_delay - Take the eeprom delay setting and convert it
613 * into a number of seconds.
615 * @eeprom: The eeprom structure in which we find the delay index to map.
617 static void eeprom_index_to_delay(struct NvRamType
*eeprom
)
619 eeprom
->delay_time
= eeprom_index_to_delay_map
[eeprom
->delay_time
];
624 * delay_to_eeprom_index - Take a delay in seconds and return the
625 * closest eeprom index which will delay for at least that amount of
628 * @delay: The delay, in seconds, to find the eeprom index for.
630 static int delay_to_eeprom_index(int delay
)
633 while (idx
< 7 && eeprom_index_to_delay_map
[idx
] < delay
)
640 * eeprom_override - Override the eeprom settings, in the provided
641 * eeprom structure, with values that have been set on the command
644 * @eeprom: The eeprom data to override with command line options.
646 static void eeprom_override(struct NvRamType
*eeprom
)
650 /* Adapter Settings */
651 if (cfg_data
[CFG_ADAPTER_ID
].value
!= CFG_PARAM_UNSET
)
652 eeprom
->scsi_id
= (u8
)cfg_data
[CFG_ADAPTER_ID
].value
;
654 if (cfg_data
[CFG_ADAPTER_MODE
].value
!= CFG_PARAM_UNSET
)
655 eeprom
->channel_cfg
= (u8
)cfg_data
[CFG_ADAPTER_MODE
].value
;
657 if (cfg_data
[CFG_RESET_DELAY
].value
!= CFG_PARAM_UNSET
)
658 eeprom
->delay_time
= delay_to_eeprom_index(
659 cfg_data
[CFG_RESET_DELAY
].value
);
661 if (cfg_data
[CFG_TAGS
].value
!= CFG_PARAM_UNSET
)
662 eeprom
->max_tag
= (u8
)cfg_data
[CFG_TAGS
].value
;
664 /* Device Settings */
665 for (id
= 0; id
< DC395x_MAX_SCSI_ID
; id
++) {
666 if (cfg_data
[CFG_DEV_MODE
].value
!= CFG_PARAM_UNSET
)
667 eeprom
->target
[id
].cfg0
=
668 (u8
)cfg_data
[CFG_DEV_MODE
].value
;
670 if (cfg_data
[CFG_MAX_SPEED
].value
!= CFG_PARAM_UNSET
)
671 eeprom
->target
[id
].period
=
672 (u8
)cfg_data
[CFG_MAX_SPEED
].value
;
678 /*---------------------------------------------------------------------------
679 ---------------------------------------------------------------------------*/
681 static unsigned int list_size(struct list_head
*head
)
683 unsigned int count
= 0;
684 struct list_head
*pos
;
685 list_for_each(pos
, head
)
691 static struct DeviceCtlBlk
*dcb_get_next(struct list_head
*head
,
692 struct DeviceCtlBlk
*pos
)
695 struct DeviceCtlBlk
* next
= NULL
;
696 struct DeviceCtlBlk
* i
;
698 if (list_empty(head
))
701 /* find supplied dcb and then select the next one */
702 list_for_each_entry(i
, head
, list
)
706 } else if (i
== pos
) {
709 /* if no next one take the head one (ie, wraparound) */
711 list_for_each_entry(i
, head
, list
) {
720 static void free_tag(struct DeviceCtlBlk
*dcb
, struct ScsiReqBlk
*srb
)
722 if (srb
->tag_number
< 255) {
723 dcb
->tag_mask
&= ~(1 << srb
->tag_number
); /* free tag mask */
724 srb
->tag_number
= 255;
729 /* Find cmd in SRB list */
730 static inline struct ScsiReqBlk
*find_cmd(struct scsi_cmnd
*cmd
,
731 struct list_head
*head
)
733 struct ScsiReqBlk
*i
;
734 list_for_each_entry(i
, head
, list
)
740 /* Sets the timer to wake us up */
741 static void waiting_set_timer(struct AdapterCtlBlk
*acb
, unsigned long to
)
743 if (timer_pending(&acb
->waiting_timer
))
745 if (time_before(jiffies
+ to
, acb
->last_reset
- HZ
/ 2))
746 acb
->waiting_timer
.expires
=
747 acb
->last_reset
- HZ
/ 2 + 1;
749 acb
->waiting_timer
.expires
= jiffies
+ to
+ 1;
750 add_timer(&acb
->waiting_timer
);
754 /* Send the next command from the waiting list to the bus */
755 static void waiting_process_next(struct AdapterCtlBlk
*acb
)
757 struct DeviceCtlBlk
*start
= NULL
;
758 struct DeviceCtlBlk
*pos
;
759 struct DeviceCtlBlk
*dcb
;
760 struct ScsiReqBlk
*srb
;
761 struct list_head
*dcb_list_head
= &acb
->dcb_list
;
764 || (acb
->acb_flag
& (RESET_DETECT
+ RESET_DONE
+ RESET_DEV
)))
767 if (timer_pending(&acb
->waiting_timer
))
768 del_timer(&acb
->waiting_timer
);
770 if (list_empty(dcb_list_head
))
774 * Find the starting dcb. Need to find it again in the list
775 * since the list may have changed since we set the ptr to it
777 list_for_each_entry(dcb
, dcb_list_head
, list
)
778 if (dcb
== acb
->dcb_run_robin
) {
783 /* This can happen! */
784 start
= list_entry(dcb_list_head
->next
, typeof(*start
), list
);
785 acb
->dcb_run_robin
= start
;
790 * Loop over the dcb, but we start somewhere (potentially) in
791 * the middle of the loop so we need to manully do this.
795 struct list_head
*waiting_list_head
= &pos
->srb_waiting_list
;
797 /* Make sure, the next another device gets scheduled ... */
798 acb
->dcb_run_robin
= dcb_get_next(dcb_list_head
,
801 if (list_empty(waiting_list_head
) ||
802 pos
->max_command
<= list_size(&pos
->srb_going_list
)) {
803 /* move to next dcb */
804 pos
= dcb_get_next(dcb_list_head
, pos
);
806 srb
= list_entry(waiting_list_head
->next
,
807 struct ScsiReqBlk
, list
);
809 /* Try to send to the bus */
810 if (!start_scsi(acb
, pos
, srb
))
811 list_move(&srb
->list
, &pos
->srb_going_list
);
813 waiting_set_timer(acb
, HZ
/50);
816 } while (pos
!= start
);
820 /* Wake up waiting queue */
821 static void waiting_timeout(struct timer_list
*t
)
824 struct AdapterCtlBlk
*acb
= from_timer(acb
, t
, waiting_timer
);
826 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb
);
827 DC395x_LOCK_IO(acb
->scsi_host
, flags
);
828 waiting_process_next(acb
);
829 DC395x_UNLOCK_IO(acb
->scsi_host
, flags
);
833 /* Get the DCB for a given ID/LUN combination */
834 static struct DeviceCtlBlk
*find_dcb(struct AdapterCtlBlk
*acb
, u8 id
, u8 lun
)
836 return acb
->children
[id
][lun
];
840 /* Send SCSI Request Block (srb) to adapter (acb) */
841 static void send_srb(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
)
843 struct DeviceCtlBlk
*dcb
= srb
->dcb
;
845 if (dcb
->max_command
<= list_size(&dcb
->srb_going_list
) ||
847 (acb
->acb_flag
& (RESET_DETECT
+ RESET_DONE
+ RESET_DEV
))) {
848 list_add_tail(&srb
->list
, &dcb
->srb_waiting_list
);
849 waiting_process_next(acb
);
853 if (!start_scsi(acb
, dcb
, srb
)) {
854 list_add_tail(&srb
->list
, &dcb
->srb_going_list
);
856 list_add(&srb
->list
, &dcb
->srb_waiting_list
);
857 waiting_set_timer(acb
, HZ
/ 50);
861 /* Prepare SRB for being sent to Device DCB w/ command *cmd */
862 static void build_srb(struct scsi_cmnd
*cmd
, struct DeviceCtlBlk
*dcb
,
863 struct ScsiReqBlk
*srb
)
866 enum dma_data_direction dir
= cmd
->sc_data_direction
;
867 dprintkdbg(DBG_0
, "build_srb: (0x%p) <%02i-%i>\n",
868 cmd
, dcb
->target_id
, dcb
->target_lun
);
873 srb
->total_xfer_length
= 0;
874 srb
->sg_bus_addr
= 0;
876 srb
->adapter_status
= 0;
877 srb
->target_status
= 0;
882 srb
->retry_count
= 0;
883 srb
->tag_number
= TAG_NONE
;
884 srb
->scsi_phase
= PH_BUS_FREE
; /* initial phase */
885 srb
->end_message
= 0;
887 nseg
= scsi_dma_map(cmd
);
890 if (dir
== DMA_NONE
|| !nseg
) {
892 "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
893 cmd
->bufflen
, scsi_sglist(cmd
), scsi_sg_count(cmd
),
894 srb
->segment_x
[0].address
);
897 u32 reqlen
= scsi_bufflen(cmd
);
898 struct scatterlist
*sg
;
899 struct SGentry
*sgp
= srb
->segment_x
;
901 srb
->sg_count
= nseg
;
904 "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
905 reqlen
, scsi_sglist(cmd
), scsi_sg_count(cmd
),
908 scsi_for_each_sg(cmd
, sg
, srb
->sg_count
, i
) {
909 u32 busaddr
= (u32
)sg_dma_address(sg
);
910 u32 seglen
= (u32
)sg
->length
;
911 sgp
[i
].address
= busaddr
;
912 sgp
[i
].length
= seglen
;
913 srb
->total_xfer_length
+= seglen
;
915 sgp
+= srb
->sg_count
- 1;
918 * adjust last page if too big as it is allocated
919 * on even page boundaries
921 if (srb
->total_xfer_length
> reqlen
) {
922 sgp
->length
-= (srb
->total_xfer_length
- reqlen
);
923 srb
->total_xfer_length
= reqlen
;
926 /* Fixup for WIDE padding - make sure length is even */
927 if (dcb
->sync_period
& WIDE_SYNC
&&
928 srb
->total_xfer_length
% 2) {
929 srb
->total_xfer_length
++;
933 srb
->sg_bus_addr
= dma_map_single(&dcb
->acb
->dev
->dev
,
934 srb
->segment_x
, SEGMENTX_LEN
, DMA_TO_DEVICE
);
936 dprintkdbg(DBG_SG
, "build_srb: [n] map sg %p->%08x(%05x)\n",
937 srb
->segment_x
, srb
->sg_bus_addr
, SEGMENTX_LEN
);
940 srb
->request_length
= srb
->total_xfer_length
;
945 * dc395x_queue_command_lck - queue scsi command passed from the mid
946 * layer, invoke 'done' on completion
948 * @cmd: pointer to scsi command object
950 * Returns 1 if the adapter (host) is busy, else returns 0. One
951 * reason for an adapter to be busy is that the number
952 * of outstanding queued commands is already equal to
953 * struct Scsi_Host::can_queue .
955 * Required: if struct Scsi_Host::can_queue is ever non-zero
956 * then this function is required.
958 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
959 * and is expected to be held on return.
962 static int dc395x_queue_command_lck(struct scsi_cmnd
*cmd
)
964 void (*done
)(struct scsi_cmnd
*) = scsi_done
;
965 struct DeviceCtlBlk
*dcb
;
966 struct ScsiReqBlk
*srb
;
967 struct AdapterCtlBlk
*acb
=
968 (struct AdapterCtlBlk
*)cmd
->device
->host
->hostdata
;
969 dprintkdbg(DBG_0
, "queue_command: (0x%p) <%02i-%i> cmnd=0x%02x\n",
970 cmd
, cmd
->device
->id
, (u8
)cmd
->device
->lun
, cmd
->cmnd
[0]);
972 /* Assume BAD_TARGET; will be cleared later */
973 set_host_byte(cmd
, DID_BAD_TARGET
);
975 /* ignore invalid targets */
976 if (cmd
->device
->id
>= acb
->scsi_host
->max_id
||
977 cmd
->device
->lun
>= acb
->scsi_host
->max_lun
||
978 cmd
->device
->lun
>31) {
982 /* does the specified lun on the specified device exist */
983 if (!(acb
->dcb_map
[cmd
->device
->id
] & (1 << cmd
->device
->lun
))) {
984 dprintkl(KERN_INFO
, "queue_command: Ignore target <%02i-%i>\n",
985 cmd
->device
->id
, (u8
)cmd
->device
->lun
);
989 /* do we have a DCB for the device */
990 dcb
= find_dcb(acb
, cmd
->device
->id
, cmd
->device
->lun
);
992 /* should never happen */
993 dprintkl(KERN_ERR
, "queue_command: No such device <%02i-%i>",
994 cmd
->device
->id
, (u8
)cmd
->device
->lun
);
998 set_host_byte(cmd
, DID_OK
);
999 set_status_byte(cmd
, SAM_STAT_GOOD
);
1001 srb
= list_first_entry_or_null(&acb
->srb_free_list
,
1002 struct ScsiReqBlk
, list
);
1005 * Return 1 since we are unable to queue this command at this
1008 dprintkdbg(DBG_0
, "queue_command: No free srb's\n");
1011 list_del(&srb
->list
);
1013 build_srb(cmd
, dcb
, srb
);
1015 if (!list_empty(&dcb
->srb_waiting_list
)) {
1016 /* append to waiting queue */
1017 list_add_tail(&srb
->list
, &dcb
->srb_waiting_list
);
1018 waiting_process_next(acb
);
1020 /* process immediately */
1023 dprintkdbg(DBG_1
, "queue_command: (0x%p) done\n", cmd
);
1028 * Complete the command immediatey, and then return 0 to
1029 * indicate that we have handled the command. This is usually
1030 * done when the commad is for things like non existent
1037 static DEF_SCSI_QCMD(dc395x_queue_command
)
1039 static void dump_register_info(struct AdapterCtlBlk
*acb
,
1040 struct DeviceCtlBlk
*dcb
, struct ScsiReqBlk
*srb
)
1043 struct pci_dev
*dev
= acb
->dev
;
1044 pci_read_config_word(dev
, PCI_STATUS
, &pstat
);
1046 dcb
= acb
->active_dcb
;
1048 srb
= dcb
->active_srb
;
1051 dprintkl(KERN_INFO
, "dump: srb=%p cmd=%p OOOPS!\n",
1054 dprintkl(KERN_INFO
, "dump: srb=%p cmd=%p "
1055 "cmnd=0x%02x <%02i-%i>\n",
1057 srb
->cmd
->cmnd
[0], srb
->cmd
->device
->id
,
1058 (u8
)srb
->cmd
->device
->lun
);
1059 printk(" sglist=%p cnt=%i idx=%i len=%zu\n",
1060 srb
->segment_x
, srb
->sg_count
, srb
->sg_index
,
1061 srb
->total_xfer_length
);
1062 printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1063 srb
->state
, srb
->status
, srb
->scsi_phase
,
1064 (acb
->active_dcb
) ? "" : "not");
1066 dprintkl(KERN_INFO
, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1067 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1068 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1069 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1070 DC395x_read16(acb
, TRM_S1040_SCSI_STATUS
),
1071 DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
),
1072 DC395x_read8(acb
, TRM_S1040_SCSI_SIGNAL
),
1073 DC395x_read8(acb
, TRM_S1040_SCSI_INTSTATUS
),
1074 DC395x_read8(acb
, TRM_S1040_SCSI_SYNC
),
1075 DC395x_read8(acb
, TRM_S1040_SCSI_TARGETID
),
1076 DC395x_read8(acb
, TRM_S1040_SCSI_IDMSG
),
1077 DC395x_read32(acb
, TRM_S1040_SCSI_COUNTER
),
1078 DC395x_read8(acb
, TRM_S1040_SCSI_INTEN
),
1079 DC395x_read16(acb
, TRM_S1040_SCSI_CONFIG0
),
1080 DC395x_read8(acb
, TRM_S1040_SCSI_CONFIG2
),
1081 DC395x_read8(acb
, TRM_S1040_SCSI_COMMAND
),
1082 DC395x_read8(acb
, TRM_S1040_SCSI_TIMEOUT
));
1083 dprintkl(KERN_INFO
, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1084 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1085 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1086 DC395x_read16(acb
, TRM_S1040_DMA_COMMAND
),
1087 DC395x_read8(acb
, TRM_S1040_DMA_FIFOCNT
),
1088 DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
),
1089 DC395x_read8(acb
, TRM_S1040_DMA_STATUS
),
1090 DC395x_read8(acb
, TRM_S1040_DMA_INTEN
),
1091 DC395x_read16(acb
, TRM_S1040_DMA_CONFIG
),
1092 DC395x_read32(acb
, TRM_S1040_DMA_XCNT
),
1093 DC395x_read32(acb
, TRM_S1040_DMA_CXCNT
),
1094 DC395x_read32(acb
, TRM_S1040_DMA_XHIGHADDR
),
1095 DC395x_read32(acb
, TRM_S1040_DMA_XLOWADDR
));
1096 dprintkl(KERN_INFO
, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1097 "pci{status=0x%04x}\n",
1098 DC395x_read8(acb
, TRM_S1040_GEN_CONTROL
),
1099 DC395x_read8(acb
, TRM_S1040_GEN_STATUS
),
1100 DC395x_read8(acb
, TRM_S1040_GEN_TIMER
),
1105 static inline void clear_fifo(struct AdapterCtlBlk
*acb
, char *txt
)
1107 #if debug_enabled(DBG_FIFO)
1108 u8 lines
= DC395x_read8(acb
, TRM_S1040_SCSI_SIGNAL
);
1109 u8 fifocnt
= DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
);
1110 if (!(fifocnt
& 0x40))
1111 dprintkdbg(DBG_FIFO
,
1112 "clear_fifo: (%i bytes) on phase %02x in %s\n",
1113 fifocnt
& 0x3f, lines
, txt
);
1115 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_CLRFIFO
);
1119 static void reset_dev_param(struct AdapterCtlBlk
*acb
)
1121 struct DeviceCtlBlk
*dcb
;
1122 struct NvRamType
*eeprom
= &acb
->eeprom
;
1123 dprintkdbg(DBG_0
, "reset_dev_param: acb=%p\n", acb
);
1125 list_for_each_entry(dcb
, &acb
->dcb_list
, list
) {
1128 dcb
->sync_mode
&= ~(SYNC_NEGO_DONE
+ WIDE_NEGO_DONE
);
1129 dcb
->sync_period
= 0;
1130 dcb
->sync_offset
= 0;
1132 dcb
->dev_mode
= eeprom
->target
[dcb
->target_id
].cfg0
;
1133 period_index
= eeprom
->target
[dcb
->target_id
].period
& 0x07;
1134 dcb
->min_nego_period
= clock_period
[period_index
];
1135 if (!(dcb
->dev_mode
& NTC_DO_WIDE_NEGO
)
1136 || !(acb
->config
& HCC_WIDE_CARD
))
1137 dcb
->sync_mode
&= ~WIDE_NEGO_ENABLE
;
1143 * perform a hard reset on the SCSI bus
1144 * @cmd - some command for this host (for fetching hooks)
1145 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1147 static int __dc395x_eh_bus_reset(struct scsi_cmnd
*cmd
)
1149 struct AdapterCtlBlk
*acb
=
1150 (struct AdapterCtlBlk
*)cmd
->device
->host
->hostdata
;
1152 "eh_bus_reset: (0%p) target=<%02i-%i> cmd=%p\n",
1153 cmd
, cmd
->device
->id
, (u8
)cmd
->device
->lun
, cmd
);
1155 if (timer_pending(&acb
->waiting_timer
))
1156 del_timer(&acb
->waiting_timer
);
1161 DC395x_write8(acb
, TRM_S1040_DMA_INTEN
, 0x00);
1162 DC395x_write8(acb
, TRM_S1040_SCSI_INTEN
, 0x00);
1163 DC395x_write8(acb
, TRM_S1040_SCSI_CONTROL
, DO_RSTMODULE
);
1164 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, DMARESETMODULE
);
1166 reset_scsi_bus(acb
);
1169 /* We may be in serious trouble. Wait some seconds */
1171 jiffies
+ 3 * HZ
/ 2 +
1172 HZ
* acb
->eeprom
.delay_time
;
1175 * re-enable interrupt
1177 /* Clear SCSI FIFO */
1178 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, CLRXFIFO
);
1179 clear_fifo(acb
, "eh_bus_reset");
1180 /* Delete pending IRQ */
1181 DC395x_read8(acb
, TRM_S1040_SCSI_INTSTATUS
);
1182 set_basic_config(acb
);
1184 reset_dev_param(acb
);
1185 doing_srb_done(acb
, DID_RESET
, cmd
, 0);
1186 acb
->active_dcb
= NULL
;
1187 acb
->acb_flag
= 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1188 waiting_process_next(acb
);
1193 static int dc395x_eh_bus_reset(struct scsi_cmnd
*cmd
)
1197 spin_lock_irq(cmd
->device
->host
->host_lock
);
1198 rc
= __dc395x_eh_bus_reset(cmd
);
1199 spin_unlock_irq(cmd
->device
->host
->host_lock
);
1205 * abort an errant SCSI command
1206 * @cmd - command to be aborted
1207 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1209 static int dc395x_eh_abort(struct scsi_cmnd
*cmd
)
1212 * Look into our command queues: If it has not been sent already,
1213 * we remove it and return success. Otherwise fail.
1215 struct AdapterCtlBlk
*acb
=
1216 (struct AdapterCtlBlk
*)cmd
->device
->host
->hostdata
;
1217 struct DeviceCtlBlk
*dcb
;
1218 struct ScsiReqBlk
*srb
;
1219 dprintkl(KERN_INFO
, "eh_abort: (0x%p) target=<%02i-%i> cmd=%p\n",
1220 cmd
, cmd
->device
->id
, (u8
)cmd
->device
->lun
, cmd
);
1222 dcb
= find_dcb(acb
, cmd
->device
->id
, cmd
->device
->lun
);
1224 dprintkl(KERN_DEBUG
, "eh_abort: No such device\n");
1228 srb
= find_cmd(cmd
, &dcb
->srb_waiting_list
);
1230 list_del(&srb
->list
);
1231 pci_unmap_srb_sense(acb
, srb
);
1232 pci_unmap_srb(acb
, srb
);
1234 list_add_tail(&srb
->list
, &acb
->srb_free_list
);
1235 dprintkl(KERN_DEBUG
, "eh_abort: Command was waiting\n");
1236 set_host_byte(cmd
, DID_ABORT
);
1239 srb
= find_cmd(cmd
, &dcb
->srb_going_list
);
1241 dprintkl(KERN_DEBUG
, "eh_abort: Command in progress\n");
1242 /* XXX: Should abort the command here */
1244 dprintkl(KERN_DEBUG
, "eh_abort: Command not found\n");
1251 static void build_sdtr(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
1252 struct ScsiReqBlk
*srb
)
1254 u8
*ptr
= srb
->msgout_buf
+ srb
->msg_count
;
1255 if (srb
->msg_count
> 1) {
1257 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1258 srb
->msg_count
, srb
->msgout_buf
[0],
1259 srb
->msgout_buf
[1]);
1262 if (!(dcb
->dev_mode
& NTC_DO_SYNC_NEGO
)) {
1263 dcb
->sync_offset
= 0;
1264 dcb
->min_nego_period
= 200 >> 2;
1265 } else if (dcb
->sync_offset
== 0)
1266 dcb
->sync_offset
= SYNC_NEGO_OFFSET
;
1268 srb
->msg_count
+= spi_populate_sync_msg(ptr
, dcb
->min_nego_period
,
1270 srb
->state
|= SRB_DO_SYNC_NEGO
;
1275 static void build_wdtr(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
1276 struct ScsiReqBlk
*srb
)
1278 u8 wide
= ((dcb
->dev_mode
& NTC_DO_WIDE_NEGO
) &
1279 (acb
->config
& HCC_WIDE_CARD
)) ? 1 : 0;
1280 u8
*ptr
= srb
->msgout_buf
+ srb
->msg_count
;
1281 if (srb
->msg_count
> 1) {
1283 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1284 srb
->msg_count
, srb
->msgout_buf
[0],
1285 srb
->msgout_buf
[1]);
1288 srb
->msg_count
+= spi_populate_width_msg(ptr
, wide
);
1289 srb
->state
|= SRB_DO_WIDE_NEGO
;
1294 /* Timer to work around chip flaw: When selecting and the bus is
1295 * busy, we sometimes miss a Selection timeout IRQ */
1296 void selection_timeout_missed(unsigned long ptr
);
1297 /* Sets the timer to wake us up */
1298 static void selto_timer(struct AdapterCtlBlk
*acb
)
1300 if (timer_pending(&acb
->selto_timer
))
1302 acb
->selto_timer
.function
= selection_timeout_missed
;
1303 acb
->selto_timer
.data
= (unsigned long) acb
;
1305 (jiffies
+ HZ
, acb
->last_reset
+ HZ
/ 2))
1306 acb
->selto_timer
.expires
=
1307 acb
->last_reset
+ HZ
/ 2 + 1;
1309 acb
->selto_timer
.expires
= jiffies
+ HZ
+ 1;
1310 add_timer(&acb
->selto_timer
);
1314 void selection_timeout_missed(unsigned long ptr
)
1316 unsigned long flags
;
1317 struct AdapterCtlBlk
*acb
= (struct AdapterCtlBlk
*)ptr
;
1318 struct ScsiReqBlk
*srb
;
1319 dprintkl(KERN_DEBUG
, "Chip forgot to produce SelTO IRQ!\n");
1320 if (!acb
->active_dcb
|| !acb
->active_dcb
->active_srb
) {
1321 dprintkl(KERN_DEBUG
, "... but no cmd pending? Oops!\n");
1324 DC395x_LOCK_IO(acb
->scsi_host
, flags
);
1325 srb
= acb
->active_dcb
->active_srb
;
1327 DC395x_UNLOCK_IO(acb
->scsi_host
, flags
);
1332 static u8
start_scsi(struct AdapterCtlBlk
* acb
, struct DeviceCtlBlk
* dcb
,
1333 struct ScsiReqBlk
* srb
)
1335 u16 __maybe_unused s_stat2
, return_code
;
1336 u8 s_stat
, scsicommand
, i
, identify_message
;
1338 dprintkdbg(DBG_0
, "start_scsi: (0x%p) <%02i-%i> srb=%p\n",
1339 dcb
->target_id
, dcb
->target_lun
, srb
);
1341 srb
->tag_number
= TAG_NONE
; /* acb->tag_max_num: had error read in eeprom */
1343 s_stat
= DC395x_read8(acb
, TRM_S1040_SCSI_SIGNAL
);
1345 s_stat2
= DC395x_read16(acb
, TRM_S1040_SCSI_STATUS
);
1347 if (s_stat
& 0x20 /* s_stat2 & 0x02000 */ ) {
1348 dprintkdbg(DBG_KG
, "start_scsi: (0x%p) BUSY %02x %04x\n",
1353 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1354 * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
1355 * (This is likely to be a bug in the hardware. Obviously, most people
1356 * only have one initiator per SCSI bus.)
1357 * Instead let this fail and have the timer make sure the command is
1358 * tried again after a short time
1360 /*selto_timer (acb); */
1364 if (acb
->active_dcb
) {
1365 dprintkl(KERN_DEBUG
, "start_scsi: (0x%p) Attempt to start a"
1366 "command while another command (0x%p) is active.",
1368 acb
->active_dcb
->active_srb
?
1369 acb
->active_dcb
->active_srb
->cmd
: NULL
);
1372 if (DC395x_read16(acb
, TRM_S1040_SCSI_STATUS
) & SCSIINTERRUPT
) {
1373 dprintkdbg(DBG_KG
, "start_scsi: (0x%p) Failed (busy)\n", srb
->cmd
);
1376 /* Allow starting of SCSI commands half a second before we allow the mid-level
1377 * to queue them again after a reset */
1378 if (time_before(jiffies
, acb
->last_reset
- HZ
/ 2)) {
1379 dprintkdbg(DBG_KG
, "start_scsi: Refuse cmds (reset wait)\n");
1384 clear_fifo(acb
, "start_scsi");
1385 DC395x_write8(acb
, TRM_S1040_SCSI_HOSTID
, acb
->scsi_host
->this_id
);
1386 DC395x_write8(acb
, TRM_S1040_SCSI_TARGETID
, dcb
->target_id
);
1387 DC395x_write8(acb
, TRM_S1040_SCSI_SYNC
, dcb
->sync_period
);
1388 DC395x_write8(acb
, TRM_S1040_SCSI_OFFSET
, dcb
->sync_offset
);
1389 srb
->scsi_phase
= PH_BUS_FREE
; /* initial phase */
1391 identify_message
= dcb
->identify_msg
;
1392 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1393 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1394 if (srb
->flag
& AUTO_REQSENSE
)
1395 identify_message
&= 0xBF;
1397 if (((srb
->cmd
->cmnd
[0] == INQUIRY
)
1398 || (srb
->cmd
->cmnd
[0] == REQUEST_SENSE
)
1399 || (srb
->flag
& AUTO_REQSENSE
))
1400 && (((dcb
->sync_mode
& WIDE_NEGO_ENABLE
)
1401 && !(dcb
->sync_mode
& WIDE_NEGO_DONE
))
1402 || ((dcb
->sync_mode
& SYNC_NEGO_ENABLE
)
1403 && !(dcb
->sync_mode
& SYNC_NEGO_DONE
)))
1404 && (dcb
->target_lun
== 0)) {
1405 srb
->msgout_buf
[0] = identify_message
;
1407 scsicommand
= SCMD_SEL_ATNSTOP
;
1408 srb
->state
= SRB_MSGOUT
;
1410 if (dcb
->sync_mode
& WIDE_NEGO_ENABLE
1411 && dcb
->inquiry7
& SCSI_INQ_WBUS16
) {
1412 build_wdtr(acb
, dcb
, srb
);
1416 if (dcb
->sync_mode
& SYNC_NEGO_ENABLE
1417 && dcb
->inquiry7
& SCSI_INQ_SYNC
) {
1418 build_sdtr(acb
, dcb
, srb
);
1421 if (dcb
->sync_mode
& WIDE_NEGO_ENABLE
1422 && dcb
->inquiry7
& SCSI_INQ_WBUS16
) {
1423 build_wdtr(acb
, dcb
, srb
);
1428 /* Send identify message */
1429 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, identify_message
);
1431 scsicommand
= SCMD_SEL_ATN
;
1432 srb
->state
= SRB_START_
;
1433 #ifndef DC395x_NO_TAGQ
1434 if ((dcb
->sync_mode
& EN_TAG_QUEUEING
)
1435 && (identify_message
& 0xC0)) {
1436 /* Send Tag message */
1439 while (tag_mask
& dcb
->tag_mask
1440 && tag_number
< dcb
->max_command
) {
1441 tag_mask
= tag_mask
<< 1;
1444 if (tag_number
>= dcb
->max_command
) {
1445 dprintkl(KERN_WARNING
, "start_scsi: (0x%p) "
1446 "Out of tags target=<%02i-%i>)\n",
1447 srb
->cmd
, srb
->cmd
->device
->id
,
1448 (u8
)srb
->cmd
->device
->lun
);
1449 srb
->state
= SRB_READY
;
1450 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
,
1455 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, SIMPLE_QUEUE_TAG
);
1456 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, tag_number
);
1457 dcb
->tag_mask
|= tag_mask
;
1458 srb
->tag_number
= tag_number
;
1459 scsicommand
= SCMD_SEL_ATN3
;
1460 srb
->state
= SRB_START_
;
1464 /* Send CDB ..command block ......... */
1465 dprintkdbg(DBG_KG
, "start_scsi: (0x%p) <%02i-%i> cmnd=0x%02x tag=%i\n",
1466 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
,
1467 srb
->cmd
->cmnd
[0], srb
->tag_number
);
1468 if (srb
->flag
& AUTO_REQSENSE
) {
1469 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, REQUEST_SENSE
);
1470 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, (dcb
->target_lun
<< 5));
1471 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 0);
1472 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 0);
1473 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, SCSI_SENSE_BUFFERSIZE
);
1474 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 0);
1476 ptr
= (u8
*)srb
->cmd
->cmnd
;
1477 for (i
= 0; i
< srb
->cmd
->cmd_len
; i
++)
1478 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, *ptr
++);
1481 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
,
1482 DO_HWRESELECT
| DO_DATALATCH
);
1483 if (DC395x_read16(acb
, TRM_S1040_SCSI_STATUS
) & SCSIINTERRUPT
) {
1485 * If start_scsi return 1:
1486 * we caught an interrupt (must be reset or reselection ... )
1487 * : Let's process it first!
1489 dprintkdbg(DBG_0
, "start_scsi: (0x%p) <%02i-%i> Failed - busy\n",
1490 srb
->cmd
, dcb
->target_id
, dcb
->target_lun
);
1491 srb
->state
= SRB_READY
;
1495 /* This IRQ should NOT get lost, as we did not acknowledge it */
1498 * If start_scsi returns 0:
1499 * we know that the SCSI processor is free
1501 srb
->scsi_phase
= PH_BUS_FREE
; /* initial phase */
1502 dcb
->active_srb
= srb
;
1503 acb
->active_dcb
= dcb
;
1505 /* it's important for atn stop */
1506 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
,
1507 DO_DATALATCH
| DO_HWRESELECT
);
1509 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, scsicommand
);
1515 #define DC395x_ENABLE_MSGOUT \
1516 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1517 srb->state |= SRB_MSGOUT
1521 static inline void enable_msgout_abort(struct AdapterCtlBlk
*acb
,
1522 struct ScsiReqBlk
*srb
)
1524 srb
->msgout_buf
[0] = ABORT
;
1526 DC395x_ENABLE_MSGOUT
;
1527 srb
->state
&= ~SRB_MSGIN
;
1528 srb
->state
|= SRB_MSGOUT
;
1533 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1534 * have been triggered for this card.
1536 * @acb: a pointer to the adpter control block
1537 * @scsi_status: the status return when we checked the card
1539 static void dc395x_handle_interrupt(struct AdapterCtlBlk
*acb
,
1542 struct DeviceCtlBlk
*dcb
;
1543 struct ScsiReqBlk
*srb
;
1546 unsigned long flags
;
1547 void (*dc395x_statev
)(struct AdapterCtlBlk
*, struct ScsiReqBlk
*,
1550 DC395x_LOCK_IO(acb
->scsi_host
, flags
);
1552 /* This acknowledges the IRQ */
1553 scsi_intstatus
= DC395x_read8(acb
, TRM_S1040_SCSI_INTSTATUS
);
1554 if ((scsi_status
& 0x2007) == 0x2002)
1555 dprintkl(KERN_DEBUG
,
1556 "COP after COP completed? %04x\n", scsi_status
);
1557 if (debug_enabled(DBG_KG
)) {
1558 if (scsi_intstatus
& INT_SELTIMEOUT
)
1559 dprintkdbg(DBG_KG
, "handle_interrupt: Selection timeout\n");
1561 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1563 if (timer_pending(&acb
->selto_timer
))
1564 del_timer(&acb
->selto_timer
);
1566 if (scsi_intstatus
& (INT_SELTIMEOUT
| INT_DISCONNECT
)) {
1567 disconnect(acb
); /* bus free interrupt */
1570 if (scsi_intstatus
& INT_RESELECTED
) {
1574 if (scsi_intstatus
& INT_SELECT
) {
1575 dprintkl(KERN_INFO
, "Host does not support target mode!\n");
1578 if (scsi_intstatus
& INT_SCSIRESET
) {
1579 scsi_reset_detect(acb
);
1582 if (scsi_intstatus
& (INT_BUSSERVICE
| INT_CMDDONE
)) {
1583 dcb
= acb
->active_dcb
;
1585 dprintkl(KERN_DEBUG
,
1586 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1587 scsi_status
, scsi_intstatus
);
1590 srb
= dcb
->active_srb
;
1591 if (dcb
->flag
& ABORT_DEV_
) {
1592 dprintkdbg(DBG_0
, "MsgOut Abort Device.....\n");
1593 enable_msgout_abort(acb
, srb
);
1596 /* software sequential machine */
1597 phase
= (u16
)srb
->scsi_phase
;
1601 * call dc395x_scsi_phase0[]... "phase entry"
1602 * handle every phase before start transfer
1604 /* data_out_phase0, phase:0 */
1605 /* data_in_phase0, phase:1 */
1606 /* command_phase0, phase:2 */
1607 /* status_phase0, phase:3 */
1608 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
1609 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
1610 /* msgout_phase0, phase:6 */
1611 /* msgin_phase0, phase:7 */
1612 dc395x_statev
= dc395x_scsi_phase0
[phase
];
1613 dc395x_statev(acb
, srb
, &scsi_status
);
1616 * if there were any exception occurred scsi_status
1617 * will be modify to bus free phase new scsi_status
1618 * transfer out from ... previous dc395x_statev
1620 srb
->scsi_phase
= scsi_status
& PHASEMASK
;
1621 phase
= (u16
)scsi_status
& PHASEMASK
;
1624 * call dc395x_scsi_phase1[]... "phase entry" handle
1625 * every phase to do transfer
1627 /* data_out_phase1, phase:0 */
1628 /* data_in_phase1, phase:1 */
1629 /* command_phase1, phase:2 */
1630 /* status_phase1, phase:3 */
1631 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
1632 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
1633 /* msgout_phase1, phase:6 */
1634 /* msgin_phase1, phase:7 */
1635 dc395x_statev
= dc395x_scsi_phase1
[phase
];
1636 dc395x_statev(acb
, srb
, &scsi_status
);
1639 DC395x_UNLOCK_IO(acb
->scsi_host
, flags
);
1643 static irqreturn_t
dc395x_interrupt(int irq
, void *dev_id
)
1645 struct AdapterCtlBlk
*acb
= dev_id
;
1648 irqreturn_t handled
= IRQ_NONE
;
1651 * Check for pending interrupt
1653 scsi_status
= DC395x_read16(acb
, TRM_S1040_SCSI_STATUS
);
1654 dma_status
= DC395x_read8(acb
, TRM_S1040_DMA_STATUS
);
1655 if (scsi_status
& SCSIINTERRUPT
) {
1656 /* interrupt pending - let's process it! */
1657 dc395x_handle_interrupt(acb
, scsi_status
);
1658 handled
= IRQ_HANDLED
;
1660 else if (dma_status
& 0x20) {
1661 /* Error from the DMA engine */
1662 dprintkl(KERN_INFO
, "Interrupt from DMA engine: 0x%02x!\n", dma_status
);
1664 dprintkl(KERN_INFO
, "This means DMA error! Try to handle ...\n");
1665 if (acb
->active_dcb
) {
1666 acb
->active_dcb
-> flag
|= ABORT_DEV_
;
1667 if (acb
->active_dcb
->active_srb
)
1668 enable_msgout_abort(acb
, acb
->active_dcb
->active_srb
);
1670 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, ABORTXFER
| CLRXFIFO
);
1672 dprintkl(KERN_INFO
, "Ignoring DMA error (probably a bad thing) ...\n");
1675 handled
= IRQ_HANDLED
;
1682 static void msgout_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
1685 dprintkdbg(DBG_0
, "msgout_phase0: (0x%p)\n", srb
->cmd
);
1686 if (srb
->state
& (SRB_UNEXPECT_RESEL
+ SRB_ABORT_SENT
))
1687 *pscsi_status
= PH_BUS_FREE
; /*.. initial phase */
1689 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
1690 srb
->state
&= ~SRB_MSGOUT
;
1694 static void msgout_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
1699 dprintkdbg(DBG_0
, "msgout_phase1: (0x%p)\n", srb
->cmd
);
1701 clear_fifo(acb
, "msgout_phase1");
1702 if (!(srb
->state
& SRB_MSGOUT
)) {
1703 srb
->state
|= SRB_MSGOUT
;
1704 dprintkl(KERN_DEBUG
,
1705 "msgout_phase1: (0x%p) Phase unexpected\n",
1706 srb
->cmd
); /* So what ? */
1708 if (!srb
->msg_count
) {
1709 dprintkdbg(DBG_0
, "msgout_phase1: (0x%p) NOP msg\n",
1711 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, NOP
);
1712 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
);
1713 /* it's important for atn stop */
1714 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_FIFO_OUT
);
1717 ptr
= (u8
*)srb
->msgout_buf
;
1718 for (i
= 0; i
< srb
->msg_count
; i
++)
1719 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, *ptr
++);
1721 if (srb
->msgout_buf
[0] == ABORT_TASK_SET
)
1722 srb
->state
= SRB_ABORT_SENT
;
1724 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_FIFO_OUT
);
1728 static void command_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
1731 dprintkdbg(DBG_0
, "command_phase0: (0x%p)\n", srb
->cmd
);
1732 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
);
1736 static void command_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
1739 struct DeviceCtlBlk
*dcb
;
1742 dprintkdbg(DBG_0
, "command_phase1: (0x%p)\n", srb
->cmd
);
1744 clear_fifo(acb
, "command_phase1");
1745 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_CLRATN
);
1746 if (!(srb
->flag
& AUTO_REQSENSE
)) {
1747 ptr
= (u8
*)srb
->cmd
->cmnd
;
1748 for (i
= 0; i
< srb
->cmd
->cmd_len
; i
++) {
1749 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, *ptr
);
1753 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, REQUEST_SENSE
);
1754 dcb
= acb
->active_dcb
;
1756 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, (dcb
->target_lun
<< 5));
1757 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 0);
1758 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 0);
1759 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, SCSI_SENSE_BUFFERSIZE
);
1760 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 0);
1762 srb
->state
|= SRB_COMMAND
;
1763 /* it's important for atn stop */
1764 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
);
1766 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_FIFO_OUT
);
1771 * Verify that the remaining space in the hw sg lists is the same as
1772 * the count of remaining bytes in srb->total_xfer_length
1774 static void sg_verify_length(struct ScsiReqBlk
*srb
)
1776 if (debug_enabled(DBG_SG
)) {
1778 unsigned idx
= srb
->sg_index
;
1779 struct SGentry
*psge
= srb
->segment_x
+ idx
;
1780 for (; idx
< srb
->sg_count
; psge
++, idx
++)
1781 len
+= psge
->length
;
1782 if (len
!= srb
->total_xfer_length
)
1784 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1785 srb
->total_xfer_length
, len
);
1791 * Compute the next Scatter Gather list index and adjust its length
1792 * and address if necessary
1794 static void sg_update_list(struct ScsiReqBlk
*srb
, u32 left
)
1797 u32 xferred
= srb
->total_xfer_length
- left
; /* bytes transferred */
1798 struct SGentry
*psge
= srb
->segment_x
+ srb
->sg_index
;
1801 "sg_update_list: Transferred %i of %i bytes, %i remain\n",
1802 xferred
, srb
->total_xfer_length
, left
);
1804 /* nothing to update since we did not transfer any data */
1808 sg_verify_length(srb
);
1809 srb
->total_xfer_length
= left
; /* update remaining count */
1810 for (idx
= srb
->sg_index
; idx
< srb
->sg_count
; idx
++) {
1811 if (xferred
>= psge
->length
) {
1812 /* Complete SG entries done */
1813 xferred
-= psge
->length
;
1815 /* Partial SG entry done */
1816 dma_sync_single_for_cpu(&srb
->dcb
->acb
->dev
->dev
,
1817 srb
->sg_bus_addr
, SEGMENTX_LEN
,
1819 psge
->length
-= xferred
;
1820 psge
->address
+= xferred
;
1821 srb
->sg_index
= idx
;
1822 dma_sync_single_for_device(&srb
->dcb
->acb
->dev
->dev
,
1823 srb
->sg_bus_addr
, SEGMENTX_LEN
,
1829 sg_verify_length(srb
);
1834 * We have transferred a single byte (PIO mode?) and need to update
1835 * the count of bytes remaining (total_xfer_length) and update the sg
1836 * entry to either point to next byte in the current sg entry, or of
1837 * already at the end to point to the start of the next sg entry
1839 static void sg_subtract_one(struct ScsiReqBlk
*srb
)
1841 sg_update_list(srb
, srb
->total_xfer_length
- 1);
1846 * cleanup_after_transfer
1848 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
1849 * KG: Currently called from StatusPhase1 ()
1850 * Should probably also be called from other places
1851 * Best might be to call it in DataXXPhase0, if new phase will differ
1853 static void cleanup_after_transfer(struct AdapterCtlBlk
*acb
,
1854 struct ScsiReqBlk
*srb
)
1856 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
1857 if (DC395x_read16(acb
, TRM_S1040_DMA_COMMAND
) & 0x0001) { /* read */
1858 if (!(DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
) & 0x40))
1859 clear_fifo(acb
, "cleanup/in");
1860 if (!(DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
) & 0x80))
1861 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, CLRXFIFO
);
1862 } else { /* write */
1863 if (!(DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
) & 0x80))
1864 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, CLRXFIFO
);
1865 if (!(DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
) & 0x40))
1866 clear_fifo(acb
, "cleanup/out");
1868 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
);
1873 * Those no of bytes will be transferred w/ PIO through the SCSI FIFO
1874 * Seems to be needed for unknown reasons; could be a hardware bug :-(
1876 #define DC395x_LASTPIO 4
1879 static void data_out_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
1882 struct DeviceCtlBlk
*dcb
= srb
->dcb
;
1883 u16 scsi_status
= *pscsi_status
;
1884 u32 d_left_counter
= 0;
1885 dprintkdbg(DBG_0
, "data_out_phase0: (0x%p) <%02i-%i>\n",
1886 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
);
1889 * KG: We need to drain the buffers before we draw any conclusions!
1890 * This means telling the DMA to push the rest into SCSI, telling
1891 * SCSI to push the rest to the bus.
1892 * However, the device might have been the one to stop us (phase
1893 * change), and the data in transit just needs to be accounted so
1894 * it can be retransmitted.)
1897 * KG: Stop DMA engine pushing more data into the SCSI FIFO
1898 * If we need more data, the DMA SG list will be freshly set up, anyway
1900 dprintkdbg(DBG_PIO
, "data_out_phase0: "
1901 "DMA{fifocnt=0x%02x fifostat=0x%02x} "
1902 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
1903 DC395x_read8(acb
, TRM_S1040_DMA_FIFOCNT
),
1904 DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
),
1905 DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
),
1906 DC395x_read32(acb
, TRM_S1040_SCSI_COUNTER
), scsi_status
,
1907 srb
->total_xfer_length
);
1908 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, STOPDMAXFER
| CLRXFIFO
);
1910 if (!(srb
->state
& SRB_XFERPAD
)) {
1911 if (scsi_status
& PARITYERROR
)
1912 srb
->status
|= PARITY_ERROR
;
1915 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
1916 * is the no of bytes it got from the DMA engine not the no it
1917 * transferred successfully to the device. (And the difference could
1918 * be as much as the FIFO size, I guess ...)
1920 if (!(scsi_status
& SCSIXFERDONE
)) {
1922 * when data transfer from DMA FIFO to SCSI FIFO
1923 * if there was some data left in SCSI FIFO
1926 (u32
)(DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
) &
1928 if (dcb
->sync_period
& WIDE_SYNC
)
1929 d_left_counter
<<= 1;
1931 dprintkdbg(DBG_KG
, "data_out_phase0: FIFO contains %i %s\n"
1932 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
1933 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
1934 DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
),
1935 (dcb
->sync_period
& WIDE_SYNC
) ? "words" : "bytes",
1936 DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
),
1937 DC395x_read32(acb
, TRM_S1040_SCSI_COUNTER
),
1938 DC395x_read8(acb
, TRM_S1040_DMA_FIFOCNT
),
1939 DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
),
1940 DC395x_read32(acb
, TRM_S1040_DMA_CXCNT
));
1943 * calculate all the residue data that not yet tranfered
1944 * SCSI transfer counter + left in SCSI FIFO data
1946 * .....TRM_S1040_SCSI_COUNTER (24bits)
1947 * The counter always decrement by one for every SCSI byte transfer.
1948 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
1949 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
1951 if (srb
->total_xfer_length
> DC395x_LASTPIO
)
1953 DC395x_read32(acb
, TRM_S1040_SCSI_COUNTER
);
1955 /* Is this a good idea? */
1956 /*clear_fifo(acb, "DOP1"); */
1957 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
1958 if (d_left_counter
== 1 && dcb
->sync_period
& WIDE_SYNC
1959 && scsi_bufflen(srb
->cmd
) % 2) {
1962 "data_out_phase0: Discard 1 byte (0x%02x)\n",
1966 * KG: Oops again. Same thinko as above: The SCSI might have been
1967 * faster than the DMA engine, so that it ran out of data.
1968 * In that case, we have to do just nothing!
1969 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
1972 * KG: This is nonsense: We have been WRITING data to the bus
1973 * If the SCSI engine has no bytes left, how should the DMA engine?
1975 if (d_left_counter
== 0) {
1976 srb
->total_xfer_length
= 0;
1979 * if transfer not yet complete
1980 * there were some data residue in SCSI FIFO or
1981 * SCSI transfer counter not empty
1984 srb
->total_xfer_length
- d_left_counter
;
1986 (dcb
->sync_period
& WIDE_SYNC
) ? 2 : 1;
1987 sg_update_list(srb
, d_left_counter
);
1988 /* KG: Most ugly hack! Apparently, this works around a chip bug */
1989 if ((srb
->segment_x
[srb
->sg_index
].length
==
1990 diff
&& scsi_sg_count(srb
->cmd
))
1991 || ((oldxferred
& ~PAGE_MASK
) ==
1994 dprintkl(KERN_INFO
, "data_out_phase0: "
1995 "Work around chip bug (%i)?\n", diff
);
1997 srb
->total_xfer_length
- diff
;
1998 sg_update_list(srb
, d_left_counter
);
1999 /*srb->total_xfer_length -= diff; */
2000 /*srb->virt_addr += diff; */
2001 /*if (srb->cmd->use_sg) */
2002 /* srb->sg_index++; */
2006 if ((*pscsi_status
& PHASEMASK
) != PH_DATA_OUT
) {
2007 cleanup_after_transfer(acb
, srb
);
2012 static void data_out_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2015 dprintkdbg(DBG_0
, "data_out_phase1: (0x%p) <%02i-%i>\n",
2016 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
);
2017 clear_fifo(acb
, "data_out_phase1");
2018 /* do prepare before transfer when data out phase */
2019 data_io_transfer(acb
, srb
, XFERDATAOUT
);
2022 static void data_in_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2025 u16 scsi_status
= *pscsi_status
;
2027 dprintkdbg(DBG_0
, "data_in_phase0: (0x%p) <%02i-%i>\n",
2028 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
);
2031 * KG: DataIn is much more tricky than DataOut. When the device is finished
2032 * and switches to another phase, the SCSI engine should be finished too.
2033 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2034 * engine and transferred to memory.
2035 * We should wait for the FIFOs to be emptied by that (is there any way to
2036 * enforce this?) and then stop the DMA engine, because it might think, that
2037 * there are more bytes to follow. Yes, the device might disconnect prior to
2038 * having all bytes transferred!
2039 * Also we should make sure that all data from the DMA engine buffer's really
2040 * made its way to the system memory! Some documentation on this would not
2041 * seem to be a bad idea, actually.
2043 if (!(srb
->state
& SRB_XFERPAD
)) {
2045 unsigned int sc
, fc
;
2047 if (scsi_status
& PARITYERROR
) {
2048 dprintkl(KERN_INFO
, "data_in_phase0: (0x%p) "
2049 "Parity Error\n", srb
->cmd
);
2050 srb
->status
|= PARITY_ERROR
;
2053 * KG: We should wait for the DMA FIFO to be empty ...
2054 * but: it would be better to wait first for the SCSI FIFO and then the
2055 * the DMA FIFO to become empty? How do we know, that the device not already
2056 * sent data to the FIFO in a MsgIn phase, eg.?
2058 if (!(DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
) & 0x80)) {
2061 dprintkl(KERN_DEBUG
,
2062 "DIP0: Wait for DMA FIFO to flush ...\n");
2063 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2064 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2065 /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2067 (DC395x_read16(acb
, TRM_S1040_DMA_FIFOSTAT
) &
2069 if (ctr
< 6000000 - 1)
2071 "DIP0: Had to wait for DMA ...\n");
2074 "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2075 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2077 dprintkdbg(DBG_KG
, "data_in_phase0: "
2078 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2079 DC395x_read8(acb
, TRM_S1040_DMA_FIFOCNT
),
2080 DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
));
2082 /* Now: Check remainig data: The SCSI counters should tell us ... */
2083 sc
= DC395x_read32(acb
, TRM_S1040_SCSI_COUNTER
);
2084 fc
= DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
);
2085 d_left_counter
= sc
+ ((fc
& 0x1f)
2086 << ((srb
->dcb
->sync_period
& WIDE_SYNC
) ? 1 :
2088 dprintkdbg(DBG_KG
, "data_in_phase0: "
2089 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2090 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2091 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2093 (srb
->dcb
->sync_period
& WIDE_SYNC
) ? "words" : "bytes",
2096 DC395x_read8(acb
, TRM_S1040_DMA_FIFOSTAT
),
2097 DC395x_read32(acb
, TRM_S1040_DMA_CXCNT
),
2098 srb
->total_xfer_length
, d_left_counter
);
2100 /* KG: Less than or equal to 4 bytes can not be transferred via DMA, it seems. */
2102 && srb
->total_xfer_length
<= DC395x_LASTPIO
) {
2103 size_t left_io
= srb
->total_xfer_length
;
2105 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2106 /*sg_update_list (srb, d_left_counter); */
2107 dprintkdbg(DBG_PIO
, "data_in_phase0: PIO (%i %s) "
2108 "for remaining %i bytes:",
2110 (srb
->dcb
->sync_period
& WIDE_SYNC
) ?
2112 srb
->total_xfer_length
);
2113 if (srb
->dcb
->sync_period
& WIDE_SYNC
)
2114 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG2
,
2117 unsigned char *virt
, *base
= NULL
;
2118 unsigned long flags
= 0;
2119 size_t len
= left_io
;
2120 size_t offset
= srb
->request_length
- left_io
;
2122 local_irq_save(flags
);
2123 /* Assumption: it's inside one page as it's at most 4 bytes and
2124 I just assume it's on a 4-byte boundary */
2125 base
= scsi_kmap_atomic_sg(scsi_sglist(srb
->cmd
),
2126 srb
->sg_count
, &offset
, &len
);
2127 virt
= base
+ offset
;
2133 byte
= DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
);
2136 if (debug_enabled(DBG_PIO
))
2137 printk(" %02x", byte
);
2140 sg_subtract_one(srb
);
2144 fc
= DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
);
2152 WARN_ON((fc
!= 0x40) == !d_left_counter
);
2154 if (fc
== 0x40 && (srb
->dcb
->sync_period
& WIDE_SYNC
)) {
2155 /* Read the last byte ... */
2156 if (srb
->total_xfer_length
> 0) {
2157 u8 byte
= DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
);
2160 srb
->total_xfer_length
--;
2161 if (debug_enabled(DBG_PIO
))
2162 printk(" %02x", byte
);
2165 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG2
, 0);
2168 scsi_kunmap_atomic_sg(base
);
2169 local_irq_restore(flags
);
2171 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2172 /*srb->total_xfer_length = 0; */
2173 if (debug_enabled(DBG_PIO
))
2176 #endif /* DC395x_LASTPIO */
2180 * KG: This was in DATAOUT. Does it also belong here?
2181 * Nobody seems to know what counter and fifo_cnt count exactly ...
2183 if (!(scsi_status
& SCSIXFERDONE
)) {
2185 * when data transfer from DMA FIFO to SCSI FIFO
2186 * if there was some data left in SCSI FIFO
2189 (u32
)(DC395x_read8(acb
, TRM_S1040_SCSI_FIFOCNT
) &
2191 if (srb
->dcb
->sync_period
& WIDE_SYNC
)
2192 d_left_counter
<<= 1;
2194 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2196 * KG: Seems to be correct ...
2200 /* KG: This should not be needed any more! */
2201 if (d_left_counter
== 0
2202 || (scsi_status
& SCSIXFERCNT_2_ZERO
)) {
2208 DC395x_read8(acb
, TRM_S1040_DMA_STATUS
);
2209 } while (!(TempDMAstatus
& DMAXFERCOMP
) && --ctr
);
2212 "Deadlock in DataInPhase0 waiting for DMA!!\n");
2213 srb
->total_xfer_length
= 0;
2215 srb
->total_xfer_length
= d_left_counter
;
2216 } else { /* phase changed */
2219 * when a transfer not yet complete
2220 * but be disconnected by target
2221 * if transfer not yet complete
2222 * there were some data residue in SCSI FIFO or
2223 * SCSI transfer counter not empty
2225 sg_update_list(srb
, d_left_counter
);
2228 /* KG: The target may decide to disconnect: Empty FIFO before! */
2229 if ((*pscsi_status
& PHASEMASK
) != PH_DATA_IN
) {
2230 cleanup_after_transfer(acb
, srb
);
2235 static void data_in_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2238 dprintkdbg(DBG_0
, "data_in_phase1: (0x%p) <%02i-%i>\n",
2239 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
);
2240 data_io_transfer(acb
, srb
, XFERDATAIN
);
2244 static void data_io_transfer(struct AdapterCtlBlk
*acb
,
2245 struct ScsiReqBlk
*srb
, u16 io_dir
)
2247 struct DeviceCtlBlk
*dcb
= srb
->dcb
;
2250 "data_io_transfer: (0x%p) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
2251 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
,
2252 ((io_dir
& DMACMD_DIR
) ? 'r' : 'w'),
2253 srb
->total_xfer_length
, srb
->sg_index
, srb
->sg_count
);
2254 if (srb
== acb
->tmp_srb
)
2255 dprintkl(KERN_ERR
, "data_io_transfer: Using tmp_srb!\n");
2256 if (srb
->sg_index
>= srb
->sg_count
) {
2257 /* can't happen? out of bounds error */
2261 if (srb
->total_xfer_length
> DC395x_LASTPIO
) {
2262 u8 dma_status
= DC395x_read8(acb
, TRM_S1040_DMA_STATUS
);
2264 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2265 * Maybe, even ABORTXFER would be appropriate
2267 if (dma_status
& XFERPENDING
) {
2268 dprintkl(KERN_DEBUG
, "data_io_transfer: Xfer pending! "
2269 "Expect trouble!\n");
2270 dump_register_info(acb
, dcb
, srb
);
2271 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, CLRXFIFO
);
2273 /* clear_fifo(acb, "IO"); */
2275 * load what physical address of Scatter/Gather list table
2276 * want to be transfer
2278 srb
->state
|= SRB_DATA_XFER
;
2279 DC395x_write32(acb
, TRM_S1040_DMA_XHIGHADDR
, 0);
2280 if (scsi_sg_count(srb
->cmd
)) { /* with S/G */
2281 io_dir
|= DMACMD_SG
;
2282 DC395x_write32(acb
, TRM_S1040_DMA_XLOWADDR
,
2284 sizeof(struct SGentry
) *
2286 /* load how many bytes in the sg list table */
2287 DC395x_write32(acb
, TRM_S1040_DMA_XCNT
,
2288 ((u32
)(srb
->sg_count
-
2289 srb
->sg_index
) << 3));
2290 } else { /* without S/G */
2291 io_dir
&= ~DMACMD_SG
;
2292 DC395x_write32(acb
, TRM_S1040_DMA_XLOWADDR
,
2293 srb
->segment_x
[0].address
);
2294 DC395x_write32(acb
, TRM_S1040_DMA_XCNT
,
2295 srb
->segment_x
[0].length
);
2297 /* load total transfer length (24bits) max value 16Mbyte */
2298 DC395x_write32(acb
, TRM_S1040_SCSI_COUNTER
,
2299 srb
->total_xfer_length
);
2300 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
2301 if (io_dir
& DMACMD_DIR
) { /* read */
2302 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
,
2304 DC395x_write16(acb
, TRM_S1040_DMA_COMMAND
, io_dir
);
2306 DC395x_write16(acb
, TRM_S1040_DMA_COMMAND
, io_dir
);
2307 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
,
2313 else if (srb
->total_xfer_length
> 0) { /* The last four bytes: Do PIO */
2315 * load what physical address of Scatter/Gather list table
2316 * want to be transfer
2318 srb
->state
|= SRB_DATA_XFER
;
2319 /* load total transfer length (24bits) max value 16Mbyte */
2320 DC395x_write32(acb
, TRM_S1040_SCSI_COUNTER
,
2321 srb
->total_xfer_length
);
2322 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
2323 if (io_dir
& DMACMD_DIR
) { /* read */
2324 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
,
2326 } else { /* write */
2327 int ln
= srb
->total_xfer_length
;
2328 size_t left_io
= srb
->total_xfer_length
;
2330 if (srb
->dcb
->sync_period
& WIDE_SYNC
)
2331 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG2
,
2335 unsigned char *virt
, *base
= NULL
;
2336 unsigned long flags
= 0;
2337 size_t len
= left_io
;
2338 size_t offset
= srb
->request_length
- left_io
;
2340 local_irq_save(flags
);
2341 /* Again, max 4 bytes */
2342 base
= scsi_kmap_atomic_sg(scsi_sglist(srb
->cmd
),
2343 srb
->sg_count
, &offset
, &len
);
2344 virt
= base
+ offset
;
2349 if (debug_enabled(DBG_PIO
))
2350 printk(" %02x", *virt
);
2352 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, *virt
++);
2354 sg_subtract_one(srb
);
2357 scsi_kunmap_atomic_sg(base
);
2358 local_irq_restore(flags
);
2360 if (srb
->dcb
->sync_period
& WIDE_SYNC
) {
2362 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 0);
2363 if (debug_enabled(DBG_PIO
))
2366 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG2
, 0);
2368 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2369 if (debug_enabled(DBG_PIO
))
2371 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
,
2375 #endif /* DC395x_LASTPIO */
2376 else { /* xfer pad */
2377 if (srb
->sg_count
) {
2378 srb
->adapter_status
= H_OVER_UNDER_RUN
;
2379 srb
->status
|= OVER_RUN
;
2382 * KG: despite the fact that we are using 16 bits I/O ops
2383 * the SCSI FIFO is only 8 bits according to the docs
2384 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2386 if (dcb
->sync_period
& WIDE_SYNC
) {
2387 DC395x_write32(acb
, TRM_S1040_SCSI_COUNTER
, 2);
2388 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG2
,
2390 if (io_dir
& DMACMD_DIR
) {
2391 DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
);
2392 DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
);
2394 /* Danger, Robinson: If you find KGs
2395 * scattered over the wide disk, the driver
2396 * or chip is to blame :-( */
2397 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 'K');
2398 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 'G');
2400 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG2
, 0);
2402 DC395x_write32(acb
, TRM_S1040_SCSI_COUNTER
, 1);
2403 /* Danger, Robinson: If you find a collection of Ks on your disk
2404 * something broke :-( */
2405 if (io_dir
& DMACMD_DIR
)
2406 DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
);
2408 DC395x_write8(acb
, TRM_S1040_SCSI_FIFO
, 'K');
2410 srb
->state
|= SRB_XFERPAD
;
2411 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
2413 bval
= (io_dir
& DMACMD_DIR
) ? SCMD_FIFO_IN
: SCMD_FIFO_OUT
;
2414 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, bval
);
2419 static void status_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2422 dprintkdbg(DBG_0
, "status_phase0: (0x%p) <%02i-%i>\n",
2423 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
);
2424 srb
->target_status
= DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
);
2425 srb
->end_message
= DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
); /* get message */
2426 srb
->state
= SRB_COMPLETED
;
2427 *pscsi_status
= PH_BUS_FREE
; /*.. initial phase */
2428 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
2429 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_MSGACCEPT
);
2433 static void status_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2436 dprintkdbg(DBG_0
, "status_phase1: (0x%p) <%02i-%i>\n",
2437 srb
->cmd
, srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
);
2438 srb
->state
= SRB_STATUS
;
2439 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
2440 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_COMP
);
2444 /* Check if the message is complete */
2445 static inline u8
msgin_completed(u8
* msgbuf
, u32 len
)
2447 if (*msgbuf
== EXTENDED_MESSAGE
) {
2450 if (len
< msgbuf
[1] + 2)
2452 } else if (*msgbuf
>= 0x20 && *msgbuf
<= 0x2f) /* two byte messages */
2459 static inline void msgin_reject(struct AdapterCtlBlk
*acb
,
2460 struct ScsiReqBlk
*srb
)
2462 srb
->msgout_buf
[0] = MESSAGE_REJECT
;
2464 DC395x_ENABLE_MSGOUT
;
2465 srb
->state
&= ~SRB_MSGIN
;
2466 srb
->state
|= SRB_MSGOUT
;
2467 dprintkl(KERN_INFO
, "msgin_reject: 0x%02x <%02i-%i>\n",
2469 srb
->dcb
->target_id
, srb
->dcb
->target_lun
);
2473 static struct ScsiReqBlk
*msgin_qtag(struct AdapterCtlBlk
*acb
,
2474 struct DeviceCtlBlk
*dcb
, u8 tag
)
2476 struct ScsiReqBlk
*srb
= NULL
;
2477 struct ScsiReqBlk
*i
;
2478 dprintkdbg(DBG_0
, "msgin_qtag: (0x%p) tag=%i srb=%p\n",
2479 srb
->cmd
, tag
, srb
);
2481 if (!(dcb
->tag_mask
& (1 << tag
)))
2482 dprintkl(KERN_DEBUG
,
2483 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2484 dcb
->tag_mask
, tag
);
2486 if (list_empty(&dcb
->srb_going_list
))
2488 list_for_each_entry(i
, &dcb
->srb_going_list
, list
) {
2489 if (i
->tag_number
== tag
) {
2497 dprintkdbg(DBG_0
, "msgin_qtag: (0x%p) <%02i-%i>\n",
2498 srb
->cmd
, srb
->dcb
->target_id
, srb
->dcb
->target_lun
);
2499 if (dcb
->flag
& ABORT_DEV_
) {
2500 /*srb->state = SRB_ABORT_SENT; */
2501 enable_msgout_abort(acb
, srb
);
2504 if (!(srb
->state
& SRB_DISCONNECT
))
2507 memcpy(srb
->msgin_buf
, dcb
->active_srb
->msgin_buf
, acb
->msg_len
);
2508 srb
->state
|= dcb
->active_srb
->state
;
2509 srb
->state
|= SRB_DATA_XFER
;
2510 dcb
->active_srb
= srb
;
2511 /* How can we make the DORS happy? */
2516 srb
->state
= SRB_UNEXPECT_RESEL
;
2517 dcb
->active_srb
= srb
;
2518 srb
->msgout_buf
[0] = ABORT_TASK
;
2520 DC395x_ENABLE_MSGOUT
;
2521 dprintkl(KERN_DEBUG
, "msgin_qtag: Unknown tag %i - abort\n", tag
);
2526 static inline void reprogram_regs(struct AdapterCtlBlk
*acb
,
2527 struct DeviceCtlBlk
*dcb
)
2529 DC395x_write8(acb
, TRM_S1040_SCSI_TARGETID
, dcb
->target_id
);
2530 DC395x_write8(acb
, TRM_S1040_SCSI_SYNC
, dcb
->sync_period
);
2531 DC395x_write8(acb
, TRM_S1040_SCSI_OFFSET
, dcb
->sync_offset
);
2532 set_xfer_rate(acb
, dcb
);
2536 /* set async transfer mode */
2537 static void msgin_set_async(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
)
2539 struct DeviceCtlBlk
*dcb
= srb
->dcb
;
2540 dprintkl(KERN_DEBUG
, "msgin_set_async: No sync transfers <%02i-%i>\n",
2541 dcb
->target_id
, dcb
->target_lun
);
2543 dcb
->sync_mode
&= ~(SYNC_NEGO_ENABLE
);
2544 dcb
->sync_mode
|= SYNC_NEGO_DONE
;
2545 /*dcb->sync_period &= 0; */
2546 dcb
->sync_offset
= 0;
2547 dcb
->min_nego_period
= 200 >> 2; /* 200ns <=> 5 MHz */
2548 srb
->state
&= ~SRB_DO_SYNC_NEGO
;
2549 reprogram_regs(acb
, dcb
);
2550 if ((dcb
->sync_mode
& WIDE_NEGO_ENABLE
)
2551 && !(dcb
->sync_mode
& WIDE_NEGO_DONE
)) {
2552 build_wdtr(acb
, dcb
, srb
);
2553 DC395x_ENABLE_MSGOUT
;
2554 dprintkdbg(DBG_0
, "msgin_set_async(rej): Try WDTR anyway\n");
2559 /* set sync transfer mode */
2560 static void msgin_set_sync(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
)
2562 struct DeviceCtlBlk
*dcb
= srb
->dcb
;
2565 dprintkdbg(DBG_1
, "msgin_set_sync: <%02i> Sync: %ins "
2566 "(%02i.%01i MHz) Offset %i\n",
2567 dcb
->target_id
, srb
->msgin_buf
[3] << 2,
2568 (250 / srb
->msgin_buf
[3]),
2569 ((250 % srb
->msgin_buf
[3]) * 10) / srb
->msgin_buf
[3],
2572 if (srb
->msgin_buf
[4] > 15)
2573 srb
->msgin_buf
[4] = 15;
2574 if (!(dcb
->dev_mode
& NTC_DO_SYNC_NEGO
))
2575 dcb
->sync_offset
= 0;
2576 else if (dcb
->sync_offset
== 0)
2577 dcb
->sync_offset
= srb
->msgin_buf
[4];
2578 if (srb
->msgin_buf
[4] > dcb
->sync_offset
)
2579 srb
->msgin_buf
[4] = dcb
->sync_offset
;
2581 dcb
->sync_offset
= srb
->msgin_buf
[4];
2583 while (bval
< 7 && (srb
->msgin_buf
[3] > clock_period
[bval
]
2584 || dcb
->min_nego_period
>
2585 clock_period
[bval
]))
2587 if (srb
->msgin_buf
[3] < clock_period
[bval
])
2589 "msgin_set_sync: Increase sync nego period to %ins\n",
2590 clock_period
[bval
] << 2);
2591 srb
->msgin_buf
[3] = clock_period
[bval
];
2592 dcb
->sync_period
&= 0xf0;
2593 dcb
->sync_period
|= ALT_SYNC
| bval
;
2594 dcb
->min_nego_period
= srb
->msgin_buf
[3];
2596 if (dcb
->sync_period
& WIDE_SYNC
)
2602 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2603 dcb
->target_id
, (fact
== 500) ? "Wide16" : "",
2604 dcb
->min_nego_period
<< 2, dcb
->sync_offset
,
2605 (fact
/ dcb
->min_nego_period
),
2606 ((fact
% dcb
->min_nego_period
) * 10 +
2607 dcb
->min_nego_period
/ 2) / dcb
->min_nego_period
);
2609 if (!(srb
->state
& SRB_DO_SYNC_NEGO
)) {
2610 /* Reply with corrected SDTR Message */
2611 dprintkl(KERN_DEBUG
, "msgin_set_sync: answer w/%ins %i\n",
2612 srb
->msgin_buf
[3] << 2, srb
->msgin_buf
[4]);
2614 memcpy(srb
->msgout_buf
, srb
->msgin_buf
, 5);
2616 DC395x_ENABLE_MSGOUT
;
2617 dcb
->sync_mode
|= SYNC_NEGO_DONE
;
2619 if ((dcb
->sync_mode
& WIDE_NEGO_ENABLE
)
2620 && !(dcb
->sync_mode
& WIDE_NEGO_DONE
)) {
2621 build_wdtr(acb
, dcb
, srb
);
2622 DC395x_ENABLE_MSGOUT
;
2623 dprintkdbg(DBG_0
, "msgin_set_sync: Also try WDTR\n");
2626 srb
->state
&= ~SRB_DO_SYNC_NEGO
;
2627 dcb
->sync_mode
|= SYNC_NEGO_DONE
| SYNC_NEGO_ENABLE
;
2629 reprogram_regs(acb
, dcb
);
2633 static inline void msgin_set_nowide(struct AdapterCtlBlk
*acb
,
2634 struct ScsiReqBlk
*srb
)
2636 struct DeviceCtlBlk
*dcb
= srb
->dcb
;
2637 dprintkdbg(DBG_1
, "msgin_set_nowide: <%02i>\n", dcb
->target_id
);
2639 dcb
->sync_period
&= ~WIDE_SYNC
;
2640 dcb
->sync_mode
&= ~(WIDE_NEGO_ENABLE
);
2641 dcb
->sync_mode
|= WIDE_NEGO_DONE
;
2642 srb
->state
&= ~SRB_DO_WIDE_NEGO
;
2643 reprogram_regs(acb
, dcb
);
2644 if ((dcb
->sync_mode
& SYNC_NEGO_ENABLE
)
2645 && !(dcb
->sync_mode
& SYNC_NEGO_DONE
)) {
2646 build_sdtr(acb
, dcb
, srb
);
2647 DC395x_ENABLE_MSGOUT
;
2648 dprintkdbg(DBG_0
, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2652 static void msgin_set_wide(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
)
2654 struct DeviceCtlBlk
*dcb
= srb
->dcb
;
2655 u8 wide
= (dcb
->dev_mode
& NTC_DO_WIDE_NEGO
2656 && acb
->config
& HCC_WIDE_CARD
) ? 1 : 0;
2657 dprintkdbg(DBG_1
, "msgin_set_wide: <%02i>\n", dcb
->target_id
);
2659 if (srb
->msgin_buf
[3] > wide
)
2660 srb
->msgin_buf
[3] = wide
;
2662 if (!(srb
->state
& SRB_DO_WIDE_NEGO
)) {
2663 dprintkl(KERN_DEBUG
,
2664 "msgin_set_wide: Wide nego initiated <%02i>\n",
2666 memcpy(srb
->msgout_buf
, srb
->msgin_buf
, 4);
2668 srb
->state
|= SRB_DO_WIDE_NEGO
;
2669 DC395x_ENABLE_MSGOUT
;
2672 dcb
->sync_mode
|= (WIDE_NEGO_ENABLE
| WIDE_NEGO_DONE
);
2673 if (srb
->msgin_buf
[3] > 0)
2674 dcb
->sync_period
|= WIDE_SYNC
;
2676 dcb
->sync_period
&= ~WIDE_SYNC
;
2677 srb
->state
&= ~SRB_DO_WIDE_NEGO
;
2678 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2680 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2681 (8 << srb
->msgin_buf
[3]), dcb
->target_id
);
2682 reprogram_regs(acb
, dcb
);
2683 if ((dcb
->sync_mode
& SYNC_NEGO_ENABLE
)
2684 && !(dcb
->sync_mode
& SYNC_NEGO_DONE
)) {
2685 build_sdtr(acb
, dcb
, srb
);
2686 DC395x_ENABLE_MSGOUT
;
2687 dprintkdbg(DBG_0
, "msgin_set_wide: Also try SDTR.\n");
2693 * extended message codes:
2698 * 00h MODIFY DATA POINTER
2699 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
2700 * 03h WIDE DATA TRANSFER REQUEST
2701 * 04h - 7Fh Reserved
2702 * 80h - FFh Vendor specific
2704 static void msgin_phase0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2707 struct DeviceCtlBlk
*dcb
= acb
->active_dcb
;
2708 dprintkdbg(DBG_0
, "msgin_phase0: (0x%p)\n", srb
->cmd
);
2710 srb
->msgin_buf
[acb
->msg_len
++] = DC395x_read8(acb
, TRM_S1040_SCSI_FIFO
);
2711 if (msgin_completed(srb
->msgin_buf
, acb
->msg_len
)) {
2712 /* Now eval the msg */
2713 switch (srb
->msgin_buf
[0]) {
2715 srb
->state
= SRB_DISCONNECT
;
2718 case SIMPLE_QUEUE_TAG
:
2719 case HEAD_OF_QUEUE_TAG
:
2720 case ORDERED_QUEUE_TAG
:
2722 msgin_qtag(acb
, dcb
,
2726 case MESSAGE_REJECT
:
2727 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
,
2728 DO_CLRATN
| DO_DATALATCH
);
2729 /* A sync nego message was rejected ! */
2730 if (srb
->state
& SRB_DO_SYNC_NEGO
) {
2731 msgin_set_async(acb
, srb
);
2734 /* A wide nego message was rejected ! */
2735 if (srb
->state
& SRB_DO_WIDE_NEGO
) {
2736 msgin_set_nowide(acb
, srb
);
2739 enable_msgout_abort(acb
, srb
);
2740 /*srb->state |= SRB_ABORT_SENT */
2743 case EXTENDED_MESSAGE
:
2745 if (srb
->msgin_buf
[1] == 3
2746 && srb
->msgin_buf
[2] == EXTENDED_SDTR
) {
2747 msgin_set_sync(acb
, srb
);
2751 if (srb
->msgin_buf
[1] == 2
2752 && srb
->msgin_buf
[2] == EXTENDED_WDTR
2753 && srb
->msgin_buf
[3] <= 2) { /* sanity check ... */
2754 msgin_set_wide(acb
, srb
);
2757 msgin_reject(acb
, srb
);
2760 case IGNORE_WIDE_RESIDUE
:
2761 /* Discard wide residual */
2762 dprintkdbg(DBG_0
, "msgin_phase0: Ignore Wide Residual!\n");
2765 case COMMAND_COMPLETE
:
2766 /* nothing has to be done */
2771 * SAVE POINTER may be ignored as we have the struct
2772 * ScsiReqBlk* associated with the scsi command.
2774 dprintkdbg(DBG_0
, "msgin_phase0: (0x%p) "
2775 "SAVE POINTER rem=%i Ignore\n",
2776 srb
->cmd
, srb
->total_xfer_length
);
2779 case RESTORE_POINTERS
:
2780 dprintkdbg(DBG_0
, "msgin_phase0: RESTORE POINTER. Ignore\n");
2784 dprintkdbg(DBG_0
, "msgin_phase0: (0x%p) "
2785 "<%02i-%i> ABORT msg\n",
2786 srb
->cmd
, dcb
->target_id
,
2788 dcb
->flag
|= ABORT_DEV_
;
2789 enable_msgout_abort(acb
, srb
);
2793 /* reject unknown messages */
2794 if (srb
->msgin_buf
[0] & IDENTIFY_BASE
) {
2795 dprintkdbg(DBG_0
, "msgin_phase0: Identify msg\n");
2797 srb
->msgout_buf
[0] = dcb
->identify_msg
;
2798 DC395x_ENABLE_MSGOUT
;
2799 srb
->state
|= SRB_MSGOUT
;
2802 msgin_reject(acb
, srb
);
2805 /* Clear counter and MsgIn state */
2806 srb
->state
&= ~SRB_MSGIN
;
2809 *pscsi_status
= PH_BUS_FREE
;
2810 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important ... you know! */
2811 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_MSGACCEPT
);
2815 static void msgin_phase1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2818 dprintkdbg(DBG_0
, "msgin_phase1: (0x%p)\n", srb
->cmd
);
2819 clear_fifo(acb
, "msgin_phase1");
2820 DC395x_write32(acb
, TRM_S1040_SCSI_COUNTER
, 1);
2821 if (!(srb
->state
& SRB_MSGIN
)) {
2822 srb
->state
&= ~SRB_DISCONNECT
;
2823 srb
->state
|= SRB_MSGIN
;
2825 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
2827 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_FIFO_IN
);
2831 static void nop0(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2837 static void nop1(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
,
2843 static void set_xfer_rate(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
)
2845 struct DeviceCtlBlk
*i
;
2847 /* set all lun device's period, offset */
2848 if (dcb
->identify_msg
& 0x07)
2851 if (acb
->scan_devices
) {
2852 current_sync_offset
= dcb
->sync_offset
;
2856 list_for_each_entry(i
, &acb
->dcb_list
, list
)
2857 if (i
->target_id
== dcb
->target_id
) {
2858 i
->sync_period
= dcb
->sync_period
;
2859 i
->sync_offset
= dcb
->sync_offset
;
2860 i
->sync_mode
= dcb
->sync_mode
;
2861 i
->min_nego_period
= dcb
->min_nego_period
;
2866 static void disconnect(struct AdapterCtlBlk
*acb
)
2868 struct DeviceCtlBlk
*dcb
= acb
->active_dcb
;
2869 struct ScsiReqBlk
*srb
;
2872 dprintkl(KERN_ERR
, "disconnect: No such device\n");
2874 /* Suspend queue for a while */
2877 HZ
* acb
->eeprom
.delay_time
;
2878 clear_fifo(acb
, "disconnectEx");
2879 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_HWRESELECT
);
2882 srb
= dcb
->active_srb
;
2883 acb
->active_dcb
= NULL
;
2884 dprintkdbg(DBG_0
, "disconnect: (0x%p)\n", srb
->cmd
);
2886 srb
->scsi_phase
= PH_BUS_FREE
; /* initial phase */
2887 clear_fifo(acb
, "disconnect");
2888 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_HWRESELECT
);
2889 if (srb
->state
& SRB_UNEXPECT_RESEL
) {
2891 "disconnect: Unexpected reselection <%02i-%i>\n",
2892 dcb
->target_id
, dcb
->target_lun
);
2894 waiting_process_next(acb
);
2895 } else if (srb
->state
& SRB_ABORT_SENT
) {
2896 dcb
->flag
&= ~ABORT_DEV_
;
2897 acb
->last_reset
= jiffies
+ HZ
/ 2 + 1;
2898 dprintkl(KERN_ERR
, "disconnect: SRB_ABORT_SENT\n");
2899 doing_srb_done(acb
, DID_ABORT
, srb
->cmd
, 1);
2900 waiting_process_next(acb
);
2902 if ((srb
->state
& (SRB_START_
+ SRB_MSGOUT
))
2904 state
& (SRB_DISCONNECT
| SRB_COMPLETED
))) {
2906 * Selection time out
2907 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
2909 /* Unexp. Disc / Sel Timeout */
2910 if (srb
->state
!= SRB_START_
2911 && srb
->state
!= SRB_MSGOUT
) {
2912 srb
->state
= SRB_READY
;
2913 dprintkl(KERN_DEBUG
,
2914 "disconnect: (0x%p) Unexpected\n",
2916 srb
->target_status
= SCSI_STAT_SEL_TIMEOUT
;
2919 /* Normal selection timeout */
2920 dprintkdbg(DBG_KG
, "disconnect: (0x%p) "
2921 "<%02i-%i> SelTO\n", srb
->cmd
,
2922 dcb
->target_id
, dcb
->target_lun
);
2923 if (srb
->retry_count
++ > DC395x_MAX_RETRIES
2924 || acb
->scan_devices
) {
2925 srb
->target_status
=
2926 SCSI_STAT_SEL_TIMEOUT
;
2930 list_move(&srb
->list
, &dcb
->srb_waiting_list
);
2932 "disconnect: (0x%p) Retry\n",
2934 waiting_set_timer(acb
, HZ
/ 20);
2936 } else if (srb
->state
& SRB_DISCONNECT
) {
2937 u8 bval
= DC395x_read8(acb
, TRM_S1040_SCSI_SIGNAL
);
2939 * SRB_DISCONNECT (This is what we expect!)
2942 dprintkdbg(DBG_0
, "disconnect: SCSI bus stat "
2943 " 0x%02x: ACK set! Other controllers?\n",
2945 /* It could come from another initiator, therefore don't do much ! */
2947 waiting_process_next(acb
);
2948 } else if (srb
->state
& SRB_COMPLETED
) {
2954 dcb
->active_srb
= NULL
;
2955 srb
->state
= SRB_FREE
;
2956 srb_done(acb
, dcb
, srb
);
2962 static void reselect(struct AdapterCtlBlk
*acb
)
2964 struct DeviceCtlBlk
*dcb
= acb
->active_dcb
;
2965 struct ScsiReqBlk
*srb
= NULL
;
2966 u16 rsel_tar_lun_id
;
2968 dprintkdbg(DBG_0
, "reselect: acb=%p\n", acb
);
2970 clear_fifo(acb
, "reselect");
2971 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
2972 /* Read Reselected Target ID and LUN */
2973 rsel_tar_lun_id
= DC395x_read16(acb
, TRM_S1040_SCSI_TARGETID
);
2974 if (dcb
) { /* Arbitration lost but Reselection win */
2975 srb
= dcb
->active_srb
;
2977 dprintkl(KERN_DEBUG
, "reselect: Arb lost Resel won, "
2978 "but active_srb == NULL\n");
2979 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
2983 if (!acb
->scan_devices
) {
2984 dprintkdbg(DBG_KG
, "reselect: (0x%p) <%02i-%i> "
2985 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
2986 srb
->cmd
, dcb
->target_id
,
2987 dcb
->target_lun
, rsel_tar_lun_id
,
2988 DC395x_read16(acb
, TRM_S1040_SCSI_STATUS
));
2989 /*srb->state |= SRB_DISCONNECT; */
2991 srb
->state
= SRB_READY
;
2993 list_move(&srb
->list
, &dcb
->srb_waiting_list
);
2994 waiting_set_timer(acb
, HZ
/ 20);
2999 /* Read Reselected Target Id and LUN */
3000 if (!(rsel_tar_lun_id
& (IDENTIFY_BASE
<< 8)))
3001 dprintkl(KERN_DEBUG
, "reselect: Expects identify msg. "
3002 "Got %i!\n", rsel_tar_lun_id
);
3003 id
= rsel_tar_lun_id
& 0xff;
3004 lun
= (rsel_tar_lun_id
>> 8) & 7;
3005 dcb
= find_dcb(acb
, id
, lun
);
3007 dprintkl(KERN_ERR
, "reselect: From non existent device "
3008 "<%02i-%i>\n", id
, lun
);
3009 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
3012 acb
->active_dcb
= dcb
;
3014 if (!(dcb
->dev_mode
& NTC_DO_DISCONNECT
))
3015 dprintkl(KERN_DEBUG
, "reselect: in spite of forbidden "
3016 "disconnection? <%02i-%i>\n",
3017 dcb
->target_id
, dcb
->target_lun
);
3019 if (dcb
->sync_mode
& EN_TAG_QUEUEING
) {
3021 dcb
->active_srb
= srb
;
3023 /* There can be only one! */
3024 srb
= dcb
->active_srb
;
3025 if (!srb
|| !(srb
->state
& SRB_DISCONNECT
)) {
3029 dprintkl(KERN_DEBUG
,
3030 "reselect: w/o disconnected cmds <%02i-%i>\n",
3031 dcb
->target_id
, dcb
->target_lun
);
3033 srb
->state
= SRB_UNEXPECT_RESEL
;
3034 dcb
->active_srb
= srb
;
3035 enable_msgout_abort(acb
, srb
);
3037 if (dcb
->flag
& ABORT_DEV_
) {
3038 /*srb->state = SRB_ABORT_SENT; */
3039 enable_msgout_abort(acb
, srb
);
3041 srb
->state
= SRB_DATA_XFER
;
3045 srb
->scsi_phase
= PH_BUS_FREE
; /* initial phase */
3047 /* Program HA ID, target ID, period and offset */
3048 dprintkdbg(DBG_0
, "reselect: select <%i>\n", dcb
->target_id
);
3049 DC395x_write8(acb
, TRM_S1040_SCSI_HOSTID
, acb
->scsi_host
->this_id
); /* host ID */
3050 DC395x_write8(acb
, TRM_S1040_SCSI_TARGETID
, dcb
->target_id
); /* target ID */
3051 DC395x_write8(acb
, TRM_S1040_SCSI_OFFSET
, dcb
->sync_offset
); /* offset */
3052 DC395x_write8(acb
, TRM_S1040_SCSI_SYNC
, dcb
->sync_period
); /* sync period, wide */
3053 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_DATALATCH
); /* it's important for atn stop */
3055 DC395x_write8(acb
, TRM_S1040_SCSI_COMMAND
, SCMD_MSGACCEPT
);
3059 static inline u8
tagq_blacklist(char *name
)
3061 #ifndef DC395x_NO_TAGQ
3064 for (i
= 0; i
< BADDEVCNT
; i
++)
3065 if (memcmp(name
, DC395x_baddevname1
[i
], 28) == 0)
3075 static void disc_tagq_set(struct DeviceCtlBlk
*dcb
, struct ScsiInqData
*ptr
)
3077 /* Check for SCSI format (ANSI and Response data format) */
3078 if ((ptr
->Vers
& 0x07) >= 2 || (ptr
->RDF
& 0x0F) == 2) {
3079 if ((ptr
->Flags
& SCSI_INQ_CMDQUEUE
)
3080 && (dcb
->dev_mode
& NTC_DO_TAG_QUEUEING
) &&
3081 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3082 /* ((dcb->dev_type == TYPE_DISK)
3083 || (dcb->dev_type == TYPE_MOD)) && */
3084 !tagq_blacklist(((char *)ptr
) + 8)) {
3085 if (dcb
->max_command
== 1)
3087 dcb
->acb
->tag_max_num
;
3088 dcb
->sync_mode
|= EN_TAG_QUEUEING
;
3089 /*dcb->tag_mask = 0; */
3091 dcb
->max_command
= 1;
3096 static void add_dev(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
3097 struct ScsiInqData
*ptr
)
3099 u8 bval1
= ptr
->DevType
& SCSI_DEVTYPE
;
3100 dcb
->dev_type
= bval1
;
3101 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3102 disc_tagq_set(dcb
, ptr
);
3106 /* unmap mapped pci regions from SRB */
3107 static void pci_unmap_srb(struct AdapterCtlBlk
*acb
, struct ScsiReqBlk
*srb
)
3109 struct scsi_cmnd
*cmd
= srb
->cmd
;
3110 enum dma_data_direction dir
= cmd
->sc_data_direction
;
3112 if (scsi_sg_count(cmd
) && dir
!= DMA_NONE
) {
3113 /* unmap DC395x SG list */
3114 dprintkdbg(DBG_SG
, "pci_unmap_srb: list=%08x(%05x)\n",
3115 srb
->sg_bus_addr
, SEGMENTX_LEN
);
3116 dma_unmap_single(&acb
->dev
->dev
, srb
->sg_bus_addr
, SEGMENTX_LEN
,
3118 dprintkdbg(DBG_SG
, "pci_unmap_srb: segs=%i buffer=%p\n",
3119 scsi_sg_count(cmd
), scsi_bufflen(cmd
));
3120 /* unmap the sg segments */
3121 scsi_dma_unmap(cmd
);
3126 /* unmap mapped pci sense buffer from SRB */
3127 static void pci_unmap_srb_sense(struct AdapterCtlBlk
*acb
,
3128 struct ScsiReqBlk
*srb
)
3130 if (!(srb
->flag
& AUTO_REQSENSE
))
3132 /* Unmap sense buffer */
3133 dprintkdbg(DBG_SG
, "pci_unmap_srb_sense: buffer=%08x\n",
3134 srb
->segment_x
[0].address
);
3135 dma_unmap_single(&acb
->dev
->dev
, srb
->segment_x
[0].address
,
3136 srb
->segment_x
[0].length
, DMA_FROM_DEVICE
);
3137 /* Restore SG stuff */
3138 srb
->total_xfer_length
= srb
->xferred
;
3139 srb
->segment_x
[0].address
=
3140 srb
->segment_x
[DC395x_MAX_SG_LISTENTRY
- 1].address
;
3141 srb
->segment_x
[0].length
=
3142 srb
->segment_x
[DC395x_MAX_SG_LISTENTRY
- 1].length
;
3147 * Complete execution of a SCSI command
3148 * Signal completion to the generic SCSI driver
3150 static void srb_done(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
3151 struct ScsiReqBlk
*srb
)
3154 struct scsi_cmnd
*cmd
= srb
->cmd
;
3155 enum dma_data_direction dir
= cmd
->sc_data_direction
;
3158 dprintkdbg(DBG_1
, "srb_done: (0x%p) <%02i-%i>\n", srb
->cmd
,
3159 srb
->cmd
->device
->id
, (u8
)srb
->cmd
->device
->lun
);
3160 dprintkdbg(DBG_SG
, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
3161 srb
, scsi_sg_count(cmd
), srb
->sg_index
, srb
->sg_count
,
3163 status
= srb
->target_status
;
3164 set_host_byte(cmd
, DID_OK
);
3165 set_status_byte(cmd
, SAM_STAT_GOOD
);
3166 if (srb
->flag
& AUTO_REQSENSE
) {
3167 dprintkdbg(DBG_0
, "srb_done: AUTO_REQSENSE1\n");
3168 pci_unmap_srb_sense(acb
, srb
);
3170 ** target status..........................
3172 srb
->flag
&= ~AUTO_REQSENSE
;
3173 srb
->adapter_status
= 0;
3174 srb
->target_status
= SAM_STAT_CHECK_CONDITION
;
3175 if (debug_enabled(DBG_1
)) {
3176 switch (cmd
->sense_buffer
[2] & 0x0f) {
3178 dprintkl(KERN_DEBUG
,
3179 "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3180 cmd
->cmnd
[0], dcb
->target_id
,
3181 dcb
->target_lun
, status
, acb
->scan_devices
);
3183 case UNIT_ATTENTION
:
3184 dprintkl(KERN_DEBUG
,
3185 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3186 cmd
->cmnd
[0], dcb
->target_id
,
3187 dcb
->target_lun
, status
, acb
->scan_devices
);
3189 case ILLEGAL_REQUEST
:
3190 dprintkl(KERN_DEBUG
,
3191 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3192 cmd
->cmnd
[0], dcb
->target_id
,
3193 dcb
->target_lun
, status
, acb
->scan_devices
);
3196 dprintkl(KERN_DEBUG
,
3197 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3198 cmd
->cmnd
[0], dcb
->target_id
,
3199 dcb
->target_lun
, status
, acb
->scan_devices
);
3201 case HARDWARE_ERROR
:
3202 dprintkl(KERN_DEBUG
,
3203 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3204 cmd
->cmnd
[0], dcb
->target_id
,
3205 dcb
->target_lun
, status
, acb
->scan_devices
);
3208 if (cmd
->sense_buffer
[7] >= 6)
3209 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3210 "(0x%08x 0x%08x)\n",
3211 cmd
->sense_buffer
[2], cmd
->sense_buffer
[12],
3212 cmd
->sense_buffer
[13],
3213 *((unsigned int *)(cmd
->sense_buffer
+ 3)),
3214 *((unsigned int *)(cmd
->sense_buffer
+ 8)));
3216 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3217 cmd
->sense_buffer
[2],
3218 *((unsigned int *)(cmd
->sense_buffer
+ 3)));
3221 if (status
== SAM_STAT_CHECK_CONDITION
) {
3222 set_host_byte(cmd
, DID_BAD_TARGET
);
3225 dprintkdbg(DBG_0
, "srb_done: AUTO_REQSENSE2\n");
3227 set_status_byte(cmd
, SAM_STAT_CHECK_CONDITION
);
3232 /*************************************************************/
3235 * target status..........................
3237 if (status
== SAM_STAT_CHECK_CONDITION
) {
3238 request_sense(acb
, dcb
, srb
);
3240 } else if (status
== SAM_STAT_TASK_SET_FULL
) {
3241 tempcnt
= (u8
)list_size(&dcb
->srb_going_list
);
3242 dprintkl(KERN_INFO
, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3243 dcb
->target_id
, dcb
->target_lun
, tempcnt
);
3246 dcb
->max_command
= tempcnt
;
3248 list_move(&srb
->list
, &dcb
->srb_waiting_list
);
3249 waiting_set_timer(acb
, HZ
/ 20);
3250 srb
->adapter_status
= 0;
3251 srb
->target_status
= 0;
3253 } else if (status
== SCSI_STAT_SEL_TIMEOUT
) {
3254 srb
->adapter_status
= H_SEL_TIMEOUT
;
3255 srb
->target_status
= 0;
3256 set_host_byte(cmd
, DID_NO_CONNECT
);
3258 srb
->adapter_status
= 0;
3259 set_host_byte(cmd
, DID_ERROR
);
3260 set_status_byte(cmd
, status
);
3264 ** process initiator status..........................
3266 status
= srb
->adapter_status
;
3267 if (status
& H_OVER_UNDER_RUN
) {
3268 srb
->target_status
= 0;
3269 scsi_msg_to_host_byte(cmd
, srb
->end_message
);
3270 } else if (srb
->status
& PARITY_ERROR
) {
3271 set_host_byte(cmd
, DID_PARITY
);
3272 } else { /* No error */
3274 srb
->adapter_status
= 0;
3275 srb
->target_status
= 0;
3280 /* Check Error Conditions */
3283 pci_unmap_srb(acb
, srb
);
3285 if (cmd
->cmnd
[0] == INQUIRY
) {
3286 unsigned char *base
= NULL
;
3287 struct ScsiInqData
*ptr
;
3288 unsigned long flags
= 0;
3289 struct scatterlist
* sg
= scsi_sglist(cmd
);
3290 size_t offset
= 0, len
= sizeof(struct ScsiInqData
);
3292 local_irq_save(flags
);
3293 base
= scsi_kmap_atomic_sg(sg
, scsi_sg_count(cmd
), &offset
, &len
);
3294 ptr
= (struct ScsiInqData
*)(base
+ offset
);
3296 if (!ckc_only
&& get_host_byte(cmd
) == DID_OK
3297 && cmd
->cmnd
[2] == 0 && scsi_bufflen(cmd
) >= 8
3298 && dir
!= DMA_NONE
&& ptr
&& (ptr
->Vers
& 0x07) >= 2)
3299 dcb
->inquiry7
= ptr
->Flags
;
3301 /*if( srb->cmd->cmnd[0] == INQUIRY && */
3302 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3303 if ((get_host_byte(cmd
) == DID_OK
) ||
3304 (get_status_byte(cmd
) == SAM_STAT_CHECK_CONDITION
)) {
3305 if (!dcb
->init_tcq_flag
) {
3306 add_dev(acb
, dcb
, ptr
);
3307 dcb
->init_tcq_flag
= 1;
3311 scsi_kunmap_atomic_sg(base
);
3312 local_irq_restore(flags
);
3315 /* Here is the info for Doug Gilbert's sg3 ... */
3316 scsi_set_resid(cmd
, srb
->total_xfer_length
);
3317 if (debug_enabled(DBG_KG
)) {
3318 if (srb
->total_xfer_length
)
3319 dprintkdbg(DBG_KG
, "srb_done: (0x%p) <%02i-%i> "
3320 "cmnd=0x%02x Missed %i bytes\n",
3321 cmd
, cmd
->device
->id
, (u8
)cmd
->device
->lun
,
3322 cmd
->cmnd
[0], srb
->total_xfer_length
);
3325 if (srb
!= acb
->tmp_srb
) {
3326 /* Add to free list */
3327 dprintkdbg(DBG_0
, "srb_done: (0x%p) done result=0x%08x\n",
3329 list_move_tail(&srb
->list
, &acb
->srb_free_list
);
3331 dprintkl(KERN_ERR
, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3335 waiting_process_next(acb
);
3339 /* abort all cmds in our queues */
3340 static void doing_srb_done(struct AdapterCtlBlk
*acb
, u8 did_flag
,
3341 struct scsi_cmnd
*cmd
, u8 force
)
3343 struct DeviceCtlBlk
*dcb
;
3344 dprintkl(KERN_INFO
, "doing_srb_done: pids ");
3346 list_for_each_entry(dcb
, &acb
->dcb_list
, list
) {
3347 struct ScsiReqBlk
*srb
;
3348 struct ScsiReqBlk
*tmp
;
3349 struct scsi_cmnd
*p
;
3351 list_for_each_entry_safe(srb
, tmp
, &dcb
->srb_going_list
, list
) {
3353 printk("G:%p(%02i-%i) ", p
,
3354 p
->device
->id
, (u8
)p
->device
->lun
);
3355 list_del(&srb
->list
);
3357 list_add_tail(&srb
->list
, &acb
->srb_free_list
);
3358 set_host_byte(p
, did_flag
);
3359 set_status_byte(p
, SAM_STAT_GOOD
);
3360 pci_unmap_srb_sense(acb
, srb
);
3361 pci_unmap_srb(acb
, srb
);
3363 /* For new EH, we normally don't need to give commands back,
3364 * as they all complete or all time out */
3368 if (!list_empty(&dcb
->srb_going_list
))
3369 dprintkl(KERN_DEBUG
,
3370 "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3371 dcb
->target_id
, dcb
->target_lun
);
3373 dprintkl(KERN_DEBUG
,
3374 "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3375 dcb
->target_id
, dcb
->target_lun
,
3379 list_for_each_entry_safe(srb
, tmp
, &dcb
->srb_waiting_list
, list
) {
3382 printk("W:%p<%02i-%i>", p
, p
->device
->id
,
3383 (u8
)p
->device
->lun
);
3384 list_move_tail(&srb
->list
, &acb
->srb_free_list
);
3385 set_host_byte(p
, did_flag
);
3386 set_status_byte(p
, SAM_STAT_GOOD
);
3387 pci_unmap_srb_sense(acb
, srb
);
3388 pci_unmap_srb(acb
, srb
);
3390 /* For new EH, we normally don't need to give commands back,
3391 * as they all complete or all time out */
3395 if (!list_empty(&dcb
->srb_waiting_list
))
3396 dprintkl(KERN_DEBUG
, "ML queued %i cmnds again to <%02i-%i>\n",
3397 list_size(&dcb
->srb_waiting_list
), dcb
->target_id
,
3399 dcb
->flag
&= ~ABORT_DEV_
;
3405 static void reset_scsi_bus(struct AdapterCtlBlk
*acb
)
3407 dprintkdbg(DBG_0
, "reset_scsi_bus: acb=%p\n", acb
);
3408 acb
->acb_flag
|= RESET_DEV
; /* RESET_DETECT, RESET_DONE, RESET_DEV */
3409 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_RSTSCSI
);
3411 while (!(DC395x_read8(acb
, TRM_S1040_SCSI_INTSTATUS
) & INT_SCSIRESET
))
3416 static void set_basic_config(struct AdapterCtlBlk
*acb
)
3420 DC395x_write8(acb
, TRM_S1040_SCSI_TIMEOUT
, acb
->sel_timeout
);
3421 if (acb
->config
& HCC_PARITY
)
3422 bval
= PHASELATCH
| INITIATOR
| BLOCKRST
| PARITYCHECK
;
3424 bval
= PHASELATCH
| INITIATOR
| BLOCKRST
;
3426 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG0
, bval
);
3428 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3429 DC395x_write8(acb
, TRM_S1040_SCSI_CONFIG1
, 0x03); /* was 0x13: default */
3430 /* program Host ID */
3431 DC395x_write8(acb
, TRM_S1040_SCSI_HOSTID
, acb
->scsi_host
->this_id
);
3432 /* set ansynchronous transfer */
3433 DC395x_write8(acb
, TRM_S1040_SCSI_OFFSET
, 0x00);
3434 /* Turn LED control off */
3435 wval
= DC395x_read16(acb
, TRM_S1040_GEN_CONTROL
) & 0x7F;
3436 DC395x_write16(acb
, TRM_S1040_GEN_CONTROL
, wval
);
3438 wval
= DC395x_read16(acb
, TRM_S1040_DMA_CONFIG
) & ~DMA_FIFO_CTRL
;
3440 DMA_FIFO_HALF_HALF
| DMA_ENHANCE
/*| DMA_MEM_MULTI_READ */ ;
3441 DC395x_write16(acb
, TRM_S1040_DMA_CONFIG
, wval
);
3442 /* Clear pending interrupt status */
3443 DC395x_read8(acb
, TRM_S1040_SCSI_INTSTATUS
);
3444 /* Enable SCSI interrupt */
3445 DC395x_write8(acb
, TRM_S1040_SCSI_INTEN
, 0x7F);
3446 DC395x_write8(acb
, TRM_S1040_DMA_INTEN
, EN_SCSIINTR
| EN_DMAXFERERROR
3447 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3452 static void scsi_reset_detect(struct AdapterCtlBlk
*acb
)
3454 dprintkl(KERN_INFO
, "scsi_reset_detect: acb=%p\n", acb
);
3455 /* delay half a second */
3456 if (timer_pending(&acb
->waiting_timer
))
3457 del_timer(&acb
->waiting_timer
);
3459 DC395x_write8(acb
, TRM_S1040_SCSI_CONTROL
, DO_RSTMODULE
);
3460 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, DMARESETMODULE
);
3461 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3463 /* Maybe we locked up the bus? Then lets wait even longer ... */
3465 jiffies
+ 5 * HZ
/ 2 +
3466 HZ
* acb
->eeprom
.delay_time
;
3468 clear_fifo(acb
, "scsi_reset_detect");
3469 set_basic_config(acb
);
3471 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3473 if (acb
->acb_flag
& RESET_DEV
) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
3474 acb
->acb_flag
|= RESET_DONE
;
3476 acb
->acb_flag
|= RESET_DETECT
;
3477 reset_dev_param(acb
);
3478 doing_srb_done(acb
, DID_RESET
, NULL
, 1);
3479 /*DC395x_RecoverSRB( acb ); */
3480 acb
->active_dcb
= NULL
;
3482 waiting_process_next(acb
);
3487 static void request_sense(struct AdapterCtlBlk
*acb
, struct DeviceCtlBlk
*dcb
,
3488 struct ScsiReqBlk
*srb
)
3490 struct scsi_cmnd
*cmd
= srb
->cmd
;
3491 dprintkdbg(DBG_1
, "request_sense: (0x%p) <%02i-%i>\n",
3492 cmd
, cmd
->device
->id
, (u8
)cmd
->device
->lun
);
3494 srb
->flag
|= AUTO_REQSENSE
;
3495 srb
->adapter_status
= 0;
3496 srb
->target_status
= 0;
3498 /* KG: Can this prevent crap sense data ? */
3499 memset(cmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
3501 /* Save some data */
3502 srb
->segment_x
[DC395x_MAX_SG_LISTENTRY
- 1].address
=
3503 srb
->segment_x
[0].address
;
3504 srb
->segment_x
[DC395x_MAX_SG_LISTENTRY
- 1].length
=
3505 srb
->segment_x
[0].length
;
3506 srb
->xferred
= srb
->total_xfer_length
;
3507 /* srb->segment_x : a one entry of S/G list table */
3508 srb
->total_xfer_length
= SCSI_SENSE_BUFFERSIZE
;
3509 srb
->segment_x
[0].length
= SCSI_SENSE_BUFFERSIZE
;
3510 /* Map sense buffer */
3511 srb
->segment_x
[0].address
= dma_map_single(&acb
->dev
->dev
,
3512 cmd
->sense_buffer
, SCSI_SENSE_BUFFERSIZE
,
3514 dprintkdbg(DBG_SG
, "request_sense: map buffer %p->%08x(%05x)\n",
3515 cmd
->sense_buffer
, srb
->segment_x
[0].address
,
3516 SCSI_SENSE_BUFFERSIZE
);
3520 if (start_scsi(acb
, dcb
, srb
)) { /* Should only happen, if sb. else grabs the bus */
3521 dprintkl(KERN_DEBUG
,
3522 "request_sense: (0x%p) failed <%02i-%i>\n",
3523 srb
->cmd
, dcb
->target_id
, dcb
->target_lun
);
3524 list_move(&srb
->list
, &dcb
->srb_waiting_list
);
3525 waiting_set_timer(acb
, HZ
/ 100);
3531 * device_alloc - Allocate a new device instance. This create the
3532 * devices instance and sets up all the data items. The adapter
3533 * instance is required to obtain confiuration information for this
3534 * device. This does *not* add this device to the adapters device
3537 * @acb: The adapter to obtain configuration information from.
3538 * @target: The target for the new device.
3539 * @lun: The lun for the new device.
3541 * Return the new device if successful or NULL on failure.
3543 static struct DeviceCtlBlk
*device_alloc(struct AdapterCtlBlk
*acb
,
3546 struct NvRamType
*eeprom
= &acb
->eeprom
;
3547 u8 period_index
= eeprom
->target
[target
].period
& 0x07;
3548 struct DeviceCtlBlk
*dcb
;
3550 dcb
= kmalloc(sizeof(struct DeviceCtlBlk
), GFP_ATOMIC
);
3551 dprintkdbg(DBG_0
, "device_alloc: <%02i-%i>\n", target
, lun
);
3555 INIT_LIST_HEAD(&dcb
->srb_going_list
);
3556 INIT_LIST_HEAD(&dcb
->srb_waiting_list
);
3557 dcb
->active_srb
= NULL
;
3559 dcb
->max_command
= 1;
3560 dcb
->target_id
= target
;
3561 dcb
->target_lun
= lun
;
3562 dcb
->dev_mode
= eeprom
->target
[target
].cfg0
;
3563 #ifndef DC395x_NO_DISCONNECT
3565 IDENTIFY(dcb
->dev_mode
& NTC_DO_DISCONNECT
, lun
);
3567 dcb
->identify_msg
= IDENTIFY(0, lun
);
3571 dcb
->min_nego_period
= clock_period
[period_index
];
3572 dcb
->sync_period
= 0;
3573 dcb
->sync_offset
= 0;
3576 #ifndef DC395x_NO_WIDE
3577 if ((dcb
->dev_mode
& NTC_DO_WIDE_NEGO
)
3578 && (acb
->config
& HCC_WIDE_CARD
))
3579 dcb
->sync_mode
|= WIDE_NEGO_ENABLE
;
3581 #ifndef DC395x_NO_SYNC
3582 if (dcb
->dev_mode
& NTC_DO_SYNC_NEGO
)
3583 if (!(lun
) || current_sync_offset
)
3584 dcb
->sync_mode
|= SYNC_NEGO_ENABLE
;
3586 if (dcb
->target_lun
!= 0) {
3588 struct DeviceCtlBlk
*p
= NULL
, *iter
;
3590 list_for_each_entry(iter
, &acb
->dcb_list
, list
)
3591 if (iter
->target_id
== dcb
->target_id
) {
3602 "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3603 dcb
->target_id
, dcb
->target_lun
,
3604 p
->target_id
, p
->target_lun
);
3605 dcb
->sync_mode
= p
->sync_mode
;
3606 dcb
->sync_period
= p
->sync_period
;
3607 dcb
->min_nego_period
= p
->min_nego_period
;
3608 dcb
->sync_offset
= p
->sync_offset
;
3609 dcb
->inquiry7
= p
->inquiry7
;
3616 * adapter_add_device - Adds the device instance to the adaptor instance.
3618 * @acb: The adapter device to be updated
3619 * @dcb: A newly created and initialised device instance to add.
3621 static void adapter_add_device(struct AdapterCtlBlk
*acb
,
3622 struct DeviceCtlBlk
*dcb
)
3624 /* backpointer to adapter */
3627 /* set run_robin to this device if it is currently empty */
3628 if (list_empty(&acb
->dcb_list
))
3629 acb
->dcb_run_robin
= dcb
;
3631 /* add device to list */
3632 list_add_tail(&dcb
->list
, &acb
->dcb_list
);
3634 /* update device maps */
3635 acb
->dcb_map
[dcb
->target_id
] |= (1 << dcb
->target_lun
);
3636 acb
->children
[dcb
->target_id
][dcb
->target_lun
] = dcb
;
3641 * adapter_remove_device - Removes the device instance from the adaptor
3642 * instance. The device instance is not check in any way or freed by this.
3643 * The caller is expected to take care of that. This will simply remove the
3644 * device from the adapters data strcutures.
3646 * @acb: The adapter device to be updated
3647 * @dcb: A device that has previously been added to the adapter.
3649 static void adapter_remove_device(struct AdapterCtlBlk
*acb
,
3650 struct DeviceCtlBlk
*dcb
)
3652 struct DeviceCtlBlk
*i
;
3653 struct DeviceCtlBlk
*tmp
;
3654 dprintkdbg(DBG_0
, "adapter_remove_device: <%02i-%i>\n",
3655 dcb
->target_id
, dcb
->target_lun
);
3657 /* fix up any pointers to this device that we have in the adapter */
3658 if (acb
->active_dcb
== dcb
)
3659 acb
->active_dcb
= NULL
;
3660 if (acb
->dcb_run_robin
== dcb
)
3661 acb
->dcb_run_robin
= dcb_get_next(&acb
->dcb_list
, dcb
);
3663 /* unlink from list */
3664 list_for_each_entry_safe(i
, tmp
, &acb
->dcb_list
, list
)
3670 /* clear map and children */
3671 acb
->dcb_map
[dcb
->target_id
] &= ~(1 << dcb
->target_lun
);
3672 acb
->children
[dcb
->target_id
][dcb
->target_lun
] = NULL
;
3678 * adapter_remove_and_free_device - Removes a single device from the adapter
3679 * and then frees the device information.
3681 * @acb: The adapter device to be updated
3682 * @dcb: A device that has previously been added to the adapter.
3684 static void adapter_remove_and_free_device(struct AdapterCtlBlk
*acb
,
3685 struct DeviceCtlBlk
*dcb
)
3687 if (list_size(&dcb
->srb_going_list
) > 1) {
3688 dprintkdbg(DBG_1
, "adapter_remove_and_free_device: <%02i-%i> "
3689 "Won't remove because of %i active requests.\n",
3690 dcb
->target_id
, dcb
->target_lun
,
3691 list_size(&dcb
->srb_going_list
));
3694 adapter_remove_device(acb
, dcb
);
3700 * adapter_remove_and_free_all_devices - Removes and frees all of the
3701 * devices associated with the specified adapter.
3703 * @acb: The adapter from which all devices should be removed.
3705 static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk
* acb
)
3707 struct DeviceCtlBlk
*dcb
;
3708 struct DeviceCtlBlk
*tmp
;
3709 dprintkdbg(DBG_1
, "adapter_remove_and_free_all_devices: num=%i\n",
3710 list_size(&acb
->dcb_list
));
3712 list_for_each_entry_safe(dcb
, tmp
, &acb
->dcb_list
, list
)
3713 adapter_remove_and_free_device(acb
, dcb
);
3718 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3719 * scsi device that we need to deal with. We allocate a new device and then
3720 * insert that device into the adapters device list.
3722 * @scsi_device: The new scsi device that we need to handle.
3724 static int dc395x_slave_alloc(struct scsi_device
*scsi_device
)
3726 struct AdapterCtlBlk
*acb
= (struct AdapterCtlBlk
*)scsi_device
->host
->hostdata
;
3727 struct DeviceCtlBlk
*dcb
;
3729 dcb
= device_alloc(acb
, scsi_device
->id
, scsi_device
->lun
);
3732 adapter_add_device(acb
, dcb
);
3739 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3740 * device that is going away.
3742 * @scsi_device: The new scsi device that we need to handle.
3744 static void dc395x_slave_destroy(struct scsi_device
*scsi_device
)
3746 struct AdapterCtlBlk
*acb
= (struct AdapterCtlBlk
*)scsi_device
->host
->hostdata
;
3747 struct DeviceCtlBlk
*dcb
= find_dcb(acb
, scsi_device
->id
, scsi_device
->lun
);
3749 adapter_remove_and_free_device(acb
, dcb
);
3756 * trms1040_wait_30us: wait for 30 us
3758 * Waits for 30us (using the chip by the looks of it..)
3760 * @io_port: base I/O address
3762 static void trms1040_wait_30us(unsigned long io_port
)
3764 /* ScsiPortStallExecution(30); wait 30 us */
3765 outb(5, io_port
+ TRM_S1040_GEN_TIMER
);
3766 while (!(inb(io_port
+ TRM_S1040_GEN_STATUS
) & GTIMEOUT
))
3772 * trms1040_write_cmd - write the secified command and address to
3775 * @io_port: base I/O address
3776 * @cmd: SB + op code (command) to send
3777 * @addr: address to send
3779 static void trms1040_write_cmd(unsigned long io_port
, u8 cmd
, u8 addr
)
3784 /* program SB + OP code */
3785 for (i
= 0; i
< 3; i
++, cmd
<<= 1) {
3786 send_data
= NVR_SELECT
;
3787 if (cmd
& 0x04) /* Start from bit 2 */
3788 send_data
|= NVR_BITOUT
;
3790 outb(send_data
, io_port
+ TRM_S1040_GEN_NVRAM
);
3791 trms1040_wait_30us(io_port
);
3792 outb((send_data
| NVR_CLOCK
),
3793 io_port
+ TRM_S1040_GEN_NVRAM
);
3794 trms1040_wait_30us(io_port
);
3798 for (i
= 0; i
< 7; i
++, addr
<<= 1) {
3799 send_data
= NVR_SELECT
;
3800 if (addr
& 0x40) /* Start from bit 6 */
3801 send_data
|= NVR_BITOUT
;
3803 outb(send_data
, io_port
+ TRM_S1040_GEN_NVRAM
);
3804 trms1040_wait_30us(io_port
);
3805 outb((send_data
| NVR_CLOCK
),
3806 io_port
+ TRM_S1040_GEN_NVRAM
);
3807 trms1040_wait_30us(io_port
);
3809 outb(NVR_SELECT
, io_port
+ TRM_S1040_GEN_NVRAM
);
3810 trms1040_wait_30us(io_port
);
3815 * trms1040_set_data - store a single byte in the eeprom
3817 * Called from write all to write a single byte into the SSEEPROM
3818 * Which is done one bit at a time.
3820 * @io_port: base I/O address
3821 * @addr: offset into EEPROM
3822 * @byte: bytes to write
3824 static void trms1040_set_data(unsigned long io_port
, u8 addr
, u8 byte
)
3829 /* Send write command & address */
3830 trms1040_write_cmd(io_port
, 0x05, addr
);
3833 for (i
= 0; i
< 8; i
++, byte
<<= 1) {
3834 send_data
= NVR_SELECT
;
3835 if (byte
& 0x80) /* Start from bit 7 */
3836 send_data
|= NVR_BITOUT
;
3838 outb(send_data
, io_port
+ TRM_S1040_GEN_NVRAM
);
3839 trms1040_wait_30us(io_port
);
3840 outb((send_data
| NVR_CLOCK
), io_port
+ TRM_S1040_GEN_NVRAM
);
3841 trms1040_wait_30us(io_port
);
3843 outb(NVR_SELECT
, io_port
+ TRM_S1040_GEN_NVRAM
);
3844 trms1040_wait_30us(io_port
);
3846 /* Disable chip select */
3847 outb(0, io_port
+ TRM_S1040_GEN_NVRAM
);
3848 trms1040_wait_30us(io_port
);
3850 outb(NVR_SELECT
, io_port
+ TRM_S1040_GEN_NVRAM
);
3851 trms1040_wait_30us(io_port
);
3853 /* Wait for write ready */
3855 outb((NVR_SELECT
| NVR_CLOCK
), io_port
+ TRM_S1040_GEN_NVRAM
);
3856 trms1040_wait_30us(io_port
);
3858 outb(NVR_SELECT
, io_port
+ TRM_S1040_GEN_NVRAM
);
3859 trms1040_wait_30us(io_port
);
3861 if (inb(io_port
+ TRM_S1040_GEN_NVRAM
) & NVR_BITIN
)
3865 /* Disable chip select */
3866 outb(0, io_port
+ TRM_S1040_GEN_NVRAM
);
3871 * trms1040_write_all - write 128 bytes to the eeprom
3873 * Write the supplied 128 bytes to the chips SEEPROM
3875 * @eeprom: the data to write
3876 * @io_port: the base io port
3878 static void trms1040_write_all(struct NvRamType
*eeprom
, unsigned long io_port
)
3880 u8
*b_eeprom
= (u8
*)eeprom
;
3883 /* Enable SEEPROM */
3884 outb((inb(io_port
+ TRM_S1040_GEN_CONTROL
) | EN_EEPROM
),
3885 io_port
+ TRM_S1040_GEN_CONTROL
);
3888 trms1040_write_cmd(io_port
, 0x04, 0xFF);
3889 outb(0, io_port
+ TRM_S1040_GEN_NVRAM
);
3890 trms1040_wait_30us(io_port
);
3893 for (addr
= 0; addr
< 128; addr
++, b_eeprom
++)
3894 trms1040_set_data(io_port
, addr
, *b_eeprom
);
3897 trms1040_write_cmd(io_port
, 0x04, 0x00);
3898 outb(0, io_port
+ TRM_S1040_GEN_NVRAM
);
3899 trms1040_wait_30us(io_port
);
3901 /* Disable SEEPROM */
3902 outb((inb(io_port
+ TRM_S1040_GEN_CONTROL
) & ~EN_EEPROM
),
3903 io_port
+ TRM_S1040_GEN_CONTROL
);
3908 * trms1040_get_data - get a single byte from the eeprom
3910 * Called from read all to read a single byte into the SSEEPROM
3911 * Which is done one bit at a time.
3913 * @io_port: base I/O address
3914 * @addr: offset into SEEPROM
3916 * Returns the byte read.
3918 static u8
trms1040_get_data(unsigned long io_port
, u8 addr
)
3924 /* Send read command & address */
3925 trms1040_write_cmd(io_port
, 0x06, addr
);
3928 for (i
= 0; i
< 8; i
++) {
3929 outb((NVR_SELECT
| NVR_CLOCK
), io_port
+ TRM_S1040_GEN_NVRAM
);
3930 trms1040_wait_30us(io_port
);
3931 outb(NVR_SELECT
, io_port
+ TRM_S1040_GEN_NVRAM
);
3933 /* Get data bit while falling edge */
3934 read_byte
= inb(io_port
+ TRM_S1040_GEN_NVRAM
);
3936 if (read_byte
& NVR_BITIN
)
3939 trms1040_wait_30us(io_port
);
3942 /* Disable chip select */
3943 outb(0, io_port
+ TRM_S1040_GEN_NVRAM
);
3949 * trms1040_read_all - read all bytes from the eeprom
3951 * Read the 128 bytes from the SEEPROM.
3953 * @eeprom: where to store the data
3954 * @io_port: the base io port
3956 static void trms1040_read_all(struct NvRamType
*eeprom
, unsigned long io_port
)
3958 u8
*b_eeprom
= (u8
*)eeprom
;
3961 /* Enable SEEPROM */
3962 outb((inb(io_port
+ TRM_S1040_GEN_CONTROL
) | EN_EEPROM
),
3963 io_port
+ TRM_S1040_GEN_CONTROL
);
3966 for (addr
= 0; addr
< 128; addr
++, b_eeprom
++)
3967 *b_eeprom
= trms1040_get_data(io_port
, addr
);
3969 /* Disable SEEPROM */
3970 outb((inb(io_port
+ TRM_S1040_GEN_CONTROL
) & ~EN_EEPROM
),
3971 io_port
+ TRM_S1040_GEN_CONTROL
);
3977 * check_eeprom - get and check contents of the eeprom
3979 * Read seeprom 128 bytes into the memory provider in eeprom.
3980 * Checks the checksum and if it's not correct it uses a set of default
3983 * @eeprom: caller allocated strcuture to read the eeprom data into
3984 * @io_port: io port to read from
3986 static void check_eeprom(struct NvRamType
*eeprom
, unsigned long io_port
)
3988 u16
*w_eeprom
= (u16
*)eeprom
;
3994 trms1040_read_all(eeprom
, io_port
); /* read eeprom */
3997 for (w_addr
= 0, w_eeprom
= (u16
*)eeprom
; w_addr
< 64;
3998 w_addr
++, w_eeprom
++)
4000 if (cksum
!= 0x1234) {
4002 * Checksum is wrong.
4003 * Load a set of defaults into the eeprom buffer
4005 dprintkl(KERN_WARNING
,
4006 "EEProm checksum error: using default values and options.\n");
4007 eeprom
->sub_vendor_id
[0] = (u8
)PCI_VENDOR_ID_TEKRAM
;
4008 eeprom
->sub_vendor_id
[1] = (u8
)(PCI_VENDOR_ID_TEKRAM
>> 8);
4009 eeprom
->sub_sys_id
[0] = (u8
)PCI_DEVICE_ID_TEKRAM_TRMS1040
;
4010 eeprom
->sub_sys_id
[1] =
4011 (u8
)(PCI_DEVICE_ID_TEKRAM_TRMS1040
>> 8);
4012 eeprom
->sub_class
= 0x00;
4013 eeprom
->vendor_id
[0] = (u8
)PCI_VENDOR_ID_TEKRAM
;
4014 eeprom
->vendor_id
[1] = (u8
)(PCI_VENDOR_ID_TEKRAM
>> 8);
4015 eeprom
->device_id
[0] = (u8
)PCI_DEVICE_ID_TEKRAM_TRMS1040
;
4016 eeprom
->device_id
[1] =
4017 (u8
)(PCI_DEVICE_ID_TEKRAM_TRMS1040
>> 8);
4018 eeprom
->reserved
= 0x00;
4020 for (d_addr
= 0, d_eeprom
= (u32
*)eeprom
->target
;
4021 d_addr
< 16; d_addr
++, d_eeprom
++)
4022 *d_eeprom
= 0x00000077; /* cfg3,cfg2,period,cfg0 */
4024 *d_eeprom
++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
4025 *d_eeprom
++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
4026 for (d_addr
= 0; d_addr
< 12; d_addr
++, d_eeprom
++)
4029 /* Now load defaults (maybe set by boot/module params) */
4030 set_safe_settings();
4032 eeprom_override(eeprom
);
4034 eeprom
->cksum
= 0x00;
4035 for (w_addr
= 0, cksum
= 0, w_eeprom
= (u16
*)eeprom
;
4036 w_addr
< 63; w_addr
++, w_eeprom
++)
4039 *w_eeprom
= 0x1234 - cksum
;
4040 trms1040_write_all(eeprom
, io_port
);
4041 eeprom
->delay_time
= cfg_data
[CFG_RESET_DELAY
].value
;
4043 set_safe_settings();
4044 eeprom_index_to_delay(eeprom
);
4045 eeprom_override(eeprom
);
4051 * print_eeprom_settings - output the eeprom settings
4052 * to the kernel log so people can see what they were.
4054 * @eeprom: The eeprom data strucutre to show details for.
4056 static void print_eeprom_settings(struct NvRamType
*eeprom
)
4058 dprintkl(KERN_INFO
, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4060 eeprom
->target
[0].period
,
4061 clock_speed
[eeprom
->target
[0].period
] / 10,
4062 clock_speed
[eeprom
->target
[0].period
] % 10,
4063 eeprom
->target
[0].cfg0
);
4064 dprintkl(KERN_INFO
, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4065 eeprom
->channel_cfg
, eeprom
->max_tag
,
4066 1 << eeprom
->max_tag
, eeprom
->delay_time
);
4070 /* Free SG tables */
4071 static void adapter_sg_tables_free(struct AdapterCtlBlk
*acb
)
4074 const unsigned srbs_per_page
= PAGE_SIZE
/SEGMENTX_LEN
;
4076 for (i
= 0; i
< DC395x_MAX_SRB_CNT
; i
+= srbs_per_page
)
4077 kfree(acb
->srb_array
[i
].segment_x
);
4082 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4083 * should never cross a page boundary */
4084 static int adapter_sg_tables_alloc(struct AdapterCtlBlk
*acb
)
4086 const unsigned mem_needed
= (DC395x_MAX_SRB_CNT
+1)
4088 int pages
= (mem_needed
+(PAGE_SIZE
-1))/PAGE_SIZE
;
4089 const unsigned srbs_per_page
= PAGE_SIZE
/SEGMENTX_LEN
;
4092 struct SGentry
*ptr
;
4094 for (i
= 0; i
< DC395x_MAX_SRB_CNT
; i
++)
4095 acb
->srb_array
[i
].segment_x
= NULL
;
4097 dprintkdbg(DBG_1
, "Allocate %i pages for SG tables\n", pages
);
4099 ptr
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4101 adapter_sg_tables_free(acb
);
4104 dprintkdbg(DBG_1
, "Allocate %li bytes at %p for SG segments %i\n",
4105 PAGE_SIZE
, ptr
, srb_idx
);
4107 while (i
< srbs_per_page
&& srb_idx
< DC395x_MAX_SRB_CNT
)
4108 acb
->srb_array
[srb_idx
++].segment_x
=
4109 ptr
+ (i
++ * DC395x_MAX_SG_LISTENTRY
);
4111 if (i
< srbs_per_page
)
4112 acb
->srb
.segment_x
=
4113 ptr
+ (i
* DC395x_MAX_SG_LISTENTRY
);
4115 dprintkl(KERN_DEBUG
, "No space for tmsrb SG table reserved?!\n");
4122 * adapter_print_config - print adapter connection and termination
4125 * The io port in the adapter needs to have been set before calling
4128 * @acb: The adapter to print the information for.
4130 static void adapter_print_config(struct AdapterCtlBlk
*acb
)
4134 bval
= DC395x_read8(acb
, TRM_S1040_GEN_STATUS
);
4135 dprintkl(KERN_INFO
, "%sConnectors: ",
4136 ((bval
& WIDESCSI
) ? "(Wide) " : ""));
4137 if (!(bval
& CON5068
))
4138 printk("ext%s ", !(bval
& EXT68HIGH
) ? "68" : "50");
4139 if (!(bval
& CON68
))
4140 printk("int68%s ", !(bval
& INT68HIGH
) ? "" : "(50)");
4141 if (!(bval
& CON50
))
4143 if ((bval
& (CON5068
| CON50
| CON68
)) ==
4144 0 /*(CON5068 | CON50 | CON68) */ )
4145 printk(" Oops! (All 3?) ");
4146 bval
= DC395x_read8(acb
, TRM_S1040_GEN_CONTROL
);
4147 printk(" Termination: ");
4148 if (bval
& DIS_TERM
)
4149 printk("Disabled\n");
4151 if (bval
& AUTOTERM
)
4153 if (bval
& LOW8TERM
)
4163 * adapter_init_params - Initialize the various parameters in the
4164 * adapter structure. Note that the pointer to the scsi_host is set
4165 * early (when this instance is created) and the io_port and irq
4166 * values are set later after they have been reserved. This just gets
4167 * everything set to a good starting position.
4169 * The eeprom structure in the adapter needs to have been set before
4170 * calling this function.
4172 * @acb: The adapter to initialize.
4174 static void adapter_init_params(struct AdapterCtlBlk
*acb
)
4176 struct NvRamType
*eeprom
= &acb
->eeprom
;
4179 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4180 /* NOTE: acb->io_port_base is set at port registration time */
4181 /* NOTE: acb->io_port_len is set at port registration time */
4183 INIT_LIST_HEAD(&acb
->dcb_list
);
4184 acb
->dcb_run_robin
= NULL
;
4185 acb
->active_dcb
= NULL
;
4187 INIT_LIST_HEAD(&acb
->srb_free_list
);
4188 /* temp SRB for Q tag used or abort command used */
4189 acb
->tmp_srb
= &acb
->srb
;
4190 timer_setup(&acb
->waiting_timer
, waiting_timeout
, 0);
4191 timer_setup(&acb
->selto_timer
, NULL
, 0);
4193 acb
->srb_count
= DC395x_MAX_SRB_CNT
;
4195 acb
->sel_timeout
= DC395x_SEL_TIMEOUT
; /* timeout=250ms */
4196 /* NOTE: acb->irq_level is set at IRQ registration time */
4198 acb
->tag_max_num
= 1 << eeprom
->max_tag
;
4199 if (acb
->tag_max_num
> 30)
4200 acb
->tag_max_num
= 30;
4202 acb
->acb_flag
= 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4203 acb
->gmode2
= eeprom
->channel_cfg
;
4204 acb
->config
= 0; /* NOTE: actually set in adapter_init_chip */
4206 if (eeprom
->channel_cfg
& NAC_SCANLUN
)
4208 acb
->scan_devices
= 1;
4210 acb
->scsi_host
->this_id
= eeprom
->scsi_id
;
4211 acb
->hostid_bit
= (1 << acb
->scsi_host
->this_id
);
4213 for (i
= 0; i
< DC395x_MAX_SCSI_ID
; i
++)
4214 acb
->dcb_map
[i
] = 0;
4218 /* link static array of srbs into the srb free list */
4219 for (i
= 0; i
< acb
->srb_count
- 1; i
++)
4220 list_add_tail(&acb
->srb_array
[i
].list
, &acb
->srb_free_list
);
4225 * adapter_init_scsi_host - Initialize the scsi host instance based on
4226 * values that we have already stored in the adapter instance. There's
4227 * some mention that a lot of these are deprecated, so we won't use
4228 * them (we'll use the ones in the adapter instance) but we'll fill
4229 * them in in case something else needs them.
4231 * The eeprom structure, irq and io ports in the adapter need to have
4232 * been set before calling this function.
4234 * @host: The scsi host instance to fill in the values for.
4236 static void adapter_init_scsi_host(struct Scsi_Host
*host
)
4238 struct AdapterCtlBlk
*acb
= (struct AdapterCtlBlk
*)host
->hostdata
;
4239 struct NvRamType
*eeprom
= &acb
->eeprom
;
4241 host
->max_cmd_len
= 24;
4242 host
->can_queue
= DC395x_MAX_CMD_QUEUE
;
4243 host
->cmd_per_lun
= DC395x_MAX_CMD_PER_LUN
;
4244 host
->this_id
= (int)eeprom
->scsi_id
;
4245 host
->io_port
= acb
->io_port_base
;
4246 host
->n_io_port
= acb
->io_port_len
;
4247 host
->dma_channel
= -1;
4248 host
->unique_id
= acb
->io_port_base
;
4249 host
->irq
= acb
->irq_level
;
4250 acb
->last_reset
= jiffies
;
4253 if (host
->max_id
- 1 == eeprom
->scsi_id
)
4256 if (eeprom
->channel_cfg
& NAC_SCANLUN
)
4264 * adapter_init_chip - Get the chip into a know state and figure out
4265 * some of the settings that apply to this adapter.
4267 * The io port in the adapter needs to have been set before calling
4268 * this function. The config will be configured correctly on return.
4270 * @acb: The adapter which we are to init.
4272 static void adapter_init_chip(struct AdapterCtlBlk
*acb
)
4274 struct NvRamType
*eeprom
= &acb
->eeprom
;
4276 /* Mask all the interrupt */
4277 DC395x_write8(acb
, TRM_S1040_DMA_INTEN
, 0x00);
4278 DC395x_write8(acb
, TRM_S1040_SCSI_INTEN
, 0x00);
4280 /* Reset SCSI module */
4281 DC395x_write16(acb
, TRM_S1040_SCSI_CONTROL
, DO_RSTMODULE
);
4283 /* Reset PCI/DMA module */
4284 DC395x_write8(acb
, TRM_S1040_DMA_CONTROL
, DMARESETMODULE
);
4287 /* program configuration 0 */
4288 acb
->config
= HCC_AUTOTERM
| HCC_PARITY
;
4289 if (DC395x_read8(acb
, TRM_S1040_GEN_STATUS
) & WIDESCSI
)
4290 acb
->config
|= HCC_WIDE_CARD
;
4292 if (eeprom
->channel_cfg
& NAC_POWERON_SCSI_RESET
)
4293 acb
->config
|= HCC_SCSI_RESET
;
4295 if (acb
->config
& HCC_SCSI_RESET
) {
4296 dprintkl(KERN_INFO
, "Performing initial SCSI bus reset\n");
4297 DC395x_write8(acb
, TRM_S1040_SCSI_CONTROL
, DO_RSTSCSI
);
4299 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4300 /*spin_unlock_irq (&io_request_lock); */
4305 HZ
* acb
->eeprom
.delay_time
;
4307 /*spin_lock_irq (&io_request_lock); */
4313 * adapter_init - Grab the resource for the card, setup the adapter
4314 * information, set the card into a known state, create the various
4315 * tables etc etc. This basically gets all adapter information all up
4316 * to date, initialised and gets the chip in sync with it.
4318 * @acb: The adapter which we are to init.
4319 * @io_port: The base I/O port
4320 * @io_port_len: The I/O port size
4323 * Returns 0 if the initialization succeeds, any other value on
4326 static int adapter_init(struct AdapterCtlBlk
*acb
, unsigned long io_port
,
4327 u32 io_port_len
, unsigned int irq
)
4329 if (!request_region(io_port
, io_port_len
, DC395X_NAME
)) {
4330 dprintkl(KERN_ERR
, "Failed to reserve IO region 0x%lx\n", io_port
);
4333 /* store port base to indicate we have registered it */
4334 acb
->io_port_base
= io_port
;
4335 acb
->io_port_len
= io_port_len
;
4337 if (request_irq(irq
, dc395x_interrupt
, IRQF_SHARED
, DC395X_NAME
, acb
)) {
4338 /* release the region we just claimed */
4339 dprintkl(KERN_INFO
, "Failed to register IRQ\n");
4342 /* store irq to indicate we have registered it */
4343 acb
->irq_level
= irq
;
4345 /* get eeprom configuration information and command line settings etc */
4346 check_eeprom(&acb
->eeprom
, io_port
);
4347 print_eeprom_settings(&acb
->eeprom
);
4349 /* setup adapter control block */
4350 adapter_init_params(acb
);
4352 /* display card connectors/termination settings */
4353 adapter_print_config(acb
);
4355 if (adapter_sg_tables_alloc(acb
)) {
4356 dprintkl(KERN_DEBUG
, "Memory allocation for SG tables failed\n");
4359 adapter_init_scsi_host(acb
->scsi_host
);
4360 adapter_init_chip(acb
);
4361 set_basic_config(acb
);
4364 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4365 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4366 acb
, acb
->dcb_map
, acb
->srb_array
, sizeof(struct AdapterCtlBlk
),
4367 sizeof(struct DeviceCtlBlk
), sizeof(struct ScsiReqBlk
));
4372 free_irq(acb
->irq_level
, acb
);
4373 if (acb
->io_port_base
)
4374 release_region(acb
->io_port_base
, acb
->io_port_len
);
4375 adapter_sg_tables_free(acb
);
4382 * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4383 * stopping all operations and disabling interrupt generation on the
4386 * @acb: The adapter which we are to shutdown.
4388 static void adapter_uninit_chip(struct AdapterCtlBlk
*acb
)
4390 /* disable interrupts */
4391 DC395x_write8(acb
, TRM_S1040_DMA_INTEN
, 0);
4392 DC395x_write8(acb
, TRM_S1040_SCSI_INTEN
, 0);
4394 /* reset the scsi bus */
4395 if (acb
->config
& HCC_SCSI_RESET
)
4396 reset_scsi_bus(acb
);
4398 /* clear any pending interrupt state */
4399 DC395x_read8(acb
, TRM_S1040_SCSI_INTSTATUS
);
4405 * adapter_uninit - Shut down the chip and release any resources that
4406 * we had allocated. Once this returns the adapter should not be used
4409 * @acb: The adapter which we are to un-initialize.
4411 static void adapter_uninit(struct AdapterCtlBlk
*acb
)
4413 unsigned long flags
;
4414 DC395x_LOCK_IO(acb
->scsi_host
, flags
);
4417 if (timer_pending(&acb
->waiting_timer
))
4418 del_timer(&acb
->waiting_timer
);
4419 if (timer_pending(&acb
->selto_timer
))
4420 del_timer(&acb
->selto_timer
);
4422 adapter_uninit_chip(acb
);
4423 adapter_remove_and_free_all_devices(acb
);
4424 DC395x_UNLOCK_IO(acb
->scsi_host
, flags
);
4427 free_irq(acb
->irq_level
, acb
);
4428 if (acb
->io_port_base
)
4429 release_region(acb
->io_port_base
, acb
->io_port_len
);
4431 adapter_sg_tables_free(acb
);
4437 if (YN) seq_printf(m, " Yes ");\
4438 else seq_printf(m, " No ")
4440 static int dc395x_show_info(struct seq_file
*m
, struct Scsi_Host
*host
)
4442 struct AdapterCtlBlk
*acb
= (struct AdapterCtlBlk
*)host
->hostdata
;
4444 struct DeviceCtlBlk
*dcb
;
4445 unsigned long flags
;
4448 seq_puts(m
, DC395X_BANNER
" PCI SCSI Host Adapter\n"
4449 " Driver Version " DC395X_VERSION
"\n");
4451 DC395x_LOCK_IO(acb
->scsi_host
, flags
);
4453 seq_printf(m
, "SCSI Host Nr %i, ", host
->host_no
);
4454 seq_printf(m
, "DC395U/UW/F DC315/U %s\n",
4455 (acb
->config
& HCC_WIDE_CARD
) ? "Wide" : "");
4456 seq_printf(m
, "io_port_base 0x%04lx, ", acb
->io_port_base
);
4457 seq_printf(m
, "irq_level 0x%04x, ", acb
->irq_level
);
4458 seq_printf(m
, " SelTimeout %ims\n", (1638 * acb
->sel_timeout
) / 1000);
4460 seq_printf(m
, "MaxID %i, MaxLUN %llu, ", host
->max_id
, host
->max_lun
);
4461 seq_printf(m
, "AdapterID %i\n", host
->this_id
);
4463 seq_printf(m
, "tag_max_num %i", acb
->tag_max_num
);
4464 /*seq_printf(m, ", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4465 seq_printf(m
, ", FilterCfg 0x%02x",
4466 DC395x_read8(acb
, TRM_S1040_SCSI_CONFIG1
));
4467 seq_printf(m
, ", DelayReset %is\n", acb
->eeprom
.delay_time
);
4468 /*seq_printf(m, "\n"); */
4470 seq_printf(m
, "Nr of DCBs: %i\n", list_size(&acb
->dcb_list
));
4471 seq_printf(m
, "Map of attached LUNs: %8ph\n", &acb
->dcb_map
[0]);
4472 seq_printf(m
, " %8ph\n", &acb
->dcb_map
[8]);
4475 "Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4478 list_for_each_entry(dcb
, &acb
->dcb_list
, list
) {
4480 seq_printf(m
, "%02i %02i %02i ", dev
, dcb
->target_id
,
4482 YESNO(dcb
->dev_mode
& NTC_DO_PARITY_CHK
);
4483 YESNO(dcb
->sync_offset
);
4484 YESNO(dcb
->sync_period
& WIDE_SYNC
);
4485 YESNO(dcb
->dev_mode
& NTC_DO_DISCONNECT
);
4486 YESNO(dcb
->dev_mode
& NTC_DO_SEND_START
);
4487 YESNO(dcb
->sync_mode
& EN_TAG_QUEUEING
);
4488 nego_period
= clock_period
[dcb
->sync_period
& 0x07] << 2;
4489 if (dcb
->sync_offset
)
4490 seq_printf(m
, " %03i ns ", nego_period
);
4492 seq_printf(m
, " (%03i ns)", (dcb
->min_nego_period
<< 2));
4494 if (dcb
->sync_offset
& 0x0f) {
4495 spd
= 1000 / (nego_period
);
4496 spd1
= 1000 % (nego_period
);
4497 spd1
= (spd1
* 10 + nego_period
/ 2) / (nego_period
);
4498 seq_printf(m
, " %2i.%1i M %02i ", spd
, spd1
,
4499 (dcb
->sync_offset
& 0x0f));
4503 /* Add more info ... */
4504 seq_printf(m
, " %02i\n", dcb
->max_command
);
4508 if (timer_pending(&acb
->waiting_timer
))
4509 seq_puts(m
, "Waiting queue timer running\n");
4513 list_for_each_entry(dcb
, &acb
->dcb_list
, list
) {
4514 struct ScsiReqBlk
*srb
;
4515 if (!list_empty(&dcb
->srb_waiting_list
))
4516 seq_printf(m
, "DCB (%02i-%i): Waiting: %i:",
4517 dcb
->target_id
, dcb
->target_lun
,
4518 list_size(&dcb
->srb_waiting_list
));
4519 list_for_each_entry(srb
, &dcb
->srb_waiting_list
, list
)
4520 seq_printf(m
, " %p", srb
->cmd
);
4521 if (!list_empty(&dcb
->srb_going_list
))
4522 seq_printf(m
, "\nDCB (%02i-%i): Going : %i:",
4523 dcb
->target_id
, dcb
->target_lun
,
4524 list_size(&dcb
->srb_going_list
));
4525 list_for_each_entry(srb
, &dcb
->srb_going_list
, list
)
4526 seq_printf(m
, " %p", srb
->cmd
);
4527 if (!list_empty(&dcb
->srb_waiting_list
) || !list_empty(&dcb
->srb_going_list
))
4531 if (debug_enabled(DBG_1
)) {
4532 seq_printf(m
, "DCB list for ACB %p:\n", acb
);
4533 list_for_each_entry(dcb
, &acb
->dcb_list
, list
) {
4534 seq_printf(m
, "%p -> ", dcb
);
4536 seq_puts(m
, "END\n");
4539 DC395x_UNLOCK_IO(acb
->scsi_host
, flags
);
4544 static const struct scsi_host_template dc395x_driver_template
= {
4545 .module
= THIS_MODULE
,
4546 .proc_name
= DC395X_NAME
,
4547 .show_info
= dc395x_show_info
,
4548 .name
= DC395X_BANNER
" " DC395X_VERSION
,
4549 .queuecommand
= dc395x_queue_command
,
4550 .slave_alloc
= dc395x_slave_alloc
,
4551 .slave_destroy
= dc395x_slave_destroy
,
4552 .can_queue
= DC395x_MAX_CAN_QUEUE
,
4554 .sg_tablesize
= DC395x_MAX_SG_TABLESIZE
,
4555 .cmd_per_lun
= DC395x_MAX_CMD_PER_LUN
,
4556 .eh_abort_handler
= dc395x_eh_abort
,
4557 .eh_bus_reset_handler
= dc395x_eh_bus_reset
,
4558 .dma_boundary
= PAGE_SIZE
- 1,
4563 * banner_display - Display banner on first instance of driver
4566 static void banner_display(void)
4568 static int banner_done
= 0;
4571 dprintkl(KERN_INFO
, "%s %s\n", DC395X_BANNER
, DC395X_VERSION
);
4578 * dc395x_init_one - Initialise a single instance of the adapter.
4580 * The PCI layer will call this once for each instance of the adapter
4581 * that it finds in the system. The pci_dev strcuture indicates which
4582 * instance we are being called from.
4584 * @dev: The PCI device to initialize.
4585 * @id: Looks like a pointer to the entry in our pci device table
4586 * that was actually matched by the PCI subsystem.
4588 * Returns 0 on success, or an error code (-ve) on failure.
4590 static int dc395x_init_one(struct pci_dev
*dev
, const struct pci_device_id
*id
)
4592 struct Scsi_Host
*scsi_host
= NULL
;
4593 struct AdapterCtlBlk
*acb
= NULL
;
4594 unsigned long io_port_base
;
4595 unsigned int io_port_len
;
4598 dprintkdbg(DBG_0
, "Init one instance (%s)\n", pci_name(dev
));
4601 if (pci_enable_device(dev
))
4603 dprintkl(KERN_INFO
, "PCI Enable device failed.\n");
4606 io_port_base
= pci_resource_start(dev
, 0) & PCI_BASE_ADDRESS_IO_MASK
;
4607 io_port_len
= pci_resource_len(dev
, 0);
4609 dprintkdbg(DBG_0
, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base
, dev
->irq
);
4611 /* allocate scsi host information (includes out adapter) */
4612 scsi_host
= scsi_host_alloc(&dc395x_driver_template
,
4613 sizeof(struct AdapterCtlBlk
));
4615 dprintkl(KERN_INFO
, "scsi_host_alloc failed\n");
4618 acb
= (struct AdapterCtlBlk
*)scsi_host
->hostdata
;
4619 acb
->scsi_host
= scsi_host
;
4622 /* initialise the adapter and everything we need */
4623 if (adapter_init(acb
, io_port_base
, io_port_len
, irq
)) {
4624 dprintkl(KERN_INFO
, "adapter init failed\n");
4629 pci_set_master(dev
);
4631 /* get the scsi mid level to scan for new devices on the bus */
4632 if (scsi_add_host(scsi_host
, &dev
->dev
)) {
4633 dprintkl(KERN_ERR
, "scsi_add_host failed\n");
4636 pci_set_drvdata(dev
, scsi_host
);
4637 scsi_scan_host(scsi_host
);
4643 adapter_uninit(acb
);
4644 if (scsi_host
!= NULL
)
4645 scsi_host_put(scsi_host
);
4646 pci_disable_device(dev
);
4652 * dc395x_remove_one - Called to remove a single instance of the
4655 * @dev: The PCI device to initialize.
4657 static void dc395x_remove_one(struct pci_dev
*dev
)
4659 struct Scsi_Host
*scsi_host
= pci_get_drvdata(dev
);
4660 struct AdapterCtlBlk
*acb
= (struct AdapterCtlBlk
*)(scsi_host
->hostdata
);
4662 dprintkdbg(DBG_0
, "dc395x_remove_one: acb=%p\n", acb
);
4664 scsi_remove_host(scsi_host
);
4665 adapter_uninit(acb
);
4666 pci_disable_device(dev
);
4667 scsi_host_put(scsi_host
);
4671 static struct pci_device_id dc395x_pci_table
[] = {
4673 .vendor
= PCI_VENDOR_ID_TEKRAM
,
4674 .device
= PCI_DEVICE_ID_TEKRAM_TRMS1040
,
4675 .subvendor
= PCI_ANY_ID
,
4676 .subdevice
= PCI_ANY_ID
,
4678 {} /* Terminating entry */
4680 MODULE_DEVICE_TABLE(pci
, dc395x_pci_table
);
4683 static struct pci_driver dc395x_driver
= {
4684 .name
= DC395X_NAME
,
4685 .id_table
= dc395x_pci_table
,
4686 .probe
= dc395x_init_one
,
4687 .remove
= dc395x_remove_one
,
4689 module_pci_driver(dc395x_driver
);
4691 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4692 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4693 MODULE_LICENSE("GPL");