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