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