1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NinjaSCSI-32Bi Cardbus, NinjaSCSI-32UDE PCI/CardBus SCSI driver
4 * Copyright (C) 2001, 2002, 2003
5 * YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
6 * GOTO Masanori <gotom@debian.or.jp>, <gotom@debian.org>
9 * 1.0: Initial Release.
10 * 1.1: Add /proc SDTR status.
11 * Remove obsolete error handler nsp32_reset.
13 * 1.2: PowerPC (big endian) support.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/timer.h>
21 #include <linux/ioport.h>
22 #include <linux/major.h>
23 #include <linux/blkdev.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/delay.h>
27 #include <linux/ctype.h>
28 #include <linux/dma-mapping.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_cmnd.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_host.h>
37 #include <scsi/scsi_ioctl.h>
42 /***********************************************************************
45 static int trans_mode
= 0; /* default: BIOS */
46 module_param (trans_mode
, int, 0);
47 MODULE_PARM_DESC(trans_mode
, "transfer mode (0: BIOS(default) 1: Async 2: Ultra20M");
49 #define ULTRA20M_MODE 2
51 static bool auto_param
= 0; /* default: ON */
52 module_param (auto_param
, bool, 0);
53 MODULE_PARM_DESC(auto_param
, "AutoParameter mode (0: ON(default) 1: OFF)");
55 static bool disc_priv
= 1; /* default: OFF */
56 module_param (disc_priv
, bool, 0);
57 MODULE_PARM_DESC(disc_priv
, "disconnection privilege mode (0: ON 1: OFF(default))");
59 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>, GOTO Masanori <gotom@debian.or.jp>");
60 MODULE_DESCRIPTION("Workbit NinjaSCSI-32Bi/UDE CardBus/PCI SCSI host bus adapter module");
61 MODULE_LICENSE("GPL");
63 static const char *nsp32_release_version
= "1.2";
66 /****************************************************************************
69 static struct pci_device_id nsp32_pci_table
[] = {
71 .vendor
= PCI_VENDOR_ID_IODATA
,
72 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_CBSC_II
,
73 .subvendor
= PCI_ANY_ID
,
74 .subdevice
= PCI_ANY_ID
,
75 .driver_data
= MODEL_IODATA
,
78 .vendor
= PCI_VENDOR_ID_WORKBIT
,
79 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_KME
,
80 .subvendor
= PCI_ANY_ID
,
81 .subdevice
= PCI_ANY_ID
,
82 .driver_data
= MODEL_KME
,
85 .vendor
= PCI_VENDOR_ID_WORKBIT
,
86 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_WBT
,
87 .subvendor
= PCI_ANY_ID
,
88 .subdevice
= PCI_ANY_ID
,
89 .driver_data
= MODEL_WORKBIT
,
92 .vendor
= PCI_VENDOR_ID_WORKBIT
,
93 .device
= PCI_DEVICE_ID_WORKBIT_STANDARD
,
94 .subvendor
= PCI_ANY_ID
,
95 .subdevice
= PCI_ANY_ID
,
96 .driver_data
= MODEL_PCI_WORKBIT
,
99 .vendor
= PCI_VENDOR_ID_WORKBIT
,
100 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_LOGITEC
,
101 .subvendor
= PCI_ANY_ID
,
102 .subdevice
= PCI_ANY_ID
,
103 .driver_data
= MODEL_LOGITEC
,
106 .vendor
= PCI_VENDOR_ID_WORKBIT
,
107 .device
= PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC
,
108 .subvendor
= PCI_ANY_ID
,
109 .subdevice
= PCI_ANY_ID
,
110 .driver_data
= MODEL_PCI_LOGITEC
,
113 .vendor
= PCI_VENDOR_ID_WORKBIT
,
114 .device
= PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO
,
115 .subvendor
= PCI_ANY_ID
,
116 .subdevice
= PCI_ANY_ID
,
117 .driver_data
= MODEL_PCI_MELCO
,
120 .vendor
= PCI_VENDOR_ID_WORKBIT
,
121 .device
= PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO_II
,
122 .subvendor
= PCI_ANY_ID
,
123 .subdevice
= PCI_ANY_ID
,
124 .driver_data
= MODEL_PCI_MELCO
,
128 MODULE_DEVICE_TABLE(pci
, nsp32_pci_table
);
130 static nsp32_hw_data nsp32_data_base
; /* probe <-> detect glue */
134 * Period/AckWidth speed conversion table
136 * Note: This period/ackwidth speed table must be in descending order.
138 static nsp32_sync_table nsp32_sync_table_40M
[] = {
139 /* {PNo, AW, SP, EP, SREQ smpl} Speed(MB/s) Period AckWidth */
140 {0x1, 0, 0x0c, 0x0c, SMPL_40M
}, /* 20.0 : 50ns, 25ns */
141 {0x2, 0, 0x0d, 0x18, SMPL_40M
}, /* 13.3 : 75ns, 25ns */
142 {0x3, 1, 0x19, 0x19, SMPL_40M
}, /* 10.0 : 100ns, 50ns */
143 {0x4, 1, 0x1a, 0x1f, SMPL_20M
}, /* 8.0 : 125ns, 50ns */
144 {0x5, 2, 0x20, 0x25, SMPL_20M
}, /* 6.7 : 150ns, 75ns */
145 {0x6, 2, 0x26, 0x31, SMPL_20M
}, /* 5.7 : 175ns, 75ns */
146 {0x7, 3, 0x32, 0x32, SMPL_20M
}, /* 5.0 : 200ns, 100ns */
147 {0x8, 3, 0x33, 0x38, SMPL_10M
}, /* 4.4 : 225ns, 100ns */
148 {0x9, 3, 0x39, 0x3e, SMPL_10M
}, /* 4.0 : 250ns, 100ns */
151 static nsp32_sync_table nsp32_sync_table_20M
[] = {
152 {0x1, 0, 0x19, 0x19, SMPL_40M
}, /* 10.0 : 100ns, 50ns */
153 {0x2, 0, 0x1a, 0x25, SMPL_20M
}, /* 6.7 : 150ns, 50ns */
154 {0x3, 1, 0x26, 0x32, SMPL_20M
}, /* 5.0 : 200ns, 100ns */
155 {0x4, 1, 0x33, 0x3e, SMPL_10M
}, /* 4.0 : 250ns, 100ns */
156 {0x5, 2, 0x3f, 0x4b, SMPL_10M
}, /* 3.3 : 300ns, 150ns */
157 {0x6, 2, 0x4c, 0x57, SMPL_10M
}, /* 2.8 : 350ns, 150ns */
158 {0x7, 3, 0x58, 0x64, SMPL_10M
}, /* 2.5 : 400ns, 200ns */
159 {0x8, 3, 0x65, 0x70, SMPL_10M
}, /* 2.2 : 450ns, 200ns */
160 {0x9, 3, 0x71, 0x7d, SMPL_10M
}, /* 2.0 : 500ns, 200ns */
163 static nsp32_sync_table nsp32_sync_table_pci
[] = {
164 {0x1, 0, 0x0c, 0x0f, SMPL_40M
}, /* 16.6 : 60ns, 30ns */
165 {0x2, 0, 0x10, 0x16, SMPL_40M
}, /* 11.1 : 90ns, 30ns */
166 {0x3, 1, 0x17, 0x1e, SMPL_20M
}, /* 8.3 : 120ns, 60ns */
167 {0x4, 1, 0x1f, 0x25, SMPL_20M
}, /* 6.7 : 150ns, 60ns */
168 {0x5, 2, 0x26, 0x2d, SMPL_20M
}, /* 5.6 : 180ns, 90ns */
169 {0x6, 2, 0x2e, 0x34, SMPL_10M
}, /* 4.8 : 210ns, 90ns */
170 {0x7, 3, 0x35, 0x3c, SMPL_10M
}, /* 4.2 : 240ns, 120ns */
171 {0x8, 3, 0x3d, 0x43, SMPL_10M
}, /* 3.7 : 270ns, 120ns */
172 {0x9, 3, 0x44, 0x4b, SMPL_10M
}, /* 3.3 : 300ns, 120ns */
176 * function declaration
178 /* module entry point */
179 static int nsp32_probe (struct pci_dev
*, const struct pci_device_id
*);
180 static void nsp32_remove(struct pci_dev
*);
181 static int __init
init_nsp32 (void);
182 static void __exit
exit_nsp32 (void);
184 /* struct struct scsi_host_template */
185 static int nsp32_show_info (struct seq_file
*, struct Scsi_Host
*);
187 static int nsp32_detect (struct pci_dev
*pdev
);
188 static int nsp32_queuecommand(struct Scsi_Host
*, struct scsi_cmnd
*);
189 static const char *nsp32_info (struct Scsi_Host
*);
190 static int nsp32_release (struct Scsi_Host
*);
192 /* SCSI error handler */
193 static int nsp32_eh_abort (struct scsi_cmnd
*);
194 static int nsp32_eh_host_reset(struct scsi_cmnd
*);
196 /* generate SCSI message */
197 static void nsp32_build_identify(struct scsi_cmnd
*);
198 static void nsp32_build_nop (struct scsi_cmnd
*);
199 static void nsp32_build_reject (struct scsi_cmnd
*);
200 static void nsp32_build_sdtr (struct scsi_cmnd
*, unsigned char, unsigned char);
202 /* SCSI message handler */
203 static int nsp32_busfree_occur(struct scsi_cmnd
*, unsigned short);
204 static void nsp32_msgout_occur (struct scsi_cmnd
*);
205 static void nsp32_msgin_occur (struct scsi_cmnd
*, unsigned long, unsigned short);
207 static int nsp32_setup_sg_table (struct scsi_cmnd
*);
208 static int nsp32_selection_autopara(struct scsi_cmnd
*);
209 static int nsp32_selection_autoscsi(struct scsi_cmnd
*);
210 static void nsp32_scsi_done (struct scsi_cmnd
*);
211 static int nsp32_arbitration (struct scsi_cmnd
*, unsigned int);
212 static int nsp32_reselection (struct scsi_cmnd
*, unsigned char);
213 static void nsp32_adjust_busfree (struct scsi_cmnd
*, unsigned int);
214 static void nsp32_restart_autoscsi (struct scsi_cmnd
*, unsigned short);
217 static void nsp32_analyze_sdtr (struct scsi_cmnd
*);
218 static int nsp32_search_period_entry(nsp32_hw_data
*, nsp32_target
*, unsigned char);
219 static void nsp32_set_async (nsp32_hw_data
*, nsp32_target
*);
220 static void nsp32_set_max_sync (nsp32_hw_data
*, nsp32_target
*, unsigned char *, unsigned char *);
221 static void nsp32_set_sync_entry (nsp32_hw_data
*, nsp32_target
*, int, unsigned char);
223 /* SCSI bus status handler */
224 static void nsp32_wait_req (nsp32_hw_data
*, int);
225 static void nsp32_wait_sack (nsp32_hw_data
*, int);
226 static void nsp32_sack_assert (nsp32_hw_data
*);
227 static void nsp32_sack_negate (nsp32_hw_data
*);
228 static void nsp32_do_bus_reset(nsp32_hw_data
*);
230 /* hardware interrupt handler */
231 static irqreturn_t
do_nsp32_isr(int, void *);
233 /* initialize hardware */
234 static int nsp32hw_init(nsp32_hw_data
*);
237 static int nsp32_getprom_param (nsp32_hw_data
*);
238 static int nsp32_getprom_at24 (nsp32_hw_data
*);
239 static int nsp32_getprom_c16 (nsp32_hw_data
*);
240 static void nsp32_prom_start (nsp32_hw_data
*);
241 static void nsp32_prom_stop (nsp32_hw_data
*);
242 static int nsp32_prom_read (nsp32_hw_data
*, int);
243 static int nsp32_prom_read_bit (nsp32_hw_data
*);
244 static void nsp32_prom_write_bit(nsp32_hw_data
*, int);
245 static void nsp32_prom_set (nsp32_hw_data
*, int, int);
246 static int nsp32_prom_get (nsp32_hw_data
*, int);
248 /* debug/warning/info message */
249 static void nsp32_message (const char *, int, char *, char *, ...);
251 static void nsp32_dmessage(const char *, int, int, char *, ...);
255 * max_sectors is currently limited up to 128.
257 static struct scsi_host_template nsp32_template
= {
258 .proc_name
= "nsp32",
259 .name
= "Workbit NinjaSCSI-32Bi/UDE",
260 .show_info
= nsp32_show_info
,
262 .queuecommand
= nsp32_queuecommand
,
264 .sg_tablesize
= NSP32_SG_SIZE
,
266 .this_id
= NSP32_HOST_SCSIID
,
267 .dma_boundary
= PAGE_SIZE
- 1,
268 .eh_abort_handler
= nsp32_eh_abort
,
269 .eh_host_reset_handler
= nsp32_eh_host_reset
,
270 /* .highmem_io = 1, */
273 #include "nsp32_io.h"
275 /***********************************************************************
279 # define NSP32_DEBUG_MASK 0x000000
280 # define nsp32_msg(type, args...) nsp32_message ("", 0, (type), args)
281 # define nsp32_dbg(mask, args...) /* */
283 # define NSP32_DEBUG_MASK 0xffffff
284 # define nsp32_msg(type, args...) \
285 nsp32_message (__func__, __LINE__, (type), args)
286 # define nsp32_dbg(mask, args...) \
287 nsp32_dmessage(__func__, __LINE__, (mask), args)
290 #define NSP32_DEBUG_QUEUECOMMAND BIT(0)
291 #define NSP32_DEBUG_REGISTER BIT(1)
292 #define NSP32_DEBUG_AUTOSCSI BIT(2)
293 #define NSP32_DEBUG_INTR BIT(3)
294 #define NSP32_DEBUG_SGLIST BIT(4)
295 #define NSP32_DEBUG_BUSFREE BIT(5)
296 #define NSP32_DEBUG_CDB_CONTENTS BIT(6)
297 #define NSP32_DEBUG_RESELECTION BIT(7)
298 #define NSP32_DEBUG_MSGINOCCUR BIT(8)
299 #define NSP32_DEBUG_EEPROM BIT(9)
300 #define NSP32_DEBUG_MSGOUTOCCUR BIT(10)
301 #define NSP32_DEBUG_BUSRESET BIT(11)
302 #define NSP32_DEBUG_RESTART BIT(12)
303 #define NSP32_DEBUG_SYNC BIT(13)
304 #define NSP32_DEBUG_WAIT BIT(14)
305 #define NSP32_DEBUG_TARGETFLAG BIT(15)
306 #define NSP32_DEBUG_PROC BIT(16)
307 #define NSP32_DEBUG_INIT BIT(17)
308 #define NSP32_SPECIAL_PRINT_REGISTER BIT(20)
310 #define NSP32_DEBUG_BUF_LEN 100
312 static void nsp32_message(const char *func
, int line
, char *type
, char *fmt
, ...)
315 char buf
[NSP32_DEBUG_BUF_LEN
];
318 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
322 printk("%snsp32: %s\n", type
, buf
);
324 printk("%snsp32: %s (%d): %s\n", type
, func
, line
, buf
);
329 static void nsp32_dmessage(const char *func
, int line
, int mask
, char *fmt
, ...)
332 char buf
[NSP32_DEBUG_BUF_LEN
];
335 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
338 if (mask
& NSP32_DEBUG_MASK
) {
339 printk("nsp32-debug: 0x%x %s (%d): %s\n", mask
, func
, line
, buf
);
345 # include "nsp32_debug.c"
347 # define show_command(arg) /* */
348 # define show_busphase(arg) /* */
349 # define show_autophase(arg) /* */
355 static void nsp32_build_identify(struct scsi_cmnd
*SCpnt
)
357 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
358 int pos
= data
->msgout_len
;
361 /* XXX: Auto DiscPriv detection is progressing... */
362 if (disc_priv
== 0) {
366 data
->msgoutbuf
[pos
] = IDENTIFY(mode
, SCpnt
->device
->lun
); pos
++;
368 data
->msgout_len
= pos
;
372 * SDTR Message Routine
374 static void nsp32_build_sdtr(struct scsi_cmnd
*SCpnt
,
375 unsigned char period
,
376 unsigned char offset
)
378 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
379 int pos
= data
->msgout_len
;
381 data
->msgoutbuf
[pos
] = EXTENDED_MESSAGE
; pos
++;
382 data
->msgoutbuf
[pos
] = EXTENDED_SDTR_LEN
; pos
++;
383 data
->msgoutbuf
[pos
] = EXTENDED_SDTR
; pos
++;
384 data
->msgoutbuf
[pos
] = period
; pos
++;
385 data
->msgoutbuf
[pos
] = offset
; pos
++;
387 data
->msgout_len
= pos
;
391 * No Operation Message
393 static void nsp32_build_nop(struct scsi_cmnd
*SCpnt
)
395 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
396 int pos
= data
->msgout_len
;
399 nsp32_msg(KERN_WARNING
,
400 "Some messages are already contained!");
404 data
->msgoutbuf
[pos
] = NOP
; pos
++;
405 data
->msgout_len
= pos
;
411 static void nsp32_build_reject(struct scsi_cmnd
*SCpnt
)
413 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
414 int pos
= data
->msgout_len
;
416 data
->msgoutbuf
[pos
] = MESSAGE_REJECT
; pos
++;
417 data
->msgout_len
= pos
;
424 static void nsp32_start_timer(struct scsi_cmnd
*SCpnt
, int time
)
426 unsigned int base
= SCpnt
->host
->io_port
;
428 nsp32_dbg(NSP32_DEBUG_INTR
, "timer=%d", time
);
430 if (time
& (~TIMER_CNT_MASK
)) {
431 nsp32_dbg(NSP32_DEBUG_INTR
, "timer set overflow");
434 nsp32_write2(base
, TIMER_SET
, time
& TIMER_CNT_MASK
);
440 * set SCSI command and other parameter to asic, and start selection phase
442 static int nsp32_selection_autopara(struct scsi_cmnd
*SCpnt
)
444 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
445 unsigned int base
= SCpnt
->device
->host
->io_port
;
446 unsigned int host_id
= SCpnt
->device
->host
->this_id
;
447 unsigned char target
= scmd_id(SCpnt
);
448 nsp32_autoparam
*param
= data
->autoparam
;
454 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "in");
459 phase
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
460 if (phase
!= BUSMON_BUS_FREE
) {
461 nsp32_msg(KERN_WARNING
, "bus busy");
462 show_busphase(phase
& BUSMON_PHASE_MASK
);
463 SCpnt
->result
= DID_BUS_BUSY
<< 16;
470 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
471 * over 3 messages needs another routine.
473 if (data
->msgout_len
== 0) {
474 nsp32_msg(KERN_ERR
, "SCSI MsgOut without any message!");
475 SCpnt
->result
= DID_ERROR
<< 16;
477 } else if (data
->msgout_len
> 0 && data
->msgout_len
<= 3) {
479 for (i
= 0; i
< data
->msgout_len
; i
++) {
481 * the sending order of the message is:
482 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
483 * MCNT 2: MSG#1 -> MSG#2
487 msgout
|= ((unsigned int)(data
->msgoutbuf
[i
]) << 24);
489 msgout
|= MV_VALID
; /* MV valid */
490 msgout
|= (unsigned int)data
->msgout_len
; /* len */
492 /* data->msgout_len > 3 */
496 // nsp_dbg(NSP32_DEBUG_AUTOSCSI, "sel time out=0x%x\n", nsp32_read2(base, SEL_TIME_OUT));
497 // nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
500 * setup asic parameter
502 memset(param
, 0, sizeof(nsp32_autoparam
));
505 for (i
= 0; i
< SCpnt
->cmd_len
; i
++) {
506 param
->cdb
[4 * i
] = SCpnt
->cmnd
[i
];
509 /* outgoing messages */
510 param
->msgout
= cpu_to_le32(msgout
);
512 /* syncreg, ackwidth, target id, SREQ sampling rate */
513 param
->syncreg
= data
->cur_target
->syncreg
;
514 param
->ackwidth
= data
->cur_target
->ackwidth
;
515 param
->target_id
= BIT(host_id
) | BIT(target
);
516 param
->sample_reg
= data
->cur_target
->sample_reg
;
518 // nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "sample rate=0x%x\n", data->cur_target->sample_reg);
520 /* command control */
521 param
->command_control
= cpu_to_le16(CLEAR_CDB_FIFO_POINTER
|
523 AUTO_MSGIN_00_OR_04
|
528 /* transfer control */
530 switch (data
->trans_method
) {
531 case NSP32_TRANSFER_BUSMASTER
:
534 case NSP32_TRANSFER_MMIO
:
537 case NSP32_TRANSFER_PIO
:
541 nsp32_msg(KERN_ERR
, "unknown trans_method");
545 * OR-ed BLIEND_MODE, FIFO intr is decreased, instead of PCI bus waits.
546 * For bus master transfer, it's taken off.
548 s
|= (TRANSFER_GO
| ALL_COUNTER_CLR
);
549 param
->transfer_control
= cpu_to_le16(s
);
552 param
->sgt_pointer
= cpu_to_le32(data
->cur_lunt
->sglun_paddr
);
555 * transfer parameter to ASIC
557 nsp32_write4(base
, SGT_ADR
, data
->auto_paddr
);
558 nsp32_write2(base
, COMMAND_CONTROL
, CLEAR_CDB_FIFO_POINTER
|
564 ret
= nsp32_arbitration(SCpnt
, base
);
571 * Selection with AUTO SCSI (without AUTO PARAMETER)
573 static int nsp32_selection_autoscsi(struct scsi_cmnd
*SCpnt
)
575 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
576 unsigned int base
= SCpnt
->device
->host
->io_port
;
577 unsigned int host_id
= SCpnt
->device
->host
->this_id
;
578 unsigned char target
= scmd_id(SCpnt
);
581 unsigned short command
= 0;
582 unsigned int msgout
= 0;
583 unsigned short execph
;
586 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "in");
591 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
596 phase
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
597 if ((phase
& BUSMON_BSY
) || (phase
& BUSMON_SEL
)) {
598 nsp32_msg(KERN_WARNING
, "bus busy");
599 SCpnt
->result
= DID_BUS_BUSY
<< 16;
607 execph
= nsp32_read2(base
, SCSI_EXECUTE_PHASE
);
610 * clear FIFO counter to set CDBs
612 nsp32_write2(base
, COMMAND_CONTROL
, CLEAR_CDB_FIFO_POINTER
);
617 for (i
= 0; i
< SCpnt
->cmd_len
; i
++) {
618 nsp32_write1(base
, COMMAND_DATA
, SCpnt
->cmnd
[i
]);
620 nsp32_dbg(NSP32_DEBUG_CDB_CONTENTS
, "CDB[0]=[0x%x]", SCpnt
->cmnd
[0]);
623 * set SCSIOUT LATCH(initiator)/TARGET(target) (OR-ed) ID
625 nsp32_write1(base
, SCSI_OUT_LATCH_TARGET_ID
, BIT(host_id
) | BIT(target
));
628 * set SCSI MSGOUT REG
630 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
631 * over 3 messages needs another routine.
633 if (data
->msgout_len
== 0) {
634 nsp32_msg(KERN_ERR
, "SCSI MsgOut without any message!");
635 SCpnt
->result
= DID_ERROR
<< 16;
638 } else if (data
->msgout_len
> 0 && data
->msgout_len
<= 3) {
640 for (i
= 0; i
< data
->msgout_len
; i
++) {
642 * the sending order of the message is:
643 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
644 * MCNT 2: MSG#1 -> MSG#2
648 msgout
|= ((unsigned int)(data
->msgoutbuf
[i
]) << 24);
650 msgout
|= MV_VALID
; /* MV valid */
651 msgout
|= (unsigned int)data
->msgout_len
; /* len */
652 nsp32_write4(base
, SCSI_MSG_OUT
, msgout
);
654 /* data->msgout_len > 3 */
655 nsp32_write4(base
, SCSI_MSG_OUT
, 0);
659 * set selection timeout(= 250ms)
661 nsp32_write2(base
, SEL_TIME_OUT
, SEL_TIMEOUT_TIME
);
664 * set SREQ hazard killer sampling rate
666 * TODO: sample_rate (BASE+0F) is 0 when internal clock = 40MHz.
667 * check other internal clock!
669 nsp32_write1(base
, SREQ_SMPL_RATE
, data
->cur_target
->sample_reg
);
674 nsp32_write1(base
, SET_ARBIT
, ARBIT_CLEAR
);
678 * Don't set BM_START_ADR before setting this register.
680 nsp32_write1(base
, SYNC_REG
, data
->cur_target
->syncreg
);
685 nsp32_write1(base
, ACK_WIDTH
, data
->cur_target
->ackwidth
);
687 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
,
688 "syncreg=0x%x, ackwidth=0x%x, sgtpaddr=0x%x, id=0x%x",
689 nsp32_read1(base
, SYNC_REG
), nsp32_read1(base
, ACK_WIDTH
),
690 nsp32_read4(base
, SGT_ADR
), nsp32_read1(base
, SCSI_OUT_LATCH_TARGET_ID
));
691 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "msgout_len=%d, msgout=0x%x",
692 data
->msgout_len
, msgout
);
695 * set SGT ADDR (physical address)
697 nsp32_write4(base
, SGT_ADR
, data
->cur_lunt
->sglun_paddr
);
700 * set TRANSFER CONTROL REG
703 command
|= (TRANSFER_GO
| ALL_COUNTER_CLR
);
704 if (data
->trans_method
& NSP32_TRANSFER_BUSMASTER
) {
705 if (scsi_bufflen(SCpnt
) > 0) {
708 } else if (data
->trans_method
& NSP32_TRANSFER_MMIO
) {
709 command
|= CB_MMIO_MODE
;
710 } else if (data
->trans_method
& NSP32_TRANSFER_PIO
) {
711 command
|= CB_IO_MODE
;
713 nsp32_write2(base
, TRANSFER_CONTROL
, command
);
716 * start AUTO SCSI, kick off arbitration
718 command
= (CLEAR_CDB_FIFO_POINTER
|
720 AUTO_MSGIN_00_OR_04
|
723 nsp32_write2(base
, COMMAND_CONTROL
, command
);
728 status
= nsp32_arbitration(SCpnt
, base
);
734 nsp32_write2(base
, IRQ_CONTROL
, 0);
741 * Arbitration Status Check
743 * Note: Arbitration counter is waited during ARBIT_GO is not lifting.
744 * Using udelay(1) consumes CPU time and system time, but
745 * arbitration delay time is defined minimal 2.4us in SCSI
746 * specification, thus udelay works as coarse grained wait timer.
748 static int nsp32_arbitration(struct scsi_cmnd
*SCpnt
, unsigned int base
)
755 arbit
= nsp32_read1(base
, ARBIT_STATUS
);
757 } while ((arbit
& (ARBIT_WIN
| ARBIT_FAIL
)) == 0 &&
758 (time
<= ARBIT_TIMEOUT_TIME
));
760 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
,
761 "arbit: 0x%x, delay time: %d", arbit
, time
);
763 if (arbit
& ARBIT_WIN
) {
764 /* Arbitration succeeded */
765 SCpnt
->result
= DID_OK
<< 16;
766 nsp32_index_write1(base
, EXT_PORT
, LED_ON
); /* PCI LED on */
767 } else if (arbit
& ARBIT_FAIL
) {
768 /* Arbitration failed */
769 SCpnt
->result
= DID_BUS_BUSY
<< 16;
773 * unknown error or ARBIT_GO timeout,
774 * something lock up! guess no connection.
776 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "arbit timeout");
777 SCpnt
->result
= DID_NO_CONNECT
<< 16;
784 nsp32_write1(base
, SET_ARBIT
, ARBIT_CLEAR
);
793 * Note: This reselection routine is called from msgin_occur,
794 * reselection target id&lun must be already set.
795 * SCSI-2 says IDENTIFY implies RESTORE_POINTER operation.
797 static int nsp32_reselection(struct scsi_cmnd
*SCpnt
, unsigned char newlun
)
799 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
800 unsigned int host_id
= SCpnt
->device
->host
->this_id
;
801 unsigned int base
= SCpnt
->device
->host
->io_port
;
802 unsigned char tmpid
, newid
;
804 nsp32_dbg(NSP32_DEBUG_RESELECTION
, "enter");
807 * calculate reselected SCSI ID
809 tmpid
= nsp32_read1(base
, RESELECT_ID
);
810 tmpid
&= (~BIT(host_id
));
821 * If reselected New ID:LUN is not existed
822 * or current nexus is not existed, unexpected
823 * reselection is occurred. Send reject message.
825 if (newid
>= ARRAY_SIZE(data
->lunt
) || newlun
>= ARRAY_SIZE(data
->lunt
[0])) {
826 nsp32_msg(KERN_WARNING
, "unknown id/lun");
828 } else if(data
->lunt
[newid
][newlun
].SCpnt
== NULL
) {
829 nsp32_msg(KERN_WARNING
, "no SCSI command is processing");
833 data
->cur_id
= newid
;
834 data
->cur_lun
= newlun
;
835 data
->cur_target
= &(data
->target
[newid
]);
836 data
->cur_lunt
= &(data
->lunt
[newid
][newlun
]);
838 /* reset SACK/SavedACK counter (or ALL clear?) */
839 nsp32_write4(base
, CLR_COUNTER
, CLRCOUNTER_ALLMASK
);
846 * nsp32_setup_sg_table - build scatter gather list for transfer data
849 * Note: NinjaSCSI-32Bi/UDE bus master can not transfer over 64KB at a time.
851 static int nsp32_setup_sg_table(struct scsi_cmnd
*SCpnt
)
853 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
854 struct scatterlist
*sg
;
855 nsp32_sgtable
*sgt
= data
->cur_lunt
->sglun
->sgt
;
860 nsp32_dbg(NSP32_DEBUG_SGLIST
, "SGT == null");
864 num
= scsi_dma_map(SCpnt
);
870 scsi_for_each_sg(SCpnt
, sg
, num
, i
) {
872 * Build nsp32_sglist, substitute sg dma addresses.
874 sgt
[i
].addr
= cpu_to_le32(sg_dma_address(sg
));
875 sgt
[i
].len
= cpu_to_le32(sg_dma_len(sg
));
877 if (le32_to_cpu(sgt
[i
].len
) > 0x10000) {
879 "can't transfer over 64KB at a time, size=0x%lx", le32_to_cpu(sgt
[i
].len
));
882 nsp32_dbg(NSP32_DEBUG_SGLIST
,
883 "num 0x%x : addr 0x%lx len 0x%lx",
885 le32_to_cpu(sgt
[i
].addr
),
886 le32_to_cpu(sgt
[i
].len
));
890 l
= le32_to_cpu(sgt
[num
-1].len
);
891 sgt
[num
-1].len
= cpu_to_le32(l
| SGTEND
);
897 static int nsp32_queuecommand_lck(struct scsi_cmnd
*SCpnt
, void (*done
)(struct scsi_cmnd
*))
899 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
900 nsp32_target
*target
;
901 nsp32_lunt
*cur_lunt
;
904 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
905 "enter. target: 0x%x LUN: 0x%llx cmnd: 0x%x cmndlen: 0x%x "
906 "use_sg: 0x%x reqbuf: 0x%lx reqlen: 0x%x",
907 SCpnt
->device
->id
, SCpnt
->device
->lun
, SCpnt
->cmnd
[0], SCpnt
->cmd_len
,
908 scsi_sg_count(SCpnt
), scsi_sglist(SCpnt
), scsi_bufflen(SCpnt
));
910 if (data
->CurrentSC
!= NULL
) {
911 nsp32_msg(KERN_ERR
, "Currentsc != NULL. Cancel this command request");
912 data
->CurrentSC
= NULL
;
913 SCpnt
->result
= DID_NO_CONNECT
<< 16;
918 /* check target ID is not same as this initiator ID */
919 if (scmd_id(SCpnt
) == SCpnt
->device
->host
->this_id
) {
920 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
, "target==host???");
921 SCpnt
->result
= DID_BAD_TARGET
<< 16;
926 /* check target LUN is allowable value */
927 if (SCpnt
->device
->lun
>= MAX_LUN
) {
928 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
, "no more lun");
929 SCpnt
->result
= DID_BAD_TARGET
<< 16;
936 SCpnt
->scsi_done
= done
;
937 data
->CurrentSC
= SCpnt
;
938 SCpnt
->SCp
.Status
= CHECK_CONDITION
;
939 SCpnt
->SCp
.Message
= 0;
940 scsi_set_resid(SCpnt
, scsi_bufflen(SCpnt
));
942 SCpnt
->SCp
.ptr
= (char *)scsi_sglist(SCpnt
);
943 SCpnt
->SCp
.this_residual
= scsi_bufflen(SCpnt
);
944 SCpnt
->SCp
.buffer
= NULL
;
945 SCpnt
->SCp
.buffers_residual
= 0;
947 /* initialize data */
948 data
->msgout_len
= 0;
950 cur_lunt
= &(data
->lunt
[SCpnt
->device
->id
][SCpnt
->device
->lun
]);
951 cur_lunt
->SCpnt
= SCpnt
;
952 cur_lunt
->save_datp
= 0;
953 cur_lunt
->msgin03
= FALSE
;
954 data
->cur_lunt
= cur_lunt
;
955 data
->cur_id
= SCpnt
->device
->id
;
956 data
->cur_lun
= SCpnt
->device
->lun
;
958 ret
= nsp32_setup_sg_table(SCpnt
);
960 nsp32_msg(KERN_ERR
, "SGT fail");
961 SCpnt
->result
= DID_ERROR
<< 16;
962 nsp32_scsi_done(SCpnt
);
967 nsp32_build_identify(SCpnt
);
970 * If target is the first time to transfer after the reset
971 * (target don't have SDTR_DONE and SDTR_INITIATOR), sync
972 * message SDTR is needed to do synchronous transfer.
974 target
= &data
->target
[scmd_id(SCpnt
)];
975 data
->cur_target
= target
;
977 if (!(target
->sync_flag
& (SDTR_DONE
| SDTR_INITIATOR
| SDTR_TARGET
))) {
978 unsigned char period
, offset
;
980 if (trans_mode
!= ASYNC_MODE
) {
981 nsp32_set_max_sync(data
, target
, &period
, &offset
);
982 nsp32_build_sdtr(SCpnt
, period
, offset
);
983 target
->sync_flag
|= SDTR_INITIATOR
;
985 nsp32_set_async(data
, target
);
986 target
->sync_flag
|= SDTR_DONE
;
989 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
990 "SDTR: entry: %d start_period: 0x%x offset: 0x%x\n",
991 target
->limit_entry
, period
, offset
);
992 } else if (target
->sync_flag
& SDTR_INITIATOR
) {
994 * It was negotiating SDTR with target, sending from the
995 * initiator, but there are no chance to remove this flag.
996 * Set async because we don't get proper negotiation.
998 nsp32_set_async(data
, target
);
999 target
->sync_flag
&= ~SDTR_INITIATOR
;
1000 target
->sync_flag
|= SDTR_DONE
;
1002 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
1003 "SDTR_INITIATOR: fall back to async");
1004 } else if (target
->sync_flag
& SDTR_TARGET
) {
1006 * It was negotiating SDTR with target, sending from target,
1007 * but there are no chance to remove this flag. Set async
1008 * because we don't get proper negotiation.
1010 nsp32_set_async(data
, target
);
1011 target
->sync_flag
&= ~SDTR_TARGET
;
1012 target
->sync_flag
|= SDTR_DONE
;
1014 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
1015 "Unknown SDTR from target is reached, fall back to async.");
1018 nsp32_dbg(NSP32_DEBUG_TARGETFLAG
,
1019 "target: %d sync_flag: 0x%x syncreg: 0x%x ackwidth: 0x%x",
1020 SCpnt
->device
->id
, target
->sync_flag
, target
->syncreg
,
1024 if (auto_param
== 0) {
1025 ret
= nsp32_selection_autopara(SCpnt
);
1027 ret
= nsp32_selection_autoscsi(SCpnt
);
1031 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
, "selection fail");
1032 nsp32_scsi_done(SCpnt
);
1038 static DEF_SCSI_QCMD(nsp32_queuecommand
)
1040 /* initialize asic */
1041 static int nsp32hw_init(nsp32_hw_data
*data
)
1043 unsigned int base
= data
->BaseAddress
;
1044 unsigned short irq_stat
;
1045 unsigned long lc_reg
;
1046 unsigned char power
;
1048 lc_reg
= nsp32_index_read4(base
, CFG_LATE_CACHE
);
1049 if ((lc_reg
& 0xff00) == 0) {
1050 lc_reg
|= (0x20 << 8);
1051 nsp32_index_write2(base
, CFG_LATE_CACHE
, lc_reg
& 0xffff);
1054 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
1055 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
1056 nsp32_write4(base
, BM_CNT
, 0);
1057 nsp32_write2(base
, SCSI_EXECUTE_PHASE
, 0);
1060 irq_stat
= nsp32_read2(base
, IRQ_STATUS
);
1061 nsp32_dbg(NSP32_DEBUG_INIT
, "irq_stat 0x%x", irq_stat
);
1062 } while (irq_stat
& IRQSTATUS_ANY_IRQ
);
1065 * Fill FIFO_FULL_SHLD, FIFO_EMPTY_SHLD. Below parameter is
1066 * designated by specification.
1068 if ((data
->trans_method
& NSP32_TRANSFER_PIO
) ||
1069 (data
->trans_method
& NSP32_TRANSFER_MMIO
)) {
1070 nsp32_index_write1(base
, FIFO_FULL_SHLD_COUNT
, 0x40);
1071 nsp32_index_write1(base
, FIFO_EMPTY_SHLD_COUNT
, 0x40);
1072 } else if (data
->trans_method
& NSP32_TRANSFER_BUSMASTER
) {
1073 nsp32_index_write1(base
, FIFO_FULL_SHLD_COUNT
, 0x10);
1074 nsp32_index_write1(base
, FIFO_EMPTY_SHLD_COUNT
, 0x60);
1076 nsp32_dbg(NSP32_DEBUG_INIT
, "unknown transfer mode");
1079 nsp32_dbg(NSP32_DEBUG_INIT
, "full 0x%x emp 0x%x",
1080 nsp32_index_read1(base
, FIFO_FULL_SHLD_COUNT
),
1081 nsp32_index_read1(base
, FIFO_EMPTY_SHLD_COUNT
));
1083 nsp32_index_write1(base
, CLOCK_DIV
, data
->clock
);
1084 nsp32_index_write1(base
, BM_CYCLE
, MEMRD_CMD1
| SGT_AUTO_PARA_MEMED_CMD
);
1085 nsp32_write1(base
, PARITY_CONTROL
, 0); /* parity check is disable */
1088 * initialize MISC_WRRD register
1090 * Note: Designated parameters is obeyed as following:
1091 * MISC_SCSI_DIRECTION_DETECTOR_SELECT: It must be set.
1092 * MISC_MASTER_TERMINATION_SELECT: It must be set.
1093 * MISC_BMREQ_NEGATE_TIMING_SEL: It should be set.
1094 * MISC_AUTOSEL_TIMING_SEL: It should be set.
1095 * MISC_BMSTOP_CHANGE2_NONDATA_PHASE: It should be set.
1096 * MISC_DELAYED_BMSTART: It's selected for safety.
1098 * Note: If MISC_BMSTOP_CHANGE2_NONDATA_PHASE is set, then
1099 * we have to set TRANSFERCONTROL_BM_START as 0 and set
1100 * appropriate value before restarting bus master transfer.
1102 nsp32_index_write2(base
, MISC_WR
,
1103 (SCSI_DIRECTION_DETECTOR_SELECT
|
1105 MASTER_TERMINATION_SELECT
|
1106 BMREQ_NEGATE_TIMING_SEL
|
1107 AUTOSEL_TIMING_SEL
|
1108 BMSTOP_CHANGE2_NONDATA_PHASE
));
1110 nsp32_index_write1(base
, TERM_PWR_CONTROL
, 0);
1111 power
= nsp32_index_read1(base
, TERM_PWR_CONTROL
);
1112 if (!(power
& SENSE
)) {
1113 nsp32_msg(KERN_INFO
, "term power on");
1114 nsp32_index_write1(base
, TERM_PWR_CONTROL
, BPWR
);
1117 nsp32_write2(base
, TIMER_SET
, TIMER_STOP
);
1118 nsp32_write2(base
, TIMER_SET
, TIMER_STOP
); /* Required 2 times */
1120 nsp32_write1(base
, SYNC_REG
, 0);
1121 nsp32_write1(base
, ACK_WIDTH
, 0);
1122 nsp32_write2(base
, SEL_TIME_OUT
, SEL_TIMEOUT_TIME
);
1125 * enable to select designated IRQ (except for
1126 * IRQSELECT_SERR, IRQSELECT_PERR, IRQSELECT_BMCNTERR)
1128 nsp32_index_write2(base
, IRQ_SELECT
, IRQSELECT_TIMER_IRQ
|
1129 IRQSELECT_SCSIRESET_IRQ
|
1130 IRQSELECT_FIFO_SHLD_IRQ
|
1131 IRQSELECT_RESELECT_IRQ
|
1132 IRQSELECT_PHASE_CHANGE_IRQ
|
1133 IRQSELECT_AUTO_SCSI_SEQ_IRQ
|
1134 // IRQSELECT_BMCNTERR_IRQ |
1135 IRQSELECT_TARGET_ABORT_IRQ
|
1136 IRQSELECT_MASTER_ABORT_IRQ
);
1137 nsp32_write2(base
, IRQ_CONTROL
, 0);
1140 nsp32_index_write1(base
, EXT_PORT_DDR
, LED_OFF
);
1141 nsp32_index_write1(base
, EXT_PORT
, LED_OFF
);
1147 /* interrupt routine */
1148 static irqreturn_t
do_nsp32_isr(int irq
, void *dev_id
)
1150 nsp32_hw_data
*data
= dev_id
;
1151 unsigned int base
= data
->BaseAddress
;
1152 struct scsi_cmnd
*SCpnt
= data
->CurrentSC
;
1153 unsigned short auto_stat
, irq_stat
, trans_stat
;
1154 unsigned char busmon
, busphase
;
1155 unsigned long flags
;
1158 struct Scsi_Host
*host
= data
->Host
;
1160 spin_lock_irqsave(host
->host_lock
, flags
);
1163 * IRQ check, then enable IRQ mask
1165 irq_stat
= nsp32_read2(base
, IRQ_STATUS
);
1166 nsp32_dbg(NSP32_DEBUG_INTR
,
1167 "enter IRQ: %d, IRQstatus: 0x%x", irq
, irq_stat
);
1168 /* is this interrupt comes from Ninja asic? */
1169 if ((irq_stat
& IRQSTATUS_ANY_IRQ
) == 0) {
1170 nsp32_dbg(NSP32_DEBUG_INTR
, "shared interrupt: irq other 0x%x", irq_stat
);
1174 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
1176 busmon
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
1177 busphase
= busmon
& BUSMON_PHASE_MASK
;
1179 trans_stat
= nsp32_read2(base
, TRANSFER_STATUS
);
1180 if ((irq_stat
== 0xffff) && (trans_stat
== 0xffff)) {
1181 nsp32_msg(KERN_INFO
, "card disconnect");
1182 if (data
->CurrentSC
!= NULL
) {
1183 nsp32_msg(KERN_INFO
, "clean up current SCSI command");
1184 SCpnt
->result
= DID_BAD_TARGET
<< 16;
1185 nsp32_scsi_done(SCpnt
);
1191 if (irq_stat
& IRQSTATUS_TIMER_IRQ
) {
1192 nsp32_dbg(NSP32_DEBUG_INTR
, "timer stop");
1193 nsp32_write2(base
, TIMER_SET
, TIMER_STOP
);
1198 if (irq_stat
& IRQSTATUS_SCSIRESET_IRQ
) {
1199 nsp32_msg(KERN_INFO
, "detected someone do bus reset");
1200 nsp32_do_bus_reset(data
);
1201 if (SCpnt
!= NULL
) {
1202 SCpnt
->result
= DID_RESET
<< 16;
1203 nsp32_scsi_done(SCpnt
);
1208 if (SCpnt
== NULL
) {
1209 nsp32_msg(KERN_WARNING
, "SCpnt==NULL this can't be happened");
1210 nsp32_msg(KERN_WARNING
, "irq_stat=0x%x trans_stat=0x%x", irq_stat
, trans_stat
);
1215 * AutoSCSI Interrupt.
1216 * Note: This interrupt is occurred when AutoSCSI is finished. Then
1217 * check SCSIEXECUTEPHASE, and do appropriate action. Each phases are
1218 * recorded when AutoSCSI sequencer has been processed.
1220 if(irq_stat
& IRQSTATUS_AUTOSCSI_IRQ
) {
1221 /* getting SCSI executed phase */
1222 auto_stat
= nsp32_read2(base
, SCSI_EXECUTE_PHASE
);
1223 nsp32_write2(base
, SCSI_EXECUTE_PHASE
, 0);
1225 /* Selection Timeout, go busfree phase. */
1226 if (auto_stat
& SELECTION_TIMEOUT
) {
1227 nsp32_dbg(NSP32_DEBUG_INTR
,
1228 "selection timeout occurred");
1230 SCpnt
->result
= DID_TIME_OUT
<< 16;
1231 nsp32_scsi_done(SCpnt
);
1235 if (auto_stat
& MSGOUT_PHASE
) {
1237 * MsgOut phase was processed.
1238 * If MSG_IN_OCCUER is not set, then MsgOut phase is
1239 * completed. Thus, msgout_len must reset. Otherwise,
1240 * nothing to do here. If MSG_OUT_OCCUER is occurred,
1241 * then we will encounter the condition and check.
1243 if (!(auto_stat
& MSG_IN_OCCUER
) &&
1244 (data
->msgout_len
<= 3)) {
1246 * !MSG_IN_OCCUER && msgout_len <=3
1247 * ---> AutoSCSI with MSGOUTreg is processed.
1249 data
->msgout_len
= 0;
1252 nsp32_dbg(NSP32_DEBUG_INTR
, "MsgOut phase processed");
1255 if ((auto_stat
& DATA_IN_PHASE
) &&
1256 (scsi_get_resid(SCpnt
) > 0) &&
1257 ((nsp32_read2(base
, FIFO_REST_CNT
) & FIFO_REST_MASK
) != 0)) {
1258 printk( "auto+fifo\n");
1259 //nsp32_pio_read(SCpnt);
1262 if (auto_stat
& (DATA_IN_PHASE
| DATA_OUT_PHASE
)) {
1263 /* DATA_IN_PHASE/DATA_OUT_PHASE was processed. */
1264 nsp32_dbg(NSP32_DEBUG_INTR
,
1265 "Data in/out phase processed");
1267 /* read BMCNT, SGT pointer addr */
1268 nsp32_dbg(NSP32_DEBUG_INTR
, "BMCNT=0x%lx",
1269 nsp32_read4(base
, BM_CNT
));
1270 nsp32_dbg(NSP32_DEBUG_INTR
, "addr=0x%lx",
1271 nsp32_read4(base
, SGT_ADR
));
1272 nsp32_dbg(NSP32_DEBUG_INTR
, "SACK=0x%lx",
1273 nsp32_read4(base
, SACK_CNT
));
1274 nsp32_dbg(NSP32_DEBUG_INTR
, "SSACK=0x%lx",
1275 nsp32_read4(base
, SAVED_SACK_CNT
));
1277 scsi_set_resid(SCpnt
, 0); /* all data transferred! */
1283 if (auto_stat
& MSG_IN_OCCUER
) {
1284 nsp32_msgin_occur(SCpnt
, irq_stat
, auto_stat
);
1290 if (auto_stat
& MSG_OUT_OCCUER
) {
1291 nsp32_msgout_occur(SCpnt
);
1297 if (auto_stat
& BUS_FREE_OCCUER
) {
1298 ret
= nsp32_busfree_occur(SCpnt
, auto_stat
);
1304 if (auto_stat
& STATUS_PHASE
) {
1306 * Read CSB and substitute CSB for SCpnt->result
1307 * to save status phase stutas byte.
1308 * scsi error handler checks host_byte (DID_*:
1309 * low level driver to indicate status), then checks
1310 * status_byte (SCSI status byte).
1312 SCpnt
->result
= (int)nsp32_read1(base
, SCSI_CSB_IN
);
1315 if (auto_stat
& ILLEGAL_PHASE
) {
1316 /* Illegal phase is detected. SACK is not back. */
1317 nsp32_msg(KERN_WARNING
,
1318 "AUTO SCSI ILLEGAL PHASE OCCUR!!!!");
1320 /* TODO: currently we don't have any action... bus reset? */
1323 * To send back SACK, assert, wait, and negate.
1325 nsp32_sack_assert(data
);
1326 nsp32_wait_req(data
, NEGATE
);
1327 nsp32_sack_negate(data
);
1331 if (auto_stat
& COMMAND_PHASE
) {
1333 nsp32_dbg(NSP32_DEBUG_INTR
, "Command phase processed");
1336 if (auto_stat
& AUTOSCSI_BUSY
) {
1337 /* AutoSCSI is running */
1340 show_autophase(auto_stat
);
1344 if (irq_stat
& IRQSTATUS_FIFO_SHLD_IRQ
) {
1345 nsp32_dbg(NSP32_DEBUG_INTR
, "FIFO IRQ");
1348 case BUSPHASE_DATA_OUT
:
1349 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/write");
1351 //nsp32_pio_write(SCpnt);
1355 case BUSPHASE_DATA_IN
:
1356 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/read");
1358 //nsp32_pio_read(SCpnt);
1362 case BUSPHASE_STATUS
:
1363 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/status");
1365 SCpnt
->SCp
.Status
= nsp32_read1(base
, SCSI_CSB_IN
);
1369 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/other phase");
1370 nsp32_dbg(NSP32_DEBUG_INTR
, "irq_stat=0x%x trans_stat=0x%x", irq_stat
, trans_stat
);
1371 show_busphase(busphase
);
1378 /* Phase Change IRQ */
1379 if (irq_stat
& IRQSTATUS_PHASE_CHANGE_IRQ
) {
1380 nsp32_dbg(NSP32_DEBUG_INTR
, "phase change IRQ");
1383 case BUSPHASE_MESSAGE_IN
:
1384 nsp32_dbg(NSP32_DEBUG_INTR
, "phase chg/msg in");
1385 nsp32_msgin_occur(SCpnt
, irq_stat
, 0);
1388 nsp32_msg(KERN_WARNING
, "phase chg/other phase?");
1389 nsp32_msg(KERN_WARNING
, "irq_stat=0x%x trans_stat=0x%x\n",
1390 irq_stat
, trans_stat
);
1391 show_busphase(busphase
);
1398 if (irq_stat
& IRQSTATUS_PCI_IRQ
) {
1399 nsp32_dbg(NSP32_DEBUG_INTR
, "PCI IRQ occurred");
1404 if (irq_stat
& IRQSTATUS_BMCNTERR_IRQ
) {
1405 nsp32_msg(KERN_ERR
, "Received unexpected BMCNTERR IRQ! ");
1407 * TODO: To be implemented improving bus master
1408 * transfer reliability when BMCNTERR is occurred in
1409 * AutoSCSI phase described in specification.
1414 nsp32_dbg(NSP32_DEBUG_INTR
,
1415 "irq_stat=0x%x trans_stat=0x%x", irq_stat
, trans_stat
);
1416 show_busphase(busphase
);
1420 /* disable IRQ mask */
1421 nsp32_write2(base
, IRQ_CONTROL
, 0);
1424 spin_unlock_irqrestore(host
->host_lock
, flags
);
1426 nsp32_dbg(NSP32_DEBUG_INTR
, "exit");
1428 return IRQ_RETVAL(handled
);
1432 static int nsp32_show_info(struct seq_file
*m
, struct Scsi_Host
*host
)
1434 unsigned long flags
;
1435 nsp32_hw_data
*data
;
1438 unsigned char mode_reg
;
1442 hostno
= host
->host_no
;
1443 data
= (nsp32_hw_data
*)host
->hostdata
;
1444 base
= host
->io_port
;
1446 seq_puts(m
, "NinjaSCSI-32 status\n\n");
1447 seq_printf(m
, "Driver version: %s, $Revision: 1.33 $\n", nsp32_release_version
);
1448 seq_printf(m
, "SCSI host No.: %d\n", hostno
);
1449 seq_printf(m
, "IRQ: %d\n", host
->irq
);
1450 seq_printf(m
, "IO: 0x%lx-0x%lx\n", host
->io_port
, host
->io_port
+ host
->n_io_port
- 1);
1451 seq_printf(m
, "MMIO(virtual address): 0x%lx-0x%lx\n", host
->base
, host
->base
+ data
->MmioLength
- 1);
1452 seq_printf(m
, "sg_tablesize: %d\n", host
->sg_tablesize
);
1453 seq_printf(m
, "Chip revision: 0x%x\n", (nsp32_read2(base
, INDEX_REG
) >> 8) & 0xff);
1455 mode_reg
= nsp32_index_read1(base
, CHIP_MODE
);
1456 model
= data
->pci_devid
->driver_data
;
1459 seq_printf(m
, "Power Management: %s\n", (mode_reg
& OPTF
) ? "yes" : "no");
1461 seq_printf(m
, "OEM: %ld, %s\n", (mode_reg
& (OEM0
|OEM1
)), nsp32_model
[model
]);
1463 spin_lock_irqsave(&(data
->Lock
), flags
);
1464 seq_printf(m
, "CurrentSC: 0x%p\n\n", data
->CurrentSC
);
1465 spin_unlock_irqrestore(&(data
->Lock
), flags
);
1468 seq_puts(m
, "SDTR status\n");
1469 for (id
= 0; id
< ARRAY_SIZE(data
->target
); id
++) {
1471 seq_printf(m
, "id %d: ", id
);
1473 if (id
== host
->this_id
) {
1474 seq_puts(m
, "----- NinjaSCSI-32 host adapter\n");
1478 if (data
->target
[id
].sync_flag
== SDTR_DONE
) {
1479 if (data
->target
[id
].period
== 0 &&
1480 data
->target
[id
].offset
== ASYNC_OFFSET
) {
1481 seq_puts(m
, "async");
1483 seq_puts(m
, " sync");
1486 seq_puts(m
, " none");
1489 if (data
->target
[id
].period
!= 0) {
1491 speed
= 1000000 / (data
->target
[id
].period
* 4);
1493 seq_printf(m
, " transfer %d.%dMB/s, offset %d",
1496 data
->target
[id
].offset
1507 * Reset parameters and call scsi_done for data->cur_lunt.
1508 * Be careful setting SCpnt->result = DID_* before calling this function.
1510 static void nsp32_scsi_done(struct scsi_cmnd
*SCpnt
)
1512 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1513 unsigned int base
= SCpnt
->device
->host
->io_port
;
1515 scsi_dma_unmap(SCpnt
);
1518 * clear TRANSFERCONTROL_BM_START
1520 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
1521 nsp32_write4(base
, BM_CNT
, 0);
1526 (*SCpnt
->scsi_done
)(SCpnt
);
1531 data
->cur_lunt
->SCpnt
= NULL
;
1532 data
->cur_lunt
= NULL
;
1533 data
->cur_target
= NULL
;
1534 data
->CurrentSC
= NULL
;
1541 * Current Phase is BUSFREE. AutoSCSI is automatically execute BUSFREE phase
1542 * with ACK reply when below condition is matched:
1543 * MsgIn 00: Command Complete.
1544 * MsgIn 02: Save Data Pointer.
1545 * MsgIn 04: Diconnect.
1546 * In other case, unexpected BUSFREE is detected.
1548 static int nsp32_busfree_occur(struct scsi_cmnd
*SCpnt
, unsigned short execph
)
1550 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1551 unsigned int base
= SCpnt
->device
->host
->io_port
;
1553 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "enter execph=0x%x", execph
);
1554 show_autophase(execph
);
1556 nsp32_write4(base
, BM_CNT
, 0);
1557 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
1560 * MsgIn 02: Save Data Pointer
1563 * Save Data Pointer is received. Adjust pointer.
1566 * SCSI-3 says if Save Data Pointer is not received, then we restart
1567 * processing and we can't adjust any SCSI data pointer in next data
1570 if (execph
& MSGIN_02_VALID
) {
1571 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "MsgIn02_Valid");
1574 * Check sack_cnt/saved_sack_cnt, then adjust sg table if
1577 if (!(execph
& MSGIN_00_VALID
) &&
1578 ((execph
& DATA_IN_PHASE
) || (execph
& DATA_OUT_PHASE
))) {
1579 unsigned int sacklen
, s_sacklen
;
1582 * Read SACK count and SAVEDSACK count, then compare.
1584 sacklen
= nsp32_read4(base
, SACK_CNT
);
1585 s_sacklen
= nsp32_read4(base
, SAVED_SACK_CNT
);
1588 * If SAVEDSACKCNT == 0, it means SavedDataPointer is
1589 * come after data transferring.
1591 if (s_sacklen
> 0) {
1593 * Comparing between sack and savedsack to
1594 * check the condition of AutoMsgIn03.
1596 * If they are same, set msgin03 == TRUE,
1597 * COMMANDCONTROL_AUTO_MSGIN_03 is enabled at
1598 * reselection. On the other hand, if they
1599 * aren't same, set msgin03 == FALSE, and
1600 * COMMANDCONTROL_AUTO_MSGIN_03 is disabled at
1603 if (sacklen
!= s_sacklen
) {
1604 data
->cur_lunt
->msgin03
= FALSE
;
1606 data
->cur_lunt
->msgin03
= TRUE
;
1609 nsp32_adjust_busfree(SCpnt
, s_sacklen
);
1613 /* This value has not substitude with valid value yet... */
1614 //data->cur_lunt->save_datp = data->cur_datp;
1621 if (execph
& MSGIN_03_VALID
) {
1622 /* MsgIn03 was valid to be processed. No need processing. */
1628 if (data
->cur_target
->sync_flag
& SDTR_INITIATOR
) {
1630 * SDTR negotiation pulled by the initiator has not
1631 * finished yet. Fall back to ASYNC mode.
1633 nsp32_set_async(data
, data
->cur_target
);
1634 data
->cur_target
->sync_flag
&= ~SDTR_INITIATOR
;
1635 data
->cur_target
->sync_flag
|= SDTR_DONE
;
1636 } else if (data
->cur_target
->sync_flag
& SDTR_TARGET
) {
1638 * SDTR negotiation pulled by the target has been
1641 if (execph
& (MSGIN_00_VALID
| MSGIN_04_VALID
)) {
1643 * If valid message is received, then
1644 * negotiation is succeeded.
1648 * On the contrary, if unexpected bus free is
1649 * occurred, then negotiation is failed. Fall
1650 * back to ASYNC mode.
1652 nsp32_set_async(data
, data
->cur_target
);
1654 data
->cur_target
->sync_flag
&= ~SDTR_TARGET
;
1655 data
->cur_target
->sync_flag
|= SDTR_DONE
;
1659 * It is always ensured by SCSI standard that initiator
1660 * switches into Bus Free Phase after
1661 * receiving message 00 (Command Complete), 04 (Disconnect).
1662 * It's the reason that processing here is valid.
1664 if (execph
& MSGIN_00_VALID
) {
1665 /* MsgIn 00: Command Complete */
1666 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "command complete");
1668 SCpnt
->SCp
.Status
= nsp32_read1(base
, SCSI_CSB_IN
);
1669 SCpnt
->SCp
.Message
= 0;
1670 nsp32_dbg(NSP32_DEBUG_BUSFREE
,
1671 "normal end stat=0x%x resid=0x%x\n",
1672 SCpnt
->SCp
.Status
, scsi_get_resid(SCpnt
));
1673 SCpnt
->result
= (DID_OK
<< 16) |
1674 (SCpnt
->SCp
.Message
<< 8) |
1675 (SCpnt
->SCp
.Status
<< 0);
1676 nsp32_scsi_done(SCpnt
);
1677 /* All operation is done */
1679 } else if (execph
& MSGIN_04_VALID
) {
1680 /* MsgIn 04: Disconnect */
1681 SCpnt
->SCp
.Status
= nsp32_read1(base
, SCSI_CSB_IN
);
1682 SCpnt
->SCp
.Message
= 4;
1684 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "disconnect");
1687 /* Unexpected bus free */
1688 nsp32_msg(KERN_WARNING
, "unexpected bus free occurred");
1691 //SCpnt->result = (DID_OK << 16) | (SCpnt->SCp.Message << 8) | (SCpnt->SCp.Status << 0);
1692 SCpnt
->result
= DID_ERROR
<< 16;
1693 nsp32_scsi_done(SCpnt
);
1701 * nsp32_adjust_busfree - adjusting SG table
1703 * Note: This driver adjust the SG table using SCSI ACK
1704 * counter instead of BMCNT counter!
1706 static void nsp32_adjust_busfree(struct scsi_cmnd
*SCpnt
, unsigned int s_sacklen
)
1708 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1709 int old_entry
= data
->cur_entry
;
1711 int sg_num
= data
->cur_lunt
->sg_num
;
1712 nsp32_sgtable
*sgt
= data
->cur_lunt
->sglun
->sgt
;
1713 unsigned int restlen
, sentlen
;
1716 nsp32_dbg(NSP32_DEBUG_SGLIST
, "old resid=0x%x", scsi_get_resid(SCpnt
));
1718 /* adjust saved SACK count with 4 byte start address boundary */
1719 s_sacklen
-= le32_to_cpu(sgt
[old_entry
].addr
) & 3;
1722 * calculate new_entry from sack count and each sgt[].len
1723 * calculate the byte which is intent to send
1726 for (new_entry
= old_entry
; new_entry
< sg_num
; new_entry
++) {
1727 sentlen
+= (le32_to_cpu(sgt
[new_entry
].len
) & ~SGTEND
);
1728 if (sentlen
> s_sacklen
) {
1733 /* all sgt is processed */
1734 if (new_entry
== sg_num
) {
1738 if (sentlen
== s_sacklen
) {
1739 /* XXX: confirm it's ok or not */
1740 /* In this case, it's ok because we are at
1741 the head element of the sg. restlen is correctly calculated. */
1744 /* calculate the rest length for transferring */
1745 restlen
= sentlen
- s_sacklen
;
1747 /* update adjusting current SG table entry */
1748 len
= le32_to_cpu(sgt
[new_entry
].len
);
1749 addr
= le32_to_cpu(sgt
[new_entry
].addr
);
1750 addr
+= (len
- restlen
);
1751 sgt
[new_entry
].addr
= cpu_to_le32(addr
);
1752 sgt
[new_entry
].len
= cpu_to_le32(restlen
);
1754 /* set cur_entry with new_entry */
1755 data
->cur_entry
= new_entry
;
1760 if (scsi_get_resid(SCpnt
) < sentlen
) {
1761 nsp32_msg(KERN_ERR
, "resid underflow");
1764 scsi_set_resid(SCpnt
, scsi_get_resid(SCpnt
) - sentlen
);
1765 nsp32_dbg(NSP32_DEBUG_SGLIST
, "new resid=0x%x", scsi_get_resid(SCpnt
));
1767 /* update hostdata and lun */
1774 * It's called MsgOut phase occur.
1775 * NinjaSCSI-32Bi/UDE automatically processes up to 3 messages in
1776 * message out phase. It, however, has more than 3 messages,
1777 * HBA creates the interrupt and we have to process by hand.
1779 static void nsp32_msgout_occur(struct scsi_cmnd
*SCpnt
)
1781 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1782 unsigned int base
= SCpnt
->device
->host
->io_port
;
1783 //unsigned short command;
1787 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
,
1788 "enter: msgout_len: 0x%x", data
->msgout_len
);
1791 * If MsgOut phase is occurred without having any
1792 * message, then No_Operation is sent (SCSI-2).
1794 if (data
->msgout_len
== 0) {
1795 nsp32_build_nop(SCpnt
);
1799 * Set SGTP ADDR current entry for restarting AUTOSCSI,
1800 * because SGTP is incremented next point.
1801 * There is few statement in the specification...
1803 new_sgtp
= data
->cur_lunt
->sglun_paddr
+
1804 (data
->cur_lunt
->cur_entry
* sizeof(nsp32_sgtable
));
1809 for (i
= 0; i
< data
->msgout_len
; i
++) {
1810 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
,
1811 "%d : 0x%x", i
, data
->msgoutbuf
[i
]);
1814 * Check REQ is asserted.
1816 nsp32_wait_req(data
, ASSERT
);
1818 if (i
== (data
->msgout_len
- 1)) {
1820 * If the last message, set the AutoSCSI restart
1821 * before send back the ack message. AutoSCSI
1822 * restart automatically negate ATN signal.
1824 //command = (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
1825 //nsp32_restart_autoscsi(SCpnt, command);
1826 nsp32_write2(base
, COMMAND_CONTROL
,
1827 (CLEAR_CDB_FIFO_POINTER
|
1828 AUTO_COMMAND_PHASE
|
1830 AUTO_MSGIN_00_OR_04
|
1834 * Write data with SACK, then wait sack is
1835 * automatically negated.
1837 nsp32_write1(base
, SCSI_DATA_WITH_ACK
, data
->msgoutbuf
[i
]);
1838 nsp32_wait_sack(data
, NEGATE
);
1840 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
, "bus: 0x%x\n",
1841 nsp32_read1(base
, SCSI_BUS_MONITOR
));
1844 data
->msgout_len
= 0;
1846 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
, "exit");
1852 * Note: Restarting AutoSCSI needs set:
1853 * SYNC_REG, ACK_WIDTH, SGT_ADR, TRANSFER_CONTROL
1855 static void nsp32_restart_autoscsi(struct scsi_cmnd
*SCpnt
, unsigned short command
)
1857 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1858 unsigned int base
= data
->BaseAddress
;
1859 unsigned short transfer
= 0;
1861 nsp32_dbg(NSP32_DEBUG_RESTART
, "enter");
1863 if (data
->cur_target
== NULL
|| data
->cur_lunt
== NULL
) {
1864 nsp32_msg(KERN_ERR
, "Target or Lun is invalid");
1869 * Don't set BM_START_ADR before setting this register.
1871 nsp32_write1(base
, SYNC_REG
, data
->cur_target
->syncreg
);
1876 nsp32_write1(base
, ACK_WIDTH
, data
->cur_target
->ackwidth
);
1879 * set SREQ hazard killer sampling rate
1881 nsp32_write1(base
, SREQ_SMPL_RATE
, data
->cur_target
->sample_reg
);
1884 * set SGT ADDR (physical address)
1886 nsp32_write4(base
, SGT_ADR
, data
->cur_lunt
->sglun_paddr
);
1889 * set TRANSFER CONTROL REG
1892 transfer
|= (TRANSFER_GO
| ALL_COUNTER_CLR
);
1893 if (data
->trans_method
& NSP32_TRANSFER_BUSMASTER
) {
1894 if (scsi_bufflen(SCpnt
) > 0) {
1895 transfer
|= BM_START
;
1897 } else if (data
->trans_method
& NSP32_TRANSFER_MMIO
) {
1898 transfer
|= CB_MMIO_MODE
;
1899 } else if (data
->trans_method
& NSP32_TRANSFER_PIO
) {
1900 transfer
|= CB_IO_MODE
;
1902 nsp32_write2(base
, TRANSFER_CONTROL
, transfer
);
1907 * TODO: COMMANDCONTROL_AUTO_COMMAND_PHASE is needed ?
1909 command
|= (CLEAR_CDB_FIFO_POINTER
|
1910 AUTO_COMMAND_PHASE
|
1912 nsp32_write2(base
, COMMAND_CONTROL
, command
);
1914 nsp32_dbg(NSP32_DEBUG_RESTART
, "exit");
1919 * cannot run automatically message in occur
1921 static void nsp32_msgin_occur(struct scsi_cmnd
*SCpnt
,
1922 unsigned long irq_status
,
1923 unsigned short execph
)
1925 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1926 unsigned int base
= SCpnt
->device
->host
->io_port
;
1928 unsigned char msgtype
;
1929 unsigned char newlun
;
1930 unsigned short command
= 0;
1931 int msgclear
= TRUE
;
1936 * read first message
1937 * Use SCSIDATA_W_ACK instead of SCSIDATAIN, because the procedure
1938 * of Message-In have to be processed before sending back SCSI ACK.
1940 msg
= nsp32_read1(base
, SCSI_DATA_IN
);
1941 data
->msginbuf
[(unsigned char)data
->msgin_len
] = msg
;
1942 msgtype
= data
->msginbuf
[0];
1943 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
,
1944 "enter: msglen: 0x%x msgin: 0x%x msgtype: 0x%x",
1945 data
->msgin_len
, msg
, msgtype
);
1948 * TODO: We need checking whether bus phase is message in?
1954 nsp32_sack_assert(data
);
1957 * processing IDENTIFY
1959 if (msgtype
& 0x80) {
1960 if (!(irq_status
& IRQSTATUS_RESELECT_OCCUER
)) {
1961 /* Invalid (non reselect) phase */
1965 newlun
= msgtype
& 0x1f; /* TODO: SPI-3 compliant? */
1966 ret
= nsp32_reselection(SCpnt
, newlun
);
1975 * processing messages except for IDENTIFY
1977 * TODO: Messages are all SCSI-2 terminology. SCSI-3 compliance is TODO.
1983 case COMMAND_COMPLETE
:
1986 * These messages should not be occurred.
1987 * They should be processed on AutoSCSI sequencer.
1989 nsp32_msg(KERN_WARNING
,
1990 "unexpected message of AutoSCSI MsgIn: 0x%x", msg
);
1993 case RESTORE_POINTERS
:
1995 * AutoMsgIn03 is disabled, and HBA gets this message.
1998 if ((execph
& DATA_IN_PHASE
) || (execph
& DATA_OUT_PHASE
)) {
1999 unsigned int s_sacklen
;
2001 s_sacklen
= nsp32_read4(base
, SAVED_SACK_CNT
);
2002 if ((execph
& MSGIN_02_VALID
) && (s_sacklen
> 0)) {
2003 nsp32_adjust_busfree(SCpnt
, s_sacklen
);
2005 /* No need to rewrite SGT */
2008 data
->cur_lunt
->msgin03
= FALSE
;
2010 /* Update with the new value */
2012 /* reset SACK/SavedACK counter (or ALL clear?) */
2013 nsp32_write4(base
, CLR_COUNTER
, CLRCOUNTER_ALLMASK
);
2016 * set new sg pointer
2018 new_sgtp
= data
->cur_lunt
->sglun_paddr
+
2019 (data
->cur_lunt
->cur_entry
* sizeof(nsp32_sgtable
));
2020 nsp32_write4(base
, SGT_ADR
, new_sgtp
);
2026 * These messages should not be occurred.
2027 * They should be processed on AutoSCSI sequencer.
2029 nsp32_msg (KERN_WARNING
,
2030 "unexpected message of AutoSCSI MsgIn: SAVE_POINTERS");
2034 case MESSAGE_REJECT
:
2035 /* If previous message_out is sending SDTR, and get
2036 message_reject from target, SDTR negotiation is failed */
2037 if (data
->cur_target
->sync_flag
&
2038 (SDTR_INITIATOR
| SDTR_TARGET
)) {
2040 * Current target is negotiating SDTR, but it's
2041 * failed. Fall back to async transfer mode, and set
2044 nsp32_set_async(data
, data
->cur_target
);
2045 data
->cur_target
->sync_flag
&= ~SDTR_INITIATOR
;
2046 data
->cur_target
->sync_flag
|= SDTR_DONE
;
2051 case LINKED_CMD_COMPLETE
:
2052 case LINKED_FLG_CMD_COMPLETE
:
2053 /* queue tag is not supported currently */
2054 nsp32_msg (KERN_WARNING
,
2055 "unsupported message: 0x%x", msgtype
);
2058 case INITIATE_RECOVERY
:
2059 /* staring ECA (Extended Contingent Allegiance) state. */
2060 /* This message is declined in SPI2 or later. */
2067 case SIMPLE_QUEUE_TAG
:
2070 * 0x23: Ignore_Wide_Residue is not declared in scsi.h.
2071 * No support is needed.
2073 if (data
->msgin_len
>= 1) {
2077 /* current position is 1-byte of 2 byte */
2085 case EXTENDED_MESSAGE
:
2086 if (data
->msgin_len
< 1) {
2088 * Current position does not reach 2-byte
2089 * (2-byte is extended message length).
2095 if ((data
->msginbuf
[1] + 1) > data
->msgin_len
) {
2097 * Current extended message has msginbuf[1] + 2
2098 * (msgin_len starts counting from 0, so buf[1] + 1).
2099 * If current message position is not finished,
2100 * continue receiving message.
2107 * Reach here means regular length of each type of
2108 * extended messages.
2110 switch (data
->msginbuf
[2]) {
2111 case EXTENDED_MODIFY_DATA_POINTER
:
2113 goto reject
; /* not implemented yet */
2118 * Exchange this message between initiator and target.
2120 if (data
->msgin_len
!= EXTENDED_SDTR_LEN
+ 1) {
2122 * received inappropriate message.
2128 nsp32_analyze_sdtr(SCpnt
);
2132 case EXTENDED_EXTENDED_IDENTIFY
:
2133 /* SCSI-I only, not supported. */
2134 goto reject
; /* not implemented yet */
2139 goto reject
; /* not implemented yet */
2153 if (msgclear
== TRUE
) {
2154 data
->msgin_len
= 0;
2157 * If restarting AutoSCSI, but there are some message to out
2158 * (msgout_len > 0), set AutoATN, and set SCSIMSGOUT as 0
2159 * (MV_VALID = 0). When commandcontrol is written with
2160 * AutoSCSI restart, at the same time MsgOutOccur should be
2161 * happened (however, such situation is really possible...?).
2163 if (data
->msgout_len
> 0) {
2164 nsp32_write4(base
, SCSI_MSG_OUT
, 0);
2165 command
|= AUTO_ATN
;
2170 * If it's failed, COMMANDCONTROL_AUTO_COMMAND_PHASE is needed.
2172 command
|= (AUTO_MSGIN_00_OR_04
| AUTO_MSGIN_02
);
2175 * If current msgin03 is TRUE, then flag on.
2177 if (data
->cur_lunt
->msgin03
== TRUE
) {
2178 command
|= AUTO_MSGIN_03
;
2180 data
->cur_lunt
->msgin03
= FALSE
;
2188 nsp32_restart_autoscsi(SCpnt
, command
);
2191 * wait SCSI REQ negate for REQ-ACK handshake
2193 nsp32_wait_req(data
, NEGATE
);
2198 nsp32_sack_negate(data
);
2200 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "exit");
2205 nsp32_msg(KERN_WARNING
,
2206 "invalid or unsupported MessageIn, rejected. "
2207 "current msg: 0x%x (len: 0x%x), processing msg: 0x%x",
2208 msg
, data
->msgin_len
, msgtype
);
2209 nsp32_build_reject(SCpnt
);
2210 data
->msgin_len
= 0;
2218 static void nsp32_analyze_sdtr(struct scsi_cmnd
*SCpnt
)
2220 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
2221 nsp32_target
*target
= data
->cur_target
;
2222 nsp32_sync_table
*synct
;
2223 unsigned char get_period
= data
->msginbuf
[3];
2224 unsigned char get_offset
= data
->msginbuf
[4];
2228 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "enter");
2230 synct
= data
->synct
;
2231 syncnum
= data
->syncnum
;
2234 * If this inititor sent the SDTR message, then target responds SDTR,
2235 * initiator SYNCREG, ACKWIDTH from SDTR parameter.
2236 * Messages are not appropriate, then send back reject message.
2237 * If initiator did not send the SDTR, but target sends SDTR,
2238 * initiator calculator the appropriate parameter and send back SDTR.
2240 if (target
->sync_flag
& SDTR_INITIATOR
) {
2242 * Initiator sent SDTR, the target responds and
2243 * send back negotiation SDTR.
2245 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "target responds SDTR");
2247 target
->sync_flag
&= ~SDTR_INITIATOR
;
2248 target
->sync_flag
|= SDTR_DONE
;
2253 if (get_offset
> SYNC_OFFSET
) {
2255 * Negotiation is failed, the target send back
2256 * unexpected offset value.
2261 if (get_offset
== ASYNC_OFFSET
) {
2263 * Negotiation is succeeded, the target want
2264 * to fall back into asynchronous transfer mode.
2271 * Check whether sync period is too short. If too short,
2272 * fall back to async mode. If it's ok, then investigate
2273 * the received sync period. If sync period is acceptable
2274 * between sync table start_period and end_period, then
2275 * set this I_T nexus as sent offset and period.
2276 * If it's not acceptable, send back reject and fall back
2279 if (get_period
< data
->synct
[0].period_num
) {
2281 * Negotiation is failed, the target send back
2282 * unexpected period value.
2287 entry
= nsp32_search_period_entry(data
, target
, get_period
);
2291 * Target want to use long period which is not
2292 * acceptable NinjaSCSI-32Bi/UDE.
2298 * Set new sync table and offset in this I_T nexus.
2300 nsp32_set_sync_entry(data
, target
, entry
, get_offset
);
2302 /* Target send SDTR to initiator. */
2303 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "target send SDTR");
2305 target
->sync_flag
|= SDTR_INITIATOR
;
2308 if (get_offset
> SYNC_OFFSET
) {
2309 /* send back as SYNC_OFFSET */
2310 get_offset
= SYNC_OFFSET
;
2314 if (get_period
< data
->synct
[0].period_num
) {
2315 get_period
= data
->synct
[0].period_num
;
2318 entry
= nsp32_search_period_entry(data
, target
, get_period
);
2320 if (get_offset
== ASYNC_OFFSET
|| entry
< 0) {
2321 nsp32_set_async(data
, target
);
2322 nsp32_build_sdtr(SCpnt
, 0, ASYNC_OFFSET
);
2324 nsp32_set_sync_entry(data
, target
, entry
, get_offset
);
2325 nsp32_build_sdtr(SCpnt
, get_period
, get_offset
);
2329 target
->period
= get_period
;
2330 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "exit");
2335 * If the current message is unacceptable, send back to the target
2336 * with reject message.
2338 nsp32_build_reject(SCpnt
);
2341 nsp32_set_async(data
, target
); /* set as ASYNC transfer mode */
2344 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "exit: set async");
2350 * Search config entry number matched in sync_table from given
2351 * target and speed period value. If failed to search, return negative value.
2353 static int nsp32_search_period_entry(nsp32_hw_data
*data
,
2354 nsp32_target
*target
,
2355 unsigned char period
)
2359 if (target
->limit_entry
>= data
->syncnum
) {
2360 nsp32_msg(KERN_ERR
, "limit_entry exceeds syncnum!");
2361 target
->limit_entry
= 0;
2364 for (i
= target
->limit_entry
; i
< data
->syncnum
; i
++) {
2365 if (period
>= data
->synct
[i
].start_period
&&
2366 period
<= data
->synct
[i
].end_period
) {
2372 * Check given period value is over the sync_table value.
2373 * If so, return max value.
2375 if (i
== data
->syncnum
) {
2384 * target <-> initiator use ASYNC transfer
2386 static void nsp32_set_async(nsp32_hw_data
*data
, nsp32_target
*target
)
2388 unsigned char period
= data
->synct
[target
->limit_entry
].period_num
;
2390 target
->offset
= ASYNC_OFFSET
;
2392 target
->syncreg
= TO_SYNCREG(period
, ASYNC_OFFSET
);
2393 target
->ackwidth
= 0;
2394 target
->sample_reg
= 0;
2396 nsp32_dbg(NSP32_DEBUG_SYNC
, "set async");
2401 * target <-> initiator use maximum SYNC transfer
2403 static void nsp32_set_max_sync(nsp32_hw_data
*data
,
2404 nsp32_target
*target
,
2405 unsigned char *period
,
2406 unsigned char *offset
)
2408 unsigned char period_num
, ackwidth
;
2410 period_num
= data
->synct
[target
->limit_entry
].period_num
;
2411 *period
= data
->synct
[target
->limit_entry
].start_period
;
2412 ackwidth
= data
->synct
[target
->limit_entry
].ackwidth
;
2413 *offset
= SYNC_OFFSET
;
2415 target
->syncreg
= TO_SYNCREG(period_num
, *offset
);
2416 target
->ackwidth
= ackwidth
;
2417 target
->offset
= *offset
;
2418 target
->sample_reg
= 0; /* disable SREQ sampling */
2423 * target <-> initiator use entry number speed
2425 static void nsp32_set_sync_entry(nsp32_hw_data
*data
,
2426 nsp32_target
*target
,
2428 unsigned char offset
)
2430 unsigned char period
, ackwidth
, sample_rate
;
2432 period
= data
->synct
[entry
].period_num
;
2433 ackwidth
= data
->synct
[entry
].ackwidth
;
2434 sample_rate
= data
->synct
[entry
].sample_rate
;
2436 target
->syncreg
= TO_SYNCREG(period
, offset
);
2437 target
->ackwidth
= ackwidth
;
2438 target
->offset
= offset
;
2439 target
->sample_reg
= sample_rate
| SAMPLING_ENABLE
;
2441 nsp32_dbg(NSP32_DEBUG_SYNC
, "set sync");
2446 * It waits until SCSI REQ becomes assertion or negation state.
2448 * Note: If nsp32_msgin_occur is called, we asserts SCSI ACK. Then
2449 * connected target responds SCSI REQ negation. We have to wait
2450 * SCSI REQ becomes negation in order to negate SCSI ACK signal for
2451 * REQ-ACK handshake.
2453 static void nsp32_wait_req(nsp32_hw_data
*data
, int state
)
2455 unsigned int base
= data
->BaseAddress
;
2457 unsigned char bus
, req_bit
;
2459 if (!((state
== ASSERT
) || (state
== NEGATE
))) {
2460 nsp32_msg(KERN_ERR
, "unknown state designation");
2463 req_bit
= (state
== ASSERT
? BUSMON_REQ
: 0);
2466 bus
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
2467 if ((bus
& BUSMON_REQ
) == req_bit
) {
2468 nsp32_dbg(NSP32_DEBUG_WAIT
,
2469 "wait_time: %d", wait_time
);
2474 } while (wait_time
< REQSACK_TIMEOUT_TIME
);
2476 nsp32_msg(KERN_WARNING
, "wait REQ timeout, req_bit: 0x%x", req_bit
);
2480 * It waits until SCSI SACK becomes assertion or negation state.
2482 static void nsp32_wait_sack(nsp32_hw_data
*data
, int state
)
2484 unsigned int base
= data
->BaseAddress
;
2486 unsigned char bus
, ack_bit
;
2488 if (!((state
== ASSERT
) || (state
== NEGATE
))) {
2489 nsp32_msg(KERN_ERR
, "unknown state designation");
2492 ack_bit
= (state
== ASSERT
? BUSMON_ACK
: 0);
2495 bus
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
2496 if ((bus
& BUSMON_ACK
) == ack_bit
) {
2497 nsp32_dbg(NSP32_DEBUG_WAIT
,
2498 "wait_time: %d", wait_time
);
2503 } while (wait_time
< REQSACK_TIMEOUT_TIME
);
2505 nsp32_msg(KERN_WARNING
, "wait SACK timeout, ack_bit: 0x%x", ack_bit
);
2511 * Note: SCSI ACK assertion needs with ACKENB=1, AUTODIRECTION=1.
2513 static void nsp32_sack_assert(nsp32_hw_data
*data
)
2515 unsigned int base
= data
->BaseAddress
;
2516 unsigned char busctrl
;
2518 busctrl
= nsp32_read1(base
, SCSI_BUS_CONTROL
);
2519 busctrl
|= (BUSCTL_ACK
| AUTODIRECTION
| ACKENB
);
2520 nsp32_write1(base
, SCSI_BUS_CONTROL
, busctrl
);
2526 static void nsp32_sack_negate(nsp32_hw_data
*data
)
2528 unsigned int base
= data
->BaseAddress
;
2529 unsigned char busctrl
;
2531 busctrl
= nsp32_read1(base
, SCSI_BUS_CONTROL
);
2532 busctrl
&= ~BUSCTL_ACK
;
2533 nsp32_write1(base
, SCSI_BUS_CONTROL
, busctrl
);
2539 * Note: n_io_port is defined as 0x7f because I/O register port is
2541 * 0x800-0x8ff: memory mapped I/O port
2542 * 0x900-0xbff: (map same 0x800-0x8ff I/O port image repeatedly)
2543 * 0xc00-0xfff: CardBus status registers
2545 static int nsp32_detect(struct pci_dev
*pdev
)
2547 struct Scsi_Host
*host
; /* registered host structure */
2548 struct resource
*res
;
2549 nsp32_hw_data
*data
;
2553 nsp32_dbg(NSP32_DEBUG_REGISTER
, "enter");
2556 * register this HBA as SCSI device
2558 host
= scsi_host_alloc(&nsp32_template
, sizeof(nsp32_hw_data
));
2560 nsp32_msg (KERN_ERR
, "failed to scsi register");
2567 data
= (nsp32_hw_data
*)host
->hostdata
;
2569 memcpy(data
, &nsp32_data_base
, sizeof(nsp32_hw_data
));
2571 host
->irq
= data
->IrqNumber
;
2572 host
->io_port
= data
->BaseAddress
;
2573 host
->unique_id
= data
->BaseAddress
;
2574 host
->n_io_port
= data
->NumAddress
;
2575 host
->base
= (unsigned long)data
->MmioAddress
;
2578 spin_lock_init(&(data
->Lock
));
2580 data
->cur_lunt
= NULL
;
2581 data
->cur_target
= NULL
;
2584 * Bus master transfer mode is supported currently.
2586 data
->trans_method
= NSP32_TRANSFER_BUSMASTER
;
2589 * Set clock div, CLOCK_4 (HBA has own external clock, and
2590 * dividing * 100ns/4).
2591 * Currently CLOCK_4 has only tested, not for CLOCK_2/PCICLK yet.
2593 data
->clock
= CLOCK_4
;
2596 * Select appropriate nsp32_sync_table and set I_CLOCKDIV.
2598 switch (data
->clock
) {
2600 /* If data->clock is CLOCK_4, then select 40M sync table. */
2601 data
->synct
= nsp32_sync_table_40M
;
2602 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_40M
);
2605 /* If data->clock is CLOCK_2, then select 20M sync table. */
2606 data
->synct
= nsp32_sync_table_20M
;
2607 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_20M
);
2610 /* If data->clock is PCICLK, then select pci sync table. */
2611 data
->synct
= nsp32_sync_table_pci
;
2612 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_pci
);
2615 nsp32_msg(KERN_WARNING
,
2616 "Invalid clock div is selected, set CLOCK_4.");
2617 /* Use default value CLOCK_4 */
2618 data
->clock
= CLOCK_4
;
2619 data
->synct
= nsp32_sync_table_40M
;
2620 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_40M
);
2630 if (dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32)) != 0) {
2631 nsp32_msg (KERN_ERR
, "failed to set PCI DMA mask");
2632 goto scsi_unregister
;
2636 * allocate autoparam DMA resource.
2638 data
->autoparam
= dma_alloc_coherent(&pdev
->dev
,
2639 sizeof(nsp32_autoparam
), &(data
->auto_paddr
),
2641 if (data
->autoparam
== NULL
) {
2642 nsp32_msg(KERN_ERR
, "failed to allocate DMA memory");
2643 goto scsi_unregister
;
2647 * allocate scatter-gather DMA resource.
2649 data
->sg_list
= dma_alloc_coherent(&pdev
->dev
, NSP32_SG_TABLE_SIZE
,
2650 &data
->sg_paddr
, GFP_KERNEL
);
2651 if (data
->sg_list
== NULL
) {
2652 nsp32_msg(KERN_ERR
, "failed to allocate DMA memory");
2653 goto free_autoparam
;
2656 for (i
= 0; i
< ARRAY_SIZE(data
->lunt
); i
++) {
2657 for (j
= 0; j
< ARRAY_SIZE(data
->lunt
[0]); j
++) {
2658 int offset
= i
* ARRAY_SIZE(data
->lunt
[0]) + j
;
2665 .sglun
= &(data
->sg_list
[offset
]),
2666 .sglun_paddr
= data
->sg_paddr
+ (offset
* sizeof(nsp32_sglun
)),
2669 data
->lunt
[i
][j
] = tmp
;
2676 for (i
= 0; i
< ARRAY_SIZE(data
->target
); i
++) {
2677 nsp32_target
*target
= &(data
->target
[i
]);
2679 target
->limit_entry
= 0;
2680 target
->sync_flag
= 0;
2681 nsp32_set_async(data
, target
);
2687 ret
= nsp32_getprom_param(data
);
2689 data
->resettime
= 3; /* default 3 */
2697 snprintf(data
->info_str
, sizeof(data
->info_str
),
2698 "NinjaSCSI-32Bi/UDE: irq %d, io 0x%lx+0x%x",
2699 host
->irq
, host
->io_port
, host
->n_io_port
);
2704 * Note: It's important to reset SCSI bus in initialization phase.
2705 * NinjaSCSI-32Bi/UDE HBA EEPROM seems to exchange SDTR when
2706 * system is coming up, so SCSI devices connected to HBA is set as
2707 * un-asynchronous mode. It brings the merit that this HBA is
2708 * ready to start synchronous transfer without any preparation,
2709 * but we are difficult to control transfer speed. In addition,
2710 * it prevents device transfer speed from effecting EEPROM start-up
2711 * SDTR. NinjaSCSI-32Bi/UDE has the feature if EEPROM is set as
2712 * Auto Mode, then FAST-10M is selected when SCSI devices are
2713 * connected same or more than 4 devices. It should be avoided
2714 * depending on this specification. Thus, resetting the SCSI bus
2715 * restores all connected SCSI devices to asynchronous mode, then
2716 * this driver set SDTR safely later, and we can control all SCSI
2717 * device transfer mode.
2719 nsp32_do_bus_reset(data
);
2721 ret
= request_irq(host
->irq
, do_nsp32_isr
, IRQF_SHARED
, "nsp32", data
);
2723 nsp32_msg(KERN_ERR
, "Unable to allocate IRQ for NinjaSCSI32 "
2724 "SCSI PCI controller. Interrupt: %d", host
->irq
);
2731 res
= request_region(host
->io_port
, host
->n_io_port
, "nsp32");
2734 "I/O region 0x%lx+0x%lx is already used",
2735 data
->BaseAddress
, data
->NumAddress
);
2739 ret
= scsi_add_host(host
, &pdev
->dev
);
2741 nsp32_msg(KERN_ERR
, "failed to add scsi host");
2744 scsi_scan_host(host
);
2745 pci_set_drvdata(pdev
, host
);
2749 release_region(host
->io_port
, host
->n_io_port
);
2752 free_irq(host
->irq
, data
);
2755 dma_free_coherent(&pdev
->dev
, NSP32_SG_TABLE_SIZE
,
2756 data
->sg_list
, data
->sg_paddr
);
2759 dma_free_coherent(&pdev
->dev
, sizeof(nsp32_autoparam
),
2760 data
->autoparam
, data
->auto_paddr
);
2763 scsi_host_put(host
);
2769 static int nsp32_release(struct Scsi_Host
*host
)
2771 nsp32_hw_data
*data
= (nsp32_hw_data
*)host
->hostdata
;
2773 if (data
->autoparam
) {
2774 dma_free_coherent(&data
->Pci
->dev
, sizeof(nsp32_autoparam
),
2775 data
->autoparam
, data
->auto_paddr
);
2778 if (data
->sg_list
) {
2779 dma_free_coherent(&data
->Pci
->dev
, NSP32_SG_TABLE_SIZE
,
2780 data
->sg_list
, data
->sg_paddr
);
2784 free_irq(host
->irq
, data
);
2787 if (host
->io_port
&& host
->n_io_port
) {
2788 release_region(host
->io_port
, host
->n_io_port
);
2791 if (data
->MmioAddress
) {
2792 iounmap(data
->MmioAddress
);
2798 static const char *nsp32_info(struct Scsi_Host
*shpnt
)
2800 nsp32_hw_data
*data
= (nsp32_hw_data
*)shpnt
->hostdata
;
2802 return data
->info_str
;
2806 /****************************************************************************
2809 static int nsp32_eh_abort(struct scsi_cmnd
*SCpnt
)
2811 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
2812 unsigned int base
= SCpnt
->device
->host
->io_port
;
2814 nsp32_msg(KERN_WARNING
, "abort");
2816 if (data
->cur_lunt
->SCpnt
== NULL
) {
2817 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "abort failed");
2821 if (data
->cur_target
->sync_flag
& (SDTR_INITIATOR
| SDTR_TARGET
)) {
2822 /* reset SDTR negotiation */
2823 data
->cur_target
->sync_flag
= 0;
2824 nsp32_set_async(data
, data
->cur_target
);
2827 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
2828 nsp32_write2(base
, BM_CNT
, 0);
2830 SCpnt
->result
= DID_ABORT
<< 16;
2831 nsp32_scsi_done(SCpnt
);
2833 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "abort success");
2837 static void nsp32_do_bus_reset(nsp32_hw_data
*data
)
2839 unsigned int base
= data
->BaseAddress
;
2840 unsigned short intrdat
;
2843 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "in");
2847 * clear TRANSFERCONTROL_BM_START
2850 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
2851 nsp32_write4(base
, BM_CNT
, 0);
2852 nsp32_write4(base
, CLR_COUNTER
, CLRCOUNTER_ALLMASK
);
2855 * fall back to asynchronous transfer mode
2856 * initialize SDTR negotiation flag
2858 for (i
= 0; i
< ARRAY_SIZE(data
->target
); i
++) {
2859 nsp32_target
*target
= &data
->target
[i
];
2861 target
->sync_flag
= 0;
2862 nsp32_set_async(data
, target
);
2868 nsp32_write1(base
, SCSI_BUS_CONTROL
, BUSCTL_RST
);
2869 mdelay(RESET_HOLD_TIME
/ 1000);
2870 nsp32_write1(base
, SCSI_BUS_CONTROL
, 0);
2871 for(i
= 0; i
< 5; i
++) {
2872 intrdat
= nsp32_read2(base
, IRQ_STATUS
); /* dummy read */
2873 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "irq:1: 0x%x", intrdat
);
2876 data
->CurrentSC
= NULL
;
2879 static int nsp32_eh_host_reset(struct scsi_cmnd
*SCpnt
)
2881 struct Scsi_Host
*host
= SCpnt
->device
->host
;
2882 unsigned int base
= SCpnt
->device
->host
->io_port
;
2883 nsp32_hw_data
*data
= (nsp32_hw_data
*)host
->hostdata
;
2885 nsp32_msg(KERN_INFO
, "Host Reset");
2886 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "SCpnt=0x%x", SCpnt
);
2888 spin_lock_irq(SCpnt
->device
->host
->host_lock
);
2891 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
2892 nsp32_do_bus_reset(data
);
2893 nsp32_write2(base
, IRQ_CONTROL
, 0);
2895 spin_unlock_irq(SCpnt
->device
->host
->host_lock
);
2896 return SUCCESS
; /* Host reset is succeeded at any time. */
2900 /**************************************************************************
2905 * getting EEPROM parameter
2907 static int nsp32_getprom_param(nsp32_hw_data
*data
)
2909 int vendor
= data
->pci_devid
->vendor
;
2910 int device
= data
->pci_devid
->device
;
2916 ret
= nsp32_prom_read(data
, 0x7e);
2918 nsp32_msg(KERN_INFO
, "No EEPROM detected: 0x%x", ret
);
2921 ret
= nsp32_prom_read(data
, 0x7f);
2923 nsp32_msg(KERN_INFO
, "Invalid number: 0x%x", ret
);
2930 if (vendor
== PCI_VENDOR_ID_WORKBIT
&&
2931 device
== PCI_DEVICE_ID_WORKBIT_STANDARD
) {
2932 ret
= nsp32_getprom_c16(data
);
2933 } else if (vendor
== PCI_VENDOR_ID_WORKBIT
&&
2934 device
== PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC
) {
2935 ret
= nsp32_getprom_at24(data
);
2936 } else if (vendor
== PCI_VENDOR_ID_WORKBIT
&&
2937 device
== PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO
) {
2938 ret
= nsp32_getprom_at24(data
);
2940 nsp32_msg(KERN_WARNING
, "Unknown EEPROM");
2944 /* for debug : SPROM data full checking */
2945 for (i
= 0; i
<= 0x1f; i
++) {
2946 val
= nsp32_prom_read(data
, i
);
2947 nsp32_dbg(NSP32_DEBUG_EEPROM
,
2948 "rom address 0x%x : 0x%x", i
, val
);
2956 * AT24C01A (Logitec: LHA-600S), AT24C02 (Melco Buffalo: IFC-USLP) data map:
2959 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
2960 * Value 0x0: ASYNC, 0x0c: Ultra-20M, 0x19: Fast-10M
2961 * 0x07 : HBA Synchronous Transfer Period
2962 * Value 0: AutoSync, 1: Manual Setting
2963 * 0x08 - 0x0f : Not Used? (0x0)
2964 * 0x10 : Bus Termination
2965 * Value 0: Auto[ON], 1: ON, 2: OFF
2966 * 0x11 : Not Used? (0)
2967 * 0x12 : Bus Reset Delay Time (0x03)
2968 * 0x13 : Bootable CD Support
2969 * Value 0: Disable, 1: Enable
2970 * 0x14 : Device Scan
2971 * Bit 7 6 5 4 3 2 1 0
2972 * | <----------------->
2973 * | SCSI ID: Value 0: Skip, 1: YES
2974 * |-> Value 0: ALL scan, Value 1: Manual
2975 * 0x15 - 0x1b : Not Used? (0)
2976 * 0x1c : Constant? (0x01) (clock div?)
2977 * 0x1d - 0x7c : Not Used (0xff)
2978 * 0x7d : Not Used? (0xff)
2979 * 0x7e : Constant (0x55), Validity signature
2980 * 0x7f : Constant (0xaa), Validity signature
2982 static int nsp32_getprom_at24(nsp32_hw_data
*data
)
2986 nsp32_target
*target
;
2990 * Reset time which is designated by EEPROM.
2992 * TODO: Not used yet.
2994 data
->resettime
= nsp32_prom_read(data
, 0x12);
2997 * HBA Synchronous Transfer Period
2999 * Note: auto_sync = 0: auto, 1: manual. Ninja SCSI HBA spec says
3000 * that if auto_sync is 0 (auto), and connected SCSI devices are
3001 * same or lower than 3, then transfer speed is set as ULTRA-20M.
3002 * On the contrary if connected SCSI devices are same or higher
3003 * than 4, then transfer speed is set as FAST-10M.
3005 * I break this rule. The number of connected SCSI devices are
3006 * only ignored. If auto_sync is 0 (auto), then transfer speed is
3007 * forced as ULTRA-20M.
3009 ret
= nsp32_prom_read(data
, 0x07);
3018 nsp32_msg(KERN_WARNING
,
3019 "Unsupported Auto Sync mode. Fall back to manual mode.");
3023 if (trans_mode
== ULTRA20M_MODE
) {
3028 * each device Synchronous Transfer Period
3030 for (i
= 0; i
< NSP32_HOST_SCSIID
; i
++) {
3031 target
= &data
->target
[i
];
3032 if (auto_sync
== TRUE
) {
3033 target
->limit_entry
= 0; /* set as ULTRA20M */
3035 ret
= nsp32_prom_read(data
, i
);
3036 entry
= nsp32_search_period_entry(data
, target
, ret
);
3038 /* search failed... set maximum speed */
3041 target
->limit_entry
= entry
;
3050 * C16 110 (I-O Data: SC-NBD) data map:
3053 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3054 * Value 0x0: 20MB/S, 0x1: 10MB/S, 0x2: 5MB/S, 0x3: ASYNC
3055 * 0x07 : 0 (HBA Synchronous Transfer Period: Auto Sync)
3056 * 0x08 - 0x0f : Not Used? (0x0)
3057 * 0x10 : Transfer Mode
3058 * Value 0: PIO, 1: Busmater
3059 * 0x11 : Bus Reset Delay Time (0x00-0x20)
3060 * 0x12 : Bus Termination
3061 * Value 0: Disable, 1: Enable
3062 * 0x13 - 0x19 : Disconnection
3063 * Value 0: Disable, 1: Enable
3064 * 0x1a - 0x7c : Not Used? (0)
3065 * 0x7d : Not Used? (0xf8)
3066 * 0x7e : Constant (0x55), Validity signature
3067 * 0x7f : Constant (0xaa), Validity signature
3069 static int nsp32_getprom_c16(nsp32_hw_data
*data
)
3072 nsp32_target
*target
;
3076 * Reset time which is designated by EEPROM.
3078 * TODO: Not used yet.
3080 data
->resettime
= nsp32_prom_read(data
, 0x11);
3083 * each device Synchronous Transfer Period
3085 for (i
= 0; i
< NSP32_HOST_SCSIID
; i
++) {
3086 target
= &data
->target
[i
];
3087 ret
= nsp32_prom_read(data
, i
);
3089 case 0: /* 20MB/s */
3092 case 1: /* 10MB/s */
3101 default: /* default 20MB/s */
3105 entry
= nsp32_search_period_entry(data
, target
, val
);
3106 if (entry
< 0 || trans_mode
== ULTRA20M_MODE
) {
3107 /* search failed... set maximum speed */
3110 target
->limit_entry
= entry
;
3118 * Atmel AT24C01A (drived in 5V) serial EEPROM routines
3120 static int nsp32_prom_read(nsp32_hw_data
*data
, int romaddr
)
3124 /* start condition */
3125 nsp32_prom_start(data
);
3127 /* device address */
3128 nsp32_prom_write_bit(data
, 1); /* 1 */
3129 nsp32_prom_write_bit(data
, 0); /* 0 */
3130 nsp32_prom_write_bit(data
, 1); /* 1 */
3131 nsp32_prom_write_bit(data
, 0); /* 0 */
3132 nsp32_prom_write_bit(data
, 0); /* A2: 0 (GND) */
3133 nsp32_prom_write_bit(data
, 0); /* A1: 0 (GND) */
3134 nsp32_prom_write_bit(data
, 0); /* A0: 0 (GND) */
3136 /* R/W: W for dummy write */
3137 nsp32_prom_write_bit(data
, 0);
3140 nsp32_prom_write_bit(data
, 0);
3143 for (i
= 7; i
>= 0; i
--) {
3144 nsp32_prom_write_bit(data
, ((romaddr
>> i
) & 1));
3148 nsp32_prom_write_bit(data
, 0);
3150 /* start condition */
3151 nsp32_prom_start(data
);
3153 /* device address */
3154 nsp32_prom_write_bit(data
, 1); /* 1 */
3155 nsp32_prom_write_bit(data
, 0); /* 0 */
3156 nsp32_prom_write_bit(data
, 1); /* 1 */
3157 nsp32_prom_write_bit(data
, 0); /* 0 */
3158 nsp32_prom_write_bit(data
, 0); /* A2: 0 (GND) */
3159 nsp32_prom_write_bit(data
, 0); /* A1: 0 (GND) */
3160 nsp32_prom_write_bit(data
, 0); /* A0: 0 (GND) */
3163 nsp32_prom_write_bit(data
, 1);
3166 nsp32_prom_write_bit(data
, 0);
3170 for (i
= 7; i
>= 0; i
--) {
3171 val
+= (nsp32_prom_read_bit(data
) << i
);
3175 nsp32_prom_write_bit(data
, 1);
3177 /* stop condition */
3178 nsp32_prom_stop(data
);
3183 static void nsp32_prom_set(nsp32_hw_data
*data
, int bit
, int val
)
3185 int base
= data
->BaseAddress
;
3188 tmp
= nsp32_index_read1(base
, SERIAL_ROM_CTL
);
3196 nsp32_index_write1(base
, SERIAL_ROM_CTL
, tmp
);
3201 static int nsp32_prom_get(nsp32_hw_data
*data
, int bit
)
3203 int base
= data
->BaseAddress
;
3207 nsp32_msg(KERN_ERR
, "return value is not appropriate");
3212 tmp
= nsp32_index_read1(base
, SERIAL_ROM_CTL
) & bit
;
3225 static void nsp32_prom_start (nsp32_hw_data
*data
)
3227 /* start condition */
3228 nsp32_prom_set(data
, SCL
, 1);
3229 nsp32_prom_set(data
, SDA
, 1);
3230 nsp32_prom_set(data
, ENA
, 1); /* output mode */
3231 nsp32_prom_set(data
, SDA
, 0); /* keeping SCL=1 and transiting
3232 * SDA 1->0 is start condition */
3233 nsp32_prom_set(data
, SCL
, 0);
3236 static void nsp32_prom_stop (nsp32_hw_data
*data
)
3238 /* stop condition */
3239 nsp32_prom_set(data
, SCL
, 1);
3240 nsp32_prom_set(data
, SDA
, 0);
3241 nsp32_prom_set(data
, ENA
, 1); /* output mode */
3242 nsp32_prom_set(data
, SDA
, 1);
3243 nsp32_prom_set(data
, SCL
, 0);
3246 static void nsp32_prom_write_bit(nsp32_hw_data
*data
, int val
)
3249 nsp32_prom_set(data
, SDA
, val
);
3250 nsp32_prom_set(data
, SCL
, 1 );
3251 nsp32_prom_set(data
, SCL
, 0 );
3254 static int nsp32_prom_read_bit(nsp32_hw_data
*data
)
3259 nsp32_prom_set(data
, ENA
, 0); /* input mode */
3260 nsp32_prom_set(data
, SCL
, 1);
3262 val
= nsp32_prom_get(data
, SDA
);
3264 nsp32_prom_set(data
, SCL
, 0);
3265 nsp32_prom_set(data
, ENA
, 1); /* output mode */
3271 /**************************************************************************
3276 /* Device suspended */
3277 static int nsp32_suspend(struct pci_dev
*pdev
, pm_message_t state
)
3279 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
3281 nsp32_msg(KERN_INFO
, "pci-suspend: pdev=0x%p, state=%ld, slot=%s, host=0x%p", pdev
, state
, pci_name(pdev
), host
);
3283 pci_save_state (pdev
);
3284 pci_disable_device (pdev
);
3285 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
3290 /* Device woken up */
3291 static int nsp32_resume(struct pci_dev
*pdev
)
3293 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
3294 nsp32_hw_data
*data
= (nsp32_hw_data
*)host
->hostdata
;
3297 nsp32_msg(KERN_INFO
, "pci-resume: pdev=0x%p, slot=%s, host=0x%p", pdev
, pci_name(pdev
), host
);
3299 pci_set_power_state(pdev
, PCI_D0
);
3300 pci_enable_wake (pdev
, PCI_D0
, 0);
3301 pci_restore_state (pdev
);
3303 reg
= nsp32_read2(data
->BaseAddress
, INDEX_REG
);
3305 nsp32_msg(KERN_INFO
, "io=0x%x reg=0x%x", data
->BaseAddress
, reg
);
3307 if (reg
== 0xffff) {
3308 nsp32_msg(KERN_INFO
, "missing device. abort resume.");
3312 nsp32hw_init (data
);
3313 nsp32_do_bus_reset(data
);
3315 nsp32_msg(KERN_INFO
, "resume success");
3322 /************************************************************************
3323 * PCI/Cardbus probe/remove routine
3325 static int nsp32_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3328 nsp32_hw_data
*data
= &nsp32_data_base
;
3330 nsp32_dbg(NSP32_DEBUG_REGISTER
, "enter");
3332 ret
= pci_enable_device(pdev
);
3334 nsp32_msg(KERN_ERR
, "failed to enable pci device");
3339 data
->pci_devid
= id
;
3340 data
->IrqNumber
= pdev
->irq
;
3341 data
->BaseAddress
= pci_resource_start(pdev
, 0);
3342 data
->NumAddress
= pci_resource_len (pdev
, 0);
3343 data
->MmioAddress
= pci_ioremap_bar(pdev
, 1);
3344 data
->MmioLength
= pci_resource_len (pdev
, 1);
3346 pci_set_master(pdev
);
3348 ret
= nsp32_detect(pdev
);
3350 nsp32_msg(KERN_INFO
, "irq: %i mmio: %p+0x%lx slot: %s model: %s",
3352 data
->MmioAddress
, data
->MmioLength
,
3354 nsp32_model
[id
->driver_data
]);
3356 nsp32_dbg(NSP32_DEBUG_REGISTER
, "exit %d", ret
);
3361 static void nsp32_remove(struct pci_dev
*pdev
)
3363 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
3365 nsp32_dbg(NSP32_DEBUG_REGISTER
, "enter");
3367 scsi_remove_host(host
);
3369 nsp32_release(host
);
3371 scsi_host_put(host
);
3374 static struct pci_driver nsp32_driver
= {
3376 .id_table
= nsp32_pci_table
,
3377 .probe
= nsp32_probe
,
3378 .remove
= nsp32_remove
,
3380 .suspend
= nsp32_suspend
,
3381 .resume
= nsp32_resume
,
3385 /*********************************************************************
3388 static int __init
init_nsp32(void) {
3389 nsp32_msg(KERN_INFO
, "loading...");
3390 return pci_register_driver(&nsp32_driver
);
3393 static void __exit
exit_nsp32(void) {
3394 nsp32_msg(KERN_INFO
, "unloading...");
3395 pci_unregister_driver(&nsp32_driver
);
3398 module_init(init_nsp32
);
3399 module_exit(exit_nsp32
);