Merge branch 'parisc-4.9-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[cascardo/linux.git] / drivers / scsi / aha1542.c
1 /*
2  *  Driver for Adaptec AHA-1542 SCSI host adapters
3  *
4  *  Copyright (C) 1992  Tommy Thorn
5  *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
6  *  Copyright (C) 2015 Ondrej Zary
7  */
8
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/isa.h>
18 #include <linux/pnp.h>
19 #include <linux/slab.h>
20 #include <linux/io.h>
21 #include <asm/dma.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_device.h>
24 #include <scsi/scsi_host.h>
25 #include "aha1542.h"
26
27 #define MAXBOARDS 4
28
29 static bool isapnp = 1;
30 module_param(isapnp, bool, 0);
31 MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
32
33 static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
34 module_param_array(io, int, NULL, 0);
35 MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
36
37 /* time AHA spends on the AT-bus during data transfer */
38 static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
39 module_param_array(bus_on, int, NULL, 0);
40 MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
41
42 /* time AHA spends off the bus (not to monopolize it) during data transfer  */
43 static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
44 module_param_array(bus_off, int, NULL, 0);
45 MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
46
47 /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
48 static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
49 module_param_array(dma_speed, int, NULL, 0);
50 MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
51
52 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
53 #define BIOS_TRANSLATION_25563 2        /* Big disk case */
54
55 struct aha1542_hostdata {
56         /* This will effectively start both of them at the first mailbox */
57         int bios_translation;   /* Mapping bios uses - for compatibility */
58         int aha1542_last_mbi_used;
59         int aha1542_last_mbo_used;
60         struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
61         struct mailbox mb[2 * AHA1542_MAILBOXES];
62         struct ccb ccb[AHA1542_MAILBOXES];
63 };
64
65 static inline void aha1542_intr_reset(u16 base)
66 {
67         outb(IRST, CONTROL(base));
68 }
69
70 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
71 {
72         bool delayed = true;
73
74         if (timeout == 0) {
75                 timeout = 3000000;
76                 delayed = false;
77         }
78
79         while (1) {
80                 u8 bits = inb(port) & mask;
81                 if ((bits & allof) == allof && ((bits & noneof) == 0))
82                         break;
83                 if (delayed)
84                         mdelay(1);
85                 if (--timeout == 0)
86                         return false;
87         }
88
89         return true;
90 }
91
92 static int aha1542_outb(unsigned int base, u8 val)
93 {
94         if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
95                 return 1;
96         outb(val, DATA(base));
97
98         return 0;
99 }
100
101 static int aha1542_out(unsigned int base, u8 *buf, int len)
102 {
103         while (len--) {
104                 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
105                         return 1;
106                 outb(*buf++, DATA(base));
107         }
108         if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
109                 return 1;
110
111         return 0;
112 }
113
114 /* Only used at boot time, so we do not need to worry about latency as much
115    here */
116
117 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
118 {
119         while (len--) {
120                 if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
121                         return 1;
122                 *buf++ = inb(DATA(base));
123         }
124         return 0;
125 }
126
127 static int makecode(unsigned hosterr, unsigned scsierr)
128 {
129         switch (hosterr) {
130         case 0x0:
131         case 0xa:               /* Linked command complete without error and linked normally */
132         case 0xb:               /* Linked command complete without error, interrupt generated */
133                 hosterr = 0;
134                 break;
135
136         case 0x11:              /* Selection time out-The initiator selection or target
137                                    reselection was not complete within the SCSI Time out period */
138                 hosterr = DID_TIME_OUT;
139                 break;
140
141         case 0x12:              /* Data overrun/underrun-The target attempted to transfer more data
142                                    than was allocated by the Data Length field or the sum of the
143                                    Scatter / Gather Data Length fields. */
144
145         case 0x13:              /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
146
147         case 0x15:              /* MBO command was not 00, 01 or 02-The first byte of the CB was
148                                    invalid. This usually indicates a software failure. */
149
150         case 0x16:              /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
151                                    This usually indicates a software failure. */
152
153         case 0x17:              /* Linked CCB does not have the same LUN-A subsequent CCB of a set
154                                    of linked CCB's does not specify the same logical unit number as
155                                    the first. */
156         case 0x18:              /* Invalid Target Direction received from Host-The direction of a
157                                    Target Mode CCB was invalid. */
158
159         case 0x19:              /* Duplicate CCB Received in Target Mode-More than once CCB was
160                                    received to service data transfer between the same target LUN
161                                    and initiator SCSI ID in the same direction. */
162
163         case 0x1a:              /* Invalid CCB or Segment List Parameter-A segment list with a zero
164                                    length segment or invalid segment list boundaries was received.
165                                    A CCB parameter was invalid. */
166 #ifdef DEBUG
167                 printk("Aha1542: %x %x\n", hosterr, scsierr);
168 #endif
169                 hosterr = DID_ERROR;    /* Couldn't find any better */
170                 break;
171
172         case 0x14:              /* Target bus phase sequence failure-An invalid bus phase or bus
173                                    phase sequence was requested by the target. The host adapter
174                                    will generate a SCSI Reset Condition, notifying the host with
175                                    a SCRD interrupt */
176                 hosterr = DID_RESET;
177                 break;
178         default:
179                 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
180                 break;
181         }
182         return scsierr | (hosterr << 16);
183 }
184
185 static int aha1542_test_port(struct Scsi_Host *sh)
186 {
187         u8 inquiry_result[4];
188         int i;
189
190         /* Quick and dirty test for presence of the card. */
191         if (inb(STATUS(sh->io_port)) == 0xff)
192                 return 0;
193
194         /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
195
196         /* In case some other card was probing here, reset interrupts */
197         aha1542_intr_reset(sh->io_port);        /* reset interrupts, so they don't block */
198
199         outb(SRST | IRST /*|SCRST */ , CONTROL(sh->io_port));
200
201         mdelay(20);             /* Wait a little bit for things to settle down. */
202
203         /* Expect INIT and IDLE, any of the others are bad */
204         if (!wait_mask(STATUS(sh->io_port), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
205                 return 0;
206
207         /* Shouldn't have generated any interrupts during reset */
208         if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
209                 return 0;
210
211         /* Perform a host adapter inquiry instead so we do not need to set
212            up the mailboxes ahead of time */
213
214         aha1542_outb(sh->io_port, CMD_INQUIRY);
215
216         for (i = 0; i < 4; i++) {
217                 if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
218                         return 0;
219                 inquiry_result[i] = inb(DATA(sh->io_port));
220         }
221
222         /* Reading port should reset DF */
223         if (inb(STATUS(sh->io_port)) & DF)
224                 return 0;
225
226         /* When HACC, command is completed, and we're though testing */
227         if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
228                 return 0;
229
230         /* Clear interrupts */
231         outb(IRST, CONTROL(sh->io_port));
232
233         return 1;
234 }
235
236 static irqreturn_t aha1542_interrupt(int irq, void *dev_id)
237 {
238         struct Scsi_Host *sh = dev_id;
239         struct aha1542_hostdata *aha1542 = shost_priv(sh);
240         void (*my_done)(struct scsi_cmnd *) = NULL;
241         int errstatus, mbi, mbo, mbistatus;
242         int number_serviced;
243         unsigned long flags;
244         struct scsi_cmnd *tmp_cmd;
245         int flag;
246         struct mailbox *mb = aha1542->mb;
247         struct ccb *ccb = aha1542->ccb;
248
249 #ifdef DEBUG
250         {
251                 flag = inb(INTRFLAGS(sh->io_port));
252                 shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
253                 if (!(flag & ANYINTR))
254                         printk("no interrupt?");
255                 if (flag & MBIF)
256                         printk("MBIF ");
257                 if (flag & MBOA)
258                         printk("MBOF ");
259                 if (flag & HACC)
260                         printk("HACC ");
261                 if (flag & SCRD)
262                         printk("SCRD ");
263                 printk("status %02x\n", inb(STATUS(sh->io_port)));
264         };
265 #endif
266         number_serviced = 0;
267
268         spin_lock_irqsave(sh->host_lock, flags);
269         while (1) {
270                 flag = inb(INTRFLAGS(sh->io_port));
271
272                 /* Check for unusual interrupts.  If any of these happen, we should
273                    probably do something special, but for now just printing a message
274                    is sufficient.  A SCSI reset detected is something that we really
275                    need to deal with in some way. */
276                 if (flag & ~MBIF) {
277                         if (flag & MBOA)
278                                 printk("MBOF ");
279                         if (flag & HACC)
280                                 printk("HACC ");
281                         if (flag & SCRD)
282                                 printk("SCRD ");
283                 }
284                 aha1542_intr_reset(sh->io_port);
285
286                 mbi = aha1542->aha1542_last_mbi_used + 1;
287                 if (mbi >= 2 * AHA1542_MAILBOXES)
288                         mbi = AHA1542_MAILBOXES;
289
290                 do {
291                         if (mb[mbi].status != 0)
292                                 break;
293                         mbi++;
294                         if (mbi >= 2 * AHA1542_MAILBOXES)
295                                 mbi = AHA1542_MAILBOXES;
296                 } while (mbi != aha1542->aha1542_last_mbi_used);
297
298                 if (mb[mbi].status == 0) {
299                         spin_unlock_irqrestore(sh->host_lock, flags);
300                         /* Hmm, no mail.  Must have read it the last time around */
301                         if (!number_serviced)
302                                 shost_printk(KERN_WARNING, sh, "interrupt received, but no mail.\n");
303                         return IRQ_HANDLED;
304                 };
305
306                 mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
307                 mbistatus = mb[mbi].status;
308                 mb[mbi].status = 0;
309                 aha1542->aha1542_last_mbi_used = mbi;
310
311 #ifdef DEBUG
312                 if (ccb[mbo].tarstat | ccb[mbo].hastat)
313                         shost_printk(KERN_DEBUG, sh, "aha1542_command: returning %x (status %d)\n",
314                                ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
315 #endif
316
317                 if (mbistatus == 3)
318                         continue;       /* Aborted command not found */
319
320 #ifdef DEBUG
321                 shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
322 #endif
323
324                 tmp_cmd = aha1542->int_cmds[mbo];
325
326                 if (!tmp_cmd || !tmp_cmd->scsi_done) {
327                         spin_unlock_irqrestore(sh->host_lock, flags);
328                         shost_printk(KERN_WARNING, sh, "Unexpected interrupt\n");
329                         shost_printk(KERN_WARNING, sh, "tarstat=%x, hastat=%x idlun=%x ccb#=%d\n", ccb[mbo].tarstat,
330                                ccb[mbo].hastat, ccb[mbo].idlun, mbo);
331                         return IRQ_HANDLED;
332                 }
333                 my_done = tmp_cmd->scsi_done;
334                 kfree(tmp_cmd->host_scribble);
335                 tmp_cmd->host_scribble = NULL;
336                 /* Fetch the sense data, and tuck it away, in the required slot.  The
337                    Adaptec automatically fetches it, and there is no guarantee that
338                    we will still have it in the cdb when we come back */
339                 if (ccb[mbo].tarstat == 2)
340                         memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
341                                SCSI_SENSE_BUFFERSIZE);
342
343
344                 /* is there mail :-) */
345
346                 /* more error checking left out here */
347                 if (mbistatus != 1)
348                         /* This is surely wrong, but I don't know what's right */
349                         errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
350                 else
351                         errstatus = 0;
352
353 #ifdef DEBUG
354                 if (errstatus)
355                         shost_printk(KERN_DEBUG, sh, "(aha1542 error:%x %x %x) ", errstatus,
356                                ccb[mbo].hastat, ccb[mbo].tarstat);
357                 if (ccb[mbo].tarstat == 2)
358                         print_hex_dump_bytes("sense: ", DUMP_PREFIX_NONE, &ccb[mbo].cdb[ccb[mbo].cdblen], 12);
359                 if (errstatus)
360                         printk("aha1542_intr_handle: returning %6x\n", errstatus);
361 #endif
362                 tmp_cmd->result = errstatus;
363                 aha1542->int_cmds[mbo] = NULL;  /* This effectively frees up the mailbox slot, as
364                                                    far as queuecommand is concerned */
365                 my_done(tmp_cmd);
366                 number_serviced++;
367         };
368 }
369
370 static int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
371 {
372         struct aha1542_hostdata *aha1542 = shost_priv(sh);
373         u8 direction;
374         u8 target = cmd->device->id;
375         u8 lun = cmd->device->lun;
376         unsigned long flags;
377         int bufflen = scsi_bufflen(cmd);
378         int mbo, sg_count;
379         struct mailbox *mb = aha1542->mb;
380         struct ccb *ccb = aha1542->ccb;
381         struct chain *cptr;
382
383         if (*cmd->cmnd == REQUEST_SENSE) {
384                 /* Don't do the command - we have the sense data already */
385                 cmd->result = 0;
386                 cmd->scsi_done(cmd);
387                 return 0;
388         }
389 #ifdef DEBUG
390         {
391                 int i = -1;
392                 if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
393                         i = xscsi2int(cmd->cmnd + 2);
394                 else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
395                         i = scsi2int(cmd->cmnd + 2);
396                 shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d",
397                                                 target, *cmd->cmnd, i, bufflen);
398                 print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
399         }
400 #endif
401         if (bufflen) {  /* allocate memory before taking host_lock */
402                 sg_count = scsi_sg_count(cmd);
403                 cptr = kmalloc(sizeof(*cptr) * sg_count, GFP_KERNEL | GFP_DMA);
404                 if (!cptr)
405                         return SCSI_MLQUEUE_HOST_BUSY;
406         } else {
407                 sg_count = 0;
408                 cptr = NULL;
409         }
410
411         /* Use the outgoing mailboxes in a round-robin fashion, because this
412            is how the host adapter will scan for them */
413
414         spin_lock_irqsave(sh->host_lock, flags);
415         mbo = aha1542->aha1542_last_mbo_used + 1;
416         if (mbo >= AHA1542_MAILBOXES)
417                 mbo = 0;
418
419         do {
420                 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
421                         break;
422                 mbo++;
423                 if (mbo >= AHA1542_MAILBOXES)
424                         mbo = 0;
425         } while (mbo != aha1542->aha1542_last_mbo_used);
426
427         if (mb[mbo].status || aha1542->int_cmds[mbo])
428                 panic("Unable to find empty mailbox for aha1542.\n");
429
430         aha1542->int_cmds[mbo] = cmd;   /* This will effectively prevent someone else from
431                                            screwing with this cdb. */
432
433         aha1542->aha1542_last_mbo_used = mbo;
434
435 #ifdef DEBUG
436         shost_printk(KERN_DEBUG, sh, "Sending command (%d %p)...", mbo, cmd->scsi_done);
437 #endif
438
439         any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));   /* This gets trashed for some reason */
440
441         memset(&ccb[mbo], 0, sizeof(struct ccb));
442
443         ccb[mbo].cdblen = cmd->cmd_len;
444
445         direction = 0;
446         if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
447                 direction = 8;
448         else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
449                 direction = 16;
450
451         memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
452
453         if (bufflen) {
454                 struct scatterlist *sg;
455                 int i;
456
457                 ccb[mbo].op = 2;        /* SCSI Initiator Command  w/scatter-gather */
458                 cmd->host_scribble = (void *)cptr;
459                 scsi_for_each_sg(cmd, sg, sg_count, i) {
460                         any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
461                                                                 + sg->offset);
462                         any2scsi(cptr[i].datalen, sg->length);
463                 };
464                 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
465                 any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
466 #ifdef DEBUG
467                 shost_printk(KERN_DEBUG, sh, "cptr %p: ", cptr);
468                 print_hex_dump_bytes("cptr: ", DUMP_PREFIX_NONE, cptr, 18);
469 #endif
470         } else {
471                 ccb[mbo].op = 0;        /* SCSI Initiator Command */
472                 cmd->host_scribble = NULL;
473                 any2scsi(ccb[mbo].datalen, 0);
474                 any2scsi(ccb[mbo].dataptr, 0);
475         };
476         ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);     /*SCSI Target Id */
477         ccb[mbo].rsalen = 16;
478         ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
479         ccb[mbo].commlinkid = 0;
480
481 #ifdef DEBUG
482         print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
483         printk("aha1542_queuecommand: now waiting for interrupt ");
484 #endif
485         mb[mbo].status = 1;
486         aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
487         spin_unlock_irqrestore(sh->host_lock, flags);
488
489         return 0;
490 }
491
492 /* Initialize mailboxes */
493 static void setup_mailboxes(struct Scsi_Host *sh)
494 {
495         struct aha1542_hostdata *aha1542 = shost_priv(sh);
496         int i;
497         struct mailbox *mb = aha1542->mb;
498         struct ccb *ccb = aha1542->ccb;
499
500         u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
501
502         for (i = 0; i < AHA1542_MAILBOXES; i++) {
503                 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
504                 any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
505         };
506         aha1542_intr_reset(sh->io_port);        /* reset interrupts, so they don't block */
507         any2scsi((mb_cmd + 2), isa_virt_to_bus(mb));
508         if (aha1542_out(sh->io_port, mb_cmd, 5))
509                 shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
510         aha1542_intr_reset(sh->io_port);
511 }
512
513 static int aha1542_getconfig(struct Scsi_Host *sh)
514 {
515         u8 inquiry_result[3];
516         int i;
517         i = inb(STATUS(sh->io_port));
518         if (i & DF) {
519                 i = inb(DATA(sh->io_port));
520         };
521         aha1542_outb(sh->io_port, CMD_RETCONF);
522         aha1542_in(sh->io_port, inquiry_result, 3, 0);
523         if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
524                 shost_printk(KERN_ERR, sh, "error querying board settings\n");
525         aha1542_intr_reset(sh->io_port);
526         switch (inquiry_result[0]) {
527         case 0x80:
528                 sh->dma_channel = 7;
529                 break;
530         case 0x40:
531                 sh->dma_channel = 6;
532                 break;
533         case 0x20:
534                 sh->dma_channel = 5;
535                 break;
536         case 0x01:
537                 sh->dma_channel = 0;
538                 break;
539         case 0:
540                 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
541                    Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
542                 sh->dma_channel = 0xFF;
543                 break;
544         default:
545                 shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
546                 return -1;
547         };
548         switch (inquiry_result[1]) {
549         case 0x40:
550                 sh->irq = 15;
551                 break;
552         case 0x20:
553                 sh->irq = 14;
554                 break;
555         case 0x8:
556                 sh->irq = 12;
557                 break;
558         case 0x4:
559                 sh->irq = 11;
560                 break;
561         case 0x2:
562                 sh->irq = 10;
563                 break;
564         case 0x1:
565                 sh->irq = 9;
566                 break;
567         default:
568                 shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
569                 return -1;
570         };
571         sh->this_id = inquiry_result[2] & 7;
572         return 0;
573 }
574
575 /* This function should only be called for 1542C boards - we can detect
576    the special firmware settings and unlock the board */
577
578 static int aha1542_mbenable(struct Scsi_Host *sh)
579 {
580         static u8 mbenable_cmd[3];
581         static u8 mbenable_result[2];
582         int retval;
583
584         retval = BIOS_TRANSLATION_6432;
585
586         aha1542_outb(sh->io_port, CMD_EXTBIOS);
587         if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
588                 return retval;
589         if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
590                 goto fail;
591         aha1542_intr_reset(sh->io_port);
592
593         if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
594                 mbenable_cmd[0] = CMD_MBENABLE;
595                 mbenable_cmd[1] = 0;
596                 mbenable_cmd[2] = mbenable_result[1];
597
598                 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
599                         retval = BIOS_TRANSLATION_25563;
600
601                 if (aha1542_out(sh->io_port, mbenable_cmd, 3))
602                         goto fail;
603         };
604         while (0) {
605 fail:
606                 shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
607         }
608         aha1542_intr_reset(sh->io_port);
609         return retval;
610 }
611
612 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
613 static int aha1542_query(struct Scsi_Host *sh)
614 {
615         struct aha1542_hostdata *aha1542 = shost_priv(sh);
616         u8 inquiry_result[4];
617         int i;
618         i = inb(STATUS(sh->io_port));
619         if (i & DF) {
620                 i = inb(DATA(sh->io_port));
621         };
622         aha1542_outb(sh->io_port, CMD_INQUIRY);
623         aha1542_in(sh->io_port, inquiry_result, 4, 0);
624         if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
625                 shost_printk(KERN_ERR, sh, "error querying card type\n");
626         aha1542_intr_reset(sh->io_port);
627
628         aha1542->bios_translation = BIOS_TRANSLATION_6432;      /* Default case */
629
630         /* For an AHA1740 series board, we ignore the board since there is a
631            hardware bug which can lead to wrong blocks being returned if the board
632            is operating in the 1542 emulation mode.  Since there is an extended mode
633            driver, we simply ignore the board and let the 1740 driver pick it up.
634          */
635
636         if (inquiry_result[0] == 0x43) {
637                 shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
638                 return 1;
639         };
640
641         /* Always call this - boards that do not support extended bios translation
642            will ignore the command, and we will set the proper default */
643
644         aha1542->bios_translation = aha1542_mbenable(sh);
645
646         return 0;
647 }
648
649 static u8 dma_speed_hw(int dma_speed)
650 {
651         switch (dma_speed) {
652         case 5:
653                 return 0x00;
654         case 6:
655                 return 0x04;
656         case 7:
657                 return 0x01;
658         case 8:
659                 return 0x02;
660         case 10:
661                 return 0x03;
662         }
663
664         return 0xff;    /* invalid */
665 }
666
667 /* Set the Bus on/off-times as not to ruin floppy performance */
668 static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
669 {
670         if (bus_on > 0) {
671                 u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
672
673                 aha1542_intr_reset(sh->io_port);
674                 if (aha1542_out(sh->io_port, oncmd, 2))
675                         goto fail;
676         }
677
678         if (bus_off > 0) {
679                 u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
680
681                 aha1542_intr_reset(sh->io_port);
682                 if (aha1542_out(sh->io_port, offcmd, 2))
683                         goto fail;
684         }
685
686         if (dma_speed_hw(dma_speed) != 0xff) {
687                 u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
688
689                 aha1542_intr_reset(sh->io_port);
690                 if (aha1542_out(sh->io_port, dmacmd, 2))
691                         goto fail;
692         }
693         aha1542_intr_reset(sh->io_port);
694         return;
695 fail:
696         shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
697         aha1542_intr_reset(sh->io_port);
698 }
699
700 /* return non-zero on detection */
701 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
702 {
703         unsigned int base_io = io[indx];
704         struct Scsi_Host *sh;
705         struct aha1542_hostdata *aha1542;
706         char dma_info[] = "no DMA";
707
708         if (base_io == 0)
709                 return NULL;
710
711         if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
712                 return NULL;
713
714         sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
715         if (!sh)
716                 goto release;
717         aha1542 = shost_priv(sh);
718
719         sh->unique_id = base_io;
720         sh->io_port = base_io;
721         sh->n_io_port = AHA1542_REGION_SIZE;
722         aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
723         aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
724
725         if (!aha1542_test_port(sh))
726                 goto unregister;
727
728         aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
729         if (aha1542_query(sh))
730                 goto unregister;
731         if (aha1542_getconfig(sh) == -1)
732                 goto unregister;
733
734         if (sh->dma_channel != 0xFF)
735                 snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
736         shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
737                                 sh->this_id, base_io, sh->irq, dma_info);
738         if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
739                 shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
740
741         setup_mailboxes(sh);
742
743         if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
744                 shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
745                 goto unregister;
746         }
747         if (sh->dma_channel != 0xFF) {
748                 if (request_dma(sh->dma_channel, "aha1542")) {
749                         shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
750                         goto free_irq;
751                 }
752                 if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
753                         set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
754                         enable_dma(sh->dma_channel);
755                 }
756         }
757
758         if (scsi_add_host(sh, pdev))
759                 goto free_dma;
760
761         scsi_scan_host(sh);
762
763         return sh;
764 free_dma:
765         if (sh->dma_channel != 0xff)
766                 free_dma(sh->dma_channel);
767 free_irq:
768         free_irq(sh->irq, sh);
769 unregister:
770         scsi_host_put(sh);
771 release:
772         release_region(base_io, AHA1542_REGION_SIZE);
773
774         return NULL;
775 }
776
777 static int aha1542_release(struct Scsi_Host *sh)
778 {
779         scsi_remove_host(sh);
780         if (sh->dma_channel != 0xff)
781                 free_dma(sh->dma_channel);
782         if (sh->irq)
783                 free_irq(sh->irq, sh);
784         if (sh->io_port && sh->n_io_port)
785                 release_region(sh->io_port, sh->n_io_port);
786         scsi_host_put(sh);
787         return 0;
788 }
789
790
791 /*
792  * This is a device reset.  This is handled by sending a special command
793  * to the device.
794  */
795 static int aha1542_dev_reset(struct scsi_cmnd *cmd)
796 {
797         struct Scsi_Host *sh = cmd->device->host;
798         struct aha1542_hostdata *aha1542 = shost_priv(sh);
799         unsigned long flags;
800         struct mailbox *mb = aha1542->mb;
801         u8 target = cmd->device->id;
802         u8 lun = cmd->device->lun;
803         int mbo;
804         struct ccb *ccb = aha1542->ccb;
805
806         spin_lock_irqsave(sh->host_lock, flags);
807         mbo = aha1542->aha1542_last_mbo_used + 1;
808         if (mbo >= AHA1542_MAILBOXES)
809                 mbo = 0;
810
811         do {
812                 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
813                         break;
814                 mbo++;
815                 if (mbo >= AHA1542_MAILBOXES)
816                         mbo = 0;
817         } while (mbo != aha1542->aha1542_last_mbo_used);
818
819         if (mb[mbo].status || aha1542->int_cmds[mbo])
820                 panic("Unable to find empty mailbox for aha1542.\n");
821
822         aha1542->int_cmds[mbo] = cmd;   /* This will effectively
823                                            prevent someone else from
824                                            screwing with this cdb. */
825
826         aha1542->aha1542_last_mbo_used = mbo;
827
828         any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));   /* This gets trashed for some reason */
829
830         memset(&ccb[mbo], 0, sizeof(struct ccb));
831
832         ccb[mbo].op = 0x81;     /* BUS DEVICE RESET */
833
834         ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);         /*SCSI Target Id */
835
836         ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
837         ccb[mbo].commlinkid = 0;
838
839         /* 
840          * Now tell the 1542 to flush all pending commands for this 
841          * target 
842          */
843         aha1542_outb(sh->io_port, CMD_START_SCSI);
844         spin_unlock_irqrestore(sh->host_lock, flags);
845
846         scmd_printk(KERN_WARNING, cmd,
847                 "Trying device reset for target\n");
848
849         return SUCCESS;
850 }
851
852 static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
853 {
854         struct Scsi_Host *sh = cmd->device->host;
855         struct aha1542_hostdata *aha1542 = shost_priv(sh);
856         unsigned long flags;
857         int i;
858
859         spin_lock_irqsave(sh->host_lock, flags);
860         /* 
861          * This does a scsi reset for all devices on the bus.
862          * In principle, we could also reset the 1542 - should
863          * we do this?  Try this first, and we can add that later
864          * if it turns out to be useful.
865          */
866         outb(reset_cmd, CONTROL(cmd->device->host->io_port));
867
868         if (!wait_mask(STATUS(cmd->device->host->io_port),
869              STATMASK, IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
870                 spin_unlock_irqrestore(sh->host_lock, flags);
871                 return FAILED;
872         }
873
874         /*
875          * We need to do this too before the 1542 can interact with
876          * us again after host reset.
877          */
878         if (reset_cmd & HRST)
879                 setup_mailboxes(cmd->device->host);
880
881         /*
882          * Now try to pick up the pieces.  For all pending commands,
883          * free any internal data structures, and basically clear things
884          * out.  We do not try and restart any commands or anything - 
885          * the strategy handler takes care of that crap.
886          */
887         shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
888
889         for (i = 0; i < AHA1542_MAILBOXES; i++) {
890                 if (aha1542->int_cmds[i] != NULL) {
891                         struct scsi_cmnd *tmp_cmd;
892                         tmp_cmd = aha1542->int_cmds[i];
893
894                         if (tmp_cmd->device->soft_reset) {
895                                 /*
896                                  * If this device implements the soft reset option,
897                                  * then it is still holding onto the command, and
898                                  * may yet complete it.  In this case, we don't
899                                  * flush the data.
900                                  */
901                                 continue;
902                         }
903                         kfree(tmp_cmd->host_scribble);
904                         tmp_cmd->host_scribble = NULL;
905                         aha1542->int_cmds[i] = NULL;
906                         aha1542->mb[i].status = 0;
907                 }
908         }
909
910         spin_unlock_irqrestore(sh->host_lock, flags);
911         return SUCCESS;
912 }
913
914 static int aha1542_bus_reset(struct scsi_cmnd *cmd)
915 {
916         return aha1542_reset(cmd, SCRST);
917 }
918
919 static int aha1542_host_reset(struct scsi_cmnd *cmd)
920 {
921         return aha1542_reset(cmd, HRST | SCRST);
922 }
923
924 static int aha1542_biosparam(struct scsi_device *sdev,
925                 struct block_device *bdev, sector_t capacity, int geom[])
926 {
927         struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
928
929         if (capacity >= 0x200000 &&
930                         aha1542->bios_translation == BIOS_TRANSLATION_25563) {
931                 /* Please verify that this is the same as what DOS returns */
932                 geom[0] = 255;  /* heads */
933                 geom[1] = 63;   /* sectors */
934         } else {
935                 geom[0] = 64;   /* heads */
936                 geom[1] = 32;   /* sectors */
937         }
938         geom[2] = sector_div(capacity, geom[0] * geom[1]);      /* cylinders */
939
940         return 0;
941 }
942 MODULE_LICENSE("GPL");
943
944 static struct scsi_host_template driver_template = {
945         .module                 = THIS_MODULE,
946         .proc_name              = "aha1542",
947         .name                   = "Adaptec 1542",
948         .queuecommand           = aha1542_queuecommand,
949         .eh_device_reset_handler= aha1542_dev_reset,
950         .eh_bus_reset_handler   = aha1542_bus_reset,
951         .eh_host_reset_handler  = aha1542_host_reset,
952         .bios_param             = aha1542_biosparam,
953         .can_queue              = AHA1542_MAILBOXES, 
954         .this_id                = 7,
955         .sg_tablesize           = 16,
956         .unchecked_isa_dma      = 1, 
957         .use_clustering         = ENABLE_CLUSTERING,
958 };
959
960 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
961 {
962         struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
963
964         if (!sh)
965                 return 0;
966
967         dev_set_drvdata(pdev, sh);
968         return 1;
969 }
970
971 static int aha1542_isa_remove(struct device *pdev,
972                                     unsigned int ndev)
973 {
974         aha1542_release(dev_get_drvdata(pdev));
975         dev_set_drvdata(pdev, NULL);
976         return 0;
977 }
978
979 static struct isa_driver aha1542_isa_driver = {
980         .match          = aha1542_isa_match,
981         .remove         = aha1542_isa_remove,
982         .driver         = {
983                 .name   = "aha1542"
984         },
985 };
986 static int isa_registered;
987
988 #ifdef CONFIG_PNP
989 static struct pnp_device_id aha1542_pnp_ids[] = {
990         { .id = "ADP1542" },
991         { .id = "" }
992 };
993 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
994
995 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
996 {
997         int indx;
998         struct Scsi_Host *sh;
999
1000         for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1001                 if (io[indx])
1002                         continue;
1003
1004                 if (pnp_activate_dev(pdev) < 0)
1005                         continue;
1006
1007                 io[indx] = pnp_port_start(pdev, 0);
1008
1009                 /* The card can be queried for its DMA, we have
1010                    the DMA set up that is enough */
1011
1012                 dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
1013         }
1014
1015         sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1016         if (!sh)
1017                 return -ENODEV;
1018
1019         pnp_set_drvdata(pdev, sh);
1020         return 0;
1021 }
1022
1023 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1024 {
1025         aha1542_release(pnp_get_drvdata(pdev));
1026         pnp_set_drvdata(pdev, NULL);
1027 }
1028
1029 static struct pnp_driver aha1542_pnp_driver = {
1030         .name           = "aha1542",
1031         .id_table       = aha1542_pnp_ids,
1032         .probe          = aha1542_pnp_probe,
1033         .remove         = aha1542_pnp_remove,
1034 };
1035 static int pnp_registered;
1036 #endif /* CONFIG_PNP */
1037
1038 static int __init aha1542_init(void)
1039 {
1040         int ret = 0;
1041
1042 #ifdef CONFIG_PNP
1043         if (isapnp) {
1044                 ret = pnp_register_driver(&aha1542_pnp_driver);
1045                 if (!ret)
1046                         pnp_registered = 1;
1047         }
1048 #endif
1049         ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1050         if (!ret)
1051                 isa_registered = 1;
1052
1053 #ifdef CONFIG_PNP
1054         if (pnp_registered)
1055                 ret = 0;
1056 #endif
1057         if (isa_registered)
1058                 ret = 0;
1059
1060         return ret;
1061 }
1062
1063 static void __exit aha1542_exit(void)
1064 {
1065 #ifdef CONFIG_PNP
1066         if (pnp_registered)
1067                 pnp_unregister_driver(&aha1542_pnp_driver);
1068 #endif
1069         if (isa_registered)
1070                 isa_unregister_driver(&aha1542_isa_driver);
1071 }
1072
1073 module_init(aha1542_init);
1074 module_exit(aha1542_exit);