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