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