[MIPS] RTLX: Protect rtlx_{read,write} with mutex.
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / scsi / nsp32.c
blobf6f561d26bf0c9c317d133e9ee9110444856eb6a
1 /*
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)
10 * any later version.
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.
18 * Revision History:
19 * 1.0: Initial Release.
20 * 1.1: Add /proc SDTR status.
21 * Remove obsolete error handler nsp32_reset.
22 * Some clean up.
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>
42 #include <asm/dma.h>
43 #include <asm/system.h>
44 #include <asm/io.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>
52 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
53 # include <linux/blk.h>
54 #endif
56 #include "nsp32.h"
59 /***********************************************************************
60 * Module parameters
62 static int trans_mode = 0; /* default: BIOS */
63 module_param (trans_mode, int, 0);
64 MODULE_PARM_DESC(trans_mode, "transfer mode (0: BIOS(default) 1: Async 2: Ultra20M");
65 #define ASYNC_MODE 1
66 #define ULTRA20M_MODE 2
68 static int auto_param = 0; /* default: ON */
69 module_param (auto_param, bool, 0);
70 MODULE_PARM_DESC(auto_param, "AutoParameter mode (0: ON(default) 1: OFF)");
72 static int disc_priv = 1; /* default: OFF */
73 module_param (disc_priv, bool, 0);
74 MODULE_PARM_DESC(disc_priv, "disconnection privilege mode (0: ON 1: OFF(default))");
76 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>, GOTO Masanori <gotom@debian.or.jp>");
77 MODULE_DESCRIPTION("Workbit NinjaSCSI-32Bi/UDE CardBus/PCI SCSI host bus adapter module");
78 MODULE_LICENSE("GPL");
80 static const char *nsp32_release_version = "1.2";
83 /****************************************************************************
84 * Supported hardware
86 static struct pci_device_id nsp32_pci_table[] __devinitdata = {
88 .vendor = PCI_VENDOR_ID_IODATA,
89 .device = PCI_DEVICE_ID_NINJASCSI_32BI_CBSC_II,
90 .subvendor = PCI_ANY_ID,
91 .subdevice = PCI_ANY_ID,
92 .driver_data = MODEL_IODATA,
95 .vendor = PCI_VENDOR_ID_WORKBIT,
96 .device = PCI_DEVICE_ID_NINJASCSI_32BI_KME,
97 .subvendor = PCI_ANY_ID,
98 .subdevice = PCI_ANY_ID,
99 .driver_data = MODEL_KME,
102 .vendor = PCI_VENDOR_ID_WORKBIT,
103 .device = PCI_DEVICE_ID_NINJASCSI_32BI_WBT,
104 .subvendor = PCI_ANY_ID,
105 .subdevice = PCI_ANY_ID,
106 .driver_data = MODEL_WORKBIT,
109 .vendor = PCI_VENDOR_ID_WORKBIT,
110 .device = PCI_DEVICE_ID_WORKBIT_STANDARD,
111 .subvendor = PCI_ANY_ID,
112 .subdevice = PCI_ANY_ID,
113 .driver_data = MODEL_PCI_WORKBIT,
116 .vendor = PCI_VENDOR_ID_WORKBIT,
117 .device = PCI_DEVICE_ID_NINJASCSI_32BI_LOGITEC,
118 .subvendor = PCI_ANY_ID,
119 .subdevice = PCI_ANY_ID,
120 .driver_data = MODEL_LOGITEC,
123 .vendor = PCI_VENDOR_ID_WORKBIT,
124 .device = PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC,
125 .subvendor = PCI_ANY_ID,
126 .subdevice = PCI_ANY_ID,
127 .driver_data = MODEL_PCI_LOGITEC,
130 .vendor = PCI_VENDOR_ID_WORKBIT,
131 .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO,
132 .subvendor = PCI_ANY_ID,
133 .subdevice = PCI_ANY_ID,
134 .driver_data = MODEL_PCI_MELCO,
137 .vendor = PCI_VENDOR_ID_WORKBIT,
138 .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO_II,
139 .subvendor = PCI_ANY_ID,
140 .subdevice = PCI_ANY_ID,
141 .driver_data = MODEL_PCI_MELCO,
143 {0,0,},
145 MODULE_DEVICE_TABLE(pci, nsp32_pci_table);
147 static nsp32_hw_data nsp32_data_base; /* probe <-> detect glue */
151 * Period/AckWidth speed conversion table
153 * Note: This period/ackwidth speed table must be in descending order.
155 static nsp32_sync_table nsp32_sync_table_40M[] = {
156 /* {PNo, AW, SP, EP, SREQ smpl} Speed(MB/s) Period AckWidth */
157 {0x1, 0, 0x0c, 0x0c, SMPL_40M}, /* 20.0 : 50ns, 25ns */
158 {0x2, 0, 0x0d, 0x18, SMPL_40M}, /* 13.3 : 75ns, 25ns */
159 {0x3, 1, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
160 {0x4, 1, 0x1a, 0x1f, SMPL_20M}, /* 8.0 : 125ns, 50ns */
161 {0x5, 2, 0x20, 0x25, SMPL_20M}, /* 6.7 : 150ns, 75ns */
162 {0x6, 2, 0x26, 0x31, SMPL_20M}, /* 5.7 : 175ns, 75ns */
163 {0x7, 3, 0x32, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
164 {0x8, 3, 0x33, 0x38, SMPL_10M}, /* 4.4 : 225ns, 100ns */
165 {0x9, 3, 0x39, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
168 static nsp32_sync_table nsp32_sync_table_20M[] = {
169 {0x1, 0, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
170 {0x2, 0, 0x1a, 0x25, SMPL_20M}, /* 6.7 : 150ns, 50ns */
171 {0x3, 1, 0x26, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
172 {0x4, 1, 0x33, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
173 {0x5, 2, 0x3f, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 150ns */
174 {0x6, 2, 0x4c, 0x57, SMPL_10M}, /* 2.8 : 350ns, 150ns */
175 {0x7, 3, 0x58, 0x64, SMPL_10M}, /* 2.5 : 400ns, 200ns */
176 {0x8, 3, 0x65, 0x70, SMPL_10M}, /* 2.2 : 450ns, 200ns */
177 {0x9, 3, 0x71, 0x7d, SMPL_10M}, /* 2.0 : 500ns, 200ns */
180 static nsp32_sync_table nsp32_sync_table_pci[] = {
181 {0x1, 0, 0x0c, 0x0f, SMPL_40M}, /* 16.6 : 60ns, 30ns */
182 {0x2, 0, 0x10, 0x16, SMPL_40M}, /* 11.1 : 90ns, 30ns */
183 {0x3, 1, 0x17, 0x1e, SMPL_20M}, /* 8.3 : 120ns, 60ns */
184 {0x4, 1, 0x1f, 0x25, SMPL_20M}, /* 6.7 : 150ns, 60ns */
185 {0x5, 2, 0x26, 0x2d, SMPL_20M}, /* 5.6 : 180ns, 90ns */
186 {0x6, 2, 0x2e, 0x34, SMPL_10M}, /* 4.8 : 210ns, 90ns */
187 {0x7, 3, 0x35, 0x3c, SMPL_10M}, /* 4.2 : 240ns, 120ns */
188 {0x8, 3, 0x3d, 0x43, SMPL_10M}, /* 3.7 : 270ns, 120ns */
189 {0x9, 3, 0x44, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 120ns */
193 * function declaration
195 /* module entry point */
196 static int __devinit nsp32_probe (struct pci_dev *, const struct pci_device_id *);
197 static void __devexit nsp32_remove(struct pci_dev *);
198 static int __init init_nsp32 (void);
199 static void __exit exit_nsp32 (void);
201 /* struct struct scsi_host_template */
202 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
203 static int nsp32_proc_info (struct Scsi_Host *, char *, char **, off_t, int, int);
204 #else
205 static int nsp32_proc_info (char *, char **, off_t, int, int, int);
206 #endif
208 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
209 static int nsp32_detect (struct pci_dev *pdev);
210 #else
211 static int nsp32_detect (struct scsi_host_template *);
212 #endif
213 static int nsp32_queuecommand(struct scsi_cmnd *,
214 void (*done)(struct scsi_cmnd *));
215 static const char *nsp32_info (struct Scsi_Host *);
216 static int nsp32_release (struct Scsi_Host *);
218 /* SCSI error handler */
219 static int nsp32_eh_abort (struct scsi_cmnd *);
220 static int nsp32_eh_bus_reset (struct scsi_cmnd *);
221 static int nsp32_eh_host_reset(struct scsi_cmnd *);
223 /* generate SCSI message */
224 static void nsp32_build_identify(struct scsi_cmnd *);
225 static void nsp32_build_nop (struct scsi_cmnd *);
226 static void nsp32_build_reject (struct scsi_cmnd *);
227 static void nsp32_build_sdtr (struct scsi_cmnd *, unsigned char, unsigned char);
229 /* SCSI message handler */
230 static int nsp32_busfree_occur(struct scsi_cmnd *, unsigned short);
231 static void nsp32_msgout_occur (struct scsi_cmnd *);
232 static void nsp32_msgin_occur (struct scsi_cmnd *, unsigned long, unsigned short);
234 static int nsp32_setup_sg_table (struct scsi_cmnd *);
235 static int nsp32_selection_autopara(struct scsi_cmnd *);
236 static int nsp32_selection_autoscsi(struct scsi_cmnd *);
237 static void nsp32_scsi_done (struct scsi_cmnd *);
238 static int nsp32_arbitration (struct scsi_cmnd *, unsigned int);
239 static int nsp32_reselection (struct scsi_cmnd *, unsigned char);
240 static void nsp32_adjust_busfree (struct scsi_cmnd *, unsigned int);
241 static void nsp32_restart_autoscsi (struct scsi_cmnd *, unsigned short);
243 /* SCSI SDTR */
244 static void nsp32_analyze_sdtr (struct scsi_cmnd *);
245 static int nsp32_search_period_entry(nsp32_hw_data *, nsp32_target *, unsigned char);
246 static void nsp32_set_async (nsp32_hw_data *, nsp32_target *);
247 static void nsp32_set_max_sync (nsp32_hw_data *, nsp32_target *, unsigned char *, unsigned char *);
248 static void nsp32_set_sync_entry (nsp32_hw_data *, nsp32_target *, int, unsigned char);
250 /* SCSI bus status handler */
251 static void nsp32_wait_req (nsp32_hw_data *, int);
252 static void nsp32_wait_sack (nsp32_hw_data *, int);
253 static void nsp32_sack_assert (nsp32_hw_data *);
254 static void nsp32_sack_negate (nsp32_hw_data *);
255 static void nsp32_do_bus_reset(nsp32_hw_data *);
257 /* hardware interrupt handler */
258 static irqreturn_t do_nsp32_isr(int, void *);
260 /* initialize hardware */
261 static int nsp32hw_init(nsp32_hw_data *);
263 /* EEPROM handler */
264 static int nsp32_getprom_param (nsp32_hw_data *);
265 static int nsp32_getprom_at24 (nsp32_hw_data *);
266 static int nsp32_getprom_c16 (nsp32_hw_data *);
267 static void nsp32_prom_start (nsp32_hw_data *);
268 static void nsp32_prom_stop (nsp32_hw_data *);
269 static int nsp32_prom_read (nsp32_hw_data *, int);
270 static int nsp32_prom_read_bit (nsp32_hw_data *);
271 static void nsp32_prom_write_bit(nsp32_hw_data *, int);
272 static void nsp32_prom_set (nsp32_hw_data *, int, int);
273 static int nsp32_prom_get (nsp32_hw_data *, int);
275 /* debug/warning/info message */
276 static void nsp32_message (const char *, int, char *, char *, ...);
277 #ifdef NSP32_DEBUG
278 static void nsp32_dmessage(const char *, int, int, char *, ...);
279 #endif
282 * max_sectors is currently limited up to 128.
284 static struct scsi_host_template nsp32_template = {
285 .proc_name = "nsp32",
286 .name = "Workbit NinjaSCSI-32Bi/UDE",
287 .proc_info = nsp32_proc_info,
288 .info = nsp32_info,
289 .queuecommand = nsp32_queuecommand,
290 .can_queue = 1,
291 .sg_tablesize = NSP32_SG_SIZE,
292 .max_sectors = 128,
293 .cmd_per_lun = 1,
294 .this_id = NSP32_HOST_SCSIID,
295 .use_clustering = DISABLE_CLUSTERING,
296 .eh_abort_handler = nsp32_eh_abort,
297 .eh_bus_reset_handler = nsp32_eh_bus_reset,
298 .eh_host_reset_handler = nsp32_eh_host_reset,
299 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,74))
300 .detect = nsp32_detect,
301 .release = nsp32_release,
302 #endif
303 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,2))
304 .use_new_eh_code = 1,
305 #else
306 /* .highmem_io = 1, */
307 #endif
310 #include "nsp32_io.h"
312 /***********************************************************************
313 * debug, error print
315 #ifndef NSP32_DEBUG
316 # define NSP32_DEBUG_MASK 0x000000
317 # define nsp32_msg(type, args...) nsp32_message ("", 0, (type), args)
318 # define nsp32_dbg(mask, args...) /* */
319 #else
320 # define NSP32_DEBUG_MASK 0xffffff
321 # define nsp32_msg(type, args...) \
322 nsp32_message (__FUNCTION__, __LINE__, (type), args)
323 # define nsp32_dbg(mask, args...) \
324 nsp32_dmessage(__FUNCTION__, __LINE__, (mask), args)
325 #endif
327 #define NSP32_DEBUG_QUEUECOMMAND BIT(0)
328 #define NSP32_DEBUG_REGISTER BIT(1)
329 #define NSP32_DEBUG_AUTOSCSI BIT(2)
330 #define NSP32_DEBUG_INTR BIT(3)
331 #define NSP32_DEBUG_SGLIST BIT(4)
332 #define NSP32_DEBUG_BUSFREE BIT(5)
333 #define NSP32_DEBUG_CDB_CONTENTS BIT(6)
334 #define NSP32_DEBUG_RESELECTION BIT(7)
335 #define NSP32_DEBUG_MSGINOCCUR BIT(8)
336 #define NSP32_DEBUG_EEPROM BIT(9)
337 #define NSP32_DEBUG_MSGOUTOCCUR BIT(10)
338 #define NSP32_DEBUG_BUSRESET BIT(11)
339 #define NSP32_DEBUG_RESTART BIT(12)
340 #define NSP32_DEBUG_SYNC BIT(13)
341 #define NSP32_DEBUG_WAIT BIT(14)
342 #define NSP32_DEBUG_TARGETFLAG BIT(15)
343 #define NSP32_DEBUG_PROC BIT(16)
344 #define NSP32_DEBUG_INIT BIT(17)
345 #define NSP32_SPECIAL_PRINT_REGISTER BIT(20)
347 #define NSP32_DEBUG_BUF_LEN 100
349 static void nsp32_message(const char *func, int line, char *type, char *fmt, ...)
351 va_list args;
352 char buf[NSP32_DEBUG_BUF_LEN];
354 va_start(args, fmt);
355 vsnprintf(buf, sizeof(buf), fmt, args);
356 va_end(args);
358 #ifndef NSP32_DEBUG
359 printk("%snsp32: %s\n", type, buf);
360 #else
361 printk("%snsp32: %s (%d): %s\n", type, func, line, buf);
362 #endif
365 #ifdef NSP32_DEBUG
366 static void nsp32_dmessage(const char *func, int line, int mask, char *fmt, ...)
368 va_list args;
369 char buf[NSP32_DEBUG_BUF_LEN];
371 va_start(args, fmt);
372 vsnprintf(buf, sizeof(buf), fmt, args);
373 va_end(args);
375 if (mask & NSP32_DEBUG_MASK) {
376 printk("nsp32-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
379 #endif
381 #ifdef NSP32_DEBUG
382 # include "nsp32_debug.c"
383 #else
384 # define show_command(arg) /* */
385 # define show_busphase(arg) /* */
386 # define show_autophase(arg) /* */
387 #endif
390 * IDENTIFY Message
392 static void nsp32_build_identify(struct scsi_cmnd *SCpnt)
394 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
395 int pos = data->msgout_len;
396 int mode = FALSE;
398 /* XXX: Auto DiscPriv detection is progressing... */
399 if (disc_priv == 0) {
400 /* mode = TRUE; */
403 data->msgoutbuf[pos] = IDENTIFY(mode, SCpnt->device->lun); pos++;
405 data->msgout_len = pos;
409 * SDTR Message Routine
411 static void nsp32_build_sdtr(struct scsi_cmnd *SCpnt,
412 unsigned char period,
413 unsigned char offset)
415 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
416 int pos = data->msgout_len;
418 data->msgoutbuf[pos] = EXTENDED_MESSAGE; pos++;
419 data->msgoutbuf[pos] = EXTENDED_SDTR_LEN; pos++;
420 data->msgoutbuf[pos] = EXTENDED_SDTR; pos++;
421 data->msgoutbuf[pos] = period; pos++;
422 data->msgoutbuf[pos] = offset; pos++;
424 data->msgout_len = pos;
428 * No Operation Message
430 static void nsp32_build_nop(struct scsi_cmnd *SCpnt)
432 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
433 int pos = data->msgout_len;
435 if (pos != 0) {
436 nsp32_msg(KERN_WARNING,
437 "Some messages are already contained!");
438 return;
441 data->msgoutbuf[pos] = NOP; pos++;
442 data->msgout_len = pos;
446 * Reject Message
448 static void nsp32_build_reject(struct scsi_cmnd *SCpnt)
450 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
451 int pos = data->msgout_len;
453 data->msgoutbuf[pos] = MESSAGE_REJECT; pos++;
454 data->msgout_len = pos;
458 * timer
460 #if 0
461 static void nsp32_start_timer(struct scsi_cmnd *SCpnt, int time)
463 unsigned int base = SCpnt->host->io_port;
465 nsp32_dbg(NSP32_DEBUG_INTR, "timer=%d", time);
467 if (time & (~TIMER_CNT_MASK)) {
468 nsp32_dbg(NSP32_DEBUG_INTR, "timer set overflow");
471 nsp32_write2(base, TIMER_SET, time & TIMER_CNT_MASK);
473 #endif
477 * set SCSI command and other parameter to asic, and start selection phase
479 static int nsp32_selection_autopara(struct scsi_cmnd *SCpnt)
481 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
482 unsigned int base = SCpnt->device->host->io_port;
483 unsigned int host_id = SCpnt->device->host->this_id;
484 unsigned char target = scmd_id(SCpnt);
485 nsp32_autoparam *param = data->autoparam;
486 unsigned char phase;
487 int i, ret;
488 unsigned int msgout;
489 u16_le s;
491 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
494 * check bus free
496 phase = nsp32_read1(base, SCSI_BUS_MONITOR);
497 if (phase != BUSMON_BUS_FREE) {
498 nsp32_msg(KERN_WARNING, "bus busy");
499 show_busphase(phase & BUSMON_PHASE_MASK);
500 SCpnt->result = DID_BUS_BUSY << 16;
501 return FALSE;
505 * message out
507 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
508 * over 3 messages needs another routine.
510 if (data->msgout_len == 0) {
511 nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
512 SCpnt->result = DID_ERROR << 16;
513 return FALSE;
514 } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
515 msgout = 0;
516 for (i = 0; i < data->msgout_len; i++) {
518 * the sending order of the message is:
519 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
520 * MCNT 2: MSG#1 -> MSG#2
521 * MCNT 1: MSG#2
523 msgout >>= 8;
524 msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
526 msgout |= MV_VALID; /* MV valid */
527 msgout |= (unsigned int)data->msgout_len; /* len */
528 } else {
529 /* data->msgout_len > 3 */
530 msgout = 0;
533 // nsp_dbg(NSP32_DEBUG_AUTOSCSI, "sel time out=0x%x\n", nsp32_read2(base, SEL_TIME_OUT));
534 // nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
537 * setup asic parameter
539 memset(param, 0, sizeof(nsp32_autoparam));
541 /* cdb */
542 for (i = 0; i < SCpnt->cmd_len; i++) {
543 param->cdb[4 * i] = SCpnt->cmnd[i];
546 /* outgoing messages */
547 param->msgout = cpu_to_le32(msgout);
549 /* syncreg, ackwidth, target id, SREQ sampling rate */
550 param->syncreg = data->cur_target->syncreg;
551 param->ackwidth = data->cur_target->ackwidth;
552 param->target_id = BIT(host_id) | BIT(target);
553 param->sample_reg = data->cur_target->sample_reg;
555 // nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "sample rate=0x%x\n", data->cur_target->sample_reg);
557 /* command control */
558 param->command_control = cpu_to_le16(CLEAR_CDB_FIFO_POINTER |
559 AUTOSCSI_START |
560 AUTO_MSGIN_00_OR_04 |
561 AUTO_MSGIN_02 |
562 AUTO_ATN );
565 /* transfer control */
566 s = 0;
567 switch (data->trans_method) {
568 case NSP32_TRANSFER_BUSMASTER:
569 s |= BM_START;
570 break;
571 case NSP32_TRANSFER_MMIO:
572 s |= CB_MMIO_MODE;
573 break;
574 case NSP32_TRANSFER_PIO:
575 s |= CB_IO_MODE;
576 break;
577 default:
578 nsp32_msg(KERN_ERR, "unknown trans_method");
579 break;
582 * OR-ed BLIEND_MODE, FIFO intr is decreased, instead of PCI bus waits.
583 * For bus master transfer, it's taken off.
585 s |= (TRANSFER_GO | ALL_COUNTER_CLR);
586 param->transfer_control = cpu_to_le16(s);
588 /* sg table addr */
589 param->sgt_pointer = cpu_to_le32(data->cur_lunt->sglun_paddr);
592 * transfer parameter to ASIC
594 nsp32_write4(base, SGT_ADR, data->auto_paddr);
595 nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER |
596 AUTO_PARAMETER );
599 * Check arbitration
601 ret = nsp32_arbitration(SCpnt, base);
603 return ret;
608 * Selection with AUTO SCSI (without AUTO PARAMETER)
610 static int nsp32_selection_autoscsi(struct scsi_cmnd *SCpnt)
612 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
613 unsigned int base = SCpnt->device->host->io_port;
614 unsigned int host_id = SCpnt->device->host->this_id;
615 unsigned char target = scmd_id(SCpnt);
616 unsigned char phase;
617 int status;
618 unsigned short command = 0;
619 unsigned int msgout = 0;
620 unsigned short execph;
621 int i;
623 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
626 * IRQ disable
628 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
631 * check bus line
633 phase = nsp32_read1(base, SCSI_BUS_MONITOR);
634 if(((phase & BUSMON_BSY) == 1) || (phase & BUSMON_SEL) == 1) {
635 nsp32_msg(KERN_WARNING, "bus busy");
636 SCpnt->result = DID_BUS_BUSY << 16;
637 status = 1;
638 goto out;
642 * clear execph
644 execph = nsp32_read2(base, SCSI_EXECUTE_PHASE);
647 * clear FIFO counter to set CDBs
649 nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER);
652 * set CDB0 - CDB15
654 for (i = 0; i < SCpnt->cmd_len; i++) {
655 nsp32_write1(base, COMMAND_DATA, SCpnt->cmnd[i]);
657 nsp32_dbg(NSP32_DEBUG_CDB_CONTENTS, "CDB[0]=[0x%x]", SCpnt->cmnd[0]);
660 * set SCSIOUT LATCH(initiator)/TARGET(target) (OR-ed) ID
662 nsp32_write1(base, SCSI_OUT_LATCH_TARGET_ID, BIT(host_id) | BIT(target));
665 * set SCSI MSGOUT REG
667 * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
668 * over 3 messages needs another routine.
670 if (data->msgout_len == 0) {
671 nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
672 SCpnt->result = DID_ERROR << 16;
673 status = 1;
674 goto out;
675 } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
676 msgout = 0;
677 for (i = 0; i < data->msgout_len; i++) {
679 * the sending order of the message is:
680 * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
681 * MCNT 2: MSG#1 -> MSG#2
682 * MCNT 1: MSG#2
684 msgout >>= 8;
685 msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
687 msgout |= MV_VALID; /* MV valid */
688 msgout |= (unsigned int)data->msgout_len; /* len */
689 nsp32_write4(base, SCSI_MSG_OUT, msgout);
690 } else {
691 /* data->msgout_len > 3 */
692 nsp32_write4(base, SCSI_MSG_OUT, 0);
696 * set selection timeout(= 250ms)
698 nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
701 * set SREQ hazard killer sampling rate
703 * TODO: sample_rate (BASE+0F) is 0 when internal clock = 40MHz.
704 * check other internal clock!
706 nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
709 * clear Arbit
711 nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
714 * set SYNCREG
715 * Don't set BM_START_ADR before setting this register.
717 nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
720 * set ACKWIDTH
722 nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
724 nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
725 "syncreg=0x%x, ackwidth=0x%x, sgtpaddr=0x%x, id=0x%x",
726 nsp32_read1(base, SYNC_REG), nsp32_read1(base, ACK_WIDTH),
727 nsp32_read4(base, SGT_ADR), nsp32_read1(base, SCSI_OUT_LATCH_TARGET_ID));
728 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "msgout_len=%d, msgout=0x%x",
729 data->msgout_len, msgout);
732 * set SGT ADDR (physical address)
734 nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
737 * set TRANSFER CONTROL REG
739 command = 0;
740 command |= (TRANSFER_GO | ALL_COUNTER_CLR);
741 if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
742 if (SCpnt->request_bufflen > 0) {
743 command |= BM_START;
745 } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
746 command |= CB_MMIO_MODE;
747 } else if (data->trans_method & NSP32_TRANSFER_PIO) {
748 command |= CB_IO_MODE;
750 nsp32_write2(base, TRANSFER_CONTROL, command);
753 * start AUTO SCSI, kick off arbitration
755 command = (CLEAR_CDB_FIFO_POINTER |
756 AUTOSCSI_START |
757 AUTO_MSGIN_00_OR_04 |
758 AUTO_MSGIN_02 |
759 AUTO_ATN );
760 nsp32_write2(base, COMMAND_CONTROL, command);
763 * Check arbitration
765 status = nsp32_arbitration(SCpnt, base);
767 out:
769 * IRQ enable
771 nsp32_write2(base, IRQ_CONTROL, 0);
773 return status;
778 * Arbitration Status Check
780 * Note: Arbitration counter is waited during ARBIT_GO is not lifting.
781 * Using udelay(1) consumes CPU time and system time, but
782 * arbitration delay time is defined minimal 2.4us in SCSI
783 * specification, thus udelay works as coarse grained wait timer.
785 static int nsp32_arbitration(struct scsi_cmnd *SCpnt, unsigned int base)
787 unsigned char arbit;
788 int status = TRUE;
789 int time = 0;
791 do {
792 arbit = nsp32_read1(base, ARBIT_STATUS);
793 time++;
794 } while ((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
795 (time <= ARBIT_TIMEOUT_TIME));
797 nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
798 "arbit: 0x%x, delay time: %d", arbit, time);
800 if (arbit & ARBIT_WIN) {
801 /* Arbitration succeeded */
802 SCpnt->result = DID_OK << 16;
803 nsp32_index_write1(base, EXT_PORT, LED_ON); /* PCI LED on */
804 } else if (arbit & ARBIT_FAIL) {
805 /* Arbitration failed */
806 SCpnt->result = DID_BUS_BUSY << 16;
807 status = FALSE;
808 } else {
810 * unknown error or ARBIT_GO timeout,
811 * something lock up! guess no connection.
813 nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "arbit timeout");
814 SCpnt->result = DID_NO_CONNECT << 16;
815 status = FALSE;
819 * clear Arbit
821 nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
823 return status;
828 * reselection
830 * Note: This reselection routine is called from msgin_occur,
831 * reselection target id&lun must be already set.
832 * SCSI-2 says IDENTIFY implies RESTORE_POINTER operation.
834 static int nsp32_reselection(struct scsi_cmnd *SCpnt, unsigned char newlun)
836 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
837 unsigned int host_id = SCpnt->device->host->this_id;
838 unsigned int base = SCpnt->device->host->io_port;
839 unsigned char tmpid, newid;
841 nsp32_dbg(NSP32_DEBUG_RESELECTION, "enter");
844 * calculate reselected SCSI ID
846 tmpid = nsp32_read1(base, RESELECT_ID);
847 tmpid &= (~BIT(host_id));
848 newid = 0;
849 while (tmpid) {
850 if (tmpid & 1) {
851 break;
853 tmpid >>= 1;
854 newid++;
858 * If reselected New ID:LUN is not existed
859 * or current nexus is not existed, unexpected
860 * reselection is occurred. Send reject message.
862 if (newid >= ARRAY_SIZE(data->lunt) || newlun >= ARRAY_SIZE(data->lunt[0])) {
863 nsp32_msg(KERN_WARNING, "unknown id/lun");
864 return FALSE;
865 } else if(data->lunt[newid][newlun].SCpnt == NULL) {
866 nsp32_msg(KERN_WARNING, "no SCSI command is processing");
867 return FALSE;
870 data->cur_id = newid;
871 data->cur_lun = newlun;
872 data->cur_target = &(data->target[newid]);
873 data->cur_lunt = &(data->lunt[newid][newlun]);
875 /* reset SACK/SavedACK counter (or ALL clear?) */
876 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
878 return TRUE;
883 * nsp32_setup_sg_table - build scatter gather list for transfer data
884 * with bus master.
886 * Note: NinjaSCSI-32Bi/UDE bus master can not transfer over 64KB at a time.
888 static int nsp32_setup_sg_table(struct scsi_cmnd *SCpnt)
890 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
891 struct scatterlist *sgl;
892 nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
893 int num, i;
894 u32_le l;
896 if (SCpnt->request_bufflen == 0) {
897 return TRUE;
900 if (sgt == NULL) {
901 nsp32_dbg(NSP32_DEBUG_SGLIST, "SGT == null");
902 return FALSE;
905 if (SCpnt->use_sg) {
906 sgl = (struct scatterlist *)SCpnt->request_buffer;
907 num = pci_map_sg(data->Pci, sgl, SCpnt->use_sg,
908 SCpnt->sc_data_direction);
909 for (i = 0; i < num; i++) {
911 * Build nsp32_sglist, substitute sg dma addresses.
913 sgt[i].addr = cpu_to_le32(sg_dma_address(sgl));
914 sgt[i].len = cpu_to_le32(sg_dma_len(sgl));
915 sgl++;
917 if (le32_to_cpu(sgt[i].len) > 0x10000) {
918 nsp32_msg(KERN_ERR,
919 "can't transfer over 64KB at a time, size=0x%lx", le32_to_cpu(sgt[i].len));
920 return FALSE;
922 nsp32_dbg(NSP32_DEBUG_SGLIST,
923 "num 0x%x : addr 0x%lx len 0x%lx",
925 le32_to_cpu(sgt[i].addr),
926 le32_to_cpu(sgt[i].len ));
929 /* set end mark */
930 l = le32_to_cpu(sgt[num-1].len);
931 sgt[num-1].len = cpu_to_le32(l | SGTEND);
933 } else {
934 SCpnt->SCp.have_data_in = pci_map_single(data->Pci,
935 SCpnt->request_buffer, SCpnt->request_bufflen,
936 SCpnt->sc_data_direction);
938 sgt[0].addr = cpu_to_le32(SCpnt->SCp.have_data_in);
939 sgt[0].len = cpu_to_le32(SCpnt->request_bufflen | SGTEND); /* set end mark */
941 if (SCpnt->request_bufflen > 0x10000) {
942 nsp32_msg(KERN_ERR,
943 "can't transfer over 64KB at a time, size=0x%lx", SCpnt->request_bufflen);
944 return FALSE;
946 nsp32_dbg(NSP32_DEBUG_SGLIST, "single : addr 0x%lx len=0x%lx",
947 le32_to_cpu(sgt[0].addr),
948 le32_to_cpu(sgt[0].len ));
951 return TRUE;
954 static int nsp32_queuecommand(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
956 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
957 nsp32_target *target;
958 nsp32_lunt *cur_lunt;
959 int ret;
961 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
962 "enter. target: 0x%x LUN: 0x%x cmnd: 0x%x cmndlen: 0x%x "
963 "use_sg: 0x%x reqbuf: 0x%lx reqlen: 0x%x",
964 SCpnt->device->id, SCpnt->device->lun, SCpnt->cmnd[0], SCpnt->cmd_len,
965 SCpnt->use_sg, SCpnt->request_buffer, SCpnt->request_bufflen);
967 if (data->CurrentSC != NULL) {
968 nsp32_msg(KERN_ERR, "Currentsc != NULL. Cancel this command request");
969 data->CurrentSC = NULL;
970 SCpnt->result = DID_NO_CONNECT << 16;
971 done(SCpnt);
972 return 0;
975 /* check target ID is not same as this initiator ID */
976 if (scmd_id(SCpnt) == SCpnt->device->host->this_id) {
977 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "terget==host???");
978 SCpnt->result = DID_BAD_TARGET << 16;
979 done(SCpnt);
980 return 0;
983 /* check target LUN is allowable value */
984 if (SCpnt->device->lun >= MAX_LUN) {
985 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "no more lun");
986 SCpnt->result = DID_BAD_TARGET << 16;
987 done(SCpnt);
988 return 0;
991 show_command(SCpnt);
993 SCpnt->scsi_done = done;
994 data->CurrentSC = SCpnt;
995 SCpnt->SCp.Status = CHECK_CONDITION;
996 SCpnt->SCp.Message = 0;
997 SCpnt->resid = SCpnt->request_bufflen;
999 SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
1000 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
1001 SCpnt->SCp.buffer = NULL;
1002 SCpnt->SCp.buffers_residual = 0;
1004 /* initialize data */
1005 data->msgout_len = 0;
1006 data->msgin_len = 0;
1007 cur_lunt = &(data->lunt[SCpnt->device->id][SCpnt->device->lun]);
1008 cur_lunt->SCpnt = SCpnt;
1009 cur_lunt->save_datp = 0;
1010 cur_lunt->msgin03 = FALSE;
1011 data->cur_lunt = cur_lunt;
1012 data->cur_id = SCpnt->device->id;
1013 data->cur_lun = SCpnt->device->lun;
1015 ret = nsp32_setup_sg_table(SCpnt);
1016 if (ret == FALSE) {
1017 nsp32_msg(KERN_ERR, "SGT fail");
1018 SCpnt->result = DID_ERROR << 16;
1019 nsp32_scsi_done(SCpnt);
1020 return 0;
1023 /* Build IDENTIFY */
1024 nsp32_build_identify(SCpnt);
1027 * If target is the first time to transfer after the reset
1028 * (target don't have SDTR_DONE and SDTR_INITIATOR), sync
1029 * message SDTR is needed to do synchronous transfer.
1031 target = &data->target[scmd_id(SCpnt)];
1032 data->cur_target = target;
1034 if (!(target->sync_flag & (SDTR_DONE | SDTR_INITIATOR | SDTR_TARGET))) {
1035 unsigned char period, offset;
1037 if (trans_mode != ASYNC_MODE) {
1038 nsp32_set_max_sync(data, target, &period, &offset);
1039 nsp32_build_sdtr(SCpnt, period, offset);
1040 target->sync_flag |= SDTR_INITIATOR;
1041 } else {
1042 nsp32_set_async(data, target);
1043 target->sync_flag |= SDTR_DONE;
1046 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
1047 "SDTR: entry: %d start_period: 0x%x offset: 0x%x\n",
1048 target->limit_entry, period, offset);
1049 } else if (target->sync_flag & SDTR_INITIATOR) {
1051 * It was negotiating SDTR with target, sending from the
1052 * initiator, but there are no chance to remove this flag.
1053 * Set async because we don't get proper negotiation.
1055 nsp32_set_async(data, target);
1056 target->sync_flag &= ~SDTR_INITIATOR;
1057 target->sync_flag |= SDTR_DONE;
1059 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
1060 "SDTR_INITIATOR: fall back to async");
1061 } else if (target->sync_flag & SDTR_TARGET) {
1063 * It was negotiating SDTR with target, sending from target,
1064 * but there are no chance to remove this flag. Set async
1065 * because we don't get proper negotiation.
1067 nsp32_set_async(data, target);
1068 target->sync_flag &= ~SDTR_TARGET;
1069 target->sync_flag |= SDTR_DONE;
1071 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
1072 "Unknown SDTR from target is reached, fall back to async.");
1075 nsp32_dbg(NSP32_DEBUG_TARGETFLAG,
1076 "target: %d sync_flag: 0x%x syncreg: 0x%x ackwidth: 0x%x",
1077 SCpnt->device->id, target->sync_flag, target->syncreg,
1078 target->ackwidth);
1080 /* Selection */
1081 if (auto_param == 0) {
1082 ret = nsp32_selection_autopara(SCpnt);
1083 } else {
1084 ret = nsp32_selection_autoscsi(SCpnt);
1087 if (ret != TRUE) {
1088 nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "selection fail");
1089 nsp32_scsi_done(SCpnt);
1092 return 0;
1095 /* initialize asic */
1096 static int nsp32hw_init(nsp32_hw_data *data)
1098 unsigned int base = data->BaseAddress;
1099 unsigned short irq_stat;
1100 unsigned long lc_reg;
1101 unsigned char power;
1103 lc_reg = nsp32_index_read4(base, CFG_LATE_CACHE);
1104 if ((lc_reg & 0xff00) == 0) {
1105 lc_reg |= (0x20 << 8);
1106 nsp32_index_write2(base, CFG_LATE_CACHE, lc_reg & 0xffff);
1109 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
1110 nsp32_write2(base, TRANSFER_CONTROL, 0);
1111 nsp32_write4(base, BM_CNT, 0);
1112 nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
1114 do {
1115 irq_stat = nsp32_read2(base, IRQ_STATUS);
1116 nsp32_dbg(NSP32_DEBUG_INIT, "irq_stat 0x%x", irq_stat);
1117 } while (irq_stat & IRQSTATUS_ANY_IRQ);
1120 * Fill FIFO_FULL_SHLD, FIFO_EMPTY_SHLD. Below parameter is
1121 * designated by specification.
1123 if ((data->trans_method & NSP32_TRANSFER_PIO) ||
1124 (data->trans_method & NSP32_TRANSFER_MMIO)) {
1125 nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x40);
1126 nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x40);
1127 } else if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
1128 nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x10);
1129 nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x60);
1130 } else {
1131 nsp32_dbg(NSP32_DEBUG_INIT, "unknown transfer mode");
1134 nsp32_dbg(NSP32_DEBUG_INIT, "full 0x%x emp 0x%x",
1135 nsp32_index_read1(base, FIFO_FULL_SHLD_COUNT),
1136 nsp32_index_read1(base, FIFO_EMPTY_SHLD_COUNT));
1138 nsp32_index_write1(base, CLOCK_DIV, data->clock);
1139 nsp32_index_write1(base, BM_CYCLE, MEMRD_CMD1 | SGT_AUTO_PARA_MEMED_CMD);
1140 nsp32_write1(base, PARITY_CONTROL, 0); /* parity check is disable */
1143 * initialize MISC_WRRD register
1145 * Note: Designated parameters is obeyed as following:
1146 * MISC_SCSI_DIRECTION_DETECTOR_SELECT: It must be set.
1147 * MISC_MASTER_TERMINATION_SELECT: It must be set.
1148 * MISC_BMREQ_NEGATE_TIMING_SEL: It should be set.
1149 * MISC_AUTOSEL_TIMING_SEL: It should be set.
1150 * MISC_BMSTOP_CHANGE2_NONDATA_PHASE: It should be set.
1151 * MISC_DELAYED_BMSTART: It's selected for safety.
1153 * Note: If MISC_BMSTOP_CHANGE2_NONDATA_PHASE is set, then
1154 * we have to set TRANSFERCONTROL_BM_START as 0 and set
1155 * appropriate value before restarting bus master transfer.
1157 nsp32_index_write2(base, MISC_WR,
1158 (SCSI_DIRECTION_DETECTOR_SELECT |
1159 DELAYED_BMSTART |
1160 MASTER_TERMINATION_SELECT |
1161 BMREQ_NEGATE_TIMING_SEL |
1162 AUTOSEL_TIMING_SEL |
1163 BMSTOP_CHANGE2_NONDATA_PHASE));
1165 nsp32_index_write1(base, TERM_PWR_CONTROL, 0);
1166 power = nsp32_index_read1(base, TERM_PWR_CONTROL);
1167 if (!(power & SENSE)) {
1168 nsp32_msg(KERN_INFO, "term power on");
1169 nsp32_index_write1(base, TERM_PWR_CONTROL, BPWR);
1172 nsp32_write2(base, TIMER_SET, TIMER_STOP);
1173 nsp32_write2(base, TIMER_SET, TIMER_STOP); /* Required 2 times */
1175 nsp32_write1(base, SYNC_REG, 0);
1176 nsp32_write1(base, ACK_WIDTH, 0);
1177 nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
1180 * enable to select designated IRQ (except for
1181 * IRQSELECT_SERR, IRQSELECT_PERR, IRQSELECT_BMCNTERR)
1183 nsp32_index_write2(base, IRQ_SELECT, IRQSELECT_TIMER_IRQ |
1184 IRQSELECT_SCSIRESET_IRQ |
1185 IRQSELECT_FIFO_SHLD_IRQ |
1186 IRQSELECT_RESELECT_IRQ |
1187 IRQSELECT_PHASE_CHANGE_IRQ |
1188 IRQSELECT_AUTO_SCSI_SEQ_IRQ |
1189 // IRQSELECT_BMCNTERR_IRQ |
1190 IRQSELECT_TARGET_ABORT_IRQ |
1191 IRQSELECT_MASTER_ABORT_IRQ );
1192 nsp32_write2(base, IRQ_CONTROL, 0);
1194 /* PCI LED off */
1195 nsp32_index_write1(base, EXT_PORT_DDR, LED_OFF);
1196 nsp32_index_write1(base, EXT_PORT, LED_OFF);
1198 return TRUE;
1202 /* interrupt routine */
1203 static irqreturn_t do_nsp32_isr(int irq, void *dev_id)
1205 nsp32_hw_data *data = dev_id;
1206 unsigned int base = data->BaseAddress;
1207 struct scsi_cmnd *SCpnt = data->CurrentSC;
1208 unsigned short auto_stat, irq_stat, trans_stat;
1209 unsigned char busmon, busphase;
1210 unsigned long flags;
1211 int ret;
1212 int handled = 0;
1214 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1215 struct Scsi_Host *host = data->Host;
1216 spin_lock_irqsave(host->host_lock, flags);
1217 #else
1218 spin_lock_irqsave(&io_request_lock, flags);
1219 #endif
1222 * IRQ check, then enable IRQ mask
1224 irq_stat = nsp32_read2(base, IRQ_STATUS);
1225 nsp32_dbg(NSP32_DEBUG_INTR,
1226 "enter IRQ: %d, IRQstatus: 0x%x", irq, irq_stat);
1227 /* is this interrupt comes from Ninja asic? */
1228 if ((irq_stat & IRQSTATUS_ANY_IRQ) == 0) {
1229 nsp32_dbg(NSP32_DEBUG_INTR, "shared interrupt: irq other 0x%x", irq_stat);
1230 goto out2;
1232 handled = 1;
1233 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
1235 busmon = nsp32_read1(base, SCSI_BUS_MONITOR);
1236 busphase = busmon & BUSMON_PHASE_MASK;
1238 trans_stat = nsp32_read2(base, TRANSFER_STATUS);
1239 if ((irq_stat == 0xffff) && (trans_stat == 0xffff)) {
1240 nsp32_msg(KERN_INFO, "card disconnect");
1241 if (data->CurrentSC != NULL) {
1242 nsp32_msg(KERN_INFO, "clean up current SCSI command");
1243 SCpnt->result = DID_BAD_TARGET << 16;
1244 nsp32_scsi_done(SCpnt);
1246 goto out;
1249 /* Timer IRQ */
1250 if (irq_stat & IRQSTATUS_TIMER_IRQ) {
1251 nsp32_dbg(NSP32_DEBUG_INTR, "timer stop");
1252 nsp32_write2(base, TIMER_SET, TIMER_STOP);
1253 goto out;
1256 /* SCSI reset */
1257 if (irq_stat & IRQSTATUS_SCSIRESET_IRQ) {
1258 nsp32_msg(KERN_INFO, "detected someone do bus reset");
1259 nsp32_do_bus_reset(data);
1260 if (SCpnt != NULL) {
1261 SCpnt->result = DID_RESET << 16;
1262 nsp32_scsi_done(SCpnt);
1264 goto out;
1267 if (SCpnt == NULL) {
1268 nsp32_msg(KERN_WARNING, "SCpnt==NULL this can't be happened");
1269 nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
1270 goto out;
1274 * AutoSCSI Interrupt.
1275 * Note: This interrupt is occurred when AutoSCSI is finished. Then
1276 * check SCSIEXECUTEPHASE, and do appropriate action. Each phases are
1277 * recorded when AutoSCSI sequencer has been processed.
1279 if(irq_stat & IRQSTATUS_AUTOSCSI_IRQ) {
1280 /* getting SCSI executed phase */
1281 auto_stat = nsp32_read2(base, SCSI_EXECUTE_PHASE);
1282 nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
1284 /* Selection Timeout, go busfree phase. */
1285 if (auto_stat & SELECTION_TIMEOUT) {
1286 nsp32_dbg(NSP32_DEBUG_INTR,
1287 "selection timeout occurred");
1289 SCpnt->result = DID_TIME_OUT << 16;
1290 nsp32_scsi_done(SCpnt);
1291 goto out;
1294 if (auto_stat & MSGOUT_PHASE) {
1296 * MsgOut phase was processed.
1297 * If MSG_IN_OCCUER is not set, then MsgOut phase is
1298 * completed. Thus, msgout_len must reset. Otherwise,
1299 * nothing to do here. If MSG_OUT_OCCUER is occurred,
1300 * then we will encounter the condition and check.
1302 if (!(auto_stat & MSG_IN_OCCUER) &&
1303 (data->msgout_len <= 3)) {
1305 * !MSG_IN_OCCUER && msgout_len <=3
1306 * ---> AutoSCSI with MSGOUTreg is processed.
1308 data->msgout_len = 0;
1311 nsp32_dbg(NSP32_DEBUG_INTR, "MsgOut phase processed");
1314 if ((auto_stat & DATA_IN_PHASE) &&
1315 (SCpnt->resid > 0) &&
1316 ((nsp32_read2(base, FIFO_REST_CNT) & FIFO_REST_MASK) != 0)) {
1317 printk( "auto+fifo\n");
1318 //nsp32_pio_read(SCpnt);
1321 if (auto_stat & (DATA_IN_PHASE | DATA_OUT_PHASE)) {
1322 /* DATA_IN_PHASE/DATA_OUT_PHASE was processed. */
1323 nsp32_dbg(NSP32_DEBUG_INTR,
1324 "Data in/out phase processed");
1326 /* read BMCNT, SGT pointer addr */
1327 nsp32_dbg(NSP32_DEBUG_INTR, "BMCNT=0x%lx",
1328 nsp32_read4(base, BM_CNT));
1329 nsp32_dbg(NSP32_DEBUG_INTR, "addr=0x%lx",
1330 nsp32_read4(base, SGT_ADR));
1331 nsp32_dbg(NSP32_DEBUG_INTR, "SACK=0x%lx",
1332 nsp32_read4(base, SACK_CNT));
1333 nsp32_dbg(NSP32_DEBUG_INTR, "SSACK=0x%lx",
1334 nsp32_read4(base, SAVED_SACK_CNT));
1336 SCpnt->resid = 0; /* all data transfered! */
1340 * MsgIn Occur
1342 if (auto_stat & MSG_IN_OCCUER) {
1343 nsp32_msgin_occur(SCpnt, irq_stat, auto_stat);
1347 * MsgOut Occur
1349 if (auto_stat & MSG_OUT_OCCUER) {
1350 nsp32_msgout_occur(SCpnt);
1354 * Bus Free Occur
1356 if (auto_stat & BUS_FREE_OCCUER) {
1357 ret = nsp32_busfree_occur(SCpnt, auto_stat);
1358 if (ret == TRUE) {
1359 goto out;
1363 if (auto_stat & STATUS_PHASE) {
1365 * Read CSB and substitute CSB for SCpnt->result
1366 * to save status phase stutas byte.
1367 * scsi error handler checks host_byte (DID_*:
1368 * low level driver to indicate status), then checks
1369 * status_byte (SCSI status byte).
1371 SCpnt->result = (int)nsp32_read1(base, SCSI_CSB_IN);
1374 if (auto_stat & ILLEGAL_PHASE) {
1375 /* Illegal phase is detected. SACK is not back. */
1376 nsp32_msg(KERN_WARNING,
1377 "AUTO SCSI ILLEGAL PHASE OCCUR!!!!");
1379 /* TODO: currently we don't have any action... bus reset? */
1382 * To send back SACK, assert, wait, and negate.
1384 nsp32_sack_assert(data);
1385 nsp32_wait_req(data, NEGATE);
1386 nsp32_sack_negate(data);
1390 if (auto_stat & COMMAND_PHASE) {
1391 /* nothing to do */
1392 nsp32_dbg(NSP32_DEBUG_INTR, "Command phase processed");
1395 if (auto_stat & AUTOSCSI_BUSY) {
1396 /* AutoSCSI is running */
1399 show_autophase(auto_stat);
1402 /* FIFO_SHLD_IRQ */
1403 if (irq_stat & IRQSTATUS_FIFO_SHLD_IRQ) {
1404 nsp32_dbg(NSP32_DEBUG_INTR, "FIFO IRQ");
1406 switch(busphase) {
1407 case BUSPHASE_DATA_OUT:
1408 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/write");
1410 //nsp32_pio_write(SCpnt);
1412 break;
1414 case BUSPHASE_DATA_IN:
1415 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/read");
1417 //nsp32_pio_read(SCpnt);
1419 break;
1421 case BUSPHASE_STATUS:
1422 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/status");
1424 SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
1426 break;
1427 default:
1428 nsp32_dbg(NSP32_DEBUG_INTR, "fifo/other phase");
1429 nsp32_dbg(NSP32_DEBUG_INTR, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
1430 show_busphase(busphase);
1431 break;
1434 goto out;
1437 /* Phase Change IRQ */
1438 if (irq_stat & IRQSTATUS_PHASE_CHANGE_IRQ) {
1439 nsp32_dbg(NSP32_DEBUG_INTR, "phase change IRQ");
1441 switch(busphase) {
1442 case BUSPHASE_MESSAGE_IN:
1443 nsp32_dbg(NSP32_DEBUG_INTR, "phase chg/msg in");
1444 nsp32_msgin_occur(SCpnt, irq_stat, 0);
1445 break;
1446 default:
1447 nsp32_msg(KERN_WARNING, "phase chg/other phase?");
1448 nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x\n",
1449 irq_stat, trans_stat);
1450 show_busphase(busphase);
1451 break;
1453 goto out;
1456 /* PCI_IRQ */
1457 if (irq_stat & IRQSTATUS_PCI_IRQ) {
1458 nsp32_dbg(NSP32_DEBUG_INTR, "PCI IRQ occurred");
1459 /* Do nothing */
1462 /* BMCNTERR_IRQ */
1463 if (irq_stat & IRQSTATUS_BMCNTERR_IRQ) {
1464 nsp32_msg(KERN_ERR, "Received unexpected BMCNTERR IRQ! ");
1466 * TODO: To be implemented improving bus master
1467 * transfer reliablity when BMCNTERR is occurred in
1468 * AutoSCSI phase described in specification.
1472 #if 0
1473 nsp32_dbg(NSP32_DEBUG_INTR,
1474 "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
1475 show_busphase(busphase);
1476 #endif
1478 out:
1479 /* disable IRQ mask */
1480 nsp32_write2(base, IRQ_CONTROL, 0);
1482 out2:
1483 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1484 spin_unlock_irqrestore(host->host_lock, flags);
1485 #else
1486 spin_unlock_irqrestore(&io_request_lock, flags);
1487 #endif
1489 nsp32_dbg(NSP32_DEBUG_INTR, "exit");
1491 return IRQ_RETVAL(handled);
1494 #undef SPRINTF
1495 #define SPRINTF(args...) \
1496 do { \
1497 if(length > (pos - buffer)) { \
1498 pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
1499 nsp32_dbg(NSP32_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length, length - (pos - buffer));\
1501 } while(0)
1502 static int nsp32_proc_info(
1503 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1504 struct Scsi_Host *host,
1505 #endif
1506 char *buffer,
1507 char **start,
1508 off_t offset,
1509 int length,
1510 #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1511 int hostno,
1512 #endif
1513 int inout)
1515 char *pos = buffer;
1516 int thislength;
1517 unsigned long flags;
1518 nsp32_hw_data *data;
1519 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1520 int hostno;
1521 #else
1522 struct Scsi_Host *host;
1523 #endif
1524 unsigned int base;
1525 unsigned char mode_reg;
1526 int id, speed;
1527 long model;
1529 /* Write is not supported, just return. */
1530 if (inout == TRUE) {
1531 return -EINVAL;
1534 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1535 hostno = host->host_no;
1536 #else
1537 /* search this HBA host */
1538 host = scsi_host_hn_get(hostno);
1539 if (host == NULL) {
1540 return -ESRCH;
1542 #endif
1543 data = (nsp32_hw_data *)host->hostdata;
1544 base = host->io_port;
1546 SPRINTF("NinjaSCSI-32 status\n\n");
1547 SPRINTF("Driver version: %s, $Revision: 1.33 $\n", nsp32_release_version);
1548 SPRINTF("SCSI host No.: %d\n", hostno);
1549 SPRINTF("IRQ: %d\n", host->irq);
1550 SPRINTF("IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
1551 SPRINTF("MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
1552 SPRINTF("sg_tablesize: %d\n", host->sg_tablesize);
1553 SPRINTF("Chip revision: 0x%x\n", (nsp32_read2(base, INDEX_REG) >> 8) & 0xff);
1555 mode_reg = nsp32_index_read1(base, CHIP_MODE);
1556 model = data->pci_devid->driver_data;
1558 #ifdef CONFIG_PM
1559 SPRINTF("Power Management: %s\n", (mode_reg & OPTF) ? "yes" : "no");
1560 #endif
1561 SPRINTF("OEM: %ld, %s\n", (mode_reg & (OEM0|OEM1)), nsp32_model[model]);
1563 spin_lock_irqsave(&(data->Lock), flags);
1564 SPRINTF("CurrentSC: 0x%p\n\n", data->CurrentSC);
1565 spin_unlock_irqrestore(&(data->Lock), flags);
1568 SPRINTF("SDTR status\n");
1569 for (id = 0; id < ARRAY_SIZE(data->target); id++) {
1571 SPRINTF("id %d: ", id);
1573 if (id == host->this_id) {
1574 SPRINTF("----- NinjaSCSI-32 host adapter\n");
1575 continue;
1578 if (data->target[id].sync_flag == SDTR_DONE) {
1579 if (data->target[id].period == 0 &&
1580 data->target[id].offset == ASYNC_OFFSET ) {
1581 SPRINTF("async");
1582 } else {
1583 SPRINTF(" sync");
1585 } else {
1586 SPRINTF(" none");
1589 if (data->target[id].period != 0) {
1591 speed = 1000000 / (data->target[id].period * 4);
1593 SPRINTF(" transfer %d.%dMB/s, offset %d",
1594 speed / 1000,
1595 speed % 1000,
1596 data->target[id].offset
1599 SPRINTF("\n");
1603 thislength = pos - (buffer + offset);
1605 if(thislength < 0) {
1606 *start = NULL;
1607 return 0;
1611 thislength = min(thislength, length);
1612 *start = buffer + offset;
1614 return thislength;
1616 #undef SPRINTF
1621 * Reset parameters and call scsi_done for data->cur_lunt.
1622 * Be careful setting SCpnt->result = DID_* before calling this function.
1624 static void nsp32_scsi_done(struct scsi_cmnd *SCpnt)
1626 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1627 unsigned int base = SCpnt->device->host->io_port;
1630 * unmap pci
1632 if (SCpnt->request_bufflen == 0) {
1633 goto skip;
1636 if (SCpnt->use_sg) {
1637 pci_unmap_sg(data->Pci,
1638 (struct scatterlist *)SCpnt->request_buffer,
1639 SCpnt->use_sg, SCpnt->sc_data_direction);
1640 } else {
1641 pci_unmap_single(data->Pci,
1642 (u32)SCpnt->SCp.have_data_in,
1643 SCpnt->request_bufflen,
1644 SCpnt->sc_data_direction);
1647 skip:
1649 * clear TRANSFERCONTROL_BM_START
1651 nsp32_write2(base, TRANSFER_CONTROL, 0);
1652 nsp32_write4(base, BM_CNT, 0);
1655 * call scsi_done
1657 (*SCpnt->scsi_done)(SCpnt);
1660 * reset parameters
1662 data->cur_lunt->SCpnt = NULL;
1663 data->cur_lunt = NULL;
1664 data->cur_target = NULL;
1665 data->CurrentSC = NULL;
1670 * Bus Free Occur
1672 * Current Phase is BUSFREE. AutoSCSI is automatically execute BUSFREE phase
1673 * with ACK reply when below condition is matched:
1674 * MsgIn 00: Command Complete.
1675 * MsgIn 02: Save Data Pointer.
1676 * MsgIn 04: Diconnect.
1677 * In other case, unexpected BUSFREE is detected.
1679 static int nsp32_busfree_occur(struct scsi_cmnd *SCpnt, unsigned short execph)
1681 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1682 unsigned int base = SCpnt->device->host->io_port;
1684 nsp32_dbg(NSP32_DEBUG_BUSFREE, "enter execph=0x%x", execph);
1685 show_autophase(execph);
1687 nsp32_write4(base, BM_CNT, 0);
1688 nsp32_write2(base, TRANSFER_CONTROL, 0);
1691 * MsgIn 02: Save Data Pointer
1693 * VALID:
1694 * Save Data Pointer is received. Adjust pointer.
1696 * NO-VALID:
1697 * SCSI-3 says if Save Data Pointer is not received, then we restart
1698 * processing and we can't adjust any SCSI data pointer in next data
1699 * phase.
1701 if (execph & MSGIN_02_VALID) {
1702 nsp32_dbg(NSP32_DEBUG_BUSFREE, "MsgIn02_Valid");
1705 * Check sack_cnt/saved_sack_cnt, then adjust sg table if
1706 * needed.
1708 if (!(execph & MSGIN_00_VALID) &&
1709 ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE))) {
1710 unsigned int sacklen, s_sacklen;
1713 * Read SACK count and SAVEDSACK count, then compare.
1715 sacklen = nsp32_read4(base, SACK_CNT );
1716 s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
1719 * If SAVEDSACKCNT == 0, it means SavedDataPointer is
1720 * come after data transfering.
1722 if (s_sacklen > 0) {
1724 * Comparing between sack and savedsack to
1725 * check the condition of AutoMsgIn03.
1727 * If they are same, set msgin03 == TRUE,
1728 * COMMANDCONTROL_AUTO_MSGIN_03 is enabled at
1729 * reselection. On the other hand, if they
1730 * aren't same, set msgin03 == FALSE, and
1731 * COMMANDCONTROL_AUTO_MSGIN_03 is disabled at
1732 * reselection.
1734 if (sacklen != s_sacklen) {
1735 data->cur_lunt->msgin03 = FALSE;
1736 } else {
1737 data->cur_lunt->msgin03 = TRUE;
1740 nsp32_adjust_busfree(SCpnt, s_sacklen);
1744 /* This value has not substitude with valid value yet... */
1745 //data->cur_lunt->save_datp = data->cur_datp;
1746 } else {
1748 * no processing.
1752 if (execph & MSGIN_03_VALID) {
1753 /* MsgIn03 was valid to be processed. No need processing. */
1757 * target SDTR check
1759 if (data->cur_target->sync_flag & SDTR_INITIATOR) {
1761 * SDTR negotiation pulled by the initiator has not
1762 * finished yet. Fall back to ASYNC mode.
1764 nsp32_set_async(data, data->cur_target);
1765 data->cur_target->sync_flag &= ~SDTR_INITIATOR;
1766 data->cur_target->sync_flag |= SDTR_DONE;
1767 } else if (data->cur_target->sync_flag & SDTR_TARGET) {
1769 * SDTR negotiation pulled by the target has been
1770 * negotiating.
1772 if (execph & (MSGIN_00_VALID | MSGIN_04_VALID)) {
1774 * If valid message is received, then
1775 * negotiation is succeeded.
1777 } else {
1779 * On the contrary, if unexpected bus free is
1780 * occurred, then negotiation is failed. Fall
1781 * back to ASYNC mode.
1783 nsp32_set_async(data, data->cur_target);
1785 data->cur_target->sync_flag &= ~SDTR_TARGET;
1786 data->cur_target->sync_flag |= SDTR_DONE;
1790 * It is always ensured by SCSI standard that initiator
1791 * switches into Bus Free Phase after
1792 * receiving message 00 (Command Complete), 04 (Disconnect).
1793 * It's the reason that processing here is valid.
1795 if (execph & MSGIN_00_VALID) {
1796 /* MsgIn 00: Command Complete */
1797 nsp32_dbg(NSP32_DEBUG_BUSFREE, "command complete");
1799 SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
1800 SCpnt->SCp.Message = 0;
1801 nsp32_dbg(NSP32_DEBUG_BUSFREE,
1802 "normal end stat=0x%x resid=0x%x\n",
1803 SCpnt->SCp.Status, SCpnt->resid);
1804 SCpnt->result = (DID_OK << 16) |
1805 (SCpnt->SCp.Message << 8) |
1806 (SCpnt->SCp.Status << 0);
1807 nsp32_scsi_done(SCpnt);
1808 /* All operation is done */
1809 return TRUE;
1810 } else if (execph & MSGIN_04_VALID) {
1811 /* MsgIn 04: Disconnect */
1812 SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
1813 SCpnt->SCp.Message = 4;
1815 nsp32_dbg(NSP32_DEBUG_BUSFREE, "disconnect");
1816 return TRUE;
1817 } else {
1818 /* Unexpected bus free */
1819 nsp32_msg(KERN_WARNING, "unexpected bus free occurred");
1821 /* DID_ERROR? */
1822 //SCpnt->result = (DID_OK << 16) | (SCpnt->SCp.Message << 8) | (SCpnt->SCp.Status << 0);
1823 SCpnt->result = DID_ERROR << 16;
1824 nsp32_scsi_done(SCpnt);
1825 return TRUE;
1827 return FALSE;
1832 * nsp32_adjust_busfree - adjusting SG table
1834 * Note: This driver adjust the SG table using SCSI ACK
1835 * counter instead of BMCNT counter!
1837 static void nsp32_adjust_busfree(struct scsi_cmnd *SCpnt, unsigned int s_sacklen)
1839 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1840 int old_entry = data->cur_entry;
1841 int new_entry;
1842 int sg_num = data->cur_lunt->sg_num;
1843 nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
1844 unsigned int restlen, sentlen;
1845 u32_le len, addr;
1847 nsp32_dbg(NSP32_DEBUG_SGLIST, "old resid=0x%x", SCpnt->resid);
1849 /* adjust saved SACK count with 4 byte start address boundary */
1850 s_sacklen -= le32_to_cpu(sgt[old_entry].addr) & 3;
1853 * calculate new_entry from sack count and each sgt[].len
1854 * calculate the byte which is intent to send
1856 sentlen = 0;
1857 for (new_entry = old_entry; new_entry < sg_num; new_entry++) {
1858 sentlen += (le32_to_cpu(sgt[new_entry].len) & ~SGTEND);
1859 if (sentlen > s_sacklen) {
1860 break;
1864 /* all sgt is processed */
1865 if (new_entry == sg_num) {
1866 goto last;
1869 if (sentlen == s_sacklen) {
1870 /* XXX: confirm it's ok or not */
1871 /* In this case, it's ok because we are at
1872 the head element of the sg. restlen is correctly calculated. */
1875 /* calculate the rest length for transfering */
1876 restlen = sentlen - s_sacklen;
1878 /* update adjusting current SG table entry */
1879 len = le32_to_cpu(sgt[new_entry].len);
1880 addr = le32_to_cpu(sgt[new_entry].addr);
1881 addr += (len - restlen);
1882 sgt[new_entry].addr = cpu_to_le32(addr);
1883 sgt[new_entry].len = cpu_to_le32(restlen);
1885 /* set cur_entry with new_entry */
1886 data->cur_entry = new_entry;
1888 return;
1890 last:
1891 if (SCpnt->resid < sentlen) {
1892 nsp32_msg(KERN_ERR, "resid underflow");
1895 SCpnt->resid -= sentlen;
1896 nsp32_dbg(NSP32_DEBUG_SGLIST, "new resid=0x%x", SCpnt->resid);
1898 /* update hostdata and lun */
1900 return;
1905 * It's called MsgOut phase occur.
1906 * NinjaSCSI-32Bi/UDE automatically processes up to 3 messages in
1907 * message out phase. It, however, has more than 3 messages,
1908 * HBA creates the interrupt and we have to process by hand.
1910 static void nsp32_msgout_occur(struct scsi_cmnd *SCpnt)
1912 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1913 unsigned int base = SCpnt->device->host->io_port;
1914 //unsigned short command;
1915 long new_sgtp;
1916 int i;
1918 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
1919 "enter: msgout_len: 0x%x", data->msgout_len);
1922 * If MsgOut phase is occurred without having any
1923 * message, then No_Operation is sent (SCSI-2).
1925 if (data->msgout_len == 0) {
1926 nsp32_build_nop(SCpnt);
1930 * Set SGTP ADDR current entry for restarting AUTOSCSI,
1931 * because SGTP is incremented next point.
1932 * There is few statement in the specification...
1934 new_sgtp = data->cur_lunt->sglun_paddr +
1935 (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
1938 * send messages
1940 for (i = 0; i < data->msgout_len; i++) {
1941 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
1942 "%d : 0x%x", i, data->msgoutbuf[i]);
1945 * Check REQ is asserted.
1947 nsp32_wait_req(data, ASSERT);
1949 if (i == (data->msgout_len - 1)) {
1951 * If the last message, set the AutoSCSI restart
1952 * before send back the ack message. AutoSCSI
1953 * restart automatically negate ATN signal.
1955 //command = (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
1956 //nsp32_restart_autoscsi(SCpnt, command);
1957 nsp32_write2(base, COMMAND_CONTROL,
1958 (CLEAR_CDB_FIFO_POINTER |
1959 AUTO_COMMAND_PHASE |
1960 AUTOSCSI_RESTART |
1961 AUTO_MSGIN_00_OR_04 |
1962 AUTO_MSGIN_02 ));
1965 * Write data with SACK, then wait sack is
1966 * automatically negated.
1968 nsp32_write1(base, SCSI_DATA_WITH_ACK, data->msgoutbuf[i]);
1969 nsp32_wait_sack(data, NEGATE);
1971 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "bus: 0x%x\n",
1972 nsp32_read1(base, SCSI_BUS_MONITOR));
1975 data->msgout_len = 0;
1977 nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "exit");
1981 * Restart AutoSCSI
1983 * Note: Restarting AutoSCSI needs set:
1984 * SYNC_REG, ACK_WIDTH, SGT_ADR, TRANSFER_CONTROL
1986 static void nsp32_restart_autoscsi(struct scsi_cmnd *SCpnt, unsigned short command)
1988 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
1989 unsigned int base = data->BaseAddress;
1990 unsigned short transfer = 0;
1992 nsp32_dbg(NSP32_DEBUG_RESTART, "enter");
1994 if (data->cur_target == NULL || data->cur_lunt == NULL) {
1995 nsp32_msg(KERN_ERR, "Target or Lun is invalid");
1999 * set SYNC_REG
2000 * Don't set BM_START_ADR before setting this register.
2002 nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
2005 * set ACKWIDTH
2007 nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
2010 * set SREQ hazard killer sampling rate
2012 nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
2015 * set SGT ADDR (physical address)
2017 nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
2020 * set TRANSFER CONTROL REG
2022 transfer = 0;
2023 transfer |= (TRANSFER_GO | ALL_COUNTER_CLR);
2024 if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
2025 if (SCpnt->request_bufflen > 0) {
2026 transfer |= BM_START;
2028 } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
2029 transfer |= CB_MMIO_MODE;
2030 } else if (data->trans_method & NSP32_TRANSFER_PIO) {
2031 transfer |= CB_IO_MODE;
2033 nsp32_write2(base, TRANSFER_CONTROL, transfer);
2036 * restart AutoSCSI
2038 * TODO: COMMANDCONTROL_AUTO_COMMAND_PHASE is needed ?
2040 command |= (CLEAR_CDB_FIFO_POINTER |
2041 AUTO_COMMAND_PHASE |
2042 AUTOSCSI_RESTART );
2043 nsp32_write2(base, COMMAND_CONTROL, command);
2045 nsp32_dbg(NSP32_DEBUG_RESTART, "exit");
2050 * cannot run automatically message in occur
2052 static void nsp32_msgin_occur(struct scsi_cmnd *SCpnt,
2053 unsigned long irq_status,
2054 unsigned short execph)
2056 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2057 unsigned int base = SCpnt->device->host->io_port;
2058 unsigned char msg;
2059 unsigned char msgtype;
2060 unsigned char newlun;
2061 unsigned short command = 0;
2062 int msgclear = TRUE;
2063 long new_sgtp;
2064 int ret;
2067 * read first message
2068 * Use SCSIDATA_W_ACK instead of SCSIDATAIN, because the procedure
2069 * of Message-In have to be processed before sending back SCSI ACK.
2071 msg = nsp32_read1(base, SCSI_DATA_IN);
2072 data->msginbuf[(unsigned char)data->msgin_len] = msg;
2073 msgtype = data->msginbuf[0];
2074 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR,
2075 "enter: msglen: 0x%x msgin: 0x%x msgtype: 0x%x",
2076 data->msgin_len, msg, msgtype);
2079 * TODO: We need checking whether bus phase is message in?
2083 * assert SCSI ACK
2085 nsp32_sack_assert(data);
2088 * processing IDENTIFY
2090 if (msgtype & 0x80) {
2091 if (!(irq_status & IRQSTATUS_RESELECT_OCCUER)) {
2092 /* Invalid (non reselect) phase */
2093 goto reject;
2096 newlun = msgtype & 0x1f; /* TODO: SPI-3 compliant? */
2097 ret = nsp32_reselection(SCpnt, newlun);
2098 if (ret == TRUE) {
2099 goto restart;
2100 } else {
2101 goto reject;
2106 * processing messages except for IDENTIFY
2108 * TODO: Messages are all SCSI-2 terminology. SCSI-3 compliance is TODO.
2110 switch (msgtype) {
2112 * 1-byte message
2114 case COMMAND_COMPLETE:
2115 case DISCONNECT:
2117 * These messages should not be occurred.
2118 * They should be processed on AutoSCSI sequencer.
2120 nsp32_msg(KERN_WARNING,
2121 "unexpected message of AutoSCSI MsgIn: 0x%x", msg);
2122 break;
2124 case RESTORE_POINTERS:
2126 * AutoMsgIn03 is disabled, and HBA gets this message.
2129 if ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE)) {
2130 unsigned int s_sacklen;
2132 s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
2133 if ((execph & MSGIN_02_VALID) && (s_sacklen > 0)) {
2134 nsp32_adjust_busfree(SCpnt, s_sacklen);
2135 } else {
2136 /* No need to rewrite SGT */
2139 data->cur_lunt->msgin03 = FALSE;
2141 /* Update with the new value */
2143 /* reset SACK/SavedACK counter (or ALL clear?) */
2144 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
2147 * set new sg pointer
2149 new_sgtp = data->cur_lunt->sglun_paddr +
2150 (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
2151 nsp32_write4(base, SGT_ADR, new_sgtp);
2153 break;
2155 case SAVE_POINTERS:
2157 * These messages should not be occurred.
2158 * They should be processed on AutoSCSI sequencer.
2160 nsp32_msg (KERN_WARNING,
2161 "unexpected message of AutoSCSI MsgIn: SAVE_POINTERS");
2163 break;
2165 case MESSAGE_REJECT:
2166 /* If previous message_out is sending SDTR, and get
2167 message_reject from target, SDTR negotiation is failed */
2168 if (data->cur_target->sync_flag &
2169 (SDTR_INITIATOR | SDTR_TARGET)) {
2171 * Current target is negotiating SDTR, but it's
2172 * failed. Fall back to async transfer mode, and set
2173 * SDTR_DONE.
2175 nsp32_set_async(data, data->cur_target);
2176 data->cur_target->sync_flag &= ~SDTR_INITIATOR;
2177 data->cur_target->sync_flag |= SDTR_DONE;
2180 break;
2182 case LINKED_CMD_COMPLETE:
2183 case LINKED_FLG_CMD_COMPLETE:
2184 /* queue tag is not supported currently */
2185 nsp32_msg (KERN_WARNING,
2186 "unsupported message: 0x%x", msgtype);
2187 break;
2189 case INITIATE_RECOVERY:
2190 /* staring ECA (Extended Contingent Allegiance) state. */
2191 /* This message is declined in SPI2 or later. */
2193 goto reject;
2196 * 2-byte message
2198 case SIMPLE_QUEUE_TAG:
2199 case 0x23:
2201 * 0x23: Ignore_Wide_Residue is not declared in scsi.h.
2202 * No support is needed.
2204 if (data->msgin_len >= 1) {
2205 goto reject;
2208 /* current position is 1-byte of 2 byte */
2209 msgclear = FALSE;
2211 break;
2214 * extended message
2216 case EXTENDED_MESSAGE:
2217 if (data->msgin_len < 1) {
2219 * Current position does not reach 2-byte
2220 * (2-byte is extended message length).
2222 msgclear = FALSE;
2223 break;
2226 if ((data->msginbuf[1] + 1) > data->msgin_len) {
2228 * Current extended message has msginbuf[1] + 2
2229 * (msgin_len starts counting from 0, so buf[1] + 1).
2230 * If current message position is not finished,
2231 * continue receiving message.
2233 msgclear = FALSE;
2234 break;
2238 * Reach here means regular length of each type of
2239 * extended messages.
2241 switch (data->msginbuf[2]) {
2242 case EXTENDED_MODIFY_DATA_POINTER:
2243 /* TODO */
2244 goto reject; /* not implemented yet */
2245 break;
2247 case EXTENDED_SDTR:
2249 * Exchange this message between initiator and target.
2251 if (data->msgin_len != EXTENDED_SDTR_LEN + 1) {
2253 * received inappropriate message.
2255 goto reject;
2256 break;
2259 nsp32_analyze_sdtr(SCpnt);
2261 break;
2263 case EXTENDED_EXTENDED_IDENTIFY:
2264 /* SCSI-I only, not supported. */
2265 goto reject; /* not implemented yet */
2267 break;
2269 case EXTENDED_WDTR:
2270 goto reject; /* not implemented yet */
2272 break;
2274 default:
2275 goto reject;
2277 break;
2279 default:
2280 goto reject;
2283 restart:
2284 if (msgclear == TRUE) {
2285 data->msgin_len = 0;
2288 * If restarting AutoSCSI, but there are some message to out
2289 * (msgout_len > 0), set AutoATN, and set SCSIMSGOUT as 0
2290 * (MV_VALID = 0). When commandcontrol is written with
2291 * AutoSCSI restart, at the same time MsgOutOccur should be
2292 * happened (however, such situation is really possible...?).
2294 if (data->msgout_len > 0) {
2295 nsp32_write4(base, SCSI_MSG_OUT, 0);
2296 command |= AUTO_ATN;
2300 * restart AutoSCSI
2301 * If it's failed, COMMANDCONTROL_AUTO_COMMAND_PHASE is needed.
2303 command |= (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
2306 * If current msgin03 is TRUE, then flag on.
2308 if (data->cur_lunt->msgin03 == TRUE) {
2309 command |= AUTO_MSGIN_03;
2311 data->cur_lunt->msgin03 = FALSE;
2312 } else {
2313 data->msgin_len++;
2317 * restart AutoSCSI
2319 nsp32_restart_autoscsi(SCpnt, command);
2322 * wait SCSI REQ negate for REQ-ACK handshake
2324 nsp32_wait_req(data, NEGATE);
2327 * negate SCSI ACK
2329 nsp32_sack_negate(data);
2331 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
2333 return;
2335 reject:
2336 nsp32_msg(KERN_WARNING,
2337 "invalid or unsupported MessageIn, rejected. "
2338 "current msg: 0x%x (len: 0x%x), processing msg: 0x%x",
2339 msg, data->msgin_len, msgtype);
2340 nsp32_build_reject(SCpnt);
2341 data->msgin_len = 0;
2343 goto restart;
2349 static void nsp32_analyze_sdtr(struct scsi_cmnd *SCpnt)
2351 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2352 nsp32_target *target = data->cur_target;
2353 nsp32_sync_table *synct;
2354 unsigned char get_period = data->msginbuf[3];
2355 unsigned char get_offset = data->msginbuf[4];
2356 int entry;
2357 int syncnum;
2359 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "enter");
2361 synct = data->synct;
2362 syncnum = data->syncnum;
2365 * If this inititor sent the SDTR message, then target responds SDTR,
2366 * initiator SYNCREG, ACKWIDTH from SDTR parameter.
2367 * Messages are not appropriate, then send back reject message.
2368 * If initiator did not send the SDTR, but target sends SDTR,
2369 * initiator calculator the appropriate parameter and send back SDTR.
2371 if (target->sync_flag & SDTR_INITIATOR) {
2373 * Initiator sent SDTR, the target responds and
2374 * send back negotiation SDTR.
2376 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target responds SDTR");
2378 target->sync_flag &= ~SDTR_INITIATOR;
2379 target->sync_flag |= SDTR_DONE;
2382 * offset:
2384 if (get_offset > SYNC_OFFSET) {
2386 * Negotiation is failed, the target send back
2387 * unexpected offset value.
2389 goto reject;
2392 if (get_offset == ASYNC_OFFSET) {
2394 * Negotiation is succeeded, the target want
2395 * to fall back into asynchronous transfer mode.
2397 goto async;
2401 * period:
2402 * Check whether sync period is too short. If too short,
2403 * fall back to async mode. If it's ok, then investigate
2404 * the received sync period. If sync period is acceptable
2405 * between sync table start_period and end_period, then
2406 * set this I_T nexus as sent offset and period.
2407 * If it's not acceptable, send back reject and fall back
2408 * to async mode.
2410 if (get_period < data->synct[0].period_num) {
2412 * Negotiation is failed, the target send back
2413 * unexpected period value.
2415 goto reject;
2418 entry = nsp32_search_period_entry(data, target, get_period);
2420 if (entry < 0) {
2422 * Target want to use long period which is not
2423 * acceptable NinjaSCSI-32Bi/UDE.
2425 goto reject;
2429 * Set new sync table and offset in this I_T nexus.
2431 nsp32_set_sync_entry(data, target, entry, get_offset);
2432 } else {
2433 /* Target send SDTR to initiator. */
2434 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target send SDTR");
2436 target->sync_flag |= SDTR_INITIATOR;
2438 /* offset: */
2439 if (get_offset > SYNC_OFFSET) {
2440 /* send back as SYNC_OFFSET */
2441 get_offset = SYNC_OFFSET;
2444 /* period: */
2445 if (get_period < data->synct[0].period_num) {
2446 get_period = data->synct[0].period_num;
2449 entry = nsp32_search_period_entry(data, target, get_period);
2451 if (get_offset == ASYNC_OFFSET || entry < 0) {
2452 nsp32_set_async(data, target);
2453 nsp32_build_sdtr(SCpnt, 0, ASYNC_OFFSET);
2454 } else {
2455 nsp32_set_sync_entry(data, target, entry, get_offset);
2456 nsp32_build_sdtr(SCpnt, get_period, get_offset);
2460 target->period = get_period;
2461 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
2462 return;
2464 reject:
2466 * If the current message is unacceptable, send back to the target
2467 * with reject message.
2469 nsp32_build_reject(SCpnt);
2471 async:
2472 nsp32_set_async(data, target); /* set as ASYNC transfer mode */
2474 target->period = 0;
2475 nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit: set async");
2476 return;
2481 * Search config entry number matched in sync_table from given
2482 * target and speed period value. If failed to search, return negative value.
2484 static int nsp32_search_period_entry(nsp32_hw_data *data,
2485 nsp32_target *target,
2486 unsigned char period)
2488 int i;
2490 if (target->limit_entry >= data->syncnum) {
2491 nsp32_msg(KERN_ERR, "limit_entry exceeds syncnum!");
2492 target->limit_entry = 0;
2495 for (i = target->limit_entry; i < data->syncnum; i++) {
2496 if (period >= data->synct[i].start_period &&
2497 period <= data->synct[i].end_period) {
2498 break;
2503 * Check given period value is over the sync_table value.
2504 * If so, return max value.
2506 if (i == data->syncnum) {
2507 i = -1;
2510 return i;
2515 * target <-> initiator use ASYNC transfer
2517 static void nsp32_set_async(nsp32_hw_data *data, nsp32_target *target)
2519 unsigned char period = data->synct[target->limit_entry].period_num;
2521 target->offset = ASYNC_OFFSET;
2522 target->period = 0;
2523 target->syncreg = TO_SYNCREG(period, ASYNC_OFFSET);
2524 target->ackwidth = 0;
2525 target->sample_reg = 0;
2527 nsp32_dbg(NSP32_DEBUG_SYNC, "set async");
2532 * target <-> initiator use maximum SYNC transfer
2534 static void nsp32_set_max_sync(nsp32_hw_data *data,
2535 nsp32_target *target,
2536 unsigned char *period,
2537 unsigned char *offset)
2539 unsigned char period_num, ackwidth;
2541 period_num = data->synct[target->limit_entry].period_num;
2542 *period = data->synct[target->limit_entry].start_period;
2543 ackwidth = data->synct[target->limit_entry].ackwidth;
2544 *offset = SYNC_OFFSET;
2546 target->syncreg = TO_SYNCREG(period_num, *offset);
2547 target->ackwidth = ackwidth;
2548 target->offset = *offset;
2549 target->sample_reg = 0; /* disable SREQ sampling */
2554 * target <-> initiator use entry number speed
2556 static void nsp32_set_sync_entry(nsp32_hw_data *data,
2557 nsp32_target *target,
2558 int entry,
2559 unsigned char offset)
2561 unsigned char period, ackwidth, sample_rate;
2563 period = data->synct[entry].period_num;
2564 ackwidth = data->synct[entry].ackwidth;
2565 offset = offset;
2566 sample_rate = data->synct[entry].sample_rate;
2568 target->syncreg = TO_SYNCREG(period, offset);
2569 target->ackwidth = ackwidth;
2570 target->offset = offset;
2571 target->sample_reg = sample_rate | SAMPLING_ENABLE;
2573 nsp32_dbg(NSP32_DEBUG_SYNC, "set sync");
2578 * It waits until SCSI REQ becomes assertion or negation state.
2580 * Note: If nsp32_msgin_occur is called, we asserts SCSI ACK. Then
2581 * connected target responds SCSI REQ negation. We have to wait
2582 * SCSI REQ becomes negation in order to negate SCSI ACK signal for
2583 * REQ-ACK handshake.
2585 static void nsp32_wait_req(nsp32_hw_data *data, int state)
2587 unsigned int base = data->BaseAddress;
2588 int wait_time = 0;
2589 unsigned char bus, req_bit;
2591 if (!((state == ASSERT) || (state == NEGATE))) {
2592 nsp32_msg(KERN_ERR, "unknown state designation");
2594 /* REQ is BIT(5) */
2595 req_bit = (state == ASSERT ? BUSMON_REQ : 0);
2597 do {
2598 bus = nsp32_read1(base, SCSI_BUS_MONITOR);
2599 if ((bus & BUSMON_REQ) == req_bit) {
2600 nsp32_dbg(NSP32_DEBUG_WAIT,
2601 "wait_time: %d", wait_time);
2602 return;
2604 udelay(1);
2605 wait_time++;
2606 } while (wait_time < REQSACK_TIMEOUT_TIME);
2608 nsp32_msg(KERN_WARNING, "wait REQ timeout, req_bit: 0x%x", req_bit);
2612 * It waits until SCSI SACK becomes assertion or negation state.
2614 static void nsp32_wait_sack(nsp32_hw_data *data, int state)
2616 unsigned int base = data->BaseAddress;
2617 int wait_time = 0;
2618 unsigned char bus, ack_bit;
2620 if (!((state == ASSERT) || (state == NEGATE))) {
2621 nsp32_msg(KERN_ERR, "unknown state designation");
2623 /* ACK is BIT(4) */
2624 ack_bit = (state == ASSERT ? BUSMON_ACK : 0);
2626 do {
2627 bus = nsp32_read1(base, SCSI_BUS_MONITOR);
2628 if ((bus & BUSMON_ACK) == ack_bit) {
2629 nsp32_dbg(NSP32_DEBUG_WAIT,
2630 "wait_time: %d", wait_time);
2631 return;
2633 udelay(1);
2634 wait_time++;
2635 } while (wait_time < REQSACK_TIMEOUT_TIME);
2637 nsp32_msg(KERN_WARNING, "wait SACK timeout, ack_bit: 0x%x", ack_bit);
2641 * assert SCSI ACK
2643 * Note: SCSI ACK assertion needs with ACKENB=1, AUTODIRECTION=1.
2645 static void nsp32_sack_assert(nsp32_hw_data *data)
2647 unsigned int base = data->BaseAddress;
2648 unsigned char busctrl;
2650 busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
2651 busctrl |= (BUSCTL_ACK | AUTODIRECTION | ACKENB);
2652 nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
2656 * negate SCSI ACK
2658 static void nsp32_sack_negate(nsp32_hw_data *data)
2660 unsigned int base = data->BaseAddress;
2661 unsigned char busctrl;
2663 busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
2664 busctrl &= ~BUSCTL_ACK;
2665 nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
2671 * Note: n_io_port is defined as 0x7f because I/O register port is
2672 * assigned as:
2673 * 0x800-0x8ff: memory mapped I/O port
2674 * 0x900-0xbff: (map same 0x800-0x8ff I/O port image repeatedly)
2675 * 0xc00-0xfff: CardBus status registers
2677 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
2678 #define DETECT_OK 0
2679 #define DETECT_NG 1
2680 #define PCIDEV pdev
2681 static int nsp32_detect(struct pci_dev *pdev)
2682 #else
2683 #define DETECT_OK 1
2684 #define DETECT_NG 0
2685 #define PCIDEV (data->Pci)
2686 static int nsp32_detect(struct scsi_host_template *sht)
2687 #endif
2689 struct Scsi_Host *host; /* registered host structure */
2690 struct resource *res;
2691 nsp32_hw_data *data;
2692 int ret;
2693 int i, j;
2695 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
2698 * register this HBA as SCSI device
2700 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
2701 host = scsi_host_alloc(&nsp32_template, sizeof(nsp32_hw_data));
2702 #else
2703 host = scsi_register(sht, sizeof(nsp32_hw_data));
2704 #endif
2705 if (host == NULL) {
2706 nsp32_msg (KERN_ERR, "failed to scsi register");
2707 goto err;
2711 * set nsp32_hw_data
2713 data = (nsp32_hw_data *)host->hostdata;
2715 memcpy(data, &nsp32_data_base, sizeof(nsp32_hw_data));
2717 host->irq = data->IrqNumber;
2718 host->io_port = data->BaseAddress;
2719 host->unique_id = data->BaseAddress;
2720 host->n_io_port = data->NumAddress;
2721 host->base = (unsigned long)data->MmioAddress;
2722 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,63))
2723 scsi_set_pci_device(host, PCIDEV);
2724 #endif
2726 data->Host = host;
2727 spin_lock_init(&(data->Lock));
2729 data->cur_lunt = NULL;
2730 data->cur_target = NULL;
2733 * Bus master transfer mode is supported currently.
2735 data->trans_method = NSP32_TRANSFER_BUSMASTER;
2738 * Set clock div, CLOCK_4 (HBA has own external clock, and
2739 * dividing * 100ns/4).
2740 * Currently CLOCK_4 has only tested, not for CLOCK_2/PCICLK yet.
2742 data->clock = CLOCK_4;
2745 * Select appropriate nsp32_sync_table and set I_CLOCKDIV.
2747 switch (data->clock) {
2748 case CLOCK_4:
2749 /* If data->clock is CLOCK_4, then select 40M sync table. */
2750 data->synct = nsp32_sync_table_40M;
2751 data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
2752 break;
2753 case CLOCK_2:
2754 /* If data->clock is CLOCK_2, then select 20M sync table. */
2755 data->synct = nsp32_sync_table_20M;
2756 data->syncnum = ARRAY_SIZE(nsp32_sync_table_20M);
2757 break;
2758 case PCICLK:
2759 /* If data->clock is PCICLK, then select pci sync table. */
2760 data->synct = nsp32_sync_table_pci;
2761 data->syncnum = ARRAY_SIZE(nsp32_sync_table_pci);
2762 break;
2763 default:
2764 nsp32_msg(KERN_WARNING,
2765 "Invalid clock div is selected, set CLOCK_4.");
2766 /* Use default value CLOCK_4 */
2767 data->clock = CLOCK_4;
2768 data->synct = nsp32_sync_table_40M;
2769 data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
2773 * setup nsp32_lunt
2777 * setup DMA
2779 if (pci_set_dma_mask(PCIDEV, DMA_32BIT_MASK) != 0) {
2780 nsp32_msg (KERN_ERR, "failed to set PCI DMA mask");
2781 goto scsi_unregister;
2785 * allocate autoparam DMA resource.
2787 data->autoparam = pci_alloc_consistent(PCIDEV, sizeof(nsp32_autoparam), &(data->auto_paddr));
2788 if (data->autoparam == NULL) {
2789 nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
2790 goto scsi_unregister;
2794 * allocate scatter-gather DMA resource.
2796 data->sg_list = pci_alloc_consistent(PCIDEV, NSP32_SG_TABLE_SIZE,
2797 &(data->sg_paddr));
2798 if (data->sg_list == NULL) {
2799 nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
2800 goto free_autoparam;
2803 for (i = 0; i < ARRAY_SIZE(data->lunt); i++) {
2804 for (j = 0; j < ARRAY_SIZE(data->lunt[0]); j++) {
2805 int offset = i * ARRAY_SIZE(data->lunt[0]) + j;
2806 nsp32_lunt tmp = {
2807 .SCpnt = NULL,
2808 .save_datp = 0,
2809 .msgin03 = FALSE,
2810 .sg_num = 0,
2811 .cur_entry = 0,
2812 .sglun = &(data->sg_list[offset]),
2813 .sglun_paddr = data->sg_paddr + (offset * sizeof(nsp32_sglun)),
2816 data->lunt[i][j] = tmp;
2821 * setup target
2823 for (i = 0; i < ARRAY_SIZE(data->target); i++) {
2824 nsp32_target *target = &(data->target[i]);
2826 target->limit_entry = 0;
2827 target->sync_flag = 0;
2828 nsp32_set_async(data, target);
2832 * EEPROM check
2834 ret = nsp32_getprom_param(data);
2835 if (ret == FALSE) {
2836 data->resettime = 3; /* default 3 */
2840 * setup HBA
2842 nsp32hw_init(data);
2844 snprintf(data->info_str, sizeof(data->info_str),
2845 "NinjaSCSI-32Bi/UDE: irq %d, io 0x%lx+0x%x",
2846 host->irq, host->io_port, host->n_io_port);
2849 * SCSI bus reset
2851 * Note: It's important to reset SCSI bus in initialization phase.
2852 * NinjaSCSI-32Bi/UDE HBA EEPROM seems to exchange SDTR when
2853 * system is coming up, so SCSI devices connected to HBA is set as
2854 * un-asynchronous mode. It brings the merit that this HBA is
2855 * ready to start synchronous transfer without any preparation,
2856 * but we are difficult to control transfer speed. In addition,
2857 * it prevents device transfer speed from effecting EEPROM start-up
2858 * SDTR. NinjaSCSI-32Bi/UDE has the feature if EEPROM is set as
2859 * Auto Mode, then FAST-10M is selected when SCSI devices are
2860 * connected same or more than 4 devices. It should be avoided
2861 * depending on this specification. Thus, resetting the SCSI bus
2862 * restores all connected SCSI devices to asynchronous mode, then
2863 * this driver set SDTR safely later, and we can control all SCSI
2864 * device transfer mode.
2866 nsp32_do_bus_reset(data);
2868 ret = request_irq(host->irq, do_nsp32_isr, IRQF_SHARED, "nsp32", data);
2869 if (ret < 0) {
2870 nsp32_msg(KERN_ERR, "Unable to allocate IRQ for NinjaSCSI32 "
2871 "SCSI PCI controller. Interrupt: %d", host->irq);
2872 goto free_sg_list;
2876 * PCI IO register
2878 res = request_region(host->io_port, host->n_io_port, "nsp32");
2879 if (res == NULL) {
2880 nsp32_msg(KERN_ERR,
2881 "I/O region 0x%lx+0x%lx is already used",
2882 data->BaseAddress, data->NumAddress);
2883 goto free_irq;
2886 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
2887 ret = scsi_add_host(host, &PCIDEV->dev);
2888 if (ret) {
2889 nsp32_msg(KERN_ERR, "failed to add scsi host");
2890 goto free_region;
2892 scsi_scan_host(host);
2893 #endif
2894 pci_set_drvdata(PCIDEV, host);
2895 return DETECT_OK;
2897 free_region:
2898 release_region(host->io_port, host->n_io_port);
2900 free_irq:
2901 free_irq(host->irq, data);
2903 free_sg_list:
2904 pci_free_consistent(PCIDEV, NSP32_SG_TABLE_SIZE,
2905 data->sg_list, data->sg_paddr);
2907 free_autoparam:
2908 pci_free_consistent(PCIDEV, sizeof(nsp32_autoparam),
2909 data->autoparam, data->auto_paddr);
2911 scsi_unregister:
2912 scsi_host_put(host);
2914 err:
2915 return DETECT_NG;
2917 #undef DETECT_OK
2918 #undef DETECT_NG
2919 #undef PCIDEV
2921 static int nsp32_release(struct Scsi_Host *host)
2923 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
2925 if (data->autoparam) {
2926 pci_free_consistent(data->Pci, sizeof(nsp32_autoparam),
2927 data->autoparam, data->auto_paddr);
2930 if (data->sg_list) {
2931 pci_free_consistent(data->Pci, NSP32_SG_TABLE_SIZE,
2932 data->sg_list, data->sg_paddr);
2935 if (host->irq) {
2936 free_irq(host->irq, data);
2939 if (host->io_port && host->n_io_port) {
2940 release_region(host->io_port, host->n_io_port);
2943 if (data->MmioAddress) {
2944 iounmap(data->MmioAddress);
2947 return 0;
2950 static const char *nsp32_info(struct Scsi_Host *shpnt)
2952 nsp32_hw_data *data = (nsp32_hw_data *)shpnt->hostdata;
2954 return data->info_str;
2958 /****************************************************************************
2959 * error handler
2961 static int nsp32_eh_abort(struct scsi_cmnd *SCpnt)
2963 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2964 unsigned int base = SCpnt->device->host->io_port;
2966 nsp32_msg(KERN_WARNING, "abort");
2968 if (data->cur_lunt->SCpnt == NULL) {
2969 nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort failed");
2970 return FAILED;
2973 if (data->cur_target->sync_flag & (SDTR_INITIATOR | SDTR_TARGET)) {
2974 /* reset SDTR negotiation */
2975 data->cur_target->sync_flag = 0;
2976 nsp32_set_async(data, data->cur_target);
2979 nsp32_write2(base, TRANSFER_CONTROL, 0);
2980 nsp32_write2(base, BM_CNT, 0);
2982 SCpnt->result = DID_ABORT << 16;
2983 nsp32_scsi_done(SCpnt);
2985 nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort success");
2986 return SUCCESS;
2989 static int nsp32_eh_bus_reset(struct scsi_cmnd *SCpnt)
2991 nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
2992 unsigned int base = SCpnt->device->host->io_port;
2994 spin_lock_irq(SCpnt->device->host->host_lock);
2996 nsp32_msg(KERN_INFO, "Bus Reset");
2997 nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
2999 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
3000 nsp32_do_bus_reset(data);
3001 nsp32_write2(base, IRQ_CONTROL, 0);
3003 spin_unlock_irq(SCpnt->device->host->host_lock);
3004 return SUCCESS; /* SCSI bus reset is succeeded at any time. */
3007 static void nsp32_do_bus_reset(nsp32_hw_data *data)
3009 unsigned int base = data->BaseAddress;
3010 unsigned short intrdat;
3011 int i;
3013 nsp32_dbg(NSP32_DEBUG_BUSRESET, "in");
3016 * stop all transfer
3017 * clear TRANSFERCONTROL_BM_START
3018 * clear counter
3020 nsp32_write2(base, TRANSFER_CONTROL, 0);
3021 nsp32_write4(base, BM_CNT, 0);
3022 nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
3025 * fall back to asynchronous transfer mode
3026 * initialize SDTR negotiation flag
3028 for (i = 0; i < ARRAY_SIZE(data->target); i++) {
3029 nsp32_target *target = &data->target[i];
3031 target->sync_flag = 0;
3032 nsp32_set_async(data, target);
3036 * reset SCSI bus
3038 nsp32_write1(base, SCSI_BUS_CONTROL, BUSCTL_RST);
3039 udelay(RESET_HOLD_TIME);
3040 nsp32_write1(base, SCSI_BUS_CONTROL, 0);
3041 for(i = 0; i < 5; i++) {
3042 intrdat = nsp32_read2(base, IRQ_STATUS); /* dummy read */
3043 nsp32_dbg(NSP32_DEBUG_BUSRESET, "irq:1: 0x%x", intrdat);
3046 data->CurrentSC = NULL;
3049 static int nsp32_eh_host_reset(struct scsi_cmnd *SCpnt)
3051 struct Scsi_Host *host = SCpnt->device->host;
3052 unsigned int base = SCpnt->device->host->io_port;
3053 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
3055 nsp32_msg(KERN_INFO, "Host Reset");
3056 nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
3058 spin_lock_irq(SCpnt->device->host->host_lock);
3060 nsp32hw_init(data);
3061 nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
3062 nsp32_do_bus_reset(data);
3063 nsp32_write2(base, IRQ_CONTROL, 0);
3065 spin_unlock_irq(SCpnt->device->host->host_lock);
3066 return SUCCESS; /* Host reset is succeeded at any time. */
3070 /**************************************************************************
3071 * EEPROM handler
3075 * getting EEPROM parameter
3077 static int nsp32_getprom_param(nsp32_hw_data *data)
3079 int vendor = data->pci_devid->vendor;
3080 int device = data->pci_devid->device;
3081 int ret, val, i;
3084 * EEPROM checking.
3086 ret = nsp32_prom_read(data, 0x7e);
3087 if (ret != 0x55) {
3088 nsp32_msg(KERN_INFO, "No EEPROM detected: 0x%x", ret);
3089 return FALSE;
3091 ret = nsp32_prom_read(data, 0x7f);
3092 if (ret != 0xaa) {
3093 nsp32_msg(KERN_INFO, "Invalid number: 0x%x", ret);
3094 return FALSE;
3098 * check EEPROM type
3100 if (vendor == PCI_VENDOR_ID_WORKBIT &&
3101 device == PCI_DEVICE_ID_WORKBIT_STANDARD) {
3102 ret = nsp32_getprom_c16(data);
3103 } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
3104 device == PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC) {
3105 ret = nsp32_getprom_at24(data);
3106 } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
3107 device == PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO ) {
3108 ret = nsp32_getprom_at24(data);
3109 } else {
3110 nsp32_msg(KERN_WARNING, "Unknown EEPROM");
3111 ret = FALSE;
3114 /* for debug : SPROM data full checking */
3115 for (i = 0; i <= 0x1f; i++) {
3116 val = nsp32_prom_read(data, i);
3117 nsp32_dbg(NSP32_DEBUG_EEPROM,
3118 "rom address 0x%x : 0x%x", i, val);
3121 return ret;
3126 * AT24C01A (Logitec: LHA-600S), AT24C02 (Melco Buffalo: IFC-USLP) data map:
3128 * ROMADDR
3129 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3130 * Value 0x0: ASYNC, 0x0c: Ultra-20M, 0x19: Fast-10M
3131 * 0x07 : HBA Synchronous Transfer Period
3132 * Value 0: AutoSync, 1: Manual Setting
3133 * 0x08 - 0x0f : Not Used? (0x0)
3134 * 0x10 : Bus Termination
3135 * Value 0: Auto[ON], 1: ON, 2: OFF
3136 * 0x11 : Not Used? (0)
3137 * 0x12 : Bus Reset Delay Time (0x03)
3138 * 0x13 : Bootable CD Support
3139 * Value 0: Disable, 1: Enable
3140 * 0x14 : Device Scan
3141 * Bit 7 6 5 4 3 2 1 0
3142 * | <----------------->
3143 * | SCSI ID: Value 0: Skip, 1: YES
3144 * |-> Value 0: ALL scan, Value 1: Manual
3145 * 0x15 - 0x1b : Not Used? (0)
3146 * 0x1c : Constant? (0x01) (clock div?)
3147 * 0x1d - 0x7c : Not Used (0xff)
3148 * 0x7d : Not Used? (0xff)
3149 * 0x7e : Constant (0x55), Validity signature
3150 * 0x7f : Constant (0xaa), Validity signature
3152 static int nsp32_getprom_at24(nsp32_hw_data *data)
3154 int ret, i;
3155 int auto_sync;
3156 nsp32_target *target;
3157 int entry;
3160 * Reset time which is designated by EEPROM.
3162 * TODO: Not used yet.
3164 data->resettime = nsp32_prom_read(data, 0x12);
3167 * HBA Synchronous Transfer Period
3169 * Note: auto_sync = 0: auto, 1: manual. Ninja SCSI HBA spec says
3170 * that if auto_sync is 0 (auto), and connected SCSI devices are
3171 * same or lower than 3, then transfer speed is set as ULTRA-20M.
3172 * On the contrary if connected SCSI devices are same or higher
3173 * than 4, then transfer speed is set as FAST-10M.
3175 * I break this rule. The number of connected SCSI devices are
3176 * only ignored. If auto_sync is 0 (auto), then transfer speed is
3177 * forced as ULTRA-20M.
3179 ret = nsp32_prom_read(data, 0x07);
3180 switch (ret) {
3181 case 0:
3182 auto_sync = TRUE;
3183 break;
3184 case 1:
3185 auto_sync = FALSE;
3186 break;
3187 default:
3188 nsp32_msg(KERN_WARNING,
3189 "Unsupported Auto Sync mode. Fall back to manual mode.");
3190 auto_sync = TRUE;
3193 if (trans_mode == ULTRA20M_MODE) {
3194 auto_sync = TRUE;
3198 * each device Synchronous Transfer Period
3200 for (i = 0; i < NSP32_HOST_SCSIID; i++) {
3201 target = &data->target[i];
3202 if (auto_sync == TRUE) {
3203 target->limit_entry = 0; /* set as ULTRA20M */
3204 } else {
3205 ret = nsp32_prom_read(data, i);
3206 entry = nsp32_search_period_entry(data, target, ret);
3207 if (entry < 0) {
3208 /* search failed... set maximum speed */
3209 entry = 0;
3211 target->limit_entry = entry;
3215 return TRUE;
3220 * C16 110 (I-O Data: SC-NBD) data map:
3222 * ROMADDR
3223 * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
3224 * Value 0x0: 20MB/S, 0x1: 10MB/S, 0x2: 5MB/S, 0x3: ASYNC
3225 * 0x07 : 0 (HBA Synchronous Transfer Period: Auto Sync)
3226 * 0x08 - 0x0f : Not Used? (0x0)
3227 * 0x10 : Transfer Mode
3228 * Value 0: PIO, 1: Busmater
3229 * 0x11 : Bus Reset Delay Time (0x00-0x20)
3230 * 0x12 : Bus Termination
3231 * Value 0: Disable, 1: Enable
3232 * 0x13 - 0x19 : Disconnection
3233 * Value 0: Disable, 1: Enable
3234 * 0x1a - 0x7c : Not Used? (0)
3235 * 0x7d : Not Used? (0xf8)
3236 * 0x7e : Constant (0x55), Validity signature
3237 * 0x7f : Constant (0xaa), Validity signature
3239 static int nsp32_getprom_c16(nsp32_hw_data *data)
3241 int ret, i;
3242 nsp32_target *target;
3243 int entry, val;
3246 * Reset time which is designated by EEPROM.
3248 * TODO: Not used yet.
3250 data->resettime = nsp32_prom_read(data, 0x11);
3253 * each device Synchronous Transfer Period
3255 for (i = 0; i < NSP32_HOST_SCSIID; i++) {
3256 target = &data->target[i];
3257 ret = nsp32_prom_read(data, i);
3258 switch (ret) {
3259 case 0: /* 20MB/s */
3260 val = 0x0c;
3261 break;
3262 case 1: /* 10MB/s */
3263 val = 0x19;
3264 break;
3265 case 2: /* 5MB/s */
3266 val = 0x32;
3267 break;
3268 case 3: /* ASYNC */
3269 val = 0x00;
3270 break;
3271 default: /* default 20MB/s */
3272 val = 0x0c;
3273 break;
3275 entry = nsp32_search_period_entry(data, target, val);
3276 if (entry < 0 || trans_mode == ULTRA20M_MODE) {
3277 /* search failed... set maximum speed */
3278 entry = 0;
3280 target->limit_entry = entry;
3283 return TRUE;
3288 * Atmel AT24C01A (drived in 5V) serial EEPROM routines
3290 static int nsp32_prom_read(nsp32_hw_data *data, int romaddr)
3292 int i, val;
3294 /* start condition */
3295 nsp32_prom_start(data);
3297 /* device address */
3298 nsp32_prom_write_bit(data, 1); /* 1 */
3299 nsp32_prom_write_bit(data, 0); /* 0 */
3300 nsp32_prom_write_bit(data, 1); /* 1 */
3301 nsp32_prom_write_bit(data, 0); /* 0 */
3302 nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
3303 nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
3304 nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
3306 /* R/W: W for dummy write */
3307 nsp32_prom_write_bit(data, 0);
3309 /* ack */
3310 nsp32_prom_write_bit(data, 0);
3312 /* word address */
3313 for (i = 7; i >= 0; i--) {
3314 nsp32_prom_write_bit(data, ((romaddr >> i) & 1));
3317 /* ack */
3318 nsp32_prom_write_bit(data, 0);
3320 /* start condition */
3321 nsp32_prom_start(data);
3323 /* device address */
3324 nsp32_prom_write_bit(data, 1); /* 1 */
3325 nsp32_prom_write_bit(data, 0); /* 0 */
3326 nsp32_prom_write_bit(data, 1); /* 1 */
3327 nsp32_prom_write_bit(data, 0); /* 0 */
3328 nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
3329 nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
3330 nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
3332 /* R/W: R */
3333 nsp32_prom_write_bit(data, 1);
3335 /* ack */
3336 nsp32_prom_write_bit(data, 0);
3338 /* data... */
3339 val = 0;
3340 for (i = 7; i >= 0; i--) {
3341 val += (nsp32_prom_read_bit(data) << i);
3344 /* no ack */
3345 nsp32_prom_write_bit(data, 1);
3347 /* stop condition */
3348 nsp32_prom_stop(data);
3350 return val;
3353 static void nsp32_prom_set(nsp32_hw_data *data, int bit, int val)
3355 int base = data->BaseAddress;
3356 int tmp;
3358 tmp = nsp32_index_read1(base, SERIAL_ROM_CTL);
3360 if (val == 0) {
3361 tmp &= ~bit;
3362 } else {
3363 tmp |= bit;
3366 nsp32_index_write1(base, SERIAL_ROM_CTL, tmp);
3368 udelay(10);
3371 static int nsp32_prom_get(nsp32_hw_data *data, int bit)
3373 int base = data->BaseAddress;
3374 int tmp, ret;
3376 if (bit != SDA) {
3377 nsp32_msg(KERN_ERR, "return value is not appropriate");
3378 return 0;
3382 tmp = nsp32_index_read1(base, SERIAL_ROM_CTL) & bit;
3384 if (tmp == 0) {
3385 ret = 0;
3386 } else {
3387 ret = 1;
3390 udelay(10);
3392 return ret;
3395 static void nsp32_prom_start (nsp32_hw_data *data)
3397 /* start condition */
3398 nsp32_prom_set(data, SCL, 1);
3399 nsp32_prom_set(data, SDA, 1);
3400 nsp32_prom_set(data, ENA, 1); /* output mode */
3401 nsp32_prom_set(data, SDA, 0); /* keeping SCL=1 and transiting
3402 * SDA 1->0 is start condition */
3403 nsp32_prom_set(data, SCL, 0);
3406 static void nsp32_prom_stop (nsp32_hw_data *data)
3408 /* stop condition */
3409 nsp32_prom_set(data, SCL, 1);
3410 nsp32_prom_set(data, SDA, 0);
3411 nsp32_prom_set(data, ENA, 1); /* output mode */
3412 nsp32_prom_set(data, SDA, 1);
3413 nsp32_prom_set(data, SCL, 0);
3416 static void nsp32_prom_write_bit(nsp32_hw_data *data, int val)
3418 /* write */
3419 nsp32_prom_set(data, SDA, val);
3420 nsp32_prom_set(data, SCL, 1 );
3421 nsp32_prom_set(data, SCL, 0 );
3424 static int nsp32_prom_read_bit(nsp32_hw_data *data)
3426 int val;
3428 /* read */
3429 nsp32_prom_set(data, ENA, 0); /* input mode */
3430 nsp32_prom_set(data, SCL, 1);
3432 val = nsp32_prom_get(data, SDA);
3434 nsp32_prom_set(data, SCL, 0);
3435 nsp32_prom_set(data, ENA, 1); /* output mode */
3437 return val;
3441 /**************************************************************************
3442 * Power Management
3444 #ifdef CONFIG_PM
3446 /* Device suspended */
3447 static int nsp32_suspend(struct pci_dev *pdev, pm_message_t state)
3449 struct Scsi_Host *host = pci_get_drvdata(pdev);
3451 nsp32_msg(KERN_INFO, "pci-suspend: pdev=0x%p, state=%ld, slot=%s, host=0x%p", pdev, state, pci_name(pdev), host);
3453 pci_save_state (pdev);
3454 pci_disable_device (pdev);
3455 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3457 return 0;
3460 /* Device woken up */
3461 static int nsp32_resume(struct pci_dev *pdev)
3463 struct Scsi_Host *host = pci_get_drvdata(pdev);
3464 nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
3465 unsigned short reg;
3467 nsp32_msg(KERN_INFO, "pci-resume: pdev=0x%p, slot=%s, host=0x%p", pdev, pci_name(pdev), host);
3469 pci_set_power_state(pdev, PCI_D0);
3470 pci_enable_wake (pdev, PCI_D0, 0);
3471 pci_restore_state (pdev);
3473 reg = nsp32_read2(data->BaseAddress, INDEX_REG);
3475 nsp32_msg(KERN_INFO, "io=0x%x reg=0x%x", data->BaseAddress, reg);
3477 if (reg == 0xffff) {
3478 nsp32_msg(KERN_INFO, "missing device. abort resume.");
3479 return 0;
3482 nsp32hw_init (data);
3483 nsp32_do_bus_reset(data);
3485 nsp32_msg(KERN_INFO, "resume success");
3487 return 0;
3490 /* Enable wake event */
3491 static int nsp32_enable_wake(struct pci_dev *pdev, pci_power_t state, int enable)
3493 struct Scsi_Host *host = pci_get_drvdata(pdev);
3495 nsp32_msg(KERN_INFO, "pci-enable_wake: stub, pdev=0x%p, enable=%d, slot=%s, host=0x%p", pdev, enable, pci_name(pdev), host);
3497 return 0;
3499 #endif
3501 /************************************************************************
3502 * PCI/Cardbus probe/remove routine
3504 static int __devinit nsp32_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3506 int ret;
3507 nsp32_hw_data *data = &nsp32_data_base;
3509 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
3511 ret = pci_enable_device(pdev);
3512 if (ret) {
3513 nsp32_msg(KERN_ERR, "failed to enable pci device");
3514 return ret;
3517 data->Pci = pdev;
3518 data->pci_devid = id;
3519 data->IrqNumber = pdev->irq;
3520 data->BaseAddress = pci_resource_start(pdev, 0);
3521 data->NumAddress = pci_resource_len (pdev, 0);
3522 data->MmioAddress = ioremap_nocache(pci_resource_start(pdev, 1),
3523 pci_resource_len (pdev, 1));
3524 data->MmioLength = pci_resource_len (pdev, 1);
3526 pci_set_master(pdev);
3528 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
3529 ret = nsp32_detect(pdev);
3530 #else
3531 ret = scsi_register_host(&nsp32_template);
3532 #endif
3534 nsp32_msg(KERN_INFO, "irq: %i mmio: %p+0x%lx slot: %s model: %s",
3535 pdev->irq,
3536 data->MmioAddress, data->MmioLength,
3537 pci_name(pdev),
3538 nsp32_model[id->driver_data]);
3540 nsp32_dbg(NSP32_DEBUG_REGISTER, "exit %d", ret);
3542 return ret;
3545 static void __devexit nsp32_remove(struct pci_dev *pdev)
3547 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
3548 struct Scsi_Host *host = pci_get_drvdata(pdev);
3549 #endif
3551 nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
3553 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
3554 scsi_remove_host(host);
3556 nsp32_release(host);
3558 scsi_host_put(host);
3559 #else
3560 scsi_unregister_host(&nsp32_template);
3561 #endif
3566 static struct pci_driver nsp32_driver = {
3567 .name = "nsp32",
3568 .id_table = nsp32_pci_table,
3569 .probe = nsp32_probe,
3570 .remove = __devexit_p(nsp32_remove),
3571 #ifdef CONFIG_PM
3572 .suspend = nsp32_suspend,
3573 .resume = nsp32_resume,
3574 .enable_wake = nsp32_enable_wake,
3575 #endif
3578 /*********************************************************************
3579 * Moule entry point
3581 static int __init init_nsp32(void) {
3582 nsp32_msg(KERN_INFO, "loading...");
3583 return pci_register_driver(&nsp32_driver);
3586 static void __exit exit_nsp32(void) {
3587 nsp32_msg(KERN_INFO, "unloading...");
3588 pci_unregister_driver(&nsp32_driver);
3591 module_init(init_nsp32);
3592 module_exit(exit_nsp32);
3594 /* end */