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