firewire: Future proof the iso ioctls by adding a handle for the iso context.
[linux-2.6] / drivers / scsi / dc395x.c
1 /*
2  * dc395x.c
3  *
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)
7  *
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.
12  *
13  *  Kurt Garloff <garloff@suse.de>
14  *  (C) 1999-2000 Kurt Garloff
15  *
16  *  Oliver Neukum <oliver@neukum.name>
17  *  Ali Akcaagac <aliakc@web.de>
18  *  Jamie Lenehan <lenehan@twibble.org>
19  *  (C) 2003
20  *
21  * License: GNU GPL
22  *
23  *************************************************************************
24  *
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.
35  *
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.
46  *
47  ************************************************************************
48  */
49 #include <linux/module.h>
50 #include <linux/moduleparam.h>
51 #include <linux/delay.h>
52 #include <linux/ctype.h>
53 #include <linux/blkdev.h>
54 #include <linux/interrupt.h>
55 #include <linux/init.h>
56 #include <linux/spinlock.h>
57 #include <linux/pci.h>
58 #include <linux/list.h>
59 #include <linux/vmalloc.h>
60 #include <asm/io.h>
61
62 #include <scsi/scsi.h>
63 #include <scsi/scsicam.h>       /* needed for scsicam_bios_param */
64 #include <scsi/scsi_cmnd.h>
65 #include <scsi/scsi_device.h>
66 #include <scsi/scsi_host.h>
67
68 #include "dc395x.h"
69
70 #define DC395X_NAME     "dc395x"
71 #define DC395X_BANNER   "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
72 #define DC395X_VERSION  "v2.05, 2004/03/08"
73
74 /*---------------------------------------------------------------------------
75                                   Features
76  ---------------------------------------------------------------------------*/
77 /*
78  * Set to disable parts of the driver
79  */
80 /*#define DC395x_NO_DISCONNECT*/
81 /*#define DC395x_NO_TAGQ*/
82 /*#define DC395x_NO_SYNC*/
83 /*#define DC395x_NO_WIDE*/
84
85 /*---------------------------------------------------------------------------
86                                   Debugging
87  ---------------------------------------------------------------------------*/
88 /*
89  * Types of debugging that can be enabled and disabled
90  */
91 #define DBG_KG          0x0001
92 #define DBG_0           0x0002
93 #define DBG_1           0x0004
94 #define DBG_SG          0x0020
95 #define DBG_FIFO        0x0040
96 #define DBG_PIO         0x0080
97
98
99 /*
100  * Set set of things to output debugging for.
101  * Undefine to remove all debugging
102  */
103 /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
104 /*#define  DEBUG_MASK   DBG_0*/
105
106
107 /*
108  * Output a kernel mesage at the specified level and append the
109  * driver name and a ": " to the start of the message
110  */
111 #define dprintkl(level, format, arg...)  \
112     printk(level DC395X_NAME ": " format , ## arg)
113
114
115 #ifdef DEBUG_MASK
116 /*
117  * print a debug message - this is formated with KERN_DEBUG, then the
118  * driver name followed by a ": " and then the message is output. 
119  * This also checks that the specified debug level is enabled before
120  * outputing the message
121  */
122 #define dprintkdbg(type, format, arg...) \
123         do { \
124                 if ((type) & (DEBUG_MASK)) \
125                         dprintkl(KERN_DEBUG , format , ## arg); \
126         } while (0)
127
128 /*
129  * Check if the specified type of debugging is enabled
130  */
131 #define debug_enabled(type)     ((DEBUG_MASK) & (type))
132
133 #else
134 /*
135  * No debugging. Do nothing
136  */
137 #define dprintkdbg(type, format, arg...) \
138         do {} while (0)
139 #define debug_enabled(type)     (0)
140
141 #endif
142
143
144 #ifndef PCI_VENDOR_ID_TEKRAM
145 #define PCI_VENDOR_ID_TEKRAM                    0x1DE1  /* Vendor ID    */
146 #endif
147 #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
148 #define PCI_DEVICE_ID_TEKRAM_TRMS1040           0x0391  /* Device ID    */
149 #endif
150
151
152 #define DC395x_LOCK_IO(dev,flags)               spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
153 #define DC395x_UNLOCK_IO(dev,flags)             spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
154
155 #define DC395x_read8(acb,address)               (u8)(inb(acb->io_port_base + (address)))
156 #define DC395x_read16(acb,address)              (u16)(inw(acb->io_port_base + (address)))
157 #define DC395x_read32(acb,address)              (u32)(inl(acb->io_port_base + (address)))
158 #define DC395x_write8(acb,address,value)        outb((value), acb->io_port_base + (address))
159 #define DC395x_write16(acb,address,value)       outw((value), acb->io_port_base + (address))
160 #define DC395x_write32(acb,address,value)       outl((value), acb->io_port_base + (address))
161
162 /* cmd->result */
163 #define RES_TARGET              0x000000FF      /* Target State */
164 #define RES_TARGET_LNX  STATUS_MASK     /* Only official ... */
165 #define RES_ENDMSG              0x0000FF00      /* End Message */
166 #define RES_DID                 0x00FF0000      /* DID_ codes */
167 #define RES_DRV                 0xFF000000      /* DRIVER_ codes */
168
169 #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
170 #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
171
172 #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
173 #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
174 #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
175 #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
176 #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
177
178 #define TAG_NONE 255
179
180 /*
181  * srb->segement_x is the hw sg list. It is always allocated as a
182  * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
183  * cross a page boundy.
184  */
185 #define SEGMENTX_LEN    (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
186
187
188 struct SGentry {
189         u32 address;            /* bus! address */
190         u32 length;
191 };
192
193 /* The SEEPROM structure for TRM_S1040 */
194 struct NVRamTarget {
195         u8 cfg0;                /* Target configuration byte 0  */
196         u8 period;              /* Target period                */
197         u8 cfg2;                /* Target configuration byte 2  */
198         u8 cfg3;                /* Target configuration byte 3  */
199 };
200
201 struct NvRamType {
202         u8 sub_vendor_id[2];    /* 0,1  Sub Vendor ID   */
203         u8 sub_sys_id[2];       /* 2,3  Sub System ID   */
204         u8 sub_class;           /* 4    Sub Class       */
205         u8 vendor_id[2];        /* 5,6  Vendor ID       */
206         u8 device_id[2];        /* 7,8  Device ID       */
207         u8 reserved;            /* 9    Reserved        */
208         struct NVRamTarget target[DC395x_MAX_SCSI_ID];
209                                                 /** 10,11,12,13
210                                                  ** 14,15,16,17
211                                                  ** ....
212                                                  ** ....
213                                                  ** 70,71,72,73
214                                                  */
215         u8 scsi_id;             /* 74 Host Adapter SCSI ID      */
216         u8 channel_cfg;         /* 75 Channel configuration     */
217         u8 delay_time;          /* 76 Power on delay time       */
218         u8 max_tag;             /* 77 Maximum tags              */
219         u8 reserved0;           /* 78  */
220         u8 boot_target;         /* 79  */
221         u8 boot_lun;            /* 80  */
222         u8 reserved1;           /* 81  */
223         u16 reserved2[22];      /* 82,..125 */
224         u16 cksum;              /* 126,127 */
225 };
226
227 struct ScsiReqBlk {
228         struct list_head list;          /* next/prev ptrs for srb lists */
229         struct DeviceCtlBlk *dcb;
230         struct scsi_cmnd *cmd;
231
232         struct SGentry *segment_x;      /* Linear array of hw sg entries (up to 64 entries) */
233         dma_addr_t sg_bus_addr;         /* Bus address of sg list (ie, of segment_x) */
234
235         u8 sg_count;                    /* No of HW sg entries for this request */
236         u8 sg_index;                    /* Index of HW sg entry for this request */
237         size_t total_xfer_length;       /* Total number of bytes remaining to be transfered */
238         size_t request_length;          /* Total number of bytes in this request */
239         /*
240          * The sense buffer handling function, request_sense, uses
241          * the first hw sg entry (segment_x[0]) and the transfer
242          * length (total_xfer_length). While doing this it stores the
243          * original values into the last sg hw list
244          * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
245          * total_xfer_length in xferred. These values are restored in
246          * pci_unmap_srb_sense. This is the only place xferred is used.
247          */
248         size_t xferred;                 /* Saved copy of total_xfer_length */
249
250         u16 state;
251
252         u8 msgin_buf[6];
253         u8 msgout_buf[6];
254
255         u8 adapter_status;
256         u8 target_status;
257         u8 msg_count;
258         u8 end_message;
259
260         u8 tag_number;
261         u8 status;
262         u8 retry_count;
263         u8 flag;
264
265         u8 scsi_phase;
266 };
267
268 struct DeviceCtlBlk {
269         struct list_head list;          /* next/prev ptrs for the dcb list */
270         struct AdapterCtlBlk *acb;
271         struct list_head srb_going_list;        /* head of going srb list */
272         struct list_head srb_waiting_list;      /* head of waiting srb list */
273
274         struct ScsiReqBlk *active_srb;
275         u32 tag_mask;
276
277         u16 max_command;
278
279         u8 target_id;           /* SCSI Target ID  (SCSI Only) */
280         u8 target_lun;          /* SCSI Log.  Unit (SCSI Only) */
281         u8 identify_msg;
282         u8 dev_mode;
283
284         u8 inquiry7;            /* To store Inquiry flags */
285         u8 sync_mode;           /* 0:async mode */
286         u8 min_nego_period;     /* for nego. */
287         u8 sync_period;         /* for reg.  */
288
289         u8 sync_offset;         /* for reg. and nego.(low nibble) */
290         u8 flag;
291         u8 dev_type;
292         u8 init_tcq_flag;
293 };
294
295 struct AdapterCtlBlk {
296         struct Scsi_Host *scsi_host;
297
298         unsigned long io_port_base;
299         unsigned long io_port_len;
300
301         struct list_head dcb_list;              /* head of going dcb list */
302         struct DeviceCtlBlk *dcb_run_robin;
303         struct DeviceCtlBlk *active_dcb;
304
305         struct list_head srb_free_list;         /* head of free srb list */
306         struct ScsiReqBlk *tmp_srb;
307         struct timer_list waiting_timer;
308         struct timer_list selto_timer;
309
310         u16 srb_count;
311
312         u8 sel_timeout;
313
314         unsigned int irq_level;
315         u8 tag_max_num;
316         u8 acb_flag;
317         u8 gmode2;
318
319         u8 config;
320         u8 lun_chk;
321         u8 scan_devices;
322         u8 hostid_bit;
323
324         u8 dcb_map[DC395x_MAX_SCSI_ID];
325         struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
326
327         struct pci_dev *dev;
328
329         u8 msg_len;
330
331         struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
332         struct ScsiReqBlk srb;
333
334         struct NvRamType eeprom;        /* eeprom settings for this adapter */
335 };
336
337
338 /*---------------------------------------------------------------------------
339                             Forward declarations
340  ---------------------------------------------------------------------------*/
341 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
342                 u16 *pscsi_status);
343 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
344                 u16 *pscsi_status);
345 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
346                 u16 *pscsi_status);
347 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
348                 u16 *pscsi_status);
349 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
350                 u16 *pscsi_status);
351 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
352                 u16 *pscsi_status);
353 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
354                 u16 *pscsi_status);
355 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
356                 u16 *pscsi_status);
357 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
358                 u16 *pscsi_status);
359 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
360                 u16 *pscsi_status);
361 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
362                 u16 *pscsi_status);
363 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
364                 u16 *pscsi_status);
365 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
366                 u16 *pscsi_status);
367 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 
368                 u16 *pscsi_status);
369 static void set_basic_config(struct AdapterCtlBlk *acb);
370 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
371                 struct ScsiReqBlk *srb);
372 static void reset_scsi_bus(struct AdapterCtlBlk *acb);
373 static void data_io_transfer(struct AdapterCtlBlk *acb,
374                 struct ScsiReqBlk *srb, u16 io_dir);
375 static void disconnect(struct AdapterCtlBlk *acb);
376 static void reselect(struct AdapterCtlBlk *acb);
377 static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
378                 struct ScsiReqBlk *srb);
379 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
380                 struct ScsiReqBlk *srb);
381 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
382                 struct ScsiReqBlk *srb);
383 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
384                 struct scsi_cmnd *cmd, u8 force);
385 static void scsi_reset_detect(struct AdapterCtlBlk *acb);
386 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
387 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
388                 struct ScsiReqBlk *srb);
389 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
390                 struct ScsiReqBlk *srb);
391 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
392                 struct ScsiReqBlk *srb);
393 static void set_xfer_rate(struct AdapterCtlBlk *acb,
394                 struct DeviceCtlBlk *dcb);
395 static void waiting_timeout(unsigned long ptr);
396
397
398 /*---------------------------------------------------------------------------
399                                  Static Data
400  ---------------------------------------------------------------------------*/
401 static u16 current_sync_offset = 0;
402
403 static void *dc395x_scsi_phase0[] = {
404         data_out_phase0,/* phase:0 */
405         data_in_phase0, /* phase:1 */
406         command_phase0, /* phase:2 */
407         status_phase0,  /* phase:3 */
408         nop0,           /* phase:4 PH_BUS_FREE .. initial phase */
409         nop0,           /* phase:5 PH_BUS_FREE .. initial phase */
410         msgout_phase0,  /* phase:6 */
411         msgin_phase0,   /* phase:7 */
412 };
413
414 static void *dc395x_scsi_phase1[] = {
415         data_out_phase1,/* phase:0 */
416         data_in_phase1, /* phase:1 */
417         command_phase1, /* phase:2 */
418         status_phase1,  /* phase:3 */
419         nop1,           /* phase:4 PH_BUS_FREE .. initial phase */
420         nop1,           /* phase:5 PH_BUS_FREE .. initial phase */
421         msgout_phase1,  /* phase:6 */
422         msgin_phase1,   /* phase:7 */
423 };
424
425 /*
426  *Fast20:       000      50ns, 20.0 MHz
427  *              001      75ns, 13.3 MHz
428  *              010     100ns, 10.0 MHz
429  *              011     125ns,  8.0 MHz
430  *              100     150ns,  6.6 MHz
431  *              101     175ns,  5.7 MHz
432  *              110     200ns,  5.0 MHz
433  *              111     250ns,  4.0 MHz
434  *
435  *Fast40(LVDS): 000      25ns, 40.0 MHz
436  *              001      50ns, 20.0 MHz
437  *              010      75ns, 13.3 MHz
438  *              011     100ns, 10.0 MHz
439  *              100     125ns,  8.0 MHz
440  *              101     150ns,  6.6 MHz
441  *              110     175ns,  5.7 MHz
442  *              111     200ns,  5.0 MHz
443  */
444 /*static u8     clock_period[] = {12,19,25,31,37,44,50,62};*/
445
446 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
447 static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
448 static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
449
450
451 /*---------------------------------------------------------------------------
452                                 Configuration
453   ---------------------------------------------------------------------------*/
454 /*
455  * Module/boot parameters currently effect *all* instances of the
456  * card in the system.
457  */
458
459 /*
460  * Command line parameters are stored in a structure below.
461  * These are the index's into the structure for the various
462  * command line options.
463  */
464 #define CFG_ADAPTER_ID          0
465 #define CFG_MAX_SPEED           1
466 #define CFG_DEV_MODE            2
467 #define CFG_ADAPTER_MODE        3
468 #define CFG_TAGS                4
469 #define CFG_RESET_DELAY         5
470
471 #define CFG_NUM                 6       /* number of configuration items */
472
473
474 /*
475  * Value used to indicate that a command line override
476  * hasn't been used to modify the value.
477  */
478 #define CFG_PARAM_UNSET -1
479
480
481 /*
482  * Hold command line parameters.
483  */
484 struct ParameterData {
485         int value;              /* value of this setting */
486         int min;                /* minimum value */
487         int max;                /* maximum value */
488         int def;                /* default value */
489         int safe;               /* safe value */
490 };
491 static struct ParameterData __devinitdata cfg_data[] = {
492         { /* adapter id */
493                 CFG_PARAM_UNSET,
494                 0,
495                 15,
496                 7,
497                 7
498         },
499         { /* max speed */
500                 CFG_PARAM_UNSET,
501                   0,
502                   7,
503                   1,    /* 13.3Mhz */
504                   4,    /*  6.7Hmz */
505         },
506         { /* dev mode */
507                 CFG_PARAM_UNSET,
508                 0,
509                 0x3f,
510                 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
511                         NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
512                         NTC_DO_SEND_START,
513                 NTC_DO_PARITY_CHK | NTC_DO_SEND_START
514         },
515         { /* adapter mode */
516                 CFG_PARAM_UNSET,
517                 0,
518                 0x2f,
519 #ifdef CONFIG_SCSI_MULTI_LUN
520                         NAC_SCANLUN |
521 #endif
522                 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
523                         /*| NAC_ACTIVE_NEG*/,
524                 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
525         },
526         { /* tags */
527                 CFG_PARAM_UNSET,
528                 0,
529                 5,
530                 3,      /* 16 tags (??) */
531                 2,
532         },
533         { /* reset delay */
534                 CFG_PARAM_UNSET,
535                 0,
536                 180,
537                 1,      /* 1 second */
538                 10,     /* 10 seconds */
539         }
540 };
541
542
543 /*
544  * Safe settings. If set to zero the the BIOS/default values with
545  * command line overrides will be used. If set to 1 then safe and
546  * slow settings will be used.
547  */
548 static int use_safe_settings = 0;
549 module_param_named(safe, use_safe_settings, bool, 0);
550 MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
551
552
553 module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
554 MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
555
556 module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
557 MODULE_PARM_DESC(max_speed, "Maximum bus speed. Default 1 (0-7) Speeds: 0=20, 1=13.3, 2=10, 3=8, 4=6.7, 5=5.8, 6=5, 7=4 Mhz");
558
559 module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
560 MODULE_PARM_DESC(dev_mode, "Device mode.");
561
562 module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
563 MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
564
565 module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
566 MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
567
568 module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
569 MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
570
571
572 /**
573  * set_safe_settings - if the use_safe_settings option is set then
574  * set all values to the safe and slow values.
575  **/
576 static void __devinit set_safe_settings(void)
577 {
578         if (use_safe_settings)
579         {
580                 int i;
581
582                 dprintkl(KERN_INFO, "Using safe settings.\n");
583                 for (i = 0; i < CFG_NUM; i++)
584                 {
585                         cfg_data[i].value = cfg_data[i].safe;
586                 }
587         }
588 }
589
590
591 /**
592  * fix_settings - reset any boot parameters which are out of range
593  * back to the default values.
594  **/
595 static void __devinit fix_settings(void)
596 {
597         int i;
598
599         dprintkdbg(DBG_1,
600                 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
601                 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
602                 cfg_data[CFG_ADAPTER_ID].value,
603                 cfg_data[CFG_MAX_SPEED].value,
604                 cfg_data[CFG_DEV_MODE].value,
605                 cfg_data[CFG_ADAPTER_MODE].value,
606                 cfg_data[CFG_TAGS].value,
607                 cfg_data[CFG_RESET_DELAY].value);
608         for (i = 0; i < CFG_NUM; i++)
609         {
610                 if (cfg_data[i].value < cfg_data[i].min
611                     || cfg_data[i].value > cfg_data[i].max)
612                         cfg_data[i].value = cfg_data[i].def;
613         }
614 }
615
616
617
618 /*
619  * Mapping from the eeprom delay index value (index into this array)
620  * to the the number of actual seconds that the delay should be for.
621  */
622 static char __devinitdata eeprom_index_to_delay_map[] = 
623         { 1, 3, 5, 10, 16, 30, 60, 120 };
624
625
626 /**
627  * eeprom_index_to_delay - Take the eeprom delay setting and convert it
628  * into a number of seconds.
629  *
630  * @eeprom: The eeprom structure in which we find the delay index to map.
631  **/
632 static void __devinit eeprom_index_to_delay(struct NvRamType *eeprom)
633 {
634         eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
635 }
636
637
638 /**
639  * delay_to_eeprom_index - Take a delay in seconds and return the
640  * closest eeprom index which will delay for at least that amount of
641  * seconds.
642  *
643  * @delay: The delay, in seconds, to find the eeprom index for.
644  **/
645 static int __devinit delay_to_eeprom_index(int delay)
646 {
647         u8 idx = 0;
648         while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
649                 idx++;
650         return idx;
651 }
652
653
654 /**
655  * eeprom_override - Override the eeprom settings, in the provided
656  * eeprom structure, with values that have been set on the command
657  * line.
658  *
659  * @eeprom: The eeprom data to override with command line options.
660  **/
661 static void __devinit eeprom_override(struct NvRamType *eeprom)
662 {
663         u8 id;
664
665         /* Adapter Settings */
666         if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
667                 eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
668
669         if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
670                 eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
671
672         if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
673                 eeprom->delay_time = delay_to_eeprom_index(
674                                         cfg_data[CFG_RESET_DELAY].value);
675
676         if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
677                 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
678
679         /* Device Settings */
680         for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
681                 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
682                         eeprom->target[id].cfg0 =
683                                 (u8)cfg_data[CFG_DEV_MODE].value;
684
685                 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
686                         eeprom->target[id].period =
687                                 (u8)cfg_data[CFG_MAX_SPEED].value;
688
689         }
690 }
691
692
693 /*---------------------------------------------------------------------------
694  ---------------------------------------------------------------------------*/
695
696 static unsigned int list_size(struct list_head *head)
697 {
698         unsigned int count = 0;
699         struct list_head *pos;
700         list_for_each(pos, head)
701                 count++;
702         return count;
703 }
704
705
706 static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
707                 struct DeviceCtlBlk *pos)
708 {
709         int use_next = 0;
710         struct DeviceCtlBlk* next = NULL;
711         struct DeviceCtlBlk* i;
712
713         if (list_empty(head))
714                 return NULL;
715
716         /* find supplied dcb and then select the next one */
717         list_for_each_entry(i, head, list)
718                 if (use_next) {
719                         next = i;
720                         break;
721                 } else if (i == pos) {
722                         use_next = 1;
723                 }
724         /* if no next one take the head one (ie, wraparound) */
725         if (!next)
726                 list_for_each_entry(i, head, list) {
727                         next = i;
728                         break;
729                 }
730
731         return next;
732 }
733
734
735 static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
736 {
737         if (srb->tag_number < 255) {
738                 dcb->tag_mask &= ~(1 << srb->tag_number);       /* free tag mask */
739                 srb->tag_number = 255;
740         }
741 }
742
743
744 /* Find cmd in SRB list */
745 static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
746                 struct list_head *head)
747 {
748         struct ScsiReqBlk *i;
749         list_for_each_entry(i, head, list)
750                 if (i->cmd == cmd)
751                         return i;
752         return NULL;
753 }
754
755
756 static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb)
757 {
758         struct list_head *head = &acb->srb_free_list;
759         struct ScsiReqBlk *srb = NULL;
760
761         if (!list_empty(head)) {
762                 srb = list_entry(head->next, struct ScsiReqBlk, list);
763                 list_del(head->next);
764                 dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb);
765         }
766         return srb;
767 }
768
769
770 static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
771 {
772         dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb);
773         list_add_tail(&srb->list, &acb->srb_free_list);
774 }
775
776
777 static void srb_waiting_insert(struct DeviceCtlBlk *dcb,
778                 struct ScsiReqBlk *srb)
779 {
780         dprintkdbg(DBG_0, "srb_waiting_insert: (pid#%li) <%02i-%i> srb=%p\n",
781                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
782         list_add(&srb->list, &dcb->srb_waiting_list);
783 }
784
785
786 static void srb_waiting_append(struct DeviceCtlBlk *dcb,
787                 struct ScsiReqBlk *srb)
788 {
789         dprintkdbg(DBG_0, "srb_waiting_append: (pid#%li) <%02i-%i> srb=%p\n",
790                  srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
791         list_add_tail(&srb->list, &dcb->srb_waiting_list);
792 }
793
794
795 static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
796 {
797         dprintkdbg(DBG_0, "srb_going_append: (pid#%li) <%02i-%i> srb=%p\n",
798                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
799         list_add_tail(&srb->list, &dcb->srb_going_list);
800 }
801
802
803 static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
804 {
805         struct ScsiReqBlk *i;
806         struct ScsiReqBlk *tmp;
807         dprintkdbg(DBG_0, "srb_going_remove: (pid#%li) <%02i-%i> srb=%p\n",
808                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
809
810         list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list)
811                 if (i == srb) {
812                         list_del(&srb->list);
813                         break;
814                 }
815 }
816
817
818 static void srb_waiting_remove(struct DeviceCtlBlk *dcb,
819                 struct ScsiReqBlk *srb)
820 {
821         struct ScsiReqBlk *i;
822         struct ScsiReqBlk *tmp;
823         dprintkdbg(DBG_0, "srb_waiting_remove: (pid#%li) <%02i-%i> srb=%p\n",
824                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
825
826         list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list)
827                 if (i == srb) {
828                         list_del(&srb->list);
829                         break;
830                 }
831 }
832
833
834 static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb,
835                 struct ScsiReqBlk *srb)
836 {
837         dprintkdbg(DBG_0,
838                 "srb_going_to_waiting_move: (pid#%li) <%02i-%i> srb=%p\n",
839                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
840         list_move(&srb->list, &dcb->srb_waiting_list);
841 }
842
843
844 static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb,
845                 struct ScsiReqBlk *srb)
846 {
847         dprintkdbg(DBG_0,
848                 "srb_waiting_to_going_move: (pid#%li) <%02i-%i> srb=%p\n",
849                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
850         list_move(&srb->list, &dcb->srb_going_list);
851 }
852
853
854 /* Sets the timer to wake us up */
855 static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
856 {
857         if (timer_pending(&acb->waiting_timer))
858                 return;
859         init_timer(&acb->waiting_timer);
860         acb->waiting_timer.function = waiting_timeout;
861         acb->waiting_timer.data = (unsigned long) acb;
862         if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
863                 acb->waiting_timer.expires =
864                     acb->scsi_host->last_reset - HZ / 2 + 1;
865         else
866                 acb->waiting_timer.expires = jiffies + to + 1;
867         add_timer(&acb->waiting_timer);
868 }
869
870
871 /* Send the next command from the waiting list to the bus */
872 static void waiting_process_next(struct AdapterCtlBlk *acb)
873 {
874         struct DeviceCtlBlk *start = NULL;
875         struct DeviceCtlBlk *pos;
876         struct DeviceCtlBlk *dcb;
877         struct ScsiReqBlk *srb;
878         struct list_head *dcb_list_head = &acb->dcb_list;
879
880         if (acb->active_dcb
881             || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
882                 return;
883
884         if (timer_pending(&acb->waiting_timer))
885                 del_timer(&acb->waiting_timer);
886
887         if (list_empty(dcb_list_head))
888                 return;
889
890         /*
891          * Find the starting dcb. Need to find it again in the list
892          * since the list may have changed since we set the ptr to it
893          */
894         list_for_each_entry(dcb, dcb_list_head, list)
895                 if (dcb == acb->dcb_run_robin) {
896                         start = dcb;
897                         break;
898                 }
899         if (!start) {
900                 /* This can happen! */
901                 start = list_entry(dcb_list_head->next, typeof(*start), list);
902                 acb->dcb_run_robin = start;
903         }
904
905
906         /*
907          * Loop over the dcb, but we start somewhere (potentially) in
908          * the middle of the loop so we need to manully do this.
909          */
910         pos = start;
911         do {
912                 struct list_head *waiting_list_head = &pos->srb_waiting_list;
913
914                 /* Make sure, the next another device gets scheduled ... */
915                 acb->dcb_run_robin = dcb_get_next(dcb_list_head,
916                                                   acb->dcb_run_robin);
917
918                 if (list_empty(waiting_list_head) ||
919                     pos->max_command <= list_size(&pos->srb_going_list)) {
920                         /* move to next dcb */
921                         pos = dcb_get_next(dcb_list_head, pos);
922                 } else {
923                         srb = list_entry(waiting_list_head->next,
924                                          struct ScsiReqBlk, list);
925
926                         /* Try to send to the bus */
927                         if (!start_scsi(acb, pos, srb))
928                                 srb_waiting_to_going_move(pos, srb);
929                         else
930                                 waiting_set_timer(acb, HZ/50);
931                         break;
932                 }
933         } while (pos != start);
934 }
935
936
937 /* Wake up waiting queue */
938 static void waiting_timeout(unsigned long ptr)
939 {
940         unsigned long flags;
941         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
942         dprintkdbg(DBG_1,
943                 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
944         DC395x_LOCK_IO(acb->scsi_host, flags);
945         waiting_process_next(acb);
946         DC395x_UNLOCK_IO(acb->scsi_host, flags);
947 }
948
949
950 /* Get the DCB for a given ID/LUN combination */
951 static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
952 {
953         return acb->children[id][lun];
954 }
955
956
957 /* Send SCSI Request Block (srb) to adapter (acb) */
958 static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
959 {
960         struct DeviceCtlBlk *dcb = srb->dcb;
961
962         if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
963             acb->active_dcb ||
964             (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
965                 srb_waiting_append(dcb, srb);
966                 waiting_process_next(acb);
967                 return;
968         }
969
970         if (!start_scsi(acb, dcb, srb))
971                 srb_going_append(dcb, srb);
972         else {
973                 srb_waiting_insert(dcb, srb);
974                 waiting_set_timer(acb, HZ / 50);
975         }
976 }
977
978 /* Prepare SRB for being sent to Device DCB w/ command *cmd */
979 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
980                 struct ScsiReqBlk *srb)
981 {
982         enum dma_data_direction dir = cmd->sc_data_direction;
983         dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n",
984                 cmd->pid, dcb->target_id, dcb->target_lun);
985
986         srb->dcb = dcb;
987         srb->cmd = cmd;
988         srb->sg_count = 0;
989         srb->total_xfer_length = 0;
990         srb->sg_bus_addr = 0;
991         srb->sg_index = 0;
992         srb->adapter_status = 0;
993         srb->target_status = 0;
994         srb->msg_count = 0;
995         srb->status = 0;
996         srb->flag = 0;
997         srb->state = 0;
998         srb->retry_count = 0;
999         srb->tag_number = TAG_NONE;
1000         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
1001         srb->end_message = 0;
1002
1003         if (dir == PCI_DMA_NONE || !cmd->request_buffer) {
1004                 dprintkdbg(DBG_0,
1005                         "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
1006                         cmd->bufflen, cmd->request_buffer,
1007                         cmd->use_sg, srb->segment_x[0].address);
1008         } else if (cmd->use_sg) {
1009                 int i;
1010                 u32 reqlen = cmd->request_bufflen;
1011                 struct scatterlist *sl = (struct scatterlist *)
1012                                          cmd->request_buffer;
1013                 struct SGentry *sgp = srb->segment_x;
1014                 srb->sg_count = pci_map_sg(dcb->acb->dev, sl, cmd->use_sg,
1015                                            dir);
1016                 dprintkdbg(DBG_0,
1017                         "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
1018                         reqlen, cmd->request_buffer, cmd->use_sg,
1019                         srb->sg_count);
1020
1021                 for (i = 0; i < srb->sg_count; i++) {
1022                         u32 busaddr = (u32)sg_dma_address(&sl[i]);
1023                         u32 seglen = (u32)sl[i].length;
1024                         sgp[i].address = busaddr;
1025                         sgp[i].length = seglen;
1026                         srb->total_xfer_length += seglen;
1027                 }
1028                 sgp += srb->sg_count - 1;
1029
1030                 /*
1031                  * adjust last page if too big as it is allocated
1032                  * on even page boundaries
1033                  */
1034                 if (srb->total_xfer_length > reqlen) {
1035                         sgp->length -= (srb->total_xfer_length - reqlen);
1036                         srb->total_xfer_length = reqlen;
1037                 }
1038
1039                 /* Fixup for WIDE padding - make sure length is even */
1040                 if (dcb->sync_period & WIDE_SYNC &&
1041                     srb->total_xfer_length % 2) {
1042                         srb->total_xfer_length++;
1043                         sgp->length++;
1044                 }
1045
1046                 srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
1047                                                 srb->segment_x,
1048                                                 SEGMENTX_LEN,
1049                                                 PCI_DMA_TODEVICE);
1050
1051                 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
1052                         srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
1053         } else {
1054                 srb->total_xfer_length = cmd->request_bufflen;
1055                 srb->sg_count = 1;
1056                 srb->segment_x[0].address =
1057                         pci_map_single(dcb->acb->dev, cmd->request_buffer,
1058                                        srb->total_xfer_length, dir);
1059
1060                 /* Fixup for WIDE padding - make sure length is even */
1061                 if (dcb->sync_period & WIDE_SYNC && srb->total_xfer_length % 2)
1062                         srb->total_xfer_length++;
1063
1064                 srb->segment_x[0].length = srb->total_xfer_length;
1065
1066                 dprintkdbg(DBG_0,
1067                         "build_srb: [1] len=%d buf=%p use_sg=%d map=%08x\n",
1068                         srb->total_xfer_length, cmd->request_buffer,
1069                         cmd->use_sg, srb->segment_x[0].address);
1070         }
1071
1072         srb->request_length = srb->total_xfer_length;
1073 }
1074
1075
1076 /**
1077  * dc395x_queue_command - queue scsi command passed from the mid
1078  * layer, invoke 'done' on completion
1079  *
1080  * @cmd: pointer to scsi command object
1081  * @done: function pointer to be invoked on completion
1082  *
1083  * Returns 1 if the adapter (host) is busy, else returns 0. One
1084  * reason for an adapter to be busy is that the number
1085  * of outstanding queued commands is already equal to
1086  * struct Scsi_Host::can_queue .
1087  *
1088  * Required: if struct Scsi_Host::can_queue is ever non-zero
1089  *           then this function is required.
1090  *
1091  * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1092  *        and is expected to be held on return.
1093  *
1094  **/
1095 static int dc395x_queue_command(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1096 {
1097         struct DeviceCtlBlk *dcb;
1098         struct ScsiReqBlk *srb;
1099         struct AdapterCtlBlk *acb =
1100             (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1101         dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n",
1102                 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
1103
1104         /* Assume BAD_TARGET; will be cleared later */
1105         cmd->result = DID_BAD_TARGET << 16;
1106
1107         /* ignore invalid targets */
1108         if (cmd->device->id >= acb->scsi_host->max_id ||
1109             cmd->device->lun >= acb->scsi_host->max_lun ||
1110             cmd->device->lun >31) {
1111                 goto complete;
1112         }
1113
1114         /* does the specified lun on the specified device exist */
1115         if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
1116                 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
1117                         cmd->device->id, cmd->device->lun);
1118                 goto complete;
1119         }
1120
1121         /* do we have a DCB for the device */
1122         dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1123         if (!dcb) {
1124                 /* should never happen */
1125                 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
1126                         cmd->device->id, cmd->device->lun);
1127                 goto complete;
1128         }
1129
1130         /* set callback and clear result in the command */
1131         cmd->scsi_done = done;
1132         cmd->result = 0;
1133
1134         srb = srb_get_free(acb);
1135         if (!srb)
1136         {
1137                 /*
1138                  * Return 1 since we are unable to queue this command at this
1139                  * point in time.
1140                  */
1141                 dprintkdbg(DBG_0, "queue_command: No free srb's\n");
1142                 return 1;
1143         }
1144
1145         build_srb(cmd, dcb, srb);
1146
1147         if (!list_empty(&dcb->srb_waiting_list)) {
1148                 /* append to waiting queue */
1149                 srb_waiting_append(dcb, srb);
1150                 waiting_process_next(acb);
1151         } else {
1152                 /* process immediately */
1153                 send_srb(acb, srb);
1154         }
1155         dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->pid);
1156         return 0;
1157
1158 complete:
1159         /*
1160          * Complete the command immediatey, and then return 0 to
1161          * indicate that we have handled the command. This is usually
1162          * done when the commad is for things like non existent
1163          * devices.
1164          */
1165         done(cmd);
1166         return 0;
1167 }
1168
1169
1170 /*
1171  * Return the disk geometry for the given SCSI device.
1172  */
1173 static int dc395x_bios_param(struct scsi_device *sdev,
1174                 struct block_device *bdev, sector_t capacity, int *info)
1175 {
1176 #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
1177         int heads, sectors, cylinders;
1178         struct AdapterCtlBlk *acb;
1179         int size = capacity;
1180
1181         dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
1182         acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
1183         heads = 64;
1184         sectors = 32;
1185         cylinders = size / (heads * sectors);
1186
1187         if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
1188                 heads = 255;
1189                 sectors = 63;
1190                 cylinders = size / (heads * sectors);
1191         }
1192         geom[0] = heads;
1193         geom[1] = sectors;
1194         geom[2] = cylinders;
1195         return 0;
1196 #else
1197         return scsicam_bios_param(bdev, capacity, info);
1198 #endif
1199 }
1200
1201
1202 static void dump_register_info(struct AdapterCtlBlk *acb,
1203                 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
1204 {
1205         u16 pstat;
1206         struct pci_dev *dev = acb->dev;
1207         pci_read_config_word(dev, PCI_STATUS, &pstat);
1208         if (!dcb)
1209                 dcb = acb->active_dcb;
1210         if (!srb && dcb)
1211                 srb = dcb->active_srb;
1212         if (srb) {
1213                 if (!srb->cmd)
1214                         dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
1215                                 srb, srb->cmd);
1216                 else
1217                         dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) "
1218                                  "cmnd=0x%02x <%02i-%i>\n",
1219                                 srb, srb->cmd, srb->cmd->pid,
1220                                 srb->cmd->cmnd[0], srb->cmd->device->id,
1221                                 srb->cmd->device->lun);
1222                 printk("  sglist=%p cnt=%i idx=%i len=%zu\n",
1223                        srb->segment_x, srb->sg_count, srb->sg_index,
1224                        srb->total_xfer_length);
1225                 printk("  state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1226                        srb->state, srb->status, srb->scsi_phase,
1227                        (acb->active_dcb) ? "" : "not");
1228         }
1229         dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1230                 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1231                 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1232                 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1233                 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1234                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1235                 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1236                 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
1237                 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1238                 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1239                 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1240                 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
1241                 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1242                 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1243                 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1244                 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1245                 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1246         dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1247                 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1248                 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1249                 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1250                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1251                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1252                 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1253                 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1254                 DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
1255                 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1256                 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1257                 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1258                 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1259         dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1260                 "pci{status=0x%04x}\n",
1261                 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1262                 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1263                 DC395x_read8(acb, TRM_S1040_GEN_TIMER),
1264                 pstat);
1265 }
1266
1267
1268 static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1269 {
1270 #if debug_enabled(DBG_FIFO)
1271         u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1272         u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1273         if (!(fifocnt & 0x40))
1274                 dprintkdbg(DBG_FIFO,
1275                         "clear_fifo: (%i bytes) on phase %02x in %s\n",
1276                         fifocnt & 0x3f, lines, txt);
1277 #endif
1278         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1279 }
1280
1281
1282 static void reset_dev_param(struct AdapterCtlBlk *acb)
1283 {
1284         struct DeviceCtlBlk *dcb;
1285         struct NvRamType *eeprom = &acb->eeprom;
1286         dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
1287
1288         list_for_each_entry(dcb, &acb->dcb_list, list) {
1289                 u8 period_index;
1290
1291                 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1292                 dcb->sync_period = 0;
1293                 dcb->sync_offset = 0;
1294
1295                 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1296                 period_index = eeprom->target[dcb->target_id].period & 0x07;
1297                 dcb->min_nego_period = clock_period[period_index];
1298                 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1299                     || !(acb->config & HCC_WIDE_CARD))
1300                         dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1301         }
1302 }
1303
1304
1305 /*
1306  * perform a hard reset on the SCSI bus
1307  * @cmd - some command for this host (for fetching hooks)
1308  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1309  */
1310 static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1311 {
1312         struct AdapterCtlBlk *acb =
1313                 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1314         dprintkl(KERN_INFO,
1315                 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n",
1316                 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1317
1318         if (timer_pending(&acb->waiting_timer))
1319                 del_timer(&acb->waiting_timer);
1320
1321         /*
1322          * disable interrupt    
1323          */
1324         DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1325         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1326         DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1327         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1328
1329         reset_scsi_bus(acb);
1330         udelay(500);
1331
1332         /* We may be in serious trouble. Wait some seconds */
1333         acb->scsi_host->last_reset =
1334             jiffies + 3 * HZ / 2 +
1335             HZ * acb->eeprom.delay_time;
1336
1337         /*
1338          * re-enable interrupt      
1339          */
1340         /* Clear SCSI FIFO          */
1341         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1342         clear_fifo(acb, "eh_bus_reset");
1343         /* Delete pending IRQ */
1344         DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1345         set_basic_config(acb);
1346
1347         reset_dev_param(acb);
1348         doing_srb_done(acb, DID_RESET, cmd, 0);
1349         acb->active_dcb = NULL;
1350         acb->acb_flag = 0;      /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1351         waiting_process_next(acb);
1352
1353         return SUCCESS;
1354 }
1355
1356 static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1357 {
1358         int rc;
1359
1360         spin_lock_irq(cmd->device->host->host_lock);
1361         rc = __dc395x_eh_bus_reset(cmd);
1362         spin_unlock_irq(cmd->device->host->host_lock);
1363
1364         return rc;
1365 }
1366
1367 /*
1368  * abort an errant SCSI command
1369  * @cmd - command to be aborted
1370  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1371  */
1372 static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1373 {
1374         /*
1375          * Look into our command queues: If it has not been sent already,
1376          * we remove it and return success. Otherwise fail.
1377          */
1378         struct AdapterCtlBlk *acb =
1379             (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1380         struct DeviceCtlBlk *dcb;
1381         struct ScsiReqBlk *srb;
1382         dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
1383                 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1384
1385         dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1386         if (!dcb) {
1387                 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1388                 return FAILED;
1389         }
1390
1391         srb = find_cmd(cmd, &dcb->srb_waiting_list);
1392         if (srb) {
1393                 srb_waiting_remove(dcb, srb);
1394                 pci_unmap_srb_sense(acb, srb);
1395                 pci_unmap_srb(acb, srb);
1396                 free_tag(dcb, srb);
1397                 srb_free_insert(acb, srb);
1398                 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1399                 cmd->result = DID_ABORT << 16;
1400                 return SUCCESS;
1401         }
1402         srb = find_cmd(cmd, &dcb->srb_going_list);
1403         if (srb) {
1404                 dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
1405                 /* XXX: Should abort the command here */
1406         } else {
1407                 dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
1408         }
1409         return FAILED;
1410 }
1411
1412
1413 /* SDTR */
1414 static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1415                 struct ScsiReqBlk *srb)
1416 {
1417         u8 *ptr = srb->msgout_buf + srb->msg_count;
1418         if (srb->msg_count > 1) {
1419                 dprintkl(KERN_INFO,
1420                         "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1421                         srb->msg_count, srb->msgout_buf[0],
1422                         srb->msgout_buf[1]);
1423                 return;
1424         }
1425         if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1426                 dcb->sync_offset = 0;
1427                 dcb->min_nego_period = 200 >> 2;
1428         } else if (dcb->sync_offset == 0)
1429                 dcb->sync_offset = SYNC_NEGO_OFFSET;
1430
1431         *ptr++ = MSG_EXTENDED;  /* (01h) */
1432         *ptr++ = 3;             /* length */
1433         *ptr++ = EXTENDED_SDTR; /* (01h) */
1434         *ptr++ = dcb->min_nego_period;  /* Transfer period (in 4ns) */
1435         *ptr++ = dcb->sync_offset;      /* Transfer period (max. REQ/ACK dist) */
1436         srb->msg_count += 5;
1437         srb->state |= SRB_DO_SYNC_NEGO;
1438 }
1439
1440
1441 /* WDTR */
1442 static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1443                 struct ScsiReqBlk *srb)
1444 {
1445         u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1446                    (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1447         u8 *ptr = srb->msgout_buf + srb->msg_count;
1448         if (srb->msg_count > 1) {
1449                 dprintkl(KERN_INFO,
1450                         "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1451                         srb->msg_count, srb->msgout_buf[0],
1452                         srb->msgout_buf[1]);
1453                 return;
1454         }
1455         *ptr++ = MSG_EXTENDED;  /* (01h) */
1456         *ptr++ = 2;             /* length */
1457         *ptr++ = EXTENDED_WDTR; /* (03h) */
1458         *ptr++ = wide;
1459         srb->msg_count += 4;
1460         srb->state |= SRB_DO_WIDE_NEGO;
1461 }
1462
1463
1464 #if 0
1465 /* Timer to work around chip flaw: When selecting and the bus is 
1466  * busy, we sometimes miss a Selection timeout IRQ */
1467 void selection_timeout_missed(unsigned long ptr);
1468 /* Sets the timer to wake us up */
1469 static void selto_timer(struct AdapterCtlBlk *acb)
1470 {
1471         if (timer_pending(&acb->selto_timer))
1472                 return;
1473         acb->selto_timer.function = selection_timeout_missed;
1474         acb->selto_timer.data = (unsigned long) acb;
1475         if (time_before
1476             (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
1477                 acb->selto_timer.expires =
1478                     acb->scsi_host->last_reset + HZ / 2 + 1;
1479         else
1480                 acb->selto_timer.expires = jiffies + HZ + 1;
1481         add_timer(&acb->selto_timer);
1482 }
1483
1484
1485 void selection_timeout_missed(unsigned long ptr)
1486 {
1487         unsigned long flags;
1488         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
1489         struct ScsiReqBlk *srb;
1490         dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1491         if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1492                 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1493                 return;
1494         }
1495         DC395x_LOCK_IO(acb->scsi_host, flags);
1496         srb = acb->active_dcb->active_srb;
1497         disconnect(acb);
1498         DC395x_UNLOCK_IO(acb->scsi_host, flags);
1499 }
1500 #endif
1501
1502
1503 static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1504                 struct ScsiReqBlk* srb)
1505 {
1506         u16 s_stat2, return_code;
1507         u8 s_stat, scsicommand, i, identify_message;
1508         u8 *ptr;
1509         dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
1510                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
1511
1512         srb->tag_number = TAG_NONE;     /* acb->tag_max_num: had error read in eeprom */
1513
1514         s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1515         s_stat2 = 0;
1516         s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1517 #if 1
1518         if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1519                 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
1520                         srb->cmd->pid, s_stat, s_stat2);
1521                 /*
1522                  * Try anyway?
1523                  *
1524                  * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1525                  * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed!
1526                  * (This is likely to be a bug in the hardware. Obviously, most people
1527                  *  only have one initiator per SCSI bus.)
1528                  * Instead let this fail and have the timer make sure the command is 
1529                  * tried again after a short time
1530                  */
1531                 /*selto_timer (acb); */
1532                 return 1;
1533         }
1534 #endif
1535         if (acb->active_dcb) {
1536                 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
1537                         "command while another command (pid#%li) is active.",
1538                         srb->cmd->pid,
1539                         acb->active_dcb->active_srb ?
1540                             acb->active_dcb->active_srb->cmd->pid : 0);
1541                 return 1;
1542         }
1543         if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1544                 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
1545                         srb->cmd->pid);
1546                 return 1;
1547         }
1548         /* Allow starting of SCSI commands half a second before we allow the mid-level
1549          * to queue them again after a reset */
1550         if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1551                 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1552                 return 1;
1553         }
1554
1555         /* Flush FIFO */
1556         clear_fifo(acb, "start_scsi");
1557         DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1558         DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1559         DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1560         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1561         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
1562
1563         identify_message = dcb->identify_msg;
1564         /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1565         /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1566         if (srb->flag & AUTO_REQSENSE)
1567                 identify_message &= 0xBF;
1568
1569         if (((srb->cmd->cmnd[0] == INQUIRY)
1570              || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1571              || (srb->flag & AUTO_REQSENSE))
1572             && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1573                  && !(dcb->sync_mode & WIDE_NEGO_DONE))
1574                 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1575                     && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1576             && (dcb->target_lun == 0)) {
1577                 srb->msgout_buf[0] = identify_message;
1578                 srb->msg_count = 1;
1579                 scsicommand = SCMD_SEL_ATNSTOP;
1580                 srb->state = SRB_MSGOUT;
1581 #ifndef SYNC_FIRST
1582                 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1583                     && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1584                         build_wdtr(acb, dcb, srb);
1585                         goto no_cmd;
1586                 }
1587 #endif
1588                 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1589                     && dcb->inquiry7 & SCSI_INQ_SYNC) {
1590                         build_sdtr(acb, dcb, srb);
1591                         goto no_cmd;
1592                 }
1593                 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1594                     && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1595                         build_wdtr(acb, dcb, srb);
1596                         goto no_cmd;
1597                 }
1598                 srb->msg_count = 0;
1599         }
1600         /* Send identify message */
1601         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1602
1603         scsicommand = SCMD_SEL_ATN;
1604         srb->state = SRB_START_;
1605 #ifndef DC395x_NO_TAGQ
1606         if ((dcb->sync_mode & EN_TAG_QUEUEING)
1607             && (identify_message & 0xC0)) {
1608                 /* Send Tag message */
1609                 u32 tag_mask = 1;
1610                 u8 tag_number = 0;
1611                 while (tag_mask & dcb->tag_mask
1612                        && tag_number <= dcb->max_command) {
1613                         tag_mask = tag_mask << 1;
1614                         tag_number++;
1615                 }
1616                 if (tag_number >= dcb->max_command) {
1617                         dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
1618                                 "Out of tags target=<%02i-%i>)\n",
1619                                 srb->cmd->pid, srb->cmd->device->id,
1620                                 srb->cmd->device->lun);
1621                         srb->state = SRB_READY;
1622                         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1623                                        DO_HWRESELECT);
1624                         return 1;
1625                 }
1626                 /* Send Tag id */
1627                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1628                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1629                 dcb->tag_mask |= tag_mask;
1630                 srb->tag_number = tag_number;
1631                 scsicommand = SCMD_SEL_ATN3;
1632                 srb->state = SRB_START_;
1633         }
1634 #endif
1635 /*polling:*/
1636         /* Send CDB ..command block ......... */
1637         dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
1638                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
1639                 srb->cmd->cmnd[0], srb->tag_number);
1640         if (srb->flag & AUTO_REQSENSE) {
1641                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1642                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1643                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1644                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1645                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1646                               sizeof(srb->cmd->sense_buffer));
1647                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1648         } else {
1649                 ptr = (u8 *)srb->cmd->cmnd;
1650                 for (i = 0; i < srb->cmd->cmd_len; i++)
1651                         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1652         }
1653       no_cmd:
1654         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1655                        DO_HWRESELECT | DO_DATALATCH);
1656         if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1657                 /* 
1658                  * If start_scsi return 1:
1659                  * we caught an interrupt (must be reset or reselection ... )
1660                  * : Let's process it first!
1661                  */
1662                 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
1663                         srb->cmd->pid, dcb->target_id, dcb->target_lun);
1664                 srb->state = SRB_READY;
1665                 free_tag(dcb, srb);
1666                 srb->msg_count = 0;
1667                 return_code = 1;
1668                 /* This IRQ should NOT get lost, as we did not acknowledge it */
1669         } else {
1670                 /* 
1671                  * If start_scsi returns 0:
1672                  * we know that the SCSI processor is free
1673                  */
1674                 srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
1675                 dcb->active_srb = srb;
1676                 acb->active_dcb = dcb;
1677                 return_code = 0;
1678                 /* it's important for atn stop */
1679                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1680                                DO_DATALATCH | DO_HWRESELECT);
1681                 /* SCSI command */
1682                 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1683         }
1684         return return_code;
1685 }
1686
1687
1688 #define DC395x_ENABLE_MSGOUT \
1689  DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1690  srb->state |= SRB_MSGOUT
1691
1692
1693 /* abort command */
1694 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1695                 struct ScsiReqBlk *srb)
1696 {
1697         srb->msgout_buf[0] = ABORT;
1698         srb->msg_count = 1;
1699         DC395x_ENABLE_MSGOUT;
1700         srb->state &= ~SRB_MSGIN;
1701         srb->state |= SRB_MSGOUT;
1702 }
1703
1704
1705 /**
1706  * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1707  *                           have been triggered for this card.
1708  *
1709  * @acb:         a pointer to the adpter control block
1710  * @scsi_status: the status return when we checked the card
1711  **/
1712 static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1713                 u16 scsi_status)
1714 {
1715         struct DeviceCtlBlk *dcb;
1716         struct ScsiReqBlk *srb;
1717         u16 phase;
1718         u8 scsi_intstatus;
1719         unsigned long flags;
1720         void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *, 
1721                               u16 *);
1722
1723         DC395x_LOCK_IO(acb->scsi_host, flags);
1724
1725         /* This acknowledges the IRQ */
1726         scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1727         if ((scsi_status & 0x2007) == 0x2002)
1728                 dprintkl(KERN_DEBUG,
1729                         "COP after COP completed? %04x\n", scsi_status);
1730         if (debug_enabled(DBG_KG)) {
1731                 if (scsi_intstatus & INT_SELTIMEOUT)
1732                         dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1733         }
1734         /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1735
1736         if (timer_pending(&acb->selto_timer))
1737                 del_timer(&acb->selto_timer);
1738
1739         if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1740                 disconnect(acb);        /* bus free interrupt  */
1741                 goto out_unlock;
1742         }
1743         if (scsi_intstatus & INT_RESELECTED) {
1744                 reselect(acb);
1745                 goto out_unlock;
1746         }
1747         if (scsi_intstatus & INT_SELECT) {
1748                 dprintkl(KERN_INFO, "Host does not support target mode!\n");
1749                 goto out_unlock;
1750         }
1751         if (scsi_intstatus & INT_SCSIRESET) {
1752                 scsi_reset_detect(acb);
1753                 goto out_unlock;
1754         }
1755         if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1756                 dcb = acb->active_dcb;
1757                 if (!dcb) {
1758                         dprintkl(KERN_DEBUG,
1759                                 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1760                                 scsi_status, scsi_intstatus);
1761                         goto out_unlock;
1762                 }
1763                 srb = dcb->active_srb;
1764                 if (dcb->flag & ABORT_DEV_) {
1765                         dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1766                         enable_msgout_abort(acb, srb);
1767                 }
1768
1769                 /* software sequential machine */
1770                 phase = (u16)srb->scsi_phase;
1771
1772                 /* 
1773                  * 62037 or 62137
1774                  * call  dc395x_scsi_phase0[]... "phase entry"
1775                  * handle every phase before start transfer
1776                  */
1777                 /* data_out_phase0,     phase:0 */
1778                 /* data_in_phase0,      phase:1 */
1779                 /* command_phase0,      phase:2 */
1780                 /* status_phase0,       phase:3 */
1781                 /* nop0,                phase:4 PH_BUS_FREE .. initial phase */
1782                 /* nop0,                phase:5 PH_BUS_FREE .. initial phase */
1783                 /* msgout_phase0,       phase:6 */
1784                 /* msgin_phase0,        phase:7 */
1785                 dc395x_statev = dc395x_scsi_phase0[phase];
1786                 dc395x_statev(acb, srb, &scsi_status);
1787
1788                 /* 
1789                  * if there were any exception occured scsi_status
1790                  * will be modify to bus free phase new scsi_status
1791                  * transfer out from ... previous dc395x_statev
1792                  */
1793                 srb->scsi_phase = scsi_status & PHASEMASK;
1794                 phase = (u16)scsi_status & PHASEMASK;
1795
1796                 /* 
1797                  * call  dc395x_scsi_phase1[]... "phase entry" handle
1798                  * every phase to do transfer
1799                  */
1800                 /* data_out_phase1,     phase:0 */
1801                 /* data_in_phase1,      phase:1 */
1802                 /* command_phase1,      phase:2 */
1803                 /* status_phase1,       phase:3 */
1804                 /* nop1,                phase:4 PH_BUS_FREE .. initial phase */
1805                 /* nop1,                phase:5 PH_BUS_FREE .. initial phase */
1806                 /* msgout_phase1,       phase:6 */
1807                 /* msgin_phase1,        phase:7 */
1808                 dc395x_statev = dc395x_scsi_phase1[phase];
1809                 dc395x_statev(acb, srb, &scsi_status);
1810         }
1811       out_unlock:
1812         DC395x_UNLOCK_IO(acb->scsi_host, flags);
1813 }
1814
1815
1816 static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
1817 {
1818         struct AdapterCtlBlk *acb = dev_id;
1819         u16 scsi_status;
1820         u8 dma_status;
1821         irqreturn_t handled = IRQ_NONE;
1822
1823         /*
1824          * Check for pending interupt
1825          */
1826         scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1827         dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1828         if (scsi_status & SCSIINTERRUPT) {
1829                 /* interupt pending - let's process it! */
1830                 dc395x_handle_interrupt(acb, scsi_status);
1831                 handled = IRQ_HANDLED;
1832         }
1833         else if (dma_status & 0x20) {
1834                 /* Error from the DMA engine */
1835                 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1836 #if 0
1837                 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
1838                 if (acb->active_dcb) {
1839                         acb->active_dcb-> flag |= ABORT_DEV_;
1840                         if (acb->active_dcb->active_srb)
1841                                 enable_msgout_abort(acb, acb->active_dcb->active_srb);
1842                 }
1843                 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
1844 #else
1845                 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
1846                 acb = NULL;
1847 #endif
1848                 handled = IRQ_HANDLED;
1849         }
1850
1851         return handled;
1852 }
1853
1854
1855 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1856                 u16 *pscsi_status)
1857 {
1858         dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->pid);
1859         if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
1860                 *pscsi_status = PH_BUS_FREE;    /*.. initial phase */
1861
1862         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
1863         srb->state &= ~SRB_MSGOUT;
1864 }
1865
1866
1867 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1868                 u16 *pscsi_status)
1869 {
1870         u16 i;
1871         u8 *ptr;
1872         dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->pid);
1873
1874         clear_fifo(acb, "msgout_phase1");
1875         if (!(srb->state & SRB_MSGOUT)) {
1876                 srb->state |= SRB_MSGOUT;
1877                 dprintkl(KERN_DEBUG,
1878                         "msgout_phase1: (pid#%li) Phase unexpected\n",
1879                         srb->cmd->pid); /* So what ? */
1880         }
1881         if (!srb->msg_count) {
1882                 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
1883                         srb->cmd->pid);
1884                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
1885                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
1886                 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1887                 return;
1888         }
1889         ptr = (u8 *)srb->msgout_buf;
1890         for (i = 0; i < srb->msg_count; i++)
1891                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1892         srb->msg_count = 0;
1893         if (srb->msgout_buf[0] == MSG_ABORT)
1894                 srb->state = SRB_ABORT_SENT;
1895
1896         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1897 }
1898
1899
1900 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1901                 u16 *pscsi_status)
1902 {
1903         dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->pid);
1904         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1905 }
1906
1907
1908 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1909                 u16 *pscsi_status)
1910 {
1911         struct DeviceCtlBlk *dcb;
1912         u8 *ptr;
1913         u16 i;
1914         dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->pid);
1915
1916         clear_fifo(acb, "command_phase1");
1917         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
1918         if (!(srb->flag & AUTO_REQSENSE)) {
1919                 ptr = (u8 *)srb->cmd->cmnd;
1920                 for (i = 0; i < srb->cmd->cmd_len; i++) {
1921                         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
1922                         ptr++;
1923                 }
1924         } else {
1925                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1926                 dcb = acb->active_dcb;
1927                 /* target id */
1928                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1929                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1930                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1931                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1932                               sizeof(srb->cmd->sense_buffer));
1933                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1934         }
1935         srb->state |= SRB_COMMAND;
1936         /* it's important for atn stop */
1937         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1938         /* SCSI command */
1939         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1940 }
1941
1942
1943 /*
1944  * Verify that the remaining space in the hw sg lists is the same as
1945  * the count of remaining bytes in srb->total_xfer_length
1946  */
1947 static void sg_verify_length(struct ScsiReqBlk *srb)
1948 {
1949         if (debug_enabled(DBG_SG)) {
1950                 unsigned len = 0;
1951                 unsigned idx = srb->sg_index;
1952                 struct SGentry *psge = srb->segment_x + idx;
1953                 for (; idx < srb->sg_count; psge++, idx++)
1954                         len += psge->length;
1955                 if (len != srb->total_xfer_length)
1956                         dprintkdbg(DBG_SG,
1957                                "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1958                                srb->total_xfer_length, len);
1959         }                              
1960 }
1961
1962
1963 /*
1964  * Compute the next Scatter Gather list index and adjust its length
1965  * and address if necessary
1966  */
1967 static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
1968 {
1969         u8 idx;
1970         u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
1971         struct SGentry *psge = srb->segment_x + srb->sg_index;
1972
1973         dprintkdbg(DBG_0,
1974                 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
1975                 xferred, srb->total_xfer_length, left);
1976         if (xferred == 0) {
1977                 /* nothing to update since we did not transfer any data */
1978                 return;
1979         }
1980
1981         sg_verify_length(srb);
1982         srb->total_xfer_length = left;  /* update remaining count */
1983         for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
1984                 if (xferred >= psge->length) {
1985                         /* Complete SG entries done */
1986                         xferred -= psge->length;
1987                 } else {
1988                         /* Partial SG entry done */
1989                         psge->length -= xferred;
1990                         psge->address += xferred;
1991                         srb->sg_index = idx;
1992                         pci_dma_sync_single_for_device(srb->dcb->
1993                                             acb->dev,
1994                                             srb->sg_bus_addr,
1995                                             SEGMENTX_LEN,
1996                                             PCI_DMA_TODEVICE);
1997                         break;
1998                 }
1999                 psge++;
2000         }
2001         sg_verify_length(srb);
2002 }
2003
2004
2005 /*
2006  * We have transfered a single byte (PIO mode?) and need to update
2007  * the count of bytes remaining (total_xfer_length) and update the sg
2008  * entry to either point to next byte in the current sg entry, or of
2009  * already at the end to point to the start of the next sg entry
2010  */
2011 static void sg_subtract_one(struct ScsiReqBlk *srb)
2012 {
2013         sg_update_list(srb, srb->total_xfer_length - 1);
2014 }
2015
2016
2017 /* 
2018  * cleanup_after_transfer
2019  * 
2020  * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
2021  * KG: Currently called from  StatusPhase1 ()
2022  * Should probably also be called from other places
2023  * Best might be to call it in DataXXPhase0, if new phase will differ 
2024  */
2025 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
2026                 struct ScsiReqBlk *srb)
2027 {
2028         /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
2029         if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) {       /* read */
2030                 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2031                         clear_fifo(acb, "cleanup/in");
2032                 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2033                         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2034         } else {                /* write */
2035                 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2036                         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2037                 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2038                         clear_fifo(acb, "cleanup/out");
2039         }
2040         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2041 }
2042
2043
2044 /*
2045  * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
2046  * Seems to be needed for unknown reasons; could be a hardware bug :-(
2047  */
2048 #define DC395x_LASTPIO 4
2049
2050
2051 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2052                 u16 *pscsi_status)
2053 {
2054         struct DeviceCtlBlk *dcb = srb->dcb;
2055         u16 scsi_status = *pscsi_status;
2056         u32 d_left_counter = 0;
2057         dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
2058                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2059
2060         /*
2061          * KG: We need to drain the buffers before we draw any conclusions!
2062          * This means telling the DMA to push the rest into SCSI, telling
2063          * SCSI to push the rest to the bus.
2064          * However, the device might have been the one to stop us (phase
2065          * change), and the data in transit just needs to be accounted so
2066          * it can be retransmitted.)
2067          */
2068         /* 
2069          * KG: Stop DMA engine pushing more data into the SCSI FIFO
2070          * If we need more data, the DMA SG list will be freshly set up, anyway
2071          */
2072         dprintkdbg(DBG_PIO, "data_out_phase0: "
2073                 "DMA{fifocnt=0x%02x fifostat=0x%02x} "
2074                 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
2075                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2076                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2077                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2078                 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2079                 srb->total_xfer_length);
2080         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2081
2082         if (!(srb->state & SRB_XFERPAD)) {
2083                 if (scsi_status & PARITYERROR)
2084                         srb->status |= PARITY_ERROR;
2085
2086                 /*
2087                  * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2088                  * is the no of bytes it got from the DMA engine not the no it 
2089                  * transferred successfully to the device. (And the difference could
2090                  * be as much as the FIFO size, I guess ...)
2091                  */
2092                 if (!(scsi_status & SCSIXFERDONE)) {
2093                         /*
2094                          * when data transfer from DMA FIFO to SCSI FIFO
2095                          * if there was some data left in SCSI FIFO
2096                          */
2097                         d_left_counter =
2098                             (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2099                                   0x1F);
2100                         if (dcb->sync_period & WIDE_SYNC)
2101                                 d_left_counter <<= 1;
2102
2103                         dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
2104                                 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
2105                                 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
2106                                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2107                                 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2108                                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2109                                 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2110                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2111                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2112                                 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2113                 }
2114                 /*
2115                  * calculate all the residue data that not yet tranfered
2116                  * SCSI transfer counter + left in SCSI FIFO data
2117                  *
2118                  * .....TRM_S1040_SCSI_COUNTER (24bits)
2119                  * The counter always decrement by one for every SCSI byte transfer.
2120                  * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2121                  * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2122                  */
2123                 if (srb->total_xfer_length > DC395x_LASTPIO)
2124                         d_left_counter +=
2125                             DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2126
2127                 /* Is this a good idea? */
2128                 /*clear_fifo(acb, "DOP1"); */
2129                 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2130                 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2131                     && srb->cmd->request_bufflen % 2) {
2132                         d_left_counter = 0;
2133                         dprintkl(KERN_INFO,
2134                                 "data_out_phase0: Discard 1 byte (0x%02x)\n",
2135                                 scsi_status);
2136                 }
2137                 /*
2138                  * KG: Oops again. Same thinko as above: The SCSI might have been
2139                  * faster than the DMA engine, so that it ran out of data.
2140                  * In that case, we have to do just nothing! 
2141                  * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2142                  */
2143                 /*
2144                  * KG: This is nonsense: We have been WRITING data to the bus
2145                  * If the SCSI engine has no bytes left, how should the DMA engine?
2146                  */
2147                 if (d_left_counter == 0) {
2148                         srb->total_xfer_length = 0;
2149                 } else {
2150                         /*
2151                          * if transfer not yet complete
2152                          * there were some data residue in SCSI FIFO or
2153                          * SCSI transfer counter not empty
2154                          */
2155                         long oldxferred =
2156                             srb->total_xfer_length - d_left_counter;
2157                         const int diff =
2158                             (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2159                         sg_update_list(srb, d_left_counter);
2160                         /* KG: Most ugly hack! Apparently, this works around a chip bug */
2161                         if ((srb->segment_x[srb->sg_index].length ==
2162                              diff && srb->cmd->use_sg)
2163                             || ((oldxferred & ~PAGE_MASK) ==
2164                                 (PAGE_SIZE - diff))
2165                             ) {
2166                                 dprintkl(KERN_INFO, "data_out_phase0: "
2167                                         "Work around chip bug (%i)?\n", diff);
2168                                 d_left_counter =
2169                                     srb->total_xfer_length - diff;
2170                                 sg_update_list(srb, d_left_counter);
2171                                 /*srb->total_xfer_length -= diff; */
2172                                 /*srb->virt_addr += diff; */
2173                                 /*if (srb->cmd->use_sg) */
2174                                 /*      srb->sg_index++; */
2175                         }
2176                 }
2177         }
2178         if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2179                 cleanup_after_transfer(acb, srb);
2180         }
2181 }
2182
2183
2184 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2185                 u16 *pscsi_status)
2186 {
2187         dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
2188                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2189         clear_fifo(acb, "data_out_phase1");
2190         /* do prepare before transfer when data out phase */
2191         data_io_transfer(acb, srb, XFERDATAOUT);
2192 }
2193
2194 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2195                 u16 *pscsi_status)
2196 {
2197         u16 scsi_status = *pscsi_status;
2198
2199         dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
2200                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2201
2202         /*
2203          * KG: DataIn is much more tricky than DataOut. When the device is finished
2204          * and switches to another phase, the SCSI engine should be finished too.
2205          * But: There might still be bytes left in its FIFO to be fetched by the DMA
2206          * engine and transferred to memory.
2207          * We should wait for the FIFOs to be emptied by that (is there any way to 
2208          * enforce this?) and then stop the DMA engine, because it might think, that
2209          * there are more bytes to follow. Yes, the device might disconnect prior to
2210          * having all bytes transferred! 
2211          * Also we should make sure that all data from the DMA engine buffer's really
2212          * made its way to the system memory! Some documentation on this would not
2213          * seem to be a bad idea, actually.
2214          */
2215         if (!(srb->state & SRB_XFERPAD)) {
2216                 u32 d_left_counter;
2217                 unsigned int sc, fc;
2218
2219                 if (scsi_status & PARITYERROR) {
2220                         dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
2221                                 "Parity Error\n", srb->cmd->pid);
2222                         srb->status |= PARITY_ERROR;
2223                 }
2224                 /*
2225                  * KG: We should wait for the DMA FIFO to be empty ...
2226                  * but: it would be better to wait first for the SCSI FIFO and then the
2227                  * the DMA FIFO to become empty? How do we know, that the device not already
2228                  * sent data to the FIFO in a MsgIn phase, eg.?
2229                  */
2230                 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2231 #if 0
2232                         int ctr = 6000000;
2233                         dprintkl(KERN_DEBUG,
2234                                 "DIP0: Wait for DMA FIFO to flush ...\n");
2235                         /*DC395x_write8  (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2236                         /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2237                         /*DC395x_write8  (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2238                         while (!
2239                                (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
2240                                 0x80) && --ctr);
2241                         if (ctr < 6000000 - 1)
2242                                 dprintkl(KERN_DEBUG
2243                                        "DIP0: Had to wait for DMA ...\n");
2244                         if (!ctr)
2245                                 dprintkl(KERN_ERR,
2246                                        "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2247                         /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2248 #endif
2249                         dprintkdbg(DBG_KG, "data_in_phase0: "
2250                                 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2251                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2252                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2253                 }
2254                 /* Now: Check remainig data: The SCSI counters should tell us ... */
2255                 sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2256                 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2257                 d_left_counter = sc + ((fc & 0x1f)
2258                        << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2259                            0));
2260                 dprintkdbg(DBG_KG, "data_in_phase0: "
2261                         "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2262                         "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2263                         "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2264                         fc,
2265                         (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2266                         sc,
2267                         fc,
2268                         DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2269                         DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
2270                         srb->total_xfer_length, d_left_counter);
2271 #if DC395x_LASTPIO
2272                 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2273                 if (d_left_counter
2274                     && srb->total_xfer_length <= DC395x_LASTPIO) {
2275                         size_t left_io = srb->total_xfer_length;
2276
2277                         /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2278                         /*sg_update_list (srb, d_left_counter); */
2279                         dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
2280                                    "for remaining %i bytes:",
2281                                 fc & 0x1f,
2282                                 (srb->dcb->sync_period & WIDE_SYNC) ?
2283                                     "words" : "bytes",
2284                                 srb->total_xfer_length);
2285                         if (srb->dcb->sync_period & WIDE_SYNC)
2286                                 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2287                                               CFG2_WIDEFIFO);
2288                         while (left_io) {
2289                                 unsigned char *virt, *base = NULL;
2290                                 unsigned long flags = 0;
2291                                 size_t len = left_io;
2292
2293                                 if (srb->cmd->use_sg) {
2294                                         size_t offset = srb->request_length - left_io;
2295                                         local_irq_save(flags);
2296                                         /* Assumption: it's inside one page as it's at most 4 bytes and
2297                                            I just assume it's on a 4-byte boundary */
2298                                         base = scsi_kmap_atomic_sg((struct scatterlist *)srb->cmd->request_buffer,
2299                                                                      srb->sg_count, &offset, &len);
2300                                         virt = base + offset;
2301                                 } else {
2302                                         virt = srb->cmd->request_buffer + srb->cmd->request_bufflen - left_io;
2303                                         len = left_io;
2304                                 }
2305                                 left_io -= len;
2306
2307                                 while (len) {
2308                                         u8 byte;
2309                                         byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2310                                         *virt++ = byte;
2311
2312                                         if (debug_enabled(DBG_PIO))
2313                                                 printk(" %02x", byte);
2314
2315                                         d_left_counter--;
2316                                         sg_subtract_one(srb);
2317
2318                                         len--;
2319
2320                                         fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2321
2322                                         if (fc == 0x40) {
2323                                                 left_io = 0;
2324                                                 break;
2325                                         }
2326                                 }
2327
2328                                 WARN_ON((fc != 0x40) == !d_left_counter);
2329
2330                                 if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
2331                                         /* Read the last byte ... */
2332                                         if (srb->total_xfer_length > 0) {
2333                                                 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2334
2335                                                 *virt++ = byte;
2336                                                 srb->total_xfer_length--;
2337                                                 if (debug_enabled(DBG_PIO))
2338                                                         printk(" %02x", byte);
2339                                         }
2340
2341                                         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2342                                 }
2343
2344                                 if (srb->cmd->use_sg) {
2345                                         scsi_kunmap_atomic_sg(base);
2346                                         local_irq_restore(flags);
2347                                 }
2348                         }
2349                         /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2350                         /*srb->total_xfer_length = 0; */
2351                         if (debug_enabled(DBG_PIO))
2352                                 printk("\n");
2353                 }
2354 #endif                          /* DC395x_LASTPIO */
2355
2356 #if 0
2357                 /*
2358                  * KG: This was in DATAOUT. Does it also belong here?
2359                  * Nobody seems to know what counter and fifo_cnt count exactly ...
2360                  */
2361                 if (!(scsi_status & SCSIXFERDONE)) {
2362                         /*
2363                          * when data transfer from DMA FIFO to SCSI FIFO
2364                          * if there was some data left in SCSI FIFO
2365                          */
2366                         d_left_counter =
2367                             (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2368                                   0x1F);
2369                         if (srb->dcb->sync_period & WIDE_SYNC)
2370                                 d_left_counter <<= 1;
2371                         /*
2372                          * if WIDE scsi SCSI FIFOCNT unit is word !!!
2373                          * so need to *= 2
2374                          * KG: Seems to be correct ...
2375                          */
2376                 }
2377 #endif
2378                 /* KG: This should not be needed any more! */
2379                 if (d_left_counter == 0
2380                     || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2381 #if 0
2382                         int ctr = 6000000;
2383                         u8 TempDMAstatus;
2384                         do {
2385                                 TempDMAstatus =
2386                                     DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2387                         } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
2388                         if (!ctr)
2389                                 dprintkl(KERN_ERR,
2390                                        "Deadlock in DataInPhase0 waiting for DMA!!\n");
2391                         srb->total_xfer_length = 0;
2392 #endif
2393                         srb->total_xfer_length = d_left_counter;
2394                 } else {        /* phase changed */
2395                         /*
2396                          * parsing the case:
2397                          * when a transfer not yet complete 
2398                          * but be disconnected by target
2399                          * if transfer not yet complete
2400                          * there were some data residue in SCSI FIFO or
2401                          * SCSI transfer counter not empty
2402                          */
2403                         sg_update_list(srb, d_left_counter);
2404                 }
2405         }
2406         /* KG: The target may decide to disconnect: Empty FIFO before! */
2407         if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2408                 cleanup_after_transfer(acb, srb);
2409         }
2410 }
2411
2412
2413 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2414                 u16 *pscsi_status)
2415 {
2416         dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
2417                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2418         data_io_transfer(acb, srb, XFERDATAIN);
2419 }
2420
2421
2422 static void data_io_transfer(struct AdapterCtlBlk *acb, 
2423                 struct ScsiReqBlk *srb, u16 io_dir)
2424 {
2425         struct DeviceCtlBlk *dcb = srb->dcb;
2426         u8 bval;
2427         dprintkdbg(DBG_0,
2428                 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
2429                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
2430                 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
2431                 srb->total_xfer_length, srb->sg_index, srb->sg_count);
2432         if (srb == acb->tmp_srb)
2433                 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
2434         if (srb->sg_index >= srb->sg_count) {
2435                 /* can't happen? out of bounds error */
2436                 return;
2437         }
2438
2439         if (srb->total_xfer_length > DC395x_LASTPIO) {
2440                 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2441                 /*
2442                  * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2443                  * Maybe, even ABORTXFER would be appropriate
2444                  */
2445                 if (dma_status & XFERPENDING) {
2446                         dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
2447                                 "Expect trouble!\n");
2448                         dump_register_info(acb, dcb, srb);
2449                         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2450                 }
2451                 /* clear_fifo(acb, "IO"); */
2452                 /* 
2453                  * load what physical address of Scatter/Gather list table
2454                  * want to be transfer
2455                  */
2456                 srb->state |= SRB_DATA_XFER;
2457                 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2458                 if (srb->cmd->use_sg) { /* with S/G */
2459                         io_dir |= DMACMD_SG;
2460                         DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2461                                        srb->sg_bus_addr +
2462                                        sizeof(struct SGentry) *
2463                                        srb->sg_index);
2464                         /* load how many bytes in the sg list table */
2465                         DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2466                                        ((u32)(srb->sg_count -
2467                                               srb->sg_index) << 3));
2468                 } else {        /* without S/G */
2469                         io_dir &= ~DMACMD_SG;
2470                         DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2471                                        srb->segment_x[0].address);
2472                         DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2473                                        srb->segment_x[0].length);
2474                 }
2475                 /* load total transfer length (24bits) max value 16Mbyte */
2476                 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2477                                srb->total_xfer_length);
2478                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
2479                 if (io_dir & DMACMD_DIR) {      /* read */
2480                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2481                                       SCMD_DMA_IN);
2482                         DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2483                 } else {
2484                         DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2485                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2486                                       SCMD_DMA_OUT);
2487                 }
2488
2489         }
2490 #if DC395x_LASTPIO
2491         else if (srb->total_xfer_length > 0) {  /* The last four bytes: Do PIO */
2492                 /* 
2493                  * load what physical address of Scatter/Gather list table
2494                  * want to be transfer
2495                  */
2496                 srb->state |= SRB_DATA_XFER;
2497                 /* load total transfer length (24bits) max value 16Mbyte */
2498                 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2499                                srb->total_xfer_length);
2500                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
2501                 if (io_dir & DMACMD_DIR) {      /* read */
2502                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2503                                       SCMD_FIFO_IN);
2504                 } else {        /* write */
2505                         int ln = srb->total_xfer_length;
2506                         size_t left_io = srb->total_xfer_length;
2507
2508                         if (srb->dcb->sync_period & WIDE_SYNC)
2509                                 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2510                                      CFG2_WIDEFIFO);
2511
2512                         while (left_io) {
2513                                 unsigned char *virt, *base = NULL;
2514                                 unsigned long flags = 0;
2515                                 size_t len = left_io;
2516
2517                                 if (srb->cmd->use_sg) {
2518                                         size_t offset = srb->request_length - left_io;
2519                                         local_irq_save(flags);
2520                                         /* Again, max 4 bytes */
2521                                         base = scsi_kmap_atomic_sg((struct scatterlist *)srb->cmd->request_buffer,
2522                                                                      srb->sg_count, &offset, &len);
2523                                         virt = base + offset;
2524                                 } else {
2525                                         virt = srb->cmd->request_buffer + srb->cmd->request_bufflen - left_io;
2526                                         len = left_io;
2527                                 }
2528                                 left_io -= len;
2529
2530                                 while (len--) {
2531                                         if (debug_enabled(DBG_PIO))
2532                                                 printk(" %02x", *virt);
2533
2534                                         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
2535
2536                                         sg_subtract_one(srb);
2537                                 }
2538
2539                                 if (srb->cmd->use_sg) {
2540                                         scsi_kunmap_atomic_sg(base);
2541                                         local_irq_restore(flags);
2542                                 }
2543                         }
2544                         if (srb->dcb->sync_period & WIDE_SYNC) {
2545                                 if (ln % 2) {
2546                                         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2547                                         if (debug_enabled(DBG_PIO))
2548                                                 printk(" |00");
2549                                 }
2550                                 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2551                         }
2552                         /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2553                         if (debug_enabled(DBG_PIO))
2554                                 printk("\n");
2555                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2556                                           SCMD_FIFO_OUT);
2557                 }
2558         }
2559 #endif                          /* DC395x_LASTPIO */
2560         else {          /* xfer pad */
2561                 u8 data = 0, data2 = 0;
2562                 if (srb->sg_count) {
2563                         srb->adapter_status = H_OVER_UNDER_RUN;
2564                         srb->status |= OVER_RUN;
2565                 }
2566                 /*
2567                  * KG: despite the fact that we are using 16 bits I/O ops
2568                  * the SCSI FIFO is only 8 bits according to the docs
2569                  * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2570                  */
2571                 if (dcb->sync_period & WIDE_SYNC) {
2572                         DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
2573                         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2574                                       CFG2_WIDEFIFO);
2575                         if (io_dir & DMACMD_DIR) {
2576                                 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2577                                 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2578                         } else {
2579                                 /* Danger, Robinson: If you find KGs
2580                                  * scattered over the wide disk, the driver
2581                                  * or chip is to blame :-( */
2582                                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2583                                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
2584                         }
2585                         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2586                 } else {
2587                         DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2588                         /* Danger, Robinson: If you find a collection of Ks on your disk
2589                          * something broke :-( */
2590                         if (io_dir & DMACMD_DIR)
2591                                 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2592                         else
2593                                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2594                 }
2595                 srb->state |= SRB_XFERPAD;
2596                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
2597                 /* SCSI command */
2598                 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
2599                 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
2600         }
2601 }
2602
2603
2604 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2605                 u16 *pscsi_status)
2606 {
2607         dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
2608                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2609         srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2610         srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);      /* get message */
2611         srb->state = SRB_COMPLETED;
2612         *pscsi_status = PH_BUS_FREE;    /*.. initial phase */
2613         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
2614         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2615 }
2616
2617
2618 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2619                 u16 *pscsi_status)
2620 {
2621         dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
2622                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2623         srb->state = SRB_STATUS;
2624         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
2625         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
2626 }
2627
2628
2629 /* Check if the message is complete */
2630 static inline u8 msgin_completed(u8 * msgbuf, u32 len)
2631 {
2632         if (*msgbuf == EXTENDED_MESSAGE) {
2633                 if (len < 2)
2634                         return 0;
2635                 if (len < msgbuf[1] + 2)
2636                         return 0;
2637         } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f)  /* two byte messages */
2638                 if (len < 2)
2639                         return 0;
2640         return 1;
2641 }
2642
2643 /* reject_msg */
2644 static inline void msgin_reject(struct AdapterCtlBlk *acb,
2645                 struct ScsiReqBlk *srb)
2646 {
2647         srb->msgout_buf[0] = MESSAGE_REJECT;
2648         srb->msg_count = 1;
2649         DC395x_ENABLE_MSGOUT;
2650         srb->state &= ~SRB_MSGIN;
2651         srb->state |= SRB_MSGOUT;
2652         dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
2653                 srb->msgin_buf[0],
2654                 srb->dcb->target_id, srb->dcb->target_lun);
2655 }
2656
2657
2658 static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
2659                 struct DeviceCtlBlk *dcb, u8 tag)
2660 {
2661         struct ScsiReqBlk *srb = NULL;
2662         struct ScsiReqBlk *i;
2663         dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
2664                    srb->cmd->pid, tag, srb);
2665
2666         if (!(dcb->tag_mask & (1 << tag)))
2667                 dprintkl(KERN_DEBUG,
2668                         "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2669                         dcb->tag_mask, tag);
2670
2671         if (list_empty(&dcb->srb_going_list))
2672                 goto mingx0;
2673         list_for_each_entry(i, &dcb->srb_going_list, list) {
2674                 if (i->tag_number == tag) {
2675                         srb = i;
2676                         break;
2677                 }
2678         }
2679         if (!srb)
2680                 goto mingx0;
2681
2682         dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
2683                 srb->cmd->pid, srb->dcb->target_id, srb->dcb->target_lun);
2684         if (dcb->flag & ABORT_DEV_) {
2685                 /*srb->state = SRB_ABORT_SENT; */
2686                 enable_msgout_abort(acb, srb);
2687         }
2688
2689         if (!(srb->state & SRB_DISCONNECT))
2690                 goto mingx0;
2691
2692         memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
2693         srb->state |= dcb->active_srb->state;
2694         srb->state |= SRB_DATA_XFER;
2695         dcb->active_srb = srb;
2696         /* How can we make the DORS happy? */
2697         return srb;
2698
2699       mingx0:
2700         srb = acb->tmp_srb;
2701         srb->state = SRB_UNEXPECT_RESEL;
2702         dcb->active_srb = srb;
2703         srb->msgout_buf[0] = MSG_ABORT_TAG;
2704         srb->msg_count = 1;
2705         DC395x_ENABLE_MSGOUT;
2706         dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
2707         return srb;
2708 }
2709
2710
2711 static inline void reprogram_regs(struct AdapterCtlBlk *acb,
2712                 struct DeviceCtlBlk *dcb)
2713 {
2714         DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
2715         DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
2716         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
2717         set_xfer_rate(acb, dcb);
2718 }
2719
2720
2721 /* set async transfer mode */
2722 static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2723 {
2724         struct DeviceCtlBlk *dcb = srb->dcb;
2725         dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
2726                 dcb->target_id, dcb->target_lun);
2727
2728         dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
2729         dcb->sync_mode |= SYNC_NEGO_DONE;
2730         /*dcb->sync_period &= 0; */
2731         dcb->sync_offset = 0;
2732         dcb->min_nego_period = 200 >> 2;        /* 200ns <=> 5 MHz */
2733         srb->state &= ~SRB_DO_SYNC_NEGO;
2734         reprogram_regs(acb, dcb);
2735         if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2736             && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2737                 build_wdtr(acb, dcb, srb);
2738                 DC395x_ENABLE_MSGOUT;
2739                 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
2740         }
2741 }
2742
2743
2744 /* set sync transfer mode */
2745 static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2746 {
2747         struct DeviceCtlBlk *dcb = srb->dcb;
2748         u8 bval;
2749         int fact;
2750         dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
2751                 "(%02i.%01i MHz) Offset %i\n",
2752                 dcb->target_id, srb->msgin_buf[3] << 2,
2753                 (250 / srb->msgin_buf[3]),
2754                 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
2755                 srb->msgin_buf[4]);
2756
2757         if (srb->msgin_buf[4] > 15)
2758                 srb->msgin_buf[4] = 15;
2759         if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
2760                 dcb->sync_offset = 0;
2761         else if (dcb->sync_offset == 0)
2762                 dcb->sync_offset = srb->msgin_buf[4];
2763         if (srb->msgin_buf[4] > dcb->sync_offset)
2764                 srb->msgin_buf[4] = dcb->sync_offset;
2765         else
2766                 dcb->sync_offset = srb->msgin_buf[4];
2767         bval = 0;
2768         while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
2769                             || dcb->min_nego_period >
2770                             clock_period[bval]))
2771                 bval++;
2772         if (srb->msgin_buf[3] < clock_period[bval])
2773                 dprintkl(KERN_INFO,
2774                         "msgin_set_sync: Increase sync nego period to %ins\n",
2775                         clock_period[bval] << 2);
2776         srb->msgin_buf[3] = clock_period[bval];
2777         dcb->sync_period &= 0xf0;
2778         dcb->sync_period |= ALT_SYNC | bval;
2779         dcb->min_nego_period = srb->msgin_buf[3];
2780
2781         if (dcb->sync_period & WIDE_SYNC)
2782                 fact = 500;
2783         else
2784                 fact = 250;
2785
2786         dprintkl(KERN_INFO,
2787                 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2788                 dcb->target_id, (fact == 500) ? "Wide16" : "",
2789                 dcb->min_nego_period << 2, dcb->sync_offset,
2790                 (fact / dcb->min_nego_period),
2791                 ((fact % dcb->min_nego_period) * 10 +
2792                 dcb->min_nego_period / 2) / dcb->min_nego_period);
2793
2794         if (!(srb->state & SRB_DO_SYNC_NEGO)) {
2795                 /* Reply with corrected SDTR Message */
2796                 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
2797                         srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
2798
2799                 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
2800                 srb->msg_count = 5;
2801                 DC395x_ENABLE_MSGOUT;
2802                 dcb->sync_mode |= SYNC_NEGO_DONE;
2803         } else {
2804                 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2805                     && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2806                         build_wdtr(acb, dcb, srb);
2807                         DC395x_ENABLE_MSGOUT;
2808                         dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
2809                 }
2810         }
2811         srb->state &= ~SRB_DO_SYNC_NEGO;
2812         dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
2813
2814         reprogram_regs(acb, dcb);
2815 }
2816
2817
2818 static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
2819                 struct ScsiReqBlk *srb)
2820 {
2821         struct DeviceCtlBlk *dcb = srb->dcb;
2822         dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
2823
2824         dcb->sync_period &= ~WIDE_SYNC;
2825         dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
2826         dcb->sync_mode |= WIDE_NEGO_DONE;
2827         srb->state &= ~SRB_DO_WIDE_NEGO;
2828         reprogram_regs(acb, dcb);
2829         if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2830             && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2831                 build_sdtr(acb, dcb, srb);
2832                 DC395x_ENABLE_MSGOUT;
2833                 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2834         }
2835 }
2836
2837 static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2838 {
2839         struct DeviceCtlBlk *dcb = srb->dcb;
2840         u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
2841                    && acb->config & HCC_WIDE_CARD) ? 1 : 0;
2842         dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
2843
2844         if (srb->msgin_buf[3] > wide)
2845                 srb->msgin_buf[3] = wide;
2846         /* Completed */
2847         if (!(srb->state & SRB_DO_WIDE_NEGO)) {
2848                 dprintkl(KERN_DEBUG,
2849                         "msgin_set_wide: Wide nego initiated <%02i>\n",
2850                         dcb->target_id);
2851                 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
2852                 srb->msg_count = 4;
2853                 srb->state |= SRB_DO_WIDE_NEGO;
2854                 DC395x_ENABLE_MSGOUT;
2855         }
2856
2857         dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
2858         if (srb->msgin_buf[3] > 0)
2859                 dcb->sync_period |= WIDE_SYNC;
2860         else
2861                 dcb->sync_period &= ~WIDE_SYNC;
2862         srb->state &= ~SRB_DO_WIDE_NEGO;
2863         /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2864         dprintkdbg(DBG_1,
2865                 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2866                 (8 << srb->msgin_buf[3]), dcb->target_id);
2867         reprogram_regs(acb, dcb);
2868         if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2869             && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2870                 build_sdtr(acb, dcb, srb);
2871                 DC395x_ENABLE_MSGOUT;
2872                 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
2873         }
2874 }
2875
2876
2877 /*
2878  * extended message codes:
2879  *
2880  *      code    description
2881  *
2882  *      02h     Reserved
2883  *      00h     MODIFY DATA  POINTER
2884  *      01h     SYNCHRONOUS DATA TRANSFER REQUEST
2885  *      03h     WIDE DATA TRANSFER REQUEST
2886  *   04h - 7Fh  Reserved
2887  *   80h - FFh  Vendor specific
2888  */
2889 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2890                 u16 *pscsi_status)
2891 {
2892         struct DeviceCtlBlk *dcb = acb->active_dcb;
2893         dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->pid);
2894
2895         srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2896         if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
2897                 /* Now eval the msg */
2898                 switch (srb->msgin_buf[0]) {
2899                 case DISCONNECT:
2900                         srb->state = SRB_DISCONNECT;
2901                         break;
2902
2903                 case SIMPLE_QUEUE_TAG:
2904                 case HEAD_OF_QUEUE_TAG:
2905                 case ORDERED_QUEUE_TAG:
2906                         srb =
2907                             msgin_qtag(acb, dcb,
2908                                               srb->msgin_buf[1]);
2909                         break;
2910
2911                 case MESSAGE_REJECT:
2912                         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2913                                        DO_CLRATN | DO_DATALATCH);
2914                         /* A sync nego message was rejected ! */
2915                         if (srb->state & SRB_DO_SYNC_NEGO) {
2916                                 msgin_set_async(acb, srb);
2917                                 break;
2918                         }
2919                         /* A wide nego message was rejected ! */
2920                         if (srb->state & SRB_DO_WIDE_NEGO) {
2921                                 msgin_set_nowide(acb, srb);
2922                                 break;
2923                         }
2924                         enable_msgout_abort(acb, srb);
2925                         /*srb->state |= SRB_ABORT_SENT */
2926                         break;
2927
2928                 case EXTENDED_MESSAGE:
2929                         /* SDTR */
2930                         if (srb->msgin_buf[1] == 3
2931                             && srb->msgin_buf[2] == EXTENDED_SDTR) {
2932                                 msgin_set_sync(acb, srb);
2933                                 break;
2934                         }
2935                         /* WDTR */
2936                         if (srb->msgin_buf[1] == 2
2937                             && srb->msgin_buf[2] == EXTENDED_WDTR
2938                             && srb->msgin_buf[3] <= 2) { /* sanity check ... */
2939                                 msgin_set_wide(acb, srb);
2940                                 break;
2941                         }
2942                         msgin_reject(acb, srb);
2943                         break;
2944
2945                 case MSG_IGNOREWIDE:
2946                         /* Discard  wide residual */
2947                         dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
2948                         break;
2949
2950                 case COMMAND_COMPLETE:
2951                         /* nothing has to be done */
2952                         break;
2953
2954                 case SAVE_POINTERS:
2955                         /*
2956                          * SAVE POINTER may be ignored as we have the struct
2957                          * ScsiReqBlk* associated with the scsi command.
2958                          */
2959                         dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2960                                 "SAVE POINTER rem=%i Ignore\n",
2961                                 srb->cmd->pid, srb->total_xfer_length);
2962                         break;
2963
2964                 case RESTORE_POINTERS:
2965                         dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
2966                         break;
2967
2968                 case ABORT:
2969                         dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2970                                 "<%02i-%i> ABORT msg\n",
2971                                 srb->cmd->pid, dcb->target_id,
2972                                 dcb->target_lun);
2973                         dcb->flag |= ABORT_DEV_;
2974                         enable_msgout_abort(acb, srb);
2975                         break;
2976
2977                 default:
2978                         /* reject unknown messages */
2979                         if (srb->msgin_buf[0] & IDENTIFY_BASE) {
2980                                 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
2981                                 srb->msg_count = 1;
2982                                 srb->msgout_buf[0] = dcb->identify_msg;
2983                                 DC395x_ENABLE_MSGOUT;
2984                                 srb->state |= SRB_MSGOUT;
2985                                 /*break; */
2986                         }
2987                         msgin_reject(acb, srb);
2988                 }
2989
2990                 /* Clear counter and MsgIn state */
2991                 srb->state &= ~SRB_MSGIN;
2992                 acb->msg_len = 0;
2993         }
2994         *pscsi_status = PH_BUS_FREE;
2995         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important ... you know! */
2996         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2997 }
2998
2999
3000 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3001                 u16 *pscsi_status)
3002 {
3003         dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->pid);
3004         clear_fifo(acb, "msgin_phase1");
3005         DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
3006         if (!(srb->state & SRB_MSGIN)) {
3007                 srb->state &= ~SRB_DISCONNECT;
3008                 srb->state |= SRB_MSGIN;
3009         }
3010         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
3011         /* SCSI command */
3012         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
3013 }
3014
3015
3016 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3017                 u16 *pscsi_status)
3018 {
3019 }
3020
3021
3022 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3023                 u16 *pscsi_status)
3024 {
3025 }
3026
3027
3028 static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
3029 {
3030         struct DeviceCtlBlk *i;
3031
3032         /* set all lun device's  period, offset */
3033         if (dcb->identify_msg & 0x07)
3034                 return;
3035
3036         if (acb->scan_devices) {
3037                 current_sync_offset = dcb->sync_offset;
3038                 return;
3039         }
3040
3041         list_for_each_entry(i, &acb->dcb_list, list)
3042                 if (i->target_id == dcb->target_id) {
3043                         i->sync_period = dcb->sync_period;
3044                         i->sync_offset = dcb->sync_offset;
3045                         i->sync_mode = dcb->sync_mode;
3046                         i->min_nego_period = dcb->min_nego_period;
3047                 }
3048 }
3049
3050
3051 static void disconnect(struct AdapterCtlBlk *acb)
3052 {
3053         struct DeviceCtlBlk *dcb = acb->active_dcb;
3054         struct ScsiReqBlk *srb;
3055
3056         if (!dcb) {
3057                 dprintkl(KERN_ERR, "disconnect: No such device\n");
3058                 udelay(500);
3059                 /* Suspend queue for a while */
3060                 acb->scsi_host->last_reset =
3061                     jiffies + HZ / 2 +
3062                     HZ * acb->eeprom.delay_time;
3063                 clear_fifo(acb, "disconnectEx");
3064                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3065                 return;
3066         }
3067         srb = dcb->active_srb;
3068         acb->active_dcb = NULL;
3069         dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->pid);
3070
3071         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
3072         clear_fifo(acb, "disconnect");
3073         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3074         if (srb->state & SRB_UNEXPECT_RESEL) {
3075                 dprintkl(KERN_ERR,
3076                         "disconnect: Unexpected reselection <%02i-%i>\n",
3077                         dcb->target_id, dcb->target_lun);
3078                 srb->state = 0;
3079                 waiting_process_next(acb);
3080         } else if (srb->state & SRB_ABORT_SENT) {
3081                 dcb->flag &= ~ABORT_DEV_;
3082                 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
3083                 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
3084                 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
3085                 waiting_process_next(acb);
3086         } else {
3087                 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
3088                     || !(srb->
3089                          state & (SRB_DISCONNECT + SRB_COMPLETED))) {
3090                         /*
3091                          * Selection time out 
3092                          * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
3093                          */
3094                         /* Unexp. Disc / Sel Timeout */
3095                         if (srb->state != SRB_START_
3096                             && srb->state != SRB_MSGOUT) {
3097                                 srb->state = SRB_READY;
3098                                 dprintkl(KERN_DEBUG,
3099                                         "disconnect: (pid#%li) Unexpected\n",
3100                                         srb->cmd->pid);
3101                                 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
3102                                 goto disc1;
3103                         } else {
3104                                 /* Normal selection timeout */
3105                                 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
3106                                         "<%02i-%i> SelTO\n", srb->cmd->pid,
3107                                         dcb->target_id, dcb->target_lun);
3108                                 if (srb->retry_count++ > DC395x_MAX_RETRIES
3109                                     || acb->scan_devices) {
3110                                         srb->target_status =
3111                                             SCSI_STAT_SEL_TIMEOUT;
3112                                         goto disc1;
3113                                 }
3114                                 free_tag(dcb, srb);
3115                                 srb_going_to_waiting_move(dcb, srb);
3116                                 dprintkdbg(DBG_KG,
3117                                         "disconnect: (pid#%li) Retry\n",
3118                                         srb->cmd->pid);
3119                                 waiting_set_timer(acb, HZ / 20);
3120                         }
3121                 } else if (srb->state & SRB_DISCONNECT) {
3122                         u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
3123                         /*
3124                          * SRB_DISCONNECT (This is what we expect!)
3125                          */
3126                         if (bval & 0x40) {
3127                                 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
3128                                         " 0x%02x: ACK set! Other controllers?\n",
3129                                         bval);
3130                                 /* It could come from another initiator, therefore don't do much ! */
3131                         } else
3132                                 waiting_process_next(acb);
3133                 } else if (srb->state & SRB_COMPLETED) {
3134                       disc1:
3135                         /*
3136                          ** SRB_COMPLETED
3137                          */
3138                         free_tag(dcb, srb);
3139                         dcb->active_srb = NULL;
3140                         srb->state = SRB_FREE;
3141                         srb_done(acb, dcb, srb);
3142                 }
3143         }
3144 }
3145
3146
3147 static void reselect(struct AdapterCtlBlk *acb)
3148 {
3149         struct DeviceCtlBlk *dcb = acb->active_dcb;
3150         struct ScsiReqBlk *srb = NULL;
3151         u16 rsel_tar_lun_id;
3152         u8 id, lun;
3153         u8 arblostflag = 0;
3154         dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
3155
3156         clear_fifo(acb, "reselect");
3157         /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3158         /* Read Reselected Target ID and LUN */
3159         rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3160         if (dcb) {              /* Arbitration lost but Reselection win */
3161                 srb = dcb->active_srb;
3162                 if (!srb) {
3163                         dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
3164                                 "but active_srb == NULL\n");
3165                         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
3166                         return;
3167                 }
3168                 /* Why the if ? */
3169                 if (!acb->scan_devices) {
3170                         dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
3171                                 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
3172                                 srb->cmd->pid, dcb->target_id,
3173                                 dcb->target_lun, rsel_tar_lun_id,
3174                                 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3175                         arblostflag = 1;
3176                         /*srb->state |= SRB_DISCONNECT; */
3177
3178                         srb->state = SRB_READY;
3179                         free_tag(dcb, srb);
3180                         srb_going_to_waiting_move(dcb, srb);
3181                         waiting_set_timer(acb, HZ / 20);
3182
3183                         /* return; */
3184                 }
3185         }
3186         /* Read Reselected Target Id and LUN */
3187         if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3188                 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
3189                         "Got %i!\n", rsel_tar_lun_id);
3190         id = rsel_tar_lun_id & 0xff;
3191         lun = (rsel_tar_lun_id >> 8) & 7;
3192         dcb = find_dcb(acb, id, lun);
3193         if (!dcb) {
3194                 dprintkl(KERN_ERR, "reselect: From non existent device "
3195                         "<%02i-%i>\n", id, lun);
3196                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
3197                 return;
3198         }
3199         acb->active_dcb = dcb;
3200
3201         if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3202                 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
3203                         "disconnection? <%02i-%i>\n",
3204                         dcb->target_id, dcb->target_lun);
3205
3206         if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
3207                 srb = acb->tmp_srb;
3208                 dcb->active_srb = srb;
3209         } else {
3210                 /* There can be only one! */
3211                 srb = dcb->active_srb;
3212                 if (!srb || !(srb->state & SRB_DISCONNECT)) {
3213                         /*
3214                          * abort command
3215                          */
3216                         dprintkl(KERN_DEBUG,
3217                                 "reselect: w/o disconnected cmds <%02i-%i>\n",
3218                                 dcb->target_id, dcb->target_lun);
3219                         srb = acb->tmp_srb;
3220                         srb->state = SRB_UNEXPECT_RESEL;
3221                         dcb->active_srb = srb;
3222                         enable_msgout_abort(acb, srb);
3223                 } else {
3224                         if (dcb->flag & ABORT_DEV_) {
3225                                 /*srb->state = SRB_ABORT_SENT; */
3226                                 enable_msgout_abort(acb, srb);
3227                         } else
3228                                 srb->state = SRB_DATA_XFER;
3229
3230                 }
3231         }
3232         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
3233
3234         /* Program HA ID, target ID, period and offset */
3235         dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
3236         DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);     /* host   ID */
3237         DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);            /* target ID */
3238         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);            /* offset    */
3239         DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);              /* sync period, wide */
3240         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);              /* it's important for atn stop */
3241         /* SCSI command */
3242         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3243 }
3244
3245
3246 static inline u8 tagq_blacklist(char *name)
3247 {
3248 #ifndef DC395x_NO_TAGQ
3249 #if 0
3250         u8 i;
3251         for (i = 0; i < BADDEVCNT; i++)
3252                 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
3253                         return 1;
3254 #endif
3255         return 0;
3256 #else
3257         return 1;
3258 #endif
3259 }
3260
3261
3262 static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
3263 {
3264         /* Check for SCSI format (ANSI and Response data format) */
3265         if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
3266                 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
3267                     && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
3268                     /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3269                     /* ((dcb->dev_type == TYPE_DISK) 
3270                        || (dcb->dev_type == TYPE_MOD)) && */
3271                     !tagq_blacklist(((char *)ptr) + 8)) {
3272                         if (dcb->max_command == 1)
3273                                 dcb->max_command =
3274                                     dcb->acb->tag_max_num;
3275                         dcb->sync_mode |= EN_TAG_QUEUEING;
3276                         /*dcb->tag_mask = 0; */
3277                 } else
3278                         dcb->max_command = 1;
3279         }
3280 }
3281
3282
3283 static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3284                 struct ScsiInqData *ptr)
3285 {
3286         u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
3287         dcb->dev_type = bval1;
3288         /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3289         disc_tagq_set(dcb, ptr);
3290 }
3291
3292
3293 /* unmap mapped pci regions from SRB */
3294 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3295 {
3296         struct scsi_cmnd *cmd = srb->cmd;
3297         enum dma_data_direction dir = cmd->sc_data_direction;
3298         if (cmd->use_sg && dir != PCI_DMA_NONE) {
3299                 /* unmap DC395x SG list */
3300                 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
3301                         srb->sg_bus_addr, SEGMENTX_LEN);
3302                 pci_unmap_single(acb->dev, srb->sg_bus_addr,
3303                                  SEGMENTX_LEN,
3304                                  PCI_DMA_TODEVICE);
3305                 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3306                         cmd->use_sg, cmd->request_buffer);
3307                 /* unmap the sg segments */
3308                 pci_unmap_sg(acb->dev,
3309                              (struct scatterlist *)cmd->request_buffer,
3310                              cmd->use_sg, dir);
3311         } else if (cmd->request_buffer && dir != PCI_DMA_NONE) {
3312                 dprintkdbg(DBG_SG, "pci_unmap_srb: buffer=%08x(%05x)\n",
3313                         srb->segment_x[0].address, cmd->request_bufflen);
3314                 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3315                                  cmd->request_bufflen, dir);
3316         }
3317 }
3318
3319
3320 /* unmap mapped pci sense buffer from SRB */
3321 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3322                 struct ScsiReqBlk *srb)
3323 {
3324         if (!(srb->flag & AUTO_REQSENSE))
3325                 return;
3326         /* Unmap sense buffer */
3327         dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
3328                srb->segment_x[0].address);
3329         pci_unmap_single(acb->dev, srb->segment_x[0].address,
3330                          srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
3331         /* Restore SG stuff */
3332         srb->total_xfer_length = srb->xferred;
3333         srb->segment_x[0].address =
3334             srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
3335         srb->segment_x[0].length =
3336             srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
3337 }
3338
3339
3340 /*
3341  * Complete execution of a SCSI command
3342  * Signal completion to the generic SCSI driver  
3343  */
3344 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3345                 struct ScsiReqBlk *srb)
3346 {
3347         u8 tempcnt, status;
3348         struct scsi_cmnd *cmd = srb->cmd;
3349         enum dma_data_direction dir = cmd->sc_data_direction;
3350         int ckc_only = 1;
3351
3352         dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->pid,
3353                 srb->cmd->device->id, srb->cmd->device->lun);
3354         dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
3355                 srb, cmd->use_sg, srb->sg_index, srb->sg_count,
3356                 cmd->request_buffer);
3357         status = srb->target_status;
3358         if (srb->flag & AUTO_REQSENSE) {
3359                 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
3360                 pci_unmap_srb_sense(acb, srb);
3361                 /*
3362                  ** target status..........................
3363                  */
3364                 srb->flag &= ~AUTO_REQSENSE;
3365                 srb->adapter_status = 0;
3366                 srb->target_status = CHECK_CONDITION << 1;
3367                 if (debug_enabled(DBG_1)) {
3368                         switch (cmd->sense_buffer[2] & 0x0f) {
3369                         case NOT_READY:
3370                                 dprintkl(KERN_DEBUG,
3371                                      "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3372                                      cmd->cmnd[0], dcb->target_id,
3373                                      dcb->target_lun, status, acb->scan_devices);
3374                                 break;
3375                         case UNIT_ATTENTION:
3376                                 dprintkl(KERN_DEBUG,
3377                                      "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3378                                      cmd->cmnd[0], dcb->target_id,
3379                                      dcb->target_lun, status, acb->scan_devices);
3380                                 break;
3381                         case ILLEGAL_REQUEST:
3382                                 dprintkl(KERN_DEBUG,
3383                                      "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3384                                      cmd->cmnd[0], dcb->target_id,
3385                                      dcb->target_lun, status, acb->scan_devices);
3386                                 break;
3387                         case MEDIUM_ERROR:
3388                                 dprintkl(KERN_DEBUG,
3389                                      "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3390                                      cmd->cmnd[0], dcb->target_id,
3391                                      dcb->target_lun, status, acb->scan_devices);
3392                                 break;
3393                         case HARDWARE_ERROR:
3394                                 dprintkl(KERN_DEBUG,
3395                                      "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3396                                      cmd->cmnd[0], dcb->target_id,
3397                                      dcb->target_lun, status, acb->scan_devices);
3398                                 break;
3399                         }
3400                         if (cmd->sense_buffer[7] >= 6)
3401                                 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3402                                         "(0x%08x 0x%08x)\n",
3403                                         cmd->sense_buffer[2], cmd->sense_buffer[12],
3404                                         cmd->sense_buffer[13],
3405                                         *((unsigned int *)(cmd->sense_buffer + 3)),
3406                                         *((unsigned int *)(cmd->sense_buffer + 8)));
3407                         else
3408                                 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3409                                         cmd->sense_buffer[2],
3410                                         *((unsigned int *)(cmd->sense_buffer + 3)));
3411                 }
3412
3413                 if (status == (CHECK_CONDITION << 1)) {
3414                         cmd->result = DID_BAD_TARGET << 16;
3415                         goto ckc_e;
3416                 }
3417                 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
3418
3419                 if (srb->total_xfer_length
3420                     && srb->total_xfer_length >= cmd->underflow)
3421                         cmd->result =
3422                             MK_RES_LNX(DRIVER_SENSE, DID_OK,
3423                                        srb->end_message, CHECK_CONDITION);
3424                 /*SET_RES_DID(cmd->result,DID_OK) */
3425                 else
3426                         cmd->result =
3427                             MK_RES_LNX(DRIVER_SENSE, DID_OK,
3428                                        srb->end_message, CHECK_CONDITION);
3429
3430                 goto ckc_e;
3431         }
3432
3433 /*************************************************************/
3434         if (status) {
3435                 /*
3436                  * target status..........................
3437                  */
3438                 if (status_byte(status) == CHECK_CONDITION) {
3439                         request_sense(acb, dcb, srb);
3440                         return;
3441                 } else if (status_byte(status) == QUEUE_FULL) {
3442                         tempcnt = (u8)list_size(&dcb->srb_going_list);
3443                         dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3444                              dcb->target_id, dcb->target_lun, tempcnt);
3445                         if (tempcnt > 1)
3446                                 tempcnt--;
3447                         dcb->max_command = tempcnt;
3448                         free_tag(dcb, srb);
3449                         srb_going_to_waiting_move(dcb, srb);
3450                         waiting_set_timer(acb, HZ / 20);
3451                         srb->adapter_status = 0;
3452                         srb->target_status = 0;
3453                         return;
3454                 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
3455                         srb->adapter_status = H_SEL_TIMEOUT;
3456                         srb->target_status = 0;
3457                         cmd->result = DID_NO_CONNECT << 16;
3458                 } else {
3459                         srb->adapter_status = 0;
3460                         SET_RES_DID(cmd->result, DID_ERROR);
3461                         SET_RES_MSG(cmd->result, srb->end_message);
3462                         SET_RES_TARGET(cmd->result, status);
3463
3464                 }
3465         } else {
3466                 /*
3467                  ** process initiator status..........................
3468                  */
3469                 status = srb->adapter_status;
3470                 if (status & H_OVER_UNDER_RUN) {
3471                         srb->target_status = 0;
3472                         SET_RES_DID(cmd->result, DID_OK);
3473                         SET_RES_MSG(cmd->result, srb->end_message);
3474                 } else if (srb->status & PARITY_ERROR) {
3475                         SET_RES_DID(cmd->result, DID_PARITY);
3476                         SET_RES_MSG(cmd->result, srb->end_message);
3477                 } else {        /* No error */
3478
3479                         srb->adapter_status = 0;
3480                         srb->target_status = 0;
3481                         SET_RES_DID(cmd->result, DID_OK);
3482                 }
3483         }
3484
3485         if (dir != PCI_DMA_NONE) {
3486                 if (cmd->use_sg)
3487                         pci_dma_sync_sg_for_cpu(acb->dev,
3488                                         (struct scatterlist *)cmd->
3489                                         request_buffer, cmd->use_sg, dir);
3490                 else if (cmd->request_buffer)
3491                         pci_dma_sync_single_for_cpu(acb->dev,
3492                                             srb->segment_x[0].address,
3493                                             cmd->request_bufflen, dir);
3494         }
3495         ckc_only = 0;
3496 /* Check Error Conditions */
3497       ckc_e:
3498
3499         if (cmd->cmnd[0] == INQUIRY) {
3500                 unsigned char *base = NULL;
3501                 struct ScsiInqData *ptr;
3502                 unsigned long flags = 0;
3503
3504                 if (cmd->use_sg) {
3505                         struct scatterlist* sg = (struct scatterlist *)cmd->request_buffer;
3506                         size_t offset = 0, len = sizeof(struct ScsiInqData);
3507
3508                         local_irq_save(flags);
3509                         base = scsi_kmap_atomic_sg(sg, cmd->use_sg, &offset, &len);
3510                         ptr = (struct ScsiInqData *)(base + offset);
3511                 } else
3512                         ptr = (struct ScsiInqData *)(cmd->request_buffer);
3513
3514                 if (!ckc_only && (cmd->result & RES_DID) == 0
3515                     && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8
3516                     && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
3517                         dcb->inquiry7 = ptr->Flags;
3518
3519         /*if( srb->cmd->cmnd[0] == INQUIRY && */
3520         /*  (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3521                 if ((cmd->result == (DID_OK << 16)
3522                      || status_byte(cmd->result) &
3523                      CHECK_CONDITION)) {
3524                         if (!dcb->init_tcq_flag) {
3525                                 add_dev(acb, dcb, ptr);
3526                                 dcb->init_tcq_flag = 1;
3527                         }
3528                 }
3529
3530                 if (cmd->use_sg) {
3531                         scsi_kunmap_atomic_sg(base);
3532                         local_irq_restore(flags);
3533                 }
3534         }
3535
3536         /* Here is the info for Doug Gilbert's sg3 ... */
3537         cmd->resid = srb->total_xfer_length;
3538         /* This may be interpreted by sb. or not ... */
3539         cmd->SCp.this_residual = srb->total_xfer_length;
3540         cmd->SCp.buffers_residual = 0;
3541         if (debug_enabled(DBG_KG)) {
3542                 if (srb->total_xfer_length)
3543                         dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
3544                                 "cmnd=0x%02x Missed %i bytes\n",
3545                                 cmd->pid, cmd->device->id, cmd->device->lun,
3546                                 cmd->cmnd[0], srb->total_xfer_length);
3547         }
3548
3549         srb_going_remove(dcb, srb);
3550         /* Add to free list */
3551         if (srb == acb->tmp_srb)
3552                 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3553         else {
3554                 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
3555                         cmd->pid, cmd->result);
3556                 srb_free_insert(acb, srb);
3557         }
3558         pci_unmap_srb(acb, srb);
3559
3560         cmd->scsi_done(cmd);
3561         waiting_process_next(acb);
3562 }
3563
3564
3565 /* abort all cmds in our queues */
3566 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
3567                 struct scsi_cmnd *cmd, u8 force)
3568 {
3569         struct DeviceCtlBlk *dcb;
3570         dprintkl(KERN_INFO, "doing_srb_done: pids ");
3571
3572         list_for_each_entry(dcb, &acb->dcb_list, list) {
3573                 struct ScsiReqBlk *srb;
3574                 struct ScsiReqBlk *tmp;
3575                 struct scsi_cmnd *p;
3576
3577                 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
3578                         enum dma_data_direction dir;
3579                         int result;
3580
3581                         p = srb->cmd;
3582                         dir = p->sc_data_direction;
3583                         result = MK_RES(0, did_flag, 0, 0);
3584                         printk("G:%li(%02i-%i) ", p->pid,
3585                                p->device->id, p->device->lun);
3586                         srb_going_remove(dcb, srb);
3587                         free_tag(dcb, srb);
3588                         srb_free_insert(acb, srb);
3589                         p->result = result;
3590                         pci_unmap_srb_sense(acb, srb);
3591                         pci_unmap_srb(acb, srb);
3592                         if (force) {
3593                                 /* For new EH, we normally don't need to give commands back,
3594                                  * as they all complete or all time out */
3595                                 p->scsi_done(p);
3596                         }
3597                 }
3598                 if (!list_empty(&dcb->srb_going_list))
3599                         dprintkl(KERN_DEBUG, 
3600                                "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3601                                dcb->target_id, dcb->target_lun);
3602                 if (dcb->tag_mask)
3603                         dprintkl(KERN_DEBUG,
3604                                "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3605                                dcb->target_id, dcb->target_lun,
3606                                dcb->tag_mask);
3607
3608                 /* Waiting queue */
3609                 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
3610                         int result;
3611                         p = srb->cmd;
3612
3613                         result = MK_RES(0, did_flag, 0, 0);
3614                         printk("W:%li<%02i-%i>", p->pid, p->device->id,
3615                                p->device->lun);
3616                         srb_waiting_remove(dcb, srb);
3617                         srb_free_insert(acb, srb);
3618                         p->result = result;
3619                         pci_unmap_srb_sense(acb, srb);
3620                         pci_unmap_srb(acb, srb);
3621                         if (force) {
3622                                 /* For new EH, we normally don't need to give commands back,
3623                                  * as they all complete or all time out */
3624                                 cmd->scsi_done(cmd);
3625                         }
3626                 }
3627                 if (!list_empty(&dcb->srb_waiting_list))
3628                         dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
3629                              list_size(&dcb->srb_waiting_list), dcb->target_id,
3630                              dcb->target_lun);
3631                 dcb->flag &= ~ABORT_DEV_;
3632         }
3633         printk("\n");
3634 }
3635
3636
3637 static void reset_scsi_bus(struct AdapterCtlBlk *acb)
3638 {
3639         dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
3640         acb->acb_flag |= RESET_DEV;     /* RESET_DETECT, RESET_DONE, RESET_DEV */
3641         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
3642
3643         while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
3644                 /* nothing */;
3645 }
3646
3647
3648 static void set_basic_config(struct AdapterCtlBlk *acb)
3649 {
3650         u8 bval;
3651         u16 wval;
3652         DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
3653         if (acb->config & HCC_PARITY)
3654                 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
3655         else
3656                 bval = PHASELATCH | INITIATOR | BLOCKRST;
3657
3658         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
3659
3660         /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3661         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03);       /* was 0x13: default */
3662         /* program Host ID                  */
3663         DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
3664         /* set ansynchronous transfer       */
3665         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
3666         /* Turn LED control off */
3667         wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
3668         DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
3669         /* DMA config          */
3670         wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
3671         wval |=
3672             DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
3673         DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
3674         /* Clear pending interrupt status */
3675         DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
3676         /* Enable SCSI interrupt    */
3677         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
3678         DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
3679                       /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3680                       );
3681 }
3682
3683
3684 static void scsi_reset_detect(struct AdapterCtlBlk *acb)
3685 {
3686         dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
3687         /* delay half a second */
3688         if (timer_pending(&acb->waiting_timer))
3689                 del_timer(&acb->waiting_timer);
3690
3691         DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
3692         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
3693         /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3694         udelay(500);
3695         /* Maybe we locked up the bus? Then lets wait even longer ... */
3696         acb->scsi_host->last_reset =
3697             jiffies + 5 * HZ / 2 +
3698             HZ * acb->eeprom.delay_time;
3699
3700         clear_fifo(acb, "scsi_reset_detect");
3701         set_basic_config(acb);
3702         /*1.25 */
3703         /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3704
3705         if (acb->acb_flag & RESET_DEV) {        /* RESET_DETECT, RESET_DONE, RESET_DEV */
3706                 acb->acb_flag |= RESET_DONE;
3707         } else {
3708                 acb->acb_flag |= RESET_DETECT;
3709                 reset_dev_param(acb);
3710                 doing_srb_done(acb, DID_RESET, NULL, 1);
3711                 /*DC395x_RecoverSRB( acb ); */
3712                 acb->active_dcb = NULL;
3713                 acb->acb_flag = 0;
3714                 waiting_process_next(acb);
3715         }
3716 }
3717
3718
3719 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3720                 struct ScsiReqBlk *srb)
3721 {
3722         struct scsi_cmnd *cmd = srb->cmd;
3723         dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
3724                 cmd->pid, cmd->device->id, cmd->device->lun);
3725
3726         srb->flag |= AUTO_REQSENSE;
3727         srb->adapter_status = 0;
3728         srb->target_status = 0;
3729
3730         /* KG: Can this prevent crap sense data ? */
3731         memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3732
3733         /* Save some data */
3734         srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
3735             srb->segment_x[0].address;
3736         srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
3737             srb->segment_x[0].length;
3738         srb->xferred = srb->total_xfer_length;
3739         /* srb->segment_x : a one entry of S/G list table */
3740         srb->total_xfer_length = sizeof(cmd->sense_buffer);
3741         srb->segment_x[0].length = sizeof(cmd->sense_buffer);
3742         /* Map sense buffer */
3743         srb->segment_x[0].address =
3744             pci_map_single(acb->dev, cmd->sense_buffer,
3745                            sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE);
3746         dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
3747                cmd->sense_buffer, srb->segment_x[0].address,
3748                sizeof(cmd->sense_buffer));
3749         srb->sg_count = 1;
3750         srb->sg_index = 0;
3751
3752         if (start_scsi(acb, dcb, srb)) {        /* Should only happen, if sb. else grabs the bus */
3753                 dprintkl(KERN_DEBUG,
3754                         "request_sense: (pid#%li) failed <%02i-%i>\n",
3755                         srb->cmd->pid, dcb->target_id, dcb->target_lun);
3756                 srb_going_to_waiting_move(dcb, srb);
3757                 waiting_set_timer(acb, HZ / 100);
3758         }
3759 }
3760
3761
3762 /**
3763  * device_alloc - Allocate a new device instance. This create the
3764  * devices instance and sets up all the data items. The adapter
3765  * instance is required to obtain confiuration information for this
3766  * device. This does *not* add this device to the adapters device
3767  * list.
3768  *
3769  * @acb: The adapter to obtain configuration information from.
3770  * @target: The target for the new device.
3771  * @lun: The lun for the new device.
3772  *
3773  * Return the new device if successful or NULL on failure.
3774  **/
3775 static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
3776                 u8 target, u8 lun)
3777 {
3778         struct NvRamType *eeprom = &acb->eeprom;
3779         u8 period_index = eeprom->target[target].period & 0x07;
3780         struct DeviceCtlBlk *dcb;
3781
3782         dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
3783         dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
3784         if (!dcb)
3785                 return NULL;
3786         dcb->acb = NULL;
3787         INIT_LIST_HEAD(&dcb->srb_going_list);
3788         INIT_LIST_HEAD(&dcb->srb_waiting_list);
3789         dcb->active_srb = NULL;
3790         dcb->tag_mask = 0;
3791         dcb->max_command = 1;
3792         dcb->target_id = target;
3793         dcb->target_lun = lun;
3794 #ifndef DC395x_NO_DISCONNECT
3795         dcb->identify_msg =
3796             IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
3797 #else
3798         dcb->identify_msg = IDENTIFY(0, lun);
3799 #endif
3800         dcb->dev_mode = eeprom->target[target].cfg0;
3801         dcb->inquiry7 = 0;
3802         dcb->sync_mode = 0;
3803         dcb->min_nego_period = clock_period[period_index];
3804         dcb->sync_period = 0;
3805         dcb->sync_offset = 0;
3806         dcb->flag = 0;
3807
3808 #ifndef DC395x_NO_WIDE
3809         if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
3810             && (acb->config & HCC_WIDE_CARD))
3811                 dcb->sync_mode |= WIDE_NEGO_ENABLE;
3812 #endif
3813 #ifndef DC395x_NO_SYNC
3814         if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
3815                 if (!(lun) || current_sync_offset)
3816                         dcb->sync_mode |= SYNC_NEGO_ENABLE;
3817 #endif
3818         if (dcb->target_lun != 0) {
3819                 /* Copy settings */
3820                 struct DeviceCtlBlk *p;
3821                 list_for_each_entry(p, &acb->dcb_list, list)
3822                         if (p->target_id == dcb->target_id)
3823                                 break;
3824                 dprintkdbg(DBG_1, 
3825                        "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3826                        dcb->target_id, dcb->target_lun,
3827                        p->target_id, p->target_lun);
3828                 dcb->sync_mode = p->sync_mode;
3829                 dcb->sync_period = p->sync_period;
3830                 dcb->min_nego_period = p->min_nego_period;
3831                 dcb->sync_offset = p->sync_offset;
3832                 dcb->inquiry7 = p->inquiry7;
3833         }
3834         return dcb;
3835 }
3836
3837
3838 /**
3839  * adapter_add_device - Adds the device instance to the adaptor instance.
3840  *
3841  * @acb: The adapter device to be updated
3842  * @dcb: A newly created and intialised device instance to add.
3843  **/
3844 static void adapter_add_device(struct AdapterCtlBlk *acb,
3845                 struct DeviceCtlBlk *dcb)
3846 {
3847         /* backpointer to adapter */
3848         dcb->acb = acb;
3849         
3850         /* set run_robin to this device if it is currently empty */
3851         if (list_empty(&acb->dcb_list))
3852                 acb->dcb_run_robin = dcb;
3853
3854         /* add device to list */
3855         list_add_tail(&dcb->list, &acb->dcb_list);
3856
3857         /* update device maps */
3858         acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
3859         acb->children[dcb->target_id][dcb->target_lun] = dcb;
3860 }
3861
3862
3863 /**
3864  * adapter_remove_device - Removes the device instance from the adaptor
3865  * instance. The device instance is not check in any way or freed by this. 
3866  * The caller is expected to take care of that. This will simply remove the
3867  * device from the adapters data strcutures.
3868  *
3869  * @acb: The adapter device to be updated
3870  * @dcb: A device that has previously been added to the adapter.
3871  **/
3872 static void adapter_remove_device(struct AdapterCtlBlk *acb,
3873                 struct DeviceCtlBlk *dcb)
3874 {
3875         struct DeviceCtlBlk *i;
3876         struct DeviceCtlBlk *tmp;
3877         dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
3878                 dcb->target_id, dcb->target_lun);
3879
3880         /* fix up any pointers to this device that we have in the adapter */
3881         if (acb->active_dcb == dcb)
3882                 acb->active_dcb = NULL;
3883         if (acb->dcb_run_robin == dcb)
3884                 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
3885
3886         /* unlink from list */
3887         list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
3888                 if (dcb == i) {
3889                         list_del(&i->list);
3890                         break;
3891                 }
3892
3893         /* clear map and children */    
3894         acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
3895         acb->children[dcb->target_id][dcb->target_lun] = NULL;
3896         dcb->acb = NULL;
3897 }
3898
3899
3900 /**
3901  * adapter_remove_and_free_device - Removes a single device from the adapter
3902  * and then frees the device information.
3903  *
3904  * @acb: The adapter device to be updated
3905  * @dcb: A device that has previously been added to the adapter.
3906  */
3907 static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
3908                 struct DeviceCtlBlk *dcb)
3909 {
3910         if (list_size(&dcb->srb_going_list) > 1) {
3911                 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
3912                            "Won't remove because of %i active requests.\n",
3913                            dcb->target_id, dcb->target_lun,
3914                            list_size(&dcb->srb_going_list));
3915                 return;
3916         }
3917         adapter_remove_device(acb, dcb);
3918         kfree(dcb);
3919 }
3920
3921
3922 /**
3923  * adapter_remove_and_free_all_devices - Removes and frees all of the
3924  * devices associated with the specified adapter.
3925  *
3926  * @acb: The adapter from which all devices should be removed.
3927  **/
3928 static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
3929 {
3930         struct DeviceCtlBlk *dcb;
3931         struct DeviceCtlBlk *tmp;
3932         dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
3933                    list_size(&acb->dcb_list));
3934
3935         list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
3936                 adapter_remove_and_free_device(acb, dcb);
3937 }
3938
3939
3940 /**
3941  * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3942  * scsi device that we need to deal with. We allocate a new device and then
3943  * insert that device into the adapters device list.
3944  *
3945  * @scsi_device: The new scsi device that we need to handle.
3946  **/
3947 static int dc395x_slave_alloc(struct scsi_device *scsi_device)
3948 {
3949         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3950         struct DeviceCtlBlk *dcb;
3951
3952         dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
3953         if (!dcb)
3954                 return -ENOMEM;
3955         adapter_add_device(acb, dcb);
3956
3957         return 0;
3958 }
3959
3960
3961 /**
3962  * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3963  * device that is going away.
3964  *
3965  * @scsi_device: The new scsi device that we need to handle.
3966  **/
3967 static void dc395x_slave_destroy(struct scsi_device *scsi_device)
3968 {
3969         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3970         struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
3971         if (dcb)
3972                 adapter_remove_and_free_device(acb, dcb);
3973 }
3974
3975
3976
3977
3978 /**
3979  * trms1040_wait_30us: wait for 30 us
3980  *
3981  * Waits for 30us (using the chip by the looks of it..)
3982  *
3983  * @io_port: base I/O address
3984  **/
3985 static void __devinit trms1040_wait_30us(unsigned long io_port)
3986 {
3987         /* ScsiPortStallExecution(30); wait 30 us */
3988         outb(5, io_port + TRM_S1040_GEN_TIMER);
3989         while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
3990                 /* nothing */ ;
3991 }
3992
3993
3994 /**
3995  * trms1040_write_cmd - write the secified command and address to
3996  * chip
3997  *
3998  * @io_port:    base I/O address
3999  * @cmd:        SB + op code (command) to send
4000  * @addr:       address to send
4001  **/
4002 static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
4003 {
4004         int i;
4005         u8 send_data;
4006
4007         /* program SB + OP code */
4008         for (i = 0; i < 3; i++, cmd <<= 1) {
4009                 send_data = NVR_SELECT;
4010                 if (cmd & 0x04) /* Start from bit 2 */
4011                         send_data |= NVR_BITOUT;
4012
4013                 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4014                 trms1040_wait_30us(io_port);
4015                 outb((send_data | NVR_CLOCK),
4016                      io_port + TRM_S1040_GEN_NVRAM);
4017                 trms1040_wait_30us(io_port);
4018         }
4019
4020         /* send address */
4021         for (i = 0; i < 7; i++, addr <<= 1) {
4022                 send_data = NVR_SELECT;
4023                 if (addr & 0x40)        /* Start from bit 6 */
4024                         send_data |= NVR_BITOUT;
4025
4026                 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4027                 trms1040_wait_30us(io_port);
4028                 outb((send_data | NVR_CLOCK),
4029                      io_port + TRM_S1040_GEN_NVRAM);
4030                 trms1040_wait_30us(io_port);
4031         }
4032         outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4033         trms1040_wait_30us(io_port);
4034 }
4035
4036
4037 /**
4038  * trms1040_set_data - store a single byte in the eeprom
4039  *
4040  * Called from write all to write a single byte into the SSEEPROM
4041  * Which is done one bit at a time.
4042  *
4043  * @io_port:    base I/O address
4044  * @addr:       offset into EEPROM
4045  * @byte:       bytes to write
4046  **/
4047 static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
4048 {
4049         int i;
4050         u8 send_data;
4051
4052         /* Send write command & address */
4053         trms1040_write_cmd(io_port, 0x05, addr);
4054
4055         /* Write data */
4056         for (i = 0; i < 8; i++, byte <<= 1) {
4057                 send_data = NVR_SELECT;
4058                 if (byte & 0x80)        /* Start from bit 7 */
4059                         send_data |= NVR_BITOUT;
4060
4061                 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4062                 trms1040_wait_30us(io_port);
4063                 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4064                 trms1040_wait_30us(io_port);
4065         }
4066         outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4067         trms1040_wait_30us(io_port);
4068
4069         /* Disable chip select */
4070         outb(0, io_port + TRM_S1040_GEN_NVRAM);
4071         trms1040_wait_30us(io_port);
4072
4073         outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4074         trms1040_wait_30us(io_port);
4075
4076         /* Wait for write ready */
4077         while (1) {
4078                 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4079                 trms1040_wait_30us(io_port);
4080
4081                 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4082                 trms1040_wait_30us(io_port);
4083
4084                 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
4085                         break;
4086         }
4087
4088         /*  Disable chip select */
4089         outb(0, io_port + TRM_S1040_GEN_NVRAM);
4090 }
4091
4092
4093 /**
4094  * trms1040_write_all - write 128 bytes to the eeprom
4095  *
4096  * Write the supplied 128 bytes to the chips SEEPROM
4097  *
4098  * @eeprom:     the data to write
4099  * @io_port:    the base io port
4100  **/
4101 static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
4102 {
4103         u8 *b_eeprom = (u8 *)eeprom;
4104         u8 addr;
4105
4106         /* Enable SEEPROM */
4107         outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4108              io_port + TRM_S1040_GEN_CONTROL);
4109
4110         /* write enable */
4111         trms1040_write_cmd(io_port, 0x04, 0xFF);
4112         outb(0, io_port + TRM_S1040_GEN_NVRAM);
4113         trms1040_wait_30us(io_port);
4114
4115         /* write */
4116         for (addr = 0; addr < 128; addr++, b_eeprom++)
4117                 trms1040_set_data(io_port, addr, *b_eeprom);
4118
4119         /* write disable */
4120         trms1040_write_cmd(io_port, 0x04, 0x00);
4121         outb(0, io_port + TRM_S1040_GEN_NVRAM);
4122         trms1040_wait_30us(io_port);
4123
4124         /* Disable SEEPROM */
4125         outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4126              io_port + TRM_S1040_GEN_CONTROL);
4127 }
4128
4129
4130 /**
4131  * trms1040_get_data - get a single byte from the eeprom
4132  *
4133  * Called from read all to read a single byte into the SSEEPROM
4134  * Which is done one bit at a time.
4135  *
4136  * @io_port:    base I/O address
4137  * @addr:       offset into SEEPROM
4138  *
4139  * Returns the the byte read.
4140  **/
4141 static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
4142 {
4143         int i;
4144         u8 read_byte;
4145         u8 result = 0;
4146
4147         /* Send read command & address */
4148         trms1040_write_cmd(io_port, 0x06, addr);
4149
4150         /* read data */
4151         for (i = 0; i < 8; i++) {
4152                 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4153                 trms1040_wait_30us(io_port);
4154                 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4155
4156                 /* Get data bit while falling edge */
4157                 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
4158                 result <<= 1;
4159                 if (read_byte & NVR_BITIN)
4160                         result |= 1;
4161
4162                 trms1040_wait_30us(io_port);
4163         }
4164
4165         /* Disable chip select */
4166         outb(0, io_port + TRM_S1040_GEN_NVRAM);
4167         return result;
4168 }
4169
4170
4171 /**
4172  * trms1040_read_all - read all bytes from the eeprom
4173  *
4174  * Read the 128 bytes from the SEEPROM.
4175  *
4176  * @eeprom:     where to store the data
4177  * @io_port:    the base io port
4178  **/
4179 static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
4180 {
4181         u8 *b_eeprom = (u8 *)eeprom;
4182         u8 addr;
4183
4184         /* Enable SEEPROM */
4185         outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4186              io_port + TRM_S1040_GEN_CONTROL);
4187
4188         /* read details */
4189         for (addr = 0; addr < 128; addr++, b_eeprom++)
4190                 *b_eeprom = trms1040_get_data(io_port, addr);
4191
4192         /* Disable SEEPROM */
4193         outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4194              io_port + TRM_S1040_GEN_CONTROL);
4195 }
4196
4197
4198
4199 /**
4200  * check_eeprom - get and check contents of the eeprom
4201  *
4202  * Read seeprom 128 bytes into the memory provider in eeprom.
4203  * Checks the checksum and if it's not correct it uses a set of default
4204  * values.
4205  *
4206  * @eeprom:     caller allocated strcuture to read the eeprom data into
4207  * @io_port:    io port to read from
4208  **/
4209 static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
4210 {
4211         u16 *w_eeprom = (u16 *)eeprom;
4212         u16 w_addr;
4213         u16 cksum;
4214         u32 d_addr;
4215         u32 *d_eeprom;
4216
4217         trms1040_read_all(eeprom, io_port);     /* read eeprom */
4218
4219         cksum = 0;
4220         for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
4221              w_addr++, w_eeprom++)
4222                 cksum += *w_eeprom;
4223         if (cksum != 0x1234) {
4224                 /*
4225                  * Checksum is wrong.
4226                  * Load a set of defaults into the eeprom buffer
4227                  */
4228                 dprintkl(KERN_WARNING,
4229                         "EEProm checksum error: using default values and options.\n");
4230                 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4231                 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4232                 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4233                 eeprom->sub_sys_id[1] =
4234                     (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4235                 eeprom->sub_class = 0x00;
4236                 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4237                 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4238                 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4239                 eeprom->device_id[1] =
4240                     (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4241                 eeprom->reserved = 0x00;
4242
4243                 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
4244                      d_addr < 16; d_addr++, d_eeprom++)
4245                         *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
4246
4247                 *d_eeprom++ = 0x04000F07;       /* max_tag,delay_time,channel_cfg,scsi_id */
4248                 *d_eeprom++ = 0x00000015;       /* reserved1,boot_lun,boot_target,reserved0 */
4249                 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
4250                         *d_eeprom = 0x00;
4251
4252                 /* Now load defaults (maybe set by boot/module params) */
4253                 set_safe_settings();
4254                 fix_settings();
4255                 eeprom_override(eeprom);
4256
4257                 eeprom->cksum = 0x00;
4258                 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
4259                      w_addr < 63; w_addr++, w_eeprom++)
4260                         cksum += *w_eeprom;
4261
4262                 *w_eeprom = 0x1234 - cksum;
4263                 trms1040_write_all(eeprom, io_port);
4264                 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
4265         } else {
4266                 set_safe_settings();
4267                 eeprom_index_to_delay(eeprom);
4268                 eeprom_override(eeprom);
4269         }
4270 }
4271
4272
4273 /**
4274  * print_eeprom_settings - output the eeprom settings
4275  * to the kernel log so people can see what they were.
4276  *
4277  * @eeprom: The eeprom data strucutre to show details for.
4278  **/
4279 static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
4280 {
4281         dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4282                 eeprom->scsi_id,
4283                 eeprom->target[0].period,
4284                 clock_speed[eeprom->target[0].period] / 10,
4285                 clock_speed[eeprom->target[0].period] % 10,
4286                 eeprom->target[0].cfg0);
4287         dprintkl(KERN_INFO, "               AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4288                 eeprom->channel_cfg, eeprom->max_tag,
4289                 1 << eeprom->max_tag, eeprom->delay_time);
4290 }
4291
4292
4293 /* Free SG tables */
4294 static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
4295 {
4296         int i;
4297         const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4298
4299         for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
4300                 kfree(acb->srb_array[i].segment_x);
4301 }
4302
4303
4304 /*
4305  * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4306  * should never cross a page boundary */
4307 static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
4308 {
4309         const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
4310                                     *SEGMENTX_LEN;
4311         int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
4312         const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4313         int srb_idx = 0;
4314         unsigned i = 0;
4315         struct SGentry *ptr;
4316
4317         for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
4318                 acb->srb_array[i].segment_x = NULL;
4319
4320         dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
4321         while (pages--) {
4322                 ptr = kmalloc(PAGE_SIZE, GFP_KERNEL);
4323                 if (!ptr) {
4324                         adapter_sg_tables_free(acb);
4325                         return 1;
4326                 }
4327                 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
4328                         PAGE_SIZE, ptr, srb_idx);
4329                 i = 0;
4330                 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4331                         acb->srb_array[srb_idx++].segment_x =
4332                             ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4333         }
4334         if (i < srbs_per_page)
4335                 acb->srb.segment_x =
4336                     ptr + (i * DC395x_MAX_SG_LISTENTRY);
4337         else
4338                 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
4339         return 0;
4340 }
4341
4342
4343
4344 /**
4345  * adapter_print_config - print adapter connection and termination
4346  * config
4347  *
4348  * The io port in the adapter needs to have been set before calling
4349  * this function.
4350  *
4351  * @acb: The adapter to print the information for.
4352  **/
4353 static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
4354 {
4355         u8 bval;
4356
4357         bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
4358         dprintkl(KERN_INFO, "%sConnectors: ",
4359                 ((bval & WIDESCSI) ? "(Wide) " : ""));
4360         if (!(bval & CON5068))
4361                 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
4362         if (!(bval & CON68))
4363                 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
4364         if (!(bval & CON50))
4365                 printk("int50 ");
4366         if ((bval & (CON5068 | CON50 | CON68)) ==
4367             0 /*(CON5068 | CON50 | CON68) */ )
4368                 printk(" Oops! (All 3?) ");
4369         bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
4370         printk(" Termination: ");
4371         if (bval & DIS_TERM)
4372                 printk("Disabled\n");
4373         else {
4374                 if (bval & AUTOTERM)
4375                         printk("Auto ");
4376                 if (bval & LOW8TERM)
4377                         printk("Low ");
4378                 if (bval & UP8TERM)
4379                         printk("High ");
4380                 printk("\n");
4381         }
4382 }
4383
4384
4385 /**
4386  * adapter_init_params - Initialize the various parameters in the
4387  * adapter structure. Note that the pointer to the scsi_host is set
4388  * early (when this instance is created) and the io_port and irq
4389  * values are set later after they have been reserved. This just gets
4390  * everything set to a good starting position.
4391  *
4392  * The eeprom structure in the adapter needs to have been set before
4393  * calling this function.
4394  *
4395  * @acb: The adapter to initialize.
4396  **/
4397 static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
4398 {
4399         struct NvRamType *eeprom = &acb->eeprom;
4400         int i;
4401
4402         /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4403         /* NOTE: acb->io_port_base is set at port registration time */
4404         /* NOTE: acb->io_port_len is set at port registration time */
4405
4406         INIT_LIST_HEAD(&acb->dcb_list);
4407         acb->dcb_run_robin = NULL;
4408         acb->active_dcb = NULL;
4409
4410         INIT_LIST_HEAD(&acb->srb_free_list);
4411         /*  temp SRB for Q tag used or abort command used  */
4412         acb->tmp_srb = &acb->srb;
4413         init_timer(&acb->waiting_timer);
4414         init_timer(&acb->selto_timer);
4415
4416         acb->srb_count = DC395x_MAX_SRB_CNT;
4417
4418         acb->sel_timeout = DC395x_SEL_TIMEOUT;  /* timeout=250ms */
4419         /* NOTE: acb->irq_level is set at IRQ registration time */
4420
4421         acb->tag_max_num = 1 << eeprom->max_tag;
4422         if (acb->tag_max_num > 30)
4423                 acb->tag_max_num = 30;
4424
4425         acb->acb_flag = 0;      /* RESET_DETECT, RESET_DONE, RESET_DEV */
4426         acb->gmode2 = eeprom->channel_cfg;
4427         acb->config = 0;        /* NOTE: actually set in adapter_init_chip */
4428
4429         if (eeprom->channel_cfg & NAC_SCANLUN)
4430                 acb->lun_chk = 1;
4431         acb->scan_devices = 1;
4432
4433         acb->scsi_host->this_id = eeprom->scsi_id;
4434         acb->hostid_bit = (1 << acb->scsi_host->this_id);
4435
4436         for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
4437                 acb->dcb_map[i] = 0;
4438
4439         acb->msg_len = 0;
4440         
4441         /* link static array of srbs into the srb free list */
4442         for (i = 0; i < acb->srb_count - 1; i++)
4443                 srb_free_insert(acb, &acb->srb_array[i]);
4444 }
4445
4446
4447 /**
4448  * adapter_init_host - Initialize the scsi host instance based on
4449  * values that we have already stored in the adapter instance. There's
4450  * some mention that a lot of these are deprecated, so we won't use
4451  * them (we'll use the ones in the adapter instance) but we'll fill
4452  * them in in case something else needs them.
4453  *
4454  * The eeprom structure, irq and io ports in the adapter need to have
4455  * been set before calling this function.
4456  *
4457  * @host: The scsi host instance to fill in the values for.
4458  **/
4459 static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
4460 {
4461         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4462         struct NvRamType *eeprom = &acb->eeprom;
4463         
4464         host->max_cmd_len = 24;
4465         host->can_queue = DC395x_MAX_CMD_QUEUE;
4466         host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4467         host->this_id = (int)eeprom->scsi_id;
4468         host->io_port = acb->io_port_base;
4469         host->n_io_port = acb->io_port_len;
4470         host->dma_channel = -1;
4471         host->unique_id = acb->io_port_base;
4472         host->irq = acb->irq_level;
4473         host->last_reset = jiffies;
4474
4475         host->max_id = 16;
4476         if (host->max_id - 1 == eeprom->scsi_id)
4477                 host->max_id--;
4478
4479 #ifdef CONFIG_SCSI_MULTI_LUN
4480         if (eeprom->channel_cfg & NAC_SCANLUN)
4481                 host->max_lun = 8;
4482         else
4483                 host->max_lun = 1;
4484 #else
4485         host->max_lun = 1;
4486 #endif
4487
4488 }
4489
4490
4491 /**
4492  * adapter_init_chip - Get the chip into a know state and figure out
4493  * some of the settings that apply to this adapter.
4494  *
4495  * The io port in the adapter needs to have been set before calling
4496  * this function. The config will be configured correctly on return.
4497  *
4498  * @acb: The adapter which we are to init.
4499  **/
4500 static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
4501 {
4502         struct NvRamType *eeprom = &acb->eeprom;
4503         
4504         /* Mask all the interrupt */
4505         DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
4506         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
4507
4508         /* Reset SCSI module */
4509         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4510
4511         /* Reset PCI/DMA module */
4512         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4513         udelay(20);
4514
4515         /* program configuration 0 */
4516         acb->config = HCC_AUTOTERM | HCC_PARITY;
4517         if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
4518                 acb->config |= HCC_WIDE_CARD;
4519
4520         if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
4521                 acb->config |= HCC_SCSI_RESET;
4522
4523         if (acb->config & HCC_SCSI_RESET) {
4524                 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
4525                 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4526
4527                 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4528                 /*spin_unlock_irq (&io_request_lock); */
4529                 udelay(500);
4530
4531                 acb->scsi_host->last_reset =
4532                     jiffies + HZ / 2 +
4533                     HZ * acb->eeprom.delay_time;
4534
4535                 /*spin_lock_irq (&io_request_lock); */
4536         }
4537 }
4538
4539
4540 /**
4541  * init_adapter - Grab the resource for the card, setup the adapter
4542  * information, set the card into a known state, create the various
4543  * tables etc etc. This basically gets all adapter information all up
4544  * to date, intialised and gets the chip in sync with it.
4545  *
4546  * @host:       This hosts adapter structure
4547  * @io_port:    The base I/O port
4548  * @irq:        IRQ
4549  *
4550  * Returns 0 if the initialization succeeds, any other value on
4551  * failure.
4552  **/
4553 static int __devinit adapter_init(struct AdapterCtlBlk *acb,
4554         unsigned long io_port, u32 io_port_len, unsigned int irq)
4555 {
4556         if (!request_region(io_port, io_port_len, DC395X_NAME)) {
4557                 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
4558                 goto failed;
4559         }
4560         /* store port base to indicate we have registered it */
4561         acb->io_port_base = io_port;
4562         acb->io_port_len = io_port_len;
4563         
4564         if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
4565                 /* release the region we just claimed */
4566                 dprintkl(KERN_INFO, "Failed to register IRQ\n");
4567                 goto failed;
4568         }
4569         /* store irq to indicate we have registered it */
4570         acb->irq_level = irq;
4571
4572         /* get eeprom configuration information and command line settings etc */
4573         check_eeprom(&acb->eeprom, io_port);
4574         print_eeprom_settings(&acb->eeprom);
4575
4576         /* setup adapter control block */       
4577         adapter_init_params(acb);
4578         
4579         /* display card connectors/termination settings */
4580         adapter_print_config(acb);
4581
4582         if (adapter_sg_tables_alloc(acb)) {
4583                 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
4584                 goto failed;
4585         }
4586         adapter_init_scsi_host(acb->scsi_host);
4587         adapter_init_chip(acb);
4588         set_basic_config(acb);
4589
4590         dprintkdbg(DBG_0,
4591                 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4592                 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4593                 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
4594                 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
4595         return 0;
4596
4597 failed:
4598         if (acb->irq_level)
4599                 free_irq(acb->irq_level, acb);
4600         if (acb->io_port_base)
4601                 release_region(acb->io_port_base, acb->io_port_len);
4602         adapter_sg_tables_free(acb);
4603
4604         return 1;
4605 }
4606
4607
4608 /**
4609  * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4610  * stopping all operations and disabling interrupt generation on the
4611  * card.
4612  *
4613  * @acb: The adapter which we are to shutdown.
4614  **/
4615 static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
4616 {
4617         /* disable interrupts */
4618         DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
4619         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
4620
4621         /* reset the scsi bus */
4622         if (acb->config & HCC_SCSI_RESET)
4623                 reset_scsi_bus(acb);
4624
4625         /* clear any pending interupt state */
4626         DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4627 }
4628
4629
4630
4631 /**
4632  * adapter_uninit - Shut down the chip and release any resources that
4633  * we had allocated. Once this returns the adapter should not be used
4634  * anymore.
4635  *
4636  * @acb: The adapter which we are to un-initialize.
4637  **/
4638 static void adapter_uninit(struct AdapterCtlBlk *acb)
4639 {
4640         unsigned long flags;
4641         DC395x_LOCK_IO(acb->scsi_host, flags);
4642
4643         /* remove timers */
4644         if (timer_pending(&acb->waiting_timer))
4645                 del_timer(&acb->waiting_timer);
4646         if (timer_pending(&acb->selto_timer))
4647                 del_timer(&acb->selto_timer);
4648
4649         adapter_uninit_chip(acb);
4650         adapter_remove_and_free_all_devices(acb);
4651         DC395x_UNLOCK_IO(acb->scsi_host, flags);
4652
4653         if (acb->irq_level)
4654                 free_irq(acb->irq_level, acb);
4655         if (acb->io_port_base)
4656                 release_region(acb->io_port_base, acb->io_port_len);
4657
4658         adapter_sg_tables_free(acb);
4659 }
4660
4661
4662 #undef SPRINTF
4663 #define SPRINTF(args...) pos += sprintf(pos, args)
4664
4665 #undef YESNO
4666 #define YESNO(YN) \
4667  if (YN) SPRINTF(" Yes ");\
4668  else SPRINTF(" No  ")
4669
4670 static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
4671                 char **start, off_t offset, int length, int inout)
4672 {
4673         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4674         int spd, spd1;
4675         char *pos = buffer;
4676         struct DeviceCtlBlk *dcb;
4677         unsigned long flags;
4678         int dev;
4679
4680         if (inout)              /* Has data been written to the file ? */
4681                 return -EPERM;
4682
4683         SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
4684         SPRINTF(" Driver Version " DC395X_VERSION "\n");
4685
4686         DC395x_LOCK_IO(acb->scsi_host, flags);
4687
4688         SPRINTF("SCSI Host Nr %i, ", host->host_no);
4689         SPRINTF("DC395U/UW/F DC315/U %s\n",
4690                 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
4691         SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
4692         SPRINTF("irq_level 0x%04x, ", acb->irq_level);
4693         SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
4694
4695         SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
4696         SPRINTF("AdapterID %i\n", host->this_id);
4697
4698         SPRINTF("tag_max_num %i", acb->tag_max_num);
4699         /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4700         SPRINTF(", FilterCfg 0x%02x",
4701                 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
4702         SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
4703         /*SPRINTF("\n"); */
4704
4705         SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4706         SPRINTF
4707             ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
4708              acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
4709              acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
4710              acb->dcb_map[6], acb->dcb_map[7]);
4711         SPRINTF
4712             ("                      %02x %02x %02x %02x %02x %02x %02x %02x\n",
4713              acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
4714              acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
4715              acb->dcb_map[14], acb->dcb_map[15]);
4716
4717         SPRINTF
4718             ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4719
4720         dev = 0;
4721         list_for_each_entry(dcb, &acb->dcb_list, list) {
4722                 int nego_period;
4723                 SPRINTF("%02i %02i  %02i ", dev, dcb->target_id,
4724                         dcb->target_lun);
4725                 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
4726                 YESNO(dcb->sync_offset);
4727                 YESNO(dcb->sync_period & WIDE_SYNC);
4728                 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
4729                 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
4730                 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
4731                 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
4732                 if (dcb->sync_offset)
4733                         SPRINTF("  %03i ns ", nego_period);
4734                 else
4735                         SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
4736
4737                 if (dcb->sync_offset & 0x0f) {
4738                         spd = 1000 / (nego_period);
4739                         spd1 = 1000 % (nego_period);
4740                         spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
4741                         SPRINTF("   %2i.%1i M     %02i ", spd, spd1,
4742                                 (dcb->sync_offset & 0x0f));
4743                 } else
4744                         SPRINTF("                 ");
4745
4746                 /* Add more info ... */
4747                 SPRINTF("     %02i\n", dcb->max_command);
4748                 dev++;
4749         }
4750
4751         if (timer_pending(&acb->waiting_timer))
4752                 SPRINTF("Waiting queue timer running\n");
4753         else
4754                 SPRINTF("\n");
4755
4756         list_for_each_entry(dcb, &acb->dcb_list, list) {
4757                 struct ScsiReqBlk *srb;
4758                 if (!list_empty(&dcb->srb_waiting_list))
4759                         SPRINTF("DCB (%02i-%i): Waiting: %i:",
4760                                 dcb->target_id, dcb->target_lun,
4761                                 list_size(&dcb->srb_waiting_list));
4762                 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
4763                         SPRINTF(" %li", srb->cmd->pid);
4764                 if (!list_empty(&dcb->srb_going_list))
4765                         SPRINTF("\nDCB (%02i-%i): Going  : %i:",
4766                                 dcb->target_id, dcb->target_lun,
4767                                 list_size(&dcb->srb_going_list));
4768                 list_for_each_entry(srb, &dcb->srb_going_list, list)
4769                         SPRINTF(" %li", srb->cmd->pid);
4770                 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4771                         SPRINTF("\n");
4772         }
4773
4774         if (debug_enabled(DBG_1)) {
4775                 SPRINTF("DCB list for ACB %p:\n", acb);
4776                 list_for_each_entry(dcb, &acb->dcb_list, list) {
4777                         SPRINTF("%p -> ", dcb);
4778                 }
4779                 SPRINTF("END\n");
4780         }
4781
4782         *start = buffer + offset;
4783         DC395x_UNLOCK_IO(acb->scsi_host, flags);
4784
4785         if (pos - buffer < offset)
4786                 return 0;
4787         else if (pos - buffer - offset < length)
4788                 return pos - buffer - offset;
4789         else
4790                 return length;
4791 }
4792
4793
4794 static struct scsi_host_template dc395x_driver_template = {
4795         .module                 = THIS_MODULE,
4796         .proc_name              = DC395X_NAME,
4797         .proc_info              = dc395x_proc_info,
4798         .name                   = DC395X_BANNER " " DC395X_VERSION,
4799         .queuecommand           = dc395x_queue_command,
4800         .bios_param             = dc395x_bios_param,
4801         .slave_alloc            = dc395x_slave_alloc,
4802         .slave_destroy          = dc395x_slave_destroy,
4803         .can_queue              = DC395x_MAX_CAN_QUEUE,
4804         .this_id                = 7,
4805         .sg_tablesize           = DC395x_MAX_SG_TABLESIZE,
4806         .cmd_per_lun            = DC395x_MAX_CMD_PER_LUN,
4807         .eh_abort_handler       = dc395x_eh_abort,
4808         .eh_bus_reset_handler   = dc395x_eh_bus_reset,
4809         .unchecked_isa_dma      = 0,
4810         .use_clustering         = DISABLE_CLUSTERING,
4811 };
4812
4813
4814 /**
4815  * banner_display - Display banner on first instance of driver
4816  * initialized.
4817  **/
4818 static void banner_display(void)
4819 {
4820         static int banner_done = 0;
4821         if (!banner_done)
4822         {
4823                 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
4824                 banner_done = 1;
4825         }
4826 }
4827
4828
4829 /**
4830  * dc395x_init_one - Initialise a single instance of the adapter.
4831  *
4832  * The PCI layer will call this once for each instance of the adapter
4833  * that it finds in the system. The pci_dev strcuture indicates which
4834  * instance we are being called from.
4835  * 
4836  * @dev: The PCI device to intialize.
4837  * @id: Looks like a pointer to the entry in our pci device table
4838  * that was actually matched by the PCI subsystem.
4839  *
4840  * Returns 0 on success, or an error code (-ve) on failure.
4841  **/
4842 static int __devinit dc395x_init_one(struct pci_dev *dev,
4843                 const struct pci_device_id *id)
4844 {
4845         struct Scsi_Host *scsi_host = NULL;
4846         struct AdapterCtlBlk *acb = NULL;
4847         unsigned long io_port_base;
4848         unsigned int io_port_len;
4849         unsigned int irq;
4850         
4851         dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
4852         banner_display();
4853
4854         if (pci_enable_device(dev))
4855         {
4856                 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
4857                 return -ENODEV;
4858         }
4859         io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
4860         io_port_len = pci_resource_len(dev, 0);
4861         irq = dev->irq;
4862         dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
4863
4864         /* allocate scsi host information (includes out adapter) */
4865         scsi_host = scsi_host_alloc(&dc395x_driver_template,
4866                                     sizeof(struct AdapterCtlBlk));
4867         if (!scsi_host) {
4868                 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
4869                 goto fail;
4870         }
4871         acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
4872         acb->scsi_host = scsi_host;
4873         acb->dev = dev;
4874
4875         /* initialise the adapter and everything we need */
4876         if (adapter_init(acb, io_port_base, io_port_len, irq)) {
4877                 dprintkl(KERN_INFO, "adapter init failed\n");
4878                 goto fail;
4879         }
4880
4881         pci_set_master(dev);
4882
4883         /* get the scsi mid level to scan for new devices on the bus */
4884         if (scsi_add_host(scsi_host, &dev->dev)) {
4885                 dprintkl(KERN_ERR, "scsi_add_host failed\n");
4886                 goto fail;
4887         }
4888         pci_set_drvdata(dev, scsi_host);
4889         scsi_scan_host(scsi_host);
4890                 
4891         return 0;
4892
4893 fail:
4894         if (acb != NULL)
4895                 adapter_uninit(acb);
4896         if (scsi_host != NULL)
4897                 scsi_host_put(scsi_host);
4898         pci_disable_device(dev);
4899         return -ENODEV;
4900 }
4901
4902
4903 /**
4904  * dc395x_remove_one - Called to remove a single instance of the
4905  * adapter.
4906  *
4907  * @dev: The PCI device to intialize.
4908  **/
4909 static void __devexit dc395x_remove_one(struct pci_dev *dev)
4910 {
4911         struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
4912         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
4913
4914         dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
4915
4916         scsi_remove_host(scsi_host);
4917         adapter_uninit(acb);
4918         pci_disable_device(dev);
4919         scsi_host_put(scsi_host);
4920         pci_set_drvdata(dev, NULL);
4921 }
4922
4923
4924 static struct pci_device_id dc395x_pci_table[] = {
4925         {
4926                 .vendor         = PCI_VENDOR_ID_TEKRAM,
4927                 .device         = PCI_DEVICE_ID_TEKRAM_TRMS1040,
4928                 .subvendor      = PCI_ANY_ID,
4929                 .subdevice      = PCI_ANY_ID,
4930          },
4931         {}                      /* Terminating entry */
4932 };
4933 MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
4934
4935
4936 static struct pci_driver dc395x_driver = {
4937         .name           = DC395X_NAME,
4938         .id_table       = dc395x_pci_table,
4939         .probe          = dc395x_init_one,
4940         .remove         = __devexit_p(dc395x_remove_one),
4941 };
4942
4943
4944 /**
4945  * dc395x_module_init - Module initialization function
4946  *
4947  * Used by both module and built-in driver to initialise this driver.
4948  **/
4949 static int __init dc395x_module_init(void)
4950 {
4951         return pci_register_driver(&dc395x_driver);
4952 }
4953
4954
4955 /**
4956  * dc395x_module_exit - Module cleanup function.
4957  **/
4958 static void __exit dc395x_module_exit(void)
4959 {
4960         pci_unregister_driver(&dc395x_driver);
4961 }
4962
4963
4964 module_init(dc395x_module_init);
4965 module_exit(dc395x_module_exit);
4966
4967 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4968 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4969 MODULE_LICENSE("GPL");