GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / dc395x.c
blob11d4f36318c6c76f9380acc6f6230520ec0711a8
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 <linux/slab.h>
61 #include <asm/io.h>
63 #include <scsi/scsi.h>
64 #include <scsi/scsicam.h> /* needed for scsicam_bios_param */
65 #include <scsi/scsi_cmnd.h>
66 #include <scsi/scsi_device.h>
67 #include <scsi/scsi_host.h>
69 #include "dc395x.h"
71 #define DC395X_NAME "dc395x"
72 #define DC395X_BANNER "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
73 #define DC395X_VERSION "v2.05, 2004/03/08"
75 /*---------------------------------------------------------------------------
76 Features
77 ---------------------------------------------------------------------------*/
79 * Set to disable parts of the driver
81 /*#define DC395x_NO_DISCONNECT*/
82 /*#define DC395x_NO_TAGQ*/
83 /*#define DC395x_NO_SYNC*/
84 /*#define DC395x_NO_WIDE*/
86 /*---------------------------------------------------------------------------
87 Debugging
88 ---------------------------------------------------------------------------*/
90 * Types of debugging that can be enabled and disabled
92 #define DBG_KG 0x0001
93 #define DBG_0 0x0002
94 #define DBG_1 0x0004
95 #define DBG_SG 0x0020
96 #define DBG_FIFO 0x0040
97 #define DBG_PIO 0x0080
101 * Set set of things to output debugging for.
102 * Undefine to remove all debugging
104 /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
105 /*#define DEBUG_MASK DBG_0*/
109 * Output a kernel mesage at the specified level and append the
110 * driver name and a ": " to the start of the message
112 #define dprintkl(level, format, arg...) \
113 printk(level DC395X_NAME ": " format , ## arg)
116 #ifdef DEBUG_MASK
118 * print a debug message - this is formated with KERN_DEBUG, then the
119 * driver name followed by a ": " and then the message is output.
120 * This also checks that the specified debug level is enabled before
121 * outputing the message
123 #define dprintkdbg(type, format, arg...) \
124 do { \
125 if ((type) & (DEBUG_MASK)) \
126 dprintkl(KERN_DEBUG , format , ## arg); \
127 } while (0)
130 * Check if the specified type of debugging is enabled
132 #define debug_enabled(type) ((DEBUG_MASK) & (type))
134 #else
136 * No debugging. Do nothing
138 #define dprintkdbg(type, format, arg...) \
139 do {} while (0)
140 #define debug_enabled(type) (0)
142 #endif
145 #ifndef PCI_VENDOR_ID_TEKRAM
146 #define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */
147 #endif
148 #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
149 #define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */
150 #endif
153 #define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
154 #define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
156 #define DC395x_read8(acb,address) (u8)(inb(acb->io_port_base + (address)))
157 #define DC395x_read16(acb,address) (u16)(inw(acb->io_port_base + (address)))
158 #define DC395x_read32(acb,address) (u32)(inl(acb->io_port_base + (address)))
159 #define DC395x_write8(acb,address,value) outb((value), acb->io_port_base + (address))
160 #define DC395x_write16(acb,address,value) outw((value), acb->io_port_base + (address))
161 #define DC395x_write32(acb,address,value) outl((value), acb->io_port_base + (address))
163 /* cmd->result */
164 #define RES_TARGET 0x000000FF /* Target State */
165 #define RES_TARGET_LNX STATUS_MASK /* Only official ... */
166 #define RES_ENDMSG 0x0000FF00 /* End Message */
167 #define RES_DID 0x00FF0000 /* DID_ codes */
168 #define RES_DRV 0xFF000000 /* DRIVER_ codes */
170 #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
171 #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
173 #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
174 #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
175 #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
176 #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
177 #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
179 #define TAG_NONE 255
182 * srb->segement_x is the hw sg list. It is always allocated as a
183 * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
184 * cross a page boundy.
186 #define SEGMENTX_LEN (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
189 struct SGentry {
190 u32 address; /* bus! address */
191 u32 length;
194 /* The SEEPROM structure for TRM_S1040 */
195 struct NVRamTarget {
196 u8 cfg0; /* Target configuration byte 0 */
197 u8 period; /* Target period */
198 u8 cfg2; /* Target configuration byte 2 */
199 u8 cfg3; /* Target configuration byte 3 */
202 struct NvRamType {
203 u8 sub_vendor_id[2]; /* 0,1 Sub Vendor ID */
204 u8 sub_sys_id[2]; /* 2,3 Sub System ID */
205 u8 sub_class; /* 4 Sub Class */
206 u8 vendor_id[2]; /* 5,6 Vendor ID */
207 u8 device_id[2]; /* 7,8 Device ID */
208 u8 reserved; /* 9 Reserved */
209 struct NVRamTarget target[DC395x_MAX_SCSI_ID];
210 /** 10,11,12,13
211 ** 14,15,16,17
212 ** ....
213 ** ....
214 ** 70,71,72,73
216 u8 scsi_id; /* 74 Host Adapter SCSI ID */
217 u8 channel_cfg; /* 75 Channel configuration */
218 u8 delay_time; /* 76 Power on delay time */
219 u8 max_tag; /* 77 Maximum tags */
220 u8 reserved0; /* 78 */
221 u8 boot_target; /* 79 */
222 u8 boot_lun; /* 80 */
223 u8 reserved1; /* 81 */
224 u16 reserved2[22]; /* 82,..125 */
225 u16 cksum; /* 126,127 */
228 struct ScsiReqBlk {
229 struct list_head list; /* next/prev ptrs for srb lists */
230 struct DeviceCtlBlk *dcb;
231 struct scsi_cmnd *cmd;
233 struct SGentry *segment_x; /* Linear array of hw sg entries (up to 64 entries) */
234 dma_addr_t sg_bus_addr; /* Bus address of sg list (ie, of segment_x) */
236 u8 sg_count; /* No of HW sg entries for this request */
237 u8 sg_index; /* Index of HW sg entry for this request */
238 size_t total_xfer_length; /* Total number of bytes remaining to be transfered */
239 size_t request_length; /* Total number of bytes in this request */
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 size_t xferred; /* Saved copy of total_xfer_length */
251 u16 state;
253 u8 msgin_buf[6];
254 u8 msgout_buf[6];
256 u8 adapter_status;
257 u8 target_status;
258 u8 msg_count;
259 u8 end_message;
261 u8 tag_number;
262 u8 status;
263 u8 retry_count;
264 u8 flag;
266 u8 scsi_phase;
269 struct DeviceCtlBlk {
270 struct list_head list; /* next/prev ptrs for the dcb list */
271 struct AdapterCtlBlk *acb;
272 struct list_head srb_going_list; /* head of going srb list */
273 struct list_head srb_waiting_list; /* head of waiting srb list */
275 struct ScsiReqBlk *active_srb;
276 u32 tag_mask;
278 u16 max_command;
280 u8 target_id; /* SCSI Target ID (SCSI Only) */
281 u8 target_lun; /* SCSI Log. Unit (SCSI Only) */
282 u8 identify_msg;
283 u8 dev_mode;
285 u8 inquiry7; /* To store Inquiry flags */
286 u8 sync_mode; /* 0:async mode */
287 u8 min_nego_period; /* for nego. */
288 u8 sync_period; /* for reg. */
290 u8 sync_offset; /* for reg. and nego.(low nibble) */
291 u8 flag;
292 u8 dev_type;
293 u8 init_tcq_flag;
296 struct AdapterCtlBlk {
297 struct Scsi_Host *scsi_host;
299 unsigned long io_port_base;
300 unsigned long io_port_len;
302 struct list_head dcb_list; /* head of going dcb list */
303 struct DeviceCtlBlk *dcb_run_robin;
304 struct DeviceCtlBlk *active_dcb;
306 struct list_head srb_free_list; /* head of free srb list */
307 struct ScsiReqBlk *tmp_srb;
308 struct timer_list waiting_timer;
309 struct timer_list selto_timer;
311 u16 srb_count;
313 u8 sel_timeout;
315 unsigned int irq_level;
316 u8 tag_max_num;
317 u8 acb_flag;
318 u8 gmode2;
320 u8 config;
321 u8 lun_chk;
322 u8 scan_devices;
323 u8 hostid_bit;
325 u8 dcb_map[DC395x_MAX_SCSI_ID];
326 struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
328 struct pci_dev *dev;
330 u8 msg_len;
332 struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
333 struct ScsiReqBlk srb;
335 struct NvRamType eeprom; /* eeprom settings for this adapter */
339 /*---------------------------------------------------------------------------
340 Forward declarations
341 ---------------------------------------------------------------------------*/
342 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
343 u16 *pscsi_status);
344 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
345 u16 *pscsi_status);
346 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
347 u16 *pscsi_status);
348 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
349 u16 *pscsi_status);
350 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
351 u16 *pscsi_status);
352 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
353 u16 *pscsi_status);
354 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
355 u16 *pscsi_status);
356 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
357 u16 *pscsi_status);
358 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
359 u16 *pscsi_status);
360 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
361 u16 *pscsi_status);
362 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
363 u16 *pscsi_status);
364 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
365 u16 *pscsi_status);
366 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
367 u16 *pscsi_status);
368 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
369 u16 *pscsi_status);
370 static void set_basic_config(struct AdapterCtlBlk *acb);
371 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
372 struct ScsiReqBlk *srb);
373 static void reset_scsi_bus(struct AdapterCtlBlk *acb);
374 static void data_io_transfer(struct AdapterCtlBlk *acb,
375 struct ScsiReqBlk *srb, u16 io_dir);
376 static void disconnect(struct AdapterCtlBlk *acb);
377 static void reselect(struct AdapterCtlBlk *acb);
378 static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
379 struct ScsiReqBlk *srb);
380 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
381 struct ScsiReqBlk *srb);
382 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
383 struct ScsiReqBlk *srb);
384 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
385 struct scsi_cmnd *cmd, u8 force);
386 static void scsi_reset_detect(struct AdapterCtlBlk *acb);
387 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
388 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
389 struct ScsiReqBlk *srb);
390 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
391 struct ScsiReqBlk *srb);
392 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
393 struct ScsiReqBlk *srb);
394 static void set_xfer_rate(struct AdapterCtlBlk *acb,
395 struct DeviceCtlBlk *dcb);
396 static void waiting_timeout(unsigned long ptr);
399 /*---------------------------------------------------------------------------
400 Static Data
401 ---------------------------------------------------------------------------*/
402 static u16 current_sync_offset = 0;
404 static void *dc395x_scsi_phase0[] = {
405 data_out_phase0,/* phase:0 */
406 data_in_phase0, /* phase:1 */
407 command_phase0, /* phase:2 */
408 status_phase0, /* phase:3 */
409 nop0, /* phase:4 PH_BUS_FREE .. initial phase */
410 nop0, /* phase:5 PH_BUS_FREE .. initial phase */
411 msgout_phase0, /* phase:6 */
412 msgin_phase0, /* phase:7 */
415 static void *dc395x_scsi_phase1[] = {
416 data_out_phase1,/* phase:0 */
417 data_in_phase1, /* phase:1 */
418 command_phase1, /* phase:2 */
419 status_phase1, /* phase:3 */
420 nop1, /* phase:4 PH_BUS_FREE .. initial phase */
421 nop1, /* phase:5 PH_BUS_FREE .. initial phase */
422 msgout_phase1, /* phase:6 */
423 msgin_phase1, /* phase:7 */
427 *Fast20: 000 50ns, 20.0 MHz
428 * 001 75ns, 13.3 MHz
429 * 010 100ns, 10.0 MHz
430 * 011 125ns, 8.0 MHz
431 * 100 150ns, 6.6 MHz
432 * 101 175ns, 5.7 MHz
433 * 110 200ns, 5.0 MHz
434 * 111 250ns, 4.0 MHz
436 *Fast40(LVDS): 000 25ns, 40.0 MHz
437 * 001 50ns, 20.0 MHz
438 * 010 75ns, 13.3 MHz
439 * 011 100ns, 10.0 MHz
440 * 100 125ns, 8.0 MHz
441 * 101 150ns, 6.6 MHz
442 * 110 175ns, 5.7 MHz
443 * 111 200ns, 5.0 MHz
445 /*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/
447 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
448 static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
449 static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
452 /*---------------------------------------------------------------------------
453 Configuration
454 ---------------------------------------------------------------------------*/
456 * Module/boot parameters currently effect *all* instances of the
457 * card in the system.
461 * Command line parameters are stored in a structure below.
462 * These are the index's into the structure for the various
463 * command line options.
465 #define CFG_ADAPTER_ID 0
466 #define CFG_MAX_SPEED 1
467 #define CFG_DEV_MODE 2
468 #define CFG_ADAPTER_MODE 3
469 #define CFG_TAGS 4
470 #define CFG_RESET_DELAY 5
472 #define CFG_NUM 6 /* number of configuration items */
476 * Value used to indicate that a command line override
477 * hasn't been used to modify the value.
479 #define CFG_PARAM_UNSET -1
483 * Hold command line parameters.
485 struct ParameterData {
486 int value; /* value of this setting */
487 int min; /* minimum value */
488 int max; /* maximum value */
489 int def; /* default value */
490 int safe; /* safe value */
492 static struct ParameterData __devinitdata cfg_data[] = {
493 { /* adapter id */
494 CFG_PARAM_UNSET,
500 { /* max speed */
501 CFG_PARAM_UNSET,
504 1, /* 13.3Mhz */
505 4, /* 6.7Hmz */
507 { /* dev mode */
508 CFG_PARAM_UNSET,
510 0x3f,
511 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
512 NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
513 NTC_DO_SEND_START,
514 NTC_DO_PARITY_CHK | NTC_DO_SEND_START
516 { /* adapter mode */
517 CFG_PARAM_UNSET,
519 0x2f,
520 #ifdef CONFIG_SCSI_MULTI_LUN
521 NAC_SCANLUN |
522 #endif
523 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
524 /*| NAC_ACTIVE_NEG*/,
525 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
527 { /* tags */
528 CFG_PARAM_UNSET,
531 3, /* 16 tags (??) */
534 { /* reset delay */
535 CFG_PARAM_UNSET,
537 180,
538 1, /* 1 second */
539 10, /* 10 seconds */
545 * Safe settings. If set to zero the BIOS/default values with
546 * command line overrides will be used. If set to 1 then safe and
547 * slow settings will be used.
549 static int use_safe_settings = 0;
550 module_param_named(safe, use_safe_settings, bool, 0);
551 MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
554 module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
555 MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
557 module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
558 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");
560 module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
561 MODULE_PARM_DESC(dev_mode, "Device mode.");
563 module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
564 MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
566 module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
567 MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
569 module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
570 MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
574 * set_safe_settings - if the use_safe_settings option is set then
575 * set all values to the safe and slow values.
577 static void __devinit set_safe_settings(void)
579 if (use_safe_settings)
581 int i;
583 dprintkl(KERN_INFO, "Using safe settings.\n");
584 for (i = 0; i < CFG_NUM; i++)
586 cfg_data[i].value = cfg_data[i].safe;
593 * fix_settings - reset any boot parameters which are out of range
594 * back to the default values.
596 static void __devinit fix_settings(void)
598 int i;
600 dprintkdbg(DBG_1,
601 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
602 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
603 cfg_data[CFG_ADAPTER_ID].value,
604 cfg_data[CFG_MAX_SPEED].value,
605 cfg_data[CFG_DEV_MODE].value,
606 cfg_data[CFG_ADAPTER_MODE].value,
607 cfg_data[CFG_TAGS].value,
608 cfg_data[CFG_RESET_DELAY].value);
609 for (i = 0; i < CFG_NUM; i++)
611 if (cfg_data[i].value < cfg_data[i].min
612 || cfg_data[i].value > cfg_data[i].max)
613 cfg_data[i].value = cfg_data[i].def;
620 * Mapping from the eeprom delay index value (index into this array)
621 * to the number of actual seconds that the delay should be for.
623 static char __devinitdata eeprom_index_to_delay_map[] =
624 { 1, 3, 5, 10, 16, 30, 60, 120 };
628 * eeprom_index_to_delay - Take the eeprom delay setting and convert it
629 * into a number of seconds.
631 * @eeprom: The eeprom structure in which we find the delay index to map.
633 static void __devinit eeprom_index_to_delay(struct NvRamType *eeprom)
635 eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
640 * delay_to_eeprom_index - Take a delay in seconds and return the
641 * closest eeprom index which will delay for at least that amount of
642 * seconds.
644 * @delay: The delay, in seconds, to find the eeprom index for.
646 static int __devinit delay_to_eeprom_index(int delay)
648 u8 idx = 0;
649 while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
650 idx++;
651 return idx;
656 * eeprom_override - Override the eeprom settings, in the provided
657 * eeprom structure, with values that have been set on the command
658 * line.
660 * @eeprom: The eeprom data to override with command line options.
662 static void __devinit eeprom_override(struct NvRamType *eeprom)
664 u8 id;
666 /* Adapter Settings */
667 if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
668 eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
670 if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
671 eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
673 if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
674 eeprom->delay_time = delay_to_eeprom_index(
675 cfg_data[CFG_RESET_DELAY].value);
677 if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
678 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
680 /* Device Settings */
681 for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
682 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
683 eeprom->target[id].cfg0 =
684 (u8)cfg_data[CFG_DEV_MODE].value;
686 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
687 eeprom->target[id].period =
688 (u8)cfg_data[CFG_MAX_SPEED].value;
694 /*---------------------------------------------------------------------------
695 ---------------------------------------------------------------------------*/
697 static unsigned int list_size(struct list_head *head)
699 unsigned int count = 0;
700 struct list_head *pos;
701 list_for_each(pos, head)
702 count++;
703 return count;
707 static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
708 struct DeviceCtlBlk *pos)
710 int use_next = 0;
711 struct DeviceCtlBlk* next = NULL;
712 struct DeviceCtlBlk* i;
714 if (list_empty(head))
715 return NULL;
717 /* find supplied dcb and then select the next one */
718 list_for_each_entry(i, head, list)
719 if (use_next) {
720 next = i;
721 break;
722 } else if (i == pos) {
723 use_next = 1;
725 /* if no next one take the head one (ie, wraparound) */
726 if (!next)
727 list_for_each_entry(i, head, list) {
728 next = i;
729 break;
732 return next;
736 static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
738 if (srb->tag_number < 255) {
739 dcb->tag_mask &= ~(1 << srb->tag_number); /* free tag mask */
740 srb->tag_number = 255;
745 /* Find cmd in SRB list */
746 static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
747 struct list_head *head)
749 struct ScsiReqBlk *i;
750 list_for_each_entry(i, head, list)
751 if (i->cmd == cmd)
752 return i;
753 return NULL;
757 static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb)
759 struct list_head *head = &acb->srb_free_list;
760 struct ScsiReqBlk *srb = NULL;
762 if (!list_empty(head)) {
763 srb = list_entry(head->next, struct ScsiReqBlk, list);
764 list_del(head->next);
765 dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb);
767 return srb;
771 static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
773 dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb);
774 list_add_tail(&srb->list, &acb->srb_free_list);
778 static void srb_waiting_insert(struct DeviceCtlBlk *dcb,
779 struct ScsiReqBlk *srb)
781 dprintkdbg(DBG_0, "srb_waiting_insert: (pid#%li) <%02i-%i> srb=%p\n",
782 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
783 list_add(&srb->list, &dcb->srb_waiting_list);
787 static void srb_waiting_append(struct DeviceCtlBlk *dcb,
788 struct ScsiReqBlk *srb)
790 dprintkdbg(DBG_0, "srb_waiting_append: (pid#%li) <%02i-%i> srb=%p\n",
791 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
792 list_add_tail(&srb->list, &dcb->srb_waiting_list);
796 static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
798 dprintkdbg(DBG_0, "srb_going_append: (pid#%li) <%02i-%i> srb=%p\n",
799 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
800 list_add_tail(&srb->list, &dcb->srb_going_list);
804 static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
806 struct ScsiReqBlk *i;
807 struct ScsiReqBlk *tmp;
808 dprintkdbg(DBG_0, "srb_going_remove: (pid#%li) <%02i-%i> srb=%p\n",
809 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
811 list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list)
812 if (i == srb) {
813 list_del(&srb->list);
814 break;
819 static void srb_waiting_remove(struct DeviceCtlBlk *dcb,
820 struct ScsiReqBlk *srb)
822 struct ScsiReqBlk *i;
823 struct ScsiReqBlk *tmp;
824 dprintkdbg(DBG_0, "srb_waiting_remove: (pid#%li) <%02i-%i> srb=%p\n",
825 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
827 list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list)
828 if (i == srb) {
829 list_del(&srb->list);
830 break;
835 static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb,
836 struct ScsiReqBlk *srb)
838 dprintkdbg(DBG_0,
839 "srb_going_to_waiting_move: (pid#%li) <%02i-%i> srb=%p\n",
840 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
841 list_move(&srb->list, &dcb->srb_waiting_list);
845 static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb,
846 struct ScsiReqBlk *srb)
848 dprintkdbg(DBG_0,
849 "srb_waiting_to_going_move: (pid#%li) <%02i-%i> srb=%p\n",
850 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
851 list_move(&srb->list, &dcb->srb_going_list);
855 /* Sets the timer to wake us up */
856 static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
858 if (timer_pending(&acb->waiting_timer))
859 return;
860 init_timer(&acb->waiting_timer);
861 acb->waiting_timer.function = waiting_timeout;
862 acb->waiting_timer.data = (unsigned long) acb;
863 if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
864 acb->waiting_timer.expires =
865 acb->scsi_host->last_reset - HZ / 2 + 1;
866 else
867 acb->waiting_timer.expires = jiffies + to + 1;
868 add_timer(&acb->waiting_timer);
872 /* Send the next command from the waiting list to the bus */
873 static void waiting_process_next(struct AdapterCtlBlk *acb)
875 struct DeviceCtlBlk *start = NULL;
876 struct DeviceCtlBlk *pos;
877 struct DeviceCtlBlk *dcb;
878 struct ScsiReqBlk *srb;
879 struct list_head *dcb_list_head = &acb->dcb_list;
881 if (acb->active_dcb
882 || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
883 return;
885 if (timer_pending(&acb->waiting_timer))
886 del_timer(&acb->waiting_timer);
888 if (list_empty(dcb_list_head))
889 return;
892 * Find the starting dcb. Need to find it again in the list
893 * since the list may have changed since we set the ptr to it
895 list_for_each_entry(dcb, dcb_list_head, list)
896 if (dcb == acb->dcb_run_robin) {
897 start = dcb;
898 break;
900 if (!start) {
901 /* This can happen! */
902 start = list_entry(dcb_list_head->next, typeof(*start), list);
903 acb->dcb_run_robin = start;
908 * Loop over the dcb, but we start somewhere (potentially) in
909 * the middle of the loop so we need to manully do this.
911 pos = start;
912 do {
913 struct list_head *waiting_list_head = &pos->srb_waiting_list;
915 /* Make sure, the next another device gets scheduled ... */
916 acb->dcb_run_robin = dcb_get_next(dcb_list_head,
917 acb->dcb_run_robin);
919 if (list_empty(waiting_list_head) ||
920 pos->max_command <= list_size(&pos->srb_going_list)) {
921 /* move to next dcb */
922 pos = dcb_get_next(dcb_list_head, pos);
923 } else {
924 srb = list_entry(waiting_list_head->next,
925 struct ScsiReqBlk, list);
927 /* Try to send to the bus */
928 if (!start_scsi(acb, pos, srb))
929 srb_waiting_to_going_move(pos, srb);
930 else
931 waiting_set_timer(acb, HZ/50);
932 break;
934 } while (pos != start);
938 /* Wake up waiting queue */
939 static void waiting_timeout(unsigned long ptr)
941 unsigned long flags;
942 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
943 dprintkdbg(DBG_1,
944 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
945 DC395x_LOCK_IO(acb->scsi_host, flags);
946 waiting_process_next(acb);
947 DC395x_UNLOCK_IO(acb->scsi_host, flags);
951 /* Get the DCB for a given ID/LUN combination */
952 static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
954 return acb->children[id][lun];
958 /* Send SCSI Request Block (srb) to adapter (acb) */
959 static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
961 struct DeviceCtlBlk *dcb = srb->dcb;
963 if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
964 acb->active_dcb ||
965 (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
966 srb_waiting_append(dcb, srb);
967 waiting_process_next(acb);
968 return;
971 if (!start_scsi(acb, dcb, srb))
972 srb_going_append(dcb, srb);
973 else {
974 srb_waiting_insert(dcb, srb);
975 waiting_set_timer(acb, HZ / 50);
979 /* Prepare SRB for being sent to Device DCB w/ command *cmd */
980 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
981 struct ScsiReqBlk *srb)
983 int nseg;
984 enum dma_data_direction dir = cmd->sc_data_direction;
985 dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n",
986 cmd->serial_number, dcb->target_id, dcb->target_lun);
988 srb->dcb = dcb;
989 srb->cmd = cmd;
990 srb->sg_count = 0;
991 srb->total_xfer_length = 0;
992 srb->sg_bus_addr = 0;
993 srb->sg_index = 0;
994 srb->adapter_status = 0;
995 srb->target_status = 0;
996 srb->msg_count = 0;
997 srb->status = 0;
998 srb->flag = 0;
999 srb->state = 0;
1000 srb->retry_count = 0;
1001 srb->tag_number = TAG_NONE;
1002 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1003 srb->end_message = 0;
1005 nseg = scsi_dma_map(cmd);
1006 BUG_ON(nseg < 0);
1008 if (dir == PCI_DMA_NONE || !nseg) {
1009 dprintkdbg(DBG_0,
1010 "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
1011 cmd->bufflen, scsi_sglist(cmd), scsi_sg_count(cmd),
1012 srb->segment_x[0].address);
1013 } else {
1014 int i;
1015 u32 reqlen = scsi_bufflen(cmd);
1016 struct scatterlist *sg;
1017 struct SGentry *sgp = srb->segment_x;
1019 srb->sg_count = nseg;
1021 dprintkdbg(DBG_0,
1022 "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
1023 reqlen, scsi_sglist(cmd), scsi_sg_count(cmd),
1024 srb->sg_count);
1026 scsi_for_each_sg(cmd, sg, srb->sg_count, i) {
1027 u32 busaddr = (u32)sg_dma_address(sg);
1028 u32 seglen = (u32)sg->length;
1029 sgp[i].address = busaddr;
1030 sgp[i].length = seglen;
1031 srb->total_xfer_length += seglen;
1033 sgp += srb->sg_count - 1;
1036 * adjust last page if too big as it is allocated
1037 * on even page boundaries
1039 if (srb->total_xfer_length > reqlen) {
1040 sgp->length -= (srb->total_xfer_length - reqlen);
1041 srb->total_xfer_length = reqlen;
1044 /* Fixup for WIDE padding - make sure length is even */
1045 if (dcb->sync_period & WIDE_SYNC &&
1046 srb->total_xfer_length % 2) {
1047 srb->total_xfer_length++;
1048 sgp->length++;
1051 srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
1052 srb->segment_x,
1053 SEGMENTX_LEN,
1054 PCI_DMA_TODEVICE);
1056 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
1057 srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
1060 srb->request_length = srb->total_xfer_length;
1065 * dc395x_queue_command - queue scsi command passed from the mid
1066 * layer, invoke 'done' on completion
1068 * @cmd: pointer to scsi command object
1069 * @done: function pointer to be invoked on completion
1071 * Returns 1 if the adapter (host) is busy, else returns 0. One
1072 * reason for an adapter to be busy is that the number
1073 * of outstanding queued commands is already equal to
1074 * struct Scsi_Host::can_queue .
1076 * Required: if struct Scsi_Host::can_queue is ever non-zero
1077 * then this function is required.
1079 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1080 * and is expected to be held on return.
1083 static int dc395x_queue_command(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1085 struct DeviceCtlBlk *dcb;
1086 struct ScsiReqBlk *srb;
1087 struct AdapterCtlBlk *acb =
1088 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1089 dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n",
1090 cmd->serial_number, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
1092 /* Assume BAD_TARGET; will be cleared later */
1093 cmd->result = DID_BAD_TARGET << 16;
1095 /* ignore invalid targets */
1096 if (cmd->device->id >= acb->scsi_host->max_id ||
1097 cmd->device->lun >= acb->scsi_host->max_lun ||
1098 cmd->device->lun >31) {
1099 goto complete;
1102 /* does the specified lun on the specified device exist */
1103 if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
1104 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
1105 cmd->device->id, cmd->device->lun);
1106 goto complete;
1109 /* do we have a DCB for the device */
1110 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1111 if (!dcb) {
1112 /* should never happen */
1113 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
1114 cmd->device->id, cmd->device->lun);
1115 goto complete;
1118 /* set callback and clear result in the command */
1119 cmd->scsi_done = done;
1120 cmd->result = 0;
1122 srb = srb_get_free(acb);
1123 if (!srb)
1126 * Return 1 since we are unable to queue this command at this
1127 * point in time.
1129 dprintkdbg(DBG_0, "queue_command: No free srb's\n");
1130 return 1;
1133 build_srb(cmd, dcb, srb);
1135 if (!list_empty(&dcb->srb_waiting_list)) {
1136 /* append to waiting queue */
1137 srb_waiting_append(dcb, srb);
1138 waiting_process_next(acb);
1139 } else {
1140 /* process immediately */
1141 send_srb(acb, srb);
1143 dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->serial_number);
1144 return 0;
1146 complete:
1148 * Complete the command immediatey, and then return 0 to
1149 * indicate that we have handled the command. This is usually
1150 * done when the commad is for things like non existent
1151 * devices.
1153 done(cmd);
1154 return 0;
1159 * Return the disk geometry for the given SCSI device.
1161 static int dc395x_bios_param(struct scsi_device *sdev,
1162 struct block_device *bdev, sector_t capacity, int *info)
1164 #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
1165 int heads, sectors, cylinders;
1166 struct AdapterCtlBlk *acb;
1167 int size = capacity;
1169 dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
1170 acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
1171 heads = 64;
1172 sectors = 32;
1173 cylinders = size / (heads * sectors);
1175 if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
1176 heads = 255;
1177 sectors = 63;
1178 cylinders = size / (heads * sectors);
1180 geom[0] = heads;
1181 geom[1] = sectors;
1182 geom[2] = cylinders;
1183 return 0;
1184 #else
1185 return scsicam_bios_param(bdev, capacity, info);
1186 #endif
1190 static void dump_register_info(struct AdapterCtlBlk *acb,
1191 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
1193 u16 pstat;
1194 struct pci_dev *dev = acb->dev;
1195 pci_read_config_word(dev, PCI_STATUS, &pstat);
1196 if (!dcb)
1197 dcb = acb->active_dcb;
1198 if (!srb && dcb)
1199 srb = dcb->active_srb;
1200 if (srb) {
1201 if (!srb->cmd)
1202 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
1203 srb, srb->cmd);
1204 else
1205 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) "
1206 "cmnd=0x%02x <%02i-%i>\n",
1207 srb, srb->cmd, srb->cmd->serial_number,
1208 srb->cmd->cmnd[0], srb->cmd->device->id,
1209 srb->cmd->device->lun);
1210 printk(" sglist=%p cnt=%i idx=%i len=%zu\n",
1211 srb->segment_x, srb->sg_count, srb->sg_index,
1212 srb->total_xfer_length);
1213 printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1214 srb->state, srb->status, srb->scsi_phase,
1215 (acb->active_dcb) ? "" : "not");
1217 dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1218 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1219 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1220 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1221 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1222 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1223 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1224 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
1225 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1226 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1227 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1228 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
1229 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1230 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1231 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1232 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1233 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1234 dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1235 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1236 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1237 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1238 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1239 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1240 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1241 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1242 DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
1243 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1244 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1245 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1246 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1247 dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1248 "pci{status=0x%04x}\n",
1249 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1250 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1251 DC395x_read8(acb, TRM_S1040_GEN_TIMER),
1252 pstat);
1256 static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1258 #if debug_enabled(DBG_FIFO)
1259 u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1260 u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1261 if (!(fifocnt & 0x40))
1262 dprintkdbg(DBG_FIFO,
1263 "clear_fifo: (%i bytes) on phase %02x in %s\n",
1264 fifocnt & 0x3f, lines, txt);
1265 #endif
1266 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1270 static void reset_dev_param(struct AdapterCtlBlk *acb)
1272 struct DeviceCtlBlk *dcb;
1273 struct NvRamType *eeprom = &acb->eeprom;
1274 dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
1276 list_for_each_entry(dcb, &acb->dcb_list, list) {
1277 u8 period_index;
1279 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1280 dcb->sync_period = 0;
1281 dcb->sync_offset = 0;
1283 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1284 period_index = eeprom->target[dcb->target_id].period & 0x07;
1285 dcb->min_nego_period = clock_period[period_index];
1286 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1287 || !(acb->config & HCC_WIDE_CARD))
1288 dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1294 * perform a hard reset on the SCSI bus
1295 * @cmd - some command for this host (for fetching hooks)
1296 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1298 static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1300 struct AdapterCtlBlk *acb =
1301 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1302 dprintkl(KERN_INFO,
1303 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n",
1304 cmd->serial_number, cmd->device->id, cmd->device->lun, cmd);
1306 if (timer_pending(&acb->waiting_timer))
1307 del_timer(&acb->waiting_timer);
1310 * disable interrupt
1312 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1313 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1314 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1315 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1317 reset_scsi_bus(acb);
1318 udelay(500);
1320 /* We may be in serious trouble. Wait some seconds */
1321 acb->scsi_host->last_reset =
1322 jiffies + 3 * HZ / 2 +
1323 HZ * acb->eeprom.delay_time;
1326 * re-enable interrupt
1328 /* Clear SCSI FIFO */
1329 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1330 clear_fifo(acb, "eh_bus_reset");
1331 /* Delete pending IRQ */
1332 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1333 set_basic_config(acb);
1335 reset_dev_param(acb);
1336 doing_srb_done(acb, DID_RESET, cmd, 0);
1337 acb->active_dcb = NULL;
1338 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1339 waiting_process_next(acb);
1341 return SUCCESS;
1344 static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1346 int rc;
1348 spin_lock_irq(cmd->device->host->host_lock);
1349 rc = __dc395x_eh_bus_reset(cmd);
1350 spin_unlock_irq(cmd->device->host->host_lock);
1352 return rc;
1356 * abort an errant SCSI command
1357 * @cmd - command to be aborted
1358 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1360 static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1363 * Look into our command queues: If it has not been sent already,
1364 * we remove it and return success. Otherwise fail.
1366 struct AdapterCtlBlk *acb =
1367 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1368 struct DeviceCtlBlk *dcb;
1369 struct ScsiReqBlk *srb;
1370 dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
1371 cmd->serial_number, cmd->device->id, cmd->device->lun, cmd);
1373 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1374 if (!dcb) {
1375 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1376 return FAILED;
1379 srb = find_cmd(cmd, &dcb->srb_waiting_list);
1380 if (srb) {
1381 srb_waiting_remove(dcb, srb);
1382 pci_unmap_srb_sense(acb, srb);
1383 pci_unmap_srb(acb, srb);
1384 free_tag(dcb, srb);
1385 srb_free_insert(acb, srb);
1386 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1387 cmd->result = DID_ABORT << 16;
1388 return SUCCESS;
1390 srb = find_cmd(cmd, &dcb->srb_going_list);
1391 if (srb) {
1392 dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
1393 } else {
1394 dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
1396 return FAILED;
1400 /* SDTR */
1401 static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1402 struct ScsiReqBlk *srb)
1404 u8 *ptr = srb->msgout_buf + srb->msg_count;
1405 if (srb->msg_count > 1) {
1406 dprintkl(KERN_INFO,
1407 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1408 srb->msg_count, srb->msgout_buf[0],
1409 srb->msgout_buf[1]);
1410 return;
1412 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1413 dcb->sync_offset = 0;
1414 dcb->min_nego_period = 200 >> 2;
1415 } else if (dcb->sync_offset == 0)
1416 dcb->sync_offset = SYNC_NEGO_OFFSET;
1418 *ptr++ = MSG_EXTENDED; /* (01h) */
1419 *ptr++ = 3; /* length */
1420 *ptr++ = EXTENDED_SDTR; /* (01h) */
1421 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */
1422 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */
1423 srb->msg_count += 5;
1424 srb->state |= SRB_DO_SYNC_NEGO;
1428 /* WDTR */
1429 static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1430 struct ScsiReqBlk *srb)
1432 u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1433 (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1434 u8 *ptr = srb->msgout_buf + srb->msg_count;
1435 if (srb->msg_count > 1) {
1436 dprintkl(KERN_INFO,
1437 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1438 srb->msg_count, srb->msgout_buf[0],
1439 srb->msgout_buf[1]);
1440 return;
1442 *ptr++ = MSG_EXTENDED; /* (01h) */
1443 *ptr++ = 2; /* length */
1444 *ptr++ = EXTENDED_WDTR; /* (03h) */
1445 *ptr++ = wide;
1446 srb->msg_count += 4;
1447 srb->state |= SRB_DO_WIDE_NEGO;
1453 static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1454 struct ScsiReqBlk* srb)
1456 u16 s_stat2, return_code;
1457 u8 s_stat, scsicommand, i, identify_message;
1458 u8 *ptr;
1459 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
1460 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
1462 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
1464 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1465 s_stat2 = 0;
1466 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1467 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1468 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
1469 srb->cmd->serial_number, s_stat, s_stat2);
1471 * Try anyway?
1473 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1474 * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
1475 * (This is likely to be a bug in the hardware. Obviously, most people
1476 * only have one initiator per SCSI bus.)
1477 * Instead let this fail and have the timer make sure the command is
1478 * tried again after a short time
1480 /*selto_timer (acb); */
1481 return 1;
1483 if (acb->active_dcb) {
1484 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
1485 "command while another command (pid#%li) is active.",
1486 srb->cmd->serial_number,
1487 acb->active_dcb->active_srb ?
1488 acb->active_dcb->active_srb->cmd->serial_number : 0);
1489 return 1;
1491 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1492 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
1493 srb->cmd->serial_number);
1494 return 1;
1496 /* Allow starting of SCSI commands half a second before we allow the mid-level
1497 * to queue them again after a reset */
1498 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1499 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1500 return 1;
1503 /* Flush FIFO */
1504 clear_fifo(acb, "start_scsi");
1505 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1506 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1507 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1508 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1509 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1511 identify_message = dcb->identify_msg;
1512 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1513 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1514 if (srb->flag & AUTO_REQSENSE)
1515 identify_message &= 0xBF;
1517 if (((srb->cmd->cmnd[0] == INQUIRY)
1518 || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1519 || (srb->flag & AUTO_REQSENSE))
1520 && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1521 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1522 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1523 && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1524 && (dcb->target_lun == 0)) {
1525 srb->msgout_buf[0] = identify_message;
1526 srb->msg_count = 1;
1527 scsicommand = SCMD_SEL_ATNSTOP;
1528 srb->state = SRB_MSGOUT;
1529 #ifndef SYNC_FIRST
1530 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1531 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1532 build_wdtr(acb, dcb, srb);
1533 goto no_cmd;
1535 #endif
1536 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1537 && dcb->inquiry7 & SCSI_INQ_SYNC) {
1538 build_sdtr(acb, dcb, srb);
1539 goto no_cmd;
1541 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1542 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1543 build_wdtr(acb, dcb, srb);
1544 goto no_cmd;
1546 srb->msg_count = 0;
1548 /* Send identify message */
1549 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1551 scsicommand = SCMD_SEL_ATN;
1552 srb->state = SRB_START_;
1553 #ifndef DC395x_NO_TAGQ
1554 if ((dcb->sync_mode & EN_TAG_QUEUEING)
1555 && (identify_message & 0xC0)) {
1556 /* Send Tag message */
1557 u32 tag_mask = 1;
1558 u8 tag_number = 0;
1559 while (tag_mask & dcb->tag_mask
1560 && tag_number < dcb->max_command) {
1561 tag_mask = tag_mask << 1;
1562 tag_number++;
1564 if (tag_number >= dcb->max_command) {
1565 dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
1566 "Out of tags target=<%02i-%i>)\n",
1567 srb->cmd->serial_number, srb->cmd->device->id,
1568 srb->cmd->device->lun);
1569 srb->state = SRB_READY;
1570 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1571 DO_HWRESELECT);
1572 return 1;
1574 /* Send Tag id */
1575 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1576 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1577 dcb->tag_mask |= tag_mask;
1578 srb->tag_number = tag_number;
1579 scsicommand = SCMD_SEL_ATN3;
1580 srb->state = SRB_START_;
1582 #endif
1583 /*polling:*/
1584 /* Send CDB ..command block ......... */
1585 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
1586 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun,
1587 srb->cmd->cmnd[0], srb->tag_number);
1588 if (srb->flag & AUTO_REQSENSE) {
1589 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1590 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1591 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1592 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1593 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
1594 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1595 } else {
1596 ptr = (u8 *)srb->cmd->cmnd;
1597 for (i = 0; i < srb->cmd->cmd_len; i++)
1598 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1600 no_cmd:
1601 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1602 DO_HWRESELECT | DO_DATALATCH);
1603 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1605 * If start_scsi return 1:
1606 * we caught an interrupt (must be reset or reselection ... )
1607 * : Let's process it first!
1609 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
1610 srb->cmd->serial_number, dcb->target_id, dcb->target_lun);
1611 srb->state = SRB_READY;
1612 free_tag(dcb, srb);
1613 srb->msg_count = 0;
1614 return_code = 1;
1615 /* This IRQ should NOT get lost, as we did not acknowledge it */
1616 } else {
1618 * If start_scsi returns 0:
1619 * we know that the SCSI processor is free
1621 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1622 dcb->active_srb = srb;
1623 acb->active_dcb = dcb;
1624 return_code = 0;
1625 /* it's important for atn stop */
1626 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1627 DO_DATALATCH | DO_HWRESELECT);
1628 /* SCSI command */
1629 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1631 return return_code;
1635 #define DC395x_ENABLE_MSGOUT \
1636 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1637 srb->state |= SRB_MSGOUT
1640 /* abort command */
1641 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1642 struct ScsiReqBlk *srb)
1644 srb->msgout_buf[0] = ABORT;
1645 srb->msg_count = 1;
1646 DC395x_ENABLE_MSGOUT;
1647 srb->state &= ~SRB_MSGIN;
1648 srb->state |= SRB_MSGOUT;
1653 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1654 * have been triggered for this card.
1656 * @acb: a pointer to the adpter control block
1657 * @scsi_status: the status return when we checked the card
1659 static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1660 u16 scsi_status)
1662 struct DeviceCtlBlk *dcb;
1663 struct ScsiReqBlk *srb;
1664 u16 phase;
1665 u8 scsi_intstatus;
1666 unsigned long flags;
1667 void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
1668 u16 *);
1670 DC395x_LOCK_IO(acb->scsi_host, flags);
1672 /* This acknowledges the IRQ */
1673 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1674 if ((scsi_status & 0x2007) == 0x2002)
1675 dprintkl(KERN_DEBUG,
1676 "COP after COP completed? %04x\n", scsi_status);
1677 if (debug_enabled(DBG_KG)) {
1678 if (scsi_intstatus & INT_SELTIMEOUT)
1679 dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1681 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1683 if (timer_pending(&acb->selto_timer))
1684 del_timer(&acb->selto_timer);
1686 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1687 disconnect(acb); /* bus free interrupt */
1688 goto out_unlock;
1690 if (scsi_intstatus & INT_RESELECTED) {
1691 reselect(acb);
1692 goto out_unlock;
1694 if (scsi_intstatus & INT_SELECT) {
1695 dprintkl(KERN_INFO, "Host does not support target mode!\n");
1696 goto out_unlock;
1698 if (scsi_intstatus & INT_SCSIRESET) {
1699 scsi_reset_detect(acb);
1700 goto out_unlock;
1702 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1703 dcb = acb->active_dcb;
1704 if (!dcb) {
1705 dprintkl(KERN_DEBUG,
1706 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1707 scsi_status, scsi_intstatus);
1708 goto out_unlock;
1710 srb = dcb->active_srb;
1711 if (dcb->flag & ABORT_DEV_) {
1712 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1713 enable_msgout_abort(acb, srb);
1716 /* software sequential machine */
1717 phase = (u16)srb->scsi_phase;
1720 * 62037 or 62137
1721 * call dc395x_scsi_phase0[]... "phase entry"
1722 * handle every phase before start transfer
1724 /* data_out_phase0, phase:0 */
1725 /* data_in_phase0, phase:1 */
1726 /* command_phase0, phase:2 */
1727 /* status_phase0, phase:3 */
1728 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
1729 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
1730 /* msgout_phase0, phase:6 */
1731 /* msgin_phase0, phase:7 */
1732 dc395x_statev = dc395x_scsi_phase0[phase];
1733 dc395x_statev(acb, srb, &scsi_status);
1736 * if there were any exception occured scsi_status
1737 * will be modify to bus free phase new scsi_status
1738 * transfer out from ... previous dc395x_statev
1740 srb->scsi_phase = scsi_status & PHASEMASK;
1741 phase = (u16)scsi_status & PHASEMASK;
1744 * call dc395x_scsi_phase1[]... "phase entry" handle
1745 * every phase to do transfer
1747 /* data_out_phase1, phase:0 */
1748 /* data_in_phase1, phase:1 */
1749 /* command_phase1, phase:2 */
1750 /* status_phase1, phase:3 */
1751 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
1752 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
1753 /* msgout_phase1, phase:6 */
1754 /* msgin_phase1, phase:7 */
1755 dc395x_statev = dc395x_scsi_phase1[phase];
1756 dc395x_statev(acb, srb, &scsi_status);
1758 out_unlock:
1759 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1763 static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
1765 struct AdapterCtlBlk *acb = dev_id;
1766 u16 scsi_status;
1767 u8 dma_status;
1768 irqreturn_t handled = IRQ_NONE;
1771 * Check for pending interrupt
1773 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1774 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1775 if (scsi_status & SCSIINTERRUPT) {
1776 /* interrupt pending - let's process it! */
1777 dc395x_handle_interrupt(acb, scsi_status);
1778 handled = IRQ_HANDLED;
1780 else if (dma_status & 0x20) {
1781 /* Error from the DMA engine */
1782 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1783 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
1784 acb = NULL;
1785 handled = IRQ_HANDLED;
1788 return handled;
1792 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1793 u16 *pscsi_status)
1795 dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->serial_number);
1796 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
1797 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
1799 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1800 srb->state &= ~SRB_MSGOUT;
1804 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1805 u16 *pscsi_status)
1807 u16 i;
1808 u8 *ptr;
1809 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->serial_number);
1811 clear_fifo(acb, "msgout_phase1");
1812 if (!(srb->state & SRB_MSGOUT)) {
1813 srb->state |= SRB_MSGOUT;
1814 dprintkl(KERN_DEBUG,
1815 "msgout_phase1: (pid#%li) Phase unexpected\n",
1816 srb->cmd->serial_number); /* So what ? */
1818 if (!srb->msg_count) {
1819 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
1820 srb->cmd->serial_number);
1821 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
1822 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1823 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1824 return;
1826 ptr = (u8 *)srb->msgout_buf;
1827 for (i = 0; i < srb->msg_count; i++)
1828 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1829 srb->msg_count = 0;
1830 if (srb->msgout_buf[0] == MSG_ABORT)
1831 srb->state = SRB_ABORT_SENT;
1833 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1837 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1838 u16 *pscsi_status)
1840 dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->serial_number);
1841 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1845 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1846 u16 *pscsi_status)
1848 struct DeviceCtlBlk *dcb;
1849 u8 *ptr;
1850 u16 i;
1851 dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->serial_number);
1853 clear_fifo(acb, "command_phase1");
1854 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
1855 if (!(srb->flag & AUTO_REQSENSE)) {
1856 ptr = (u8 *)srb->cmd->cmnd;
1857 for (i = 0; i < srb->cmd->cmd_len; i++) {
1858 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
1859 ptr++;
1861 } else {
1862 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1863 dcb = acb->active_dcb;
1864 /* target id */
1865 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1866 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1867 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1868 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
1869 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1871 srb->state |= SRB_COMMAND;
1872 /* it's important for atn stop */
1873 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1874 /* SCSI command */
1875 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1880 * Verify that the remaining space in the hw sg lists is the same as
1881 * the count of remaining bytes in srb->total_xfer_length
1883 static void sg_verify_length(struct ScsiReqBlk *srb)
1885 if (debug_enabled(DBG_SG)) {
1886 unsigned len = 0;
1887 unsigned idx = srb->sg_index;
1888 struct SGentry *psge = srb->segment_x + idx;
1889 for (; idx < srb->sg_count; psge++, idx++)
1890 len += psge->length;
1891 if (len != srb->total_xfer_length)
1892 dprintkdbg(DBG_SG,
1893 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1894 srb->total_xfer_length, len);
1900 * Compute the next Scatter Gather list index and adjust its length
1901 * and address if necessary
1903 static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
1905 u8 idx;
1906 u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
1907 struct SGentry *psge = srb->segment_x + srb->sg_index;
1909 dprintkdbg(DBG_0,
1910 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
1911 xferred, srb->total_xfer_length, left);
1912 if (xferred == 0) {
1913 /* nothing to update since we did not transfer any data */
1914 return;
1917 sg_verify_length(srb);
1918 srb->total_xfer_length = left; /* update remaining count */
1919 for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
1920 if (xferred >= psge->length) {
1921 /* Complete SG entries done */
1922 xferred -= psge->length;
1923 } else {
1924 /* Partial SG entry done */
1925 psge->length -= xferred;
1926 psge->address += xferred;
1927 srb->sg_index = idx;
1928 pci_dma_sync_single_for_device(srb->dcb->
1929 acb->dev,
1930 srb->sg_bus_addr,
1931 SEGMENTX_LEN,
1932 PCI_DMA_TODEVICE);
1933 break;
1935 psge++;
1937 sg_verify_length(srb);
1942 * We have transfered a single byte (PIO mode?) and need to update
1943 * the count of bytes remaining (total_xfer_length) and update the sg
1944 * entry to either point to next byte in the current sg entry, or of
1945 * already at the end to point to the start of the next sg entry
1947 static void sg_subtract_one(struct ScsiReqBlk *srb)
1949 sg_update_list(srb, srb->total_xfer_length - 1);
1954 * cleanup_after_transfer
1956 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
1957 * KG: Currently called from StatusPhase1 ()
1958 * Should probably also be called from other places
1959 * Best might be to call it in DataXXPhase0, if new phase will differ
1961 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
1962 struct ScsiReqBlk *srb)
1964 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
1965 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
1966 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
1967 clear_fifo(acb, "cleanup/in");
1968 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
1969 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1970 } else { /* write */
1971 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
1972 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1973 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
1974 clear_fifo(acb, "cleanup/out");
1976 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1981 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
1982 * Seems to be needed for unknown reasons; could be a hardware bug :-(
1984 #define DC395x_LASTPIO 4
1987 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1988 u16 *pscsi_status)
1990 struct DeviceCtlBlk *dcb = srb->dcb;
1991 u16 scsi_status = *pscsi_status;
1992 u32 d_left_counter = 0;
1993 dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
1994 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
1997 * KG: We need to drain the buffers before we draw any conclusions!
1998 * This means telling the DMA to push the rest into SCSI, telling
1999 * SCSI to push the rest to the bus.
2000 * However, the device might have been the one to stop us (phase
2001 * change), and the data in transit just needs to be accounted so
2002 * it can be retransmitted.)
2005 * KG: Stop DMA engine pushing more data into the SCSI FIFO
2006 * If we need more data, the DMA SG list will be freshly set up, anyway
2008 dprintkdbg(DBG_PIO, "data_out_phase0: "
2009 "DMA{fifocnt=0x%02x fifostat=0x%02x} "
2010 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
2011 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2012 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2013 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2014 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2015 srb->total_xfer_length);
2016 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2018 if (!(srb->state & SRB_XFERPAD)) {
2019 if (scsi_status & PARITYERROR)
2020 srb->status |= PARITY_ERROR;
2023 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2024 * is the no of bytes it got from the DMA engine not the no it
2025 * transferred successfully to the device. (And the difference could
2026 * be as much as the FIFO size, I guess ...)
2028 if (!(scsi_status & SCSIXFERDONE)) {
2030 * when data transfer from DMA FIFO to SCSI FIFO
2031 * if there was some data left in SCSI FIFO
2033 d_left_counter =
2034 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2035 0x1F);
2036 if (dcb->sync_period & WIDE_SYNC)
2037 d_left_counter <<= 1;
2039 dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
2040 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
2041 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
2042 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2043 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2044 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2045 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2046 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2047 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2048 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2051 * calculate all the residue data that not yet tranfered
2052 * SCSI transfer counter + left in SCSI FIFO data
2054 * .....TRM_S1040_SCSI_COUNTER (24bits)
2055 * The counter always decrement by one for every SCSI byte transfer.
2056 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2057 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2059 if (srb->total_xfer_length > DC395x_LASTPIO)
2060 d_left_counter +=
2061 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2063 /* Is this a good idea? */
2064 /*clear_fifo(acb, "DOP1"); */
2065 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2066 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2067 && scsi_bufflen(srb->cmd) % 2) {
2068 d_left_counter = 0;
2069 dprintkl(KERN_INFO,
2070 "data_out_phase0: Discard 1 byte (0x%02x)\n",
2071 scsi_status);
2074 * KG: Oops again. Same thinko as above: The SCSI might have been
2075 * faster than the DMA engine, so that it ran out of data.
2076 * In that case, we have to do just nothing!
2077 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2080 * KG: This is nonsense: We have been WRITING data to the bus
2081 * If the SCSI engine has no bytes left, how should the DMA engine?
2083 if (d_left_counter == 0) {
2084 srb->total_xfer_length = 0;
2085 } else {
2087 * if transfer not yet complete
2088 * there were some data residue in SCSI FIFO or
2089 * SCSI transfer counter not empty
2091 long oldxferred =
2092 srb->total_xfer_length - d_left_counter;
2093 const int diff =
2094 (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2095 sg_update_list(srb, d_left_counter);
2096 /* KG: Most ugly hack! Apparently, this works around a chip bug */
2097 if ((srb->segment_x[srb->sg_index].length ==
2098 diff && scsi_sg_count(srb->cmd))
2099 || ((oldxferred & ~PAGE_MASK) ==
2100 (PAGE_SIZE - diff))
2102 dprintkl(KERN_INFO, "data_out_phase0: "
2103 "Work around chip bug (%i)?\n", diff);
2104 d_left_counter =
2105 srb->total_xfer_length - diff;
2106 sg_update_list(srb, d_left_counter);
2107 /*srb->total_xfer_length -= diff; */
2108 /*srb->virt_addr += diff; */
2109 /*if (srb->cmd->use_sg) */
2110 /* srb->sg_index++; */
2114 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2115 cleanup_after_transfer(acb, srb);
2120 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2121 u16 *pscsi_status)
2123 dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
2124 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
2125 clear_fifo(acb, "data_out_phase1");
2126 /* do prepare before transfer when data out phase */
2127 data_io_transfer(acb, srb, XFERDATAOUT);
2130 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2131 u16 *pscsi_status)
2133 u16 scsi_status = *pscsi_status;
2135 dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
2136 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
2139 * KG: DataIn is much more tricky than DataOut. When the device is finished
2140 * and switches to another phase, the SCSI engine should be finished too.
2141 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2142 * engine and transferred to memory.
2143 * We should wait for the FIFOs to be emptied by that (is there any way to
2144 * enforce this?) and then stop the DMA engine, because it might think, that
2145 * there are more bytes to follow. Yes, the device might disconnect prior to
2146 * having all bytes transferred!
2147 * Also we should make sure that all data from the DMA engine buffer's really
2148 * made its way to the system memory! Some documentation on this would not
2149 * seem to be a bad idea, actually.
2151 if (!(srb->state & SRB_XFERPAD)) {
2152 u32 d_left_counter;
2153 unsigned int sc, fc;
2155 if (scsi_status & PARITYERROR) {
2156 dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
2157 "Parity Error\n", srb->cmd->serial_number);
2158 srb->status |= PARITY_ERROR;
2161 * KG: We should wait for the DMA FIFO to be empty ...
2162 * but: it would be better to wait first for the SCSI FIFO and then the
2163 * the DMA FIFO to become empty? How do we know, that the device not already
2164 * sent data to the FIFO in a MsgIn phase, eg.?
2166 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2167 dprintkdbg(DBG_KG, "data_in_phase0: "
2168 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2169 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2170 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2172 /* Now: Check remainig data: The SCSI counters should tell us ... */
2173 sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2174 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2175 d_left_counter = sc + ((fc & 0x1f)
2176 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2177 0));
2178 dprintkdbg(DBG_KG, "data_in_phase0: "
2179 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2180 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2181 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2183 (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2186 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2187 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
2188 srb->total_xfer_length, d_left_counter);
2189 #if DC395x_LASTPIO
2190 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2191 if (d_left_counter
2192 && srb->total_xfer_length <= DC395x_LASTPIO) {
2193 size_t left_io = srb->total_xfer_length;
2195 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2196 /*sg_update_list (srb, d_left_counter); */
2197 dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
2198 "for remaining %i bytes:",
2199 fc & 0x1f,
2200 (srb->dcb->sync_period & WIDE_SYNC) ?
2201 "words" : "bytes",
2202 srb->total_xfer_length);
2203 if (srb->dcb->sync_period & WIDE_SYNC)
2204 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2205 CFG2_WIDEFIFO);
2206 while (left_io) {
2207 unsigned char *virt, *base = NULL;
2208 unsigned long flags = 0;
2209 size_t len = left_io;
2210 size_t offset = srb->request_length - left_io;
2212 local_irq_save(flags);
2213 /* Assumption: it's inside one page as it's at most 4 bytes and
2214 I just assume it's on a 4-byte boundary */
2215 base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2216 srb->sg_count, &offset, &len);
2217 virt = base + offset;
2219 left_io -= len;
2221 while (len) {
2222 u8 byte;
2223 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2224 *virt++ = byte;
2226 if (debug_enabled(DBG_PIO))
2227 printk(" %02x", byte);
2229 d_left_counter--;
2230 sg_subtract_one(srb);
2232 len--;
2234 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2236 if (fc == 0x40) {
2237 left_io = 0;
2238 break;
2242 WARN_ON((fc != 0x40) == !d_left_counter);
2244 if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
2245 /* Read the last byte ... */
2246 if (srb->total_xfer_length > 0) {
2247 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2249 *virt++ = byte;
2250 srb->total_xfer_length--;
2251 if (debug_enabled(DBG_PIO))
2252 printk(" %02x", byte);
2255 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2258 scsi_kunmap_atomic_sg(base);
2259 local_irq_restore(flags);
2261 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2262 /*srb->total_xfer_length = 0; */
2263 if (debug_enabled(DBG_PIO))
2264 printk("\n");
2266 #endif /* DC395x_LASTPIO */
2268 /* KG: This should not be needed any more! */
2269 if (d_left_counter == 0
2270 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2271 srb->total_xfer_length = d_left_counter;
2272 } else { /* phase changed */
2274 * parsing the case:
2275 * when a transfer not yet complete
2276 * but be disconnected by target
2277 * if transfer not yet complete
2278 * there were some data residue in SCSI FIFO or
2279 * SCSI transfer counter not empty
2281 sg_update_list(srb, d_left_counter);
2284 /* KG: The target may decide to disconnect: Empty FIFO before! */
2285 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2286 cleanup_after_transfer(acb, srb);
2291 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2292 u16 *pscsi_status)
2294 dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
2295 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
2296 data_io_transfer(acb, srb, XFERDATAIN);
2300 static void data_io_transfer(struct AdapterCtlBlk *acb,
2301 struct ScsiReqBlk *srb, u16 io_dir)
2303 struct DeviceCtlBlk *dcb = srb->dcb;
2304 u8 bval;
2305 dprintkdbg(DBG_0,
2306 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
2307 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun,
2308 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
2309 srb->total_xfer_length, srb->sg_index, srb->sg_count);
2310 if (srb == acb->tmp_srb)
2311 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
2312 if (srb->sg_index >= srb->sg_count) {
2313 /* can't happen? out of bounds error */
2314 return;
2317 if (srb->total_xfer_length > DC395x_LASTPIO) {
2318 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2320 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2321 * Maybe, even ABORTXFER would be appropriate
2323 if (dma_status & XFERPENDING) {
2324 dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
2325 "Expect trouble!\n");
2326 dump_register_info(acb, dcb, srb);
2327 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2329 /* clear_fifo(acb, "IO"); */
2331 * load what physical address of Scatter/Gather list table
2332 * want to be transfer
2334 srb->state |= SRB_DATA_XFER;
2335 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2336 if (scsi_sg_count(srb->cmd)) { /* with S/G */
2337 io_dir |= DMACMD_SG;
2338 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2339 srb->sg_bus_addr +
2340 sizeof(struct SGentry) *
2341 srb->sg_index);
2342 /* load how many bytes in the sg list table */
2343 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2344 ((u32)(srb->sg_count -
2345 srb->sg_index) << 3));
2346 } else { /* without S/G */
2347 io_dir &= ~DMACMD_SG;
2348 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2349 srb->segment_x[0].address);
2350 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2351 srb->segment_x[0].length);
2353 /* load total transfer length (24bits) max value 16Mbyte */
2354 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2355 srb->total_xfer_length);
2356 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2357 if (io_dir & DMACMD_DIR) { /* read */
2358 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2359 SCMD_DMA_IN);
2360 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2361 } else {
2362 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2363 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2364 SCMD_DMA_OUT);
2368 #if DC395x_LASTPIO
2369 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
2371 * load what physical address of Scatter/Gather list table
2372 * want to be transfer
2374 srb->state |= SRB_DATA_XFER;
2375 /* load total transfer length (24bits) max value 16Mbyte */
2376 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2377 srb->total_xfer_length);
2378 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2379 if (io_dir & DMACMD_DIR) { /* read */
2380 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2381 SCMD_FIFO_IN);
2382 } else { /* write */
2383 int ln = srb->total_xfer_length;
2384 size_t left_io = srb->total_xfer_length;
2386 if (srb->dcb->sync_period & WIDE_SYNC)
2387 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2388 CFG2_WIDEFIFO);
2390 while (left_io) {
2391 unsigned char *virt, *base = NULL;
2392 unsigned long flags = 0;
2393 size_t len = left_io;
2394 size_t offset = srb->request_length - left_io;
2396 local_irq_save(flags);
2397 /* Again, max 4 bytes */
2398 base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2399 srb->sg_count, &offset, &len);
2400 virt = base + offset;
2402 left_io -= len;
2404 while (len--) {
2405 if (debug_enabled(DBG_PIO))
2406 printk(" %02x", *virt);
2408 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
2410 sg_subtract_one(srb);
2413 scsi_kunmap_atomic_sg(base);
2414 local_irq_restore(flags);
2416 if (srb->dcb->sync_period & WIDE_SYNC) {
2417 if (ln % 2) {
2418 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2419 if (debug_enabled(DBG_PIO))
2420 printk(" |00");
2422 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2424 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2425 if (debug_enabled(DBG_PIO))
2426 printk("\n");
2427 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2428 SCMD_FIFO_OUT);
2431 #endif /* DC395x_LASTPIO */
2432 else { /* xfer pad */
2433 u8 data = 0, data2 = 0;
2434 if (srb->sg_count) {
2435 srb->adapter_status = H_OVER_UNDER_RUN;
2436 srb->status |= OVER_RUN;
2439 * KG: despite the fact that we are using 16 bits I/O ops
2440 * the SCSI FIFO is only 8 bits according to the docs
2441 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2443 if (dcb->sync_period & WIDE_SYNC) {
2444 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
2445 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2446 CFG2_WIDEFIFO);
2447 if (io_dir & DMACMD_DIR) {
2448 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2449 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2450 } else {
2451 /* Danger, Robinson: If you find KGs
2452 * scattered over the wide disk, the driver
2453 * or chip is to blame :-( */
2454 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2455 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
2457 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2458 } else {
2459 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2460 /* Danger, Robinson: If you find a collection of Ks on your disk
2461 * something broke :-( */
2462 if (io_dir & DMACMD_DIR)
2463 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2464 else
2465 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2467 srb->state |= SRB_XFERPAD;
2468 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2469 /* SCSI command */
2470 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
2471 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
2476 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2477 u16 *pscsi_status)
2479 dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
2480 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
2481 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2482 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
2483 srb->state = SRB_COMPLETED;
2484 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
2485 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2486 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2490 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2491 u16 *pscsi_status)
2493 dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
2494 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
2495 srb->state = SRB_STATUS;
2496 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2497 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
2501 /* Check if the message is complete */
2502 static inline u8 msgin_completed(u8 * msgbuf, u32 len)
2504 if (*msgbuf == EXTENDED_MESSAGE) {
2505 if (len < 2)
2506 return 0;
2507 if (len < msgbuf[1] + 2)
2508 return 0;
2509 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
2510 if (len < 2)
2511 return 0;
2512 return 1;
2515 /* reject_msg */
2516 static inline void msgin_reject(struct AdapterCtlBlk *acb,
2517 struct ScsiReqBlk *srb)
2519 srb->msgout_buf[0] = MESSAGE_REJECT;
2520 srb->msg_count = 1;
2521 DC395x_ENABLE_MSGOUT;
2522 srb->state &= ~SRB_MSGIN;
2523 srb->state |= SRB_MSGOUT;
2524 dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
2525 srb->msgin_buf[0],
2526 srb->dcb->target_id, srb->dcb->target_lun);
2530 static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
2531 struct DeviceCtlBlk *dcb, u8 tag)
2533 struct ScsiReqBlk *srb = NULL;
2534 struct ScsiReqBlk *i;
2535 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
2536 srb->cmd->serial_number, tag, srb);
2538 if (!(dcb->tag_mask & (1 << tag)))
2539 dprintkl(KERN_DEBUG,
2540 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2541 dcb->tag_mask, tag);
2543 if (list_empty(&dcb->srb_going_list))
2544 goto mingx0;
2545 list_for_each_entry(i, &dcb->srb_going_list, list) {
2546 if (i->tag_number == tag) {
2547 srb = i;
2548 break;
2551 if (!srb)
2552 goto mingx0;
2554 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
2555 srb->cmd->serial_number, srb->dcb->target_id, srb->dcb->target_lun);
2556 if (dcb->flag & ABORT_DEV_) {
2557 /*srb->state = SRB_ABORT_SENT; */
2558 enable_msgout_abort(acb, srb);
2561 if (!(srb->state & SRB_DISCONNECT))
2562 goto mingx0;
2564 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
2565 srb->state |= dcb->active_srb->state;
2566 srb->state |= SRB_DATA_XFER;
2567 dcb->active_srb = srb;
2568 /* How can we make the DORS happy? */
2569 return srb;
2571 mingx0:
2572 srb = acb->tmp_srb;
2573 srb->state = SRB_UNEXPECT_RESEL;
2574 dcb->active_srb = srb;
2575 srb->msgout_buf[0] = MSG_ABORT_TAG;
2576 srb->msg_count = 1;
2577 DC395x_ENABLE_MSGOUT;
2578 dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
2579 return srb;
2583 static inline void reprogram_regs(struct AdapterCtlBlk *acb,
2584 struct DeviceCtlBlk *dcb)
2586 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
2587 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
2588 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
2589 set_xfer_rate(acb, dcb);
2593 /* set async transfer mode */
2594 static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2596 struct DeviceCtlBlk *dcb = srb->dcb;
2597 dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
2598 dcb->target_id, dcb->target_lun);
2600 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
2601 dcb->sync_mode |= SYNC_NEGO_DONE;
2602 /*dcb->sync_period &= 0; */
2603 dcb->sync_offset = 0;
2604 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
2605 srb->state &= ~SRB_DO_SYNC_NEGO;
2606 reprogram_regs(acb, dcb);
2607 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2608 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2609 build_wdtr(acb, dcb, srb);
2610 DC395x_ENABLE_MSGOUT;
2611 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
2616 /* set sync transfer mode */
2617 static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2619 struct DeviceCtlBlk *dcb = srb->dcb;
2620 u8 bval;
2621 int fact;
2622 dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
2623 "(%02i.%01i MHz) Offset %i\n",
2624 dcb->target_id, srb->msgin_buf[3] << 2,
2625 (250 / srb->msgin_buf[3]),
2626 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
2627 srb->msgin_buf[4]);
2629 if (srb->msgin_buf[4] > 15)
2630 srb->msgin_buf[4] = 15;
2631 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
2632 dcb->sync_offset = 0;
2633 else if (dcb->sync_offset == 0)
2634 dcb->sync_offset = srb->msgin_buf[4];
2635 if (srb->msgin_buf[4] > dcb->sync_offset)
2636 srb->msgin_buf[4] = dcb->sync_offset;
2637 else
2638 dcb->sync_offset = srb->msgin_buf[4];
2639 bval = 0;
2640 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
2641 || dcb->min_nego_period >
2642 clock_period[bval]))
2643 bval++;
2644 if (srb->msgin_buf[3] < clock_period[bval])
2645 dprintkl(KERN_INFO,
2646 "msgin_set_sync: Increase sync nego period to %ins\n",
2647 clock_period[bval] << 2);
2648 srb->msgin_buf[3] = clock_period[bval];
2649 dcb->sync_period &= 0xf0;
2650 dcb->sync_period |= ALT_SYNC | bval;
2651 dcb->min_nego_period = srb->msgin_buf[3];
2653 if (dcb->sync_period & WIDE_SYNC)
2654 fact = 500;
2655 else
2656 fact = 250;
2658 dprintkl(KERN_INFO,
2659 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2660 dcb->target_id, (fact == 500) ? "Wide16" : "",
2661 dcb->min_nego_period << 2, dcb->sync_offset,
2662 (fact / dcb->min_nego_period),
2663 ((fact % dcb->min_nego_period) * 10 +
2664 dcb->min_nego_period / 2) / dcb->min_nego_period);
2666 if (!(srb->state & SRB_DO_SYNC_NEGO)) {
2667 /* Reply with corrected SDTR Message */
2668 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
2669 srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
2671 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
2672 srb->msg_count = 5;
2673 DC395x_ENABLE_MSGOUT;
2674 dcb->sync_mode |= SYNC_NEGO_DONE;
2675 } else {
2676 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2677 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2678 build_wdtr(acb, dcb, srb);
2679 DC395x_ENABLE_MSGOUT;
2680 dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
2683 srb->state &= ~SRB_DO_SYNC_NEGO;
2684 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
2686 reprogram_regs(acb, dcb);
2690 static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
2691 struct ScsiReqBlk *srb)
2693 struct DeviceCtlBlk *dcb = srb->dcb;
2694 dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
2696 dcb->sync_period &= ~WIDE_SYNC;
2697 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
2698 dcb->sync_mode |= WIDE_NEGO_DONE;
2699 srb->state &= ~SRB_DO_WIDE_NEGO;
2700 reprogram_regs(acb, dcb);
2701 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2702 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2703 build_sdtr(acb, dcb, srb);
2704 DC395x_ENABLE_MSGOUT;
2705 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2709 static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2711 struct DeviceCtlBlk *dcb = srb->dcb;
2712 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
2713 && acb->config & HCC_WIDE_CARD) ? 1 : 0;
2714 dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
2716 if (srb->msgin_buf[3] > wide)
2717 srb->msgin_buf[3] = wide;
2718 /* Completed */
2719 if (!(srb->state & SRB_DO_WIDE_NEGO)) {
2720 dprintkl(KERN_DEBUG,
2721 "msgin_set_wide: Wide nego initiated <%02i>\n",
2722 dcb->target_id);
2723 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
2724 srb->msg_count = 4;
2725 srb->state |= SRB_DO_WIDE_NEGO;
2726 DC395x_ENABLE_MSGOUT;
2729 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
2730 if (srb->msgin_buf[3] > 0)
2731 dcb->sync_period |= WIDE_SYNC;
2732 else
2733 dcb->sync_period &= ~WIDE_SYNC;
2734 srb->state &= ~SRB_DO_WIDE_NEGO;
2735 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2736 dprintkdbg(DBG_1,
2737 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2738 (8 << srb->msgin_buf[3]), dcb->target_id);
2739 reprogram_regs(acb, dcb);
2740 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2741 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2742 build_sdtr(acb, dcb, srb);
2743 DC395x_ENABLE_MSGOUT;
2744 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
2750 * extended message codes:
2752 * code description
2754 * 02h Reserved
2755 * 00h MODIFY DATA POINTER
2756 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
2757 * 03h WIDE DATA TRANSFER REQUEST
2758 * 04h - 7Fh Reserved
2759 * 80h - FFh Vendor specific
2761 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2762 u16 *pscsi_status)
2764 struct DeviceCtlBlk *dcb = acb->active_dcb;
2765 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->serial_number);
2767 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2768 if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
2769 /* Now eval the msg */
2770 switch (srb->msgin_buf[0]) {
2771 case DISCONNECT:
2772 srb->state = SRB_DISCONNECT;
2773 break;
2775 case SIMPLE_QUEUE_TAG:
2776 case HEAD_OF_QUEUE_TAG:
2777 case ORDERED_QUEUE_TAG:
2778 srb =
2779 msgin_qtag(acb, dcb,
2780 srb->msgin_buf[1]);
2781 break;
2783 case MESSAGE_REJECT:
2784 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2785 DO_CLRATN | DO_DATALATCH);
2786 /* A sync nego message was rejected ! */
2787 if (srb->state & SRB_DO_SYNC_NEGO) {
2788 msgin_set_async(acb, srb);
2789 break;
2791 /* A wide nego message was rejected ! */
2792 if (srb->state & SRB_DO_WIDE_NEGO) {
2793 msgin_set_nowide(acb, srb);
2794 break;
2796 enable_msgout_abort(acb, srb);
2797 /*srb->state |= SRB_ABORT_SENT */
2798 break;
2800 case EXTENDED_MESSAGE:
2801 /* SDTR */
2802 if (srb->msgin_buf[1] == 3
2803 && srb->msgin_buf[2] == EXTENDED_SDTR) {
2804 msgin_set_sync(acb, srb);
2805 break;
2807 /* WDTR */
2808 if (srb->msgin_buf[1] == 2
2809 && srb->msgin_buf[2] == EXTENDED_WDTR
2810 && srb->msgin_buf[3] <= 2) { /* sanity check ... */
2811 msgin_set_wide(acb, srb);
2812 break;
2814 msgin_reject(acb, srb);
2815 break;
2817 case MSG_IGNOREWIDE:
2818 /* Discard wide residual */
2819 dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
2820 break;
2822 case COMMAND_COMPLETE:
2823 /* nothing has to be done */
2824 break;
2826 case SAVE_POINTERS:
2828 * SAVE POINTER may be ignored as we have the struct
2829 * ScsiReqBlk* associated with the scsi command.
2831 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2832 "SAVE POINTER rem=%i Ignore\n",
2833 srb->cmd->serial_number, srb->total_xfer_length);
2834 break;
2836 case RESTORE_POINTERS:
2837 dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
2838 break;
2840 case ABORT:
2841 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2842 "<%02i-%i> ABORT msg\n",
2843 srb->cmd->serial_number, dcb->target_id,
2844 dcb->target_lun);
2845 dcb->flag |= ABORT_DEV_;
2846 enable_msgout_abort(acb, srb);
2847 break;
2849 default:
2850 /* reject unknown messages */
2851 if (srb->msgin_buf[0] & IDENTIFY_BASE) {
2852 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
2853 srb->msg_count = 1;
2854 srb->msgout_buf[0] = dcb->identify_msg;
2855 DC395x_ENABLE_MSGOUT;
2856 srb->state |= SRB_MSGOUT;
2857 /*break; */
2859 msgin_reject(acb, srb);
2862 /* Clear counter and MsgIn state */
2863 srb->state &= ~SRB_MSGIN;
2864 acb->msg_len = 0;
2866 *pscsi_status = PH_BUS_FREE;
2867 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
2868 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2872 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2873 u16 *pscsi_status)
2875 dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->serial_number);
2876 clear_fifo(acb, "msgin_phase1");
2877 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2878 if (!(srb->state & SRB_MSGIN)) {
2879 srb->state &= ~SRB_DISCONNECT;
2880 srb->state |= SRB_MSGIN;
2882 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2883 /* SCSI command */
2884 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
2888 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2889 u16 *pscsi_status)
2894 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2895 u16 *pscsi_status)
2900 static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
2902 struct DeviceCtlBlk *i;
2904 /* set all lun device's period, offset */
2905 if (dcb->identify_msg & 0x07)
2906 return;
2908 if (acb->scan_devices) {
2909 current_sync_offset = dcb->sync_offset;
2910 return;
2913 list_for_each_entry(i, &acb->dcb_list, list)
2914 if (i->target_id == dcb->target_id) {
2915 i->sync_period = dcb->sync_period;
2916 i->sync_offset = dcb->sync_offset;
2917 i->sync_mode = dcb->sync_mode;
2918 i->min_nego_period = dcb->min_nego_period;
2923 static void disconnect(struct AdapterCtlBlk *acb)
2925 struct DeviceCtlBlk *dcb = acb->active_dcb;
2926 struct ScsiReqBlk *srb;
2928 if (!dcb) {
2929 dprintkl(KERN_ERR, "disconnect: No such device\n");
2930 udelay(500);
2931 /* Suspend queue for a while */
2932 acb->scsi_host->last_reset =
2933 jiffies + HZ / 2 +
2934 HZ * acb->eeprom.delay_time;
2935 clear_fifo(acb, "disconnectEx");
2936 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
2937 return;
2939 srb = dcb->active_srb;
2940 acb->active_dcb = NULL;
2941 dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->serial_number);
2943 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
2944 clear_fifo(acb, "disconnect");
2945 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
2946 if (srb->state & SRB_UNEXPECT_RESEL) {
2947 dprintkl(KERN_ERR,
2948 "disconnect: Unexpected reselection <%02i-%i>\n",
2949 dcb->target_id, dcb->target_lun);
2950 srb->state = 0;
2951 waiting_process_next(acb);
2952 } else if (srb->state & SRB_ABORT_SENT) {
2953 dcb->flag &= ~ABORT_DEV_;
2954 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
2955 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
2956 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
2957 waiting_process_next(acb);
2958 } else {
2959 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
2960 || !(srb->
2961 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
2963 * Selection time out
2964 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
2966 /* Unexp. Disc / Sel Timeout */
2967 if (srb->state != SRB_START_
2968 && srb->state != SRB_MSGOUT) {
2969 srb->state = SRB_READY;
2970 dprintkl(KERN_DEBUG,
2971 "disconnect: (pid#%li) Unexpected\n",
2972 srb->cmd->serial_number);
2973 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
2974 goto disc1;
2975 } else {
2976 /* Normal selection timeout */
2977 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
2978 "<%02i-%i> SelTO\n", srb->cmd->serial_number,
2979 dcb->target_id, dcb->target_lun);
2980 if (srb->retry_count++ > DC395x_MAX_RETRIES
2981 || acb->scan_devices) {
2982 srb->target_status =
2983 SCSI_STAT_SEL_TIMEOUT;
2984 goto disc1;
2986 free_tag(dcb, srb);
2987 srb_going_to_waiting_move(dcb, srb);
2988 dprintkdbg(DBG_KG,
2989 "disconnect: (pid#%li) Retry\n",
2990 srb->cmd->serial_number);
2991 waiting_set_timer(acb, HZ / 20);
2993 } else if (srb->state & SRB_DISCONNECT) {
2994 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
2996 * SRB_DISCONNECT (This is what we expect!)
2998 if (bval & 0x40) {
2999 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
3000 " 0x%02x: ACK set! Other controllers?\n",
3001 bval);
3002 /* It could come from another initiator, therefore don't do much ! */
3003 } else
3004 waiting_process_next(acb);
3005 } else if (srb->state & SRB_COMPLETED) {
3006 disc1:
3008 ** SRB_COMPLETED
3010 free_tag(dcb, srb);
3011 dcb->active_srb = NULL;
3012 srb->state = SRB_FREE;
3013 srb_done(acb, dcb, srb);
3019 static void reselect(struct AdapterCtlBlk *acb)
3021 struct DeviceCtlBlk *dcb = acb->active_dcb;
3022 struct ScsiReqBlk *srb = NULL;
3023 u16 rsel_tar_lun_id;
3024 u8 id, lun;
3025 u8 arblostflag = 0;
3026 dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
3028 clear_fifo(acb, "reselect");
3029 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3030 /* Read Reselected Target ID and LUN */
3031 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3032 if (dcb) { /* Arbitration lost but Reselection win */
3033 srb = dcb->active_srb;
3034 if (!srb) {
3035 dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
3036 "but active_srb == NULL\n");
3037 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3038 return;
3040 /* Why the if ? */
3041 if (!acb->scan_devices) {
3042 dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
3043 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
3044 srb->cmd->serial_number, dcb->target_id,
3045 dcb->target_lun, rsel_tar_lun_id,
3046 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3047 arblostflag = 1;
3048 /*srb->state |= SRB_DISCONNECT; */
3050 srb->state = SRB_READY;
3051 free_tag(dcb, srb);
3052 srb_going_to_waiting_move(dcb, srb);
3053 waiting_set_timer(acb, HZ / 20);
3055 /* return; */
3058 /* Read Reselected Target Id and LUN */
3059 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3060 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
3061 "Got %i!\n", rsel_tar_lun_id);
3062 id = rsel_tar_lun_id & 0xff;
3063 lun = (rsel_tar_lun_id >> 8) & 7;
3064 dcb = find_dcb(acb, id, lun);
3065 if (!dcb) {
3066 dprintkl(KERN_ERR, "reselect: From non existent device "
3067 "<%02i-%i>\n", id, lun);
3068 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3069 return;
3071 acb->active_dcb = dcb;
3073 if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3074 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
3075 "disconnection? <%02i-%i>\n",
3076 dcb->target_id, dcb->target_lun);
3078 if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
3079 srb = acb->tmp_srb;
3080 dcb->active_srb = srb;
3081 } else {
3082 /* There can be only one! */
3083 srb = dcb->active_srb;
3084 if (!srb || !(srb->state & SRB_DISCONNECT)) {
3086 * abort command
3088 dprintkl(KERN_DEBUG,
3089 "reselect: w/o disconnected cmds <%02i-%i>\n",
3090 dcb->target_id, dcb->target_lun);
3091 srb = acb->tmp_srb;
3092 srb->state = SRB_UNEXPECT_RESEL;
3093 dcb->active_srb = srb;
3094 enable_msgout_abort(acb, srb);
3095 } else {
3096 if (dcb->flag & ABORT_DEV_) {
3097 /*srb->state = SRB_ABORT_SENT; */
3098 enable_msgout_abort(acb, srb);
3099 } else
3100 srb->state = SRB_DATA_XFER;
3104 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3106 /* Program HA ID, target ID, period and offset */
3107 dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
3108 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
3109 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
3110 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
3111 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
3112 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3113 /* SCSI command */
3114 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3118 static inline u8 tagq_blacklist(char *name)
3120 #ifndef DC395x_NO_TAGQ
3121 return 0;
3122 #else
3123 return 1;
3124 #endif
3128 static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
3130 /* Check for SCSI format (ANSI and Response data format) */
3131 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
3132 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
3133 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
3134 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3135 /* ((dcb->dev_type == TYPE_DISK)
3136 || (dcb->dev_type == TYPE_MOD)) && */
3137 !tagq_blacklist(((char *)ptr) + 8)) {
3138 if (dcb->max_command == 1)
3139 dcb->max_command =
3140 dcb->acb->tag_max_num;
3141 dcb->sync_mode |= EN_TAG_QUEUEING;
3142 /*dcb->tag_mask = 0; */
3143 } else
3144 dcb->max_command = 1;
3149 static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3150 struct ScsiInqData *ptr)
3152 u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
3153 dcb->dev_type = bval1;
3154 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3155 disc_tagq_set(dcb, ptr);
3159 /* unmap mapped pci regions from SRB */
3160 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3162 struct scsi_cmnd *cmd = srb->cmd;
3163 enum dma_data_direction dir = cmd->sc_data_direction;
3165 if (scsi_sg_count(cmd) && dir != PCI_DMA_NONE) {
3166 /* unmap DC395x SG list */
3167 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
3168 srb->sg_bus_addr, SEGMENTX_LEN);
3169 pci_unmap_single(acb->dev, srb->sg_bus_addr,
3170 SEGMENTX_LEN,
3171 PCI_DMA_TODEVICE);
3172 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3173 scsi_sg_count(cmd), scsi_bufflen(cmd));
3174 /* unmap the sg segments */
3175 scsi_dma_unmap(cmd);
3180 /* unmap mapped pci sense buffer from SRB */
3181 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3182 struct ScsiReqBlk *srb)
3184 if (!(srb->flag & AUTO_REQSENSE))
3185 return;
3186 /* Unmap sense buffer */
3187 dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
3188 srb->segment_x[0].address);
3189 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3190 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
3191 /* Restore SG stuff */
3192 srb->total_xfer_length = srb->xferred;
3193 srb->segment_x[0].address =
3194 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
3195 srb->segment_x[0].length =
3196 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
3201 * Complete execution of a SCSI command
3202 * Signal completion to the generic SCSI driver
3204 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3205 struct ScsiReqBlk *srb)
3207 u8 tempcnt, status;
3208 struct scsi_cmnd *cmd = srb->cmd;
3209 enum dma_data_direction dir = cmd->sc_data_direction;
3210 int ckc_only = 1;
3212 dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->serial_number,
3213 srb->cmd->device->id, srb->cmd->device->lun);
3214 dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
3215 srb, scsi_sg_count(cmd), srb->sg_index, srb->sg_count,
3216 scsi_sgtalbe(cmd));
3217 status = srb->target_status;
3218 if (srb->flag & AUTO_REQSENSE) {
3219 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
3220 pci_unmap_srb_sense(acb, srb);
3222 ** target status..........................
3224 srb->flag &= ~AUTO_REQSENSE;
3225 srb->adapter_status = 0;
3226 srb->target_status = CHECK_CONDITION << 1;
3227 if (debug_enabled(DBG_1)) {
3228 switch (cmd->sense_buffer[2] & 0x0f) {
3229 case NOT_READY:
3230 dprintkl(KERN_DEBUG,
3231 "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3232 cmd->cmnd[0], dcb->target_id,
3233 dcb->target_lun, status, acb->scan_devices);
3234 break;
3235 case UNIT_ATTENTION:
3236 dprintkl(KERN_DEBUG,
3237 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3238 cmd->cmnd[0], dcb->target_id,
3239 dcb->target_lun, status, acb->scan_devices);
3240 break;
3241 case ILLEGAL_REQUEST:
3242 dprintkl(KERN_DEBUG,
3243 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3244 cmd->cmnd[0], dcb->target_id,
3245 dcb->target_lun, status, acb->scan_devices);
3246 break;
3247 case MEDIUM_ERROR:
3248 dprintkl(KERN_DEBUG,
3249 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3250 cmd->cmnd[0], dcb->target_id,
3251 dcb->target_lun, status, acb->scan_devices);
3252 break;
3253 case HARDWARE_ERROR:
3254 dprintkl(KERN_DEBUG,
3255 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3256 cmd->cmnd[0], dcb->target_id,
3257 dcb->target_lun, status, acb->scan_devices);
3258 break;
3260 if (cmd->sense_buffer[7] >= 6)
3261 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3262 "(0x%08x 0x%08x)\n",
3263 cmd->sense_buffer[2], cmd->sense_buffer[12],
3264 cmd->sense_buffer[13],
3265 *((unsigned int *)(cmd->sense_buffer + 3)),
3266 *((unsigned int *)(cmd->sense_buffer + 8)));
3267 else
3268 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3269 cmd->sense_buffer[2],
3270 *((unsigned int *)(cmd->sense_buffer + 3)));
3273 if (status == (CHECK_CONDITION << 1)) {
3274 cmd->result = DID_BAD_TARGET << 16;
3275 goto ckc_e;
3277 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
3279 if (srb->total_xfer_length
3280 && srb->total_xfer_length >= cmd->underflow)
3281 cmd->result =
3282 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3283 srb->end_message, CHECK_CONDITION);
3284 /*SET_RES_DID(cmd->result,DID_OK) */
3285 else
3286 cmd->result =
3287 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3288 srb->end_message, CHECK_CONDITION);
3290 goto ckc_e;
3293 /*************************************************************/
3294 if (status) {
3296 * target status..........................
3298 if (status_byte(status) == CHECK_CONDITION) {
3299 request_sense(acb, dcb, srb);
3300 return;
3301 } else if (status_byte(status) == QUEUE_FULL) {
3302 tempcnt = (u8)list_size(&dcb->srb_going_list);
3303 dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3304 dcb->target_id, dcb->target_lun, tempcnt);
3305 if (tempcnt > 1)
3306 tempcnt--;
3307 dcb->max_command = tempcnt;
3308 free_tag(dcb, srb);
3309 srb_going_to_waiting_move(dcb, srb);
3310 waiting_set_timer(acb, HZ / 20);
3311 srb->adapter_status = 0;
3312 srb->target_status = 0;
3313 return;
3314 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
3315 srb->adapter_status = H_SEL_TIMEOUT;
3316 srb->target_status = 0;
3317 cmd->result = DID_NO_CONNECT << 16;
3318 } else {
3319 srb->adapter_status = 0;
3320 SET_RES_DID(cmd->result, DID_ERROR);
3321 SET_RES_MSG(cmd->result, srb->end_message);
3322 SET_RES_TARGET(cmd->result, status);
3325 } else {
3327 ** process initiator status..........................
3329 status = srb->adapter_status;
3330 if (status & H_OVER_UNDER_RUN) {
3331 srb->target_status = 0;
3332 SET_RES_DID(cmd->result, DID_OK);
3333 SET_RES_MSG(cmd->result, srb->end_message);
3334 } else if (srb->status & PARITY_ERROR) {
3335 SET_RES_DID(cmd->result, DID_PARITY);
3336 SET_RES_MSG(cmd->result, srb->end_message);
3337 } else { /* No error */
3339 srb->adapter_status = 0;
3340 srb->target_status = 0;
3341 SET_RES_DID(cmd->result, DID_OK);
3345 if (dir != PCI_DMA_NONE && scsi_sg_count(cmd))
3346 pci_dma_sync_sg_for_cpu(acb->dev, scsi_sglist(cmd),
3347 scsi_sg_count(cmd), dir);
3349 ckc_only = 0;
3350 /* Check Error Conditions */
3351 ckc_e:
3353 if (cmd->cmnd[0] == INQUIRY) {
3354 unsigned char *base = NULL;
3355 struct ScsiInqData *ptr;
3356 unsigned long flags = 0;
3357 struct scatterlist* sg = scsi_sglist(cmd);
3358 size_t offset = 0, len = sizeof(struct ScsiInqData);
3360 local_irq_save(flags);
3361 base = scsi_kmap_atomic_sg(sg, scsi_sg_count(cmd), &offset, &len);
3362 ptr = (struct ScsiInqData *)(base + offset);
3364 if (!ckc_only && (cmd->result & RES_DID) == 0
3365 && cmd->cmnd[2] == 0 && scsi_bufflen(cmd) >= 8
3366 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
3367 dcb->inquiry7 = ptr->Flags;
3369 /*if( srb->cmd->cmnd[0] == INQUIRY && */
3370 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3371 if ((cmd->result == (DID_OK << 16)
3372 || status_byte(cmd->result) &
3373 CHECK_CONDITION)) {
3374 if (!dcb->init_tcq_flag) {
3375 add_dev(acb, dcb, ptr);
3376 dcb->init_tcq_flag = 1;
3380 scsi_kunmap_atomic_sg(base);
3381 local_irq_restore(flags);
3384 /* Here is the info for Doug Gilbert's sg3 ... */
3385 scsi_set_resid(cmd, srb->total_xfer_length);
3386 /* This may be interpreted by sb. or not ... */
3387 cmd->SCp.this_residual = srb->total_xfer_length;
3388 cmd->SCp.buffers_residual = 0;
3389 if (debug_enabled(DBG_KG)) {
3390 if (srb->total_xfer_length)
3391 dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
3392 "cmnd=0x%02x Missed %i bytes\n",
3393 cmd->serial_number, cmd->device->id, cmd->device->lun,
3394 cmd->cmnd[0], srb->total_xfer_length);
3397 srb_going_remove(dcb, srb);
3398 /* Add to free list */
3399 if (srb == acb->tmp_srb)
3400 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3401 else {
3402 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
3403 cmd->serial_number, cmd->result);
3404 srb_free_insert(acb, srb);
3406 pci_unmap_srb(acb, srb);
3408 cmd->scsi_done(cmd);
3409 waiting_process_next(acb);
3413 /* abort all cmds in our queues */
3414 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
3415 struct scsi_cmnd *cmd, u8 force)
3417 struct DeviceCtlBlk *dcb;
3418 dprintkl(KERN_INFO, "doing_srb_done: pids ");
3420 list_for_each_entry(dcb, &acb->dcb_list, list) {
3421 struct ScsiReqBlk *srb;
3422 struct ScsiReqBlk *tmp;
3423 struct scsi_cmnd *p;
3425 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
3426 enum dma_data_direction dir;
3427 int result;
3429 p = srb->cmd;
3430 dir = p->sc_data_direction;
3431 result = MK_RES(0, did_flag, 0, 0);
3432 printk("G:%li(%02i-%i) ", p->serial_number,
3433 p->device->id, p->device->lun);
3434 srb_going_remove(dcb, srb);
3435 free_tag(dcb, srb);
3436 srb_free_insert(acb, srb);
3437 p->result = result;
3438 pci_unmap_srb_sense(acb, srb);
3439 pci_unmap_srb(acb, srb);
3440 if (force) {
3441 /* For new EH, we normally don't need to give commands back,
3442 * as they all complete or all time out */
3443 p->scsi_done(p);
3446 if (!list_empty(&dcb->srb_going_list))
3447 dprintkl(KERN_DEBUG,
3448 "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3449 dcb->target_id, dcb->target_lun);
3450 if (dcb->tag_mask)
3451 dprintkl(KERN_DEBUG,
3452 "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3453 dcb->target_id, dcb->target_lun,
3454 dcb->tag_mask);
3456 /* Waiting queue */
3457 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
3458 int result;
3459 p = srb->cmd;
3461 result = MK_RES(0, did_flag, 0, 0);
3462 printk("W:%li<%02i-%i>", p->serial_number, p->device->id,
3463 p->device->lun);
3464 srb_waiting_remove(dcb, srb);
3465 srb_free_insert(acb, srb);
3466 p->result = result;
3467 pci_unmap_srb_sense(acb, srb);
3468 pci_unmap_srb(acb, srb);
3469 if (force) {
3470 /* For new EH, we normally don't need to give commands back,
3471 * as they all complete or all time out */
3472 cmd->scsi_done(cmd);
3475 if (!list_empty(&dcb->srb_waiting_list))
3476 dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
3477 list_size(&dcb->srb_waiting_list), dcb->target_id,
3478 dcb->target_lun);
3479 dcb->flag &= ~ABORT_DEV_;
3481 printk("\n");
3485 static void reset_scsi_bus(struct AdapterCtlBlk *acb)
3487 dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
3488 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
3489 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
3491 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
3492 /* nothing */;
3496 static void set_basic_config(struct AdapterCtlBlk *acb)
3498 u8 bval;
3499 u16 wval;
3500 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
3501 if (acb->config & HCC_PARITY)
3502 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
3503 else
3504 bval = PHASELATCH | INITIATOR | BLOCKRST;
3506 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
3508 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3509 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
3510 /* program Host ID */
3511 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
3512 /* set ansynchronous transfer */
3513 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
3514 /* Turn LED control off */
3515 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
3516 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
3517 /* DMA config */
3518 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
3519 wval |=
3520 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
3521 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
3522 /* Clear pending interrupt status */
3523 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
3524 /* Enable SCSI interrupt */
3525 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
3526 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
3527 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3532 static void scsi_reset_detect(struct AdapterCtlBlk *acb)
3534 dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
3535 /* delay half a second */
3536 if (timer_pending(&acb->waiting_timer))
3537 del_timer(&acb->waiting_timer);
3539 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
3540 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
3541 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3542 udelay(500);
3543 /* Maybe we locked up the bus? Then lets wait even longer ... */
3544 acb->scsi_host->last_reset =
3545 jiffies + 5 * HZ / 2 +
3546 HZ * acb->eeprom.delay_time;
3548 clear_fifo(acb, "scsi_reset_detect");
3549 set_basic_config(acb);
3550 /*1.25 */
3551 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3553 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
3554 acb->acb_flag |= RESET_DONE;
3555 } else {
3556 acb->acb_flag |= RESET_DETECT;
3557 reset_dev_param(acb);
3558 doing_srb_done(acb, DID_RESET, NULL, 1);
3559 /*DC395x_RecoverSRB( acb ); */
3560 acb->active_dcb = NULL;
3561 acb->acb_flag = 0;
3562 waiting_process_next(acb);
3567 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3568 struct ScsiReqBlk *srb)
3570 struct scsi_cmnd *cmd = srb->cmd;
3571 dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
3572 cmd->serial_number, cmd->device->id, cmd->device->lun);
3574 srb->flag |= AUTO_REQSENSE;
3575 srb->adapter_status = 0;
3576 srb->target_status = 0;
3578 /* KG: Can this prevent crap sense data ? */
3579 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
3581 /* Save some data */
3582 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
3583 srb->segment_x[0].address;
3584 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
3585 srb->segment_x[0].length;
3586 srb->xferred = srb->total_xfer_length;
3587 /* srb->segment_x : a one entry of S/G list table */
3588 srb->total_xfer_length = SCSI_SENSE_BUFFERSIZE;
3589 srb->segment_x[0].length = SCSI_SENSE_BUFFERSIZE;
3590 /* Map sense buffer */
3591 srb->segment_x[0].address =
3592 pci_map_single(acb->dev, cmd->sense_buffer,
3593 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
3594 dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
3595 cmd->sense_buffer, srb->segment_x[0].address,
3596 SCSI_SENSE_BUFFERSIZE);
3597 srb->sg_count = 1;
3598 srb->sg_index = 0;
3600 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
3601 dprintkl(KERN_DEBUG,
3602 "request_sense: (pid#%li) failed <%02i-%i>\n",
3603 srb->cmd->serial_number, dcb->target_id, dcb->target_lun);
3604 srb_going_to_waiting_move(dcb, srb);
3605 waiting_set_timer(acb, HZ / 100);
3611 * device_alloc - Allocate a new device instance. This create the
3612 * devices instance and sets up all the data items. The adapter
3613 * instance is required to obtain confiuration information for this
3614 * device. This does *not* add this device to the adapters device
3615 * list.
3617 * @acb: The adapter to obtain configuration information from.
3618 * @target: The target for the new device.
3619 * @lun: The lun for the new device.
3621 * Return the new device if successful or NULL on failure.
3623 static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
3624 u8 target, u8 lun)
3626 struct NvRamType *eeprom = &acb->eeprom;
3627 u8 period_index = eeprom->target[target].period & 0x07;
3628 struct DeviceCtlBlk *dcb;
3630 dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
3631 dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
3632 if (!dcb)
3633 return NULL;
3634 dcb->acb = NULL;
3635 INIT_LIST_HEAD(&dcb->srb_going_list);
3636 INIT_LIST_HEAD(&dcb->srb_waiting_list);
3637 dcb->active_srb = NULL;
3638 dcb->tag_mask = 0;
3639 dcb->max_command = 1;
3640 dcb->target_id = target;
3641 dcb->target_lun = lun;
3642 #ifndef DC395x_NO_DISCONNECT
3643 dcb->identify_msg =
3644 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
3645 #else
3646 dcb->identify_msg = IDENTIFY(0, lun);
3647 #endif
3648 dcb->dev_mode = eeprom->target[target].cfg0;
3649 dcb->inquiry7 = 0;
3650 dcb->sync_mode = 0;
3651 dcb->min_nego_period = clock_period[period_index];
3652 dcb->sync_period = 0;
3653 dcb->sync_offset = 0;
3654 dcb->flag = 0;
3656 #ifndef DC395x_NO_WIDE
3657 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
3658 && (acb->config & HCC_WIDE_CARD))
3659 dcb->sync_mode |= WIDE_NEGO_ENABLE;
3660 #endif
3661 #ifndef DC395x_NO_SYNC
3662 if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
3663 if (!(lun) || current_sync_offset)
3664 dcb->sync_mode |= SYNC_NEGO_ENABLE;
3665 #endif
3666 if (dcb->target_lun != 0) {
3667 /* Copy settings */
3668 struct DeviceCtlBlk *p;
3669 list_for_each_entry(p, &acb->dcb_list, list)
3670 if (p->target_id == dcb->target_id)
3671 break;
3672 dprintkdbg(DBG_1,
3673 "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3674 dcb->target_id, dcb->target_lun,
3675 p->target_id, p->target_lun);
3676 dcb->sync_mode = p->sync_mode;
3677 dcb->sync_period = p->sync_period;
3678 dcb->min_nego_period = p->min_nego_period;
3679 dcb->sync_offset = p->sync_offset;
3680 dcb->inquiry7 = p->inquiry7;
3682 return dcb;
3687 * adapter_add_device - Adds the device instance to the adaptor instance.
3689 * @acb: The adapter device to be updated
3690 * @dcb: A newly created and intialised device instance to add.
3692 static void adapter_add_device(struct AdapterCtlBlk *acb,
3693 struct DeviceCtlBlk *dcb)
3695 /* backpointer to adapter */
3696 dcb->acb = acb;
3698 /* set run_robin to this device if it is currently empty */
3699 if (list_empty(&acb->dcb_list))
3700 acb->dcb_run_robin = dcb;
3702 /* add device to list */
3703 list_add_tail(&dcb->list, &acb->dcb_list);
3705 /* update device maps */
3706 acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
3707 acb->children[dcb->target_id][dcb->target_lun] = dcb;
3712 * adapter_remove_device - Removes the device instance from the adaptor
3713 * instance. The device instance is not check in any way or freed by this.
3714 * The caller is expected to take care of that. This will simply remove the
3715 * device from the adapters data strcutures.
3717 * @acb: The adapter device to be updated
3718 * @dcb: A device that has previously been added to the adapter.
3720 static void adapter_remove_device(struct AdapterCtlBlk *acb,
3721 struct DeviceCtlBlk *dcb)
3723 struct DeviceCtlBlk *i;
3724 struct DeviceCtlBlk *tmp;
3725 dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
3726 dcb->target_id, dcb->target_lun);
3728 /* fix up any pointers to this device that we have in the adapter */
3729 if (acb->active_dcb == dcb)
3730 acb->active_dcb = NULL;
3731 if (acb->dcb_run_robin == dcb)
3732 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
3734 /* unlink from list */
3735 list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
3736 if (dcb == i) {
3737 list_del(&i->list);
3738 break;
3741 /* clear map and children */
3742 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
3743 acb->children[dcb->target_id][dcb->target_lun] = NULL;
3744 dcb->acb = NULL;
3749 * adapter_remove_and_free_device - Removes a single device from the adapter
3750 * and then frees the device information.
3752 * @acb: The adapter device to be updated
3753 * @dcb: A device that has previously been added to the adapter.
3755 static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
3756 struct DeviceCtlBlk *dcb)
3758 if (list_size(&dcb->srb_going_list) > 1) {
3759 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
3760 "Won't remove because of %i active requests.\n",
3761 dcb->target_id, dcb->target_lun,
3762 list_size(&dcb->srb_going_list));
3763 return;
3765 adapter_remove_device(acb, dcb);
3766 kfree(dcb);
3771 * adapter_remove_and_free_all_devices - Removes and frees all of the
3772 * devices associated with the specified adapter.
3774 * @acb: The adapter from which all devices should be removed.
3776 static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
3778 struct DeviceCtlBlk *dcb;
3779 struct DeviceCtlBlk *tmp;
3780 dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
3781 list_size(&acb->dcb_list));
3783 list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
3784 adapter_remove_and_free_device(acb, dcb);
3789 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3790 * scsi device that we need to deal with. We allocate a new device and then
3791 * insert that device into the adapters device list.
3793 * @scsi_device: The new scsi device that we need to handle.
3795 static int dc395x_slave_alloc(struct scsi_device *scsi_device)
3797 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3798 struct DeviceCtlBlk *dcb;
3800 dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
3801 if (!dcb)
3802 return -ENOMEM;
3803 adapter_add_device(acb, dcb);
3805 return 0;
3810 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3811 * device that is going away.
3813 * @scsi_device: The new scsi device that we need to handle.
3815 static void dc395x_slave_destroy(struct scsi_device *scsi_device)
3817 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3818 struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
3819 if (dcb)
3820 adapter_remove_and_free_device(acb, dcb);
3827 * trms1040_wait_30us: wait for 30 us
3829 * Waits for 30us (using the chip by the looks of it..)
3831 * @io_port: base I/O address
3833 static void __devinit trms1040_wait_30us(unsigned long io_port)
3835 /* ScsiPortStallExecution(30); wait 30 us */
3836 outb(5, io_port + TRM_S1040_GEN_TIMER);
3837 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
3838 /* nothing */ ;
3843 * trms1040_write_cmd - write the secified command and address to
3844 * chip
3846 * @io_port: base I/O address
3847 * @cmd: SB + op code (command) to send
3848 * @addr: address to send
3850 static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
3852 int i;
3853 u8 send_data;
3855 /* program SB + OP code */
3856 for (i = 0; i < 3; i++, cmd <<= 1) {
3857 send_data = NVR_SELECT;
3858 if (cmd & 0x04) /* Start from bit 2 */
3859 send_data |= NVR_BITOUT;
3861 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3862 trms1040_wait_30us(io_port);
3863 outb((send_data | NVR_CLOCK),
3864 io_port + TRM_S1040_GEN_NVRAM);
3865 trms1040_wait_30us(io_port);
3868 /* send address */
3869 for (i = 0; i < 7; i++, addr <<= 1) {
3870 send_data = NVR_SELECT;
3871 if (addr & 0x40) /* Start from bit 6 */
3872 send_data |= NVR_BITOUT;
3874 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3875 trms1040_wait_30us(io_port);
3876 outb((send_data | NVR_CLOCK),
3877 io_port + TRM_S1040_GEN_NVRAM);
3878 trms1040_wait_30us(io_port);
3880 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
3881 trms1040_wait_30us(io_port);
3886 * trms1040_set_data - store a single byte in the eeprom
3888 * Called from write all to write a single byte into the SSEEPROM
3889 * Which is done one bit at a time.
3891 * @io_port: base I/O address
3892 * @addr: offset into EEPROM
3893 * @byte: bytes to write
3895 static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
3897 int i;
3898 u8 send_data;
3900 /* Send write command & address */
3901 trms1040_write_cmd(io_port, 0x05, addr);
3903 /* Write data */
3904 for (i = 0; i < 8; i++, byte <<= 1) {
3905 send_data = NVR_SELECT;
3906 if (byte & 0x80) /* Start from bit 7 */
3907 send_data |= NVR_BITOUT;
3909 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3910 trms1040_wait_30us(io_port);
3911 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
3912 trms1040_wait_30us(io_port);
3914 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
3915 trms1040_wait_30us(io_port);
3917 /* Disable chip select */
3918 outb(0, io_port + TRM_S1040_GEN_NVRAM);
3919 trms1040_wait_30us(io_port);
3921 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
3922 trms1040_wait_30us(io_port);
3924 /* Wait for write ready */
3925 while (1) {
3926 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
3927 trms1040_wait_30us(io_port);
3929 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
3930 trms1040_wait_30us(io_port);
3932 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
3933 break;
3936 /* Disable chip select */
3937 outb(0, io_port + TRM_S1040_GEN_NVRAM);
3942 * trms1040_write_all - write 128 bytes to the eeprom
3944 * Write the supplied 128 bytes to the chips SEEPROM
3946 * @eeprom: the data to write
3947 * @io_port: the base io port
3949 static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
3951 u8 *b_eeprom = (u8 *)eeprom;
3952 u8 addr;
3954 /* Enable SEEPROM */
3955 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
3956 io_port + TRM_S1040_GEN_CONTROL);
3958 /* write enable */
3959 trms1040_write_cmd(io_port, 0x04, 0xFF);
3960 outb(0, io_port + TRM_S1040_GEN_NVRAM);
3961 trms1040_wait_30us(io_port);
3963 /* write */
3964 for (addr = 0; addr < 128; addr++, b_eeprom++)
3965 trms1040_set_data(io_port, addr, *b_eeprom);
3967 /* write disable */
3968 trms1040_write_cmd(io_port, 0x04, 0x00);
3969 outb(0, io_port + TRM_S1040_GEN_NVRAM);
3970 trms1040_wait_30us(io_port);
3972 /* Disable SEEPROM */
3973 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
3974 io_port + TRM_S1040_GEN_CONTROL);
3979 * trms1040_get_data - get a single byte from the eeprom
3981 * Called from read all to read a single byte into the SSEEPROM
3982 * Which is done one bit at a time.
3984 * @io_port: base I/O address
3985 * @addr: offset into SEEPROM
3987 * Returns the byte read.
3989 static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
3991 int i;
3992 u8 read_byte;
3993 u8 result = 0;
3995 /* Send read command & address */
3996 trms1040_write_cmd(io_port, 0x06, addr);
3998 /* read data */
3999 for (i = 0; i < 8; i++) {
4000 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4001 trms1040_wait_30us(io_port);
4002 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4004 /* Get data bit while falling edge */
4005 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
4006 result <<= 1;
4007 if (read_byte & NVR_BITIN)
4008 result |= 1;
4010 trms1040_wait_30us(io_port);
4013 /* Disable chip select */
4014 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4015 return result;
4020 * trms1040_read_all - read all bytes from the eeprom
4022 * Read the 128 bytes from the SEEPROM.
4024 * @eeprom: where to store the data
4025 * @io_port: the base io port
4027 static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
4029 u8 *b_eeprom = (u8 *)eeprom;
4030 u8 addr;
4032 /* Enable SEEPROM */
4033 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4034 io_port + TRM_S1040_GEN_CONTROL);
4036 /* read details */
4037 for (addr = 0; addr < 128; addr++, b_eeprom++)
4038 *b_eeprom = trms1040_get_data(io_port, addr);
4040 /* Disable SEEPROM */
4041 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4042 io_port + TRM_S1040_GEN_CONTROL);
4048 * check_eeprom - get and check contents of the eeprom
4050 * Read seeprom 128 bytes into the memory provider in eeprom.
4051 * Checks the checksum and if it's not correct it uses a set of default
4052 * values.
4054 * @eeprom: caller allocated strcuture to read the eeprom data into
4055 * @io_port: io port to read from
4057 static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
4059 u16 *w_eeprom = (u16 *)eeprom;
4060 u16 w_addr;
4061 u16 cksum;
4062 u32 d_addr;
4063 u32 *d_eeprom;
4065 trms1040_read_all(eeprom, io_port); /* read eeprom */
4067 cksum = 0;
4068 for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
4069 w_addr++, w_eeprom++)
4070 cksum += *w_eeprom;
4071 if (cksum != 0x1234) {
4073 * Checksum is wrong.
4074 * Load a set of defaults into the eeprom buffer
4076 dprintkl(KERN_WARNING,
4077 "EEProm checksum error: using default values and options.\n");
4078 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4079 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4080 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4081 eeprom->sub_sys_id[1] =
4082 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4083 eeprom->sub_class = 0x00;
4084 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4085 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4086 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4087 eeprom->device_id[1] =
4088 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4089 eeprom->reserved = 0x00;
4091 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
4092 d_addr < 16; d_addr++, d_eeprom++)
4093 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
4095 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
4096 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
4097 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
4098 *d_eeprom = 0x00;
4100 /* Now load defaults (maybe set by boot/module params) */
4101 set_safe_settings();
4102 fix_settings();
4103 eeprom_override(eeprom);
4105 eeprom->cksum = 0x00;
4106 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
4107 w_addr < 63; w_addr++, w_eeprom++)
4108 cksum += *w_eeprom;
4110 *w_eeprom = 0x1234 - cksum;
4111 trms1040_write_all(eeprom, io_port);
4112 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
4113 } else {
4114 set_safe_settings();
4115 eeprom_index_to_delay(eeprom);
4116 eeprom_override(eeprom);
4122 * print_eeprom_settings - output the eeprom settings
4123 * to the kernel log so people can see what they were.
4125 * @eeprom: The eeprom data strucutre to show details for.
4127 static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
4129 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4130 eeprom->scsi_id,
4131 eeprom->target[0].period,
4132 clock_speed[eeprom->target[0].period] / 10,
4133 clock_speed[eeprom->target[0].period] % 10,
4134 eeprom->target[0].cfg0);
4135 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4136 eeprom->channel_cfg, eeprom->max_tag,
4137 1 << eeprom->max_tag, eeprom->delay_time);
4141 /* Free SG tables */
4142 static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
4144 int i;
4145 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4147 for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
4148 kfree(acb->srb_array[i].segment_x);
4153 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4154 * should never cross a page boundary */
4155 static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
4157 const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
4158 *SEGMENTX_LEN;
4159 int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
4160 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4161 int srb_idx = 0;
4162 unsigned i = 0;
4163 struct SGentry *uninitialized_var(ptr);
4165 for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
4166 acb->srb_array[i].segment_x = NULL;
4168 dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
4169 while (pages--) {
4170 ptr = kmalloc(PAGE_SIZE, GFP_KERNEL);
4171 if (!ptr) {
4172 adapter_sg_tables_free(acb);
4173 return 1;
4175 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
4176 PAGE_SIZE, ptr, srb_idx);
4177 i = 0;
4178 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4179 acb->srb_array[srb_idx++].segment_x =
4180 ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4182 if (i < srbs_per_page)
4183 acb->srb.segment_x =
4184 ptr + (i * DC395x_MAX_SG_LISTENTRY);
4185 else
4186 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
4187 return 0;
4193 * adapter_print_config - print adapter connection and termination
4194 * config
4196 * The io port in the adapter needs to have been set before calling
4197 * this function.
4199 * @acb: The adapter to print the information for.
4201 static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
4203 u8 bval;
4205 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
4206 dprintkl(KERN_INFO, "%sConnectors: ",
4207 ((bval & WIDESCSI) ? "(Wide) " : ""));
4208 if (!(bval & CON5068))
4209 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
4210 if (!(bval & CON68))
4211 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
4212 if (!(bval & CON50))
4213 printk("int50 ");
4214 if ((bval & (CON5068 | CON50 | CON68)) ==
4215 0 /*(CON5068 | CON50 | CON68) */ )
4216 printk(" Oops! (All 3?) ");
4217 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
4218 printk(" Termination: ");
4219 if (bval & DIS_TERM)
4220 printk("Disabled\n");
4221 else {
4222 if (bval & AUTOTERM)
4223 printk("Auto ");
4224 if (bval & LOW8TERM)
4225 printk("Low ");
4226 if (bval & UP8TERM)
4227 printk("High ");
4228 printk("\n");
4234 * adapter_init_params - Initialize the various parameters in the
4235 * adapter structure. Note that the pointer to the scsi_host is set
4236 * early (when this instance is created) and the io_port and irq
4237 * values are set later after they have been reserved. This just gets
4238 * everything set to a good starting position.
4240 * The eeprom structure in the adapter needs to have been set before
4241 * calling this function.
4243 * @acb: The adapter to initialize.
4245 static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
4247 struct NvRamType *eeprom = &acb->eeprom;
4248 int i;
4250 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4251 /* NOTE: acb->io_port_base is set at port registration time */
4252 /* NOTE: acb->io_port_len is set at port registration time */
4254 INIT_LIST_HEAD(&acb->dcb_list);
4255 acb->dcb_run_robin = NULL;
4256 acb->active_dcb = NULL;
4258 INIT_LIST_HEAD(&acb->srb_free_list);
4259 /* temp SRB for Q tag used or abort command used */
4260 acb->tmp_srb = &acb->srb;
4261 init_timer(&acb->waiting_timer);
4262 init_timer(&acb->selto_timer);
4264 acb->srb_count = DC395x_MAX_SRB_CNT;
4266 acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */
4267 /* NOTE: acb->irq_level is set at IRQ registration time */
4269 acb->tag_max_num = 1 << eeprom->max_tag;
4270 if (acb->tag_max_num > 30)
4271 acb->tag_max_num = 30;
4273 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4274 acb->gmode2 = eeprom->channel_cfg;
4275 acb->config = 0; /* NOTE: actually set in adapter_init_chip */
4277 if (eeprom->channel_cfg & NAC_SCANLUN)
4278 acb->lun_chk = 1;
4279 acb->scan_devices = 1;
4281 acb->scsi_host->this_id = eeprom->scsi_id;
4282 acb->hostid_bit = (1 << acb->scsi_host->this_id);
4284 for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
4285 acb->dcb_map[i] = 0;
4287 acb->msg_len = 0;
4289 /* link static array of srbs into the srb free list */
4290 for (i = 0; i < acb->srb_count - 1; i++)
4291 srb_free_insert(acb, &acb->srb_array[i]);
4296 * adapter_init_host - Initialize the scsi host instance based on
4297 * values that we have already stored in the adapter instance. There's
4298 * some mention that a lot of these are deprecated, so we won't use
4299 * them (we'll use the ones in the adapter instance) but we'll fill
4300 * them in in case something else needs them.
4302 * The eeprom structure, irq and io ports in the adapter need to have
4303 * been set before calling this function.
4305 * @host: The scsi host instance to fill in the values for.
4307 static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
4309 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4310 struct NvRamType *eeprom = &acb->eeprom;
4312 host->max_cmd_len = 24;
4313 host->can_queue = DC395x_MAX_CMD_QUEUE;
4314 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4315 host->this_id = (int)eeprom->scsi_id;
4316 host->io_port = acb->io_port_base;
4317 host->n_io_port = acb->io_port_len;
4318 host->dma_channel = -1;
4319 host->unique_id = acb->io_port_base;
4320 host->irq = acb->irq_level;
4321 host->last_reset = jiffies;
4323 host->max_id = 16;
4324 if (host->max_id - 1 == eeprom->scsi_id)
4325 host->max_id--;
4327 #ifdef CONFIG_SCSI_MULTI_LUN
4328 if (eeprom->channel_cfg & NAC_SCANLUN)
4329 host->max_lun = 8;
4330 else
4331 host->max_lun = 1;
4332 #else
4333 host->max_lun = 1;
4334 #endif
4340 * adapter_init_chip - Get the chip into a know state and figure out
4341 * some of the settings that apply to this adapter.
4343 * The io port in the adapter needs to have been set before calling
4344 * this function. The config will be configured correctly on return.
4346 * @acb: The adapter which we are to init.
4348 static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
4350 struct NvRamType *eeprom = &acb->eeprom;
4352 /* Mask all the interrupt */
4353 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
4354 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
4356 /* Reset SCSI module */
4357 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4359 /* Reset PCI/DMA module */
4360 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4361 udelay(20);
4363 /* program configuration 0 */
4364 acb->config = HCC_AUTOTERM | HCC_PARITY;
4365 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
4366 acb->config |= HCC_WIDE_CARD;
4368 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
4369 acb->config |= HCC_SCSI_RESET;
4371 if (acb->config & HCC_SCSI_RESET) {
4372 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
4373 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4375 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4376 /*spin_unlock_irq (&io_request_lock); */
4377 udelay(500);
4379 acb->scsi_host->last_reset =
4380 jiffies + HZ / 2 +
4381 HZ * acb->eeprom.delay_time;
4383 /*spin_lock_irq (&io_request_lock); */
4389 * init_adapter - Grab the resource for the card, setup the adapter
4390 * information, set the card into a known state, create the various
4391 * tables etc etc. This basically gets all adapter information all up
4392 * to date, intialised and gets the chip in sync with it.
4394 * @host: This hosts adapter structure
4395 * @io_port: The base I/O port
4396 * @irq: IRQ
4398 * Returns 0 if the initialization succeeds, any other value on
4399 * failure.
4401 static int __devinit adapter_init(struct AdapterCtlBlk *acb,
4402 unsigned long io_port, u32 io_port_len, unsigned int irq)
4404 if (!request_region(io_port, io_port_len, DC395X_NAME)) {
4405 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
4406 goto failed;
4408 /* store port base to indicate we have registered it */
4409 acb->io_port_base = io_port;
4410 acb->io_port_len = io_port_len;
4412 if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
4413 /* release the region we just claimed */
4414 dprintkl(KERN_INFO, "Failed to register IRQ\n");
4415 goto failed;
4417 /* store irq to indicate we have registered it */
4418 acb->irq_level = irq;
4420 /* get eeprom configuration information and command line settings etc */
4421 check_eeprom(&acb->eeprom, io_port);
4422 print_eeprom_settings(&acb->eeprom);
4424 /* setup adapter control block */
4425 adapter_init_params(acb);
4427 /* display card connectors/termination settings */
4428 adapter_print_config(acb);
4430 if (adapter_sg_tables_alloc(acb)) {
4431 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
4432 goto failed;
4434 adapter_init_scsi_host(acb->scsi_host);
4435 adapter_init_chip(acb);
4436 set_basic_config(acb);
4438 dprintkdbg(DBG_0,
4439 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4440 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4441 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
4442 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
4443 return 0;
4445 failed:
4446 if (acb->irq_level)
4447 free_irq(acb->irq_level, acb);
4448 if (acb->io_port_base)
4449 release_region(acb->io_port_base, acb->io_port_len);
4450 adapter_sg_tables_free(acb);
4452 return 1;
4457 * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4458 * stopping all operations and disabling interrupt generation on the
4459 * card.
4461 * @acb: The adapter which we are to shutdown.
4463 static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
4465 /* disable interrupts */
4466 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
4467 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
4469 /* reset the scsi bus */
4470 if (acb->config & HCC_SCSI_RESET)
4471 reset_scsi_bus(acb);
4473 /* clear any pending interrupt state */
4474 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4480 * adapter_uninit - Shut down the chip and release any resources that
4481 * we had allocated. Once this returns the adapter should not be used
4482 * anymore.
4484 * @acb: The adapter which we are to un-initialize.
4486 static void adapter_uninit(struct AdapterCtlBlk *acb)
4488 unsigned long flags;
4489 DC395x_LOCK_IO(acb->scsi_host, flags);
4491 /* remove timers */
4492 if (timer_pending(&acb->waiting_timer))
4493 del_timer(&acb->waiting_timer);
4494 if (timer_pending(&acb->selto_timer))
4495 del_timer(&acb->selto_timer);
4497 adapter_uninit_chip(acb);
4498 adapter_remove_and_free_all_devices(acb);
4499 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4501 if (acb->irq_level)
4502 free_irq(acb->irq_level, acb);
4503 if (acb->io_port_base)
4504 release_region(acb->io_port_base, acb->io_port_len);
4506 adapter_sg_tables_free(acb);
4510 #undef SPRINTF
4511 #define SPRINTF(args...) pos += sprintf(pos, args)
4513 #undef YESNO
4514 #define YESNO(YN) \
4515 if (YN) SPRINTF(" Yes ");\
4516 else SPRINTF(" No ")
4518 static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
4519 char **start, off_t offset, int length, int inout)
4521 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4522 int spd, spd1;
4523 char *pos = buffer;
4524 struct DeviceCtlBlk *dcb;
4525 unsigned long flags;
4526 int dev;
4528 if (inout) /* Has data been written to the file ? */
4529 return -EPERM;
4531 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
4532 SPRINTF(" Driver Version " DC395X_VERSION "\n");
4534 DC395x_LOCK_IO(acb->scsi_host, flags);
4536 SPRINTF("SCSI Host Nr %i, ", host->host_no);
4537 SPRINTF("DC395U/UW/F DC315/U %s\n",
4538 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
4539 SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
4540 SPRINTF("irq_level 0x%04x, ", acb->irq_level);
4541 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
4543 SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
4544 SPRINTF("AdapterID %i\n", host->this_id);
4546 SPRINTF("tag_max_num %i", acb->tag_max_num);
4547 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4548 SPRINTF(", FilterCfg 0x%02x",
4549 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
4550 SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
4551 /*SPRINTF("\n"); */
4553 SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4554 SPRINTF
4555 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
4556 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
4557 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
4558 acb->dcb_map[6], acb->dcb_map[7]);
4559 SPRINTF
4560 (" %02x %02x %02x %02x %02x %02x %02x %02x\n",
4561 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
4562 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
4563 acb->dcb_map[14], acb->dcb_map[15]);
4565 SPRINTF
4566 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4568 dev = 0;
4569 list_for_each_entry(dcb, &acb->dcb_list, list) {
4570 int nego_period;
4571 SPRINTF("%02i %02i %02i ", dev, dcb->target_id,
4572 dcb->target_lun);
4573 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
4574 YESNO(dcb->sync_offset);
4575 YESNO(dcb->sync_period & WIDE_SYNC);
4576 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
4577 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
4578 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
4579 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
4580 if (dcb->sync_offset)
4581 SPRINTF(" %03i ns ", nego_period);
4582 else
4583 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
4585 if (dcb->sync_offset & 0x0f) {
4586 spd = 1000 / (nego_period);
4587 spd1 = 1000 % (nego_period);
4588 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
4589 SPRINTF(" %2i.%1i M %02i ", spd, spd1,
4590 (dcb->sync_offset & 0x0f));
4591 } else
4592 SPRINTF(" ");
4594 /* Add more info ... */
4595 SPRINTF(" %02i\n", dcb->max_command);
4596 dev++;
4599 if (timer_pending(&acb->waiting_timer))
4600 SPRINTF("Waiting queue timer running\n");
4601 else
4602 SPRINTF("\n");
4604 list_for_each_entry(dcb, &acb->dcb_list, list) {
4605 struct ScsiReqBlk *srb;
4606 if (!list_empty(&dcb->srb_waiting_list))
4607 SPRINTF("DCB (%02i-%i): Waiting: %i:",
4608 dcb->target_id, dcb->target_lun,
4609 list_size(&dcb->srb_waiting_list));
4610 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
4611 SPRINTF(" %li", srb->cmd->serial_number);
4612 if (!list_empty(&dcb->srb_going_list))
4613 SPRINTF("\nDCB (%02i-%i): Going : %i:",
4614 dcb->target_id, dcb->target_lun,
4615 list_size(&dcb->srb_going_list));
4616 list_for_each_entry(srb, &dcb->srb_going_list, list)
4617 SPRINTF(" %li", srb->cmd->serial_number);
4618 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4619 SPRINTF("\n");
4622 if (debug_enabled(DBG_1)) {
4623 SPRINTF("DCB list for ACB %p:\n", acb);
4624 list_for_each_entry(dcb, &acb->dcb_list, list) {
4625 SPRINTF("%p -> ", dcb);
4627 SPRINTF("END\n");
4630 *start = buffer + offset;
4631 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4633 if (pos - buffer < offset)
4634 return 0;
4635 else if (pos - buffer - offset < length)
4636 return pos - buffer - offset;
4637 else
4638 return length;
4642 static struct scsi_host_template dc395x_driver_template = {
4643 .module = THIS_MODULE,
4644 .proc_name = DC395X_NAME,
4645 .proc_info = dc395x_proc_info,
4646 .name = DC395X_BANNER " " DC395X_VERSION,
4647 .queuecommand = dc395x_queue_command,
4648 .bios_param = dc395x_bios_param,
4649 .slave_alloc = dc395x_slave_alloc,
4650 .slave_destroy = dc395x_slave_destroy,
4651 .can_queue = DC395x_MAX_CAN_QUEUE,
4652 .this_id = 7,
4653 .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
4654 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
4655 .eh_abort_handler = dc395x_eh_abort,
4656 .eh_bus_reset_handler = dc395x_eh_bus_reset,
4657 .use_clustering = DISABLE_CLUSTERING,
4662 * banner_display - Display banner on first instance of driver
4663 * initialized.
4665 static void banner_display(void)
4667 static int banner_done = 0;
4668 if (!banner_done)
4670 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
4671 banner_done = 1;
4677 * dc395x_init_one - Initialise a single instance of the adapter.
4679 * The PCI layer will call this once for each instance of the adapter
4680 * that it finds in the system. The pci_dev strcuture indicates which
4681 * instance we are being called from.
4683 * @dev: The PCI device to intialize.
4684 * @id: Looks like a pointer to the entry in our pci device table
4685 * that was actually matched by the PCI subsystem.
4687 * Returns 0 on success, or an error code (-ve) on failure.
4689 static int __devinit dc395x_init_one(struct pci_dev *dev,
4690 const struct pci_device_id *id)
4692 struct Scsi_Host *scsi_host = NULL;
4693 struct AdapterCtlBlk *acb = NULL;
4694 unsigned long io_port_base;
4695 unsigned int io_port_len;
4696 unsigned int irq;
4698 dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
4699 banner_display();
4701 if (pci_enable_device(dev))
4703 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
4704 return -ENODEV;
4706 io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
4707 io_port_len = pci_resource_len(dev, 0);
4708 irq = dev->irq;
4709 dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
4711 /* allocate scsi host information (includes out adapter) */
4712 scsi_host = scsi_host_alloc(&dc395x_driver_template,
4713 sizeof(struct AdapterCtlBlk));
4714 if (!scsi_host) {
4715 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
4716 goto fail;
4718 acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
4719 acb->scsi_host = scsi_host;
4720 acb->dev = dev;
4722 /* initialise the adapter and everything we need */
4723 if (adapter_init(acb, io_port_base, io_port_len, irq)) {
4724 dprintkl(KERN_INFO, "adapter init failed\n");
4725 goto fail;
4728 pci_set_master(dev);
4730 /* get the scsi mid level to scan for new devices on the bus */
4731 if (scsi_add_host(scsi_host, &dev->dev)) {
4732 dprintkl(KERN_ERR, "scsi_add_host failed\n");
4733 goto fail;
4735 pci_set_drvdata(dev, scsi_host);
4736 scsi_scan_host(scsi_host);
4738 return 0;
4740 fail:
4741 if (acb != NULL)
4742 adapter_uninit(acb);
4743 if (scsi_host != NULL)
4744 scsi_host_put(scsi_host);
4745 pci_disable_device(dev);
4746 return -ENODEV;
4751 * dc395x_remove_one - Called to remove a single instance of the
4752 * adapter.
4754 * @dev: The PCI device to intialize.
4756 static void __devexit dc395x_remove_one(struct pci_dev *dev)
4758 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
4759 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
4761 dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
4763 scsi_remove_host(scsi_host);
4764 adapter_uninit(acb);
4765 pci_disable_device(dev);
4766 scsi_host_put(scsi_host);
4767 pci_set_drvdata(dev, NULL);
4771 static struct pci_device_id dc395x_pci_table[] = {
4773 .vendor = PCI_VENDOR_ID_TEKRAM,
4774 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
4775 .subvendor = PCI_ANY_ID,
4776 .subdevice = PCI_ANY_ID,
4778 {} /* Terminating entry */
4780 MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
4783 static struct pci_driver dc395x_driver = {
4784 .name = DC395X_NAME,
4785 .id_table = dc395x_pci_table,
4786 .probe = dc395x_init_one,
4787 .remove = __devexit_p(dc395x_remove_one),
4792 * dc395x_module_init - Module initialization function
4794 * Used by both module and built-in driver to initialise this driver.
4796 static int __init dc395x_module_init(void)
4798 return pci_register_driver(&dc395x_driver);
4803 * dc395x_module_exit - Module cleanup function.
4805 static void __exit dc395x_module_exit(void)
4807 pci_unregister_driver(&dc395x_driver);
4811 module_init(dc395x_module_init);
4812 module_exit(dc395x_module_exit);
4814 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4815 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4816 MODULE_LICENSE("GPL");