Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Driver for Future Domain TMC-16x0 and TMC-3260 SCSI host adapters
  4 * Copyright 2019 Ondrej Zary
  5 *
  6 * Original driver by
  7 * Rickard E. Faith, faith@cs.unc.edu
  8 *
  9 * Future Domain BIOS versions supported for autodetect:
 10 *    2.0, 3.0, 3.2, 3.4 (1.0), 3.5 (2.0), 3.6, 3.61
 11 * Chips supported:
 12 *    TMC-1800, TMC-18C50, TMC-18C30, TMC-36C70
 13 * Boards supported:
 14 *    Future Domain TMC-1650, TMC-1660, TMC-1670, TMC-1680, TMC-1610M/MER/MEX
 15 *    Future Domain TMC-3260 (PCI)
 16 *    Quantum ISA-200S, ISA-250MG
 17 *    Adaptec AHA-2920A (PCI) [BUT *NOT* AHA-2920C -- use aic7xxx instead]
 18 *    IBM ?
 19 *
 20 * NOTE:
 21 *
 22 * The Adaptec AHA-2920C has an Adaptec AIC-7850 chip on it.
 23 * Use the aic7xxx driver for this board.
 24 *
 25 * The Adaptec AHA-2920A has a Future Domain chip on it, so this is the right
 26 * driver for that card.  Unfortunately, the boxes will probably just say
 27 * "2920", so you'll have to look on the card for a Future Domain logo, or a
 28 * letter after the 2920.
 29 *
 30 * If you have a TMC-8xx or TMC-9xx board, then this is not the driver for
 31 * your board.
 32 *
 33 * DESCRIPTION:
 34 *
 35 * This is the Linux low-level SCSI driver for Future Domain TMC-1660/1680
 36 * TMC-1650/1670, and TMC-3260 SCSI host adapters.  The 1650 and 1670 have a
 37 * 25-pin external connector, whereas the 1660 and 1680 have a SCSI-2 50-pin
 38 * high-density external connector.  The 1670 and 1680 have floppy disk
 39 * controllers built in.  The TMC-3260 is a PCI bus card.
 40 *
 41 * Future Domain's older boards are based on the TMC-1800 chip, and this
 42 * driver was originally written for a TMC-1680 board with the TMC-1800 chip.
 43 * More recently, boards are being produced with the TMC-18C50 and TMC-18C30
 44 * chips.
 45 *
 46 * Please note that the drive ordering that Future Domain implemented in BIOS
 47 * versions 3.4 and 3.5 is the opposite of the order (currently) used by the
 48 * rest of the SCSI industry.
 49 *
 50 *
 51 * REFERENCES USED:
 52 *
 53 * "TMC-1800 SCSI Chip Specification (FDC-1800T)", Future Domain Corporation,
 54 * 1990.
 55 *
 56 * "Technical Reference Manual: 18C50 SCSI Host Adapter Chip", Future Domain
 57 * Corporation, January 1992.
 58 *
 59 * "LXT SCSI Products: Specifications and OEM Technical Manual (Revision
 60 * B/September 1991)", Maxtor Corporation, 1991.
 61 *
 62 * "7213S product Manual (Revision P3)", Maxtor Corporation, 1992.
 63 *
 64 * "Draft Proposed American National Standard: Small Computer System
 65 * Interface - 2 (SCSI-2)", Global Engineering Documents. (X3T9.2/86-109,
 66 * revision 10h, October 17, 1991)
 67 *
 68 * Private communications, Drew Eckhardt (drew@cs.colorado.edu) and Eric
 69 * Youngdale (ericy@cais.com), 1992.
 70 *
 71 * Private communication, Tuong Le (Future Domain Engineering department),
 72 * 1994. (Disk geometry computations for Future Domain BIOS version 3.4, and
 73 * TMC-18C30 detection.)
 74 *
 75 * Hogan, Thom. The Programmer's PC Sourcebook. Microsoft Press, 1988. Page
 76 * 60 (2.39: Disk Partition Table Layout).
 77 *
 78 * "18C30 Technical Reference Manual", Future Domain Corporation, 1993, page
 79 * 6-1.
 80 */
 81
 82#include <linux/module.h>
 83#include <linux/interrupt.h>
 84#include <linux/delay.h>
 85#include <linux/pci.h>
 86#include <linux/workqueue.h>
 87#include <scsi/scsicam.h>
 88#include <scsi/scsi_cmnd.h>
 89#include <scsi/scsi_device.h>
 90#include <scsi/scsi_host.h>
 91#include "fdomain.h"
 92
 93/*
 94 * FIFO_COUNT: The host adapter has an 8K cache (host adapters based on the
 95 * 18C30 chip have a 2k cache).  When this many 512 byte blocks are filled by
 96 * the SCSI device, an interrupt will be raised.  Therefore, this could be as
 97 * low as 0, or as high as 16.  Note, however, that values which are too high
 98 * or too low seem to prevent any interrupts from occurring, and thereby lock
 99 * up the machine.
100 */
101#define FIFO_COUNT	2	/* Number of 512 byte blocks before INTR */
102#define PARITY_MASK	ACTL_PAREN	/* Parity enabled, 0 = disabled */
103
104enum chip_type {
105	unknown		= 0x00,
106	tmc1800		= 0x01,
107	tmc18c50	= 0x02,
108	tmc18c30	= 0x03,
109};
110
111struct fdomain {
112	int base;
113	struct scsi_cmnd *cur_cmd;
114	enum chip_type chip;
115	struct work_struct work;
116};
117
118static struct scsi_pointer *fdomain_scsi_pointer(struct scsi_cmnd *cmd)
119{
120	return scsi_cmd_priv(cmd);
121}
122
123static inline void fdomain_make_bus_idle(struct fdomain *fd)
124{
125	outb(0, fd->base + REG_BCTL);
126	outb(0, fd->base + REG_MCTL);
127	if (fd->chip == tmc18c50 || fd->chip == tmc18c30)
128		/* Clear forced intr. */
129		outb(ACTL_RESET | ACTL_CLRFIRQ | PARITY_MASK,
130		     fd->base + REG_ACTL);
131	else
132		outb(ACTL_RESET | PARITY_MASK, fd->base + REG_ACTL);
133}
134
135static enum chip_type fdomain_identify(int port)
136{
137	u16 id = inb(port + REG_ID_LSB) | inb(port + REG_ID_MSB) << 8;
138
139	switch (id) {
140	case 0x6127:
141		return tmc1800;
142	case 0x60e9: /* 18c50 or 18c30 */
143		break;
144	default:
145		return unknown;
146	}
147
148	/* Try to toggle 32-bit mode. This only works on an 18c30 chip. */
149	outb(CFG2_32BIT, port + REG_CFG2);
150	if ((inb(port + REG_CFG2) & CFG2_32BIT)) {
151		outb(0, port + REG_CFG2);
152		if ((inb(port + REG_CFG2) & CFG2_32BIT) == 0)
153			return tmc18c30;
154	}
155	/* If that failed, we are an 18c50. */
156	return tmc18c50;
157}
158
159static int fdomain_test_loopback(int base)
160{
161	int i;
162
163	for (i = 0; i < 255; i++) {
164		outb(i, base + REG_LOOPBACK);
165		if (inb(base + REG_LOOPBACK) != i)
166			return 1;
167	}
168
169	return 0;
170}
171
172static void fdomain_reset(int base)
173{
174	outb(BCTL_RST, base + REG_BCTL);
175	mdelay(20);
176	outb(0, base + REG_BCTL);
177	mdelay(1150);
178	outb(0, base + REG_MCTL);
179	outb(PARITY_MASK, base + REG_ACTL);
180}
181
182static int fdomain_select(struct Scsi_Host *sh, int target)
183{
184	int status;
185	unsigned long timeout;
186	struct fdomain *fd = shost_priv(sh);
187
188	outb(BCTL_BUSEN | BCTL_SEL, fd->base + REG_BCTL);
189	outb(BIT(sh->this_id) | BIT(target), fd->base + REG_SCSI_DATA_NOACK);
190
191	/* Stop arbitration and enable parity */
192	outb(PARITY_MASK, fd->base + REG_ACTL);
193
194	timeout = 350;	/* 350 msec */
195
196	do {
197		status = inb(fd->base + REG_BSTAT);
198		if (status & BSTAT_BSY) {
199			/* Enable SCSI Bus */
200			/* (on error, should make bus idle with 0) */
201			outb(BCTL_BUSEN, fd->base + REG_BCTL);
202			return 0;
203		}
204		mdelay(1);
205	} while (--timeout);
206	fdomain_make_bus_idle(fd);
207	return 1;
208}
209
210static void fdomain_finish_cmd(struct fdomain *fd)
211{
212	outb(0, fd->base + REG_ICTL);
213	fdomain_make_bus_idle(fd);
214	scsi_done(fd->cur_cmd);
 
215	fd->cur_cmd = NULL;
216}
217
218static void fdomain_read_data(struct scsi_cmnd *cmd)
219{
220	struct fdomain *fd = shost_priv(cmd->device->host);
221	unsigned char *virt, *ptr;
222	size_t offset, len;
223
224	while ((len = inw(fd->base + REG_FIFO_COUNT)) > 0) {
225		offset = scsi_bufflen(cmd) - scsi_get_resid(cmd);
226		virt = scsi_kmap_atomic_sg(scsi_sglist(cmd), scsi_sg_count(cmd),
227					   &offset, &len);
228		ptr = virt + offset;
229		if (len & 1)
230			*ptr++ = inb(fd->base + REG_FIFO);
231		if (len > 1)
232			insw(fd->base + REG_FIFO, ptr, len >> 1);
233		scsi_set_resid(cmd, scsi_get_resid(cmd) - len);
234		scsi_kunmap_atomic_sg(virt);
235	}
236}
237
238static void fdomain_write_data(struct scsi_cmnd *cmd)
239{
240	struct fdomain *fd = shost_priv(cmd->device->host);
241	/* 8k FIFO for pre-tmc18c30 chips, 2k FIFO for tmc18c30 */
242	int FIFO_Size = fd->chip == tmc18c30 ? 0x800 : 0x2000;
243	unsigned char *virt, *ptr;
244	size_t offset, len;
245
246	while ((len = FIFO_Size - inw(fd->base + REG_FIFO_COUNT)) > 512) {
247		offset = scsi_bufflen(cmd) - scsi_get_resid(cmd);
248		if (len + offset > scsi_bufflen(cmd)) {
249			len = scsi_bufflen(cmd) - offset;
250			if (len == 0)
251				break;
252		}
253		virt = scsi_kmap_atomic_sg(scsi_sglist(cmd), scsi_sg_count(cmd),
254					   &offset, &len);
255		ptr = virt + offset;
256		if (len & 1)
257			outb(*ptr++, fd->base + REG_FIFO);
258		if (len > 1)
259			outsw(fd->base + REG_FIFO, ptr, len >> 1);
260		scsi_set_resid(cmd, scsi_get_resid(cmd) - len);
261		scsi_kunmap_atomic_sg(virt);
262	}
263}
264
265static void fdomain_work(struct work_struct *work)
266{
267	struct fdomain *fd = container_of(work, struct fdomain, work);
268	struct Scsi_Host *sh = container_of((void *)fd, struct Scsi_Host,
269					    hostdata);
270	struct scsi_cmnd *cmd = fd->cur_cmd;
271	struct scsi_pointer *scsi_pointer = fdomain_scsi_pointer(cmd);
272	unsigned long flags;
273	int status;
274	int done = 0;
275
276	spin_lock_irqsave(sh->host_lock, flags);
277
278	if (scsi_pointer->phase & in_arbitration) {
279		status = inb(fd->base + REG_ASTAT);
280		if (!(status & ASTAT_ARB)) {
281			set_host_byte(cmd, DID_BUS_BUSY);
282			fdomain_finish_cmd(fd);
283			goto out;
284		}
285		scsi_pointer->phase = in_selection;
286
287		outb(ICTL_SEL | FIFO_COUNT, fd->base + REG_ICTL);
288		outb(BCTL_BUSEN | BCTL_SEL, fd->base + REG_BCTL);
289		outb(BIT(cmd->device->host->this_id) | BIT(scmd_id(cmd)),
290		     fd->base + REG_SCSI_DATA_NOACK);
291		/* Stop arbitration and enable parity */
292		outb(ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL);
293		goto out;
294	} else if (scsi_pointer->phase & in_selection) {
295		status = inb(fd->base + REG_BSTAT);
296		if (!(status & BSTAT_BSY)) {
297			/* Try again, for slow devices */
298			if (fdomain_select(cmd->device->host, scmd_id(cmd))) {
299				set_host_byte(cmd, DID_NO_CONNECT);
300				fdomain_finish_cmd(fd);
301				goto out;
302			}
303			/* Stop arbitration and enable parity */
304			outb(ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL);
305		}
306		scsi_pointer->phase = in_other;
307		outb(ICTL_FIFO | ICTL_REQ | FIFO_COUNT, fd->base + REG_ICTL);
308		outb(BCTL_BUSEN, fd->base + REG_BCTL);
309		goto out;
310	}
311
312	/* fdomain_scsi_pointer(cur_cmd)->phase == in_other: this is the body of the routine */
313	status = inb(fd->base + REG_BSTAT);
314
315	if (status & BSTAT_REQ) {
316		switch (status & (BSTAT_MSG | BSTAT_CMD | BSTAT_IO)) {
317		case BSTAT_CMD:	/* COMMAND OUT */
318			outb(cmd->cmnd[scsi_pointer->sent_command++],
319			     fd->base + REG_SCSI_DATA);
320			break;
321		case 0:	/* DATA OUT -- tmc18c50/tmc18c30 only */
322			if (fd->chip != tmc1800 && !scsi_pointer->have_data_in) {
323				scsi_pointer->have_data_in = -1;
324				outb(ACTL_IRQEN | ACTL_FIFOWR | ACTL_FIFOEN |
325				     PARITY_MASK, fd->base + REG_ACTL);
326			}
327			break;
328		case BSTAT_IO:	/* DATA IN -- tmc18c50/tmc18c30 only */
329			if (fd->chip != tmc1800 && !scsi_pointer->have_data_in) {
330				scsi_pointer->have_data_in = 1;
331				outb(ACTL_IRQEN | ACTL_FIFOEN | PARITY_MASK,
332				     fd->base + REG_ACTL);
333			}
334			break;
335		case BSTAT_CMD | BSTAT_IO:	/* STATUS IN */
336			scsi_pointer->Status = inb(fd->base + REG_SCSI_DATA);
337			break;
338		case BSTAT_MSG | BSTAT_CMD:	/* MESSAGE OUT */
339			outb(MESSAGE_REJECT, fd->base + REG_SCSI_DATA);
340			break;
341		case BSTAT_MSG | BSTAT_CMD | BSTAT_IO:	/* MESSAGE IN */
342			scsi_pointer->Message = inb(fd->base + REG_SCSI_DATA);
343			if (scsi_pointer->Message == COMMAND_COMPLETE)
344				++done;
345			break;
346		}
347	}
348
349	if (fd->chip == tmc1800 && !scsi_pointer->have_data_in &&
350	    scsi_pointer->sent_command >= cmd->cmd_len) {
351		if (cmd->sc_data_direction == DMA_TO_DEVICE) {
352			scsi_pointer->have_data_in = -1;
353			outb(ACTL_IRQEN | ACTL_FIFOWR | ACTL_FIFOEN |
354			     PARITY_MASK, fd->base + REG_ACTL);
355		} else {
356			scsi_pointer->have_data_in = 1;
357			outb(ACTL_IRQEN | ACTL_FIFOEN | PARITY_MASK,
358			     fd->base + REG_ACTL);
359		}
360	}
361
362	if (scsi_pointer->have_data_in == -1) /* DATA OUT */
363		fdomain_write_data(cmd);
364
365	if (scsi_pointer->have_data_in == 1) /* DATA IN */
366		fdomain_read_data(cmd);
367
368	if (done) {
369		set_status_byte(cmd, scsi_pointer->Status);
370		set_host_byte(cmd, DID_OK);
371		scsi_msg_to_host_byte(cmd, scsi_pointer->Message);
372		fdomain_finish_cmd(fd);
373	} else {
374		if (scsi_pointer->phase & disconnect) {
375			outb(ICTL_FIFO | ICTL_SEL | ICTL_REQ | FIFO_COUNT,
376			     fd->base + REG_ICTL);
377			outb(0, fd->base + REG_BCTL);
378		} else
379			outb(ICTL_FIFO | ICTL_REQ | FIFO_COUNT,
380			     fd->base + REG_ICTL);
381	}
382out:
383	spin_unlock_irqrestore(sh->host_lock, flags);
384}
385
386static irqreturn_t fdomain_irq(int irq, void *dev_id)
387{
388	struct fdomain *fd = dev_id;
389
390	/* Is it our IRQ? */
391	if ((inb(fd->base + REG_ASTAT) & ASTAT_IRQ) == 0)
392		return IRQ_NONE;
393
394	outb(0, fd->base + REG_ICTL);
395
396	/* We usually have one spurious interrupt after each command. */
397	if (!fd->cur_cmd)	/* Spurious interrupt */
398		return IRQ_NONE;
399
400	schedule_work(&fd->work);
401
402	return IRQ_HANDLED;
403}
404
405static int fdomain_queue(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
406{
407	struct scsi_pointer *scsi_pointer = fdomain_scsi_pointer(cmd);
408	struct fdomain *fd = shost_priv(cmd->device->host);
409	unsigned long flags;
410
411	scsi_pointer->Status		= 0;
412	scsi_pointer->Message		= 0;
413	scsi_pointer->have_data_in	= 0;
414	scsi_pointer->sent_command	= 0;
415	scsi_pointer->phase		= in_arbitration;
416	scsi_set_resid(cmd, scsi_bufflen(cmd));
417
418	spin_lock_irqsave(sh->host_lock, flags);
419
420	fd->cur_cmd = cmd;
421
422	fdomain_make_bus_idle(fd);
423
424	/* Start arbitration */
425	outb(0, fd->base + REG_ICTL);
426	outb(0, fd->base + REG_BCTL);	/* Disable data drivers */
427	/* Set our id bit */
428	outb(BIT(cmd->device->host->this_id), fd->base + REG_SCSI_DATA_NOACK);
429	outb(ICTL_ARB, fd->base + REG_ICTL);
430	/* Start arbitration */
431	outb(ACTL_ARB | ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL);
432
433	spin_unlock_irqrestore(sh->host_lock, flags);
434
435	return 0;
436}
437
438static int fdomain_abort(struct scsi_cmnd *cmd)
439{
440	struct Scsi_Host *sh = cmd->device->host;
441	struct fdomain *fd = shost_priv(sh);
442	unsigned long flags;
443
444	if (!fd->cur_cmd)
445		return FAILED;
446
447	spin_lock_irqsave(sh->host_lock, flags);
448
449	fdomain_make_bus_idle(fd);
450	fdomain_scsi_pointer(fd->cur_cmd)->phase |= aborted;
 
451
452	/* Aborts are not done well. . . */
453	set_host_byte(fd->cur_cmd, DID_ABORT);
454	fdomain_finish_cmd(fd);
455	spin_unlock_irqrestore(sh->host_lock, flags);
456	return SUCCESS;
457}
458
459static int fdomain_host_reset(struct scsi_cmnd *cmd)
460{
461	struct Scsi_Host *sh = cmd->device->host;
462	struct fdomain *fd = shost_priv(sh);
463	unsigned long flags;
464
465	spin_lock_irqsave(sh->host_lock, flags);
466	fdomain_reset(fd->base);
467	spin_unlock_irqrestore(sh->host_lock, flags);
468	return SUCCESS;
469}
470
471static int fdomain_biosparam(struct scsi_device *sdev,
472			     struct block_device *bdev,	sector_t capacity,
473			     int geom[])
474{
475	unsigned char *p = scsi_bios_ptable(bdev);
476
477	if (p && p[65] == 0xaa && p[64] == 0x55 /* Partition table valid */
478	    && p[4]) {	 /* Partition type */
479		geom[0] = p[5] + 1;	/* heads */
480		geom[1] = p[6] & 0x3f;	/* sectors */
481	} else {
482		if (capacity >= 0x7e0000) {
483			geom[0] = 255;	/* heads */
484			geom[1] = 63;	/* sectors */
485		} else if (capacity >= 0x200000) {
486			geom[0] = 128;	/* heads */
487			geom[1] = 63;	/* sectors */
488		} else {
489			geom[0] = 64;	/* heads */
490			geom[1] = 32;	/* sectors */
491		}
492	}
493	geom[2] = sector_div(capacity, geom[0] * geom[1]);
494	kfree(p);
495
496	return 0;
497}
498
499static const struct scsi_host_template fdomain_template = {
500	.module			= THIS_MODULE,
501	.name			= "Future Domain TMC-16x0",
502	.proc_name		= "fdomain",
503	.queuecommand		= fdomain_queue,
504	.eh_abort_handler	= fdomain_abort,
505	.eh_host_reset_handler	= fdomain_host_reset,
506	.bios_param		= fdomain_biosparam,
507	.can_queue		= 1,
508	.this_id		= 7,
509	.sg_tablesize		= 64,
510	.dma_boundary		= PAGE_SIZE - 1,
511	.cmd_size		= sizeof(struct scsi_pointer),
512};
513
514struct Scsi_Host *fdomain_create(int base, int irq, int this_id,
515				 struct device *dev)
516{
517	struct Scsi_Host *sh;
518	struct fdomain *fd;
519	enum chip_type chip;
520	static const char * const chip_names[] = {
521		"Unknown", "TMC-1800", "TMC-18C50", "TMC-18C30"
522	};
523	unsigned long irq_flags = 0;
524
525	chip = fdomain_identify(base);
526	if (!chip)
527		return NULL;
528
529	fdomain_reset(base);
530
531	if (fdomain_test_loopback(base))
532		return NULL;
533
534	if (!irq) {
535		dev_err(dev, "card has no IRQ assigned");
536		return NULL;
537	}
538
539	sh = scsi_host_alloc(&fdomain_template, sizeof(struct fdomain));
540	if (!sh)
541		return NULL;
542
543	if (this_id)
544		sh->this_id = this_id & 0x07;
545
546	sh->irq = irq;
547	sh->io_port = base;
548	sh->n_io_port = FDOMAIN_REGION_SIZE;
549
550	fd = shost_priv(sh);
551	fd->base = base;
552	fd->chip = chip;
553	INIT_WORK(&fd->work, fdomain_work);
554
555	if (dev_is_pci(dev) || !strcmp(dev->bus->name, "pcmcia"))
556		irq_flags = IRQF_SHARED;
557
558	if (request_irq(irq, fdomain_irq, irq_flags, "fdomain", fd))
559		goto fail_put;
560
561	shost_printk(KERN_INFO, sh, "%s chip at 0x%x irq %d SCSI ID %d\n",
562		     dev_is_pci(dev) ? "TMC-36C70 (PCI bus)" : chip_names[chip],
563		     base, irq, sh->this_id);
564
565	if (scsi_add_host(sh, dev))
566		goto fail_free_irq;
567
568	scsi_scan_host(sh);
569
570	return sh;
571
572fail_free_irq:
573	free_irq(irq, fd);
574fail_put:
575	scsi_host_put(sh);
576	return NULL;
577}
578EXPORT_SYMBOL_GPL(fdomain_create);
579
580int fdomain_destroy(struct Scsi_Host *sh)
581{
582	struct fdomain *fd = shost_priv(sh);
583
584	cancel_work_sync(&fd->work);
585	scsi_remove_host(sh);
586	if (sh->irq)
587		free_irq(sh->irq, fd);
588	scsi_host_put(sh);
589	return 0;
590}
591EXPORT_SYMBOL_GPL(fdomain_destroy);
592
593#ifdef CONFIG_PM_SLEEP
594static int fdomain_resume(struct device *dev)
595{
596	struct fdomain *fd = shost_priv(dev_get_drvdata(dev));
597
598	fdomain_reset(fd->base);
599	return 0;
600}
601
602static SIMPLE_DEV_PM_OPS(fdomain_pm_ops, NULL, fdomain_resume);
603#endif /* CONFIG_PM_SLEEP */
604
605MODULE_AUTHOR("Ondrej Zary, Rickard E. Faith");
606MODULE_DESCRIPTION("Future Domain TMC-16x0/TMC-3260 SCSI driver");
607MODULE_LICENSE("GPL");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Driver for Future Domain TMC-16x0 and TMC-3260 SCSI host adapters
  4 * Copyright 2019 Ondrej Zary
  5 *
  6 * Original driver by
  7 * Rickard E. Faith, faith@cs.unc.edu
  8 *
  9 * Future Domain BIOS versions supported for autodetect:
 10 *    2.0, 3.0, 3.2, 3.4 (1.0), 3.5 (2.0), 3.6, 3.61
 11 * Chips supported:
 12 *    TMC-1800, TMC-18C50, TMC-18C30, TMC-36C70
 13 * Boards supported:
 14 *    Future Domain TMC-1650, TMC-1660, TMC-1670, TMC-1680, TMC-1610M/MER/MEX
 15 *    Future Domain TMC-3260 (PCI)
 16 *    Quantum ISA-200S, ISA-250MG
 17 *    Adaptec AHA-2920A (PCI) [BUT *NOT* AHA-2920C -- use aic7xxx instead]
 18 *    IBM ?
 19 *
 20 * NOTE:
 21 *
 22 * The Adaptec AHA-2920C has an Adaptec AIC-7850 chip on it.
 23 * Use the aic7xxx driver for this board.
 24 *
 25 * The Adaptec AHA-2920A has a Future Domain chip on it, so this is the right
 26 * driver for that card.  Unfortunately, the boxes will probably just say
 27 * "2920", so you'll have to look on the card for a Future Domain logo, or a
 28 * letter after the 2920.
 29 *
 30 * If you have a TMC-8xx or TMC-9xx board, then this is not the driver for
 31 * your board.
 32 *
 33 * DESCRIPTION:
 34 *
 35 * This is the Linux low-level SCSI driver for Future Domain TMC-1660/1680
 36 * TMC-1650/1670, and TMC-3260 SCSI host adapters.  The 1650 and 1670 have a
 37 * 25-pin external connector, whereas the 1660 and 1680 have a SCSI-2 50-pin
 38 * high-density external connector.  The 1670 and 1680 have floppy disk
 39 * controllers built in.  The TMC-3260 is a PCI bus card.
 40 *
 41 * Future Domain's older boards are based on the TMC-1800 chip, and this
 42 * driver was originally written for a TMC-1680 board with the TMC-1800 chip.
 43 * More recently, boards are being produced with the TMC-18C50 and TMC-18C30
 44 * chips.
 45 *
 46 * Please note that the drive ordering that Future Domain implemented in BIOS
 47 * versions 3.4 and 3.5 is the opposite of the order (currently) used by the
 48 * rest of the SCSI industry.
 49 *
 50 *
 51 * REFERENCES USED:
 52 *
 53 * "TMC-1800 SCSI Chip Specification (FDC-1800T)", Future Domain Corporation,
 54 * 1990.
 55 *
 56 * "Technical Reference Manual: 18C50 SCSI Host Adapter Chip", Future Domain
 57 * Corporation, January 1992.
 58 *
 59 * "LXT SCSI Products: Specifications and OEM Technical Manual (Revision
 60 * B/September 1991)", Maxtor Corporation, 1991.
 61 *
 62 * "7213S product Manual (Revision P3)", Maxtor Corporation, 1992.
 63 *
 64 * "Draft Proposed American National Standard: Small Computer System
 65 * Interface - 2 (SCSI-2)", Global Engineering Documents. (X3T9.2/86-109,
 66 * revision 10h, October 17, 1991)
 67 *
 68 * Private communications, Drew Eckhardt (drew@cs.colorado.edu) and Eric
 69 * Youngdale (ericy@cais.com), 1992.
 70 *
 71 * Private communication, Tuong Le (Future Domain Engineering department),
 72 * 1994. (Disk geometry computations for Future Domain BIOS version 3.4, and
 73 * TMC-18C30 detection.)
 74 *
 75 * Hogan, Thom. The Programmer's PC Sourcebook. Microsoft Press, 1988. Page
 76 * 60 (2.39: Disk Partition Table Layout).
 77 *
 78 * "18C30 Technical Reference Manual", Future Domain Corporation, 1993, page
 79 * 6-1.
 80 */
 81
 82#include <linux/module.h>
 83#include <linux/interrupt.h>
 84#include <linux/delay.h>
 85#include <linux/pci.h>
 86#include <linux/workqueue.h>
 87#include <scsi/scsicam.h>
 88#include <scsi/scsi_cmnd.h>
 89#include <scsi/scsi_device.h>
 90#include <scsi/scsi_host.h>
 91#include "fdomain.h"
 92
 93/*
 94 * FIFO_COUNT: The host adapter has an 8K cache (host adapters based on the
 95 * 18C30 chip have a 2k cache).  When this many 512 byte blocks are filled by
 96 * the SCSI device, an interrupt will be raised.  Therefore, this could be as
 97 * low as 0, or as high as 16.  Note, however, that values which are too high
 98 * or too low seem to prevent any interrupts from occurring, and thereby lock
 99 * up the machine.
100 */
101#define FIFO_COUNT	2	/* Number of 512 byte blocks before INTR */
102#define PARITY_MASK	ACTL_PAREN	/* Parity enabled, 0 = disabled */
103
104enum chip_type {
105	unknown		= 0x00,
106	tmc1800		= 0x01,
107	tmc18c50	= 0x02,
108	tmc18c30	= 0x03,
109};
110
111struct fdomain {
112	int base;
113	struct scsi_cmnd *cur_cmd;
114	enum chip_type chip;
115	struct work_struct work;
116};
117
 
 
 
 
 
118static inline void fdomain_make_bus_idle(struct fdomain *fd)
119{
120	outb(0, fd->base + REG_BCTL);
121	outb(0, fd->base + REG_MCTL);
122	if (fd->chip == tmc18c50 || fd->chip == tmc18c30)
123		/* Clear forced intr. */
124		outb(ACTL_RESET | ACTL_CLRFIRQ | PARITY_MASK,
125		     fd->base + REG_ACTL);
126	else
127		outb(ACTL_RESET | PARITY_MASK, fd->base + REG_ACTL);
128}
129
130static enum chip_type fdomain_identify(int port)
131{
132	u16 id = inb(port + REG_ID_LSB) | inb(port + REG_ID_MSB) << 8;
133
134	switch (id) {
135	case 0x6127:
136		return tmc1800;
137	case 0x60e9: /* 18c50 or 18c30 */
138		break;
139	default:
140		return unknown;
141	}
142
143	/* Try to toggle 32-bit mode. This only works on an 18c30 chip. */
144	outb(CFG2_32BIT, port + REG_CFG2);
145	if ((inb(port + REG_CFG2) & CFG2_32BIT)) {
146		outb(0, port + REG_CFG2);
147		if ((inb(port + REG_CFG2) & CFG2_32BIT) == 0)
148			return tmc18c30;
149	}
150	/* If that failed, we are an 18c50. */
151	return tmc18c50;
152}
153
154static int fdomain_test_loopback(int base)
155{
156	int i;
157
158	for (i = 0; i < 255; i++) {
159		outb(i, base + REG_LOOPBACK);
160		if (inb(base + REG_LOOPBACK) != i)
161			return 1;
162	}
163
164	return 0;
165}
166
167static void fdomain_reset(int base)
168{
169	outb(BCTL_RST, base + REG_BCTL);
170	mdelay(20);
171	outb(0, base + REG_BCTL);
172	mdelay(1150);
173	outb(0, base + REG_MCTL);
174	outb(PARITY_MASK, base + REG_ACTL);
175}
176
177static int fdomain_select(struct Scsi_Host *sh, int target)
178{
179	int status;
180	unsigned long timeout;
181	struct fdomain *fd = shost_priv(sh);
182
183	outb(BCTL_BUSEN | BCTL_SEL, fd->base + REG_BCTL);
184	outb(BIT(sh->this_id) | BIT(target), fd->base + REG_SCSI_DATA_NOACK);
185
186	/* Stop arbitration and enable parity */
187	outb(PARITY_MASK, fd->base + REG_ACTL);
188
189	timeout = 350;	/* 350 msec */
190
191	do {
192		status = inb(fd->base + REG_BSTAT);
193		if (status & BSTAT_BSY) {
194			/* Enable SCSI Bus */
195			/* (on error, should make bus idle with 0) */
196			outb(BCTL_BUSEN, fd->base + REG_BCTL);
197			return 0;
198		}
199		mdelay(1);
200	} while (--timeout);
201	fdomain_make_bus_idle(fd);
202	return 1;
203}
204
205static void fdomain_finish_cmd(struct fdomain *fd, int result)
206{
207	outb(0, fd->base + REG_ICTL);
208	fdomain_make_bus_idle(fd);
209	fd->cur_cmd->result = result;
210	fd->cur_cmd->scsi_done(fd->cur_cmd);
211	fd->cur_cmd = NULL;
212}
213
214static void fdomain_read_data(struct scsi_cmnd *cmd)
215{
216	struct fdomain *fd = shost_priv(cmd->device->host);
217	unsigned char *virt, *ptr;
218	size_t offset, len;
219
220	while ((len = inw(fd->base + REG_FIFO_COUNT)) > 0) {
221		offset = scsi_bufflen(cmd) - scsi_get_resid(cmd);
222		virt = scsi_kmap_atomic_sg(scsi_sglist(cmd), scsi_sg_count(cmd),
223					   &offset, &len);
224		ptr = virt + offset;
225		if (len & 1)
226			*ptr++ = inb(fd->base + REG_FIFO);
227		if (len > 1)
228			insw(fd->base + REG_FIFO, ptr, len >> 1);
229		scsi_set_resid(cmd, scsi_get_resid(cmd) - len);
230		scsi_kunmap_atomic_sg(virt);
231	}
232}
233
234static void fdomain_write_data(struct scsi_cmnd *cmd)
235{
236	struct fdomain *fd = shost_priv(cmd->device->host);
237	/* 8k FIFO for pre-tmc18c30 chips, 2k FIFO for tmc18c30 */
238	int FIFO_Size = fd->chip == tmc18c30 ? 0x800 : 0x2000;
239	unsigned char *virt, *ptr;
240	size_t offset, len;
241
242	while ((len = FIFO_Size - inw(fd->base + REG_FIFO_COUNT)) > 512) {
243		offset = scsi_bufflen(cmd) - scsi_get_resid(cmd);
244		if (len + offset > scsi_bufflen(cmd)) {
245			len = scsi_bufflen(cmd) - offset;
246			if (len == 0)
247				break;
248		}
249		virt = scsi_kmap_atomic_sg(scsi_sglist(cmd), scsi_sg_count(cmd),
250					   &offset, &len);
251		ptr = virt + offset;
252		if (len & 1)
253			outb(*ptr++, fd->base + REG_FIFO);
254		if (len > 1)
255			outsw(fd->base + REG_FIFO, ptr, len >> 1);
256		scsi_set_resid(cmd, scsi_get_resid(cmd) - len);
257		scsi_kunmap_atomic_sg(virt);
258	}
259}
260
261static void fdomain_work(struct work_struct *work)
262{
263	struct fdomain *fd = container_of(work, struct fdomain, work);
264	struct Scsi_Host *sh = container_of((void *)fd, struct Scsi_Host,
265					    hostdata);
266	struct scsi_cmnd *cmd = fd->cur_cmd;
 
267	unsigned long flags;
268	int status;
269	int done = 0;
270
271	spin_lock_irqsave(sh->host_lock, flags);
272
273	if (cmd->SCp.phase & in_arbitration) {
274		status = inb(fd->base + REG_ASTAT);
275		if (!(status & ASTAT_ARB)) {
276			fdomain_finish_cmd(fd, DID_BUS_BUSY << 16);
 
277			goto out;
278		}
279		cmd->SCp.phase = in_selection;
280
281		outb(ICTL_SEL | FIFO_COUNT, fd->base + REG_ICTL);
282		outb(BCTL_BUSEN | BCTL_SEL, fd->base + REG_BCTL);
283		outb(BIT(cmd->device->host->this_id) | BIT(scmd_id(cmd)),
284		     fd->base + REG_SCSI_DATA_NOACK);
285		/* Stop arbitration and enable parity */
286		outb(ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL);
287		goto out;
288	} else if (cmd->SCp.phase & in_selection) {
289		status = inb(fd->base + REG_BSTAT);
290		if (!(status & BSTAT_BSY)) {
291			/* Try again, for slow devices */
292			if (fdomain_select(cmd->device->host, scmd_id(cmd))) {
293				fdomain_finish_cmd(fd, DID_NO_CONNECT << 16);
 
294				goto out;
295			}
296			/* Stop arbitration and enable parity */
297			outb(ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL);
298		}
299		cmd->SCp.phase = in_other;
300		outb(ICTL_FIFO | ICTL_REQ | FIFO_COUNT, fd->base + REG_ICTL);
301		outb(BCTL_BUSEN, fd->base + REG_BCTL);
302		goto out;
303	}
304
305	/* cur_cmd->SCp.phase == in_other: this is the body of the routine */
306	status = inb(fd->base + REG_BSTAT);
307
308	if (status & BSTAT_REQ) {
309		switch (status & (BSTAT_MSG | BSTAT_CMD | BSTAT_IO)) {
310		case BSTAT_CMD:	/* COMMAND OUT */
311			outb(cmd->cmnd[cmd->SCp.sent_command++],
312			     fd->base + REG_SCSI_DATA);
313			break;
314		case 0:	/* DATA OUT -- tmc18c50/tmc18c30 only */
315			if (fd->chip != tmc1800 && !cmd->SCp.have_data_in) {
316				cmd->SCp.have_data_in = -1;
317				outb(ACTL_IRQEN | ACTL_FIFOWR | ACTL_FIFOEN |
318				     PARITY_MASK, fd->base + REG_ACTL);
319			}
320			break;
321		case BSTAT_IO:	/* DATA IN -- tmc18c50/tmc18c30 only */
322			if (fd->chip != tmc1800 && !cmd->SCp.have_data_in) {
323				cmd->SCp.have_data_in = 1;
324				outb(ACTL_IRQEN | ACTL_FIFOEN | PARITY_MASK,
325				     fd->base + REG_ACTL);
326			}
327			break;
328		case BSTAT_CMD | BSTAT_IO:	/* STATUS IN */
329			cmd->SCp.Status = inb(fd->base + REG_SCSI_DATA);
330			break;
331		case BSTAT_MSG | BSTAT_CMD:	/* MESSAGE OUT */
332			outb(MESSAGE_REJECT, fd->base + REG_SCSI_DATA);
333			break;
334		case BSTAT_MSG | BSTAT_CMD | BSTAT_IO:	/* MESSAGE IN */
335			cmd->SCp.Message = inb(fd->base + REG_SCSI_DATA);
336			if (!cmd->SCp.Message)
337				++done;
338			break;
339		}
340	}
341
342	if (fd->chip == tmc1800 && !cmd->SCp.have_data_in &&
343	    cmd->SCp.sent_command >= cmd->cmd_len) {
344		if (cmd->sc_data_direction == DMA_TO_DEVICE) {
345			cmd->SCp.have_data_in = -1;
346			outb(ACTL_IRQEN | ACTL_FIFOWR | ACTL_FIFOEN |
347			     PARITY_MASK, fd->base + REG_ACTL);
348		} else {
349			cmd->SCp.have_data_in = 1;
350			outb(ACTL_IRQEN | ACTL_FIFOEN | PARITY_MASK,
351			     fd->base + REG_ACTL);
352		}
353	}
354
355	if (cmd->SCp.have_data_in == -1) /* DATA OUT */
356		fdomain_write_data(cmd);
357
358	if (cmd->SCp.have_data_in == 1) /* DATA IN */
359		fdomain_read_data(cmd);
360
361	if (done) {
362		fdomain_finish_cmd(fd, (cmd->SCp.Status & 0xff) |
363				   ((cmd->SCp.Message & 0xff) << 8) |
364				   (DID_OK << 16));
 
365	} else {
366		if (cmd->SCp.phase & disconnect) {
367			outb(ICTL_FIFO | ICTL_SEL | ICTL_REQ | FIFO_COUNT,
368			     fd->base + REG_ICTL);
369			outb(0, fd->base + REG_BCTL);
370		} else
371			outb(ICTL_FIFO | ICTL_REQ | FIFO_COUNT,
372			     fd->base + REG_ICTL);
373	}
374out:
375	spin_unlock_irqrestore(sh->host_lock, flags);
376}
377
378static irqreturn_t fdomain_irq(int irq, void *dev_id)
379{
380	struct fdomain *fd = dev_id;
381
382	/* Is it our IRQ? */
383	if ((inb(fd->base + REG_ASTAT) & ASTAT_IRQ) == 0)
384		return IRQ_NONE;
385
386	outb(0, fd->base + REG_ICTL);
387
388	/* We usually have one spurious interrupt after each command. */
389	if (!fd->cur_cmd)	/* Spurious interrupt */
390		return IRQ_NONE;
391
392	schedule_work(&fd->work);
393
394	return IRQ_HANDLED;
395}
396
397static int fdomain_queue(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
398{
 
399	struct fdomain *fd = shost_priv(cmd->device->host);
400	unsigned long flags;
401
402	cmd->SCp.Status		= 0;
403	cmd->SCp.Message	= 0;
404	cmd->SCp.have_data_in	= 0;
405	cmd->SCp.sent_command	= 0;
406	cmd->SCp.phase		= in_arbitration;
407	scsi_set_resid(cmd, scsi_bufflen(cmd));
408
409	spin_lock_irqsave(sh->host_lock, flags);
410
411	fd->cur_cmd = cmd;
412
413	fdomain_make_bus_idle(fd);
414
415	/* Start arbitration */
416	outb(0, fd->base + REG_ICTL);
417	outb(0, fd->base + REG_BCTL);	/* Disable data drivers */
418	/* Set our id bit */
419	outb(BIT(cmd->device->host->this_id), fd->base + REG_SCSI_DATA_NOACK);
420	outb(ICTL_ARB, fd->base + REG_ICTL);
421	/* Start arbitration */
422	outb(ACTL_ARB | ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL);
423
424	spin_unlock_irqrestore(sh->host_lock, flags);
425
426	return 0;
427}
428
429static int fdomain_abort(struct scsi_cmnd *cmd)
430{
431	struct Scsi_Host *sh = cmd->device->host;
432	struct fdomain *fd = shost_priv(sh);
433	unsigned long flags;
434
435	if (!fd->cur_cmd)
436		return FAILED;
437
438	spin_lock_irqsave(sh->host_lock, flags);
439
440	fdomain_make_bus_idle(fd);
441	fd->cur_cmd->SCp.phase |= aborted;
442	fd->cur_cmd->result = DID_ABORT << 16;
443
444	/* Aborts are not done well. . . */
445	fdomain_finish_cmd(fd, DID_ABORT << 16);
 
446	spin_unlock_irqrestore(sh->host_lock, flags);
447	return SUCCESS;
448}
449
450static int fdomain_host_reset(struct scsi_cmnd *cmd)
451{
452	struct Scsi_Host *sh = cmd->device->host;
453	struct fdomain *fd = shost_priv(sh);
454	unsigned long flags;
455
456	spin_lock_irqsave(sh->host_lock, flags);
457	fdomain_reset(fd->base);
458	spin_unlock_irqrestore(sh->host_lock, flags);
459	return SUCCESS;
460}
461
462static int fdomain_biosparam(struct scsi_device *sdev,
463			     struct block_device *bdev,	sector_t capacity,
464			     int geom[])
465{
466	unsigned char *p = scsi_bios_ptable(bdev);
467
468	if (p && p[65] == 0xaa && p[64] == 0x55 /* Partition table valid */
469	    && p[4]) {	 /* Partition type */
470		geom[0] = p[5] + 1;	/* heads */
471		geom[1] = p[6] & 0x3f;	/* sectors */
472	} else {
473		if (capacity >= 0x7e0000) {
474			geom[0] = 255;	/* heads */
475			geom[1] = 63;	/* sectors */
476		} else if (capacity >= 0x200000) {
477			geom[0] = 128;	/* heads */
478			geom[1] = 63;	/* sectors */
479		} else {
480			geom[0] = 64;	/* heads */
481			geom[1] = 32;	/* sectors */
482		}
483	}
484	geom[2] = sector_div(capacity, geom[0] * geom[1]);
485	kfree(p);
486
487	return 0;
488}
489
490static struct scsi_host_template fdomain_template = {
491	.module			= THIS_MODULE,
492	.name			= "Future Domain TMC-16x0",
493	.proc_name		= "fdomain",
494	.queuecommand		= fdomain_queue,
495	.eh_abort_handler	= fdomain_abort,
496	.eh_host_reset_handler	= fdomain_host_reset,
497	.bios_param		= fdomain_biosparam,
498	.can_queue		= 1,
499	.this_id		= 7,
500	.sg_tablesize		= 64,
501	.dma_boundary		= PAGE_SIZE - 1,
 
502};
503
504struct Scsi_Host *fdomain_create(int base, int irq, int this_id,
505				 struct device *dev)
506{
507	struct Scsi_Host *sh;
508	struct fdomain *fd;
509	enum chip_type chip;
510	static const char * const chip_names[] = {
511		"Unknown", "TMC-1800", "TMC-18C50", "TMC-18C30"
512	};
513	unsigned long irq_flags = 0;
514
515	chip = fdomain_identify(base);
516	if (!chip)
517		return NULL;
518
519	fdomain_reset(base);
520
521	if (fdomain_test_loopback(base))
522		return NULL;
523
524	if (!irq) {
525		dev_err(dev, "card has no IRQ assigned");
526		return NULL;
527	}
528
529	sh = scsi_host_alloc(&fdomain_template, sizeof(struct fdomain));
530	if (!sh)
531		return NULL;
532
533	if (this_id)
534		sh->this_id = this_id & 0x07;
535
536	sh->irq = irq;
537	sh->io_port = base;
538	sh->n_io_port = FDOMAIN_REGION_SIZE;
539
540	fd = shost_priv(sh);
541	fd->base = base;
542	fd->chip = chip;
543	INIT_WORK(&fd->work, fdomain_work);
544
545	if (dev_is_pci(dev) || !strcmp(dev->bus->name, "pcmcia"))
546		irq_flags = IRQF_SHARED;
547
548	if (request_irq(irq, fdomain_irq, irq_flags, "fdomain", fd))
549		goto fail_put;
550
551	shost_printk(KERN_INFO, sh, "%s chip at 0x%x irq %d SCSI ID %d\n",
552		     dev_is_pci(dev) ? "TMC-36C70 (PCI bus)" : chip_names[chip],
553		     base, irq, sh->this_id);
554
555	if (scsi_add_host(sh, dev))
556		goto fail_free_irq;
557
558	scsi_scan_host(sh);
559
560	return sh;
561
562fail_free_irq:
563	free_irq(irq, fd);
564fail_put:
565	scsi_host_put(sh);
566	return NULL;
567}
568EXPORT_SYMBOL_GPL(fdomain_create);
569
570int fdomain_destroy(struct Scsi_Host *sh)
571{
572	struct fdomain *fd = shost_priv(sh);
573
574	cancel_work_sync(&fd->work);
575	scsi_remove_host(sh);
576	if (sh->irq)
577		free_irq(sh->irq, fd);
578	scsi_host_put(sh);
579	return 0;
580}
581EXPORT_SYMBOL_GPL(fdomain_destroy);
582
583#ifdef CONFIG_PM_SLEEP
584static int fdomain_resume(struct device *dev)
585{
586	struct fdomain *fd = shost_priv(dev_get_drvdata(dev));
587
588	fdomain_reset(fd->base);
589	return 0;
590}
591
592static SIMPLE_DEV_PM_OPS(fdomain_pm_ops, NULL, fdomain_resume);
593#endif /* CONFIG_PM_SLEEP */
594
595MODULE_AUTHOR("Ondrej Zary, Rickard E. Faith");
596MODULE_DESCRIPTION("Future Domain TMC-16x0/TMC-3260 SCSI driver");
597MODULE_LICENSE("GPL");