kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[dragonfly.git] / sys / dev / disk / buslogic / bt_isa.c
1 /*
2  * Product specific probe and attach routines for:
3  *      Buslogic BT-54X and BT-445 cards
4  *
5  * Copyright (c) 1998, 1999 Justin T. Gibbs
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/buslogic/bt_isa.c,v 1.18 1999/10/12 21:35:43 dfr Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38
39 #include <bus/isa/isavar.h>
40 #include "btreg.h"
41
42 #include <bus/cam/scsi/scsi_all.h>
43
44 static  bus_dmamap_callback_t btmapsensebuffers;
45
46 static int
47 bt_isa_alloc_resources(device_t dev, u_long portstart, u_long portend)
48 {
49         int rid;
50         struct resource *port;
51         struct resource *irq;
52         struct resource *drq;
53
54         rid = 0;
55         port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
56                                   portstart, portend, BT_NREGS, RF_ACTIVE);
57         if (!port)
58                 return (ENOMEM);
59
60         if (isa_get_irq(dev) != -1) {
61                 rid = 0;
62                 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
63                                          0, ~0, 1, RF_ACTIVE);
64                 if (!irq) {
65                         if (port)
66                                 bus_release_resource(dev, SYS_RES_IOPORT,
67                                                      0, port);
68                         return (ENOMEM);
69                 }
70         } else
71                 irq = 0;
72
73         if (isa_get_drq(dev) != -1) {
74                 rid = 0;
75                 drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
76                                          0, ~0, 1, RF_ACTIVE);
77                 if (!drq) {
78                         if (port)
79                                 bus_release_resource(dev, SYS_RES_IOPORT,
80                                                      0, port);
81                         if (irq)
82                                 bus_release_resource(dev, SYS_RES_IRQ,
83                                                      0, irq);
84                         return (ENOMEM);
85                 }
86         } else
87                 drq = 0;
88
89         bt_init_softc(dev, port, irq, drq);
90
91         return (0);
92 }
93
94 static void
95 bt_isa_release_resources(device_t dev)
96 {
97         struct  bt_softc *bt = device_get_softc(dev);
98
99         if (bt->port)
100                 bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
101         if (bt->irq)
102                 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
103         if (bt->drq)
104                 bus_release_resource(dev, SYS_RES_DRQ, 0, bt->drq);
105         bt_free_softc(dev);
106 }
107
108 /*
109  * Check if the device can be found at the port given
110  * and if so, set it up ready for further work
111  * as an argument, takes the isa_device structure from
112  * autoconf.c
113  */
114 static int
115 bt_isa_probe(device_t dev)
116 {
117         /*
118          * find unit and check we have that many defined
119          */
120         int     port_index;
121         int     max_port_index;
122
123         /* No pnp support */
124         if (isa_get_vendorid(dev))
125                 return (ENXIO);
126
127         port_index = 0;
128         max_port_index = BT_NUM_ISAPORTS - 1;
129         /*
130          * Bound our board search if the user has
131          * specified an exact port.
132          */
133         bt_find_probe_range(isa_get_port(dev), &port_index, &max_port_index);
134
135         if (port_index < 0)
136                 return (ENXIO);
137
138         /* Attempt to find an adapter */
139         for (;port_index <= max_port_index; port_index++) {
140                 struct bt_probe_info info;
141                 u_int ioport;
142
143                 ioport = bt_iop_from_bio(port_index);
144
145                 /*
146                  * Ensure this port has not already been claimed already
147                  * by a PCI, EISA or ISA adapter.
148                  */
149                 if (bt_check_probed_iop(ioport) != 0)
150                         continue;
151
152                 /* Initialise the softc for use during probing */
153                 if (bt_isa_alloc_resources(dev, ioport,
154                                            ioport + BT_NREGS -1) != 0)
155                         continue;
156
157                 /* We're going to attempt to probe it now, so mark it probed */
158                 bt_mark_probed_bio(port_index);
159
160                 if (bt_port_probe(dev, &info) != 0) {
161                         if (bootverbose)
162                                 kprintf("bt_isa_probe: Probe failed at 0x%x\n",
163                                        ioport);
164                         bt_isa_release_resources(dev);
165                         continue;
166                 }
167
168                 bt_isa_release_resources(dev);
169
170                 bus_set_resource(dev, SYS_RES_DRQ, 0, info.drq, 1);
171                 bus_set_resource(dev, SYS_RES_IRQ, 0, info.irq, 1);
172
173                 return (0);
174         }
175
176         return (ENXIO);
177 }
178
179 /*
180  * Attach all the sub-devices we can find
181  */
182 static int
183 bt_isa_attach(device_t dev)
184 {
185         struct  bt_softc *bt = device_get_softc(dev);
186         bus_dma_filter_t *filter;
187         void             *filter_arg;
188         bus_addr_t       lowaddr;
189         int              error, drq;
190
191         /* Initialise softc */
192         error = bt_isa_alloc_resources(dev, 0, ~0);
193         if (error) {
194                 device_printf(dev, "can't allocate resources in bt_isa_attach\n");
195                 return error;
196         }
197
198         /* Program the DMA channel for external control */
199         if ((drq = isa_get_drq(dev)) != -1)
200                 isa_dmacascade(drq);
201
202         /* Allocate our parent dmatag */
203         filter = NULL;
204         filter_arg = NULL;
205         lowaddr = BUS_SPACE_MAXADDR_24BIT;
206
207         /* XXX Should be a child of the ISA bus dma tag */
208         if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/1, /*boundary*/0,
209                                lowaddr, /*highaddr*/BUS_SPACE_MAXADDR,
210                                filter, filter_arg,
211                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
212                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
213                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
214                                /*flags*/0, &bt->parent_dmat) != 0) {
215                 bt_isa_release_resources(dev);
216                 return (ENOMEM);
217         }                              
218
219         error = bt_init(dev);
220         if (error) {
221                 bt_isa_release_resources(dev);
222                 return (ENOMEM);
223         }
224
225         if (lowaddr != BUS_SPACE_MAXADDR_32BIT) {
226                 /* DMA tag for our sense buffers */
227                 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1,
228                                        /*boundary*/0,
229                                        /*lowaddr*/BUS_SPACE_MAXADDR,
230                                        /*highaddr*/BUS_SPACE_MAXADDR,
231                                        /*filter*/NULL, /*filterarg*/NULL,
232                                        bt->max_ccbs
233                                            * sizeof(struct scsi_sense_data),
234                                        /*nsegments*/1,
235                                        /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
236                                        /*flags*/0, &bt->sense_dmat) != 0) {
237                         bt_isa_release_resources(dev);
238                         return (ENOMEM);
239                 }
240
241                 bt->init_level++;
242
243                 /* Allocation of sense buffers */
244                 if (bus_dmamem_alloc(bt->sense_dmat,
245                                      (void *)&bt->sense_buffers,
246                                      BUS_DMA_NOWAIT, &bt->sense_dmamap) != 0) {
247                         bt_isa_release_resources(dev);
248                         return (ENOMEM);
249                 }
250
251                 bt->init_level++;
252
253                 /* And permanently map them */
254                 bus_dmamap_load(bt->sense_dmat, bt->sense_dmamap,
255                                 bt->sense_buffers,
256                                 bt->max_ccbs * sizeof(*bt->sense_buffers),
257                                 btmapsensebuffers, bt, /*flags*/0);
258
259                 bt->init_level++;
260         }
261
262         error = bt_attach(dev);
263         if (error) {
264                 bt_isa_release_resources(dev);
265                 return (error);
266         }
267
268         return (0);
269 }
270
271 #define BIOS_MAP_SIZE (16 * 1024)
272
273 static void
274 btmapsensebuffers(void *arg, bus_dma_segment_t *segs, int nseg, int error)
275 {
276         struct bt_softc* bt;
277
278         bt = (struct bt_softc*)arg;
279         bt->sense_buffers_physbase = segs->ds_addr;
280 }
281
282 static device_method_t bt_isa_methods[] = {
283         /* Device interface */
284         DEVMETHOD(device_probe,         bt_isa_probe),
285         DEVMETHOD(device_attach,        bt_isa_attach),
286
287         { 0, 0 }
288 };
289
290 static driver_t bt_isa_driver = {
291         "bt",
292         bt_isa_methods,
293         sizeof(struct bt_softc),
294 };
295
296 static devclass_t bt_devclass;
297
298 DRIVER_MODULE(bt, isa, bt_isa_driver, bt_devclass, NULL, NULL);