blob: cca41cf8d3e7cf23fc78cdb41e425c2cc2941767 [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 */
1313static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1314{
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
1359
1360/*
1361 * abort an errant SCSI command
1362 * @cmd - command to be aborted
1363 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1364 */
1365static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1366{
1367 /*
1368 * Look into our command queues: If it has not been sent already,
1369 * we remove it and return success. Otherwise fail.
1370 */
1371 struct AdapterCtlBlk *acb =
1372 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1373 struct DeviceCtlBlk *dcb;
1374 struct ScsiReqBlk *srb;
1375 dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
1376 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
1377
1378 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1379 if (!dcb) {
1380 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1381 return FAILED;
1382 }
1383
1384 srb = find_cmd(cmd, &dcb->srb_waiting_list);
1385 if (srb) {
1386 srb_waiting_remove(dcb, srb);
1387 pci_unmap_srb_sense(acb, srb);
1388 pci_unmap_srb(acb, srb);
1389 free_tag(dcb, srb);
1390 srb_free_insert(acb, srb);
1391 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1392 cmd->result = DID_ABORT << 16;
1393 return SUCCESS;
1394 }
1395 srb = find_cmd(cmd, &dcb->srb_going_list);
1396 if (srb) {
1397 dprintkl(KERN_DEBUG, "eh_abort: Command in progress");
1398 /* XXX: Should abort the command here */
1399 } else {
1400 dprintkl(KERN_DEBUG, "eh_abort: Command not found");
1401 }
1402 return FAILED;
1403}
1404
1405
1406/* SDTR */
1407static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1408 struct ScsiReqBlk *srb)
1409{
1410 u8 *ptr = srb->msgout_buf + srb->msg_count;
1411 if (srb->msg_count > 1) {
1412 dprintkl(KERN_INFO,
1413 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1414 srb->msg_count, srb->msgout_buf[0],
1415 srb->msgout_buf[1]);
1416 return;
1417 }
1418 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1419 dcb->sync_offset = 0;
1420 dcb->min_nego_period = 200 >> 2;
1421 } else if (dcb->sync_offset == 0)
1422 dcb->sync_offset = SYNC_NEGO_OFFSET;
1423
1424 *ptr++ = MSG_EXTENDED; /* (01h) */
1425 *ptr++ = 3; /* length */
1426 *ptr++ = EXTENDED_SDTR; /* (01h) */
1427 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */
1428 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */
1429 srb->msg_count += 5;
1430 srb->state |= SRB_DO_SYNC_NEGO;
1431}
1432
1433
1434/* WDTR */
1435static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1436 struct ScsiReqBlk *srb)
1437{
1438 u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1439 (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1440 u8 *ptr = srb->msgout_buf + srb->msg_count;
1441 if (srb->msg_count > 1) {
1442 dprintkl(KERN_INFO,
1443 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1444 srb->msg_count, srb->msgout_buf[0],
1445 srb->msgout_buf[1]);
1446 return;
1447 }
1448 *ptr++ = MSG_EXTENDED; /* (01h) */
1449 *ptr++ = 2; /* length */
1450 *ptr++ = EXTENDED_WDTR; /* (03h) */
1451 *ptr++ = wide;
1452 srb->msg_count += 4;
1453 srb->state |= SRB_DO_WIDE_NEGO;
1454}
1455
1456
1457#if 0
1458/* Timer to work around chip flaw: When selecting and the bus is
1459 * busy, we sometimes miss a Selection timeout IRQ */
1460void selection_timeout_missed(unsigned long ptr);
1461/* Sets the timer to wake us up */
1462static void selto_timer(struct AdapterCtlBlk *acb)
1463{
1464 if (timer_pending(&acb->selto_timer))
1465 return;
1466 acb->selto_timer.function = selection_timeout_missed;
1467 acb->selto_timer.data = (unsigned long) acb;
1468 if (time_before
1469 (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
1470 acb->selto_timer.expires =
1471 acb->scsi_host->last_reset + HZ / 2 + 1;
1472 else
1473 acb->selto_timer.expires = jiffies + HZ + 1;
1474 add_timer(&acb->selto_timer);
1475}
1476
1477
1478void selection_timeout_missed(unsigned long ptr)
1479{
1480 unsigned long flags;
1481 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
1482 struct ScsiReqBlk *srb;
1483 dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1484 if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1485 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1486 return;
1487 }
1488 DC395x_LOCK_IO(acb->scsi_host, flags);
1489 srb = acb->active_dcb->active_srb;
1490 disconnect(acb);
1491 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1492}
1493#endif
1494
1495
1496static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1497 struct ScsiReqBlk* srb)
1498{
1499 u16 s_stat2, return_code;
1500 u8 s_stat, scsicommand, i, identify_message;
1501 u8 *ptr;
1502 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
1503 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
1504
1505 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
1506
1507 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1508 s_stat2 = 0;
1509 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1510#if 1
1511 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1512 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
1513 srb->cmd->pid, s_stat, s_stat2);
1514 /*
1515 * Try anyway?
1516 *
1517 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1518 * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed!
1519 * (This is likely to be a bug in the hardware. Obviously, most people
1520 * only have one initiator per SCSI bus.)
1521 * Instead let this fail and have the timer make sure the command is
1522 * tried again after a short time
1523 */
1524 /*selto_timer (acb); */
1525 return 1;
1526 }
1527#endif
1528 if (acb->active_dcb) {
1529 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
1530 "command while another command (pid#%li) is active.",
1531 srb->cmd->pid,
1532 acb->active_dcb->active_srb ?
1533 acb->active_dcb->active_srb->cmd->pid : 0);
1534 return 1;
1535 }
1536 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1537 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
1538 srb->cmd->pid);
1539 return 1;
1540 }
1541 /* Allow starting of SCSI commands half a second before we allow the mid-level
1542 * to queue them again after a reset */
1543 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1544 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1545 return 1;
1546 }
1547
1548 /* Flush FIFO */
1549 clear_fifo(acb, "start_scsi");
1550 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1551 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1552 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1553 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1554 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1555
1556 identify_message = dcb->identify_msg;
1557 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1558 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1559 if (srb->flag & AUTO_REQSENSE)
1560 identify_message &= 0xBF;
1561
1562 if (((srb->cmd->cmnd[0] == INQUIRY)
1563 || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1564 || (srb->flag & AUTO_REQSENSE))
1565 && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1566 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1567 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1568 && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1569 && (dcb->target_lun == 0)) {
1570 srb->msgout_buf[0] = identify_message;
1571 srb->msg_count = 1;
1572 scsicommand = SCMD_SEL_ATNSTOP;
1573 srb->state = SRB_MSGOUT;
1574#ifndef SYNC_FIRST
1575 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1576 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1577 build_wdtr(acb, dcb, srb);
1578 goto no_cmd;
1579 }
1580#endif
1581 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1582 && dcb->inquiry7 & SCSI_INQ_SYNC) {
1583 build_sdtr(acb, dcb, srb);
1584 goto no_cmd;
1585 }
1586 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1587 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1588 build_wdtr(acb, dcb, srb);
1589 goto no_cmd;
1590 }
1591 srb->msg_count = 0;
1592 }
1593 /* Send identify message */
1594 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1595
1596 scsicommand = SCMD_SEL_ATN;
1597 srb->state = SRB_START_;
1598#ifndef DC395x_NO_TAGQ
1599 if ((dcb->sync_mode & EN_TAG_QUEUEING)
1600 && (identify_message & 0xC0)) {
1601 /* Send Tag message */
1602 u32 tag_mask = 1;
1603 u8 tag_number = 0;
1604 while (tag_mask & dcb->tag_mask
1605 && tag_number <= dcb->max_command) {
1606 tag_mask = tag_mask << 1;
1607 tag_number++;
1608 }
1609 if (tag_number >= dcb->max_command) {
1610 dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
1611 "Out of tags target=<%02i-%i>)\n",
1612 srb->cmd->pid, srb->cmd->device->id,
1613 srb->cmd->device->lun);
1614 srb->state = SRB_READY;
1615 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1616 DO_HWRESELECT);
1617 return 1;
1618 }
1619 /* Send Tag id */
1620 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1621 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1622 dcb->tag_mask |= tag_mask;
1623 srb->tag_number = tag_number;
1624 scsicommand = SCMD_SEL_ATN3;
1625 srb->state = SRB_START_;
1626 }
1627#endif
1628/*polling:*/
1629 /* Send CDB ..command block ......... */
1630 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
1631 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
1632 srb->cmd->cmnd[0], srb->tag_number);
1633 if (srb->flag & AUTO_REQSENSE) {
1634 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1635 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1636 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1637 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1638 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1639 sizeof(srb->cmd->sense_buffer));
1640 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1641 } else {
1642 ptr = (u8 *)srb->cmd->cmnd;
1643 for (i = 0; i < srb->cmd->cmd_len; i++)
1644 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1645 }
1646 no_cmd:
1647 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1648 DO_HWRESELECT | DO_DATALATCH);
1649 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1650 /*
1651 * If start_scsi return 1:
1652 * we caught an interrupt (must be reset or reselection ... )
1653 * : Let's process it first!
1654 */
1655 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
1656 srb->cmd->pid, dcb->target_id, dcb->target_lun);
1657 srb->state = SRB_READY;
1658 free_tag(dcb, srb);
1659 srb->msg_count = 0;
1660 return_code = 1;
1661 /* This IRQ should NOT get lost, as we did not acknowledge it */
1662 } else {
1663 /*
1664 * If start_scsi returns 0:
1665 * we know that the SCSI processor is free
1666 */
1667 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1668 dcb->active_srb = srb;
1669 acb->active_dcb = dcb;
1670 return_code = 0;
1671 /* it's important for atn stop */
1672 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1673 DO_DATALATCH | DO_HWRESELECT);
1674 /* SCSI command */
1675 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1676 }
1677 return return_code;
1678}
1679
1680
1681#define DC395x_ENABLE_MSGOUT \
1682 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1683 srb->state |= SRB_MSGOUT
1684
1685
1686/* abort command */
1687static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1688 struct ScsiReqBlk *srb)
1689{
1690 srb->msgout_buf[0] = ABORT;
1691 srb->msg_count = 1;
1692 DC395x_ENABLE_MSGOUT;
1693 srb->state &= ~SRB_MSGIN;
1694 srb->state |= SRB_MSGOUT;
1695}
1696
1697
1698/**
1699 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1700 * have been triggered for this card.
1701 *
1702 * @acb: a pointer to the adpter control block
1703 * @scsi_status: the status return when we checked the card
1704 **/
1705static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1706 u16 scsi_status)
1707{
1708 struct DeviceCtlBlk *dcb;
1709 struct ScsiReqBlk *srb;
1710 u16 phase;
1711 u8 scsi_intstatus;
1712 unsigned long flags;
1713 void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
1714 u16 *);
1715
1716 DC395x_LOCK_IO(acb->scsi_host, flags);
1717
1718 /* This acknowledges the IRQ */
1719 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1720 if ((scsi_status & 0x2007) == 0x2002)
1721 dprintkl(KERN_DEBUG,
1722 "COP after COP completed? %04x\n", scsi_status);
1723 if (debug_enabled(DBG_KG)) {
1724 if (scsi_intstatus & INT_SELTIMEOUT)
1725 dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1726 }
1727 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1728
1729 if (timer_pending(&acb->selto_timer))
1730 del_timer(&acb->selto_timer);
1731
1732 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1733 disconnect(acb); /* bus free interrupt */
1734 goto out_unlock;
1735 }
1736 if (scsi_intstatus & INT_RESELECTED) {
1737 reselect(acb);
1738 goto out_unlock;
1739 }
1740 if (scsi_intstatus & INT_SELECT) {
1741 dprintkl(KERN_INFO, "Host does not support target mode!\n");
1742 goto out_unlock;
1743 }
1744 if (scsi_intstatus & INT_SCSIRESET) {
1745 scsi_reset_detect(acb);
1746 goto out_unlock;
1747 }
1748 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1749 dcb = acb->active_dcb;
1750 if (!dcb) {
1751 dprintkl(KERN_DEBUG,
1752 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1753 scsi_status, scsi_intstatus);
1754 goto out_unlock;
1755 }
1756 srb = dcb->active_srb;
1757 if (dcb->flag & ABORT_DEV_) {
1758 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1759 enable_msgout_abort(acb, srb);
1760 }
1761
1762 /* software sequential machine */
1763 phase = (u16)srb->scsi_phase;
1764
1765 /*
1766 * 62037 or 62137
1767 * call dc395x_scsi_phase0[]... "phase entry"
1768 * handle every phase before start transfer
1769 */
1770 /* data_out_phase0, phase:0 */
1771 /* data_in_phase0, phase:1 */
1772 /* command_phase0, phase:2 */
1773 /* status_phase0, phase:3 */
1774 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
1775 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
1776 /* msgout_phase0, phase:6 */
1777 /* msgin_phase0, phase:7 */
1778 dc395x_statev = dc395x_scsi_phase0[phase];
1779 dc395x_statev(acb, srb, &scsi_status);
1780
1781 /*
1782 * if there were any exception occured scsi_status
1783 * will be modify to bus free phase new scsi_status
1784 * transfer out from ... previous dc395x_statev
1785 */
1786 srb->scsi_phase = scsi_status & PHASEMASK;
1787 phase = (u16)scsi_status & PHASEMASK;
1788
1789 /*
1790 * call dc395x_scsi_phase1[]... "phase entry" handle
1791 * every phase to do transfer
1792 */
1793 /* data_out_phase1, phase:0 */
1794 /* data_in_phase1, phase:1 */
1795 /* command_phase1, phase:2 */
1796 /* status_phase1, phase:3 */
1797 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
1798 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
1799 /* msgout_phase1, phase:6 */
1800 /* msgin_phase1, phase:7 */
1801 dc395x_statev = dc395x_scsi_phase1[phase];
1802 dc395x_statev(acb, srb, &scsi_status);
1803 }
1804 out_unlock:
1805 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1806}
1807
1808
1809static irqreturn_t dc395x_interrupt(int irq, void *dev_id,
1810 struct pt_regs *regs)
1811{
1812 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)dev_id;
1813 u16 scsi_status;
1814 u8 dma_status;
1815 irqreturn_t handled = IRQ_NONE;
1816
1817 /*
1818 * Check for pending interupt
1819 */
1820 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1821 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1822 if (scsi_status & SCSIINTERRUPT) {
1823 /* interupt pending - let's process it! */
1824 dc395x_handle_interrupt(acb, scsi_status);
1825 handled = IRQ_HANDLED;
1826 }
1827 else if (dma_status & 0x20) {
1828 /* Error from the DMA engine */
1829 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1830#if 0
1831 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
1832 if (acb->active_dcb) {
1833 acb->active_dcb-> flag |= ABORT_DEV_;
1834 if (acb->active_dcb->active_srb)
1835 enable_msgout_abort(acb, acb->active_dcb->active_srb);
1836 }
1837 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
1838#else
1839 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
1840 acb = NULL;
1841#endif
1842 handled = IRQ_HANDLED;
1843 }
1844
1845 return handled;
1846}
1847
1848
1849static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1850 u16 *pscsi_status)
1851{
1852 dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->pid);
1853 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
1854 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
1855
1856 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1857 srb->state &= ~SRB_MSGOUT;
1858}
1859
1860
1861static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1862 u16 *pscsi_status)
1863{
1864 u16 i;
1865 u8 *ptr;
1866 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->pid);
1867
1868 clear_fifo(acb, "msgout_phase1");
1869 if (!(srb->state & SRB_MSGOUT)) {
1870 srb->state |= SRB_MSGOUT;
1871 dprintkl(KERN_DEBUG,
1872 "msgout_phase1: (pid#%li) Phase unexpected\n",
1873 srb->cmd->pid); /* So what ? */
1874 }
1875 if (!srb->msg_count) {
1876 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
1877 srb->cmd->pid);
1878 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
1879 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1880 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1881 return;
1882 }
1883 ptr = (u8 *)srb->msgout_buf;
1884 for (i = 0; i < srb->msg_count; i++)
1885 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1886 srb->msg_count = 0;
1887 if (srb->msgout_buf[0] == MSG_ABORT)
1888 srb->state = SRB_ABORT_SENT;
1889
1890 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1891}
1892
1893
1894static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1895 u16 *pscsi_status)
1896{
1897 dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->pid);
1898 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1899}
1900
1901
1902static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1903 u16 *pscsi_status)
1904{
1905 struct DeviceCtlBlk *dcb;
1906 u8 *ptr;
1907 u16 i;
1908 dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->pid);
1909
1910 clear_fifo(acb, "command_phase1");
1911 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
1912 if (!(srb->flag & AUTO_REQSENSE)) {
1913 ptr = (u8 *)srb->cmd->cmnd;
1914 for (i = 0; i < srb->cmd->cmd_len; i++) {
1915 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
1916 ptr++;
1917 }
1918 } else {
1919 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1920 dcb = acb->active_dcb;
1921 /* target id */
1922 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1923 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1924 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1925 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
1926 sizeof(srb->cmd->sense_buffer));
1927 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1928 }
1929 srb->state |= SRB_COMMAND;
1930 /* it's important for atn stop */
1931 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1932 /* SCSI command */
1933 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1934}
1935
1936
1937/*
1938 * Verify that the remaining space in the hw sg lists is the same as
1939 * the count of remaining bytes in srb->total_xfer_length
1940 */
1941static void sg_verify_length(struct ScsiReqBlk *srb)
1942{
1943 if (debug_enabled(DBG_SG)) {
1944 unsigned len = 0;
1945 unsigned idx = srb->sg_index;
1946 struct SGentry *psge = srb->segment_x + idx;
1947 for (; idx < srb->sg_count; psge++, idx++)
1948 len += psge->length;
1949 if (len != srb->total_xfer_length)
1950 dprintkdbg(DBG_SG,
1951 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1952 srb->total_xfer_length, len);
1953 }
1954}
1955
1956
1957/*
1958 * Compute the next Scatter Gather list index and adjust its length
1959 * and address if necessary; also compute virt_addr
1960 */
1961static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
1962{
1963 u8 idx;
1964 struct scatterlist *sg;
1965 struct scsi_cmnd *cmd = srb->cmd;
1966 int segment = cmd->use_sg;
1967 u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
1968 struct SGentry *psge = srb->segment_x + srb->sg_index;
1969 void **virt = srb->virt_map;
1970
1971 dprintkdbg(DBG_0,
1972 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
1973 xferred, srb->total_xfer_length, left);
1974 if (xferred == 0) {
1975 /* nothing to update since we did not transfer any data */
1976 return;
1977 }
1978
1979 sg_verify_length(srb);
1980 srb->total_xfer_length = left; /* update remaining count */
1981 for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
1982 if (xferred >= psge->length) {
1983 /* Complete SG entries done */
1984 xferred -= psge->length;
1985 } else {
1986 /* Partial SG entry done */
1987 psge->length -= xferred;
1988 psge->address += xferred;
1989 srb->sg_index = idx;
1990 pci_dma_sync_single_for_device(srb->dcb->
1991 acb->dev,
1992 srb->sg_bus_addr,
1993 SEGMENTX_LEN,
1994 PCI_DMA_TODEVICE);
1995 break;
1996 }
1997 psge++;
1998 }
1999 sg_verify_length(srb);
2000
2001 /* we need the corresponding virtual address */
2002 if (!segment) {
2003 srb->virt_addr += xferred;
2004 return;
2005 }
2006
2007 /* We have to walk the scatterlist to find it */
2008 sg = (struct scatterlist *)cmd->request_buffer;
2009 idx = 0;
2010 while (segment--) {
2011 unsigned long mask =
2012 ~((unsigned long)sg->length - 1) & PAGE_MASK;
2013 if ((sg_dma_address(sg) & mask) == (psge->address & mask)) {
2014 srb->virt_addr = virt[idx] + (psge->address & ~PAGE_MASK);
2015 return;
2016 }
2017 ++sg;
2018 ++idx;
2019 }
2020
2021 dprintkl(KERN_ERR, "sg_update_list: sg_to_virt failed\n");
2022 srb->virt_addr = NULL;
2023}
2024
2025
2026/*
2027 * We have transfered a single byte (PIO mode?) and need to update
2028 * the count of bytes remaining (total_xfer_length) and update the sg
2029 * entry to either point to next byte in the current sg entry, or of
2030 * already at the end to point to the start of the next sg entry
2031 */
2032static void sg_subtract_one(struct ScsiReqBlk *srb)
2033{
2034 srb->total_xfer_length--;
2035 srb->segment_x[srb->sg_index].length--;
2036 if (srb->total_xfer_length &&
2037 !srb->segment_x[srb->sg_index].length) {
2038 if (debug_enabled(DBG_PIO))
2039 printk(" (next segment)");
2040 srb->sg_index++;
2041 sg_update_list(srb, srb->total_xfer_length);
2042 }
2043}
2044
2045
2046/*
2047 * cleanup_after_transfer
2048 *
2049 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
2050 * KG: Currently called from StatusPhase1 ()
2051 * Should probably also be called from other places
2052 * Best might be to call it in DataXXPhase0, if new phase will differ
2053 */
2054static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
2055 struct ScsiReqBlk *srb)
2056{
2057 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
2058 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
2059 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2060 clear_fifo(acb, "cleanup/in");
2061 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2062 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2063 } else { /* write */
2064 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2065 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2066 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2067 clear_fifo(acb, "cleanup/out");
2068 }
2069 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2070}
2071
2072
2073/*
2074 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
2075 * Seems to be needed for unknown reasons; could be a hardware bug :-(
2076 */
2077#define DC395x_LASTPIO 4
2078
2079
2080static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2081 u16 *pscsi_status)
2082{
2083 struct DeviceCtlBlk *dcb = srb->dcb;
2084 u16 scsi_status = *pscsi_status;
2085 u32 d_left_counter = 0;
2086 dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
2087 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2088
2089 /*
2090 * KG: We need to drain the buffers before we draw any conclusions!
2091 * This means telling the DMA to push the rest into SCSI, telling
2092 * SCSI to push the rest to the bus.
2093 * However, the device might have been the one to stop us (phase
2094 * change), and the data in transit just needs to be accounted so
2095 * it can be retransmitted.)
2096 */
2097 /*
2098 * KG: Stop DMA engine pushing more data into the SCSI FIFO
2099 * If we need more data, the DMA SG list will be freshly set up, anyway
2100 */
2101 dprintkdbg(DBG_PIO, "data_out_phase0: "
2102 "DMA{fifcnt=0x%02x fifostat=0x%02x} "
2103 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
2104 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2105 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2106 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2107 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2108 srb->total_xfer_length);
2109 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2110
2111 if (!(srb->state & SRB_XFERPAD)) {
2112 if (scsi_status & PARITYERROR)
2113 srb->status |= PARITY_ERROR;
2114
2115 /*
2116 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2117 * is the no of bytes it got from the DMA engine not the no it
2118 * transferred successfully to the device. (And the difference could
2119 * be as much as the FIFO size, I guess ...)
2120 */
2121 if (!(scsi_status & SCSIXFERDONE)) {
2122 /*
2123 * when data transfer from DMA FIFO to SCSI FIFO
2124 * if there was some data left in SCSI FIFO
2125 */
2126 d_left_counter =
2127 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2128 0x1F);
2129 if (dcb->sync_period & WIDE_SYNC)
2130 d_left_counter <<= 1;
2131
2132 dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
2133 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
2134 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
2135 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2136 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2137 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2138 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2139 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2140 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2141 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2142 }
2143 /*
2144 * calculate all the residue data that not yet transfered
2145 * SCSI transfer counter + left in SCSI FIFO data
2146 *
2147 * .....TRM_S1040_SCSI_COUNTER (24bits)
2148 * The counter always decrement by one for every SCSI byte transfer.
2149 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2150 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2151 */
2152 if (srb->total_xfer_length > DC395x_LASTPIO)
2153 d_left_counter +=
2154 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2155
2156 /* Is this a good idea? */
2157 /*clear_fifo(acb, "DOP1"); */
2158 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2159 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2160 && srb->cmd->request_bufflen % 2) {
2161 d_left_counter = 0;
2162 dprintkl(KERN_INFO,
2163 "data_out_phase0: Discard 1 byte (0x%02x)\n",
2164 scsi_status);
2165 }
2166 /*
2167 * KG: Oops again. Same thinko as above: The SCSI might have been
2168 * faster than the DMA engine, so that it ran out of data.
2169 * In that case, we have to do just nothing!
2170 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2171 */
2172 /*
2173 * KG: This is nonsense: We have been WRITING data to the bus
2174 * If the SCSI engine has no bytes left, how should the DMA engine?
2175 */
2176 if (d_left_counter == 0) {
2177 srb->total_xfer_length = 0;
2178 } else {
2179 /*
2180 * if transfer not yet complete
2181 * there were some data residue in SCSI FIFO or
2182 * SCSI transfer counter not empty
2183 */
2184 long oldxferred =
2185 srb->total_xfer_length - d_left_counter;
2186 const int diff =
2187 (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2188 sg_update_list(srb, d_left_counter);
2189 /* KG: Most ugly hack! Apparently, this works around a chip bug */
2190 if ((srb->segment_x[srb->sg_index].length ==
2191 diff && srb->cmd->use_sg)
2192 || ((oldxferred & ~PAGE_MASK) ==
2193 (PAGE_SIZE - diff))
2194 ) {
2195 dprintkl(KERN_INFO, "data_out_phase0: "
2196 "Work around chip bug (%i)?\n", diff);
2197 d_left_counter =
2198 srb->total_xfer_length - diff;
2199 sg_update_list(srb, d_left_counter);
2200 /*srb->total_xfer_length -= diff; */
2201 /*srb->virt_addr += diff; */
2202 /*if (srb->cmd->use_sg) */
2203 /* srb->sg_index++; */
2204 }
2205 }
2206 }
2207 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2208 cleanup_after_transfer(acb, srb);
2209 }
2210}
2211
2212
2213static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2214 u16 *pscsi_status)
2215{
2216 dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
2217 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2218 clear_fifo(acb, "data_out_phase1");
2219 /* do prepare before transfer when data out phase */
2220 data_io_transfer(acb, srb, XFERDATAOUT);
2221}
2222
2223
2224static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2225 u16 *pscsi_status)
2226{
2227 u16 scsi_status = *pscsi_status;
2228 u32 d_left_counter = 0;
2229 dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
2230 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2231
2232 /*
2233 * KG: DataIn is much more tricky than DataOut. When the device is finished
2234 * and switches to another phase, the SCSI engine should be finished too.
2235 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2236 * engine and transferred to memory.
2237 * We should wait for the FIFOs to be emptied by that (is there any way to
2238 * enforce this?) and then stop the DMA engine, because it might think, that
2239 * there are more bytes to follow. Yes, the device might disconnect prior to
2240 * having all bytes transferred!
2241 * Also we should make sure that all data from the DMA engine buffer's really
2242 * made its way to the system memory! Some documentation on this would not
2243 * seem to be a bad idea, actually.
2244 */
2245 if (!(srb->state & SRB_XFERPAD)) {
2246 if (scsi_status & PARITYERROR) {
2247 dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
2248 "Parity Error\n", srb->cmd->pid);
2249 srb->status |= PARITY_ERROR;
2250 }
2251 /*
2252 * KG: We should wait for the DMA FIFO to be empty ...
2253 * but: it would be better to wait first for the SCSI FIFO and then the
2254 * the DMA FIFO to become empty? How do we know, that the device not already
2255 * sent data to the FIFO in a MsgIn phase, eg.?
2256 */
2257 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2258#if 0
2259 int ctr = 6000000;
2260 dprintkl(KERN_DEBUG,
2261 "DIP0: Wait for DMA FIFO to flush ...\n");
2262 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2263 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2264 /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2265 while (!
2266 (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
2267 0x80) && --ctr);
2268 if (ctr < 6000000 - 1)
2269 dprintkl(KERN_DEBUG
2270 "DIP0: Had to wait for DMA ...\n");
2271 if (!ctr)
2272 dprintkl(KERN_ERR,
2273 "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2274 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2275#endif
2276 dprintkdbg(DBG_KG, "data_in_phase0: "
2277 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2278 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2279 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2280 }
2281 /* Now: Check remainig data: The SCSI counters should tell us ... */
2282 d_left_counter = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER)
2283 + ((DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f)
2284 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2285 0));
2286 dprintkdbg(DBG_KG, "data_in_phase0: "
2287 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2288 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2289 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2290 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2291 (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2292 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2293 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2294 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2295 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
2296 srb->total_xfer_length, d_left_counter);
2297#if DC395x_LASTPIO
2298 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2299 if (d_left_counter
2300 && srb->total_xfer_length <= DC395x_LASTPIO) {
2301 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2302 /*sg_update_list (srb, d_left_counter); */
2303 dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) to "
2304 "%p for remaining %i bytes:",
2305 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f,
2306 (srb->dcb->sync_period & WIDE_SYNC) ?
2307 "words" : "bytes",
2308 srb->virt_addr,
2309 srb->total_xfer_length);
2310 if (srb->dcb->sync_period & WIDE_SYNC)
2311 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2312 CFG2_WIDEFIFO);
2313 while (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) != 0x40) {
2314 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2315 *(srb->virt_addr)++ = byte;
2316 if (debug_enabled(DBG_PIO))
2317 printk(" %02x", byte);
2318 d_left_counter--;
2319 sg_subtract_one(srb);
2320 }
2321 if (srb->dcb->sync_period & WIDE_SYNC) {
2322#if 1
2323 /* Read the last byte ... */
2324 if (srb->total_xfer_length > 0) {
2325 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2326 *(srb->virt_addr)++ = byte;
2327 srb->total_xfer_length--;
2328 if (debug_enabled(DBG_PIO))
2329 printk(" %02x", byte);
2330 }
2331#endif
2332 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2333 }
2334 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2335 /*srb->total_xfer_length = 0; */
2336 if (debug_enabled(DBG_PIO))
2337 printk("\n");
2338 }
2339#endif /* DC395x_LASTPIO */
2340
2341#if 0
2342 /*
2343 * KG: This was in DATAOUT. Does it also belong here?
2344 * Nobody seems to know what counter and fifo_cnt count exactly ...
2345 */
2346 if (!(scsi_status & SCSIXFERDONE)) {
2347 /*
2348 * when data transfer from DMA FIFO to SCSI FIFO
2349 * if there was some data left in SCSI FIFO
2350 */
2351 d_left_counter =
2352 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2353 0x1F);
2354 if (srb->dcb->sync_period & WIDE_SYNC)
2355 d_left_counter <<= 1;
2356 /*
2357 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2358 * so need to *= 2
2359 * KG: Seems to be correct ...
2360 */
2361 }
2362#endif
2363 /* KG: This should not be needed any more! */
2364 if (d_left_counter == 0
2365 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2366#if 0
2367 int ctr = 6000000;
2368 u8 TempDMAstatus;
2369 do {
2370 TempDMAstatus =
2371 DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2372 } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
2373 if (!ctr)
2374 dprintkl(KERN_ERR,
2375 "Deadlock in DataInPhase0 waiting for DMA!!\n");
2376 srb->total_xfer_length = 0;
2377#endif
2378 srb->total_xfer_length = d_left_counter;
2379 } else { /* phase changed */
2380 /*
2381 * parsing the case:
2382 * when a transfer not yet complete
2383 * but be disconnected by target
2384 * if transfer not yet complete
2385 * there were some data residue in SCSI FIFO or
2386 * SCSI transfer counter not empty
2387 */
2388 sg_update_list(srb, d_left_counter);
2389 }
2390 }
2391 /* KG: The target may decide to disconnect: Empty FIFO before! */
2392 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2393 cleanup_after_transfer(acb, srb);
2394 }
2395}
2396
2397
2398static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2399 u16 *pscsi_status)
2400{
2401 dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
2402 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2403 data_io_transfer(acb, srb, XFERDATAIN);
2404}
2405
2406
2407static void data_io_transfer(struct AdapterCtlBlk *acb,
2408 struct ScsiReqBlk *srb, u16 io_dir)
2409{
2410 struct DeviceCtlBlk *dcb = srb->dcb;
2411 u8 bval;
2412 dprintkdbg(DBG_0,
2413 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
2414 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
2415 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
2416 srb->total_xfer_length, srb->sg_index, srb->sg_count);
2417 if (srb == acb->tmp_srb)
2418 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
2419 if (srb->sg_index >= srb->sg_count) {
2420 /* can't happen? out of bounds error */
2421 return;
2422 }
2423
2424 if (srb->total_xfer_length > DC395x_LASTPIO) {
2425 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2426 /*
2427 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2428 * Maybe, even ABORTXFER would be appropriate
2429 */
2430 if (dma_status & XFERPENDING) {
2431 dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
2432 "Expect trouble!\n");
2433 dump_register_info(acb, dcb, srb);
2434 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2435 }
2436 /* clear_fifo(acb, "IO"); */
2437 /*
2438 * load what physical address of Scatter/Gather list table
2439 * want to be transfer
2440 */
2441 srb->state |= SRB_DATA_XFER;
2442 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2443 if (srb->cmd->use_sg) { /* with S/G */
2444 io_dir |= DMACMD_SG;
2445 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2446 srb->sg_bus_addr +
2447 sizeof(struct SGentry) *
2448 srb->sg_index);
2449 /* load how many bytes in the sg list table */
2450 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2451 ((u32)(srb->sg_count -
2452 srb->sg_index) << 3));
2453 } else { /* without S/G */
2454 io_dir &= ~DMACMD_SG;
2455 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2456 srb->segment_x[0].address);
2457 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2458 srb->segment_x[0].length);
2459 }
2460 /* load total transfer length (24bits) max value 16Mbyte */
2461 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2462 srb->total_xfer_length);
2463 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2464 if (io_dir & DMACMD_DIR) { /* read */
2465 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2466 SCMD_DMA_IN);
2467 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2468 } else {
2469 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2470 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2471 SCMD_DMA_OUT);
2472 }
2473
2474 }
2475#if DC395x_LASTPIO
2476 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
2477 /*
2478 * load what physical address of Scatter/Gather list table
2479 * want to be transfer
2480 */
2481 srb->state |= SRB_DATA_XFER;
2482 /* load total transfer length (24bits) max value 16Mbyte */
2483 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2484 srb->total_xfer_length);
2485 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2486 if (io_dir & DMACMD_DIR) { /* read */
2487 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2488 SCMD_FIFO_IN);
2489 } else { /* write */
2490 int ln = srb->total_xfer_length;
2491 if (srb->dcb->sync_period & WIDE_SYNC)
2492 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2493 CFG2_WIDEFIFO);
2494 dprintkdbg(DBG_PIO,
2495 "data_io_transfer: PIO %i bytes from %p:",
2496 srb->total_xfer_length, srb->virt_addr);
2497
2498 while (srb->total_xfer_length) {
2499 if (debug_enabled(DBG_PIO))
2500 printk(" %02x", (unsigned char) *(srb->virt_addr));
2501
2502 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
2503 *(srb->virt_addr)++);
2504
2505 sg_subtract_one(srb);
2506 }
2507 if (srb->dcb->sync_period & WIDE_SYNC) {
2508 if (ln % 2) {
2509 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2510 if (debug_enabled(DBG_PIO))
2511 printk(" |00");
2512 }
2513 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2514 }
2515 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2516 if (debug_enabled(DBG_PIO))
2517 printk("\n");
2518 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2519 SCMD_FIFO_OUT);
2520 }
2521 }
2522#endif /* DC395x_LASTPIO */
2523 else { /* xfer pad */
2524 u8 data = 0, data2 = 0;
2525 if (srb->sg_count) {
2526 srb->adapter_status = H_OVER_UNDER_RUN;
2527 srb->status |= OVER_RUN;
2528 }
2529 /*
2530 * KG: despite the fact that we are using 16 bits I/O ops
2531 * the SCSI FIFO is only 8 bits according to the docs
2532 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2533 */
2534 if (dcb->sync_period & WIDE_SYNC) {
2535 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
2536 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2537 CFG2_WIDEFIFO);
2538 if (io_dir & DMACMD_DIR) {
2539 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2540 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2541 } else {
2542 /* Danger, Robinson: If you find KGs
2543 * scattered over the wide disk, the driver
2544 * or chip is to blame :-( */
2545 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2546 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
2547 }
2548 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2549 } else {
2550 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2551 /* Danger, Robinson: If you find a collection of Ks on your disk
2552 * something broke :-( */
2553 if (io_dir & DMACMD_DIR)
2554 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2555 else
2556 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2557 }
2558 srb->state |= SRB_XFERPAD;
2559 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2560 /* SCSI command */
2561 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
2562 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
2563 }
2564}
2565
2566
2567static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2568 u16 *pscsi_status)
2569{
2570 dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
2571 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2572 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2573 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
2574 srb->state = SRB_COMPLETED;
2575 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
2576 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2577 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2578}
2579
2580
2581static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2582 u16 *pscsi_status)
2583{
2584 dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
2585 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
2586 srb->state = SRB_STATUS;
2587 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2588 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
2589}
2590
2591
2592/* Check if the message is complete */
2593static inline u8 msgin_completed(u8 * msgbuf, u32 len)
2594{
2595 if (*msgbuf == EXTENDED_MESSAGE) {
2596 if (len < 2)
2597 return 0;
2598 if (len < msgbuf[1] + 2)
2599 return 0;
2600 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
2601 if (len < 2)
2602 return 0;
2603 return 1;
2604}
2605
2606/* reject_msg */
2607static inline void msgin_reject(struct AdapterCtlBlk *acb,
2608 struct ScsiReqBlk *srb)
2609{
2610 srb->msgout_buf[0] = MESSAGE_REJECT;
2611 srb->msg_count = 1;
2612 DC395x_ENABLE_MSGOUT;
2613 srb->state &= ~SRB_MSGIN;
2614 srb->state |= SRB_MSGOUT;
2615 dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
2616 srb->msgin_buf[0],
2617 srb->dcb->target_id, srb->dcb->target_lun);
2618}
2619
2620
2621static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
2622 struct DeviceCtlBlk *dcb, u8 tag)
2623{
2624 struct ScsiReqBlk *srb = NULL;
2625 struct ScsiReqBlk *i;
2626 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
2627 srb->cmd->pid, tag, srb);
2628
2629 if (!(dcb->tag_mask & (1 << tag)))
2630 dprintkl(KERN_DEBUG,
2631 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2632 dcb->tag_mask, tag);
2633
2634 if (list_empty(&dcb->srb_going_list))
2635 goto mingx0;
2636 list_for_each_entry(i, &dcb->srb_going_list, list) {
2637 if (i->tag_number == tag) {
2638 srb = i;
2639 break;
2640 }
2641 }
2642 if (!srb)
2643 goto mingx0;
2644
2645 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
2646 srb->cmd->pid, srb->dcb->target_id, srb->dcb->target_lun);
2647 if (dcb->flag & ABORT_DEV_) {
2648 /*srb->state = SRB_ABORT_SENT; */
2649 enable_msgout_abort(acb, srb);
2650 }
2651
2652 if (!(srb->state & SRB_DISCONNECT))
2653 goto mingx0;
2654
2655 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
2656 srb->state |= dcb->active_srb->state;
2657 srb->state |= SRB_DATA_XFER;
2658 dcb->active_srb = srb;
2659 /* How can we make the DORS happy? */
2660 return srb;
2661
2662 mingx0:
2663 srb = acb->tmp_srb;
2664 srb->state = SRB_UNEXPECT_RESEL;
2665 dcb->active_srb = srb;
2666 srb->msgout_buf[0] = MSG_ABORT_TAG;
2667 srb->msg_count = 1;
2668 DC395x_ENABLE_MSGOUT;
2669 dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
2670 return srb;
2671}
2672
2673
2674static inline void reprogram_regs(struct AdapterCtlBlk *acb,
2675 struct DeviceCtlBlk *dcb)
2676{
2677 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
2678 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
2679 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
2680 set_xfer_rate(acb, dcb);
2681}
2682
2683
2684/* set async transfer mode */
2685static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2686{
2687 struct DeviceCtlBlk *dcb = srb->dcb;
2688 dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
2689 dcb->target_id, dcb->target_lun);
2690
2691 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
2692 dcb->sync_mode |= SYNC_NEGO_DONE;
2693 /*dcb->sync_period &= 0; */
2694 dcb->sync_offset = 0;
2695 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
2696 srb->state &= ~SRB_DO_SYNC_NEGO;
2697 reprogram_regs(acb, dcb);
2698 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2699 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2700 build_wdtr(acb, dcb, srb);
2701 DC395x_ENABLE_MSGOUT;
2702 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
2703 }
2704}
2705
2706
2707/* set sync transfer mode */
2708static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2709{
2710 struct DeviceCtlBlk *dcb = srb->dcb;
2711 u8 bval;
2712 int fact;
2713 dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
2714 "(%02i.%01i MHz) Offset %i\n",
2715 dcb->target_id, srb->msgin_buf[3] << 2,
2716 (250 / srb->msgin_buf[3]),
2717 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
2718 srb->msgin_buf[4]);
2719
2720 if (srb->msgin_buf[4] > 15)
2721 srb->msgin_buf[4] = 15;
2722 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
2723 dcb->sync_offset = 0;
2724 else if (dcb->sync_offset == 0)
2725 dcb->sync_offset = srb->msgin_buf[4];
2726 if (srb->msgin_buf[4] > dcb->sync_offset)
2727 srb->msgin_buf[4] = dcb->sync_offset;
2728 else
2729 dcb->sync_offset = srb->msgin_buf[4];
2730 bval = 0;
2731 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
2732 || dcb->min_nego_period >
2733 clock_period[bval]))
2734 bval++;
2735 if (srb->msgin_buf[3] < clock_period[bval])
2736 dprintkl(KERN_INFO,
2737 "msgin_set_sync: Increase sync nego period to %ins\n",
2738 clock_period[bval] << 2);
2739 srb->msgin_buf[3] = clock_period[bval];
2740 dcb->sync_period &= 0xf0;
2741 dcb->sync_period |= ALT_SYNC | bval;
2742 dcb->min_nego_period = srb->msgin_buf[3];
2743
2744 if (dcb->sync_period & WIDE_SYNC)
2745 fact = 500;
2746 else
2747 fact = 250;
2748
2749 dprintkl(KERN_INFO,
2750 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2751 dcb->target_id, (fact == 500) ? "Wide16" : "",
2752 dcb->min_nego_period << 2, dcb->sync_offset,
2753 (fact / dcb->min_nego_period),
2754 ((fact % dcb->min_nego_period) * 10 +
2755 dcb->min_nego_period / 2) / dcb->min_nego_period);
2756
2757 if (!(srb->state & SRB_DO_SYNC_NEGO)) {
2758 /* Reply with corrected SDTR Message */
2759 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
2760 srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
2761
2762 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
2763 srb->msg_count = 5;
2764 DC395x_ENABLE_MSGOUT;
2765 dcb->sync_mode |= SYNC_NEGO_DONE;
2766 } else {
2767 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2768 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2769 build_wdtr(acb, dcb, srb);
2770 DC395x_ENABLE_MSGOUT;
2771 dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
2772 }
2773 }
2774 srb->state &= ~SRB_DO_SYNC_NEGO;
2775 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
2776
2777 reprogram_regs(acb, dcb);
2778}
2779
2780
2781static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
2782 struct ScsiReqBlk *srb)
2783{
2784 struct DeviceCtlBlk *dcb = srb->dcb;
2785 dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
2786
2787 dcb->sync_period &= ~WIDE_SYNC;
2788 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
2789 dcb->sync_mode |= WIDE_NEGO_DONE;
2790 srb->state &= ~SRB_DO_WIDE_NEGO;
2791 reprogram_regs(acb, dcb);
2792 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2793 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2794 build_sdtr(acb, dcb, srb);
2795 DC395x_ENABLE_MSGOUT;
2796 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2797 }
2798}
2799
2800static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2801{
2802 struct DeviceCtlBlk *dcb = srb->dcb;
2803 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
2804 && acb->config & HCC_WIDE_CARD) ? 1 : 0;
2805 dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
2806
2807 if (srb->msgin_buf[3] > wide)
2808 srb->msgin_buf[3] = wide;
2809 /* Completed */
2810 if (!(srb->state & SRB_DO_WIDE_NEGO)) {
2811 dprintkl(KERN_DEBUG,
2812 "msgin_set_wide: Wide nego initiated <%02i>\n",
2813 dcb->target_id);
2814 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
2815 srb->msg_count = 4;
2816 srb->state |= SRB_DO_WIDE_NEGO;
2817 DC395x_ENABLE_MSGOUT;
2818 }
2819
2820 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
2821 if (srb->msgin_buf[3] > 0)
2822 dcb->sync_period |= WIDE_SYNC;
2823 else
2824 dcb->sync_period &= ~WIDE_SYNC;
2825 srb->state &= ~SRB_DO_WIDE_NEGO;
2826 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2827 dprintkdbg(DBG_1,
2828 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2829 (8 << srb->msgin_buf[3]), dcb->target_id);
2830 reprogram_regs(acb, dcb);
2831 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2832 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2833 build_sdtr(acb, dcb, srb);
2834 DC395x_ENABLE_MSGOUT;
2835 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
2836 }
2837}
2838
2839
2840/*
2841 * extended message codes:
2842 *
2843 * code description
2844 *
2845 * 02h Reserved
2846 * 00h MODIFY DATA POINTER
2847 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
2848 * 03h WIDE DATA TRANSFER REQUEST
2849 * 04h - 7Fh Reserved
2850 * 80h - FFh Vendor specific
2851 */
2852static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2853 u16 *pscsi_status)
2854{
2855 struct DeviceCtlBlk *dcb = acb->active_dcb;
2856 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->pid);
2857
2858 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2859 if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
2860 /* Now eval the msg */
2861 switch (srb->msgin_buf[0]) {
2862 case DISCONNECT:
2863 srb->state = SRB_DISCONNECT;
2864 break;
2865
2866 case SIMPLE_QUEUE_TAG:
2867 case HEAD_OF_QUEUE_TAG:
2868 case ORDERED_QUEUE_TAG:
2869 srb =
2870 msgin_qtag(acb, dcb,
2871 srb->msgin_buf[1]);
2872 break;
2873
2874 case MESSAGE_REJECT:
2875 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2876 DO_CLRATN | DO_DATALATCH);
2877 /* A sync nego message was rejected ! */
2878 if (srb->state & SRB_DO_SYNC_NEGO) {
2879 msgin_set_async(acb, srb);
2880 break;
2881 }
2882 /* A wide nego message was rejected ! */
2883 if (srb->state & SRB_DO_WIDE_NEGO) {
2884 msgin_set_nowide(acb, srb);
2885 break;
2886 }
2887 enable_msgout_abort(acb, srb);
2888 /*srb->state |= SRB_ABORT_SENT */
2889 break;
2890
2891 case EXTENDED_MESSAGE:
2892 /* SDTR */
2893 if (srb->msgin_buf[1] == 3
2894 && srb->msgin_buf[2] == EXTENDED_SDTR) {
2895 msgin_set_sync(acb, srb);
2896 break;
2897 }
2898 /* WDTR */
2899 if (srb->msgin_buf[1] == 2
2900 && srb->msgin_buf[2] == EXTENDED_WDTR
2901 && srb->msgin_buf[3] <= 2) { /* sanity check ... */
2902 msgin_set_wide(acb, srb);
2903 break;
2904 }
2905 msgin_reject(acb, srb);
2906 break;
2907
2908 case MSG_IGNOREWIDE:
2909 /* Discard wide residual */
2910 dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
2911 break;
2912
2913 case COMMAND_COMPLETE:
2914 /* nothing has to be done */
2915 break;
2916
2917 case SAVE_POINTERS:
2918 /*
2919 * SAVE POINTER may be ignored as we have the struct
2920 * ScsiReqBlk* associated with the scsi command.
2921 */
2922 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2923 "SAVE POINTER rem=%i Ignore\n",
2924 srb->cmd->pid, srb->total_xfer_length);
2925 break;
2926
2927 case RESTORE_POINTERS:
2928 dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
2929 break;
2930
2931 case ABORT:
2932 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2933 "<%02i-%i> ABORT msg\n",
2934 srb->cmd->pid, dcb->target_id,
2935 dcb->target_lun);
2936 dcb->flag |= ABORT_DEV_;
2937 enable_msgout_abort(acb, srb);
2938 break;
2939
2940 default:
2941 /* reject unknown messages */
2942 if (srb->msgin_buf[0] & IDENTIFY_BASE) {
2943 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
2944 srb->msg_count = 1;
2945 srb->msgout_buf[0] = dcb->identify_msg;
2946 DC395x_ENABLE_MSGOUT;
2947 srb->state |= SRB_MSGOUT;
2948 /*break; */
2949 }
2950 msgin_reject(acb, srb);
2951 }
2952
2953 /* Clear counter and MsgIn state */
2954 srb->state &= ~SRB_MSGIN;
2955 acb->msg_len = 0;
2956 }
2957 *pscsi_status = PH_BUS_FREE;
2958 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
2959 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2960}
2961
2962
2963static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2964 u16 *pscsi_status)
2965{
2966 dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->pid);
2967 clear_fifo(acb, "msgin_phase1");
2968 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2969 if (!(srb->state & SRB_MSGIN)) {
2970 srb->state &= ~SRB_DISCONNECT;
2971 srb->state |= SRB_MSGIN;
2972 }
2973 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2974 /* SCSI command */
2975 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
2976}
2977
2978
2979static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2980 u16 *pscsi_status)
2981{
2982}
2983
2984
2985static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2986 u16 *pscsi_status)
2987{
2988}
2989
2990
2991static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
2992{
2993 struct DeviceCtlBlk *i;
2994
2995 /* set all lun device's period, offset */
2996 if (dcb->identify_msg & 0x07)
2997 return;
2998
2999 if (acb->scan_devices) {
3000 current_sync_offset = dcb->sync_offset;
3001 return;
3002 }
3003
3004 list_for_each_entry(i, &acb->dcb_list, list)
3005 if (i->target_id == dcb->target_id) {
3006 i->sync_period = dcb->sync_period;
3007 i->sync_offset = dcb->sync_offset;
3008 i->sync_mode = dcb->sync_mode;
3009 i->min_nego_period = dcb->min_nego_period;
3010 }
3011}
3012
3013
3014static void disconnect(struct AdapterCtlBlk *acb)
3015{
3016 struct DeviceCtlBlk *dcb = acb->active_dcb;
3017 struct ScsiReqBlk *srb;
3018
3019 if (!dcb) {
3020 dprintkl(KERN_ERR, "disconnect: No such device\n");
3021 udelay(500);
3022 /* Suspend queue for a while */
3023 acb->scsi_host->last_reset =
3024 jiffies + HZ / 2 +
3025 HZ * acb->eeprom.delay_time;
3026 clear_fifo(acb, "disconnectEx");
3027 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3028 return;
3029 }
3030 srb = dcb->active_srb;
3031 acb->active_dcb = NULL;
3032 dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->pid);
3033
3034 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3035 clear_fifo(acb, "disconnect");
3036 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3037 if (srb->state & SRB_UNEXPECT_RESEL) {
3038 dprintkl(KERN_ERR,
3039 "disconnect: Unexpected reselection <%02i-%i>\n",
3040 dcb->target_id, dcb->target_lun);
3041 srb->state = 0;
3042 waiting_process_next(acb);
3043 } else if (srb->state & SRB_ABORT_SENT) {
3044 dcb->flag &= ~ABORT_DEV_;
3045 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
3046 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
3047 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
3048 waiting_process_next(acb);
3049 } else {
3050 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
3051 || !(srb->
3052 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
3053 /*
3054 * Selection time out
3055 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
3056 */
3057 /* Unexp. Disc / Sel Timeout */
3058 if (srb->state != SRB_START_
3059 && srb->state != SRB_MSGOUT) {
3060 srb->state = SRB_READY;
3061 dprintkl(KERN_DEBUG,
3062 "disconnect: (pid#%li) Unexpected\n",
3063 srb->cmd->pid);
3064 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
3065 goto disc1;
3066 } else {
3067 /* Normal selection timeout */
3068 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
3069 "<%02i-%i> SelTO\n", srb->cmd->pid,
3070 dcb->target_id, dcb->target_lun);
3071 if (srb->retry_count++ > DC395x_MAX_RETRIES
3072 || acb->scan_devices) {
3073 srb->target_status =
3074 SCSI_STAT_SEL_TIMEOUT;
3075 goto disc1;
3076 }
3077 free_tag(dcb, srb);
3078 srb_going_to_waiting_move(dcb, srb);
3079 dprintkdbg(DBG_KG,
3080 "disconnect: (pid#%li) Retry\n",
3081 srb->cmd->pid);
3082 waiting_set_timer(acb, HZ / 20);
3083 }
3084 } else if (srb->state & SRB_DISCONNECT) {
3085 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
3086 /*
3087 * SRB_DISCONNECT (This is what we expect!)
3088 */
3089 if (bval & 0x40) {
3090 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
3091 " 0x%02x: ACK set! Other controllers?\n",
3092 bval);
3093 /* It could come from another initiator, therefore don't do much ! */
3094 } else
3095 waiting_process_next(acb);
3096 } else if (srb->state & SRB_COMPLETED) {
3097 disc1:
3098 /*
3099 ** SRB_COMPLETED
3100 */
3101 free_tag(dcb, srb);
3102 dcb->active_srb = NULL;
3103 srb->state = SRB_FREE;
3104 srb_done(acb, dcb, srb);
3105 }
3106 }
3107}
3108
3109
3110static void reselect(struct AdapterCtlBlk *acb)
3111{
3112 struct DeviceCtlBlk *dcb = acb->active_dcb;
3113 struct ScsiReqBlk *srb = NULL;
3114 u16 rsel_tar_lun_id;
3115 u8 id, lun;
3116 u8 arblostflag = 0;
3117 dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
3118
3119 clear_fifo(acb, "reselect");
3120 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3121 /* Read Reselected Target ID and LUN */
3122 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3123 if (dcb) { /* Arbitration lost but Reselection win */
3124 srb = dcb->active_srb;
3125 if (!srb) {
3126 dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
3127 "but active_srb == NULL\n");
3128 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3129 return;
3130 }
3131 /* Why the if ? */
3132 if (!acb->scan_devices) {
3133 dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
3134 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
3135 srb->cmd->pid, dcb->target_id,
3136 dcb->target_lun, rsel_tar_lun_id,
3137 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3138 arblostflag = 1;
3139 /*srb->state |= SRB_DISCONNECT; */
3140
3141 srb->state = SRB_READY;
3142 free_tag(dcb, srb);
3143 srb_going_to_waiting_move(dcb, srb);
3144 waiting_set_timer(acb, HZ / 20);
3145
3146 /* return; */
3147 }
3148 }
3149 /* Read Reselected Target Id and LUN */
3150 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3151 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
3152 "Got %i!\n", rsel_tar_lun_id);
3153 id = rsel_tar_lun_id & 0xff;
3154 lun = (rsel_tar_lun_id >> 8) & 7;
3155 dcb = find_dcb(acb, id, lun);
3156 if (!dcb) {
3157 dprintkl(KERN_ERR, "reselect: From non existent device "
3158 "<%02i-%i>\n", id, lun);
3159 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3160 return;
3161 }
3162 acb->active_dcb = dcb;
3163
3164 if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3165 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
3166 "disconnection? <%02i-%i>\n",
3167 dcb->target_id, dcb->target_lun);
3168
3169 if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
3170 srb = acb->tmp_srb;
3171 dcb->active_srb = srb;
3172 } else {
3173 /* There can be only one! */
3174 srb = dcb->active_srb;
3175 if (!srb || !(srb->state & SRB_DISCONNECT)) {
3176 /*
3177 * abort command
3178 */
3179 dprintkl(KERN_DEBUG,
3180 "reselect: w/o disconnected cmds <%02i-%i>\n",
3181 dcb->target_id, dcb->target_lun);
3182 srb = acb->tmp_srb;
3183 srb->state = SRB_UNEXPECT_RESEL;
3184 dcb->active_srb = srb;
3185 enable_msgout_abort(acb, srb);
3186 } else {
3187 if (dcb->flag & ABORT_DEV_) {
3188 /*srb->state = SRB_ABORT_SENT; */
3189 enable_msgout_abort(acb, srb);
3190 } else
3191 srb->state = SRB_DATA_XFER;
3192
3193 }
3194 }
3195 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3196
3197 /* Program HA ID, target ID, period and offset */
3198 dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
3199 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
3200 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
3201 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
3202 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
3203 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3204 /* SCSI command */
3205 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3206}
3207
3208
3209static inline u8 tagq_blacklist(char *name)
3210{
3211#ifndef DC395x_NO_TAGQ
3212#if 0
3213 u8 i;
3214 for (i = 0; i < BADDEVCNT; i++)
3215 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
3216 return 1;
3217#endif
3218 return 0;
3219#else
3220 return 1;
3221#endif
3222}
3223
3224
3225static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
3226{
3227 /* Check for SCSI format (ANSI and Response data format) */
3228 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
3229 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
3230 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
3231 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3232 /* ((dcb->dev_type == TYPE_DISK)
3233 || (dcb->dev_type == TYPE_MOD)) && */
3234 !tagq_blacklist(((char *)ptr) + 8)) {
3235 if (dcb->max_command == 1)
3236 dcb->max_command =
3237 dcb->acb->tag_max_num;
3238 dcb->sync_mode |= EN_TAG_QUEUEING;
3239 /*dcb->tag_mask = 0; */
3240 } else
3241 dcb->max_command = 1;
3242 }
3243}
3244
3245
3246static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3247 struct ScsiInqData *ptr)
3248{
3249 u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
3250 dcb->dev_type = bval1;
3251 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3252 disc_tagq_set(dcb, ptr);
3253}
3254
3255
3256/* unmap mapped pci regions from SRB */
3257static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3258{
3259 struct scsi_cmnd *cmd = srb->cmd;
3260 enum dma_data_direction dir = cmd->sc_data_direction;
3261 if (cmd->use_sg && dir != PCI_DMA_NONE) {
3262 int i;
3263 /* unmap DC395x SG list */
3264 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
3265 srb->sg_bus_addr, SEGMENTX_LEN);
3266 pci_unmap_single(acb->dev, srb->sg_bus_addr,
3267 SEGMENTX_LEN,
3268 PCI_DMA_TODEVICE);
3269 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3270 cmd->use_sg, cmd->request_buffer);
3271 /* unmap the sg segments */
3272 for (i = 0; i < srb->sg_count; i++)
3273 kunmap(virt_to_page(srb->virt_map[i]));
3274 pci_unmap_sg(acb->dev,
3275 (struct scatterlist *)cmd->request_buffer,
3276 cmd->use_sg, dir);
3277 } else if (cmd->request_buffer && dir != PCI_DMA_NONE) {
3278 dprintkdbg(DBG_SG, "pci_unmap_srb: buffer=%08x(%05x)\n",
3279 srb->segment_x[0].address, cmd->request_bufflen);
3280 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3281 cmd->request_bufflen, dir);
3282 }
3283}
3284
3285
3286/* unmap mapped pci sense buffer from SRB */
3287static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3288 struct ScsiReqBlk *srb)
3289{
3290 if (!(srb->flag & AUTO_REQSENSE))
3291 return;
3292 /* Unmap sense buffer */
3293 dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
3294 srb->segment_x[0].address);
3295 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3296 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
3297 /* Restore SG stuff */
3298 srb->total_xfer_length = srb->xferred;
3299 srb->segment_x[0].address =
3300 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
3301 srb->segment_x[0].length =
3302 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
3303}
3304
3305
3306/*
3307 * Complete execution of a SCSI command
3308 * Signal completion to the generic SCSI driver
3309 */
3310static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3311 struct ScsiReqBlk *srb)
3312{
3313 u8 tempcnt, status;
3314 struct scsi_cmnd *cmd = srb->cmd;
3315 struct ScsiInqData *ptr;
3316 enum dma_data_direction dir = cmd->sc_data_direction;
3317
3318 if (cmd->use_sg) {
3319 struct scatterlist* sg = (struct scatterlist *)cmd->request_buffer;
3320 ptr = (struct ScsiInqData *)(srb->virt_map[0] + sg->offset);
3321 } else {
3322 ptr = (struct ScsiInqData *)(cmd->request_buffer);
3323 }
3324
3325 dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->pid,
3326 srb->cmd->device->id, srb->cmd->device->lun);
3327 dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p addr=%p\n",
3328 srb, cmd->use_sg, srb->sg_index, srb->sg_count,
3329 cmd->request_buffer, ptr);
3330 status = srb->target_status;
3331 if (srb->flag & AUTO_REQSENSE) {
3332 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
3333 pci_unmap_srb_sense(acb, srb);
3334 /*
3335 ** target status..........................
3336 */
3337 srb->flag &= ~AUTO_REQSENSE;
3338 srb->adapter_status = 0;
3339 srb->target_status = CHECK_CONDITION << 1;
3340 if (debug_enabled(DBG_1)) {
3341 switch (cmd->sense_buffer[2] & 0x0f) {
3342 case NOT_READY:
3343 dprintkl(KERN_DEBUG,
3344 "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3345 cmd->cmnd[0], dcb->target_id,
3346 dcb->target_lun, status, acb->scan_devices);
3347 break;
3348 case UNIT_ATTENTION:
3349 dprintkl(KERN_DEBUG,
3350 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3351 cmd->cmnd[0], dcb->target_id,
3352 dcb->target_lun, status, acb->scan_devices);
3353 break;
3354 case ILLEGAL_REQUEST:
3355 dprintkl(KERN_DEBUG,
3356 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3357 cmd->cmnd[0], dcb->target_id,
3358 dcb->target_lun, status, acb->scan_devices);
3359 break;
3360 case MEDIUM_ERROR:
3361 dprintkl(KERN_DEBUG,
3362 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3363 cmd->cmnd[0], dcb->target_id,
3364 dcb->target_lun, status, acb->scan_devices);
3365 break;
3366 case HARDWARE_ERROR:
3367 dprintkl(KERN_DEBUG,
3368 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3369 cmd->cmnd[0], dcb->target_id,
3370 dcb->target_lun, status, acb->scan_devices);
3371 break;
3372 }
3373 if (cmd->sense_buffer[7] >= 6)
3374 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3375 "(0x%08x 0x%08x)\n",
3376 cmd->sense_buffer[2], cmd->sense_buffer[12],
3377 cmd->sense_buffer[13],
3378 *((unsigned int *)(cmd->sense_buffer + 3)),
3379 *((unsigned int *)(cmd->sense_buffer + 8)));
3380 else
3381 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3382 cmd->sense_buffer[2],
3383 *((unsigned int *)(cmd->sense_buffer + 3)));
3384 }
3385
3386 if (status == (CHECK_CONDITION << 1)) {
3387 cmd->result = DID_BAD_TARGET << 16;
3388 goto ckc_e;
3389 }
3390 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
3391
3392 if (srb->total_xfer_length
3393 && srb->total_xfer_length >= cmd->underflow)
3394 cmd->result =
3395 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3396 srb->end_message, CHECK_CONDITION);
3397 /*SET_RES_DID(cmd->result,DID_OK) */
3398 else
3399 cmd->result =
3400 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3401 srb->end_message, CHECK_CONDITION);
3402
3403 goto ckc_e;
3404 }
3405
3406/*************************************************************/
3407 if (status) {
3408 /*
3409 * target status..........................
3410 */
3411 if (status_byte(status) == CHECK_CONDITION) {
3412 request_sense(acb, dcb, srb);
3413 return;
3414 } else if (status_byte(status) == QUEUE_FULL) {
3415 tempcnt = (u8)list_size(&dcb->srb_going_list);
3416 dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3417 dcb->target_id, dcb->target_lun, tempcnt);
3418 if (tempcnt > 1)
3419 tempcnt--;
3420 dcb->max_command = tempcnt;
3421 free_tag(dcb, srb);
3422 srb_going_to_waiting_move(dcb, srb);
3423 waiting_set_timer(acb, HZ / 20);
3424 srb->adapter_status = 0;
3425 srb->target_status = 0;
3426 return;
3427 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
3428 srb->adapter_status = H_SEL_TIMEOUT;
3429 srb->target_status = 0;
3430 cmd->result = DID_NO_CONNECT << 16;
3431 } else {
3432 srb->adapter_status = 0;
3433 SET_RES_DID(cmd->result, DID_ERROR);
3434 SET_RES_MSG(cmd->result, srb->end_message);
3435 SET_RES_TARGET(cmd->result, status);
3436
3437 }
3438 } else {
3439 /*
3440 ** process initiator status..........................
3441 */
3442 status = srb->adapter_status;
3443 if (status & H_OVER_UNDER_RUN) {
3444 srb->target_status = 0;
3445 SET_RES_DID(cmd->result, DID_OK);
3446 SET_RES_MSG(cmd->result, srb->end_message);
3447 } else if (srb->status & PARITY_ERROR) {
3448 SET_RES_DID(cmd->result, DID_PARITY);
3449 SET_RES_MSG(cmd->result, srb->end_message);
3450 } else { /* No error */
3451
3452 srb->adapter_status = 0;
3453 srb->target_status = 0;
3454 SET_RES_DID(cmd->result, DID_OK);
3455 }
3456 }
3457
3458 if (dir != PCI_DMA_NONE) {
3459 if (cmd->use_sg)
3460 pci_dma_sync_sg_for_cpu(acb->dev,
3461 (struct scatterlist *)cmd->
3462 request_buffer, cmd->use_sg, dir);
3463 else if (cmd->request_buffer)
3464 pci_dma_sync_single_for_cpu(acb->dev,
3465 srb->segment_x[0].address,
3466 cmd->request_bufflen, dir);
3467 }
3468
3469 if ((cmd->result & RES_DID) == 0 && cmd->cmnd[0] == INQUIRY
3470 && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8
3471 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
3472 dcb->inquiry7 = ptr->Flags;
3473/* Check Error Conditions */
3474 ckc_e:
3475
3476 /*if( srb->cmd->cmnd[0] == INQUIRY && */
3477 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3478 if (cmd->cmnd[0] == INQUIRY && (cmd->result == (DID_OK << 16)
3479 || status_byte(cmd->
3480 result) &
3481 CHECK_CONDITION)) {
3482
3483 if (!dcb->init_tcq_flag) {
3484 add_dev(acb, dcb, ptr);
3485 dcb->init_tcq_flag = 1;
3486 }
3487
3488 }
3489
3490
3491 /* Here is the info for Doug Gilbert's sg3 ... */
3492 cmd->resid = srb->total_xfer_length;
3493 /* This may be interpreted by sb. or not ... */
3494 cmd->SCp.this_residual = srb->total_xfer_length;
3495 cmd->SCp.buffers_residual = 0;
3496 if (debug_enabled(DBG_KG)) {
3497 if (srb->total_xfer_length)
3498 dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
3499 "cmnd=0x%02x Missed %i bytes\n",
3500 cmd->pid, cmd->device->id, cmd->device->lun,
3501 cmd->cmnd[0], srb->total_xfer_length);
3502 }
3503
3504 srb_going_remove(dcb, srb);
3505 /* Add to free list */
3506 if (srb == acb->tmp_srb)
3507 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3508 else {
3509 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
3510 cmd->pid, cmd->result);
3511 srb_free_insert(acb, srb);
3512 }
3513 pci_unmap_srb(acb, srb);
3514
3515 cmd->scsi_done(cmd);
3516 waiting_process_next(acb);
3517}
3518
3519
3520/* abort all cmds in our queues */
3521static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
3522 struct scsi_cmnd *cmd, u8 force)
3523{
3524 struct DeviceCtlBlk *dcb;
3525 dprintkl(KERN_INFO, "doing_srb_done: pids ");
3526
3527 list_for_each_entry(dcb, &acb->dcb_list, list) {
3528 struct ScsiReqBlk *srb;
3529 struct ScsiReqBlk *tmp;
3530 struct scsi_cmnd *p;
3531
3532 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
3533 enum dma_data_direction dir;
3534 int result;
3535
3536 p = srb->cmd;
3537 dir = p->sc_data_direction;
3538 result = MK_RES(0, did_flag, 0, 0);
3539 printk("G:%li(%02i-%i) ", p->pid,
3540 p->device->id, p->device->lun);
3541 srb_going_remove(dcb, srb);
3542 free_tag(dcb, srb);
3543 srb_free_insert(acb, srb);
3544 p->result = result;
3545 pci_unmap_srb_sense(acb, srb);
3546 pci_unmap_srb(acb, srb);
3547 if (force) {
3548 /* For new EH, we normally don't need to give commands back,
3549 * as they all complete or all time out */
3550 p->scsi_done(p);
3551 }
3552 }
3553 if (!list_empty(&dcb->srb_going_list))
3554 dprintkl(KERN_DEBUG,
3555 "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3556 dcb->target_id, dcb->target_lun);
3557 if (dcb->tag_mask)
3558 dprintkl(KERN_DEBUG,
3559 "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3560 dcb->target_id, dcb->target_lun,
3561 dcb->tag_mask);
3562
3563 /* Waiting queue */
3564 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
3565 int result;
3566 p = srb->cmd;
3567
3568 result = MK_RES(0, did_flag, 0, 0);
3569 printk("W:%li<%02i-%i>", p->pid, p->device->id,
3570 p->device->lun);
3571 srb_waiting_remove(dcb, srb);
3572 srb_free_insert(acb, srb);
3573 p->result = result;
3574 pci_unmap_srb_sense(acb, srb);
3575 pci_unmap_srb(acb, srb);
3576 if (force) {
3577 /* For new EH, we normally don't need to give commands back,
3578 * as they all complete or all time out */
3579 cmd->scsi_done(cmd);
3580 }
3581 }
3582 if (!list_empty(&dcb->srb_waiting_list))
3583 dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
3584 list_size(&dcb->srb_waiting_list), dcb->target_id,
3585 dcb->target_lun);
3586 dcb->flag &= ~ABORT_DEV_;
3587 }
3588 printk("\n");
3589}
3590
3591
3592static void reset_scsi_bus(struct AdapterCtlBlk *acb)
3593{
3594 dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
3595 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
3596 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
3597
3598 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
3599 /* nothing */;
3600}
3601
3602
3603static void set_basic_config(struct AdapterCtlBlk *acb)
3604{
3605 u8 bval;
3606 u16 wval;
3607 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
3608 if (acb->config & HCC_PARITY)
3609 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
3610 else
3611 bval = PHASELATCH | INITIATOR | BLOCKRST;
3612
3613 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
3614
3615 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3616 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
3617 /* program Host ID */
3618 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
3619 /* set ansynchronous transfer */
3620 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
3621 /* Turn LED control off */
3622 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
3623 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
3624 /* DMA config */
3625 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
3626 wval |=
3627 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
3628 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
3629 /* Clear pending interrupt status */
3630 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
3631 /* Enable SCSI interrupt */
3632 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
3633 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
3634 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3635 );
3636}
3637
3638
3639static void scsi_reset_detect(struct AdapterCtlBlk *acb)
3640{
3641 dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
3642 /* delay half a second */
3643 if (timer_pending(&acb->waiting_timer))
3644 del_timer(&acb->waiting_timer);
3645
3646 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
3647 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
3648 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3649 udelay(500);
3650 /* Maybe we locked up the bus? Then lets wait even longer ... */
3651 acb->scsi_host->last_reset =
3652 jiffies + 5 * HZ / 2 +
3653 HZ * acb->eeprom.delay_time;
3654
3655 clear_fifo(acb, "scsi_reset_detect");
3656 set_basic_config(acb);
3657 /*1.25 */
3658 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3659
3660 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
3661 acb->acb_flag |= RESET_DONE;
3662 } else {
3663 acb->acb_flag |= RESET_DETECT;
3664 reset_dev_param(acb);
3665 doing_srb_done(acb, DID_RESET, NULL, 1);
3666 /*DC395x_RecoverSRB( acb ); */
3667 acb->active_dcb = NULL;
3668 acb->acb_flag = 0;
3669 waiting_process_next(acb);
3670 }
3671}
3672
3673
3674static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3675 struct ScsiReqBlk *srb)
3676{
3677 struct scsi_cmnd *cmd = srb->cmd;
3678 dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
3679 cmd->pid, cmd->device->id, cmd->device->lun);
3680
3681 srb->flag |= AUTO_REQSENSE;
3682 srb->adapter_status = 0;
3683 srb->target_status = 0;
3684
3685 /* KG: Can this prevent crap sense data ? */
3686 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3687
3688 /* Save some data */
3689 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
3690 srb->segment_x[0].address;
3691 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
3692 srb->segment_x[0].length;
3693 srb->xferred = srb->total_xfer_length;
3694 /* srb->segment_x : a one entry of S/G list table */
3695 srb->total_xfer_length = sizeof(cmd->sense_buffer);
3696 srb->segment_x[0].length = sizeof(cmd->sense_buffer);
3697 /* Map sense buffer */
3698 srb->segment_x[0].address =
3699 pci_map_single(acb->dev, cmd->sense_buffer,
3700 sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE);
3701 dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
3702 cmd->sense_buffer, srb->segment_x[0].address,
3703 sizeof(cmd->sense_buffer));
3704 srb->sg_count = 1;
3705 srb->sg_index = 0;
3706
3707 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
3708 dprintkl(KERN_DEBUG,
3709 "request_sense: (pid#%li) failed <%02i-%i>\n",
3710 srb->cmd->pid, dcb->target_id, dcb->target_lun);
3711 srb_going_to_waiting_move(dcb, srb);
3712 waiting_set_timer(acb, HZ / 100);
3713 }
3714}
3715
3716
3717/**
3718 * device_alloc - Allocate a new device instance. This create the
3719 * devices instance and sets up all the data items. The adapter
3720 * instance is required to obtain confiuration information for this
3721 * device. This does *not* add this device to the adapters device
3722 * list.
3723 *
3724 * @acb: The adapter to obtain configuration information from.
3725 * @target: The target for the new device.
3726 * @lun: The lun for the new device.
3727 *
3728 * Return the new device if succesfull or NULL on failure.
3729 **/
3730static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
3731 u8 target, u8 lun)
3732{
3733 struct NvRamType *eeprom = &acb->eeprom;
3734 u8 period_index = eeprom->target[target].period & 0x07;
3735 struct DeviceCtlBlk *dcb;
3736
3737 dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
3738 dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
3739 if (!dcb)
3740 return NULL;
3741 dcb->acb = NULL;
3742 INIT_LIST_HEAD(&dcb->srb_going_list);
3743 INIT_LIST_HEAD(&dcb->srb_waiting_list);
3744 dcb->active_srb = NULL;
3745 dcb->tag_mask = 0;
3746 dcb->max_command = 1;
3747 dcb->target_id = target;
3748 dcb->target_lun = lun;
3749#ifndef DC395x_NO_DISCONNECT
3750 dcb->identify_msg =
3751 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
3752#else
3753 dcb->identify_msg = IDENTIFY(0, lun);
3754#endif
3755 dcb->dev_mode = eeprom->target[target].cfg0;
3756 dcb->inquiry7 = 0;
3757 dcb->sync_mode = 0;
3758 dcb->min_nego_period = clock_period[period_index];
3759 dcb->sync_period = 0;
3760 dcb->sync_offset = 0;
3761 dcb->flag = 0;
3762
3763#ifndef DC395x_NO_WIDE
3764 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
3765 && (acb->config & HCC_WIDE_CARD))
3766 dcb->sync_mode |= WIDE_NEGO_ENABLE;
3767#endif
3768#ifndef DC395x_NO_SYNC
3769 if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
3770 if (!(lun) || current_sync_offset)
3771 dcb->sync_mode |= SYNC_NEGO_ENABLE;
3772#endif
3773 if (dcb->target_lun != 0) {
3774 /* Copy settings */
3775 struct DeviceCtlBlk *p;
3776 list_for_each_entry(p, &acb->dcb_list, list)
3777 if (p->target_id == dcb->target_id)
3778 break;
3779 dprintkdbg(DBG_1,
3780 "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3781 dcb->target_id, dcb->target_lun,
3782 p->target_id, p->target_lun);
3783 dcb->sync_mode = p->sync_mode;
3784 dcb->sync_period = p->sync_period;
3785 dcb->min_nego_period = p->min_nego_period;
3786 dcb->sync_offset = p->sync_offset;
3787 dcb->inquiry7 = p->inquiry7;
3788 }
3789 return dcb;
3790}
3791
3792
3793/**
3794 * adapter_add_device - Adds the device instance to the adaptor instance.
3795 *
3796 * @acb: The adapter device to be updated
3797 * @dcb: A newly created and intialised device instance to add.
3798 **/
3799static void adapter_add_device(struct AdapterCtlBlk *acb,
3800 struct DeviceCtlBlk *dcb)
3801{
3802 /* backpointer to adapter */
3803 dcb->acb = acb;
3804
3805 /* set run_robin to this device if it is currently empty */
3806 if (list_empty(&acb->dcb_list))
3807 acb->dcb_run_robin = dcb;
3808
3809 /* add device to list */
3810 list_add_tail(&dcb->list, &acb->dcb_list);
3811
3812 /* update device maps */
3813 acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
3814 acb->children[dcb->target_id][dcb->target_lun] = dcb;
3815}
3816
3817
3818/**
3819 * adapter_remove_device - Removes the device instance from the adaptor
3820 * instance. The device instance is not check in any way or freed by this.
3821 * The caller is expected to take care of that. This will simply remove the
3822 * device from the adapters data strcutures.
3823 *
3824 * @acb: The adapter device to be updated
3825 * @dcb: A device that has previously been added to the adapter.
3826 **/
3827static void adapter_remove_device(struct AdapterCtlBlk *acb,
3828 struct DeviceCtlBlk *dcb)
3829{
3830 struct DeviceCtlBlk *i;
3831 struct DeviceCtlBlk *tmp;
3832 dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
3833 dcb->target_id, dcb->target_lun);
3834
3835 /* fix up any pointers to this device that we have in the adapter */
3836 if (acb->active_dcb == dcb)
3837 acb->active_dcb = NULL;
3838 if (acb->dcb_run_robin == dcb)
3839 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
3840
3841 /* unlink from list */
3842 list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
3843 if (dcb == i) {
3844 list_del(&i->list);
3845 break;
3846 }
3847
3848 /* clear map and children */
3849 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
3850 acb->children[dcb->target_id][dcb->target_lun] = NULL;
3851 dcb->acb = NULL;
3852}
3853
3854
3855/**
3856 * adapter_remove_and_free_device - Removes a single device from the adapter
3857 * and then frees the device information.
3858 *
3859 * @acb: The adapter device to be updated
3860 * @dcb: A device that has previously been added to the adapter.
3861 */
3862static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
3863 struct DeviceCtlBlk *dcb)
3864{
3865 if (list_size(&dcb->srb_going_list) > 1) {
3866 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
3867 "Won't remove because of %i active requests.\n",
3868 dcb->target_id, dcb->target_lun,
3869 list_size(&dcb->srb_going_list));
3870 return;
3871 }
3872 adapter_remove_device(acb, dcb);
3873 kfree(dcb);
3874}
3875
3876
3877/**
3878 * adapter_remove_and_free_all_devices - Removes and frees all of the
3879 * devices associated with the specified adapter.
3880 *
3881 * @acb: The adapter from which all devices should be removed.
3882 **/
3883static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
3884{
3885 struct DeviceCtlBlk *dcb;
3886 struct DeviceCtlBlk *tmp;
3887 dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
3888 list_size(&acb->dcb_list));
3889
3890 list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
3891 adapter_remove_and_free_device(acb, dcb);
3892}
3893
3894
3895/**
3896 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3897 * scsi device that we need to deal with. We allocate a new device and then
3898 * insert that device into the adapters device list.
3899 *
3900 * @scsi_device: The new scsi device that we need to handle.
3901 **/
3902static int dc395x_slave_alloc(struct scsi_device *scsi_device)
3903{
3904 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3905 struct DeviceCtlBlk *dcb;
3906
3907 dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
3908 if (!dcb)
3909 return -ENOMEM;
3910 adapter_add_device(acb, dcb);
3911
3912 return 0;
3913}
3914
3915
3916/**
3917 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3918 * device that is going away.
3919 *
3920 * @scsi_device: The new scsi device that we need to handle.
3921 **/
3922static void dc395x_slave_destroy(struct scsi_device *scsi_device)
3923{
3924 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3925 struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
3926 if (dcb)
3927 adapter_remove_and_free_device(acb, dcb);
3928}
3929
3930
3931
3932
3933/**
3934 * trms1040_wait_30us: wait for 30 us
3935 *
3936 * Waits for 30us (using the chip by the looks of it..)
3937 *
3938 * @io_port: base I/O address
3939 **/
3940static void __devinit trms1040_wait_30us(unsigned long io_port)
3941{
3942 /* ScsiPortStallExecution(30); wait 30 us */
3943 outb(5, io_port + TRM_S1040_GEN_TIMER);
3944 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
3945 /* nothing */ ;
3946}
3947
3948
3949/**
3950 * trms1040_write_cmd - write the secified command and address to
3951 * chip
3952 *
3953 * @io_port: base I/O address
3954 * @cmd: SB + op code (command) to send
3955 * @addr: address to send
3956 **/
3957static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
3958{
3959 int i;
3960 u8 send_data;
3961
3962 /* program SB + OP code */
3963 for (i = 0; i < 3; i++, cmd <<= 1) {
3964 send_data = NVR_SELECT;
3965 if (cmd & 0x04) /* Start from bit 2 */
3966 send_data |= NVR_BITOUT;
3967
3968 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3969 trms1040_wait_30us(io_port);
3970 outb((send_data | NVR_CLOCK),
3971 io_port + TRM_S1040_GEN_NVRAM);
3972 trms1040_wait_30us(io_port);
3973 }
3974
3975 /* send address */
3976 for (i = 0; i < 7; i++, addr <<= 1) {
3977 send_data = NVR_SELECT;
3978 if (addr & 0x40) /* Start from bit 6 */
3979 send_data |= NVR_BITOUT;
3980
3981 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3982 trms1040_wait_30us(io_port);
3983 outb((send_data | NVR_CLOCK),
3984 io_port + TRM_S1040_GEN_NVRAM);
3985 trms1040_wait_30us(io_port);
3986 }
3987 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
3988 trms1040_wait_30us(io_port);
3989}
3990
3991
3992/**
3993 * trms1040_set_data - store a single byte in the eeprom
3994 *
3995 * Called from write all to write a single byte into the SSEEPROM
3996 * Which is done one bit at a time.
3997 *
3998 * @io_port: base I/O address
3999 * @addr: offset into EEPROM
4000 * @byte: bytes to write
4001 **/
4002static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
4003{
4004 int i;
4005 u8 send_data;
4006
4007 /* Send write command & address */
4008 trms1040_write_cmd(io_port, 0x05, addr);
4009
4010 /* Write data */
4011 for (i = 0; i < 8; i++, byte <<= 1) {
4012 send_data = NVR_SELECT;
4013 if (byte & 0x80) /* Start from bit 7 */
4014 send_data |= NVR_BITOUT;
4015
4016 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4017 trms1040_wait_30us(io_port);
4018 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4019 trms1040_wait_30us(io_port);
4020 }
4021 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4022 trms1040_wait_30us(io_port);
4023
4024 /* Disable chip select */
4025 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4026 trms1040_wait_30us(io_port);
4027
4028 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4029 trms1040_wait_30us(io_port);
4030
4031 /* Wait for write ready */
4032 while (1) {
4033 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4034 trms1040_wait_30us(io_port);
4035
4036 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4037 trms1040_wait_30us(io_port);
4038
4039 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
4040 break;
4041 }
4042
4043 /* Disable chip select */
4044 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4045}
4046
4047
4048/**
4049 * trms1040_write_all - write 128 bytes to the eeprom
4050 *
4051 * Write the supplied 128 bytes to the chips SEEPROM
4052 *
4053 * @eeprom: the data to write
4054 * @io_port: the base io port
4055 **/
4056static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
4057{
4058 u8 *b_eeprom = (u8 *)eeprom;
4059 u8 addr;
4060
4061 /* Enable SEEPROM */
4062 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4063 io_port + TRM_S1040_GEN_CONTROL);
4064
4065 /* write enable */
4066 trms1040_write_cmd(io_port, 0x04, 0xFF);
4067 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4068 trms1040_wait_30us(io_port);
4069
4070 /* write */
4071 for (addr = 0; addr < 128; addr++, b_eeprom++)
4072 trms1040_set_data(io_port, addr, *b_eeprom);
4073
4074 /* write disable */
4075 trms1040_write_cmd(io_port, 0x04, 0x00);
4076 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4077 trms1040_wait_30us(io_port);
4078
4079 /* Disable SEEPROM */
4080 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4081 io_port + TRM_S1040_GEN_CONTROL);
4082}
4083
4084
4085/**
4086 * trms1040_get_data - get a single byte from the eeprom
4087 *
4088 * Called from read all to read a single byte into the SSEEPROM
4089 * Which is done one bit at a time.
4090 *
4091 * @io_port: base I/O address
4092 * @addr: offset into SEEPROM
4093 *
4094 * Returns the the byte read.
4095 **/
4096static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
4097{
4098 int i;
4099 u8 read_byte;
4100 u8 result = 0;
4101
4102 /* Send read command & address */
4103 trms1040_write_cmd(io_port, 0x06, addr);
4104
4105 /* read data */
4106 for (i = 0; i < 8; i++) {
4107 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4108 trms1040_wait_30us(io_port);
4109 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4110
4111 /* Get data bit while falling edge */
4112 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
4113 result <<= 1;
4114 if (read_byte & NVR_BITIN)
4115 result |= 1;
4116
4117 trms1040_wait_30us(io_port);
4118 }
4119
4120 /* Disable chip select */
4121 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4122 return result;
4123}
4124
4125
4126/**
4127 * trms1040_read_all - read all bytes from the eeprom
4128 *
4129 * Read the 128 bytes from the SEEPROM.
4130 *
4131 * @eeprom: where to store the data
4132 * @io_port: the base io port
4133 **/
4134static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
4135{
4136 u8 *b_eeprom = (u8 *)eeprom;
4137 u8 addr;
4138
4139 /* Enable SEEPROM */
4140 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4141 io_port + TRM_S1040_GEN_CONTROL);
4142
4143 /* read details */
4144 for (addr = 0; addr < 128; addr++, b_eeprom++)
4145 *b_eeprom = trms1040_get_data(io_port, addr);
4146
4147 /* Disable SEEPROM */
4148 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4149 io_port + TRM_S1040_GEN_CONTROL);
4150}
4151
4152
4153
4154/**
4155 * check_eeprom - get and check contents of the eeprom
4156 *
4157 * Read seeprom 128 bytes into the memory provider in eeprom.
4158 * Checks the checksum and if it's not correct it uses a set of default
4159 * values.
4160 *
4161 * @eeprom: caller allocated strcuture to read the eeprom data into
4162 * @io_port: io port to read from
4163 **/
4164static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
4165{
4166 u16 *w_eeprom = (u16 *)eeprom;
4167 u16 w_addr;
4168 u16 cksum;
4169 u32 d_addr;
4170 u32 *d_eeprom;
4171
4172 trms1040_read_all(eeprom, io_port); /* read eeprom */
4173
4174 cksum = 0;
4175 for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
4176 w_addr++, w_eeprom++)
4177 cksum += *w_eeprom;
4178 if (cksum != 0x1234) {
4179 /*
4180 * Checksum is wrong.
4181 * Load a set of defaults into the eeprom buffer
4182 */
4183 dprintkl(KERN_WARNING,
4184 "EEProm checksum error: using default values and options.\n");
4185 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4186 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4187 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4188 eeprom->sub_sys_id[1] =
4189 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4190 eeprom->sub_class = 0x00;
4191 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4192 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4193 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4194 eeprom->device_id[1] =
4195 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4196 eeprom->reserved = 0x00;
4197
4198 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
4199 d_addr < 16; d_addr++, d_eeprom++)
4200 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
4201
4202 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
4203 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
4204 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
4205 *d_eeprom = 0x00;
4206
4207 /* Now load defaults (maybe set by boot/module params) */
4208 set_safe_settings();
4209 fix_settings();
4210 eeprom_override(eeprom);
4211
4212 eeprom->cksum = 0x00;
4213 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
4214 w_addr < 63; w_addr++, w_eeprom++)
4215 cksum += *w_eeprom;
4216
4217 *w_eeprom = 0x1234 - cksum;
4218 trms1040_write_all(eeprom, io_port);
4219 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
4220 } else {
4221 set_safe_settings();
4222 eeprom_index_to_delay(eeprom);
4223 eeprom_override(eeprom);
4224 }
4225}
4226
4227
4228/**
4229 * print_eeprom_settings - output the eeprom settings
4230 * to the kernel log so people can see what they were.
4231 *
4232 * @eeprom: The eeprom data strucutre to show details for.
4233 **/
4234static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
4235{
4236 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4237 eeprom->scsi_id,
4238 eeprom->target[0].period,
4239 clock_speed[eeprom->target[0].period] / 10,
4240 clock_speed[eeprom->target[0].period] % 10,
4241 eeprom->target[0].cfg0);
4242 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4243 eeprom->channel_cfg, eeprom->max_tag,
4244 1 << eeprom->max_tag, eeprom->delay_time);
4245}
4246
4247
4248/* Free SG tables */
4249static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
4250{
4251 int i;
4252 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4253
4254 for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
4255 kfree(acb->srb_array[i].segment_x);
4256
4257 vfree(acb->srb_array[0].virt_map);
4258}
4259
4260
4261/*
4262 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4263 * should never cross a page boundary */
4264static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
4265{
4266 const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
4267 *SEGMENTX_LEN;
4268 int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
4269 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4270 int srb_idx = 0;
4271 unsigned i = 0;
4272 struct SGentry *ptr;
4273 void **virt_array;
4274
4275 for (i = 0; i < DC395x_MAX_SRB_CNT; i++) {
4276 acb->srb_array[i].segment_x = NULL;
4277 acb->srb_array[i].virt_map = NULL;
4278 }
4279
4280 dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
4281 while (pages--) {
4282 ptr = (struct SGentry *)kmalloc(PAGE_SIZE, GFP_KERNEL);
4283 if (!ptr) {
4284 adapter_sg_tables_free(acb);
4285 return 1;
4286 }
4287 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
4288 PAGE_SIZE, ptr, srb_idx);
4289 i = 0;
4290 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4291 acb->srb_array[srb_idx++].segment_x =
4292 ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4293 }
4294 if (i < srbs_per_page)
4295 acb->srb.segment_x =
4296 ptr + (i * DC395x_MAX_SG_LISTENTRY);
4297 else
4298 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
4299
4300 virt_array = vmalloc((DC395x_MAX_SRB_CNT + 1) * DC395x_MAX_SG_LISTENTRY * sizeof(void*));
4301
4302 if (!virt_array) {
4303 adapter_sg_tables_free(acb);
4304 return 1;
4305 }
4306
4307 for (i = 0; i < DC395x_MAX_SRB_CNT + 1; i++) {
4308 acb->srb_array[i].virt_map = virt_array;
4309 virt_array += DC395x_MAX_SG_LISTENTRY;
4310 }
4311
4312 return 0;
4313}
4314
4315
4316
4317/**
4318 * adapter_print_config - print adapter connection and termination
4319 * config
4320 *
4321 * The io port in the adapter needs to have been set before calling
4322 * this function.
4323 *
4324 * @acb: The adapter to print the information for.
4325 **/
4326static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
4327{
4328 u8 bval;
4329
4330 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
4331 dprintkl(KERN_INFO, "%sConnectors: ",
4332 ((bval & WIDESCSI) ? "(Wide) " : ""));
4333 if (!(bval & CON5068))
4334 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
4335 if (!(bval & CON68))
4336 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
4337 if (!(bval & CON50))
4338 printk("int50 ");
4339 if ((bval & (CON5068 | CON50 | CON68)) ==
4340 0 /*(CON5068 | CON50 | CON68) */ )
4341 printk(" Oops! (All 3?) ");
4342 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
4343 printk(" Termination: ");
4344 if (bval & DIS_TERM)
4345 printk("Disabled\n");
4346 else {
4347 if (bval & AUTOTERM)
4348 printk("Auto ");
4349 if (bval & LOW8TERM)
4350 printk("Low ");
4351 if (bval & UP8TERM)
4352 printk("High ");
4353 printk("\n");
4354 }
4355}
4356
4357
4358/**
4359 * adapter_init_params - Initialize the various parameters in the
4360 * adapter structure. Note that the pointer to the scsi_host is set
4361 * early (when this instance is created) and the io_port and irq
4362 * values are set later after they have been reserved. This just gets
4363 * everything set to a good starting position.
4364 *
4365 * The eeprom structure in the adapter needs to have been set before
4366 * calling this function.
4367 *
4368 * @acb: The adapter to initialize.
4369 **/
4370static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
4371{
4372 struct NvRamType *eeprom = &acb->eeprom;
4373 int i;
4374
4375 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4376 /* NOTE: acb->io_port_base is set at port registration time */
4377 /* NOTE: acb->io_port_len is set at port registration time */
4378
4379 INIT_LIST_HEAD(&acb->dcb_list);
4380 acb->dcb_run_robin = NULL;
4381 acb->active_dcb = NULL;
4382
4383 INIT_LIST_HEAD(&acb->srb_free_list);
4384 /* temp SRB for Q tag used or abort command used */
4385 acb->tmp_srb = &acb->srb;
4386 init_timer(&acb->waiting_timer);
4387 init_timer(&acb->selto_timer);
4388
4389 acb->srb_count = DC395x_MAX_SRB_CNT;
4390
4391 acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */
4392 /* NOTE: acb->irq_level is set at IRQ registration time */
4393
4394 acb->tag_max_num = 1 << eeprom->max_tag;
4395 if (acb->tag_max_num > 30)
4396 acb->tag_max_num = 30;
4397
4398 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4399 acb->gmode2 = eeprom->channel_cfg;
4400 acb->config = 0; /* NOTE: actually set in adapter_init_chip */
4401
4402 if (eeprom->channel_cfg & NAC_SCANLUN)
4403 acb->lun_chk = 1;
4404 acb->scan_devices = 1;
4405
4406 acb->scsi_host->this_id = eeprom->scsi_id;
4407 acb->hostid_bit = (1 << acb->scsi_host->this_id);
4408
4409 for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
4410 acb->dcb_map[i] = 0;
4411
4412 acb->msg_len = 0;
4413
4414 /* link static array of srbs into the srb free list */
4415 for (i = 0; i < acb->srb_count - 1; i++)
4416 srb_free_insert(acb, &acb->srb_array[i]);
4417}
4418
4419
4420/**
4421 * adapter_init_host - Initialize the scsi host instance based on
4422 * values that we have already stored in the adapter instance. There's
4423 * some mention that a lot of these are deprecated, so we won't use
4424 * them (we'll use the ones in the adapter instance) but we'll fill
4425 * them in in case something else needs them.
4426 *
4427 * The eeprom structure, irq and io ports in the adapter need to have
4428 * been set before calling this function.
4429 *
4430 * @host: The scsi host instance to fill in the values for.
4431 **/
4432static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
4433{
4434 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4435 struct NvRamType *eeprom = &acb->eeprom;
4436
4437 host->max_cmd_len = 24;
4438 host->can_queue = DC395x_MAX_CMD_QUEUE;
4439 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4440 host->this_id = (int)eeprom->scsi_id;
4441 host->io_port = acb->io_port_base;
4442 host->n_io_port = acb->io_port_len;
4443 host->dma_channel = -1;
4444 host->unique_id = acb->io_port_base;
4445 host->irq = acb->irq_level;
4446 host->last_reset = jiffies;
4447
4448 host->max_id = 16;
4449 if (host->max_id - 1 == eeprom->scsi_id)
4450 host->max_id--;
4451
4452#ifdef CONFIG_SCSI_MULTI_LUN
4453 if (eeprom->channel_cfg & NAC_SCANLUN)
4454 host->max_lun = 8;
4455 else
4456 host->max_lun = 1;
4457#else
4458 host->max_lun = 1;
4459#endif
4460
4461}
4462
4463
4464/**
4465 * adapter_init_chip - Get the chip into a know state and figure out
4466 * some of the settings that apply to this adapter.
4467 *
4468 * The io port in the adapter needs to have been set before calling
4469 * this function. The config will be configured correctly on return.
4470 *
4471 * @acb: The adapter which we are to init.
4472 **/
4473static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
4474{
4475 struct NvRamType *eeprom = &acb->eeprom;
4476
4477 /* Mask all the interrupt */
4478 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
4479 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
4480
4481 /* Reset SCSI module */
4482 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4483
4484 /* Reset PCI/DMA module */
4485 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4486 udelay(20);
4487
4488 /* program configuration 0 */
4489 acb->config = HCC_AUTOTERM | HCC_PARITY;
4490 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
4491 acb->config |= HCC_WIDE_CARD;
4492
4493 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
4494 acb->config |= HCC_SCSI_RESET;
4495
4496 if (acb->config & HCC_SCSI_RESET) {
4497 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
4498 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4499
4500 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4501 /*spin_unlock_irq (&io_request_lock); */
4502 udelay(500);
4503
4504 acb->scsi_host->last_reset =
4505 jiffies + HZ / 2 +
4506 HZ * acb->eeprom.delay_time;
4507
4508 /*spin_lock_irq (&io_request_lock); */
4509 }
4510}
4511
4512
4513/**
4514 * init_adapter - Grab the resource for the card, setup the adapter
4515 * information, set the card into a known state, create the various
4516 * tables etc etc. This basically gets all adapter information all up
4517 * to date, intialised and gets the chip in sync with it.
4518 *
4519 * @host: This hosts adapter structure
4520 * @io_port: The base I/O port
4521 * @irq: IRQ
4522 *
4523 * Returns 0 if the initialization succeeds, any other value on
4524 * failure.
4525 **/
4526static int __devinit adapter_init(struct AdapterCtlBlk *acb,
4527 unsigned long io_port, u32 io_port_len, unsigned int irq)
4528{
4529 if (!request_region(io_port, io_port_len, DC395X_NAME)) {
4530 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
4531 goto failed;
4532 }
4533 /* store port base to indicate we have registered it */
4534 acb->io_port_base = io_port;
4535 acb->io_port_len = io_port_len;
4536
4537 if (request_irq(irq, dc395x_interrupt, SA_SHIRQ, DC395X_NAME, acb)) {
4538 /* release the region we just claimed */
4539 dprintkl(KERN_INFO, "Failed to register IRQ\n");
4540 goto failed;
4541 }
4542 /* store irq to indicate we have registered it */
4543 acb->irq_level = irq;
4544
4545 /* get eeprom configuration information and command line settings etc */
4546 check_eeprom(&acb->eeprom, io_port);
4547 print_eeprom_settings(&acb->eeprom);
4548
4549 /* setup adapter control block */
4550 adapter_init_params(acb);
4551
4552 /* display card connectors/termination settings */
4553 adapter_print_config(acb);
4554
4555 if (adapter_sg_tables_alloc(acb)) {
4556 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
4557 goto failed;
4558 }
4559 adapter_init_scsi_host(acb->scsi_host);
4560 adapter_init_chip(acb);
4561 set_basic_config(acb);
4562
4563 dprintkdbg(DBG_0,
4564 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4565 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4566 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
4567 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
4568 return 0;
4569
4570failed:
4571 if (acb->irq_level)
4572 free_irq(acb->irq_level, acb);
4573 if (acb->io_port_base)
4574 release_region(acb->io_port_base, acb->io_port_len);
4575 adapter_sg_tables_free(acb);
4576
4577 return 1;
4578}
4579
4580
4581/**
4582 * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4583 * stopping all operations and disabling interrupt generation on the
4584 * card.
4585 *
4586 * @acb: The adapter which we are to shutdown.
4587 **/
4588static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
4589{
4590 /* disable interrupts */
4591 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
4592 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
4593
4594 /* reset the scsi bus */
4595 if (acb->config & HCC_SCSI_RESET)
4596 reset_scsi_bus(acb);
4597
4598 /* clear any pending interupt state */
4599 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4600}
4601
4602
4603
4604/**
4605 * adapter_uninit - Shut down the chip and release any resources that
4606 * we had allocated. Once this returns the adapter should not be used
4607 * anymore.
4608 *
4609 * @acb: The adapter which we are to un-initialize.
4610 **/
4611static void adapter_uninit(struct AdapterCtlBlk *acb)
4612{
4613 unsigned long flags;
4614 DC395x_LOCK_IO(acb->scsi_host, flags);
4615
4616 /* remove timers */
4617 if (timer_pending(&acb->waiting_timer))
4618 del_timer(&acb->waiting_timer);
4619 if (timer_pending(&acb->selto_timer))
4620 del_timer(&acb->selto_timer);
4621
4622 adapter_uninit_chip(acb);
4623 adapter_remove_and_free_all_devices(acb);
4624 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4625
4626 if (acb->irq_level)
4627 free_irq(acb->irq_level, acb);
4628 if (acb->io_port_base)
4629 release_region(acb->io_port_base, acb->io_port_len);
4630
4631 adapter_sg_tables_free(acb);
4632}
4633
4634
4635#undef SPRINTF
4636#define SPRINTF(args...) pos += sprintf(pos, args)
4637
4638#undef YESNO
4639#define YESNO(YN) \
4640 if (YN) SPRINTF(" Yes ");\
4641 else SPRINTF(" No ")
4642
4643static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
4644 char **start, off_t offset, int length, int inout)
4645{
4646 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4647 int spd, spd1;
4648 char *pos = buffer;
4649 struct DeviceCtlBlk *dcb;
4650 unsigned long flags;
4651 int dev;
4652
4653 if (inout) /* Has data been written to the file ? */
4654 return -EPERM;
4655
4656 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
4657 SPRINTF(" Driver Version " DC395X_VERSION "\n");
4658
4659 DC395x_LOCK_IO(acb->scsi_host, flags);
4660
4661 SPRINTF("SCSI Host Nr %i, ", host->host_no);
4662 SPRINTF("DC395U/UW/F DC315/U %s\n",
4663 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
4664 SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
4665 SPRINTF("irq_level 0x%04x, ", acb->irq_level);
4666 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
4667
4668 SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
4669 SPRINTF("AdapterID %i\n", host->this_id);
4670
4671 SPRINTF("tag_max_num %i", acb->tag_max_num);
4672 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4673 SPRINTF(", FilterCfg 0x%02x",
4674 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
4675 SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
4676 /*SPRINTF("\n"); */
4677
4678 SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4679 SPRINTF
4680 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
4681 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
4682 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
4683 acb->dcb_map[6], acb->dcb_map[7]);
4684 SPRINTF
4685 (" %02x %02x %02x %02x %02x %02x %02x %02x\n",
4686 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
4687 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
4688 acb->dcb_map[14], acb->dcb_map[15]);
4689
4690 SPRINTF
4691 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4692
4693 dev = 0;
4694 list_for_each_entry(dcb, &acb->dcb_list, list) {
4695 int nego_period;
4696 SPRINTF("%02i %02i %02i ", dev, dcb->target_id,
4697 dcb->target_lun);
4698 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
4699 YESNO(dcb->sync_offset);
4700 YESNO(dcb->sync_period & WIDE_SYNC);
4701 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
4702 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
4703 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
4704 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
4705 if (dcb->sync_offset)
4706 SPRINTF(" %03i ns ", nego_period);
4707 else
4708 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
4709
4710 if (dcb->sync_offset & 0x0f) {
4711 spd = 1000 / (nego_period);
4712 spd1 = 1000 % (nego_period);
4713 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
4714 SPRINTF(" %2i.%1i M %02i ", spd, spd1,
4715 (dcb->sync_offset & 0x0f));
4716 } else
4717 SPRINTF(" ");
4718
4719 /* Add more info ... */
4720 SPRINTF(" %02i\n", dcb->max_command);
4721 dev++;
4722 }
4723
4724 if (timer_pending(&acb->waiting_timer))
4725 SPRINTF("Waiting queue timer running\n");
4726 else
4727 SPRINTF("\n");
4728
4729 list_for_each_entry(dcb, &acb->dcb_list, list) {
4730 struct ScsiReqBlk *srb;
4731 if (!list_empty(&dcb->srb_waiting_list))
4732 SPRINTF("DCB (%02i-%i): Waiting: %i:",
4733 dcb->target_id, dcb->target_lun,
4734 list_size(&dcb->srb_waiting_list));
4735 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
4736 SPRINTF(" %li", srb->cmd->pid);
4737 if (!list_empty(&dcb->srb_going_list))
4738 SPRINTF("\nDCB (%02i-%i): Going : %i:",
4739 dcb->target_id, dcb->target_lun,
4740 list_size(&dcb->srb_going_list));
4741 list_for_each_entry(srb, &dcb->srb_going_list, list)
4742 SPRINTF(" %li", srb->cmd->pid);
4743 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4744 SPRINTF("\n");
4745 }
4746
4747 if (debug_enabled(DBG_1)) {
4748 SPRINTF("DCB list for ACB %p:\n", acb);
4749 list_for_each_entry(dcb, &acb->dcb_list, list) {
4750 SPRINTF("%p -> ", dcb);
4751 }
4752 SPRINTF("END\n");
4753 }
4754
4755 *start = buffer + offset;
4756 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4757
4758 if (pos - buffer < offset)
4759 return 0;
4760 else if (pos - buffer - offset < length)
4761 return pos - buffer - offset;
4762 else
4763 return length;
4764}
4765
4766
4767static struct scsi_host_template dc395x_driver_template = {
4768 .module = THIS_MODULE,
4769 .proc_name = DC395X_NAME,
4770 .proc_info = dc395x_proc_info,
4771 .name = DC395X_BANNER " " DC395X_VERSION,
4772 .queuecommand = dc395x_queue_command,
4773 .bios_param = dc395x_bios_param,
4774 .slave_alloc = dc395x_slave_alloc,
4775 .slave_destroy = dc395x_slave_destroy,
4776 .can_queue = DC395x_MAX_CAN_QUEUE,
4777 .this_id = 7,
4778 .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
4779 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
4780 .eh_abort_handler = dc395x_eh_abort,
4781 .eh_bus_reset_handler = dc395x_eh_bus_reset,
4782 .unchecked_isa_dma = 0,
4783 .use_clustering = DISABLE_CLUSTERING,
4784};
4785
4786
4787/**
4788 * banner_display - Display banner on first instance of driver
4789 * initialized.
4790 **/
4791static void banner_display(void)
4792{
4793 static int banner_done = 0;
4794 if (!banner_done)
4795 {
4796 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
4797 banner_done = 1;
4798 }
4799}
4800
4801
4802/**
4803 * dc395x_init_one - Initialise a single instance of the adapter.
4804 *
4805 * The PCI layer will call this once for each instance of the adapter
4806 * that it finds in the system. The pci_dev strcuture indicates which
4807 * instance we are being called from.
4808 *
4809 * @dev: The PCI device to intialize.
4810 * @id: Looks like a pointer to the entry in our pci device table
4811 * that was actually matched by the PCI subsystem.
4812 *
4813 * Returns 0 on success, or an error code (-ve) on failure.
4814 **/
4815static int __devinit dc395x_init_one(struct pci_dev *dev,
4816 const struct pci_device_id *id)
4817{
4818 struct Scsi_Host *scsi_host = NULL;
4819 struct AdapterCtlBlk *acb = NULL;
4820 unsigned long io_port_base;
4821 unsigned int io_port_len;
4822 unsigned int irq;
4823
4824 dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
4825 banner_display();
4826
4827 if (pci_enable_device(dev))
4828 {
4829 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
4830 return -ENODEV;
4831 }
4832 io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
4833 io_port_len = pci_resource_len(dev, 0);
4834 irq = dev->irq;
4835 dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
4836
4837 /* allocate scsi host information (includes out adapter) */
4838 scsi_host = scsi_host_alloc(&dc395x_driver_template,
4839 sizeof(struct AdapterCtlBlk));
4840 if (!scsi_host) {
4841 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
4842 goto fail;
4843 }
4844 acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
4845 acb->scsi_host = scsi_host;
4846 acb->dev = dev;
4847
4848 /* initialise the adapter and everything we need */
4849 if (adapter_init(acb, io_port_base, io_port_len, irq)) {
4850 dprintkl(KERN_INFO, "adapter init failed\n");
4851 goto fail;
4852 }
4853
4854 pci_set_master(dev);
4855
4856 /* get the scsi mid level to scan for new devices on the bus */
4857 if (scsi_add_host(scsi_host, &dev->dev)) {
4858 dprintkl(KERN_ERR, "scsi_add_host failed\n");
4859 goto fail;
4860 }
4861 pci_set_drvdata(dev, scsi_host);
4862 scsi_scan_host(scsi_host);
4863
4864 return 0;
4865
4866fail:
4867 if (acb != NULL)
4868 adapter_uninit(acb);
4869 if (scsi_host != NULL)
4870 scsi_host_put(scsi_host);
4871 pci_disable_device(dev);
4872 return -ENODEV;
4873}
4874
4875
4876/**
4877 * dc395x_remove_one - Called to remove a single instance of the
4878 * adapter.
4879 *
4880 * @dev: The PCI device to intialize.
4881 **/
4882static void __devexit dc395x_remove_one(struct pci_dev *dev)
4883{
4884 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
4885 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
4886
4887 dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
4888
4889 scsi_remove_host(scsi_host);
4890 adapter_uninit(acb);
4891 pci_disable_device(dev);
4892 scsi_host_put(scsi_host);
4893 pci_set_drvdata(dev, NULL);
4894}
4895
4896
4897static struct pci_device_id dc395x_pci_table[] = {
4898 {
4899 .vendor = PCI_VENDOR_ID_TEKRAM,
4900 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
4901 .subvendor = PCI_ANY_ID,
4902 .subdevice = PCI_ANY_ID,
4903 },
4904 {} /* Terminating entry */
4905};
4906MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
4907
4908
4909static struct pci_driver dc395x_driver = {
4910 .name = DC395X_NAME,
4911 .id_table = dc395x_pci_table,
4912 .probe = dc395x_init_one,
4913 .remove = __devexit_p(dc395x_remove_one),
4914};
4915
4916
4917/**
4918 * dc395x_module_init - Module initialization function
4919 *
4920 * Used by both module and built-in driver to initialise this driver.
4921 **/
4922static int __init dc395x_module_init(void)
4923{
4924 return pci_module_init(&dc395x_driver);
4925}
4926
4927
4928/**
4929 * dc395x_module_exit - Module cleanup function.
4930 **/
4931static void __exit dc395x_module_exit(void)
4932{
4933 pci_unregister_driver(&dc395x_driver);
4934}
4935
4936
4937module_init(dc395x_module_init);
4938module_exit(dc395x_module_exit);
4939
4940MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4941MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4942MODULE_LICENSE("GPL");