1 /*======================================================================
3 NinjaSCSI-3 / NinjaSCSI-32Bi PCMCIA SCSI host adapter card driver
4 By: YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
6 Ver.2.8 Support 32bit MMIO mode
7 Support Synchronous Data Transfer Request (SDTR) mode
8 Ver.2.0 Support 32bit PIO mode
9 Ver.1.1.2 Fix for scatter list buffer exceeds
10 Ver.1.1 Support scatter list
11 Ver.0.1 Initial version
13 This software may be used and distributed according to the terms of
14 the GNU General Public License.
16 ======================================================================*/
18 /***********************************************************************
19 This driver is for these PCcards.
21 I-O DATA PCSC-F (Workbit NinjaSCSI-3)
22 "WBT", "NinjaSCSI-3", "R1.0"
23 I-O DATA CBSC-II (Workbit NinjaSCSI-32Bi in 16bit mode)
24 "IO DATA", "CBSC16 ", "1"
26 ***********************************************************************/
28 /* $Id: nsp_cs.c,v 1.23 2003/08/18 11:09:19 elca Exp $ */
30 #include <linux/version.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/timer.h>
38 #include <linux/ioport.h>
39 #include <linux/delay.h>
40 #include <linux/interrupt.h>
41 #include <linux/major.h>
42 #include <linux/blkdev.h>
43 #include <linux/stat.h>
48 #include <../drivers/scsi/scsi.h>
49 #include <scsi/scsi_host.h>
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_ioctl.h>
54 #include <pcmcia/cs_types.h>
55 #include <pcmcia/cs.h>
56 #include <pcmcia/cistpl.h>
57 #include <pcmcia/cisreg.h>
58 #include <pcmcia/ds.h>
62 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>");
63 MODULE_DESCRIPTION("WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module $Revision: 1.23 $");
64 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
66 MODULE_LICENSE("GPL");
71 /*====================================================================*/
72 /* Parameters that can be set with 'insmod' */
74 static int nsp_burst_mode = BURST_MEM32;
75 module_param(nsp_burst_mode, int, 0);
76 MODULE_PARM_DESC(nsp_burst_mode, "Burst transfer mode (0=io8, 1=io32, 2=mem32(default))");
78 /* Release IO ports after configuration? */
79 static int free_ports = 0;
80 module_param(free_ports, bool, 0);
81 MODULE_PARM_DESC(free_ports, "Release IO ports after configuration? (default: 0 (=no))");
83 static struct scsi_host_template nsp_driver_template = {
84 .proc_name = "nsp_cs",
85 .proc_info = nsp_proc_info,
86 .name = "WorkBit NinjaSCSI-3/32Bi(16bit)",
87 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
88 .detect = nsp_detect_old,
89 .release = nsp_release_old,
92 .queuecommand = nsp_queuecommand,
93 /* .eh_abort_handler = nsp_eh_abort,*/
94 .eh_bus_reset_handler = nsp_eh_bus_reset,
95 .eh_host_reset_handler = nsp_eh_host_reset,
97 .this_id = NSP_INITIATOR_ID,
98 .sg_tablesize = SG_ALL,
100 .use_clustering = DISABLE_CLUSTERING,
101 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,2))
102 .use_new_eh_code = 1,
106 static nsp_hw_data nsp_data_base; /* attach <-> detect glue */
114 # define NSP_DEBUG_MASK 0x000000
115 # define nsp_msg(type, args...) nsp_cs_message("", 0, (type), args)
116 # define nsp_dbg(mask, args...) /* */
118 # define NSP_DEBUG_MASK 0xffffff
119 # define nsp_msg(type, args...) \
120 nsp_cs_message (__FUNCTION__, __LINE__, (type), args)
121 # define nsp_dbg(mask, args...) \
122 nsp_cs_dmessage(__FUNCTION__, __LINE__, (mask), args)
125 #define NSP_DEBUG_QUEUECOMMAND BIT(0)
126 #define NSP_DEBUG_REGISTER BIT(1)
127 #define NSP_DEBUG_AUTOSCSI BIT(2)
128 #define NSP_DEBUG_INTR BIT(3)
129 #define NSP_DEBUG_SGLIST BIT(4)
130 #define NSP_DEBUG_BUSFREE BIT(5)
131 #define NSP_DEBUG_CDB_CONTENTS BIT(6)
132 #define NSP_DEBUG_RESELECTION BIT(7)
133 #define NSP_DEBUG_MSGINOCCUR BIT(8)
134 #define NSP_DEBUG_EEPROM BIT(9)
135 #define NSP_DEBUG_MSGOUTOCCUR BIT(10)
136 #define NSP_DEBUG_BUSRESET BIT(11)
137 #define NSP_DEBUG_RESTART BIT(12)
138 #define NSP_DEBUG_SYNC BIT(13)
139 #define NSP_DEBUG_WAIT BIT(14)
140 #define NSP_DEBUG_TARGETFLAG BIT(15)
141 #define NSP_DEBUG_PROC BIT(16)
142 #define NSP_DEBUG_INIT BIT(17)
143 #define NSP_DEBUG_DATA_IO BIT(18)
144 #define NSP_SPECIAL_PRINT_REGISTER BIT(20)
146 #define NSP_DEBUG_BUF_LEN 150
148 static void nsp_cs_message(const char *func, int line, char *type, char *fmt, ...)
151 char buf[NSP_DEBUG_BUF_LEN];
154 vsnprintf(buf, sizeof(buf), fmt, args);
158 printk("%snsp_cs: %s\n", type, buf);
160 printk("%snsp_cs: %s (%d): %s\n", type, func, line, buf);
165 static void nsp_cs_dmessage(const char *func, int line, int mask, char *fmt, ...)
168 char buf[NSP_DEBUG_BUF_LEN];
171 vsnprintf(buf, sizeof(buf), fmt, args);
174 if (mask & NSP_DEBUG_MASK) {
175 printk("nsp_cs-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
180 /***********************************************************/
182 /*====================================================
183 * Clenaup parameters and call done() functions.
184 * You must be set SCpnt->result before call this function.
186 static void nsp_scsi_done(Scsi_Cmnd *SCpnt)
188 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
190 data->CurrentSC = NULL;
192 SCpnt->scsi_done(SCpnt);
195 static int nsp_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
198 /*unsigned int host_id = SCpnt->device->host->this_id;*/
199 /*unsigned int base = SCpnt->device->host->io_port;*/
200 unsigned char target = scmd_id(SCpnt);
202 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
204 nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "SCpnt=0x%p target=%d lun=%d buff=0x%p bufflen=%d use_sg=%d",
205 SCpnt, target, SCpnt->device->lun, SCpnt->request_buffer, SCpnt->request_bufflen, SCpnt->use_sg);
206 //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "before CurrentSC=0x%p", data->CurrentSC);
208 SCpnt->scsi_done = done;
210 if (data->CurrentSC != NULL) {
211 nsp_msg(KERN_DEBUG, "CurrentSC!=NULL this can't be happen");
212 SCpnt->result = DID_BAD_TARGET << 16;
213 nsp_scsi_done(SCpnt);
218 /* XXX: pcmcia-cs generates SCSI command with "scsi_info" utility.
219 This makes kernel crash when suspending... */
220 if (data->ScsiInfo->stop != 0) {
221 nsp_msg(KERN_INFO, "suspending device. reject command.");
222 SCpnt->result = DID_BAD_TARGET << 16;
223 nsp_scsi_done(SCpnt);
224 return SCSI_MLQUEUE_HOST_BUSY;
230 data->CurrentSC = SCpnt;
232 SCpnt->SCp.Status = CHECK_CONDITION;
233 SCpnt->SCp.Message = 0;
234 SCpnt->SCp.have_data_in = IO_UNKNOWN;
235 SCpnt->SCp.sent_command = 0;
236 SCpnt->SCp.phase = PH_UNDETERMINED;
237 SCpnt->resid = SCpnt->request_bufflen;
239 /* setup scratch area
240 SCp.ptr : buffer pointer
241 SCp.this_residual : buffer length
242 SCp.buffer : next buffer
243 SCp.buffers_residual : left buffers in list
244 SCp.phase : current state of the command */
246 SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
247 SCpnt->SCp.ptr = BUFFER_ADDR;
248 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
249 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
251 SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
252 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
253 SCpnt->SCp.buffer = NULL;
254 SCpnt->SCp.buffers_residual = 0;
257 if (nsphw_start_selection(SCpnt) == FALSE) {
258 nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "selection fail");
259 SCpnt->result = DID_BUS_BUSY << 16;
260 nsp_scsi_done(SCpnt);
265 //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "out");
273 * setup PIO FIFO transfer mode and enable/disable to data out
275 static void nsp_setup_fifo(nsp_hw_data *data, int enabled)
277 unsigned int base = data->BaseAddress;
278 unsigned char transfer_mode_reg;
280 //nsp_dbg(NSP_DEBUG_DATA_IO, "enabled=%d", enabled);
282 if (enabled != FALSE) {
283 transfer_mode_reg = TRANSFER_GO | BRAIND;
285 transfer_mode_reg = 0;
288 transfer_mode_reg |= data->TransferMode;
290 nsp_index_write(base, TRANSFERMODE, transfer_mode_reg);
293 static void nsphw_init_sync(nsp_hw_data *data)
295 sync_data tmp_sync = { .SyncNegotiation = SYNC_NOT_YET,
301 /* setup sync data */
302 for ( i = 0; i < ARRAY_SIZE(data->Sync); i++ ) {
303 data->Sync[i] = tmp_sync;
308 * Initialize Ninja hardware
310 static int nsphw_init(nsp_hw_data *data)
312 unsigned int base = data->BaseAddress;
314 nsp_dbg(NSP_DEBUG_INIT, "in base=0x%x", base);
316 data->ScsiClockDiv = CLOCK_40M | FAST_20;
317 data->CurrentSC = NULL;
319 data->TransferMode = MODE_IO8;
321 nsphw_init_sync(data);
323 /* block all interrupts */
324 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
326 /* setup SCSI interface */
327 nsp_write(base, IFSELECT, IF_IFSEL);
329 nsp_index_write(base, SCSIIRQMODE, 0);
331 nsp_index_write(base, TRANSFERMODE, MODE_IO8);
332 nsp_index_write(base, CLOCKDIV, data->ScsiClockDiv);
334 nsp_index_write(base, PARITYCTRL, 0);
335 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
340 /* setup fifo asic */
341 nsp_write(base, IFSELECT, IF_REGSEL);
342 nsp_index_write(base, TERMPWRCTRL, 0);
343 if ((nsp_index_read(base, OTHERCONTROL) & TPWR_SENSE) == 0) {
344 nsp_msg(KERN_INFO, "terminator power on");
345 nsp_index_write(base, TERMPWRCTRL, POWER_ON);
348 nsp_index_write(base, TIMERCOUNT, 0);
349 nsp_index_write(base, TIMERCOUNT, 0); /* requires 2 times!! */
351 nsp_index_write(base, SYNCREG, 0);
352 nsp_index_write(base, ACKWIDTH, 0);
354 /* enable interrupts and ack them */
355 nsp_index_write(base, SCSIIRQMODE, SCSI_PHASE_CHANGE_EI |
358 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
360 nsp_setup_fifo(data, FALSE);
366 * Start selection phase
368 static int nsphw_start_selection(Scsi_Cmnd *SCpnt)
370 unsigned int host_id = SCpnt->device->host->this_id;
371 unsigned int base = SCpnt->device->host->io_port;
372 unsigned char target = scmd_id(SCpnt);
373 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
375 unsigned char phase, arbit;
377 //nsp_dbg(NSP_DEBUG_RESELECTION, "in");
379 phase = nsp_index_read(base, SCSIBUSMON);
380 if(phase != BUSMON_BUS_FREE) {
381 //nsp_dbg(NSP_DEBUG_RESELECTION, "bus busy");
385 /* start arbitration */
386 //nsp_dbg(NSP_DEBUG_RESELECTION, "start arbit");
387 SCpnt->SCp.phase = PH_ARBSTART;
388 nsp_index_write(base, SETARBIT, ARBIT_GO);
392 /* XXX: what a stupid chip! */
393 arbit = nsp_index_read(base, ARBITSTATUS);
394 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit=%d, wait_count=%d", arbit, wait_count);
395 udelay(1); /* hold 1.2us */
396 } while((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
399 if (!(arbit & ARBIT_WIN)) {
400 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit fail");
401 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
405 /* assert select line */
406 //nsp_dbg(NSP_DEBUG_RESELECTION, "assert SEL line");
407 SCpnt->SCp.phase = PH_SELSTART;
408 udelay(3); /* wait 2.4us */
409 nsp_index_write(base, SCSIDATALATCH, BIT(host_id) | BIT(target));
410 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_ATN);
411 udelay(2); /* wait >1.2us */
412 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_DATAOUT_ENB | SCSI_ATN);
413 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
414 /*udelay(1);*/ /* wait >90ns */
415 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_DATAOUT_ENB | SCSI_ATN);
417 /* check selection timeout */
418 nsp_start_timer(SCpnt, 1000/51);
419 data->SelectionTimeOut = 1;
424 struct nsp_sync_table {
425 unsigned int min_period;
426 unsigned int max_period;
427 unsigned int chip_period;
428 unsigned int ack_width;
431 static struct nsp_sync_table nsp_sync_table_40M[] = {
432 {0x0c, 0x0c, 0x1, 0}, /* 20MB 50ns*/
433 {0x19, 0x19, 0x3, 1}, /* 10MB 100ns*/
434 {0x1a, 0x25, 0x5, 2}, /* 7.5MB 150ns*/
435 {0x26, 0x32, 0x7, 3}, /* 5MB 200ns*/
439 static struct nsp_sync_table nsp_sync_table_20M[] = {
440 {0x19, 0x19, 0x1, 0}, /* 10MB 100ns*/
441 {0x1a, 0x25, 0x2, 0}, /* 7.5MB 150ns*/
442 {0x26, 0x32, 0x3, 1}, /* 5MB 200ns*/
447 * setup synchronous data transfer mode
449 static int nsp_analyze_sdtr(Scsi_Cmnd *SCpnt)
451 unsigned char target = scmd_id(SCpnt);
452 // unsigned char lun = SCpnt->device->lun;
453 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
454 sync_data *sync = &(data->Sync[target]);
455 struct nsp_sync_table *sync_table;
456 unsigned int period, offset;
460 nsp_dbg(NSP_DEBUG_SYNC, "in");
462 period = sync->SyncPeriod;
463 offset = sync->SyncOffset;
465 nsp_dbg(NSP_DEBUG_SYNC, "period=0x%x, offset=0x%x", period, offset);
467 if ((data->ScsiClockDiv & (BIT(0)|BIT(1))) == CLOCK_20M) {
468 sync_table = nsp_sync_table_20M;
470 sync_table = nsp_sync_table_40M;
473 for ( i = 0; sync_table->max_period != 0; i++, sync_table++) {
474 if ( period >= sync_table->min_period &&
475 period <= sync_table->max_period ) {
480 if (period != 0 && sync_table->max_period == 0) {
482 * No proper period/offset found
484 nsp_dbg(NSP_DEBUG_SYNC, "no proper period/offset");
486 sync->SyncPeriod = 0;
487 sync->SyncOffset = 0;
488 sync->SyncRegister = 0;
494 sync->SyncRegister = (sync_table->chip_period << SYNCREG_PERIOD_SHIFT) |
495 (offset & SYNCREG_OFFSET_MASK);
496 sync->AckWidth = sync_table->ack_width;
498 nsp_dbg(NSP_DEBUG_SYNC, "sync_reg=0x%x, ack_width=0x%x", sync->SyncRegister, sync->AckWidth);
505 * start ninja hardware timer
507 static void nsp_start_timer(Scsi_Cmnd *SCpnt, int time)
509 unsigned int base = SCpnt->device->host->io_port;
510 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
512 //nsp_dbg(NSP_DEBUG_INTR, "in SCpnt=0x%p, time=%d", SCpnt, time);
513 data->TimerCount = time;
514 nsp_index_write(base, TIMERCOUNT, time);
518 * wait for bus phase change
520 static int nsp_negate_signal(Scsi_Cmnd *SCpnt, unsigned char mask, char *str)
522 unsigned int base = SCpnt->device->host->io_port;
526 //nsp_dbg(NSP_DEBUG_INTR, "in");
531 reg = nsp_index_read(base, SCSIBUSMON);
535 } while ((time_out-- != 0) && (reg & mask) != 0);
538 nsp_msg(KERN_DEBUG, " %s signal off timeut", str);
547 static int nsp_expect_signal(Scsi_Cmnd *SCpnt,
548 unsigned char current_phase,
551 unsigned int base = SCpnt->device->host->io_port;
553 unsigned char phase, i_src;
555 //nsp_dbg(NSP_DEBUG_INTR, "current_phase=0x%x, mask=0x%x", current_phase, mask);
559 phase = nsp_index_read(base, SCSIBUSMON);
561 //nsp_dbg(NSP_DEBUG_INTR, "ret -1");
564 i_src = nsp_read(base, IRQSTATUS);
565 if (i_src & IRQSTATUS_SCSI) {
566 //nsp_dbg(NSP_DEBUG_INTR, "ret 0 found scsi signal");
569 if ((phase & mask) != 0 && (phase & BUSMON_PHASE_MASK) == current_phase) {
570 //nsp_dbg(NSP_DEBUG_INTR, "ret 1 phase=0x%x", phase);
573 } while(time_out-- != 0);
575 //nsp_dbg(NSP_DEBUG_INTR, "timeout");
580 * transfer SCSI message
582 static int nsp_xfer(Scsi_Cmnd *SCpnt, int phase)
584 unsigned int base = SCpnt->device->host->io_port;
585 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
586 char *buf = data->MsgBuffer;
587 int len = min(MSGBUF_SIZE, data->MsgLen);
591 //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
592 for (ptr = 0; len > 0; len--, ptr++) {
594 ret = nsp_expect_signal(SCpnt, phase, BUSMON_REQ);
596 nsp_dbg(NSP_DEBUG_DATA_IO, "xfer quit");
600 /* if last byte, negate ATN */
601 if (len == 1 && SCpnt->SCp.phase == PH_MSG_OUT) {
602 nsp_index_write(base, SCSIBUSCTRL, AUTODIRECTION | ACKENB);
605 /* read & write message */
606 if (phase & BUSMON_IO) {
607 nsp_dbg(NSP_DEBUG_DATA_IO, "read msg");
608 buf[ptr] = nsp_index_read(base, SCSIDATAWITHACK);
610 nsp_dbg(NSP_DEBUG_DATA_IO, "write msg");
611 nsp_index_write(base, SCSIDATAWITHACK, buf[ptr]);
613 nsp_negate_signal(SCpnt, BUSMON_ACK, "xfer<ack>");
620 * get extra SCSI data from fifo
622 static int nsp_dataphase_bypass(Scsi_Cmnd *SCpnt)
624 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
627 //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
629 if (SCpnt->SCp.have_data_in != IO_IN) {
633 count = nsp_fifo_count(SCpnt);
634 if (data->FifoCount == count) {
635 //nsp_dbg(NSP_DEBUG_DATA_IO, "not use bypass quirk");
641 * data phase skip only occures in case of SCSI_LOW_READ
643 nsp_dbg(NSP_DEBUG_DATA_IO, "use bypass quirk");
644 SCpnt->SCp.phase = PH_DATA;
646 nsp_setup_fifo(data, FALSE);
654 static int nsp_reselected(Scsi_Cmnd *SCpnt)
656 unsigned int base = SCpnt->device->host->io_port;
657 unsigned int host_id = SCpnt->device->host->this_id;
658 //nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
659 unsigned char bus_reg;
660 unsigned char id_reg, tmp;
663 nsp_dbg(NSP_DEBUG_RESELECTION, "in");
665 id_reg = nsp_index_read(base, RESELECTID);
666 tmp = id_reg & (~BIT(host_id));
676 if (scmd_id(SCpnt) != target) {
677 nsp_msg(KERN_ERR, "XXX: reselect ID must be %d in this implementation.", target);
680 nsp_negate_signal(SCpnt, BUSMON_SEL, "reselect<SEL>");
683 bus_reg = nsp_index_read(base, SCSIBUSCTRL) & ~(SCSI_BSY | SCSI_ATN);
684 nsp_index_write(base, SCSIBUSCTRL, bus_reg);
685 nsp_index_write(base, SCSIBUSCTRL, bus_reg | AUTODIRECTION | ACKENB);
691 * count how many data transferd
693 static int nsp_fifo_count(Scsi_Cmnd *SCpnt)
695 unsigned int base = SCpnt->device->host->io_port;
697 unsigned int l, m, h, dummy;
699 nsp_index_write(base, POINTERCLR, POINTER_CLEAR | ACK_COUNTER);
701 l = nsp_index_read(base, TRANSFERCOUNT);
702 m = nsp_index_read(base, TRANSFERCOUNT);
703 h = nsp_index_read(base, TRANSFERCOUNT);
704 dummy = nsp_index_read(base, TRANSFERCOUNT); /* required this! */
706 count = (h << 16) | (m << 8) | (l << 0);
708 //nsp_dbg(NSP_DEBUG_DATA_IO, "count=0x%x", count);
714 #define RFIFO_CRIT 64
715 #define WFIFO_CRIT 64
718 * read data in DATA IN phase
720 static void nsp_pio_read(Scsi_Cmnd *SCpnt)
722 unsigned int base = SCpnt->device->host->io_port;
723 unsigned long mmio_base = SCpnt->device->host->base;
724 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
727 unsigned char stat, fifo_stat;
729 ocount = data->FifoCount;
731 nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p resid=%d ocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d",
732 SCpnt, SCpnt->resid, ocount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual);
736 while ((time_out-- != 0) &&
737 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0 ) ) {
739 stat = nsp_index_read(base, SCSIBUSMON);
740 stat &= BUSMON_PHASE_MASK;
743 res = nsp_fifo_count(SCpnt) - ocount;
744 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x ocount=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount, res);
745 if (res == 0) { /* if some data avilable ? */
746 if (stat == BUSPHASE_DATA_IN) { /* phase changed? */
747 //nsp_dbg(NSP_DEBUG_DATA_IO, " wait for data this=%d", SCpnt->SCp.this_residual);
750 nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x", stat);
755 fifo_stat = nsp_read(base, FIFOSTATUS);
756 if ((fifo_stat & FIFOSTATUS_FULL_EMPTY) == 0 &&
757 stat == BUSPHASE_DATA_IN) {
761 res = min(res, SCpnt->SCp.this_residual);
763 switch (data->TransferMode) {
765 res &= ~(BIT(1)|BIT(0)); /* align 4 */
766 nsp_fifo32_read(base, SCpnt->SCp.ptr, res >> 2);
769 nsp_fifo8_read (base, SCpnt->SCp.ptr, res );
773 res &= ~(BIT(1)|BIT(0)); /* align 4 */
774 nsp_mmio_fifo32_read(mmio_base, SCpnt->SCp.ptr, res >> 2);
778 nsp_dbg(NSP_DEBUG_DATA_IO, "unknown read mode");
783 SCpnt->SCp.ptr += res;
784 SCpnt->SCp.this_residual -= res;
786 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this_residual=0x%x ocount=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount);
788 /* go to next scatter list if available */
789 if (SCpnt->SCp.this_residual == 0 &&
790 SCpnt->SCp.buffers_residual != 0 ) {
791 //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next timeout=%d", time_out);
792 SCpnt->SCp.buffers_residual--;
794 SCpnt->SCp.ptr = BUFFER_ADDR;
795 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
798 //nsp_dbg(NSP_DEBUG_DATA_IO, "page: 0x%p, off: 0x%x", SCpnt->SCp.buffer->page, SCpnt->SCp.buffer->offset);
802 data->FifoCount = ocount;
805 nsp_msg(KERN_DEBUG, "pio read timeout resid=%d this_residual=%d buffers_residual=%d",
806 SCpnt->resid, SCpnt->SCp.this_residual, SCpnt->SCp.buffers_residual);
808 nsp_dbg(NSP_DEBUG_DATA_IO, "read ocount=0x%x", ocount);
809 nsp_dbg(NSP_DEBUG_DATA_IO, "r cmd=%d resid=0x%x\n", data->CmdId, SCpnt->resid);
813 * write data in DATA OUT phase
815 static void nsp_pio_write(Scsi_Cmnd *SCpnt)
817 unsigned int base = SCpnt->device->host->io_port;
818 unsigned long mmio_base = SCpnt->device->host->base;
819 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
824 ocount = data->FifoCount;
826 nsp_dbg(NSP_DEBUG_DATA_IO, "in fifocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d resid=0x%x",
827 data->FifoCount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual, SCpnt->resid);
831 while ((time_out-- != 0) &&
832 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0)) {
833 stat = nsp_index_read(base, SCSIBUSMON);
834 stat &= BUSMON_PHASE_MASK;
836 if (stat != BUSPHASE_DATA_OUT) {
837 res = ocount - nsp_fifo_count(SCpnt);
839 nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x, res=%d\n", stat, res);
840 /* Put back pointer */
842 SCpnt->SCp.ptr -= res;
843 SCpnt->SCp.this_residual += res;
849 res = ocount - nsp_fifo_count(SCpnt);
850 if (res > 0) { /* write all data? */
851 nsp_dbg(NSP_DEBUG_DATA_IO, "wait for all data out. ocount=0x%x res=%d", ocount, res);
855 res = min(SCpnt->SCp.this_residual, WFIFO_CRIT);
857 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, res);
858 switch (data->TransferMode) {
860 res &= ~(BIT(1)|BIT(0)); /* align 4 */
861 nsp_fifo32_write(base, SCpnt->SCp.ptr, res >> 2);
864 nsp_fifo8_write (base, SCpnt->SCp.ptr, res );
868 res &= ~(BIT(1)|BIT(0)); /* align 4 */
869 nsp_mmio_fifo32_write(mmio_base, SCpnt->SCp.ptr, res >> 2);
873 nsp_dbg(NSP_DEBUG_DATA_IO, "unknown write mode");
878 SCpnt->SCp.ptr += res;
879 SCpnt->SCp.this_residual -= res;
882 /* go to next scatter list if available */
883 if (SCpnt->SCp.this_residual == 0 &&
884 SCpnt->SCp.buffers_residual != 0 ) {
885 //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next");
886 SCpnt->SCp.buffers_residual--;
888 SCpnt->SCp.ptr = BUFFER_ADDR;
889 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
894 data->FifoCount = ocount;
897 nsp_msg(KERN_DEBUG, "pio write timeout resid=0x%x", SCpnt->resid);
899 nsp_dbg(NSP_DEBUG_DATA_IO, "write ocount=0x%x", ocount);
900 nsp_dbg(NSP_DEBUG_DATA_IO, "w cmd=%d resid=0x%x\n", data->CmdId, SCpnt->resid);
906 * setup synchronous/asynchronous data transfer mode
908 static int nsp_nexus(Scsi_Cmnd *SCpnt)
910 unsigned int base = SCpnt->device->host->io_port;
911 unsigned char target = scmd_id(SCpnt);
912 // unsigned char lun = SCpnt->device->lun;
913 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
914 sync_data *sync = &(data->Sync[target]);
916 //nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p", SCpnt);
918 /* setup synch transfer registers */
919 nsp_index_write(base, SYNCREG, sync->SyncRegister);
920 nsp_index_write(base, ACKWIDTH, sync->AckWidth);
922 if (SCpnt->use_sg == 0 ||
923 SCpnt->resid % 4 != 0 ||
924 SCpnt->resid <= PAGE_SIZE ) {
925 data->TransferMode = MODE_IO8;
926 } else if (nsp_burst_mode == BURST_MEM32) {
927 data->TransferMode = MODE_MEM32;
928 } else if (nsp_burst_mode == BURST_IO32) {
929 data->TransferMode = MODE_IO32;
931 data->TransferMode = MODE_IO8;
934 /* setup pdma fifo */
935 nsp_setup_fifo(data, TRUE);
937 /* clear ack counter */
939 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
947 #include "nsp_message.c"
951 static irqreturn_t nspintr(int irq, void *dev_id)
954 unsigned char irq_status, irq_phase, phase;
956 unsigned char target, lun;
957 unsigned int *sync_neg;
962 //nsp_dbg(NSP_DEBUG_INTR, "dev_id=0x%p", dev_id);
963 //nsp_dbg(NSP_DEBUG_INTR, "host=0x%p", ((scsi_info_t *)dev_id)->host);
965 if ( dev_id != NULL &&
966 ((scsi_info_t *)dev_id)->host != NULL ) {
967 scsi_info_t *info = (scsi_info_t *)dev_id;
969 data = (nsp_hw_data *)info->host->hostdata;
971 nsp_dbg(NSP_DEBUG_INTR, "host data wrong");
975 //nsp_dbg(NSP_DEBUG_INTR, "&nsp_data_base=0x%p, dev_id=0x%p", &nsp_data_base, dev_id);
977 base = data->BaseAddress;
978 //nsp_dbg(NSP_DEBUG_INTR, "base=0x%x", base);
983 nsp_write(base, IRQCONTROL, IRQCONTROL_IRQDISABLE);
984 irq_status = nsp_read(base, IRQSTATUS);
985 //nsp_dbg(NSP_DEBUG_INTR, "irq_status=0x%x", irq_status);
986 if ((irq_status == 0xff) || ((irq_status & IRQSTATUS_MASK) == 0)) {
987 nsp_write(base, IRQCONTROL, 0);
988 //nsp_dbg(NSP_DEBUG_INTR, "no irq/shared irq");
993 * Do not read an irq_phase register if no scsi phase interrupt.
994 * Unless, you should lose a scsi phase interrupt.
996 phase = nsp_index_read(base, SCSIBUSMON);
997 if((irq_status & IRQSTATUS_SCSI) != 0) {
998 irq_phase = nsp_index_read(base, IRQPHASESENCE);
1003 //nsp_dbg(NSP_DEBUG_INTR, "irq_phase=0x%x", irq_phase);
1006 * timer interrupt handler (scsi vs timer interrupts)
1008 //nsp_dbg(NSP_DEBUG_INTR, "timercount=%d", data->TimerCount);
1009 if (data->TimerCount != 0) {
1010 //nsp_dbg(NSP_DEBUG_INTR, "stop timer");
1011 nsp_index_write(base, TIMERCOUNT, 0);
1012 nsp_index_write(base, TIMERCOUNT, 0);
1013 data->TimerCount = 0;
1016 if ((irq_status & IRQSTATUS_MASK) == IRQSTATUS_TIMER &&
1017 data->SelectionTimeOut == 0) {
1018 //nsp_dbg(NSP_DEBUG_INTR, "timer start");
1019 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR);
1023 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR | IRQCONTROL_FIFO_CLEAR);
1025 if ((irq_status & IRQSTATUS_SCSI) &&
1026 (irq_phase & SCSI_RESET_IRQ)) {
1027 nsp_msg(KERN_ERR, "bus reset (power off?)");
1030 nsp_bus_reset(data);
1032 if(data->CurrentSC != NULL) {
1033 tmpSC = data->CurrentSC;
1034 tmpSC->result = (DID_RESET << 16) |
1035 ((tmpSC->SCp.Message & 0xff) << 8) |
1036 ((tmpSC->SCp.Status & 0xff) << 0);
1037 nsp_scsi_done(tmpSC);
1042 if (data->CurrentSC == NULL) {
1043 nsp_msg(KERN_ERR, "CurrentSC==NULL irq_status=0x%x phase=0x%x irq_phase=0x%x this can't be happen. reset everything", irq_status, phase, irq_phase);
1045 nsp_bus_reset(data);
1049 tmpSC = data->CurrentSC;
1050 target = tmpSC->device->id;
1051 lun = tmpSC->device->lun;
1052 sync_neg = &(data->Sync[target].SyncNegotiation);
1055 * parse hardware SCSI irq reasons register
1057 if (irq_status & IRQSTATUS_SCSI) {
1058 if (irq_phase & RESELECT_IRQ) {
1059 nsp_dbg(NSP_DEBUG_INTR, "reselect");
1060 nsp_write(base, IRQCONTROL, IRQCONTROL_RESELECT_CLEAR);
1061 if (nsp_reselected(tmpSC) != FALSE) {
1066 if ((irq_phase & (PHASE_CHANGE_IRQ | LATCHED_BUS_FREE)) == 0) {
1071 //show_phase(tmpSC);
1073 switch(tmpSC->SCp.phase) {
1075 // *sync_neg = SYNC_NOT_YET;
1076 if ((phase & BUSMON_BSY) == 0) {
1077 //nsp_dbg(NSP_DEBUG_INTR, "selection count=%d", data->SelectionTimeOut);
1078 if (data->SelectionTimeOut >= NSP_SELTIMEOUT) {
1079 nsp_dbg(NSP_DEBUG_INTR, "selection time out");
1080 data->SelectionTimeOut = 0;
1081 nsp_index_write(base, SCSIBUSCTRL, 0);
1083 tmpSC->result = DID_TIME_OUT << 16;
1084 nsp_scsi_done(tmpSC);
1088 data->SelectionTimeOut += 1;
1089 nsp_start_timer(tmpSC, 1000/51);
1093 /* attention assert */
1094 //nsp_dbg(NSP_DEBUG_INTR, "attention assert");
1095 data->SelectionTimeOut = 0;
1096 tmpSC->SCp.phase = PH_SELECTED;
1097 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN);
1099 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN | AUTODIRECTION | ACKENB);
1105 //nsp_dbg(NSP_DEBUG_INTR, "phase reselect");
1106 // *sync_neg = SYNC_NOT_YET;
1107 if ((phase & BUSMON_PHASE_MASK) != BUSPHASE_MESSAGE_IN) {
1109 tmpSC->result = DID_ABORT << 16;
1110 nsp_scsi_done(tmpSC);
1115 if ((irq_status & (IRQSTATUS_SCSI | IRQSTATUS_FIFO)) == 0) {
1124 //nsp_dbg(NSP_DEBUG_INTR, "start scsi seq");
1126 /* normal disconnect */
1127 if (((tmpSC->SCp.phase == PH_MSG_IN) || (tmpSC->SCp.phase == PH_MSG_OUT)) &&
1128 (irq_phase & LATCHED_BUS_FREE) != 0 ) {
1129 nsp_dbg(NSP_DEBUG_INTR, "normal disconnect irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1131 //*sync_neg = SYNC_NOT_YET;
1133 if ((tmpSC->SCp.Message == MSG_COMMAND_COMPLETE)) { /* all command complete and return status */
1134 tmpSC->result = (DID_OK << 16) |
1135 ((tmpSC->SCp.Message & 0xff) << 8) |
1136 ((tmpSC->SCp.Status & 0xff) << 0);
1137 nsp_dbg(NSP_DEBUG_INTR, "command complete result=0x%x", tmpSC->result);
1138 nsp_scsi_done(tmpSC);
1147 /* check unexpected bus free state */
1149 nsp_msg(KERN_DEBUG, "unexpected bus free. irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1151 *sync_neg = SYNC_NG;
1152 tmpSC->result = DID_ERROR << 16;
1153 nsp_scsi_done(tmpSC);
1157 switch (phase & BUSMON_PHASE_MASK) {
1158 case BUSPHASE_COMMAND:
1159 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_COMMAND");
1160 if ((phase & BUSMON_REQ) == 0) {
1161 nsp_dbg(NSP_DEBUG_INTR, "REQ == 0");
1165 tmpSC->SCp.phase = PH_COMMAND;
1169 /* write scsi command */
1170 nsp_dbg(NSP_DEBUG_INTR, "cmd_len=%d", tmpSC->cmd_len);
1171 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER);
1172 for (i = 0; i < tmpSC->cmd_len; i++) {
1173 nsp_index_write(base, COMMANDDATA, tmpSC->cmnd[i]);
1175 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER | AUTO_COMMAND_GO);
1178 case BUSPHASE_DATA_OUT:
1179 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_OUT");
1181 tmpSC->SCp.phase = PH_DATA;
1182 tmpSC->SCp.have_data_in = IO_OUT;
1184 nsp_pio_write(tmpSC);
1188 case BUSPHASE_DATA_IN:
1189 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_IN");
1191 tmpSC->SCp.phase = PH_DATA;
1192 tmpSC->SCp.have_data_in = IO_IN;
1194 nsp_pio_read(tmpSC);
1198 case BUSPHASE_STATUS:
1199 nsp_dataphase_bypass(tmpSC);
1200 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_STATUS");
1202 tmpSC->SCp.phase = PH_STATUS;
1204 tmpSC->SCp.Status = nsp_index_read(base, SCSIDATAWITHACK);
1205 nsp_dbg(NSP_DEBUG_INTR, "message=0x%x status=0x%x", tmpSC->SCp.Message, tmpSC->SCp.Status);
1209 case BUSPHASE_MESSAGE_OUT:
1210 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_OUT");
1211 if ((phase & BUSMON_REQ) == 0) {
1215 tmpSC->SCp.phase = PH_MSG_OUT;
1217 //*sync_neg = SYNC_NOT_YET;
1219 data->MsgLen = i = 0;
1220 data->MsgBuffer[i] = IDENTIFY(TRUE, lun); i++;
1222 if (*sync_neg == SYNC_NOT_YET) {
1223 data->Sync[target].SyncPeriod = 0;
1224 data->Sync[target].SyncOffset = 0;
1227 data->MsgBuffer[i] = MSG_EXTENDED; i++;
1228 data->MsgBuffer[i] = 3; i++;
1229 data->MsgBuffer[i] = MSG_EXT_SDTR; i++;
1230 data->MsgBuffer[i] = 0x0c; i++;
1231 data->MsgBuffer[i] = 15; i++;
1236 nsp_analyze_sdtr(tmpSC);
1238 nsp_message_out(tmpSC);
1241 case BUSPHASE_MESSAGE_IN:
1242 nsp_dataphase_bypass(tmpSC);
1243 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_IN");
1244 if ((phase & BUSMON_REQ) == 0) {
1248 tmpSC->SCp.phase = PH_MSG_IN;
1249 nsp_message_in(tmpSC);
1252 if (*sync_neg == SYNC_NOT_YET) {
1253 //nsp_dbg(NSP_DEBUG_INTR, "sync target=%d,lun=%d",target,lun);
1255 if (data->MsgLen >= 5 &&
1256 data->MsgBuffer[0] == MSG_EXTENDED &&
1257 data->MsgBuffer[1] == 3 &&
1258 data->MsgBuffer[2] == MSG_EXT_SDTR ) {
1259 data->Sync[target].SyncPeriod = data->MsgBuffer[3];
1260 data->Sync[target].SyncOffset = data->MsgBuffer[4];
1261 //nsp_dbg(NSP_DEBUG_INTR, "sync ok, %d %d", data->MsgBuffer[3], data->MsgBuffer[4]);
1262 *sync_neg = SYNC_OK;
1264 data->Sync[target].SyncPeriod = 0;
1265 data->Sync[target].SyncOffset = 0;
1266 *sync_neg = SYNC_NG;
1268 nsp_analyze_sdtr(tmpSC);
1272 /* search last messeage byte */
1274 for (i = 0; i < data->MsgLen; i++) {
1275 tmp = data->MsgBuffer[i];
1276 if (data->MsgBuffer[i] == MSG_EXTENDED) {
1277 i += (1 + data->MsgBuffer[i+1]);
1280 tmpSC->SCp.Message = tmp;
1282 nsp_dbg(NSP_DEBUG_INTR, "message=0x%x len=%d", tmpSC->SCp.Message, data->MsgLen);
1287 case BUSPHASE_SELECT:
1289 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE other");
1294 //nsp_dbg(NSP_DEBUG_INTR, "out");
1298 nsp_start_timer(tmpSC, 1000/102);
1303 #include "nsp_debug.c"
1304 #endif /* NSP_DEBUG */
1306 /*----------------------------------------------------------------*/
1307 /* look for ninja3 card and init if found */
1308 /*----------------------------------------------------------------*/
1309 static struct Scsi_Host *nsp_detect(struct scsi_host_template *sht)
1311 struct Scsi_Host *host; /* registered host structure */
1312 nsp_hw_data *data_b = &nsp_data_base, *data;
1314 nsp_dbg(NSP_DEBUG_INIT, "this_id=%d", sht->this_id);
1315 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1316 host = scsi_host_alloc(&nsp_driver_template, sizeof(nsp_hw_data));
1318 host = scsi_register(sht, sizeof(nsp_hw_data));
1321 nsp_dbg(NSP_DEBUG_INIT, "host failed");
1325 memcpy(host->hostdata, data_b, sizeof(nsp_hw_data));
1326 data = (nsp_hw_data *)host->hostdata;
1327 data->ScsiInfo->host = host;
1332 nsp_dbg(NSP_DEBUG_INIT, "irq=%d,%d", data_b->IrqNumber, ((nsp_hw_data *)host->hostdata)->IrqNumber);
1334 host->unique_id = data->BaseAddress;
1335 host->io_port = data->BaseAddress;
1336 host->n_io_port = data->NumAddress;
1337 host->irq = data->IrqNumber;
1338 host->base = data->MmioAddress;
1340 spin_lock_init(&(data->Lock));
1342 snprintf(data->nspinfo,
1343 sizeof(data->nspinfo),
1344 "NinjaSCSI-3/32Bi Driver $Revision: 1.23 $ IO:0x%04lx-0x%04lx MMIO(virt addr):0x%04lx IRQ:%02d",
1345 host->io_port, host->io_port + host->n_io_port - 1,
1348 sht->name = data->nspinfo;
1350 nsp_dbg(NSP_DEBUG_INIT, "end");
1353 return host; /* detect done. */
1356 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1357 static int nsp_detect_old(struct scsi_host_template *sht)
1359 if (nsp_detect(sht) == NULL) {
1362 //MOD_INC_USE_COUNT;
1368 static int nsp_release_old(struct Scsi_Host *shpnt)
1370 //nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
1372 /* PCMCIA Card Service dose same things below. */
1373 /* So we do nothing. */
1375 // free_irq(shpnt->irq, data->ScsiInfo);
1377 //if (shpnt->io_port) {
1378 // release_region(shpnt->io_port, shpnt->n_io_port);
1381 //MOD_DEC_USE_COUNT;
1387 /*----------------------------------------------------------------*/
1388 /* return info string */
1389 /*----------------------------------------------------------------*/
1390 static const char *nsp_info(struct Scsi_Host *shpnt)
1392 nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
1394 return data->nspinfo;
1398 #define SPRINTF(args...) \
1400 if(length > (pos - buffer)) { \
1401 pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
1402 nsp_dbg(NSP_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length, length - (pos - buffer));\
1407 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1408 struct Scsi_Host *host,
1414 #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1423 unsigned long flags;
1425 #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1426 struct Scsi_Host *host;
1434 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
1435 hostno = host->host_no;
1437 /* search this HBA host */
1438 host = scsi_host_hn_get(hostno);
1443 data = (nsp_hw_data *)host->hostdata;
1446 SPRINTF("NinjaSCSI status\n\n");
1447 SPRINTF("Driver version: $Revision: 1.23 $\n");
1448 SPRINTF("SCSI host No.: %d\n", hostno);
1449 SPRINTF("IRQ: %d\n", host->irq);
1450 SPRINTF("IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
1451 SPRINTF("MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
1452 SPRINTF("sg_tablesize: %d\n", host->sg_tablesize);
1454 SPRINTF("burst transfer mode: ");
1455 switch (nsp_burst_mode) {
1472 spin_lock_irqsave(&(data->Lock), flags);
1473 SPRINTF("CurrentSC: 0x%p\n\n", data->CurrentSC);
1474 spin_unlock_irqrestore(&(data->Lock), flags);
1476 SPRINTF("SDTR status\n");
1477 for(id = 0; id < ARRAY_SIZE(data->Sync); id++) {
1479 SPRINTF("id %d: ", id);
1481 if (id == host->this_id) {
1482 SPRINTF("----- NinjaSCSI-3 host adapter\n");
1486 switch(data->Sync[id].SyncNegotiation) {
1501 if (data->Sync[id].SyncPeriod != 0) {
1502 speed = 1000000 / (data->Sync[id].SyncPeriod * 4);
1504 SPRINTF(" transfer %d.%dMB/s, offset %d",
1507 data->Sync[id].SyncOffset
1513 thislength = pos - (buffer + offset);
1515 if(thislength < 0) {
1521 thislength = min(thislength, length);
1522 *start = buffer + offset;
1528 /*---------------------------------------------------------------*/
1530 /*---------------------------------------------------------------*/
1533 static int nsp_eh_abort(Scsi_Cmnd *SCpnt)
1535 nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1537 return nsp_eh_bus_reset(SCpnt);
1540 static int nsp_bus_reset(nsp_hw_data *data)
1542 unsigned int base = data->BaseAddress;
1545 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
1547 nsp_index_write(base, SCSIBUSCTRL, SCSI_RST);
1548 mdelay(100); /* 100ms */
1549 nsp_index_write(base, SCSIBUSCTRL, 0);
1550 for(i = 0; i < 5; i++) {
1551 nsp_index_read(base, IRQPHASESENCE); /* dummy read */
1554 nsphw_init_sync(data);
1556 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
1561 static int nsp_eh_bus_reset(Scsi_Cmnd *SCpnt)
1563 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1565 nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1567 return nsp_bus_reset(data);
1570 static int nsp_eh_host_reset(Scsi_Cmnd *SCpnt)
1572 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1574 nsp_dbg(NSP_DEBUG_BUSRESET, "in");
1582 /**********************************************************************
1584 **********************************************************************/
1586 /*======================================================================
1587 nsp_cs_attach() creates an "instance" of the driver, allocating
1588 local data structures for one device. The device is registered
1591 The dev_link structure is initialized, but we don't actually
1592 configure the card at this point -- we wait until we receive a
1593 card insertion event.
1594 ======================================================================*/
1595 static int nsp_cs_probe(struct pcmcia_device *link)
1598 nsp_hw_data *data = &nsp_data_base;
1601 nsp_dbg(NSP_DEBUG_INIT, "in");
1603 /* Create new SCSI device */
1604 info = kmalloc(sizeof(*info), GFP_KERNEL);
1605 if (info == NULL) { return -ENOMEM; }
1606 memset(info, 0, sizeof(*info));
1609 data->ScsiInfo = info;
1611 nsp_dbg(NSP_DEBUG_INIT, "info=0x%p", info);
1613 /* The io structure describes IO port mapping */
1614 link->io.NumPorts1 = 0x10;
1615 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
1616 link->io.IOAddrLines = 10; /* not used */
1618 /* Interrupt setup */
1619 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1620 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
1622 /* Interrupt handler */
1623 link->irq.Handler = &nspintr;
1624 link->irq.Instance = info;
1625 link->irq.Attributes |= IRQF_SHARED;
1627 /* General socket configuration */
1628 link->conf.Attributes = CONF_ENABLE_IRQ;
1629 link->conf.IntType = INT_MEMORY_AND_IO;
1630 link->conf.Present = PRESENT_OPTION;
1632 ret = nsp_cs_config(link);
1634 nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1636 } /* nsp_cs_attach */
1639 /*======================================================================
1640 This deletes a driver "instance". The device is de-registered
1641 with Card Services. If it has been released, all local data
1642 structures are freed. Otherwise, the structures will be freed
1643 when the device is released.
1644 ======================================================================*/
1645 static void nsp_cs_detach(struct pcmcia_device *link)
1647 nsp_dbg(NSP_DEBUG_INIT, "in, link=0x%p", link);
1649 ((scsi_info_t *)link->priv)->stop = 1;
1650 nsp_cs_release(link);
1654 } /* nsp_cs_detach */
1657 /*======================================================================
1658 nsp_cs_config() is scheduled to run after a CARD_INSERTION event
1659 is received, to configure the PCMCIA socket, and to make the
1660 ethernet device available to the system.
1661 ======================================================================*/
1662 #define CS_CHECK(fn, ret) \
1663 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
1664 /*====================================================================*/
1665 static int nsp_cs_config(struct pcmcia_device *link)
1668 scsi_info_t *info = link->priv;
1671 int last_ret, last_fn;
1672 unsigned char tuple_data[64];
1676 cistpl_cftable_entry_t dflt = { 0 };
1677 struct Scsi_Host *host;
1678 nsp_hw_data *data = &nsp_data_base;
1679 #if !(LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,74))
1680 struct scsi_device *dev;
1681 dev_node_t **tail, *node;
1684 nsp_dbg(NSP_DEBUG_INIT, "in");
1686 tuple.DesiredTuple = CISTPL_CONFIG;
1687 tuple.Attributes = 0;
1688 tuple.TupleData = tuple_data;
1689 tuple.TupleDataMax = sizeof(tuple_data);
1690 tuple.TupleOffset = 0;
1691 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
1692 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
1693 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
1694 link->conf.ConfigBase = parse.config.base;
1695 link->conf.Present = parse.config.rmask[0];
1697 /* Look up the current Vcc */
1698 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(link, &conf));
1700 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
1701 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
1703 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
1705 if (pcmcia_get_tuple_data(link, &tuple) != 0 ||
1706 pcmcia_parse_tuple(link, &tuple, &parse) != 0)
1709 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) { dflt = *cfg; }
1710 if (cfg->index == 0) { goto next_entry; }
1711 link->conf.ConfigIndex = cfg->index;
1713 /* Does this card need audio output? */
1714 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
1715 link->conf.Attributes |= CONF_ENABLE_SPKR;
1716 link->conf.Status = CCSR_AUDIO_ENA;
1719 /* Use power settings for Vcc and Vpp if present */
1720 /* Note that the CIS values need to be rescaled */
1721 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
1722 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000) {
1725 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
1726 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM]/10000) {
1731 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM)) {
1733 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
1734 } else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM)) {
1736 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
1739 /* Do we need to allocate an interrupt? */
1740 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) {
1741 link->conf.Attributes |= CONF_ENABLE_IRQ;
1744 /* IO window settings */
1745 link->io.NumPorts1 = link->io.NumPorts2 = 0;
1746 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
1747 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
1748 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
1749 if (!(io->flags & CISTPL_IO_8BIT))
1750 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
1751 if (!(io->flags & CISTPL_IO_16BIT))
1752 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1753 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
1754 link->io.BasePort1 = io->win[0].base;
1755 link->io.NumPorts1 = io->win[0].len;
1757 link->io.Attributes2 = link->io.Attributes1;
1758 link->io.BasePort2 = io->win[1].base;
1759 link->io.NumPorts2 = io->win[1].len;
1761 /* This reserves IO space but doesn't actually enable it */
1762 if (pcmcia_request_io(link, &link->io) != 0)
1766 if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
1768 (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
1769 req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM;
1770 req.Attributes |= WIN_ENABLE;
1771 req.Base = mem->win[0].host_addr;
1772 req.Size = mem->win[0].len;
1773 if (req.Size < 0x1000) {
1776 req.AccessSpeed = 0;
1777 if (pcmcia_request_window(&link, &req, &link->win) != 0)
1779 map.Page = 0; map.CardOffset = mem->win[0].card_addr;
1780 if (pcmcia_map_mem_page(link->win, &map) != 0)
1783 data->MmioAddress = (unsigned long)ioremap_nocache(req.Base, req.Size);
1784 data->MmioLength = req.Size;
1786 /* If we got this far, we're cool! */
1790 nsp_dbg(NSP_DEBUG_INIT, "next");
1791 pcmcia_disable_device(link);
1792 CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(link, &tuple));
1795 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
1796 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
1798 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
1801 if (link->io.BasePort1) {
1802 release_region(link->io.BasePort1, link->io.NumPorts1);
1804 if (link->io.BasePort2) {
1805 release_region(link->io.BasePort2, link->io.NumPorts2);
1809 /* Set port and IRQ */
1810 data->BaseAddress = link->io.BasePort1;
1811 data->NumAddress = link->io.NumPorts1;
1812 data->IrqNumber = link->irq.AssignedIRQ;
1814 nsp_dbg(NSP_DEBUG_INIT, "I/O[0x%x+0x%x] IRQ %d",
1815 data->BaseAddress, data->NumAddress, data->IrqNumber);
1817 if(nsphw_init(data) == FALSE) {
1821 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,2))
1822 host = nsp_detect(&nsp_driver_template);
1824 scsi_register_host(&nsp_driver_template);
1825 for (host = scsi_host_get_next(NULL); host != NULL;
1826 host = scsi_host_get_next(host)) {
1827 if (host->hostt == &nsp_driver_template) {
1834 nsp_dbg(NSP_DEBUG_INIT, "detect failed");
1839 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,74))
1840 ret = scsi_add_host (host, NULL);
1844 scsi_scan_host(host);
1846 snprintf(info->node.dev_name, sizeof(info->node.dev_name), "scsi%d", host->host_no);
1847 link->dev_node = &info->node;
1851 nsp_dbg(NSP_DEBUG_INIT, "GET_SCSI_INFO");
1852 tail = &link->dev_node;
1855 nsp_dbg(NSP_DEBUG_INIT, "host=0x%p", host);
1857 for (dev = host->host_queue; dev != NULL; dev = dev->next) {
1859 id = (dev->id & 0x0f) + ((dev->lun & 0x0f) << 4) +
1860 ((dev->channel & 0x0f) << 8) +
1861 ((dev->host->host_no & 0x0f) << 12);
1862 node = &info->node[info->ndev];
1864 switch (dev->type) {
1866 node->major = SCSI_TAPE_MAJOR;
1867 snprintf(node->dev_name, sizeof(node->dev_name), "st#%04lx", id);
1871 node->major = SCSI_DISK0_MAJOR;
1872 snprintf(node->dev_name, sizeof(node->dev_name), "sd#%04lx", id);
1876 node->major = SCSI_CDROM_MAJOR;
1877 snprintf(node->dev_name, sizeof(node->dev_name), "sr#%04lx", id);
1880 node->major = SCSI_GENERIC_MAJOR;
1881 snprintf(node->dev_name, sizeof(node->dev_name), "sg#%04lx", id);
1884 *tail = node; tail = &node->next;
1886 info->host = dev->host;
1890 if (info->ndev == 0) {
1891 nsp_msg(KERN_INFO, "no SCSI devices found");
1893 nsp_dbg(NSP_DEBUG_INIT, "host=0x%p", host);
1896 /* Finally, report what we've done */
1897 printk(KERN_INFO "nsp_cs: index 0x%02x: ",
1898 link->conf.ConfigIndex);
1899 if (link->conf.Vpp) {
1900 printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
1902 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
1903 printk(", irq %d", link->irq.AssignedIRQ);
1905 if (link->io.NumPorts1) {
1906 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
1907 link->io.BasePort1+link->io.NumPorts1-1);
1909 if (link->io.NumPorts2)
1910 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
1911 link->io.BasePort2+link->io.NumPorts2-1);
1913 printk(", mem 0x%06lx-0x%06lx", req.Base,
1914 req.Base+req.Size-1);
1920 nsp_dbg(NSP_DEBUG_INIT, "config fail");
1921 cs_error(link, last_fn, last_ret);
1922 nsp_cs_release(link);
1925 } /* nsp_cs_config */
1929 /*======================================================================
1930 After a card is removed, nsp_cs_release() will unregister the net
1931 device, and release the PCMCIA configuration. If the device is
1932 still open, this will be postponed until it is closed.
1933 ======================================================================*/
1934 static void nsp_cs_release(struct pcmcia_device *link)
1936 scsi_info_t *info = link->priv;
1937 nsp_hw_data *data = NULL;
1939 if (info->host == NULL) {
1940 nsp_msg(KERN_DEBUG, "unexpected card release call.");
1942 data = (nsp_hw_data *)info->host->hostdata;
1945 nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1947 /* Unlink the device chain */
1948 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2))
1949 if (info->host != NULL) {
1950 scsi_remove_host(info->host);
1953 scsi_unregister_host(&nsp_driver_template);
1955 link->dev_node = NULL;
1959 iounmap((void *)(data->MmioAddress));
1962 pcmcia_disable_device(link);
1964 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2))
1965 if (info->host != NULL) {
1966 scsi_host_put(info->host);
1969 } /* nsp_cs_release */
1971 static int nsp_cs_suspend(struct pcmcia_device *link)
1973 scsi_info_t *info = link->priv;
1976 nsp_dbg(NSP_DEBUG_INIT, "event: suspend");
1978 if (info->host != NULL) {
1979 nsp_msg(KERN_INFO, "clear SDTR status");
1981 data = (nsp_hw_data *)info->host->hostdata;
1983 nsphw_init_sync(data);
1991 static int nsp_cs_resume(struct pcmcia_device *link)
1993 scsi_info_t *info = link->priv;
1996 nsp_dbg(NSP_DEBUG_INIT, "event: resume");
2000 if (info->host != NULL) {
2001 nsp_msg(KERN_INFO, "reset host and bus");
2003 data = (nsp_hw_data *)info->host->hostdata;
2006 nsp_bus_reset(data);
2012 /*======================================================================*
2013 * module entry point
2014 *====================================================================*/
2015 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,68))
2016 static struct pcmcia_device_id nsp_cs_ids[] = {
2017 PCMCIA_DEVICE_PROD_ID123("IO DATA", "CBSC16 ", "1", 0x547e66dc, 0x0d63a3fd, 0x51de003a),
2018 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-001", "1", 0x534c02bc, 0x52008408, 0x51de003a),
2019 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-002", "1", 0x534c02bc, 0xcb09d5b2, 0x51de003a),
2020 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-003", "1", 0x534c02bc, 0xbc0ee524, 0x51de003a),
2021 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-004", "1", 0x534c02bc, 0x226a7087, 0x51de003a),
2022 PCMCIA_DEVICE_PROD_ID123("WBT", "NinjaSCSI-3", "R1.0", 0xc7ba805f, 0xfdc7c97d, 0x6973710e),
2023 PCMCIA_DEVICE_PROD_ID123("WORKBIT", "UltraNinja-16", "1", 0x28191418, 0xb70f4b09, 0x51de003a),
2026 MODULE_DEVICE_TABLE(pcmcia, nsp_cs_ids);
2028 static struct pcmcia_driver nsp_driver = {
2029 .owner = THIS_MODULE,
2033 .probe = nsp_cs_probe,
2034 .remove = nsp_cs_detach,
2035 .id_table = nsp_cs_ids,
2036 .suspend = nsp_cs_suspend,
2037 .resume = nsp_cs_resume,
2041 static int __init nsp_cs_init(void)
2043 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,68))
2044 nsp_msg(KERN_INFO, "loading...");
2046 return pcmcia_register_driver(&nsp_driver);
2050 nsp_msg(KERN_INFO, "loading...");
2051 pcmcia_get_card_services_info(&serv);
2052 if (serv.Revision != CS_RELEASE_CODE) {
2053 nsp_msg(KERN_DEBUG, "Card Services release does not match!");
2056 register_pcmcia_driver(&dev_info, &nsp_cs_attach, &nsp_cs_detach);
2058 nsp_dbg(NSP_DEBUG_INIT, "out");
2063 static void __exit nsp_cs_exit(void)
2065 nsp_msg(KERN_INFO, "unloading...");
2066 pcmcia_unregister_driver(&nsp_driver);
2070 module_init(nsp_cs_init)
2071 module_exit(nsp_cs_exit)