blob: b0f8523e665f94d6d292a40a2e66bdd578462673 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/io.h>
62
63#include <scsi/scsi.h>
64#include <scsi/scsicam.h> /* needed for scsicam_bios_param */
65#include <scsi/scsi_cmnd.h>
66#include <scsi/scsi_device.h>
67#include <scsi/scsi_host.h>
68
69#include "dc395x.h"
70
71#define DC395X_NAME "dc395x"
72#define DC395X_BANNER "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
73#define DC395X_VERSION "v2.05, 2004/03/08"
74
75/*---------------------------------------------------------------------------
76 Features
77 ---------------------------------------------------------------------------*/
78/*
79 * Set to disable parts of the driver
80 */
81/*#define DC395x_NO_DISCONNECT*/
82/*#define DC395x_NO_TAGQ*/
83/*#define DC395x_NO_SYNC*/
84/*#define DC395x_NO_WIDE*/
85
86/*---------------------------------------------------------------------------
87 Debugging
88 ---------------------------------------------------------------------------*/
89/*
90 * Types of debugging that can be enabled and disabled
91 */
92#define DBG_KG 0x0001
93#define DBG_0 0x0002
94#define DBG_1 0x0004
95#define DBG_SG 0x0020
96#define DBG_FIFO 0x0040
97#define DBG_PIO 0x0080
98
99
100/*
101 * Set set of things to output debugging for.
102 * Undefine to remove all debugging
103 */
104/*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
105/*#define DEBUG_MASK DBG_0*/
106
107
108/*
109 * Output a kernel mesage at the specified level and append the
110 * driver name and a ": " to the start of the message
111 */
112#define dprintkl(level, format, arg...) \
113 printk(level DC395X_NAME ": " format , ## arg)
114
115
116#ifdef DEBUG_MASK
117/*
118 * print a debug message - this is formated with KERN_DEBUG, then the
119 * driver name followed by a ": " and then the message is output.
120 * This also checks that the specified debug level is enabled before
121 * outputing the message
122 */
123#define dprintkdbg(type, format, arg...) \
124 do { \
125 if ((type) & (DEBUG_MASK)) \
126 dprintkl(KERN_DEBUG , format , ## arg); \
127 } while (0)
128
129/*
130 * Check if the specified type of debugging is enabled
131 */
132#define debug_enabled(type) ((DEBUG_MASK) & (type))
133
134#else
135/*
136 * No debugging. Do nothing
137 */
138#define dprintkdbg(type, format, arg...) \
139 do {} while (0)
140#define debug_enabled(type) (0)
141
142#endif
143
144
145#ifndef PCI_VENDOR_ID_TEKRAM
146#define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */
147#endif
148#ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
149#define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */
150#endif
151
152
153#define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
154#define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
155
156#define DC395x_read8(acb,address) (u8)(inb(acb->io_port_base + (address)))
157#define DC395x_read16(acb,address) (u16)(inw(acb->io_port_base + (address)))
158#define DC395x_read32(acb,address) (u32)(inl(acb->io_port_base + (address)))
159#define DC395x_write8(acb,address,value) outb((value), acb->io_port_base + (address))
160#define DC395x_write16(acb,address,value) outw((value), acb->io_port_base + (address))
161#define DC395x_write32(acb,address,value) outl((value), acb->io_port_base + (address))
162
163/* cmd->result */
164#define RES_TARGET 0x000000FF /* Target State */
165#define RES_TARGET_LNX STATUS_MASK /* Only official ... */
166#define RES_ENDMSG 0x0000FF00 /* End Message */
167#define RES_DID 0x00FF0000 /* DID_ codes */
168#define RES_DRV 0xFF000000 /* DRIVER_ codes */
169
170#define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
171#define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
172
173#define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
174#define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
175#define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
176#define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
177#define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
178
179#define TAG_NONE 255
180
181/*
182 * srb->segement_x is the hw sg list. It is always allocated as a
183 * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
184 * cross a page boundy.
185 */
186#define SEGMENTX_LEN (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
Linus Torvaldsb4b08e52005-08-14 15:43:39 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189struct SGentry {
190 u32 address; /* bus! address */
191 u32 length;
192};
193
194/* The SEEPROM structure for TRM_S1040 */
195struct NVRamTarget {
196 u8 cfg0; /* Target configuration byte 0 */
197 u8 period; /* Target period */
198 u8 cfg2; /* Target configuration byte 2 */
199 u8 cfg3; /* Target configuration byte 3 */
200};
201
202struct NvRamType {
203 u8 sub_vendor_id[2]; /* 0,1 Sub Vendor ID */
204 u8 sub_sys_id[2]; /* 2,3 Sub System ID */
205 u8 sub_class; /* 4 Sub Class */
206 u8 vendor_id[2]; /* 5,6 Vendor ID */
207 u8 device_id[2]; /* 7,8 Device ID */
208 u8 reserved; /* 9 Reserved */
209 struct NVRamTarget target[DC395x_MAX_SCSI_ID];
210 /** 10,11,12,13
211 ** 14,15,16,17
212 ** ....
213 ** ....
214 ** 70,71,72,73
215 */
216 u8 scsi_id; /* 74 Host Adapter SCSI ID */
217 u8 channel_cfg; /* 75 Channel configuration */
218 u8 delay_time; /* 76 Power on delay time */
219 u8 max_tag; /* 77 Maximum tags */
220 u8 reserved0; /* 78 */
221 u8 boot_target; /* 79 */
222 u8 boot_lun; /* 80 */
223 u8 reserved1; /* 81 */
224 u16 reserved2[22]; /* 82,..125 */
225 u16 cksum; /* 126,127 */
226};
227
228struct ScsiReqBlk {
229 struct list_head list; /* next/prev ptrs for srb lists */
230 struct DeviceCtlBlk *dcb;
231 struct scsi_cmnd *cmd;
232
233 struct SGentry *segment_x; /* Linear array of hw sg entries (up to 64 entries) */
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +0200234 dma_addr_t sg_bus_addr; /* Bus address of sg list (ie, of segment_x) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 u8 sg_count; /* No of HW sg entries for this request */
237 u8 sg_index; /* Index of HW sg entry for this request */
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +0200238 size_t total_xfer_length; /* Total number of bytes remaining to be transfered */
239 size_t request_length; /* Total number of bytes in this request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /*
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 */
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +0200249 size_t xferred; /* Saved copy of total_xfer_length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
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/*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200545 * Safe settings. If set to zero the BIOS/default values with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * 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)
Michael Opdenacker59c51592007-05-09 08:57:56 +0200621 * to the number of actual seconds that the delay should be for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
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",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600782 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 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",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600791 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 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",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600799 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 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",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600809 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
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",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600825 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
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",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600840 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 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",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600850 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/* Prepare SRB for being sent to Device DCB w/ command *cmd */
980static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
981 struct ScsiReqBlk *srb)
982{
FUJITA Tomonoria862ea32007-05-26 02:07:09 +0900983 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 enum dma_data_direction dir = cmd->sc_data_direction;
985 dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -0600986 cmd->serial_number, dcb->target_id, dcb->target_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 srb->dcb = dcb;
989 srb->cmd = cmd;
990 srb->sg_count = 0;
991 srb->total_xfer_length = 0;
992 srb->sg_bus_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 srb->sg_index = 0;
994 srb->adapter_status = 0;
995 srb->target_status = 0;
996 srb->msg_count = 0;
997 srb->status = 0;
998 srb->flag = 0;
999 srb->state = 0;
1000 srb->retry_count = 0;
1001 srb->tag_number = TAG_NONE;
1002 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1003 srb->end_message = 0;
1004
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09001005 nseg = scsi_dma_map(cmd);
1006 BUG_ON(nseg < 0);
1007
1008 if (dir == PCI_DMA_NONE || !nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 dprintkdbg(DBG_0,
1010 "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09001011 cmd->bufflen, scsi_sglist(cmd), scsi_sg_count(cmd),
1012 srb->segment_x[0].address);
1013 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int i;
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09001015 u32 reqlen = scsi_bufflen(cmd);
1016 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct SGentry *sgp = srb->segment_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09001019 srb->sg_count = nseg;
1020
1021 dprintkdbg(DBG_0,
1022 "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
1023 reqlen, scsi_sglist(cmd), scsi_sg_count(cmd),
1024 srb->sg_count);
1025
1026 scsi_for_each_sg(cmd, sg, srb->sg_count, i) {
1027 u32 busaddr = (u32)sg_dma_address(sg);
1028 u32 seglen = (u32)sg->length;
Linus Torvaldsb4b08e52005-08-14 15:43:39 -07001029 sgp[i].address = busaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 sgp[i].length = seglen;
1031 srb->total_xfer_length += seglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 sgp += srb->sg_count - 1;
1034
1035 /*
1036 * adjust last page if too big as it is allocated
1037 * on even page boundaries
1038 */
1039 if (srb->total_xfer_length > reqlen) {
1040 sgp->length -= (srb->total_xfer_length - reqlen);
1041 srb->total_xfer_length = reqlen;
1042 }
1043
1044 /* Fixup for WIDE padding - make sure length is even */
1045 if (dcb->sync_period & WIDE_SYNC &&
1046 srb->total_xfer_length % 2) {
1047 srb->total_xfer_length++;
1048 sgp->length++;
1049 }
1050
1051 srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
1052 srb->segment_x,
1053 SEGMENTX_LEN,
1054 PCI_DMA_TODEVICE);
1055
1056 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
1057 srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02001059
1060 srb->request_length = srb->total_xfer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063
1064/**
1065 * dc395x_queue_command - queue scsi command passed from the mid
1066 * layer, invoke 'done' on completion
1067 *
1068 * @cmd: pointer to scsi command object
1069 * @done: function pointer to be invoked on completion
1070 *
1071 * Returns 1 if the adapter (host) is busy, else returns 0. One
1072 * reason for an adapter to be busy is that the number
1073 * of outstanding queued commands is already equal to
1074 * struct Scsi_Host::can_queue .
1075 *
1076 * Required: if struct Scsi_Host::can_queue is ever non-zero
1077 * then this function is required.
1078 *
1079 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1080 * and is expected to be held on return.
1081 *
1082 **/
Jeff Garzikf2812332010-11-16 02:10:29 -05001083static int dc395x_queue_command_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct DeviceCtlBlk *dcb;
1086 struct ScsiReqBlk *srb;
1087 struct AdapterCtlBlk *acb =
1088 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1089 dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001090 cmd->serial_number, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /* Assume BAD_TARGET; will be cleared later */
1093 cmd->result = DID_BAD_TARGET << 16;
1094
1095 /* ignore invalid targets */
1096 if (cmd->device->id >= acb->scsi_host->max_id ||
1097 cmd->device->lun >= acb->scsi_host->max_lun ||
1098 cmd->device->lun >31) {
1099 goto complete;
1100 }
1101
1102 /* does the specified lun on the specified device exist */
1103 if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
1104 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
1105 cmd->device->id, cmd->device->lun);
1106 goto complete;
1107 }
1108
1109 /* do we have a DCB for the device */
1110 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1111 if (!dcb) {
1112 /* should never happen */
1113 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
1114 cmd->device->id, cmd->device->lun);
1115 goto complete;
1116 }
1117
1118 /* set callback and clear result in the command */
1119 cmd->scsi_done = done;
1120 cmd->result = 0;
1121
1122 srb = srb_get_free(acb);
1123 if (!srb)
1124 {
1125 /*
1126 * Return 1 since we are unable to queue this command at this
1127 * point in time.
1128 */
1129 dprintkdbg(DBG_0, "queue_command: No free srb's\n");
1130 return 1;
1131 }
1132
1133 build_srb(cmd, dcb, srb);
1134
1135 if (!list_empty(&dcb->srb_waiting_list)) {
1136 /* append to waiting queue */
1137 srb_waiting_append(dcb, srb);
1138 waiting_process_next(acb);
1139 } else {
1140 /* process immediately */
1141 send_srb(acb, srb);
1142 }
Matthew Wilcox12a44162007-09-18 19:54:43 -06001143 dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return 0;
1145
1146complete:
1147 /*
1148 * Complete the command immediatey, and then return 0 to
1149 * indicate that we have handled the command. This is usually
1150 * done when the commad is for things like non existent
1151 * devices.
1152 */
1153 done(cmd);
1154 return 0;
1155}
1156
Jeff Garzikf2812332010-11-16 02:10:29 -05001157static DEF_SCSI_QCMD(dc395x_queue_command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159/*
1160 * Return the disk geometry for the given SCSI device.
1161 */
1162static int dc395x_bios_param(struct scsi_device *sdev,
1163 struct block_device *bdev, sector_t capacity, int *info)
1164{
1165#ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
1166 int heads, sectors, cylinders;
1167 struct AdapterCtlBlk *acb;
1168 int size = capacity;
1169
1170 dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
1171 acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
1172 heads = 64;
1173 sectors = 32;
1174 cylinders = size / (heads * sectors);
1175
1176 if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
1177 heads = 255;
1178 sectors = 63;
1179 cylinders = size / (heads * sectors);
1180 }
1181 geom[0] = heads;
1182 geom[1] = sectors;
1183 geom[2] = cylinders;
1184 return 0;
1185#else
1186 return scsicam_bios_param(bdev, capacity, info);
1187#endif
1188}
1189
1190
1191static void dump_register_info(struct AdapterCtlBlk *acb,
1192 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
1193{
1194 u16 pstat;
1195 struct pci_dev *dev = acb->dev;
1196 pci_read_config_word(dev, PCI_STATUS, &pstat);
1197 if (!dcb)
1198 dcb = acb->active_dcb;
1199 if (!srb && dcb)
1200 srb = dcb->active_srb;
1201 if (srb) {
1202 if (!srb->cmd)
1203 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
1204 srb, srb->cmd);
1205 else
1206 dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) "
1207 "cmnd=0x%02x <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001208 srb, srb->cmd, srb->cmd->serial_number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 srb->cmd->cmnd[0], srb->cmd->device->id,
1210 srb->cmd->device->lun);
Al Viro3c5c6652006-10-10 22:49:47 +01001211 printk(" sglist=%p cnt=%i idx=%i len=%zu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 srb->segment_x, srb->sg_count, srb->sg_index,
1213 srb->total_xfer_length);
1214 printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1215 srb->state, srb->status, srb->scsi_phase,
1216 (acb->active_dcb) ? "" : "not");
1217 }
1218 dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1219 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1220 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1221 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1222 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1223 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1224 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1225 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
1226 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1227 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1228 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1229 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
1230 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1231 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1232 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1233 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1234 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1235 dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1236 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1237 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1238 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1239 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1240 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1241 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1242 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1243 DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
1244 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1245 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1246 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1247 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1248 dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1249 "pci{status=0x%04x}\n",
1250 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1251 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1252 DC395x_read8(acb, TRM_S1040_GEN_TIMER),
1253 pstat);
1254}
1255
1256
1257static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1258{
1259#if debug_enabled(DBG_FIFO)
1260 u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1261 u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1262 if (!(fifocnt & 0x40))
1263 dprintkdbg(DBG_FIFO,
1264 "clear_fifo: (%i bytes) on phase %02x in %s\n",
1265 fifocnt & 0x3f, lines, txt);
1266#endif
1267 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1268}
1269
1270
1271static void reset_dev_param(struct AdapterCtlBlk *acb)
1272{
1273 struct DeviceCtlBlk *dcb;
1274 struct NvRamType *eeprom = &acb->eeprom;
1275 dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
1276
1277 list_for_each_entry(dcb, &acb->dcb_list, list) {
1278 u8 period_index;
1279
1280 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1281 dcb->sync_period = 0;
1282 dcb->sync_offset = 0;
1283
1284 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1285 period_index = eeprom->target[dcb->target_id].period & 0x07;
1286 dcb->min_nego_period = clock_period[period_index];
1287 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1288 || !(acb->config & HCC_WIDE_CARD))
1289 dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1290 }
1291}
1292
1293
1294/*
1295 * perform a hard reset on the SCSI bus
1296 * @cmd - some command for this host (for fetching hooks)
1297 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1298 */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04001299static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct AdapterCtlBlk *acb =
1302 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1303 dprintkl(KERN_INFO,
1304 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001305 cmd->serial_number, cmd->device->id, cmd->device->lun, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (timer_pending(&acb->waiting_timer))
1308 del_timer(&acb->waiting_timer);
1309
1310 /*
1311 * disable interrupt
1312 */
1313 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1314 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1315 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1316 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1317
1318 reset_scsi_bus(acb);
1319 udelay(500);
1320
1321 /* We may be in serious trouble. Wait some seconds */
1322 acb->scsi_host->last_reset =
1323 jiffies + 3 * HZ / 2 +
1324 HZ * acb->eeprom.delay_time;
1325
1326 /*
1327 * re-enable interrupt
1328 */
1329 /* Clear SCSI FIFO */
1330 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1331 clear_fifo(acb, "eh_bus_reset");
1332 /* Delete pending IRQ */
1333 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1334 set_basic_config(acb);
1335
1336 reset_dev_param(acb);
1337 doing_srb_done(acb, DID_RESET, cmd, 0);
1338 acb->active_dcb = NULL;
1339 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
1340 waiting_process_next(acb);
1341
1342 return SUCCESS;
1343}
1344
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04001345static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1346{
1347 int rc;
1348
1349 spin_lock_irq(cmd->device->host->host_lock);
1350 rc = __dc395x_eh_bus_reset(cmd);
1351 spin_unlock_irq(cmd->device->host->host_lock);
1352
1353 return rc;
1354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356/*
1357 * abort an errant SCSI command
1358 * @cmd - command to be aborted
1359 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1360 */
1361static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1362{
1363 /*
1364 * Look into our command queues: If it has not been sent already,
1365 * we remove it and return success. Otherwise fail.
1366 */
1367 struct AdapterCtlBlk *acb =
1368 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1369 struct DeviceCtlBlk *dcb;
1370 struct ScsiReqBlk *srb;
1371 dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001372 cmd->serial_number, cmd->device->id, cmd->device->lun, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1375 if (!dcb) {
1376 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1377 return FAILED;
1378 }
1379
1380 srb = find_cmd(cmd, &dcb->srb_waiting_list);
1381 if (srb) {
1382 srb_waiting_remove(dcb, srb);
1383 pci_unmap_srb_sense(acb, srb);
1384 pci_unmap_srb(acb, srb);
1385 free_tag(dcb, srb);
1386 srb_free_insert(acb, srb);
1387 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1388 cmd->result = DID_ABORT << 16;
1389 return SUCCESS;
1390 }
1391 srb = find_cmd(cmd, &dcb->srb_going_list);
1392 if (srb) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02001393 dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* XXX: Should abort the command here */
1395 } else {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02001396 dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 return FAILED;
1399}
1400
1401
1402/* SDTR */
1403static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1404 struct ScsiReqBlk *srb)
1405{
1406 u8 *ptr = srb->msgout_buf + srb->msg_count;
1407 if (srb->msg_count > 1) {
1408 dprintkl(KERN_INFO,
1409 "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1410 srb->msg_count, srb->msgout_buf[0],
1411 srb->msgout_buf[1]);
1412 return;
1413 }
1414 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1415 dcb->sync_offset = 0;
1416 dcb->min_nego_period = 200 >> 2;
1417 } else if (dcb->sync_offset == 0)
1418 dcb->sync_offset = SYNC_NEGO_OFFSET;
1419
1420 *ptr++ = MSG_EXTENDED; /* (01h) */
1421 *ptr++ = 3; /* length */
1422 *ptr++ = EXTENDED_SDTR; /* (01h) */
1423 *ptr++ = dcb->min_nego_period; /* Transfer period (in 4ns) */
1424 *ptr++ = dcb->sync_offset; /* Transfer period (max. REQ/ACK dist) */
1425 srb->msg_count += 5;
1426 srb->state |= SRB_DO_SYNC_NEGO;
1427}
1428
1429
1430/* WDTR */
1431static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1432 struct ScsiReqBlk *srb)
1433{
1434 u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1435 (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1436 u8 *ptr = srb->msgout_buf + srb->msg_count;
1437 if (srb->msg_count > 1) {
1438 dprintkl(KERN_INFO,
1439 "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1440 srb->msg_count, srb->msgout_buf[0],
1441 srb->msgout_buf[1]);
1442 return;
1443 }
1444 *ptr++ = MSG_EXTENDED; /* (01h) */
1445 *ptr++ = 2; /* length */
1446 *ptr++ = EXTENDED_WDTR; /* (03h) */
1447 *ptr++ = wide;
1448 srb->msg_count += 4;
1449 srb->state |= SRB_DO_WIDE_NEGO;
1450}
1451
1452
1453#if 0
1454/* Timer to work around chip flaw: When selecting and the bus is
1455 * busy, we sometimes miss a Selection timeout IRQ */
1456void selection_timeout_missed(unsigned long ptr);
1457/* Sets the timer to wake us up */
1458static void selto_timer(struct AdapterCtlBlk *acb)
1459{
1460 if (timer_pending(&acb->selto_timer))
1461 return;
1462 acb->selto_timer.function = selection_timeout_missed;
1463 acb->selto_timer.data = (unsigned long) acb;
1464 if (time_before
1465 (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
1466 acb->selto_timer.expires =
1467 acb->scsi_host->last_reset + HZ / 2 + 1;
1468 else
1469 acb->selto_timer.expires = jiffies + HZ + 1;
1470 add_timer(&acb->selto_timer);
1471}
1472
1473
1474void selection_timeout_missed(unsigned long ptr)
1475{
1476 unsigned long flags;
1477 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
1478 struct ScsiReqBlk *srb;
1479 dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1480 if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1481 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1482 return;
1483 }
1484 DC395x_LOCK_IO(acb->scsi_host, flags);
1485 srb = acb->active_dcb->active_srb;
1486 disconnect(acb);
1487 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1488}
1489#endif
1490
1491
1492static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1493 struct ScsiReqBlk* srb)
1494{
1495 u16 s_stat2, return_code;
1496 u8 s_stat, scsicommand, i, identify_message;
1497 u8 *ptr;
1498 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001499 srb->cmd->serial_number, dcb->target_id, dcb->target_lun, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
1502
1503 s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1504 s_stat2 = 0;
1505 s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1506#if 1
1507 if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1508 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001509 srb->cmd->serial_number, s_stat, s_stat2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * Try anyway?
1512 *
1513 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +02001514 * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * (This is likely to be a bug in the hardware. Obviously, most people
1516 * only have one initiator per SCSI bus.)
1517 * Instead let this fail and have the timer make sure the command is
1518 * tried again after a short time
1519 */
1520 /*selto_timer (acb); */
1521 return 1;
1522 }
1523#endif
1524 if (acb->active_dcb) {
1525 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
1526 "command while another command (pid#%li) is active.",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001527 srb->cmd->serial_number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 acb->active_dcb->active_srb ?
Matthew Wilcox12a44162007-09-18 19:54:43 -06001529 acb->active_dcb->active_srb->cmd->serial_number : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return 1;
1531 }
1532 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1533 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001534 srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return 1;
1536 }
1537 /* Allow starting of SCSI commands half a second before we allow the mid-level
1538 * to queue them again after a reset */
1539 if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
1540 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1541 return 1;
1542 }
1543
1544 /* Flush FIFO */
1545 clear_fifo(acb, "start_scsi");
1546 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1547 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1548 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1549 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1550 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1551
1552 identify_message = dcb->identify_msg;
1553 /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1554 /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1555 if (srb->flag & AUTO_REQSENSE)
1556 identify_message &= 0xBF;
1557
1558 if (((srb->cmd->cmnd[0] == INQUIRY)
1559 || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1560 || (srb->flag & AUTO_REQSENSE))
1561 && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1562 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1563 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1564 && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1565 && (dcb->target_lun == 0)) {
1566 srb->msgout_buf[0] = identify_message;
1567 srb->msg_count = 1;
1568 scsicommand = SCMD_SEL_ATNSTOP;
1569 srb->state = SRB_MSGOUT;
1570#ifndef SYNC_FIRST
1571 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1572 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1573 build_wdtr(acb, dcb, srb);
1574 goto no_cmd;
1575 }
1576#endif
1577 if (dcb->sync_mode & SYNC_NEGO_ENABLE
1578 && dcb->inquiry7 & SCSI_INQ_SYNC) {
1579 build_sdtr(acb, dcb, srb);
1580 goto no_cmd;
1581 }
1582 if (dcb->sync_mode & WIDE_NEGO_ENABLE
1583 && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1584 build_wdtr(acb, dcb, srb);
1585 goto no_cmd;
1586 }
1587 srb->msg_count = 0;
1588 }
1589 /* Send identify message */
1590 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1591
1592 scsicommand = SCMD_SEL_ATN;
1593 srb->state = SRB_START_;
1594#ifndef DC395x_NO_TAGQ
1595 if ((dcb->sync_mode & EN_TAG_QUEUEING)
1596 && (identify_message & 0xC0)) {
1597 /* Send Tag message */
1598 u32 tag_mask = 1;
1599 u8 tag_number = 0;
1600 while (tag_mask & dcb->tag_mask
Roel Kluind8187b92010-08-10 18:01:17 -07001601 && tag_number < dcb->max_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 tag_mask = tag_mask << 1;
1603 tag_number++;
1604 }
1605 if (tag_number >= dcb->max_command) {
1606 dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
1607 "Out of tags target=<%02i-%i>)\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001608 srb->cmd->serial_number, srb->cmd->device->id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 srb->cmd->device->lun);
1610 srb->state = SRB_READY;
1611 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1612 DO_HWRESELECT);
1613 return 1;
1614 }
1615 /* Send Tag id */
1616 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1617 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1618 dcb->tag_mask |= tag_mask;
1619 srb->tag_number = tag_number;
1620 scsicommand = SCMD_SEL_ATN3;
1621 srb->state = SRB_START_;
1622 }
1623#endif
1624/*polling:*/
1625 /* Send CDB ..command block ......... */
1626 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001627 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 srb->cmd->cmnd[0], srb->tag_number);
1629 if (srb->flag & AUTO_REQSENSE) {
1630 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1631 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1632 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1633 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001634 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1636 } else {
1637 ptr = (u8 *)srb->cmd->cmnd;
1638 for (i = 0; i < srb->cmd->cmd_len; i++)
1639 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1640 }
1641 no_cmd:
1642 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1643 DO_HWRESELECT | DO_DATALATCH);
1644 if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1645 /*
1646 * If start_scsi return 1:
1647 * we caught an interrupt (must be reset or reselection ... )
1648 * : Let's process it first!
1649 */
1650 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001651 srb->cmd->serial_number, dcb->target_id, dcb->target_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 srb->state = SRB_READY;
1653 free_tag(dcb, srb);
1654 srb->msg_count = 0;
1655 return_code = 1;
1656 /* This IRQ should NOT get lost, as we did not acknowledge it */
1657 } else {
1658 /*
1659 * If start_scsi returns 0:
1660 * we know that the SCSI processor is free
1661 */
1662 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
1663 dcb->active_srb = srb;
1664 acb->active_dcb = dcb;
1665 return_code = 0;
1666 /* it's important for atn stop */
1667 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1668 DO_DATALATCH | DO_HWRESELECT);
1669 /* SCSI command */
1670 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1671 }
1672 return return_code;
1673}
1674
1675
1676#define DC395x_ENABLE_MSGOUT \
1677 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1678 srb->state |= SRB_MSGOUT
1679
1680
1681/* abort command */
1682static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1683 struct ScsiReqBlk *srb)
1684{
1685 srb->msgout_buf[0] = ABORT;
1686 srb->msg_count = 1;
1687 DC395x_ENABLE_MSGOUT;
1688 srb->state &= ~SRB_MSGIN;
1689 srb->state |= SRB_MSGOUT;
1690}
1691
1692
1693/**
1694 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1695 * have been triggered for this card.
1696 *
1697 * @acb: a pointer to the adpter control block
1698 * @scsi_status: the status return when we checked the card
1699 **/
1700static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1701 u16 scsi_status)
1702{
1703 struct DeviceCtlBlk *dcb;
1704 struct ScsiReqBlk *srb;
1705 u16 phase;
1706 u8 scsi_intstatus;
1707 unsigned long flags;
1708 void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
1709 u16 *);
1710
1711 DC395x_LOCK_IO(acb->scsi_host, flags);
1712
1713 /* This acknowledges the IRQ */
1714 scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1715 if ((scsi_status & 0x2007) == 0x2002)
1716 dprintkl(KERN_DEBUG,
1717 "COP after COP completed? %04x\n", scsi_status);
1718 if (debug_enabled(DBG_KG)) {
1719 if (scsi_intstatus & INT_SELTIMEOUT)
1720 dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1721 }
1722 /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1723
1724 if (timer_pending(&acb->selto_timer))
1725 del_timer(&acb->selto_timer);
1726
1727 if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1728 disconnect(acb); /* bus free interrupt */
1729 goto out_unlock;
1730 }
1731 if (scsi_intstatus & INT_RESELECTED) {
1732 reselect(acb);
1733 goto out_unlock;
1734 }
1735 if (scsi_intstatus & INT_SELECT) {
1736 dprintkl(KERN_INFO, "Host does not support target mode!\n");
1737 goto out_unlock;
1738 }
1739 if (scsi_intstatus & INT_SCSIRESET) {
1740 scsi_reset_detect(acb);
1741 goto out_unlock;
1742 }
1743 if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1744 dcb = acb->active_dcb;
1745 if (!dcb) {
1746 dprintkl(KERN_DEBUG,
1747 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1748 scsi_status, scsi_intstatus);
1749 goto out_unlock;
1750 }
1751 srb = dcb->active_srb;
1752 if (dcb->flag & ABORT_DEV_) {
1753 dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1754 enable_msgout_abort(acb, srb);
1755 }
1756
1757 /* software sequential machine */
1758 phase = (u16)srb->scsi_phase;
1759
1760 /*
1761 * 62037 or 62137
1762 * call dc395x_scsi_phase0[]... "phase entry"
1763 * handle every phase before start transfer
1764 */
1765 /* data_out_phase0, phase:0 */
1766 /* data_in_phase0, phase:1 */
1767 /* command_phase0, phase:2 */
1768 /* status_phase0, phase:3 */
1769 /* nop0, phase:4 PH_BUS_FREE .. initial phase */
1770 /* nop0, phase:5 PH_BUS_FREE .. initial phase */
1771 /* msgout_phase0, phase:6 */
1772 /* msgin_phase0, phase:7 */
1773 dc395x_statev = dc395x_scsi_phase0[phase];
1774 dc395x_statev(acb, srb, &scsi_status);
1775
1776 /*
1777 * if there were any exception occured scsi_status
1778 * will be modify to bus free phase new scsi_status
1779 * transfer out from ... previous dc395x_statev
1780 */
1781 srb->scsi_phase = scsi_status & PHASEMASK;
1782 phase = (u16)scsi_status & PHASEMASK;
1783
1784 /*
1785 * call dc395x_scsi_phase1[]... "phase entry" handle
1786 * every phase to do transfer
1787 */
1788 /* data_out_phase1, phase:0 */
1789 /* data_in_phase1, phase:1 */
1790 /* command_phase1, phase:2 */
1791 /* status_phase1, phase:3 */
1792 /* nop1, phase:4 PH_BUS_FREE .. initial phase */
1793 /* nop1, phase:5 PH_BUS_FREE .. initial phase */
1794 /* msgout_phase1, phase:6 */
1795 /* msgin_phase1, phase:7 */
1796 dc395x_statev = dc395x_scsi_phase1[phase];
1797 dc395x_statev(acb, srb, &scsi_status);
1798 }
1799 out_unlock:
1800 DC395x_UNLOCK_IO(acb->scsi_host, flags);
1801}
1802
1803
David Howells7d12e782006-10-05 14:55:46 +01001804static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -04001806 struct AdapterCtlBlk *acb = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 u16 scsi_status;
1808 u8 dma_status;
1809 irqreturn_t handled = IRQ_NONE;
1810
1811 /*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001812 * Check for pending interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
1814 scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1815 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1816 if (scsi_status & SCSIINTERRUPT) {
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001817 /* interrupt pending - let's process it! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 dc395x_handle_interrupt(acb, scsi_status);
1819 handled = IRQ_HANDLED;
1820 }
1821 else if (dma_status & 0x20) {
1822 /* Error from the DMA engine */
1823 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1824#if 0
1825 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
1826 if (acb->active_dcb) {
1827 acb->active_dcb-> flag |= ABORT_DEV_;
1828 if (acb->active_dcb->active_srb)
1829 enable_msgout_abort(acb, acb->active_dcb->active_srb);
1830 }
1831 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
1832#else
1833 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
1834 acb = NULL;
1835#endif
1836 handled = IRQ_HANDLED;
1837 }
1838
1839 return handled;
1840}
1841
1842
1843static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1844 u16 *pscsi_status)
1845{
Matthew Wilcox12a44162007-09-18 19:54:43 -06001846 dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
1848 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
1849
1850 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1851 srb->state &= ~SRB_MSGOUT;
1852}
1853
1854
1855static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1856 u16 *pscsi_status)
1857{
1858 u16 i;
1859 u8 *ptr;
Matthew Wilcox12a44162007-09-18 19:54:43 -06001860 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 clear_fifo(acb, "msgout_phase1");
1863 if (!(srb->state & SRB_MSGOUT)) {
1864 srb->state |= SRB_MSGOUT;
1865 dprintkl(KERN_DEBUG,
1866 "msgout_phase1: (pid#%li) Phase unexpected\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001867 srb->cmd->serial_number); /* So what ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 if (!srb->msg_count) {
1870 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06001871 srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
1873 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
1874 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1875 return;
1876 }
1877 ptr = (u8 *)srb->msgout_buf;
1878 for (i = 0; i < srb->msg_count; i++)
1879 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1880 srb->msg_count = 0;
1881 if (srb->msgout_buf[0] == MSG_ABORT)
1882 srb->state = SRB_ABORT_SENT;
1883
1884 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1885}
1886
1887
1888static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1889 u16 *pscsi_status)
1890{
Matthew Wilcox12a44162007-09-18 19:54:43 -06001891 dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1893}
1894
1895
1896static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
1897 u16 *pscsi_status)
1898{
1899 struct DeviceCtlBlk *dcb;
1900 u8 *ptr;
1901 u16 i;
Matthew Wilcox12a44162007-09-18 19:54:43 -06001902 dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 clear_fifo(acb, "command_phase1");
1905 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
1906 if (!(srb->flag & AUTO_REQSENSE)) {
1907 ptr = (u8 *)srb->cmd->cmnd;
1908 for (i = 0; i < srb->cmd->cmd_len; i++) {
1909 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
1910 ptr++;
1911 }
1912 } else {
1913 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1914 dcb = acb->active_dcb;
1915 /* target id */
1916 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1917 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1918 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001919 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1921 }
1922 srb->state |= SRB_COMMAND;
1923 /* it's important for atn stop */
1924 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
1925 /* SCSI command */
1926 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
1927}
1928
1929
1930/*
1931 * Verify that the remaining space in the hw sg lists is the same as
1932 * the count of remaining bytes in srb->total_xfer_length
1933 */
1934static void sg_verify_length(struct ScsiReqBlk *srb)
1935{
1936 if (debug_enabled(DBG_SG)) {
1937 unsigned len = 0;
1938 unsigned idx = srb->sg_index;
1939 struct SGentry *psge = srb->segment_x + idx;
1940 for (; idx < srb->sg_count; psge++, idx++)
1941 len += psge->length;
1942 if (len != srb->total_xfer_length)
1943 dprintkdbg(DBG_SG,
1944 "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
1945 srb->total_xfer_length, len);
1946 }
1947}
1948
1949
1950/*
1951 * Compute the next Scatter Gather list index and adjust its length
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02001952 * and address if necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
1954static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
1955{
1956 u8 idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
1958 struct SGentry *psge = srb->segment_x + srb->sg_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 dprintkdbg(DBG_0,
1961 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
1962 xferred, srb->total_xfer_length, left);
1963 if (xferred == 0) {
1964 /* nothing to update since we did not transfer any data */
1965 return;
1966 }
1967
1968 sg_verify_length(srb);
1969 srb->total_xfer_length = left; /* update remaining count */
1970 for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
1971 if (xferred >= psge->length) {
1972 /* Complete SG entries done */
1973 xferred -= psge->length;
1974 } else {
1975 /* Partial SG entry done */
1976 psge->length -= xferred;
1977 psge->address += xferred;
1978 srb->sg_index = idx;
1979 pci_dma_sync_single_for_device(srb->dcb->
1980 acb->dev,
1981 srb->sg_bus_addr,
1982 SEGMENTX_LEN,
1983 PCI_DMA_TODEVICE);
1984 break;
1985 }
1986 psge++;
1987 }
1988 sg_verify_length(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991
1992/*
1993 * We have transfered a single byte (PIO mode?) and need to update
1994 * the count of bytes remaining (total_xfer_length) and update the sg
1995 * entry to either point to next byte in the current sg entry, or of
1996 * already at the end to point to the start of the next sg entry
1997 */
1998static void sg_subtract_one(struct ScsiReqBlk *srb)
1999{
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002000 sg_update_list(srb, srb->total_xfer_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
2003
2004/*
2005 * cleanup_after_transfer
2006 *
2007 * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
2008 * KG: Currently called from StatusPhase1 ()
2009 * Should probably also be called from other places
2010 * Best might be to call it in DataXXPhase0, if new phase will differ
2011 */
2012static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
2013 struct ScsiReqBlk *srb)
2014{
2015 /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
2016 if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
2017 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2018 clear_fifo(acb, "cleanup/in");
2019 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2020 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2021 } else { /* write */
2022 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
2023 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2024 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
2025 clear_fifo(acb, "cleanup/out");
2026 }
2027 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
2028}
2029
2030
2031/*
2032 * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
2033 * Seems to be needed for unknown reasons; could be a hardware bug :-(
2034 */
2035#define DC395x_LASTPIO 4
2036
2037
2038static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2039 u16 *pscsi_status)
2040{
2041 struct DeviceCtlBlk *dcb = srb->dcb;
2042 u16 scsi_status = *pscsi_status;
2043 u32 d_left_counter = 0;
2044 dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002045 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /*
2048 * KG: We need to drain the buffers before we draw any conclusions!
2049 * This means telling the DMA to push the rest into SCSI, telling
2050 * SCSI to push the rest to the bus.
2051 * However, the device might have been the one to stop us (phase
2052 * change), and the data in transit just needs to be accounted so
2053 * it can be retransmitted.)
2054 */
2055 /*
2056 * KG: Stop DMA engine pushing more data into the SCSI FIFO
2057 * If we need more data, the DMA SG list will be freshly set up, anyway
2058 */
2059 dprintkdbg(DBG_PIO, "data_out_phase0: "
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002060 "DMA{fifocnt=0x%02x fifostat=0x%02x} "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
2062 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2063 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2064 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2065 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
2066 srb->total_xfer_length);
2067 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
2068
2069 if (!(srb->state & SRB_XFERPAD)) {
2070 if (scsi_status & PARITYERROR)
2071 srb->status |= PARITY_ERROR;
2072
2073 /*
2074 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
2075 * is the no of bytes it got from the DMA engine not the no it
2076 * transferred successfully to the device. (And the difference could
2077 * be as much as the FIFO size, I guess ...)
2078 */
2079 if (!(scsi_status & SCSIXFERDONE)) {
2080 /*
2081 * when data transfer from DMA FIFO to SCSI FIFO
2082 * if there was some data left in SCSI FIFO
2083 */
2084 d_left_counter =
2085 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2086 0x1F);
2087 if (dcb->sync_period & WIDE_SYNC)
2088 d_left_counter <<= 1;
2089
2090 dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
2091 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
2092 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
2093 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2094 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2095 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
2096 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
2097 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2098 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2099 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
2100 }
2101 /*
Linus Torvaldsb4b08e52005-08-14 15:43:39 -07002102 * calculate all the residue data that not yet tranfered
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 * SCSI transfer counter + left in SCSI FIFO data
2104 *
2105 * .....TRM_S1040_SCSI_COUNTER (24bits)
2106 * The counter always decrement by one for every SCSI byte transfer.
2107 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
2108 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
2109 */
2110 if (srb->total_xfer_length > DC395x_LASTPIO)
2111 d_left_counter +=
2112 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2113
2114 /* Is this a good idea? */
2115 /*clear_fifo(acb, "DOP1"); */
2116 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
2117 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002118 && scsi_bufflen(srb->cmd) % 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 d_left_counter = 0;
2120 dprintkl(KERN_INFO,
2121 "data_out_phase0: Discard 1 byte (0x%02x)\n",
2122 scsi_status);
2123 }
2124 /*
2125 * KG: Oops again. Same thinko as above: The SCSI might have been
2126 * faster than the DMA engine, so that it ran out of data.
2127 * In that case, we have to do just nothing!
2128 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
2129 */
2130 /*
2131 * KG: This is nonsense: We have been WRITING data to the bus
2132 * If the SCSI engine has no bytes left, how should the DMA engine?
2133 */
2134 if (d_left_counter == 0) {
2135 srb->total_xfer_length = 0;
2136 } else {
2137 /*
2138 * if transfer not yet complete
2139 * there were some data residue in SCSI FIFO or
2140 * SCSI transfer counter not empty
2141 */
2142 long oldxferred =
2143 srb->total_xfer_length - d_left_counter;
2144 const int diff =
2145 (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
2146 sg_update_list(srb, d_left_counter);
2147 /* KG: Most ugly hack! Apparently, this works around a chip bug */
2148 if ((srb->segment_x[srb->sg_index].length ==
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002149 diff && scsi_sg_count(srb->cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 || ((oldxferred & ~PAGE_MASK) ==
2151 (PAGE_SIZE - diff))
2152 ) {
2153 dprintkl(KERN_INFO, "data_out_phase0: "
2154 "Work around chip bug (%i)?\n", diff);
2155 d_left_counter =
2156 srb->total_xfer_length - diff;
2157 sg_update_list(srb, d_left_counter);
2158 /*srb->total_xfer_length -= diff; */
2159 /*srb->virt_addr += diff; */
2160 /*if (srb->cmd->use_sg) */
2161 /* srb->sg_index++; */
2162 }
2163 }
2164 }
2165 if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
2166 cleanup_after_transfer(acb, srb);
2167 }
2168}
2169
2170
2171static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2172 u16 *pscsi_status)
2173{
2174 dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002175 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 clear_fifo(acb, "data_out_phase1");
2177 /* do prepare before transfer when data out phase */
2178 data_io_transfer(acb, srb, XFERDATAOUT);
2179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2182 u16 *pscsi_status)
2183{
2184 u16 scsi_status = *pscsi_status;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002187 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /*
2190 * KG: DataIn is much more tricky than DataOut. When the device is finished
2191 * and switches to another phase, the SCSI engine should be finished too.
2192 * But: There might still be bytes left in its FIFO to be fetched by the DMA
2193 * engine and transferred to memory.
2194 * We should wait for the FIFOs to be emptied by that (is there any way to
2195 * enforce this?) and then stop the DMA engine, because it might think, that
2196 * there are more bytes to follow. Yes, the device might disconnect prior to
2197 * having all bytes transferred!
2198 * Also we should make sure that all data from the DMA engine buffer's really
2199 * made its way to the system memory! Some documentation on this would not
2200 * seem to be a bad idea, actually.
2201 */
2202 if (!(srb->state & SRB_XFERPAD)) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002203 u32 d_left_counter;
2204 unsigned int sc, fc;
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (scsi_status & PARITYERROR) {
2207 dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
Matthew Wilcox12a44162007-09-18 19:54:43 -06002208 "Parity Error\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 srb->status |= PARITY_ERROR;
2210 }
2211 /*
2212 * KG: We should wait for the DMA FIFO to be empty ...
2213 * but: it would be better to wait first for the SCSI FIFO and then the
2214 * the DMA FIFO to become empty? How do we know, that the device not already
2215 * sent data to the FIFO in a MsgIn phase, eg.?
2216 */
2217 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
2218#if 0
2219 int ctr = 6000000;
2220 dprintkl(KERN_DEBUG,
2221 "DIP0: Wait for DMA FIFO to flush ...\n");
2222 /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
2223 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
2224 /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
2225 while (!
2226 (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
2227 0x80) && --ctr);
2228 if (ctr < 6000000 - 1)
2229 dprintkl(KERN_DEBUG
2230 "DIP0: Had to wait for DMA ...\n");
2231 if (!ctr)
2232 dprintkl(KERN_ERR,
2233 "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
2234 /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
2235#endif
2236 dprintkdbg(DBG_KG, "data_in_phase0: "
2237 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
2238 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
2239 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
2240 }
2241 /* Now: Check remainig data: The SCSI counters should tell us ... */
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002242 sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2243 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2244 d_left_counter = sc + ((fc & 0x1f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
2246 0));
2247 dprintkdbg(DBG_KG, "data_in_phase0: "
2248 "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
2249 "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
2250 "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002251 fc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002253 sc,
2254 fc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
2256 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
2257 srb->total_xfer_length, d_left_counter);
2258#if DC395x_LASTPIO
2259 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
2260 if (d_left_counter
2261 && srb->total_xfer_length <= DC395x_LASTPIO) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002262 size_t left_io = srb->total_xfer_length;
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /*u32 addr = (srb->segment_x[srb->sg_index].address); */
2265 /*sg_update_list (srb, d_left_counter); */
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002266 dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
2267 "for remaining %i bytes:",
2268 fc & 0x1f,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 (srb->dcb->sync_period & WIDE_SYNC) ?
2270 "words" : "bytes",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 srb->total_xfer_length);
2272 if (srb->dcb->sync_period & WIDE_SYNC)
2273 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2274 CFG2_WIDEFIFO);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002275 while (left_io) {
2276 unsigned char *virt, *base = NULL;
2277 unsigned long flags = 0;
2278 size_t len = left_io;
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002279 size_t offset = srb->request_length - left_io;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002280
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002281 local_irq_save(flags);
2282 /* Assumption: it's inside one page as it's at most 4 bytes and
2283 I just assume it's on a 4-byte boundary */
2284 base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2285 srb->sg_count, &offset, &len);
2286 virt = base + offset;
2287
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002288 left_io -= len;
2289
2290 while (len) {
2291 u8 byte;
2292 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2293 *virt++ = byte;
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (debug_enabled(DBG_PIO))
2296 printk(" %02x", byte);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002297
2298 d_left_counter--;
2299 sg_subtract_one(srb);
2300
2301 len--;
2302
2303 fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2304
2305 if (fc == 0x40) {
2306 left_io = 0;
2307 break;
2308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002310
2311 WARN_ON((fc != 0x40) == !d_left_counter);
2312
2313 if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
2314 /* Read the last byte ... */
2315 if (srb->total_xfer_length > 0) {
2316 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2317
2318 *virt++ = byte;
2319 srb->total_xfer_length--;
2320 if (debug_enabled(DBG_PIO))
2321 printk(" %02x", byte);
2322 }
2323
2324 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2325 }
2326
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002327 scsi_kunmap_atomic_sg(base);
2328 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
2331 /*srb->total_xfer_length = 0; */
2332 if (debug_enabled(DBG_PIO))
2333 printk("\n");
2334 }
2335#endif /* DC395x_LASTPIO */
2336
2337#if 0
2338 /*
2339 * KG: This was in DATAOUT. Does it also belong here?
2340 * Nobody seems to know what counter and fifo_cnt count exactly ...
2341 */
2342 if (!(scsi_status & SCSIXFERDONE)) {
2343 /*
2344 * when data transfer from DMA FIFO to SCSI FIFO
2345 * if there was some data left in SCSI FIFO
2346 */
2347 d_left_counter =
2348 (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
2349 0x1F);
2350 if (srb->dcb->sync_period & WIDE_SYNC)
2351 d_left_counter <<= 1;
2352 /*
2353 * if WIDE scsi SCSI FIFOCNT unit is word !!!
2354 * so need to *= 2
2355 * KG: Seems to be correct ...
2356 */
2357 }
2358#endif
2359 /* KG: This should not be needed any more! */
2360 if (d_left_counter == 0
2361 || (scsi_status & SCSIXFERCNT_2_ZERO)) {
2362#if 0
2363 int ctr = 6000000;
2364 u8 TempDMAstatus;
2365 do {
2366 TempDMAstatus =
2367 DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2368 } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
2369 if (!ctr)
2370 dprintkl(KERN_ERR,
2371 "Deadlock in DataInPhase0 waiting for DMA!!\n");
2372 srb->total_xfer_length = 0;
2373#endif
2374 srb->total_xfer_length = d_left_counter;
2375 } else { /* phase changed */
2376 /*
2377 * parsing the case:
2378 * when a transfer not yet complete
2379 * but be disconnected by target
2380 * if transfer not yet complete
2381 * there were some data residue in SCSI FIFO or
2382 * SCSI transfer counter not empty
2383 */
2384 sg_update_list(srb, d_left_counter);
2385 }
2386 }
2387 /* KG: The target may decide to disconnect: Empty FIFO before! */
2388 if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
2389 cleanup_after_transfer(acb, srb);
2390 }
2391}
2392
2393
2394static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2395 u16 *pscsi_status)
2396{
2397 dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002398 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 data_io_transfer(acb, srb, XFERDATAIN);
2400}
2401
2402
2403static void data_io_transfer(struct AdapterCtlBlk *acb,
2404 struct ScsiReqBlk *srb, u16 io_dir)
2405{
2406 struct DeviceCtlBlk *dcb = srb->dcb;
2407 u8 bval;
2408 dprintkdbg(DBG_0,
2409 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002410 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
2412 srb->total_xfer_length, srb->sg_index, srb->sg_count);
2413 if (srb == acb->tmp_srb)
2414 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
2415 if (srb->sg_index >= srb->sg_count) {
2416 /* can't happen? out of bounds error */
2417 return;
2418 }
2419
2420 if (srb->total_xfer_length > DC395x_LASTPIO) {
2421 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
2422 /*
2423 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
2424 * Maybe, even ABORTXFER would be appropriate
2425 */
2426 if (dma_status & XFERPENDING) {
2427 dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
2428 "Expect trouble!\n");
2429 dump_register_info(acb, dcb, srb);
2430 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
2431 }
2432 /* clear_fifo(acb, "IO"); */
2433 /*
2434 * load what physical address of Scatter/Gather list table
2435 * want to be transfer
2436 */
2437 srb->state |= SRB_DATA_XFER;
2438 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002439 if (scsi_sg_count(srb->cmd)) { /* with S/G */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 io_dir |= DMACMD_SG;
2441 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2442 srb->sg_bus_addr +
2443 sizeof(struct SGentry) *
2444 srb->sg_index);
2445 /* load how many bytes in the sg list table */
2446 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2447 ((u32)(srb->sg_count -
2448 srb->sg_index) << 3));
2449 } else { /* without S/G */
2450 io_dir &= ~DMACMD_SG;
2451 DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
2452 srb->segment_x[0].address);
2453 DC395x_write32(acb, TRM_S1040_DMA_XCNT,
2454 srb->segment_x[0].length);
2455 }
2456 /* load total transfer length (24bits) max value 16Mbyte */
2457 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2458 srb->total_xfer_length);
2459 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2460 if (io_dir & DMACMD_DIR) { /* read */
2461 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2462 SCMD_DMA_IN);
2463 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2464 } else {
2465 DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
2466 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2467 SCMD_DMA_OUT);
2468 }
2469
2470 }
2471#if DC395x_LASTPIO
2472 else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
2473 /*
2474 * load what physical address of Scatter/Gather list table
2475 * want to be transfer
2476 */
2477 srb->state |= SRB_DATA_XFER;
2478 /* load total transfer length (24bits) max value 16Mbyte */
2479 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
2480 srb->total_xfer_length);
2481 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2482 if (io_dir & DMACMD_DIR) { /* read */
2483 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2484 SCMD_FIFO_IN);
2485 } else { /* write */
2486 int ln = srb->total_xfer_length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002487 size_t left_io = srb->total_xfer_length;
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (srb->dcb->sync_period & WIDE_SYNC)
2490 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2491 CFG2_WIDEFIFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002493 while (left_io) {
2494 unsigned char *virt, *base = NULL;
2495 unsigned long flags = 0;
2496 size_t len = left_io;
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002497 size_t offset = srb->request_length - left_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002499 local_irq_save(flags);
2500 /* Again, max 4 bytes */
2501 base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2502 srb->sg_count, &offset, &len);
2503 virt = base + offset;
2504
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002505 left_io -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02002507 while (len--) {
2508 if (debug_enabled(DBG_PIO))
2509 printk(" %02x", *virt);
2510
2511 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
2512
2513 sg_subtract_one(srb);
2514 }
2515
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09002516 scsi_kunmap_atomic_sg(base);
2517 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
2519 if (srb->dcb->sync_period & WIDE_SYNC) {
2520 if (ln % 2) {
2521 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
2522 if (debug_enabled(DBG_PIO))
2523 printk(" |00");
2524 }
2525 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2526 }
2527 /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
2528 if (debug_enabled(DBG_PIO))
2529 printk("\n");
2530 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
2531 SCMD_FIFO_OUT);
2532 }
2533 }
2534#endif /* DC395x_LASTPIO */
2535 else { /* xfer pad */
2536 u8 data = 0, data2 = 0;
2537 if (srb->sg_count) {
2538 srb->adapter_status = H_OVER_UNDER_RUN;
2539 srb->status |= OVER_RUN;
2540 }
2541 /*
2542 * KG: despite the fact that we are using 16 bits I/O ops
2543 * the SCSI FIFO is only 8 bits according to the docs
2544 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
2545 */
2546 if (dcb->sync_period & WIDE_SYNC) {
2547 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
2548 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
2549 CFG2_WIDEFIFO);
2550 if (io_dir & DMACMD_DIR) {
2551 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2552 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2553 } else {
2554 /* Danger, Robinson: If you find KGs
2555 * scattered over the wide disk, the driver
2556 * or chip is to blame :-( */
2557 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2558 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
2559 }
2560 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
2561 } else {
2562 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2563 /* Danger, Robinson: If you find a collection of Ks on your disk
2564 * something broke :-( */
2565 if (io_dir & DMACMD_DIR)
2566 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2567 else
2568 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
2569 }
2570 srb->state |= SRB_XFERPAD;
2571 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2572 /* SCSI command */
2573 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
2574 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
2575 }
2576}
2577
2578
2579static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2580 u16 *pscsi_status)
2581{
2582 dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002583 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2585 srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
2586 srb->state = SRB_COMPLETED;
2587 *pscsi_status = PH_BUS_FREE; /*.. initial phase */
2588 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2589 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2590}
2591
2592
2593static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2594 u16 *pscsi_status)
2595{
2596 dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002597 srb->cmd->serial_number, srb->cmd->device->id, srb->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 srb->state = SRB_STATUS;
2599 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2600 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
2601}
2602
2603
2604/* Check if the message is complete */
2605static inline u8 msgin_completed(u8 * msgbuf, u32 len)
2606{
2607 if (*msgbuf == EXTENDED_MESSAGE) {
2608 if (len < 2)
2609 return 0;
2610 if (len < msgbuf[1] + 2)
2611 return 0;
2612 } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
2613 if (len < 2)
2614 return 0;
2615 return 1;
2616}
2617
2618/* reject_msg */
2619static inline void msgin_reject(struct AdapterCtlBlk *acb,
2620 struct ScsiReqBlk *srb)
2621{
2622 srb->msgout_buf[0] = MESSAGE_REJECT;
2623 srb->msg_count = 1;
2624 DC395x_ENABLE_MSGOUT;
2625 srb->state &= ~SRB_MSGIN;
2626 srb->state |= SRB_MSGOUT;
2627 dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
2628 srb->msgin_buf[0],
2629 srb->dcb->target_id, srb->dcb->target_lun);
2630}
2631
2632
2633static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
2634 struct DeviceCtlBlk *dcb, u8 tag)
2635{
2636 struct ScsiReqBlk *srb = NULL;
2637 struct ScsiReqBlk *i;
2638 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002639 srb->cmd->serial_number, tag, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 if (!(dcb->tag_mask & (1 << tag)))
2642 dprintkl(KERN_DEBUG,
2643 "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
2644 dcb->tag_mask, tag);
2645
2646 if (list_empty(&dcb->srb_going_list))
2647 goto mingx0;
2648 list_for_each_entry(i, &dcb->srb_going_list, list) {
2649 if (i->tag_number == tag) {
2650 srb = i;
2651 break;
2652 }
2653 }
2654 if (!srb)
2655 goto mingx0;
2656
2657 dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002658 srb->cmd->serial_number, srb->dcb->target_id, srb->dcb->target_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (dcb->flag & ABORT_DEV_) {
2660 /*srb->state = SRB_ABORT_SENT; */
2661 enable_msgout_abort(acb, srb);
2662 }
2663
2664 if (!(srb->state & SRB_DISCONNECT))
2665 goto mingx0;
2666
2667 memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
2668 srb->state |= dcb->active_srb->state;
2669 srb->state |= SRB_DATA_XFER;
2670 dcb->active_srb = srb;
2671 /* How can we make the DORS happy? */
2672 return srb;
2673
2674 mingx0:
2675 srb = acb->tmp_srb;
2676 srb->state = SRB_UNEXPECT_RESEL;
2677 dcb->active_srb = srb;
2678 srb->msgout_buf[0] = MSG_ABORT_TAG;
2679 srb->msg_count = 1;
2680 DC395x_ENABLE_MSGOUT;
2681 dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
2682 return srb;
2683}
2684
2685
2686static inline void reprogram_regs(struct AdapterCtlBlk *acb,
2687 struct DeviceCtlBlk *dcb)
2688{
2689 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
2690 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
2691 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
2692 set_xfer_rate(acb, dcb);
2693}
2694
2695
2696/* set async transfer mode */
2697static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2698{
2699 struct DeviceCtlBlk *dcb = srb->dcb;
2700 dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
2701 dcb->target_id, dcb->target_lun);
2702
2703 dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
2704 dcb->sync_mode |= SYNC_NEGO_DONE;
2705 /*dcb->sync_period &= 0; */
2706 dcb->sync_offset = 0;
2707 dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
2708 srb->state &= ~SRB_DO_SYNC_NEGO;
2709 reprogram_regs(acb, dcb);
2710 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2711 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2712 build_wdtr(acb, dcb, srb);
2713 DC395x_ENABLE_MSGOUT;
2714 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
2715 }
2716}
2717
2718
2719/* set sync transfer mode */
2720static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2721{
2722 struct DeviceCtlBlk *dcb = srb->dcb;
2723 u8 bval;
2724 int fact;
2725 dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
2726 "(%02i.%01i MHz) Offset %i\n",
2727 dcb->target_id, srb->msgin_buf[3] << 2,
2728 (250 / srb->msgin_buf[3]),
2729 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
2730 srb->msgin_buf[4]);
2731
2732 if (srb->msgin_buf[4] > 15)
2733 srb->msgin_buf[4] = 15;
2734 if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
2735 dcb->sync_offset = 0;
2736 else if (dcb->sync_offset == 0)
2737 dcb->sync_offset = srb->msgin_buf[4];
2738 if (srb->msgin_buf[4] > dcb->sync_offset)
2739 srb->msgin_buf[4] = dcb->sync_offset;
2740 else
2741 dcb->sync_offset = srb->msgin_buf[4];
2742 bval = 0;
2743 while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
2744 || dcb->min_nego_period >
2745 clock_period[bval]))
2746 bval++;
2747 if (srb->msgin_buf[3] < clock_period[bval])
2748 dprintkl(KERN_INFO,
2749 "msgin_set_sync: Increase sync nego period to %ins\n",
2750 clock_period[bval] << 2);
2751 srb->msgin_buf[3] = clock_period[bval];
2752 dcb->sync_period &= 0xf0;
2753 dcb->sync_period |= ALT_SYNC | bval;
2754 dcb->min_nego_period = srb->msgin_buf[3];
2755
2756 if (dcb->sync_period & WIDE_SYNC)
2757 fact = 500;
2758 else
2759 fact = 250;
2760
2761 dprintkl(KERN_INFO,
2762 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
2763 dcb->target_id, (fact == 500) ? "Wide16" : "",
2764 dcb->min_nego_period << 2, dcb->sync_offset,
2765 (fact / dcb->min_nego_period),
2766 ((fact % dcb->min_nego_period) * 10 +
2767 dcb->min_nego_period / 2) / dcb->min_nego_period);
2768
2769 if (!(srb->state & SRB_DO_SYNC_NEGO)) {
2770 /* Reply with corrected SDTR Message */
2771 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
2772 srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
2773
2774 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
2775 srb->msg_count = 5;
2776 DC395x_ENABLE_MSGOUT;
2777 dcb->sync_mode |= SYNC_NEGO_DONE;
2778 } else {
2779 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
2780 && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
2781 build_wdtr(acb, dcb, srb);
2782 DC395x_ENABLE_MSGOUT;
2783 dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
2784 }
2785 }
2786 srb->state &= ~SRB_DO_SYNC_NEGO;
2787 dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
2788
2789 reprogram_regs(acb, dcb);
2790}
2791
2792
2793static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
2794 struct ScsiReqBlk *srb)
2795{
2796 struct DeviceCtlBlk *dcb = srb->dcb;
2797 dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
2798
2799 dcb->sync_period &= ~WIDE_SYNC;
2800 dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
2801 dcb->sync_mode |= WIDE_NEGO_DONE;
2802 srb->state &= ~SRB_DO_WIDE_NEGO;
2803 reprogram_regs(acb, dcb);
2804 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2805 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2806 build_sdtr(acb, dcb, srb);
2807 DC395x_ENABLE_MSGOUT;
2808 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
2809 }
2810}
2811
2812static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
2813{
2814 struct DeviceCtlBlk *dcb = srb->dcb;
2815 u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
2816 && acb->config & HCC_WIDE_CARD) ? 1 : 0;
2817 dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
2818
2819 if (srb->msgin_buf[3] > wide)
2820 srb->msgin_buf[3] = wide;
2821 /* Completed */
2822 if (!(srb->state & SRB_DO_WIDE_NEGO)) {
2823 dprintkl(KERN_DEBUG,
2824 "msgin_set_wide: Wide nego initiated <%02i>\n",
2825 dcb->target_id);
2826 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
2827 srb->msg_count = 4;
2828 srb->state |= SRB_DO_WIDE_NEGO;
2829 DC395x_ENABLE_MSGOUT;
2830 }
2831
2832 dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
2833 if (srb->msgin_buf[3] > 0)
2834 dcb->sync_period |= WIDE_SYNC;
2835 else
2836 dcb->sync_period &= ~WIDE_SYNC;
2837 srb->state &= ~SRB_DO_WIDE_NEGO;
2838 /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
2839 dprintkdbg(DBG_1,
2840 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
2841 (8 << srb->msgin_buf[3]), dcb->target_id);
2842 reprogram_regs(acb, dcb);
2843 if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
2844 && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
2845 build_sdtr(acb, dcb, srb);
2846 DC395x_ENABLE_MSGOUT;
2847 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
2848 }
2849}
2850
2851
2852/*
2853 * extended message codes:
2854 *
2855 * code description
2856 *
2857 * 02h Reserved
2858 * 00h MODIFY DATA POINTER
2859 * 01h SYNCHRONOUS DATA TRANSFER REQUEST
2860 * 03h WIDE DATA TRANSFER REQUEST
2861 * 04h - 7Fh Reserved
2862 * 80h - FFh Vendor specific
2863 */
2864static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2865 u16 *pscsi_status)
2866{
2867 struct DeviceCtlBlk *dcb = acb->active_dcb;
Matthew Wilcox12a44162007-09-18 19:54:43 -06002868 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2871 if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
2872 /* Now eval the msg */
2873 switch (srb->msgin_buf[0]) {
2874 case DISCONNECT:
2875 srb->state = SRB_DISCONNECT;
2876 break;
2877
2878 case SIMPLE_QUEUE_TAG:
2879 case HEAD_OF_QUEUE_TAG:
2880 case ORDERED_QUEUE_TAG:
2881 srb =
2882 msgin_qtag(acb, dcb,
2883 srb->msgin_buf[1]);
2884 break;
2885
2886 case MESSAGE_REJECT:
2887 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
2888 DO_CLRATN | DO_DATALATCH);
2889 /* A sync nego message was rejected ! */
2890 if (srb->state & SRB_DO_SYNC_NEGO) {
2891 msgin_set_async(acb, srb);
2892 break;
2893 }
2894 /* A wide nego message was rejected ! */
2895 if (srb->state & SRB_DO_WIDE_NEGO) {
2896 msgin_set_nowide(acb, srb);
2897 break;
2898 }
2899 enable_msgout_abort(acb, srb);
2900 /*srb->state |= SRB_ABORT_SENT */
2901 break;
2902
2903 case EXTENDED_MESSAGE:
2904 /* SDTR */
2905 if (srb->msgin_buf[1] == 3
2906 && srb->msgin_buf[2] == EXTENDED_SDTR) {
2907 msgin_set_sync(acb, srb);
2908 break;
2909 }
2910 /* WDTR */
2911 if (srb->msgin_buf[1] == 2
2912 && srb->msgin_buf[2] == EXTENDED_WDTR
2913 && srb->msgin_buf[3] <= 2) { /* sanity check ... */
2914 msgin_set_wide(acb, srb);
2915 break;
2916 }
2917 msgin_reject(acb, srb);
2918 break;
2919
2920 case MSG_IGNOREWIDE:
2921 /* Discard wide residual */
2922 dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
2923 break;
2924
2925 case COMMAND_COMPLETE:
2926 /* nothing has to be done */
2927 break;
2928
2929 case SAVE_POINTERS:
2930 /*
2931 * SAVE POINTER may be ignored as we have the struct
2932 * ScsiReqBlk* associated with the scsi command.
2933 */
2934 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2935 "SAVE POINTER rem=%i Ignore\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002936 srb->cmd->serial_number, srb->total_xfer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 break;
2938
2939 case RESTORE_POINTERS:
2940 dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
2941 break;
2942
2943 case ABORT:
2944 dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
2945 "<%02i-%i> ABORT msg\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06002946 srb->cmd->serial_number, dcb->target_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 dcb->target_lun);
2948 dcb->flag |= ABORT_DEV_;
2949 enable_msgout_abort(acb, srb);
2950 break;
2951
2952 default:
2953 /* reject unknown messages */
2954 if (srb->msgin_buf[0] & IDENTIFY_BASE) {
2955 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
2956 srb->msg_count = 1;
2957 srb->msgout_buf[0] = dcb->identify_msg;
2958 DC395x_ENABLE_MSGOUT;
2959 srb->state |= SRB_MSGOUT;
2960 /*break; */
2961 }
2962 msgin_reject(acb, srb);
2963 }
2964
2965 /* Clear counter and MsgIn state */
2966 srb->state &= ~SRB_MSGIN;
2967 acb->msg_len = 0;
2968 }
2969 *pscsi_status = PH_BUS_FREE;
2970 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
2971 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
2972}
2973
2974
2975static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2976 u16 *pscsi_status)
2977{
Matthew Wilcox12a44162007-09-18 19:54:43 -06002978 dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 clear_fifo(acb, "msgin_phase1");
2980 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
2981 if (!(srb->state & SRB_MSGIN)) {
2982 srb->state &= ~SRB_DISCONNECT;
2983 srb->state |= SRB_MSGIN;
2984 }
2985 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
2986 /* SCSI command */
2987 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
2988}
2989
2990
2991static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2992 u16 *pscsi_status)
2993{
2994}
2995
2996
2997static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2998 u16 *pscsi_status)
2999{
3000}
3001
3002
3003static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
3004{
3005 struct DeviceCtlBlk *i;
3006
3007 /* set all lun device's period, offset */
3008 if (dcb->identify_msg & 0x07)
3009 return;
3010
3011 if (acb->scan_devices) {
3012 current_sync_offset = dcb->sync_offset;
3013 return;
3014 }
3015
3016 list_for_each_entry(i, &acb->dcb_list, list)
3017 if (i->target_id == dcb->target_id) {
3018 i->sync_period = dcb->sync_period;
3019 i->sync_offset = dcb->sync_offset;
3020 i->sync_mode = dcb->sync_mode;
3021 i->min_nego_period = dcb->min_nego_period;
3022 }
3023}
3024
3025
3026static void disconnect(struct AdapterCtlBlk *acb)
3027{
3028 struct DeviceCtlBlk *dcb = acb->active_dcb;
3029 struct ScsiReqBlk *srb;
3030
3031 if (!dcb) {
3032 dprintkl(KERN_ERR, "disconnect: No such device\n");
3033 udelay(500);
3034 /* Suspend queue for a while */
3035 acb->scsi_host->last_reset =
3036 jiffies + HZ / 2 +
3037 HZ * acb->eeprom.delay_time;
3038 clear_fifo(acb, "disconnectEx");
3039 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3040 return;
3041 }
3042 srb = dcb->active_srb;
3043 acb->active_dcb = NULL;
Matthew Wilcox12a44162007-09-18 19:54:43 -06003044 dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3047 clear_fifo(acb, "disconnect");
3048 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
3049 if (srb->state & SRB_UNEXPECT_RESEL) {
3050 dprintkl(KERN_ERR,
3051 "disconnect: Unexpected reselection <%02i-%i>\n",
3052 dcb->target_id, dcb->target_lun);
3053 srb->state = 0;
3054 waiting_process_next(acb);
3055 } else if (srb->state & SRB_ABORT_SENT) {
3056 dcb->flag &= ~ABORT_DEV_;
3057 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
3058 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
3059 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
3060 waiting_process_next(acb);
3061 } else {
3062 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
3063 || !(srb->
3064 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
3065 /*
3066 * Selection time out
3067 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
3068 */
3069 /* Unexp. Disc / Sel Timeout */
3070 if (srb->state != SRB_START_
3071 && srb->state != SRB_MSGOUT) {
3072 srb->state = SRB_READY;
3073 dprintkl(KERN_DEBUG,
3074 "disconnect: (pid#%li) Unexpected\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06003075 srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
3077 goto disc1;
3078 } else {
3079 /* Normal selection timeout */
3080 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
Matthew Wilcox12a44162007-09-18 19:54:43 -06003081 "<%02i-%i> SelTO\n", srb->cmd->serial_number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 dcb->target_id, dcb->target_lun);
3083 if (srb->retry_count++ > DC395x_MAX_RETRIES
3084 || acb->scan_devices) {
3085 srb->target_status =
3086 SCSI_STAT_SEL_TIMEOUT;
3087 goto disc1;
3088 }
3089 free_tag(dcb, srb);
3090 srb_going_to_waiting_move(dcb, srb);
3091 dprintkdbg(DBG_KG,
3092 "disconnect: (pid#%li) Retry\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06003093 srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 waiting_set_timer(acb, HZ / 20);
3095 }
3096 } else if (srb->state & SRB_DISCONNECT) {
3097 u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
3098 /*
3099 * SRB_DISCONNECT (This is what we expect!)
3100 */
3101 if (bval & 0x40) {
3102 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
3103 " 0x%02x: ACK set! Other controllers?\n",
3104 bval);
3105 /* It could come from another initiator, therefore don't do much ! */
3106 } else
3107 waiting_process_next(acb);
3108 } else if (srb->state & SRB_COMPLETED) {
3109 disc1:
3110 /*
3111 ** SRB_COMPLETED
3112 */
3113 free_tag(dcb, srb);
3114 dcb->active_srb = NULL;
3115 srb->state = SRB_FREE;
3116 srb_done(acb, dcb, srb);
3117 }
3118 }
3119}
3120
3121
3122static void reselect(struct AdapterCtlBlk *acb)
3123{
3124 struct DeviceCtlBlk *dcb = acb->active_dcb;
3125 struct ScsiReqBlk *srb = NULL;
3126 u16 rsel_tar_lun_id;
3127 u8 id, lun;
3128 u8 arblostflag = 0;
3129 dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
3130
3131 clear_fifo(acb, "reselect");
3132 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
3133 /* Read Reselected Target ID and LUN */
3134 rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
3135 if (dcb) { /* Arbitration lost but Reselection win */
3136 srb = dcb->active_srb;
3137 if (!srb) {
3138 dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
3139 "but active_srb == NULL\n");
3140 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3141 return;
3142 }
3143 /* Why the if ? */
3144 if (!acb->scan_devices) {
3145 dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
3146 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06003147 srb->cmd->serial_number, dcb->target_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 dcb->target_lun, rsel_tar_lun_id,
3149 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
3150 arblostflag = 1;
3151 /*srb->state |= SRB_DISCONNECT; */
3152
3153 srb->state = SRB_READY;
3154 free_tag(dcb, srb);
3155 srb_going_to_waiting_move(dcb, srb);
3156 waiting_set_timer(acb, HZ / 20);
3157
3158 /* return; */
3159 }
3160 }
3161 /* Read Reselected Target Id and LUN */
3162 if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
3163 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
3164 "Got %i!\n", rsel_tar_lun_id);
3165 id = rsel_tar_lun_id & 0xff;
3166 lun = (rsel_tar_lun_id >> 8) & 7;
3167 dcb = find_dcb(acb, id, lun);
3168 if (!dcb) {
3169 dprintkl(KERN_ERR, "reselect: From non existent device "
3170 "<%02i-%i>\n", id, lun);
3171 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3172 return;
3173 }
3174 acb->active_dcb = dcb;
3175
3176 if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
3177 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
3178 "disconnection? <%02i-%i>\n",
3179 dcb->target_id, dcb->target_lun);
3180
3181 if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
3182 srb = acb->tmp_srb;
3183 dcb->active_srb = srb;
3184 } else {
3185 /* There can be only one! */
3186 srb = dcb->active_srb;
3187 if (!srb || !(srb->state & SRB_DISCONNECT)) {
3188 /*
3189 * abort command
3190 */
3191 dprintkl(KERN_DEBUG,
3192 "reselect: w/o disconnected cmds <%02i-%i>\n",
3193 dcb->target_id, dcb->target_lun);
3194 srb = acb->tmp_srb;
3195 srb->state = SRB_UNEXPECT_RESEL;
3196 dcb->active_srb = srb;
3197 enable_msgout_abort(acb, srb);
3198 } else {
3199 if (dcb->flag & ABORT_DEV_) {
3200 /*srb->state = SRB_ABORT_SENT; */
3201 enable_msgout_abort(acb, srb);
3202 } else
3203 srb->state = SRB_DATA_XFER;
3204
3205 }
3206 }
3207 srb->scsi_phase = PH_BUS_FREE; /* initial phase */
3208
3209 /* Program HA ID, target ID, period and offset */
3210 dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
3211 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
3212 DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
3213 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
3214 DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
3215 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
3216 /* SCSI command */
3217 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
3218}
3219
3220
3221static inline u8 tagq_blacklist(char *name)
3222{
3223#ifndef DC395x_NO_TAGQ
3224#if 0
3225 u8 i;
3226 for (i = 0; i < BADDEVCNT; i++)
3227 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
3228 return 1;
3229#endif
3230 return 0;
3231#else
3232 return 1;
3233#endif
3234}
3235
3236
3237static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
3238{
3239 /* Check for SCSI format (ANSI and Response data format) */
3240 if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
3241 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
3242 && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
3243 /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
3244 /* ((dcb->dev_type == TYPE_DISK)
3245 || (dcb->dev_type == TYPE_MOD)) && */
3246 !tagq_blacklist(((char *)ptr) + 8)) {
3247 if (dcb->max_command == 1)
3248 dcb->max_command =
3249 dcb->acb->tag_max_num;
3250 dcb->sync_mode |= EN_TAG_QUEUEING;
3251 /*dcb->tag_mask = 0; */
3252 } else
3253 dcb->max_command = 1;
3254 }
3255}
3256
3257
3258static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3259 struct ScsiInqData *ptr)
3260{
3261 u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
3262 dcb->dev_type = bval1;
3263 /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
3264 disc_tagq_set(dcb, ptr);
3265}
3266
3267
3268/* unmap mapped pci regions from SRB */
3269static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
3270{
3271 struct scsi_cmnd *cmd = srb->cmd;
3272 enum dma_data_direction dir = cmd->sc_data_direction;
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003273
3274 if (scsi_sg_count(cmd) && dir != PCI_DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 /* unmap DC395x SG list */
3276 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
3277 srb->sg_bus_addr, SEGMENTX_LEN);
3278 pci_unmap_single(acb->dev, srb->sg_bus_addr,
3279 SEGMENTX_LEN,
3280 PCI_DMA_TODEVICE);
3281 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003282 scsi_sg_count(cmd), scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* unmap the sg segments */
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003284 scsi_dma_unmap(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
3286}
3287
3288
3289/* unmap mapped pci sense buffer from SRB */
3290static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3291 struct ScsiReqBlk *srb)
3292{
3293 if (!(srb->flag & AUTO_REQSENSE))
3294 return;
3295 /* Unmap sense buffer */
3296 dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
3297 srb->segment_x[0].address);
3298 pci_unmap_single(acb->dev, srb->segment_x[0].address,
3299 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
3300 /* Restore SG stuff */
3301 srb->total_xfer_length = srb->xferred;
3302 srb->segment_x[0].address =
3303 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
3304 srb->segment_x[0].length =
3305 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
3306}
3307
3308
3309/*
3310 * Complete execution of a SCSI command
3311 * Signal completion to the generic SCSI driver
3312 */
3313static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3314 struct ScsiReqBlk *srb)
3315{
3316 u8 tempcnt, status;
3317 struct scsi_cmnd *cmd = srb->cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 enum dma_data_direction dir = cmd->sc_data_direction;
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003319 int ckc_only = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Matthew Wilcox12a44162007-09-18 19:54:43 -06003321 dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->serial_number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 srb->cmd->device->id, srb->cmd->device->lun);
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003323 dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003324 srb, scsi_sg_count(cmd), srb->sg_index, srb->sg_count,
3325 scsi_sgtalbe(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 status = srb->target_status;
3327 if (srb->flag & AUTO_REQSENSE) {
3328 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
3329 pci_unmap_srb_sense(acb, srb);
3330 /*
3331 ** target status..........................
3332 */
3333 srb->flag &= ~AUTO_REQSENSE;
3334 srb->adapter_status = 0;
3335 srb->target_status = CHECK_CONDITION << 1;
3336 if (debug_enabled(DBG_1)) {
3337 switch (cmd->sense_buffer[2] & 0x0f) {
3338 case NOT_READY:
3339 dprintkl(KERN_DEBUG,
3340 "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3341 cmd->cmnd[0], dcb->target_id,
3342 dcb->target_lun, status, acb->scan_devices);
3343 break;
3344 case UNIT_ATTENTION:
3345 dprintkl(KERN_DEBUG,
3346 "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3347 cmd->cmnd[0], dcb->target_id,
3348 dcb->target_lun, status, acb->scan_devices);
3349 break;
3350 case ILLEGAL_REQUEST:
3351 dprintkl(KERN_DEBUG,
3352 "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3353 cmd->cmnd[0], dcb->target_id,
3354 dcb->target_lun, status, acb->scan_devices);
3355 break;
3356 case MEDIUM_ERROR:
3357 dprintkl(KERN_DEBUG,
3358 "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3359 cmd->cmnd[0], dcb->target_id,
3360 dcb->target_lun, status, acb->scan_devices);
3361 break;
3362 case HARDWARE_ERROR:
3363 dprintkl(KERN_DEBUG,
3364 "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
3365 cmd->cmnd[0], dcb->target_id,
3366 dcb->target_lun, status, acb->scan_devices);
3367 break;
3368 }
3369 if (cmd->sense_buffer[7] >= 6)
3370 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
3371 "(0x%08x 0x%08x)\n",
3372 cmd->sense_buffer[2], cmd->sense_buffer[12],
3373 cmd->sense_buffer[13],
3374 *((unsigned int *)(cmd->sense_buffer + 3)),
3375 *((unsigned int *)(cmd->sense_buffer + 8)));
3376 else
3377 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
3378 cmd->sense_buffer[2],
3379 *((unsigned int *)(cmd->sense_buffer + 3)));
3380 }
3381
3382 if (status == (CHECK_CONDITION << 1)) {
3383 cmd->result = DID_BAD_TARGET << 16;
3384 goto ckc_e;
3385 }
3386 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
3387
3388 if (srb->total_xfer_length
3389 && srb->total_xfer_length >= cmd->underflow)
3390 cmd->result =
3391 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3392 srb->end_message, CHECK_CONDITION);
3393 /*SET_RES_DID(cmd->result,DID_OK) */
3394 else
3395 cmd->result =
3396 MK_RES_LNX(DRIVER_SENSE, DID_OK,
3397 srb->end_message, CHECK_CONDITION);
3398
3399 goto ckc_e;
3400 }
3401
3402/*************************************************************/
3403 if (status) {
3404 /*
3405 * target status..........................
3406 */
3407 if (status_byte(status) == CHECK_CONDITION) {
3408 request_sense(acb, dcb, srb);
3409 return;
3410 } else if (status_byte(status) == QUEUE_FULL) {
3411 tempcnt = (u8)list_size(&dcb->srb_going_list);
3412 dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
3413 dcb->target_id, dcb->target_lun, tempcnt);
3414 if (tempcnt > 1)
3415 tempcnt--;
3416 dcb->max_command = tempcnt;
3417 free_tag(dcb, srb);
3418 srb_going_to_waiting_move(dcb, srb);
3419 waiting_set_timer(acb, HZ / 20);
3420 srb->adapter_status = 0;
3421 srb->target_status = 0;
3422 return;
3423 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
3424 srb->adapter_status = H_SEL_TIMEOUT;
3425 srb->target_status = 0;
3426 cmd->result = DID_NO_CONNECT << 16;
3427 } else {
3428 srb->adapter_status = 0;
3429 SET_RES_DID(cmd->result, DID_ERROR);
3430 SET_RES_MSG(cmd->result, srb->end_message);
3431 SET_RES_TARGET(cmd->result, status);
3432
3433 }
3434 } else {
3435 /*
3436 ** process initiator status..........................
3437 */
3438 status = srb->adapter_status;
3439 if (status & H_OVER_UNDER_RUN) {
3440 srb->target_status = 0;
3441 SET_RES_DID(cmd->result, DID_OK);
3442 SET_RES_MSG(cmd->result, srb->end_message);
3443 } else if (srb->status & PARITY_ERROR) {
3444 SET_RES_DID(cmd->result, DID_PARITY);
3445 SET_RES_MSG(cmd->result, srb->end_message);
3446 } else { /* No error */
3447
3448 srb->adapter_status = 0;
3449 srb->target_status = 0;
3450 SET_RES_DID(cmd->result, DID_OK);
3451 }
3452 }
3453
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003454 if (dir != PCI_DMA_NONE && scsi_sg_count(cmd))
3455 pci_dma_sync_sg_for_cpu(acb->dev, scsi_sglist(cmd),
3456 scsi_sg_count(cmd), dir);
3457
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003458 ckc_only = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459/* Check Error Conditions */
3460 ckc_e:
3461
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003462 if (cmd->cmnd[0] == INQUIRY) {
3463 unsigned char *base = NULL;
3464 struct ScsiInqData *ptr;
3465 unsigned long flags = 0;
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003466 struct scatterlist* sg = scsi_sglist(cmd);
3467 size_t offset = 0, len = sizeof(struct ScsiInqData);
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003468
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003469 local_irq_save(flags);
3470 base = scsi_kmap_atomic_sg(sg, scsi_sg_count(cmd), &offset, &len);
3471 ptr = (struct ScsiInqData *)(base + offset);
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003472
3473 if (!ckc_only && (cmd->result & RES_DID) == 0
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003474 && cmd->cmnd[2] == 0 && scsi_bufflen(cmd) >= 8
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003475 && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
3476 dcb->inquiry7 = ptr->Flags;
3477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 /*if( srb->cmd->cmnd[0] == INQUIRY && */
3479 /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
Guennadi Liakhovetski9dc399d2006-06-09 22:25:23 +02003480 if ((cmd->result == (DID_OK << 16)
3481 || status_byte(cmd->result) &
3482 CHECK_CONDITION)) {
3483 if (!dcb->init_tcq_flag) {
3484 add_dev(acb, dcb, ptr);
3485 dcb->init_tcq_flag = 1;
3486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
3488
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003489 scsi_kunmap_atomic_sg(base);
3490 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 }
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 /* Here is the info for Doug Gilbert's sg3 ... */
FUJITA Tomonoria862ea32007-05-26 02:07:09 +09003494 scsi_set_resid(cmd, srb->total_xfer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 /* This may be interpreted by sb. or not ... */
3496 cmd->SCp.this_residual = srb->total_xfer_length;
3497 cmd->SCp.buffers_residual = 0;
3498 if (debug_enabled(DBG_KG)) {
3499 if (srb->total_xfer_length)
3500 dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
3501 "cmnd=0x%02x Missed %i bytes\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06003502 cmd->serial_number, cmd->device->id, cmd->device->lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 cmd->cmnd[0], srb->total_xfer_length);
3504 }
3505
3506 srb_going_remove(dcb, srb);
3507 /* Add to free list */
3508 if (srb == acb->tmp_srb)
3509 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
3510 else {
3511 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06003512 cmd->serial_number, cmd->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 srb_free_insert(acb, srb);
3514 }
3515 pci_unmap_srb(acb, srb);
3516
3517 cmd->scsi_done(cmd);
3518 waiting_process_next(acb);
3519}
3520
3521
3522/* abort all cmds in our queues */
3523static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
3524 struct scsi_cmnd *cmd, u8 force)
3525{
3526 struct DeviceCtlBlk *dcb;
3527 dprintkl(KERN_INFO, "doing_srb_done: pids ");
3528
3529 list_for_each_entry(dcb, &acb->dcb_list, list) {
3530 struct ScsiReqBlk *srb;
3531 struct ScsiReqBlk *tmp;
3532 struct scsi_cmnd *p;
3533
3534 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
3535 enum dma_data_direction dir;
3536 int result;
3537
3538 p = srb->cmd;
3539 dir = p->sc_data_direction;
3540 result = MK_RES(0, did_flag, 0, 0);
Matthew Wilcox12a44162007-09-18 19:54:43 -06003541 printk("G:%li(%02i-%i) ", p->serial_number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 p->device->id, p->device->lun);
3543 srb_going_remove(dcb, srb);
3544 free_tag(dcb, srb);
3545 srb_free_insert(acb, srb);
3546 p->result = result;
3547 pci_unmap_srb_sense(acb, srb);
3548 pci_unmap_srb(acb, srb);
3549 if (force) {
3550 /* For new EH, we normally don't need to give commands back,
3551 * as they all complete or all time out */
3552 p->scsi_done(p);
3553 }
3554 }
3555 if (!list_empty(&dcb->srb_going_list))
3556 dprintkl(KERN_DEBUG,
3557 "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
3558 dcb->target_id, dcb->target_lun);
3559 if (dcb->tag_mask)
3560 dprintkl(KERN_DEBUG,
3561 "tag_mask for <%02i-%i> should be empty, is %08x!\n",
3562 dcb->target_id, dcb->target_lun,
3563 dcb->tag_mask);
3564
3565 /* Waiting queue */
3566 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
3567 int result;
3568 p = srb->cmd;
3569
3570 result = MK_RES(0, did_flag, 0, 0);
Matthew Wilcox12a44162007-09-18 19:54:43 -06003571 printk("W:%li<%02i-%i>", p->serial_number, p->device->id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 p->device->lun);
3573 srb_waiting_remove(dcb, srb);
3574 srb_free_insert(acb, srb);
3575 p->result = result;
3576 pci_unmap_srb_sense(acb, srb);
3577 pci_unmap_srb(acb, srb);
3578 if (force) {
3579 /* For new EH, we normally don't need to give commands back,
3580 * as they all complete or all time out */
3581 cmd->scsi_done(cmd);
3582 }
3583 }
3584 if (!list_empty(&dcb->srb_waiting_list))
3585 dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
3586 list_size(&dcb->srb_waiting_list), dcb->target_id,
3587 dcb->target_lun);
3588 dcb->flag &= ~ABORT_DEV_;
3589 }
3590 printk("\n");
3591}
3592
3593
3594static void reset_scsi_bus(struct AdapterCtlBlk *acb)
3595{
3596 dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
3597 acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
3598 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
3599
3600 while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
3601 /* nothing */;
3602}
3603
3604
3605static void set_basic_config(struct AdapterCtlBlk *acb)
3606{
3607 u8 bval;
3608 u16 wval;
3609 DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
3610 if (acb->config & HCC_PARITY)
3611 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
3612 else
3613 bval = PHASELATCH | INITIATOR | BLOCKRST;
3614
3615 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
3616
3617 /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
3618 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
3619 /* program Host ID */
3620 DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
3621 /* set ansynchronous transfer */
3622 DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
3623 /* Turn LED control off */
3624 wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
3625 DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
3626 /* DMA config */
3627 wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
3628 wval |=
3629 DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
3630 DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
3631 /* Clear pending interrupt status */
3632 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
3633 /* Enable SCSI interrupt */
3634 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
3635 DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
3636 /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
3637 );
3638}
3639
3640
3641static void scsi_reset_detect(struct AdapterCtlBlk *acb)
3642{
3643 dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
3644 /* delay half a second */
3645 if (timer_pending(&acb->waiting_timer))
3646 del_timer(&acb->waiting_timer);
3647
3648 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
3649 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
3650 /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
3651 udelay(500);
3652 /* Maybe we locked up the bus? Then lets wait even longer ... */
3653 acb->scsi_host->last_reset =
3654 jiffies + 5 * HZ / 2 +
3655 HZ * acb->eeprom.delay_time;
3656
3657 clear_fifo(acb, "scsi_reset_detect");
3658 set_basic_config(acb);
3659 /*1.25 */
3660 /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
3661
3662 if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
3663 acb->acb_flag |= RESET_DONE;
3664 } else {
3665 acb->acb_flag |= RESET_DETECT;
3666 reset_dev_param(acb);
3667 doing_srb_done(acb, DID_RESET, NULL, 1);
3668 /*DC395x_RecoverSRB( acb ); */
3669 acb->active_dcb = NULL;
3670 acb->acb_flag = 0;
3671 waiting_process_next(acb);
3672 }
3673}
3674
3675
3676static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3677 struct ScsiReqBlk *srb)
3678{
3679 struct scsi_cmnd *cmd = srb->cmd;
3680 dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06003681 cmd->serial_number, cmd->device->id, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 srb->flag |= AUTO_REQSENSE;
3684 srb->adapter_status = 0;
3685 srb->target_status = 0;
3686
3687 /* KG: Can this prevent crap sense data ? */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09003688 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
3690 /* Save some data */
3691 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
3692 srb->segment_x[0].address;
3693 srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
3694 srb->segment_x[0].length;
3695 srb->xferred = srb->total_xfer_length;
3696 /* srb->segment_x : a one entry of S/G list table */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09003697 srb->total_xfer_length = SCSI_SENSE_BUFFERSIZE;
3698 srb->segment_x[0].length = SCSI_SENSE_BUFFERSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 /* Map sense buffer */
3700 srb->segment_x[0].address =
3701 pci_map_single(acb->dev, cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09003702 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
3704 cmd->sense_buffer, srb->segment_x[0].address,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09003705 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 srb->sg_count = 1;
3707 srb->sg_index = 0;
3708
3709 if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
3710 dprintkl(KERN_DEBUG,
3711 "request_sense: (pid#%li) failed <%02i-%i>\n",
Matthew Wilcox12a44162007-09-18 19:54:43 -06003712 srb->cmd->serial_number, dcb->target_id, dcb->target_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 srb_going_to_waiting_move(dcb, srb);
3714 waiting_set_timer(acb, HZ / 100);
3715 }
3716}
3717
3718
3719/**
3720 * device_alloc - Allocate a new device instance. This create the
3721 * devices instance and sets up all the data items. The adapter
3722 * instance is required to obtain confiuration information for this
3723 * device. This does *not* add this device to the adapters device
3724 * list.
3725 *
3726 * @acb: The adapter to obtain configuration information from.
3727 * @target: The target for the new device.
3728 * @lun: The lun for the new device.
3729 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02003730 * Return the new device if successful or NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 **/
3732static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
3733 u8 target, u8 lun)
3734{
3735 struct NvRamType *eeprom = &acb->eeprom;
3736 u8 period_index = eeprom->target[target].period & 0x07;
3737 struct DeviceCtlBlk *dcb;
3738
3739 dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
3740 dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
3741 if (!dcb)
3742 return NULL;
3743 dcb->acb = NULL;
3744 INIT_LIST_HEAD(&dcb->srb_going_list);
3745 INIT_LIST_HEAD(&dcb->srb_waiting_list);
3746 dcb->active_srb = NULL;
3747 dcb->tag_mask = 0;
3748 dcb->max_command = 1;
3749 dcb->target_id = target;
3750 dcb->target_lun = lun;
3751#ifndef DC395x_NO_DISCONNECT
3752 dcb->identify_msg =
3753 IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
3754#else
3755 dcb->identify_msg = IDENTIFY(0, lun);
3756#endif
3757 dcb->dev_mode = eeprom->target[target].cfg0;
3758 dcb->inquiry7 = 0;
3759 dcb->sync_mode = 0;
3760 dcb->min_nego_period = clock_period[period_index];
3761 dcb->sync_period = 0;
3762 dcb->sync_offset = 0;
3763 dcb->flag = 0;
3764
3765#ifndef DC395x_NO_WIDE
3766 if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
3767 && (acb->config & HCC_WIDE_CARD))
3768 dcb->sync_mode |= WIDE_NEGO_ENABLE;
3769#endif
3770#ifndef DC395x_NO_SYNC
3771 if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
3772 if (!(lun) || current_sync_offset)
3773 dcb->sync_mode |= SYNC_NEGO_ENABLE;
3774#endif
3775 if (dcb->target_lun != 0) {
3776 /* Copy settings */
3777 struct DeviceCtlBlk *p;
3778 list_for_each_entry(p, &acb->dcb_list, list)
3779 if (p->target_id == dcb->target_id)
3780 break;
3781 dprintkdbg(DBG_1,
3782 "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
3783 dcb->target_id, dcb->target_lun,
3784 p->target_id, p->target_lun);
3785 dcb->sync_mode = p->sync_mode;
3786 dcb->sync_period = p->sync_period;
3787 dcb->min_nego_period = p->min_nego_period;
3788 dcb->sync_offset = p->sync_offset;
3789 dcb->inquiry7 = p->inquiry7;
3790 }
3791 return dcb;
3792}
3793
3794
3795/**
3796 * adapter_add_device - Adds the device instance to the adaptor instance.
3797 *
3798 * @acb: The adapter device to be updated
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003799 * @dcb: A newly created and initialised device instance to add.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 **/
3801static void adapter_add_device(struct AdapterCtlBlk *acb,
3802 struct DeviceCtlBlk *dcb)
3803{
3804 /* backpointer to adapter */
3805 dcb->acb = acb;
3806
3807 /* set run_robin to this device if it is currently empty */
3808 if (list_empty(&acb->dcb_list))
3809 acb->dcb_run_robin = dcb;
3810
3811 /* add device to list */
3812 list_add_tail(&dcb->list, &acb->dcb_list);
3813
3814 /* update device maps */
3815 acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
3816 acb->children[dcb->target_id][dcb->target_lun] = dcb;
3817}
3818
3819
3820/**
3821 * adapter_remove_device - Removes the device instance from the adaptor
3822 * instance. The device instance is not check in any way or freed by this.
3823 * The caller is expected to take care of that. This will simply remove the
3824 * device from the adapters data strcutures.
3825 *
3826 * @acb: The adapter device to be updated
3827 * @dcb: A device that has previously been added to the adapter.
3828 **/
3829static void adapter_remove_device(struct AdapterCtlBlk *acb,
3830 struct DeviceCtlBlk *dcb)
3831{
3832 struct DeviceCtlBlk *i;
3833 struct DeviceCtlBlk *tmp;
3834 dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
3835 dcb->target_id, dcb->target_lun);
3836
3837 /* fix up any pointers to this device that we have in the adapter */
3838 if (acb->active_dcb == dcb)
3839 acb->active_dcb = NULL;
3840 if (acb->dcb_run_robin == dcb)
3841 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
3842
3843 /* unlink from list */
3844 list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
3845 if (dcb == i) {
3846 list_del(&i->list);
3847 break;
3848 }
3849
3850 /* clear map and children */
3851 acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
3852 acb->children[dcb->target_id][dcb->target_lun] = NULL;
3853 dcb->acb = NULL;
3854}
3855
3856
3857/**
3858 * adapter_remove_and_free_device - Removes a single device from the adapter
3859 * and then frees the device information.
3860 *
3861 * @acb: The adapter device to be updated
3862 * @dcb: A device that has previously been added to the adapter.
3863 */
3864static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
3865 struct DeviceCtlBlk *dcb)
3866{
3867 if (list_size(&dcb->srb_going_list) > 1) {
3868 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
3869 "Won't remove because of %i active requests.\n",
3870 dcb->target_id, dcb->target_lun,
3871 list_size(&dcb->srb_going_list));
3872 return;
3873 }
3874 adapter_remove_device(acb, dcb);
3875 kfree(dcb);
3876}
3877
3878
3879/**
3880 * adapter_remove_and_free_all_devices - Removes and frees all of the
3881 * devices associated with the specified adapter.
3882 *
3883 * @acb: The adapter from which all devices should be removed.
3884 **/
3885static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
3886{
3887 struct DeviceCtlBlk *dcb;
3888 struct DeviceCtlBlk *tmp;
3889 dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
3890 list_size(&acb->dcb_list));
3891
3892 list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
3893 adapter_remove_and_free_device(acb, dcb);
3894}
3895
3896
3897/**
3898 * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
3899 * scsi device that we need to deal with. We allocate a new device and then
3900 * insert that device into the adapters device list.
3901 *
3902 * @scsi_device: The new scsi device that we need to handle.
3903 **/
3904static int dc395x_slave_alloc(struct scsi_device *scsi_device)
3905{
3906 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3907 struct DeviceCtlBlk *dcb;
3908
3909 dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
3910 if (!dcb)
3911 return -ENOMEM;
3912 adapter_add_device(acb, dcb);
3913
3914 return 0;
3915}
3916
3917
3918/**
3919 * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
3920 * device that is going away.
3921 *
3922 * @scsi_device: The new scsi device that we need to handle.
3923 **/
3924static void dc395x_slave_destroy(struct scsi_device *scsi_device)
3925{
3926 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
3927 struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
3928 if (dcb)
3929 adapter_remove_and_free_device(acb, dcb);
3930}
3931
3932
3933
3934
3935/**
3936 * trms1040_wait_30us: wait for 30 us
3937 *
3938 * Waits for 30us (using the chip by the looks of it..)
3939 *
3940 * @io_port: base I/O address
3941 **/
3942static void __devinit trms1040_wait_30us(unsigned long io_port)
3943{
3944 /* ScsiPortStallExecution(30); wait 30 us */
3945 outb(5, io_port + TRM_S1040_GEN_TIMER);
3946 while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
3947 /* nothing */ ;
3948}
3949
3950
3951/**
3952 * trms1040_write_cmd - write the secified command and address to
3953 * chip
3954 *
3955 * @io_port: base I/O address
3956 * @cmd: SB + op code (command) to send
3957 * @addr: address to send
3958 **/
3959static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
3960{
3961 int i;
3962 u8 send_data;
3963
3964 /* program SB + OP code */
3965 for (i = 0; i < 3; i++, cmd <<= 1) {
3966 send_data = NVR_SELECT;
3967 if (cmd & 0x04) /* Start from bit 2 */
3968 send_data |= NVR_BITOUT;
3969
3970 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3971 trms1040_wait_30us(io_port);
3972 outb((send_data | NVR_CLOCK),
3973 io_port + TRM_S1040_GEN_NVRAM);
3974 trms1040_wait_30us(io_port);
3975 }
3976
3977 /* send address */
3978 for (i = 0; i < 7; i++, addr <<= 1) {
3979 send_data = NVR_SELECT;
3980 if (addr & 0x40) /* Start from bit 6 */
3981 send_data |= NVR_BITOUT;
3982
3983 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
3984 trms1040_wait_30us(io_port);
3985 outb((send_data | NVR_CLOCK),
3986 io_port + TRM_S1040_GEN_NVRAM);
3987 trms1040_wait_30us(io_port);
3988 }
3989 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
3990 trms1040_wait_30us(io_port);
3991}
3992
3993
3994/**
3995 * trms1040_set_data - store a single byte in the eeprom
3996 *
3997 * Called from write all to write a single byte into the SSEEPROM
3998 * Which is done one bit at a time.
3999 *
4000 * @io_port: base I/O address
4001 * @addr: offset into EEPROM
4002 * @byte: bytes to write
4003 **/
4004static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
4005{
4006 int i;
4007 u8 send_data;
4008
4009 /* Send write command & address */
4010 trms1040_write_cmd(io_port, 0x05, addr);
4011
4012 /* Write data */
4013 for (i = 0; i < 8; i++, byte <<= 1) {
4014 send_data = NVR_SELECT;
4015 if (byte & 0x80) /* Start from bit 7 */
4016 send_data |= NVR_BITOUT;
4017
4018 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
4019 trms1040_wait_30us(io_port);
4020 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4021 trms1040_wait_30us(io_port);
4022 }
4023 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4024 trms1040_wait_30us(io_port);
4025
4026 /* Disable chip select */
4027 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4028 trms1040_wait_30us(io_port);
4029
4030 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4031 trms1040_wait_30us(io_port);
4032
4033 /* Wait for write ready */
4034 while (1) {
4035 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4036 trms1040_wait_30us(io_port);
4037
4038 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4039 trms1040_wait_30us(io_port);
4040
4041 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
4042 break;
4043 }
4044
4045 /* Disable chip select */
4046 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4047}
4048
4049
4050/**
4051 * trms1040_write_all - write 128 bytes to the eeprom
4052 *
4053 * Write the supplied 128 bytes to the chips SEEPROM
4054 *
4055 * @eeprom: the data to write
4056 * @io_port: the base io port
4057 **/
4058static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
4059{
4060 u8 *b_eeprom = (u8 *)eeprom;
4061 u8 addr;
4062
4063 /* Enable SEEPROM */
4064 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4065 io_port + TRM_S1040_GEN_CONTROL);
4066
4067 /* write enable */
4068 trms1040_write_cmd(io_port, 0x04, 0xFF);
4069 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4070 trms1040_wait_30us(io_port);
4071
4072 /* write */
4073 for (addr = 0; addr < 128; addr++, b_eeprom++)
4074 trms1040_set_data(io_port, addr, *b_eeprom);
4075
4076 /* write disable */
4077 trms1040_write_cmd(io_port, 0x04, 0x00);
4078 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4079 trms1040_wait_30us(io_port);
4080
4081 /* Disable SEEPROM */
4082 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4083 io_port + TRM_S1040_GEN_CONTROL);
4084}
4085
4086
4087/**
4088 * trms1040_get_data - get a single byte from the eeprom
4089 *
4090 * Called from read all to read a single byte into the SSEEPROM
4091 * Which is done one bit at a time.
4092 *
4093 * @io_port: base I/O address
4094 * @addr: offset into SEEPROM
4095 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02004096 * Returns the byte read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 **/
4098static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
4099{
4100 int i;
4101 u8 read_byte;
4102 u8 result = 0;
4103
4104 /* Send read command & address */
4105 trms1040_write_cmd(io_port, 0x06, addr);
4106
4107 /* read data */
4108 for (i = 0; i < 8; i++) {
4109 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
4110 trms1040_wait_30us(io_port);
4111 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
4112
4113 /* Get data bit while falling edge */
4114 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
4115 result <<= 1;
4116 if (read_byte & NVR_BITIN)
4117 result |= 1;
4118
4119 trms1040_wait_30us(io_port);
4120 }
4121
4122 /* Disable chip select */
4123 outb(0, io_port + TRM_S1040_GEN_NVRAM);
4124 return result;
4125}
4126
4127
4128/**
4129 * trms1040_read_all - read all bytes from the eeprom
4130 *
4131 * Read the 128 bytes from the SEEPROM.
4132 *
4133 * @eeprom: where to store the data
4134 * @io_port: the base io port
4135 **/
4136static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
4137{
4138 u8 *b_eeprom = (u8 *)eeprom;
4139 u8 addr;
4140
4141 /* Enable SEEPROM */
4142 outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
4143 io_port + TRM_S1040_GEN_CONTROL);
4144
4145 /* read details */
4146 for (addr = 0; addr < 128; addr++, b_eeprom++)
4147 *b_eeprom = trms1040_get_data(io_port, addr);
4148
4149 /* Disable SEEPROM */
4150 outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
4151 io_port + TRM_S1040_GEN_CONTROL);
4152}
4153
4154
4155
4156/**
4157 * check_eeprom - get and check contents of the eeprom
4158 *
4159 * Read seeprom 128 bytes into the memory provider in eeprom.
4160 * Checks the checksum and if it's not correct it uses a set of default
4161 * values.
4162 *
4163 * @eeprom: caller allocated strcuture to read the eeprom data into
4164 * @io_port: io port to read from
4165 **/
4166static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
4167{
4168 u16 *w_eeprom = (u16 *)eeprom;
4169 u16 w_addr;
4170 u16 cksum;
4171 u32 d_addr;
4172 u32 *d_eeprom;
4173
4174 trms1040_read_all(eeprom, io_port); /* read eeprom */
4175
4176 cksum = 0;
4177 for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
4178 w_addr++, w_eeprom++)
4179 cksum += *w_eeprom;
4180 if (cksum != 0x1234) {
4181 /*
4182 * Checksum is wrong.
4183 * Load a set of defaults into the eeprom buffer
4184 */
4185 dprintkl(KERN_WARNING,
4186 "EEProm checksum error: using default values and options.\n");
4187 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4188 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4189 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4190 eeprom->sub_sys_id[1] =
4191 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4192 eeprom->sub_class = 0x00;
4193 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
4194 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
4195 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
4196 eeprom->device_id[1] =
4197 (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
4198 eeprom->reserved = 0x00;
4199
4200 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
4201 d_addr < 16; d_addr++, d_eeprom++)
4202 *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
4203
4204 *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
4205 *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
4206 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
4207 *d_eeprom = 0x00;
4208
4209 /* Now load defaults (maybe set by boot/module params) */
4210 set_safe_settings();
4211 fix_settings();
4212 eeprom_override(eeprom);
4213
4214 eeprom->cksum = 0x00;
4215 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
4216 w_addr < 63; w_addr++, w_eeprom++)
4217 cksum += *w_eeprom;
4218
4219 *w_eeprom = 0x1234 - cksum;
4220 trms1040_write_all(eeprom, io_port);
4221 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
4222 } else {
4223 set_safe_settings();
4224 eeprom_index_to_delay(eeprom);
4225 eeprom_override(eeprom);
4226 }
4227}
4228
4229
4230/**
4231 * print_eeprom_settings - output the eeprom settings
4232 * to the kernel log so people can see what they were.
4233 *
4234 * @eeprom: The eeprom data strucutre to show details for.
4235 **/
4236static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
4237{
4238 dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
4239 eeprom->scsi_id,
4240 eeprom->target[0].period,
4241 clock_speed[eeprom->target[0].period] / 10,
4242 clock_speed[eeprom->target[0].period] % 10,
4243 eeprom->target[0].cfg0);
4244 dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
4245 eeprom->channel_cfg, eeprom->max_tag,
4246 1 << eeprom->max_tag, eeprom->delay_time);
4247}
4248
4249
4250/* Free SG tables */
4251static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
4252{
4253 int i;
4254 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4255
4256 for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004257 kfree(acb->srb_array[i].segment_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
4260
4261/*
4262 * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
4263 * should never cross a page boundary */
4264static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
4265{
4266 const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
4267 *SEGMENTX_LEN;
4268 int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
4269 const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
4270 int srb_idx = 0;
4271 unsigned i = 0;
Andrew Morton8144f212008-02-04 23:53:26 -08004272 struct SGentry *uninitialized_var(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Linus Torvaldsb4b08e52005-08-14 15:43:39 -07004274 for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 acb->srb_array[i].segment_x = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
4278 while (pages--) {
Robert P. J. Day5cbded52006-12-13 00:35:56 -08004279 ptr = kmalloc(PAGE_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 if (!ptr) {
4281 adapter_sg_tables_free(acb);
4282 return 1;
4283 }
4284 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
4285 PAGE_SIZE, ptr, srb_idx);
4286 i = 0;
4287 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
4288 acb->srb_array[srb_idx++].segment_x =
4289 ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
4290 }
4291 if (i < srbs_per_page)
4292 acb->srb.segment_x =
4293 ptr + (i * DC395x_MAX_SG_LISTENTRY);
4294 else
4295 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return 0;
4297}
4298
4299
4300
4301/**
4302 * adapter_print_config - print adapter connection and termination
4303 * config
4304 *
4305 * The io port in the adapter needs to have been set before calling
4306 * this function.
4307 *
4308 * @acb: The adapter to print the information for.
4309 **/
4310static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
4311{
4312 u8 bval;
4313
4314 bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
4315 dprintkl(KERN_INFO, "%sConnectors: ",
4316 ((bval & WIDESCSI) ? "(Wide) " : ""));
4317 if (!(bval & CON5068))
4318 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
4319 if (!(bval & CON68))
4320 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
4321 if (!(bval & CON50))
4322 printk("int50 ");
4323 if ((bval & (CON5068 | CON50 | CON68)) ==
4324 0 /*(CON5068 | CON50 | CON68) */ )
4325 printk(" Oops! (All 3?) ");
4326 bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
4327 printk(" Termination: ");
4328 if (bval & DIS_TERM)
4329 printk("Disabled\n");
4330 else {
4331 if (bval & AUTOTERM)
4332 printk("Auto ");
4333 if (bval & LOW8TERM)
4334 printk("Low ");
4335 if (bval & UP8TERM)
4336 printk("High ");
4337 printk("\n");
4338 }
4339}
4340
4341
4342/**
4343 * adapter_init_params - Initialize the various parameters in the
4344 * adapter structure. Note that the pointer to the scsi_host is set
4345 * early (when this instance is created) and the io_port and irq
4346 * values are set later after they have been reserved. This just gets
4347 * everything set to a good starting position.
4348 *
4349 * The eeprom structure in the adapter needs to have been set before
4350 * calling this function.
4351 *
4352 * @acb: The adapter to initialize.
4353 **/
4354static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
4355{
4356 struct NvRamType *eeprom = &acb->eeprom;
4357 int i;
4358
4359 /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
4360 /* NOTE: acb->io_port_base is set at port registration time */
4361 /* NOTE: acb->io_port_len is set at port registration time */
4362
4363 INIT_LIST_HEAD(&acb->dcb_list);
4364 acb->dcb_run_robin = NULL;
4365 acb->active_dcb = NULL;
4366
4367 INIT_LIST_HEAD(&acb->srb_free_list);
4368 /* temp SRB for Q tag used or abort command used */
4369 acb->tmp_srb = &acb->srb;
4370 init_timer(&acb->waiting_timer);
4371 init_timer(&acb->selto_timer);
4372
4373 acb->srb_count = DC395x_MAX_SRB_CNT;
4374
4375 acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */
4376 /* NOTE: acb->irq_level is set at IRQ registration time */
4377
4378 acb->tag_max_num = 1 << eeprom->max_tag;
4379 if (acb->tag_max_num > 30)
4380 acb->tag_max_num = 30;
4381
4382 acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
4383 acb->gmode2 = eeprom->channel_cfg;
4384 acb->config = 0; /* NOTE: actually set in adapter_init_chip */
4385
4386 if (eeprom->channel_cfg & NAC_SCANLUN)
4387 acb->lun_chk = 1;
4388 acb->scan_devices = 1;
4389
4390 acb->scsi_host->this_id = eeprom->scsi_id;
4391 acb->hostid_bit = (1 << acb->scsi_host->this_id);
4392
4393 for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
4394 acb->dcb_map[i] = 0;
4395
4396 acb->msg_len = 0;
4397
4398 /* link static array of srbs into the srb free list */
4399 for (i = 0; i < acb->srb_count - 1; i++)
4400 srb_free_insert(acb, &acb->srb_array[i]);
4401}
4402
4403
4404/**
4405 * adapter_init_host - Initialize the scsi host instance based on
4406 * values that we have already stored in the adapter instance. There's
4407 * some mention that a lot of these are deprecated, so we won't use
4408 * them (we'll use the ones in the adapter instance) but we'll fill
4409 * them in in case something else needs them.
4410 *
4411 * The eeprom structure, irq and io ports in the adapter need to have
4412 * been set before calling this function.
4413 *
4414 * @host: The scsi host instance to fill in the values for.
4415 **/
4416static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
4417{
4418 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4419 struct NvRamType *eeprom = &acb->eeprom;
4420
4421 host->max_cmd_len = 24;
4422 host->can_queue = DC395x_MAX_CMD_QUEUE;
4423 host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
4424 host->this_id = (int)eeprom->scsi_id;
4425 host->io_port = acb->io_port_base;
4426 host->n_io_port = acb->io_port_len;
4427 host->dma_channel = -1;
4428 host->unique_id = acb->io_port_base;
4429 host->irq = acb->irq_level;
4430 host->last_reset = jiffies;
4431
4432 host->max_id = 16;
4433 if (host->max_id - 1 == eeprom->scsi_id)
4434 host->max_id--;
4435
4436#ifdef CONFIG_SCSI_MULTI_LUN
4437 if (eeprom->channel_cfg & NAC_SCANLUN)
4438 host->max_lun = 8;
4439 else
4440 host->max_lun = 1;
4441#else
4442 host->max_lun = 1;
4443#endif
4444
4445}
4446
4447
4448/**
4449 * adapter_init_chip - Get the chip into a know state and figure out
4450 * some of the settings that apply to this adapter.
4451 *
4452 * The io port in the adapter needs to have been set before calling
4453 * this function. The config will be configured correctly on return.
4454 *
4455 * @acb: The adapter which we are to init.
4456 **/
4457static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
4458{
4459 struct NvRamType *eeprom = &acb->eeprom;
4460
4461 /* Mask all the interrupt */
4462 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
4463 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
4464
4465 /* Reset SCSI module */
4466 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
4467
4468 /* Reset PCI/DMA module */
4469 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
4470 udelay(20);
4471
4472 /* program configuration 0 */
4473 acb->config = HCC_AUTOTERM | HCC_PARITY;
4474 if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
4475 acb->config |= HCC_WIDE_CARD;
4476
4477 if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
4478 acb->config |= HCC_SCSI_RESET;
4479
4480 if (acb->config & HCC_SCSI_RESET) {
4481 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
4482 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
4483
4484 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
4485 /*spin_unlock_irq (&io_request_lock); */
4486 udelay(500);
4487
4488 acb->scsi_host->last_reset =
4489 jiffies + HZ / 2 +
4490 HZ * acb->eeprom.delay_time;
4491
4492 /*spin_lock_irq (&io_request_lock); */
4493 }
4494}
4495
4496
4497/**
4498 * init_adapter - Grab the resource for the card, setup the adapter
4499 * information, set the card into a known state, create the various
4500 * tables etc etc. This basically gets all adapter information all up
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04004501 * to date, initialised and gets the chip in sync with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 *
4503 * @host: This hosts adapter structure
4504 * @io_port: The base I/O port
4505 * @irq: IRQ
4506 *
4507 * Returns 0 if the initialization succeeds, any other value on
4508 * failure.
4509 **/
4510static int __devinit adapter_init(struct AdapterCtlBlk *acb,
4511 unsigned long io_port, u32 io_port_len, unsigned int irq)
4512{
4513 if (!request_region(io_port, io_port_len, DC395X_NAME)) {
4514 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
4515 goto failed;
4516 }
4517 /* store port base to indicate we have registered it */
4518 acb->io_port_base = io_port;
4519 acb->io_port_len = io_port_len;
4520
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07004521 if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 /* release the region we just claimed */
4523 dprintkl(KERN_INFO, "Failed to register IRQ\n");
4524 goto failed;
4525 }
4526 /* store irq to indicate we have registered it */
4527 acb->irq_level = irq;
4528
4529 /* get eeprom configuration information and command line settings etc */
4530 check_eeprom(&acb->eeprom, io_port);
4531 print_eeprom_settings(&acb->eeprom);
4532
4533 /* setup adapter control block */
4534 adapter_init_params(acb);
4535
4536 /* display card connectors/termination settings */
4537 adapter_print_config(acb);
4538
4539 if (adapter_sg_tables_alloc(acb)) {
4540 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
4541 goto failed;
4542 }
4543 adapter_init_scsi_host(acb->scsi_host);
4544 adapter_init_chip(acb);
4545 set_basic_config(acb);
4546
4547 dprintkdbg(DBG_0,
4548 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
4549 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
4550 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
4551 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
4552 return 0;
4553
4554failed:
4555 if (acb->irq_level)
4556 free_irq(acb->irq_level, acb);
4557 if (acb->io_port_base)
4558 release_region(acb->io_port_base, acb->io_port_len);
4559 adapter_sg_tables_free(acb);
4560
4561 return 1;
4562}
4563
4564
4565/**
4566 * adapter_uninit_chip - cleanly shut down the scsi controller chip,
4567 * stopping all operations and disabling interrupt generation on the
4568 * card.
4569 *
4570 * @acb: The adapter which we are to shutdown.
4571 **/
4572static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
4573{
4574 /* disable interrupts */
4575 DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
4576 DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
4577
4578 /* reset the scsi bus */
4579 if (acb->config & HCC_SCSI_RESET)
4580 reset_scsi_bus(acb);
4581
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02004582 /* clear any pending interrupt state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
4584}
4585
4586
4587
4588/**
4589 * adapter_uninit - Shut down the chip and release any resources that
4590 * we had allocated. Once this returns the adapter should not be used
4591 * anymore.
4592 *
4593 * @acb: The adapter which we are to un-initialize.
4594 **/
4595static void adapter_uninit(struct AdapterCtlBlk *acb)
4596{
4597 unsigned long flags;
4598 DC395x_LOCK_IO(acb->scsi_host, flags);
4599
4600 /* remove timers */
4601 if (timer_pending(&acb->waiting_timer))
4602 del_timer(&acb->waiting_timer);
4603 if (timer_pending(&acb->selto_timer))
4604 del_timer(&acb->selto_timer);
4605
4606 adapter_uninit_chip(acb);
4607 adapter_remove_and_free_all_devices(acb);
4608 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4609
4610 if (acb->irq_level)
4611 free_irq(acb->irq_level, acb);
4612 if (acb->io_port_base)
4613 release_region(acb->io_port_base, acb->io_port_len);
4614
4615 adapter_sg_tables_free(acb);
4616}
4617
4618
4619#undef SPRINTF
4620#define SPRINTF(args...) pos += sprintf(pos, args)
4621
4622#undef YESNO
4623#define YESNO(YN) \
4624 if (YN) SPRINTF(" Yes ");\
4625 else SPRINTF(" No ")
4626
4627static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
4628 char **start, off_t offset, int length, int inout)
4629{
4630 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
4631 int spd, spd1;
4632 char *pos = buffer;
4633 struct DeviceCtlBlk *dcb;
4634 unsigned long flags;
4635 int dev;
4636
4637 if (inout) /* Has data been written to the file ? */
4638 return -EPERM;
4639
4640 SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
4641 SPRINTF(" Driver Version " DC395X_VERSION "\n");
4642
4643 DC395x_LOCK_IO(acb->scsi_host, flags);
4644
4645 SPRINTF("SCSI Host Nr %i, ", host->host_no);
4646 SPRINTF("DC395U/UW/F DC315/U %s\n",
4647 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
4648 SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
4649 SPRINTF("irq_level 0x%04x, ", acb->irq_level);
4650 SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
4651
4652 SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
4653 SPRINTF("AdapterID %i\n", host->this_id);
4654
4655 SPRINTF("tag_max_num %i", acb->tag_max_num);
4656 /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
4657 SPRINTF(", FilterCfg 0x%02x",
4658 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
4659 SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
4660 /*SPRINTF("\n"); */
4661
4662 SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4663 SPRINTF
4664 ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
4665 acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
4666 acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
4667 acb->dcb_map[6], acb->dcb_map[7]);
4668 SPRINTF
4669 (" %02x %02x %02x %02x %02x %02x %02x %02x\n",
4670 acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
4671 acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
4672 acb->dcb_map[14], acb->dcb_map[15]);
4673
4674 SPRINTF
4675 ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
4676
4677 dev = 0;
4678 list_for_each_entry(dcb, &acb->dcb_list, list) {
4679 int nego_period;
4680 SPRINTF("%02i %02i %02i ", dev, dcb->target_id,
4681 dcb->target_lun);
4682 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
4683 YESNO(dcb->sync_offset);
4684 YESNO(dcb->sync_period & WIDE_SYNC);
4685 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
4686 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
4687 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
4688 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
4689 if (dcb->sync_offset)
4690 SPRINTF(" %03i ns ", nego_period);
4691 else
4692 SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
4693
4694 if (dcb->sync_offset & 0x0f) {
4695 spd = 1000 / (nego_period);
4696 spd1 = 1000 % (nego_period);
4697 spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
4698 SPRINTF(" %2i.%1i M %02i ", spd, spd1,
4699 (dcb->sync_offset & 0x0f));
4700 } else
4701 SPRINTF(" ");
4702
4703 /* Add more info ... */
4704 SPRINTF(" %02i\n", dcb->max_command);
4705 dev++;
4706 }
4707
4708 if (timer_pending(&acb->waiting_timer))
4709 SPRINTF("Waiting queue timer running\n");
4710 else
4711 SPRINTF("\n");
4712
4713 list_for_each_entry(dcb, &acb->dcb_list, list) {
4714 struct ScsiReqBlk *srb;
4715 if (!list_empty(&dcb->srb_waiting_list))
4716 SPRINTF("DCB (%02i-%i): Waiting: %i:",
4717 dcb->target_id, dcb->target_lun,
4718 list_size(&dcb->srb_waiting_list));
4719 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
Matthew Wilcox12a44162007-09-18 19:54:43 -06004720 SPRINTF(" %li", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (!list_empty(&dcb->srb_going_list))
4722 SPRINTF("\nDCB (%02i-%i): Going : %i:",
4723 dcb->target_id, dcb->target_lun,
4724 list_size(&dcb->srb_going_list));
4725 list_for_each_entry(srb, &dcb->srb_going_list, list)
Matthew Wilcox12a44162007-09-18 19:54:43 -06004726 SPRINTF(" %li", srb->cmd->serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4728 SPRINTF("\n");
4729 }
4730
4731 if (debug_enabled(DBG_1)) {
4732 SPRINTF("DCB list for ACB %p:\n", acb);
4733 list_for_each_entry(dcb, &acb->dcb_list, list) {
4734 SPRINTF("%p -> ", dcb);
4735 }
4736 SPRINTF("END\n");
4737 }
4738
4739 *start = buffer + offset;
4740 DC395x_UNLOCK_IO(acb->scsi_host, flags);
4741
4742 if (pos - buffer < offset)
4743 return 0;
4744 else if (pos - buffer - offset < length)
4745 return pos - buffer - offset;
4746 else
4747 return length;
4748}
4749
4750
4751static struct scsi_host_template dc395x_driver_template = {
4752 .module = THIS_MODULE,
4753 .proc_name = DC395X_NAME,
4754 .proc_info = dc395x_proc_info,
4755 .name = DC395X_BANNER " " DC395X_VERSION,
4756 .queuecommand = dc395x_queue_command,
4757 .bios_param = dc395x_bios_param,
4758 .slave_alloc = dc395x_slave_alloc,
4759 .slave_destroy = dc395x_slave_destroy,
4760 .can_queue = DC395x_MAX_CAN_QUEUE,
4761 .this_id = 7,
4762 .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
4763 .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
4764 .eh_abort_handler = dc395x_eh_abort,
4765 .eh_bus_reset_handler = dc395x_eh_bus_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 .use_clustering = DISABLE_CLUSTERING,
4767};
4768
4769
4770/**
4771 * banner_display - Display banner on first instance of driver
4772 * initialized.
4773 **/
4774static void banner_display(void)
4775{
4776 static int banner_done = 0;
4777 if (!banner_done)
4778 {
4779 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
4780 banner_done = 1;
4781 }
4782}
4783
4784
4785/**
4786 * dc395x_init_one - Initialise a single instance of the adapter.
4787 *
4788 * The PCI layer will call this once for each instance of the adapter
4789 * that it finds in the system. The pci_dev strcuture indicates which
4790 * instance we are being called from.
4791 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04004792 * @dev: The PCI device to initialize.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 * @id: Looks like a pointer to the entry in our pci device table
4794 * that was actually matched by the PCI subsystem.
4795 *
4796 * Returns 0 on success, or an error code (-ve) on failure.
4797 **/
4798static int __devinit dc395x_init_one(struct pci_dev *dev,
4799 const struct pci_device_id *id)
4800{
4801 struct Scsi_Host *scsi_host = NULL;
4802 struct AdapterCtlBlk *acb = NULL;
4803 unsigned long io_port_base;
4804 unsigned int io_port_len;
4805 unsigned int irq;
4806
4807 dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
4808 banner_display();
4809
4810 if (pci_enable_device(dev))
4811 {
4812 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
4813 return -ENODEV;
4814 }
4815 io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
4816 io_port_len = pci_resource_len(dev, 0);
4817 irq = dev->irq;
4818 dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
4819
4820 /* allocate scsi host information (includes out adapter) */
4821 scsi_host = scsi_host_alloc(&dc395x_driver_template,
4822 sizeof(struct AdapterCtlBlk));
4823 if (!scsi_host) {
4824 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
4825 goto fail;
4826 }
4827 acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
4828 acb->scsi_host = scsi_host;
4829 acb->dev = dev;
4830
4831 /* initialise the adapter and everything we need */
4832 if (adapter_init(acb, io_port_base, io_port_len, irq)) {
4833 dprintkl(KERN_INFO, "adapter init failed\n");
4834 goto fail;
4835 }
4836
4837 pci_set_master(dev);
4838
4839 /* get the scsi mid level to scan for new devices on the bus */
4840 if (scsi_add_host(scsi_host, &dev->dev)) {
4841 dprintkl(KERN_ERR, "scsi_add_host failed\n");
4842 goto fail;
4843 }
4844 pci_set_drvdata(dev, scsi_host);
4845 scsi_scan_host(scsi_host);
4846
4847 return 0;
4848
4849fail:
4850 if (acb != NULL)
4851 adapter_uninit(acb);
4852 if (scsi_host != NULL)
4853 scsi_host_put(scsi_host);
4854 pci_disable_device(dev);
4855 return -ENODEV;
4856}
4857
4858
4859/**
4860 * dc395x_remove_one - Called to remove a single instance of the
4861 * adapter.
4862 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04004863 * @dev: The PCI device to initialize.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 **/
4865static void __devexit dc395x_remove_one(struct pci_dev *dev)
4866{
4867 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
4868 struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
4869
4870 dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
4871
4872 scsi_remove_host(scsi_host);
4873 adapter_uninit(acb);
4874 pci_disable_device(dev);
4875 scsi_host_put(scsi_host);
4876 pci_set_drvdata(dev, NULL);
4877}
4878
4879
4880static struct pci_device_id dc395x_pci_table[] = {
4881 {
4882 .vendor = PCI_VENDOR_ID_TEKRAM,
4883 .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
4884 .subvendor = PCI_ANY_ID,
4885 .subdevice = PCI_ANY_ID,
4886 },
4887 {} /* Terminating entry */
4888};
4889MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
4890
4891
4892static struct pci_driver dc395x_driver = {
4893 .name = DC395X_NAME,
4894 .id_table = dc395x_pci_table,
4895 .probe = dc395x_init_one,
4896 .remove = __devexit_p(dc395x_remove_one),
4897};
4898
4899
4900/**
4901 * dc395x_module_init - Module initialization function
4902 *
4903 * Used by both module and built-in driver to initialise this driver.
4904 **/
4905static int __init dc395x_module_init(void)
4906{
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07004907 return pci_register_driver(&dc395x_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
4910
4911/**
4912 * dc395x_module_exit - Module cleanup function.
4913 **/
4914static void __exit dc395x_module_exit(void)
4915{
4916 pci_unregister_driver(&dc395x_driver);
4917}
4918
4919
4920module_init(dc395x_module_init);
4921module_exit(dc395x_module_exit);
4922
4923MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
4924MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
4925MODULE_LICENSE("GPL");