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