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/version.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/timer.h>
33 #include <linux/ioport.h>
34 #include <linux/major.h>
35 #include <linux/blkdev.h>
36 #include <linux/interrupt.h>
37 #include <linux/pci.h>
38 #include <linux/delay.h>
39 #include <linux/ctype.h>
40 #include <linux/dma-mapping.h>
43 #include <asm/system.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_cmnd.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_host.h>
50 #include <scsi/scsi_ioctl.h>
55 /***********************************************************************
58 static int trans_mode = 0; /* default: BIOS */
59 module_param (trans_mode, int, 0);
60 MODULE_PARM_DESC(trans_mode, "transfer mode (0: BIOS(default) 1: Async 2: Ultra20M");
62 #define ULTRA20M_MODE 2
64 static int auto_param = 0; /* default: ON */
65 module_param (auto_param, bool, 0);
66 MODULE_PARM_DESC(auto_param, "AutoParameter mode (0: ON(default) 1: OFF)");
68 static int disc_priv = 1; /* default: OFF */
69 module_param (disc_priv, bool, 0);
70 MODULE_PARM_DESC(disc_priv, "disconnection privilege mode (0: ON 1: OFF(default))");
72 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>, GOTO Masanori <gotom@debian.or.jp>");
73 MODULE_DESCRIPTION("Workbit NinjaSCSI-32Bi/UDE CardBus/PCI SCSI host bus adapter module");
74 MODULE_LICENSE("GPL");
76 static const char *nsp32_release_version = "1.2";
79 /****************************************************************************
82 static struct pci_device_id nsp32_pci_table[] __devinitdata = {
84 .vendor = PCI_VENDOR_ID_IODATA,
85 .device = PCI_DEVICE_ID_NINJASCSI_32BI_CBSC_II,
86 .subvendor = PCI_ANY_ID,
87 .subdevice = PCI_ANY_ID,
88 .driver_data = MODEL_IODATA,
91 .vendor = PCI_VENDOR_ID_WORKBIT,
92 .device = PCI_DEVICE_ID_NINJASCSI_32BI_KME,
93 .subvendor = PCI_ANY_ID,
94 .subdevice = PCI_ANY_ID,
95 .driver_data = MODEL_KME,
98 .vendor = PCI_VENDOR_ID_WORKBIT,
99 .device = PCI_DEVICE_ID_NINJASCSI_32BI_WBT,
100 .subvendor = PCI_ANY_ID,
101 .subdevice = PCI_ANY_ID,
102 .driver_data = MODEL_WORKBIT,
105 .vendor = PCI_VENDOR_ID_WORKBIT,
106 .device = PCI_DEVICE_ID_WORKBIT_STANDARD,
107 .subvendor = PCI_ANY_ID,
108 .subdevice = PCI_ANY_ID,
109 .driver_data = MODEL_PCI_WORKBIT,
112 .vendor = PCI_VENDOR_ID_WORKBIT,
113 .device = PCI_DEVICE_ID_NINJASCSI_32BI_LOGITEC,
114 .subvendor = PCI_ANY_ID,
115 .subdevice = PCI_ANY_ID,
116 .driver_data = MODEL_LOGITEC,
119 .vendor = PCI_VENDOR_ID_WORKBIT,
120 .device = PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC,
121 .subvendor = PCI_ANY_ID,
122 .subdevice = PCI_ANY_ID,
123 .driver_data = MODEL_PCI_LOGITEC,
126 .vendor = PCI_VENDOR_ID_WORKBIT,
127 .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO,
128 .subvendor = PCI_ANY_ID,
129 .subdevice = PCI_ANY_ID,
130 .driver_data = MODEL_PCI_MELCO,
133 .vendor = PCI_VENDOR_ID_WORKBIT,
134 .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO_II,
135 .subvendor = PCI_ANY_ID,
136 .subdevice = PCI_ANY_ID,
137 .driver_data = MODEL_PCI_MELCO,
141 MODULE_DEVICE_TABLE(pci, nsp32_pci_table);
143 static nsp32_hw_data nsp32_data_base; /* probe <-> detect glue */
147 * Period/AckWidth speed conversion table
149 * Note: This period/ackwidth speed table must be in descending order.
151 static nsp32_sync_table nsp32_sync_table_40M[] = {
152 /* {PNo, AW, SP, EP, SREQ smpl} Speed(MB/s) Period AckWidth */
153 {0x1, 0, 0x0c, 0x0c, SMPL_40M}, /* 20.0 : 50ns, 25ns */
154 {0x2, 0, 0x0d, 0x18, SMPL_40M}, /* 13.3 : 75ns, 25ns */
155 {0x3, 1, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
156 {0x4, 1, 0x1a, 0x1f, SMPL_20M}, /* 8.0 : 125ns, 50ns */
157 {0x5, 2, 0x20, 0x25, SMPL_20M}, /* 6.7 : 150ns, 75ns */
158 {0x6, 2, 0x26, 0x31, SMPL_20M}, /* 5.7 : 175ns, 75ns */
159 {0x7, 3, 0x32, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
160 {0x8, 3, 0x33, 0x38, SMPL_10M}, /* 4.4 : 225ns, 100ns */
161 {0x9, 3, 0x39, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
164 static nsp32_sync_table nsp32_sync_table_20M[] = {
165 {0x1, 0, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
166 {0x2, 0, 0x1a, 0x25, SMPL_20M}, /* 6.7 : 150ns, 50ns */
167 {0x3, 1, 0x26, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
168 {0x4, 1, 0x33, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
169 {0x5, 2, 0x3f, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 150ns */
170 {0x6, 2, 0x4c, 0x57, SMPL_10M}, /* 2.8 : 350ns, 150ns */
171 {0x7, 3, 0x58, 0x64, SMPL_10M}, /* 2.5 : 400ns, 200ns */
172 {0x8, 3, 0x65, 0x70, SMPL_10M}, /* 2.2 : 450ns, 200ns */
173 {0x9, 3, 0x71, 0x7d, SMPL_10M}, /* 2.0 : 500ns, 200ns */
176 static nsp32_sync_table nsp32_sync_table_pci[] = {
177 {0x1, 0, 0x0c, 0x0f, SMPL_40M}, /* 16.6 : 60ns, 30ns */
178 {0x2, 0, 0x10, 0x16, SMPL_40M}, /* 11.1 : 90ns, 30ns */
179 {0x3, 1, 0x17, 0x1e, SMPL_20M}, /* 8.3 : 120ns, 60ns */
180 {0x4, 1, 0x1f, 0x25, SMPL_20M}, /* 6.7 : 150ns, 60ns */
181 {0x5, 2, 0x26, 0x2d, SMPL_20M}, /* 5.6 : 180ns, 90ns */
182 {0x6, 2, 0x2e, 0x34, SMPL_10M}, /* 4.8 : 210ns, 90ns */
183 {0x7, 3, 0x35, 0x3c, SMPL_10M}, /* 4.2 : 240ns, 120ns */
184 {0x8, 3, 0x3d, 0x43, SMPL_10M}, /* 3.7 : 270ns, 120ns */
185 {0x9, 3, 0x44, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 120ns */
189 * function declaration
191 /* module entry point */
192 static int __devinit nsp32_probe (struct pci_dev *, const struct pci_device_id *);
193 static void __devexit nsp32_remove(struct pci_dev *);
194 static int __init init_nsp32 (void);
195 static void __exit exit_nsp32 (void);
197 /* struct struct scsi_host_template */
198 static int nsp32_proc_info (struct Scsi_Host *, char *, char **, off_t, int, int);
200 static int nsp32_detect (struct pci_dev *pdev);
201 static int nsp32_queuecommand(struct scsi_cmnd *,
202 void (*done)(struct scsi_cmnd *));
203 static const char *nsp32_info (struct Scsi_Host *);
204 static int nsp32_release (struct Scsi_Host *);
206 /* SCSI error handler */
207 static int nsp32_eh_abort (struct scsi_cmnd *);
208 static int nsp32_eh_bus_reset (struct scsi_cmnd *);
209 static int nsp32_eh_host_reset(struct scsi_cmnd *);
211 /* generate SCSI message */
212 static void nsp32_build_identify(struct scsi_cmnd *);
213 static void nsp32_build_nop (struct scsi_cmnd *);
214 static void nsp32_build_reject (struct scsi_cmnd *);
215 static void nsp32_build_sdtr (struct scsi_cmnd *, unsigned char, unsigned char);
217 /* SCSI message handler */
218 static int nsp32_busfree_occur(struct scsi_cmnd *, unsigned short);
219 static void nsp32_msgout_occur (struct scsi_cmnd *);
220 static void nsp32_msgin_occur (struct scsi_cmnd *, unsigned long, unsigned short);
222 static int nsp32_setup_sg_table (struct scsi_cmnd *);
223 static int nsp32_selection_autopara(struct scsi_cmnd *);
224 static int nsp32_selection_autoscsi(struct scsi_cmnd *);
225 static void nsp32_scsi_done (struct scsi_cmnd *);
226 static int nsp32_arbitration (struct scsi_cmnd *, unsigned int);
227 static int nsp32_reselection (struct scsi_cmnd *, unsigned char);
228 static void nsp32_adjust_busfree (struct scsi_cmnd *, unsigned int);
229 static void nsp32_restart_autoscsi (struct scsi_cmnd *, unsigned short);
232 static void nsp32_analyze_sdtr (struct scsi_cmnd *);
233 static int nsp32_search_period_entry(nsp32_hw_data *, nsp32_target *, unsigned char);
234 static void nsp32_set_async (nsp32_hw_data *, nsp32_target *);
235 static void nsp32_set_max_sync (nsp32_hw_data *, nsp32_target *, unsigned char *, unsigned char *);
236 static void nsp32_set_sync_entry (nsp32_hw_data *, nsp32_target *, int, unsigned char);
238 /* SCSI bus status handler */
239 static void nsp32_wait_req (nsp32_hw_data *, int);
240 static void nsp32_wait_sack (nsp32_hw_data *, int);
241 static void nsp32_sack_assert (nsp32_hw_data *);
242 static void nsp32_sack_negate (nsp32_hw_data *);
243 static void nsp32_do_bus_reset(nsp32_hw_data *);
245 /* hardware interrupt handler */
246 static irqreturn_t do_nsp32_isr(int, void *);
248 /* initialize hardware */
249 static int nsp32hw_init(nsp32_hw_data *);
252 static int nsp32_getprom_param (nsp32_hw_data *);
253 static int nsp32_getprom_at24 (nsp32_hw_data *);
254 static int nsp32_getprom_c16 (nsp32_hw_data *);
255 static void nsp32_prom_start (nsp32_hw_data *);
256 static void nsp32_prom_stop (nsp32_hw_data *);
257 static int nsp32_prom_read (nsp32_hw_data *, int);
258 static int nsp32_prom_read_bit (nsp32_hw_data *);
259 static void nsp32_prom_write_bit(nsp32_hw_data *, int);
260 static void nsp32_prom_set (nsp32_hw_data *, int, int);
261 static int nsp32_prom_get (nsp32_hw_data *, int);
263 /* debug/warning/info message */
264 static void nsp32_message (const char *, int, char *, char *, ...);
266 static void nsp32_dmessage(const char *, int, int, char *, ...);
270 * max_sectors is currently limited up to 128.
272 static struct scsi_host_template nsp32_template = {
273 .proc_name = "nsp32",
274 .name = "Workbit NinjaSCSI-32Bi/UDE",
275 .proc_info = nsp32_proc_info,
277 .queuecommand = nsp32_queuecommand,
279 .sg_tablesize = NSP32_SG_SIZE,
282 .this_id = NSP32_HOST_SCSIID,
283 .use_clustering = DISABLE_CLUSTERING,
284 .use_sg_chaining = ENABLE_SG_CHAINING,
285 .eh_abort_handler = nsp32_eh_abort,
286 .eh_bus_reset_handler = nsp32_eh_bus_reset,
287 .eh_host_reset_handler = nsp32_eh_host_reset,
288 /* .highmem_io = 1, */
291 #include "nsp32_io.h"
293 /***********************************************************************
297 # define NSP32_DEBUG_MASK 0x000000
298 # define nsp32_msg(type, args...) nsp32_message ("", 0, (type), args)
299 # define nsp32_dbg(mask, args...) /* */
301 # define NSP32_DEBUG_MASK 0xffffff
302 # define nsp32_msg(type, args...) \
303 nsp32_message (__FUNCTION__, __LINE__, (type), args)
304 # define nsp32_dbg(mask, args...) \
305 nsp32_dmessage(__FUNCTION__, __LINE__, (mask), args)
308 #define NSP32_DEBUG_QUEUECOMMAND BIT(0)
309 #define NSP32_DEBUG_REGISTER BIT(1)
310 #define NSP32_DEBUG_AUTOSCSI BIT(2)
311 #define NSP32_DEBUG_INTR BIT(3)
312 #define NSP32_DEBUG_SGLIST BIT(4)
313 #define NSP32_DEBUG_BUSFREE BIT(5)
314 #define NSP32_DEBUG_CDB_CONTENTS BIT(6)
315 #define NSP32_DEBUG_RESELECTION BIT(7)
316 #define NSP32_DEBUG_MSGINOCCUR BIT(8)
317 #define NSP32_DEBUG_EEPROM BIT(9)
318 #define NSP32_DEBUG_MSGOUTOCCUR BIT(10)
319 #define NSP32_DEBUG_BUSRESET BIT(11)
320 #define NSP32_DEBUG_RESTART BIT(12)
321 #define NSP32_DEBUG_SYNC BIT(13)
322 #define NSP32_DEBUG_WAIT BIT(14)
323 #define NSP32_DEBUG_TARGETFLAG BIT(15)
324 #define NSP32_DEBUG_PROC BIT(16)
325 #define NSP32_DEBUG_INIT BIT(17)
326 #define NSP32_SPECIAL_PRINT_REGISTER BIT(20)
328 #define NSP32_DEBUG_BUF_LEN 100
330 static void nsp32_message(const char *func, int line, char *type, char *fmt, ...)
333 char buf[NSP32_DEBUG_BUF_LEN];
336 vsnprintf(buf, sizeof(buf), fmt, args);
340 printk("%snsp32: %s\n", type, buf);
342 printk("%snsp32: %s (%d): %s\n", type, func, line, buf);
347 static void nsp32_dmessage(const char *func, int line, int mask, char *fmt, ...)
350 char buf[NSP32_DEBUG_BUF_LEN];
353 vsnprintf(buf, sizeof(buf), fmt, args);
356 if (mask & NSP32_DEBUG_MASK) {
357 printk("nsp32-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
363 # include "nsp32_debug.c"
365 # define show_command(arg) /* */
366 # define show_busphase(arg) /* */
367 # define show_autophase(arg) /* */
373 static void nsp32_build_identify(struct scsi_cmnd *SCpnt)
375 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
376 int pos = data->msgout_len;
379 /* XXX: Auto DiscPriv detection is progressing... */
380 if (disc_priv == 0) {
384 data->msgoutbuf[pos] = IDENTIFY(mode, SCpnt->device->lun); pos++;
386 data->msgout_len = pos;
390 * SDTR Message Routine
392 static void nsp32_build_sdtr(struct scsi_cmnd *SCpnt,
393 unsigned char period,
394 unsigned char offset)
396 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
397 int pos = data->msgout_len;
399 data->msgoutbuf[pos] = EXTENDED_MESSAGE; pos++;
400 data->msgoutbuf[pos] = EXTENDED_SDTR_LEN; pos++;
401 data->msgoutbuf[pos] = EXTENDED_SDTR; pos++;
402 data->msgoutbuf[pos] = period; pos++;
403 data->msgoutbuf[pos] = offset; pos++;
405 data->msgout_len = pos;
409 * No Operation Message
411 static void nsp32_build_nop(struct scsi_cmnd *SCpnt)
413 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
414 int pos = data->msgout_len;
417 nsp32_msg(KERN_WARNING,
418 "Some messages are already contained!");
422 data->msgoutbuf[pos] = NOP; pos++;
423 data->msgout_len = pos;
429 static void nsp32_build_reject(struct scsi_cmnd *SCpnt)
431 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
432 int pos = data->msgout_len;
434 data->msgoutbuf[pos] = MESSAGE_REJECT; pos++;
435 data->msgout_len = pos;
442 static void nsp32_start_timer(struct scsi_cmnd *SCpnt, int time)
444 unsigned int base = SCpnt->host->io_port;
446 nsp32_dbg(NSP32_DEBUG_INTR, "timer=%d", time);
448 if (time & (~TIMER_CNT_MASK)) {
449 nsp32_dbg(NSP32_DEBUG_INTR, "timer set overflow");
452 nsp32_write2(base, TIMER_SET, time & TIMER_CNT_MASK);
458 * set SCSI command and other parameter to asic, and start selection phase
460 static int nsp32_selection_autopara(struct scsi_cmnd *SCpnt)
462 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
463 unsigned int base = SCpnt->device->host->io_port;
464 unsigned int host_id = SCpnt->device->host->this_id;
465 unsigned char target = scmd_id(SCpnt);
466 nsp32_autoparam *param = data->autoparam;
472 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
477 phase = nsp32_read1(base, SCSI_BUS_MONITOR);
478 if (phase != BUSMON_BUS_FREE) {
479 nsp32_msg(KERN_WARNING, "bus busy");
480 show_busphase(phase & BUSMON_PHASE_MASK);
481 SCpnt->result = DID_BUS_BUSY << 16;
488 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
489 * over 3 messages needs another routine.
491 if (data->msgout_len == 0) {
492 nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
493 SCpnt->result = DID_ERROR << 16;
495 } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
497 for (i = 0; i < data->msgout_len; i++) {
499 * the sending order of the message is:
500 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
501 * MCNT 2: MSG#1 -> MSG#2
505 msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
507 msgout |= MV_VALID; /* MV valid */
508 msgout |= (unsigned int)data->msgout_len; /* len */
510 /* data->msgout_len > 3 */
514 // nsp_dbg(NSP32_DEBUG_AUTOSCSI, "sel time out=0x%x\n", nsp32_read2(base, SEL_TIME_OUT));
515 // nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
518 * setup asic parameter
520 memset(param, 0, sizeof(nsp32_autoparam));
523 for (i = 0; i < SCpnt->cmd_len; i++) {
524 param->cdb[4 * i] = SCpnt->cmnd[i];
527 /* outgoing messages */
528 param->msgout = cpu_to_le32(msgout);
530 /* syncreg, ackwidth, target id, SREQ sampling rate */
531 param->syncreg = data->cur_target->syncreg;
532 param->ackwidth = data->cur_target->ackwidth;
533 param->target_id = BIT(host_id) | BIT(target);
534 param->sample_reg = data->cur_target->sample_reg;
536 // nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "sample rate=0x%x\n", data->cur_target->sample_reg);
538 /* command control */
539 param->command_control = cpu_to_le16(CLEAR_CDB_FIFO_POINTER |
541 AUTO_MSGIN_00_OR_04 |
546 /* transfer control */
548 switch (data->trans_method) {
549 case NSP32_TRANSFER_BUSMASTER:
552 case NSP32_TRANSFER_MMIO:
555 case NSP32_TRANSFER_PIO:
559 nsp32_msg(KERN_ERR, "unknown trans_method");
563 * OR-ed BLIEND_MODE, FIFO intr is decreased, instead of PCI bus waits.
564 * For bus master transfer, it's taken off.
566 s |= (TRANSFER_GO | ALL_COUNTER_CLR);
567 param->transfer_control = cpu_to_le16(s);
570 param->sgt_pointer = cpu_to_le32(data->cur_lunt->sglun_paddr);
573 * transfer parameter to ASIC
575 nsp32_write4(base, SGT_ADR, data->auto_paddr);
576 nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER |
582 ret = nsp32_arbitration(SCpnt, base);
589 * Selection with AUTO SCSI (without AUTO PARAMETER)
591 static int nsp32_selection_autoscsi(struct scsi_cmnd *SCpnt)
593 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
594 unsigned int base = SCpnt->device->host->io_port;
595 unsigned int host_id = SCpnt->device->host->this_id;
596 unsigned char target = scmd_id(SCpnt);
599 unsigned short command = 0;
600 unsigned int msgout = 0;
601 unsigned short execph;
604 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
609 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
614 phase = nsp32_read1(base, SCSI_BUS_MONITOR);
615 if(((phase & BUSMON_BSY) == 1) || (phase & BUSMON_SEL) == 1) {
616 nsp32_msg(KERN_WARNING, "bus busy");
617 SCpnt->result = DID_BUS_BUSY << 16;
625 execph = nsp32_read2(base, SCSI_EXECUTE_PHASE);
628 * clear FIFO counter to set CDBs
630 nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER);
635 for (i = 0; i < SCpnt->cmd_len; i++) {
636 nsp32_write1(base, COMMAND_DATA, SCpnt->cmnd[i]);
638 nsp32_dbg(NSP32_DEBUG_CDB_CONTENTS, "CDB[0]=[0x%x]", SCpnt->cmnd[0]);
641 * set SCSIOUT LATCH(initiator)/TARGET(target) (OR-ed) ID
643 nsp32_write1(base, SCSI_OUT_LATCH_TARGET_ID, BIT(host_id) | BIT(target));
646 * set SCSI MSGOUT REG
648 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
649 * over 3 messages needs another routine.
651 if (data->msgout_len == 0) {
652 nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
653 SCpnt->result = DID_ERROR << 16;
656 } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
658 for (i = 0; i < data->msgout_len; i++) {
660 * the sending order of the message is:
661 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
662 * MCNT 2: MSG#1 -> MSG#2
666 msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
668 msgout |= MV_VALID; /* MV valid */
669 msgout |= (unsigned int)data->msgout_len; /* len */
670 nsp32_write4(base, SCSI_MSG_OUT, msgout);
672 /* data->msgout_len > 3 */
673 nsp32_write4(base, SCSI_MSG_OUT, 0);
677 * set selection timeout(= 250ms)
679 nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
682 * set SREQ hazard killer sampling rate
684 * TODO: sample_rate (BASE+0F) is 0 when internal clock = 40MHz.
685 * check other internal clock!
687 nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
692 nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
696 * Don't set BM_START_ADR before setting this register.
698 nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
703 nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
705 nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
706 "syncreg=0x%x, ackwidth=0x%x, sgtpaddr=0x%x, id=0x%x",
707 nsp32_read1(base, SYNC_REG), nsp32_read1(base, ACK_WIDTH),
708 nsp32_read4(base, SGT_ADR), nsp32_read1(base, SCSI_OUT_LATCH_TARGET_ID));
709 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "msgout_len=%d, msgout=0x%x",
710 data->msgout_len, msgout);
713 * set SGT ADDR (physical address)
715 nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
718 * set TRANSFER CONTROL REG
721 command |= (TRANSFER_GO | ALL_COUNTER_CLR);
722 if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
723 if (scsi_bufflen(SCpnt) > 0) {
726 } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
727 command |= CB_MMIO_MODE;
728 } else if (data->trans_method & NSP32_TRANSFER_PIO) {
729 command |= CB_IO_MODE;
731 nsp32_write2(base, TRANSFER_CONTROL, command);
734 * start AUTO SCSI, kick off arbitration
736 command = (CLEAR_CDB_FIFO_POINTER |
738 AUTO_MSGIN_00_OR_04 |
741 nsp32_write2(base, COMMAND_CONTROL, command);
746 status = nsp32_arbitration(SCpnt, base);
752 nsp32_write2(base, IRQ_CONTROL, 0);
759 * Arbitration Status Check
761 * Note: Arbitration counter is waited during ARBIT_GO is not lifting.
762 * Using udelay(1) consumes CPU time and system time, but
763 * arbitration delay time is defined minimal 2.4us in SCSI
764 * specification, thus udelay works as coarse grained wait timer.
766 static int nsp32_arbitration(struct scsi_cmnd *SCpnt, unsigned int base)
773 arbit = nsp32_read1(base, ARBIT_STATUS);
775 } while ((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
776 (time <= ARBIT_TIMEOUT_TIME));
778 nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
779 "arbit: 0x%x, delay time: %d", arbit, time);
781 if (arbit & ARBIT_WIN) {
782 /* Arbitration succeeded */
783 SCpnt->result = DID_OK << 16;
784 nsp32_index_write1(base, EXT_PORT, LED_ON); /* PCI LED on */
785 } else if (arbit & ARBIT_FAIL) {
786 /* Arbitration failed */
787 SCpnt->result = DID_BUS_BUSY << 16;
791 * unknown error or ARBIT_GO timeout,
792 * something lock up! guess no connection.
794 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "arbit timeout");
795 SCpnt->result = DID_NO_CONNECT << 16;
802 nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
811 * Note: This reselection routine is called from msgin_occur,
812 * reselection target id&lun must be already set.
813 * SCSI-2 says IDENTIFY implies RESTORE_POINTER operation.
815 static int nsp32_reselection(struct scsi_cmnd *SCpnt, unsigned char newlun)
817 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
818 unsigned int host_id = SCpnt->device->host->this_id;
819 unsigned int base = SCpnt->device->host->io_port;
820 unsigned char tmpid, newid;
822 nsp32_dbg(NSP32_DEBUG_RESELECTION, "enter");
825 * calculate reselected SCSI ID
827 tmpid = nsp32_read1(base, RESELECT_ID);
828 tmpid &= (~BIT(host_id));
839 * If reselected New ID:LUN is not existed
840 * or current nexus is not existed, unexpected
841 * reselection is occurred. Send reject message.
843 if (newid >= ARRAY_SIZE(data->lunt) || newlun >= ARRAY_SIZE(data->lunt[0])) {
844 nsp32_msg(KERN_WARNING, "unknown id/lun");
846 } else if(data->lunt[newid][newlun].SCpnt == NULL) {
847 nsp32_msg(KERN_WARNING, "no SCSI command is processing");
851 data->cur_id = newid;
852 data->cur_lun = newlun;
853 data->cur_target = &(data->target[newid]);
854 data->cur_lunt = &(data->lunt[newid][newlun]);
856 /* reset SACK/SavedACK counter (or ALL clear?) */
857 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
864 * nsp32_setup_sg_table - build scatter gather list for transfer data
867 * Note: NinjaSCSI-32Bi/UDE bus master can not transfer over 64KB at a time.
869 static int nsp32_setup_sg_table(struct scsi_cmnd *SCpnt)
871 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
872 struct scatterlist *sg;
873 nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
878 nsp32_dbg(NSP32_DEBUG_SGLIST, "SGT == null");
882 num = scsi_dma_map(SCpnt);
888 scsi_for_each_sg(SCpnt, sg, num, i) {
890 * Build nsp32_sglist, substitute sg dma addresses.
892 sgt[i].addr = cpu_to_le32(sg_dma_address(sg));
893 sgt[i].len = cpu_to_le32(sg_dma_len(sg));
895 if (le32_to_cpu(sgt[i].len) > 0x10000) {
897 "can't transfer over 64KB at a time, size=0x%lx", le32_to_cpu(sgt[i].len));
900 nsp32_dbg(NSP32_DEBUG_SGLIST,
901 "num 0x%x : addr 0x%lx len 0x%lx",
903 le32_to_cpu(sgt[i].addr),
904 le32_to_cpu(sgt[i].len ));
908 l = le32_to_cpu(sgt[num-1].len);
909 sgt[num-1].len = cpu_to_le32(l | SGTEND);
915 static int nsp32_queuecommand(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
917 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
918 nsp32_target *target;
919 nsp32_lunt *cur_lunt;
922 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
923 "enter. target: 0x%x LUN: 0x%x cmnd: 0x%x cmndlen: 0x%x "
924 "use_sg: 0x%x reqbuf: 0x%lx reqlen: 0x%x",
925 SCpnt->device->id, SCpnt->device->lun, SCpnt->cmnd[0], SCpnt->cmd_len,
926 scsi_sg_count(SCpnt), scsi_sglist(SCpnt), scsi_bufflen(SCpnt));
928 if (data->CurrentSC != NULL) {
929 nsp32_msg(KERN_ERR, "Currentsc != NULL. Cancel this command request");
930 data->CurrentSC = NULL;
931 SCpnt->result = DID_NO_CONNECT << 16;
936 /* check target ID is not same as this initiator ID */
937 if (scmd_id(SCpnt) == SCpnt->device->host->this_id) {
938 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "terget==host???");
939 SCpnt->result = DID_BAD_TARGET << 16;
944 /* check target LUN is allowable value */
945 if (SCpnt->device->lun >= MAX_LUN) {
946 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "no more lun");
947 SCpnt->result = DID_BAD_TARGET << 16;
954 SCpnt->scsi_done = done;
955 data->CurrentSC = SCpnt;
956 SCpnt->SCp.Status = CHECK_CONDITION;
957 SCpnt->SCp.Message = 0;
958 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
960 SCpnt->SCp.ptr = (char *)scsi_sglist(SCpnt);
961 SCpnt->SCp.this_residual = scsi_bufflen(SCpnt);
962 SCpnt->SCp.buffer = NULL;
963 SCpnt->SCp.buffers_residual = 0;
965 /* initialize data */
966 data->msgout_len = 0;
968 cur_lunt = &(data->lunt[SCpnt->device->id][SCpnt->device->lun]);
969 cur_lunt->SCpnt = SCpnt;
970 cur_lunt->save_datp = 0;
971 cur_lunt->msgin03 = FALSE;
972 data->cur_lunt = cur_lunt;
973 data->cur_id = SCpnt->device->id;
974 data->cur_lun = SCpnt->device->lun;
976 ret = nsp32_setup_sg_table(SCpnt);
978 nsp32_msg(KERN_ERR, "SGT fail");
979 SCpnt->result = DID_ERROR << 16;
980 nsp32_scsi_done(SCpnt);
985 nsp32_build_identify(SCpnt);
988 * If target is the first time to transfer after the reset
989 * (target don't have SDTR_DONE and SDTR_INITIATOR), sync
990 * message SDTR is needed to do synchronous transfer.
992 target = &data->target[scmd_id(SCpnt)];
993 data->cur_target = target;
995 if (!(target->sync_flag & (SDTR_DONE | SDTR_INITIATOR | SDTR_TARGET))) {
996 unsigned char period, offset;
998 if (trans_mode != ASYNC_MODE) {
999 nsp32_set_max_sync(data, target, &period, &offset);
1000 nsp32_build_sdtr(SCpnt, period, offset);
1001 target->sync_flag |= SDTR_INITIATOR;
1003 nsp32_set_async(data, target);
1004 target->sync_flag |= SDTR_DONE;
1007 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
1008 "SDTR: entry: %d start_period: 0x%x offset: 0x%x\n",
1009 target->limit_entry, period, offset);
1010 } else if (target->sync_flag & SDTR_INITIATOR) {
1012 * It was negotiating SDTR with target, sending from the
1013 * initiator, but there are no chance to remove this flag.
1014 * Set async because we don't get proper negotiation.
1016 nsp32_set_async(data, target);
1017 target->sync_flag &= ~SDTR_INITIATOR;
1018 target->sync_flag |= SDTR_DONE;
1020 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
1021 "SDTR_INITIATOR: fall back to async");
1022 } else if (target->sync_flag & SDTR_TARGET) {
1024 * It was negotiating SDTR with target, sending from target,
1025 * but there are no chance to remove this flag. Set async
1026 * because we don't get proper negotiation.
1028 nsp32_set_async(data, target);
1029 target->sync_flag &= ~SDTR_TARGET;
1030 target->sync_flag |= SDTR_DONE;
1032 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
1033 "Unknown SDTR from target is reached, fall back to async.");
1036 nsp32_dbg(NSP32_DEBUG_TARGETFLAG,
1037 "target: %d sync_flag: 0x%x syncreg: 0x%x ackwidth: 0x%x",
1038 SCpnt->device->id, target->sync_flag, target->syncreg,
1042 if (auto_param == 0) {
1043 ret = nsp32_selection_autopara(SCpnt);
1045 ret = nsp32_selection_autoscsi(SCpnt);
1049 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "selection fail");
1050 nsp32_scsi_done(SCpnt);
1056 /* initialize asic */
1057 static int nsp32hw_init(nsp32_hw_data *data)
1059 unsigned int base = data->BaseAddress;
1060 unsigned short irq_stat;
1061 unsigned long lc_reg;
1062 unsigned char power;
1064 lc_reg = nsp32_index_read4(base, CFG_LATE_CACHE);
1065 if ((lc_reg & 0xff00) == 0) {
1066 lc_reg |= (0x20 << 8);
1067 nsp32_index_write2(base, CFG_LATE_CACHE, lc_reg & 0xffff);
1070 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
1071 nsp32_write2(base, TRANSFER_CONTROL, 0);
1072 nsp32_write4(base, BM_CNT, 0);
1073 nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
1076 irq_stat = nsp32_read2(base, IRQ_STATUS);
1077 nsp32_dbg(NSP32_DEBUG_INIT, "irq_stat 0x%x", irq_stat);
1078 } while (irq_stat & IRQSTATUS_ANY_IRQ);
1081 * Fill FIFO_FULL_SHLD, FIFO_EMPTY_SHLD. Below parameter is
1082 * designated by specification.
1084 if ((data->trans_method & NSP32_TRANSFER_PIO) ||
1085 (data->trans_method & NSP32_TRANSFER_MMIO)) {
1086 nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x40);
1087 nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x40);
1088 } else if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
1089 nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x10);
1090 nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x60);
1092 nsp32_dbg(NSP32_DEBUG_INIT, "unknown transfer mode");
1095 nsp32_dbg(NSP32_DEBUG_INIT, "full 0x%x emp 0x%x",
1096 nsp32_index_read1(base, FIFO_FULL_SHLD_COUNT),
1097 nsp32_index_read1(base, FIFO_EMPTY_SHLD_COUNT));
1099 nsp32_index_write1(base, CLOCK_DIV, data->clock);
1100 nsp32_index_write1(base, BM_CYCLE, MEMRD_CMD1 | SGT_AUTO_PARA_MEMED_CMD);
1101 nsp32_write1(base, PARITY_CONTROL, 0); /* parity check is disable */
1104 * initialize MISC_WRRD register
1106 * Note: Designated parameters is obeyed as following:
1107 * MISC_SCSI_DIRECTION_DETECTOR_SELECT: It must be set.
1108 * MISC_MASTER_TERMINATION_SELECT: It must be set.
1109 * MISC_BMREQ_NEGATE_TIMING_SEL: It should be set.
1110 * MISC_AUTOSEL_TIMING_SEL: It should be set.
1111 * MISC_BMSTOP_CHANGE2_NONDATA_PHASE: It should be set.
1112 * MISC_DELAYED_BMSTART: It's selected for safety.
1114 * Note: If MISC_BMSTOP_CHANGE2_NONDATA_PHASE is set, then
1115 * we have to set TRANSFERCONTROL_BM_START as 0 and set
1116 * appropriate value before restarting bus master transfer.
1118 nsp32_index_write2(base, MISC_WR,
1119 (SCSI_DIRECTION_DETECTOR_SELECT |
1121 MASTER_TERMINATION_SELECT |
1122 BMREQ_NEGATE_TIMING_SEL |
1123 AUTOSEL_TIMING_SEL |
1124 BMSTOP_CHANGE2_NONDATA_PHASE));
1126 nsp32_index_write1(base, TERM_PWR_CONTROL, 0);
1127 power = nsp32_index_read1(base, TERM_PWR_CONTROL);
1128 if (!(power & SENSE)) {
1129 nsp32_msg(KERN_INFO, "term power on");
1130 nsp32_index_write1(base, TERM_PWR_CONTROL, BPWR);
1133 nsp32_write2(base, TIMER_SET, TIMER_STOP);
1134 nsp32_write2(base, TIMER_SET, TIMER_STOP); /* Required 2 times */
1136 nsp32_write1(base, SYNC_REG, 0);
1137 nsp32_write1(base, ACK_WIDTH, 0);
1138 nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
1141 * enable to select designated IRQ (except for
1142 * IRQSELECT_SERR, IRQSELECT_PERR, IRQSELECT_BMCNTERR)
1144 nsp32_index_write2(base, IRQ_SELECT, IRQSELECT_TIMER_IRQ |
1145 IRQSELECT_SCSIRESET_IRQ |
1146 IRQSELECT_FIFO_SHLD_IRQ |
1147 IRQSELECT_RESELECT_IRQ |
1148 IRQSELECT_PHASE_CHANGE_IRQ |
1149 IRQSELECT_AUTO_SCSI_SEQ_IRQ |
1150 // IRQSELECT_BMCNTERR_IRQ |
1151 IRQSELECT_TARGET_ABORT_IRQ |
1152 IRQSELECT_MASTER_ABORT_IRQ );
1153 nsp32_write2(base, IRQ_CONTROL, 0);
1156 nsp32_index_write1(base, EXT_PORT_DDR, LED_OFF);
1157 nsp32_index_write1(base, EXT_PORT, LED_OFF);
1163 /* interrupt routine */
1164 static irqreturn_t do_nsp32_isr(int irq, void *dev_id)
1166 nsp32_hw_data *data = dev_id;
1167 unsigned int base = data->BaseAddress;
1168 struct scsi_cmnd *SCpnt = data->CurrentSC;
1169 unsigned short auto_stat, irq_stat, trans_stat;
1170 unsigned char busmon, busphase;
1171 unsigned long flags;
1174 struct Scsi_Host *host = data->Host;
1176 spin_lock_irqsave(host->host_lock, flags);
1179 * IRQ check, then enable IRQ mask
1181 irq_stat = nsp32_read2(base, IRQ_STATUS);
1182 nsp32_dbg(NSP32_DEBUG_INTR,
1183 "enter IRQ: %d, IRQstatus: 0x%x", irq, irq_stat);
1184 /* is this interrupt comes from Ninja asic? */
1185 if ((irq_stat & IRQSTATUS_ANY_IRQ) == 0) {
1186 nsp32_dbg(NSP32_DEBUG_INTR, "shared interrupt: irq other 0x%x", irq_stat);
1190 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
1192 busmon = nsp32_read1(base, SCSI_BUS_MONITOR);
1193 busphase = busmon & BUSMON_PHASE_MASK;
1195 trans_stat = nsp32_read2(base, TRANSFER_STATUS);
1196 if ((irq_stat == 0xffff) && (trans_stat == 0xffff)) {
1197 nsp32_msg(KERN_INFO, "card disconnect");
1198 if (data->CurrentSC != NULL) {
1199 nsp32_msg(KERN_INFO, "clean up current SCSI command");
1200 SCpnt->result = DID_BAD_TARGET << 16;
1201 nsp32_scsi_done(SCpnt);
1207 if (irq_stat & IRQSTATUS_TIMER_IRQ) {
1208 nsp32_dbg(NSP32_DEBUG_INTR, "timer stop");
1209 nsp32_write2(base, TIMER_SET, TIMER_STOP);
1214 if (irq_stat & IRQSTATUS_SCSIRESET_IRQ) {
1215 nsp32_msg(KERN_INFO, "detected someone do bus reset");
1216 nsp32_do_bus_reset(data);
1217 if (SCpnt != NULL) {
1218 SCpnt->result = DID_RESET << 16;
1219 nsp32_scsi_done(SCpnt);
1224 if (SCpnt == NULL) {
1225 nsp32_msg(KERN_WARNING, "SCpnt==NULL this can't be happened");
1226 nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
1231 * AutoSCSI Interrupt.
1232 * Note: This interrupt is occurred when AutoSCSI is finished. Then
1233 * check SCSIEXECUTEPHASE, and do appropriate action. Each phases are
1234 * recorded when AutoSCSI sequencer has been processed.
1236 if(irq_stat & IRQSTATUS_AUTOSCSI_IRQ) {
1237 /* getting SCSI executed phase */
1238 auto_stat = nsp32_read2(base, SCSI_EXECUTE_PHASE);
1239 nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
1241 /* Selection Timeout, go busfree phase. */
1242 if (auto_stat & SELECTION_TIMEOUT) {
1243 nsp32_dbg(NSP32_DEBUG_INTR,
1244 "selection timeout occurred");
1246 SCpnt->result = DID_TIME_OUT << 16;
1247 nsp32_scsi_done(SCpnt);
1251 if (auto_stat & MSGOUT_PHASE) {
1253 * MsgOut phase was processed.
1254 * If MSG_IN_OCCUER is not set, then MsgOut phase is
1255 * completed. Thus, msgout_len must reset. Otherwise,
1256 * nothing to do here. If MSG_OUT_OCCUER is occurred,
1257 * then we will encounter the condition and check.
1259 if (!(auto_stat & MSG_IN_OCCUER) &&
1260 (data->msgout_len <= 3)) {
1262 * !MSG_IN_OCCUER && msgout_len <=3
1263 * ---> AutoSCSI with MSGOUTreg is processed.
1265 data->msgout_len = 0;
1268 nsp32_dbg(NSP32_DEBUG_INTR, "MsgOut phase processed");
1271 if ((auto_stat & DATA_IN_PHASE) &&
1272 (scsi_get_resid(SCpnt) > 0) &&
1273 ((nsp32_read2(base, FIFO_REST_CNT) & FIFO_REST_MASK) != 0)) {
1274 printk( "auto+fifo\n");
1275 //nsp32_pio_read(SCpnt);
1278 if (auto_stat & (DATA_IN_PHASE | DATA_OUT_PHASE)) {
1279 /* DATA_IN_PHASE/DATA_OUT_PHASE was processed. */
1280 nsp32_dbg(NSP32_DEBUG_INTR,
1281 "Data in/out phase processed");
1283 /* read BMCNT, SGT pointer addr */
1284 nsp32_dbg(NSP32_DEBUG_INTR, "BMCNT=0x%lx",
1285 nsp32_read4(base, BM_CNT));
1286 nsp32_dbg(NSP32_DEBUG_INTR, "addr=0x%lx",
1287 nsp32_read4(base, SGT_ADR));
1288 nsp32_dbg(NSP32_DEBUG_INTR, "SACK=0x%lx",
1289 nsp32_read4(base, SACK_CNT));
1290 nsp32_dbg(NSP32_DEBUG_INTR, "SSACK=0x%lx",
1291 nsp32_read4(base, SAVED_SACK_CNT));
1293 scsi_set_resid(SCpnt, 0); /* all data transfered! */
1299 if (auto_stat & MSG_IN_OCCUER) {
1300 nsp32_msgin_occur(SCpnt, irq_stat, auto_stat);
1306 if (auto_stat & MSG_OUT_OCCUER) {
1307 nsp32_msgout_occur(SCpnt);
1313 if (auto_stat & BUS_FREE_OCCUER) {
1314 ret = nsp32_busfree_occur(SCpnt, auto_stat);
1320 if (auto_stat & STATUS_PHASE) {
1322 * Read CSB and substitute CSB for SCpnt->result
1323 * to save status phase stutas byte.
1324 * scsi error handler checks host_byte (DID_*:
1325 * low level driver to indicate status), then checks
1326 * status_byte (SCSI status byte).
1328 SCpnt->result = (int)nsp32_read1(base, SCSI_CSB_IN);
1331 if (auto_stat & ILLEGAL_PHASE) {
1332 /* Illegal phase is detected. SACK is not back. */
1333 nsp32_msg(KERN_WARNING,
1334 "AUTO SCSI ILLEGAL PHASE OCCUR!!!!");
1336 /* TODO: currently we don't have any action... bus reset? */
1339 * To send back SACK, assert, wait, and negate.
1341 nsp32_sack_assert(data);
1342 nsp32_wait_req(data, NEGATE);
1343 nsp32_sack_negate(data);
1347 if (auto_stat & COMMAND_PHASE) {
1349 nsp32_dbg(NSP32_DEBUG_INTR, "Command phase processed");
1352 if (auto_stat & AUTOSCSI_BUSY) {
1353 /* AutoSCSI is running */
1356 show_autophase(auto_stat);
1360 if (irq_stat & IRQSTATUS_FIFO_SHLD_IRQ) {
1361 nsp32_dbg(NSP32_DEBUG_INTR, "FIFO IRQ");
1364 case BUSPHASE_DATA_OUT:
1365 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/write");
1367 //nsp32_pio_write(SCpnt);
1371 case BUSPHASE_DATA_IN:
1372 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/read");
1374 //nsp32_pio_read(SCpnt);
1378 case BUSPHASE_STATUS:
1379 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/status");
1381 SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
1385 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/other phase");
1386 nsp32_dbg(NSP32_DEBUG_INTR, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
1387 show_busphase(busphase);
1394 /* Phase Change IRQ */
1395 if (irq_stat & IRQSTATUS_PHASE_CHANGE_IRQ) {
1396 nsp32_dbg(NSP32_DEBUG_INTR, "phase change IRQ");
1399 case BUSPHASE_MESSAGE_IN:
1400 nsp32_dbg(NSP32_DEBUG_INTR, "phase chg/msg in");
1401 nsp32_msgin_occur(SCpnt, irq_stat, 0);
1404 nsp32_msg(KERN_WARNING, "phase chg/other phase?");
1405 nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x\n",
1406 irq_stat, trans_stat);
1407 show_busphase(busphase);
1414 if (irq_stat & IRQSTATUS_PCI_IRQ) {
1415 nsp32_dbg(NSP32_DEBUG_INTR, "PCI IRQ occurred");
1420 if (irq_stat & IRQSTATUS_BMCNTERR_IRQ) {
1421 nsp32_msg(KERN_ERR, "Received unexpected BMCNTERR IRQ! ");
1423 * TODO: To be implemented improving bus master
1424 * transfer reliablity when BMCNTERR is occurred in
1425 * AutoSCSI phase described in specification.
1430 nsp32_dbg(NSP32_DEBUG_INTR,
1431 "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
1432 show_busphase(busphase);
1436 /* disable IRQ mask */
1437 nsp32_write2(base, IRQ_CONTROL, 0);
1440 spin_unlock_irqrestore(host->host_lock, flags);
1442 nsp32_dbg(NSP32_DEBUG_INTR, "exit");
1444 return IRQ_RETVAL(handled);
1448 #define SPRINTF(args...) \
1450 if(length > (pos - buffer)) { \
1451 pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
1452 nsp32_dbg(NSP32_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length, length - (pos - buffer));\
1456 static int nsp32_proc_info(struct Scsi_Host *host, char *buffer, char **start,
1457 off_t offset, int length, int inout)
1461 unsigned long flags;
1462 nsp32_hw_data *data;
1465 unsigned char mode_reg;
1469 /* Write is not supported, just return. */
1470 if (inout == TRUE) {
1474 hostno = host->host_no;
1475 data = (nsp32_hw_data *)host->hostdata;
1476 base = host->io_port;
1478 SPRINTF("NinjaSCSI-32 status\n\n");
1479 SPRINTF("Driver version: %s, $Revision: 1.33 $\n", nsp32_release_version);
1480 SPRINTF("SCSI host No.: %d\n", hostno);
1481 SPRINTF("IRQ: %d\n", host->irq);
1482 SPRINTF("IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
1483 SPRINTF("MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
1484 SPRINTF("sg_tablesize: %d\n", host->sg_tablesize);
1485 SPRINTF("Chip revision: 0x%x\n", (nsp32_read2(base, INDEX_REG) >> 8) & 0xff);
1487 mode_reg = nsp32_index_read1(base, CHIP_MODE);
1488 model = data->pci_devid->driver_data;
1491 SPRINTF("Power Management: %s\n", (mode_reg & OPTF) ? "yes" : "no");
1493 SPRINTF("OEM: %ld, %s\n", (mode_reg & (OEM0|OEM1)), nsp32_model[model]);
1495 spin_lock_irqsave(&(data->Lock), flags);
1496 SPRINTF("CurrentSC: 0x%p\n\n", data->CurrentSC);
1497 spin_unlock_irqrestore(&(data->Lock), flags);
1500 SPRINTF("SDTR status\n");
1501 for (id = 0; id < ARRAY_SIZE(data->target); id++) {
1503 SPRINTF("id %d: ", id);
1505 if (id == host->this_id) {
1506 SPRINTF("----- NinjaSCSI-32 host adapter\n");
1510 if (data->target[id].sync_flag == SDTR_DONE) {
1511 if (data->target[id].period == 0 &&
1512 data->target[id].offset == ASYNC_OFFSET ) {
1521 if (data->target[id].period != 0) {
1523 speed = 1000000 / (data->target[id].period * 4);
1525 SPRINTF(" transfer %d.%dMB/s, offset %d",
1528 data->target[id].offset
1535 thislength = pos - (buffer + offset);
1537 if(thislength < 0) {
1543 thislength = min(thislength, length);
1544 *start = buffer + offset;
1553 * Reset parameters and call scsi_done for data->cur_lunt.
1554 * Be careful setting SCpnt->result = DID_* before calling this function.
1556 static void nsp32_scsi_done(struct scsi_cmnd *SCpnt)
1558 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1559 unsigned int base = SCpnt->device->host->io_port;
1561 scsi_dma_unmap(SCpnt);
1564 * clear TRANSFERCONTROL_BM_START
1566 nsp32_write2(base, TRANSFER_CONTROL, 0);
1567 nsp32_write4(base, BM_CNT, 0);
1572 (*SCpnt->scsi_done)(SCpnt);
1577 data->cur_lunt->SCpnt = NULL;
1578 data->cur_lunt = NULL;
1579 data->cur_target = NULL;
1580 data->CurrentSC = NULL;
1587 * Current Phase is BUSFREE. AutoSCSI is automatically execute BUSFREE phase
1588 * with ACK reply when below condition is matched:
1589 * MsgIn 00: Command Complete.
1590 * MsgIn 02: Save Data Pointer.
1591 * MsgIn 04: Diconnect.
1592 * In other case, unexpected BUSFREE is detected.
1594 static int nsp32_busfree_occur(struct scsi_cmnd *SCpnt, unsigned short execph)
1596 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1597 unsigned int base = SCpnt->device->host->io_port;
1599 nsp32_dbg(NSP32_DEBUG_BUSFREE, "enter execph=0x%x", execph);
1600 show_autophase(execph);
1602 nsp32_write4(base, BM_CNT, 0);
1603 nsp32_write2(base, TRANSFER_CONTROL, 0);
1606 * MsgIn 02: Save Data Pointer
1609 * Save Data Pointer is received. Adjust pointer.
1612 * SCSI-3 says if Save Data Pointer is not received, then we restart
1613 * processing and we can't adjust any SCSI data pointer in next data
1616 if (execph & MSGIN_02_VALID) {
1617 nsp32_dbg(NSP32_DEBUG_BUSFREE, "MsgIn02_Valid");
1620 * Check sack_cnt/saved_sack_cnt, then adjust sg table if
1623 if (!(execph & MSGIN_00_VALID) &&
1624 ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE))) {
1625 unsigned int sacklen, s_sacklen;
1628 * Read SACK count and SAVEDSACK count, then compare.
1630 sacklen = nsp32_read4(base, SACK_CNT );
1631 s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
1634 * If SAVEDSACKCNT == 0, it means SavedDataPointer is
1635 * come after data transfering.
1637 if (s_sacklen > 0) {
1639 * Comparing between sack and savedsack to
1640 * check the condition of AutoMsgIn03.
1642 * If they are same, set msgin03 == TRUE,
1643 * COMMANDCONTROL_AUTO_MSGIN_03 is enabled at
1644 * reselection. On the other hand, if they
1645 * aren't same, set msgin03 == FALSE, and
1646 * COMMANDCONTROL_AUTO_MSGIN_03 is disabled at
1649 if (sacklen != s_sacklen) {
1650 data->cur_lunt->msgin03 = FALSE;
1652 data->cur_lunt->msgin03 = TRUE;
1655 nsp32_adjust_busfree(SCpnt, s_sacklen);
1659 /* This value has not substitude with valid value yet... */
1660 //data->cur_lunt->save_datp = data->cur_datp;
1667 if (execph & MSGIN_03_VALID) {
1668 /* MsgIn03 was valid to be processed. No need processing. */
1674 if (data->cur_target->sync_flag & SDTR_INITIATOR) {
1676 * SDTR negotiation pulled by the initiator has not
1677 * finished yet. Fall back to ASYNC mode.
1679 nsp32_set_async(data, data->cur_target);
1680 data->cur_target->sync_flag &= ~SDTR_INITIATOR;
1681 data->cur_target->sync_flag |= SDTR_DONE;
1682 } else if (data->cur_target->sync_flag & SDTR_TARGET) {
1684 * SDTR negotiation pulled by the target has been
1687 if (execph & (MSGIN_00_VALID | MSGIN_04_VALID)) {
1689 * If valid message is received, then
1690 * negotiation is succeeded.
1694 * On the contrary, if unexpected bus free is
1695 * occurred, then negotiation is failed. Fall
1696 * back to ASYNC mode.
1698 nsp32_set_async(data, data->cur_target);
1700 data->cur_target->sync_flag &= ~SDTR_TARGET;
1701 data->cur_target->sync_flag |= SDTR_DONE;
1705 * It is always ensured by SCSI standard that initiator
1706 * switches into Bus Free Phase after
1707 * receiving message 00 (Command Complete), 04 (Disconnect).
1708 * It's the reason that processing here is valid.
1710 if (execph & MSGIN_00_VALID) {
1711 /* MsgIn 00: Command Complete */
1712 nsp32_dbg(NSP32_DEBUG_BUSFREE, "command complete");
1714 SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
1715 SCpnt->SCp.Message = 0;
1716 nsp32_dbg(NSP32_DEBUG_BUSFREE,
1717 "normal end stat=0x%x resid=0x%x\n",
1718 SCpnt->SCp.Status, scsi_get_resid(SCpnt));
1719 SCpnt->result = (DID_OK << 16) |
1720 (SCpnt->SCp.Message << 8) |
1721 (SCpnt->SCp.Status << 0);
1722 nsp32_scsi_done(SCpnt);
1723 /* All operation is done */
1725 } else if (execph & MSGIN_04_VALID) {
1726 /* MsgIn 04: Disconnect */
1727 SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
1728 SCpnt->SCp.Message = 4;
1730 nsp32_dbg(NSP32_DEBUG_BUSFREE, "disconnect");
1733 /* Unexpected bus free */
1734 nsp32_msg(KERN_WARNING, "unexpected bus free occurred");
1737 //SCpnt->result = (DID_OK << 16) | (SCpnt->SCp.Message << 8) | (SCpnt->SCp.Status << 0);
1738 SCpnt->result = DID_ERROR << 16;
1739 nsp32_scsi_done(SCpnt);
1747 * nsp32_adjust_busfree - adjusting SG table
1749 * Note: This driver adjust the SG table using SCSI ACK
1750 * counter instead of BMCNT counter!
1752 static void nsp32_adjust_busfree(struct scsi_cmnd *SCpnt, unsigned int s_sacklen)
1754 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1755 int old_entry = data->cur_entry;
1757 int sg_num = data->cur_lunt->sg_num;
1758 nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
1759 unsigned int restlen, sentlen;
1762 nsp32_dbg(NSP32_DEBUG_SGLIST, "old resid=0x%x", scsi_get_resid(SCpnt));
1764 /* adjust saved SACK count with 4 byte start address boundary */
1765 s_sacklen -= le32_to_cpu(sgt[old_entry].addr) & 3;
1768 * calculate new_entry from sack count and each sgt[].len
1769 * calculate the byte which is intent to send
1772 for (new_entry = old_entry; new_entry < sg_num; new_entry++) {
1773 sentlen += (le32_to_cpu(sgt[new_entry].len) & ~SGTEND);
1774 if (sentlen > s_sacklen) {
1779 /* all sgt is processed */
1780 if (new_entry == sg_num) {
1784 if (sentlen == s_sacklen) {
1785 /* XXX: confirm it's ok or not */
1786 /* In this case, it's ok because we are at
1787 the head element of the sg. restlen is correctly calculated. */
1790 /* calculate the rest length for transfering */
1791 restlen = sentlen - s_sacklen;
1793 /* update adjusting current SG table entry */
1794 len = le32_to_cpu(sgt[new_entry].len);
1795 addr = le32_to_cpu(sgt[new_entry].addr);
1796 addr += (len - restlen);
1797 sgt[new_entry].addr = cpu_to_le32(addr);
1798 sgt[new_entry].len = cpu_to_le32(restlen);
1800 /* set cur_entry with new_entry */
1801 data->cur_entry = new_entry;
1806 if (scsi_get_resid(SCpnt) < sentlen) {
1807 nsp32_msg(KERN_ERR, "resid underflow");
1810 scsi_set_resid(SCpnt, scsi_get_resid(SCpnt) - sentlen);
1811 nsp32_dbg(NSP32_DEBUG_SGLIST, "new resid=0x%x", scsi_get_resid(SCpnt));
1813 /* update hostdata and lun */
1820 * It's called MsgOut phase occur.
1821 * NinjaSCSI-32Bi/UDE automatically processes up to 3 messages in
1822 * message out phase. It, however, has more than 3 messages,
1823 * HBA creates the interrupt and we have to process by hand.
1825 static void nsp32_msgout_occur(struct scsi_cmnd *SCpnt)
1827 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1828 unsigned int base = SCpnt->device->host->io_port;
1829 //unsigned short command;
1833 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
1834 "enter: msgout_len: 0x%x", data->msgout_len);
1837 * If MsgOut phase is occurred without having any
1838 * message, then No_Operation is sent (SCSI-2).
1840 if (data->msgout_len == 0) {
1841 nsp32_build_nop(SCpnt);
1845 * Set SGTP ADDR current entry for restarting AUTOSCSI,
1846 * because SGTP is incremented next point.
1847 * There is few statement in the specification...
1849 new_sgtp = data->cur_lunt->sglun_paddr +
1850 (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
1855 for (i = 0; i < data->msgout_len; i++) {
1856 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
1857 "%d : 0x%x", i, data->msgoutbuf[i]);
1860 * Check REQ is asserted.
1862 nsp32_wait_req(data, ASSERT);
1864 if (i == (data->msgout_len - 1)) {
1866 * If the last message, set the AutoSCSI restart
1867 * before send back the ack message. AutoSCSI
1868 * restart automatically negate ATN signal.
1870 //command = (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
1871 //nsp32_restart_autoscsi(SCpnt, command);
1872 nsp32_write2(base, COMMAND_CONTROL,
1873 (CLEAR_CDB_FIFO_POINTER |
1874 AUTO_COMMAND_PHASE |
1876 AUTO_MSGIN_00_OR_04 |
1880 * Write data with SACK, then wait sack is
1881 * automatically negated.
1883 nsp32_write1(base, SCSI_DATA_WITH_ACK, data->msgoutbuf[i]);
1884 nsp32_wait_sack(data, NEGATE);
1886 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "bus: 0x%x\n",
1887 nsp32_read1(base, SCSI_BUS_MONITOR));
1890 data->msgout_len = 0;
1892 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "exit");
1898 * Note: Restarting AutoSCSI needs set:
1899 * SYNC_REG, ACK_WIDTH, SGT_ADR, TRANSFER_CONTROL
1901 static void nsp32_restart_autoscsi(struct scsi_cmnd *SCpnt, unsigned short command)
1903 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1904 unsigned int base = data->BaseAddress;
1905 unsigned short transfer = 0;
1907 nsp32_dbg(NSP32_DEBUG_RESTART, "enter");
1909 if (data->cur_target == NULL || data->cur_lunt == NULL) {
1910 nsp32_msg(KERN_ERR, "Target or Lun is invalid");
1915 * Don't set BM_START_ADR before setting this register.
1917 nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
1922 nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
1925 * set SREQ hazard killer sampling rate
1927 nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
1930 * set SGT ADDR (physical address)
1932 nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
1935 * set TRANSFER CONTROL REG
1938 transfer |= (TRANSFER_GO | ALL_COUNTER_CLR);
1939 if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
1940 if (scsi_bufflen(SCpnt) > 0) {
1941 transfer |= BM_START;
1943 } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
1944 transfer |= CB_MMIO_MODE;
1945 } else if (data->trans_method & NSP32_TRANSFER_PIO) {
1946 transfer |= CB_IO_MODE;
1948 nsp32_write2(base, TRANSFER_CONTROL, transfer);
1953 * TODO: COMMANDCONTROL_AUTO_COMMAND_PHASE is needed ?
1955 command |= (CLEAR_CDB_FIFO_POINTER |
1956 AUTO_COMMAND_PHASE |
1958 nsp32_write2(base, COMMAND_CONTROL, command);
1960 nsp32_dbg(NSP32_DEBUG_RESTART, "exit");
1965 * cannot run automatically message in occur
1967 static void nsp32_msgin_occur(struct scsi_cmnd *SCpnt,
1968 unsigned long irq_status,
1969 unsigned short execph)
1971 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1972 unsigned int base = SCpnt->device->host->io_port;
1974 unsigned char msgtype;
1975 unsigned char newlun;
1976 unsigned short command = 0;
1977 int msgclear = TRUE;
1982 * read first message
1983 * Use SCSIDATA_W_ACK instead of SCSIDATAIN, because the procedure
1984 * of Message-In have to be processed before sending back SCSI ACK.
1986 msg = nsp32_read1(base, SCSI_DATA_IN);
1987 data->msginbuf[(unsigned char)data->msgin_len] = msg;
1988 msgtype = data->msginbuf[0];
1989 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR,
1990 "enter: msglen: 0x%x msgin: 0x%x msgtype: 0x%x",
1991 data->msgin_len, msg, msgtype);
1994 * TODO: We need checking whether bus phase is message in?
2000 nsp32_sack_assert(data);
2003 * processing IDENTIFY
2005 if (msgtype & 0x80) {
2006 if (!(irq_status & IRQSTATUS_RESELECT_OCCUER)) {
2007 /* Invalid (non reselect) phase */
2011 newlun = msgtype & 0x1f; /* TODO: SPI-3 compliant? */
2012 ret = nsp32_reselection(SCpnt, newlun);
2021 * processing messages except for IDENTIFY
2023 * TODO: Messages are all SCSI-2 terminology. SCSI-3 compliance is TODO.
2029 case COMMAND_COMPLETE:
2032 * These messages should not be occurred.
2033 * They should be processed on AutoSCSI sequencer.
2035 nsp32_msg(KERN_WARNING,
2036 "unexpected message of AutoSCSI MsgIn: 0x%x", msg);
2039 case RESTORE_POINTERS:
2041 * AutoMsgIn03 is disabled, and HBA gets this message.
2044 if ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE)) {
2045 unsigned int s_sacklen;
2047 s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
2048 if ((execph & MSGIN_02_VALID) && (s_sacklen > 0)) {
2049 nsp32_adjust_busfree(SCpnt, s_sacklen);
2051 /* No need to rewrite SGT */
2054 data->cur_lunt->msgin03 = FALSE;
2056 /* Update with the new value */
2058 /* reset SACK/SavedACK counter (or ALL clear?) */
2059 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
2062 * set new sg pointer
2064 new_sgtp = data->cur_lunt->sglun_paddr +
2065 (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
2066 nsp32_write4(base, SGT_ADR, new_sgtp);
2072 * These messages should not be occurred.
2073 * They should be processed on AutoSCSI sequencer.
2075 nsp32_msg (KERN_WARNING,
2076 "unexpected message of AutoSCSI MsgIn: SAVE_POINTERS");
2080 case MESSAGE_REJECT:
2081 /* If previous message_out is sending SDTR, and get
2082 message_reject from target, SDTR negotiation is failed */
2083 if (data->cur_target->sync_flag &
2084 (SDTR_INITIATOR | SDTR_TARGET)) {
2086 * Current target is negotiating SDTR, but it's
2087 * failed. Fall back to async transfer mode, and set
2090 nsp32_set_async(data, data->cur_target);
2091 data->cur_target->sync_flag &= ~SDTR_INITIATOR;
2092 data->cur_target->sync_flag |= SDTR_DONE;
2097 case LINKED_CMD_COMPLETE:
2098 case LINKED_FLG_CMD_COMPLETE:
2099 /* queue tag is not supported currently */
2100 nsp32_msg (KERN_WARNING,
2101 "unsupported message: 0x%x", msgtype);
2104 case INITIATE_RECOVERY:
2105 /* staring ECA (Extended Contingent Allegiance) state. */
2106 /* This message is declined in SPI2 or later. */
2113 case SIMPLE_QUEUE_TAG:
2116 * 0x23: Ignore_Wide_Residue is not declared in scsi.h.
2117 * No support is needed.
2119 if (data->msgin_len >= 1) {
2123 /* current position is 1-byte of 2 byte */
2131 case EXTENDED_MESSAGE:
2132 if (data->msgin_len < 1) {
2134 * Current position does not reach 2-byte
2135 * (2-byte is extended message length).
2141 if ((data->msginbuf[1] + 1) > data->msgin_len) {
2143 * Current extended message has msginbuf[1] + 2
2144 * (msgin_len starts counting from 0, so buf[1] + 1).
2145 * If current message position is not finished,
2146 * continue receiving message.
2153 * Reach here means regular length of each type of
2154 * extended messages.
2156 switch (data->msginbuf[2]) {
2157 case EXTENDED_MODIFY_DATA_POINTER:
2159 goto reject; /* not implemented yet */
2164 * Exchange this message between initiator and target.
2166 if (data->msgin_len != EXTENDED_SDTR_LEN + 1) {
2168 * received inappropriate message.
2174 nsp32_analyze_sdtr(SCpnt);
2178 case EXTENDED_EXTENDED_IDENTIFY:
2179 /* SCSI-I only, not supported. */
2180 goto reject; /* not implemented yet */
2185 goto reject; /* not implemented yet */
2199 if (msgclear == TRUE) {
2200 data->msgin_len = 0;
2203 * If restarting AutoSCSI, but there are some message to out
2204 * (msgout_len > 0), set AutoATN, and set SCSIMSGOUT as 0
2205 * (MV_VALID = 0). When commandcontrol is written with
2206 * AutoSCSI restart, at the same time MsgOutOccur should be
2207 * happened (however, such situation is really possible...?).
2209 if (data->msgout_len > 0) {
2210 nsp32_write4(base, SCSI_MSG_OUT, 0);
2211 command |= AUTO_ATN;
2216 * If it's failed, COMMANDCONTROL_AUTO_COMMAND_PHASE is needed.
2218 command |= (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
2221 * If current msgin03 is TRUE, then flag on.
2223 if (data->cur_lunt->msgin03 == TRUE) {
2224 command |= AUTO_MSGIN_03;
2226 data->cur_lunt->msgin03 = FALSE;
2234 nsp32_restart_autoscsi(SCpnt, command);
2237 * wait SCSI REQ negate for REQ-ACK handshake
2239 nsp32_wait_req(data, NEGATE);
2244 nsp32_sack_negate(data);
2246 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
2251 nsp32_msg(KERN_WARNING,
2252 "invalid or unsupported MessageIn, rejected. "
2253 "current msg: 0x%x (len: 0x%x), processing msg: 0x%x",
2254 msg, data->msgin_len, msgtype);
2255 nsp32_build_reject(SCpnt);
2256 data->msgin_len = 0;
2264 static void nsp32_analyze_sdtr(struct scsi_cmnd *SCpnt)
2266 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2267 nsp32_target *target = data->cur_target;
2268 nsp32_sync_table *synct;
2269 unsigned char get_period = data->msginbuf[3];
2270 unsigned char get_offset = data->msginbuf[4];
2274 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "enter");
2276 synct = data->synct;
2277 syncnum = data->syncnum;
2280 * If this inititor sent the SDTR message, then target responds SDTR,
2281 * initiator SYNCREG, ACKWIDTH from SDTR parameter.
2282 * Messages are not appropriate, then send back reject message.
2283 * If initiator did not send the SDTR, but target sends SDTR,
2284 * initiator calculator the appropriate parameter and send back SDTR.
2286 if (target->sync_flag & SDTR_INITIATOR) {
2288 * Initiator sent SDTR, the target responds and
2289 * send back negotiation SDTR.
2291 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target responds SDTR");
2293 target->sync_flag &= ~SDTR_INITIATOR;
2294 target->sync_flag |= SDTR_DONE;
2299 if (get_offset > SYNC_OFFSET) {
2301 * Negotiation is failed, the target send back
2302 * unexpected offset value.
2307 if (get_offset == ASYNC_OFFSET) {
2309 * Negotiation is succeeded, the target want
2310 * to fall back into asynchronous transfer mode.
2317 * Check whether sync period is too short. If too short,
2318 * fall back to async mode. If it's ok, then investigate
2319 * the received sync period. If sync period is acceptable
2320 * between sync table start_period and end_period, then
2321 * set this I_T nexus as sent offset and period.
2322 * If it's not acceptable, send back reject and fall back
2325 if (get_period < data->synct[0].period_num) {
2327 * Negotiation is failed, the target send back
2328 * unexpected period value.
2333 entry = nsp32_search_period_entry(data, target, get_period);
2337 * Target want to use long period which is not
2338 * acceptable NinjaSCSI-32Bi/UDE.
2344 * Set new sync table and offset in this I_T nexus.
2346 nsp32_set_sync_entry(data, target, entry, get_offset);
2348 /* Target send SDTR to initiator. */
2349 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target send SDTR");
2351 target->sync_flag |= SDTR_INITIATOR;
2354 if (get_offset > SYNC_OFFSET) {
2355 /* send back as SYNC_OFFSET */
2356 get_offset = SYNC_OFFSET;
2360 if (get_period < data->synct[0].period_num) {
2361 get_period = data->synct[0].period_num;
2364 entry = nsp32_search_period_entry(data, target, get_period);
2366 if (get_offset == ASYNC_OFFSET || entry < 0) {
2367 nsp32_set_async(data, target);
2368 nsp32_build_sdtr(SCpnt, 0, ASYNC_OFFSET);
2370 nsp32_set_sync_entry(data, target, entry, get_offset);
2371 nsp32_build_sdtr(SCpnt, get_period, get_offset);
2375 target->period = get_period;
2376 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
2381 * If the current message is unacceptable, send back to the target
2382 * with reject message.
2384 nsp32_build_reject(SCpnt);
2387 nsp32_set_async(data, target); /* set as ASYNC transfer mode */
2390 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit: set async");
2396 * Search config entry number matched in sync_table from given
2397 * target and speed period value. If failed to search, return negative value.
2399 static int nsp32_search_period_entry(nsp32_hw_data *data,
2400 nsp32_target *target,
2401 unsigned char period)
2405 if (target->limit_entry >= data->syncnum) {
2406 nsp32_msg(KERN_ERR, "limit_entry exceeds syncnum!");
2407 target->limit_entry = 0;
2410 for (i = target->limit_entry; i < data->syncnum; i++) {
2411 if (period >= data->synct[i].start_period &&
2412 period <= data->synct[i].end_period) {
2418 * Check given period value is over the sync_table value.
2419 * If so, return max value.
2421 if (i == data->syncnum) {
2430 * target <-> initiator use ASYNC transfer
2432 static void nsp32_set_async(nsp32_hw_data *data, nsp32_target *target)
2434 unsigned char period = data->synct[target->limit_entry].period_num;
2436 target->offset = ASYNC_OFFSET;
2438 target->syncreg = TO_SYNCREG(period, ASYNC_OFFSET);
2439 target->ackwidth = 0;
2440 target->sample_reg = 0;
2442 nsp32_dbg(NSP32_DEBUG_SYNC, "set async");
2447 * target <-> initiator use maximum SYNC transfer
2449 static void nsp32_set_max_sync(nsp32_hw_data *data,
2450 nsp32_target *target,
2451 unsigned char *period,
2452 unsigned char *offset)
2454 unsigned char period_num, ackwidth;
2456 period_num = data->synct[target->limit_entry].period_num;
2457 *period = data->synct[target->limit_entry].start_period;
2458 ackwidth = data->synct[target->limit_entry].ackwidth;
2459 *offset = SYNC_OFFSET;
2461 target->syncreg = TO_SYNCREG(period_num, *offset);
2462 target->ackwidth = ackwidth;
2463 target->offset = *offset;
2464 target->sample_reg = 0; /* disable SREQ sampling */
2469 * target <-> initiator use entry number speed
2471 static void nsp32_set_sync_entry(nsp32_hw_data *data,
2472 nsp32_target *target,
2474 unsigned char offset)
2476 unsigned char period, ackwidth, sample_rate;
2478 period = data->synct[entry].period_num;
2479 ackwidth = data->synct[entry].ackwidth;
2481 sample_rate = data->synct[entry].sample_rate;
2483 target->syncreg = TO_SYNCREG(period, offset);
2484 target->ackwidth = ackwidth;
2485 target->offset = offset;
2486 target->sample_reg = sample_rate | SAMPLING_ENABLE;
2488 nsp32_dbg(NSP32_DEBUG_SYNC, "set sync");
2493 * It waits until SCSI REQ becomes assertion or negation state.
2495 * Note: If nsp32_msgin_occur is called, we asserts SCSI ACK. Then
2496 * connected target responds SCSI REQ negation. We have to wait
2497 * SCSI REQ becomes negation in order to negate SCSI ACK signal for
2498 * REQ-ACK handshake.
2500 static void nsp32_wait_req(nsp32_hw_data *data, int state)
2502 unsigned int base = data->BaseAddress;
2504 unsigned char bus, req_bit;
2506 if (!((state == ASSERT) || (state == NEGATE))) {
2507 nsp32_msg(KERN_ERR, "unknown state designation");
2510 req_bit = (state == ASSERT ? BUSMON_REQ : 0);
2513 bus = nsp32_read1(base, SCSI_BUS_MONITOR);
2514 if ((bus & BUSMON_REQ) == req_bit) {
2515 nsp32_dbg(NSP32_DEBUG_WAIT,
2516 "wait_time: %d", wait_time);
2521 } while (wait_time < REQSACK_TIMEOUT_TIME);
2523 nsp32_msg(KERN_WARNING, "wait REQ timeout, req_bit: 0x%x", req_bit);
2527 * It waits until SCSI SACK becomes assertion or negation state.
2529 static void nsp32_wait_sack(nsp32_hw_data *data, int state)
2531 unsigned int base = data->BaseAddress;
2533 unsigned char bus, ack_bit;
2535 if (!((state == ASSERT) || (state == NEGATE))) {
2536 nsp32_msg(KERN_ERR, "unknown state designation");
2539 ack_bit = (state == ASSERT ? BUSMON_ACK : 0);
2542 bus = nsp32_read1(base, SCSI_BUS_MONITOR);
2543 if ((bus & BUSMON_ACK) == ack_bit) {
2544 nsp32_dbg(NSP32_DEBUG_WAIT,
2545 "wait_time: %d", wait_time);
2550 } while (wait_time < REQSACK_TIMEOUT_TIME);
2552 nsp32_msg(KERN_WARNING, "wait SACK timeout, ack_bit: 0x%x", ack_bit);
2558 * Note: SCSI ACK assertion needs with ACKENB=1, AUTODIRECTION=1.
2560 static void nsp32_sack_assert(nsp32_hw_data *data)
2562 unsigned int base = data->BaseAddress;
2563 unsigned char busctrl;
2565 busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
2566 busctrl |= (BUSCTL_ACK | AUTODIRECTION | ACKENB);
2567 nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
2573 static void nsp32_sack_negate(nsp32_hw_data *data)
2575 unsigned int base = data->BaseAddress;
2576 unsigned char busctrl;
2578 busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
2579 busctrl &= ~BUSCTL_ACK;
2580 nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
2586 * Note: n_io_port is defined as 0x7f because I/O register port is
2588 * 0x800-0x8ff: memory mapped I/O port
2589 * 0x900-0xbff: (map same 0x800-0x8ff I/O port image repeatedly)
2590 * 0xc00-0xfff: CardBus status registers
2592 static int nsp32_detect(struct pci_dev *pdev)
2594 struct Scsi_Host *host; /* registered host structure */
2595 struct resource *res;
2596 nsp32_hw_data *data;
2600 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
2603 * register this HBA as SCSI device
2605 host = scsi_host_alloc(&nsp32_template, sizeof(nsp32_hw_data));
2607 nsp32_msg (KERN_ERR, "failed to scsi register");
2614 data = (nsp32_hw_data *)host->hostdata;
2616 memcpy(data, &nsp32_data_base, sizeof(nsp32_hw_data));
2618 host->irq = data->IrqNumber;
2619 host->io_port = data->BaseAddress;
2620 host->unique_id = data->BaseAddress;
2621 host->n_io_port = data->NumAddress;
2622 host->base = (unsigned long)data->MmioAddress;
2625 spin_lock_init(&(data->Lock));
2627 data->cur_lunt = NULL;
2628 data->cur_target = NULL;
2631 * Bus master transfer mode is supported currently.
2633 data->trans_method = NSP32_TRANSFER_BUSMASTER;
2636 * Set clock div, CLOCK_4 (HBA has own external clock, and
2637 * dividing * 100ns/4).
2638 * Currently CLOCK_4 has only tested, not for CLOCK_2/PCICLK yet.
2640 data->clock = CLOCK_4;
2643 * Select appropriate nsp32_sync_table and set I_CLOCKDIV.
2645 switch (data->clock) {
2647 /* If data->clock is CLOCK_4, then select 40M sync table. */
2648 data->synct = nsp32_sync_table_40M;
2649 data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
2652 /* If data->clock is CLOCK_2, then select 20M sync table. */
2653 data->synct = nsp32_sync_table_20M;
2654 data->syncnum = ARRAY_SIZE(nsp32_sync_table_20M);
2657 /* If data->clock is PCICLK, then select pci sync table. */
2658 data->synct = nsp32_sync_table_pci;
2659 data->syncnum = ARRAY_SIZE(nsp32_sync_table_pci);
2662 nsp32_msg(KERN_WARNING,
2663 "Invalid clock div is selected, set CLOCK_4.");
2664 /* Use default value CLOCK_4 */
2665 data->clock = CLOCK_4;
2666 data->synct = nsp32_sync_table_40M;
2667 data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
2677 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0) {
2678 nsp32_msg (KERN_ERR, "failed to set PCI DMA mask");
2679 goto scsi_unregister;
2683 * allocate autoparam DMA resource.
2685 data->autoparam = pci_alloc_consistent(pdev, sizeof(nsp32_autoparam), &(data->auto_paddr));
2686 if (data->autoparam == NULL) {
2687 nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
2688 goto scsi_unregister;
2692 * allocate scatter-gather DMA resource.
2694 data->sg_list = pci_alloc_consistent(pdev, NSP32_SG_TABLE_SIZE,
2696 if (data->sg_list == NULL) {
2697 nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
2698 goto free_autoparam;
2701 for (i = 0; i < ARRAY_SIZE(data->lunt); i++) {
2702 for (j = 0; j < ARRAY_SIZE(data->lunt[0]); j++) {
2703 int offset = i * ARRAY_SIZE(data->lunt[0]) + j;
2710 .sglun = &(data->sg_list[offset]),
2711 .sglun_paddr = data->sg_paddr + (offset * sizeof(nsp32_sglun)),
2714 data->lunt[i][j] = tmp;
2721 for (i = 0; i < ARRAY_SIZE(data->target); i++) {
2722 nsp32_target *target = &(data->target[i]);
2724 target->limit_entry = 0;
2725 target->sync_flag = 0;
2726 nsp32_set_async(data, target);
2732 ret = nsp32_getprom_param(data);
2734 data->resettime = 3; /* default 3 */
2742 snprintf(data->info_str, sizeof(data->info_str),
2743 "NinjaSCSI-32Bi/UDE: irq %d, io 0x%lx+0x%x",
2744 host->irq, host->io_port, host->n_io_port);
2749 * Note: It's important to reset SCSI bus in initialization phase.
2750 * NinjaSCSI-32Bi/UDE HBA EEPROM seems to exchange SDTR when
2751 * system is coming up, so SCSI devices connected to HBA is set as
2752 * un-asynchronous mode. It brings the merit that this HBA is
2753 * ready to start synchronous transfer without any preparation,
2754 * but we are difficult to control transfer speed. In addition,
2755 * it prevents device transfer speed from effecting EEPROM start-up
2756 * SDTR. NinjaSCSI-32Bi/UDE has the feature if EEPROM is set as
2757 * Auto Mode, then FAST-10M is selected when SCSI devices are
2758 * connected same or more than 4 devices. It should be avoided
2759 * depending on this specification. Thus, resetting the SCSI bus
2760 * restores all connected SCSI devices to asynchronous mode, then
2761 * this driver set SDTR safely later, and we can control all SCSI
2762 * device transfer mode.
2764 nsp32_do_bus_reset(data);
2766 ret = request_irq(host->irq, do_nsp32_isr, IRQF_SHARED, "nsp32", data);
2768 nsp32_msg(KERN_ERR, "Unable to allocate IRQ for NinjaSCSI32 "
2769 "SCSI PCI controller. Interrupt: %d", host->irq);
2776 res = request_region(host->io_port, host->n_io_port, "nsp32");
2779 "I/O region 0x%lx+0x%lx is already used",
2780 data->BaseAddress, data->NumAddress);
2784 ret = scsi_add_host(host, &pdev->dev);
2786 nsp32_msg(KERN_ERR, "failed to add scsi host");
2789 scsi_scan_host(host);
2790 pci_set_drvdata(pdev, host);
2794 release_region(host->io_port, host->n_io_port);
2797 free_irq(host->irq, data);
2800 pci_free_consistent(pdev, NSP32_SG_TABLE_SIZE,
2801 data->sg_list, data->sg_paddr);
2804 pci_free_consistent(pdev, sizeof(nsp32_autoparam),
2805 data->autoparam, data->auto_paddr);
2808 scsi_host_put(host);
2814 static int nsp32_release(struct Scsi_Host *host)
2816 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
2818 if (data->autoparam) {
2819 pci_free_consistent(data->Pci, sizeof(nsp32_autoparam),
2820 data->autoparam, data->auto_paddr);
2823 if (data->sg_list) {
2824 pci_free_consistent(data->Pci, NSP32_SG_TABLE_SIZE,
2825 data->sg_list, data->sg_paddr);
2829 free_irq(host->irq, data);
2832 if (host->io_port && host->n_io_port) {
2833 release_region(host->io_port, host->n_io_port);
2836 if (data->MmioAddress) {
2837 iounmap(data->MmioAddress);
2843 static const char *nsp32_info(struct Scsi_Host *shpnt)
2845 nsp32_hw_data *data = (nsp32_hw_data *)shpnt->hostdata;
2847 return data->info_str;
2851 /****************************************************************************
2854 static int nsp32_eh_abort(struct scsi_cmnd *SCpnt)
2856 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2857 unsigned int base = SCpnt->device->host->io_port;
2859 nsp32_msg(KERN_WARNING, "abort");
2861 if (data->cur_lunt->SCpnt == NULL) {
2862 nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort failed");
2866 if (data->cur_target->sync_flag & (SDTR_INITIATOR | SDTR_TARGET)) {
2867 /* reset SDTR negotiation */
2868 data->cur_target->sync_flag = 0;
2869 nsp32_set_async(data, data->cur_target);
2872 nsp32_write2(base, TRANSFER_CONTROL, 0);
2873 nsp32_write2(base, BM_CNT, 0);
2875 SCpnt->result = DID_ABORT << 16;
2876 nsp32_scsi_done(SCpnt);
2878 nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort success");
2882 static int nsp32_eh_bus_reset(struct scsi_cmnd *SCpnt)
2884 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2885 unsigned int base = SCpnt->device->host->io_port;
2887 spin_lock_irq(SCpnt->device->host->host_lock);
2889 nsp32_msg(KERN_INFO, "Bus Reset");
2890 nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
2892 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
2893 nsp32_do_bus_reset(data);
2894 nsp32_write2(base, IRQ_CONTROL, 0);
2896 spin_unlock_irq(SCpnt->device->host->host_lock);
2897 return SUCCESS; /* SCSI bus reset is succeeded at any time. */
2900 static void nsp32_do_bus_reset(nsp32_hw_data *data)
2902 unsigned int base = data->BaseAddress;
2903 unsigned short intrdat;
2906 nsp32_dbg(NSP32_DEBUG_BUSRESET, "in");
2910 * clear TRANSFERCONTROL_BM_START
2913 nsp32_write2(base, TRANSFER_CONTROL, 0);
2914 nsp32_write4(base, BM_CNT, 0);
2915 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
2918 * fall back to asynchronous transfer mode
2919 * initialize SDTR negotiation flag
2921 for (i = 0; i < ARRAY_SIZE(data->target); i++) {
2922 nsp32_target *target = &data->target[i];
2924 target->sync_flag = 0;
2925 nsp32_set_async(data, target);
2931 nsp32_write1(base, SCSI_BUS_CONTROL, BUSCTL_RST);
2932 udelay(RESET_HOLD_TIME);
2933 nsp32_write1(base, SCSI_BUS_CONTROL, 0);
2934 for(i = 0; i < 5; i++) {
2935 intrdat = nsp32_read2(base, IRQ_STATUS); /* dummy read */
2936 nsp32_dbg(NSP32_DEBUG_BUSRESET, "irq:1: 0x%x", intrdat);
2939 data->CurrentSC = NULL;
2942 static int nsp32_eh_host_reset(struct scsi_cmnd *SCpnt)
2944 struct Scsi_Host *host = SCpnt->device->host;
2945 unsigned int base = SCpnt->device->host->io_port;
2946 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
2948 nsp32_msg(KERN_INFO, "Host Reset");
2949 nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
2951 spin_lock_irq(SCpnt->device->host->host_lock);
2954 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
2955 nsp32_do_bus_reset(data);
2956 nsp32_write2(base, IRQ_CONTROL, 0);
2958 spin_unlock_irq(SCpnt->device->host->host_lock);
2959 return SUCCESS; /* Host reset is succeeded at any time. */
2963 /**************************************************************************
2968 * getting EEPROM parameter
2970 static int nsp32_getprom_param(nsp32_hw_data *data)
2972 int vendor = data->pci_devid->vendor;
2973 int device = data->pci_devid->device;
2979 ret = nsp32_prom_read(data, 0x7e);
2981 nsp32_msg(KERN_INFO, "No EEPROM detected: 0x%x", ret);
2984 ret = nsp32_prom_read(data, 0x7f);
2986 nsp32_msg(KERN_INFO, "Invalid number: 0x%x", ret);
2993 if (vendor == PCI_VENDOR_ID_WORKBIT &&
2994 device == PCI_DEVICE_ID_WORKBIT_STANDARD) {
2995 ret = nsp32_getprom_c16(data);
2996 } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
2997 device == PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC) {
2998 ret = nsp32_getprom_at24(data);
2999 } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
3000 device == PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO ) {
3001 ret = nsp32_getprom_at24(data);
3003 nsp32_msg(KERN_WARNING, "Unknown EEPROM");
3007 /* for debug : SPROM data full checking */
3008 for (i = 0; i <= 0x1f; i++) {
3009 val = nsp32_prom_read(data, i);
3010 nsp32_dbg(NSP32_DEBUG_EEPROM,
3011 "rom address 0x%x : 0x%x", i, val);
3019 * AT24C01A (Logitec: LHA-600S), AT24C02 (Melco Buffalo: IFC-USLP) data map:
3022 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3023 * Value 0x0: ASYNC, 0x0c: Ultra-20M, 0x19: Fast-10M
3024 * 0x07 : HBA Synchronous Transfer Period
3025 * Value 0: AutoSync, 1: Manual Setting
3026 * 0x08 - 0x0f : Not Used? (0x0)
3027 * 0x10 : Bus Termination
3028 * Value 0: Auto[ON], 1: ON, 2: OFF
3029 * 0x11 : Not Used? (0)
3030 * 0x12 : Bus Reset Delay Time (0x03)
3031 * 0x13 : Bootable CD Support
3032 * Value 0: Disable, 1: Enable
3033 * 0x14 : Device Scan
3034 * Bit 7 6 5 4 3 2 1 0
3035 * | <----------------->
3036 * | SCSI ID: Value 0: Skip, 1: YES
3037 * |-> Value 0: ALL scan, Value 1: Manual
3038 * 0x15 - 0x1b : Not Used? (0)
3039 * 0x1c : Constant? (0x01) (clock div?)
3040 * 0x1d - 0x7c : Not Used (0xff)
3041 * 0x7d : Not Used? (0xff)
3042 * 0x7e : Constant (0x55), Validity signature
3043 * 0x7f : Constant (0xaa), Validity signature
3045 static int nsp32_getprom_at24(nsp32_hw_data *data)
3049 nsp32_target *target;
3053 * Reset time which is designated by EEPROM.
3055 * TODO: Not used yet.
3057 data->resettime = nsp32_prom_read(data, 0x12);
3060 * HBA Synchronous Transfer Period
3062 * Note: auto_sync = 0: auto, 1: manual. Ninja SCSI HBA spec says
3063 * that if auto_sync is 0 (auto), and connected SCSI devices are
3064 * same or lower than 3, then transfer speed is set as ULTRA-20M.
3065 * On the contrary if connected SCSI devices are same or higher
3066 * than 4, then transfer speed is set as FAST-10M.
3068 * I break this rule. The number of connected SCSI devices are
3069 * only ignored. If auto_sync is 0 (auto), then transfer speed is
3070 * forced as ULTRA-20M.
3072 ret = nsp32_prom_read(data, 0x07);
3081 nsp32_msg(KERN_WARNING,
3082 "Unsupported Auto Sync mode. Fall back to manual mode.");
3086 if (trans_mode == ULTRA20M_MODE) {
3091 * each device Synchronous Transfer Period
3093 for (i = 0; i < NSP32_HOST_SCSIID; i++) {
3094 target = &data->target[i];
3095 if (auto_sync == TRUE) {
3096 target->limit_entry = 0; /* set as ULTRA20M */
3098 ret = nsp32_prom_read(data, i);
3099 entry = nsp32_search_period_entry(data, target, ret);
3101 /* search failed... set maximum speed */
3104 target->limit_entry = entry;
3113 * C16 110 (I-O Data: SC-NBD) data map:
3116 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3117 * Value 0x0: 20MB/S, 0x1: 10MB/S, 0x2: 5MB/S, 0x3: ASYNC
3118 * 0x07 : 0 (HBA Synchronous Transfer Period: Auto Sync)
3119 * 0x08 - 0x0f : Not Used? (0x0)
3120 * 0x10 : Transfer Mode
3121 * Value 0: PIO, 1: Busmater
3122 * 0x11 : Bus Reset Delay Time (0x00-0x20)
3123 * 0x12 : Bus Termination
3124 * Value 0: Disable, 1: Enable
3125 * 0x13 - 0x19 : Disconnection
3126 * Value 0: Disable, 1: Enable
3127 * 0x1a - 0x7c : Not Used? (0)
3128 * 0x7d : Not Used? (0xf8)
3129 * 0x7e : Constant (0x55), Validity signature
3130 * 0x7f : Constant (0xaa), Validity signature
3132 static int nsp32_getprom_c16(nsp32_hw_data *data)
3135 nsp32_target *target;
3139 * Reset time which is designated by EEPROM.
3141 * TODO: Not used yet.
3143 data->resettime = nsp32_prom_read(data, 0x11);
3146 * each device Synchronous Transfer Period
3148 for (i = 0; i < NSP32_HOST_SCSIID; i++) {
3149 target = &data->target[i];
3150 ret = nsp32_prom_read(data, i);
3152 case 0: /* 20MB/s */
3155 case 1: /* 10MB/s */
3164 default: /* default 20MB/s */
3168 entry = nsp32_search_period_entry(data, target, val);
3169 if (entry < 0 || trans_mode == ULTRA20M_MODE) {
3170 /* search failed... set maximum speed */
3173 target->limit_entry = entry;
3181 * Atmel AT24C01A (drived in 5V) serial EEPROM routines
3183 static int nsp32_prom_read(nsp32_hw_data *data, int romaddr)
3187 /* start condition */
3188 nsp32_prom_start(data);
3190 /* device address */
3191 nsp32_prom_write_bit(data, 1); /* 1 */
3192 nsp32_prom_write_bit(data, 0); /* 0 */
3193 nsp32_prom_write_bit(data, 1); /* 1 */
3194 nsp32_prom_write_bit(data, 0); /* 0 */
3195 nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
3196 nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
3197 nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
3199 /* R/W: W for dummy write */
3200 nsp32_prom_write_bit(data, 0);
3203 nsp32_prom_write_bit(data, 0);
3206 for (i = 7; i >= 0; i--) {
3207 nsp32_prom_write_bit(data, ((romaddr >> i) & 1));
3211 nsp32_prom_write_bit(data, 0);
3213 /* start condition */
3214 nsp32_prom_start(data);
3216 /* device address */
3217 nsp32_prom_write_bit(data, 1); /* 1 */
3218 nsp32_prom_write_bit(data, 0); /* 0 */
3219 nsp32_prom_write_bit(data, 1); /* 1 */
3220 nsp32_prom_write_bit(data, 0); /* 0 */
3221 nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
3222 nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
3223 nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
3226 nsp32_prom_write_bit(data, 1);
3229 nsp32_prom_write_bit(data, 0);
3233 for (i = 7; i >= 0; i--) {
3234 val += (nsp32_prom_read_bit(data) << i);
3238 nsp32_prom_write_bit(data, 1);
3240 /* stop condition */
3241 nsp32_prom_stop(data);
3246 static void nsp32_prom_set(nsp32_hw_data *data, int bit, int val)
3248 int base = data->BaseAddress;
3251 tmp = nsp32_index_read1(base, SERIAL_ROM_CTL);
3259 nsp32_index_write1(base, SERIAL_ROM_CTL, tmp);
3264 static int nsp32_prom_get(nsp32_hw_data *data, int bit)
3266 int base = data->BaseAddress;
3270 nsp32_msg(KERN_ERR, "return value is not appropriate");
3275 tmp = nsp32_index_read1(base, SERIAL_ROM_CTL) & bit;
3288 static void nsp32_prom_start (nsp32_hw_data *data)
3290 /* start condition */
3291 nsp32_prom_set(data, SCL, 1);
3292 nsp32_prom_set(data, SDA, 1);
3293 nsp32_prom_set(data, ENA, 1); /* output mode */
3294 nsp32_prom_set(data, SDA, 0); /* keeping SCL=1 and transiting
3295 * SDA 1->0 is start condition */
3296 nsp32_prom_set(data, SCL, 0);
3299 static void nsp32_prom_stop (nsp32_hw_data *data)
3301 /* stop condition */
3302 nsp32_prom_set(data, SCL, 1);
3303 nsp32_prom_set(data, SDA, 0);
3304 nsp32_prom_set(data, ENA, 1); /* output mode */
3305 nsp32_prom_set(data, SDA, 1);
3306 nsp32_prom_set(data, SCL, 0);
3309 static void nsp32_prom_write_bit(nsp32_hw_data *data, int val)
3312 nsp32_prom_set(data, SDA, val);
3313 nsp32_prom_set(data, SCL, 1 );
3314 nsp32_prom_set(data, SCL, 0 );
3317 static int nsp32_prom_read_bit(nsp32_hw_data *data)
3322 nsp32_prom_set(data, ENA, 0); /* input mode */
3323 nsp32_prom_set(data, SCL, 1);
3325 val = nsp32_prom_get(data, SDA);
3327 nsp32_prom_set(data, SCL, 0);
3328 nsp32_prom_set(data, ENA, 1); /* output mode */
3334 /**************************************************************************
3339 /* Device suspended */
3340 static int nsp32_suspend(struct pci_dev *pdev, pm_message_t state)
3342 struct Scsi_Host *host = pci_get_drvdata(pdev);
3344 nsp32_msg(KERN_INFO, "pci-suspend: pdev=0x%p, state=%ld, slot=%s, host=0x%p", pdev, state, pci_name(pdev), host);
3346 pci_save_state (pdev);
3347 pci_disable_device (pdev);
3348 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3353 /* Device woken up */
3354 static int nsp32_resume(struct pci_dev *pdev)
3356 struct Scsi_Host *host = pci_get_drvdata(pdev);
3357 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
3360 nsp32_msg(KERN_INFO, "pci-resume: pdev=0x%p, slot=%s, host=0x%p", pdev, pci_name(pdev), host);
3362 pci_set_power_state(pdev, PCI_D0);
3363 pci_enable_wake (pdev, PCI_D0, 0);
3364 pci_restore_state (pdev);
3366 reg = nsp32_read2(data->BaseAddress, INDEX_REG);
3368 nsp32_msg(KERN_INFO, "io=0x%x reg=0x%x", data->BaseAddress, reg);
3370 if (reg == 0xffff) {
3371 nsp32_msg(KERN_INFO, "missing device. abort resume.");
3375 nsp32hw_init (data);
3376 nsp32_do_bus_reset(data);
3378 nsp32_msg(KERN_INFO, "resume success");
3385 /************************************************************************
3386 * PCI/Cardbus probe/remove routine
3388 static int __devinit nsp32_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3391 nsp32_hw_data *data = &nsp32_data_base;
3393 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
3395 ret = pci_enable_device(pdev);
3397 nsp32_msg(KERN_ERR, "failed to enable pci device");
3402 data->pci_devid = id;
3403 data->IrqNumber = pdev->irq;
3404 data->BaseAddress = pci_resource_start(pdev, 0);
3405 data->NumAddress = pci_resource_len (pdev, 0);
3406 data->MmioAddress = ioremap_nocache(pci_resource_start(pdev, 1),
3407 pci_resource_len (pdev, 1));
3408 data->MmioLength = pci_resource_len (pdev, 1);
3410 pci_set_master(pdev);
3412 ret = nsp32_detect(pdev);
3414 nsp32_msg(KERN_INFO, "irq: %i mmio: %p+0x%lx slot: %s model: %s",
3416 data->MmioAddress, data->MmioLength,
3418 nsp32_model[id->driver_data]);
3420 nsp32_dbg(NSP32_DEBUG_REGISTER, "exit %d", ret);
3425 static void __devexit nsp32_remove(struct pci_dev *pdev)
3427 struct Scsi_Host *host = pci_get_drvdata(pdev);
3429 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
3431 scsi_remove_host(host);
3433 nsp32_release(host);
3435 scsi_host_put(host);
3438 static struct pci_driver nsp32_driver = {
3440 .id_table = nsp32_pci_table,
3441 .probe = nsp32_probe,
3442 .remove = __devexit_p(nsp32_remove),
3444 .suspend = nsp32_suspend,
3445 .resume = nsp32_resume,
3449 /*********************************************************************
3452 static int __init init_nsp32(void) {
3453 nsp32_msg(KERN_INFO, "loading...");
3454 return pci_register_driver(&nsp32_driver);
3457 static void __exit exit_nsp32(void) {
3458 nsp32_msg(KERN_INFO, "unloading...");
3459 pci_unregister_driver(&nsp32_driver);
3462 module_init(init_nsp32);
3463 module_exit(exit_nsp32);