aha_isa.c revision 117126
1/*
2 * Product specific probe and attach routines for:
3 *      Adaptec 154x.
4 *
5 * Derived from code written by:
6 *
7 * Copyright (c) 1998 Justin T. Gibbs
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions, and the following disclaimer,
15 *    without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $FreeBSD: head/sys/dev/aha/aha_isa.c 117126 2003-07-01 15:52:06Z scottl $
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39
40#include <machine/bus_pio.h>
41#include <machine/bus.h>
42#include <machine/resource.h>
43#include <sys/module.h>
44#include <sys/bus.h>
45#include <sys/rman.h>
46
47#include <isa/isavar.h>
48
49#include <dev/aha/ahareg.h>
50
51#include <cam/scsi/scsi_all.h>
52
53static struct isa_pnp_id aha_ids[] = {
54	{ADP0100_PNP,		"Adaptec 1540/1542 ISA SCSI"},	/* ADP0100 */
55	{AHA1540_PNP,		"Adaptec 1540/aha-1640/aha-1535"},/* ADP1542 */
56	{AHA1542_PNP,		"Adaptec 1542/aha-1535"},	/* ADP1542 */
57	{AHA1542_PNPCOMPAT,	"Adaptec 1542 compatible"},	/* PNP00A0 */
58	{ICU0091_PNP,		"Adaptec AHA-1540/1542 SCSI"},	/* ICU0091 */
59	{0}
60};
61
62/*
63 * Check if the device can be found at the port given
64 * and if so, set it up ready for further work
65 * as an argument, takes the isa_device structure from
66 * autoconf.c
67 */
68static int
69aha_isa_probe(device_t dev)
70{
71	/*
72	 * find unit and check we have that many defined
73	 */
74	struct	aha_softc **sc = device_get_softc(dev);
75	struct	aha_softc *aha;
76	int	port_index;
77	int	max_port_index;
78	int	error;
79	u_long	port_start, port_count;
80	struct resource *port_res;
81	int	port_rid;
82	int	drq;
83	int	irq;
84
85	aha = NULL;
86
87	/* Check isapnp ids */
88	if (ISA_PNP_PROBE(device_get_parent(dev), dev, aha_ids) == ENXIO)
89		return (ENXIO);
90
91	error = bus_get_resource(dev, SYS_RES_IOPORT, 0,
92				 &port_start, &port_count);
93	if (error != 0)
94		port_start = 0;
95
96	/*
97	 * Bound our board search if the user has
98	 * specified an exact port.
99	 */
100	aha_find_probe_range(port_start, &port_index, &max_port_index);
101
102	if (port_index < 0)
103		return ENXIO;
104
105	/* Attempt to find an adapter */
106	for (;port_index <= max_port_index; port_index++) {
107		config_data_t config_data;
108		u_int ioport;
109		int error;
110
111		ioport = aha_iop_from_bio(port_index);
112
113		error = bus_set_resource(dev, SYS_RES_IOPORT, 0,
114					 ioport, AHA_NREGS);
115		if (error)
116			return error;
117
118		port_rid = 0;
119		port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &port_rid,
120		    0, ~0, AHA_NREGS, RF_ACTIVE);
121		if (!port_res)
122			continue;
123
124		/* Allocate a softc for use during probing */
125		aha = aha_alloc(device_get_unit(dev), rman_get_bustag(port_res),
126		    rman_get_bushandle(port_res));
127
128		if (aha == NULL) {
129			bus_release_resource(dev, SYS_RES_IOPORT, port_rid,
130			    port_res);
131			break;
132		}
133
134		/* See if there is really a card present */
135		if (aha_probe(aha) || aha_fetch_adapter_info(aha)) {
136			aha_free(aha);
137			bus_release_resource(dev, SYS_RES_IOPORT, port_rid,
138			    port_res);
139			continue;
140		}
141
142		/*
143		 * Determine our IRQ, and DMA settings and
144		 * export them to the configuration system.
145		 */
146		error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
147		    (u_int8_t*)&config_data, sizeof(config_data),
148		    DEFAULT_CMD_TIMEOUT);
149
150		if (error != 0) {
151			printf("aha_isa_probe: Could not determine IRQ or DMA "
152			    "settings for adapter at 0x%x.  Failing probe\n",
153			    ioport);
154			aha_free(aha);
155			bus_release_resource(dev, SYS_RES_IOPORT, port_rid,
156			    port_res);
157			continue;
158		}
159
160		bus_release_resource(dev, SYS_RES_IOPORT, port_rid, port_res);
161
162		switch (config_data.dma_chan) {
163		case DMA_CHAN_5:
164			drq = 5;
165			break;
166		case DMA_CHAN_6:
167			drq = 6;
168			break;
169		case DMA_CHAN_7:
170			drq = 7;
171			break;
172		default:
173			printf("aha_isa_probe: Invalid DMA setting "
174			    "detected for adapter at 0x%x.  "
175			    "Failing probe\n", ioport);
176			return (ENXIO);
177		}
178		error = bus_set_resource(dev, SYS_RES_DRQ, 0, drq, 1);
179		if (error)
180			return error;
181
182		irq = ffs(config_data.irq) + 8;
183		error = bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1);
184		if (error)
185			return error;
186
187		*sc = aha;
188		aha_unit++;
189
190		return (0);
191	}
192
193	return (ENXIO);
194}
195
196/*
197 * Attach all the sub-devices we can find
198 */
199static int
200aha_isa_attach(device_t dev)
201{
202	struct	aha_softc **sc = device_get_softc(dev);
203	struct	aha_softc *aha;
204	bus_dma_filter_t *filter;
205	void		 *filter_arg;
206	bus_addr_t	 lowaddr;
207	void		 *ih;
208	int		 error;
209
210	aha = *sc;
211	aha->portrid = 0;
212	aha->port = bus_alloc_resource(dev, SYS_RES_IOPORT, &aha->portrid,
213	    0, ~0, AHA_NREGS, RF_ACTIVE);
214	if (!aha->port) {
215		device_printf(dev, "Unable to allocate I/O ports\n");
216		return ENOMEM;
217	}
218
219	aha->irqrid = 0;
220	aha->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &aha->irqrid, 0, ~0, 1,
221	    RF_ACTIVE);
222	if (!aha->irq) {
223		device_printf(dev, "Unable to allocate excluse use of irq\n");
224		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
225		return ENOMEM;
226	}
227
228	aha->drqrid = 0;
229	aha->drq = bus_alloc_resource(dev, SYS_RES_DRQ, &aha->drqrid, 0, ~0, 1,
230	    RF_ACTIVE);
231	if (!aha->drq) {
232		device_printf(dev, "Unable to allocate drq\n");
233		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
234		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
235		return ENOMEM;
236	}
237
238#if 0				/* is the drq ever unset? */
239	if (dev->id_drq != -1)
240		isa_dmacascade(dev->id_drq);
241#endif
242	isa_dmacascade(rman_get_start(aha->drq));
243
244	/* Allocate our parent dmatag */
245	filter = NULL;
246	filter_arg = NULL;
247	lowaddr = BUS_SPACE_MAXADDR_24BIT;
248
249	if (bus_dma_tag_create(	/* parent	*/ NULL,
250				/* alignemnt	*/ 1,
251				/* boundary	*/ 0,
252				/* lowaddr	*/ lowaddr,
253				/* highaddr	*/ BUS_SPACE_MAXADDR,
254				/* filter	*/ filter,
255				/* filterarg	*/ filter_arg,
256				/* maxsize	*/ BUS_SPACE_MAXSIZE_24BIT,
257				/* nsegments	*/ ~0,
258				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
259				/* flags	*/ 0,
260				/* lockfunc	*/ busdma_lock_mutex,
261				/* lockarg	*/ &Giant,
262				&aha->parent_dmat) != 0) {
263                aha_free(aha);
264		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
265		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
266		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
267                return (ENOMEM);
268        }
269
270        if (aha_init(aha)) {
271		device_printf(dev, "init failed\n");
272                aha_free(aha);
273		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
274		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
275		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
276                return (ENOMEM);
277        }
278
279	error = aha_attach(aha);
280	if (error) {
281		device_printf(dev, "attach failed\n");
282                aha_free(aha);
283		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
284		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
285		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
286                return (error);
287	}
288
289	error = bus_setup_intr(dev, aha->irq, INTR_TYPE_CAM|INTR_ENTROPY, aha_intr, aha,
290	    &ih);
291	if (error) {
292		device_printf(dev, "Unable to register interrupt handler\n");
293                aha_free(aha);
294		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
295		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
296		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
297                return (error);
298	}
299
300	return (0);
301}
302
303static int
304aha_isa_detach(device_t dev)
305{
306	struct aha_softc *aha = *(struct aha_softc **) device_get_softc(dev);
307	int error;
308
309	error = bus_teardown_intr(dev, aha->irq, aha->ih);
310	if (error) {
311		device_printf(dev, "failed to unregister interrupt handler\n");
312	}
313
314	bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
315	bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
316	bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
317
318	error = aha_detach(aha);
319	if (error) {
320		device_printf(dev, "detach failed\n");
321		return (error);
322	}
323	aha_free(aha);
324
325	return (0);
326}
327
328static void
329aha_isa_identify(driver_t *driver, device_t parent)
330{
331}
332
333static device_method_t aha_isa_methods[] = {
334	/* Device interface */
335	DEVMETHOD(device_probe,		aha_isa_probe),
336	DEVMETHOD(device_attach,	aha_isa_attach),
337	DEVMETHOD(device_detach,	aha_isa_detach),
338	DEVMETHOD(device_identify,	aha_isa_identify),
339
340	{ 0, 0 }
341};
342
343static driver_t aha_isa_driver = {
344	"aha",
345	aha_isa_methods,
346	sizeof(struct aha_softc*),
347};
348
349static devclass_t aha_devclass;
350
351DRIVER_MODULE(aha, isa, aha_isa_driver, aha_devclass, 0, 0);
352