1 /*- 2 * Copyright (c) 2002-2003 3 * Hidetoshi Shimokawa. 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * 16 * This product includes software developed by Hidetoshi Shimokawa. 17 * 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #ifdef __FreeBSD__ 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 #endif 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/types.h> 44 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/conf.h> 48 #include <sys/sysctl.h> 49 #include <sys/bio.h> 50 51 #include <sys/bus.h> 52 #include <machine/bus.h> 53 54 #include <sys/signal.h> 55 #include <sys/mman.h> 56 #include <sys/ioccom.h> 57 #include <sys/fcntl.h> 58 59 #include <dev/firewire/firewire.h> 60 #include <dev/firewire/firewirereg.h> 61 #include <dev/firewire/fwmem.h> 62 63 static int fwmem_speed=2, fwmem_debug=0; 64 static struct fw_eui64 fwmem_eui64; 65 SYSCTL_DECL(_hw_firewire); 66 static SYSCTL_NODE(_hw_firewire, OID_AUTO, fwmem, CTLFLAG_RD, 0, 67 "FireWire Memory Access"); 68 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW, 69 &fwmem_eui64.hi, 0, "Fwmem target EUI64 high"); 70 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW, 71 &fwmem_eui64.lo, 0, "Fwmem target EUI64 low"); 72 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0, 73 "Fwmem link speed"); 74 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0, 75 "Fwmem driver debug flag"); 76 77 static MALLOC_DEFINE(M_FWMEM, "fwmem", "fwmem/FireWire"); 78 79 #define MAXLEN (512 << fwmem_speed) 80 81 struct fwmem_softc { 82 struct fw_eui64 eui; 83 struct firewire_softc *sc; 84 int refcount; 85 }; 86 87 static struct fw_xfer * 88 fwmem_xfer_req( 89 struct fw_device *fwdev, 90 caddr_t sc, 91 int spd, 92 int slen, 93 int rlen, 94 void *hand) 95 { 96 struct fw_xfer *xfer; 97 98 xfer = fw_xfer_alloc(M_FWMEM); 99 if (xfer == NULL) 100 return NULL; 101 102 xfer->fc = fwdev->fc; 103 xfer->send.hdr.mode.hdr.dst = FWLOCALBUS | fwdev->dst; 104 if (spd < 0) 105 xfer->send.spd = fwdev->speed; 106 else 107 xfer->send.spd = min(spd, fwdev->speed); 108 xfer->hand = hand; 109 xfer->sc = sc; 110 xfer->send.pay_len = slen; 111 xfer->recv.pay_len = rlen; 112 113 return xfer; 114 } 115 116 struct fw_xfer * 117 fwmem_read_quad( 118 struct fw_device *fwdev, 119 caddr_t sc, 120 uint8_t spd, 121 uint16_t dst_hi, 122 uint32_t dst_lo, 123 void *data, 124 void (*hand)(struct fw_xfer *)) 125 { 126 struct fw_xfer *xfer; 127 struct fw_pkt *fp; 128 129 xfer = fwmem_xfer_req(fwdev, (void *)sc, spd, 0, 4, hand); 130 if (xfer == NULL) { 131 return NULL; 132 } 133 134 fp = &xfer->send.hdr; 135 fp->mode.rreqq.tcode = FWTCODE_RREQQ; 136 fp->mode.rreqq.dest_hi = dst_hi; 137 fp->mode.rreqq.dest_lo = dst_lo; 138 139 xfer->send.payload = NULL; 140 xfer->recv.payload = (uint32_t *)data; 141 142 if (fwmem_debug) 143 printf("fwmem_read_quad: %d %04x:%08x\n", fwdev->dst, 144 dst_hi, dst_lo); 145 146 if (fw_asyreq(xfer->fc, -1, xfer) == 0) 147 return xfer; 148 149 fw_xfer_free(xfer); 150 return NULL; 151 } 152 153 struct fw_xfer * 154 fwmem_write_quad( 155 struct fw_device *fwdev, 156 caddr_t sc, 157 uint8_t spd, 158 uint16_t dst_hi, 159 uint32_t dst_lo, 160 void *data, 161 void (*hand)(struct fw_xfer *)) 162 { 163 struct fw_xfer *xfer; 164 struct fw_pkt *fp; 165 166 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 0, hand); 167 if (xfer == NULL) 168 return NULL; 169 170 fp = &xfer->send.hdr; 171 fp->mode.wreqq.tcode = FWTCODE_WREQQ; 172 fp->mode.wreqq.dest_hi = dst_hi; 173 fp->mode.wreqq.dest_lo = dst_lo; 174 fp->mode.wreqq.data = *(uint32_t *)data; 175 176 xfer->send.payload = xfer->recv.payload = NULL; 177 178 if (fwmem_debug) 179 printf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst, 180 dst_hi, dst_lo, *(uint32_t *)data); 181 182 if (fw_asyreq(xfer->fc, -1, xfer) == 0) 183 return xfer; 184 185 fw_xfer_free(xfer); 186 return NULL; 187 } 188 189 struct fw_xfer * 190 fwmem_read_block( 191 struct fw_device *fwdev, 192 caddr_t sc, 193 uint8_t spd, 194 uint16_t dst_hi, 195 uint32_t dst_lo, 196 int len, 197 void *data, 198 void (*hand)(struct fw_xfer *)) 199 { 200 struct fw_xfer *xfer; 201 struct fw_pkt *fp; 202 203 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, roundup2(len, 4), hand); 204 if (xfer == NULL) 205 return NULL; 206 207 fp = &xfer->send.hdr; 208 fp->mode.rreqb.tcode = FWTCODE_RREQB; 209 fp->mode.rreqb.dest_hi = dst_hi; 210 fp->mode.rreqb.dest_lo = dst_lo; 211 fp->mode.rreqb.len = len; 212 fp->mode.rreqb.extcode = 0; 213 214 xfer->send.payload = NULL; 215 xfer->recv.payload = data; 216 217 if (fwmem_debug) 218 printf("fwmem_read_block: %d %04x:%08x %d\n", fwdev->dst, 219 dst_hi, dst_lo, len); 220 if (fw_asyreq(xfer->fc, -1, xfer) == 0) 221 return xfer; 222 223 fw_xfer_free(xfer); 224 return NULL; 225 } 226 227 struct fw_xfer * 228 fwmem_write_block( 229 struct fw_device *fwdev, 230 caddr_t sc, 231 uint8_t spd, 232 uint16_t dst_hi, 233 uint32_t dst_lo, 234 int len, 235 void *data, 236 void (*hand)(struct fw_xfer *)) 237 { 238 struct fw_xfer *xfer; 239 struct fw_pkt *fp; 240 241 xfer = fwmem_xfer_req(fwdev, sc, spd, len, 0, hand); 242 if (xfer == NULL) 243 return NULL; 244 245 fp = &xfer->send.hdr; 246 fp->mode.wreqb.tcode = FWTCODE_WREQB; 247 fp->mode.wreqb.dest_hi = dst_hi; 248 fp->mode.wreqb.dest_lo = dst_lo; 249 fp->mode.wreqb.len = len; 250 fp->mode.wreqb.extcode = 0; 251 252 xfer->send.payload = data; 253 xfer->recv.payload = NULL; 254 255 if (fwmem_debug) 256 printf("fwmem_write_block: %d %04x:%08x %d\n", fwdev->dst, 257 dst_hi, dst_lo, len); 258 if (fw_asyreq(xfer->fc, -1, xfer) == 0) 259 return xfer; 260 261 fw_xfer_free(xfer); 262 return NULL; 263 } 264 265 266 int 267 fwmem_open (struct cdev *dev, int flags, int fmt, fw_proc *td) 268 { 269 struct fwmem_softc *fms; 270 struct firewire_softc *sc; 271 int unit = DEV2UNIT(dev); 272 273 sc = devclass_get_softc(firewire_devclass, unit); 274 if (sc == NULL) 275 return (ENXIO); 276 277 FW_GLOCK(sc->fc); 278 if (dev->si_drv1 != NULL) { 279 if ((flags & FWRITE) != 0) { 280 FW_GUNLOCK(sc->fc); 281 return(EBUSY); 282 } 283 FW_GUNLOCK(sc->fc); 284 fms = (struct fwmem_softc *)dev->si_drv1; 285 fms->refcount ++; 286 } else { 287 dev->si_drv1 = (void *)-1; 288 FW_GUNLOCK(sc->fc); 289 dev->si_drv1 = malloc(sizeof(struct fwmem_softc), 290 M_FWMEM, M_WAITOK); 291 if (dev->si_drv1 == NULL) 292 return(ENOMEM); 293 dev->si_iosize_max = DFLTPHYS; 294 fms = (struct fwmem_softc *)dev->si_drv1; 295 bcopy(&fwmem_eui64, &fms->eui, sizeof(struct fw_eui64)); 296 fms->sc = sc; 297 fms->refcount = 1; 298 } 299 if (fwmem_debug) 300 printf("%s: refcount=%d\n", __func__, fms->refcount); 301 302 return (0); 303 } 304 305 int 306 fwmem_close (struct cdev *dev, int flags, int fmt, fw_proc *td) 307 { 308 struct fwmem_softc *fms; 309 310 fms = (struct fwmem_softc *)dev->si_drv1; 311 312 FW_GLOCK(fms->sc->fc); 313 fms->refcount --; 314 FW_GUNLOCK(fms->sc->fc); 315 if (fwmem_debug) 316 printf("%s: refcount=%d\n", __func__, fms->refcount); 317 if (fms->refcount < 1) { 318 free(dev->si_drv1, M_FWMEM); 319 dev->si_drv1 = NULL; 320 } 321 322 return (0); 323 } 324 325 326 static void 327 fwmem_biodone(struct fw_xfer *xfer) 328 { 329 struct bio *bp; 330 331 bp = (struct bio *)xfer->sc; 332 bp->bio_error = xfer->resp; 333 334 if (bp->bio_error != 0) { 335 if (fwmem_debug) 336 printf("%s: err=%d\n", __func__, bp->bio_error); 337 bp->bio_flags |= BIO_ERROR; 338 bp->bio_resid = bp->bio_bcount; 339 } 340 341 fw_xfer_free(xfer); 342 biodone(bp); 343 } 344 345 void 346 fwmem_strategy(struct bio *bp) 347 { 348 struct fwmem_softc *fms; 349 struct fw_device *fwdev; 350 struct fw_xfer *xfer; 351 struct cdev *dev; 352 int err=0, s, iolen; 353 354 dev = bp->bio_dev; 355 /* XXX check request length */ 356 357 s = splfw(); 358 fms = (struct fwmem_softc *)dev->si_drv1; 359 fwdev = fw_noderesolve_eui64(fms->sc->fc, &fms->eui); 360 if (fwdev == NULL) { 361 if (fwmem_debug) 362 printf("fwmem: no such device ID:%08x%08x\n", 363 fms->eui.hi, fms->eui.lo); 364 err = EINVAL; 365 goto error; 366 } 367 368 iolen = MIN(bp->bio_bcount, MAXLEN); 369 if ((bp->bio_cmd & BIO_READ) == BIO_READ) { 370 if (iolen == 4 && (bp->bio_offset & 3) == 0) 371 xfer = fwmem_read_quad(fwdev, 372 (void *) bp, fwmem_speed, 373 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff, 374 bp->bio_data, fwmem_biodone); 375 else 376 xfer = fwmem_read_block(fwdev, 377 (void *) bp, fwmem_speed, 378 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff, 379 iolen, bp->bio_data, fwmem_biodone); 380 } else { 381 if (iolen == 4 && (bp->bio_offset & 3) == 0) 382 xfer = fwmem_write_quad(fwdev, 383 (void *)bp, fwmem_speed, 384 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff, 385 bp->bio_data, fwmem_biodone); 386 else 387 xfer = fwmem_write_block(fwdev, 388 (void *)bp, fwmem_speed, 389 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff, 390 iolen, bp->bio_data, fwmem_biodone); 391 } 392 if (xfer == NULL) { 393 err = EIO; 394 goto error; 395 } 396 /* XXX */ 397 bp->bio_resid = bp->bio_bcount - iolen; 398 error: 399 splx(s); 400 if (err != 0) { 401 if (fwmem_debug) 402 printf("%s: err=%d\n", __func__, err); 403 bp->bio_error = err; 404 bp->bio_flags |= BIO_ERROR; 405 bp->bio_resid = bp->bio_bcount; 406 biodone(bp); 407 } 408 } 409 410 int 411 fwmem_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, fw_proc *td) 412 { 413 struct fwmem_softc *fms; 414 int err = 0; 415 416 fms = (struct fwmem_softc *)dev->si_drv1; 417 switch (cmd) { 418 case FW_SDEUI64: 419 bcopy(data, &fms->eui, sizeof(struct fw_eui64)); 420 break; 421 case FW_GDEUI64: 422 bcopy(&fms->eui, data, sizeof(struct fw_eui64)); 423 break; 424 default: 425 err = EINVAL; 426 } 427 return(err); 428 } 429 int 430 fwmem_poll (struct cdev *dev, int events, fw_proc *td) 431 { 432 return EINVAL; 433 } 434 int 435 fwmem_mmap (struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, 436 int nproto, vm_memattr_t *memattr) 437 { 438 return EINVAL; 439 } 440