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 (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 ***********************************************************************/
31 #include <pcmcia/config.h>
32 #include <pcmcia/k_compat.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/timer.h>
42 #include <linux/ioport.h>
43 #include <linux/delay.h>
44 #include <linux/interrupt.h>
45 #include <linux/module.h>
46 #include <linux/major.h>
47 #include <linux/blk.h>
48 #include <linux/stat.h>
56 #include <scsi/scsi.h>
57 #include <scsi/scsi_ioctl.h>
59 #include <pcmcia/version.h>
60 #include <pcmcia/cs_types.h>
61 #include <pcmcia/cs.h>
62 #include <pcmcia/cistpl.h>
63 #include <pcmcia/cisreg.h>
64 #include <pcmcia/ds.h>
68 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>");
69 MODULE_DESCRIPTION("WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module $Revision: 1.1 $");
70 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
72 MODULE_LICENSE("GPL");
76 static int pc_debug = PCMCIA_DEBUG;
77 MODULE_PARM(pc_debug, "i");
78 MODULE_PARM_DESC(pc_debug, "set debug level");
79 static char *version = "";
80 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
82 #define DEBUG(n, args...) /* */
87 /*====================================================================*/
89 typedef struct scsi_info_t {
92 struct Scsi_Host *host;
97 /*----------------------------------------------------------------*/
99 #if (KERNEL_VERSION(2,4,0) > LINUX_VERSION_CODE)
100 #define PROC_SCSI_NSP PROC_SCSI_IBMMCA /* bad hack... */
101 static struct proc_dir_entry proc_scsi_nsp = {
102 PROC_SCSI_NSP, 6, "nsp_cs",
103 S_IFDIR | S_IRUGO | S_IXUGO, 2
107 /*====================================================================*/
108 /* Parameters that can be set with 'insmod' */
110 static unsigned int irq_mask = 0xffff;
111 MODULE_PARM(irq_mask, "i");
112 MODULE_PARM_DESC(irq_mask, "IRQ mask bits (default: 0xffff)");
114 static int irq_list[4] = { -1 };
115 MODULE_PARM(irq_list, "1-4i");
116 MODULE_PARM_DESC(irq_list, "Use specified IRQ number. (default: auto select)");
118 static int nsp_burst_mode = 2;
119 MODULE_PARM(nsp_burst_mode, "i");
120 MODULE_PARM_DESC(nsp_burst_mode, "Burst transfer mode (0=io8, 1=io32, 2=mem32(default))");
122 /* Release IO ports after configuration? */
123 static int free_ports = 0;
124 MODULE_PARM(free_ports, "i");
125 MODULE_PARM_DESC(free_ports, "Release IO ports after configuration? (default: 0 (=no))");
127 /* /usr/src/linux/drivers/scsi/hosts.h */
128 static Scsi_Host_Template nsp_driver_template = {
129 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0))
130 .proc_name = "nsp_cs", /* kernel 2.4 */
132 .proc_dir = &proc_scsi_nsp, /* kernel 2.2 */
134 .proc_info = nsp_proc_info,
135 .name = "WorkBit NinjaSCSI-3/32Bi(16bit)",
136 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
137 .detect = nsp_detect,
138 .release = nsp_release,
141 .queuecommand = nsp_queuecommand,
142 /* .eh_strategy_handler = nsp_eh_strategy,*/
143 /* .eh_abort_handler = nsp_eh_abort,*/
144 /* .eh_device_reset_handler = nsp_eh_device_reset,*/
145 .eh_bus_reset_handler = nsp_eh_bus_reset,
146 .eh_host_reset_handler = nsp_eh_host_reset,
148 .this_id = NSP_INITIATOR_ID,
149 .sg_tablesize = SG_ALL,
151 .use_clustering = DISABLE_CLUSTERING,
152 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,2))
153 .use_new_eh_code = 1,
157 static dev_link_t *dev_list = NULL;
158 static dev_info_t dev_info = {"nsp_cs"};
160 static nsp_hw_data nsp_data;
162 /***********************************************************/
164 static int nsp_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
167 /*unsigned int host_id = SCpnt->host->this_id;*/
168 /*unsigned int base = SCpnt->host->io_port;*/
169 unsigned char target = SCpnt->target;
171 nsp_hw_data *data = &nsp_data;
173 DEBUG(0, "%s: SCpnt=0x%p target=%d lun=%d buff=0x%p bufflen=%d use_sg=%d\n",
174 __FUNCTION__, SCpnt, target, SCpnt->lun, SCpnt->request_buffer, SCpnt->request_bufflen, SCpnt->use_sg);
175 //DEBUG(0, " before CurrentSC=0x%p\n", data->CurrentSC);
177 if(data->CurrentSC != NULL) {
178 printk(KERN_DEBUG " %s: CurrentSC!=NULL this can't be happen\n", __FUNCTION__);
179 data->CurrentSC = NULL;
180 SCpnt->result = DID_BAD_TARGET << 16;
187 SCpnt->scsi_done = done;
188 data->CurrentSC = SCpnt;
190 SCpnt->SCp.Status = CHECK_CONDITION;
191 SCpnt->SCp.Message = 0;
192 SCpnt->SCp.have_data_in = IO_UNKNOWN;
193 SCpnt->SCp.sent_command = 0;
194 SCpnt->SCp.phase = PH_UNDETERMINED;
195 RESID = SCpnt->request_bufflen;
197 /* setup scratch area
198 SCp.ptr : buffer pointer
199 SCp.this_residual : buffer length
200 SCp.buffer : next buffer
201 SCp.buffers_residual : left buffers in list
202 SCp.phase : current state of the command */
204 SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
205 SCpnt->SCp.ptr = BUFFER_ADDR;
206 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
207 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
209 SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
210 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
211 SCpnt->SCp.buffer = NULL;
212 SCpnt->SCp.buffers_residual = 0;
215 if(nsphw_start_selection(SCpnt, data) == FALSE) {
216 DEBUG(0, " selection fail\n");
217 data->CurrentSC = NULL;
218 SCpnt->result = DID_NO_CONNECT << 16;
224 //DEBUG(0, "%s: out\n", __FUNCTION__);
229 * setup PIO FIFO transfer mode and enable/disable to data out
231 static void nsp_setup_fifo(nsp_hw_data *data, int enabled)
233 unsigned int base = data->BaseAddress;
234 unsigned char transfer_mode_reg;
236 //DEBUG(0, "%s: enabled=%d\n", __FUNCTION__, enabled);
238 if (enabled != FALSE) {
239 transfer_mode_reg = TRANSFER_GO | BRAIND;
241 transfer_mode_reg = 0;
244 transfer_mode_reg |= data->TransferMode;
246 nsp_index_write(base, TRANSFERMODE, transfer_mode_reg);
249 static void nsphw_init_sync(nsp_hw_data *data)
251 sync_data tmp_sync = { .SyncNegotiation = SYNC_NOT_YET,
257 /* setup sync data */
258 for ( i = 0; i < NUMBER(data->Sync); i++ ) {
259 data->Sync[i] = tmp_sync;
264 * Initialize Ninja hardware
266 static int nsphw_init(nsp_hw_data *data)
268 unsigned int base = data->BaseAddress;
270 DEBUG(0, "%s: in base=0x%x\n", __FUNCTION__, base);
272 data->ScsiClockDiv = CLOCK_40M | FAST_20;
273 data->CurrentSC = NULL;
275 data->TransferMode = MODE_IO8;
277 nsphw_init_sync(data);
279 /* block all interrupts */
280 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
282 /* setup SCSI interface */
283 nsp_write(base, IFSELECT, IF_IFSEL);
285 nsp_index_write(base, SCSIIRQMODE, 0);
287 nsp_index_write(base, TRANSFERMODE, MODE_IO8);
288 nsp_index_write(base, CLOCKDIV, data->ScsiClockDiv);
290 nsp_index_write(base, PARITYCTRL, 0);
291 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
296 /* setup fifo asic */
297 nsp_write(base, IFSELECT, IF_REGSEL);
298 nsp_index_write(base, TERMPWRCTRL, 0);
299 if ((nsp_index_read(base, OTHERCONTROL) & TPWR_SENSE) == 0) {
300 printk(KERN_INFO "nsp_cs: terminator power on\n");
301 nsp_index_write(base, TERMPWRCTRL, POWER_ON);
304 nsp_index_write(base, TIMERCOUNT, 0);
305 nsp_index_write(base, TIMERCOUNT, 0); /* requires 2 times!! */
307 nsp_index_write(base, SYNCREG, 0);
308 nsp_index_write(base, ACKWIDTH, 0);
310 /* enable interrupts and ack them */
311 nsp_index_write(base, SCSIIRQMODE, SCSI_PHASE_CHANGE_EI |
314 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
316 nsp_setup_fifo(data, FALSE);
322 * Start selection phase
324 static unsigned int nsphw_start_selection(Scsi_Cmnd *SCpnt,
327 unsigned int host_id = SCpnt->device->host->this_id;
328 unsigned int base = SCpnt->device->host->io_port;
329 unsigned char target = SCpnt->device->id;
331 unsigned char phase, arbit;
333 //DEBUG(0, "%s:in\n", __FUNCTION__);
335 phase = nsp_index_read(base, SCSIBUSMON);
336 if(phase != BUSMON_BUS_FREE) {
337 //DEBUG(0, " bus busy\n");
341 /* start arbitration */
342 //DEBUG(0, " start arbit\n");
343 SCpnt->SCp.phase = PH_ARBSTART;
344 nsp_index_write(base, SETARBIT, ARBIT_GO);
348 /* XXX: what a stupid chip! */
349 arbit = nsp_index_read(base, ARBITSTATUS);
350 //DEBUG(0, " arbit=%d, wait_count=%d\n", arbit, wait_count);
351 udelay(1); /* hold 1.2us */
352 } while((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
355 if((arbit & ARBIT_WIN) == 0) {
356 //DEBUG(0, " arbit fail\n");
357 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
361 /* assert select line */
362 //DEBUG(0, " assert SEL line\n");
363 SCpnt->SCp.phase = PH_SELSTART;
365 nsp_index_write(base, SCSIDATALATCH, BIT(host_id) | BIT(target));
366 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_ATN);
368 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_DATAOUT_ENB | SCSI_ATN);
369 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
371 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_DATAOUT_ENB | SCSI_ATN);
373 /* check selection timeout */
374 nsp_start_timer(SCpnt, data, 1000/51);
375 data->SelectionTimeOut = 1;
380 struct nsp_sync_table {
381 unsigned int min_period;
382 unsigned int max_period;
383 unsigned int chip_period;
384 unsigned int ack_width;
387 static struct nsp_sync_table nsp_sync_table_40M[] = {
388 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
389 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
390 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
391 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
395 static struct nsp_sync_table nsp_sync_table_20M[] = {
396 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
397 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
398 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
403 * setup synchronous data transfer mode
405 static int nsp_msg(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
407 unsigned char target = SCpnt->device->id;
408 // unsigned char lun = SCpnt->lun;
409 sync_data *sync = &(data->Sync[target]);
410 struct nsp_sync_table *sync_table;
411 unsigned int period, offset;
415 DEBUG(0, "%s:\n", __FUNCTION__);
417 period = sync->SyncPeriod;
418 offset = sync->SyncOffset;
420 DEBUG(0, " period=0x%x, offset=0x%x\n", period, offset);
422 if ((data->ScsiClockDiv & (BIT(0)|BIT(1))) == CLOCK_20M) {
423 sync_table = &nsp_sync_table_20M[0];
425 sync_table = &nsp_sync_table_40M[0];
428 for ( i = 0; sync_table->max_period != 0; i++, sync_table++) {
429 if ( period >= sync_table->min_period &&
430 period <= sync_table->max_period ) {
435 if (period != 0 && sync_table->max_period == 0) {
437 * No proper period/offset found
439 DEBUG(0, " no proper period/offset\n");
441 sync->SyncPeriod = 0;
442 sync->SyncOffset = 0;
443 sync->SyncRegister = 0;
449 sync->SyncRegister = (sync_table->chip_period << SYNCREG_PERIOD_SHIFT) |
450 (offset & SYNCREG_OFFSET_MASK);
451 sync->AckWidth = sync_table->ack_width;
453 DEBUG(0, " sync_reg=0x%x, ack_width=0x%x\n", sync->SyncRegister, sync->AckWidth);
460 * start ninja hardware timer
462 static void nsp_start_timer(Scsi_Cmnd *SCpnt, nsp_hw_data *data, int time)
464 unsigned int base = SCpnt->device->host->io_port;
466 //DEBUG(0, "%s: in SCpnt=0x%p, time=%d\n", __FUNCTION__, SCpnt, time);
467 data->TimerCount = time;
468 nsp_index_write(base, TIMERCOUNT, time);
472 * wait for bus phase change
474 static int nsp_negate_signal(Scsi_Cmnd *SCpnt, unsigned char mask, char *str)
476 unsigned int base = SCpnt->device->host->io_port;
480 //DEBUG(0, "%s:\n", __FUNCTION__);
485 reg = nsp_index_read(base, SCSIBUSMON);
489 } while ((time_out-- != 0) && (reg & mask) != 0);
492 printk(KERN_DEBUG "%s:: %s signal off timeut\n", __FUNCTION__, str);
501 static int nsp_expect_signal(Scsi_Cmnd *SCpnt,
502 unsigned char current_phase,
505 unsigned int base = SCpnt->device->host->io_port;
507 unsigned char phase, i_src;
509 //DEBUG(0, "%s: current_phase=0x%x, mask=0x%x\n", __FUNCTION__, current_phase, mask);
513 phase = nsp_index_read(base, SCSIBUSMON);
515 //DEBUG(0, " ret -1\n");
518 i_src = nsp_read(base, IRQSTATUS);
519 if (i_src & IRQSTATUS_SCSI) {
520 //DEBUG(0, " ret 0 found scsi signal\n");
523 if ((phase & mask) != 0 && (phase & BUSMON_PHASE_MASK) == current_phase) {
524 //DEBUG(0, " ret 1 phase=0x%x\n", phase);
527 } while(time_out-- != 0);
529 //DEBUG(0, "%s: timeout\n", __FUNCTION__);
534 * transfer SCSI message
536 static int nsp_xfer(Scsi_Cmnd *SCpnt, nsp_hw_data *data, int phase)
538 unsigned int base = SCpnt->device->host->io_port;
539 char *buf = data->MsgBuffer;
540 int len = MIN(MSGBUF_SIZE, data->MsgLen);
544 //DEBUG(0, "%s:\n", __FUNCTION__);
545 for (ptr = 0; len > 0; len --, ptr ++) {
547 ret = nsp_expect_signal(SCpnt, phase, BUSMON_REQ);
549 DEBUG(0, " xfer quit\n");
553 /* if last byte, negate ATN */
554 if (len == 1 && SCpnt->SCp.phase == PH_MSG_OUT) {
555 nsp_index_write(base, SCSIBUSCTRL, AUTODIRECTION | ACKENB);
558 /* read & write message */
559 if (phase & BUSMON_IO) {
560 DEBUG(0, " read msg\n");
561 buf[ptr] = nsp_index_read(base, SCSIDATAWITHACK);
563 DEBUG(0, " write msg\n");
564 nsp_index_write(base, SCSIDATAWITHACK, buf[ptr]);
566 nsp_negate_signal(SCpnt, BUSMON_ACK, "xfer<ack>");
573 * get extra SCSI data from fifo
575 static int nsp_dataphase_bypass(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
579 //DEBUG(0, "%s:\n", __FUNCTION__);
581 if (SCpnt->SCp.have_data_in != IO_IN) {
585 count = nsp_fifo_count(SCpnt);
586 if (data->FifoCount == count) {
587 //DEBUG(0, " not use bypass quirk\n");
593 * data phase skip only occures in case of SCSI_LOW_READ
595 DEBUG(0, " use bypass quirk\n");
596 SCpnt->SCp.phase = PH_DATA;
597 nsp_pio_read(SCpnt, data);
598 nsp_setup_fifo(data, FALSE);
606 static int nsp_reselected(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
608 unsigned int base = SCpnt->device->host->io_port;
611 //DEBUG(0, "%s:\n", __FUNCTION__);
613 nsp_negate_signal(SCpnt, BUSMON_SEL, "reselect<SEL>");
615 nsp_nexus(SCpnt, data);
616 reg = nsp_index_read(base, SCSIBUSCTRL) & ~(SCSI_BSY | SCSI_ATN);
617 nsp_index_write(base, SCSIBUSCTRL, reg);
618 nsp_index_write(base, SCSIBUSCTRL, reg | AUTODIRECTION | ACKENB);
624 * count how many data transferd
626 static int nsp_fifo_count(Scsi_Cmnd *SCpnt)
628 unsigned int base = SCpnt->device->host->io_port;
630 unsigned int l, m, h, dummy;
632 nsp_index_write(base, POINTERCLR, POINTER_CLEAR | ACK_COUNTER);
634 l = nsp_index_read(base, TRANSFERCOUNT);
635 m = nsp_index_read(base, TRANSFERCOUNT);
636 h = nsp_index_read(base, TRANSFERCOUNT);
637 dummy = nsp_index_read(base, TRANSFERCOUNT);
639 count = (h << 16) | (m << 8) | (l << 0);
641 //DEBUG(0, "%s: =0x%x\n", __FUNCTION__, count);
647 #define RFIFO_CRIT 64
648 #define WFIFO_CRIT 64
651 * read data in DATA IN phase
653 static void nsp_pio_read(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
655 unsigned int base = SCpnt->device->host->io_port;
656 unsigned long mmio_base = SCpnt->device->host->base;
659 unsigned char stat, fifo_stat;
661 ocount = data->FifoCount;
663 DEBUG(0, "%s: in SCpnt=0x%p resid=%d ocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d\n",
664 __FUNCTION__, SCpnt, RESID, ocount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual);
668 while ((time_out-- != 0) &&
669 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0 ) ) {
671 stat = nsp_index_read(base, SCSIBUSMON);
672 stat &= BUSMON_PHASE_MASK;
675 res = nsp_fifo_count(SCpnt) - ocount;
676 //DEBUG(0, " ptr=0x%p this=0x%x ocount=0x%x res=0x%x\n", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount, res);
677 if (res == 0) { /* if some data avilable ? */
678 if (stat == BUSPHASE_DATA_IN) { /* phase changed? */
679 //DEBUG(0, " wait for data this=%d\n", SCpnt->SCp.this_residual);
682 DEBUG(0, " phase changed stat=0x%x\n", stat);
687 fifo_stat = nsp_read(base, FIFOSTATUS);
688 if ((fifo_stat & FIFOSTATUS_FULL_EMPTY) == 0 &&
689 stat == BUSPHASE_DATA_IN) {
693 res = MIN(res, SCpnt->SCp.this_residual);
695 switch (data->TransferMode) {
697 res &= ~(BIT(1)|BIT(0)); /* align 4 */
698 nsp_fifo32_read(base, SCpnt->SCp.ptr, res >> 2);
701 nsp_fifo8_read (base, SCpnt->SCp.ptr, res );
705 res &= ~(BIT(1)|BIT(0)); /* align 4 */
706 nsp_mmio_fifo32_read(mmio_base, SCpnt->SCp.ptr, res >> 2);
710 DEBUG(0, "unknown read mode\n");
715 SCpnt->SCp.ptr += res;
716 SCpnt->SCp.this_residual -= res;
718 //DEBUG(0, " ptr=0x%p this_residual=0x%x ocount=0x%x\n", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount);
720 /* go to next scatter list if availavle */
721 if (SCpnt->SCp.this_residual == 0 &&
722 SCpnt->SCp.buffers_residual != 0 ) {
723 //DEBUG(0, " scatterlist next timeout=%d\n", time_out);
724 SCpnt->SCp.buffers_residual--;
726 SCpnt->SCp.ptr = BUFFER_ADDR;
727 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
730 //DEBUG(0, "page: 0x%p, off: 0x%x\n", SCpnt->SCp.buffer->page, SCpnt->SCp.buffer->offset);
734 data->FifoCount = ocount;
737 printk(KERN_DEBUG "%s: pio read timeout resid=%d this_residual=%d buffers_residual=%d\n",
738 __FUNCTION__, RESID, SCpnt->SCp.this_residual, SCpnt->SCp.buffers_residual);
740 DEBUG(0, " read ocount=0x%x\n", ocount);
744 * write data in DATA OUT phase
746 static void nsp_pio_write(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
748 unsigned int base = SCpnt->device->host->io_port;
749 unsigned long mmio_base = SCpnt->device->host->base;
754 ocount = data->FifoCount;
756 DEBUG(0, "%s: in fifocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d resid=0x%x\n",
757 __FUNCTION__, data->FifoCount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual, RESID);
761 while ((time_out-- != 0) &&
762 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0)) {
763 stat = nsp_index_read(base, SCSIBUSMON);
764 stat &= BUSMON_PHASE_MASK;
766 if (stat != BUSPHASE_DATA_OUT) {
767 res = ocount - nsp_fifo_count(SCpnt);
769 DEBUG(0, " phase changed stat=0x%x, res=%d\n", stat, res);
770 /* Put back pointer */
772 SCpnt->SCp.ptr -= res;
773 SCpnt->SCp.this_residual += res;
779 res = ocount - nsp_fifo_count(SCpnt);
780 if (res > 0) { /* write all data? */
781 DEBUG(0, " wait for all data out. ocount=0x%x res=%d\n", ocount, res);
785 res = MIN(SCpnt->SCp.this_residual, WFIFO_CRIT);
787 //DEBUG(0, " ptr=0x%p this=0x%x res=0x%x\n", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, res);
788 switch (data->TransferMode) {
790 res &= ~(BIT(1)|BIT(0)); /* align 4 */
791 nsp_fifo32_write(base, SCpnt->SCp.ptr, res >> 2);
794 nsp_fifo8_write (base, SCpnt->SCp.ptr, res );
798 res &= ~(BIT(1)|BIT(0)); /* align 4 */
799 nsp_mmio_fifo32_write(mmio_base, SCpnt->SCp.ptr, res >> 2);
803 DEBUG(0, "unknown write mode\n");
808 SCpnt->SCp.ptr += res;
809 SCpnt->SCp.this_residual -= res;
812 /* go to next scatter list if availavle */
813 if (SCpnt->SCp.this_residual == 0 &&
814 SCpnt->SCp.buffers_residual != 0 ) {
815 //DEBUG(0, " scatterlist next\n");
816 SCpnt->SCp.buffers_residual--;
818 SCpnt->SCp.ptr = BUFFER_ADDR;
819 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
824 data->FifoCount = ocount;
827 printk(KERN_DEBUG "%s: pio write timeout resid=0x%x\n", __FUNCTION__, RESID);
829 DEBUG(0, " write ocount=0x%x\n", ocount);
836 * setup synchronous/asynchronous data transfer mode
838 static int nsp_nexus(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
840 unsigned int base = SCpnt->device->host->io_port;
841 unsigned char target = SCpnt->device->id;
842 // unsigned char lun = SCpnt->lun;
843 sync_data *sync = &(data->Sync[target]);
845 //DEBUG(0, "%s: in SCpnt=0x%p\n", __FUNCTION__, SCpnt);
847 /* setup synch transfer registers */
848 nsp_index_write(base, SYNCREG, sync->SyncRegister);
849 nsp_index_write(base, ACKWIDTH, sync->AckWidth);
851 if (SCpnt->use_sg == 0 ||
853 RESID <= PAGE_SIZE ) {
854 data->TransferMode = MODE_IO8;
855 } else if (nsp_burst_mode == BURST_MEM32) {
856 data->TransferMode = MODE_MEM32;
857 } else if (nsp_burst_mode == BURST_IO32) {
858 data->TransferMode = MODE_IO32;
860 data->TransferMode = MODE_IO8;
863 /* setup pdma fifo */
864 nsp_setup_fifo(data, TRUE);
866 /* clear ack counter */
868 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
876 #include "nsp_message.c"
880 static void nspintr(int irq, void *dev_id, struct pt_regs *regs)
883 unsigned char i_src, irq_phase, phase;
885 unsigned char target, lun;
886 unsigned int *sync_neg;
888 nsp_hw_data *data = dev_id;
890 //printk("&nsp_data=0x%p, dev_id=0x%p\n", &nsp_data, dev_id);
892 base = data->BaseAddress;
893 //DEBUG(0, " base=0x%x\n", base);
898 nsp_write(base, IRQCONTROL, IRQCONTROL_IRQDISABLE);
899 i_src = nsp_read(base, IRQSTATUS);
900 //DEBUG(0, " i_src=0x%x\n", i_src);
901 if ((i_src == 0xff) || ((i_src & IRQSTATUS_MASK) == 0)) {
902 nsp_write(base, IRQCONTROL, 0);
903 //DEBUG(0, " no irq/shared irq\n");
909 * Do not read an irq_phase register if no scsi phase interrupt.
910 * Unless, you should lose a scsi phase interrupt.
912 phase = nsp_index_read(base, SCSIBUSMON);
913 if((i_src & IRQSTATUS_SCSI) != 0) {
914 irq_phase = nsp_index_read(base, IRQPHASESENCE);
919 //DEBUG(0, " irq_phase=0x%x\n", irq_phase);
922 * timer interrupt handler (scsi vs timer interrupts)
924 //DEBUG(0, " timercount=%d\n", data->TimerCount);
925 if (data->TimerCount != 0) {
926 //DEBUG(0, " stop timer\n");
927 nsp_index_write(base, TIMERCOUNT, 0);
928 nsp_index_write(base, TIMERCOUNT, 0);
929 data->TimerCount = 0;
932 if ((i_src & IRQSTATUS_MASK) == IRQSTATUS_TIMER &&
933 data->SelectionTimeOut == 0) {
934 //DEBUG(0, " timer start\n");
935 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR);
939 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR | IRQCONTROL_FIFO_CLEAR);
941 if (data->CurrentSC == NULL) {
942 printk(KERN_DEBUG "%s: CurrentSC==NULL irq_status=0x%x phase=0x%x irq_phase=0x%x this can't be happen\n", __FUNCTION__, i_src, phase, irq_phase);
945 tmpSC = data->CurrentSC;
946 target = tmpSC->device->id;
947 lun = tmpSC->device->lun;
948 sync_neg = &(data->Sync[target].SyncNegotiation);
952 * parse hardware SCSI irq reasons register
954 if ((i_src & IRQSTATUS_SCSI) != 0) {
955 if ((irq_phase & SCSI_RESET_IRQ) != 0) {
956 printk(KERN_DEBUG " %s: bus reset (power off?)\n", __FUNCTION__);
957 *sync_neg = SYNC_NOT_YET;
958 data->CurrentSC = NULL;
959 tmpSC->result = (DID_RESET << 16) |
960 ((tmpSC->SCp.Message & 0xff) << 8) |
961 ((tmpSC->SCp.Status & 0xff) << 0);
962 tmpSC->scsi_done(tmpSC);
966 if ((irq_phase & RESELECT_IRQ) != 0) {
967 DEBUG(0, " reselect\n");
968 nsp_write(base, IRQCONTROL, IRQCONTROL_RESELECT_CLEAR);
969 if (nsp_reselected(tmpSC, data) != FALSE) {
974 if ((irq_phase & (PHASE_CHANGE_IRQ | LATCHED_BUS_FREE)) == 0) {
981 switch(tmpSC->SCp.phase) {
983 //*sync_neg = SYNC_NOT_YET;
984 if ((phase & BUSMON_BSY) == 0) {
985 //DEBUG(0, " selection count=%d\n", data->SelectionTimeOut);
986 if (data->SelectionTimeOut >= NSP_SELTIMEOUT) {
987 DEBUG(0, " selection time out\n");
988 data->SelectionTimeOut = 0;
989 nsp_index_write(base, SCSIBUSCTRL, 0);
991 data->CurrentSC = NULL;
992 tmpSC->result = DID_NO_CONNECT << 16;
993 tmpSC->scsi_done(tmpSC);
997 data->SelectionTimeOut += 1;
998 nsp_start_timer(tmpSC, data, 1000/51);
1002 /* attention assert */
1003 //DEBUG(0, " attention assert\n");
1004 data->SelectionTimeOut = 0;
1005 tmpSC->SCp.phase = PH_SELECTED;
1006 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN);
1008 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN | AUTODIRECTION | ACKENB);
1014 //DEBUG(0, " phase reselect\n");
1015 //*sync_neg = SYNC_NOT_YET;
1016 if ((phase & BUSMON_PHASE_MASK) != BUSPHASE_MESSAGE_IN) {
1018 data->CurrentSC = NULL;
1019 tmpSC->result = DID_ABORT << 16;
1020 tmpSC->scsi_done(tmpSC);
1025 if ((i_src & (IRQSTATUS_SCSI | IRQSTATUS_FIFO)) == 0) {
1034 //DEBUG(0, " start scsi seq\n");
1036 /* normal disconnect */
1037 if (((tmpSC->SCp.phase == PH_MSG_IN) || (tmpSC->SCp.phase == PH_MSG_OUT)) &&
1038 (irq_phase & LATCHED_BUS_FREE) != 0 ) {
1039 DEBUG(0, " normal disconnect i_src=0x%x, phase=0x%x, irq_phase=0x%x\n", i_src, phase, irq_phase);
1040 if ((tmpSC->SCp.Message == MSG_COMMAND_COMPLETE)) { /* all command complete and return status */
1041 //*sync_neg = SYNC_NOT_YET;
1042 data->CurrentSC = NULL;
1043 tmpSC->result = (DID_OK << 16) |
1044 ((tmpSC->SCp.Message & 0xff) << 8) |
1045 ((tmpSC->SCp.Status & 0xff) << 0);
1046 DEBUG(0, " command complete result=0x%x\n", tmpSC->result);
1047 tmpSC->scsi_done(tmpSC);
1056 /* check unexpected bus free state */
1058 printk(KERN_DEBUG " %s: unexpected bus free. i_src=0x%x, phase=0x%x, irq_phase=0x%x\n", __FUNCTION__, i_src, phase, irq_phase);
1060 *sync_neg = SYNC_NOT_YET;
1061 data->CurrentSC = NULL;
1062 tmpSC->result = DID_ERROR << 16;
1063 tmpSC->scsi_done(tmpSC);
1067 switch (phase & BUSMON_PHASE_MASK) {
1068 case BUSPHASE_COMMAND:
1069 DEBUG(0, " BUSPHASE_COMMAND\n");
1070 if ((phase & BUSMON_REQ) == 0) {
1071 DEBUG(0, " REQ == 0\n");
1075 tmpSC->SCp.phase = PH_COMMAND;
1077 nsp_nexus(tmpSC, data);
1079 /* write scsi command */
1080 DEBUG(0, " cmd_len=%d\n", tmpSC->cmd_len);
1081 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER);
1082 for (i = 0; i < tmpSC->cmd_len; i++) {
1083 nsp_index_write(base, COMMANDDATA, tmpSC->cmnd[i]);
1085 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER | AUTO_COMMAND_GO);
1088 case BUSPHASE_DATA_OUT:
1089 DEBUG(0, " BUSPHASE_DATA_OUT\n");
1091 tmpSC->SCp.phase = PH_DATA;
1092 tmpSC->SCp.have_data_in = IO_OUT;
1094 nsp_pio_write(tmpSC, data);
1098 case BUSPHASE_DATA_IN:
1099 DEBUG(0, " BUSPHASE_DATA_IN\n");
1101 tmpSC->SCp.phase = PH_DATA;
1102 tmpSC->SCp.have_data_in = IO_IN;
1104 nsp_pio_read(tmpSC, data);
1108 case BUSPHASE_STATUS:
1109 nsp_dataphase_bypass(tmpSC, data);
1110 DEBUG(0, " BUSPHASE_STATUS\n");
1112 tmpSC->SCp.phase = PH_STATUS;
1114 tmpSC->SCp.Status = nsp_index_read(base, SCSIDATAWITHACK);
1115 DEBUG(0, " message=0x%x status=0x%x\n", tmpSC->SCp.Message, tmpSC->SCp.Status);
1119 case BUSPHASE_MESSAGE_OUT:
1120 DEBUG(0, " BUSPHASE_MESSAGE_OUT\n");
1121 if ((phase & BUSMON_REQ) == 0) {
1125 tmpSC->SCp.phase = PH_MSG_OUT;
1127 data->MsgLen = i = 0;
1128 data->MsgBuffer[i] = IDENTIFY(TRUE, lun); i++;
1130 if (*sync_neg == SYNC_NOT_YET) {
1131 data->Sync[target].SyncPeriod = 0;
1132 data->Sync[target].SyncOffset = 0;
1135 data->MsgBuffer[i] = MSG_EXTENDED; i++;
1136 data->MsgBuffer[i] = 3; i++;
1137 data->MsgBuffer[i] = MSG_EXT_SDTR; i++;
1138 data->MsgBuffer[i] = 0x0c; i++;
1139 data->MsgBuffer[i] = 15; i++;
1144 nsp_msg(tmpSC, data);
1146 nsp_message_out(tmpSC, data);
1149 case BUSPHASE_MESSAGE_IN:
1150 nsp_dataphase_bypass(tmpSC, data);
1151 DEBUG(0, " BUSPHASE_MESSAGE_IN\n");
1152 if ((phase & BUSMON_REQ) == 0) {
1156 tmpSC->SCp.phase = PH_MSG_IN;
1157 nsp_message_in(tmpSC, data);
1160 if (*sync_neg == SYNC_NOT_YET) {
1161 //printk("%d,%d\n",target,lun);
1163 if (data->MsgLen >= 5 &&
1164 data->MsgBuffer[0] == MSG_EXTENDED &&
1165 data->MsgBuffer[1] == 3 &&
1166 data->MsgBuffer[2] == MSG_EXT_SDTR ) {
1167 data->Sync[target].SyncPeriod = data->MsgBuffer[3];
1168 data->Sync[target].SyncOffset = data->MsgBuffer[4];
1169 //printk("sync ok, %d %d\n", data->MsgBuffer[3], data->MsgBuffer[4]);
1170 *sync_neg = SYNC_OK;
1172 data->Sync[target].SyncPeriod = 0;
1173 data->Sync[target].SyncOffset = 0;
1174 *sync_neg = SYNC_NG;
1176 nsp_msg(tmpSC, data);
1180 /* search last messeage byte */
1182 for (i = 0; i < data->MsgLen; i++) {
1183 tmp = data->MsgBuffer[i];
1184 if (data->MsgBuffer[i] == MSG_EXTENDED) {
1185 i += (1 + data->MsgBuffer[i+1]);
1188 tmpSC->SCp.Message = tmp;
1190 DEBUG(0, " message=0x%x len=%d\n", tmpSC->SCp.Message, data->MsgLen);
1195 case BUSPHASE_SELECT:
1197 DEBUG(0, " BUSPHASE other\n");
1202 //DEBUG(0, "%s: out\n", __FUNCTION__);
1206 nsp_start_timer(tmpSC, data, 1000/102);
1211 #include "nsp_debug.c"
1212 #endif /* DBG_SHOWCOMMAND */
1214 /*----------------------------------------------------------------*/
1215 /* look for ninja3 card and init if found */
1216 /*----------------------------------------------------------------*/
1217 static struct Scsi_Host *__nsp_detect(Scsi_Host_Template *sht)
1219 struct Scsi_Host *host; /* registered host structure */
1220 nsp_hw_data *data = &nsp_data;
1222 DEBUG(0, "%s: this_id=%d\n", __FUNCTION__, sht->this_id);
1224 request_region(data->BaseAddress, data->NumAddress, "nsp_cs");
1225 host = scsi_register(sht, 0);
1229 host->unique_id = data->BaseAddress;
1230 host->io_port = data->BaseAddress;
1231 host->n_io_port = data->NumAddress;
1232 host->irq = data->IrqNumber;
1233 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0))
1234 host->base = data->MmioAddress; /* kernel 2.4 */
1236 host->base = (char *)(data->MmioAddress); /* 2.2 */
1239 spin_lock_init(&(data->Lock));
1241 snprintf(data->nspinfo,
1242 sizeof(data->nspinfo),
1243 "NinjaSCSI-3/32Bi Driver $Revision: 1.1 $ IO:0x%04lx-0x%04lx MMIO(virt addr):0x%04lx IRQ:%02d",
1244 host->io_port, host->io_port + host->n_io_port - 1,
1247 data->nspinfo[sizeof(data->nspinfo) - 1] = '\0';
1248 sht->name = data->nspinfo;
1250 DEBUG(0, "%s: end\n", __FUNCTION__);
1252 //MOD_INC_USE_COUNT;
1257 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1258 static int nsp_detect(Scsi_Host_Template *sht)
1260 return (__nsp_detect(sht) != NULL);
1263 static int nsp_release(struct Scsi_Host *shpnt)
1265 //nsp_hw_data *data = &nsp_data;
1267 /* PCMCIA Card Service dose same things */
1269 // free_irq(shpnt->irq, data);
1271 //if (shpnt->io_port) {
1272 // release_region(shpnt->io_port, shpnt->n_io_port);
1275 //MOD_DEC_USE_COUNT;
1281 /*----------------------------------------------------------------*/
1282 /* return info string */
1283 /*----------------------------------------------------------------*/
1284 static const char *nsp_info(struct Scsi_Host *shpnt)
1286 nsp_hw_data *data = &nsp_data;
1288 return data->nspinfo;
1292 #define SPRINTF(args...) \
1293 do { if(pos < buffer + length) pos += sprintf(pos, ## args); } while(0)
1294 static int nsp_proc_info(struct Scsi_Host *host, char *buffer,
1304 unsigned long flags;
1305 nsp_hw_data *data = &nsp_data;
1311 SPRINTF("NinjaSCSI status\n\n");
1312 SPRINTF("Driver version: $Revision: 1.1 $\n");
1313 SPRINTF("SCSI host No.: %d\n", host->host_no);
1314 SPRINTF("IRQ: %d\n", host->irq);
1315 SPRINTF("IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
1316 SPRINTF("MMIO(virtual address): 0x%lx\n", host->base);
1317 SPRINTF("sg_tablesize: %d\n", host->sg_tablesize);
1319 SPRINTF("burst transfer mode: ");
1320 switch (nsp_burst_mode) {
1337 spin_lock_irqsave(&(data->Lock), flags);
1338 SPRINTF("CurrentSC: 0x%p\n\n", data->CurrentSC);
1339 spin_unlock_irqrestore(&(data->Lock), flags);
1341 SPRINTF("SDTR status\n");
1342 for(id = 0; id < N_TARGET; id++) {
1344 SPRINTF("id %d: ", id);
1346 if (id == host->this_id) {
1347 SPRINTF("----- NinjaSCSI-3 host adapter\n");
1351 switch(data->Sync[id].SyncNegotiation) {
1366 if (data->Sync[id].SyncPeriod != 0) {
1367 speed = 1000000 / (data->Sync[id].SyncPeriod * 4);
1369 SPRINTF(" transfer %d.%dMB/s, offset %d",
1372 data->Sync[id].SyncOffset
1378 thislength = pos - (buffer + offset);
1380 if(thislength < 0) {
1386 thislength = MIN(thislength, length);
1387 *start = buffer + offset;
1394 /*static int nsp_eh_strategy(struct Scsi_Host *Shost)
1400 static int nsp_eh_abort(Scsi_Cmnd *SCpnt)
1402 DEBUG(0, "%s: SCpnt=0x%p\n", __FUNCTION__, SCpnt);
1404 return nsp_eh_bus_reset(SCpnt);
1408 static int nsp_eh_device_reset(Scsi_Cmnd *SCpnt)
1410 DEBUG(0, "%s: SCpnt=0x%p\n", __FUNCTION__, SCpnt);
1415 static int nsp_eh_bus_reset(Scsi_Cmnd *SCpnt)
1417 nsp_hw_data *data = &nsp_data;
1418 unsigned int base = SCpnt->device->host->io_port;
1421 DEBUG(0, "%s: SCpnt=0x%p base=0x%x\n", __FUNCTION__, SCpnt, base);
1423 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
1425 nsp_index_write(base, SCSIBUSCTRL, SCSI_RST);
1426 mdelay(100); /* 100ms */
1427 nsp_index_write(base, SCSIBUSCTRL, 0);
1428 for(i = 0; i < 5; i++) {
1429 nsp_index_read(base, IRQPHASESENCE); /* dummy read */
1432 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
1434 nsphw_init_sync(data);
1439 static int nsp_eh_host_reset(Scsi_Cmnd *SCpnt)
1441 nsp_hw_data *data = &nsp_data;
1443 DEBUG(0, "%s:\n", __FUNCTION__);
1451 /**********************************************************************
1453 **********************************************************************/
1455 /*======================================================================
1456 nsp_cs_attach() creates an "instance" of the driver, allocating
1457 local data structures for one device. The device is registered
1460 The dev_link structure is initialized, but we don't actually
1461 configure the card at this point -- we wait until we receive a
1462 card insertion event.
1463 ======================================================================*/
1464 static dev_link_t *nsp_cs_attach(void)
1467 client_reg_t client_reg;
1471 DEBUG(0, "%s:\n", __FUNCTION__);
1473 /* Create new SCSI device */
1474 info = kmalloc(sizeof(*info), GFP_KERNEL);
1475 if (!info) { return NULL; }
1476 memset(info, 0, sizeof(*info));
1480 /* Initialize the dev_link_t structure */
1481 link->release.function = &nsp_cs_release;
1482 link->release.data = (u_long)link;
1484 /* The io structure describes IO port mapping */
1485 link->io.NumPorts1 = 0x10;
1486 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
1487 link->io.IOAddrLines = 10; /* not used */
1489 /* Interrupt setup */
1490 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1491 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
1492 if (irq_list[0] == -1) {
1493 link->irq.IRQInfo2 = irq_mask;
1495 for (i = 0; i < 4; i++) {
1496 link->irq.IRQInfo2 |= 1 << irq_list[i];
1500 /* IRQ
\e$B$N3NJ]$O$3$3$G
\e(B PCMCIA
\e$B$N4X?t$rMQ$$$F9T$J$&$N$G
\e(B
1501 * host->hostdata
\e$B$r
\e(B irq.Instance
\e$B$KBeF~$G$-$J$$!#
\e(B
1502 * host->hostdata
\e$B$,;H$($l$PJ#?t$N
\e(B NinjaSCSI
\e$B$,
\e(B
1503 *
\e$B;HMQ$G$-$k$N$@$,!#
\e(B
1505 link->irq.Handler = &nspintr;
1506 link->irq.Instance = &nsp_data;
1507 link->irq.Attributes |= (SA_SHIRQ | SA_SAMPLE_RANDOM);
1509 /* General socket configuration */
1510 link->conf.Attributes = CONF_ENABLE_IRQ;
1511 link->conf.Vcc = 50;
1512 link->conf.IntType = INT_MEMORY_AND_IO;
1513 link->conf.Present = PRESENT_OPTION;
1516 /* Register with Card Services */
1517 link->next = dev_list;
1519 client_reg.dev_info = &dev_info;
1520 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
1521 client_reg.EventMask =
1522 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
1523 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
1524 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME ;
1525 client_reg.event_handler = &nsp_cs_event;
1526 client_reg.Version = 0x0210;
1527 client_reg.event_callback_args.client_data = link;
1528 ret = CardServices(RegisterClient, &link->handle, &client_reg);
1529 if (ret != CS_SUCCESS) {
1530 cs_error(link->handle, RegisterClient, ret);
1531 nsp_cs_detach(link);
1536 } /* nsp_cs_attach */
1539 /*======================================================================
1540 This deletes a driver "instance". The device is de-registered
1541 with Card Services. If it has been released, all local data
1542 structures are freed. Otherwise, the structures will be freed
1543 when the device is released.
1544 ======================================================================*/
1545 static void nsp_cs_detach(dev_link_t *link)
1549 DEBUG(0, "%s(0x%p)\n", __FUNCTION__, link);
1551 /* Locate device structure */
1552 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) {
1553 if (*linkp == link) {
1557 if (*linkp == NULL) {
1561 del_timer(&link->release);
1562 if (link->state & DEV_CONFIG) {
1563 nsp_cs_release((u_long)link);
1564 if (link->state & DEV_STALE_CONFIG) {
1565 link->state |= DEV_STALE_LINK;
1570 /* Break the link with Card Services */
1572 CardServices(DeregisterClient, link->handle);
1575 /* Unlink device structure, free bits */
1576 *linkp = link->next;
1580 } /* nsp_cs_detach */
1583 /*======================================================================
1584 nsp_cs_config() is scheduled to run after a CARD_INSERTION event
1585 is received, to configure the PCMCIA socket, and to make the
1586 ethernet device available to the system.
1587 ======================================================================*/
1588 #define CS_CHECK(fn, args...) \
1589 while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
1590 #define CFG_CHECK(fn, args...) \
1591 if (CardServices(fn, args) != 0) goto next_entry
1592 /*====================================================================*/
1594 static void nsp_cs_config(dev_link_t *link)
1596 client_handle_t handle = link->handle;
1597 scsi_info_t *info = link->priv;
1600 int last_ret, last_fn;
1601 u_char tuple_data[64];
1605 cistpl_cftable_entry_t dflt = { 0 };
1607 struct Scsi_Host *host;
1608 nsp_hw_data *data = &nsp_data;
1611 DEBUG(0, "%s: in\n", __FUNCTION__);
1613 tuple.DesiredTuple = CISTPL_CONFIG;
1614 tuple.Attributes = 0;
1615 tuple.TupleData = tuple_data;
1616 tuple.TupleDataMax = sizeof(tuple_data);
1617 tuple.TupleOffset = 0;
1618 CS_CHECK(GetFirstTuple, handle, &tuple);
1619 CS_CHECK(GetTupleData, handle, &tuple);
1620 CS_CHECK(ParseTuple, handle, &tuple, &parse);
1621 link->conf.ConfigBase = parse.config.base;
1622 link->conf.Present = parse.config.rmask[0];
1624 /* Configure card */
1625 link->state |= DEV_CONFIG;
1627 /* Look up the current Vcc */
1628 CS_CHECK(GetConfigurationInfo, handle, &conf);
1629 link->conf.Vcc = conf.Vcc;
1631 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
1632 CS_CHECK(GetFirstTuple, handle, &tuple);
1634 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
1636 CFG_CHECK(GetTupleData, handle, &tuple);
1637 CFG_CHECK(ParseTuple, handle, &tuple, &parse);
1639 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) { dflt = *cfg; }
1640 if (cfg->index == 0) { goto next_entry; }
1641 link->conf.ConfigIndex = cfg->index;
1643 /* Does this card need audio output? */
1644 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
1645 link->conf.Attributes |= CONF_ENABLE_SPKR;
1646 link->conf.Status = CCSR_AUDIO_ENA;
1649 /* Use power settings for Vcc and Vpp if present */
1650 /* Note that the CIS values need to be rescaled */
1651 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
1652 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000) {
1655 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
1656 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM]/10000) {
1661 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM)) {
1662 link->conf.Vpp1 = link->conf.Vpp2 =
1663 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
1664 } else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM)) {
1665 link->conf.Vpp1 = link->conf.Vpp2 =
1666 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
1669 /* Do we need to allocate an interrupt? */
1670 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) {
1671 link->conf.Attributes |= CONF_ENABLE_IRQ;
1674 /* IO window settings */
1675 link->io.NumPorts1 = link->io.NumPorts2 = 0;
1676 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
1677 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
1678 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
1679 if (!(io->flags & CISTPL_IO_8BIT))
1680 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
1681 if (!(io->flags & CISTPL_IO_16BIT))
1682 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1683 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
1684 link->io.BasePort1 = io->win[0].base;
1685 link->io.NumPorts1 = io->win[0].len;
1687 link->io.Attributes2 = link->io.Attributes1;
1688 link->io.BasePort2 = io->win[1].base;
1689 link->io.NumPorts2 = io->win[1].len;
1691 /* This reserves IO space but doesn't actually enable it */
1692 CFG_CHECK(RequestIO, link->handle, &link->io);
1695 if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
1697 (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
1698 req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM;
1699 req.Attributes |= WIN_ENABLE;
1700 req.Base = mem->win[0].host_addr;
1701 req.Size = mem->win[0].len;
1702 if (req.Size < 0x1000)
1704 req.AccessSpeed = 0;
1705 link->win = (window_handle_t)link->handle;
1706 CFG_CHECK(RequestWindow, &link->win, &req);
1707 map.Page = 0; map.CardOffset = mem->win[0].card_addr;
1708 CFG_CHECK(MapMemPage, link->win, &map);
1710 data->MmioAddress = (u_long)ioremap_nocache(req.Base, req.Size);
1712 /* If we got this far, we're cool! */
1716 DEBUG(0, "%s: next\n", __FUNCTION__);
1718 if (link->io.NumPorts1)
1719 CardServices(ReleaseIO, link->handle, &link->io);
1720 CS_CHECK(GetNextTuple, handle, &tuple);
1723 if (link->conf.Attributes & CONF_ENABLE_IRQ)
1724 CS_CHECK(RequestIRQ, link->handle, &link->irq);
1725 CS_CHECK(RequestConfiguration, handle, &link->conf);
1728 if (link->io.BasePort1)
1729 release_region(link->io.BasePort1, link->io.NumPorts1);
1730 if (link->io.BasePort2)
1731 release_region(link->io.BasePort2, link->io.NumPorts2);
1734 /* Set port and IRQ */
1735 data->BaseAddress = link->io.BasePort1;
1736 data->NumAddress = link->io.NumPorts1;
1737 data->IrqNumber = link->irq.AssignedIRQ;
1739 DEBUG(0, "%s: I/O[0x%x+0x%x] IRQ %d\n",
1740 __FUNCTION__, data->BaseAddress, data->NumAddress, data->IrqNumber);
1742 if(nsphw_init(data) == FALSE) {
1746 host = __nsp_detect(&nsp_driver_template);
1750 sprintf(info->node.dev_name, "scsi%d", host->host_no);
1751 link->dev = &info->node;
1754 /* Finally, report what we've done */
1755 printk(KERN_INFO "nsp_cs: index 0x%02x: Vcc %d.%d",
1756 link->conf.ConfigIndex,
1757 link->conf.Vcc/10, link->conf.Vcc%10);
1758 if (link->conf.Vpp1) {
1759 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
1761 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
1762 printk(", irq %d", link->irq.AssignedIRQ);
1764 if (link->io.NumPorts1) {
1765 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
1766 link->io.BasePort1+link->io.NumPorts1-1);
1768 if (link->io.NumPorts2)
1769 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
1770 link->io.BasePort2+link->io.NumPorts2-1);
1772 printk(", mem 0x%06lx-0x%06lx", req.Base,
1773 req.Base+req.Size-1);
1776 scsi_add_host(host, NULL);
1777 link->state &= ~DEV_CONFIG_PENDING;
1781 cs_error(link->handle, last_fn, last_ret);
1782 nsp_cs_release((u_long)link);
1785 } /* nsp_cs_config */
1789 /*======================================================================
1790 After a card is removed, nsp_cs_release() will unregister the net
1791 device, and release the PCMCIA configuration. If the device is
1792 still open, this will be postponed until it is closed.
1793 ======================================================================*/
1794 static void nsp_cs_release(u_long arg)
1796 dev_link_t *link = (dev_link_t *)arg;
1797 scsi_info_t *info = link->priv;
1799 DEBUG(0, "%s(0x%p)\n", __FUNCTION__, link);
1802 * If the device is currently in use, we won't release until it
1803 * is actually closed.
1806 DEBUG(1, "nsp_cs: release postponed, '%s' still open\n",
1807 link->dev->dev_name);
1808 link->state |= DEV_STALE_CONFIG;
1812 /* Unlink the device chain */
1813 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,2))
1814 scsi_unregister_module(MODULE_SCSI_HA, &nsp_driver_template);
1816 scsi_remove_host(info->host);
1817 scsi_unregister(info->host);
1822 iounmap((void *)(nsp_data.MmioAddress));
1823 CardServices(ReleaseWindow, link->win);
1825 CardServices(ReleaseConfiguration, link->handle);
1826 if (link->io.NumPorts1) {
1827 CardServices(ReleaseIO, link->handle, &link->io);
1829 if (link->irq.AssignedIRQ) {
1830 CardServices(ReleaseIRQ, link->handle, &link->irq);
1832 link->state &= ~DEV_CONFIG;
1834 if (link->state & DEV_STALE_LINK) {
1835 nsp_cs_detach(link);
1837 } /* nsp_cs_release */
1839 /*======================================================================
1841 The card status event handler. Mostly, this schedules other
1842 stuff to run after an event is received. A CARD_REMOVAL event
1843 also sets some flags to discourage the net drivers from trying
1844 to talk to the card any more.
1846 When a CARD_REMOVAL event is received, we immediately set a flag
1847 to block future accesses to this device. All the functions that
1848 actually access the device should check this flag to make sure
1849 the card is still present.
1851 ======================================================================*/
1852 static int nsp_cs_event(event_t event,
1854 event_callback_args_t *args)
1856 dev_link_t *link = args->client_data;
1857 scsi_info_t *info = link->priv;
1860 DEBUG(1, "%s(0x%06x)\n", __FUNCTION__, event);
1863 case CS_EVENT_CARD_REMOVAL:
1864 DEBUG(0, " event: remove\n");
1865 link->state &= ~DEV_PRESENT;
1866 if (link->state & DEV_CONFIG) {
1867 ((scsi_info_t *)link->priv)->stop = 1;
1868 mod_timer(&link->release, jiffies + HZ/20);
1872 case CS_EVENT_CARD_INSERTION:
1873 DEBUG(0, " event: insert\n");
1874 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1875 nsp_cs_config(link);
1878 case CS_EVENT_PM_SUSPEND:
1879 link->state |= DEV_SUSPEND;
1880 /* Fall through... */
1881 case CS_EVENT_RESET_PHYSICAL:
1882 /* Mark the device as stopped, to block IO until later */
1884 if (link->state & DEV_CONFIG) {
1885 CardServices(ReleaseConfiguration, link->handle);
1889 case CS_EVENT_PM_RESUME:
1890 link->state &= ~DEV_SUSPEND;
1891 /* Fall through... */
1892 case CS_EVENT_CARD_RESET:
1893 DEBUG(0, " event: reset\n");
1894 if (link->state & DEV_CONFIG) {
1895 CardServices(RequestConfiguration, link->handle, &link->conf);
1899 tmp.device->host = info->host;
1900 nsp_eh_host_reset(&tmp);
1901 nsp_eh_bus_reset(&tmp);
1906 DEBUG(0, " event: unknown\n");
1909 DEBUG(0, "%s: end\n", __FUNCTION__);
1911 } /* nsp_cs_event */
1913 static struct pcmcia_driver nsp_driver = {
1914 .owner = THIS_MODULE,
1918 .attach = nsp_cs_attach,
1919 .detach = nsp_cs_detach,
1922 static int __init nsp_cs_init(void)
1924 return pcmcia_register_driver(&nsp_driver);
1928 static void __exit nsp_cs_exit(void)
1930 pcmcia_unregister_driver(&nsp_driver);
1932 /* XXX: this really needs to move into generic code.. */
1933 while (dev_list != NULL) {
1934 if (dev_list->state & DEV_CONFIG) {
1935 nsp_cs_release((u_long)dev_list);
1937 nsp_cs_detach(dev_list);
1941 module_init(nsp_cs_init)
1942 module_exit(nsp_cs_exit)