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