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 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: releng/5.2/sys/dev/buslogic/bt_mca.c 119418 2003-08-24 17:55:58Z obrien $");
30
31 /*
32 * Written using the bt_isa/bt_pci code as a reference.
33 *
34 * Thanks to Andy Farkas <andyf@speednet.com.au> for
35 * testing and feedback.
36 */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44
45 #include <machine/cpufunc.h>
46 #include <machine/md_var.h>
47
48 #include <sys/module.h>
49 #include <sys/bus.h>
50
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <sys/rman.h>
54
55 #include <dev/mca/mca_busreg.h>
56 #include <dev/mca/mca_busvar.h>
57
58 #include <i386/isa/isa_dma.h>
59
60 #include <dev/buslogic/btreg.h>
61
62 #include <cam/scsi/scsi_all.h>
63
64 static struct mca_ident bt_mca_devs[] = {
65 { 0x0708, "BusLogic 32 Bit Bus Master MCA-to-SCSI Host Adapter" },
66 { 0x0708, "BusTek BT-640A Micro Channel to SCSI Host Adapter" },
67 { 0x0708, "Storage Dimensions SDC3211B 32-bit SCSI Host Adapter" },
68 { 0x0709, "Storage Dimensions SDC3211F 32-bit FAST SCSI Host Adapter" },
69 { 0, NULL },
70 };
71
72 #define BT_MCA_IOPORT_POS1 MCA_ADP_POS(MCA_POS0)
73 #define BT_MCA_IOPORT_POS2 MCA_ADP_POS(MCA_POS1)
74 #define BT_MCA_IOPORT_MASK1 0x10
75 #define BT_MCA_IOPORT_MASK2 0x03
76 #define BT_MCA_IOPORT_SIZE 0x03
77 #define BT_MCA_IOPORT(pos) (0x30 + \
78 (((u_int32_t)pos &\
79 BT_MCA_IOPORT_MASK2) << 8) + \
80 (((u_int32_t)pos &\
81 BT_MCA_IOPORT_MASK1) >> 2))
82
83 #define BT_MCA_IRQ_POS MCA_ADP_POS(MCA_POS0)
84 #define BT_MCA_IRQ_MASK 0x0e
85 #define BT_MCA_IRQ(pos) (((pos & BT_MCA_IRQ_MASK) >> 1) + 8)
86
87 #define BT_MCA_DRQ_POS MCA_ADP_POS(MCA_POS3)
88 #define BT_MCA_DRQ_MASK 0x0f
89 #define BT_MCA_DRQ(pos) (pos & BT_MCA_DRQ_MASK)
90
91 #define BT_MCA_SCSIID_POS MCA_ADP_POS(MCA_POS2)
92 #define BT_MCA_SCSIID_MASK 0xe0
93 #define BT_MCA_SCSIID(pos) ((pos & BT_MCA_SCSIID_MASK) >> 5)
94
95 static bus_dma_filter_t btvlbouncefilter;
96 static bus_dmamap_callback_t btmapsensebuffers;
97
98 static void
99 bt_mca_release_resources (device_t dev)
100 {
101 struct bt_softc * bt = device_get_softc(dev);
102
103 if (bt->port)
104 bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
105 if (bt->irq)
106 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
107 if (bt->drq)
108 bus_release_resource(dev, SYS_RES_DRQ, 0, bt->drq);
109
110 bt_free_softc(dev);
111 }
112
113 #define BT_MCA_PROBE 0
114 #define BT_MCA_ATTACH 1
115
116 static int
117 bt_mca_alloc_resources(device_t dev, int mode)
118 {
119 struct resource * io = NULL;
120 struct resource * irq = NULL;
121 struct resource * drq = NULL;
122 int rid;
123
124 rid = 0;
125 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
126 0, ~0, 1, RF_ACTIVE);
127 if (io == NULL) {
128 printf("bt_mca_alloc_resources() failed to allocate IOPORT\n");
129 return (ENOMEM);
130 }
131
132 if (mode == BT_MCA_ATTACH) {
133
134 rid = 0;
135 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
136 0, ~0, 1, RF_ACTIVE);
137 if (irq == NULL) {
138 printf("bt_mca_alloc_resources() failed to allocate IRQ\n");
139 goto bad;
140 }
141
142 rid = 0;
143 drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
144 0, ~0, 1, RF_ACTIVE);
145 if (drq == NULL) {
146 printf("bt_mca_alloc_resources() failed to allocate DRQ\n");
147 goto bad;
148 }
149 }
150
151 bt_init_softc(dev, io, irq, drq);
152
153 return (0);
154 bad:
155 bt_mca_release_resources(dev);
156 return (ENOMEM);
157 }
158
159 static int
160 bt_mca_probe (device_t dev)
161 {
162 const char * desc;
163 mca_id_t id = mca_get_id(dev);
164 struct bt_probe_info info;
165 u_int32_t iobase = 0;
166 u_int32_t iosize = 0;
167 u_int8_t drq = 0;
168 u_int8_t irq = 0;
169 u_int8_t pos;
170 int result;
171
172 desc = mca_match_id(id, bt_mca_devs);
173 if (!desc)
174 return (ENXIO);
175 device_set_desc(dev, desc);
176
177 pos = (mca_pos_read(dev, BT_MCA_IOPORT_POS1) & BT_MCA_IOPORT_MASK1) |
178 (mca_pos_read(dev, BT_MCA_IOPORT_POS2) & BT_MCA_IOPORT_MASK2);
179 iobase = BT_MCA_IOPORT(pos);
180 iosize = BT_MCA_IOPORT_SIZE;
181
182 pos = mca_pos_read(dev, BT_MCA_DRQ_POS);
183 drq = BT_MCA_DRQ(pos);
184
185 pos = mca_pos_read(dev, BT_MCA_IRQ_POS);
186 irq = BT_MCA_IRQ(pos);
187
188 bt_mark_probed_iop(iobase);
189
190 mca_add_iospace(dev, iobase, iosize);
191
192 /* And allocate them */
193 bt_mca_alloc_resources(dev, BT_MCA_PROBE);
194
195 if (bt_port_probe(dev, &info) != 0) {
196 printf("bt_mca_probe: Probe failed for "
197 "card at slot %d\n", mca_get_slot(dev) + 1);
198 result = ENXIO;
199 } else {
200 mca_add_drq(dev, drq);
201 mca_add_irq(dev, irq);
202 result = 0;
203 }
204 bt_mca_release_resources(dev);
205
206 return (result);
207 }
208
209 static int
210 bt_mca_attach (device_t dev)
211 {
212 struct bt_softc * bt = device_get_softc(dev);
213 int error = 0;
214
215 /* Allocate resources */
216 if ((error = bt_mca_alloc_resources(dev, BT_MCA_ATTACH))) {
217 device_printf(dev, "Unable to allocate resources in bt_mca_attach()\n");
218 return (error);
219 }
220
221 isa_dmacascade(rman_get_start(bt->drq));
222
223 /* Allocate a dmatag for our CCB DMA maps */
224 if (bus_dma_tag_create( /* parent */ NULL,
225 /* alignemnt */ 1,
226 /* boundary */ 0,
227 /* lowaddr */ BUS_SPACE_MAXADDR_24BIT,
228 /* highaddr */ BUS_SPACE_MAXADDR,
229 /* filter */ btvlbouncefilter,
230 /* filterarg */ bt,
231 /* maxsize */ BUS_SPACE_MAXSIZE_32BIT,
232 /* nsegments */ ~0,
233 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
234 /* flags */ 0,
235 /* lockfunc */ busdma_lock_mutex,
236 /* lockarg */ &Giant,
237 &bt->parent_dmat) != 0) {
238 bt_mca_release_resources(dev);
239 return (ENOMEM);
240 }
241
242 if (bt_init(dev)) {
243 bt_mca_release_resources(dev);
244 return (ENOMEM);
245 }
246
247 /* DMA tag for our sense buffers */
248 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
249 /* alignment */ 1,
250 /* boundary */ 0,
251 /* lowaddr */ BUS_SPACE_MAXADDR,
252 /* highaddr */ BUS_SPACE_MAXADDR,
253 /* filter */ NULL,
254 /* filterarg */ NULL,
255 /* maxsize */ bt->max_ccbs *
256 sizeof(struct scsi_sense_data),
257 /* nsegments */ 1,
258 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
259 /* flags */ 0,
260 /* lockfunc */ busdma_lock_mutex,
261 /* lockarg */ &Giant,
262 &bt->sense_dmat) != 0) {
263 bt_mca_release_resources(dev);
264 return (ENOMEM);
265 }
266
267 bt->init_level++;
268
269 /* Allocation of sense buffers */
270 if (bus_dmamem_alloc(bt->sense_dmat,
271 (void **)&bt->sense_buffers,
272 BUS_DMA_NOWAIT, &bt->sense_dmamap) != 0) {
273 bt_mca_release_resources(dev);
274 return (ENOMEM);
275 }
276
277 bt->init_level++;
278
279 /* And permanently map them */
280 bus_dmamap_load(bt->sense_dmat, bt->sense_dmamap,
281 bt->sense_buffers,
282 bt->max_ccbs * sizeof(*bt->sense_buffers),
283 btmapsensebuffers, bt, /*flags*/0);
284
285 bt->init_level++;
286
287 if ((error = bt_attach(dev))) {
288 bt_mca_release_resources(dev);
289 return (error);
290 }
291
292 return (0);
293 }
294
295 /*
296 * This code should be shared with the ISA
297 * stubs as its exactly the same.
298 */
299
300 #define BIOS_MAP_SIZE (16 * 1024)
301
302 static int
303 btvlbouncefilter(void *arg, bus_addr_t addr)
304 {
305 struct bt_softc *bt;
306
307 bt = (struct bt_softc *)arg;
308
309 addr &= BUS_SPACE_MAXADDR_24BIT;
310
311 if (addr == 0
312 || (addr >= bt->bios_addr
313 && addr < (bt->bios_addr + BIOS_MAP_SIZE)))
314 return (1);
315 return (0);
316 }
317
318 static void
319 btmapsensebuffers(void *arg, bus_dma_segment_t *segs, int nseg, int error)
320 {
321 struct bt_softc* bt;
322
323 bt = (struct bt_softc*)arg;
324 bt->sense_buffers_physbase = segs->ds_addr;
325 }
326
327 static device_method_t bt_mca_methods[] = {
328 /* Device interface */
329 DEVMETHOD(device_probe, bt_mca_probe),
330 DEVMETHOD(device_attach, bt_mca_attach),
331
332 { 0, 0 }
333 };
334
335 static driver_t bt_mca_driver = {
336 "bt",
337 bt_mca_methods,
338 sizeof(struct bt_softc),
339 };
340
341 static devclass_t bt_devclass;
342
343 DRIVER_MODULE(bt, mca, bt_mca_driver, bt_devclass, 0, 0);
Cache object: 02199e82dba1b05b027f4558dd27fe50
|