2 * NinjaSCSI-32Bi Cardbus, NinjaSCSI-32UDE PCI/CardBus SCSI driver
3 * Copyright (C) 2001, 2002, 2003
4 * YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
5 * GOTO Masanori <gotom@debian.or.jp>, <gotom@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 * 1.0: Initial Release.
20 * 1.1: Add /proc SDTR status.
21 * Remove obsolete error handler nsp32_reset.
23 * 1.2: PowerPC (big endian) support.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/string.h>
30 #include <linux/timer.h>
31 #include <linux/ioport.h>
32 #include <linux/major.h>
33 #include <linux/blkdev.h>
34 #include <linux/interrupt.h>
35 #include <linux/pci.h>
36 #include <linux/delay.h>
37 #include <linux/ctype.h>
38 #include <linux/dma-mapping.h>
41 #include <asm/system.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/scsi_device.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi_ioctl.h>
53 /***********************************************************************
56 static int trans_mode
= 0; /* default: BIOS */
57 module_param (trans_mode
, int, 0);
58 MODULE_PARM_DESC(trans_mode
, "transfer mode (0: BIOS(default) 1: Async 2: Ultra20M");
60 #define ULTRA20M_MODE 2
62 static bool auto_param
= 0; /* default: ON */
63 module_param (auto_param
, bool, 0);
64 MODULE_PARM_DESC(auto_param
, "AutoParameter mode (0: ON(default) 1: OFF)");
66 static bool disc_priv
= 1; /* default: OFF */
67 module_param (disc_priv
, bool, 0);
68 MODULE_PARM_DESC(disc_priv
, "disconnection privilege mode (0: ON 1: OFF(default))");
70 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>, GOTO Masanori <gotom@debian.or.jp>");
71 MODULE_DESCRIPTION("Workbit NinjaSCSI-32Bi/UDE CardBus/PCI SCSI host bus adapter module");
72 MODULE_LICENSE("GPL");
74 static const char *nsp32_release_version
= "1.2";
77 /****************************************************************************
80 static struct pci_device_id nsp32_pci_table
[] __devinitdata
= {
82 .vendor
= PCI_VENDOR_ID_IODATA
,
83 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_CBSC_II
,
84 .subvendor
= PCI_ANY_ID
,
85 .subdevice
= PCI_ANY_ID
,
86 .driver_data
= MODEL_IODATA
,
89 .vendor
= PCI_VENDOR_ID_WORKBIT
,
90 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_KME
,
91 .subvendor
= PCI_ANY_ID
,
92 .subdevice
= PCI_ANY_ID
,
93 .driver_data
= MODEL_KME
,
96 .vendor
= PCI_VENDOR_ID_WORKBIT
,
97 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_WBT
,
98 .subvendor
= PCI_ANY_ID
,
99 .subdevice
= PCI_ANY_ID
,
100 .driver_data
= MODEL_WORKBIT
,
103 .vendor
= PCI_VENDOR_ID_WORKBIT
,
104 .device
= PCI_DEVICE_ID_WORKBIT_STANDARD
,
105 .subvendor
= PCI_ANY_ID
,
106 .subdevice
= PCI_ANY_ID
,
107 .driver_data
= MODEL_PCI_WORKBIT
,
110 .vendor
= PCI_VENDOR_ID_WORKBIT
,
111 .device
= PCI_DEVICE_ID_NINJASCSI_32BI_LOGITEC
,
112 .subvendor
= PCI_ANY_ID
,
113 .subdevice
= PCI_ANY_ID
,
114 .driver_data
= MODEL_LOGITEC
,
117 .vendor
= PCI_VENDOR_ID_WORKBIT
,
118 .device
= PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC
,
119 .subvendor
= PCI_ANY_ID
,
120 .subdevice
= PCI_ANY_ID
,
121 .driver_data
= MODEL_PCI_LOGITEC
,
124 .vendor
= PCI_VENDOR_ID_WORKBIT
,
125 .device
= PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO
,
126 .subvendor
= PCI_ANY_ID
,
127 .subdevice
= PCI_ANY_ID
,
128 .driver_data
= MODEL_PCI_MELCO
,
131 .vendor
= PCI_VENDOR_ID_WORKBIT
,
132 .device
= PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO_II
,
133 .subvendor
= PCI_ANY_ID
,
134 .subdevice
= PCI_ANY_ID
,
135 .driver_data
= MODEL_PCI_MELCO
,
139 MODULE_DEVICE_TABLE(pci
, nsp32_pci_table
);
141 static nsp32_hw_data nsp32_data_base
; /* probe <-> detect glue */
145 * Period/AckWidth speed conversion table
147 * Note: This period/ackwidth speed table must be in descending order.
149 static nsp32_sync_table nsp32_sync_table_40M
[] = {
150 /* {PNo, AW, SP, EP, SREQ smpl} Speed(MB/s) Period AckWidth */
151 {0x1, 0, 0x0c, 0x0c, SMPL_40M
}, /* 20.0 : 50ns, 25ns */
152 {0x2, 0, 0x0d, 0x18, SMPL_40M
}, /* 13.3 : 75ns, 25ns */
153 {0x3, 1, 0x19, 0x19, SMPL_40M
}, /* 10.0 : 100ns, 50ns */
154 {0x4, 1, 0x1a, 0x1f, SMPL_20M
}, /* 8.0 : 125ns, 50ns */
155 {0x5, 2, 0x20, 0x25, SMPL_20M
}, /* 6.7 : 150ns, 75ns */
156 {0x6, 2, 0x26, 0x31, SMPL_20M
}, /* 5.7 : 175ns, 75ns */
157 {0x7, 3, 0x32, 0x32, SMPL_20M
}, /* 5.0 : 200ns, 100ns */
158 {0x8, 3, 0x33, 0x38, SMPL_10M
}, /* 4.4 : 225ns, 100ns */
159 {0x9, 3, 0x39, 0x3e, SMPL_10M
}, /* 4.0 : 250ns, 100ns */
162 static nsp32_sync_table nsp32_sync_table_20M
[] = {
163 {0x1, 0, 0x19, 0x19, SMPL_40M
}, /* 10.0 : 100ns, 50ns */
164 {0x2, 0, 0x1a, 0x25, SMPL_20M
}, /* 6.7 : 150ns, 50ns */
165 {0x3, 1, 0x26, 0x32, SMPL_20M
}, /* 5.0 : 200ns, 100ns */
166 {0x4, 1, 0x33, 0x3e, SMPL_10M
}, /* 4.0 : 250ns, 100ns */
167 {0x5, 2, 0x3f, 0x4b, SMPL_10M
}, /* 3.3 : 300ns, 150ns */
168 {0x6, 2, 0x4c, 0x57, SMPL_10M
}, /* 2.8 : 350ns, 150ns */
169 {0x7, 3, 0x58, 0x64, SMPL_10M
}, /* 2.5 : 400ns, 200ns */
170 {0x8, 3, 0x65, 0x70, SMPL_10M
}, /* 2.2 : 450ns, 200ns */
171 {0x9, 3, 0x71, 0x7d, SMPL_10M
}, /* 2.0 : 500ns, 200ns */
174 static nsp32_sync_table nsp32_sync_table_pci
[] = {
175 {0x1, 0, 0x0c, 0x0f, SMPL_40M
}, /* 16.6 : 60ns, 30ns */
176 {0x2, 0, 0x10, 0x16, SMPL_40M
}, /* 11.1 : 90ns, 30ns */
177 {0x3, 1, 0x17, 0x1e, SMPL_20M
}, /* 8.3 : 120ns, 60ns */
178 {0x4, 1, 0x1f, 0x25, SMPL_20M
}, /* 6.7 : 150ns, 60ns */
179 {0x5, 2, 0x26, 0x2d, SMPL_20M
}, /* 5.6 : 180ns, 90ns */
180 {0x6, 2, 0x2e, 0x34, SMPL_10M
}, /* 4.8 : 210ns, 90ns */
181 {0x7, 3, 0x35, 0x3c, SMPL_10M
}, /* 4.2 : 240ns, 120ns */
182 {0x8, 3, 0x3d, 0x43, SMPL_10M
}, /* 3.7 : 270ns, 120ns */
183 {0x9, 3, 0x44, 0x4b, SMPL_10M
}, /* 3.3 : 300ns, 120ns */
187 * function declaration
189 /* module entry point */
190 static int __devinit
nsp32_probe (struct pci_dev
*, const struct pci_device_id
*);
191 static void __devexit
nsp32_remove(struct pci_dev
*);
192 static int __init
init_nsp32 (void);
193 static void __exit
exit_nsp32 (void);
195 /* struct struct scsi_host_template */
196 static int nsp32_proc_info (struct Scsi_Host
*, char *, char **, off_t
, int, int);
198 static int nsp32_detect (struct pci_dev
*pdev
);
199 static int nsp32_queuecommand(struct Scsi_Host
*, struct scsi_cmnd
*);
200 static const char *nsp32_info (struct Scsi_Host
*);
201 static int nsp32_release (struct Scsi_Host
*);
203 /* SCSI error handler */
204 static int nsp32_eh_abort (struct scsi_cmnd
*);
205 static int nsp32_eh_bus_reset (struct scsi_cmnd
*);
206 static int nsp32_eh_host_reset(struct scsi_cmnd
*);
208 /* generate SCSI message */
209 static void nsp32_build_identify(struct scsi_cmnd
*);
210 static void nsp32_build_nop (struct scsi_cmnd
*);
211 static void nsp32_build_reject (struct scsi_cmnd
*);
212 static void nsp32_build_sdtr (struct scsi_cmnd
*, unsigned char, unsigned char);
214 /* SCSI message handler */
215 static int nsp32_busfree_occur(struct scsi_cmnd
*, unsigned short);
216 static void nsp32_msgout_occur (struct scsi_cmnd
*);
217 static void nsp32_msgin_occur (struct scsi_cmnd
*, unsigned long, unsigned short);
219 static int nsp32_setup_sg_table (struct scsi_cmnd
*);
220 static int nsp32_selection_autopara(struct scsi_cmnd
*);
221 static int nsp32_selection_autoscsi(struct scsi_cmnd
*);
222 static void nsp32_scsi_done (struct scsi_cmnd
*);
223 static int nsp32_arbitration (struct scsi_cmnd
*, unsigned int);
224 static int nsp32_reselection (struct scsi_cmnd
*, unsigned char);
225 static void nsp32_adjust_busfree (struct scsi_cmnd
*, unsigned int);
226 static void nsp32_restart_autoscsi (struct scsi_cmnd
*, unsigned short);
229 static void nsp32_analyze_sdtr (struct scsi_cmnd
*);
230 static int nsp32_search_period_entry(nsp32_hw_data
*, nsp32_target
*, unsigned char);
231 static void nsp32_set_async (nsp32_hw_data
*, nsp32_target
*);
232 static void nsp32_set_max_sync (nsp32_hw_data
*, nsp32_target
*, unsigned char *, unsigned char *);
233 static void nsp32_set_sync_entry (nsp32_hw_data
*, nsp32_target
*, int, unsigned char);
235 /* SCSI bus status handler */
236 static void nsp32_wait_req (nsp32_hw_data
*, int);
237 static void nsp32_wait_sack (nsp32_hw_data
*, int);
238 static void nsp32_sack_assert (nsp32_hw_data
*);
239 static void nsp32_sack_negate (nsp32_hw_data
*);
240 static void nsp32_do_bus_reset(nsp32_hw_data
*);
242 /* hardware interrupt handler */
243 static irqreturn_t
do_nsp32_isr(int, void *);
245 /* initialize hardware */
246 static int nsp32hw_init(nsp32_hw_data
*);
249 static int nsp32_getprom_param (nsp32_hw_data
*);
250 static int nsp32_getprom_at24 (nsp32_hw_data
*);
251 static int nsp32_getprom_c16 (nsp32_hw_data
*);
252 static void nsp32_prom_start (nsp32_hw_data
*);
253 static void nsp32_prom_stop (nsp32_hw_data
*);
254 static int nsp32_prom_read (nsp32_hw_data
*, int);
255 static int nsp32_prom_read_bit (nsp32_hw_data
*);
256 static void nsp32_prom_write_bit(nsp32_hw_data
*, int);
257 static void nsp32_prom_set (nsp32_hw_data
*, int, int);
258 static int nsp32_prom_get (nsp32_hw_data
*, int);
260 /* debug/warning/info message */
261 static void nsp32_message (const char *, int, char *, char *, ...);
263 static void nsp32_dmessage(const char *, int, int, char *, ...);
267 * max_sectors is currently limited up to 128.
269 static struct scsi_host_template nsp32_template
= {
270 .proc_name
= "nsp32",
271 .name
= "Workbit NinjaSCSI-32Bi/UDE",
272 .proc_info
= nsp32_proc_info
,
274 .queuecommand
= nsp32_queuecommand
,
276 .sg_tablesize
= NSP32_SG_SIZE
,
279 .this_id
= NSP32_HOST_SCSIID
,
280 .use_clustering
= DISABLE_CLUSTERING
,
281 .eh_abort_handler
= nsp32_eh_abort
,
282 .eh_bus_reset_handler
= nsp32_eh_bus_reset
,
283 .eh_host_reset_handler
= nsp32_eh_host_reset
,
284 /* .highmem_io = 1, */
287 #include "nsp32_io.h"
289 /***********************************************************************
293 # define NSP32_DEBUG_MASK 0x000000
294 # define nsp32_msg(type, args...) nsp32_message ("", 0, (type), args)
295 # define nsp32_dbg(mask, args...) /* */
297 # define NSP32_DEBUG_MASK 0xffffff
298 # define nsp32_msg(type, args...) \
299 nsp32_message (__func__, __LINE__, (type), args)
300 # define nsp32_dbg(mask, args...) \
301 nsp32_dmessage(__func__, __LINE__, (mask), args)
304 #define NSP32_DEBUG_QUEUECOMMAND BIT(0)
305 #define NSP32_DEBUG_REGISTER BIT(1)
306 #define NSP32_DEBUG_AUTOSCSI BIT(2)
307 #define NSP32_DEBUG_INTR BIT(3)
308 #define NSP32_DEBUG_SGLIST BIT(4)
309 #define NSP32_DEBUG_BUSFREE BIT(5)
310 #define NSP32_DEBUG_CDB_CONTENTS BIT(6)
311 #define NSP32_DEBUG_RESELECTION BIT(7)
312 #define NSP32_DEBUG_MSGINOCCUR BIT(8)
313 #define NSP32_DEBUG_EEPROM BIT(9)
314 #define NSP32_DEBUG_MSGOUTOCCUR BIT(10)
315 #define NSP32_DEBUG_BUSRESET BIT(11)
316 #define NSP32_DEBUG_RESTART BIT(12)
317 #define NSP32_DEBUG_SYNC BIT(13)
318 #define NSP32_DEBUG_WAIT BIT(14)
319 #define NSP32_DEBUG_TARGETFLAG BIT(15)
320 #define NSP32_DEBUG_PROC BIT(16)
321 #define NSP32_DEBUG_INIT BIT(17)
322 #define NSP32_SPECIAL_PRINT_REGISTER BIT(20)
324 #define NSP32_DEBUG_BUF_LEN 100
326 static void nsp32_message(const char *func
, int line
, char *type
, char *fmt
, ...)
329 char buf
[NSP32_DEBUG_BUF_LEN
];
332 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
336 printk("%snsp32: %s\n", type
, buf
);
338 printk("%snsp32: %s (%d): %s\n", type
, func
, line
, buf
);
343 static void nsp32_dmessage(const char *func
, int line
, int mask
, char *fmt
, ...)
346 char buf
[NSP32_DEBUG_BUF_LEN
];
349 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
352 if (mask
& NSP32_DEBUG_MASK
) {
353 printk("nsp32-debug: 0x%x %s (%d): %s\n", mask
, func
, line
, buf
);
359 # include "nsp32_debug.c"
361 # define show_command(arg) /* */
362 # define show_busphase(arg) /* */
363 # define show_autophase(arg) /* */
369 static void nsp32_build_identify(struct scsi_cmnd
*SCpnt
)
371 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
372 int pos
= data
->msgout_len
;
375 /* XXX: Auto DiscPriv detection is progressing... */
376 if (disc_priv
== 0) {
380 data
->msgoutbuf
[pos
] = IDENTIFY(mode
, SCpnt
->device
->lun
); pos
++;
382 data
->msgout_len
= pos
;
386 * SDTR Message Routine
388 static void nsp32_build_sdtr(struct scsi_cmnd
*SCpnt
,
389 unsigned char period
,
390 unsigned char offset
)
392 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
393 int pos
= data
->msgout_len
;
395 data
->msgoutbuf
[pos
] = EXTENDED_MESSAGE
; pos
++;
396 data
->msgoutbuf
[pos
] = EXTENDED_SDTR_LEN
; pos
++;
397 data
->msgoutbuf
[pos
] = EXTENDED_SDTR
; pos
++;
398 data
->msgoutbuf
[pos
] = period
; pos
++;
399 data
->msgoutbuf
[pos
] = offset
; pos
++;
401 data
->msgout_len
= pos
;
405 * No Operation Message
407 static void nsp32_build_nop(struct scsi_cmnd
*SCpnt
)
409 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
410 int pos
= data
->msgout_len
;
413 nsp32_msg(KERN_WARNING
,
414 "Some messages are already contained!");
418 data
->msgoutbuf
[pos
] = NOP
; pos
++;
419 data
->msgout_len
= pos
;
425 static void nsp32_build_reject(struct scsi_cmnd
*SCpnt
)
427 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
428 int pos
= data
->msgout_len
;
430 data
->msgoutbuf
[pos
] = MESSAGE_REJECT
; pos
++;
431 data
->msgout_len
= pos
;
438 static void nsp32_start_timer(struct scsi_cmnd
*SCpnt
, int time
)
440 unsigned int base
= SCpnt
->host
->io_port
;
442 nsp32_dbg(NSP32_DEBUG_INTR
, "timer=%d", time
);
444 if (time
& (~TIMER_CNT_MASK
)) {
445 nsp32_dbg(NSP32_DEBUG_INTR
, "timer set overflow");
448 nsp32_write2(base
, TIMER_SET
, time
& TIMER_CNT_MASK
);
454 * set SCSI command and other parameter to asic, and start selection phase
456 static int nsp32_selection_autopara(struct scsi_cmnd
*SCpnt
)
458 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
459 unsigned int base
= SCpnt
->device
->host
->io_port
;
460 unsigned int host_id
= SCpnt
->device
->host
->this_id
;
461 unsigned char target
= scmd_id(SCpnt
);
462 nsp32_autoparam
*param
= data
->autoparam
;
468 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "in");
473 phase
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
474 if (phase
!= BUSMON_BUS_FREE
) {
475 nsp32_msg(KERN_WARNING
, "bus busy");
476 show_busphase(phase
& BUSMON_PHASE_MASK
);
477 SCpnt
->result
= DID_BUS_BUSY
<< 16;
484 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
485 * over 3 messages needs another routine.
487 if (data
->msgout_len
== 0) {
488 nsp32_msg(KERN_ERR
, "SCSI MsgOut without any message!");
489 SCpnt
->result
= DID_ERROR
<< 16;
491 } else if (data
->msgout_len
> 0 && data
->msgout_len
<= 3) {
493 for (i
= 0; i
< data
->msgout_len
; i
++) {
495 * the sending order of the message is:
496 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
497 * MCNT 2: MSG#1 -> MSG#2
501 msgout
|= ((unsigned int)(data
->msgoutbuf
[i
]) << 24);
503 msgout
|= MV_VALID
; /* MV valid */
504 msgout
|= (unsigned int)data
->msgout_len
; /* len */
506 /* data->msgout_len > 3 */
510 // nsp_dbg(NSP32_DEBUG_AUTOSCSI, "sel time out=0x%x\n", nsp32_read2(base, SEL_TIME_OUT));
511 // nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
514 * setup asic parameter
516 memset(param
, 0, sizeof(nsp32_autoparam
));
519 for (i
= 0; i
< SCpnt
->cmd_len
; i
++) {
520 param
->cdb
[4 * i
] = SCpnt
->cmnd
[i
];
523 /* outgoing messages */
524 param
->msgout
= cpu_to_le32(msgout
);
526 /* syncreg, ackwidth, target id, SREQ sampling rate */
527 param
->syncreg
= data
->cur_target
->syncreg
;
528 param
->ackwidth
= data
->cur_target
->ackwidth
;
529 param
->target_id
= BIT(host_id
) | BIT(target
);
530 param
->sample_reg
= data
->cur_target
->sample_reg
;
532 // nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "sample rate=0x%x\n", data->cur_target->sample_reg);
534 /* command control */
535 param
->command_control
= cpu_to_le16(CLEAR_CDB_FIFO_POINTER
|
537 AUTO_MSGIN_00_OR_04
|
542 /* transfer control */
544 switch (data
->trans_method
) {
545 case NSP32_TRANSFER_BUSMASTER
:
548 case NSP32_TRANSFER_MMIO
:
551 case NSP32_TRANSFER_PIO
:
555 nsp32_msg(KERN_ERR
, "unknown trans_method");
559 * OR-ed BLIEND_MODE, FIFO intr is decreased, instead of PCI bus waits.
560 * For bus master transfer, it's taken off.
562 s
|= (TRANSFER_GO
| ALL_COUNTER_CLR
);
563 param
->transfer_control
= cpu_to_le16(s
);
566 param
->sgt_pointer
= cpu_to_le32(data
->cur_lunt
->sglun_paddr
);
569 * transfer parameter to ASIC
571 nsp32_write4(base
, SGT_ADR
, data
->auto_paddr
);
572 nsp32_write2(base
, COMMAND_CONTROL
, CLEAR_CDB_FIFO_POINTER
|
578 ret
= nsp32_arbitration(SCpnt
, base
);
585 * Selection with AUTO SCSI (without AUTO PARAMETER)
587 static int nsp32_selection_autoscsi(struct scsi_cmnd
*SCpnt
)
589 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
590 unsigned int base
= SCpnt
->device
->host
->io_port
;
591 unsigned int host_id
= SCpnt
->device
->host
->this_id
;
592 unsigned char target
= scmd_id(SCpnt
);
595 unsigned short command
= 0;
596 unsigned int msgout
= 0;
597 unsigned short execph
;
600 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "in");
605 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
610 phase
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
611 if(((phase
& BUSMON_BSY
) == 1) || (phase
& BUSMON_SEL
) == 1) {
612 nsp32_msg(KERN_WARNING
, "bus busy");
613 SCpnt
->result
= DID_BUS_BUSY
<< 16;
621 execph
= nsp32_read2(base
, SCSI_EXECUTE_PHASE
);
624 * clear FIFO counter to set CDBs
626 nsp32_write2(base
, COMMAND_CONTROL
, CLEAR_CDB_FIFO_POINTER
);
631 for (i
= 0; i
< SCpnt
->cmd_len
; i
++) {
632 nsp32_write1(base
, COMMAND_DATA
, SCpnt
->cmnd
[i
]);
634 nsp32_dbg(NSP32_DEBUG_CDB_CONTENTS
, "CDB[0]=[0x%x]", SCpnt
->cmnd
[0]);
637 * set SCSIOUT LATCH(initiator)/TARGET(target) (OR-ed) ID
639 nsp32_write1(base
, SCSI_OUT_LATCH_TARGET_ID
, BIT(host_id
) | BIT(target
));
642 * set SCSI MSGOUT REG
644 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
645 * over 3 messages needs another routine.
647 if (data
->msgout_len
== 0) {
648 nsp32_msg(KERN_ERR
, "SCSI MsgOut without any message!");
649 SCpnt
->result
= DID_ERROR
<< 16;
652 } else if (data
->msgout_len
> 0 && data
->msgout_len
<= 3) {
654 for (i
= 0; i
< data
->msgout_len
; i
++) {
656 * the sending order of the message is:
657 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
658 * MCNT 2: MSG#1 -> MSG#2
662 msgout
|= ((unsigned int)(data
->msgoutbuf
[i
]) << 24);
664 msgout
|= MV_VALID
; /* MV valid */
665 msgout
|= (unsigned int)data
->msgout_len
; /* len */
666 nsp32_write4(base
, SCSI_MSG_OUT
, msgout
);
668 /* data->msgout_len > 3 */
669 nsp32_write4(base
, SCSI_MSG_OUT
, 0);
673 * set selection timeout(= 250ms)
675 nsp32_write2(base
, SEL_TIME_OUT
, SEL_TIMEOUT_TIME
);
678 * set SREQ hazard killer sampling rate
680 * TODO: sample_rate (BASE+0F) is 0 when internal clock = 40MHz.
681 * check other internal clock!
683 nsp32_write1(base
, SREQ_SMPL_RATE
, data
->cur_target
->sample_reg
);
688 nsp32_write1(base
, SET_ARBIT
, ARBIT_CLEAR
);
692 * Don't set BM_START_ADR before setting this register.
694 nsp32_write1(base
, SYNC_REG
, data
->cur_target
->syncreg
);
699 nsp32_write1(base
, ACK_WIDTH
, data
->cur_target
->ackwidth
);
701 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
,
702 "syncreg=0x%x, ackwidth=0x%x, sgtpaddr=0x%x, id=0x%x",
703 nsp32_read1(base
, SYNC_REG
), nsp32_read1(base
, ACK_WIDTH
),
704 nsp32_read4(base
, SGT_ADR
), nsp32_read1(base
, SCSI_OUT_LATCH_TARGET_ID
));
705 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "msgout_len=%d, msgout=0x%x",
706 data
->msgout_len
, msgout
);
709 * set SGT ADDR (physical address)
711 nsp32_write4(base
, SGT_ADR
, data
->cur_lunt
->sglun_paddr
);
714 * set TRANSFER CONTROL REG
717 command
|= (TRANSFER_GO
| ALL_COUNTER_CLR
);
718 if (data
->trans_method
& NSP32_TRANSFER_BUSMASTER
) {
719 if (scsi_bufflen(SCpnt
) > 0) {
722 } else if (data
->trans_method
& NSP32_TRANSFER_MMIO
) {
723 command
|= CB_MMIO_MODE
;
724 } else if (data
->trans_method
& NSP32_TRANSFER_PIO
) {
725 command
|= CB_IO_MODE
;
727 nsp32_write2(base
, TRANSFER_CONTROL
, command
);
730 * start AUTO SCSI, kick off arbitration
732 command
= (CLEAR_CDB_FIFO_POINTER
|
734 AUTO_MSGIN_00_OR_04
|
737 nsp32_write2(base
, COMMAND_CONTROL
, command
);
742 status
= nsp32_arbitration(SCpnt
, base
);
748 nsp32_write2(base
, IRQ_CONTROL
, 0);
755 * Arbitration Status Check
757 * Note: Arbitration counter is waited during ARBIT_GO is not lifting.
758 * Using udelay(1) consumes CPU time and system time, but
759 * arbitration delay time is defined minimal 2.4us in SCSI
760 * specification, thus udelay works as coarse grained wait timer.
762 static int nsp32_arbitration(struct scsi_cmnd
*SCpnt
, unsigned int base
)
769 arbit
= nsp32_read1(base
, ARBIT_STATUS
);
771 } while ((arbit
& (ARBIT_WIN
| ARBIT_FAIL
)) == 0 &&
772 (time
<= ARBIT_TIMEOUT_TIME
));
774 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
,
775 "arbit: 0x%x, delay time: %d", arbit
, time
);
777 if (arbit
& ARBIT_WIN
) {
778 /* Arbitration succeeded */
779 SCpnt
->result
= DID_OK
<< 16;
780 nsp32_index_write1(base
, EXT_PORT
, LED_ON
); /* PCI LED on */
781 } else if (arbit
& ARBIT_FAIL
) {
782 /* Arbitration failed */
783 SCpnt
->result
= DID_BUS_BUSY
<< 16;
787 * unknown error or ARBIT_GO timeout,
788 * something lock up! guess no connection.
790 nsp32_dbg(NSP32_DEBUG_AUTOSCSI
, "arbit timeout");
791 SCpnt
->result
= DID_NO_CONNECT
<< 16;
798 nsp32_write1(base
, SET_ARBIT
, ARBIT_CLEAR
);
807 * Note: This reselection routine is called from msgin_occur,
808 * reselection target id&lun must be already set.
809 * SCSI-2 says IDENTIFY implies RESTORE_POINTER operation.
811 static int nsp32_reselection(struct scsi_cmnd
*SCpnt
, unsigned char newlun
)
813 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
814 unsigned int host_id
= SCpnt
->device
->host
->this_id
;
815 unsigned int base
= SCpnt
->device
->host
->io_port
;
816 unsigned char tmpid
, newid
;
818 nsp32_dbg(NSP32_DEBUG_RESELECTION
, "enter");
821 * calculate reselected SCSI ID
823 tmpid
= nsp32_read1(base
, RESELECT_ID
);
824 tmpid
&= (~BIT(host_id
));
835 * If reselected New ID:LUN is not existed
836 * or current nexus is not existed, unexpected
837 * reselection is occurred. Send reject message.
839 if (newid
>= ARRAY_SIZE(data
->lunt
) || newlun
>= ARRAY_SIZE(data
->lunt
[0])) {
840 nsp32_msg(KERN_WARNING
, "unknown id/lun");
842 } else if(data
->lunt
[newid
][newlun
].SCpnt
== NULL
) {
843 nsp32_msg(KERN_WARNING
, "no SCSI command is processing");
847 data
->cur_id
= newid
;
848 data
->cur_lun
= newlun
;
849 data
->cur_target
= &(data
->target
[newid
]);
850 data
->cur_lunt
= &(data
->lunt
[newid
][newlun
]);
852 /* reset SACK/SavedACK counter (or ALL clear?) */
853 nsp32_write4(base
, CLR_COUNTER
, CLRCOUNTER_ALLMASK
);
860 * nsp32_setup_sg_table - build scatter gather list for transfer data
863 * Note: NinjaSCSI-32Bi/UDE bus master can not transfer over 64KB at a time.
865 static int nsp32_setup_sg_table(struct scsi_cmnd
*SCpnt
)
867 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
868 struct scatterlist
*sg
;
869 nsp32_sgtable
*sgt
= data
->cur_lunt
->sglun
->sgt
;
874 nsp32_dbg(NSP32_DEBUG_SGLIST
, "SGT == null");
878 num
= scsi_dma_map(SCpnt
);
884 scsi_for_each_sg(SCpnt
, sg
, num
, i
) {
886 * Build nsp32_sglist, substitute sg dma addresses.
888 sgt
[i
].addr
= cpu_to_le32(sg_dma_address(sg
));
889 sgt
[i
].len
= cpu_to_le32(sg_dma_len(sg
));
891 if (le32_to_cpu(sgt
[i
].len
) > 0x10000) {
893 "can't transfer over 64KB at a time, size=0x%lx", le32_to_cpu(sgt
[i
].len
));
896 nsp32_dbg(NSP32_DEBUG_SGLIST
,
897 "num 0x%x : addr 0x%lx len 0x%lx",
899 le32_to_cpu(sgt
[i
].addr
),
900 le32_to_cpu(sgt
[i
].len
));
904 l
= le32_to_cpu(sgt
[num
-1].len
);
905 sgt
[num
-1].len
= cpu_to_le32(l
| SGTEND
);
911 static int nsp32_queuecommand_lck(struct scsi_cmnd
*SCpnt
, void (*done
)(struct scsi_cmnd
*))
913 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
914 nsp32_target
*target
;
915 nsp32_lunt
*cur_lunt
;
918 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
919 "enter. target: 0x%x LUN: 0x%x cmnd: 0x%x cmndlen: 0x%x "
920 "use_sg: 0x%x reqbuf: 0x%lx reqlen: 0x%x",
921 SCpnt
->device
->id
, SCpnt
->device
->lun
, SCpnt
->cmnd
[0], SCpnt
->cmd_len
,
922 scsi_sg_count(SCpnt
), scsi_sglist(SCpnt
), scsi_bufflen(SCpnt
));
924 if (data
->CurrentSC
!= NULL
) {
925 nsp32_msg(KERN_ERR
, "Currentsc != NULL. Cancel this command request");
926 data
->CurrentSC
= NULL
;
927 SCpnt
->result
= DID_NO_CONNECT
<< 16;
932 /* check target ID is not same as this initiator ID */
933 if (scmd_id(SCpnt
) == SCpnt
->device
->host
->this_id
) {
934 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
, "terget==host???");
935 SCpnt
->result
= DID_BAD_TARGET
<< 16;
940 /* check target LUN is allowable value */
941 if (SCpnt
->device
->lun
>= MAX_LUN
) {
942 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
, "no more lun");
943 SCpnt
->result
= DID_BAD_TARGET
<< 16;
950 SCpnt
->scsi_done
= done
;
951 data
->CurrentSC
= SCpnt
;
952 SCpnt
->SCp
.Status
= CHECK_CONDITION
;
953 SCpnt
->SCp
.Message
= 0;
954 scsi_set_resid(SCpnt
, scsi_bufflen(SCpnt
));
956 SCpnt
->SCp
.ptr
= (char *)scsi_sglist(SCpnt
);
957 SCpnt
->SCp
.this_residual
= scsi_bufflen(SCpnt
);
958 SCpnt
->SCp
.buffer
= NULL
;
959 SCpnt
->SCp
.buffers_residual
= 0;
961 /* initialize data */
962 data
->msgout_len
= 0;
964 cur_lunt
= &(data
->lunt
[SCpnt
->device
->id
][SCpnt
->device
->lun
]);
965 cur_lunt
->SCpnt
= SCpnt
;
966 cur_lunt
->save_datp
= 0;
967 cur_lunt
->msgin03
= FALSE
;
968 data
->cur_lunt
= cur_lunt
;
969 data
->cur_id
= SCpnt
->device
->id
;
970 data
->cur_lun
= SCpnt
->device
->lun
;
972 ret
= nsp32_setup_sg_table(SCpnt
);
974 nsp32_msg(KERN_ERR
, "SGT fail");
975 SCpnt
->result
= DID_ERROR
<< 16;
976 nsp32_scsi_done(SCpnt
);
981 nsp32_build_identify(SCpnt
);
984 * If target is the first time to transfer after the reset
985 * (target don't have SDTR_DONE and SDTR_INITIATOR), sync
986 * message SDTR is needed to do synchronous transfer.
988 target
= &data
->target
[scmd_id(SCpnt
)];
989 data
->cur_target
= target
;
991 if (!(target
->sync_flag
& (SDTR_DONE
| SDTR_INITIATOR
| SDTR_TARGET
))) {
992 unsigned char period
, offset
;
994 if (trans_mode
!= ASYNC_MODE
) {
995 nsp32_set_max_sync(data
, target
, &period
, &offset
);
996 nsp32_build_sdtr(SCpnt
, period
, offset
);
997 target
->sync_flag
|= SDTR_INITIATOR
;
999 nsp32_set_async(data
, target
);
1000 target
->sync_flag
|= SDTR_DONE
;
1003 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
1004 "SDTR: entry: %d start_period: 0x%x offset: 0x%x\n",
1005 target
->limit_entry
, period
, offset
);
1006 } else if (target
->sync_flag
& SDTR_INITIATOR
) {
1008 * It was negotiating SDTR with target, sending from the
1009 * initiator, but there are no chance to remove this flag.
1010 * Set async because we don't get proper negotiation.
1012 nsp32_set_async(data
, target
);
1013 target
->sync_flag
&= ~SDTR_INITIATOR
;
1014 target
->sync_flag
|= SDTR_DONE
;
1016 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
1017 "SDTR_INITIATOR: fall back to async");
1018 } else if (target
->sync_flag
& SDTR_TARGET
) {
1020 * It was negotiating SDTR with target, sending from target,
1021 * but there are no chance to remove this flag. Set async
1022 * because we don't get proper negotiation.
1024 nsp32_set_async(data
, target
);
1025 target
->sync_flag
&= ~SDTR_TARGET
;
1026 target
->sync_flag
|= SDTR_DONE
;
1028 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
,
1029 "Unknown SDTR from target is reached, fall back to async.");
1032 nsp32_dbg(NSP32_DEBUG_TARGETFLAG
,
1033 "target: %d sync_flag: 0x%x syncreg: 0x%x ackwidth: 0x%x",
1034 SCpnt
->device
->id
, target
->sync_flag
, target
->syncreg
,
1038 if (auto_param
== 0) {
1039 ret
= nsp32_selection_autopara(SCpnt
);
1041 ret
= nsp32_selection_autoscsi(SCpnt
);
1045 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND
, "selection fail");
1046 nsp32_scsi_done(SCpnt
);
1052 static DEF_SCSI_QCMD(nsp32_queuecommand
)
1054 /* initialize asic */
1055 static int nsp32hw_init(nsp32_hw_data
*data
)
1057 unsigned int base
= data
->BaseAddress
;
1058 unsigned short irq_stat
;
1059 unsigned long lc_reg
;
1060 unsigned char power
;
1062 lc_reg
= nsp32_index_read4(base
, CFG_LATE_CACHE
);
1063 if ((lc_reg
& 0xff00) == 0) {
1064 lc_reg
|= (0x20 << 8);
1065 nsp32_index_write2(base
, CFG_LATE_CACHE
, lc_reg
& 0xffff);
1068 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
1069 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
1070 nsp32_write4(base
, BM_CNT
, 0);
1071 nsp32_write2(base
, SCSI_EXECUTE_PHASE
, 0);
1074 irq_stat
= nsp32_read2(base
, IRQ_STATUS
);
1075 nsp32_dbg(NSP32_DEBUG_INIT
, "irq_stat 0x%x", irq_stat
);
1076 } while (irq_stat
& IRQSTATUS_ANY_IRQ
);
1079 * Fill FIFO_FULL_SHLD, FIFO_EMPTY_SHLD. Below parameter is
1080 * designated by specification.
1082 if ((data
->trans_method
& NSP32_TRANSFER_PIO
) ||
1083 (data
->trans_method
& NSP32_TRANSFER_MMIO
)) {
1084 nsp32_index_write1(base
, FIFO_FULL_SHLD_COUNT
, 0x40);
1085 nsp32_index_write1(base
, FIFO_EMPTY_SHLD_COUNT
, 0x40);
1086 } else if (data
->trans_method
& NSP32_TRANSFER_BUSMASTER
) {
1087 nsp32_index_write1(base
, FIFO_FULL_SHLD_COUNT
, 0x10);
1088 nsp32_index_write1(base
, FIFO_EMPTY_SHLD_COUNT
, 0x60);
1090 nsp32_dbg(NSP32_DEBUG_INIT
, "unknown transfer mode");
1093 nsp32_dbg(NSP32_DEBUG_INIT
, "full 0x%x emp 0x%x",
1094 nsp32_index_read1(base
, FIFO_FULL_SHLD_COUNT
),
1095 nsp32_index_read1(base
, FIFO_EMPTY_SHLD_COUNT
));
1097 nsp32_index_write1(base
, CLOCK_DIV
, data
->clock
);
1098 nsp32_index_write1(base
, BM_CYCLE
, MEMRD_CMD1
| SGT_AUTO_PARA_MEMED_CMD
);
1099 nsp32_write1(base
, PARITY_CONTROL
, 0); /* parity check is disable */
1102 * initialize MISC_WRRD register
1104 * Note: Designated parameters is obeyed as following:
1105 * MISC_SCSI_DIRECTION_DETECTOR_SELECT: It must be set.
1106 * MISC_MASTER_TERMINATION_SELECT: It must be set.
1107 * MISC_BMREQ_NEGATE_TIMING_SEL: It should be set.
1108 * MISC_AUTOSEL_TIMING_SEL: It should be set.
1109 * MISC_BMSTOP_CHANGE2_NONDATA_PHASE: It should be set.
1110 * MISC_DELAYED_BMSTART: It's selected for safety.
1112 * Note: If MISC_BMSTOP_CHANGE2_NONDATA_PHASE is set, then
1113 * we have to set TRANSFERCONTROL_BM_START as 0 and set
1114 * appropriate value before restarting bus master transfer.
1116 nsp32_index_write2(base
, MISC_WR
,
1117 (SCSI_DIRECTION_DETECTOR_SELECT
|
1119 MASTER_TERMINATION_SELECT
|
1120 BMREQ_NEGATE_TIMING_SEL
|
1121 AUTOSEL_TIMING_SEL
|
1122 BMSTOP_CHANGE2_NONDATA_PHASE
));
1124 nsp32_index_write1(base
, TERM_PWR_CONTROL
, 0);
1125 power
= nsp32_index_read1(base
, TERM_PWR_CONTROL
);
1126 if (!(power
& SENSE
)) {
1127 nsp32_msg(KERN_INFO
, "term power on");
1128 nsp32_index_write1(base
, TERM_PWR_CONTROL
, BPWR
);
1131 nsp32_write2(base
, TIMER_SET
, TIMER_STOP
);
1132 nsp32_write2(base
, TIMER_SET
, TIMER_STOP
); /* Required 2 times */
1134 nsp32_write1(base
, SYNC_REG
, 0);
1135 nsp32_write1(base
, ACK_WIDTH
, 0);
1136 nsp32_write2(base
, SEL_TIME_OUT
, SEL_TIMEOUT_TIME
);
1139 * enable to select designated IRQ (except for
1140 * IRQSELECT_SERR, IRQSELECT_PERR, IRQSELECT_BMCNTERR)
1142 nsp32_index_write2(base
, IRQ_SELECT
, IRQSELECT_TIMER_IRQ
|
1143 IRQSELECT_SCSIRESET_IRQ
|
1144 IRQSELECT_FIFO_SHLD_IRQ
|
1145 IRQSELECT_RESELECT_IRQ
|
1146 IRQSELECT_PHASE_CHANGE_IRQ
|
1147 IRQSELECT_AUTO_SCSI_SEQ_IRQ
|
1148 // IRQSELECT_BMCNTERR_IRQ |
1149 IRQSELECT_TARGET_ABORT_IRQ
|
1150 IRQSELECT_MASTER_ABORT_IRQ
);
1151 nsp32_write2(base
, IRQ_CONTROL
, 0);
1154 nsp32_index_write1(base
, EXT_PORT_DDR
, LED_OFF
);
1155 nsp32_index_write1(base
, EXT_PORT
, LED_OFF
);
1161 /* interrupt routine */
1162 static irqreturn_t
do_nsp32_isr(int irq
, void *dev_id
)
1164 nsp32_hw_data
*data
= dev_id
;
1165 unsigned int base
= data
->BaseAddress
;
1166 struct scsi_cmnd
*SCpnt
= data
->CurrentSC
;
1167 unsigned short auto_stat
, irq_stat
, trans_stat
;
1168 unsigned char busmon
, busphase
;
1169 unsigned long flags
;
1172 struct Scsi_Host
*host
= data
->Host
;
1174 spin_lock_irqsave(host
->host_lock
, flags
);
1177 * IRQ check, then enable IRQ mask
1179 irq_stat
= nsp32_read2(base
, IRQ_STATUS
);
1180 nsp32_dbg(NSP32_DEBUG_INTR
,
1181 "enter IRQ: %d, IRQstatus: 0x%x", irq
, irq_stat
);
1182 /* is this interrupt comes from Ninja asic? */
1183 if ((irq_stat
& IRQSTATUS_ANY_IRQ
) == 0) {
1184 nsp32_dbg(NSP32_DEBUG_INTR
, "shared interrupt: irq other 0x%x", irq_stat
);
1188 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
1190 busmon
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
1191 busphase
= busmon
& BUSMON_PHASE_MASK
;
1193 trans_stat
= nsp32_read2(base
, TRANSFER_STATUS
);
1194 if ((irq_stat
== 0xffff) && (trans_stat
== 0xffff)) {
1195 nsp32_msg(KERN_INFO
, "card disconnect");
1196 if (data
->CurrentSC
!= NULL
) {
1197 nsp32_msg(KERN_INFO
, "clean up current SCSI command");
1198 SCpnt
->result
= DID_BAD_TARGET
<< 16;
1199 nsp32_scsi_done(SCpnt
);
1205 if (irq_stat
& IRQSTATUS_TIMER_IRQ
) {
1206 nsp32_dbg(NSP32_DEBUG_INTR
, "timer stop");
1207 nsp32_write2(base
, TIMER_SET
, TIMER_STOP
);
1212 if (irq_stat
& IRQSTATUS_SCSIRESET_IRQ
) {
1213 nsp32_msg(KERN_INFO
, "detected someone do bus reset");
1214 nsp32_do_bus_reset(data
);
1215 if (SCpnt
!= NULL
) {
1216 SCpnt
->result
= DID_RESET
<< 16;
1217 nsp32_scsi_done(SCpnt
);
1222 if (SCpnt
== NULL
) {
1223 nsp32_msg(KERN_WARNING
, "SCpnt==NULL this can't be happened");
1224 nsp32_msg(KERN_WARNING
, "irq_stat=0x%x trans_stat=0x%x", irq_stat
, trans_stat
);
1229 * AutoSCSI Interrupt.
1230 * Note: This interrupt is occurred when AutoSCSI is finished. Then
1231 * check SCSIEXECUTEPHASE, and do appropriate action. Each phases are
1232 * recorded when AutoSCSI sequencer has been processed.
1234 if(irq_stat
& IRQSTATUS_AUTOSCSI_IRQ
) {
1235 /* getting SCSI executed phase */
1236 auto_stat
= nsp32_read2(base
, SCSI_EXECUTE_PHASE
);
1237 nsp32_write2(base
, SCSI_EXECUTE_PHASE
, 0);
1239 /* Selection Timeout, go busfree phase. */
1240 if (auto_stat
& SELECTION_TIMEOUT
) {
1241 nsp32_dbg(NSP32_DEBUG_INTR
,
1242 "selection timeout occurred");
1244 SCpnt
->result
= DID_TIME_OUT
<< 16;
1245 nsp32_scsi_done(SCpnt
);
1249 if (auto_stat
& MSGOUT_PHASE
) {
1251 * MsgOut phase was processed.
1252 * If MSG_IN_OCCUER is not set, then MsgOut phase is
1253 * completed. Thus, msgout_len must reset. Otherwise,
1254 * nothing to do here. If MSG_OUT_OCCUER is occurred,
1255 * then we will encounter the condition and check.
1257 if (!(auto_stat
& MSG_IN_OCCUER
) &&
1258 (data
->msgout_len
<= 3)) {
1260 * !MSG_IN_OCCUER && msgout_len <=3
1261 * ---> AutoSCSI with MSGOUTreg is processed.
1263 data
->msgout_len
= 0;
1266 nsp32_dbg(NSP32_DEBUG_INTR
, "MsgOut phase processed");
1269 if ((auto_stat
& DATA_IN_PHASE
) &&
1270 (scsi_get_resid(SCpnt
) > 0) &&
1271 ((nsp32_read2(base
, FIFO_REST_CNT
) & FIFO_REST_MASK
) != 0)) {
1272 printk( "auto+fifo\n");
1273 //nsp32_pio_read(SCpnt);
1276 if (auto_stat
& (DATA_IN_PHASE
| DATA_OUT_PHASE
)) {
1277 /* DATA_IN_PHASE/DATA_OUT_PHASE was processed. */
1278 nsp32_dbg(NSP32_DEBUG_INTR
,
1279 "Data in/out phase processed");
1281 /* read BMCNT, SGT pointer addr */
1282 nsp32_dbg(NSP32_DEBUG_INTR
, "BMCNT=0x%lx",
1283 nsp32_read4(base
, BM_CNT
));
1284 nsp32_dbg(NSP32_DEBUG_INTR
, "addr=0x%lx",
1285 nsp32_read4(base
, SGT_ADR
));
1286 nsp32_dbg(NSP32_DEBUG_INTR
, "SACK=0x%lx",
1287 nsp32_read4(base
, SACK_CNT
));
1288 nsp32_dbg(NSP32_DEBUG_INTR
, "SSACK=0x%lx",
1289 nsp32_read4(base
, SAVED_SACK_CNT
));
1291 scsi_set_resid(SCpnt
, 0); /* all data transferred! */
1297 if (auto_stat
& MSG_IN_OCCUER
) {
1298 nsp32_msgin_occur(SCpnt
, irq_stat
, auto_stat
);
1304 if (auto_stat
& MSG_OUT_OCCUER
) {
1305 nsp32_msgout_occur(SCpnt
);
1311 if (auto_stat
& BUS_FREE_OCCUER
) {
1312 ret
= nsp32_busfree_occur(SCpnt
, auto_stat
);
1318 if (auto_stat
& STATUS_PHASE
) {
1320 * Read CSB and substitute CSB for SCpnt->result
1321 * to save status phase stutas byte.
1322 * scsi error handler checks host_byte (DID_*:
1323 * low level driver to indicate status), then checks
1324 * status_byte (SCSI status byte).
1326 SCpnt
->result
= (int)nsp32_read1(base
, SCSI_CSB_IN
);
1329 if (auto_stat
& ILLEGAL_PHASE
) {
1330 /* Illegal phase is detected. SACK is not back. */
1331 nsp32_msg(KERN_WARNING
,
1332 "AUTO SCSI ILLEGAL PHASE OCCUR!!!!");
1334 /* TODO: currently we don't have any action... bus reset? */
1337 * To send back SACK, assert, wait, and negate.
1339 nsp32_sack_assert(data
);
1340 nsp32_wait_req(data
, NEGATE
);
1341 nsp32_sack_negate(data
);
1345 if (auto_stat
& COMMAND_PHASE
) {
1347 nsp32_dbg(NSP32_DEBUG_INTR
, "Command phase processed");
1350 if (auto_stat
& AUTOSCSI_BUSY
) {
1351 /* AutoSCSI is running */
1354 show_autophase(auto_stat
);
1358 if (irq_stat
& IRQSTATUS_FIFO_SHLD_IRQ
) {
1359 nsp32_dbg(NSP32_DEBUG_INTR
, "FIFO IRQ");
1362 case BUSPHASE_DATA_OUT
:
1363 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/write");
1365 //nsp32_pio_write(SCpnt);
1369 case BUSPHASE_DATA_IN
:
1370 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/read");
1372 //nsp32_pio_read(SCpnt);
1376 case BUSPHASE_STATUS
:
1377 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/status");
1379 SCpnt
->SCp
.Status
= nsp32_read1(base
, SCSI_CSB_IN
);
1383 nsp32_dbg(NSP32_DEBUG_INTR
, "fifo/other phase");
1384 nsp32_dbg(NSP32_DEBUG_INTR
, "irq_stat=0x%x trans_stat=0x%x", irq_stat
, trans_stat
);
1385 show_busphase(busphase
);
1392 /* Phase Change IRQ */
1393 if (irq_stat
& IRQSTATUS_PHASE_CHANGE_IRQ
) {
1394 nsp32_dbg(NSP32_DEBUG_INTR
, "phase change IRQ");
1397 case BUSPHASE_MESSAGE_IN
:
1398 nsp32_dbg(NSP32_DEBUG_INTR
, "phase chg/msg in");
1399 nsp32_msgin_occur(SCpnt
, irq_stat
, 0);
1402 nsp32_msg(KERN_WARNING
, "phase chg/other phase?");
1403 nsp32_msg(KERN_WARNING
, "irq_stat=0x%x trans_stat=0x%x\n",
1404 irq_stat
, trans_stat
);
1405 show_busphase(busphase
);
1412 if (irq_stat
& IRQSTATUS_PCI_IRQ
) {
1413 nsp32_dbg(NSP32_DEBUG_INTR
, "PCI IRQ occurred");
1418 if (irq_stat
& IRQSTATUS_BMCNTERR_IRQ
) {
1419 nsp32_msg(KERN_ERR
, "Received unexpected BMCNTERR IRQ! ");
1421 * TODO: To be implemented improving bus master
1422 * transfer reliability when BMCNTERR is occurred in
1423 * AutoSCSI phase described in specification.
1428 nsp32_dbg(NSP32_DEBUG_INTR
,
1429 "irq_stat=0x%x trans_stat=0x%x", irq_stat
, trans_stat
);
1430 show_busphase(busphase
);
1434 /* disable IRQ mask */
1435 nsp32_write2(base
, IRQ_CONTROL
, 0);
1438 spin_unlock_irqrestore(host
->host_lock
, flags
);
1440 nsp32_dbg(NSP32_DEBUG_INTR
, "exit");
1442 return IRQ_RETVAL(handled
);
1446 #define SPRINTF(args...) \
1448 if(length > (pos - buffer)) { \
1449 pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
1450 nsp32_dbg(NSP32_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length, length - (pos - buffer));\
1454 static int nsp32_proc_info(struct Scsi_Host
*host
, char *buffer
, char **start
,
1455 off_t offset
, int length
, int inout
)
1459 unsigned long flags
;
1460 nsp32_hw_data
*data
;
1463 unsigned char mode_reg
;
1467 /* Write is not supported, just return. */
1468 if (inout
== TRUE
) {
1472 hostno
= host
->host_no
;
1473 data
= (nsp32_hw_data
*)host
->hostdata
;
1474 base
= host
->io_port
;
1476 SPRINTF("NinjaSCSI-32 status\n\n");
1477 SPRINTF("Driver version: %s, $Revision: 1.33 $\n", nsp32_release_version
);
1478 SPRINTF("SCSI host No.: %d\n", hostno
);
1479 SPRINTF("IRQ: %d\n", host
->irq
);
1480 SPRINTF("IO: 0x%lx-0x%lx\n", host
->io_port
, host
->io_port
+ host
->n_io_port
- 1);
1481 SPRINTF("MMIO(virtual address): 0x%lx-0x%lx\n", host
->base
, host
->base
+ data
->MmioLength
- 1);
1482 SPRINTF("sg_tablesize: %d\n", host
->sg_tablesize
);
1483 SPRINTF("Chip revision: 0x%x\n", (nsp32_read2(base
, INDEX_REG
) >> 8) & 0xff);
1485 mode_reg
= nsp32_index_read1(base
, CHIP_MODE
);
1486 model
= data
->pci_devid
->driver_data
;
1489 SPRINTF("Power Management: %s\n", (mode_reg
& OPTF
) ? "yes" : "no");
1491 SPRINTF("OEM: %ld, %s\n", (mode_reg
& (OEM0
|OEM1
)), nsp32_model
[model
]);
1493 spin_lock_irqsave(&(data
->Lock
), flags
);
1494 SPRINTF("CurrentSC: 0x%p\n\n", data
->CurrentSC
);
1495 spin_unlock_irqrestore(&(data
->Lock
), flags
);
1498 SPRINTF("SDTR status\n");
1499 for (id
= 0; id
< ARRAY_SIZE(data
->target
); id
++) {
1501 SPRINTF("id %d: ", id
);
1503 if (id
== host
->this_id
) {
1504 SPRINTF("----- NinjaSCSI-32 host adapter\n");
1508 if (data
->target
[id
].sync_flag
== SDTR_DONE
) {
1509 if (data
->target
[id
].period
== 0 &&
1510 data
->target
[id
].offset
== ASYNC_OFFSET
) {
1519 if (data
->target
[id
].period
!= 0) {
1521 speed
= 1000000 / (data
->target
[id
].period
* 4);
1523 SPRINTF(" transfer %d.%dMB/s, offset %d",
1526 data
->target
[id
].offset
1533 thislength
= pos
- (buffer
+ offset
);
1535 if(thislength
< 0) {
1541 thislength
= min(thislength
, length
);
1542 *start
= buffer
+ offset
;
1551 * Reset parameters and call scsi_done for data->cur_lunt.
1552 * Be careful setting SCpnt->result = DID_* before calling this function.
1554 static void nsp32_scsi_done(struct scsi_cmnd
*SCpnt
)
1556 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1557 unsigned int base
= SCpnt
->device
->host
->io_port
;
1559 scsi_dma_unmap(SCpnt
);
1562 * clear TRANSFERCONTROL_BM_START
1564 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
1565 nsp32_write4(base
, BM_CNT
, 0);
1570 (*SCpnt
->scsi_done
)(SCpnt
);
1575 data
->cur_lunt
->SCpnt
= NULL
;
1576 data
->cur_lunt
= NULL
;
1577 data
->cur_target
= NULL
;
1578 data
->CurrentSC
= NULL
;
1585 * Current Phase is BUSFREE. AutoSCSI is automatically execute BUSFREE phase
1586 * with ACK reply when below condition is matched:
1587 * MsgIn 00: Command Complete.
1588 * MsgIn 02: Save Data Pointer.
1589 * MsgIn 04: Diconnect.
1590 * In other case, unexpected BUSFREE is detected.
1592 static int nsp32_busfree_occur(struct scsi_cmnd
*SCpnt
, unsigned short execph
)
1594 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1595 unsigned int base
= SCpnt
->device
->host
->io_port
;
1597 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "enter execph=0x%x", execph
);
1598 show_autophase(execph
);
1600 nsp32_write4(base
, BM_CNT
, 0);
1601 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
1604 * MsgIn 02: Save Data Pointer
1607 * Save Data Pointer is received. Adjust pointer.
1610 * SCSI-3 says if Save Data Pointer is not received, then we restart
1611 * processing and we can't adjust any SCSI data pointer in next data
1614 if (execph
& MSGIN_02_VALID
) {
1615 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "MsgIn02_Valid");
1618 * Check sack_cnt/saved_sack_cnt, then adjust sg table if
1621 if (!(execph
& MSGIN_00_VALID
) &&
1622 ((execph
& DATA_IN_PHASE
) || (execph
& DATA_OUT_PHASE
))) {
1623 unsigned int sacklen
, s_sacklen
;
1626 * Read SACK count and SAVEDSACK count, then compare.
1628 sacklen
= nsp32_read4(base
, SACK_CNT
);
1629 s_sacklen
= nsp32_read4(base
, SAVED_SACK_CNT
);
1632 * If SAVEDSACKCNT == 0, it means SavedDataPointer is
1633 * come after data transferring.
1635 if (s_sacklen
> 0) {
1637 * Comparing between sack and savedsack to
1638 * check the condition of AutoMsgIn03.
1640 * If they are same, set msgin03 == TRUE,
1641 * COMMANDCONTROL_AUTO_MSGIN_03 is enabled at
1642 * reselection. On the other hand, if they
1643 * aren't same, set msgin03 == FALSE, and
1644 * COMMANDCONTROL_AUTO_MSGIN_03 is disabled at
1647 if (sacklen
!= s_sacklen
) {
1648 data
->cur_lunt
->msgin03
= FALSE
;
1650 data
->cur_lunt
->msgin03
= TRUE
;
1653 nsp32_adjust_busfree(SCpnt
, s_sacklen
);
1657 /* This value has not substitude with valid value yet... */
1658 //data->cur_lunt->save_datp = data->cur_datp;
1665 if (execph
& MSGIN_03_VALID
) {
1666 /* MsgIn03 was valid to be processed. No need processing. */
1672 if (data
->cur_target
->sync_flag
& SDTR_INITIATOR
) {
1674 * SDTR negotiation pulled by the initiator has not
1675 * finished yet. Fall back to ASYNC mode.
1677 nsp32_set_async(data
, data
->cur_target
);
1678 data
->cur_target
->sync_flag
&= ~SDTR_INITIATOR
;
1679 data
->cur_target
->sync_flag
|= SDTR_DONE
;
1680 } else if (data
->cur_target
->sync_flag
& SDTR_TARGET
) {
1682 * SDTR negotiation pulled by the target has been
1685 if (execph
& (MSGIN_00_VALID
| MSGIN_04_VALID
)) {
1687 * If valid message is received, then
1688 * negotiation is succeeded.
1692 * On the contrary, if unexpected bus free is
1693 * occurred, then negotiation is failed. Fall
1694 * back to ASYNC mode.
1696 nsp32_set_async(data
, data
->cur_target
);
1698 data
->cur_target
->sync_flag
&= ~SDTR_TARGET
;
1699 data
->cur_target
->sync_flag
|= SDTR_DONE
;
1703 * It is always ensured by SCSI standard that initiator
1704 * switches into Bus Free Phase after
1705 * receiving message 00 (Command Complete), 04 (Disconnect).
1706 * It's the reason that processing here is valid.
1708 if (execph
& MSGIN_00_VALID
) {
1709 /* MsgIn 00: Command Complete */
1710 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "command complete");
1712 SCpnt
->SCp
.Status
= nsp32_read1(base
, SCSI_CSB_IN
);
1713 SCpnt
->SCp
.Message
= 0;
1714 nsp32_dbg(NSP32_DEBUG_BUSFREE
,
1715 "normal end stat=0x%x resid=0x%x\n",
1716 SCpnt
->SCp
.Status
, scsi_get_resid(SCpnt
));
1717 SCpnt
->result
= (DID_OK
<< 16) |
1718 (SCpnt
->SCp
.Message
<< 8) |
1719 (SCpnt
->SCp
.Status
<< 0);
1720 nsp32_scsi_done(SCpnt
);
1721 /* All operation is done */
1723 } else if (execph
& MSGIN_04_VALID
) {
1724 /* MsgIn 04: Disconnect */
1725 SCpnt
->SCp
.Status
= nsp32_read1(base
, SCSI_CSB_IN
);
1726 SCpnt
->SCp
.Message
= 4;
1728 nsp32_dbg(NSP32_DEBUG_BUSFREE
, "disconnect");
1731 /* Unexpected bus free */
1732 nsp32_msg(KERN_WARNING
, "unexpected bus free occurred");
1735 //SCpnt->result = (DID_OK << 16) | (SCpnt->SCp.Message << 8) | (SCpnt->SCp.Status << 0);
1736 SCpnt
->result
= DID_ERROR
<< 16;
1737 nsp32_scsi_done(SCpnt
);
1745 * nsp32_adjust_busfree - adjusting SG table
1747 * Note: This driver adjust the SG table using SCSI ACK
1748 * counter instead of BMCNT counter!
1750 static void nsp32_adjust_busfree(struct scsi_cmnd
*SCpnt
, unsigned int s_sacklen
)
1752 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1753 int old_entry
= data
->cur_entry
;
1755 int sg_num
= data
->cur_lunt
->sg_num
;
1756 nsp32_sgtable
*sgt
= data
->cur_lunt
->sglun
->sgt
;
1757 unsigned int restlen
, sentlen
;
1760 nsp32_dbg(NSP32_DEBUG_SGLIST
, "old resid=0x%x", scsi_get_resid(SCpnt
));
1762 /* adjust saved SACK count with 4 byte start address boundary */
1763 s_sacklen
-= le32_to_cpu(sgt
[old_entry
].addr
) & 3;
1766 * calculate new_entry from sack count and each sgt[].len
1767 * calculate the byte which is intent to send
1770 for (new_entry
= old_entry
; new_entry
< sg_num
; new_entry
++) {
1771 sentlen
+= (le32_to_cpu(sgt
[new_entry
].len
) & ~SGTEND
);
1772 if (sentlen
> s_sacklen
) {
1777 /* all sgt is processed */
1778 if (new_entry
== sg_num
) {
1782 if (sentlen
== s_sacklen
) {
1783 /* XXX: confirm it's ok or not */
1784 /* In this case, it's ok because we are at
1785 the head element of the sg. restlen is correctly calculated. */
1788 /* calculate the rest length for transferring */
1789 restlen
= sentlen
- s_sacklen
;
1791 /* update adjusting current SG table entry */
1792 len
= le32_to_cpu(sgt
[new_entry
].len
);
1793 addr
= le32_to_cpu(sgt
[new_entry
].addr
);
1794 addr
+= (len
- restlen
);
1795 sgt
[new_entry
].addr
= cpu_to_le32(addr
);
1796 sgt
[new_entry
].len
= cpu_to_le32(restlen
);
1798 /* set cur_entry with new_entry */
1799 data
->cur_entry
= new_entry
;
1804 if (scsi_get_resid(SCpnt
) < sentlen
) {
1805 nsp32_msg(KERN_ERR
, "resid underflow");
1808 scsi_set_resid(SCpnt
, scsi_get_resid(SCpnt
) - sentlen
);
1809 nsp32_dbg(NSP32_DEBUG_SGLIST
, "new resid=0x%x", scsi_get_resid(SCpnt
));
1811 /* update hostdata and lun */
1818 * It's called MsgOut phase occur.
1819 * NinjaSCSI-32Bi/UDE automatically processes up to 3 messages in
1820 * message out phase. It, however, has more than 3 messages,
1821 * HBA creates the interrupt and we have to process by hand.
1823 static void nsp32_msgout_occur(struct scsi_cmnd
*SCpnt
)
1825 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1826 unsigned int base
= SCpnt
->device
->host
->io_port
;
1827 //unsigned short command;
1831 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
,
1832 "enter: msgout_len: 0x%x", data
->msgout_len
);
1835 * If MsgOut phase is occurred without having any
1836 * message, then No_Operation is sent (SCSI-2).
1838 if (data
->msgout_len
== 0) {
1839 nsp32_build_nop(SCpnt
);
1843 * Set SGTP ADDR current entry for restarting AUTOSCSI,
1844 * because SGTP is incremented next point.
1845 * There is few statement in the specification...
1847 new_sgtp
= data
->cur_lunt
->sglun_paddr
+
1848 (data
->cur_lunt
->cur_entry
* sizeof(nsp32_sgtable
));
1853 for (i
= 0; i
< data
->msgout_len
; i
++) {
1854 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
,
1855 "%d : 0x%x", i
, data
->msgoutbuf
[i
]);
1858 * Check REQ is asserted.
1860 nsp32_wait_req(data
, ASSERT
);
1862 if (i
== (data
->msgout_len
- 1)) {
1864 * If the last message, set the AutoSCSI restart
1865 * before send back the ack message. AutoSCSI
1866 * restart automatically negate ATN signal.
1868 //command = (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
1869 //nsp32_restart_autoscsi(SCpnt, command);
1870 nsp32_write2(base
, COMMAND_CONTROL
,
1871 (CLEAR_CDB_FIFO_POINTER
|
1872 AUTO_COMMAND_PHASE
|
1874 AUTO_MSGIN_00_OR_04
|
1878 * Write data with SACK, then wait sack is
1879 * automatically negated.
1881 nsp32_write1(base
, SCSI_DATA_WITH_ACK
, data
->msgoutbuf
[i
]);
1882 nsp32_wait_sack(data
, NEGATE
);
1884 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
, "bus: 0x%x\n",
1885 nsp32_read1(base
, SCSI_BUS_MONITOR
));
1888 data
->msgout_len
= 0;
1890 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR
, "exit");
1896 * Note: Restarting AutoSCSI needs set:
1897 * SYNC_REG, ACK_WIDTH, SGT_ADR, TRANSFER_CONTROL
1899 static void nsp32_restart_autoscsi(struct scsi_cmnd
*SCpnt
, unsigned short command
)
1901 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1902 unsigned int base
= data
->BaseAddress
;
1903 unsigned short transfer
= 0;
1905 nsp32_dbg(NSP32_DEBUG_RESTART
, "enter");
1907 if (data
->cur_target
== NULL
|| data
->cur_lunt
== NULL
) {
1908 nsp32_msg(KERN_ERR
, "Target or Lun is invalid");
1913 * Don't set BM_START_ADR before setting this register.
1915 nsp32_write1(base
, SYNC_REG
, data
->cur_target
->syncreg
);
1920 nsp32_write1(base
, ACK_WIDTH
, data
->cur_target
->ackwidth
);
1923 * set SREQ hazard killer sampling rate
1925 nsp32_write1(base
, SREQ_SMPL_RATE
, data
->cur_target
->sample_reg
);
1928 * set SGT ADDR (physical address)
1930 nsp32_write4(base
, SGT_ADR
, data
->cur_lunt
->sglun_paddr
);
1933 * set TRANSFER CONTROL REG
1936 transfer
|= (TRANSFER_GO
| ALL_COUNTER_CLR
);
1937 if (data
->trans_method
& NSP32_TRANSFER_BUSMASTER
) {
1938 if (scsi_bufflen(SCpnt
) > 0) {
1939 transfer
|= BM_START
;
1941 } else if (data
->trans_method
& NSP32_TRANSFER_MMIO
) {
1942 transfer
|= CB_MMIO_MODE
;
1943 } else if (data
->trans_method
& NSP32_TRANSFER_PIO
) {
1944 transfer
|= CB_IO_MODE
;
1946 nsp32_write2(base
, TRANSFER_CONTROL
, transfer
);
1951 * TODO: COMMANDCONTROL_AUTO_COMMAND_PHASE is needed ?
1953 command
|= (CLEAR_CDB_FIFO_POINTER
|
1954 AUTO_COMMAND_PHASE
|
1956 nsp32_write2(base
, COMMAND_CONTROL
, command
);
1958 nsp32_dbg(NSP32_DEBUG_RESTART
, "exit");
1963 * cannot run automatically message in occur
1965 static void nsp32_msgin_occur(struct scsi_cmnd
*SCpnt
,
1966 unsigned long irq_status
,
1967 unsigned short execph
)
1969 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
1970 unsigned int base
= SCpnt
->device
->host
->io_port
;
1972 unsigned char msgtype
;
1973 unsigned char newlun
;
1974 unsigned short command
= 0;
1975 int msgclear
= TRUE
;
1980 * read first message
1981 * Use SCSIDATA_W_ACK instead of SCSIDATAIN, because the procedure
1982 * of Message-In have to be processed before sending back SCSI ACK.
1984 msg
= nsp32_read1(base
, SCSI_DATA_IN
);
1985 data
->msginbuf
[(unsigned char)data
->msgin_len
] = msg
;
1986 msgtype
= data
->msginbuf
[0];
1987 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
,
1988 "enter: msglen: 0x%x msgin: 0x%x msgtype: 0x%x",
1989 data
->msgin_len
, msg
, msgtype
);
1992 * TODO: We need checking whether bus phase is message in?
1998 nsp32_sack_assert(data
);
2001 * processing IDENTIFY
2003 if (msgtype
& 0x80) {
2004 if (!(irq_status
& IRQSTATUS_RESELECT_OCCUER
)) {
2005 /* Invalid (non reselect) phase */
2009 newlun
= msgtype
& 0x1f; /* TODO: SPI-3 compliant? */
2010 ret
= nsp32_reselection(SCpnt
, newlun
);
2019 * processing messages except for IDENTIFY
2021 * TODO: Messages are all SCSI-2 terminology. SCSI-3 compliance is TODO.
2027 case COMMAND_COMPLETE
:
2030 * These messages should not be occurred.
2031 * They should be processed on AutoSCSI sequencer.
2033 nsp32_msg(KERN_WARNING
,
2034 "unexpected message of AutoSCSI MsgIn: 0x%x", msg
);
2037 case RESTORE_POINTERS
:
2039 * AutoMsgIn03 is disabled, and HBA gets this message.
2042 if ((execph
& DATA_IN_PHASE
) || (execph
& DATA_OUT_PHASE
)) {
2043 unsigned int s_sacklen
;
2045 s_sacklen
= nsp32_read4(base
, SAVED_SACK_CNT
);
2046 if ((execph
& MSGIN_02_VALID
) && (s_sacklen
> 0)) {
2047 nsp32_adjust_busfree(SCpnt
, s_sacklen
);
2049 /* No need to rewrite SGT */
2052 data
->cur_lunt
->msgin03
= FALSE
;
2054 /* Update with the new value */
2056 /* reset SACK/SavedACK counter (or ALL clear?) */
2057 nsp32_write4(base
, CLR_COUNTER
, CLRCOUNTER_ALLMASK
);
2060 * set new sg pointer
2062 new_sgtp
= data
->cur_lunt
->sglun_paddr
+
2063 (data
->cur_lunt
->cur_entry
* sizeof(nsp32_sgtable
));
2064 nsp32_write4(base
, SGT_ADR
, new_sgtp
);
2070 * These messages should not be occurred.
2071 * They should be processed on AutoSCSI sequencer.
2073 nsp32_msg (KERN_WARNING
,
2074 "unexpected message of AutoSCSI MsgIn: SAVE_POINTERS");
2078 case MESSAGE_REJECT
:
2079 /* If previous message_out is sending SDTR, and get
2080 message_reject from target, SDTR negotiation is failed */
2081 if (data
->cur_target
->sync_flag
&
2082 (SDTR_INITIATOR
| SDTR_TARGET
)) {
2084 * Current target is negotiating SDTR, but it's
2085 * failed. Fall back to async transfer mode, and set
2088 nsp32_set_async(data
, data
->cur_target
);
2089 data
->cur_target
->sync_flag
&= ~SDTR_INITIATOR
;
2090 data
->cur_target
->sync_flag
|= SDTR_DONE
;
2095 case LINKED_CMD_COMPLETE
:
2096 case LINKED_FLG_CMD_COMPLETE
:
2097 /* queue tag is not supported currently */
2098 nsp32_msg (KERN_WARNING
,
2099 "unsupported message: 0x%x", msgtype
);
2102 case INITIATE_RECOVERY
:
2103 /* staring ECA (Extended Contingent Allegiance) state. */
2104 /* This message is declined in SPI2 or later. */
2111 case SIMPLE_QUEUE_TAG
:
2114 * 0x23: Ignore_Wide_Residue is not declared in scsi.h.
2115 * No support is needed.
2117 if (data
->msgin_len
>= 1) {
2121 /* current position is 1-byte of 2 byte */
2129 case EXTENDED_MESSAGE
:
2130 if (data
->msgin_len
< 1) {
2132 * Current position does not reach 2-byte
2133 * (2-byte is extended message length).
2139 if ((data
->msginbuf
[1] + 1) > data
->msgin_len
) {
2141 * Current extended message has msginbuf[1] + 2
2142 * (msgin_len starts counting from 0, so buf[1] + 1).
2143 * If current message position is not finished,
2144 * continue receiving message.
2151 * Reach here means regular length of each type of
2152 * extended messages.
2154 switch (data
->msginbuf
[2]) {
2155 case EXTENDED_MODIFY_DATA_POINTER
:
2157 goto reject
; /* not implemented yet */
2162 * Exchange this message between initiator and target.
2164 if (data
->msgin_len
!= EXTENDED_SDTR_LEN
+ 1) {
2166 * received inappropriate message.
2172 nsp32_analyze_sdtr(SCpnt
);
2176 case EXTENDED_EXTENDED_IDENTIFY
:
2177 /* SCSI-I only, not supported. */
2178 goto reject
; /* not implemented yet */
2183 goto reject
; /* not implemented yet */
2197 if (msgclear
== TRUE
) {
2198 data
->msgin_len
= 0;
2201 * If restarting AutoSCSI, but there are some message to out
2202 * (msgout_len > 0), set AutoATN, and set SCSIMSGOUT as 0
2203 * (MV_VALID = 0). When commandcontrol is written with
2204 * AutoSCSI restart, at the same time MsgOutOccur should be
2205 * happened (however, such situation is really possible...?).
2207 if (data
->msgout_len
> 0) {
2208 nsp32_write4(base
, SCSI_MSG_OUT
, 0);
2209 command
|= AUTO_ATN
;
2214 * If it's failed, COMMANDCONTROL_AUTO_COMMAND_PHASE is needed.
2216 command
|= (AUTO_MSGIN_00_OR_04
| AUTO_MSGIN_02
);
2219 * If current msgin03 is TRUE, then flag on.
2221 if (data
->cur_lunt
->msgin03
== TRUE
) {
2222 command
|= AUTO_MSGIN_03
;
2224 data
->cur_lunt
->msgin03
= FALSE
;
2232 nsp32_restart_autoscsi(SCpnt
, command
);
2235 * wait SCSI REQ negate for REQ-ACK handshake
2237 nsp32_wait_req(data
, NEGATE
);
2242 nsp32_sack_negate(data
);
2244 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "exit");
2249 nsp32_msg(KERN_WARNING
,
2250 "invalid or unsupported MessageIn, rejected. "
2251 "current msg: 0x%x (len: 0x%x), processing msg: 0x%x",
2252 msg
, data
->msgin_len
, msgtype
);
2253 nsp32_build_reject(SCpnt
);
2254 data
->msgin_len
= 0;
2262 static void nsp32_analyze_sdtr(struct scsi_cmnd
*SCpnt
)
2264 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
2265 nsp32_target
*target
= data
->cur_target
;
2266 nsp32_sync_table
*synct
;
2267 unsigned char get_period
= data
->msginbuf
[3];
2268 unsigned char get_offset
= data
->msginbuf
[4];
2272 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "enter");
2274 synct
= data
->synct
;
2275 syncnum
= data
->syncnum
;
2278 * If this inititor sent the SDTR message, then target responds SDTR,
2279 * initiator SYNCREG, ACKWIDTH from SDTR parameter.
2280 * Messages are not appropriate, then send back reject message.
2281 * If initiator did not send the SDTR, but target sends SDTR,
2282 * initiator calculator the appropriate parameter and send back SDTR.
2284 if (target
->sync_flag
& SDTR_INITIATOR
) {
2286 * Initiator sent SDTR, the target responds and
2287 * send back negotiation SDTR.
2289 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "target responds SDTR");
2291 target
->sync_flag
&= ~SDTR_INITIATOR
;
2292 target
->sync_flag
|= SDTR_DONE
;
2297 if (get_offset
> SYNC_OFFSET
) {
2299 * Negotiation is failed, the target send back
2300 * unexpected offset value.
2305 if (get_offset
== ASYNC_OFFSET
) {
2307 * Negotiation is succeeded, the target want
2308 * to fall back into asynchronous transfer mode.
2315 * Check whether sync period is too short. If too short,
2316 * fall back to async mode. If it's ok, then investigate
2317 * the received sync period. If sync period is acceptable
2318 * between sync table start_period and end_period, then
2319 * set this I_T nexus as sent offset and period.
2320 * If it's not acceptable, send back reject and fall back
2323 if (get_period
< data
->synct
[0].period_num
) {
2325 * Negotiation is failed, the target send back
2326 * unexpected period value.
2331 entry
= nsp32_search_period_entry(data
, target
, get_period
);
2335 * Target want to use long period which is not
2336 * acceptable NinjaSCSI-32Bi/UDE.
2342 * Set new sync table and offset in this I_T nexus.
2344 nsp32_set_sync_entry(data
, target
, entry
, get_offset
);
2346 /* Target send SDTR to initiator. */
2347 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "target send SDTR");
2349 target
->sync_flag
|= SDTR_INITIATOR
;
2352 if (get_offset
> SYNC_OFFSET
) {
2353 /* send back as SYNC_OFFSET */
2354 get_offset
= SYNC_OFFSET
;
2358 if (get_period
< data
->synct
[0].period_num
) {
2359 get_period
= data
->synct
[0].period_num
;
2362 entry
= nsp32_search_period_entry(data
, target
, get_period
);
2364 if (get_offset
== ASYNC_OFFSET
|| entry
< 0) {
2365 nsp32_set_async(data
, target
);
2366 nsp32_build_sdtr(SCpnt
, 0, ASYNC_OFFSET
);
2368 nsp32_set_sync_entry(data
, target
, entry
, get_offset
);
2369 nsp32_build_sdtr(SCpnt
, get_period
, get_offset
);
2373 target
->period
= get_period
;
2374 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "exit");
2379 * If the current message is unacceptable, send back to the target
2380 * with reject message.
2382 nsp32_build_reject(SCpnt
);
2385 nsp32_set_async(data
, target
); /* set as ASYNC transfer mode */
2388 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR
, "exit: set async");
2394 * Search config entry number matched in sync_table from given
2395 * target and speed period value. If failed to search, return negative value.
2397 static int nsp32_search_period_entry(nsp32_hw_data
*data
,
2398 nsp32_target
*target
,
2399 unsigned char period
)
2403 if (target
->limit_entry
>= data
->syncnum
) {
2404 nsp32_msg(KERN_ERR
, "limit_entry exceeds syncnum!");
2405 target
->limit_entry
= 0;
2408 for (i
= target
->limit_entry
; i
< data
->syncnum
; i
++) {
2409 if (period
>= data
->synct
[i
].start_period
&&
2410 period
<= data
->synct
[i
].end_period
) {
2416 * Check given period value is over the sync_table value.
2417 * If so, return max value.
2419 if (i
== data
->syncnum
) {
2428 * target <-> initiator use ASYNC transfer
2430 static void nsp32_set_async(nsp32_hw_data
*data
, nsp32_target
*target
)
2432 unsigned char period
= data
->synct
[target
->limit_entry
].period_num
;
2434 target
->offset
= ASYNC_OFFSET
;
2436 target
->syncreg
= TO_SYNCREG(period
, ASYNC_OFFSET
);
2437 target
->ackwidth
= 0;
2438 target
->sample_reg
= 0;
2440 nsp32_dbg(NSP32_DEBUG_SYNC
, "set async");
2445 * target <-> initiator use maximum SYNC transfer
2447 static void nsp32_set_max_sync(nsp32_hw_data
*data
,
2448 nsp32_target
*target
,
2449 unsigned char *period
,
2450 unsigned char *offset
)
2452 unsigned char period_num
, ackwidth
;
2454 period_num
= data
->synct
[target
->limit_entry
].period_num
;
2455 *period
= data
->synct
[target
->limit_entry
].start_period
;
2456 ackwidth
= data
->synct
[target
->limit_entry
].ackwidth
;
2457 *offset
= SYNC_OFFSET
;
2459 target
->syncreg
= TO_SYNCREG(period_num
, *offset
);
2460 target
->ackwidth
= ackwidth
;
2461 target
->offset
= *offset
;
2462 target
->sample_reg
= 0; /* disable SREQ sampling */
2467 * target <-> initiator use entry number speed
2469 static void nsp32_set_sync_entry(nsp32_hw_data
*data
,
2470 nsp32_target
*target
,
2472 unsigned char offset
)
2474 unsigned char period
, ackwidth
, sample_rate
;
2476 period
= data
->synct
[entry
].period_num
;
2477 ackwidth
= data
->synct
[entry
].ackwidth
;
2479 sample_rate
= data
->synct
[entry
].sample_rate
;
2481 target
->syncreg
= TO_SYNCREG(period
, offset
);
2482 target
->ackwidth
= ackwidth
;
2483 target
->offset
= offset
;
2484 target
->sample_reg
= sample_rate
| SAMPLING_ENABLE
;
2486 nsp32_dbg(NSP32_DEBUG_SYNC
, "set sync");
2491 * It waits until SCSI REQ becomes assertion or negation state.
2493 * Note: If nsp32_msgin_occur is called, we asserts SCSI ACK. Then
2494 * connected target responds SCSI REQ negation. We have to wait
2495 * SCSI REQ becomes negation in order to negate SCSI ACK signal for
2496 * REQ-ACK handshake.
2498 static void nsp32_wait_req(nsp32_hw_data
*data
, int state
)
2500 unsigned int base
= data
->BaseAddress
;
2502 unsigned char bus
, req_bit
;
2504 if (!((state
== ASSERT
) || (state
== NEGATE
))) {
2505 nsp32_msg(KERN_ERR
, "unknown state designation");
2508 req_bit
= (state
== ASSERT
? BUSMON_REQ
: 0);
2511 bus
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
2512 if ((bus
& BUSMON_REQ
) == req_bit
) {
2513 nsp32_dbg(NSP32_DEBUG_WAIT
,
2514 "wait_time: %d", wait_time
);
2519 } while (wait_time
< REQSACK_TIMEOUT_TIME
);
2521 nsp32_msg(KERN_WARNING
, "wait REQ timeout, req_bit: 0x%x", req_bit
);
2525 * It waits until SCSI SACK becomes assertion or negation state.
2527 static void nsp32_wait_sack(nsp32_hw_data
*data
, int state
)
2529 unsigned int base
= data
->BaseAddress
;
2531 unsigned char bus
, ack_bit
;
2533 if (!((state
== ASSERT
) || (state
== NEGATE
))) {
2534 nsp32_msg(KERN_ERR
, "unknown state designation");
2537 ack_bit
= (state
== ASSERT
? BUSMON_ACK
: 0);
2540 bus
= nsp32_read1(base
, SCSI_BUS_MONITOR
);
2541 if ((bus
& BUSMON_ACK
) == ack_bit
) {
2542 nsp32_dbg(NSP32_DEBUG_WAIT
,
2543 "wait_time: %d", wait_time
);
2548 } while (wait_time
< REQSACK_TIMEOUT_TIME
);
2550 nsp32_msg(KERN_WARNING
, "wait SACK timeout, ack_bit: 0x%x", ack_bit
);
2556 * Note: SCSI ACK assertion needs with ACKENB=1, AUTODIRECTION=1.
2558 static void nsp32_sack_assert(nsp32_hw_data
*data
)
2560 unsigned int base
= data
->BaseAddress
;
2561 unsigned char busctrl
;
2563 busctrl
= nsp32_read1(base
, SCSI_BUS_CONTROL
);
2564 busctrl
|= (BUSCTL_ACK
| AUTODIRECTION
| ACKENB
);
2565 nsp32_write1(base
, SCSI_BUS_CONTROL
, busctrl
);
2571 static void nsp32_sack_negate(nsp32_hw_data
*data
)
2573 unsigned int base
= data
->BaseAddress
;
2574 unsigned char busctrl
;
2576 busctrl
= nsp32_read1(base
, SCSI_BUS_CONTROL
);
2577 busctrl
&= ~BUSCTL_ACK
;
2578 nsp32_write1(base
, SCSI_BUS_CONTROL
, busctrl
);
2584 * Note: n_io_port is defined as 0x7f because I/O register port is
2586 * 0x800-0x8ff: memory mapped I/O port
2587 * 0x900-0xbff: (map same 0x800-0x8ff I/O port image repeatedly)
2588 * 0xc00-0xfff: CardBus status registers
2590 static int nsp32_detect(struct pci_dev
*pdev
)
2592 struct Scsi_Host
*host
; /* registered host structure */
2593 struct resource
*res
;
2594 nsp32_hw_data
*data
;
2598 nsp32_dbg(NSP32_DEBUG_REGISTER
, "enter");
2601 * register this HBA as SCSI device
2603 host
= scsi_host_alloc(&nsp32_template
, sizeof(nsp32_hw_data
));
2605 nsp32_msg (KERN_ERR
, "failed to scsi register");
2612 data
= (nsp32_hw_data
*)host
->hostdata
;
2614 memcpy(data
, &nsp32_data_base
, sizeof(nsp32_hw_data
));
2616 host
->irq
= data
->IrqNumber
;
2617 host
->io_port
= data
->BaseAddress
;
2618 host
->unique_id
= data
->BaseAddress
;
2619 host
->n_io_port
= data
->NumAddress
;
2620 host
->base
= (unsigned long)data
->MmioAddress
;
2623 spin_lock_init(&(data
->Lock
));
2625 data
->cur_lunt
= NULL
;
2626 data
->cur_target
= NULL
;
2629 * Bus master transfer mode is supported currently.
2631 data
->trans_method
= NSP32_TRANSFER_BUSMASTER
;
2634 * Set clock div, CLOCK_4 (HBA has own external clock, and
2635 * dividing * 100ns/4).
2636 * Currently CLOCK_4 has only tested, not for CLOCK_2/PCICLK yet.
2638 data
->clock
= CLOCK_4
;
2641 * Select appropriate nsp32_sync_table and set I_CLOCKDIV.
2643 switch (data
->clock
) {
2645 /* If data->clock is CLOCK_4, then select 40M sync table. */
2646 data
->synct
= nsp32_sync_table_40M
;
2647 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_40M
);
2650 /* If data->clock is CLOCK_2, then select 20M sync table. */
2651 data
->synct
= nsp32_sync_table_20M
;
2652 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_20M
);
2655 /* If data->clock is PCICLK, then select pci sync table. */
2656 data
->synct
= nsp32_sync_table_pci
;
2657 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_pci
);
2660 nsp32_msg(KERN_WARNING
,
2661 "Invalid clock div is selected, set CLOCK_4.");
2662 /* Use default value CLOCK_4 */
2663 data
->clock
= CLOCK_4
;
2664 data
->synct
= nsp32_sync_table_40M
;
2665 data
->syncnum
= ARRAY_SIZE(nsp32_sync_table_40M
);
2675 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0) {
2676 nsp32_msg (KERN_ERR
, "failed to set PCI DMA mask");
2677 goto scsi_unregister
;
2681 * allocate autoparam DMA resource.
2683 data
->autoparam
= pci_alloc_consistent(pdev
, sizeof(nsp32_autoparam
), &(data
->auto_paddr
));
2684 if (data
->autoparam
== NULL
) {
2685 nsp32_msg(KERN_ERR
, "failed to allocate DMA memory");
2686 goto scsi_unregister
;
2690 * allocate scatter-gather DMA resource.
2692 data
->sg_list
= pci_alloc_consistent(pdev
, NSP32_SG_TABLE_SIZE
,
2694 if (data
->sg_list
== NULL
) {
2695 nsp32_msg(KERN_ERR
, "failed to allocate DMA memory");
2696 goto free_autoparam
;
2699 for (i
= 0; i
< ARRAY_SIZE(data
->lunt
); i
++) {
2700 for (j
= 0; j
< ARRAY_SIZE(data
->lunt
[0]); j
++) {
2701 int offset
= i
* ARRAY_SIZE(data
->lunt
[0]) + j
;
2708 .sglun
= &(data
->sg_list
[offset
]),
2709 .sglun_paddr
= data
->sg_paddr
+ (offset
* sizeof(nsp32_sglun
)),
2712 data
->lunt
[i
][j
] = tmp
;
2719 for (i
= 0; i
< ARRAY_SIZE(data
->target
); i
++) {
2720 nsp32_target
*target
= &(data
->target
[i
]);
2722 target
->limit_entry
= 0;
2723 target
->sync_flag
= 0;
2724 nsp32_set_async(data
, target
);
2730 ret
= nsp32_getprom_param(data
);
2732 data
->resettime
= 3; /* default 3 */
2740 snprintf(data
->info_str
, sizeof(data
->info_str
),
2741 "NinjaSCSI-32Bi/UDE: irq %d, io 0x%lx+0x%x",
2742 host
->irq
, host
->io_port
, host
->n_io_port
);
2747 * Note: It's important to reset SCSI bus in initialization phase.
2748 * NinjaSCSI-32Bi/UDE HBA EEPROM seems to exchange SDTR when
2749 * system is coming up, so SCSI devices connected to HBA is set as
2750 * un-asynchronous mode. It brings the merit that this HBA is
2751 * ready to start synchronous transfer without any preparation,
2752 * but we are difficult to control transfer speed. In addition,
2753 * it prevents device transfer speed from effecting EEPROM start-up
2754 * SDTR. NinjaSCSI-32Bi/UDE has the feature if EEPROM is set as
2755 * Auto Mode, then FAST-10M is selected when SCSI devices are
2756 * connected same or more than 4 devices. It should be avoided
2757 * depending on this specification. Thus, resetting the SCSI bus
2758 * restores all connected SCSI devices to asynchronous mode, then
2759 * this driver set SDTR safely later, and we can control all SCSI
2760 * device transfer mode.
2762 nsp32_do_bus_reset(data
);
2764 ret
= request_irq(host
->irq
, do_nsp32_isr
, IRQF_SHARED
, "nsp32", data
);
2766 nsp32_msg(KERN_ERR
, "Unable to allocate IRQ for NinjaSCSI32 "
2767 "SCSI PCI controller. Interrupt: %d", host
->irq
);
2774 res
= request_region(host
->io_port
, host
->n_io_port
, "nsp32");
2777 "I/O region 0x%lx+0x%lx is already used",
2778 data
->BaseAddress
, data
->NumAddress
);
2782 ret
= scsi_add_host(host
, &pdev
->dev
);
2784 nsp32_msg(KERN_ERR
, "failed to add scsi host");
2787 scsi_scan_host(host
);
2788 pci_set_drvdata(pdev
, host
);
2792 release_region(host
->io_port
, host
->n_io_port
);
2795 free_irq(host
->irq
, data
);
2798 pci_free_consistent(pdev
, NSP32_SG_TABLE_SIZE
,
2799 data
->sg_list
, data
->sg_paddr
);
2802 pci_free_consistent(pdev
, sizeof(nsp32_autoparam
),
2803 data
->autoparam
, data
->auto_paddr
);
2806 scsi_host_put(host
);
2812 static int nsp32_release(struct Scsi_Host
*host
)
2814 nsp32_hw_data
*data
= (nsp32_hw_data
*)host
->hostdata
;
2816 if (data
->autoparam
) {
2817 pci_free_consistent(data
->Pci
, sizeof(nsp32_autoparam
),
2818 data
->autoparam
, data
->auto_paddr
);
2821 if (data
->sg_list
) {
2822 pci_free_consistent(data
->Pci
, NSP32_SG_TABLE_SIZE
,
2823 data
->sg_list
, data
->sg_paddr
);
2827 free_irq(host
->irq
, data
);
2830 if (host
->io_port
&& host
->n_io_port
) {
2831 release_region(host
->io_port
, host
->n_io_port
);
2834 if (data
->MmioAddress
) {
2835 iounmap(data
->MmioAddress
);
2841 static const char *nsp32_info(struct Scsi_Host
*shpnt
)
2843 nsp32_hw_data
*data
= (nsp32_hw_data
*)shpnt
->hostdata
;
2845 return data
->info_str
;
2849 /****************************************************************************
2852 static int nsp32_eh_abort(struct scsi_cmnd
*SCpnt
)
2854 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
2855 unsigned int base
= SCpnt
->device
->host
->io_port
;
2857 nsp32_msg(KERN_WARNING
, "abort");
2859 if (data
->cur_lunt
->SCpnt
== NULL
) {
2860 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "abort failed");
2864 if (data
->cur_target
->sync_flag
& (SDTR_INITIATOR
| SDTR_TARGET
)) {
2865 /* reset SDTR negotiation */
2866 data
->cur_target
->sync_flag
= 0;
2867 nsp32_set_async(data
, data
->cur_target
);
2870 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
2871 nsp32_write2(base
, BM_CNT
, 0);
2873 SCpnt
->result
= DID_ABORT
<< 16;
2874 nsp32_scsi_done(SCpnt
);
2876 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "abort success");
2880 static int nsp32_eh_bus_reset(struct scsi_cmnd
*SCpnt
)
2882 nsp32_hw_data
*data
= (nsp32_hw_data
*)SCpnt
->device
->host
->hostdata
;
2883 unsigned int base
= SCpnt
->device
->host
->io_port
;
2885 spin_lock_irq(SCpnt
->device
->host
->host_lock
);
2887 nsp32_msg(KERN_INFO
, "Bus Reset");
2888 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "SCpnt=0x%x", SCpnt
);
2890 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
2891 nsp32_do_bus_reset(data
);
2892 nsp32_write2(base
, IRQ_CONTROL
, 0);
2894 spin_unlock_irq(SCpnt
->device
->host
->host_lock
);
2895 return SUCCESS
; /* SCSI bus reset is succeeded at any time. */
2898 static void nsp32_do_bus_reset(nsp32_hw_data
*data
)
2900 unsigned int base
= data
->BaseAddress
;
2901 unsigned short intrdat
;
2904 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "in");
2908 * clear TRANSFERCONTROL_BM_START
2911 nsp32_write2(base
, TRANSFER_CONTROL
, 0);
2912 nsp32_write4(base
, BM_CNT
, 0);
2913 nsp32_write4(base
, CLR_COUNTER
, CLRCOUNTER_ALLMASK
);
2916 * fall back to asynchronous transfer mode
2917 * initialize SDTR negotiation flag
2919 for (i
= 0; i
< ARRAY_SIZE(data
->target
); i
++) {
2920 nsp32_target
*target
= &data
->target
[i
];
2922 target
->sync_flag
= 0;
2923 nsp32_set_async(data
, target
);
2929 nsp32_write1(base
, SCSI_BUS_CONTROL
, BUSCTL_RST
);
2930 udelay(RESET_HOLD_TIME
);
2931 nsp32_write1(base
, SCSI_BUS_CONTROL
, 0);
2932 for(i
= 0; i
< 5; i
++) {
2933 intrdat
= nsp32_read2(base
, IRQ_STATUS
); /* dummy read */
2934 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "irq:1: 0x%x", intrdat
);
2937 data
->CurrentSC
= NULL
;
2940 static int nsp32_eh_host_reset(struct scsi_cmnd
*SCpnt
)
2942 struct Scsi_Host
*host
= SCpnt
->device
->host
;
2943 unsigned int base
= SCpnt
->device
->host
->io_port
;
2944 nsp32_hw_data
*data
= (nsp32_hw_data
*)host
->hostdata
;
2946 nsp32_msg(KERN_INFO
, "Host Reset");
2947 nsp32_dbg(NSP32_DEBUG_BUSRESET
, "SCpnt=0x%x", SCpnt
);
2949 spin_lock_irq(SCpnt
->device
->host
->host_lock
);
2952 nsp32_write2(base
, IRQ_CONTROL
, IRQ_CONTROL_ALL_IRQ_MASK
);
2953 nsp32_do_bus_reset(data
);
2954 nsp32_write2(base
, IRQ_CONTROL
, 0);
2956 spin_unlock_irq(SCpnt
->device
->host
->host_lock
);
2957 return SUCCESS
; /* Host reset is succeeded at any time. */
2961 /**************************************************************************
2966 * getting EEPROM parameter
2968 static int nsp32_getprom_param(nsp32_hw_data
*data
)
2970 int vendor
= data
->pci_devid
->vendor
;
2971 int device
= data
->pci_devid
->device
;
2977 ret
= nsp32_prom_read(data
, 0x7e);
2979 nsp32_msg(KERN_INFO
, "No EEPROM detected: 0x%x", ret
);
2982 ret
= nsp32_prom_read(data
, 0x7f);
2984 nsp32_msg(KERN_INFO
, "Invalid number: 0x%x", ret
);
2991 if (vendor
== PCI_VENDOR_ID_WORKBIT
&&
2992 device
== PCI_DEVICE_ID_WORKBIT_STANDARD
) {
2993 ret
= nsp32_getprom_c16(data
);
2994 } else if (vendor
== PCI_VENDOR_ID_WORKBIT
&&
2995 device
== PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC
) {
2996 ret
= nsp32_getprom_at24(data
);
2997 } else if (vendor
== PCI_VENDOR_ID_WORKBIT
&&
2998 device
== PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO
) {
2999 ret
= nsp32_getprom_at24(data
);
3001 nsp32_msg(KERN_WARNING
, "Unknown EEPROM");
3005 /* for debug : SPROM data full checking */
3006 for (i
= 0; i
<= 0x1f; i
++) {
3007 val
= nsp32_prom_read(data
, i
);
3008 nsp32_dbg(NSP32_DEBUG_EEPROM
,
3009 "rom address 0x%x : 0x%x", i
, val
);
3017 * AT24C01A (Logitec: LHA-600S), AT24C02 (Melco Buffalo: IFC-USLP) data map:
3020 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3021 * Value 0x0: ASYNC, 0x0c: Ultra-20M, 0x19: Fast-10M
3022 * 0x07 : HBA Synchronous Transfer Period
3023 * Value 0: AutoSync, 1: Manual Setting
3024 * 0x08 - 0x0f : Not Used? (0x0)
3025 * 0x10 : Bus Termination
3026 * Value 0: Auto[ON], 1: ON, 2: OFF
3027 * 0x11 : Not Used? (0)
3028 * 0x12 : Bus Reset Delay Time (0x03)
3029 * 0x13 : Bootable CD Support
3030 * Value 0: Disable, 1: Enable
3031 * 0x14 : Device Scan
3032 * Bit 7 6 5 4 3 2 1 0
3033 * | <----------------->
3034 * | SCSI ID: Value 0: Skip, 1: YES
3035 * |-> Value 0: ALL scan, Value 1: Manual
3036 * 0x15 - 0x1b : Not Used? (0)
3037 * 0x1c : Constant? (0x01) (clock div?)
3038 * 0x1d - 0x7c : Not Used (0xff)
3039 * 0x7d : Not Used? (0xff)
3040 * 0x7e : Constant (0x55), Validity signature
3041 * 0x7f : Constant (0xaa), Validity signature
3043 static int nsp32_getprom_at24(nsp32_hw_data
*data
)
3047 nsp32_target
*target
;
3051 * Reset time which is designated by EEPROM.
3053 * TODO: Not used yet.
3055 data
->resettime
= nsp32_prom_read(data
, 0x12);
3058 * HBA Synchronous Transfer Period
3060 * Note: auto_sync = 0: auto, 1: manual. Ninja SCSI HBA spec says
3061 * that if auto_sync is 0 (auto), and connected SCSI devices are
3062 * same or lower than 3, then transfer speed is set as ULTRA-20M.
3063 * On the contrary if connected SCSI devices are same or higher
3064 * than 4, then transfer speed is set as FAST-10M.
3066 * I break this rule. The number of connected SCSI devices are
3067 * only ignored. If auto_sync is 0 (auto), then transfer speed is
3068 * forced as ULTRA-20M.
3070 ret
= nsp32_prom_read(data
, 0x07);
3079 nsp32_msg(KERN_WARNING
,
3080 "Unsupported Auto Sync mode. Fall back to manual mode.");
3084 if (trans_mode
== ULTRA20M_MODE
) {
3089 * each device Synchronous Transfer Period
3091 for (i
= 0; i
< NSP32_HOST_SCSIID
; i
++) {
3092 target
= &data
->target
[i
];
3093 if (auto_sync
== TRUE
) {
3094 target
->limit_entry
= 0; /* set as ULTRA20M */
3096 ret
= nsp32_prom_read(data
, i
);
3097 entry
= nsp32_search_period_entry(data
, target
, ret
);
3099 /* search failed... set maximum speed */
3102 target
->limit_entry
= entry
;
3111 * C16 110 (I-O Data: SC-NBD) data map:
3114 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3115 * Value 0x0: 20MB/S, 0x1: 10MB/S, 0x2: 5MB/S, 0x3: ASYNC
3116 * 0x07 : 0 (HBA Synchronous Transfer Period: Auto Sync)
3117 * 0x08 - 0x0f : Not Used? (0x0)
3118 * 0x10 : Transfer Mode
3119 * Value 0: PIO, 1: Busmater
3120 * 0x11 : Bus Reset Delay Time (0x00-0x20)
3121 * 0x12 : Bus Termination
3122 * Value 0: Disable, 1: Enable
3123 * 0x13 - 0x19 : Disconnection
3124 * Value 0: Disable, 1: Enable
3125 * 0x1a - 0x7c : Not Used? (0)
3126 * 0x7d : Not Used? (0xf8)
3127 * 0x7e : Constant (0x55), Validity signature
3128 * 0x7f : Constant (0xaa), Validity signature
3130 static int nsp32_getprom_c16(nsp32_hw_data
*data
)
3133 nsp32_target
*target
;
3137 * Reset time which is designated by EEPROM.
3139 * TODO: Not used yet.
3141 data
->resettime
= nsp32_prom_read(data
, 0x11);
3144 * each device Synchronous Transfer Period
3146 for (i
= 0; i
< NSP32_HOST_SCSIID
; i
++) {
3147 target
= &data
->target
[i
];
3148 ret
= nsp32_prom_read(data
, i
);
3150 case 0: /* 20MB/s */
3153 case 1: /* 10MB/s */
3162 default: /* default 20MB/s */
3166 entry
= nsp32_search_period_entry(data
, target
, val
);
3167 if (entry
< 0 || trans_mode
== ULTRA20M_MODE
) {
3168 /* search failed... set maximum speed */
3171 target
->limit_entry
= entry
;
3179 * Atmel AT24C01A (drived in 5V) serial EEPROM routines
3181 static int nsp32_prom_read(nsp32_hw_data
*data
, int romaddr
)
3185 /* start condition */
3186 nsp32_prom_start(data
);
3188 /* device address */
3189 nsp32_prom_write_bit(data
, 1); /* 1 */
3190 nsp32_prom_write_bit(data
, 0); /* 0 */
3191 nsp32_prom_write_bit(data
, 1); /* 1 */
3192 nsp32_prom_write_bit(data
, 0); /* 0 */
3193 nsp32_prom_write_bit(data
, 0); /* A2: 0 (GND) */
3194 nsp32_prom_write_bit(data
, 0); /* A1: 0 (GND) */
3195 nsp32_prom_write_bit(data
, 0); /* A0: 0 (GND) */
3197 /* R/W: W for dummy write */
3198 nsp32_prom_write_bit(data
, 0);
3201 nsp32_prom_write_bit(data
, 0);
3204 for (i
= 7; i
>= 0; i
--) {
3205 nsp32_prom_write_bit(data
, ((romaddr
>> i
) & 1));
3209 nsp32_prom_write_bit(data
, 0);
3211 /* start condition */
3212 nsp32_prom_start(data
);
3214 /* device address */
3215 nsp32_prom_write_bit(data
, 1); /* 1 */
3216 nsp32_prom_write_bit(data
, 0); /* 0 */
3217 nsp32_prom_write_bit(data
, 1); /* 1 */
3218 nsp32_prom_write_bit(data
, 0); /* 0 */
3219 nsp32_prom_write_bit(data
, 0); /* A2: 0 (GND) */
3220 nsp32_prom_write_bit(data
, 0); /* A1: 0 (GND) */
3221 nsp32_prom_write_bit(data
, 0); /* A0: 0 (GND) */
3224 nsp32_prom_write_bit(data
, 1);
3227 nsp32_prom_write_bit(data
, 0);
3231 for (i
= 7; i
>= 0; i
--) {
3232 val
+= (nsp32_prom_read_bit(data
) << i
);
3236 nsp32_prom_write_bit(data
, 1);
3238 /* stop condition */
3239 nsp32_prom_stop(data
);
3244 static void nsp32_prom_set(nsp32_hw_data
*data
, int bit
, int val
)
3246 int base
= data
->BaseAddress
;
3249 tmp
= nsp32_index_read1(base
, SERIAL_ROM_CTL
);
3257 nsp32_index_write1(base
, SERIAL_ROM_CTL
, tmp
);
3262 static int nsp32_prom_get(nsp32_hw_data
*data
, int bit
)
3264 int base
= data
->BaseAddress
;
3268 nsp32_msg(KERN_ERR
, "return value is not appropriate");
3273 tmp
= nsp32_index_read1(base
, SERIAL_ROM_CTL
) & bit
;
3286 static void nsp32_prom_start (nsp32_hw_data
*data
)
3288 /* start condition */
3289 nsp32_prom_set(data
, SCL
, 1);
3290 nsp32_prom_set(data
, SDA
, 1);
3291 nsp32_prom_set(data
, ENA
, 1); /* output mode */
3292 nsp32_prom_set(data
, SDA
, 0); /* keeping SCL=1 and transiting
3293 * SDA 1->0 is start condition */
3294 nsp32_prom_set(data
, SCL
, 0);
3297 static void nsp32_prom_stop (nsp32_hw_data
*data
)
3299 /* stop condition */
3300 nsp32_prom_set(data
, SCL
, 1);
3301 nsp32_prom_set(data
, SDA
, 0);
3302 nsp32_prom_set(data
, ENA
, 1); /* output mode */
3303 nsp32_prom_set(data
, SDA
, 1);
3304 nsp32_prom_set(data
, SCL
, 0);
3307 static void nsp32_prom_write_bit(nsp32_hw_data
*data
, int val
)
3310 nsp32_prom_set(data
, SDA
, val
);
3311 nsp32_prom_set(data
, SCL
, 1 );
3312 nsp32_prom_set(data
, SCL
, 0 );
3315 static int nsp32_prom_read_bit(nsp32_hw_data
*data
)
3320 nsp32_prom_set(data
, ENA
, 0); /* input mode */
3321 nsp32_prom_set(data
, SCL
, 1);
3323 val
= nsp32_prom_get(data
, SDA
);
3325 nsp32_prom_set(data
, SCL
, 0);
3326 nsp32_prom_set(data
, ENA
, 1); /* output mode */
3332 /**************************************************************************
3337 /* Device suspended */
3338 static int nsp32_suspend(struct pci_dev
*pdev
, pm_message_t state
)
3340 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
3342 nsp32_msg(KERN_INFO
, "pci-suspend: pdev=0x%p, state=%ld, slot=%s, host=0x%p", pdev
, state
, pci_name(pdev
), host
);
3344 pci_save_state (pdev
);
3345 pci_disable_device (pdev
);
3346 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
3351 /* Device woken up */
3352 static int nsp32_resume(struct pci_dev
*pdev
)
3354 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
3355 nsp32_hw_data
*data
= (nsp32_hw_data
*)host
->hostdata
;
3358 nsp32_msg(KERN_INFO
, "pci-resume: pdev=0x%p, slot=%s, host=0x%p", pdev
, pci_name(pdev
), host
);
3360 pci_set_power_state(pdev
, PCI_D0
);
3361 pci_enable_wake (pdev
, PCI_D0
, 0);
3362 pci_restore_state (pdev
);
3364 reg
= nsp32_read2(data
->BaseAddress
, INDEX_REG
);
3366 nsp32_msg(KERN_INFO
, "io=0x%x reg=0x%x", data
->BaseAddress
, reg
);
3368 if (reg
== 0xffff) {
3369 nsp32_msg(KERN_INFO
, "missing device. abort resume.");
3373 nsp32hw_init (data
);
3374 nsp32_do_bus_reset(data
);
3376 nsp32_msg(KERN_INFO
, "resume success");
3383 /************************************************************************
3384 * PCI/Cardbus probe/remove routine
3386 static int __devinit
nsp32_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3389 nsp32_hw_data
*data
= &nsp32_data_base
;
3391 nsp32_dbg(NSP32_DEBUG_REGISTER
, "enter");
3393 ret
= pci_enable_device(pdev
);
3395 nsp32_msg(KERN_ERR
, "failed to enable pci device");
3400 data
->pci_devid
= id
;
3401 data
->IrqNumber
= pdev
->irq
;
3402 data
->BaseAddress
= pci_resource_start(pdev
, 0);
3403 data
->NumAddress
= pci_resource_len (pdev
, 0);
3404 data
->MmioAddress
= pci_ioremap_bar(pdev
, 1);
3405 data
->MmioLength
= pci_resource_len (pdev
, 1);
3407 pci_set_master(pdev
);
3409 ret
= nsp32_detect(pdev
);
3411 nsp32_msg(KERN_INFO
, "irq: %i mmio: %p+0x%lx slot: %s model: %s",
3413 data
->MmioAddress
, data
->MmioLength
,
3415 nsp32_model
[id
->driver_data
]);
3417 nsp32_dbg(NSP32_DEBUG_REGISTER
, "exit %d", ret
);
3422 static void __devexit
nsp32_remove(struct pci_dev
*pdev
)
3424 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
3426 nsp32_dbg(NSP32_DEBUG_REGISTER
, "enter");
3428 scsi_remove_host(host
);
3430 nsp32_release(host
);
3432 scsi_host_put(host
);
3435 static struct pci_driver nsp32_driver
= {
3437 .id_table
= nsp32_pci_table
,
3438 .probe
= nsp32_probe
,
3439 .remove
= __devexit_p(nsp32_remove
),
3441 .suspend
= nsp32_suspend
,
3442 .resume
= nsp32_resume
,
3446 /*********************************************************************
3449 static int __init
init_nsp32(void) {
3450 nsp32_msg(KERN_INFO
, "loading...");
3451 return pci_register_driver(&nsp32_driver
);
3454 static void __exit
exit_nsp32(void) {
3455 nsp32_msg(KERN_INFO
, "unloading...");
3456 pci_unregister_driver(&nsp32_driver
);
3459 module_init(init_nsp32
);
3460 module_exit(exit_nsp32
);