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