Fix typo.
[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  * $DragonFly: src/sys/dev/disk/buslogic/bt_isa.c,v 1.5 2006/12/22 23:26:16 swildner Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/rman.h>
39
40 #include <bus/isa/isavar.h>
41 #include "btreg.h"
42
43 #include <bus/cam/scsi/scsi_all.h>
44
45 static  bus_dma_filter_t btvlbouncefilter;
46 static  bus_dmamap_callback_t btmapsensebuffers;
47
48 static int
49 bt_isa_alloc_resources(device_t dev, u_long portstart, u_long portend)
50 {
51         int rid;
52         struct resource *port;
53         struct resource *irq;
54         struct resource *drq;
55
56         rid = 0;
57         port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
58                                   portstart, portend, BT_NREGS, RF_ACTIVE);
59         if (!port)
60                 return (ENOMEM);
61
62         if (isa_get_irq(dev) != -1) {
63                 rid = 0;
64                 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
65                                          0, ~0, 1, RF_ACTIVE);
66                 if (!irq) {
67                         if (port)
68                                 bus_release_resource(dev, SYS_RES_IOPORT,
69                                                      0, port);
70                         return (ENOMEM);
71                 }
72         } else
73                 irq = 0;
74
75         if (isa_get_drq(dev) != -1) {
76                 rid = 0;
77                 drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
78                                          0, ~0, 1, RF_ACTIVE);
79                 if (!drq) {
80                         if (port)
81                                 bus_release_resource(dev, SYS_RES_IOPORT,
82                                                      0, port);
83                         if (irq)
84                                 bus_release_resource(dev, SYS_RES_IRQ,
85                                                      0, irq);
86                         return (ENOMEM);
87                 }
88         } else
89                 drq = 0;
90
91         bt_init_softc(dev, port, irq, drq);
92
93         return (0);
94 }
95
96 static void
97 bt_isa_release_resources(device_t dev)
98 {
99         struct  bt_softc *bt = device_get_softc(dev);
100
101         if (bt->port)
102                 bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
103         if (bt->irq)
104                 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
105         if (bt->drq)
106                 bus_release_resource(dev, SYS_RES_DRQ, 0, bt->drq);
107         bt_free_softc(dev);
108 }
109
110 /*
111  * Check if the device can be found at the port given
112  * and if so, set it up ready for further work
113  * as an argument, takes the isa_device structure from
114  * autoconf.c
115  */
116 static int
117 bt_isa_probe(device_t dev)
118 {
119         /*
120          * find unit and check we have that many defined
121          */
122         int     port_index;
123         int     max_port_index;
124
125         /* No pnp support */
126         if (isa_get_vendorid(dev))
127                 return (ENXIO);
128
129         port_index = 0;
130         max_port_index = BT_NUM_ISAPORTS - 1;
131         /*
132          * Bound our board search if the user has
133          * specified an exact port.
134          */
135         bt_find_probe_range(isa_get_port(dev), &port_index, &max_port_index);
136
137         if (port_index < 0)
138                 return (ENXIO);
139
140         /* Attempt to find an adapter */
141         for (;port_index <= max_port_index; port_index++) {
142                 struct bt_probe_info info;
143                 u_int ioport;
144
145                 ioport = bt_iop_from_bio(port_index);
146
147                 /*
148                  * Ensure this port has not already been claimed already
149                  * by a PCI, EISA or ISA adapter.
150                  */
151                 if (bt_check_probed_iop(ioport) != 0)
152                         continue;
153
154                 /* Initialise the softc for use during probing */
155                 if (bt_isa_alloc_resources(dev, ioport,
156                                            ioport + BT_NREGS -1) != 0)
157                         continue;
158
159                 /* We're going to attempt to probe it now, so mark it probed */
160                 bt_mark_probed_bio(port_index);
161
162                 if (bt_port_probe(dev, &info) != 0) {
163                         if (bootverbose)
164                                 kprintf("bt_isa_probe: Probe failed at 0x%x\n",
165                                        ioport);
166                         bt_isa_release_resources(dev);
167                         continue;
168                 }
169
170                 bt_isa_release_resources(dev);
171
172                 bus_set_resource(dev, SYS_RES_DRQ, 0, info.drq, 1);
173                 bus_set_resource(dev, SYS_RES_IRQ, 0, info.irq, 1);
174
175                 return (0);
176         }
177
178         return (ENXIO);
179 }
180
181 /*
182  * Attach all the sub-devices we can find
183  */
184 static int
185 bt_isa_attach(device_t dev)
186 {
187         struct  bt_softc *bt = device_get_softc(dev);
188         bus_dma_filter_t *filter;
189         void             *filter_arg;
190         bus_addr_t       lowaddr;
191         int              error, drq;
192
193         /* Initialise softc */
194         error = bt_isa_alloc_resources(dev, 0, ~0);
195         if (error) {
196                 device_printf(dev, "can't allocate resources in bt_isa_attach\n");
197                 return error;
198         }
199
200         /* Program the DMA channel for external control */
201         if ((drq = isa_get_drq(dev)) != -1)
202                 isa_dmacascade(drq);
203
204         /* Allocate our parent dmatag */
205         filter = NULL;
206         filter_arg = NULL;
207         lowaddr = BUS_SPACE_MAXADDR_24BIT;
208         if (bt->model[0] == '4') {
209                 /*
210                  * This is a VL adapter.  Typically, VL devices have access
211                  * to the full 32bit address space.  On BT-445S adapters
212                  * prior to revision E, there is a hardware bug that causes
213                  * corruption of transfers to/from addresses in the range of
214                  * the BIOS modulo 16MB.  The only properly functioning
215                  * BT-445S Host Adapters have firmware version 3.37.
216                  * If we encounter one of these adapters and the BIOS is
217                  * installed, install a filter function for our bus_dma_map
218                  * that will catch these accesses and bounce them to a safe
219                  * region of memory.
220                  */
221                 if (bt->bios_addr != 0
222                  && strcmp(bt->model, "445S") == 0
223                  && strcmp(bt->firmware_ver, "3.37") < 0) {
224                         filter = btvlbouncefilter;
225                         filter_arg = bt;
226                 } else {
227                         lowaddr = BUS_SPACE_MAXADDR_32BIT;
228                 }
229         }
230                         
231         /* XXX Should be a child of the ISA or VL bus dma tag */
232         if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/1, /*boundary*/0,
233                                lowaddr, /*highaddr*/BUS_SPACE_MAXADDR,
234                                filter, filter_arg,
235                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
236                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
237                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
238                                /*flags*/0, &bt->parent_dmat) != 0) {
239                 bt_isa_release_resources(dev);
240                 return (ENOMEM);
241         }                              
242
243         error = bt_init(dev);
244         if (error) {
245                 bt_isa_release_resources(dev);
246                 return (ENOMEM);
247         }
248
249         if (lowaddr != BUS_SPACE_MAXADDR_32BIT) {
250                 /* DMA tag for our sense buffers */
251                 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1,
252                                        /*boundary*/0,
253                                        /*lowaddr*/BUS_SPACE_MAXADDR,
254                                        /*highaddr*/BUS_SPACE_MAXADDR,
255                                        /*filter*/NULL, /*filterarg*/NULL,
256                                        bt->max_ccbs
257                                            * sizeof(struct scsi_sense_data),
258                                        /*nsegments*/1,
259                                        /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
260                                        /*flags*/0, &bt->sense_dmat) != 0) {
261                         bt_isa_release_resources(dev);
262                         return (ENOMEM);
263                 }
264
265                 bt->init_level++;
266
267                 /* Allocation of sense buffers */
268                 if (bus_dmamem_alloc(bt->sense_dmat,
269                                      (void **)&bt->sense_buffers,
270                                      BUS_DMA_NOWAIT, &bt->sense_dmamap) != 0) {
271                         bt_isa_release_resources(dev);
272                         return (ENOMEM);
273                 }
274
275                 bt->init_level++;
276
277                 /* And permanently map them */
278                 bus_dmamap_load(bt->sense_dmat, bt->sense_dmamap,
279                                 bt->sense_buffers,
280                                 bt->max_ccbs * sizeof(*bt->sense_buffers),
281                                 btmapsensebuffers, bt, /*flags*/0);
282
283                 bt->init_level++;
284         }
285
286         error = bt_attach(dev);
287         if (error) {
288                 bt_isa_release_resources(dev);
289                 return (error);
290         }
291
292         return (0);
293 }
294
295 #define BIOS_MAP_SIZE (16 * 1024)
296
297 static int
298 btvlbouncefilter(void *arg, bus_addr_t addr)
299 {
300         struct bt_softc *bt;
301
302         bt = (struct bt_softc *)arg;
303
304         addr &= BUS_SPACE_MAXADDR_24BIT;
305
306         if (addr == 0
307          || (addr >= bt->bios_addr
308           && addr < (bt->bios_addr + BIOS_MAP_SIZE)))
309                 return (1);
310         return (0);
311 }
312
313 static void
314 btmapsensebuffers(void *arg, bus_dma_segment_t *segs, int nseg, int error)
315 {
316         struct bt_softc* bt;
317
318         bt = (struct bt_softc*)arg;
319         bt->sense_buffers_physbase = segs->ds_addr;
320 }
321
322 static device_method_t bt_isa_methods[] = {
323         /* Device interface */
324         DEVMETHOD(device_probe,         bt_isa_probe),
325         DEVMETHOD(device_attach,        bt_isa_attach),
326
327         { 0, 0 }
328 };
329
330 static driver_t bt_isa_driver = {
331         "bt",
332         bt_isa_methods,
333         sizeof(struct bt_softc),
334 };
335
336 static devclass_t bt_devclass;
337
338 DRIVER_MODULE(bt, isa, bt_isa_driver, bt_devclass, 0, 0);