Linux Audio

Check our new training course

Loading...
v4.10.11
  1/*  $Id$
  2 *  1993/03/31
  3 *  linux/kernel/aha1740.c
  4 *
  5 *  Based loosely on aha1542.c which is
  6 *  Copyright (C) 1992  Tommy Thorn and
  7 *  Modified by Eric Youngdale
  8 *
  9 *  This file is aha1740.c, written and
 10 *  Copyright (C) 1992,1993  Brad McLean
 11 *  brad@saturn.gaylord.com or brad@bradpc.gaylord.com.
 12 *  
 13 *  Modifications to makecode and queuecommand
 14 *  for proper handling of multiple devices courteously
 15 *  provided by Michael Weller, March, 1993
 16 *
 17 *  Multiple adapter support, extended translation detection,
 18 *  update to current scsi subsystem changes, proc fs support,
 19 *  working (!) module support based on patches from Andreas Arens,
 20 *  by Andreas Degert <ad@papyrus.hamburg.com>, 2/1997
 21 *
 22 * aha1740_makecode may still need even more work
 23 * if it doesn't work for your devices, take a look.
 24 *
 25 * Reworked for new_eh and new locking by Alan Cox <alan@lxorguk.ukuu.org.uk>
 26 *
 27 * Converted to EISA and generic DMA APIs by Marc Zyngier
 28 * <maz@wild-wind.fr.eu.org>, 4/2003.
 29 *
 30 * Shared interrupt support added by Rask Ingemann Lambertsen
 31 * <rask@sygehus.dk>, 10/2003
 32 *
 33 * For the avoidance of doubt the "preferred form" of this code is one which
 34 * is in an open non patent encumbered format. Where cryptographic key signing
 35 * forms part of the process of creating an executable the information
 36 * including keys needed to generate an equivalently functional executable
 37 * are deemed to be part of the source code.
 38 */
 39
 40#include <linux/blkdev.h>
 41#include <linux/interrupt.h>
 42#include <linux/module.h>
 43#include <linux/kernel.h>
 44#include <linux/types.h>
 45#include <linux/string.h>
 46#include <linux/ioport.h>
 47#include <linux/proc_fs.h>
 48#include <linux/stat.h>
 49#include <linux/init.h>
 50#include <linux/device.h>
 51#include <linux/eisa.h>
 52#include <linux/dma-mapping.h>
 53#include <linux/gfp.h>
 54
 55#include <asm/dma.h>
 56#include <asm/io.h>
 57
 58#include "scsi.h"
 
 
 
 59#include <scsi/scsi_host.h>
 
 60#include "aha1740.h"
 61
 62/* IF YOU ARE HAVING PROBLEMS WITH THIS DRIVER, AND WANT TO WATCH
 63   IT WORK, THEN:
 64#define DEBUG
 65*/
 66#ifdef DEBUG
 67#define DEB(x) x
 68#else
 69#define DEB(x)
 70#endif
 71
 72struct aha1740_hostdata {
 73	struct eisa_device *edev;
 74	unsigned int translation;
 75	unsigned int last_ecb_used;
 76	dma_addr_t ecb_dma_addr;
 77	struct ecb ecb[AHA1740_ECBS];
 78};
 79
 80struct aha1740_sg {
 81	struct aha1740_chain sg_chain[AHA1740_SCATTER];
 82	dma_addr_t sg_dma_addr;
 83	dma_addr_t buf_dma_addr;
 84};
 85
 86#define HOSTDATA(host) ((struct aha1740_hostdata *) &host->hostdata)
 87
 88static inline struct ecb *ecb_dma_to_cpu (struct Scsi_Host *host,
 89					  dma_addr_t dma)
 90{
 91	struct aha1740_hostdata *hdata = HOSTDATA (host);
 92	dma_addr_t offset;
 93
 94	offset = dma - hdata->ecb_dma_addr;
 95
 96	return (struct ecb *)(((char *) hdata->ecb) + (unsigned int) offset);
 97}
 98
 99static inline dma_addr_t ecb_cpu_to_dma (struct Scsi_Host *host, void *cpu)
100{
101	struct aha1740_hostdata *hdata = HOSTDATA (host);
102	dma_addr_t offset;
103    
104	offset = (char *) cpu - (char *) hdata->ecb;
105
106	return hdata->ecb_dma_addr + offset;
107}
108
109static int aha1740_show_info(struct seq_file *m, struct Scsi_Host *shpnt)
110{
111	struct aha1740_hostdata *host = HOSTDATA(shpnt);
112	seq_printf(m, "aha174x at IO:%lx, IRQ %d, SLOT %d.\n"
113		      "Extended translation %sabled.\n",
114		      shpnt->io_port, shpnt->irq, host->edev->slot,
115		      host->translation ? "en" : "dis");
116	return 0;
117}
118
119static int aha1740_makecode(unchar *sense, unchar *status)
120{
121	struct statusword
122	{
123		ushort	don:1,	/* Command Done - No Error */
124			du:1,	/* Data underrun */
125		    :1,	qf:1,	/* Queue full */
126		        sc:1,	/* Specification Check */
127		        dor:1,	/* Data overrun */
128		        ch:1,	/* Chaining Halted */
129		        intr:1,	/* Interrupt issued */
130		        asa:1,	/* Additional Status Available */
131		        sns:1,	/* Sense information Stored */
132		    :1,	ini:1,	/* Initialization Required */
133			me:1,	/* Major error or exception */
134		    :1,	eca:1,  /* Extended Contingent alliance */
135		    :1;
136	} status_word;
137	int retval = DID_OK;
138
139	status_word = * (struct statusword *) status;
140#ifdef DEBUG
141	printk("makecode from %x,%x,%x,%x %x,%x,%x,%x",
142	       status[0], status[1], status[2], status[3],
143	       sense[0], sense[1], sense[2], sense[3]);
144#endif
145	if (!status_word.don) { /* Anything abnormal was detected */
146		if ( (status[1]&0x18) || status_word.sc ) {
147			/*Additional info available*/
148			/* Use the supplied info for further diagnostics */
149			switch ( status[2] ) {
150			case 0x12:
151				if ( status_word.dor )
152					retval=DID_ERROR; /* It's an Overrun */
153				/* If not overrun, assume underrun and
154				 * ignore it! */
 
155			case 0x00: /* No info, assume no error, should
156				    * not occur */
157				break;
158			case 0x11:
159			case 0x21:
160				retval=DID_TIME_OUT;
161				break;
162			case 0x0a:
163				retval=DID_BAD_TARGET;
164				break;
165			case 0x04:
166			case 0x05:
167				retval=DID_ABORT;
168				/* Either by this driver or the
169				 * AHA1740 itself */
170				break;
171			default:
172				retval=DID_ERROR; /* No further
173						   * diagnostics
174						   * possible */
175			}
176		} else {
177			/* Michael suggests, and Brad concurs: */
178			if ( status_word.qf ) {
179				retval = DID_TIME_OUT; /* forces a redo */
180				/* I think this specific one should
181				 * not happen -Brad */
182				printk("aha1740.c: WARNING: AHA1740 queue overflow!\n");
183			} else
184				if ( status[0]&0x60 ) {
185					 /* Didn't find a better error */
186					retval = DID_ERROR;
187				}
188			/* In any other case return DID_OK so for example
189			   CONDITION_CHECKS make it through to the appropriate
190			   device driver */
191		}
192	}
193	/* Under all circumstances supply the target status -Michael */
194	return status[3] | retval << 16;
195}
196
197static int aha1740_test_port(unsigned int base)
198{
199	if ( inb(PORTADR(base)) & PORTADDR_ENH )
200		return 1;   /* Okay, we're all set */
201	
202	printk("aha174x: Board detected, but not in enhanced mode, so disabled it.\n");
203	return 0;
204}
205
206/* A "high" level interrupt handler */
207static irqreturn_t aha1740_intr_handle(int irq, void *dev_id)
208{
209	struct Scsi_Host *host = (struct Scsi_Host *) dev_id;
210        void (*my_done)(Scsi_Cmnd *);
211	int errstatus, adapstat;
212	int number_serviced;
213	struct ecb *ecbptr;
214	Scsi_Cmnd *SCtmp;
215	unsigned int base;
216	unsigned long flags;
217	int handled = 0;
218	struct aha1740_sg *sgptr;
219	struct eisa_device *edev;
220	
221	if (!host)
222		panic("aha1740.c: Irq from unknown host!\n");
223	spin_lock_irqsave(host->host_lock, flags);
224	base = host->io_port;
225	number_serviced = 0;
226	edev = HOSTDATA(host)->edev;
227
228	while(inb(G2STAT(base)) & G2STAT_INTPEND) {
229		handled = 1;
230		DEB(printk("aha1740_intr top of loop.\n"));
231		adapstat = inb(G2INTST(base));
232		ecbptr = ecb_dma_to_cpu (host, inl(MBOXIN0(base)));
233		outb(G2CNTRL_IRST,G2CNTRL(base)); /* interrupt reset */
234      
235		switch ( adapstat & G2INTST_MASK ) {
236		case	G2INTST_CCBRETRY:
237		case	G2INTST_CCBERROR:
238		case	G2INTST_CCBGOOD:
239			/* Host Ready -> Mailbox in complete */
240			outb(G2CNTRL_HRDY,G2CNTRL(base));
241			if (!ecbptr) {
242				printk("Aha1740 null ecbptr in interrupt (%x,%x,%x,%d)\n",
243				       inb(G2STAT(base)),adapstat,
244				       inb(G2INTST(base)), number_serviced++);
245				continue;
246			}
247			SCtmp = ecbptr->SCpnt;
248			if (!SCtmp) {
249				printk("Aha1740 null SCtmp in interrupt (%x,%x,%x,%d)\n",
250				       inb(G2STAT(base)),adapstat,
251				       inb(G2INTST(base)), number_serviced++);
252				continue;
253			}
254			sgptr = (struct aha1740_sg *) SCtmp->host_scribble;
255			scsi_dma_unmap(SCtmp);
256
257			/* Free the sg block */
258			dma_free_coherent (&edev->dev,
259					   sizeof (struct aha1740_sg),
260					   SCtmp->host_scribble,
261					   sgptr->sg_dma_addr);
262	    
263			/* Fetch the sense data, and tuck it away, in
264			   the required slot.  The Adaptec
265			   automatically fetches it, and there is no
266			   guarantee that we will still have it in the
267			   cdb when we come back */
268			if ( (adapstat & G2INTST_MASK) == G2INTST_CCBERROR ) {
269				memcpy(SCtmp->sense_buffer, ecbptr->sense, 
270				       SCSI_SENSE_BUFFERSIZE);
 
 
 
271				errstatus = aha1740_makecode(ecbptr->sense,ecbptr->status);
272			} else
273				errstatus = 0;
274			DEB(if (errstatus)
275			    printk("aha1740_intr_handle: returning %6x\n",
276				   errstatus));
277			SCtmp->result = errstatus;
278			my_done = ecbptr->done;
279			memset(ecbptr,0,sizeof(struct ecb)); 
280			if ( my_done )
281				my_done(SCtmp);
282			break;
283			
284		case	G2INTST_HARDFAIL:
285			printk(KERN_ALERT "aha1740 hardware failure!\n");
286			panic("aha1740.c");	/* Goodbye */
287			
288		case	G2INTST_ASNEVENT:
289			printk("aha1740 asynchronous event: %02x %02x %02x %02x %02x\n",
290			       adapstat,
291			       inb(MBOXIN0(base)),
292			       inb(MBOXIN1(base)),
293			       inb(MBOXIN2(base)),
294			       inb(MBOXIN3(base))); /* Say What? */
295			/* Host Ready -> Mailbox in complete */
296			outb(G2CNTRL_HRDY,G2CNTRL(base));
297			break;
298			
299		case	G2INTST_CMDGOOD:
300			/* set immediate command success flag here: */
301			break;
302			
303		case	G2INTST_CMDERROR:
304			/* Set immediate command failure flag here: */
305			break;
306		}
307		number_serviced++;
308	}
309
310	spin_unlock_irqrestore(host->host_lock, flags);
311	return IRQ_RETVAL(handled);
312}
313
314static int aha1740_queuecommand_lck(Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
315{
 
316	unchar direction;
317	unchar *cmd = (unchar *) SCpnt->cmnd;
318	unchar target = scmd_id(SCpnt);
319	struct aha1740_hostdata *host = HOSTDATA(SCpnt->device->host);
320	unsigned long flags;
321	dma_addr_t sg_dma;
322	struct aha1740_sg *sgptr;
323	int ecbno, nseg;
324	DEB(int i);
325
326	if(*cmd == REQUEST_SENSE) {
327		SCpnt->result = 0;
328		done(SCpnt); 
329		return 0;
330	}
331
332#ifdef DEBUG
333	if (*cmd == READ_10 || *cmd == WRITE_10)
334		i = xscsi2int(cmd+2);
335	else if (*cmd == READ_6 || *cmd == WRITE_6)
336		i = scsi2int(cmd+2);
337	else
338		i = -1;
339	printk("aha1740_queuecommand: dev %d cmd %02x pos %d len %d ",
340	       target, *cmd, i, bufflen);
341	printk("scsi cmd:");
342	for (i = 0; i < SCpnt->cmd_len; i++) printk("%02x ", cmd[i]);
343	printk("\n");
344#endif
345
346	/* locate an available ecb */
347	spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
348	ecbno = host->last_ecb_used + 1; /* An optimization */
349	if (ecbno >= AHA1740_ECBS)
350		ecbno = 0;
351	do {
352		if (!host->ecb[ecbno].cmdw)
353			break;
354		ecbno++;
355		if (ecbno >= AHA1740_ECBS)
356			ecbno = 0;
357	} while (ecbno != host->last_ecb_used);
358
359	if (host->ecb[ecbno].cmdw)
360		panic("Unable to find empty ecb for aha1740.\n");
361
362	host->ecb[ecbno].cmdw = AHA1740CMD_INIT; /* SCSI Initiator Command
363						    doubles as reserved flag */
364
365	host->last_ecb_used = ecbno;    
366	spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
367
368#ifdef DEBUG
369	printk("Sending command (%d %x)...", ecbno, done);
370#endif
371
372	host->ecb[ecbno].cdblen = SCpnt->cmd_len; /* SCSI Command
373						   * Descriptor Block
374						   * Length */
375
376	direction = 0;
377	if (*cmd == READ_10 || *cmd == READ_6)
378		direction = 1;
379	else if (*cmd == WRITE_10 || *cmd == WRITE_6)
380		direction = 0;
381
382	memcpy(host->ecb[ecbno].cdb, cmd, SCpnt->cmd_len);
383
384	SCpnt->host_scribble = dma_alloc_coherent (&host->edev->dev,
385						   sizeof (struct aha1740_sg),
386						   &sg_dma, GFP_ATOMIC);
387	if(SCpnt->host_scribble == NULL) {
388		printk(KERN_WARNING "aha1740: out of memory in queuecommand!\n");
389		return 1;
390	}
391	sgptr = (struct aha1740_sg *) SCpnt->host_scribble;
392	sgptr->sg_dma_addr = sg_dma;
393
394	nseg = scsi_dma_map(SCpnt);
395	BUG_ON(nseg < 0);
396	if (nseg) {
397		struct scatterlist *sg;
398		struct aha1740_chain * cptr;
399		int i;
400		DEB(unsigned char * ptr);
401
402		host->ecb[ecbno].sg = 1;  /* SCSI Initiator Command
403					   * w/scatter-gather*/
404		cptr = sgptr->sg_chain;
405		scsi_for_each_sg(SCpnt, sg, nseg, i) {
406			cptr[i].datalen = sg_dma_len (sg);
407			cptr[i].dataptr = sg_dma_address (sg);
408		}
409		host->ecb[ecbno].datalen = nseg * sizeof(struct aha1740_chain);
410		host->ecb[ecbno].dataptr = sg_dma;
411#ifdef DEBUG
412		printk("cptr %x: ",cptr);
413		ptr = (unsigned char *) cptr;
414		for(i=0;i<24;i++) printk("%02x ", ptr[i]);
415#endif
416	} else {
417		host->ecb[ecbno].datalen = 0;
418		host->ecb[ecbno].dataptr = 0;
419	}
420	host->ecb[ecbno].lun = SCpnt->device->lun;
421	host->ecb[ecbno].ses = 1; /* Suppress underrun errors */
422	host->ecb[ecbno].dir = direction;
423	host->ecb[ecbno].ars = 1; /* Yes, get the sense on an error */
424	host->ecb[ecbno].senselen = 12;
425	host->ecb[ecbno].senseptr = ecb_cpu_to_dma (SCpnt->device->host,
426						    host->ecb[ecbno].sense);
427	host->ecb[ecbno].statusptr = ecb_cpu_to_dma (SCpnt->device->host,
428						     host->ecb[ecbno].status);
429	host->ecb[ecbno].done = done;
430	host->ecb[ecbno].SCpnt = SCpnt;
431#ifdef DEBUG
432	{
433		int i;
434		printk("aha1740_command: sending.. ");
435		for (i = 0; i < sizeof(host->ecb[ecbno]) - 10; i++)
436			printk("%02x ", ((unchar *)&host->ecb[ecbno])[i]);
437	}
438	printk("\n");
439#endif
440	if (done) {
441	/* The Adaptec Spec says the card is so fast that the loops
442           will only be executed once in the code below. Even if this
443           was true with the fastest processors when the spec was
444           written, it doesn't seem to be true with today's fast
445           processors. We print a warning if the code is executed more
446           often than LOOPCNT_WARN. If this happens, it should be
447           investigated. If the count reaches LOOPCNT_MAX, we assume
448           something is broken; since there is no way to return an
449           error (the return value is ignored by the mid-level scsi
450           layer) we have to panic (and maybe that's the best thing we
451           can do then anyhow). */
452
453#define LOOPCNT_WARN 10		/* excessive mbxout wait -> syslog-msg */
454#define LOOPCNT_MAX 1000000	/* mbxout deadlock -> panic() after ~ 2 sec. */
455		int loopcnt;
456		unsigned int base = SCpnt->device->host->io_port;
457		DEB(printk("aha1740[%d] critical section\n",ecbno));
458
459		spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
460		for (loopcnt = 0; ; loopcnt++) {
461			if (inb(G2STAT(base)) & G2STAT_MBXOUT) break;
462			if (loopcnt == LOOPCNT_WARN) {
463				printk("aha1740[%d]_mbxout wait!\n",ecbno);
464			}
465			if (loopcnt == LOOPCNT_MAX)
466				panic("aha1740.c: mbxout busy!\n");
467		}
468		outl (ecb_cpu_to_dma (SCpnt->device->host, host->ecb + ecbno),
469		      MBOXOUT0(base));
470		for (loopcnt = 0; ; loopcnt++) {
471			if (! (inb(G2STAT(base)) & G2STAT_BUSY)) break;
472			if (loopcnt == LOOPCNT_WARN) {
473				printk("aha1740[%d]_attn wait!\n",ecbno);
474			}
475			if (loopcnt == LOOPCNT_MAX)
476				panic("aha1740.c: attn wait failed!\n");
477		}
478		outb(ATTN_START | (target & 7), ATTN(base)); /* Start it up */
479		spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
480		DEB(printk("aha1740[%d] request queued.\n",ecbno));
481	} else
482		printk(KERN_ALERT "aha1740_queuecommand: done can't be NULL\n");
483	return 0;
484}
485
486static DEF_SCSI_QCMD(aha1740_queuecommand)
487
488/* Query the board for its irq_level and irq_type.  Nothing else matters
489   in enhanced mode on an EISA bus. */
490
491static void aha1740_getconfig(unsigned int base, unsigned int *irq_level,
492			      unsigned int *irq_type,
493			      unsigned int *translation)
494{
495	static int intab[] = { 9, 10, 11, 12, 0, 14, 15, 0 };
496
497	*irq_level = intab[inb(INTDEF(base)) & 0x7];
498	*irq_type  = (inb(INTDEF(base)) & 0x8) >> 3;
499	*translation = inb(RESV1(base)) & 0x1;
500	outb(inb(INTDEF(base)) | 0x10, INTDEF(base));
501}
502
503static int aha1740_biosparam(struct scsi_device *sdev,
504			     struct block_device *dev,
505			     sector_t capacity, int* ip)
506{
507	int size = capacity;
508	int extended = HOSTDATA(sdev->host)->translation;
509
510	DEB(printk("aha1740_biosparam\n"));
511	if (extended && (ip[2] > 1024))	{
512		ip[0] = 255;
513		ip[1] = 63;
514		ip[2] = size / (255 * 63);
515	} else {
516		ip[0] = 64;
517		ip[1] = 32;
518		ip[2] = size >> 11;
519	}
520	return 0;
521}
522
523static int aha1740_eh_abort_handler (Scsi_Cmnd *dummy)
524{
525/*
526 * From Alan Cox :
527 * The AHA1740 has firmware handled abort/reset handling. The "head in
528 * sand" kernel code is correct for once 8)
529 *
530 * So we define a dummy handler just to keep the kernel SCSI code as
531 * quiet as possible...
532 */
533
534	return SUCCESS;
535}
536
537static struct scsi_host_template aha1740_template = {
538	.module           = THIS_MODULE,
539	.proc_name        = "aha1740",
540	.show_info        = aha1740_show_info,
541	.name             = "Adaptec 174x (EISA)",
542	.queuecommand     = aha1740_queuecommand,
543	.bios_param       = aha1740_biosparam,
544	.can_queue        = AHA1740_ECBS,
545	.this_id          = 7,
546	.sg_tablesize     = AHA1740_SCATTER,
547	.use_clustering   = ENABLE_CLUSTERING,
548	.eh_abort_handler = aha1740_eh_abort_handler,
549};
550
551static int aha1740_probe (struct device *dev)
552{
553	int slotbase, rc;
554	unsigned int irq_level, irq_type, translation;
555	struct Scsi_Host *shpnt;
556	struct aha1740_hostdata *host;
557	struct eisa_device *edev = to_eisa_device (dev);
558
559	DEB(printk("aha1740_probe: \n"));
560	
561	slotbase = edev->base_addr + EISA_VENDOR_ID_OFFSET;
562	if (!request_region(slotbase, SLOTSIZE, "aha1740")) /* See if in use */
563		return -EBUSY;
564	if (!aha1740_test_port(slotbase))
565		goto err_release_region;
566	aha1740_getconfig(slotbase,&irq_level,&irq_type,&translation);
567	if ((inb(G2STAT(slotbase)) &
568	     (G2STAT_MBXOUT|G2STAT_BUSY)) != G2STAT_MBXOUT) {
569		/* If the card isn't ready, hard reset it */
570		outb(G2CNTRL_HRST, G2CNTRL(slotbase));
571		outb(0, G2CNTRL(slotbase));
572	}
573	printk(KERN_INFO "Configuring slot %d at IO:%x, IRQ %u (%s)\n",
574	       edev->slot, slotbase, irq_level, irq_type ? "edge" : "level");
575	printk(KERN_INFO "aha174x: Extended translation %sabled.\n",
576	       translation ? "en" : "dis");
577	shpnt = scsi_host_alloc(&aha1740_template,
578			      sizeof(struct aha1740_hostdata));
579	if(shpnt == NULL)
580		goto err_release_region;
581
582	shpnt->base = 0;
583	shpnt->io_port = slotbase;
584	shpnt->n_io_port = SLOTSIZE;
585	shpnt->irq = irq_level;
586	shpnt->dma_channel = 0xff;
587	host = HOSTDATA(shpnt);
588	host->edev = edev;
589	host->translation = translation;
590	host->ecb_dma_addr = dma_map_single (&edev->dev, host->ecb,
591					     sizeof (host->ecb),
592					     DMA_BIDIRECTIONAL);
593	if (!host->ecb_dma_addr) {
594		printk (KERN_ERR "aha1740_probe: Couldn't map ECB, giving up\n");
595		scsi_unregister (shpnt);
596		goto err_host_put;
597	}
598	
599	DEB(printk("aha1740_probe: enable interrupt channel %d\n",irq_level));
600	if (request_irq(irq_level,aha1740_intr_handle,irq_type ? 0 : IRQF_SHARED,
601			"aha1740",shpnt)) {
602		printk(KERN_ERR "aha1740_probe: Unable to allocate IRQ %d.\n",
603		       irq_level);
604		goto err_unmap;
605	}
606
607	eisa_set_drvdata (edev, shpnt);
608
609	rc = scsi_add_host (shpnt, dev);
610	if (rc)
611		goto err_irq;
612
613	scsi_scan_host (shpnt);
614	return 0;
615
616 err_irq:
617 	free_irq(irq_level, shpnt);
618 err_unmap:
619	dma_unmap_single (&edev->dev, host->ecb_dma_addr,
620			  sizeof (host->ecb), DMA_BIDIRECTIONAL);
621 err_host_put:
622	scsi_host_put (shpnt);
623 err_release_region:
624	release_region(slotbase, SLOTSIZE);
625
626	return -ENODEV;
627}
628
629static int aha1740_remove (struct device *dev)
630{
631	struct Scsi_Host *shpnt = dev_get_drvdata(dev);
632	struct aha1740_hostdata *host = HOSTDATA (shpnt);
633
634	scsi_remove_host(shpnt);
635	
636	free_irq (shpnt->irq, shpnt);
637	dma_unmap_single (dev, host->ecb_dma_addr,
638			  sizeof (host->ecb), DMA_BIDIRECTIONAL);
639	release_region (shpnt->io_port, SLOTSIZE);
640
641	scsi_host_put (shpnt);
642	
643	return 0;
644}
645
646static struct eisa_device_id aha1740_ids[] = {
647	{ "ADP0000" },		/* 1740  */
648	{ "ADP0001" },		/* 1740A */
649	{ "ADP0002" },		/* 1742A */
650	{ "ADP0400" },		/* 1744  */
651	{ "" }
652};
653MODULE_DEVICE_TABLE(eisa, aha1740_ids);
654
655static struct eisa_driver aha1740_driver = {
656	.id_table = aha1740_ids,
657	.driver   = {
658		.name    = "aha1740",
659		.probe   = aha1740_probe,
660		.remove  = aha1740_remove,
661	},
662};
663
664static __init int aha1740_init (void)
665{
666	return eisa_driver_register (&aha1740_driver);
667}
668
669static __exit void aha1740_exit (void)
670{
671	eisa_driver_unregister (&aha1740_driver);
672}
673
674module_init (aha1740_init);
675module_exit (aha1740_exit);
676
 
677MODULE_LICENSE("GPL");
v6.13.7
  1/*  $Id$
  2 *  1993/03/31
  3 *  linux/kernel/aha1740.c
  4 *
  5 *  Based loosely on aha1542.c which is
  6 *  Copyright (C) 1992  Tommy Thorn and
  7 *  Modified by Eric Youngdale
  8 *
  9 *  This file is aha1740.c, written and
 10 *  Copyright (C) 1992,1993  Brad McLean
 11 *  brad@saturn.gaylord.com or brad@bradpc.gaylord.com.
 12 *  
 13 *  Modifications to makecode and queuecommand
 14 *  for proper handling of multiple devices courteously
 15 *  provided by Michael Weller, March, 1993
 16 *
 17 *  Multiple adapter support, extended translation detection,
 18 *  update to current scsi subsystem changes, proc fs support,
 19 *  working (!) module support based on patches from Andreas Arens,
 20 *  by Andreas Degert <ad@papyrus.hamburg.com>, 2/1997
 21 *
 22 * aha1740_makecode may still need even more work
 23 * if it doesn't work for your devices, take a look.
 24 *
 25 * Reworked for new_eh and new locking by Alan Cox <alan@lxorguk.ukuu.org.uk>
 26 *
 27 * Converted to EISA and generic DMA APIs by Marc Zyngier
 28 * <maz@wild-wind.fr.eu.org>, 4/2003.
 29 *
 30 * Shared interrupt support added by Rask Ingemann Lambertsen
 31 * <rask@sygehus.dk>, 10/2003
 32 *
 33 * For the avoidance of doubt the "preferred form" of this code is one which
 34 * is in an open non patent encumbered format. Where cryptographic key signing
 35 * forms part of the process of creating an executable the information
 36 * including keys needed to generate an equivalently functional executable
 37 * are deemed to be part of the source code.
 38 */
 39
 40#include <linux/blkdev.h>
 41#include <linux/interrupt.h>
 42#include <linux/module.h>
 43#include <linux/kernel.h>
 44#include <linux/types.h>
 45#include <linux/string.h>
 46#include <linux/ioport.h>
 47#include <linux/proc_fs.h>
 48#include <linux/stat.h>
 49#include <linux/init.h>
 50#include <linux/device.h>
 51#include <linux/eisa.h>
 52#include <linux/dma-mapping.h>
 53#include <linux/gfp.h>
 54
 55#include <asm/dma.h>
 56#include <asm/io.h>
 57
 58#include <scsi/scsi.h>
 59#include <scsi/scsi_cmnd.h>
 60#include <scsi/scsi_device.h>
 61#include <scsi/scsi_eh.h>
 62#include <scsi/scsi_host.h>
 63#include <scsi/scsi_tcq.h>
 64#include "aha1740.h"
 65
 66/* IF YOU ARE HAVING PROBLEMS WITH THIS DRIVER, AND WANT TO WATCH
 67   IT WORK, THEN:
 68#define DEBUG
 69*/
 70#ifdef DEBUG
 71#define DEB(x) x
 72#else
 73#define DEB(x)
 74#endif
 75
 76struct aha1740_hostdata {
 77	struct eisa_device *edev;
 78	unsigned int translation;
 79	unsigned int last_ecb_used;
 80	dma_addr_t ecb_dma_addr;
 81	struct ecb ecb[AHA1740_ECBS];
 82};
 83
 84struct aha1740_sg {
 85	struct aha1740_chain sg_chain[AHA1740_SCATTER];
 86	dma_addr_t sg_dma_addr;
 87	dma_addr_t buf_dma_addr;
 88};
 89
 90#define HOSTDATA(host) ((struct aha1740_hostdata *) &host->hostdata)
 91
 92static inline struct ecb *ecb_dma_to_cpu (struct Scsi_Host *host,
 93					  dma_addr_t dma)
 94{
 95	struct aha1740_hostdata *hdata = HOSTDATA (host);
 96	dma_addr_t offset;
 97
 98	offset = dma - hdata->ecb_dma_addr;
 99
100	return (struct ecb *)(((char *) hdata->ecb) + (unsigned int) offset);
101}
102
103static inline dma_addr_t ecb_cpu_to_dma (struct Scsi_Host *host, void *cpu)
104{
105	struct aha1740_hostdata *hdata = HOSTDATA (host);
106	dma_addr_t offset;
107    
108	offset = (char *) cpu - (char *) hdata->ecb;
109
110	return hdata->ecb_dma_addr + offset;
111}
112
113static int aha1740_show_info(struct seq_file *m, struct Scsi_Host *shpnt)
114{
115	struct aha1740_hostdata *host = HOSTDATA(shpnt);
116	seq_printf(m, "aha174x at IO:%lx, IRQ %d, SLOT %d.\n"
117		      "Extended translation %sabled.\n",
118		      shpnt->io_port, shpnt->irq, host->edev->slot,
119		      host->translation ? "en" : "dis");
120	return 0;
121}
122
123static int aha1740_makecode(unchar *sense, unchar *status)
124{
125	struct statusword
126	{
127		ushort	don:1,	/* Command Done - No Error */
128			du:1,	/* Data underrun */
129		    :1,	qf:1,	/* Queue full */
130		        sc:1,	/* Specification Check */
131		        dor:1,	/* Data overrun */
132		        ch:1,	/* Chaining Halted */
133		        intr:1,	/* Interrupt issued */
134		        asa:1,	/* Additional Status Available */
135		        sns:1,	/* Sense information Stored */
136		    :1,	ini:1,	/* Initialization Required */
137			me:1,	/* Major error or exception */
138		    :1,	eca:1,  /* Extended Contingent alliance */
139		    :1;
140	} status_word;
141	int retval = DID_OK;
142
143	status_word = * (struct statusword *) status;
144#ifdef DEBUG
145	printk("makecode from %x,%x,%x,%x %x,%x,%x,%x",
146	       status[0], status[1], status[2], status[3],
147	       sense[0], sense[1], sense[2], sense[3]);
148#endif
149	if (!status_word.don) { /* Anything abnormal was detected */
150		if ( (status[1]&0x18) || status_word.sc ) {
151			/*Additional info available*/
152			/* Use the supplied info for further diagnostics */
153			switch ( status[2] ) {
154			case 0x12:
155				if ( status_word.dor )
156					retval=DID_ERROR; /* It's an Overrun */
157				/* If not overrun, assume underrun and
158				 * ignore it! */
159				break;
160			case 0x00: /* No info, assume no error, should
161				    * not occur */
162				break;
163			case 0x11:
164			case 0x21:
165				retval=DID_TIME_OUT;
166				break;
167			case 0x0a:
168				retval=DID_BAD_TARGET;
169				break;
170			case 0x04:
171			case 0x05:
172				retval=DID_ABORT;
173				/* Either by this driver or the
174				 * AHA1740 itself */
175				break;
176			default:
177				retval=DID_ERROR; /* No further
178						   * diagnostics
179						   * possible */
180			}
181		} else {
182			/* Michael suggests, and Brad concurs: */
183			if ( status_word.qf ) {
184				retval = DID_TIME_OUT; /* forces a redo */
185				/* I think this specific one should
186				 * not happen -Brad */
187				printk("aha1740.c: WARNING: AHA1740 queue overflow!\n");
188			} else
189				if ( status[0]&0x60 ) {
190					 /* Didn't find a better error */
191					retval = DID_ERROR;
192				}
193			/* In any other case return DID_OK so for example
194			   CONDITION_CHECKS make it through to the appropriate
195			   device driver */
196		}
197	}
198	/* Under all circumstances supply the target status -Michael */
199	return status[3] | retval << 16;
200}
201
202static int aha1740_test_port(unsigned int base)
203{
204	if ( inb(PORTADR(base)) & PORTADDR_ENH )
205		return 1;   /* Okay, we're all set */
206	
207	printk("aha174x: Board detected, but not in enhanced mode, so disabled it.\n");
208	return 0;
209}
210
211/* A "high" level interrupt handler */
212static irqreturn_t aha1740_intr_handle(int irq, void *dev_id)
213{
214	struct Scsi_Host *host = (struct Scsi_Host *) dev_id;
215        void (*my_done)(struct scsi_cmnd *);
216	int errstatus, adapstat;
217	int number_serviced;
218	struct ecb *ecbptr;
219	struct scsi_cmnd *SCtmp;
220	unsigned int base;
221	unsigned long flags;
222	int handled = 0;
223	struct aha1740_sg *sgptr;
224	struct eisa_device *edev;
225	
226	if (!host)
227		panic("aha1740.c: Irq from unknown host!\n");
228	spin_lock_irqsave(host->host_lock, flags);
229	base = host->io_port;
230	number_serviced = 0;
231	edev = HOSTDATA(host)->edev;
232
233	while(inb(G2STAT(base)) & G2STAT_INTPEND) {
234		handled = 1;
235		DEB(printk("aha1740_intr top of loop.\n"));
236		adapstat = inb(G2INTST(base));
237		ecbptr = ecb_dma_to_cpu (host, inl(MBOXIN0(base)));
238		outb(G2CNTRL_IRST,G2CNTRL(base)); /* interrupt reset */
239      
240		switch ( adapstat & G2INTST_MASK ) {
241		case	G2INTST_CCBRETRY:
242		case	G2INTST_CCBERROR:
243		case	G2INTST_CCBGOOD:
244			/* Host Ready -> Mailbox in complete */
245			outb(G2CNTRL_HRDY,G2CNTRL(base));
246			if (!ecbptr) {
247				printk("Aha1740 null ecbptr in interrupt (%x,%x,%x,%d)\n",
248				       inb(G2STAT(base)),adapstat,
249				       inb(G2INTST(base)), number_serviced++);
250				continue;
251			}
252			SCtmp = ecbptr->SCpnt;
253			if (!SCtmp) {
254				printk("Aha1740 null SCtmp in interrupt (%x,%x,%x,%d)\n",
255				       inb(G2STAT(base)),adapstat,
256				       inb(G2INTST(base)), number_serviced++);
257				continue;
258			}
259			sgptr = (struct aha1740_sg *) SCtmp->host_scribble;
260			scsi_dma_unmap(SCtmp);
261
262			/* Free the sg block */
263			dma_free_coherent (&edev->dev,
264					   sizeof (struct aha1740_sg),
265					   SCtmp->host_scribble,
266					   sgptr->sg_dma_addr);
267	    
268			/* Fetch the sense data, and tuck it away, in
269			   the required slot.  The Adaptec
270			   automatically fetches it, and there is no
271			   guarantee that we will still have it in the
272			   cdb when we come back */
273			if ( (adapstat & G2INTST_MASK) == G2INTST_CCBERROR ) {
274				memcpy_and_pad(SCtmp->sense_buffer,
275					       SCSI_SENSE_BUFFERSIZE,
276					       ecbptr->sense,
277					       sizeof(ecbptr->sense),
278					       0);
279				errstatus = aha1740_makecode(ecbptr->sense,ecbptr->status);
280			} else
281				errstatus = 0;
282			DEB(if (errstatus)
283			    printk("aha1740_intr_handle: returning %6x\n",
284				   errstatus));
285			SCtmp->result = errstatus;
286			my_done = ecbptr->done;
287			memset(ecbptr,0,sizeof(struct ecb)); 
288			if ( my_done )
289				my_done(SCtmp);
290			break;
291			
292		case	G2INTST_HARDFAIL:
293			printk(KERN_ALERT "aha1740 hardware failure!\n");
294			panic("aha1740.c");	/* Goodbye */
295			
296		case	G2INTST_ASNEVENT:
297			printk("aha1740 asynchronous event: %02x %02x %02x %02x %02x\n",
298			       adapstat,
299			       inb(MBOXIN0(base)),
300			       inb(MBOXIN1(base)),
301			       inb(MBOXIN2(base)),
302			       inb(MBOXIN3(base))); /* Say What? */
303			/* Host Ready -> Mailbox in complete */
304			outb(G2CNTRL_HRDY,G2CNTRL(base));
305			break;
306			
307		case	G2INTST_CMDGOOD:
308			/* set immediate command success flag here: */
309			break;
310			
311		case	G2INTST_CMDERROR:
312			/* Set immediate command failure flag here: */
313			break;
314		}
315		number_serviced++;
316	}
317
318	spin_unlock_irqrestore(host->host_lock, flags);
319	return IRQ_RETVAL(handled);
320}
321
322static int aha1740_queuecommand_lck(struct scsi_cmnd *SCpnt)
323{
324	void (*done)(struct scsi_cmnd *) = scsi_done;
325	unchar direction;
326	unchar *cmd = (unchar *) SCpnt->cmnd;
327	unchar target = scmd_id(SCpnt);
328	struct aha1740_hostdata *host = HOSTDATA(SCpnt->device->host);
329	unsigned long flags;
330	dma_addr_t sg_dma;
331	struct aha1740_sg *sgptr;
332	int ecbno, nseg;
333	DEB(int i);
334
335	if(*cmd == REQUEST_SENSE) {
336		SCpnt->result = 0;
337		done(SCpnt); 
338		return 0;
339	}
340
341#ifdef DEBUG
342	if (*cmd == READ_10 || *cmd == WRITE_10)
343		i = xscsi2int(cmd+2);
344	else if (*cmd == READ_6 || *cmd == WRITE_6)
345		i = scsi2int(cmd+2);
346	else
347		i = -1;
348	printk("aha1740_queuecommand: dev %d cmd %02x pos %d len %d ",
349	       target, *cmd, i, bufflen);
350	printk("scsi cmd:");
351	for (i = 0; i < SCpnt->cmd_len; i++) printk("%02x ", cmd[i]);
352	printk("\n");
353#endif
354
355	/* locate an available ecb */
356	spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
357	ecbno = host->last_ecb_used + 1; /* An optimization */
358	if (ecbno >= AHA1740_ECBS)
359		ecbno = 0;
360	do {
361		if (!host->ecb[ecbno].cmdw)
362			break;
363		ecbno++;
364		if (ecbno >= AHA1740_ECBS)
365			ecbno = 0;
366	} while (ecbno != host->last_ecb_used);
367
368	if (host->ecb[ecbno].cmdw)
369		panic("Unable to find empty ecb for aha1740.\n");
370
371	host->ecb[ecbno].cmdw = AHA1740CMD_INIT; /* SCSI Initiator Command
372						    doubles as reserved flag */
373
374	host->last_ecb_used = ecbno;    
375	spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
376
377#ifdef DEBUG
378	printk("Sending command (%d %x)...", ecbno, done);
379#endif
380
381	host->ecb[ecbno].cdblen = SCpnt->cmd_len; /* SCSI Command
382						   * Descriptor Block
383						   * Length */
384
385	direction = 0;
386	if (*cmd == READ_10 || *cmd == READ_6)
387		direction = 1;
388	else if (*cmd == WRITE_10 || *cmd == WRITE_6)
389		direction = 0;
390
391	memcpy(host->ecb[ecbno].cdb, cmd, SCpnt->cmd_len);
392
393	SCpnt->host_scribble = dma_alloc_coherent (&host->edev->dev,
394						   sizeof (struct aha1740_sg),
395						   &sg_dma, GFP_ATOMIC);
396	if(SCpnt->host_scribble == NULL) {
397		printk(KERN_WARNING "aha1740: out of memory in queuecommand!\n");
398		return 1;
399	}
400	sgptr = (struct aha1740_sg *) SCpnt->host_scribble;
401	sgptr->sg_dma_addr = sg_dma;
402
403	nseg = scsi_dma_map(SCpnt);
404	BUG_ON(nseg < 0);
405	if (nseg) {
406		struct scatterlist *sg;
407		struct aha1740_chain * cptr;
408		int i;
409		DEB(unsigned char * ptr);
410
411		host->ecb[ecbno].sg = 1;  /* SCSI Initiator Command
412					   * w/scatter-gather*/
413		cptr = sgptr->sg_chain;
414		scsi_for_each_sg(SCpnt, sg, nseg, i) {
415			cptr[i].datalen = sg_dma_len (sg);
416			cptr[i].dataptr = sg_dma_address (sg);
417		}
418		host->ecb[ecbno].datalen = nseg * sizeof(struct aha1740_chain);
419		host->ecb[ecbno].dataptr = sg_dma;
420#ifdef DEBUG
421		printk("cptr %x: ",cptr);
422		ptr = (unsigned char *) cptr;
423		for(i=0;i<24;i++) printk("%02x ", ptr[i]);
424#endif
425	} else {
426		host->ecb[ecbno].datalen = 0;
427		host->ecb[ecbno].dataptr = 0;
428	}
429	host->ecb[ecbno].lun = SCpnt->device->lun;
430	host->ecb[ecbno].ses = 1; /* Suppress underrun errors */
431	host->ecb[ecbno].dir = direction;
432	host->ecb[ecbno].ars = 1; /* Yes, get the sense on an error */
433	host->ecb[ecbno].senselen = 12;
434	host->ecb[ecbno].senseptr = ecb_cpu_to_dma (SCpnt->device->host,
435						    host->ecb[ecbno].sense);
436	host->ecb[ecbno].statusptr = ecb_cpu_to_dma (SCpnt->device->host,
437						     host->ecb[ecbno].status);
438	host->ecb[ecbno].done = done;
439	host->ecb[ecbno].SCpnt = SCpnt;
440#ifdef DEBUG
441	{
442		int i;
443		printk("aha1740_command: sending.. ");
444		for (i = 0; i < sizeof(host->ecb[ecbno]) - 10; i++)
445			printk("%02x ", ((unchar *)&host->ecb[ecbno])[i]);
446	}
447	printk("\n");
448#endif
449	if (done) {
450	/* The Adaptec Spec says the card is so fast that the loops
451           will only be executed once in the code below. Even if this
452           was true with the fastest processors when the spec was
453           written, it doesn't seem to be true with today's fast
454           processors. We print a warning if the code is executed more
455           often than LOOPCNT_WARN. If this happens, it should be
456           investigated. If the count reaches LOOPCNT_MAX, we assume
457           something is broken; since there is no way to return an
458           error (the return value is ignored by the mid-level scsi
459           layer) we have to panic (and maybe that's the best thing we
460           can do then anyhow). */
461
462#define LOOPCNT_WARN 10		/* excessive mbxout wait -> syslog-msg */
463#define LOOPCNT_MAX 1000000	/* mbxout deadlock -> panic() after ~ 2 sec. */
464		int loopcnt;
465		unsigned int base = SCpnt->device->host->io_port;
466		DEB(printk("aha1740[%d] critical section\n",ecbno));
467
468		spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
469		for (loopcnt = 0; ; loopcnt++) {
470			if (inb(G2STAT(base)) & G2STAT_MBXOUT) break;
471			if (loopcnt == LOOPCNT_WARN) {
472				printk("aha1740[%d]_mbxout wait!\n",ecbno);
473			}
474			if (loopcnt == LOOPCNT_MAX)
475				panic("aha1740.c: mbxout busy!\n");
476		}
477		outl (ecb_cpu_to_dma (SCpnt->device->host, host->ecb + ecbno),
478		      MBOXOUT0(base));
479		for (loopcnt = 0; ; loopcnt++) {
480			if (! (inb(G2STAT(base)) & G2STAT_BUSY)) break;
481			if (loopcnt == LOOPCNT_WARN) {
482				printk("aha1740[%d]_attn wait!\n",ecbno);
483			}
484			if (loopcnt == LOOPCNT_MAX)
485				panic("aha1740.c: attn wait failed!\n");
486		}
487		outb(ATTN_START | (target & 7), ATTN(base)); /* Start it up */
488		spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
489		DEB(printk("aha1740[%d] request queued.\n",ecbno));
490	} else
491		printk(KERN_ALERT "aha1740_queuecommand: done can't be NULL\n");
492	return 0;
493}
494
495static DEF_SCSI_QCMD(aha1740_queuecommand)
496
497/* Query the board for its irq_level and irq_type.  Nothing else matters
498   in enhanced mode on an EISA bus. */
499
500static void aha1740_getconfig(unsigned int base, unsigned int *irq_level,
501			      unsigned int *irq_type,
502			      unsigned int *translation)
503{
504	static int intab[] = { 9, 10, 11, 12, 0, 14, 15, 0 };
505
506	*irq_level = intab[inb(INTDEF(base)) & 0x7];
507	*irq_type  = (inb(INTDEF(base)) & 0x8) >> 3;
508	*translation = inb(RESV1(base)) & 0x1;
509	outb(inb(INTDEF(base)) | 0x10, INTDEF(base));
510}
511
512static int aha1740_biosparam(struct scsi_device *sdev,
513			     struct block_device *dev,
514			     sector_t capacity, int* ip)
515{
516	int size = capacity;
517	int extended = HOSTDATA(sdev->host)->translation;
518
519	DEB(printk("aha1740_biosparam\n"));
520	if (extended && (ip[2] > 1024))	{
521		ip[0] = 255;
522		ip[1] = 63;
523		ip[2] = size / (255 * 63);
524	} else {
525		ip[0] = 64;
526		ip[1] = 32;
527		ip[2] = size >> 11;
528	}
529	return 0;
530}
531
532static int aha1740_eh_abort_handler (struct scsi_cmnd *dummy)
533{
534/*
535 * From Alan Cox :
536 * The AHA1740 has firmware handled abort/reset handling. The "head in
537 * sand" kernel code is correct for once 8)
538 *
539 * So we define a dummy handler just to keep the kernel SCSI code as
540 * quiet as possible...
541 */
542
543	return SUCCESS;
544}
545
546static const struct scsi_host_template aha1740_template = {
547	.module           = THIS_MODULE,
548	.proc_name        = "aha1740",
549	.show_info        = aha1740_show_info,
550	.name             = "Adaptec 174x (EISA)",
551	.queuecommand     = aha1740_queuecommand,
552	.bios_param       = aha1740_biosparam,
553	.can_queue        = AHA1740_ECBS,
554	.this_id          = 7,
555	.sg_tablesize     = AHA1740_SCATTER,
 
556	.eh_abort_handler = aha1740_eh_abort_handler,
557};
558
559static int aha1740_probe (struct device *dev)
560{
561	int slotbase, rc;
562	unsigned int irq_level, irq_type, translation;
563	struct Scsi_Host *shpnt;
564	struct aha1740_hostdata *host;
565	struct eisa_device *edev = to_eisa_device (dev);
566
567	DEB(printk("aha1740_probe: \n"));
568	
569	slotbase = edev->base_addr + EISA_VENDOR_ID_OFFSET;
570	if (!request_region(slotbase, SLOTSIZE, "aha1740")) /* See if in use */
571		return -EBUSY;
572	if (!aha1740_test_port(slotbase))
573		goto err_release_region;
574	aha1740_getconfig(slotbase,&irq_level,&irq_type,&translation);
575	if ((inb(G2STAT(slotbase)) &
576	     (G2STAT_MBXOUT|G2STAT_BUSY)) != G2STAT_MBXOUT) {
577		/* If the card isn't ready, hard reset it */
578		outb(G2CNTRL_HRST, G2CNTRL(slotbase));
579		outb(0, G2CNTRL(slotbase));
580	}
581	printk(KERN_INFO "Configuring slot %d at IO:%x, IRQ %u (%s)\n",
582	       edev->slot, slotbase, irq_level, irq_type ? "edge" : "level");
583	printk(KERN_INFO "aha174x: Extended translation %sabled.\n",
584	       translation ? "en" : "dis");
585	shpnt = scsi_host_alloc(&aha1740_template,
586			      sizeof(struct aha1740_hostdata));
587	if(shpnt == NULL)
588		goto err_release_region;
589
590	shpnt->base = 0;
591	shpnt->io_port = slotbase;
592	shpnt->n_io_port = SLOTSIZE;
593	shpnt->irq = irq_level;
594	shpnt->dma_channel = 0xff;
595	host = HOSTDATA(shpnt);
596	host->edev = edev;
597	host->translation = translation;
598	host->ecb_dma_addr = dma_map_single (&edev->dev, host->ecb,
599					     sizeof (host->ecb),
600					     DMA_BIDIRECTIONAL);
601	if (!host->ecb_dma_addr) {
602		printk (KERN_ERR "aha1740_probe: Couldn't map ECB, giving up\n");
 
603		goto err_host_put;
604	}
605	
606	DEB(printk("aha1740_probe: enable interrupt channel %d\n",irq_level));
607	if (request_irq(irq_level,aha1740_intr_handle,irq_type ? 0 : IRQF_SHARED,
608			"aha1740",shpnt)) {
609		printk(KERN_ERR "aha1740_probe: Unable to allocate IRQ %d.\n",
610		       irq_level);
611		goto err_unmap;
612	}
613
614	eisa_set_drvdata (edev, shpnt);
615
616	rc = scsi_add_host (shpnt, dev);
617	if (rc)
618		goto err_irq;
619
620	scsi_scan_host (shpnt);
621	return 0;
622
623 err_irq:
624 	free_irq(irq_level, shpnt);
625 err_unmap:
626	dma_unmap_single (&edev->dev, host->ecb_dma_addr,
627			  sizeof (host->ecb), DMA_BIDIRECTIONAL);
628 err_host_put:
629	scsi_host_put (shpnt);
630 err_release_region:
631	release_region(slotbase, SLOTSIZE);
632
633	return -ENODEV;
634}
635
636static int aha1740_remove (struct device *dev)
637{
638	struct Scsi_Host *shpnt = dev_get_drvdata(dev);
639	struct aha1740_hostdata *host = HOSTDATA (shpnt);
640
641	scsi_remove_host(shpnt);
642	
643	free_irq (shpnt->irq, shpnt);
644	dma_unmap_single (dev, host->ecb_dma_addr,
645			  sizeof (host->ecb), DMA_BIDIRECTIONAL);
646	release_region (shpnt->io_port, SLOTSIZE);
647
648	scsi_host_put (shpnt);
649	
650	return 0;
651}
652
653static struct eisa_device_id aha1740_ids[] = {
654	{ "ADP0000" },		/* 1740  */
655	{ "ADP0001" },		/* 1740A */
656	{ "ADP0002" },		/* 1742A */
657	{ "ADP0400" },		/* 1744  */
658	{ "" }
659};
660MODULE_DEVICE_TABLE(eisa, aha1740_ids);
661
662static struct eisa_driver aha1740_driver = {
663	.id_table = aha1740_ids,
664	.driver   = {
665		.name    = "aha1740",
666		.probe   = aha1740_probe,
667		.remove  = aha1740_remove,
668	},
669};
670
671static __init int aha1740_init (void)
672{
673	return eisa_driver_register (&aha1740_driver);
674}
675
676static __exit void aha1740_exit (void)
677{
678	eisa_driver_unregister (&aha1740_driver);
679}
680
681module_init (aha1740_init);
682module_exit (aha1740_exit);
683
684MODULE_DESCRIPTION("Adaptec AHA1740 SCSI host adapter driver");
685MODULE_LICENSE("GPL");