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