28 #include <linux/module.h>
29 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
39 #include <linux/stat.h>
44 #include <../drivers/scsi/scsi.h>
47 #include <scsi/scsi.h>
57 MODULE_DESCRIPTION(
"WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module");
70 MODULE_PARM_DESC(nsp_burst_mode,
"Burst transfer mode (0=io8, 1=io32, 2=mem32(default))");
73 static bool free_ports = 0;
75 MODULE_PARM_DESC(free_ports,
"Release IO ports after configuration? (default: 0 (=no))");
78 .proc_name =
"nsp_cs",
79 .proc_info = nsp_proc_info,
80 .name =
"WorkBit NinjaSCSI-3/32Bi(16bit)",
82 .queuecommand = nsp_queuecommand,
84 .eh_bus_reset_handler = nsp_eh_bus_reset,
85 .eh_host_reset_handler = nsp_eh_host_reset,
101 # define NSP_DEBUG_MASK 0x000000
102 # define nsp_msg(type, args...) nsp_cs_message("", 0, (type), args)
103 # define nsp_dbg(mask, args...)
105 # define NSP_DEBUG_MASK 0xffffff
106 # define nsp_msg(type, args...) \
107 nsp_cs_message (__func__, __LINE__, (type), args)
108 # define nsp_dbg(mask, args...) \
109 nsp_cs_dmessage(__func__, __LINE__, (mask), args)
112 #define NSP_DEBUG_QUEUECOMMAND BIT(0)
113 #define NSP_DEBUG_REGISTER BIT(1)
114 #define NSP_DEBUG_AUTOSCSI BIT(2)
115 #define NSP_DEBUG_INTR BIT(3)
116 #define NSP_DEBUG_SGLIST BIT(4)
117 #define NSP_DEBUG_BUSFREE BIT(5)
118 #define NSP_DEBUG_CDB_CONTENTS BIT(6)
119 #define NSP_DEBUG_RESELECTION BIT(7)
120 #define NSP_DEBUG_MSGINOCCUR BIT(8)
121 #define NSP_DEBUG_EEPROM BIT(9)
122 #define NSP_DEBUG_MSGOUTOCCUR BIT(10)
123 #define NSP_DEBUG_BUSRESET BIT(11)
124 #define NSP_DEBUG_RESTART BIT(12)
125 #define NSP_DEBUG_SYNC BIT(13)
126 #define NSP_DEBUG_WAIT BIT(14)
127 #define NSP_DEBUG_TARGETFLAG BIT(15)
128 #define NSP_DEBUG_PROC BIT(16)
129 #define NSP_DEBUG_INIT BIT(17)
130 #define NSP_DEBUG_DATA_IO BIT(18)
131 #define NSP_SPECIAL_PRINT_REGISTER BIT(20)
133 #define NSP_DEBUG_BUF_LEN 150
135 static inline void nsp_inc_resid(
struct scsi_cmnd *SCpnt,
int residInc)
137 scsi_set_resid(SCpnt, scsi_get_resid(SCpnt) + residInc);
140 static void nsp_cs_message(
const char *
func,
int line,
char *
type,
char *
fmt, ...)
150 printk(
"%snsp_cs: %s\n", type, buf);
152 printk(
"%snsp_cs: %s (%d): %s\n", type, func, line, buf);
157 static void nsp_cs_dmessage(
const char *func,
int line,
int mask,
char *fmt, ...)
167 printk(
"nsp_cs-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
178 static void nsp_scsi_done(
struct scsi_cmnd *SCpnt)
187 static int nsp_queuecommand_lck(
struct scsi_cmnd *SCpnt,
198 "SCpnt=0x%p target=%d lun=%d sglist=0x%p bufflen=%d sg_count=%d",
199 SCpnt, target, SCpnt->
device->lun, scsi_sglist(SCpnt),
200 scsi_bufflen(SCpnt), scsi_sg_count(SCpnt));
208 nsp_scsi_done(SCpnt);
218 nsp_scsi_done(SCpnt);
228 SCpnt->
SCp.Message = 0;
230 SCpnt->
SCp.sent_command = 0;
232 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
240 if (scsi_bufflen(SCpnt)) {
241 SCpnt->
SCp.buffer = scsi_sglist(SCpnt);
243 SCpnt->
SCp.this_residual = SCpnt->
SCp.buffer->length;
244 SCpnt->
SCp.buffers_residual = scsi_sg_count(SCpnt) - 1;
247 SCpnt->
SCp.this_residual = 0;
249 SCpnt->
SCp.buffers_residual = 0;
252 if (nsphw_start_selection(SCpnt) ==
FALSE) {
255 nsp_scsi_done(SCpnt);
274 unsigned int base = data->BaseAddress;
275 unsigned char transfer_mode_reg;
279 if (enabled !=
FALSE) {
282 transfer_mode_reg = 0;
285 transfer_mode_reg |= data->TransferMode;
300 data->
Sync[
i] = tmp_sync;
318 nsphw_init_sync(data);
348 nsp_index_write(base,
SYNCREG, 0);
357 nsp_setup_fifo(data,
FALSE);
365 static int nsphw_start_selection(
struct scsi_cmnd *SCpnt)
368 unsigned int base = SCpnt->
device->host->io_port;
369 unsigned char target =
scmd_id(SCpnt);
372 unsigned char phase, arbit;
415 nsp_start_timer(SCpnt, 1000/51);
429 {0x0c, 0x0c, 0x1, 0},
430 {0x19, 0x19, 0x3, 1},
431 {0x1a, 0x25, 0x5, 2},
432 {0x26, 0x32, 0x7, 3},
437 {0x19, 0x19, 0x1, 0},
438 {0x1a, 0x25, 0x2, 0},
439 {0x26, 0x32, 0x3, 1},
446 static int nsp_analyze_sdtr(
struct scsi_cmnd *SCpnt)
448 unsigned char target =
scmd_id(SCpnt);
459 period =
sync->SyncPeriod;
460 offset =
sync->SyncOffset;
465 sync_table = nsp_sync_table_20M;
467 sync_table = nsp_sync_table_40M;
470 for ( i = 0; sync_table->
max_period != 0; i++, sync_table++) {
477 if (period != 0 && sync_table->
max_period == 0) {
483 sync->SyncPeriod = 0;
484 sync->SyncOffset = 0;
485 sync->SyncRegister = 0;
504 static void nsp_start_timer(
struct scsi_cmnd *SCpnt,
int time)
506 unsigned int base = SCpnt->
device->host->io_port;
517 static int nsp_negate_signal(
struct scsi_cmnd *SCpnt,
unsigned char mask,
520 unsigned int base = SCpnt->
device->host->io_port;
533 }
while ((--time_out != 0) && (reg & mask) != 0);
545 static int nsp_expect_signal(
struct scsi_cmnd *SCpnt,
546 unsigned char current_phase,
549 unsigned int base = SCpnt->
device->host->io_port;
551 unsigned char phase, i_src;
571 }
while(time_out-- != 0);
580 static int nsp_xfer(
struct scsi_cmnd *SCpnt,
int phase)
582 unsigned int base = SCpnt->
device->host->io_port;
590 for (ptr = 0;
len > 0;
len--, ptr++) {
592 ret = nsp_expect_signal(SCpnt, phase,
BUSMON_REQ);
611 nsp_negate_signal(SCpnt,
BUSMON_ACK,
"xfer<ack>");
620 static int nsp_dataphase_bypass(
struct scsi_cmnd *SCpnt)
631 count = nsp_fifo_count(SCpnt);
644 nsp_setup_fifo(data,
FALSE);
652 static int nsp_reselected(
struct scsi_cmnd *SCpnt)
654 unsigned int base = SCpnt->
device->host->io_port;
655 unsigned int host_id = SCpnt->
device->host->this_id;
657 unsigned char bus_reg;
658 unsigned char id_reg,
tmp;
664 tmp = id_reg & (~
BIT(host_id));
675 nsp_msg(
KERN_ERR,
"XXX: reselect ID must be %d in this implementation.", target);
678 nsp_negate_signal(SCpnt,
BUSMON_SEL,
"reselect<SEL>");
691 static int nsp_fifo_count(
struct scsi_cmnd *SCpnt)
693 unsigned int base = SCpnt->
device->host->io_port;
704 count = (h << 16) | (m << 8) | (l << 0);
712 #define RFIFO_CRIT 64
713 #define WFIFO_CRIT 64
718 static void nsp_pio_read(
struct scsi_cmnd *SCpnt)
720 unsigned int base = SCpnt->
device->host->io_port;
725 unsigned char stat, fifo_stat;
730 SCpnt, scsi_get_resid(SCpnt), ocount, SCpnt->
SCp.ptr,
731 SCpnt->
SCp.this_residual, SCpnt->
SCp.buffer,
732 SCpnt->
SCp.buffers_residual);
736 while ((time_out-- != 0) &&
737 (SCpnt->
SCp.this_residual > 0 || SCpnt->
SCp.buffers_residual > 0 ) ) {
743 res = nsp_fifo_count(SCpnt) - ocount;
766 nsp_fifo32_read(base, SCpnt->
SCp.ptr,
res >> 2);
769 nsp_fifo8_read (base, SCpnt->
SCp.ptr,
res );
774 nsp_mmio_fifo32_read(mmio_base, SCpnt->
SCp.ptr,
res >> 2);
782 nsp_inc_resid(SCpnt, -
res);
784 SCpnt->
SCp.this_residual -=
res;
789 if (SCpnt->
SCp.this_residual == 0 &&
790 SCpnt->
SCp.buffers_residual != 0 ) {
792 SCpnt->
SCp.buffers_residual--;
795 SCpnt->
SCp.this_residual = SCpnt->
SCp.buffer->length;
805 nsp_msg(
KERN_DEBUG,
"pio read timeout resid=%d this_residual=%d buffers_residual=%d",
806 scsi_get_resid(SCpnt), SCpnt->
SCp.this_residual,
807 SCpnt->
SCp.buffers_residual);
811 scsi_get_resid(SCpnt));
817 static void nsp_pio_write(
struct scsi_cmnd *SCpnt)
819 unsigned int base = SCpnt->
device->host->io_port;
830 SCpnt->
SCp.buffer, SCpnt->
SCp.buffers_residual,
831 scsi_get_resid(SCpnt));
835 while ((time_out-- != 0) &&
836 (SCpnt->
SCp.this_residual > 0 || SCpnt->
SCp.buffers_residual > 0)) {
841 res = ocount - nsp_fifo_count(SCpnt);
845 nsp_inc_resid(SCpnt,
res);
847 SCpnt->
SCp.this_residual +=
res;
853 res = ocount - nsp_fifo_count(SCpnt);
865 nsp_fifo32_write(base, SCpnt->
SCp.ptr,
res >> 2);
868 nsp_fifo8_write (base, SCpnt->
SCp.ptr,
res );
873 nsp_mmio_fifo32_write(mmio_base, SCpnt->
SCp.ptr,
res >> 2);
881 nsp_inc_resid(SCpnt, -
res);
883 SCpnt->
SCp.this_residual -=
res;
887 if (SCpnt->
SCp.this_residual == 0 &&
888 SCpnt->
SCp.buffers_residual != 0 ) {
890 SCpnt->
SCp.buffers_residual--;
893 SCpnt->
SCp.this_residual = SCpnt->
SCp.buffer->length;
902 scsi_get_resid(SCpnt));
906 scsi_get_resid(SCpnt));
914 static int nsp_nexus(
struct scsi_cmnd *SCpnt)
916 unsigned int base = SCpnt->
device->host->io_port;
917 unsigned char target =
scmd_id(SCpnt);
925 nsp_index_write(base,
SYNCREG,
sync->SyncRegister);
928 if (scsi_get_resid(SCpnt) % 4 != 0 ||
940 nsp_setup_fifo(data,
TRUE);
959 unsigned char irq_status, irq_phase,
phase;
962 unsigned int *sync_neg;
970 if ( dev_id !=
NULL &&
991 if ((irq_status == 0xff) || ((irq_status &
IRQSTATUS_MASK) == 0)) {
1002 if((irq_status & IRQSTATUS_SCSI) != 0) {
1030 if ((irq_status & IRQSTATUS_SCSI) &&
1035 nsp_bus_reset(data);
1040 ((tmpSC->
SCp.Message & 0xff) << 8) |
1041 ((tmpSC->
SCp.Status & 0xff) << 0);
1042 nsp_scsi_done(tmpSC);
1048 nsp_msg(
KERN_ERR,
"CurrentSC==NULL irq_status=0x%x phase=0x%x irq_phase=0x%x this can't be happen. reset everything", irq_status, phase, irq_phase);
1050 nsp_bus_reset(data);
1055 target = tmpSC->
device->id;
1056 lun = tmpSC->
device->lun;
1062 if (irq_status & IRQSTATUS_SCSI) {
1066 if (nsp_reselected(tmpSC) !=
FALSE) {
1078 switch(tmpSC->
SCp.phase) {
1089 nsp_scsi_done(tmpSC);
1094 nsp_start_timer(tmpSC, 1000/51);
1115 nsp_scsi_done(tmpSC);
1134 nsp_dbg(
NSP_DEBUG_INTR,
"normal disconnect irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1140 ((tmpSC->
SCp.Message & 0xff) << 8) |
1141 ((tmpSC->
SCp.Status & 0xff) << 0);
1143 nsp_scsi_done(tmpSC);
1154 nsp_msg(
KERN_DEBUG,
"unexpected bus free. irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1158 nsp_scsi_done(tmpSC);
1162 switch (phase & BUSMON_PHASE_MASK) {
1177 for (i = 0; i < tmpSC->
cmd_len; i++) {
1189 nsp_pio_write(tmpSC);
1199 nsp_pio_read(tmpSC);
1204 nsp_dataphase_bypass(tmpSC);
1216 if ((phase & BUSMON_REQ) == 0) {
1241 nsp_analyze_sdtr(tmpSC);
1243 nsp_message_out(tmpSC);
1247 nsp_dataphase_bypass(tmpSC);
1249 if ((phase & BUSMON_REQ) == 0) {
1254 nsp_message_in(tmpSC);
1273 nsp_analyze_sdtr(tmpSC);
1279 for (i = 0; i < data->
MsgLen; i++) {
1285 tmpSC->
SCp.Message =
tmp;
1303 nsp_start_timer(tmpSC, 1000/102);
1328 data->ScsiInfo->host = host;
1336 host->
io_port = data->BaseAddress;
1338 host->
irq = data->IrqNumber;
1339 host->
base = data->MmioAddress;
1344 sizeof(data->nspinfo),
1345 "NinjaSCSI-3/32Bi Driver $Revision: 1.23 $ IO:0x%04lx-0x%04lx MMIO(virt addr):0x%04lx IRQ:%02d",
1349 sht->
name = data->nspinfo;
1360 static const char *nsp_info(
struct Scsi_Host *shpnt)
1368 #define SPRINTF(args...) \
1370 if(length > (pos - buffer)) { \
1371 pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
1372 nsp_dbg(NSP_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length, length - (pos - buffer));\
1383 unsigned long flags;
1395 SPRINTF(
"NinjaSCSI status\n\n");
1396 SPRINTF(
"Driver version: $Revision: 1.23 $\n");
1397 SPRINTF(
"SCSI host No.: %d\n", hostno);
1403 SPRINTF(
"burst transfer mode: ");
1404 switch (nsp_burst_mode) {
1423 spin_unlock_irqrestore(&(data->
Lock), flags);
1431 SPRINTF(
"----- NinjaSCSI-3 host adapter\n");
1453 SPRINTF(
" transfer %d.%dMB/s, offset %d",
1462 thislength = pos - (buffer +
offset);
1464 if(thislength < 0) {
1470 thislength =
min(thislength, length);
1471 *start = buffer +
offset;
1499 for(i = 0; i < 5; i++) {
1503 nsphw_init_sync(data);
1510 static int nsp_eh_bus_reset(
struct scsi_cmnd *SCpnt)
1516 return nsp_bus_reset(data);
1519 static int nsp_eh_host_reset(
struct scsi_cmnd *SCpnt)
1535 static int nsp_cs_probe(
struct pcmcia_device *
link)
1552 ret = nsp_cs_config(link);
1559 static void nsp_cs_detach(
struct pcmcia_device *link)
1564 nsp_cs_release(link);
1571 static int nsp_cs_config_check(
struct pcmcia_device *p_dev,
void *priv_data)
1575 if (p_dev->config_index == 0)
1582 if (resource_size(p_dev->resource[2])) {
1583 p_dev->resource[2]->flags |= (WIN_DATA_WIDTH_16 |
1584 WIN_MEMORY_TYPE_CM |
1586 if (p_dev->resource[2]->end < 0x1000)
1587 p_dev->resource[2]->end = 0x1000;
1591 p_dev->card_addr) != 0)
1596 resource_size(p_dev->resource[2]));
1597 data->
MmioLength = resource_size(p_dev->resource[2]);
1608 static int nsp_cs_config(
struct pcmcia_device *link)
1617 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_CHECK_VCC |
1618 CONF_AUTO_SET_VPP | CONF_AUTO_AUDIO | CONF_AUTO_SET_IOMEM |
1621 ret = pcmcia_loop_config(link, nsp_cs_config_check, data);
1633 if (link->resource[0]) {
1635 resource_size(link->resource[0]));
1637 if (link->resource[1]) {
1639 resource_size(link->resource[1]));
1645 data->
NumAddress = resource_size(link->resource[0]);
1651 if(nsphw_init(data) ==
FALSE) {
1655 host = nsp_detect(&nsp_driver_template);
1663 ret = scsi_add_host (host,
NULL);
1675 nsp_cs_release(link);
1681 static void nsp_cs_release(
struct pcmcia_device *link)
1699 if (resource_size(link->resource[2])) {
1711 static int nsp_cs_suspend(
struct pcmcia_device *link)
1723 nsphw_init_sync(data);
1731 static int nsp_cs_resume(
struct pcmcia_device *link)
1746 nsp_bus_reset(data);
1756 PCMCIA_DEVICE_PROD_ID123(
"IO DATA",
"CBSC16 ",
"1", 0x547e66dc, 0x0d63a3fd, 0x51de003a),
1757 PCMCIA_DEVICE_PROD_ID123(
"KME ",
"SCSI-CARD-001",
"1", 0x534c02bc, 0x52008408, 0x51de003a),
1758 PCMCIA_DEVICE_PROD_ID123(
"KME ",
"SCSI-CARD-002",
"1", 0x534c02bc, 0xcb09d5b2, 0x51de003a),
1759 PCMCIA_DEVICE_PROD_ID123(
"KME ",
"SCSI-CARD-003",
"1", 0x534c02bc, 0xbc0ee524, 0x51de003a),
1760 PCMCIA_DEVICE_PROD_ID123(
"KME ",
"SCSI-CARD-004",
"1", 0x534c02bc, 0x226a7087, 0x51de003a),
1761 PCMCIA_DEVICE_PROD_ID123(
"WBT",
"NinjaSCSI-3",
"R1.0", 0xc7ba805f, 0xfdc7c97d, 0x6973710e),
1762 PCMCIA_DEVICE_PROD_ID123(
"WORKBIT",
"UltraNinja-16",
"1", 0x28191418, 0xb70f4b09, 0x51de003a),
1767 static struct pcmcia_driver nsp_driver = {
1770 .probe = nsp_cs_probe,
1771 .remove = nsp_cs_detach,
1772 .id_table = nsp_cs_ids,
1773 .suspend = nsp_cs_suspend,
1774 .resume = nsp_cs_resume,
1777 static int __init nsp_cs_init(
void)
1782 static void __exit nsp_cs_exit(
void)