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