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 * $DragonFly: src/sys/bus/firewire/fwmem.c,v 1.16 2008/01/06 16:55:49 swildner Exp $ 35 */ 36 37 #ifndef __DragonFly__ 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 $"); 40 #endif 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/types.h> 45 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/conf.h> 49 #include <sys/bus.h> 50 #include <sys/sysctl.h> 51 #if defined(__DragonFly__) || __FreeBSD_version < 500000 52 #include <sys/buf.h> 53 #else 54 #include <sys/bio.h> 55 #endif 56 57 #include <sys/signal.h> 58 #include <sys/mman.h> 59 #include <sys/fcntl.h> 60 #include <sys/thread2.h> 61 62 #ifdef __DragonFly__ 63 #include "firewire.h" 64 #include "firewirereg.h" 65 #include "fwmem.h" 66 #else 67 #include <dev/firewire/firewire.h> 68 #include <dev/firewire/firewirereg.h> 69 #include <dev/firewire/fwmem.h> 70 #endif 71 72 static int fwmem_speed=2, fwmem_debug=0; 73 static struct fw_eui64 fwmem_eui64; 74 SYSCTL_DECL(_hw_firewire); 75 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwmem, CTLFLAG_RD, 0, 76 "FireWire Memory Access"); 77 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW, 78 &fwmem_eui64.hi, 0, "Fwmem target EUI64 high"); 79 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW, 80 &fwmem_eui64.lo, 0, "Fwmem target EUI64 low"); 81 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0, 82 "Fwmem link speed"); 83 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0, 84 "Fwmem driver debug flag"); 85 86 MALLOC_DEFINE(M_FWMEM, "fwmem", "fwmem/FireWire"); 87 88 #define MAXLEN (512 << fwmem_speed) 89 90 struct fwmem_softc { 91 struct fw_eui64 eui; 92 int refcount; 93 }; 94 95 static struct fw_xfer * 96 fwmem_xfer_req( 97 struct fw_device *fwdev, 98 caddr_t sc, 99 int spd, 100 int slen, 101 int rlen, 102 void *hand) 103 { 104 struct fw_xfer *xfer; 105 106 xfer = fw_xfer_alloc(M_FWMEM); 107 if (xfer == NULL) 108 return NULL; 109 110 xfer->fc = fwdev->fc; 111 xfer->send.hdr.mode.hdr.dst = FWLOCALBUS | fwdev->dst; 112 if (spd < 0) 113 xfer->send.spd = fwdev->speed; 114 else 115 xfer->send.spd = min(spd, fwdev->speed); 116 xfer->act.hand = hand; 117 xfer->retry_req = fw_asybusy; 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 u_int8_t spd, 130 u_int16_t dst_hi, 131 u_int32_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 = (u_int32_t *)data; 150 151 if (fwmem_debug) 152 kprintf("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 u_int8_t spd, 167 u_int16_t dst_hi, 168 u_int32_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 = *(u_int32_t *)data; 184 185 xfer->send.payload = xfer->recv.payload = NULL; 186 187 if (fwmem_debug) 188 kprintf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst, 189 dst_hi, dst_lo, *(u_int32_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 u_int8_t spd, 203 u_int16_t dst_hi, 204 u_int32_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 kprintf("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 u_int8_t spd, 241 u_int16_t dst_hi, 242 u_int32_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 kprintf("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 dev_open_args *ap) 277 { 278 cdev_t dev = ap->a_head.a_dev; 279 struct fwmem_softc *fms; 280 281 if (dev->si_drv1 != NULL) { 282 if ((ap->a_oflags & FWRITE) != 0) 283 return (EBUSY); 284 fms = (struct fwmem_softc *)dev->si_drv1; 285 fms->refcount ++; 286 } else { 287 fms = (struct fwmem_softc *)kmalloc(sizeof(struct fwmem_softc), 288 M_FWMEM, M_WAITOK); 289 bcopy(&fwmem_eui64, &fms->eui, sizeof(struct fw_eui64)); 290 dev->si_drv1 = (void *)fms; 291 dev->si_iosize_max = DFLTPHYS; 292 fms->refcount = 1; 293 } 294 if (fwmem_debug) 295 kprintf("%s: refcount=%d\n", __func__, fms->refcount); 296 297 return (0); 298 } 299 300 int 301 fwmem_close (struct dev_close_args *ap) 302 { 303 cdev_t dev = ap->a_head.a_dev; 304 struct fwmem_softc *fms; 305 306 fms = (struct fwmem_softc *)dev->si_drv1; 307 fms->refcount --; 308 if (fwmem_debug) 309 kprintf("%s: refcount=%d\n", __func__, fms->refcount); 310 if (fms->refcount < 1) { 311 kfree(dev->si_drv1, M_FW); 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 *bio; 323 struct buf *bp; 324 325 bio = (struct bio *)xfer->sc; 326 bp = bio->bio_buf; 327 bp->b_error = xfer->resp; 328 329 if (bp->b_error != 0) { 330 if (fwmem_debug) 331 kprintf("%s: err=%d\n", __func__, bp->b_error); 332 bp->b_flags |= B_ERROR; 333 bp->b_resid = bp->b_bcount; 334 } 335 fw_xfer_free(xfer); 336 biodone(bio); 337 } 338 339 int 340 fwmem_strategy(struct dev_strategy_args *ap) 341 { 342 cdev_t dev = ap->a_head.a_dev; 343 struct bio *bio = ap->a_bio; 344 struct buf *bp = bio->bio_buf; 345 struct firewire_softc *sc; 346 struct fwmem_softc *fms; 347 struct fw_device *fwdev; 348 struct fw_xfer *xfer; 349 int unit, err=0, iolen; 350 351 /* XXX check request length */ 352 353 unit = DEV2UNIT(dev); 354 sc = devclass_get_softc(firewire_devclass, unit); 355 356 crit_enter(); 357 fms = (struct fwmem_softc *)dev->si_drv1; 358 fwdev = fw_noderesolve_eui64(sc->fc, &fms->eui); 359 if (fwdev == NULL) { 360 if (fwmem_debug) 361 kprintf("fwmem: no such device ID:%08x%08x\n", 362 fms->eui.hi, fms->eui.lo); 363 err = EINVAL; 364 goto error; 365 } 366 if (bio->bio_offset == NOOFFSET) { 367 kprintf("fwmem: offset was not set bp %p\n", bp); 368 err = EINVAL; 369 goto error; 370 } 371 372 iolen = MIN(bp->b_bcount, MAXLEN); 373 if (bp->b_cmd == BUF_CMD_READ) { 374 if (iolen == 4 && (bio->bio_offset & 3) == 0) 375 xfer = fwmem_read_quad(fwdev, 376 (void *) bio, fwmem_speed, 377 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff, 378 bp->b_data, fwmem_biodone); 379 else 380 xfer = fwmem_read_block(fwdev, 381 (void *) bio, fwmem_speed, 382 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff, 383 iolen, bp->b_data, fwmem_biodone); 384 } else { 385 if (iolen == 4 && (bio->bio_offset & 3) == 0) 386 xfer = fwmem_write_quad(fwdev, 387 (void *)bio, fwmem_speed, 388 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff, 389 bp->b_data, fwmem_biodone); 390 else 391 xfer = fwmem_write_block(fwdev, 392 (void *)bio, fwmem_speed, 393 bio->bio_offset >> 32, bio->bio_offset & 0xffffffff, 394 iolen, bp->b_data, fwmem_biodone); 395 } 396 if (xfer == NULL) { 397 err = EIO; 398 goto error; 399 } 400 /* XXX */ 401 bp->b_resid = bp->b_bcount - iolen; 402 error: 403 crit_exit(); 404 if (err != 0) { 405 if (fwmem_debug) 406 kprintf("%s: err=%d\n", __func__, err); 407 bp->b_error = err; 408 bp->b_flags |= B_ERROR; 409 bp->b_resid = bp->b_bcount; 410 biodone(bio); 411 } 412 return(0); 413 } 414 415 int 416 fwmem_ioctl(struct dev_ioctl_args *ap) 417 { 418 cdev_t dev = ap->a_head.a_dev; 419 struct fwmem_softc *fms; 420 int err = 0; 421 422 fms = (struct fwmem_softc *)dev->si_drv1; 423 switch (ap->a_cmd) { 424 case FW_SDEUI64: 425 bcopy(ap->a_data, &fms->eui, sizeof(struct fw_eui64)); 426 break; 427 case FW_GDEUI64: 428 bcopy(&fms->eui, ap->a_data, sizeof(struct fw_eui64)); 429 break; 430 default: 431 err = EINVAL; 432 } 433 return(err); 434 } 435 int 436 fwmem_poll(struct dev_poll_args *ap) 437 { 438 return EINVAL; 439 } 440 int 441 fwmem_mmap(struct dev_mmap_args *ap) 442 { 443 return EINVAL; 444 } 445