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$");
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$ 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(char *buffer
,
1305 unsigned long flags
;
1306 nsp_hw_data
*data
= &nsp_data
;
1307 struct Scsi_Host
*host
= NULL
;
1313 /* search this HBA host */
1314 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,45))
1315 host
= scsi_host_hn_get(hostno
);
1317 for (host
=scsi_hostlist
; host
; host
=host
->next
) {
1318 if (host
->host_no
== hostno
) {
1327 SPRINTF("NinjaSCSI status\n\n");
1328 SPRINTF("Driver version: $Revision$\n");
1329 SPRINTF("SCSI host No.: %d\n", hostno
);
1330 SPRINTF("IRQ: %d\n", host
->irq
);
1331 SPRINTF("IO: 0x%lx-0x%lx\n", host
->io_port
, host
->io_port
+ host
->n_io_port
- 1);
1332 SPRINTF("MMIO(virtual address): 0x%lx\n", host
->base
);
1333 SPRINTF("sg_tablesize: %d\n", host
->sg_tablesize
);
1335 SPRINTF("burst transfer mode: ");
1336 switch (nsp_burst_mode
) {
1353 spin_lock_irqsave(&(data
->Lock
), flags
);
1354 SPRINTF("CurrentSC: 0x%p\n\n", data
->CurrentSC
);
1355 spin_unlock_irqrestore(&(data
->Lock
), flags
);
1357 SPRINTF("SDTR status\n");
1358 for(id
= 0; id
< N_TARGET
; id
++) {
1360 SPRINTF("id %d: ", id
);
1362 if (id
== host
->this_id
) {
1363 SPRINTF("----- NinjaSCSI-3 host adapter\n");
1367 switch(data
->Sync
[id
].SyncNegotiation
) {
1382 if (data
->Sync
[id
].SyncPeriod
!= 0) {
1383 speed
= 1000000 / (data
->Sync
[id
].SyncPeriod
* 4);
1385 SPRINTF(" transfer %d.%dMB/s, offset %d",
1388 data
->Sync
[id
].SyncOffset
1394 thislength
= pos
- (buffer
+ offset
);
1396 if(thislength
< 0) {
1402 thislength
= MIN(thislength
, length
);
1403 *start
= buffer
+ offset
;
1410 /*static int nsp_eh_strategy(struct Scsi_Host *Shost)
1416 static int nsp_eh_abort(Scsi_Cmnd *SCpnt)
1418 DEBUG(0, "%s: SCpnt=0x%p\n", __FUNCTION__, SCpnt);
1420 return nsp_eh_bus_reset(SCpnt);
1424 static int nsp_eh_device_reset(Scsi_Cmnd *SCpnt)
1426 DEBUG(0, "%s: SCpnt=0x%p\n", __FUNCTION__, SCpnt);
1431 static int nsp_eh_bus_reset(Scsi_Cmnd
*SCpnt
)
1433 nsp_hw_data
*data
= &nsp_data
;
1434 unsigned int base
= SCpnt
->device
->host
->io_port
;
1437 DEBUG(0, "%s: SCpnt=0x%p base=0x%x\n", __FUNCTION__
, SCpnt
, base
);
1439 nsp_write(base
, IRQCONTROL
, IRQCONTROL_ALLMASK
);
1441 nsp_index_write(base
, SCSIBUSCTRL
, SCSI_RST
);
1442 mdelay(100); /* 100ms */
1443 nsp_index_write(base
, SCSIBUSCTRL
, 0);
1444 for(i
= 0; i
< 5; i
++) {
1445 nsp_index_read(base
, IRQPHASESENCE
); /* dummy read */
1448 nsp_write(base
, IRQCONTROL
, IRQCONTROL_ALLCLEAR
);
1450 nsphw_init_sync(data
);
1455 static int nsp_eh_host_reset(Scsi_Cmnd
*SCpnt
)
1457 nsp_hw_data
*data
= &nsp_data
;
1459 DEBUG(0, "%s:\n", __FUNCTION__
);
1467 /**********************************************************************
1469 **********************************************************************/
1471 /*======================================================================
1472 nsp_cs_attach() creates an "instance" of the driver, allocating
1473 local data structures for one device. The device is registered
1476 The dev_link structure is initialized, but we don't actually
1477 configure the card at this point -- we wait until we receive a
1478 card insertion event.
1479 ======================================================================*/
1480 static dev_link_t
*nsp_cs_attach(void)
1483 client_reg_t client_reg
;
1487 DEBUG(0, "%s:\n", __FUNCTION__
);
1489 /* Create new SCSI device */
1490 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
1491 if (!info
) { return NULL
; }
1492 memset(info
, 0, sizeof(*info
));
1496 /* Initialize the dev_link_t structure */
1497 link
->release
.function
= &nsp_cs_release
;
1498 link
->release
.data
= (u_long
)link
;
1500 /* The io structure describes IO port mapping */
1501 link
->io
.NumPorts1
= 0x10;
1502 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_AUTO
;
1503 link
->io
.IOAddrLines
= 10; /* not used */
1505 /* Interrupt setup */
1506 link
->irq
.Attributes
= IRQ_TYPE_EXCLUSIVE
| IRQ_HANDLE_PRESENT
;
1507 link
->irq
.IRQInfo1
= IRQ_INFO2_VALID
| IRQ_LEVEL_ID
;
1508 if (irq_list
[0] == -1) {
1509 link
->irq
.IRQInfo2
= irq_mask
;
1511 for (i
= 0; i
< 4; i
++) {
1512 link
->irq
.IRQInfo2
|= 1 << irq_list
[i
];
1516 /* IRQ \e$B$N3NJ]$O$3$3$G\e(B PCMCIA \e$B$N4X?t$rMQ$$$F9T$J$&$N$G\e(B
1517 * host->hostdata \e$B$r\e(B irq.Instance \e$B$KBeF~$G$-$J$$!#\e(B
1518 * host->hostdata \e$B$,;H$($l$PJ#?t$N\e(B NinjaSCSI \e$B$,\e(B
1519 * \e$B;HMQ$G$-$k$N$@$,!#\e(B
1521 link
->irq
.Handler
= &nspintr
;
1522 link
->irq
.Instance
= &nsp_data
;
1523 link
->irq
.Attributes
|= (SA_SHIRQ
| SA_SAMPLE_RANDOM
);
1525 /* General socket configuration */
1526 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
1527 link
->conf
.Vcc
= 50;
1528 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
1529 link
->conf
.Present
= PRESENT_OPTION
;
1532 /* Register with Card Services */
1533 link
->next
= dev_list
;
1535 client_reg
.dev_info
= &dev_info
;
1536 client_reg
.Attributes
= INFO_IO_CLIENT
| INFO_CARD_SHARE
;
1537 client_reg
.EventMask
=
1538 CS_EVENT_CARD_INSERTION
| CS_EVENT_CARD_REMOVAL
|
1539 CS_EVENT_RESET_PHYSICAL
| CS_EVENT_CARD_RESET
|
1540 CS_EVENT_PM_SUSPEND
| CS_EVENT_PM_RESUME
;
1541 client_reg
.event_handler
= &nsp_cs_event
;
1542 client_reg
.Version
= 0x0210;
1543 client_reg
.event_callback_args
.client_data
= link
;
1544 ret
= CardServices(RegisterClient
, &link
->handle
, &client_reg
);
1545 if (ret
!= CS_SUCCESS
) {
1546 cs_error(link
->handle
, RegisterClient
, ret
);
1547 nsp_cs_detach(link
);
1552 } /* nsp_cs_attach */
1555 /*======================================================================
1556 This deletes a driver "instance". The device is de-registered
1557 with Card Services. If it has been released, all local data
1558 structures are freed. Otherwise, the structures will be freed
1559 when the device is released.
1560 ======================================================================*/
1561 static void nsp_cs_detach(dev_link_t
*link
)
1565 DEBUG(0, "%s(0x%p)\n", __FUNCTION__
, link
);
1567 /* Locate device structure */
1568 for (linkp
= &dev_list
; *linkp
; linkp
= &(*linkp
)->next
) {
1569 if (*linkp
== link
) {
1573 if (*linkp
== NULL
) {
1577 del_timer(&link
->release
);
1578 if (link
->state
& DEV_CONFIG
) {
1579 nsp_cs_release((u_long
)link
);
1580 if (link
->state
& DEV_STALE_CONFIG
) {
1581 link
->state
|= DEV_STALE_LINK
;
1586 /* Break the link with Card Services */
1588 CardServices(DeregisterClient
, link
->handle
);
1591 /* Unlink device structure, free bits */
1592 *linkp
= link
->next
;
1596 } /* nsp_cs_detach */
1599 /*======================================================================
1600 nsp_cs_config() is scheduled to run after a CARD_INSERTION event
1601 is received, to configure the PCMCIA socket, and to make the
1602 ethernet device available to the system.
1603 ======================================================================*/
1604 #define CS_CHECK(fn, args...) \
1605 while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
1606 #define CFG_CHECK(fn, args...) \
1607 if (CardServices(fn, args) != 0) goto next_entry
1608 /*====================================================================*/
1610 static void nsp_cs_config(dev_link_t
*link
)
1612 client_handle_t handle
= link
->handle
;
1613 scsi_info_t
*info
= link
->priv
;
1616 int last_ret
, last_fn
;
1617 u_char tuple_data
[64];
1621 cistpl_cftable_entry_t dflt
= { 0 };
1623 struct Scsi_Host
*host
;
1624 nsp_hw_data
*data
= &nsp_data
;
1627 DEBUG(0, "%s: in\n", __FUNCTION__
);
1629 tuple
.DesiredTuple
= CISTPL_CONFIG
;
1630 tuple
.Attributes
= 0;
1631 tuple
.TupleData
= tuple_data
;
1632 tuple
.TupleDataMax
= sizeof(tuple_data
);
1633 tuple
.TupleOffset
= 0;
1634 CS_CHECK(GetFirstTuple
, handle
, &tuple
);
1635 CS_CHECK(GetTupleData
, handle
, &tuple
);
1636 CS_CHECK(ParseTuple
, handle
, &tuple
, &parse
);
1637 link
->conf
.ConfigBase
= parse
.config
.base
;
1638 link
->conf
.Present
= parse
.config
.rmask
[0];
1640 /* Configure card */
1641 link
->state
|= DEV_CONFIG
;
1643 /* Look up the current Vcc */
1644 CS_CHECK(GetConfigurationInfo
, handle
, &conf
);
1645 link
->conf
.Vcc
= conf
.Vcc
;
1647 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
1648 CS_CHECK(GetFirstTuple
, handle
, &tuple
);
1650 cistpl_cftable_entry_t
*cfg
= &(parse
.cftable_entry
);
1652 CFG_CHECK(GetTupleData
, handle
, &tuple
);
1653 CFG_CHECK(ParseTuple
, handle
, &tuple
, &parse
);
1655 if (cfg
->flags
& CISTPL_CFTABLE_DEFAULT
) { dflt
= *cfg
; }
1656 if (cfg
->index
== 0) { goto next_entry
; }
1657 link
->conf
.ConfigIndex
= cfg
->index
;
1659 /* Does this card need audio output? */
1660 if (cfg
->flags
& CISTPL_CFTABLE_AUDIO
) {
1661 link
->conf
.Attributes
|= CONF_ENABLE_SPKR
;
1662 link
->conf
.Status
= CCSR_AUDIO_ENA
;
1665 /* Use power settings for Vcc and Vpp if present */
1666 /* Note that the CIS values need to be rescaled */
1667 if (cfg
->vcc
.present
& (1<<CISTPL_POWER_VNOM
)) {
1668 if (conf
.Vcc
!= cfg
->vcc
.param
[CISTPL_POWER_VNOM
]/10000) {
1671 } else if (dflt
.vcc
.present
& (1<<CISTPL_POWER_VNOM
)) {
1672 if (conf
.Vcc
!= dflt
.vcc
.param
[CISTPL_POWER_VNOM
]/10000) {
1677 if (cfg
->vpp1
.present
& (1<<CISTPL_POWER_VNOM
)) {
1678 link
->conf
.Vpp1
= link
->conf
.Vpp2
=
1679 cfg
->vpp1
.param
[CISTPL_POWER_VNOM
]/10000;
1680 } else if (dflt
.vpp1
.present
& (1<<CISTPL_POWER_VNOM
)) {
1681 link
->conf
.Vpp1
= link
->conf
.Vpp2
=
1682 dflt
.vpp1
.param
[CISTPL_POWER_VNOM
]/10000;
1685 /* Do we need to allocate an interrupt? */
1686 if (cfg
->irq
.IRQInfo1
|| dflt
.irq
.IRQInfo1
) {
1687 link
->conf
.Attributes
|= CONF_ENABLE_IRQ
;
1690 /* IO window settings */
1691 link
->io
.NumPorts1
= link
->io
.NumPorts2
= 0;
1692 if ((cfg
->io
.nwin
> 0) || (dflt
.io
.nwin
> 0)) {
1693 cistpl_io_t
*io
= (cfg
->io
.nwin
) ? &cfg
->io
: &dflt
.io
;
1694 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_AUTO
;
1695 if (!(io
->flags
& CISTPL_IO_8BIT
))
1696 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_16
;
1697 if (!(io
->flags
& CISTPL_IO_16BIT
))
1698 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_8
;
1699 link
->io
.IOAddrLines
= io
->flags
& CISTPL_IO_LINES_MASK
;
1700 link
->io
.BasePort1
= io
->win
[0].base
;
1701 link
->io
.NumPorts1
= io
->win
[0].len
;
1703 link
->io
.Attributes2
= link
->io
.Attributes1
;
1704 link
->io
.BasePort2
= io
->win
[1].base
;
1705 link
->io
.NumPorts2
= io
->win
[1].len
;
1707 /* This reserves IO space but doesn't actually enable it */
1708 CFG_CHECK(RequestIO
, link
->handle
, &link
->io
);
1711 if ((cfg
->mem
.nwin
> 0) || (dflt
.mem
.nwin
> 0)) {
1713 (cfg
->mem
.nwin
) ? &cfg
->mem
: &dflt
.mem
;
1714 req
.Attributes
= WIN_DATA_WIDTH_16
|WIN_MEMORY_TYPE_CM
;
1715 req
.Attributes
|= WIN_ENABLE
;
1716 req
.Base
= mem
->win
[0].host_addr
;
1717 req
.Size
= mem
->win
[0].len
;
1718 if (req
.Size
< 0x1000)
1720 req
.AccessSpeed
= 0;
1721 link
->win
= (window_handle_t
)link
->handle
;
1722 CFG_CHECK(RequestWindow
, &link
->win
, &req
);
1723 map
.Page
= 0; map
.CardOffset
= mem
->win
[0].card_addr
;
1724 CFG_CHECK(MapMemPage
, link
->win
, &map
);
1726 data
->MmioAddress
= (u_long
)ioremap_nocache(req
.Base
, req
.Size
);
1728 /* If we got this far, we're cool! */
1732 DEBUG(0, "%s: next\n", __FUNCTION__
);
1734 if (link
->io
.NumPorts1
)
1735 CardServices(ReleaseIO
, link
->handle
, &link
->io
);
1736 CS_CHECK(GetNextTuple
, handle
, &tuple
);
1739 if (link
->conf
.Attributes
& CONF_ENABLE_IRQ
)
1740 CS_CHECK(RequestIRQ
, link
->handle
, &link
->irq
);
1741 CS_CHECK(RequestConfiguration
, handle
, &link
->conf
);
1744 if (link
->io
.BasePort1
)
1745 release_region(link
->io
.BasePort1
, link
->io
.NumPorts1
);
1746 if (link
->io
.BasePort2
)
1747 release_region(link
->io
.BasePort2
, link
->io
.NumPorts2
);
1750 /* Set port and IRQ */
1751 data
->BaseAddress
= link
->io
.BasePort1
;
1752 data
->NumAddress
= link
->io
.NumPorts1
;
1753 data
->IrqNumber
= link
->irq
.AssignedIRQ
;
1755 DEBUG(0, "%s: I/O[0x%x+0x%x] IRQ %d\n",
1756 __FUNCTION__
, data
->BaseAddress
, data
->NumAddress
, data
->IrqNumber
);
1758 if(nsphw_init(data
) == FALSE
) {
1762 host
= __nsp_detect(&nsp_driver_template
);
1766 sprintf(info
->node
.dev_name
, "scsi%d", host
->host_no
);
1767 link
->dev
= &info
->node
;
1770 /* Finally, report what we've done */
1771 printk(KERN_INFO
"nsp_cs: index 0x%02x: Vcc %d.%d",
1772 link
->conf
.ConfigIndex
,
1773 link
->conf
.Vcc
/10, link
->conf
.Vcc
%10);
1774 if (link
->conf
.Vpp1
) {
1775 printk(", Vpp %d.%d", link
->conf
.Vpp1
/10, link
->conf
.Vpp1
%10);
1777 if (link
->conf
.Attributes
& CONF_ENABLE_IRQ
) {
1778 printk(", irq %d", link
->irq
.AssignedIRQ
);
1780 if (link
->io
.NumPorts1
) {
1781 printk(", io 0x%04x-0x%04x", link
->io
.BasePort1
,
1782 link
->io
.BasePort1
+link
->io
.NumPorts1
-1);
1784 if (link
->io
.NumPorts2
)
1785 printk(" & 0x%04x-0x%04x", link
->io
.BasePort2
,
1786 link
->io
.BasePort2
+link
->io
.NumPorts2
-1);
1788 printk(", mem 0x%06lx-0x%06lx", req
.Base
,
1789 req
.Base
+req
.Size
-1);
1792 scsi_add_host(host
, NULL
);
1793 link
->state
&= ~DEV_CONFIG_PENDING
;
1797 cs_error(link
->handle
, last_fn
, last_ret
);
1798 nsp_cs_release((u_long
)link
);
1801 } /* nsp_cs_config */
1805 /*======================================================================
1806 After a card is removed, nsp_cs_release() will unregister the net
1807 device, and release the PCMCIA configuration. If the device is
1808 still open, this will be postponed until it is closed.
1809 ======================================================================*/
1810 static void nsp_cs_release(u_long arg
)
1812 dev_link_t
*link
= (dev_link_t
*)arg
;
1813 scsi_info_t
*info
= link
->priv
;
1815 DEBUG(0, "%s(0x%p)\n", __FUNCTION__
, link
);
1818 * If the device is currently in use, we won't release until it
1819 * is actually closed.
1822 DEBUG(1, "nsp_cs: release postponed, '%s' still open\n",
1823 link
->dev
->dev_name
);
1824 link
->state
|= DEV_STALE_CONFIG
;
1828 /* Unlink the device chain */
1829 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,2))
1830 scsi_unregister_module(MODULE_SCSI_HA
, &nsp_driver_template
);
1832 scsi_remove_host(info
->host
);
1833 scsi_unregister(info
->host
);
1838 iounmap((void *)(nsp_data
.MmioAddress
));
1839 CardServices(ReleaseWindow
, link
->win
);
1841 CardServices(ReleaseConfiguration
, link
->handle
);
1842 if (link
->io
.NumPorts1
) {
1843 CardServices(ReleaseIO
, link
->handle
, &link
->io
);
1845 if (link
->irq
.AssignedIRQ
) {
1846 CardServices(ReleaseIRQ
, link
->handle
, &link
->irq
);
1848 link
->state
&= ~DEV_CONFIG
;
1850 if (link
->state
& DEV_STALE_LINK
) {
1851 nsp_cs_detach(link
);
1853 } /* nsp_cs_release */
1855 /*======================================================================
1857 The card status event handler. Mostly, this schedules other
1858 stuff to run after an event is received. A CARD_REMOVAL event
1859 also sets some flags to discourage the net drivers from trying
1860 to talk to the card any more.
1862 When a CARD_REMOVAL event is received, we immediately set a flag
1863 to block future accesses to this device. All the functions that
1864 actually access the device should check this flag to make sure
1865 the card is still present.
1867 ======================================================================*/
1868 static int nsp_cs_event(event_t event
,
1870 event_callback_args_t
*args
)
1872 dev_link_t
*link
= args
->client_data
;
1873 scsi_info_t
*info
= link
->priv
;
1876 DEBUG(1, "%s(0x%06x)\n", __FUNCTION__
, event
);
1879 case CS_EVENT_CARD_REMOVAL
:
1880 DEBUG(0, " event: remove\n");
1881 link
->state
&= ~DEV_PRESENT
;
1882 if (link
->state
& DEV_CONFIG
) {
1883 ((scsi_info_t
*)link
->priv
)->stop
= 1;
1884 mod_timer(&link
->release
, jiffies
+ HZ
/20);
1888 case CS_EVENT_CARD_INSERTION
:
1889 DEBUG(0, " event: insert\n");
1890 link
->state
|= DEV_PRESENT
| DEV_CONFIG_PENDING
;
1891 nsp_cs_config(link
);
1894 case CS_EVENT_PM_SUSPEND
:
1895 link
->state
|= DEV_SUSPEND
;
1896 /* Fall through... */
1897 case CS_EVENT_RESET_PHYSICAL
:
1898 /* Mark the device as stopped, to block IO until later */
1900 if (link
->state
& DEV_CONFIG
) {
1901 CardServices(ReleaseConfiguration
, link
->handle
);
1905 case CS_EVENT_PM_RESUME
:
1906 link
->state
&= ~DEV_SUSPEND
;
1907 /* Fall through... */
1908 case CS_EVENT_CARD_RESET
:
1909 DEBUG(0, " event: reset\n");
1910 if (link
->state
& DEV_CONFIG
) {
1911 CardServices(RequestConfiguration
, link
->handle
, &link
->conf
);
1915 tmp
.device
->host
= info
->host
;
1916 nsp_eh_host_reset(&tmp
);
1917 nsp_eh_bus_reset(&tmp
);
1922 DEBUG(0, " event: unknown\n");
1925 DEBUG(0, "%s: end\n", __FUNCTION__
);
1927 } /* nsp_cs_event */
1929 static struct pcmcia_driver nsp_driver
= {
1930 .owner
= THIS_MODULE
,
1934 .attach
= nsp_cs_attach
,
1935 .detach
= nsp_cs_detach
,
1938 static int __init
nsp_cs_init(void)
1940 return pcmcia_register_driver(&nsp_driver
);
1944 static void __exit
nsp_cs_exit(void)
1946 pcmcia_unregister_driver(&nsp_driver
);
1948 /* XXX: this really needs to move into generic code.. */
1949 while (dev_list
!= NULL
) {
1950 if (dev_list
->state
& DEV_CONFIG
) {
1951 nsp_cs_release((u_long
)dev_list
);
1953 nsp_cs_detach(dev_list
);
1957 module_init(nsp_cs_init
)
1958 module_exit(nsp_cs_exit
)