Really use M_WAITOK.
[dragonfly.git] / sys / dev / disk / buslogic / bt_mca.c
1 /*-
2  * Copyright (c) 1999 Matthew N. Dodd <winter@jurai.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/buslogic/bt_mca.c,v 1.4 1999/10/09 04:02:02 mdodd Exp $
27  * $DragonFly: src/sys/dev/disk/buslogic/Attic/bt_mca.c,v 1.3 2003/08/07 21:16:52 dillon Exp $
28  */
29
30 /*
31  * Written using the bt_isa/bt_pci code as a reference.
32  *
33  * Thanks to Andy Farkas <andyf@speednet.com.au> for
34  * testing and feedback.
35  */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41
42 #include <machine/cpufunc.h>
43 #include <machine/md_var.h>
44
45 #include <sys/module.h>
46 #include <sys/bus.h>
47
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/rman.h>
51
52 #include <bus/mca/mca_busreg.h>
53 #include <bus/mca/mca_busvar.h>
54
55 #include <bus/isa/i386/isa_dma.h>
56
57 #include "btreg.h"
58
59 #include <bus/cam/scsi/scsi_all.h>
60
61 static struct mca_ident bt_mca_devs[] = {
62         { 0x0708, "BusLogic 32 Bit Bus Master MCA-to-SCSI Host Adapter" },
63         { 0x0708, "BusTek BT-640A Micro Channel to SCSI Host Adapter" },
64         { 0x0708, "Storage Dimensions SDC3211B 32-bit SCSI Host Adapter" },
65         { 0x0709, "Storage Dimensions SDC3211F 32-bit FAST SCSI Host Adapter" },
66         { 0, NULL },
67 };
68
69 #define BT_MCA_IOPORT_POS1              MCA_ADP_POS(MCA_POS0)
70 #define BT_MCA_IOPORT_POS2              MCA_ADP_POS(MCA_POS1)
71 #define BT_MCA_IOPORT_MASK1             0x10
72 #define BT_MCA_IOPORT_MASK2             0x03
73 #define BT_MCA_IOPORT_SIZE              0x03
74 #define BT_MCA_IOPORT(pos)              (0x30 + \
75                                         (((u_int32_t)pos &\
76                                                 BT_MCA_IOPORT_MASK2) << 8) + \
77                                         (((u_int32_t)pos &\
78                                                 BT_MCA_IOPORT_MASK1) >> 2))
79
80 #define BT_MCA_IRQ_POS                  MCA_ADP_POS(MCA_POS0)
81 #define BT_MCA_IRQ_MASK                 0x0e
82 #define BT_MCA_IRQ(pos)                 (((pos & BT_MCA_IRQ_MASK) >> 1) + 8)
83
84 #define BT_MCA_DRQ_POS                  MCA_ADP_POS(MCA_POS3)
85 #define BT_MCA_DRQ_MASK                 0x0f
86 #define BT_MCA_DRQ(pos)                 (pos & BT_MCA_DRQ_MASK)
87
88 #define BT_MCA_SCSIID_POS               MCA_ADP_POS(MCA_POS2)
89 #define BT_MCA_SCSIID_MASK              0xe0
90 #define BT_MCA_SCSIID(pos)              ((pos & BT_MCA_SCSIID_MASK) >> 5)
91
92 static  bus_dma_filter_t        btvlbouncefilter;
93 static  bus_dmamap_callback_t   btmapsensebuffers;
94
95 static void
96 bt_mca_release_resources (device_t dev)
97 {
98         struct  bt_softc *      bt = device_get_softc(dev);
99
100         if (bt->port)
101                 bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
102         if (bt->irq)
103                 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
104         if (bt->drq)
105                 bus_release_resource(dev, SYS_RES_DRQ, 0, bt->drq);
106
107         bt_free_softc(dev);
108 }
109
110 #define BT_MCA_PROBE    0
111 #define BT_MCA_ATTACH   1
112
113 static int
114 bt_mca_alloc_resources(device_t dev, int mode)
115 {
116         struct resource *       io = NULL;
117         struct resource *       irq = NULL;
118         struct resource *       drq = NULL;
119         int                     rid;
120
121         rid = 0;
122         io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
123                                 0, ~0, 1, RF_ACTIVE);
124         if (io == NULL) {
125                 printf("bt_mca_alloc_resources() failed to allocate IOPORT\n");
126                 return (ENOMEM);
127         }
128
129         if (mode == BT_MCA_ATTACH) {
130
131                 rid = 0;
132                 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
133                                          0, ~0, 1, RF_ACTIVE);
134                 if (irq == NULL) {
135                         printf("bt_mca_alloc_resources() failed to allocate IRQ\n");
136                         goto bad;
137                 }
138         
139                 rid = 0;
140                 drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
141                                                  0, ~0, 1, RF_ACTIVE);
142                 if (drq == NULL) {
143                         printf("bt_mca_alloc_resources() failed to allocate DRQ\n");
144                         goto bad;
145                 }
146         }
147         
148         bt_init_softc(dev, io, irq, drq);
149
150         return (0);
151 bad:
152         bt_mca_release_resources(dev);
153         return (ENOMEM);
154 }
155
156 static int
157 bt_mca_probe (device_t dev)
158 {
159         const char *            desc;
160         mca_id_t                id = mca_get_id(dev);
161         struct bt_probe_info    info;
162         u_int32_t               iobase = 0;
163         u_int32_t               iosize = 0;
164         u_int8_t                drq = 0;
165         u_int8_t                irq = 0;
166         u_int8_t                pos;
167         int                     result;
168
169         desc = mca_match_id(id, bt_mca_devs);
170         if (!desc)
171                 return (ENXIO);
172         device_set_desc(dev, desc);
173
174         pos = (mca_pos_read(dev, BT_MCA_IOPORT_POS1) & BT_MCA_IOPORT_MASK1) |
175               (mca_pos_read(dev, BT_MCA_IOPORT_POS2) & BT_MCA_IOPORT_MASK2);
176         iobase = BT_MCA_IOPORT(pos);
177         iosize = BT_MCA_IOPORT_SIZE;
178
179         pos = mca_pos_read(dev, BT_MCA_DRQ_POS);
180         drq = BT_MCA_DRQ(pos);
181
182         pos = mca_pos_read(dev, BT_MCA_IRQ_POS);
183         irq = BT_MCA_IRQ(pos);
184
185         bt_mark_probed_iop(iobase);
186
187         mca_add_iospace(dev, iobase, iosize);
188
189         /* And allocate them */
190         bt_mca_alloc_resources(dev, BT_MCA_PROBE);
191                         
192         if (bt_port_probe(dev, &info) != 0) {
193                 printf("bt_mca_probe: Probe failed for "
194                        "card at slot %d\n", mca_get_slot(dev) + 1);
195                 result = ENXIO;
196         } else {               
197                 mca_add_drq(dev, drq);
198                 mca_add_irq(dev, irq);
199                 result = 0;    
200         }              
201         bt_mca_release_resources(dev);
202
203         return (result);
204 }
205
206 static int
207 bt_mca_attach (device_t dev)
208 {
209         struct bt_softc *       bt = device_get_softc(dev);
210         int                     error = 0;
211
212         /* Allocate resources */      
213         if ((error = bt_mca_alloc_resources(dev, BT_MCA_ATTACH))) {
214                 device_printf(dev, "Unable to allocate resources in bt_mca_attach()\n");
215                 return (error);
216         }
217
218         isa_dmacascade(rman_get_start(bt->drq));
219
220         /* Allocate a dmatag for our CCB DMA maps */
221         if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/1, /*boundary*/0,
222                                /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
223                                /*highaddr*/BUS_SPACE_MAXADDR,
224                                /*filter*/btvlbouncefilter,
225                                /*filterarg*/bt,
226                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
227                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
228                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
229                                /*flags*/0, &bt->parent_dmat) != 0) {
230                 bt_mca_release_resources(dev);
231                 return (ENOMEM);
232         }
233
234         if (bt_init(dev)) {
235                 bt_mca_release_resources(dev);
236                 return (ENOMEM);
237         }
238
239         /* DMA tag for our sense buffers */
240         if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, 
241                                /*boundary*/0,
242                                /*lowaddr*/BUS_SPACE_MAXADDR,    
243                                /*highaddr*/BUS_SPACE_MAXADDR,   
244                                /*filter*/NULL, /*filterarg*/NULL,
245                                bt->max_ccbs * sizeof(struct scsi_sense_data),
246                                /*nsegments*/1,
247                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
248                                /*flags*/0, &bt->sense_dmat) != 0) {
249                 bt_mca_release_resources(dev);
250                 return (ENOMEM);
251         }
252
253         bt->init_level++;     
254
255         /* Allocation of sense buffers */
256         if (bus_dmamem_alloc(bt->sense_dmat,
257                              (void **)&bt->sense_buffers,       
258                              BUS_DMA_NOWAIT, &bt->sense_dmamap) != 0) {
259                 bt_mca_release_resources(dev);
260                 return (ENOMEM);
261         }
262
263         bt->init_level++;     
264
265         /* And permanently map them */
266         bus_dmamap_load(bt->sense_dmat, bt->sense_dmamap,       
267                         bt->sense_buffers,
268                         bt->max_ccbs * sizeof(*bt->sense_buffers),
269                         btmapsensebuffers, bt, /*flags*/0);     
270
271         bt->init_level++;     
272
273         if ((error = bt_attach(dev))) {
274                 bt_mca_release_resources(dev);
275                 return (error);
276         }
277
278         return (0);
279 }
280
281 /*
282  * This code should be shared with the ISA
283  * stubs as its exactly the same.
284  */
285
286 #define BIOS_MAP_SIZE (16 * 1024)
287
288 static int
289 btvlbouncefilter(void *arg, bus_addr_t addr)
290 {
291         struct bt_softc *bt;
292
293         bt = (struct bt_softc *)arg;
294
295         addr &= BUS_SPACE_MAXADDR_24BIT;
296
297         if (addr == 0
298          || (addr >= bt->bios_addr
299           && addr < (bt->bios_addr + BIOS_MAP_SIZE)))
300                 return (1);
301         return (0);
302 }
303
304 static void
305 btmapsensebuffers(void *arg, bus_dma_segment_t *segs, int nseg, int error)
306 {
307         struct bt_softc* bt;
308
309         bt = (struct bt_softc*)arg;
310         bt->sense_buffers_physbase = segs->ds_addr;
311 }
312
313 static device_method_t bt_mca_methods[] = {
314         /* Device interface */
315         DEVMETHOD(device_probe,         bt_mca_probe),
316         DEVMETHOD(device_attach,        bt_mca_attach),
317
318         { 0, 0 }
319 };
320
321 static driver_t bt_mca_driver = {
322         "bt",
323         bt_mca_methods,
324         sizeof(struct bt_softc),
325 };
326
327 static devclass_t bt_devclass;
328
329 DRIVER_MODULE(bt, mca, bt_mca_driver, bt_devclass, 0, 0);