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