28 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
40 #include <linux/slab.h>
49 #define SCSI_BUF_PA(address) isa_virt_to_bus(address)
50 #define SCSI_SG_PA(sgent) (isa_page_to_bus(sg_page((sgent))) + (sgent)->offset)
52 #include <linux/stat.h>
102 static bool isapnp = 0;
103 static int aha1542[] = {0x330, 11, 4, -1};
119 static int isapnp = 1;
122 #define BIOS_TRANSLATION_1632 0
123 #define BIOS_TRANSLATION_6432 1
124 #define BIOS_TRANSLATION_25563 2
136 #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
142 #define WAITnexttimeout 3000000
144 static void setup_mailboxes(
int base_io,
struct Scsi_Host *shpnt);
148 #define aha1542_intr_reset(base) outb(IRST, CONTROL(base))
150 #define WAIT(port, mask, allof, noneof) \
151 { register int WAITbits; \
152 register int WAITtimeout = WAITnexttimeout; \
154 WAITbits = inb(port) & (mask); \
155 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
157 if (--WAITtimeout == 0) goto fail; \
163 #define WAITd(port, mask, allof, noneof, timeout) \
164 { register int WAITbits; \
165 register int WAITtimeout = timeout; \
167 WAITbits = inb(port) & (mask); \
168 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
171 if (--WAITtimeout == 0) goto fail; \
175 static void aha1542_stat(
void)
185 static int aha1542_out(
unsigned int base,
unchar * cmdp,
int len)
187 unsigned long flags = 0;
196 spin_unlock_irqrestore(&aha1542_lock, flags);
200 spin_unlock_irqrestore(&aha1542_lock, flags);
210 spin_unlock_irqrestore(&aha1542_lock, flags);
215 spin_unlock_irqrestore(&aha1542_lock, flags);
224 static int __init aha1542_in(
unsigned int base,
unchar * cmdp,
int len)
233 spin_unlock_irqrestore(&aha1542_lock, flags);
236 spin_unlock_irqrestore(&aha1542_lock, flags);
245 static int __init aha1542_in1(
unsigned int base,
unchar * cmdp,
int len)
254 spin_unlock_irqrestore(&aha1542_lock, flags);
257 spin_unlock_irqrestore(&aha1542_lock, flags);
261 static int makecode(
unsigned hosterr,
unsigned scsierr)
300 DEB(
printk(
"Aha1542: %x %x\n", hosterr, scsierr));
311 printk(
KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
314 return scsierr | (hosterr << 16);
323 volatile int debug = 0;
353 aha1542_out(bse, inquiry_cmd, 1);
357 cmdp = &inquiry_result[0];
392 aha1542_intr_handle(shost);
393 spin_unlock_irqrestore(shost->
host_lock, flags);
401 int errstatus,
mbi, mbo, mbistatus;
453 mbi =
HOSTDATA(shost)->aha1542_last_mbi_used + 1;
463 }
while (mbi !=
HOSTDATA(shost)->aha1542_last_mbi_used);
465 if (mb[mbi].
status == 0) {
466 spin_unlock_irqrestore(&aha1542_lock, flags);
468 if (!number_serviced && !needs_restart)
473 aha1542_restart(shost);
481 spin_unlock_irqrestore(&aha1542_lock, flags);
487 ccb[mbo].
tarstat + ((
int) ccb[mbo].hastat << 16), mb[mbi].
status);
498 SCtmp =
HOSTDATA(shost)->SCint[mbo];
503 ccb[mbo].hastat, ccb[mbo].
idlun, mbo);
522 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].
tarstat);
529 ccb[mbo].hastat, ccb[mbo].
tarstat);
536 DEB(
printk(
"aha1542_intr_handle: sense:"));
538 for (i = 0; i < 12; i++)
549 DEB(
if (errstatus)
printk(
"aha1542_intr_handle: returning %6x\n", errstatus));
550 SCtmp->
result = errstatus;
566 int bufflen = scsi_bufflen(SCpnt);
578 done(SCpnt);
return 0;
589 "for request sense (%d)\n",
bufflen);
608 for (i = 0; i < SCpnt->
cmd_len; i++)
628 }
while (mbo !=
HOSTDATA(SCpnt->
device->host)->aha1542_last_mbo_used);
631 panic(
"Unable to find empty mailbox for aha1542.\n");
637 spin_unlock_irqrestore(&aha1542_lock, flags);
645 memset(&ccb[mbo], 0,
sizeof(
struct ccb));
680 printk(
"cptr %x: ", cptr);
681 ptr = (
unsigned char *) cptr;
682 for (i = 0; i < 18; i++)
700 for (i = 0; i <
sizeof(ccb[mbo]) - 10; i++)
706 DEB(
printk(
"aha1542_queuecommand: now waiting for interrupt ");
710 aha1542_out(SCpnt->
device->host->io_port, &ahacmd, 1);
713 printk(
"aha1542_queuecommand: done can't be NULL\n");
738 aha1542_out(bse, cmd, 5);
747 static int __init aha1542_getconfig(
int base_io,
unsigned char *irq_level,
unsigned char *
dma_chan,
unsigned char *
scsi_id)
756 aha1542_out(base_io, inquiry_cmd, 1);
757 aha1542_in(base_io, inquiry_result, 3);
764 switch (inquiry_result[0]) {
783 printk(
KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
786 switch (inquiry_result[1]) {
806 printk(
KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
809 *scsi_id = inquiry_result[2] & 7;
816 static int __init aha1542_mbenable(
int base)
818 static unchar mbenable_cmd[3];
819 static unchar mbenable_result[2];
825 aha1542_out(base, mbenable_cmd, 1);
826 if (aha1542_in1(base, mbenable_result, 2))
831 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
834 mbenable_cmd[2] = mbenable_result[1];
836 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
839 aha1542_out(base, mbenable_cmd, 3);
851 static int __init aha1542_query(
int base_io,
int *transl)
860 aha1542_out(base_io, inquiry_cmd, 1);
861 aha1542_in(base_io, inquiry_result, 4);
877 if (inquiry_result[0] == 0x43) {
878 printk(
KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
885 *transl = aha1542_mbenable(base_io);
892 static int setup_idx = 0;
894 static void __init aha1542_setup(
char *
str,
int *ints)
896 const char *ahausage =
"aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
900 printk(
KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
906 if (ints[0] < 1 || ints[0] > 4) {
909 printk(
KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
911 setup_called[setup_idx] = ints[0];
912 setup_str[setup_idx] =
str;
914 setup_portbase = ints[0] >= 1 ? ints[1] : 0;
915 setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
916 setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
939 printk(
KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
942 setup_dmaspeed[setup_idx] = atbt;
944 if (setup_portbase != 0)
945 bases[setup_idx] = setup_portbase;
950 static int __init do_setup(
char *str)
957 aha1542_setup(str,ints);
959 return count<setup_idx;
968 unsigned char dma_chan;
969 unsigned char irq_level;
972 unsigned int base_io;
983 bases[0] = aha1542[0];
984 setup_buson[0] = aha1542[1];
985 setup_busoff[0] = aha1542[2];
988 switch (aha1542[3]) {
1005 setup_dmaspeed[0] = atbt;
1016 for(indx = 0; indx <
ARRAY_SIZE(bases); indx++) {
1035 if(!pnp_port_valid(pdev, 0)) {
1040 bases[indx] = pnp_port_start(pdev, 0);
1045 printk(
KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1048 for (indx = 0; indx <
ARRAY_SIZE(bases); indx++)
1049 if (bases[indx] != 0 &&
request_region(bases[indx], 4,
"aha1542")) {
1057 if (!aha1542_test_port(bases[indx], shpnt))
1060 base_io = bases[indx];
1067 if (setup_called[indx]) {
1068 oncmd[1] = setup_buson[indx];
1069 offcmd[1] = setup_busoff[indx];
1072 aha1542_out(base_io, oncmd, 2);
1075 aha1542_out(base_io, offcmd, 2);
1077 if (setup_dmaspeed[indx] >= 0) {
1079 dmacmd[1] = setup_dmaspeed[indx];
1081 aha1542_out(base_io, dmacmd, 2);
1086 printk(
KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
1090 if (aha1542_query(base_io, &trans))
1093 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
1096 printk(
KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1097 if (dma_chan != 0xFF)
1098 printk(
", DMA priority %d", dma_chan);
1101 DEB(aha1542_stat());
1102 setup_mailboxes(base_io, shpnt);
1104 DEB(aha1542_stat());
1106 DEB(
printk(
"aha1542_detect: enable interrupt channel %d\n", irq_level));
1108 if (
request_irq(irq_level, do_aha1542_intr_handle, 0,
1109 "aha1542", shpnt)) {
1110 printk(
KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1111 spin_unlock_irqrestore(&aha1542_lock, flags);
1114 if (dma_chan != 0xFF) {
1116 printk(
KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
1118 spin_unlock_irqrestore(&aha1542_lock, flags);
1121 if (dma_chan == 0 || dma_chan >= 5) {
1132 shpnt->
irq = irq_level;
1136 HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1137 HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1139 spin_unlock_irqrestore(&aha1542_lock, flags);
1141 DEB(
printk(
" *** READ CAPACITY ***\n"));
1148 for (i = 0; i <
sizeof(
buf); ++
i)
1150 for (i = 0; i < 2; ++
i)
1151 if (!aha1542_command(i, cmd, buf,
sizeof(buf))) {
1157 DEB(
printk(
" *** NOW RUNNING MY OWN TEST *** \n"));
1159 for (i = 0; i < 4; ++
i) {
1160 unsigned char cmd[10];
1170 aha1542_command(0, cmd,
buffer, 512);
1185 static int aha1542_release(
struct Scsi_Host *shost)
1197 static int aha1542_restart(
struct Scsi_Host *shost)
1217 aha1542_out(shost->
io_port, &ahacmd, 1);
1226 static int aha1542_dev_reset(
Scsi_Cmnd * SCpnt)
1228 unsigned long flags;
1241 if (mbo >= AHA1542_MAILBOXES)
1248 if (mbo >= AHA1542_MAILBOXES)
1250 }
while (mbo !=
HOSTDATA(SCpnt->
device->host)->aha1542_last_mbo_used);
1253 panic(
"Unable to find empty mailbox for aha1542.\n");
1260 spin_unlock_irqrestore(&aha1542_lock, flags);
1264 memset(&ccb[mbo], 0,
sizeof(
struct ccb));
1268 ccb[mbo].
idlun = (target & 7) << 5 | (lun & 7);
1277 aha1542_out(SCpnt->
device->host->io_port, &ahacmd, 1);
1280 "Trying device reset for target\n");
1285 #ifdef ERIC_neverdef
1302 if (
HOSTDATA(SCpnt->host)->SCint[i] &&
1303 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1305 SCtmp =
HOSTDATA(SCpnt->host)->SCint[
i];
1309 HOSTDATA(SCpnt->host)->mb[
i].status = 0;
1318 static int aha1542_bus_reset(
Scsi_Cmnd * SCpnt)
1339 spin_lock_irq(SCpnt->
device->host->host_lock);
1358 if (SCtmp->
device->soft_reset) {
1374 spin_unlock_irq(SCpnt->
device->host->host_lock);
1378 spin_unlock_irq(SCpnt->
device->host->host_lock);
1382 static int aha1542_host_reset(
Scsi_Cmnd * SCpnt)
1402 spin_lock_irq(SCpnt->
device->host->host_lock);
1411 setup_mailboxes(SCpnt->
device->host->io_port, SCpnt->
device->host);
1426 if (SCtmp->
device->soft_reset) {
1442 spin_unlock_irq(SCpnt->
device->host->host_lock);
1446 spin_unlock_irq(SCpnt->
device->host->host_lock);
1455 static int aha1542_old_abort(
Scsi_Cmnd * SCpnt)
1459 unsigned long flags;
1469 mbi =
HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
1470 if (mbi >= 2 * AHA1542_MAILBOXES)
1477 if (mbi >= 2 * AHA1542_MAILBOXES)
1479 }
while (mbi !=
HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
1480 spin_unlock_irqrestore(&aha1542_lock, flags);
1483 printk(
KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
1485 aha1542_intr_handle(SCpnt->host,
NULL);
1492 if (
HOSTDATA(SCpnt->host)->SCint[i]) {
1493 if (
HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1496 SCpnt->
request->rq_disk->disk_name :
"?"
1498 if (
HOSTDATA(SCpnt->host)->mb[i].status) {
1500 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1505 SCpnt->
request->rq_disk->disk_name :
"?"
1514 if (SCpnt ==
HOSTDATA(SCpnt->host)->SCint[mbo]) {
1516 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1517 spin_unlock_irqrestore(&aha1542_lock, flags);
1521 if (AHA1542_MAILBOXES == mbo)
1522 spin_unlock_irqrestore(&aha1542_lock, flags);
1533 static int aha1542_old_reset(
Scsi_Cmnd * SCpnt,
unsigned int reset_flags)
1564 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1577 SCtmp =
HOSTDATA(SCpnt->host)->SCint[
i];
1585 HOSTDATA(SCpnt->host)->mb[
i].status = 0;
1603 if (
HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1604 HOSTDATA(SCpnt->host)->ccb[
i].op = 0x81;
1606 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1618 if (
HOSTDATA(SCpnt->host)->SCint[i] &&
1619 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1621 SCtmp =
HOSTDATA(SCpnt->host)->SCint[
i];
1629 HOSTDATA(SCpnt->host)->mb[
i].status = 0;
1641 static int aha1542_biosparam(
struct scsi_device *sdev,
1644 int translation_algorithm;
1647 translation_algorithm =
HOSTDATA(sdev->
host)->bios_translation;
1653 ip[2] = size / 255 / 63;
1666 .proc_name =
"aha1542",
1667 .name =
"Adaptec 1542",
1668 .detect = aha1542_detect,
1669 .release = aha1542_release,
1670 .queuecommand = aha1542_queuecommand,
1671 .eh_device_reset_handler= aha1542_dev_reset,
1672 .eh_bus_reset_handler = aha1542_bus_reset,
1673 .eh_host_reset_handler = aha1542_host_reset,
1674 .bios_param = aha1542_biosparam,
1679 .unchecked_isa_dma = 1,