2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2002-2003
5 * Hidetoshi Shimokawa. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
18 * This product includes software developed by Hidetoshi Shimokawa.
20 * 4. Neither the name of the author nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/sysctl.h>
54 #include <machine/bus.h>
56 #include <sys/signal.h>
58 #include <sys/ioccom.h>
59 #include <sys/fcntl.h>
61 #include <dev/firewire/firewire.h>
62 #include <dev/firewire/firewirereg.h>
63 #include <dev/firewire/fwmem.h>
65 static int fwmem_speed = 2, fwmem_debug = 0;
66 static struct fw_eui64 fwmem_eui64;
67 SYSCTL_DECL(_hw_firewire);
68 static SYSCTL_NODE(_hw_firewire, OID_AUTO, fwmem,
69 CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
70 "FireWire Memory Access");
71 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW,
72 &fwmem_eui64.hi, 0, "Fwmem target EUI64 high");
73 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW,
74 &fwmem_eui64.lo, 0, "Fwmem target EUI64 low");
75 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0,
77 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0,
78 "Fwmem driver debug flag");
80 static MALLOC_DEFINE(M_FWMEM, "fwmem", "fwmem/FireWire");
82 #define MAXLEN (512 << fwmem_speed)
86 struct firewire_softc *sc;
90 static struct fw_xfer *
92 struct fw_device *fwdev,
101 xfer = fw_xfer_alloc(M_FWMEM);
105 xfer->fc = fwdev->fc;
106 xfer->send.hdr.mode.hdr.dst = FWLOCALBUS | fwdev->dst;
108 xfer->send.spd = fwdev->speed;
110 xfer->send.spd = min(spd, fwdev->speed);
113 xfer->send.pay_len = slen;
114 xfer->recv.pay_len = rlen;
121 struct fw_device *fwdev,
127 void (*hand)(struct fw_xfer *))
129 struct fw_xfer *xfer;
132 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 4, hand);
137 fp = &xfer->send.hdr;
138 fp->mode.rreqq.tcode = FWTCODE_RREQQ;
139 fp->mode.rreqq.dest_hi = dst_hi;
140 fp->mode.rreqq.dest_lo = dst_lo;
142 xfer->send.payload = NULL;
143 xfer->recv.payload = (uint32_t *)data;
146 printf("fwmem_read_quad: %d %04x:%08x\n", fwdev->dst,
149 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
158 struct fw_device *fwdev,
164 void (*hand)(struct fw_xfer *))
166 struct fw_xfer *xfer;
169 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 0, hand);
173 fp = &xfer->send.hdr;
174 fp->mode.wreqq.tcode = FWTCODE_WREQQ;
175 fp->mode.wreqq.dest_hi = dst_hi;
176 fp->mode.wreqq.dest_lo = dst_lo;
177 fp->mode.wreqq.data = *(uint32_t *)data;
179 xfer->send.payload = xfer->recv.payload = NULL;
182 printf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst,
183 dst_hi, dst_lo, *(uint32_t *)data);
185 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
194 struct fw_device *fwdev,
201 void (*hand)(struct fw_xfer *))
203 struct fw_xfer *xfer;
206 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, roundup2(len, 4), hand);
210 fp = &xfer->send.hdr;
211 fp->mode.rreqb.tcode = FWTCODE_RREQB;
212 fp->mode.rreqb.dest_hi = dst_hi;
213 fp->mode.rreqb.dest_lo = dst_lo;
214 fp->mode.rreqb.len = len;
215 fp->mode.rreqb.extcode = 0;
217 xfer->send.payload = NULL;
218 xfer->recv.payload = data;
221 printf("fwmem_read_block: %d %04x:%08x %d\n", fwdev->dst,
222 dst_hi, dst_lo, len);
223 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
232 struct fw_device *fwdev,
239 void (*hand)(struct fw_xfer *))
241 struct fw_xfer *xfer;
244 xfer = fwmem_xfer_req(fwdev, sc, spd, len, 0, hand);
248 fp = &xfer->send.hdr;
249 fp->mode.wreqb.tcode = FWTCODE_WREQB;
250 fp->mode.wreqb.dest_hi = dst_hi;
251 fp->mode.wreqb.dest_lo = dst_lo;
252 fp->mode.wreqb.len = len;
253 fp->mode.wreqb.extcode = 0;
255 xfer->send.payload = data;
256 xfer->recv.payload = NULL;
259 printf("fwmem_write_block: %d %04x:%08x %d\n", fwdev->dst,
260 dst_hi, dst_lo, len);
261 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
269 fwmem_open(struct cdev *dev, int flags, int fmt, fw_proc *td)
271 struct fwmem_softc *fms;
272 struct firewire_softc *sc;
273 int unit = DEV2UNIT(dev);
275 sc = devclass_get_softc(firewire_devclass, unit);
280 if (dev->si_drv1 != NULL) {
281 if ((flags & FWRITE) != 0) {
289 dev->si_drv1 = (void *)-1;
291 dev->si_drv1 = malloc(sizeof(struct fwmem_softc),
293 dev->si_iosize_max = DFLTPHYS;
295 bcopy(&fwmem_eui64, &fms->eui, sizeof(struct fw_eui64));
300 printf("%s: refcount=%d\n", __func__, fms->refcount);
306 fwmem_close (struct cdev *dev, int flags, int fmt, fw_proc *td)
308 struct fwmem_softc *fms;
312 FW_GLOCK(fms->sc->fc);
314 FW_GUNLOCK(fms->sc->fc);
316 printf("%s: refcount=%d\n", __func__, fms->refcount);
317 if (fms->refcount < 1) {
318 free(dev->si_drv1, M_FWMEM);
327 fwmem_biodone(struct fw_xfer *xfer)
331 bp = (struct bio *)xfer->sc;
332 bp->bio_error = xfer->resp;
334 if (bp->bio_error != 0) {
336 printf("%s: err=%d\n", __func__, bp->bio_error);
337 bp->bio_flags |= BIO_ERROR;
338 bp->bio_resid = bp->bio_bcount;
346 fwmem_strategy(struct bio *bp)
348 struct fwmem_softc *fms;
349 struct fw_device *fwdev;
350 struct fw_xfer *xfer;
355 /* XXX check request length */
358 fwdev = fw_noderesolve_eui64(fms->sc->fc, &fms->eui);
361 printf("fwmem: no such device ID:%08x%08x\n",
362 fms->eui.hi, fms->eui.lo);
367 iolen = MIN(bp->bio_bcount, MAXLEN);
368 if (bp->bio_cmd == BIO_READ) {
369 if (iolen == 4 && (bp->bio_offset & 3) == 0)
370 xfer = fwmem_read_quad(fwdev,
371 (void *)bp, fwmem_speed,
372 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
373 bp->bio_data, fwmem_biodone);
375 xfer = fwmem_read_block(fwdev,
376 (void *)bp, fwmem_speed,
377 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
378 iolen, bp->bio_data, fwmem_biodone);
380 if (iolen == 4 && (bp->bio_offset & 3) == 0)
381 xfer = fwmem_write_quad(fwdev,
382 (void *)bp, fwmem_speed,
383 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
384 bp->bio_data, fwmem_biodone);
386 xfer = fwmem_write_block(fwdev,
387 (void *)bp, fwmem_speed,
388 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
389 iolen, bp->bio_data, fwmem_biodone);
396 bp->bio_resid = bp->bio_bcount - iolen;
400 printf("%s: err=%d\n", __func__, err);
402 bp->bio_flags |= BIO_ERROR;
403 bp->bio_resid = bp->bio_bcount;
409 fwmem_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
411 struct fwmem_softc *fms;
417 bcopy(data, &fms->eui, sizeof(struct fw_eui64));
420 bcopy(&fms->eui, data, sizeof(struct fw_eui64));
429 fwmem_poll(struct cdev *dev, int events, fw_proc *td)
435 fwmem_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
436 int nproto, vm_memattr_t *memattr)