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