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
313 static void nsp32_message(const char *func, int line, char *type, char *fmt, ...)
316 char buf[NSP32_DEBUG_BUF_LEN];
319 vsnprintf(buf, sizeof(buf), fmt, args);
323 printk("%snsp32: %s\n", type, buf);
325 printk("%snsp32: %s (%d): %s\n", type, func, line, buf);
330 static void nsp32_dmessage(const char *func, int line, int mask, char *fmt, ...)
333 char buf[NSP32_DEBUG_BUF_LEN];
336 vsnprintf(buf, sizeof(buf), fmt, args);
339 if (mask & NSP32_DEBUG_MASK) {
340 printk("nsp32-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
346 # include "nsp32_debug.c"
348 # define show_command(arg) /* */
349 # define show_busphase(arg) /* */
350 # define show_autophase(arg) /* */
356 static void nsp32_build_identify(struct scsi_cmnd *SCpnt)
358 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
359 int pos = data->msgout_len;
362 /* XXX: Auto DiscPriv detection is progressing... */
363 if (disc_priv == 0) {
367 data->msgoutbuf[pos] = IDENTIFY(mode, SCpnt->device->lun); pos++;
369 data->msgout_len = pos;
373 * SDTR Message Routine
375 static void nsp32_build_sdtr(struct scsi_cmnd *SCpnt,
376 unsigned char period,
377 unsigned char offset)
379 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
380 int pos = data->msgout_len;
382 data->msgoutbuf[pos] = EXTENDED_MESSAGE; pos++;
383 data->msgoutbuf[pos] = EXTENDED_SDTR_LEN; pos++;
384 data->msgoutbuf[pos] = EXTENDED_SDTR; pos++;
385 data->msgoutbuf[pos] = period; pos++;
386 data->msgoutbuf[pos] = offset; pos++;
388 data->msgout_len = pos;
392 * No Operation Message
394 static void nsp32_build_nop(struct scsi_cmnd *SCpnt)
396 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
397 int pos = data->msgout_len;
400 nsp32_msg(KERN_WARNING,
401 "Some messages are already contained!");
405 data->msgoutbuf[pos] = NOP; pos++;
406 data->msgout_len = pos;
412 static void nsp32_build_reject(struct scsi_cmnd *SCpnt)
414 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
415 int pos = data->msgout_len;
417 data->msgoutbuf[pos] = MESSAGE_REJECT; pos++;
418 data->msgout_len = pos;
425 static void nsp32_start_timer(struct scsi_cmnd *SCpnt, int time)
427 unsigned int base = SCpnt->host->io_port;
429 nsp32_dbg(NSP32_DEBUG_INTR, "timer=%d", time);
431 if (time & (~TIMER_CNT_MASK)) {
432 nsp32_dbg(NSP32_DEBUG_INTR, "timer set overflow");
435 nsp32_write2(base, TIMER_SET, time & TIMER_CNT_MASK);
441 * set SCSI command and other parameter to asic, and start selection phase
443 static int nsp32_selection_autopara(struct scsi_cmnd *SCpnt)
445 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
446 unsigned int base = SCpnt->device->host->io_port;
447 unsigned int host_id = SCpnt->device->host->this_id;
448 unsigned char target = scmd_id(SCpnt);
449 nsp32_autoparam *param = data->autoparam;
455 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
460 phase = nsp32_read1(base, SCSI_BUS_MONITOR);
461 if (phase != BUSMON_BUS_FREE) {
462 nsp32_msg(KERN_WARNING, "bus busy");
463 show_busphase(phase & BUSMON_PHASE_MASK);
464 SCpnt->result = DID_BUS_BUSY << 16;
471 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
472 * over 3 messages needs another routine.
474 if (data->msgout_len == 0) {
475 nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
476 SCpnt->result = DID_ERROR << 16;
478 } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
480 for (i = 0; i < data->msgout_len; i++) {
482 * the sending order of the message is:
483 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
484 * MCNT 2: MSG#1 -> MSG#2
488 msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
490 msgout |= MV_VALID; /* MV valid */
491 msgout |= (unsigned int)data->msgout_len; /* len */
493 /* data->msgout_len > 3 */
497 // nsp_dbg(NSP32_DEBUG_AUTOSCSI, "sel time out=0x%x\n", nsp32_read2(base, SEL_TIME_OUT));
498 // nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
501 * setup asic parameter
503 memset(param, 0, sizeof(nsp32_autoparam));
506 for (i = 0; i < SCpnt->cmd_len; i++) {
507 param->cdb[4 * i] = SCpnt->cmnd[i];
510 /* outgoing messages */
511 param->msgout = cpu_to_le32(msgout);
513 /* syncreg, ackwidth, target id, SREQ sampling rate */
514 param->syncreg = data->cur_target->syncreg;
515 param->ackwidth = data->cur_target->ackwidth;
516 param->target_id = BIT(host_id) | BIT(target);
517 param->sample_reg = data->cur_target->sample_reg;
519 // nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "sample rate=0x%x\n", data->cur_target->sample_reg);
521 /* command control */
522 param->command_control = cpu_to_le16(CLEAR_CDB_FIFO_POINTER |
524 AUTO_MSGIN_00_OR_04 |
529 /* transfer control */
531 switch (data->trans_method) {
532 case NSP32_TRANSFER_BUSMASTER:
535 case NSP32_TRANSFER_MMIO:
538 case NSP32_TRANSFER_PIO:
542 nsp32_msg(KERN_ERR, "unknown trans_method");
546 * OR-ed BLIEND_MODE, FIFO intr is decreased, instead of PCI bus waits.
547 * For bus master transfer, it's taken off.
549 s |= (TRANSFER_GO | ALL_COUNTER_CLR);
550 param->transfer_control = cpu_to_le16(s);
553 param->sgt_pointer = cpu_to_le32(data->cur_lunt->sglun_paddr);
556 * transfer parameter to ASIC
558 nsp32_write4(base, SGT_ADR, data->auto_paddr);
559 nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER |
565 ret = nsp32_arbitration(SCpnt, base);
572 * Selection with AUTO SCSI (without AUTO PARAMETER)
574 static int nsp32_selection_autoscsi(struct scsi_cmnd *SCpnt)
576 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
577 unsigned int base = SCpnt->device->host->io_port;
578 unsigned int host_id = SCpnt->device->host->this_id;
579 unsigned char target = scmd_id(SCpnt);
582 unsigned short command = 0;
583 unsigned int msgout = 0;
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 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%x", 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 = SAM_STAT_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: Disconnect.
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;
1785 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
1786 "enter: msgout_len: 0x%x", data->msgout_len);
1789 * If MsgOut phase is occurred without having any
1790 * message, then No_Operation is sent (SCSI-2).
1792 if (data->msgout_len == 0) {
1793 nsp32_build_nop(SCpnt);
1799 for (i = 0; i < data->msgout_len; i++) {
1800 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
1801 "%d : 0x%x", i, data->msgoutbuf[i]);
1804 * Check REQ is asserted.
1806 nsp32_wait_req(data, ASSERT);
1808 if (i == (data->msgout_len - 1)) {
1810 * If the last message, set the AutoSCSI restart
1811 * before send back the ack message. AutoSCSI
1812 * restart automatically negate ATN signal.
1814 //command = (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
1815 //nsp32_restart_autoscsi(SCpnt, command);
1816 nsp32_write2(base, COMMAND_CONTROL,
1817 (CLEAR_CDB_FIFO_POINTER |
1818 AUTO_COMMAND_PHASE |
1820 AUTO_MSGIN_00_OR_04 |
1824 * Write data with SACK, then wait sack is
1825 * automatically negated.
1827 nsp32_write1(base, SCSI_DATA_WITH_ACK, data->msgoutbuf[i]);
1828 nsp32_wait_sack(data, NEGATE);
1830 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "bus: 0x%x\n",
1831 nsp32_read1(base, SCSI_BUS_MONITOR));
1834 data->msgout_len = 0;
1836 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "exit");
1842 * Note: Restarting AutoSCSI needs set:
1843 * SYNC_REG, ACK_WIDTH, SGT_ADR, TRANSFER_CONTROL
1845 static void nsp32_restart_autoscsi(struct scsi_cmnd *SCpnt, unsigned short command)
1847 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1848 unsigned int base = data->BaseAddress;
1849 unsigned short transfer = 0;
1851 nsp32_dbg(NSP32_DEBUG_RESTART, "enter");
1853 if (data->cur_target == NULL || data->cur_lunt == NULL) {
1854 nsp32_msg(KERN_ERR, "Target or Lun is invalid");
1859 * Don't set BM_START_ADR before setting this register.
1861 nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
1866 nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
1869 * set SREQ hazard killer sampling rate
1871 nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
1874 * set SGT ADDR (physical address)
1876 nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
1879 * set TRANSFER CONTROL REG
1882 transfer |= (TRANSFER_GO | ALL_COUNTER_CLR);
1883 if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
1884 if (scsi_bufflen(SCpnt) > 0) {
1885 transfer |= BM_START;
1887 } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
1888 transfer |= CB_MMIO_MODE;
1889 } else if (data->trans_method & NSP32_TRANSFER_PIO) {
1890 transfer |= CB_IO_MODE;
1892 nsp32_write2(base, TRANSFER_CONTROL, transfer);
1897 * TODO: COMMANDCONTROL_AUTO_COMMAND_PHASE is needed ?
1899 command |= (CLEAR_CDB_FIFO_POINTER |
1900 AUTO_COMMAND_PHASE |
1902 nsp32_write2(base, COMMAND_CONTROL, command);
1904 nsp32_dbg(NSP32_DEBUG_RESTART, "exit");
1909 * cannot run automatically message in occur
1911 static void nsp32_msgin_occur(struct scsi_cmnd *SCpnt,
1912 unsigned long irq_status,
1913 unsigned short execph)
1915 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1916 unsigned int base = SCpnt->device->host->io_port;
1918 unsigned char msgtype;
1919 unsigned char newlun;
1920 unsigned short command = 0;
1921 int msgclear = TRUE;
1926 * read first message
1927 * Use SCSIDATA_W_ACK instead of SCSIDATAIN, because the procedure
1928 * of Message-In have to be processed before sending back SCSI ACK.
1930 msg = nsp32_read1(base, SCSI_DATA_IN);
1931 data->msginbuf[(unsigned char)data->msgin_len] = msg;
1932 msgtype = data->msginbuf[0];
1933 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR,
1934 "enter: msglen: 0x%x msgin: 0x%x msgtype: 0x%x",
1935 data->msgin_len, msg, msgtype);
1938 * TODO: We need checking whether bus phase is message in?
1944 nsp32_sack_assert(data);
1947 * processing IDENTIFY
1949 if (msgtype & 0x80) {
1950 if (!(irq_status & IRQSTATUS_RESELECT_OCCUER)) {
1951 /* Invalid (non reselect) phase */
1955 newlun = msgtype & 0x1f; /* TODO: SPI-3 compliant? */
1956 ret = nsp32_reselection(SCpnt, newlun);
1965 * processing messages except for IDENTIFY
1967 * TODO: Messages are all SCSI-2 terminology. SCSI-3 compliance is TODO.
1973 case COMMAND_COMPLETE:
1976 * These messages should not be occurred.
1977 * They should be processed on AutoSCSI sequencer.
1979 nsp32_msg(KERN_WARNING,
1980 "unexpected message of AutoSCSI MsgIn: 0x%x", msg);
1983 case RESTORE_POINTERS:
1985 * AutoMsgIn03 is disabled, and HBA gets this message.
1988 if ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE)) {
1989 unsigned int s_sacklen;
1991 s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
1992 if ((execph & MSGIN_02_VALID) && (s_sacklen > 0)) {
1993 nsp32_adjust_busfree(SCpnt, s_sacklen);
1995 /* No need to rewrite SGT */
1998 data->cur_lunt->msgin03 = FALSE;
2000 /* Update with the new value */
2002 /* reset SACK/SavedACK counter (or ALL clear?) */
2003 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
2006 * set new sg pointer
2008 new_sgtp = data->cur_lunt->sglun_paddr +
2009 (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
2010 nsp32_write4(base, SGT_ADR, new_sgtp);
2016 * These messages should not be occurred.
2017 * They should be processed on AutoSCSI sequencer.
2019 nsp32_msg (KERN_WARNING,
2020 "unexpected message of AutoSCSI MsgIn: SAVE_POINTERS");
2024 case MESSAGE_REJECT:
2025 /* If previous message_out is sending SDTR, and get
2026 message_reject from target, SDTR negotiation is failed */
2027 if (data->cur_target->sync_flag &
2028 (SDTR_INITIATOR | SDTR_TARGET)) {
2030 * Current target is negotiating SDTR, but it's
2031 * failed. Fall back to async transfer mode, and set
2034 nsp32_set_async(data, data->cur_target);
2035 data->cur_target->sync_flag &= ~SDTR_INITIATOR;
2036 data->cur_target->sync_flag |= SDTR_DONE;
2041 case LINKED_CMD_COMPLETE:
2042 case LINKED_FLG_CMD_COMPLETE:
2043 /* queue tag is not supported currently */
2044 nsp32_msg (KERN_WARNING,
2045 "unsupported message: 0x%x", msgtype);
2048 case INITIATE_RECOVERY:
2049 /* staring ECA (Extended Contingent Allegiance) state. */
2050 /* This message is declined in SPI2 or later. */
2057 case SIMPLE_QUEUE_TAG:
2060 * 0x23: Ignore_Wide_Residue is not declared in scsi.h.
2061 * No support is needed.
2063 if (data->msgin_len >= 1) {
2067 /* current position is 1-byte of 2 byte */
2075 case EXTENDED_MESSAGE:
2076 if (data->msgin_len < 1) {
2078 * Current position does not reach 2-byte
2079 * (2-byte is extended message length).
2085 if ((data->msginbuf[1] + 1) > data->msgin_len) {
2087 * Current extended message has msginbuf[1] + 2
2088 * (msgin_len starts counting from 0, so buf[1] + 1).
2089 * If current message position is not finished,
2090 * continue receiving message.
2097 * Reach here means regular length of each type of
2098 * extended messages.
2100 switch (data->msginbuf[2]) {
2101 case EXTENDED_MODIFY_DATA_POINTER:
2103 goto reject; /* not implemented yet */
2108 * Exchange this message between initiator and target.
2110 if (data->msgin_len != EXTENDED_SDTR_LEN + 1) {
2112 * received inappropriate message.
2118 nsp32_analyze_sdtr(SCpnt);
2122 case EXTENDED_EXTENDED_IDENTIFY:
2123 /* SCSI-I only, not supported. */
2124 goto reject; /* not implemented yet */
2129 goto reject; /* not implemented yet */
2143 if (msgclear == TRUE) {
2144 data->msgin_len = 0;
2147 * If restarting AutoSCSI, but there are some message to out
2148 * (msgout_len > 0), set AutoATN, and set SCSIMSGOUT as 0
2149 * (MV_VALID = 0). When commandcontrol is written with
2150 * AutoSCSI restart, at the same time MsgOutOccur should be
2151 * happened (however, such situation is really possible...?).
2153 if (data->msgout_len > 0) {
2154 nsp32_write4(base, SCSI_MSG_OUT, 0);
2155 command |= AUTO_ATN;
2160 * If it's failed, COMMANDCONTROL_AUTO_COMMAND_PHASE is needed.
2162 command |= (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
2165 * If current msgin03 is TRUE, then flag on.
2167 if (data->cur_lunt->msgin03 == TRUE) {
2168 command |= AUTO_MSGIN_03;
2170 data->cur_lunt->msgin03 = FALSE;
2178 nsp32_restart_autoscsi(SCpnt, command);
2181 * wait SCSI REQ negate for REQ-ACK handshake
2183 nsp32_wait_req(data, NEGATE);
2188 nsp32_sack_negate(data);
2190 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
2195 nsp32_msg(KERN_WARNING,
2196 "invalid or unsupported MessageIn, rejected. "
2197 "current msg: 0x%x (len: 0x%x), processing msg: 0x%x",
2198 msg, data->msgin_len, msgtype);
2199 nsp32_build_reject(SCpnt);
2200 data->msgin_len = 0;
2208 static void nsp32_analyze_sdtr(struct scsi_cmnd *SCpnt)
2210 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2211 nsp32_target *target = data->cur_target;
2212 unsigned char get_period = data->msginbuf[3];
2213 unsigned char get_offset = data->msginbuf[4];
2216 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "enter");
2219 * If this inititor sent the SDTR message, then target responds SDTR,
2220 * initiator SYNCREG, ACKWIDTH from SDTR parameter.
2221 * Messages are not appropriate, then send back reject message.
2222 * If initiator did not send the SDTR, but target sends SDTR,
2223 * initiator calculator the appropriate parameter and send back SDTR.
2225 if (target->sync_flag & SDTR_INITIATOR) {
2227 * Initiator sent SDTR, the target responds and
2228 * send back negotiation SDTR.
2230 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target responds SDTR");
2232 target->sync_flag &= ~SDTR_INITIATOR;
2233 target->sync_flag |= SDTR_DONE;
2238 if (get_offset > SYNC_OFFSET) {
2240 * Negotiation is failed, the target send back
2241 * unexpected offset value.
2246 if (get_offset == ASYNC_OFFSET) {
2248 * Negotiation is succeeded, the target want
2249 * to fall back into asynchronous transfer mode.
2256 * Check whether sync period is too short. If too short,
2257 * fall back to async mode. If it's ok, then investigate
2258 * the received sync period. If sync period is acceptable
2259 * between sync table start_period and end_period, then
2260 * set this I_T nexus as sent offset and period.
2261 * If it's not acceptable, send back reject and fall back
2264 if (get_period < data->synct[0].period_num) {
2266 * Negotiation is failed, the target send back
2267 * unexpected period value.
2272 entry = nsp32_search_period_entry(data, target, get_period);
2276 * Target want to use long period which is not
2277 * acceptable NinjaSCSI-32Bi/UDE.
2283 * Set new sync table and offset in this I_T nexus.
2285 nsp32_set_sync_entry(data, target, entry, get_offset);
2287 /* Target send SDTR to initiator. */
2288 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target send SDTR");
2290 target->sync_flag |= SDTR_INITIATOR;
2293 if (get_offset > SYNC_OFFSET) {
2294 /* send back as SYNC_OFFSET */
2295 get_offset = SYNC_OFFSET;
2299 if (get_period < data->synct[0].period_num) {
2300 get_period = data->synct[0].period_num;
2303 entry = nsp32_search_period_entry(data, target, get_period);
2305 if (get_offset == ASYNC_OFFSET || entry < 0) {
2306 nsp32_set_async(data, target);
2307 nsp32_build_sdtr(SCpnt, 0, ASYNC_OFFSET);
2309 nsp32_set_sync_entry(data, target, entry, get_offset);
2310 nsp32_build_sdtr(SCpnt, get_period, get_offset);
2314 target->period = get_period;
2315 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
2320 * If the current message is unacceptable, send back to the target
2321 * with reject message.
2323 nsp32_build_reject(SCpnt);
2326 nsp32_set_async(data, target); /* set as ASYNC transfer mode */
2329 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit: set async");
2335 * Search config entry number matched in sync_table from given
2336 * target and speed period value. If failed to search, return negative value.
2338 static int nsp32_search_period_entry(nsp32_hw_data *data,
2339 nsp32_target *target,
2340 unsigned char period)
2344 if (target->limit_entry >= data->syncnum) {
2345 nsp32_msg(KERN_ERR, "limit_entry exceeds syncnum!");
2346 target->limit_entry = 0;
2349 for (i = target->limit_entry; i < data->syncnum; i++) {
2350 if (period >= data->synct[i].start_period &&
2351 period <= data->synct[i].end_period) {
2357 * Check given period value is over the sync_table value.
2358 * If so, return max value.
2360 if (i == data->syncnum) {
2369 * target <-> initiator use ASYNC transfer
2371 static void nsp32_set_async(nsp32_hw_data *data, nsp32_target *target)
2373 unsigned char period = data->synct[target->limit_entry].period_num;
2375 target->offset = ASYNC_OFFSET;
2377 target->syncreg = TO_SYNCREG(period, ASYNC_OFFSET);
2378 target->ackwidth = 0;
2379 target->sample_reg = 0;
2381 nsp32_dbg(NSP32_DEBUG_SYNC, "set async");
2386 * target <-> initiator use maximum SYNC transfer
2388 static void nsp32_set_max_sync(nsp32_hw_data *data,
2389 nsp32_target *target,
2390 unsigned char *period,
2391 unsigned char *offset)
2393 unsigned char period_num, ackwidth;
2395 period_num = data->synct[target->limit_entry].period_num;
2396 *period = data->synct[target->limit_entry].start_period;
2397 ackwidth = data->synct[target->limit_entry].ackwidth;
2398 *offset = SYNC_OFFSET;
2400 target->syncreg = TO_SYNCREG(period_num, *offset);
2401 target->ackwidth = ackwidth;
2402 target->offset = *offset;
2403 target->sample_reg = 0; /* disable SREQ sampling */
2408 * target <-> initiator use entry number speed
2410 static void nsp32_set_sync_entry(nsp32_hw_data *data,
2411 nsp32_target *target,
2413 unsigned char offset)
2415 unsigned char period, ackwidth, sample_rate;
2417 period = data->synct[entry].period_num;
2418 ackwidth = data->synct[entry].ackwidth;
2419 sample_rate = data->synct[entry].sample_rate;
2421 target->syncreg = TO_SYNCREG(period, offset);
2422 target->ackwidth = ackwidth;
2423 target->offset = offset;
2424 target->sample_reg = sample_rate | SAMPLING_ENABLE;
2426 nsp32_dbg(NSP32_DEBUG_SYNC, "set sync");
2431 * It waits until SCSI REQ becomes assertion or negation state.
2433 * Note: If nsp32_msgin_occur is called, we asserts SCSI ACK. Then
2434 * connected target responds SCSI REQ negation. We have to wait
2435 * SCSI REQ becomes negation in order to negate SCSI ACK signal for
2436 * REQ-ACK handshake.
2438 static void nsp32_wait_req(nsp32_hw_data *data, int state)
2440 unsigned int base = data->BaseAddress;
2442 unsigned char bus, req_bit;
2444 if (!((state == ASSERT) || (state == NEGATE))) {
2445 nsp32_msg(KERN_ERR, "unknown state designation");
2448 req_bit = (state == ASSERT ? BUSMON_REQ : 0);
2451 bus = nsp32_read1(base, SCSI_BUS_MONITOR);
2452 if ((bus & BUSMON_REQ) == req_bit) {
2453 nsp32_dbg(NSP32_DEBUG_WAIT,
2454 "wait_time: %d", wait_time);
2459 } while (wait_time < REQSACK_TIMEOUT_TIME);
2461 nsp32_msg(KERN_WARNING, "wait REQ timeout, req_bit: 0x%x", req_bit);
2465 * It waits until SCSI SACK becomes assertion or negation state.
2467 static void nsp32_wait_sack(nsp32_hw_data *data, int state)
2469 unsigned int base = data->BaseAddress;
2471 unsigned char bus, ack_bit;
2473 if (!((state == ASSERT) || (state == NEGATE))) {
2474 nsp32_msg(KERN_ERR, "unknown state designation");
2477 ack_bit = (state == ASSERT ? BUSMON_ACK : 0);
2480 bus = nsp32_read1(base, SCSI_BUS_MONITOR);
2481 if ((bus & BUSMON_ACK) == ack_bit) {
2482 nsp32_dbg(NSP32_DEBUG_WAIT,
2483 "wait_time: %d", wait_time);
2488 } while (wait_time < REQSACK_TIMEOUT_TIME);
2490 nsp32_msg(KERN_WARNING, "wait SACK timeout, ack_bit: 0x%x", ack_bit);
2496 * Note: SCSI ACK assertion needs with ACKENB=1, AUTODIRECTION=1.
2498 static void nsp32_sack_assert(nsp32_hw_data *data)
2500 unsigned int base = data->BaseAddress;
2501 unsigned char busctrl;
2503 busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
2504 busctrl |= (BUSCTL_ACK | AUTODIRECTION | ACKENB);
2505 nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
2511 static void nsp32_sack_negate(nsp32_hw_data *data)
2513 unsigned int base = data->BaseAddress;
2514 unsigned char busctrl;
2516 busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
2517 busctrl &= ~BUSCTL_ACK;
2518 nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
2524 * Note: n_io_port is defined as 0x7f because I/O register port is
2526 * 0x800-0x8ff: memory mapped I/O port
2527 * 0x900-0xbff: (map same 0x800-0x8ff I/O port image repeatedly)
2528 * 0xc00-0xfff: CardBus status registers
2530 static int nsp32_detect(struct pci_dev *pdev)
2532 struct Scsi_Host *host; /* registered host structure */
2533 struct resource *res;
2534 nsp32_hw_data *data;
2538 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
2541 * register this HBA as SCSI device
2543 host = scsi_host_alloc(&nsp32_template, sizeof(nsp32_hw_data));
2545 nsp32_msg (KERN_ERR, "failed to scsi register");
2552 data = (nsp32_hw_data *)host->hostdata;
2554 memcpy(data, &nsp32_data_base, sizeof(nsp32_hw_data));
2556 host->irq = data->IrqNumber;
2557 host->io_port = data->BaseAddress;
2558 host->unique_id = data->BaseAddress;
2559 host->n_io_port = data->NumAddress;
2560 host->base = (unsigned long)data->MmioAddress;
2563 spin_lock_init(&(data->Lock));
2565 data->cur_lunt = NULL;
2566 data->cur_target = NULL;
2569 * Bus master transfer mode is supported currently.
2571 data->trans_method = NSP32_TRANSFER_BUSMASTER;
2574 * Set clock div, CLOCK_4 (HBA has own external clock, and
2575 * dividing * 100ns/4).
2576 * Currently CLOCK_4 has only tested, not for CLOCK_2/PCICLK yet.
2578 data->clock = CLOCK_4;
2581 * Select appropriate nsp32_sync_table and set I_CLOCKDIV.
2583 switch (data->clock) {
2585 /* If data->clock is CLOCK_4, then select 40M sync table. */
2586 data->synct = nsp32_sync_table_40M;
2587 data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
2590 /* If data->clock is CLOCK_2, then select 20M sync table. */
2591 data->synct = nsp32_sync_table_20M;
2592 data->syncnum = ARRAY_SIZE(nsp32_sync_table_20M);
2595 /* If data->clock is PCICLK, then select pci sync table. */
2596 data->synct = nsp32_sync_table_pci;
2597 data->syncnum = ARRAY_SIZE(nsp32_sync_table_pci);
2600 nsp32_msg(KERN_WARNING,
2601 "Invalid clock div is selected, set CLOCK_4.");
2602 /* Use default value CLOCK_4 */
2603 data->clock = CLOCK_4;
2604 data->synct = nsp32_sync_table_40M;
2605 data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
2615 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
2616 nsp32_msg (KERN_ERR, "failed to set PCI DMA mask");
2617 goto scsi_unregister;
2621 * allocate autoparam DMA resource.
2623 data->autoparam = dma_alloc_coherent(&pdev->dev,
2624 sizeof(nsp32_autoparam), &(data->auto_paddr),
2626 if (data->autoparam == NULL) {
2627 nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
2628 goto scsi_unregister;
2632 * allocate scatter-gather DMA resource.
2634 data->sg_list = dma_alloc_coherent(&pdev->dev, NSP32_SG_TABLE_SIZE,
2635 &data->sg_paddr, GFP_KERNEL);
2636 if (data->sg_list == NULL) {
2637 nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
2638 goto free_autoparam;
2641 for (i = 0; i < ARRAY_SIZE(data->lunt); i++) {
2642 for (j = 0; j < ARRAY_SIZE(data->lunt[0]); j++) {
2643 int offset = i * ARRAY_SIZE(data->lunt[0]) + j;
2650 .sglun = &(data->sg_list[offset]),
2651 .sglun_paddr = data->sg_paddr + (offset * sizeof(nsp32_sglun)),
2654 data->lunt[i][j] = tmp;
2661 for (i = 0; i < ARRAY_SIZE(data->target); i++) {
2662 nsp32_target *target = &(data->target[i]);
2664 target->limit_entry = 0;
2665 target->sync_flag = 0;
2666 nsp32_set_async(data, target);
2672 ret = nsp32_getprom_param(data);
2674 data->resettime = 3; /* default 3 */
2682 snprintf(data->info_str, sizeof(data->info_str),
2683 "NinjaSCSI-32Bi/UDE: irq %d, io 0x%lx+0x%x",
2684 host->irq, host->io_port, host->n_io_port);
2689 * Note: It's important to reset SCSI bus in initialization phase.
2690 * NinjaSCSI-32Bi/UDE HBA EEPROM seems to exchange SDTR when
2691 * system is coming up, so SCSI devices connected to HBA is set as
2692 * un-asynchronous mode. It brings the merit that this HBA is
2693 * ready to start synchronous transfer without any preparation,
2694 * but we are difficult to control transfer speed. In addition,
2695 * it prevents device transfer speed from effecting EEPROM start-up
2696 * SDTR. NinjaSCSI-32Bi/UDE has the feature if EEPROM is set as
2697 * Auto Mode, then FAST-10M is selected when SCSI devices are
2698 * connected same or more than 4 devices. It should be avoided
2699 * depending on this specification. Thus, resetting the SCSI bus
2700 * restores all connected SCSI devices to asynchronous mode, then
2701 * this driver set SDTR safely later, and we can control all SCSI
2702 * device transfer mode.
2704 nsp32_do_bus_reset(data);
2706 ret = request_irq(host->irq, do_nsp32_isr, IRQF_SHARED, "nsp32", data);
2708 nsp32_msg(KERN_ERR, "Unable to allocate IRQ for NinjaSCSI32 "
2709 "SCSI PCI controller. Interrupt: %d", host->irq);
2716 res = request_region(host->io_port, host->n_io_port, "nsp32");
2719 "I/O region 0x%x+0x%x is already used",
2720 data->BaseAddress, data->NumAddress);
2724 ret = scsi_add_host(host, &pdev->dev);
2726 nsp32_msg(KERN_ERR, "failed to add scsi host");
2729 scsi_scan_host(host);
2730 pci_set_drvdata(pdev, host);
2734 release_region(host->io_port, host->n_io_port);
2737 free_irq(host->irq, data);
2740 dma_free_coherent(&pdev->dev, NSP32_SG_TABLE_SIZE,
2741 data->sg_list, data->sg_paddr);
2744 dma_free_coherent(&pdev->dev, sizeof(nsp32_autoparam),
2745 data->autoparam, data->auto_paddr);
2748 scsi_host_put(host);
2754 static int nsp32_release(struct Scsi_Host *host)
2756 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
2758 if (data->autoparam) {
2759 dma_free_coherent(&data->Pci->dev, sizeof(nsp32_autoparam),
2760 data->autoparam, data->auto_paddr);
2763 if (data->sg_list) {
2764 dma_free_coherent(&data->Pci->dev, NSP32_SG_TABLE_SIZE,
2765 data->sg_list, data->sg_paddr);
2769 free_irq(host->irq, data);
2772 if (host->io_port && host->n_io_port) {
2773 release_region(host->io_port, host->n_io_port);
2776 if (data->MmioAddress) {
2777 iounmap(data->MmioAddress);
2783 static const char *nsp32_info(struct Scsi_Host *shpnt)
2785 nsp32_hw_data *data = (nsp32_hw_data *)shpnt->hostdata;
2787 return data->info_str;
2791 /****************************************************************************
2794 static int nsp32_eh_abort(struct scsi_cmnd *SCpnt)
2796 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2797 unsigned int base = SCpnt->device->host->io_port;
2799 nsp32_msg(KERN_WARNING, "abort");
2801 if (data->cur_lunt->SCpnt == NULL) {
2802 nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort failed");
2806 if (data->cur_target->sync_flag & (SDTR_INITIATOR | SDTR_TARGET)) {
2807 /* reset SDTR negotiation */
2808 data->cur_target->sync_flag = 0;
2809 nsp32_set_async(data, data->cur_target);
2812 nsp32_write2(base, TRANSFER_CONTROL, 0);
2813 nsp32_write2(base, BM_CNT, 0);
2815 SCpnt->result = DID_ABORT << 16;
2816 nsp32_scsi_done(SCpnt);
2818 nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort success");
2822 static void nsp32_do_bus_reset(nsp32_hw_data *data)
2824 unsigned int base = data->BaseAddress;
2826 unsigned short __maybe_unused intrdat;
2828 nsp32_dbg(NSP32_DEBUG_BUSRESET, "in");
2832 * clear TRANSFERCONTROL_BM_START
2835 nsp32_write2(base, TRANSFER_CONTROL, 0);
2836 nsp32_write4(base, BM_CNT, 0);
2837 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
2840 * fall back to asynchronous transfer mode
2841 * initialize SDTR negotiation flag
2843 for (i = 0; i < ARRAY_SIZE(data->target); i++) {
2844 nsp32_target *target = &data->target[i];
2846 target->sync_flag = 0;
2847 nsp32_set_async(data, target);
2853 nsp32_write1(base, SCSI_BUS_CONTROL, BUSCTL_RST);
2854 mdelay(RESET_HOLD_TIME / 1000);
2855 nsp32_write1(base, SCSI_BUS_CONTROL, 0);
2856 for(i = 0; i < 5; i++) {
2857 intrdat = nsp32_read2(base, IRQ_STATUS); /* dummy read */
2858 nsp32_dbg(NSP32_DEBUG_BUSRESET, "irq:1: 0x%x", intrdat);
2861 data->CurrentSC = NULL;
2864 static int nsp32_eh_host_reset(struct scsi_cmnd *SCpnt)
2866 struct Scsi_Host *host = SCpnt->device->host;
2867 unsigned int base = SCpnt->device->host->io_port;
2868 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
2870 nsp32_msg(KERN_INFO, "Host Reset");
2871 nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
2873 spin_lock_irq(SCpnt->device->host->host_lock);
2876 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
2877 nsp32_do_bus_reset(data);
2878 nsp32_write2(base, IRQ_CONTROL, 0);
2880 spin_unlock_irq(SCpnt->device->host->host_lock);
2881 return SUCCESS; /* Host reset is succeeded at any time. */
2885 /**************************************************************************
2890 * getting EEPROM parameter
2892 static int nsp32_getprom_param(nsp32_hw_data *data)
2894 int vendor = data->pci_devid->vendor;
2895 int device = data->pci_devid->device;
2897 int __maybe_unused val;
2902 ret = nsp32_prom_read(data, 0x7e);
2904 nsp32_msg(KERN_INFO, "No EEPROM detected: 0x%x", ret);
2907 ret = nsp32_prom_read(data, 0x7f);
2909 nsp32_msg(KERN_INFO, "Invalid number: 0x%x", ret);
2916 if (vendor == PCI_VENDOR_ID_WORKBIT &&
2917 device == PCI_DEVICE_ID_WORKBIT_STANDARD) {
2918 ret = nsp32_getprom_c16(data);
2919 } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
2920 device == PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC) {
2921 ret = nsp32_getprom_at24(data);
2922 } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
2923 device == PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO ) {
2924 ret = nsp32_getprom_at24(data);
2926 nsp32_msg(KERN_WARNING, "Unknown EEPROM");
2930 /* for debug : SPROM data full checking */
2931 for (i = 0; i <= 0x1f; i++) {
2932 val = nsp32_prom_read(data, i);
2933 nsp32_dbg(NSP32_DEBUG_EEPROM,
2934 "rom address 0x%x : 0x%x", i, val);
2942 * AT24C01A (Logitec: LHA-600S), AT24C02 (Melco Buffalo: IFC-USLP) data map:
2945 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
2946 * Value 0x0: ASYNC, 0x0c: Ultra-20M, 0x19: Fast-10M
2947 * 0x07 : HBA Synchronous Transfer Period
2948 * Value 0: AutoSync, 1: Manual Setting
2949 * 0x08 - 0x0f : Not Used? (0x0)
2950 * 0x10 : Bus Termination
2951 * Value 0: Auto[ON], 1: ON, 2: OFF
2952 * 0x11 : Not Used? (0)
2953 * 0x12 : Bus Reset Delay Time (0x03)
2954 * 0x13 : Bootable CD Support
2955 * Value 0: Disable, 1: Enable
2956 * 0x14 : Device Scan
2957 * Bit 7 6 5 4 3 2 1 0
2958 * | <----------------->
2959 * | SCSI ID: Value 0: Skip, 1: YES
2960 * |-> Value 0: ALL scan, Value 1: Manual
2961 * 0x15 - 0x1b : Not Used? (0)
2962 * 0x1c : Constant? (0x01) (clock div?)
2963 * 0x1d - 0x7c : Not Used (0xff)
2964 * 0x7d : Not Used? (0xff)
2965 * 0x7e : Constant (0x55), Validity signature
2966 * 0x7f : Constant (0xaa), Validity signature
2968 static int nsp32_getprom_at24(nsp32_hw_data *data)
2972 nsp32_target *target;
2976 * Reset time which is designated by EEPROM.
2978 * TODO: Not used yet.
2980 data->resettime = nsp32_prom_read(data, 0x12);
2983 * HBA Synchronous Transfer Period
2985 * Note: auto_sync = 0: auto, 1: manual. Ninja SCSI HBA spec says
2986 * that if auto_sync is 0 (auto), and connected SCSI devices are
2987 * same or lower than 3, then transfer speed is set as ULTRA-20M.
2988 * On the contrary if connected SCSI devices are same or higher
2989 * than 4, then transfer speed is set as FAST-10M.
2991 * I break this rule. The number of connected SCSI devices are
2992 * only ignored. If auto_sync is 0 (auto), then transfer speed is
2993 * forced as ULTRA-20M.
2995 ret = nsp32_prom_read(data, 0x07);
3004 nsp32_msg(KERN_WARNING,
3005 "Unsupported Auto Sync mode. Fall back to manual mode.");
3009 if (trans_mode == ULTRA20M_MODE) {
3014 * each device Synchronous Transfer Period
3016 for (i = 0; i < NSP32_HOST_SCSIID; i++) {
3017 target = &data->target[i];
3018 if (auto_sync == TRUE) {
3019 target->limit_entry = 0; /* set as ULTRA20M */
3021 ret = nsp32_prom_read(data, i);
3022 entry = nsp32_search_period_entry(data, target, ret);
3024 /* search failed... set maximum speed */
3027 target->limit_entry = entry;
3036 * C16 110 (I-O Data: SC-NBD) data map:
3039 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3040 * Value 0x0: 20MB/S, 0x1: 10MB/S, 0x2: 5MB/S, 0x3: ASYNC
3041 * 0x07 : 0 (HBA Synchronous Transfer Period: Auto Sync)
3042 * 0x08 - 0x0f : Not Used? (0x0)
3043 * 0x10 : Transfer Mode
3044 * Value 0: PIO, 1: Busmater
3045 * 0x11 : Bus Reset Delay Time (0x00-0x20)
3046 * 0x12 : Bus Termination
3047 * Value 0: Disable, 1: Enable
3048 * 0x13 - 0x19 : Disconnection
3049 * Value 0: Disable, 1: Enable
3050 * 0x1a - 0x7c : Not Used? (0)
3051 * 0x7d : Not Used? (0xf8)
3052 * 0x7e : Constant (0x55), Validity signature
3053 * 0x7f : Constant (0xaa), Validity signature
3055 static int nsp32_getprom_c16(nsp32_hw_data *data)
3058 nsp32_target *target;
3062 * Reset time which is designated by EEPROM.
3064 * TODO: Not used yet.
3066 data->resettime = nsp32_prom_read(data, 0x11);
3069 * each device Synchronous Transfer Period
3071 for (i = 0; i < NSP32_HOST_SCSIID; i++) {
3072 target = &data->target[i];
3073 ret = nsp32_prom_read(data, i);
3075 case 0: /* 20MB/s */
3078 case 1: /* 10MB/s */
3087 default: /* default 20MB/s */
3091 entry = nsp32_search_period_entry(data, target, val);
3092 if (entry < 0 || trans_mode == ULTRA20M_MODE) {
3093 /* search failed... set maximum speed */
3096 target->limit_entry = entry;
3104 * Atmel AT24C01A (drived in 5V) serial EEPROM routines
3106 static int nsp32_prom_read(nsp32_hw_data *data, int romaddr)
3110 /* start condition */
3111 nsp32_prom_start(data);
3113 /* device address */
3114 nsp32_prom_write_bit(data, 1); /* 1 */
3115 nsp32_prom_write_bit(data, 0); /* 0 */
3116 nsp32_prom_write_bit(data, 1); /* 1 */
3117 nsp32_prom_write_bit(data, 0); /* 0 */
3118 nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
3119 nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
3120 nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
3122 /* R/W: W for dummy write */
3123 nsp32_prom_write_bit(data, 0);
3126 nsp32_prom_write_bit(data, 0);
3129 for (i = 7; i >= 0; i--) {
3130 nsp32_prom_write_bit(data, ((romaddr >> i) & 1));
3134 nsp32_prom_write_bit(data, 0);
3136 /* start condition */
3137 nsp32_prom_start(data);
3139 /* device address */
3140 nsp32_prom_write_bit(data, 1); /* 1 */
3141 nsp32_prom_write_bit(data, 0); /* 0 */
3142 nsp32_prom_write_bit(data, 1); /* 1 */
3143 nsp32_prom_write_bit(data, 0); /* 0 */
3144 nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
3145 nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
3146 nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
3149 nsp32_prom_write_bit(data, 1);
3152 nsp32_prom_write_bit(data, 0);
3156 for (i = 7; i >= 0; i--) {
3157 val += (nsp32_prom_read_bit(data) << i);
3161 nsp32_prom_write_bit(data, 1);
3163 /* stop condition */
3164 nsp32_prom_stop(data);
3169 static void nsp32_prom_set(nsp32_hw_data *data, int bit, int val)
3171 int base = data->BaseAddress;
3174 tmp = nsp32_index_read1(base, SERIAL_ROM_CTL);
3182 nsp32_index_write1(base, SERIAL_ROM_CTL, tmp);
3187 static int nsp32_prom_get(nsp32_hw_data *data, int bit)
3189 int base = data->BaseAddress;
3193 nsp32_msg(KERN_ERR, "return value is not appropriate");
3198 tmp = nsp32_index_read1(base, SERIAL_ROM_CTL) & bit;
3211 static void nsp32_prom_start (nsp32_hw_data *data)
3213 /* start condition */
3214 nsp32_prom_set(data, SCL, 1);
3215 nsp32_prom_set(data, SDA, 1);
3216 nsp32_prom_set(data, ENA, 1); /* output mode */
3217 nsp32_prom_set(data, SDA, 0); /* keeping SCL=1 and transiting
3218 * SDA 1->0 is start condition */
3219 nsp32_prom_set(data, SCL, 0);
3222 static void nsp32_prom_stop (nsp32_hw_data *data)
3224 /* stop condition */
3225 nsp32_prom_set(data, SCL, 1);
3226 nsp32_prom_set(data, SDA, 0);
3227 nsp32_prom_set(data, ENA, 1); /* output mode */
3228 nsp32_prom_set(data, SDA, 1);
3229 nsp32_prom_set(data, SCL, 0);
3232 static void nsp32_prom_write_bit(nsp32_hw_data *data, int val)
3235 nsp32_prom_set(data, SDA, val);
3236 nsp32_prom_set(data, SCL, 1 );
3237 nsp32_prom_set(data, SCL, 0 );
3240 static int nsp32_prom_read_bit(nsp32_hw_data *data)
3245 nsp32_prom_set(data, ENA, 0); /* input mode */
3246 nsp32_prom_set(data, SCL, 1);
3248 val = nsp32_prom_get(data, SDA);
3250 nsp32_prom_set(data, SCL, 0);
3251 nsp32_prom_set(data, ENA, 1); /* output mode */
3257 /**************************************************************************
3262 /* Device suspended */
3263 static int nsp32_suspend(struct pci_dev *pdev, pm_message_t state)
3265 struct Scsi_Host *host = pci_get_drvdata(pdev);
3267 nsp32_msg(KERN_INFO, "pci-suspend: pdev=0x%p, state.event=%x, slot=%s, host=0x%p",
3268 pdev, state.event, pci_name(pdev), host);
3270 pci_save_state (pdev);
3271 pci_disable_device (pdev);
3272 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3277 /* Device woken up */
3278 static int nsp32_resume(struct pci_dev *pdev)
3280 struct Scsi_Host *host = pci_get_drvdata(pdev);
3281 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
3284 nsp32_msg(KERN_INFO, "pci-resume: pdev=0x%p, slot=%s, host=0x%p", pdev, pci_name(pdev), host);
3286 pci_set_power_state(pdev, PCI_D0);
3287 pci_enable_wake (pdev, PCI_D0, 0);
3288 pci_restore_state (pdev);
3290 reg = nsp32_read2(data->BaseAddress, INDEX_REG);
3292 nsp32_msg(KERN_INFO, "io=0x%x reg=0x%x", data->BaseAddress, reg);
3294 if (reg == 0xffff) {
3295 nsp32_msg(KERN_INFO, "missing device. abort resume.");
3299 nsp32hw_init (data);
3300 nsp32_do_bus_reset(data);
3302 nsp32_msg(KERN_INFO, "resume success");
3309 /************************************************************************
3310 * PCI/Cardbus probe/remove routine
3312 static int nsp32_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3315 nsp32_hw_data *data = &nsp32_data_base;
3317 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
3319 ret = pci_enable_device(pdev);
3321 nsp32_msg(KERN_ERR, "failed to enable pci device");
3326 data->pci_devid = id;
3327 data->IrqNumber = pdev->irq;
3328 data->BaseAddress = pci_resource_start(pdev, 0);
3329 data->NumAddress = pci_resource_len (pdev, 0);
3330 data->MmioAddress = pci_ioremap_bar(pdev, 1);
3331 data->MmioLength = pci_resource_len (pdev, 1);
3333 pci_set_master(pdev);
3335 ret = nsp32_detect(pdev);
3337 nsp32_msg(KERN_INFO, "irq: %i mmio: %p+0x%lx slot: %s model: %s",
3339 data->MmioAddress, data->MmioLength,
3341 nsp32_model[id->driver_data]);
3343 nsp32_dbg(NSP32_DEBUG_REGISTER, "exit %d", ret);
3348 static void nsp32_remove(struct pci_dev *pdev)
3350 struct Scsi_Host *host = pci_get_drvdata(pdev);
3352 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
3354 scsi_remove_host(host);
3356 nsp32_release(host);
3358 scsi_host_put(host);
3361 static struct pci_driver nsp32_driver = {
3363 .id_table = nsp32_pci_table,
3364 .probe = nsp32_probe,
3365 .remove = nsp32_remove,
3367 .suspend = nsp32_suspend,
3368 .resume = nsp32_resume,
3372 /*********************************************************************
3375 static int __init init_nsp32(void) {
3376 nsp32_msg(KERN_INFO, "loading...");
3377 return pci_register_driver(&nsp32_driver);
3380 static void __exit exit_nsp32(void) {
3381 nsp32_msg(KERN_INFO, "unloading...");
3382 pci_unregister_driver(&nsp32_driver);
3385 module_init(init_nsp32);
3386 module_exit(exit_nsp32);