aha_isa.c revision 122340
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
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/aha/aha_isa.c 122340 2003-11-09 00:51:52Z imp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40
41#include <machine/bus_pio.h>
42#include <machine/bus.h>
43#include <machine/resource.h>
44#include <sys/module.h>
45#include <sys/bus.h>
46#include <sys/rman.h>
47
48#include <isa/isavar.h>
49
50#include <dev/aha/ahareg.h>
51
52#include <cam/scsi/scsi_all.h>
53
54static struct isa_pnp_id aha_ids[] = {
55	{ADP0100_PNP,		"Adaptec 1540/1542 ISA SCSI"},	/* ADP0100 */
56	{AHA1540_PNP,		"Adaptec 1540/aha-1640/aha-1535"},/* ADP1542 */
57	{AHA1542_PNP,		"Adaptec 1542/aha-1535"},	/* ADP1542 */
58	{AHA1542_PNPCOMPAT,	"Adaptec 1542 compatible"},	/* PNP00A0 */
59	{ICU0091_PNP,		"Adaptec AHA-1540/1542 SCSI"},	/* ICU0091 */
60	{0}
61};
62
63/*
64 * Check if the device can be found at the port given
65 * and if so, set it up ready for further work
66 * as an argument, takes the isa_device structure from
67 * autoconf.c
68 */
69static int
70aha_isa_probe(device_t dev)
71{
72	/*
73	 * find unit and check we have that many defined
74	 */
75	struct	aha_softc **sc = device_get_softc(dev);
76	struct	aha_softc *aha;
77	int	port_index;
78	int	max_port_index;
79	int	error;
80	u_long	port_start, port_count;
81	struct resource *port_res;
82	int	port_rid;
83	int	drq;
84	int	irq;
85
86	aha = NULL;
87
88	/* Check isapnp ids */
89	if (ISA_PNP_PROBE(device_get_parent(dev), dev, aha_ids) == ENXIO)
90		return (ENXIO);
91
92	error = bus_get_resource(dev, SYS_RES_IOPORT, 0,
93				 &port_start, &port_count);
94	if (error != 0)
95		port_start = 0;
96
97	/*
98	 * Bound our board search if the user has
99	 * specified an exact port.
100	 */
101	aha_find_probe_range(port_start, &port_index, &max_port_index);
102
103	if (port_index < 0)
104		return ENXIO;
105
106	/* Attempt to find an adapter */
107	for (;port_index <= max_port_index; port_index++) {
108		config_data_t config_data;
109		u_int ioport;
110		int error;
111
112		ioport = aha_iop_from_bio(port_index);
113
114		error = bus_set_resource(dev, SYS_RES_IOPORT, 0,
115					 ioport, AHA_NREGS);
116		if (error)
117			return error;
118
119		port_rid = 0;
120		port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &port_rid,
121		    0, ~0, AHA_NREGS, RF_ACTIVE);
122		if (!port_res)
123			continue;
124
125		/* Allocate a softc for use during probing */
126		aha = aha_alloc(device_get_unit(dev), rman_get_bustag(port_res),
127		    rman_get_bushandle(port_res));
128
129		if (aha == NULL) {
130			bus_release_resource(dev, SYS_RES_IOPORT, port_rid,
131			    port_res);
132			break;
133		}
134
135		/* See if there is really a card present */
136		if (aha_probe(aha) || aha_fetch_adapter_info(aha)) {
137			aha_free(aha);
138			bus_release_resource(dev, SYS_RES_IOPORT, port_rid,
139			    port_res);
140			continue;
141		}
142
143		/*
144		 * Determine our IRQ, and DMA settings and
145		 * export them to the configuration system.
146		 */
147		error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
148		    (uint8_t*)&config_data, sizeof(config_data),
149		    DEFAULT_CMD_TIMEOUT);
150
151		if (error != 0) {
152			printf("aha_isa_probe: Could not determine IRQ or DMA "
153			    "settings for adapter at 0x%x.  Failing probe\n",
154			    ioport);
155			aha_free(aha);
156			bus_release_resource(dev, SYS_RES_IOPORT, port_rid,
157			    port_res);
158			continue;
159		}
160
161		bus_release_resource(dev, SYS_RES_IOPORT, port_rid, port_res);
162
163		switch (config_data.dma_chan) {
164		case DMA_CHAN_5:
165			drq = 5;
166			break;
167		case DMA_CHAN_6:
168			drq = 6;
169			break;
170		case DMA_CHAN_7:
171			drq = 7;
172			break;
173		default:
174			printf("aha_isa_probe: Invalid DMA setting "
175			    "detected for adapter at 0x%x.  "
176			    "Failing probe\n", ioport);
177			return (ENXIO);
178		}
179		error = bus_set_resource(dev, SYS_RES_DRQ, 0, drq, 1);
180		if (error)
181			return error;
182
183		irq = ffs(config_data.irq) + 8;
184		error = bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1);
185		if (error)
186			return error;
187
188		*sc = aha;
189		aha_unit++;
190
191		return (0);
192	}
193
194	return (ENXIO);
195}
196
197/*
198 * Attach all the sub-devices we can find
199 */
200static int
201aha_isa_attach(device_t dev)
202{
203	struct	aha_softc **sc = device_get_softc(dev);
204	struct	aha_softc *aha;
205	bus_dma_filter_t *filter;
206	void		 *filter_arg;
207	bus_addr_t	 lowaddr;
208	void		 *ih;
209	int		 error;
210
211	aha = *sc;
212	aha->portrid = 0;
213	aha->port = bus_alloc_resource(dev, SYS_RES_IOPORT, &aha->portrid,
214	    0, ~0, AHA_NREGS, RF_ACTIVE);
215	if (!aha->port) {
216		device_printf(dev, "Unable to allocate I/O ports\n");
217		return ENOMEM;
218	}
219
220	aha->irqrid = 0;
221	aha->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &aha->irqrid, 0, ~0, 1,
222	    RF_ACTIVE);
223	if (!aha->irq) {
224		device_printf(dev, "Unable to allocate excluse use of irq\n");
225		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
226		return ENOMEM;
227	}
228
229	aha->drqrid = 0;
230	aha->drq = bus_alloc_resource(dev, SYS_RES_DRQ, &aha->drqrid, 0, ~0, 1,
231	    RF_ACTIVE);
232	if (!aha->drq) {
233		device_printf(dev, "Unable to allocate drq\n");
234		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
235		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
236		return ENOMEM;
237	}
238
239#if 0				/* is the drq ever unset? */
240	if (dev->id_drq != -1)
241		isa_dmacascade(dev->id_drq);
242#endif
243	isa_dmacascade(rman_get_start(aha->drq));
244
245	/* Allocate our parent dmatag */
246	filter = NULL;
247	filter_arg = NULL;
248	lowaddr = BUS_SPACE_MAXADDR_24BIT;
249
250	if (bus_dma_tag_create(	/* parent	*/ NULL,
251				/* alignemnt	*/ 1,
252				/* boundary	*/ 0,
253				/* lowaddr	*/ lowaddr,
254				/* highaddr	*/ BUS_SPACE_MAXADDR,
255				/* filter	*/ filter,
256				/* filterarg	*/ filter_arg,
257				/* maxsize	*/ BUS_SPACE_MAXSIZE_24BIT,
258				/* nsegments	*/ ~0,
259				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
260				/* flags	*/ 0,
261				/* lockfunc	*/ busdma_lock_mutex,
262				/* lockarg	*/ &Giant,
263				&aha->parent_dmat) != 0) {
264                aha_free(aha);
265		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
266		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
267		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
268                return (ENOMEM);
269        }
270
271        if (aha_init(aha)) {
272		device_printf(dev, "init failed\n");
273                aha_free(aha);
274		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
275		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
276		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
277                return (ENOMEM);
278        }
279
280	error = aha_attach(aha);
281	if (error) {
282		device_printf(dev, "attach failed\n");
283                aha_free(aha);
284		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
285		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
286		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
287                return (error);
288	}
289
290	error = bus_setup_intr(dev, aha->irq, INTR_TYPE_CAM|INTR_ENTROPY, aha_intr, aha,
291	    &ih);
292	if (error) {
293		device_printf(dev, "Unable to register interrupt handler\n");
294                aha_free(aha);
295		bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
296		bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
297		bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
298                return (error);
299	}
300
301	return (0);
302}
303
304static int
305aha_isa_detach(device_t dev)
306{
307	struct aha_softc *aha = *(struct aha_softc **) device_get_softc(dev);
308	int error;
309
310	error = bus_teardown_intr(dev, aha->irq, aha->ih);
311	if (error) {
312		device_printf(dev, "failed to unregister interrupt handler\n");
313	}
314
315	bus_release_resource(dev, SYS_RES_IOPORT, aha->portrid, aha->port);
316	bus_release_resource(dev, SYS_RES_IRQ, aha->irqrid, aha->irq);
317	bus_release_resource(dev, SYS_RES_DRQ, aha->drqrid, aha->drq);
318
319	error = aha_detach(aha);
320	if (error) {
321		device_printf(dev, "detach failed\n");
322		return (error);
323	}
324	aha_free(aha);
325
326	return (0);
327}
328
329static void
330aha_isa_identify(driver_t *driver, device_t parent)
331{
332}
333
334static device_method_t aha_isa_methods[] = {
335	/* Device interface */
336	DEVMETHOD(device_probe,		aha_isa_probe),
337	DEVMETHOD(device_attach,	aha_isa_attach),
338	DEVMETHOD(device_detach,	aha_isa_detach),
339	DEVMETHOD(device_identify,	aha_isa_identify),
340
341	{ 0, 0 }
342};
343
344static driver_t aha_isa_driver = {
345	"aha",
346	aha_isa_methods,
347	sizeof(struct aha_softc*),
348};
349
350static devclass_t aha_devclass;
351
352DRIVER_MODULE(aha, isa, aha_isa_driver, aha_devclass, 0, 0);
353