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