130 #include <linux/module.h>
133 #include <linux/stddef.h>
134 #include <linux/string.h>
135 #include <linux/kernel.h>
139 #include <linux/stat.h>
140 #include <linux/bitops.h>
146 #define ULTRASTOR_PRIVATE
154 #ifndef ULTRASTOR_DEBUG
155 #define ULTRASTOR_DEBUG (UD_ABORT|UD_CSIR|UD_RESET)
158 #define VERSION "1.12"
160 #define PACKED __attribute__((packed))
161 #define ALIGNED(x) __attribute__((aligned(x)))
179 unsigned char xdir: 2;
180 unsigned char dcn: 1;
185 unsigned char lun: 3;
187 unsigned int transfer_data_length
PACKED;
206 #define U14F_PRODUCT_ID(port) ((port) + 0x4)
207 #define CONFIG(port) ((port) + 0x6)
210 #define LCL_DOORBELL_MASK(port) ((port) + 0x0)
211 #define LCL_DOORBELL_INTR(port) ((port) + 0x1)
212 #define SYS_DOORBELL_MASK(port) ((port) + 0x2)
213 #define SYS_DOORBELL_INTR(port) ((port) + 0x3)
225 static struct ultrastor_config
227 unsigned short port_address;
228 unsigned short doorbell_address;
229 unsigned short ogm_address;
230 unsigned short icm_address;
231 const void *bios_segment;
234 unsigned char bios_drive_number: 1;
237 unsigned char ha_scsi_id: 3;
238 unsigned char subversion: 4;
244 #ifdef PRINT_U24F_VERSION
245 volatile int csir_done;
252 #if ULTRASTOR_MAX_CMDS == 1
253 unsigned char mscp_busy;
255 unsigned long mscp_free;
258 struct mscp mscp[ULTRASTOR_MAX_CMDS];
262 static int ultrastor_bus_reset;
266 static const void *
const bios_segment_table[8] = {
267 NULL, (
void *)0xC4000, (
void *)0xC8000, (
void *)0xCC000,
268 (
void *)0xD0000, (
void *)0xD4000, (
void *)0xD8000, (
void *)0xDC000,
272 static const unsigned char interrupt_table_14f[4] = { 15, 14, 11, 10 };
275 static const unsigned char dma_channel_table_14f[4] = { 5, 6, 7, 0 };
278 static const struct {
281 } mapping_table[4] = { { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 } };
283 #ifndef PORT_OVERRIDE
285 static const unsigned short ultrastor_ports_14f[] = {
286 0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140,
290 static void ultrastor_interrupt(
void *);
291 static irqreturn_t do_ultrastor_interrupt(
int,
void *);
292 static inline void build_sg_list(
struct mscp *,
struct scsi_cmnd *SCpnt);
297 static inline int find_and_clear_bit_16(
unsigned long *
field)
302 panic(
"No free mscp");
309 :
"=&r" (rv),
"+m" (*field) :);
322 static inline unsigned char xchgb(
unsigned char reg,
323 volatile unsigned char *
mem)
325 __asm__ (
"xchgb %0,%1" :
"=q" (reg),
"=m" (*mem) :
"0" (reg));
329 #if ULTRASTOR_DEBUG & (UD_COMMAND | UD_ABORT)
332 static void log_ultrastor_abort(
struct ultrastor_config *
config,
335 static char fmt[80] =
"abort %d (%x); MSCP free pool: %x;";
341 if (! (config->mscp_free & (1 << i)))
342 fmt[21 + i*2] =
'0' + config->mscp[
i].target_id;
346 fmt[20 + ULTRASTOR_MAX_CMDS * 2] =
'\n';
347 fmt[21 + ULTRASTOR_MAX_CMDS * 2] = 0;
348 printk(fmt, command, &config->mscp[command], config->mscp_free);
356 unsigned char in_byte, version_byte = 0;
358 unsigned char bios_segment: 3;
359 unsigned char removable_disks_as_fixed: 1;
364 unsigned char ha_scsi_id: 3;
365 unsigned char mapping_mode: 2;
366 unsigned char bios_drive_number: 1;
367 unsigned char tfr_port: 2;
370 #if (ULTRASTOR_DEBUG & UD_DETECT)
371 printk(
"US14F: detect: called\n");
375 if (config.bios_segment)
380 printk(
"Ultrastor I/O space already in use\n");
383 config.port_address = PORT_OVERRIDE;
385 for (i = 0; i <
ARRAY_SIZE(ultrastor_ports_14f); i++) {
386 if(!
request_region(ultrastor_ports_14f[i], 0x0c,
"ultrastor"))
continue;
387 config.port_address = ultrastor_ports_14f[
i];
390 #if (ULTRASTOR_DEBUG & UD_DETECT)
391 printk(
"US14F: detect: testing port address %03X\n", config.port_address);
395 if (in_byte != US14F_PRODUCT_ID_0) {
396 #if (ULTRASTOR_DEBUG & UD_DETECT)
397 # ifdef PORT_OVERRIDE
398 printk(
"US14F: detect: wrong product ID 0 - %02X\n", in_byte);
400 printk(
"US14F: detect: no adapter at port %03X\n", config.port_address);
404 goto out_release_port;
412 if ((in_byte & 0xF0) != US14F_PRODUCT_ID_1) {
413 #if (ULTRASTOR_DEBUG & UD_DETECT)
414 # ifdef PORT_OVERRIDE
415 printk(
"US14F: detect: wrong product ID 1 - %02X\n", in_byte);
417 printk(
"US14F: detect: no adapter at port %03X\n", config.port_address);
421 goto out_release_port;
427 version_byte = in_byte;
428 #ifndef PORT_OVERRIDE
432 # if (ULTRASTOR_DEBUG & UD_DETECT)
433 printk(
"US14F: detect: no port address found!\n");
440 #if (ULTRASTOR_DEBUG & UD_DETECT)
441 printk(
"US14F: detect: adapter found at port address %03X\n",
442 config.port_address);
454 *(
char *)&config_1 =
inb(
CONFIG(config.port_address + 0));
455 *(
char *)&config_2 =
inb(
CONFIG(config.port_address + 1));
456 config.bios_segment = bios_segment_table[config_1.bios_segment];
457 config.doorbell_address = config.port_address;
458 config.ogm_address = config.port_address + 0x8;
459 config.icm_address = config.port_address + 0xC;
460 config.interrupt = interrupt_table_14f[config_1.interrupt];
461 config.ha_scsi_id = config_2.ha_scsi_id;
462 config.heads = mapping_table[config_2.mapping_mode].heads;
463 config.sectors = mapping_table[config_2.mapping_mode].sectors;
464 config.bios_drive_number = config_2.bios_drive_number;
465 config.subversion = (version_byte & 0x0F);
466 if (config.subversion == U34F)
467 config.dma_channel = 0;
469 config.dma_channel = dma_channel_table_14f[config_1.dma_channel];
471 if (!config.bios_segment) {
472 #if (ULTRASTOR_DEBUG & UD_DETECT)
473 printk(
"US14F: detect: not detected.\n");
475 goto out_release_port;
479 if (config.subversion != U34F)
480 if (!config.dma_channel || !(config_2.tfr_port & 0x2)) {
481 #if (ULTRASTOR_DEBUG & UD_DETECT)
482 printk(
"US14F: detect: consistency check failed\n");
484 goto out_release_port;
492 #if (ULTRASTOR_DEBUG & UD_DETECT)
493 printk(
"US14F: detect: detect succeeded\n"
494 " Port address: %03X\n"
495 " BIOS segment: %05X\n"
500 config.port_address, config.bios_segment, config.interrupt,
501 config.dma_channel, config.ha_scsi_id, config.subversion);
503 tpnt->
this_id = config.ha_scsi_id;
506 #if ULTRASTOR_MAX_CMDS > 1
507 config.mscp_free = ~0;
514 if (
request_irq(config.interrupt, do_ultrastor_interrupt, 0,
"Ultrastor", &config.mscp[0].SCint->device->host)) {
515 printk(
"Unable to allocate IRQ%u for UltraStor controller.\n",
517 goto out_release_port;
519 if (config.dma_channel &&
request_dma(config.dma_channel,
"Ultrastor")) {
520 printk(
"Unable to allocate DMA channel %u for UltraStor controller.\n",
523 goto out_release_port;
526 printk(
"UltraStor driver version" VERSION ". Using %d SG lists.\n",
540 #if (ULTRASTOR_DEBUG & UD_DETECT)
545 for (i = 1; i < 15; i++)
547 unsigned char config_1, config_2;
550 if (
inb(addr) != US24F_PRODUCT_ID_0 &&
551 inb(addr+1) != US24F_PRODUCT_ID_1 &&
552 inb(addr+2) != US24F_PRODUCT_ID_2)
555 config.revision =
inb(addr+3);
557 if (! (
inb(addr+4) & 1))
559 #if (ULTRASTOR_DEBUG & UD_DETECT)
560 printk(
"U24F: found disabled card in slot %u\n", i);
564 #if (ULTRASTOR_DEBUG & UD_DETECT)
565 printk(
"U24F: found card in slot %u\n", i);
567 config_1 =
inb(addr + 5);
568 config.bios_segment = bios_segment_table[config_1 & 7];
569 switch(config_1 >> 4)
572 config.interrupt = 15;
575 config.interrupt = 14;
578 config.interrupt = 11;
581 config.interrupt = 10;
584 printk(
"U24F: invalid IRQ\n");
590 config.port_address =
addr;
591 config.doorbell_address = addr + 12;
592 config.ogm_address = addr + 0x17;
593 config.icm_address = addr + 0x1C;
594 config_2 =
inb(addr + 7);
595 config.ha_scsi_id = config_2 & 7;
596 config.heads = mapping_table[(config_2 >> 3) & 3].
heads;
597 config.sectors = mapping_table[(config_2 >> 3) & 3].
sectors;
598 #if (ULTRASTOR_DEBUG & UD_DETECT)
599 printk(
"US24F: detect: detect succeeded\n"
600 " Port address: %03X\n"
601 " BIOS segment: %05X\n"
603 " H/A SCSI ID: %u\n",
604 config.port_address, config.bios_segment,
605 config.interrupt, config.ha_scsi_id);
607 tpnt->
this_id = config.ha_scsi_id;
613 printk(
KERN_WARNING "(ultrastor:) Could not register scsi device. Aborting registration.\n");
614 free_irq(config.interrupt, do_ultrastor_interrupt);
618 if (
request_irq(config.interrupt, do_ultrastor_interrupt, 0,
"Ultrastor", shpnt))
620 printk(
"Unable to allocate IRQ%u for UltraStor controller.\n",
625 shpnt->
irq = config.interrupt;
627 shpnt->
io_port = config.port_address;
629 #if ULTRASTOR_MAX_CMDS > 1
630 config.mscp_free = ~0;
633 outb(0, addr + 0x16);
634 outb(0, addr + 0x1B);
640 printk(
"UltraStor driver version " VERSION ". Using %d SG lists.\n",
650 return ultrastor_14f_detect(tpnt) || ultrastor_24f_detect(tpnt);
665 static const char *ultrastor_info(
struct Scsi_Host * shpnt)
670 sprintf(buf,
"UltraStor 24F SCSI @ Slot %u IRQ%u",
671 config.slot, config.interrupt);
672 else if (config.subversion)
673 sprintf(buf,
"UltraStor 34F SCSI @ Port %03X BIOS %05X IRQ%u",
674 config.port_address, (
int)config.bios_segment,
677 sprintf(buf,
"UltraStor 14F SCSI @ Port %03X BIOS %05X IRQ%u DMA%u",
678 config.port_address, (
int)config.bios_segment,
679 config.interrupt, config.dma_channel);
686 long transfer_length = 0;
689 max = scsi_sg_count(SCpnt);
693 transfer_length += sg->
length;
696 mscp->transfer_data = isa_virt_to_bus(mscp->
sglist);
700 mscp->transfer_data_length = transfer_length;
703 static int ultrastor_queuecommand_lck(
struct scsi_cmnd *SCpnt,
706 struct mscp *my_mscp;
707 #if ULTRASTOR_MAX_CMDS > 1
713 if ((config.mscp_free & ((1
U << ULTRASTOR_MAX_CMDS) - 1)) == 0)
714 panic(
"ultrastor_queuecommand: no free MSCP\n");
715 mscp_index = find_and_clear_bit_16(&config.mscp_free);
718 if (xchgb(0xff, &config.aborted[mscp_index]) != 0)
724 my_mscp = &config.mscp[mscp_index];
739 if (scsi_sg_count(SCpnt)) {
742 build_sg_list(my_mscp, SCpnt);
746 my_mscp->transfer_data = isa_virt_to_bus(scsi_sglist(SCpnt));
747 my_mscp->transfer_data_length = scsi_bufflen(SCpnt);
749 my_mscp->command_link = 0;
756 my_mscp->sense_data = isa_virt_to_bus(&SCpnt->
sense_buffer);
769 while (
inb(config.ogm_address - 1) != 0 && config.aborted[mscp_index] == 0xff)
774 while ((
inb(
LCL_DOORBELL_INTR(config.doorbell_address)) & (config.slot ? 2 : 1)) && config.aborted[mscp_index] == 0xff)
785 status = xchgb(0, &config.aborted[mscp_index]);
786 if (status != 0xff) {
788 #if ULTRASTOR_DEBUG & (UD_COMMAND | UD_ABORT)
789 printk(
"USx4F: queuecommand: aborted\n");
790 #if ULTRASTOR_MAX_CMDS > 1
791 log_ultrastor_abort(&config, mscp_index);
797 set_bit(mscp_index, &config.mscp_free);
800 #if ULTRASTOR_MAX_CMDS > 1
810 outl(isa_virt_to_bus(my_mscp), config.ogm_address);
815 outb(1, config.ogm_address - 1);
821 #if (ULTRASTOR_DEBUG & UD_COMMAND)
822 printk(
"USx4F: queuecommand: returning\n");
847 #if ULTRASTOR_DEBUG & UD_ABORT
849 unsigned char icm_status = 0, ogm_status = 0;
850 unsigned int icm_addr = 0, ogm_addr = 0;
852 unsigned int mscp_index;
853 unsigned char old_aborted;
862 if(!SCpnt->host_scribble)
865 mscp_index = ((
struct mscp *)SCpnt->host_scribble) - config.mscp;
866 if (mscp_index >= ULTRASTOR_MAX_CMDS)
867 panic(
"Ux4F aborting invalid MSCP");
869 #if ULTRASTOR_DEBUG & UD_ABORT
872 int port0 = (config.slot << 12) | 0xc80;
877 strcpy(out,
"OGM %d:%x ICM %d:%x ports: ");
878 for (i = 0; i < 16; i++)
880 unsigned char p =
inb(port0 + i);
881 out[28 + i * 3] =
"0123456789abcdef"[p >> 4];
882 out[29 + i * 3] =
"0123456789abcdef"[p & 15];
883 out[30 + i * 3] =
' ';
885 out[28 + i * 3] =
'\n';
887 ogm_status =
inb(port0 + 22);
889 icm_status =
inb(port0 + 27);
891 spin_unlock_irqrestore(host->
host_lock, flags);
897 if (config.slot ?
inb(config.icm_address - 1) == 2 :
900 printk(
"Ux4F: abort while completed command pending\n");
904 ultrastor_interrupt(
NULL);
905 spin_unlock_irqrestore(host->
host_lock, flags);
910 old_aborted = xchgb(
DID_ABORT, &config.aborted[mscp_index]);
914 if (old_aborted == 0xff)
919 if (config.slot &&
inb(config.ogm_address - 1) == 0)
924 outl(isa_virt_to_bus(&config.mscp[mscp_index]), config.ogm_address);
926 outb(0x80, config.ogm_address - 1);
928 #if ULTRASTOR_DEBUG & UD_ABORT
929 log_ultrastor_abort(&config, mscp_index);
930 printk(out, ogm_status, ogm_addr, icm_status, icm_addr);
932 spin_unlock_irqrestore(host->
host_lock, flags);
937 #if ULTRASTOR_DEBUG & UD_ABORT
938 log_ultrastor_abort(&config, mscp_index);
951 #if ULTRASTOR_DEBUG & UD_ABORT
952 if (config.mscp[mscp_index].SCint != SCpnt)
953 printk(
"abort: command mismatch, %p != %p\n",
954 config.mscp[mscp_index].SCint, SCpnt);
956 if (config.mscp[mscp_index].SCint ==
NULL)
959 if (config.mscp[mscp_index].SCint != SCpnt)
panic(
"Bad abort");
960 config.mscp[mscp_index].SCint =
NULL;
961 done = config.mscp[mscp_index].done;
962 config.mscp[mscp_index].done =
NULL;
972 static int ultrastor_host_reset(
struct scsi_cmnd * SCpnt)
978 #if (ULTRASTOR_DEBUG & UD_RESET)
979 printk(
"US14F: reset: called\n");
991 outb(0, config.ogm_address - 1);
992 outb(0, config.icm_address - 1);
995 #if ULTRASTOR_MAX_CMDS == 1
996 if (config.mscp_busy && config.mscp->done && config.mscp->SCint)
998 config.mscp->SCint->result =
DID_RESET << 16;
999 config.mscp->done(config.mscp->SCint);
1001 config.mscp->SCint = 0;
1005 if (! (config.mscp_free & (1 << i)) &&
1006 config.mscp[i].done && config.mscp[i].SCint)
1008 config.mscp[
i].SCint->result =
DID_RESET << 16;
1009 config.mscp[
i].done(config.mscp[i].SCint);
1010 config.mscp[
i].done =
NULL;
1012 config.mscp[
i].SCint =
NULL;
1022 memset((
unsigned char *)config.aborted, 0,
sizeof config.aborted);
1023 #if ULTRASTOR_MAX_CMDS == 1
1024 config.mscp_busy = 0;
1026 config.mscp_free = ~0;
1029 spin_unlock_irqrestore(host->
host_lock, flags);
1038 unsigned int s = config.heads * config.sectors;
1040 dkinfo[0] = config.heads;
1041 dkinfo[1] = config.sectors;
1042 dkinfo[2] = size /
s;
1044 if (dkinfo[2] > 1024)
1050 static void ultrastor_interrupt(
void *
dev_id)
1053 #if ULTRASTOR_MAX_CMDS > 1
1054 unsigned int mscp_index;
1060 #if ULTRASTOR_MAX_CMDS == 1
1061 mscp = &config.mscp[0];
1064 mscp_index = mscp - config.mscp;
1065 if (mscp_index >= ULTRASTOR_MAX_CMDS) {
1066 printk(
"Ux4F interrupt: bad MSCP address %x\n", (
unsigned int) mscp);
1069 ultrastor_host_reset(dev_id);
1076 unsigned char icm_status =
inb(config.icm_address - 1);
1077 #if ULTRASTOR_DEBUG & (UD_INTERRUPT|UD_ERROR|UD_ABORT)
1078 if (icm_status != 1 && icm_status != 2)
1079 printk(
"US24F: ICM status %x for MSCP %d (%x)\n", icm_status,
1080 mscp_index, (
unsigned int) mscp);
1085 outb(0, config.icm_address - 1);
1086 if (icm_status == 4) {
1087 printk(
"UltraStor abort command failed\n");
1090 if (icm_status == 3) {
1103 SCtmp = mscp->
SCint;
1108 #if ULTRASTOR_DEBUG & (UD_ABORT|UD_INTERRUPT)
1109 printk(
"MSCP %d (%x): no command\n", mscp_index, (
unsigned int) mscp);
1111 #if ULTRASTOR_MAX_CMDS == 1
1112 config.mscp_busy =
FALSE;
1114 set_bit(mscp_index, &config.mscp_free);
1116 config.aborted[mscp_index] = 0;
1151 #if ULTRASTOR_MAX_CMDS == 1
1152 config.mscp_busy =
FALSE;
1154 set_bit(mscp_index, &config.mscp_free);
1157 #if ULTRASTOR_DEBUG & (UD_ABORT|UD_INTERRUPT)
1158 if (config.aborted[mscp_index])
1159 printk(
"Ux4 interrupt: MSCP %d (%x) aborted = %d\n",
1160 mscp_index, (
unsigned int) mscp, config.aborted[mscp_index]);
1162 config.aborted[mscp_index] = 0;
1167 printk(
"US14F: interrupt: unexpected interrupt\n");
1169 if (config.slot ?
inb(config.icm_address - 1) :
1171 #if (ULTRASTOR_DEBUG & UD_MULTI_CMD)
1172 printk(
"Ux4F: multiple commands completed\n");
1177 #if (ULTRASTOR_DEBUG & UD_INTERRUPT)
1178 printk(
"USx4F: interrupt: returning\n");
1182 static irqreturn_t do_ultrastor_interrupt(
int irq,
void *dev_id)
1184 unsigned long flags;
1188 ultrastor_interrupt(dev_id);
1189 spin_unlock_irqrestore(dev->
host_lock, flags);
1196 .name =
"UltraStor 14F/24F/34F",
1197 .detect = ultrastor_detect,
1198 .release = ultrastor_release,
1199 .info = ultrastor_info,
1200 .queuecommand = ultrastor_queuecommand,
1201 .eh_abort_handler = ultrastor_abort,
1202 .eh_host_reset_handler = ultrastor_host_reset,
1207 .unchecked_isa_dma = 1,