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