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