[PATCH] Move read_mostly definition to asm/cache.h
[linux-2.6/mini2440.git] / drivers / scsi / dc395x.c
blobcbf825263f3b4a89512cf3ea18afc7aa74f92571
1 /*
2 * dc395x.c
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)
8 * Authors:
9 * C.L. Huang <ching@tekram.com.tw>
10 * Erich Chen <erich@tekram.com.tw>
11 * (C) Copyright 1995-1999 Tekram Technology Co., Ltd.
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>
19 * (C) 2003
21 * License: GNU GPL
23 *************************************************************************
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
27 * are met:
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. The name of the author may not be used to endorse or promote products
34 * derived from this software without specific prior written permission.
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 <asm/io.h>
62 #include <scsi/scsi.h>
63 #include <scsi/scsicam.h> /* needed for scsicam_bios_param */
64 #include <scsi/scsi_cmnd.h>
65 #include <scsi/scsi_device.h>
66 #include <scsi/scsi_host.h>
68 #include "dc395x.h"
70 #define DC395X_NAME "dc395x"
71 #define DC395X_BANNER "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
72 #define DC395X_VERSION "v2.05, 2004/03/08"
74 /*---------------------------------------------------------------------------
75 Features
76 ---------------------------------------------------------------------------*/
78 * Set to disable parts of the driver
80 /*#define DC395x_NO_DISCONNECT*/
81 /*#define DC395x_NO_TAGQ*/
82 /*#define DC395x_NO_SYNC*/
83 /*#define DC395x_NO_WIDE*/
85 /*---------------------------------------------------------------------------
86 Debugging
87 ---------------------------------------------------------------------------*/
89 * Types of debugging that can be enabled and disabled
91 #define DBG_KG 0x0001
92 #define DBG_0 0x0002
93 #define DBG_1 0x0004
94 #define DBG_SG 0x0020
95 #define DBG_FIFO 0x0040
96 #define DBG_PIO 0x0080
100 * Set set of things to output debugging for.
101 * Undefine to remove all debugging
103 /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
104 /*#define DEBUG_MASK DBG_0*/
108 * Output a kernel mesage at the specified level and append the
109 * driver name and a ": " to the start of the message
111 #define dprintkl(level, format, arg...) \
112 printk(level DC395X_NAME ": " format , ## arg)
115 #ifdef DEBUG_MASK
117 * print a debug message - this is formated with KERN_DEBUG, then the
118 * driver name followed by a ": " and then the message is output.
119 * This also checks that the specified debug level is enabled before
120 * outputing the message
122 #define dprintkdbg(type, format, arg...) \
123 do { \
124 if ((type) & (DEBUG_MASK)) \
125 dprintkl(KERN_DEBUG , format , ## arg); \
126 } while (0)
129 * Check if the specified type of debugging is enabled
131 #define debug_enabled(type) ((DEBUG_MASK) & (type))
133 #else
135 * No debugging. Do nothing
137 #define dprintkdbg(type, format, arg...) \
138 do {} while (0)
139 #define debug_enabled(type) (0)
141 #endif
144 #ifndef PCI_VENDOR_ID_TEKRAM
145 #define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */
146 #endif
147 #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
148 #define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */
149 #endif
152 #define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
153 #define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
155 #define DC395x_read8(acb,address) (u8)(inb(acb->io_port_base + (address)))
156 #define DC395x_read16(acb,address) (u16)(inw(acb->io_port_base + (address)))
157 #define DC395x_read32(acb,address) (u32)(inl(acb->io_port_base + (address)))
158 #define DC395x_write8(acb,address,value) outb((value), acb->io_port_base + (address))
159 #define DC395x_write16(acb,address,value) outw((value), acb->io_port_base + (address))
160 #define DC395x_write32(acb,address,value) outl((value), acb->io_port_base + (address))
162 /* cmd->result */
163 #define RES_TARGET 0x000000FF /* Target State */
164 #define RES_TARGET_LNX STATUS_MASK /* Only official ... */
165 #define RES_ENDMSG 0x0000FF00 /* End Message */
166 #define RES_DID 0x00FF0000 /* DID_ codes */
167 #define RES_DRV 0xFF000000 /* DRIVER_ codes */
169 #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
170 #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
172 #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
173 #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
174 #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
175 #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
176 #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
178 #define TAG_NONE 255
181 * srb->segement_x is the hw sg list. It is always allocated as a
182 * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
183 * cross a page boundy.
185 #define SEGMENTX_LEN (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
188 struct SGentry {
189 u32 address; /* bus! address */
190 u32 length;
193 /* The SEEPROM structure for TRM_S1040 */
194 struct NVRamTarget {
195 u8 cfg0; /* Target configuration byte 0 */
196 u8 period; /* Target period */
197 u8 cfg2; /* Target configuration byte 2 */
198 u8 cfg3; /* Target configuration byte 3 */
201 struct NvRamType {
202 u8 sub_vendor_id[2]; /* 0,1 Sub Vendor ID */
203 u8 sub_sys_id[2]; /* 2,3 Sub System ID */
204 u8 sub_class; /* 4 Sub Class */
205 u8 vendor_id[2]; /* 5,6 Vendor ID */
206 u8 device_id[2]; /* 7,8 Device ID */
207 u8 reserved; /* 9 Reserved */
208 struct NVRamTarget target[DC395x_MAX_SCSI_ID];
209 /** 10,11,12,13
210 ** 14,15,16,17
211 ** ....
212 ** ....
213 ** 70,71,72,73
215 u8 scsi_id; /* 74 Host Adapter SCSI ID */
216 u8 channel_cfg; /* 75 Channel configuration */
217 u8 delay_time; /* 76 Power on delay time */
218 u8 max_tag; /* 77 Maximum tags */
219 u8 reserved0; /* 78 */
220 u8 boot_target; /* 79 */
221 u8 boot_lun; /* 80 */
222 u8 reserved1; /* 81 */
223 u16 reserved2[22]; /* 82,..125 */
224 u16 cksum; /* 126,127 */
227 struct ScsiReqBlk {
228 struct list_head list; /* next/prev ptrs for srb lists */
229 struct DeviceCtlBlk *dcb;
230 struct scsi_cmnd *cmd;
232 struct SGentry *segment_x; /* Linear array of hw sg entries (up to 64 entries) */
233 u32 sg_bus_addr; /* Bus address of sg list (ie, of segment_x) */
235 u8 sg_count; /* No of HW sg entries for this request */
236 u8 sg_index; /* Index of HW sg entry for this request */
237 u32 total_xfer_length; /* Total number of bytes remaining to be transfered */
238 unsigned char *virt_addr; /* Virtual address of current transfer position */
241 * The sense buffer handling function, request_sense, uses
242 * the first hw sg entry (segment_x[0]) and the transfer
243 * length (total_xfer_length). While doing this it stores the
244 * original values into the last sg hw list
245 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
246 * total_xfer_length in xferred. These values are restored in
247 * pci_unmap_srb_sense. This is the only place xferred is used.
249 unsigned char *virt_addr_req; /* Saved virtual address of the request buffer */
250 u32 xferred; /* Saved copy of total_xfer_length */
252 u16 state;
254 u8 msgin_buf[6];
255 u8 msgout_buf[6];
257 u8 adapter_status;
258 u8 target_status;
259 u8 msg_count;
260 u8 end_message;
262 u8 tag_number;
263 u8 status;
264 u8 retry_count;
265 u8 flag;
267 u8 scsi_phase;
270 struct DeviceCtlBlk {
271 struct list_head list; /* next/prev ptrs for the dcb list */
272 struct AdapterCtlBlk *acb;
273 struct list_head srb_going_list; /* head of going srb list */
274 struct list_head srb_waiting_list; /* head of waiting srb list */
276 struct ScsiReqBlk *active_srb;
277 u32 tag_mask;
279 u16 max_command;
281 u8 target_id; /* SCSI Target ID (SCSI Only) */
282 u8 target_lun; /* SCSI Log. Unit (SCSI Only) */
283 u8 identify_msg;
284 u8 dev_mode;
286 u8 inquiry7; /* To store Inquiry flags */
287 u8 sync_mode; /* 0:async mode */
288 u8 min_nego_period; /* for nego. */
289 u8 sync_period; /* for reg. */
291 u8 sync_offset; /* for reg. and nego.(low nibble) */
292 u8 flag;
293 u8 dev_type;
294 u8 init_tcq_flag;
297 struct AdapterCtlBlk {
298 struct Scsi_Host *scsi_host;
300 unsigned long io_port_base;
301 unsigned long io_port_len;
303 struct list_head dcb_list; /* head of going dcb list */
304 struct DeviceCtlBlk *dcb_run_robin;
305 struct DeviceCtlBlk *active_dcb;
307 struct list_head srb_free_list; /* head of free srb list */
308 struct ScsiReqBlk *tmp_srb;
309 struct timer_list waiting_timer;
310 struct timer_list selto_timer;
312 u16 srb_count;
314 u8 sel_timeout;
316 unsigned int irq_level;
317 u8 tag_max_num;
318 u8 acb_flag;
319 u8 gmode2;
321 u8 config;
322 u8 lun_chk;
323 u8 scan_devices;
324 u8 hostid_bit;
326 u8 dcb_map[DC395x_MAX_SCSI_ID];
327 struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
329 struct pci_dev *dev;
331 u8 msg_len;
333 struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
334 struct ScsiReqBlk srb;
336 struct NvRamType eeprom; /* eeprom settings for this adapter */
340 /*---------------------------------------------------------------------------
341 Forward declarations
342 ---------------------------------------------------------------------------*/
343 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
344 u16 *pscsi_status);
345 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
346 u16 *pscsi_status);
347 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
348 u16 *pscsi_status);
349 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
350 u16 *pscsi_status);
351 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
352 u16 *pscsi_status);
353 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
354 u16 *pscsi_status);
355 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
356 u16 *pscsi_status);
357 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
358 u16 *pscsi_status);
359 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
360 u16 *pscsi_status);
361 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
362 u16 *pscsi_status);
363 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
364 u16 *pscsi_status);
365 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
366 u16 *pscsi_status);
367 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
368 u16 *pscsi_status);
369 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
370 u16 *pscsi_status);
371 static void set_basic_config(struct AdapterCtlBlk *acb);
372 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
373 struct ScsiReqBlk *srb);
374 static void reset_scsi_bus(struct AdapterCtlBlk *acb);
375 static void data_io_transfer(struct AdapterCtlBlk *acb,
376 struct ScsiReqBlk *srb, u16 io_dir);
377 static void disconnect(struct AdapterCtlBlk *acb);
378 static void reselect(struct AdapterCtlBlk *acb);
379 static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
380 struct ScsiReqBlk *srb);
381 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
382 struct ScsiReqBlk *srb);
383 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
384 struct ScsiReqBlk *srb);
385 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
386 struct scsi_cmnd *cmd, u8 force);
387 static void scsi_reset_detect(struct AdapterCtlBlk *acb);
388 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
389 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
390 struct ScsiReqBlk *srb);
391 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
392 struct ScsiReqBlk *srb);
393 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
394 struct ScsiReqBlk *srb);
395 static void set_xfer_rate(struct AdapterCtlBlk *acb,
396 struct DeviceCtlBlk *dcb);
397 static void waiting_timeout(unsigned long ptr);
400 /*---------------------------------------------------------------------------
401 Static Data
402 ---------------------------------------------------------------------------*/
403 static u16 current_sync_offset = 0;
405 static void *dc395x_scsi_phase0[] = {
406 data_out_phase0,/* phase:0 */
407 data_in_phase0, /* phase:1 */
408 command_phase0, /* phase:2 */
409 status_phase0, /* phase:3 */
410 nop0, /* phase:4 PH_BUS_FREE .. initial phase */
411 nop0, /* phase:5 PH_BUS_FREE .. initial phase */
412 msgout_phase0, /* phase:6 */
413 msgin_phase0, /* phase:7 */
416 static void *dc395x_scsi_phase1[] = {
417 data_out_phase1,/* phase:0 */
418 data_in_phase1, /* phase:1 */
419 command_phase1, /* phase:2 */
420 status_phase1, /* phase:3 */
421 nop1, /* phase:4 PH_BUS_FREE .. initial phase */
422 nop1, /* phase:5 PH_BUS_FREE .. initial phase */
423 msgout_phase1, /* phase:6 */
424 msgin_phase1, /* phase:7 */
428 *Fast20: 000 50ns, 20.0 MHz
429 * 001 75ns, 13.3 MHz
430 * 010 100ns, 10.0 MHz
431 * 011 125ns, 8.0 MHz
432 * 100 150ns, 6.6 MHz
433 * 101 175ns, 5.7 MHz
434 * 110 200ns, 5.0 MHz
435 * 111 250ns, 4.0 MHz
437 *Fast40(LVDS): 000 25ns, 40.0 MHz
438 * 001 50ns, 20.0 MHz
439 * 010 75ns, 13.3 MHz
440 * 011 100ns, 10.0 MHz
441 * 100 125ns, 8.0 MHz
442 * 101 150ns, 6.6 MHz
443 * 110 175ns, 5.7 MHz
444 * 111 200ns, 5.0 MHz
446 /*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/
448 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
449 static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
450 static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
453 /*---------------------------------------------------------------------------
454 Configuration
455 ---------------------------------------------------------------------------*/
457 * Module/boot parameters currently effect *all* instances of the
458 * card in the system.
462 * Command line parameters are stored in a structure below.
463 * These are the index's into the structure for the various
464 * command line options.
466 #define CFG_ADAPTER_ID 0
467 #define CFG_MAX_SPEED 1
468 #define CFG_DEV_MODE 2
469 #define CFG_ADAPTER_MODE 3
470 #define CFG_TAGS 4
471 #define CFG_RESET_DELAY 5
473 #define CFG_NUM 6 /* number of configuration items */
477 * Value used to indicate that a command line override
478 * hasn't been used to modify the value.
480 #define CFG_PARAM_UNSET -1
484 * Hold command line parameters.
486 struct ParameterData {
487 int value; /* value of this setting */
488 int min; /* minimum value */
489 int max; /* maximum value */
490 int def; /* default value */
491 int safe; /* safe value */
493 static struct ParameterData __devinitdata cfg_data[] = {
494 { /* adapter id */
495 CFG_PARAM_UNSET,
501 { /* max speed */
502 CFG_PARAM_UNSET,
505 1, /* 13.3Mhz */
506 4, /* 6.7Hmz */
508 { /* dev mode */
509 CFG_PARAM_UNSET,
511 0x3f,
512 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
513 NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
514 NTC_DO_SEND_START,
515 NTC_DO_PARITY_CHK | NTC_DO_SEND_START
517 { /* adapter mode */
518 CFG_PARAM_UNSET,
520 0x2f,
521 #ifdef CONFIG_SCSI_MULTI_LUN
522 NAC_SCANLUN |
523 #endif
524 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
525 /*| NAC_ACTIVE_NEG*/,
526 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
528 { /* tags */
529 CFG_PARAM_UNSET,
532 3, /* 16 tags (??) */
535 { /* reset delay */
536 CFG_PARAM_UNSET,
538 180,
539 1, /* 1 second */
540 10, /* 10 seconds */
546 * Safe settings. If set to zero the the BIOS/default values with
547 * command line overrides will be used. If set to 1 then safe and
548 * slow settings will be used.
550 static int use_safe_settings = 0;
551 module_param_named(safe, use_safe_settings, bool, 0);
552 MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
555 module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
556 MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
558 module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
559 MODULE_PARM_DESC(max_speed, "Maximum bus speed. Default 1 (0-7) Speeds: 0=20, 1=13.3, 2=10, 3=8, 4=6.7, 5=5.8, 6=5, 7=4 Mhz");
561 module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
562 MODULE_PARM_DESC(dev_mode, "Device mode.");
564 module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
565 MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
567 module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
568 MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
570 module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
571 MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
575 * set_safe_settings - if the use_safe_settings option is set then
576 * set all values to the safe and slow values.
578 static void __devinit set_safe_settings(void)
580 if (use_safe_settings)
582 int i;
584 dprintkl(KERN_INFO, "Using safe settings.\n");
585 for (i = 0; i < CFG_NUM; i++)
587 cfg_data[i].value = cfg_data[i].safe;
594 * fix_settings - reset any boot parameters which are out of range
595 * back to the default values.
597 static void __devinit fix_settings(void)
599 int i;
601 dprintkdbg(DBG_1,
602 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
603 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
604 cfg_data[CFG_ADAPTER_ID].value,
605 cfg_data[CFG_MAX_SPEED].value,
606 cfg_data[CFG_DEV_MODE].value,
607 cfg_data[CFG_ADAPTER_MODE].value,
608 cfg_data[CFG_TAGS].value,
609 cfg_data[CFG_RESET_DELAY].value);
610 for (i = 0; i < CFG_NUM; i++)
612 if (cfg_data[i].value < cfg_data[i].min
613 || cfg_data[i].value > cfg_data[i].max)
614 cfg_data[i].value = cfg_data[i].def;
621 * Mapping from the eeprom delay index value (index into this array)
622 * to the the number of actual seconds that the delay should be for.
624 static char __devinitdata eeprom_index_to_delay_map[] =
625 { 1, 3, 5, 10, 16, 30, 60, 120 };
629 * eeprom_index_to_delay - Take the eeprom delay setting and convert it
630 * into a number of seconds.
632 * @eeprom: The eeprom structure in which we find the delay index to map.
634 static void __devinit eeprom_index_to_delay(struct NvRamType *eeprom)
636 eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
641 * delay_to_eeprom_index - Take a delay in seconds and return the
642 * closest eeprom index which will delay for at least that amount of
643 * seconds.
645 * @delay: The delay, in seconds, to find the eeprom index for.
647 static int __devinit delay_to_eeprom_index(int delay)
649 u8 idx = 0;
650 while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
651 idx++;
652 return idx;
657 * eeprom_override - Override the eeprom settings, in the provided
658 * eeprom structure, with values that have been set on the command
659 * line.
661 * @eeprom: The eeprom data to override with command line options.
663 static void __devinit eeprom_override(struct NvRamType *eeprom)
665 u8 id;
667 /* Adapter Settings */
668 if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
669 eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
671 if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
672 eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
674 if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
675 eeprom->delay_time = delay_to_eeprom_index(
676 cfg_data[CFG_RESET_DELAY].value);
678 if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
679 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
681 /* Device Settings */
682 for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
683 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
684 eeprom->target[id].cfg0 =
685 (u8)cfg_data[CFG_DEV_MODE].value;
687 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
688 eeprom->target[id].period =
689 (u8)cfg_data[CFG_MAX_SPEED].value;
695 /*---------------------------------------------------------------------------
696 ---------------------------------------------------------------------------*/
698 static unsigned int list_size(struct list_head *head)
700 unsigned int count = 0;
701 struct list_head *pos;
702 list_for_each(pos, head)
703 count++;
704 return count;
708 static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
709 struct DeviceCtlBlk *pos)
711 int use_next = 0;
712 struct DeviceCtlBlk* next = NULL;
713 struct DeviceCtlBlk* i;
715 if (list_empty(head))
716 return NULL;
718 /* find supplied dcb and then select the next one */
719 list_for_each_entry(i, head, list)
720 if (use_next) {
721 next = i;
722 break;
723 } else if (i == pos) {
724 use_next = 1;
726 /* if no next one take the head one (ie, wraparound) */
727 if (!next)
728 list_for_each_entry(i, head, list) {
729 next = i;
730 break;
733 return next;
737 static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
739 if (srb->tag_number < 255) {
740 dcb->tag_mask &= ~(1 << srb->tag_number); /* free tag mask */
741 srb->tag_number = 255;
746 /* Find cmd in SRB list */
747 static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
748 struct list_head *head)
750 struct ScsiReqBlk *i;
751 list_for_each_entry(i, head, list)
752 if (i->cmd == cmd)
753 return i;
754 return NULL;
758 static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb)
760 struct list_head *head = &acb->srb_free_list;
761 struct ScsiReqBlk *srb = NULL;
763 if (!list_empty(head)) {
764 srb = list_entry(head->next, struct ScsiReqBlk, list);
765 list_del(head->next);
766 dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb);
768 return srb;
772 static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
774 dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb);
775 list_add_tail(&srb->list, &acb->srb_free_list);
779 static void srb_waiting_insert(struct DeviceCtlBlk *dcb,
780 struct ScsiReqBlk *srb)
782 dprintkdbg(DBG_0, "srb_waiting_insert: (pid#%li) <%02i-%i> srb=%p\n",
783 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
784 list_add(&srb->list, &dcb->srb_waiting_list);
788 static void srb_waiting_append(struct DeviceCtlBlk *dcb,
789 struct ScsiReqBlk *srb)
791 dprintkdbg(DBG_0, "srb_waiting_append: (pid#%li) <%02i-%i> srb=%p\n",
792 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
793 list_add_tail(&srb->list, &dcb->srb_waiting_list);
797 static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
799 dprintkdbg(DBG_0, "srb_going_append: (pid#%li) <%02i-%i> srb=%p\n",
800 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
801 list_add_tail(&srb->list, &dcb->srb_going_list);
805 static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
807 struct ScsiReqBlk *i;
808 struct ScsiReqBlk *tmp;
809 dprintkdbg(DBG_0, "srb_going_remove: (pid#%li) <%02i-%i> srb=%p\n",
810 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
812 list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list)
813 if (i == srb) {
814 list_del(&srb->list);
815 break;
820 static void srb_waiting_remove(struct DeviceCtlBlk *dcb,
821 struct ScsiReqBlk *srb)
823 struct ScsiReqBlk *i;
824 struct ScsiReqBlk *tmp;
825 dprintkdbg(DBG_0, "srb_waiting_remove: (pid#%li) <%02i-%i> srb=%p\n",
826 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
828 list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list)
829 if (i == srb) {
830 list_del(&srb->list);
831 break;
836 static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb,
837 struct ScsiReqBlk *srb)
839 dprintkdbg(DBG_0,
840 "srb_going_to_waiting_move: (pid#%li) <%02i-%i> srb=%p\n",
841 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
842 list_move(&srb->list, &dcb->srb_waiting_list);
846 static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb,
847 struct ScsiReqBlk *srb)
849 dprintkdbg(DBG_0,
850 "srb_waiting_to_going_move: (pid#%li) <%02i-%i> srb=%p\n",
851 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
852 list_move(&srb->list, &dcb->srb_going_list);
856 /* Sets the timer to wake us up */
857 static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
859 if (timer_pending(&acb->waiting_timer))
860 return;
861 init_timer(&acb->waiting_timer);
862 acb->waiting_timer.function = waiting_timeout;
863 acb->waiting_timer.data = (unsigned long) acb;
864 if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
865 acb->waiting_timer.expires =
866 acb->scsi_host->last_reset - HZ / 2 + 1;
867 else
868 acb->waiting_timer.expires = jiffies + to + 1;
869 add_timer(&acb->waiting_timer);
873 /* Send the next command from the waiting list to the bus */
874 static void waiting_process_next(struct AdapterCtlBlk *acb)
876 struct DeviceCtlBlk *start = NULL;
877 struct DeviceCtlBlk *pos;
878 struct DeviceCtlBlk *dcb;
879 struct ScsiReqBlk *srb;
880 struct list_head *dcb_list_head = &acb->dcb_list;
882 if (acb->active_dcb
883 || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
884 return;
886 if (timer_pending(&acb->waiting_timer))
887 del_timer(&acb->waiting_timer);
889 if (list_empty(dcb_list_head))
890 return;
893 * Find the starting dcb. Need to find it again in the list
894 * since the list may have changed since we set the ptr to it
896 list_for_each_entry(dcb, dcb_list_head, list)
897 if (dcb == acb->dcb_run_robin) {
898 start = dcb;
899 break;
901 if (!start) {
902 /* This can happen! */
903 start = list_entry(dcb_list_head->next, typeof(*start), list);
904 acb->dcb_run_robin = start;
909 * Loop over the dcb, but we start somewhere (potentially) in
910 * the middle of the loop so we need to manully do this.
912 pos = start;
913 do {
914 struct list_head *waiting_list_head = &pos->srb_waiting_list;
916 /* Make sure, the next another device gets scheduled ... */
917 acb->dcb_run_robin = dcb_get_next(dcb_list_head,
918 acb->dcb_run_robin);
920 if (list_empty(waiting_list_head) ||
921 pos->max_command <= list_size(&pos->srb_going_list)) {
922 /* move to next dcb */
923 pos = dcb_get_next(dcb_list_head, pos);
924 } else {
925 srb = list_entry(waiting_list_head->next,
926 struct ScsiReqBlk, list);
928 /* Try to send to the bus */
929 if (!start_scsi(acb, pos, srb))
930 srb_waiting_to_going_move(pos, srb);
931 else
932 waiting_set_timer(acb, HZ/50);
933 break;
935 } while (pos != start);
939 /* Wake up waiting queue */
940 static void waiting_timeout(unsigned long ptr)
942 unsigned long flags;
943 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
944 dprintkdbg(DBG_1,
945 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
946 DC395x_LOCK_IO(acb->scsi_host, flags);
947 waiting_process_next(acb);
948 DC395x_UNLOCK_IO(acb->scsi_host, flags);
952 /* Get the DCB for a given ID/LUN combination */
953 static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
955 return acb->children[id][lun];
959 /* Send SCSI Request Block (srb) to adapter (acb) */
960 static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
962 struct DeviceCtlBlk *dcb = srb->dcb;
964 if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
965 acb->active_dcb ||
966 (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
967 srb_waiting_append(dcb, srb);
968 waiting_process_next(acb);
969 return;
972 if (!start_scsi(acb, dcb, srb))
973 srb_going_append(dcb, srb);
974 else {
975 srb_waiting_insert(dcb, srb);
976 waiting_set_timer(acb, HZ / 50);
980 static inline void pio_trigger(void)
982 static int feedback_requested;
984 if (!feedback_requested) {
985 feedback_requested = 1;
986 printk(KERN_WARNING "%s: Please, contact <linux-scsi@vger.kernel.org> "
987 "to help improve support for your system.\n", __FILE__);
991 /* Prepare SRB for being sent to Device DCB w/ command *cmd */
992 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
993 struct ScsiReqBlk *srb)
995 enum dma_data_direction dir = cmd->sc_data_direction;
996 dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n",
997 cmd->pid, dcb->target_id, dcb->target_lun);
999 srb->dcb = dcb;
1000 srb->cmd = cmd;
1001 srb->sg_count = 0;
1002 srb->total_xfer_length = 0;
1003 srb->sg_bus_addr = 0;
1004 srb->virt_addr = NULL;
1005 srb->sg_index = 0;
1006 srb->adapter_status = 0;
1007 srb->target_status = 0;
1008 srb->msg_count = 0;
1009 srb->status = 0;
1010 srb->flag = 0;
1011 srb->state = 0;
1012 srb->retry_count = 0;
1013 srb->tag_number = TAG_NONE;
1014 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1015 srb->end_message = 0;
1017 if (dir == PCI_DMA_NONE || !cmd->request_buffer) {
1018 dprintkdbg(DBG_0,
1019 "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
1020 cmd->bufflen, cmd->request_buffer,
1021 cmd->use_sg, srb->segment_x[0].address);
1022 } else if (cmd->use_sg) {
1023 int i;
1024 u32 reqlen = cmd->request_bufflen;
1025 struct scatterlist *sl = (struct scatterlist *)
1026 cmd->request_buffer;
1027 struct SGentry *sgp = srb->segment_x;
1028 srb->sg_count = pci_map_sg(dcb->acb->dev, sl, cmd->use_sg,
1029 dir);
1030 dprintkdbg(DBG_0,
1031 "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
1032 reqlen, cmd->request_buffer, cmd->use_sg,
1033 srb->sg_count);
1035 srb->virt_addr = page_address(sl->page);
1036 for (i = 0; i < srb->sg_count; i++) {
1037 u32 busaddr = (u32)sg_dma_address(&sl[i]);
1038 u32 seglen = (u32)sl[i].length;
1039 sgp[i].address = busaddr;
1040 sgp[i].length = seglen;
1041 srb->total_xfer_length += seglen;
1043 sgp += srb->sg_count - 1;
1046 * adjust last page if too big as it is allocated
1047 * on even page boundaries
1049 if (srb->total_xfer_length > reqlen) {
1050 sgp->length -= (srb->total_xfer_length - reqlen);
1051 srb->total_xfer_length = reqlen;
1054 /* Fixup for WIDE padding - make sure length is even */
1055 if (dcb->sync_period & WIDE_SYNC &&
1056 srb->total_xfer_length % 2) {
1057 srb->total_xfer_length++;
1058 sgp->length++;
1061 srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
1062 srb->segment_x,
1063 SEGMENTX_LEN,
1064 PCI_DMA_TODEVICE);
1066 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
1067 srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
1068 } else {
1069 srb->total_xfer_length = cmd->request_bufflen;
1070 srb->sg_count = 1;
1071 srb->segment_x[0].address =
1072 pci_map_single(dcb->acb->dev, cmd->request_buffer,
1073 srb->total_xfer_length, dir);
1075 /* Fixup for WIDE padding - make sure length is even */
1076 if (dcb->sync_period & WIDE_SYNC && srb->total_xfer_length % 2)
1077 srb->total_xfer_length++;
1079 srb->segment_x[0].length = srb->total_xfer_length;
1080 srb->virt_addr = cmd->request_buffer;
1081 dprintkdbg(DBG_0,
1082 "build_srb: [1] len=%d buf=%p use_sg=%d map=%08x\n",
1083 srb->total_xfer_length, cmd->request_buffer,
1084 cmd->use_sg, srb->segment_x[0].address);
1090 * dc395x_queue_command - queue scsi command passed from the mid
1091 * layer, invoke 'done' on completion
1093 * @cmd: pointer to scsi command object
1094 * @done: function pointer to be invoked on completion
1096 * Returns 1 if the adapter (host) is busy, else returns 0. One
1097 * reason for an adapter to be busy is that the number
1098 * of outstanding queued commands is already equal to
1099 * struct Scsi_Host::can_queue .
1101 * Required: if struct Scsi_Host::can_queue is ever non-zero
1102 * then this function is required.
1104 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1105 * and is expected to be held on return.
1108 static int dc395x_queue_command(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1110 struct DeviceCtlBlk *dcb;
1111 struct ScsiReqBlk *srb;
1112 struct AdapterCtlBlk *acb =
1113 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1114 dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n",
1115 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
1117 /* Assume BAD_TARGET; will be cleared later */
1118 cmd->result = DID_BAD_TARGET << 16;
1120 /* ignore invalid targets */
1121 if (cmd->device->id >= acb->scsi_host->max_id ||
1122 cmd->device->lun >= acb->scsi_host->max_lun ||
1123 cmd->device->lun >31) {
1124 goto complete;
1127 /* does the specified lun on the specified device exist */
1128 if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
1129 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
1130 cmd->device->id, cmd->device->lun);
1131 goto complete;
1134 /* do we have a DCB for the device */
1135 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1136 if (!dcb) {
1137 /* should never happen */
1138 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
1139 cmd->device->id, cmd->device->lun);
1140 goto complete;
1143 /* set callback and clear result in the command */
1144 cmd->scsi_done = done;
1145 cmd->result = 0;
1147 srb = srb_get_free(acb);
1148 if (!srb)
1151 * Return 1 since we are unable to queue this command at this
1152 * point in time.
1154 dprintkdbg(DBG_0, "queue_command: No free srb's\n");
1155 return 1;
1158 build_srb(cmd, dcb, srb);
1160 if (!list_empty(&dcb->srb_waiting_list)) {
1161 /* append to waiting queue */
1162 srb_waiting_append(dcb, srb);
1163 waiting_process_next(acb);
1164 } else {
1165 /* process immediately */
1166 send_srb(acb, srb);
1168 dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->pid);
1169 return 0;
1171 complete:
1173 * Complete the command immediatey, and then return 0 to
1174 * indicate that we have handled the command. This is usually
1175 * done when the commad is for things like non existent
1176 * devices.
1178 done(cmd);
1179 return 0;
1184 * Return the disk geometry for the given SCSI device.
1186 static int dc395x_bios_param(struct scsi_device *sdev,
1187 struct block_device *bdev, sector_t capacity, int *info)
1189 #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
1190 int heads, sectors, cylinders;
1191 struct AdapterCtlBlk *acb;
1192 int size = capacity;
1194 dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
1195 acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
1196 heads = 64;
1197 sectors = 32;
1198 cylinders = size / (heads * sectors);
1200 if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
1201 heads = 255;
1202 sectors = 63;
1203 cylinders = size / (heads * sectors);
1205 geom[0] = heads;
1206 geom[1] = sectors;
1207 geom[2] = cylinders;
1208 return 0;
1209 #else
1210 return scsicam_bios_param(bdev, capacity, info);
1211 #endif
1215 static void dump_register_info(struct AdapterCtlBlk *acb,
1216 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
1218 u16 pstat;
1219 struct pci_dev *dev = acb->dev;
1220 pci_read_config_word(dev, PCI_STATUS, &pstat);
1221 if (!dcb)
1222 dcb = acb->active_dcb;
1223 if (!srb && dcb)
1224 srb = dcb->active_srb;
1225 if (srb) {
1226 if (!srb->cmd)
1227 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
1228 srb, srb->cmd);
1229 else
1230 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) "
1231 "cmnd=0x%02x <%02i-%i>\n",
1232 srb, srb->cmd, srb->cmd->pid,
1233 srb->cmd->cmnd[0], srb->cmd->device->id,
1234 srb->cmd->device->lun);
1235 printk(" sglist=%p cnt=%i idx=%i len=%i\n",
1236 srb->segment_x, srb->sg_count, srb->sg_index,
1237 srb->total_xfer_length);
1238 printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1239 srb->state, srb->status, srb->scsi_phase,
1240 (acb->active_dcb) ? "" : "not");
1242 dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1243 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1244 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1245 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1246 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1247 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1248 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1249 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
1250 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1251 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1252 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1253 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
1254 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1255 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1256 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1257 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1258 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1259 dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1260 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1261 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1262 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1263 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1264 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1265 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1266 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1267 DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
1268 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1269 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1270 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1271 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1272 dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1273 "pci{status=0x%04x}\n",
1274 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1275 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1276 DC395x_read8(acb, TRM_S1040_GEN_TIMER),
1277 pstat);
1281 static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1283 #if debug_enabled(DBG_FIFO)
1284 u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1285 u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1286 if (!(fifocnt & 0x40))
1287 dprintkdbg(DBG_FIFO,
1288 "clear_fifo: (%i bytes) on phase %02x in %s\n",
1289 fifocnt & 0x3f, lines, txt);
1290 #endif
1291 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1295 static void reset_dev_param(struct AdapterCtlBlk *acb)
1297 struct DeviceCtlBlk *dcb;
1298 struct NvRamType *eeprom = &acb->eeprom;
1299 dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
1301 list_for_each_entry(dcb, &acb->dcb_list, list) {
1302 u8 period_index;
1304 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1305 dcb->sync_period = 0;
1306 dcb->sync_offset = 0;
1308 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1309 period_index = eeprom->target[dcb->target_id].period & 0x07;
1310 dcb->min_nego_period = clock_period[period_index];
1311 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1312 || !(acb->config & HCC_WIDE_CARD))
1313 dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1319 * perform a hard reset on the SCSI bus
1320 * @cmd - some command for this host (for fetching hooks)
1321 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1323 static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1325 struct AdapterCtlBlk *acb =
1326 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1327 dprintkl(KERN_INFO,
1328 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n",
1329 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1331 if (timer_pending(&acb->waiting_timer))
1332 del_timer(&acb->waiting_timer);
1335 * disable interrupt
1337 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1338 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1339 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1340 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1342 reset_scsi_bus(acb);
1343 udelay(500);
1345 /* We may be in serious trouble. Wait some seconds */
1346 acb->scsi_host->last_reset =
1347 jiffies + 3 * HZ / 2 +
1348 HZ * acb->eeprom.delay_time;
1351 * re-enable interrupt
1353 /* Clear SCSI FIFO */
1354 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1355 clear_fifo(acb, "eh_bus_reset");
1356 /* Delete pending IRQ */
1357 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1358 set_basic_config(acb);
1360 reset_dev_param(acb);
1361 doing_srb_done(acb, DID_RESET, cmd, 0);
1362 acb->active_dcb = NULL;
1363 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1364 waiting_process_next(acb);
1366 return SUCCESS;
1369 static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1371 int rc;
1373 spin_lock_irq(cmd->device->host->host_lock);
1374 rc = __dc395x_eh_bus_reset(cmd);
1375 spin_unlock_irq(cmd->device->host->host_lock);
1377 return rc;
1381 * abort an errant SCSI command
1382 * @cmd - command to be aborted
1383 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1385 static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1388 * Look into our command queues: If it has not been sent already,
1389 * we remove it and return success. Otherwise fail.
1391 struct AdapterCtlBlk *acb =
1392 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1393 struct DeviceCtlBlk *dcb;
1394 struct ScsiReqBlk *srb;
1395 dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
1396 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1398 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1399 if (!dcb) {
1400 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1401 return FAILED;
1404 srb = find_cmd(cmd, &dcb->srb_waiting_list);
1405 if (srb) {
1406 srb_waiting_remove(dcb, srb);
1407 pci_unmap_srb_sense(acb, srb);
1408 pci_unmap_srb(acb, srb);
1409 free_tag(dcb, srb);
1410 srb_free_insert(acb, srb);
1411 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1412 cmd->result = DID_ABORT << 16;
1413 return SUCCESS;
1415 srb = find_cmd(cmd, &dcb->srb_going_list);
1416 if (srb) {
1417 dprintkl(KERN_DEBUG, "eh_abort: Command in progress");
1418 /* XXX: Should abort the command here */
1419 } else {
1420 dprintkl(KERN_DEBUG, "eh_abort: Command not found");
1422 return FAILED;
1426 /* SDTR */
1427 static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1428 struct ScsiReqBlk *srb)
1430 u8 *ptr = srb->msgout_buf + srb->msg_count;
1431 if (srb->msg_count > 1) {
1432 dprintkl(KERN_INFO,
1433 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1434 srb->msg_count, srb->msgout_buf[0],
1435 srb->msgout_buf[1]);
1436 return;
1438 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1439 dcb->sync_offset = 0;
1440 dcb->min_nego_period = 200 >> 2;
1441 } else if (dcb->sync_offset == 0)
1442 dcb->sync_offset = SYNC_NEGO_OFFSET;
1444 *ptr++ = MSG_EXTENDED; /* (01h) */
1445 *ptr++ = 3; /* length */
1446 *ptr++ = EXTENDED_SDTR; /* (01h) */
1447 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */
1448 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */
1449 srb->msg_count += 5;
1450 srb->state |= SRB_DO_SYNC_NEGO;
1454 /* WDTR */
1455 static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1456 struct ScsiReqBlk *srb)
1458 u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1459 (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1460 u8 *ptr = srb->msgout_buf + srb->msg_count;
1461 if (srb->msg_count > 1) {
1462 dprintkl(KERN_INFO,
1463 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1464 srb->msg_count, srb->msgout_buf[0],
1465 srb->msgout_buf[1]);
1466 return;
1468 *ptr++ = MSG_EXTENDED; /* (01h) */
1469 *ptr++ = 2; /* length */
1470 *ptr++ = EXTENDED_WDTR; /* (03h) */
1471 *ptr++ = wide;
1472 srb->msg_count += 4;
1473 srb->state |= SRB_DO_WIDE_NEGO;
1477 #if 0
1478 /* Timer to work around chip flaw: When selecting and the bus is
1479 * busy, we sometimes miss a Selection timeout IRQ */
1480 void selection_timeout_missed(unsigned long ptr);
1481 /* Sets the timer to wake us up */
1482 static void selto_timer(struct AdapterCtlBlk *acb)
1484 if (timer_pending(&acb->selto_timer))
1485 return;
1486 acb->selto_timer.function = selection_timeout_missed;
1487 acb->selto_timer.data = (unsigned long) acb;
1488 if (time_before
1489 (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
1490 acb->selto_timer.expires =
1491 acb->scsi_host->last_reset + HZ / 2 + 1;
1492 else
1493 acb->selto_timer.expires = jiffies + HZ + 1;
1494 add_timer(&acb->selto_timer);
1498 void selection_timeout_missed(unsigned long ptr)
1500 unsigned long flags;
1501 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
1502 struct ScsiReqBlk *srb;
1503 dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1504 if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1505 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1506 return;
1508 DC395x_LOCK_IO(acb->scsi_host, flags);
1509 srb = acb->active_dcb->active_srb;
1510 disconnect(acb);
1511 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1513 #endif
1516 static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1517 struct ScsiReqBlk* srb)
1519 u16 s_stat2, return_code;
1520 u8 s_stat, scsicommand, i, identify_message;
1521 u8 *ptr;
1522 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
1523 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
1525 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
1527 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1528 s_stat2 = 0;
1529 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1530 #if 1
1531 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1532 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
1533 srb->cmd->pid, s_stat, s_stat2);
1535 * Try anyway?
1537 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1538 * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed!
1539 * (This is likely to be a bug in the hardware. Obviously, most people
1540 * only have one initiator per SCSI bus.)
1541 * Instead let this fail and have the timer make sure the command is
1542 * tried again after a short time
1544 /*selto_timer (acb); */
1545 return 1;
1547 #endif
1548 if (acb->active_dcb) {
1549 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
1550 "command while another command (pid#%li) is active.",
1551 srb->cmd->pid,
1552 acb->active_dcb->active_srb ?
1553 acb->active_dcb->active_srb->cmd->pid : 0);
1554 return 1;
1556 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1557 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
1558 srb->cmd->pid);
1559 return 1;
1561 /* Allow starting of SCSI commands half a second before we allow the mid-level
1562 * to queue them again after a reset */
1563 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1564 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1565 return 1;
1568 /* Flush FIFO */
1569 clear_fifo(acb, "start_scsi");
1570 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1571 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1572 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1573 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1574 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1576 identify_message = dcb->identify_msg;
1577 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1578 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1579 if (srb->flag & AUTO_REQSENSE)
1580 identify_message &= 0xBF;
1582 if (((srb->cmd->cmnd[0] == INQUIRY)
1583 || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1584 || (srb->flag & AUTO_REQSENSE))
1585 && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1586 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1587 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1588 && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1589 && (dcb->target_lun == 0)) {
1590 srb->msgout_buf[0] = identify_message;
1591 srb->msg_count = 1;
1592 scsicommand = SCMD_SEL_ATNSTOP;
1593 srb->state = SRB_MSGOUT;
1594 #ifndef SYNC_FIRST
1595 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1596 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1597 build_wdtr(acb, dcb, srb);
1598 goto no_cmd;
1600 #endif
1601 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1602 && dcb->inquiry7 & SCSI_INQ_SYNC) {
1603 build_sdtr(acb, dcb, srb);
1604 goto no_cmd;
1606 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1607 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1608 build_wdtr(acb, dcb, srb);
1609 goto no_cmd;
1611 srb->msg_count = 0;
1613 /* Send identify message */
1614 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1616 scsicommand = SCMD_SEL_ATN;
1617 srb->state = SRB_START_;
1618 #ifndef DC395x_NO_TAGQ
1619 if ((dcb->sync_mode & EN_TAG_QUEUEING)
1620 && (identify_message & 0xC0)) {
1621 /* Send Tag message */
1622 u32 tag_mask = 1;
1623 u8 tag_number = 0;
1624 while (tag_mask & dcb->tag_mask
1625 && tag_number <= dcb->max_command) {
1626 tag_mask = tag_mask << 1;
1627 tag_number++;
1629 if (tag_number >= dcb->max_command) {
1630 dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
1631 "Out of tags target=<%02i-%i>)\n",
1632 srb->cmd->pid, srb->cmd->device->id,
1633 srb->cmd->device->lun);
1634 srb->state = SRB_READY;
1635 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1636 DO_HWRESELECT);
1637 return 1;
1639 /* Send Tag id */
1640 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1641 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1642 dcb->tag_mask |= tag_mask;
1643 srb->tag_number = tag_number;
1644 scsicommand = SCMD_SEL_ATN3;
1645 srb->state = SRB_START_;
1647 #endif
1648 /*polling:*/
1649 /* Send CDB ..command block ......... */
1650 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
1651 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
1652 srb->cmd->cmnd[0], srb->tag_number);
1653 if (srb->flag & AUTO_REQSENSE) {
1654 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1655 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1656 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1657 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1658 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1659 sizeof(srb->cmd->sense_buffer));
1660 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1661 } else {
1662 ptr = (u8 *)srb->cmd->cmnd;
1663 for (i = 0; i < srb->cmd->cmd_len; i++)
1664 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1666 no_cmd:
1667 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1668 DO_HWRESELECT | DO_DATALATCH);
1669 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1671 * If start_scsi return 1:
1672 * we caught an interrupt (must be reset or reselection ... )
1673 * : Let's process it first!
1675 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
1676 srb->cmd->pid, dcb->target_id, dcb->target_lun);
1677 srb->state = SRB_READY;
1678 free_tag(dcb, srb);
1679 srb->msg_count = 0;
1680 return_code = 1;
1681 /* This IRQ should NOT get lost, as we did not acknowledge it */
1682 } else {
1684 * If start_scsi returns 0:
1685 * we know that the SCSI processor is free
1687 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1688 dcb->active_srb = srb;
1689 acb->active_dcb = dcb;
1690 return_code = 0;
1691 /* it's important for atn stop */
1692 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1693 DO_DATALATCH | DO_HWRESELECT);
1694 /* SCSI command */
1695 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1697 return return_code;
1701 #define DC395x_ENABLE_MSGOUT \
1702 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1703 srb->state |= SRB_MSGOUT
1706 /* abort command */
1707 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1708 struct ScsiReqBlk *srb)
1710 srb->msgout_buf[0] = ABORT;
1711 srb->msg_count = 1;
1712 DC395x_ENABLE_MSGOUT;
1713 srb->state &= ~SRB_MSGIN;
1714 srb->state |= SRB_MSGOUT;
1719 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1720 * have been triggered for this card.
1722 * @acb: a pointer to the adpter control block
1723 * @scsi_status: the status return when we checked the card
1725 static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1726 u16 scsi_status)
1728 struct DeviceCtlBlk *dcb;
1729 struct ScsiReqBlk *srb;
1730 u16 phase;
1731 u8 scsi_intstatus;
1732 unsigned long flags;
1733 void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
1734 u16 *);
1736 DC395x_LOCK_IO(acb->scsi_host, flags);
1738 /* This acknowledges the IRQ */
1739 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1740 if ((scsi_status & 0x2007) == 0x2002)
1741 dprintkl(KERN_DEBUG,
1742 "COP after COP completed? %04x\n", scsi_status);
1743 if (debug_enabled(DBG_KG)) {
1744 if (scsi_intstatus & INT_SELTIMEOUT)
1745 dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1747 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1749 if (timer_pending(&acb->selto_timer))
1750 del_timer(&acb->selto_timer);
1752 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1753 disconnect(acb); /* bus free interrupt */
1754 goto out_unlock;
1756 if (scsi_intstatus & INT_RESELECTED) {
1757 reselect(acb);
1758 goto out_unlock;
1760 if (scsi_intstatus & INT_SELECT) {
1761 dprintkl(KERN_INFO, "Host does not support target mode!\n");
1762 goto out_unlock;
1764 if (scsi_intstatus & INT_SCSIRESET) {
1765 scsi_reset_detect(acb);
1766 goto out_unlock;
1768 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1769 dcb = acb->active_dcb;
1770 if (!dcb) {
1771 dprintkl(KERN_DEBUG,
1772 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1773 scsi_status, scsi_intstatus);
1774 goto out_unlock;
1776 srb = dcb->active_srb;
1777 if (dcb->flag & ABORT_DEV_) {
1778 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1779 enable_msgout_abort(acb, srb);
1782 /* software sequential machine */
1783 phase = (u16)srb->scsi_phase;
1786 * 62037 or 62137
1787 * call dc395x_scsi_phase0[]... "phase entry"
1788 * handle every phase before start transfer
1790 /* data_out_phase0, phase:0 */
1791 /* data_in_phase0, phase:1 */
1792 /* command_phase0, phase:2 */
1793 /* status_phase0, phase:3 */
1794 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
1795 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
1796 /* msgout_phase0, phase:6 */
1797 /* msgin_phase0, phase:7 */
1798 dc395x_statev = dc395x_scsi_phase0[phase];
1799 dc395x_statev(acb, srb, &scsi_status);
1802 * if there were any exception occured scsi_status
1803 * will be modify to bus free phase new scsi_status
1804 * transfer out from ... previous dc395x_statev
1806 srb->scsi_phase = scsi_status & PHASEMASK;
1807 phase = (u16)scsi_status & PHASEMASK;
1810 * call dc395x_scsi_phase1[]... "phase entry" handle
1811 * every phase to do transfer
1813 /* data_out_phase1, phase:0 */
1814 /* data_in_phase1, phase:1 */
1815 /* command_phase1, phase:2 */
1816 /* status_phase1, phase:3 */
1817 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
1818 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
1819 /* msgout_phase1, phase:6 */
1820 /* msgin_phase1, phase:7 */
1821 dc395x_statev = dc395x_scsi_phase1[phase];
1822 dc395x_statev(acb, srb, &scsi_status);
1824 out_unlock:
1825 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1829 static irqreturn_t dc395x_interrupt(int irq, void *dev_id,
1830 struct pt_regs *regs)
1832 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)dev_id;
1833 u16 scsi_status;
1834 u8 dma_status;
1835 irqreturn_t handled = IRQ_NONE;
1838 * Check for pending interupt
1840 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1841 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1842 if (scsi_status & SCSIINTERRUPT) {
1843 /* interupt pending - let's process it! */
1844 dc395x_handle_interrupt(acb, scsi_status);
1845 handled = IRQ_HANDLED;
1847 else if (dma_status & 0x20) {
1848 /* Error from the DMA engine */
1849 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1850 #if 0
1851 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
1852 if (acb->active_dcb) {
1853 acb->active_dcb-> flag |= ABORT_DEV_;
1854 if (acb->active_dcb->active_srb)
1855 enable_msgout_abort(acb, acb->active_dcb->active_srb);
1857 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
1858 #else
1859 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
1860 acb = NULL;
1861 #endif
1862 handled = IRQ_HANDLED;
1865 return handled;
1869 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1870 u16 *pscsi_status)
1872 dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->pid);
1873 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
1874 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
1876 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1877 srb->state &= ~SRB_MSGOUT;
1881 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1882 u16 *pscsi_status)
1884 u16 i;
1885 u8 *ptr;
1886 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->pid);
1888 clear_fifo(acb, "msgout_phase1");
1889 if (!(srb->state & SRB_MSGOUT)) {
1890 srb->state |= SRB_MSGOUT;
1891 dprintkl(KERN_DEBUG,
1892 "msgout_phase1: (pid#%li) Phase unexpected\n",
1893 srb->cmd->pid); /* So what ? */
1895 if (!srb->msg_count) {
1896 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
1897 srb->cmd->pid);
1898 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
1899 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1900 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1901 return;
1903 ptr = (u8 *)srb->msgout_buf;
1904 for (i = 0; i < srb->msg_count; i++)
1905 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1906 srb->msg_count = 0;
1907 if (srb->msgout_buf[0] == MSG_ABORT)
1908 srb->state = SRB_ABORT_SENT;
1910 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1914 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1915 u16 *pscsi_status)
1917 dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->pid);
1918 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1922 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1923 u16 *pscsi_status)
1925 struct DeviceCtlBlk *dcb;
1926 u8 *ptr;
1927 u16 i;
1928 dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->pid);
1930 clear_fifo(acb, "command_phase1");
1931 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
1932 if (!(srb->flag & AUTO_REQSENSE)) {
1933 ptr = (u8 *)srb->cmd->cmnd;
1934 for (i = 0; i < srb->cmd->cmd_len; i++) {
1935 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
1936 ptr++;
1938 } else {
1939 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1940 dcb = acb->active_dcb;
1941 /* target id */
1942 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1943 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1944 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1945 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1946 sizeof(srb->cmd->sense_buffer));
1947 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1949 srb->state |= SRB_COMMAND;
1950 /* it's important for atn stop */
1951 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1952 /* SCSI command */
1953 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1958 * Verify that the remaining space in the hw sg lists is the same as
1959 * the count of remaining bytes in srb->total_xfer_length
1961 static void sg_verify_length(struct ScsiReqBlk *srb)
1963 if (debug_enabled(DBG_SG)) {
1964 unsigned len = 0;
1965 unsigned idx = srb->sg_index;
1966 struct SGentry *psge = srb->segment_x + idx;
1967 for (; idx < srb->sg_count; psge++, idx++)
1968 len += psge->length;
1969 if (len != srb->total_xfer_length)
1970 dprintkdbg(DBG_SG,
1971 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1972 srb->total_xfer_length, len);
1978 * Compute the next Scatter Gather list index and adjust its length
1979 * and address if necessary; also compute virt_addr
1981 static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
1983 u8 idx;
1984 struct scatterlist *sg;
1985 struct scsi_cmnd *cmd = srb->cmd;
1986 int segment = cmd->use_sg;
1987 u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
1988 struct SGentry *psge = srb->segment_x + srb->sg_index;
1990 dprintkdbg(DBG_0,
1991 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
1992 xferred, srb->total_xfer_length, left);
1993 if (xferred == 0) {
1994 /* nothing to update since we did not transfer any data */
1995 return;
1998 sg_verify_length(srb);
1999 srb->total_xfer_length = left; /* update remaining count */
2000 for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
2001 if (xferred >= psge->length) {
2002 /* Complete SG entries done */
2003 xferred -= psge->length;
2004 } else {
2005 /* Partial SG entry done */
2006 psge->length -= xferred;
2007 psge->address += xferred;
2008 srb->sg_index = idx;
2009 pci_dma_sync_single_for_device(srb->dcb->
2010 acb->dev,
2011 srb->sg_bus_addr,
2012 SEGMENTX_LEN,
2013 PCI_DMA_TODEVICE);
2014 break;
2016 psge++;
2018 sg_verify_length(srb);
2020 /* we need the corresponding virtual address */
2021 if (!segment || (srb->flag & AUTO_REQSENSE)) {
2022 srb->virt_addr += xferred;
2023 return;
2026 /* We have to walk the scatterlist to find it */
2027 sg = (struct scatterlist *)cmd->request_buffer;
2028 while (segment--) {
2029 unsigned long mask =
2030 ~((unsigned long)sg->length - 1) & PAGE_MASK;
2031 if ((sg_dma_address(sg) & mask) == (psge->address & mask)) {
2032 srb->virt_addr = (page_address(sg->page)
2033 + psge->address -
2034 (psge->address & PAGE_MASK));
2035 return;
2037 ++sg;
2040 dprintkl(KERN_ERR, "sg_update_list: sg_to_virt failed\n");
2041 srb->virt_addr = NULL;
2046 * We have transfered a single byte (PIO mode?) and need to update
2047 * the count of bytes remaining (total_xfer_length) and update the sg
2048 * entry to either point to next byte in the current sg entry, or of
2049 * already at the end to point to the start of the next sg entry
2051 static void sg_subtract_one(struct ScsiReqBlk *srb)
2053 srb->total_xfer_length--;
2054 srb->segment_x[srb->sg_index].length--;
2055 if (srb->total_xfer_length &&
2056 !srb->segment_x[srb->sg_index].length) {
2057 if (debug_enabled(DBG_PIO))
2058 printk(" (next segment)");
2059 srb->sg_index++;
2060 sg_update_list(srb, srb->total_xfer_length);
2066 * cleanup_after_transfer
2068 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
2069 * KG: Currently called from StatusPhase1 ()
2070 * Should probably also be called from other places
2071 * Best might be to call it in DataXXPhase0, if new phase will differ
2073 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
2074 struct ScsiReqBlk *srb)
2076 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
2077 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
2078 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2079 clear_fifo(acb, "cleanup/in");
2080 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2081 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2082 } else { /* write */
2083 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2084 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2085 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2086 clear_fifo(acb, "cleanup/out");
2088 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2093 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
2094 * Seems to be needed for unknown reasons; could be a hardware bug :-(
2096 #define DC395x_LASTPIO 4
2099 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2100 u16 *pscsi_status)
2102 struct DeviceCtlBlk *dcb = srb->dcb;
2103 u16 scsi_status = *pscsi_status;
2104 u32 d_left_counter = 0;
2105 dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
2106 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2109 * KG: We need to drain the buffers before we draw any conclusions!
2110 * This means telling the DMA to push the rest into SCSI, telling
2111 * SCSI to push the rest to the bus.
2112 * However, the device might have been the one to stop us (phase
2113 * change), and the data in transit just needs to be accounted so
2114 * it can be retransmitted.)
2117 * KG: Stop DMA engine pushing more data into the SCSI FIFO
2118 * If we need more data, the DMA SG list will be freshly set up, anyway
2120 dprintkdbg(DBG_PIO, "data_out_phase0: "
2121 "DMA{fifcnt=0x%02x fifostat=0x%02x} "
2122 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
2123 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2124 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2125 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2126 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2127 srb->total_xfer_length);
2128 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2130 if (!(srb->state & SRB_XFERPAD)) {
2131 if (scsi_status & PARITYERROR)
2132 srb->status |= PARITY_ERROR;
2135 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2136 * is the no of bytes it got from the DMA engine not the no it
2137 * transferred successfully to the device. (And the difference could
2138 * be as much as the FIFO size, I guess ...)
2140 if (!(scsi_status & SCSIXFERDONE)) {
2142 * when data transfer from DMA FIFO to SCSI FIFO
2143 * if there was some data left in SCSI FIFO
2145 d_left_counter =
2146 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2147 0x1F);
2148 if (dcb->sync_period & WIDE_SYNC)
2149 d_left_counter <<= 1;
2151 dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
2152 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
2153 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
2154 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2155 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2156 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2157 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2158 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2159 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2160 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2163 * calculate all the residue data that not yet tranfered
2164 * SCSI transfer counter + left in SCSI FIFO data
2166 * .....TRM_S1040_SCSI_COUNTER (24bits)
2167 * The counter always decrement by one for every SCSI byte transfer.
2168 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2169 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2171 if (srb->total_xfer_length > DC395x_LASTPIO)
2172 d_left_counter +=
2173 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2175 /* Is this a good idea? */
2176 /*clear_fifo(acb, "DOP1"); */
2177 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2178 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2179 && srb->cmd->request_bufflen % 2) {
2180 d_left_counter = 0;
2181 dprintkl(KERN_INFO,
2182 "data_out_phase0: Discard 1 byte (0x%02x)\n",
2183 scsi_status);
2186 * KG: Oops again. Same thinko as above: The SCSI might have been
2187 * faster than the DMA engine, so that it ran out of data.
2188 * In that case, we have to do just nothing!
2189 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2192 * KG: This is nonsense: We have been WRITING data to the bus
2193 * If the SCSI engine has no bytes left, how should the DMA engine?
2195 if (d_left_counter == 0) {
2196 srb->total_xfer_length = 0;
2197 } else {
2199 * if transfer not yet complete
2200 * there were some data residue in SCSI FIFO or
2201 * SCSI transfer counter not empty
2203 long oldxferred =
2204 srb->total_xfer_length - d_left_counter;
2205 const int diff =
2206 (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2207 sg_update_list(srb, d_left_counter);
2208 /* KG: Most ugly hack! Apparently, this works around a chip bug */
2209 if ((srb->segment_x[srb->sg_index].length ==
2210 diff && srb->cmd->use_sg)
2211 || ((oldxferred & ~PAGE_MASK) ==
2212 (PAGE_SIZE - diff))
2214 dprintkl(KERN_INFO, "data_out_phase0: "
2215 "Work around chip bug (%i)?\n", diff);
2216 d_left_counter =
2217 srb->total_xfer_length - diff;
2218 sg_update_list(srb, d_left_counter);
2219 /*srb->total_xfer_length -= diff; */
2220 /*srb->virt_addr += diff; */
2221 /*if (srb->cmd->use_sg) */
2222 /* srb->sg_index++; */
2226 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2227 cleanup_after_transfer(acb, srb);
2232 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2233 u16 *pscsi_status)
2235 dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
2236 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2237 clear_fifo(acb, "data_out_phase1");
2238 /* do prepare before transfer when data out phase */
2239 data_io_transfer(acb, srb, XFERDATAOUT);
2243 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2244 u16 *pscsi_status)
2246 u16 scsi_status = *pscsi_status;
2247 u32 d_left_counter = 0;
2248 dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
2249 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2252 * KG: DataIn is much more tricky than DataOut. When the device is finished
2253 * and switches to another phase, the SCSI engine should be finished too.
2254 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2255 * engine and transferred to memory.
2256 * We should wait for the FIFOs to be emptied by that (is there any way to
2257 * enforce this?) and then stop the DMA engine, because it might think, that
2258 * there are more bytes to follow. Yes, the device might disconnect prior to
2259 * having all bytes transferred!
2260 * Also we should make sure that all data from the DMA engine buffer's really
2261 * made its way to the system memory! Some documentation on this would not
2262 * seem to be a bad idea, actually.
2264 if (!(srb->state & SRB_XFERPAD)) {
2265 if (scsi_status & PARITYERROR) {
2266 dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
2267 "Parity Error\n", srb->cmd->pid);
2268 srb->status |= PARITY_ERROR;
2271 * KG: We should wait for the DMA FIFO to be empty ...
2272 * but: it would be better to wait first for the SCSI FIFO and then the
2273 * the DMA FIFO to become empty? How do we know, that the device not already
2274 * sent data to the FIFO in a MsgIn phase, eg.?
2276 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2277 #if 0
2278 int ctr = 6000000;
2279 dprintkl(KERN_DEBUG,
2280 "DIP0: Wait for DMA FIFO to flush ...\n");
2281 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2282 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2283 /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2284 while (!
2285 (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
2286 0x80) && --ctr);
2287 if (ctr < 6000000 - 1)
2288 dprintkl(KERN_DEBUG
2289 "DIP0: Had to wait for DMA ...\n");
2290 if (!ctr)
2291 dprintkl(KERN_ERR,
2292 "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2293 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2294 #endif
2295 dprintkdbg(DBG_KG, "data_in_phase0: "
2296 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2297 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2298 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2300 /* Now: Check remainig data: The SCSI counters should tell us ... */
2301 d_left_counter = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER)
2302 + ((DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f)
2303 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2304 0));
2305 dprintkdbg(DBG_KG, "data_in_phase0: "
2306 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2307 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2308 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2309 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2310 (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2311 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2312 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2313 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2314 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
2315 srb->total_xfer_length, d_left_counter);
2316 #if DC395x_LASTPIO
2317 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2318 if (d_left_counter
2319 && srb->total_xfer_length <= DC395x_LASTPIO) {
2320 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2321 /*sg_update_list (srb, d_left_counter); */
2322 dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) to "
2323 "%p for remaining %i bytes:",
2324 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f,
2325 (srb->dcb->sync_period & WIDE_SYNC) ?
2326 "words" : "bytes",
2327 srb->virt_addr,
2328 srb->total_xfer_length);
2329 if (srb->dcb->sync_period & WIDE_SYNC)
2330 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2331 CFG2_WIDEFIFO);
2332 while (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) != 0x40) {
2333 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2334 pio_trigger();
2335 *(srb->virt_addr)++ = byte;
2336 if (debug_enabled(DBG_PIO))
2337 printk(" %02x", byte);
2338 d_left_counter--;
2339 sg_subtract_one(srb);
2341 if (srb->dcb->sync_period & WIDE_SYNC) {
2342 #if 1
2343 /* Read the last byte ... */
2344 if (srb->total_xfer_length > 0) {
2345 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2346 pio_trigger();
2347 *(srb->virt_addr)++ = byte;
2348 srb->total_xfer_length--;
2349 if (debug_enabled(DBG_PIO))
2350 printk(" %02x", byte);
2352 #endif
2353 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2355 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2356 /*srb->total_xfer_length = 0; */
2357 if (debug_enabled(DBG_PIO))
2358 printk("\n");
2360 #endif /* DC395x_LASTPIO */
2362 #if 0
2364 * KG: This was in DATAOUT. Does it also belong here?
2365 * Nobody seems to know what counter and fifo_cnt count exactly ...
2367 if (!(scsi_status & SCSIXFERDONE)) {
2369 * when data transfer from DMA FIFO to SCSI FIFO
2370 * if there was some data left in SCSI FIFO
2372 d_left_counter =
2373 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2374 0x1F);
2375 if (srb->dcb->sync_period & WIDE_SYNC)
2376 d_left_counter <<= 1;
2378 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2379 * so need to *= 2
2380 * KG: Seems to be correct ...
2383 #endif
2384 /* KG: This should not be needed any more! */
2385 if (d_left_counter == 0
2386 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2387 #if 0
2388 int ctr = 6000000;
2389 u8 TempDMAstatus;
2390 do {
2391 TempDMAstatus =
2392 DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2393 } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
2394 if (!ctr)
2395 dprintkl(KERN_ERR,
2396 "Deadlock in DataInPhase0 waiting for DMA!!\n");
2397 srb->total_xfer_length = 0;
2398 #endif
2399 srb->total_xfer_length = d_left_counter;
2400 } else { /* phase changed */
2402 * parsing the case:
2403 * when a transfer not yet complete
2404 * but be disconnected by target
2405 * if transfer not yet complete
2406 * there were some data residue in SCSI FIFO or
2407 * SCSI transfer counter not empty
2409 sg_update_list(srb, d_left_counter);
2412 /* KG: The target may decide to disconnect: Empty FIFO before! */
2413 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2414 cleanup_after_transfer(acb, srb);
2419 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2420 u16 *pscsi_status)
2422 dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
2423 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2424 data_io_transfer(acb, srb, XFERDATAIN);
2428 static void data_io_transfer(struct AdapterCtlBlk *acb,
2429 struct ScsiReqBlk *srb, u16 io_dir)
2431 struct DeviceCtlBlk *dcb = srb->dcb;
2432 u8 bval;
2433 dprintkdbg(DBG_0,
2434 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
2435 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
2436 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
2437 srb->total_xfer_length, srb->sg_index, srb->sg_count);
2438 if (srb == acb->tmp_srb)
2439 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
2440 if (srb->sg_index >= srb->sg_count) {
2441 /* can't happen? out of bounds error */
2442 return;
2445 if (srb->total_xfer_length > DC395x_LASTPIO) {
2446 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2448 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2449 * Maybe, even ABORTXFER would be appropriate
2451 if (dma_status & XFERPENDING) {
2452 dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
2453 "Expect trouble!\n");
2454 dump_register_info(acb, dcb, srb);
2455 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2457 /* clear_fifo(acb, "IO"); */
2459 * load what physical address of Scatter/Gather list table
2460 * want to be transfer
2462 srb->state |= SRB_DATA_XFER;
2463 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2464 if (srb->cmd->use_sg) { /* with S/G */
2465 io_dir |= DMACMD_SG;
2466 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2467 srb->sg_bus_addr +
2468 sizeof(struct SGentry) *
2469 srb->sg_index);
2470 /* load how many bytes in the sg list table */
2471 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2472 ((u32)(srb->sg_count -
2473 srb->sg_index) << 3));
2474 } else { /* without S/G */
2475 io_dir &= ~DMACMD_SG;
2476 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2477 srb->segment_x[0].address);
2478 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2479 srb->segment_x[0].length);
2481 /* load total transfer length (24bits) max value 16Mbyte */
2482 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2483 srb->total_xfer_length);
2484 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2485 if (io_dir & DMACMD_DIR) { /* read */
2486 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2487 SCMD_DMA_IN);
2488 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2489 } else {
2490 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2491 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2492 SCMD_DMA_OUT);
2496 #if DC395x_LASTPIO
2497 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
2499 * load what physical address of Scatter/Gather list table
2500 * want to be transfer
2502 srb->state |= SRB_DATA_XFER;
2503 /* load total transfer length (24bits) max value 16Mbyte */
2504 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2505 srb->total_xfer_length);
2506 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2507 if (io_dir & DMACMD_DIR) { /* read */
2508 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2509 SCMD_FIFO_IN);
2510 } else { /* write */
2511 int ln = srb->total_xfer_length;
2512 if (srb->dcb->sync_period & WIDE_SYNC)
2513 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2514 CFG2_WIDEFIFO);
2515 dprintkdbg(DBG_PIO,
2516 "data_io_transfer: PIO %i bytes from %p:",
2517 srb->total_xfer_length, srb->virt_addr);
2519 while (srb->total_xfer_length) {
2520 if (debug_enabled(DBG_PIO))
2521 printk(" %02x", (unsigned char) *(srb->virt_addr));
2523 pio_trigger();
2524 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
2525 *(srb->virt_addr)++);
2527 sg_subtract_one(srb);
2529 if (srb->dcb->sync_period & WIDE_SYNC) {
2530 if (ln % 2) {
2531 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2532 if (debug_enabled(DBG_PIO))
2533 printk(" |00");
2535 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2537 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2538 if (debug_enabled(DBG_PIO))
2539 printk("\n");
2540 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2541 SCMD_FIFO_OUT);
2544 #endif /* DC395x_LASTPIO */
2545 else { /* xfer pad */
2546 u8 data = 0, data2 = 0;
2547 if (srb->sg_count) {
2548 srb->adapter_status = H_OVER_UNDER_RUN;
2549 srb->status |= OVER_RUN;
2552 * KG: despite the fact that we are using 16 bits I/O ops
2553 * the SCSI FIFO is only 8 bits according to the docs
2554 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2556 if (dcb->sync_period & WIDE_SYNC) {
2557 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
2558 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2559 CFG2_WIDEFIFO);
2560 if (io_dir & DMACMD_DIR) {
2561 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2562 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2563 } else {
2564 /* Danger, Robinson: If you find KGs
2565 * scattered over the wide disk, the driver
2566 * or chip is to blame :-( */
2567 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2568 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
2570 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2571 } else {
2572 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2573 /* Danger, Robinson: If you find a collection of Ks on your disk
2574 * something broke :-( */
2575 if (io_dir & DMACMD_DIR)
2576 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2577 else
2578 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2580 srb->state |= SRB_XFERPAD;
2581 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2582 /* SCSI command */
2583 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
2584 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
2589 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2590 u16 *pscsi_status)
2592 dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
2593 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2594 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2595 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
2596 srb->state = SRB_COMPLETED;
2597 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
2598 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2599 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2603 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2604 u16 *pscsi_status)
2606 dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
2607 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2608 srb->state = SRB_STATUS;
2609 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2610 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
2614 /* Check if the message is complete */
2615 static inline u8 msgin_completed(u8 * msgbuf, u32 len)
2617 if (*msgbuf == EXTENDED_MESSAGE) {
2618 if (len < 2)
2619 return 0;
2620 if (len < msgbuf[1] + 2)
2621 return 0;
2622 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
2623 if (len < 2)
2624 return 0;
2625 return 1;
2628 /* reject_msg */
2629 static inline void msgin_reject(struct AdapterCtlBlk *acb,
2630 struct ScsiReqBlk *srb)
2632 srb->msgout_buf[0] = MESSAGE_REJECT;
2633 srb->msg_count = 1;
2634 DC395x_ENABLE_MSGOUT;
2635 srb->state &= ~SRB_MSGIN;
2636 srb->state |= SRB_MSGOUT;
2637 dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
2638 srb->msgin_buf[0],
2639 srb->dcb->target_id, srb->dcb->target_lun);
2643 static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
2644 struct DeviceCtlBlk *dcb, u8 tag)
2646 struct ScsiReqBlk *srb = NULL;
2647 struct ScsiReqBlk *i;
2648 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
2649 srb->cmd->pid, tag, srb);
2651 if (!(dcb->tag_mask & (1 << tag)))
2652 dprintkl(KERN_DEBUG,
2653 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2654 dcb->tag_mask, tag);
2656 if (list_empty(&dcb->srb_going_list))
2657 goto mingx0;
2658 list_for_each_entry(i, &dcb->srb_going_list, list) {
2659 if (i->tag_number == tag) {
2660 srb = i;
2661 break;
2664 if (!srb)
2665 goto mingx0;
2667 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
2668 srb->cmd->pid, srb->dcb->target_id, srb->dcb->target_lun);
2669 if (dcb->flag & ABORT_DEV_) {
2670 /*srb->state = SRB_ABORT_SENT; */
2671 enable_msgout_abort(acb, srb);
2674 if (!(srb->state & SRB_DISCONNECT))
2675 goto mingx0;
2677 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
2678 srb->state |= dcb->active_srb->state;
2679 srb->state |= SRB_DATA_XFER;
2680 dcb->active_srb = srb;
2681 /* How can we make the DORS happy? */
2682 return srb;
2684 mingx0:
2685 srb = acb->tmp_srb;
2686 srb->state = SRB_UNEXPECT_RESEL;
2687 dcb->active_srb = srb;
2688 srb->msgout_buf[0] = MSG_ABORT_TAG;
2689 srb->msg_count = 1;
2690 DC395x_ENABLE_MSGOUT;
2691 dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
2692 return srb;
2696 static inline void reprogram_regs(struct AdapterCtlBlk *acb,
2697 struct DeviceCtlBlk *dcb)
2699 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
2700 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
2701 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
2702 set_xfer_rate(acb, dcb);
2706 /* set async transfer mode */
2707 static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2709 struct DeviceCtlBlk *dcb = srb->dcb;
2710 dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
2711 dcb->target_id, dcb->target_lun);
2713 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
2714 dcb->sync_mode |= SYNC_NEGO_DONE;
2715 /*dcb->sync_period &= 0; */
2716 dcb->sync_offset = 0;
2717 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
2718 srb->state &= ~SRB_DO_SYNC_NEGO;
2719 reprogram_regs(acb, dcb);
2720 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2721 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2722 build_wdtr(acb, dcb, srb);
2723 DC395x_ENABLE_MSGOUT;
2724 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
2729 /* set sync transfer mode */
2730 static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2732 struct DeviceCtlBlk *dcb = srb->dcb;
2733 u8 bval;
2734 int fact;
2735 dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
2736 "(%02i.%01i MHz) Offset %i\n",
2737 dcb->target_id, srb->msgin_buf[3] << 2,
2738 (250 / srb->msgin_buf[3]),
2739 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
2740 srb->msgin_buf[4]);
2742 if (srb->msgin_buf[4] > 15)
2743 srb->msgin_buf[4] = 15;
2744 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
2745 dcb->sync_offset = 0;
2746 else if (dcb->sync_offset == 0)
2747 dcb->sync_offset = srb->msgin_buf[4];
2748 if (srb->msgin_buf[4] > dcb->sync_offset)
2749 srb->msgin_buf[4] = dcb->sync_offset;
2750 else
2751 dcb->sync_offset = srb->msgin_buf[4];
2752 bval = 0;
2753 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
2754 || dcb->min_nego_period >
2755 clock_period[bval]))
2756 bval++;
2757 if (srb->msgin_buf[3] < clock_period[bval])
2758 dprintkl(KERN_INFO,
2759 "msgin_set_sync: Increase sync nego period to %ins\n",
2760 clock_period[bval] << 2);
2761 srb->msgin_buf[3] = clock_period[bval];
2762 dcb->sync_period &= 0xf0;
2763 dcb->sync_period |= ALT_SYNC | bval;
2764 dcb->min_nego_period = srb->msgin_buf[3];
2766 if (dcb->sync_period & WIDE_SYNC)
2767 fact = 500;
2768 else
2769 fact = 250;
2771 dprintkl(KERN_INFO,
2772 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2773 dcb->target_id, (fact == 500) ? "Wide16" : "",
2774 dcb->min_nego_period << 2, dcb->sync_offset,
2775 (fact / dcb->min_nego_period),
2776 ((fact % dcb->min_nego_period) * 10 +
2777 dcb->min_nego_period / 2) / dcb->min_nego_period);
2779 if (!(srb->state & SRB_DO_SYNC_NEGO)) {
2780 /* Reply with corrected SDTR Message */
2781 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
2782 srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
2784 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
2785 srb->msg_count = 5;
2786 DC395x_ENABLE_MSGOUT;
2787 dcb->sync_mode |= SYNC_NEGO_DONE;
2788 } else {
2789 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2790 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2791 build_wdtr(acb, dcb, srb);
2792 DC395x_ENABLE_MSGOUT;
2793 dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
2796 srb->state &= ~SRB_DO_SYNC_NEGO;
2797 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
2799 reprogram_regs(acb, dcb);
2803 static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
2804 struct ScsiReqBlk *srb)
2806 struct DeviceCtlBlk *dcb = srb->dcb;
2807 dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
2809 dcb->sync_period &= ~WIDE_SYNC;
2810 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
2811 dcb->sync_mode |= WIDE_NEGO_DONE;
2812 srb->state &= ~SRB_DO_WIDE_NEGO;
2813 reprogram_regs(acb, dcb);
2814 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2815 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2816 build_sdtr(acb, dcb, srb);
2817 DC395x_ENABLE_MSGOUT;
2818 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2822 static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2824 struct DeviceCtlBlk *dcb = srb->dcb;
2825 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
2826 && acb->config & HCC_WIDE_CARD) ? 1 : 0;
2827 dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
2829 if (srb->msgin_buf[3] > wide)
2830 srb->msgin_buf[3] = wide;
2831 /* Completed */
2832 if (!(srb->state & SRB_DO_WIDE_NEGO)) {
2833 dprintkl(KERN_DEBUG,
2834 "msgin_set_wide: Wide nego initiated <%02i>\n",
2835 dcb->target_id);
2836 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
2837 srb->msg_count = 4;
2838 srb->state |= SRB_DO_WIDE_NEGO;
2839 DC395x_ENABLE_MSGOUT;
2842 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
2843 if (srb->msgin_buf[3] > 0)
2844 dcb->sync_period |= WIDE_SYNC;
2845 else
2846 dcb->sync_period &= ~WIDE_SYNC;
2847 srb->state &= ~SRB_DO_WIDE_NEGO;
2848 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2849 dprintkdbg(DBG_1,
2850 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2851 (8 << srb->msgin_buf[3]), dcb->target_id);
2852 reprogram_regs(acb, dcb);
2853 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2854 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2855 build_sdtr(acb, dcb, srb);
2856 DC395x_ENABLE_MSGOUT;
2857 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
2863 * extended message codes:
2865 * code description
2867 * 02h Reserved
2868 * 00h MODIFY DATA POINTER
2869 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
2870 * 03h WIDE DATA TRANSFER REQUEST
2871 * 04h - 7Fh Reserved
2872 * 80h - FFh Vendor specific
2874 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2875 u16 *pscsi_status)
2877 struct DeviceCtlBlk *dcb = acb->active_dcb;
2878 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->pid);
2880 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2881 if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
2882 /* Now eval the msg */
2883 switch (srb->msgin_buf[0]) {
2884 case DISCONNECT:
2885 srb->state = SRB_DISCONNECT;
2886 break;
2888 case SIMPLE_QUEUE_TAG:
2889 case HEAD_OF_QUEUE_TAG:
2890 case ORDERED_QUEUE_TAG:
2891 srb =
2892 msgin_qtag(acb, dcb,
2893 srb->msgin_buf[1]);
2894 break;
2896 case MESSAGE_REJECT:
2897 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2898 DO_CLRATN | DO_DATALATCH);
2899 /* A sync nego message was rejected ! */
2900 if (srb->state & SRB_DO_SYNC_NEGO) {
2901 msgin_set_async(acb, srb);
2902 break;
2904 /* A wide nego message was rejected ! */
2905 if (srb->state & SRB_DO_WIDE_NEGO) {
2906 msgin_set_nowide(acb, srb);
2907 break;
2909 enable_msgout_abort(acb, srb);
2910 /*srb->state |= SRB_ABORT_SENT */
2911 break;
2913 case EXTENDED_MESSAGE:
2914 /* SDTR */
2915 if (srb->msgin_buf[1] == 3
2916 && srb->msgin_buf[2] == EXTENDED_SDTR) {
2917 msgin_set_sync(acb, srb);
2918 break;
2920 /* WDTR */
2921 if (srb->msgin_buf[1] == 2
2922 && srb->msgin_buf[2] == EXTENDED_WDTR
2923 && srb->msgin_buf[3] <= 2) { /* sanity check ... */
2924 msgin_set_wide(acb, srb);
2925 break;
2927 msgin_reject(acb, srb);
2928 break;
2930 case MSG_IGNOREWIDE:
2931 /* Discard wide residual */
2932 dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
2933 break;
2935 case COMMAND_COMPLETE:
2936 /* nothing has to be done */
2937 break;
2939 case SAVE_POINTERS:
2941 * SAVE POINTER may be ignored as we have the struct
2942 * ScsiReqBlk* associated with the scsi command.
2944 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2945 "SAVE POINTER rem=%i Ignore\n",
2946 srb->cmd->pid, srb->total_xfer_length);
2947 break;
2949 case RESTORE_POINTERS:
2950 dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
2951 break;
2953 case ABORT:
2954 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2955 "<%02i-%i> ABORT msg\n",
2956 srb->cmd->pid, dcb->target_id,
2957 dcb->target_lun);
2958 dcb->flag |= ABORT_DEV_;
2959 enable_msgout_abort(acb, srb);
2960 break;
2962 default:
2963 /* reject unknown messages */
2964 if (srb->msgin_buf[0] & IDENTIFY_BASE) {
2965 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
2966 srb->msg_count = 1;
2967 srb->msgout_buf[0] = dcb->identify_msg;
2968 DC395x_ENABLE_MSGOUT;
2969 srb->state |= SRB_MSGOUT;
2970 /*break; */
2972 msgin_reject(acb, srb);
2975 /* Clear counter and MsgIn state */
2976 srb->state &= ~SRB_MSGIN;
2977 acb->msg_len = 0;
2979 *pscsi_status = PH_BUS_FREE;
2980 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
2981 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2985 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2986 u16 *pscsi_status)
2988 dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->pid);
2989 clear_fifo(acb, "msgin_phase1");
2990 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2991 if (!(srb->state & SRB_MSGIN)) {
2992 srb->state &= ~SRB_DISCONNECT;
2993 srb->state |= SRB_MSGIN;
2995 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2996 /* SCSI command */
2997 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
3001 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3002 u16 *pscsi_status)
3007 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3008 u16 *pscsi_status)
3013 static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
3015 struct DeviceCtlBlk *i;
3017 /* set all lun device's period, offset */
3018 if (dcb->identify_msg & 0x07)
3019 return;
3021 if (acb->scan_devices) {
3022 current_sync_offset = dcb->sync_offset;
3023 return;
3026 list_for_each_entry(i, &acb->dcb_list, list)
3027 if (i->target_id == dcb->target_id) {
3028 i->sync_period = dcb->sync_period;
3029 i->sync_offset = dcb->sync_offset;
3030 i->sync_mode = dcb->sync_mode;
3031 i->min_nego_period = dcb->min_nego_period;
3036 static void disconnect(struct AdapterCtlBlk *acb)
3038 struct DeviceCtlBlk *dcb = acb->active_dcb;
3039 struct ScsiReqBlk *srb;
3041 if (!dcb) {
3042 dprintkl(KERN_ERR, "disconnect: No such device\n");
3043 udelay(500);
3044 /* Suspend queue for a while */
3045 acb->scsi_host->last_reset =
3046 jiffies + HZ / 2 +
3047 HZ * acb->eeprom.delay_time;
3048 clear_fifo(acb, "disconnectEx");
3049 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3050 return;
3052 srb = dcb->active_srb;
3053 acb->active_dcb = NULL;
3054 dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->pid);
3056 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3057 clear_fifo(acb, "disconnect");
3058 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3059 if (srb->state & SRB_UNEXPECT_RESEL) {
3060 dprintkl(KERN_ERR,
3061 "disconnect: Unexpected reselection <%02i-%i>\n",
3062 dcb->target_id, dcb->target_lun);
3063 srb->state = 0;
3064 waiting_process_next(acb);
3065 } else if (srb->state & SRB_ABORT_SENT) {
3066 dcb->flag &= ~ABORT_DEV_;
3067 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
3068 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
3069 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
3070 waiting_process_next(acb);
3071 } else {
3072 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
3073 || !(srb->
3074 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
3076 * Selection time out
3077 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
3079 /* Unexp. Disc / Sel Timeout */
3080 if (srb->state != SRB_START_
3081 && srb->state != SRB_MSGOUT) {
3082 srb->state = SRB_READY;
3083 dprintkl(KERN_DEBUG,
3084 "disconnect: (pid#%li) Unexpected\n",
3085 srb->cmd->pid);
3086 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
3087 goto disc1;
3088 } else {
3089 /* Normal selection timeout */
3090 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
3091 "<%02i-%i> SelTO\n", srb->cmd->pid,
3092 dcb->target_id, dcb->target_lun);
3093 if (srb->retry_count++ > DC395x_MAX_RETRIES
3094 || acb->scan_devices) {
3095 srb->target_status =
3096 SCSI_STAT_SEL_TIMEOUT;
3097 goto disc1;
3099 free_tag(dcb, srb);
3100 srb_going_to_waiting_move(dcb, srb);
3101 dprintkdbg(DBG_KG,
3102 "disconnect: (pid#%li) Retry\n",
3103 srb->cmd->pid);
3104 waiting_set_timer(acb, HZ / 20);
3106 } else if (srb->state & SRB_DISCONNECT) {
3107 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
3109 * SRB_DISCONNECT (This is what we expect!)
3111 if (bval & 0x40) {
3112 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
3113 " 0x%02x: ACK set! Other controllers?\n",
3114 bval);
3115 /* It could come from another initiator, therefore don't do much ! */
3116 } else
3117 waiting_process_next(acb);
3118 } else if (srb->state & SRB_COMPLETED) {
3119 disc1:
3121 ** SRB_COMPLETED
3123 free_tag(dcb, srb);
3124 dcb->active_srb = NULL;
3125 srb->state = SRB_FREE;
3126 srb_done(acb, dcb, srb);
3132 static void reselect(struct AdapterCtlBlk *acb)
3134 struct DeviceCtlBlk *dcb = acb->active_dcb;
3135 struct ScsiReqBlk *srb = NULL;
3136 u16 rsel_tar_lun_id;
3137 u8 id, lun;
3138 u8 arblostflag = 0;
3139 dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
3141 clear_fifo(acb, "reselect");
3142 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3143 /* Read Reselected Target ID and LUN */
3144 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3145 if (dcb) { /* Arbitration lost but Reselection win */
3146 srb = dcb->active_srb;
3147 if (!srb) {
3148 dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
3149 "but active_srb == NULL\n");
3150 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3151 return;
3153 /* Why the if ? */
3154 if (!acb->scan_devices) {
3155 dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
3156 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
3157 srb->cmd->pid, dcb->target_id,
3158 dcb->target_lun, rsel_tar_lun_id,
3159 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3160 arblostflag = 1;
3161 /*srb->state |= SRB_DISCONNECT; */
3163 srb->state = SRB_READY;
3164 free_tag(dcb, srb);
3165 srb_going_to_waiting_move(dcb, srb);
3166 waiting_set_timer(acb, HZ / 20);
3168 /* return; */
3171 /* Read Reselected Target Id and LUN */
3172 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3173 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
3174 "Got %i!\n", rsel_tar_lun_id);
3175 id = rsel_tar_lun_id & 0xff;
3176 lun = (rsel_tar_lun_id >> 8) & 7;
3177 dcb = find_dcb(acb, id, lun);
3178 if (!dcb) {
3179 dprintkl(KERN_ERR, "reselect: From non existent device "
3180 "<%02i-%i>\n", id, lun);
3181 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3182 return;
3184 acb->active_dcb = dcb;
3186 if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3187 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
3188 "disconnection? <%02i-%i>\n",
3189 dcb->target_id, dcb->target_lun);
3191 if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
3192 srb = acb->tmp_srb;
3193 dcb->active_srb = srb;
3194 } else {
3195 /* There can be only one! */
3196 srb = dcb->active_srb;
3197 if (!srb || !(srb->state & SRB_DISCONNECT)) {
3199 * abort command
3201 dprintkl(KERN_DEBUG,
3202 "reselect: w/o disconnected cmds <%02i-%i>\n",
3203 dcb->target_id, dcb->target_lun);
3204 srb = acb->tmp_srb;
3205 srb->state = SRB_UNEXPECT_RESEL;
3206 dcb->active_srb = srb;
3207 enable_msgout_abort(acb, srb);
3208 } else {
3209 if (dcb->flag & ABORT_DEV_) {
3210 /*srb->state = SRB_ABORT_SENT; */
3211 enable_msgout_abort(acb, srb);
3212 } else
3213 srb->state = SRB_DATA_XFER;
3217 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3219 /* Program HA ID, target ID, period and offset */
3220 dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
3221 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
3222 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
3223 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
3224 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
3225 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3226 /* SCSI command */
3227 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3231 static inline u8 tagq_blacklist(char *name)
3233 #ifndef DC395x_NO_TAGQ
3234 #if 0
3235 u8 i;
3236 for (i = 0; i < BADDEVCNT; i++)
3237 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
3238 return 1;
3239 #endif
3240 return 0;
3241 #else
3242 return 1;
3243 #endif
3247 static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
3249 /* Check for SCSI format (ANSI and Response data format) */
3250 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
3251 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
3252 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
3253 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3254 /* ((dcb->dev_type == TYPE_DISK)
3255 || (dcb->dev_type == TYPE_MOD)) && */
3256 !tagq_blacklist(((char *)ptr) + 8)) {
3257 if (dcb->max_command == 1)
3258 dcb->max_command =
3259 dcb->acb->tag_max_num;
3260 dcb->sync_mode |= EN_TAG_QUEUEING;
3261 /*dcb->tag_mask = 0; */
3262 } else
3263 dcb->max_command = 1;
3268 static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3269 struct ScsiInqData *ptr)
3271 u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
3272 dcb->dev_type = bval1;
3273 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3274 disc_tagq_set(dcb, ptr);
3278 /* unmap mapped pci regions from SRB */
3279 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3281 struct scsi_cmnd *cmd = srb->cmd;
3282 enum dma_data_direction dir = cmd->sc_data_direction;
3283 if (cmd->use_sg && dir != PCI_DMA_NONE) {
3284 /* unmap DC395x SG list */
3285 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
3286 srb->sg_bus_addr, SEGMENTX_LEN);
3287 pci_unmap_single(acb->dev, srb->sg_bus_addr,
3288 SEGMENTX_LEN,
3289 PCI_DMA_TODEVICE);
3290 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3291 cmd->use_sg, cmd->request_buffer);
3292 /* unmap the sg segments */
3293 pci_unmap_sg(acb->dev,
3294 (struct scatterlist *)cmd->request_buffer,
3295 cmd->use_sg, dir);
3296 } else if (cmd->request_buffer && dir != PCI_DMA_NONE) {
3297 dprintkdbg(DBG_SG, "pci_unmap_srb: buffer=%08x(%05x)\n",
3298 srb->segment_x[0].address, cmd->request_bufflen);
3299 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3300 cmd->request_bufflen, dir);
3305 /* unmap mapped pci sense buffer from SRB */
3306 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3307 struct ScsiReqBlk *srb)
3309 if (!(srb->flag & AUTO_REQSENSE))
3310 return;
3311 /* Unmap sense buffer */
3312 dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
3313 srb->segment_x[0].address);
3314 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3315 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
3316 /* Restore SG stuff */
3317 srb->total_xfer_length = srb->xferred;
3318 srb->segment_x[0].address =
3319 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
3320 srb->segment_x[0].length =
3321 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
3322 srb->virt_addr = srb->virt_addr_req;
3327 * Complete execution of a SCSI command
3328 * Signal completion to the generic SCSI driver
3330 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3331 struct ScsiReqBlk *srb)
3333 u8 tempcnt, status;
3334 struct scsi_cmnd *cmd = srb->cmd;
3335 struct ScsiInqData *ptr;
3336 enum dma_data_direction dir = cmd->sc_data_direction;
3338 if (cmd->use_sg) {
3339 struct scatterlist* sg = (struct scatterlist *)cmd->request_buffer;
3340 ptr = (struct ScsiInqData *)(page_address(sg->page) + sg->offset);
3341 } else {
3342 ptr = (struct ScsiInqData *)(cmd->request_buffer);
3345 dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->pid,
3346 srb->cmd->device->id, srb->cmd->device->lun);
3347 dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p addr=%p\n",
3348 srb, cmd->use_sg, srb->sg_index, srb->sg_count,
3349 cmd->request_buffer, ptr);
3350 status = srb->target_status;
3351 if (srb->flag & AUTO_REQSENSE) {
3352 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
3353 pci_unmap_srb_sense(acb, srb);
3355 ** target status..........................
3357 srb->flag &= ~AUTO_REQSENSE;
3358 srb->adapter_status = 0;
3359 srb->target_status = CHECK_CONDITION << 1;
3360 if (debug_enabled(DBG_1)) {
3361 switch (cmd->sense_buffer[2] & 0x0f) {
3362 case NOT_READY:
3363 dprintkl(KERN_DEBUG,
3364 "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3365 cmd->cmnd[0], dcb->target_id,
3366 dcb->target_lun, status, acb->scan_devices);
3367 break;
3368 case UNIT_ATTENTION:
3369 dprintkl(KERN_DEBUG,
3370 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3371 cmd->cmnd[0], dcb->target_id,
3372 dcb->target_lun, status, acb->scan_devices);
3373 break;
3374 case ILLEGAL_REQUEST:
3375 dprintkl(KERN_DEBUG,
3376 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3377 cmd->cmnd[0], dcb->target_id,
3378 dcb->target_lun, status, acb->scan_devices);
3379 break;
3380 case MEDIUM_ERROR:
3381 dprintkl(KERN_DEBUG,
3382 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3383 cmd->cmnd[0], dcb->target_id,
3384 dcb->target_lun, status, acb->scan_devices);
3385 break;
3386 case HARDWARE_ERROR:
3387 dprintkl(KERN_DEBUG,
3388 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3389 cmd->cmnd[0], dcb->target_id,
3390 dcb->target_lun, status, acb->scan_devices);
3391 break;
3393 if (cmd->sense_buffer[7] >= 6)
3394 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3395 "(0x%08x 0x%08x)\n",
3396 cmd->sense_buffer[2], cmd->sense_buffer[12],
3397 cmd->sense_buffer[13],
3398 *((unsigned int *)(cmd->sense_buffer + 3)),
3399 *((unsigned int *)(cmd->sense_buffer + 8)));
3400 else
3401 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3402 cmd->sense_buffer[2],
3403 *((unsigned int *)(cmd->sense_buffer + 3)));
3406 if (status == (CHECK_CONDITION << 1)) {
3407 cmd->result = DID_BAD_TARGET << 16;
3408 goto ckc_e;
3410 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
3412 if (srb->total_xfer_length
3413 && srb->total_xfer_length >= cmd->underflow)
3414 cmd->result =
3415 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3416 srb->end_message, CHECK_CONDITION);
3417 /*SET_RES_DID(cmd->result,DID_OK) */
3418 else
3419 cmd->result =
3420 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3421 srb->end_message, CHECK_CONDITION);
3423 goto ckc_e;
3426 /*************************************************************/
3427 if (status) {
3429 * target status..........................
3431 if (status_byte(status) == CHECK_CONDITION) {
3432 request_sense(acb, dcb, srb);
3433 return;
3434 } else if (status_byte(status) == QUEUE_FULL) {
3435 tempcnt = (u8)list_size(&dcb->srb_going_list);
3436 dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3437 dcb->target_id, dcb->target_lun, tempcnt);
3438 if (tempcnt > 1)
3439 tempcnt--;
3440 dcb->max_command = tempcnt;
3441 free_tag(dcb, srb);
3442 srb_going_to_waiting_move(dcb, srb);
3443 waiting_set_timer(acb, HZ / 20);
3444 srb->adapter_status = 0;
3445 srb->target_status = 0;
3446 return;
3447 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
3448 srb->adapter_status = H_SEL_TIMEOUT;
3449 srb->target_status = 0;
3450 cmd->result = DID_NO_CONNECT << 16;
3451 } else {
3452 srb->adapter_status = 0;
3453 SET_RES_DID(cmd->result, DID_ERROR);
3454 SET_RES_MSG(cmd->result, srb->end_message);
3455 SET_RES_TARGET(cmd->result, status);
3458 } else {
3460 ** process initiator status..........................
3462 status = srb->adapter_status;
3463 if (status & H_OVER_UNDER_RUN) {
3464 srb->target_status = 0;
3465 SET_RES_DID(cmd->result, DID_OK);
3466 SET_RES_MSG(cmd->result, srb->end_message);
3467 } else if (srb->status & PARITY_ERROR) {
3468 SET_RES_DID(cmd->result, DID_PARITY);
3469 SET_RES_MSG(cmd->result, srb->end_message);
3470 } else { /* No error */
3472 srb->adapter_status = 0;
3473 srb->target_status = 0;
3474 SET_RES_DID(cmd->result, DID_OK);
3478 if (dir != PCI_DMA_NONE) {
3479 if (cmd->use_sg)
3480 pci_dma_sync_sg_for_cpu(acb->dev,
3481 (struct scatterlist *)cmd->
3482 request_buffer, cmd->use_sg, dir);
3483 else if (cmd->request_buffer)
3484 pci_dma_sync_single_for_cpu(acb->dev,
3485 srb->segment_x[0].address,
3486 cmd->request_bufflen, dir);
3489 if ((cmd->result & RES_DID) == 0 && cmd->cmnd[0] == INQUIRY
3490 && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8
3491 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
3492 dcb->inquiry7 = ptr->Flags;
3493 /* Check Error Conditions */
3494 ckc_e:
3496 /*if( srb->cmd->cmnd[0] == INQUIRY && */
3497 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3498 if (cmd->cmnd[0] == INQUIRY && (cmd->result == (DID_OK << 16)
3499 || status_byte(cmd->
3500 result) &
3501 CHECK_CONDITION)) {
3503 if (!dcb->init_tcq_flag) {
3504 add_dev(acb, dcb, ptr);
3505 dcb->init_tcq_flag = 1;
3511 /* Here is the info for Doug Gilbert's sg3 ... */
3512 cmd->resid = srb->total_xfer_length;
3513 /* This may be interpreted by sb. or not ... */
3514 cmd->SCp.this_residual = srb->total_xfer_length;
3515 cmd->SCp.buffers_residual = 0;
3516 if (debug_enabled(DBG_KG)) {
3517 if (srb->total_xfer_length)
3518 dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
3519 "cmnd=0x%02x Missed %i bytes\n",
3520 cmd->pid, cmd->device->id, cmd->device->lun,
3521 cmd->cmnd[0], srb->total_xfer_length);
3524 srb_going_remove(dcb, srb);
3525 /* Add to free list */
3526 if (srb == acb->tmp_srb)
3527 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3528 else {
3529 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
3530 cmd->pid, cmd->result);
3531 srb_free_insert(acb, srb);
3533 pci_unmap_srb(acb, srb);
3535 cmd->scsi_done(cmd);
3536 waiting_process_next(acb);
3540 /* abort all cmds in our queues */
3541 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
3542 struct scsi_cmnd *cmd, u8 force)
3544 struct DeviceCtlBlk *dcb;
3545 dprintkl(KERN_INFO, "doing_srb_done: pids ");
3547 list_for_each_entry(dcb, &acb->dcb_list, list) {
3548 struct ScsiReqBlk *srb;
3549 struct ScsiReqBlk *tmp;
3550 struct scsi_cmnd *p;
3552 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
3553 enum dma_data_direction dir;
3554 int result;
3556 p = srb->cmd;
3557 dir = p->sc_data_direction;
3558 result = MK_RES(0, did_flag, 0, 0);
3559 printk("G:%li(%02i-%i) ", p->pid,
3560 p->device->id, p->device->lun);
3561 srb_going_remove(dcb, srb);
3562 free_tag(dcb, srb);
3563 srb_free_insert(acb, srb);
3564 p->result = result;
3565 pci_unmap_srb_sense(acb, srb);
3566 pci_unmap_srb(acb, srb);
3567 if (force) {
3568 /* For new EH, we normally don't need to give commands back,
3569 * as they all complete or all time out */
3570 p->scsi_done(p);
3573 if (!list_empty(&dcb->srb_going_list))
3574 dprintkl(KERN_DEBUG,
3575 "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3576 dcb->target_id, dcb->target_lun);
3577 if (dcb->tag_mask)
3578 dprintkl(KERN_DEBUG,
3579 "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3580 dcb->target_id, dcb->target_lun,
3581 dcb->tag_mask);
3583 /* Waiting queue */
3584 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
3585 int result;
3586 p = srb->cmd;
3588 result = MK_RES(0, did_flag, 0, 0);
3589 printk("W:%li<%02i-%i>", p->pid, p->device->id,
3590 p->device->lun);
3591 srb_waiting_remove(dcb, srb);
3592 srb_free_insert(acb, srb);
3593 p->result = result;
3594 pci_unmap_srb_sense(acb, srb);
3595 pci_unmap_srb(acb, srb);
3596 if (force) {
3597 /* For new EH, we normally don't need to give commands back,
3598 * as they all complete or all time out */
3599 cmd->scsi_done(cmd);
3602 if (!list_empty(&dcb->srb_waiting_list))
3603 dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
3604 list_size(&dcb->srb_waiting_list), dcb->target_id,
3605 dcb->target_lun);
3606 dcb->flag &= ~ABORT_DEV_;
3608 printk("\n");
3612 static void reset_scsi_bus(struct AdapterCtlBlk *acb)
3614 dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
3615 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
3616 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
3618 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
3619 /* nothing */;
3623 static void set_basic_config(struct AdapterCtlBlk *acb)
3625 u8 bval;
3626 u16 wval;
3627 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
3628 if (acb->config & HCC_PARITY)
3629 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
3630 else
3631 bval = PHASELATCH | INITIATOR | BLOCKRST;
3633 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
3635 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3636 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
3637 /* program Host ID */
3638 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
3639 /* set ansynchronous transfer */
3640 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
3641 /* Turn LED control off */
3642 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
3643 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
3644 /* DMA config */
3645 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
3646 wval |=
3647 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
3648 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
3649 /* Clear pending interrupt status */
3650 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
3651 /* Enable SCSI interrupt */
3652 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
3653 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
3654 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3659 static void scsi_reset_detect(struct AdapterCtlBlk *acb)
3661 dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
3662 /* delay half a second */
3663 if (timer_pending(&acb->waiting_timer))
3664 del_timer(&acb->waiting_timer);
3666 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
3667 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
3668 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3669 udelay(500);
3670 /* Maybe we locked up the bus? Then lets wait even longer ... */
3671 acb->scsi_host->last_reset =
3672 jiffies + 5 * HZ / 2 +
3673 HZ * acb->eeprom.delay_time;
3675 clear_fifo(acb, "scsi_reset_detect");
3676 set_basic_config(acb);
3677 /*1.25 */
3678 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3680 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
3681 acb->acb_flag |= RESET_DONE;
3682 } else {
3683 acb->acb_flag |= RESET_DETECT;
3684 reset_dev_param(acb);
3685 doing_srb_done(acb, DID_RESET, NULL, 1);
3686 /*DC395x_RecoverSRB( acb ); */
3687 acb->active_dcb = NULL;
3688 acb->acb_flag = 0;
3689 waiting_process_next(acb);
3694 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3695 struct ScsiReqBlk *srb)
3697 struct scsi_cmnd *cmd = srb->cmd;
3698 dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
3699 cmd->pid, cmd->device->id, cmd->device->lun);
3701 srb->flag |= AUTO_REQSENSE;
3702 srb->adapter_status = 0;
3703 srb->target_status = 0;
3705 /* KG: Can this prevent crap sense data ? */
3706 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3708 /* Save some data */
3709 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
3710 srb->segment_x[0].address;
3711 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
3712 srb->segment_x[0].length;
3713 srb->xferred = srb->total_xfer_length;
3714 /* srb->segment_x : a one entry of S/G list table */
3715 srb->total_xfer_length = sizeof(cmd->sense_buffer);
3716 srb->virt_addr_req = srb->virt_addr;
3717 srb->virt_addr = cmd->sense_buffer;
3718 srb->segment_x[0].length = sizeof(cmd->sense_buffer);
3719 /* Map sense buffer */
3720 srb->segment_x[0].address =
3721 pci_map_single(acb->dev, cmd->sense_buffer,
3722 sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE);
3723 dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
3724 cmd->sense_buffer, srb->segment_x[0].address,
3725 sizeof(cmd->sense_buffer));
3726 srb->sg_count = 1;
3727 srb->sg_index = 0;
3729 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
3730 dprintkl(KERN_DEBUG,
3731 "request_sense: (pid#%li) failed <%02i-%i>\n",
3732 srb->cmd->pid, dcb->target_id, dcb->target_lun);
3733 srb_going_to_waiting_move(dcb, srb);
3734 waiting_set_timer(acb, HZ / 100);
3740 * device_alloc - Allocate a new device instance. This create the
3741 * devices instance and sets up all the data items. The adapter
3742 * instance is required to obtain confiuration information for this
3743 * device. This does *not* add this device to the adapters device
3744 * list.
3746 * @acb: The adapter to obtain configuration information from.
3747 * @target: The target for the new device.
3748 * @lun: The lun for the new device.
3750 * Return the new device if succesfull or NULL on failure.
3752 static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
3753 u8 target, u8 lun)
3755 struct NvRamType *eeprom = &acb->eeprom;
3756 u8 period_index = eeprom->target[target].period & 0x07;
3757 struct DeviceCtlBlk *dcb;
3759 dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
3760 dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
3761 if (!dcb)
3762 return NULL;
3763 dcb->acb = NULL;
3764 INIT_LIST_HEAD(&dcb->srb_going_list);
3765 INIT_LIST_HEAD(&dcb->srb_waiting_list);
3766 dcb->active_srb = NULL;
3767 dcb->tag_mask = 0;
3768 dcb->max_command = 1;
3769 dcb->target_id = target;
3770 dcb->target_lun = lun;
3771 #ifndef DC395x_NO_DISCONNECT
3772 dcb->identify_msg =
3773 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
3774 #else
3775 dcb->identify_msg = IDENTIFY(0, lun);
3776 #endif
3777 dcb->dev_mode = eeprom->target[target].cfg0;
3778 dcb->inquiry7 = 0;
3779 dcb->sync_mode = 0;
3780 dcb->min_nego_period = clock_period[period_index];
3781 dcb->sync_period = 0;
3782 dcb->sync_offset = 0;
3783 dcb->flag = 0;
3785 #ifndef DC395x_NO_WIDE
3786 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
3787 && (acb->config & HCC_WIDE_CARD))
3788 dcb->sync_mode |= WIDE_NEGO_ENABLE;
3789 #endif
3790 #ifndef DC395x_NO_SYNC
3791 if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
3792 if (!(lun) || current_sync_offset)
3793 dcb->sync_mode |= SYNC_NEGO_ENABLE;
3794 #endif
3795 if (dcb->target_lun != 0) {
3796 /* Copy settings */
3797 struct DeviceCtlBlk *p;
3798 list_for_each_entry(p, &acb->dcb_list, list)
3799 if (p->target_id == dcb->target_id)
3800 break;
3801 dprintkdbg(DBG_1,
3802 "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3803 dcb->target_id, dcb->target_lun,
3804 p->target_id, p->target_lun);
3805 dcb->sync_mode = p->sync_mode;
3806 dcb->sync_period = p->sync_period;
3807 dcb->min_nego_period = p->min_nego_period;
3808 dcb->sync_offset = p->sync_offset;
3809 dcb->inquiry7 = p->inquiry7;
3811 return dcb;
3816 * adapter_add_device - Adds the device instance to the adaptor instance.
3818 * @acb: The adapter device to be updated
3819 * @dcb: A newly created and intialised device instance to add.
3821 static void adapter_add_device(struct AdapterCtlBlk *acb,
3822 struct DeviceCtlBlk *dcb)
3824 /* backpointer to adapter */
3825 dcb->acb = acb;
3827 /* set run_robin to this device if it is currently empty */
3828 if (list_empty(&acb->dcb_list))
3829 acb->dcb_run_robin = dcb;
3831 /* add device to list */
3832 list_add_tail(&dcb->list, &acb->dcb_list);
3834 /* update device maps */
3835 acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
3836 acb->children[dcb->target_id][dcb->target_lun] = dcb;
3841 * adapter_remove_device - Removes the device instance from the adaptor
3842 * instance. The device instance is not check in any way or freed by this.
3843 * The caller is expected to take care of that. This will simply remove the
3844 * device from the adapters data strcutures.
3846 * @acb: The adapter device to be updated
3847 * @dcb: A device that has previously been added to the adapter.
3849 static void adapter_remove_device(struct AdapterCtlBlk *acb,
3850 struct DeviceCtlBlk *dcb)
3852 struct DeviceCtlBlk *i;
3853 struct DeviceCtlBlk *tmp;
3854 dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
3855 dcb->target_id, dcb->target_lun);
3857 /* fix up any pointers to this device that we have in the adapter */
3858 if (acb->active_dcb == dcb)
3859 acb->active_dcb = NULL;
3860 if (acb->dcb_run_robin == dcb)
3861 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
3863 /* unlink from list */
3864 list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
3865 if (dcb == i) {
3866 list_del(&i->list);
3867 break;
3870 /* clear map and children */
3871 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
3872 acb->children[dcb->target_id][dcb->target_lun] = NULL;
3873 dcb->acb = NULL;
3878 * adapter_remove_and_free_device - Removes a single device from the adapter
3879 * and then frees the device information.
3881 * @acb: The adapter device to be updated
3882 * @dcb: A device that has previously been added to the adapter.
3884 static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
3885 struct DeviceCtlBlk *dcb)
3887 if (list_size(&dcb->srb_going_list) > 1) {
3888 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
3889 "Won't remove because of %i active requests.\n",
3890 dcb->target_id, dcb->target_lun,
3891 list_size(&dcb->srb_going_list));
3892 return;
3894 adapter_remove_device(acb, dcb);
3895 kfree(dcb);
3900 * adapter_remove_and_free_all_devices - Removes and frees all of the
3901 * devices associated with the specified adapter.
3903 * @acb: The adapter from which all devices should be removed.
3905 static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
3907 struct DeviceCtlBlk *dcb;
3908 struct DeviceCtlBlk *tmp;
3909 dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
3910 list_size(&acb->dcb_list));
3912 list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
3913 adapter_remove_and_free_device(acb, dcb);
3918 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3919 * scsi device that we need to deal with. We allocate a new device and then
3920 * insert that device into the adapters device list.
3922 * @scsi_device: The new scsi device that we need to handle.
3924 static int dc395x_slave_alloc(struct scsi_device *scsi_device)
3926 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3927 struct DeviceCtlBlk *dcb;
3929 dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
3930 if (!dcb)
3931 return -ENOMEM;
3932 adapter_add_device(acb, dcb);
3934 return 0;
3939 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3940 * device that is going away.
3942 * @scsi_device: The new scsi device that we need to handle.
3944 static void dc395x_slave_destroy(struct scsi_device *scsi_device)
3946 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3947 struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
3948 if (dcb)
3949 adapter_remove_and_free_device(acb, dcb);
3956 * trms1040_wait_30us: wait for 30 us
3958 * Waits for 30us (using the chip by the looks of it..)
3960 * @io_port: base I/O address
3962 static void __devinit trms1040_wait_30us(unsigned long io_port)
3964 /* ScsiPortStallExecution(30); wait 30 us */
3965 outb(5, io_port + TRM_S1040_GEN_TIMER);
3966 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
3967 /* nothing */ ;
3972 * trms1040_write_cmd - write the secified command and address to
3973 * chip
3975 * @io_port: base I/O address
3976 * @cmd: SB + op code (command) to send
3977 * @addr: address to send
3979 static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
3981 int i;
3982 u8 send_data;
3984 /* program SB + OP code */
3985 for (i = 0; i < 3; i++, cmd <<= 1) {
3986 send_data = NVR_SELECT;
3987 if (cmd & 0x04) /* Start from bit 2 */
3988 send_data |= NVR_BITOUT;
3990 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3991 trms1040_wait_30us(io_port);
3992 outb((send_data | NVR_CLOCK),
3993 io_port + TRM_S1040_GEN_NVRAM);
3994 trms1040_wait_30us(io_port);
3997 /* send address */
3998 for (i = 0; i < 7; i++, addr <<= 1) {
3999 send_data = NVR_SELECT;
4000 if (addr & 0x40) /* Start from bit 6 */
4001 send_data |= NVR_BITOUT;
4003 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4004 trms1040_wait_30us(io_port);
4005 outb((send_data | NVR_CLOCK),
4006 io_port + TRM_S1040_GEN_NVRAM);
4007 trms1040_wait_30us(io_port);
4009 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4010 trms1040_wait_30us(io_port);
4015 * trms1040_set_data - store a single byte in the eeprom
4017 * Called from write all to write a single byte into the SSEEPROM
4018 * Which is done one bit at a time.
4020 * @io_port: base I/O address
4021 * @addr: offset into EEPROM
4022 * @byte: bytes to write
4024 static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
4026 int i;
4027 u8 send_data;
4029 /* Send write command & address */
4030 trms1040_write_cmd(io_port, 0x05, addr);
4032 /* Write data */
4033 for (i = 0; i < 8; i++, byte <<= 1) {
4034 send_data = NVR_SELECT;
4035 if (byte & 0x80) /* Start from bit 7 */
4036 send_data |= NVR_BITOUT;
4038 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4039 trms1040_wait_30us(io_port);
4040 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4041 trms1040_wait_30us(io_port);
4043 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4044 trms1040_wait_30us(io_port);
4046 /* Disable chip select */
4047 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4048 trms1040_wait_30us(io_port);
4050 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4051 trms1040_wait_30us(io_port);
4053 /* Wait for write ready */
4054 while (1) {
4055 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4056 trms1040_wait_30us(io_port);
4058 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4059 trms1040_wait_30us(io_port);
4061 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
4062 break;
4065 /* Disable chip select */
4066 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4071 * trms1040_write_all - write 128 bytes to the eeprom
4073 * Write the supplied 128 bytes to the chips SEEPROM
4075 * @eeprom: the data to write
4076 * @io_port: the base io port
4078 static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
4080 u8 *b_eeprom = (u8 *)eeprom;
4081 u8 addr;
4083 /* Enable SEEPROM */
4084 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4085 io_port + TRM_S1040_GEN_CONTROL);
4087 /* write enable */
4088 trms1040_write_cmd(io_port, 0x04, 0xFF);
4089 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4090 trms1040_wait_30us(io_port);
4092 /* write */
4093 for (addr = 0; addr < 128; addr++, b_eeprom++)
4094 trms1040_set_data(io_port, addr, *b_eeprom);
4096 /* write disable */
4097 trms1040_write_cmd(io_port, 0x04, 0x00);
4098 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4099 trms1040_wait_30us(io_port);
4101 /* Disable SEEPROM */
4102 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4103 io_port + TRM_S1040_GEN_CONTROL);
4108 * trms1040_get_data - get a single byte from the eeprom
4110 * Called from read all to read a single byte into the SSEEPROM
4111 * Which is done one bit at a time.
4113 * @io_port: base I/O address
4114 * @addr: offset into SEEPROM
4116 * Returns the the byte read.
4118 static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
4120 int i;
4121 u8 read_byte;
4122 u8 result = 0;
4124 /* Send read command & address */
4125 trms1040_write_cmd(io_port, 0x06, addr);
4127 /* read data */
4128 for (i = 0; i < 8; i++) {
4129 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4130 trms1040_wait_30us(io_port);
4131 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4133 /* Get data bit while falling edge */
4134 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
4135 result <<= 1;
4136 if (read_byte & NVR_BITIN)
4137 result |= 1;
4139 trms1040_wait_30us(io_port);
4142 /* Disable chip select */
4143 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4144 return result;
4149 * trms1040_read_all - read all bytes from the eeprom
4151 * Read the 128 bytes from the SEEPROM.
4153 * @eeprom: where to store the data
4154 * @io_port: the base io port
4156 static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
4158 u8 *b_eeprom = (u8 *)eeprom;
4159 u8 addr;
4161 /* Enable SEEPROM */
4162 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4163 io_port + TRM_S1040_GEN_CONTROL);
4165 /* read details */
4166 for (addr = 0; addr < 128; addr++, b_eeprom++)
4167 *b_eeprom = trms1040_get_data(io_port, addr);
4169 /* Disable SEEPROM */
4170 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4171 io_port + TRM_S1040_GEN_CONTROL);
4177 * check_eeprom - get and check contents of the eeprom
4179 * Read seeprom 128 bytes into the memory provider in eeprom.
4180 * Checks the checksum and if it's not correct it uses a set of default
4181 * values.
4183 * @eeprom: caller allocated strcuture to read the eeprom data into
4184 * @io_port: io port to read from
4186 static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
4188 u16 *w_eeprom = (u16 *)eeprom;
4189 u16 w_addr;
4190 u16 cksum;
4191 u32 d_addr;
4192 u32 *d_eeprom;
4194 trms1040_read_all(eeprom, io_port); /* read eeprom */
4196 cksum = 0;
4197 for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
4198 w_addr++, w_eeprom++)
4199 cksum += *w_eeprom;
4200 if (cksum != 0x1234) {
4202 * Checksum is wrong.
4203 * Load a set of defaults into the eeprom buffer
4205 dprintkl(KERN_WARNING,
4206 "EEProm checksum error: using default values and options.\n");
4207 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4208 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4209 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4210 eeprom->sub_sys_id[1] =
4211 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4212 eeprom->sub_class = 0x00;
4213 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4214 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4215 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4216 eeprom->device_id[1] =
4217 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4218 eeprom->reserved = 0x00;
4220 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
4221 d_addr < 16; d_addr++, d_eeprom++)
4222 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
4224 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
4225 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
4226 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
4227 *d_eeprom = 0x00;
4229 /* Now load defaults (maybe set by boot/module params) */
4230 set_safe_settings();
4231 fix_settings();
4232 eeprom_override(eeprom);
4234 eeprom->cksum = 0x00;
4235 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
4236 w_addr < 63; w_addr++, w_eeprom++)
4237 cksum += *w_eeprom;
4239 *w_eeprom = 0x1234 - cksum;
4240 trms1040_write_all(eeprom, io_port);
4241 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
4242 } else {
4243 set_safe_settings();
4244 eeprom_index_to_delay(eeprom);
4245 eeprom_override(eeprom);
4251 * print_eeprom_settings - output the eeprom settings
4252 * to the kernel log so people can see what they were.
4254 * @eeprom: The eeprom data strucutre to show details for.
4256 static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
4258 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4259 eeprom->scsi_id,
4260 eeprom->target[0].period,
4261 clock_speed[eeprom->target[0].period] / 10,
4262 clock_speed[eeprom->target[0].period] % 10,
4263 eeprom->target[0].cfg0);
4264 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4265 eeprom->channel_cfg, eeprom->max_tag,
4266 1 << eeprom->max_tag, eeprom->delay_time);
4270 /* Free SG tables */
4271 static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
4273 int i;
4274 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4276 for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
4277 kfree(acb->srb_array[i].segment_x);
4282 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4283 * should never cross a page boundary */
4284 static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
4286 const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
4287 *SEGMENTX_LEN;
4288 int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
4289 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4290 int srb_idx = 0;
4291 unsigned i = 0;
4292 struct SGentry *ptr;
4294 for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
4295 acb->srb_array[i].segment_x = NULL;
4297 dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
4298 while (pages--) {
4299 ptr = (struct SGentry *)kmalloc(PAGE_SIZE, GFP_KERNEL);
4300 if (!ptr) {
4301 adapter_sg_tables_free(acb);
4302 return 1;
4304 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
4305 PAGE_SIZE, ptr, srb_idx);
4306 i = 0;
4307 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4308 acb->srb_array[srb_idx++].segment_x =
4309 ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4311 if (i < srbs_per_page)
4312 acb->srb.segment_x =
4313 ptr + (i * DC395x_MAX_SG_LISTENTRY);
4314 else
4315 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
4316 return 0;
4322 * adapter_print_config - print adapter connection and termination
4323 * config
4325 * The io port in the adapter needs to have been set before calling
4326 * this function.
4328 * @acb: The adapter to print the information for.
4330 static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
4332 u8 bval;
4334 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
4335 dprintkl(KERN_INFO, "%sConnectors: ",
4336 ((bval & WIDESCSI) ? "(Wide) " : ""));
4337 if (!(bval & CON5068))
4338 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
4339 if (!(bval & CON68))
4340 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
4341 if (!(bval & CON50))
4342 printk("int50 ");
4343 if ((bval & (CON5068 | CON50 | CON68)) ==
4344 0 /*(CON5068 | CON50 | CON68) */ )
4345 printk(" Oops! (All 3?) ");
4346 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
4347 printk(" Termination: ");
4348 if (bval & DIS_TERM)
4349 printk("Disabled\n");
4350 else {
4351 if (bval & AUTOTERM)
4352 printk("Auto ");
4353 if (bval & LOW8TERM)
4354 printk("Low ");
4355 if (bval & UP8TERM)
4356 printk("High ");
4357 printk("\n");
4363 * adapter_init_params - Initialize the various parameters in the
4364 * adapter structure. Note that the pointer to the scsi_host is set
4365 * early (when this instance is created) and the io_port and irq
4366 * values are set later after they have been reserved. This just gets
4367 * everything set to a good starting position.
4369 * The eeprom structure in the adapter needs to have been set before
4370 * calling this function.
4372 * @acb: The adapter to initialize.
4374 static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
4376 struct NvRamType *eeprom = &acb->eeprom;
4377 int i;
4379 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4380 /* NOTE: acb->io_port_base is set at port registration time */
4381 /* NOTE: acb->io_port_len is set at port registration time */
4383 INIT_LIST_HEAD(&acb->dcb_list);
4384 acb->dcb_run_robin = NULL;
4385 acb->active_dcb = NULL;
4387 INIT_LIST_HEAD(&acb->srb_free_list);
4388 /* temp SRB for Q tag used or abort command used */
4389 acb->tmp_srb = &acb->srb;
4390 init_timer(&acb->waiting_timer);
4391 init_timer(&acb->selto_timer);
4393 acb->srb_count = DC395x_MAX_SRB_CNT;
4395 acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */
4396 /* NOTE: acb->irq_level is set at IRQ registration time */
4398 acb->tag_max_num = 1 << eeprom->max_tag;
4399 if (acb->tag_max_num > 30)
4400 acb->tag_max_num = 30;
4402 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4403 acb->gmode2 = eeprom->channel_cfg;
4404 acb->config = 0; /* NOTE: actually set in adapter_init_chip */
4406 if (eeprom->channel_cfg & NAC_SCANLUN)
4407 acb->lun_chk = 1;
4408 acb->scan_devices = 1;
4410 acb->scsi_host->this_id = eeprom->scsi_id;
4411 acb->hostid_bit = (1 << acb->scsi_host->this_id);
4413 for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
4414 acb->dcb_map[i] = 0;
4416 acb->msg_len = 0;
4418 /* link static array of srbs into the srb free list */
4419 for (i = 0; i < acb->srb_count - 1; i++)
4420 srb_free_insert(acb, &acb->srb_array[i]);
4425 * adapter_init_host - Initialize the scsi host instance based on
4426 * values that we have already stored in the adapter instance. There's
4427 * some mention that a lot of these are deprecated, so we won't use
4428 * them (we'll use the ones in the adapter instance) but we'll fill
4429 * them in in case something else needs them.
4431 * The eeprom structure, irq and io ports in the adapter need to have
4432 * been set before calling this function.
4434 * @host: The scsi host instance to fill in the values for.
4436 static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
4438 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4439 struct NvRamType *eeprom = &acb->eeprom;
4441 host->max_cmd_len = 24;
4442 host->can_queue = DC395x_MAX_CMD_QUEUE;
4443 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4444 host->this_id = (int)eeprom->scsi_id;
4445 host->io_port = acb->io_port_base;
4446 host->n_io_port = acb->io_port_len;
4447 host->dma_channel = -1;
4448 host->unique_id = acb->io_port_base;
4449 host->irq = acb->irq_level;
4450 host->last_reset = jiffies;
4452 host->max_id = 16;
4453 if (host->max_id - 1 == eeprom->scsi_id)
4454 host->max_id--;
4456 #ifdef CONFIG_SCSI_MULTI_LUN
4457 if (eeprom->channel_cfg & NAC_SCANLUN)
4458 host->max_lun = 8;
4459 else
4460 host->max_lun = 1;
4461 #else
4462 host->max_lun = 1;
4463 #endif
4469 * adapter_init_chip - Get the chip into a know state and figure out
4470 * some of the settings that apply to this adapter.
4472 * The io port in the adapter needs to have been set before calling
4473 * this function. The config will be configured correctly on return.
4475 * @acb: The adapter which we are to init.
4477 static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
4479 struct NvRamType *eeprom = &acb->eeprom;
4481 /* Mask all the interrupt */
4482 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
4483 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
4485 /* Reset SCSI module */
4486 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4488 /* Reset PCI/DMA module */
4489 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4490 udelay(20);
4492 /* program configuration 0 */
4493 acb->config = HCC_AUTOTERM | HCC_PARITY;
4494 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
4495 acb->config |= HCC_WIDE_CARD;
4497 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
4498 acb->config |= HCC_SCSI_RESET;
4500 if (acb->config & HCC_SCSI_RESET) {
4501 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
4502 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4504 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4505 /*spin_unlock_irq (&io_request_lock); */
4506 udelay(500);
4508 acb->scsi_host->last_reset =
4509 jiffies + HZ / 2 +
4510 HZ * acb->eeprom.delay_time;
4512 /*spin_lock_irq (&io_request_lock); */
4518 * init_adapter - Grab the resource for the card, setup the adapter
4519 * information, set the card into a known state, create the various
4520 * tables etc etc. This basically gets all adapter information all up
4521 * to date, intialised and gets the chip in sync with it.
4523 * @host: This hosts adapter structure
4524 * @io_port: The base I/O port
4525 * @irq: IRQ
4527 * Returns 0 if the initialization succeeds, any other value on
4528 * failure.
4530 static int __devinit adapter_init(struct AdapterCtlBlk *acb,
4531 unsigned long io_port, u32 io_port_len, unsigned int irq)
4533 if (!request_region(io_port, io_port_len, DC395X_NAME)) {
4534 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
4535 goto failed;
4537 /* store port base to indicate we have registered it */
4538 acb->io_port_base = io_port;
4539 acb->io_port_len = io_port_len;
4541 if (request_irq(irq, dc395x_interrupt, SA_SHIRQ, DC395X_NAME, acb)) {
4542 /* release the region we just claimed */
4543 dprintkl(KERN_INFO, "Failed to register IRQ\n");
4544 goto failed;
4546 /* store irq to indicate we have registered it */
4547 acb->irq_level = irq;
4549 /* get eeprom configuration information and command line settings etc */
4550 check_eeprom(&acb->eeprom, io_port);
4551 print_eeprom_settings(&acb->eeprom);
4553 /* setup adapter control block */
4554 adapter_init_params(acb);
4556 /* display card connectors/termination settings */
4557 adapter_print_config(acb);
4559 if (adapter_sg_tables_alloc(acb)) {
4560 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
4561 goto failed;
4563 adapter_init_scsi_host(acb->scsi_host);
4564 adapter_init_chip(acb);
4565 set_basic_config(acb);
4567 dprintkdbg(DBG_0,
4568 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4569 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4570 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
4571 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
4572 return 0;
4574 failed:
4575 if (acb->irq_level)
4576 free_irq(acb->irq_level, acb);
4577 if (acb->io_port_base)
4578 release_region(acb->io_port_base, acb->io_port_len);
4579 adapter_sg_tables_free(acb);
4581 return 1;
4586 * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4587 * stopping all operations and disabling interrupt generation on the
4588 * card.
4590 * @acb: The adapter which we are to shutdown.
4592 static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
4594 /* disable interrupts */
4595 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
4596 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
4598 /* reset the scsi bus */
4599 if (acb->config & HCC_SCSI_RESET)
4600 reset_scsi_bus(acb);
4602 /* clear any pending interupt state */
4603 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4609 * adapter_uninit - Shut down the chip and release any resources that
4610 * we had allocated. Once this returns the adapter should not be used
4611 * anymore.
4613 * @acb: The adapter which we are to un-initialize.
4615 static void adapter_uninit(struct AdapterCtlBlk *acb)
4617 unsigned long flags;
4618 DC395x_LOCK_IO(acb->scsi_host, flags);
4620 /* remove timers */
4621 if (timer_pending(&acb->waiting_timer))
4622 del_timer(&acb->waiting_timer);
4623 if (timer_pending(&acb->selto_timer))
4624 del_timer(&acb->selto_timer);
4626 adapter_uninit_chip(acb);
4627 adapter_remove_and_free_all_devices(acb);
4628 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4630 if (acb->irq_level)
4631 free_irq(acb->irq_level, acb);
4632 if (acb->io_port_base)
4633 release_region(acb->io_port_base, acb->io_port_len);
4635 adapter_sg_tables_free(acb);
4639 #undef SPRINTF
4640 #define SPRINTF(args...) pos += sprintf(pos, args)
4642 #undef YESNO
4643 #define YESNO(YN) \
4644 if (YN) SPRINTF(" Yes ");\
4645 else SPRINTF(" No ")
4647 static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
4648 char **start, off_t offset, int length, int inout)
4650 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4651 int spd, spd1;
4652 char *pos = buffer;
4653 struct DeviceCtlBlk *dcb;
4654 unsigned long flags;
4655 int dev;
4657 if (inout) /* Has data been written to the file ? */
4658 return -EPERM;
4660 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
4661 SPRINTF(" Driver Version " DC395X_VERSION "\n");
4663 DC395x_LOCK_IO(acb->scsi_host, flags);
4665 SPRINTF("SCSI Host Nr %i, ", host->host_no);
4666 SPRINTF("DC395U/UW/F DC315/U %s\n",
4667 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
4668 SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
4669 SPRINTF("irq_level 0x%04x, ", acb->irq_level);
4670 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
4672 SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
4673 SPRINTF("AdapterID %i\n", host->this_id);
4675 SPRINTF("tag_max_num %i", acb->tag_max_num);
4676 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4677 SPRINTF(", FilterCfg 0x%02x",
4678 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
4679 SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
4680 /*SPRINTF("\n"); */
4682 SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4683 SPRINTF
4684 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
4685 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
4686 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
4687 acb->dcb_map[6], acb->dcb_map[7]);
4688 SPRINTF
4689 (" %02x %02x %02x %02x %02x %02x %02x %02x\n",
4690 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
4691 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
4692 acb->dcb_map[14], acb->dcb_map[15]);
4694 SPRINTF
4695 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4697 dev = 0;
4698 list_for_each_entry(dcb, &acb->dcb_list, list) {
4699 int nego_period;
4700 SPRINTF("%02i %02i %02i ", dev, dcb->target_id,
4701 dcb->target_lun);
4702 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
4703 YESNO(dcb->sync_offset);
4704 YESNO(dcb->sync_period & WIDE_SYNC);
4705 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
4706 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
4707 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
4708 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
4709 if (dcb->sync_offset)
4710 SPRINTF(" %03i ns ", nego_period);
4711 else
4712 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
4714 if (dcb->sync_offset & 0x0f) {
4715 spd = 1000 / (nego_period);
4716 spd1 = 1000 % (nego_period);
4717 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
4718 SPRINTF(" %2i.%1i M %02i ", spd, spd1,
4719 (dcb->sync_offset & 0x0f));
4720 } else
4721 SPRINTF(" ");
4723 /* Add more info ... */
4724 SPRINTF(" %02i\n", dcb->max_command);
4725 dev++;
4728 if (timer_pending(&acb->waiting_timer))
4729 SPRINTF("Waiting queue timer running\n");
4730 else
4731 SPRINTF("\n");
4733 list_for_each_entry(dcb, &acb->dcb_list, list) {
4734 struct ScsiReqBlk *srb;
4735 if (!list_empty(&dcb->srb_waiting_list))
4736 SPRINTF("DCB (%02i-%i): Waiting: %i:",
4737 dcb->target_id, dcb->target_lun,
4738 list_size(&dcb->srb_waiting_list));
4739 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
4740 SPRINTF(" %li", srb->cmd->pid);
4741 if (!list_empty(&dcb->srb_going_list))
4742 SPRINTF("\nDCB (%02i-%i): Going : %i:",
4743 dcb->target_id, dcb->target_lun,
4744 list_size(&dcb->srb_going_list));
4745 list_for_each_entry(srb, &dcb->srb_going_list, list)
4746 SPRINTF(" %li", srb->cmd->pid);
4747 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4748 SPRINTF("\n");
4751 if (debug_enabled(DBG_1)) {
4752 SPRINTF("DCB list for ACB %p:\n", acb);
4753 list_for_each_entry(dcb, &acb->dcb_list, list) {
4754 SPRINTF("%p -> ", dcb);
4756 SPRINTF("END\n");
4759 *start = buffer + offset;
4760 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4762 if (pos - buffer < offset)
4763 return 0;
4764 else if (pos - buffer - offset < length)
4765 return pos - buffer - offset;
4766 else
4767 return length;
4771 static struct scsi_host_template dc395x_driver_template = {
4772 .module = THIS_MODULE,
4773 .proc_name = DC395X_NAME,
4774 .proc_info = dc395x_proc_info,
4775 .name = DC395X_BANNER " " DC395X_VERSION,
4776 .queuecommand = dc395x_queue_command,
4777 .bios_param = dc395x_bios_param,
4778 .slave_alloc = dc395x_slave_alloc,
4779 .slave_destroy = dc395x_slave_destroy,
4780 .can_queue = DC395x_MAX_CAN_QUEUE,
4781 .this_id = 7,
4782 .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
4783 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
4784 .eh_abort_handler = dc395x_eh_abort,
4785 .eh_bus_reset_handler = dc395x_eh_bus_reset,
4786 .unchecked_isa_dma = 0,
4787 .use_clustering = DISABLE_CLUSTERING,
4792 * banner_display - Display banner on first instance of driver
4793 * initialized.
4795 static void banner_display(void)
4797 static int banner_done = 0;
4798 if (!banner_done)
4800 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
4801 banner_done = 1;
4807 * dc395x_init_one - Initialise a single instance of the adapter.
4809 * The PCI layer will call this once for each instance of the adapter
4810 * that it finds in the system. The pci_dev strcuture indicates which
4811 * instance we are being called from.
4813 * @dev: The PCI device to intialize.
4814 * @id: Looks like a pointer to the entry in our pci device table
4815 * that was actually matched by the PCI subsystem.
4817 * Returns 0 on success, or an error code (-ve) on failure.
4819 static int __devinit dc395x_init_one(struct pci_dev *dev,
4820 const struct pci_device_id *id)
4822 struct Scsi_Host *scsi_host = NULL;
4823 struct AdapterCtlBlk *acb = NULL;
4824 unsigned long io_port_base;
4825 unsigned int io_port_len;
4826 unsigned int irq;
4828 dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
4829 banner_display();
4831 if (pci_enable_device(dev))
4833 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
4834 return -ENODEV;
4836 io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
4837 io_port_len = pci_resource_len(dev, 0);
4838 irq = dev->irq;
4839 dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
4841 /* allocate scsi host information (includes out adapter) */
4842 scsi_host = scsi_host_alloc(&dc395x_driver_template,
4843 sizeof(struct AdapterCtlBlk));
4844 if (!scsi_host) {
4845 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
4846 goto fail;
4848 acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
4849 acb->scsi_host = scsi_host;
4850 acb->dev = dev;
4852 /* initialise the adapter and everything we need */
4853 if (adapter_init(acb, io_port_base, io_port_len, irq)) {
4854 dprintkl(KERN_INFO, "adapter init failed\n");
4855 goto fail;
4858 pci_set_master(dev);
4860 /* get the scsi mid level to scan for new devices on the bus */
4861 if (scsi_add_host(scsi_host, &dev->dev)) {
4862 dprintkl(KERN_ERR, "scsi_add_host failed\n");
4863 goto fail;
4865 pci_set_drvdata(dev, scsi_host);
4866 scsi_scan_host(scsi_host);
4868 return 0;
4870 fail:
4871 if (acb != NULL)
4872 adapter_uninit(acb);
4873 if (scsi_host != NULL)
4874 scsi_host_put(scsi_host);
4875 pci_disable_device(dev);
4876 return -ENODEV;
4881 * dc395x_remove_one - Called to remove a single instance of the
4882 * adapter.
4884 * @dev: The PCI device to intialize.
4886 static void __devexit dc395x_remove_one(struct pci_dev *dev)
4888 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
4889 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
4891 dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
4893 scsi_remove_host(scsi_host);
4894 adapter_uninit(acb);
4895 pci_disable_device(dev);
4896 scsi_host_put(scsi_host);
4897 pci_set_drvdata(dev, NULL);
4901 static struct pci_device_id dc395x_pci_table[] = {
4903 .vendor = PCI_VENDOR_ID_TEKRAM,
4904 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
4905 .subvendor = PCI_ANY_ID,
4906 .subdevice = PCI_ANY_ID,
4908 {} /* Terminating entry */
4910 MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
4913 static struct pci_driver dc395x_driver = {
4914 .name = DC395X_NAME,
4915 .id_table = dc395x_pci_table,
4916 .probe = dc395x_init_one,
4917 .remove = __devexit_p(dc395x_remove_one),
4922 * dc395x_module_init - Module initialization function
4924 * Used by both module and built-in driver to initialise this driver.
4926 static int __init dc395x_module_init(void)
4928 return pci_module_init(&dc395x_driver);
4933 * dc395x_module_exit - Module cleanup function.
4935 static void __exit dc395x_module_exit(void)
4937 pci_unregister_driver(&dc395x_driver);
4941 module_init(dc395x_module_init);
4942 module_exit(dc395x_module_exit);
4944 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4945 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4946 MODULE_LICENSE("GPL");