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, CTLFLAG_RD, 0,
69 "FireWire Memory Access");
70 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW,
71 &fwmem_eui64.hi, 0, "Fwmem target EUI64 high");
72 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW,
73 &fwmem_eui64.lo, 0, "Fwmem target EUI64 low");
74 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0,
76 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0,
77 "Fwmem driver debug flag");
79 static MALLOC_DEFINE(M_FWMEM, "fwmem", "fwmem/FireWire");
81 #define MAXLEN (512 << fwmem_speed)
85 struct firewire_softc *sc;
89 static struct fw_xfer *
91 struct fw_device *fwdev,
100 xfer = fw_xfer_alloc(M_FWMEM);
104 xfer->fc = fwdev->fc;
105 xfer->send.hdr.mode.hdr.dst = FWLOCALBUS | fwdev->dst;
107 xfer->send.spd = fwdev->speed;
109 xfer->send.spd = min(spd, fwdev->speed);
112 xfer->send.pay_len = slen;
113 xfer->recv.pay_len = rlen;
120 struct fw_device *fwdev,
126 void (*hand)(struct fw_xfer *))
128 struct fw_xfer *xfer;
131 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 4, hand);
136 fp = &xfer->send.hdr;
137 fp->mode.rreqq.tcode = FWTCODE_RREQQ;
138 fp->mode.rreqq.dest_hi = dst_hi;
139 fp->mode.rreqq.dest_lo = dst_lo;
141 xfer->send.payload = NULL;
142 xfer->recv.payload = (uint32_t *)data;
145 printf("fwmem_read_quad: %d %04x:%08x\n", fwdev->dst,
148 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
157 struct fw_device *fwdev,
163 void (*hand)(struct fw_xfer *))
165 struct fw_xfer *xfer;
168 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 0, hand);
172 fp = &xfer->send.hdr;
173 fp->mode.wreqq.tcode = FWTCODE_WREQQ;
174 fp->mode.wreqq.dest_hi = dst_hi;
175 fp->mode.wreqq.dest_lo = dst_lo;
176 fp->mode.wreqq.data = *(uint32_t *)data;
178 xfer->send.payload = xfer->recv.payload = NULL;
181 printf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst,
182 dst_hi, dst_lo, *(uint32_t *)data);
184 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
193 struct fw_device *fwdev,
200 void (*hand)(struct fw_xfer *))
202 struct fw_xfer *xfer;
205 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, roundup2(len, 4), hand);
209 fp = &xfer->send.hdr;
210 fp->mode.rreqb.tcode = FWTCODE_RREQB;
211 fp->mode.rreqb.dest_hi = dst_hi;
212 fp->mode.rreqb.dest_lo = dst_lo;
213 fp->mode.rreqb.len = len;
214 fp->mode.rreqb.extcode = 0;
216 xfer->send.payload = NULL;
217 xfer->recv.payload = data;
220 printf("fwmem_read_block: %d %04x:%08x %d\n", fwdev->dst,
221 dst_hi, dst_lo, len);
222 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
231 struct fw_device *fwdev,
238 void (*hand)(struct fw_xfer *))
240 struct fw_xfer *xfer;
243 xfer = fwmem_xfer_req(fwdev, sc, spd, len, 0, hand);
247 fp = &xfer->send.hdr;
248 fp->mode.wreqb.tcode = FWTCODE_WREQB;
249 fp->mode.wreqb.dest_hi = dst_hi;
250 fp->mode.wreqb.dest_lo = dst_lo;
251 fp->mode.wreqb.len = len;
252 fp->mode.wreqb.extcode = 0;
254 xfer->send.payload = data;
255 xfer->recv.payload = NULL;
258 printf("fwmem_write_block: %d %04x:%08x %d\n", fwdev->dst,
259 dst_hi, dst_lo, len);
260 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
268 fwmem_open(struct cdev *dev, int flags, int fmt, fw_proc *td)
270 struct fwmem_softc *fms;
271 struct firewire_softc *sc;
272 int unit = DEV2UNIT(dev);
274 sc = devclass_get_softc(firewire_devclass, unit);
279 if (dev->si_drv1 != NULL) {
280 if ((flags & FWRITE) != 0) {
288 dev->si_drv1 = (void *)-1;
290 dev->si_drv1 = malloc(sizeof(struct fwmem_softc),
292 dev->si_iosize_max = DFLTPHYS;
294 bcopy(&fwmem_eui64, &fms->eui, sizeof(struct fw_eui64));
299 printf("%s: refcount=%d\n", __func__, fms->refcount);
305 fwmem_close (struct cdev *dev, int flags, int fmt, fw_proc *td)
307 struct fwmem_softc *fms;
311 FW_GLOCK(fms->sc->fc);
313 FW_GUNLOCK(fms->sc->fc);
315 printf("%s: refcount=%d\n", __func__, fms->refcount);
316 if (fms->refcount < 1) {
317 free(dev->si_drv1, M_FWMEM);
326 fwmem_biodone(struct fw_xfer *xfer)
330 bp = (struct bio *)xfer->sc;
331 bp->bio_error = xfer->resp;
333 if (bp->bio_error != 0) {
335 printf("%s: err=%d\n", __func__, bp->bio_error);
336 bp->bio_flags |= BIO_ERROR;
337 bp->bio_resid = bp->bio_bcount;
345 fwmem_strategy(struct bio *bp)
347 struct fwmem_softc *fms;
348 struct fw_device *fwdev;
349 struct fw_xfer *xfer;
354 /* XXX check request length */
357 fwdev = fw_noderesolve_eui64(fms->sc->fc, &fms->eui);
360 printf("fwmem: no such device ID:%08x%08x\n",
361 fms->eui.hi, fms->eui.lo);
366 iolen = MIN(bp->bio_bcount, MAXLEN);
367 if (bp->bio_cmd == BIO_READ) {
368 if (iolen == 4 && (bp->bio_offset & 3) == 0)
369 xfer = fwmem_read_quad(fwdev,
370 (void *)bp, fwmem_speed,
371 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
372 bp->bio_data, fwmem_biodone);
374 xfer = fwmem_read_block(fwdev,
375 (void *)bp, fwmem_speed,
376 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
377 iolen, bp->bio_data, fwmem_biodone);
379 if (iolen == 4 && (bp->bio_offset & 3) == 0)
380 xfer = fwmem_write_quad(fwdev,
381 (void *)bp, fwmem_speed,
382 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
383 bp->bio_data, fwmem_biodone);
385 xfer = fwmem_write_block(fwdev,
386 (void *)bp, fwmem_speed,
387 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
388 iolen, bp->bio_data, fwmem_biodone);
395 bp->bio_resid = bp->bio_bcount - iolen;
399 printf("%s: err=%d\n", __func__, err);
401 bp->bio_flags |= BIO_ERROR;
402 bp->bio_resid = bp->bio_bcount;
408 fwmem_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
410 struct fwmem_softc *fms;
416 bcopy(data, &fms->eui, sizeof(struct fw_eui64));
419 bcopy(&fms->eui, data, sizeof(struct fw_eui64));
428 fwmem_poll(struct cdev *dev, int events, fw_proc *td)
434 fwmem_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
435 int nproto, vm_memattr_t *memattr)