2 * Copyright (c) 2002-2003
3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/bus/firewire/fwmem.c,v 1.9 2006/04/30 17:22:15 dillon Exp $
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/sys/dev/firewire/fwmem.c,v 1.26 2004/01/05 14:21:18 simokawa Exp $");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
50 #if defined(__DragonFly__) || __FreeBSD_version < 500000
57 #include <machine/bus.h>
59 #include <sys/signal.h>
61 #include <sys/ioccom.h>
62 #include <sys/fcntl.h>
63 #include <sys/thread2.h>
67 #include "firewirereg.h"
70 #include <dev/firewire/firewire.h>
71 #include <dev/firewire/firewirereg.h>
72 #include <dev/firewire/fwmem.h>
75 static int fwmem_speed=2, fwmem_debug=0;
76 static struct fw_eui64 fwmem_eui64;
77 SYSCTL_DECL(_hw_firewire);
78 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwmem, CTLFLAG_RD, 0,
79 "FireWire Memory Access");
80 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW,
81 &fwmem_eui64.hi, 0, "Fwmem target EUI64 high");
82 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW,
83 &fwmem_eui64.lo, 0, "Fwmem target EUI64 low");
84 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0,
86 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0,
87 "Fwmem driver debug flag");
89 MALLOC_DEFINE(M_FWMEM, "fwmem", "fwmem/FireWire");
91 #define MAXLEN (512 << fwmem_speed)
98 static struct fw_xfer *
100 struct fw_device *fwdev,
107 struct fw_xfer *xfer;
109 xfer = fw_xfer_alloc(M_FWMEM);
113 xfer->fc = fwdev->fc;
114 xfer->send.hdr.mode.hdr.dst = FWLOCALBUS | fwdev->dst;
116 xfer->send.spd = fwdev->speed;
118 xfer->send.spd = min(spd, fwdev->speed);
119 xfer->act.hand = hand;
120 xfer->retry_req = fw_asybusy;
122 xfer->send.pay_len = slen;
123 xfer->recv.pay_len = rlen;
130 struct fw_device *fwdev,
136 void (*hand)(struct fw_xfer *))
138 struct fw_xfer *xfer;
141 xfer = fwmem_xfer_req(fwdev, (void *)sc, spd, 0, 4, hand);
146 fp = &xfer->send.hdr;
147 fp->mode.rreqq.tcode = FWTCODE_RREQQ;
148 fp->mode.rreqq.dest_hi = dst_hi;
149 fp->mode.rreqq.dest_lo = dst_lo;
151 xfer->send.payload = NULL;
152 xfer->recv.payload = (u_int32_t *)data;
155 printf("fwmem_read_quad: %d %04x:%08x\n", fwdev->dst,
158 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
167 struct fw_device *fwdev,
173 void (*hand)(struct fw_xfer *))
175 struct fw_xfer *xfer;
178 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 0, hand);
182 fp = &xfer->send.hdr;
183 fp->mode.wreqq.tcode = FWTCODE_WREQQ;
184 fp->mode.wreqq.dest_hi = dst_hi;
185 fp->mode.wreqq.dest_lo = dst_lo;
186 fp->mode.wreqq.data = *(u_int32_t *)data;
188 xfer->send.payload = xfer->recv.payload = NULL;
191 printf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst,
192 dst_hi, dst_lo, *(u_int32_t *)data);
194 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
203 struct fw_device *fwdev,
210 void (*hand)(struct fw_xfer *))
212 struct fw_xfer *xfer;
215 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, roundup2(len, 4), hand);
219 fp = &xfer->send.hdr;
220 fp->mode.rreqb.tcode = FWTCODE_RREQB;
221 fp->mode.rreqb.dest_hi = dst_hi;
222 fp->mode.rreqb.dest_lo = dst_lo;
223 fp->mode.rreqb.len = len;
224 fp->mode.rreqb.extcode = 0;
226 xfer->send.payload = NULL;
227 xfer->recv.payload = data;
230 printf("fwmem_read_block: %d %04x:%08x %d\n", fwdev->dst,
231 dst_hi, dst_lo, len);
232 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
241 struct fw_device *fwdev,
248 void (*hand)(struct fw_xfer *))
250 struct fw_xfer *xfer;
253 xfer = fwmem_xfer_req(fwdev, sc, spd, len, 0, hand);
257 fp = &xfer->send.hdr;
258 fp->mode.wreqb.tcode = FWTCODE_WREQB;
259 fp->mode.wreqb.dest_hi = dst_hi;
260 fp->mode.wreqb.dest_lo = dst_lo;
261 fp->mode.wreqb.len = len;
262 fp->mode.wreqb.extcode = 0;
264 xfer->send.payload = data;
265 xfer->recv.payload = NULL;
268 printf("fwmem_write_block: %d %04x:%08x %d\n", fwdev->dst,
269 dst_hi, dst_lo, len);
270 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
279 fwmem_open (dev_t dev, int flags, int fmt, fw_proc *td)
281 struct fwmem_softc *fms;
283 if (dev->si_drv1 != NULL) {
284 if ((flags & FWRITE) != 0)
286 fms = (struct fwmem_softc *)dev->si_drv1;
289 fms = (struct fwmem_softc *)malloc(sizeof(struct fwmem_softc),
293 bcopy(&fwmem_eui64, &fms->eui, sizeof(struct fw_eui64));
294 dev->si_drv1 = (void *)fms;
295 dev->si_iosize_max = DFLTPHYS;
299 printf("%s: refcount=%d\n", __func__, fms->refcount);
305 fwmem_close (dev_t dev, int flags, int fmt, fw_proc *td)
307 struct fwmem_softc *fms;
309 fms = (struct fwmem_softc *)dev->si_drv1;
312 printf("%s: refcount=%d\n", __func__, fms->refcount);
313 if (fms->refcount < 1) {
314 free(dev->si_drv1, M_FW);
323 fwmem_biodone(struct fw_xfer *xfer)
328 bio = (struct bio *)xfer->sc;
330 bp->b_error = xfer->resp;
332 if (bp->b_error != 0) {
334 printf("%s: err=%d\n", __func__, bp->b_error);
335 bp->b_flags |= B_ERROR;
336 bp->b_resid = bp->b_bcount;
343 fwmem_strategy(dev_t dev, struct bio *bio)
345 struct buf *bp = bio->bio_buf;
346 struct firewire_softc *sc;
347 struct fwmem_softc *fms;
348 struct fw_device *fwdev;
349 struct fw_xfer *xfer;
350 int unit, err=0, iolen;
352 /* XXX check request length */
354 unit = DEV2UNIT(dev);
355 sc = devclass_get_softc(firewire_devclass, unit);
358 fms = (struct fwmem_softc *)dev->si_drv1;
359 fwdev = fw_noderesolve_eui64(sc->fc, &fms->eui);
362 printf("fwmem: no such device ID:%08x%08x\n",
363 fms->eui.hi, fms->eui.lo);
367 if (bio->bio_offset == NOOFFSET) {
368 printf("fwmem: offset was not set bp %p\n", bp);
373 iolen = MIN(bp->b_bcount, MAXLEN);
374 if (bp->b_cmd == BUF_CMD_READ) {
375 if (iolen == 4 && (bio->bio_offset & 3) == 0)
376 xfer = fwmem_read_quad(fwdev,
377 (void *) bio, fwmem_speed,
378 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff,
379 bp->b_data, fwmem_biodone);
381 xfer = fwmem_read_block(fwdev,
382 (void *) bio, fwmem_speed,
383 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff,
384 iolen, bp->b_data, fwmem_biodone);
386 if (iolen == 4 && (bio->bio_offset & 3) == 0)
387 xfer = fwmem_write_quad(fwdev,
388 (void *)bio, fwmem_speed,
389 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff,
390 bp->b_data, fwmem_biodone);
392 xfer = fwmem_write_block(fwdev,
393 (void *)bio, fwmem_speed,
394 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff,
395 iolen, bp->b_data, fwmem_biodone);
402 bp->b_resid = bp->b_bcount - iolen;
407 printf("%s: err=%d\n", __func__, err);
409 bp->b_flags |= B_ERROR;
410 bp->b_resid = bp->b_bcount;
416 fwmem_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
418 struct fwmem_softc *fms;
421 fms = (struct fwmem_softc *)dev->si_drv1;
424 bcopy(data, &fms->eui, sizeof(struct fw_eui64));
427 bcopy(&fms->eui, data, sizeof(struct fw_eui64));
435 fwmem_poll (dev_t dev, int events, fw_proc *td)
440 #if defined(__DragonFly__) || __FreeBSD_version < 500102
441 fwmem_mmap (dev_t dev, vm_offset_t offset, int nproto)
443 fwmem_mmap (dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int nproto)