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