xref: /freebsd/sys/dev/firewire/fwmem.c (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2002-2003
5  * 	Hidetoshi Shimokawa. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *
18  *	This product includes software developed by Hidetoshi Shimokawa.
19  *
20  * 4. Neither the name of the author nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37 
38 #ifdef __FreeBSD__
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 #endif
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
46 
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/conf.h>
50 #include <sys/sysctl.h>
51 #include <sys/bio.h>
52 
53 #include <sys/bus.h>
54 #include <machine/bus.h>
55 
56 #include <sys/signal.h>
57 #include <sys/mman.h>
58 #include <sys/ioccom.h>
59 #include <sys/fcntl.h>
60 
61 #include <dev/firewire/firewire.h>
62 #include <dev/firewire/firewirereg.h>
63 #include <dev/firewire/fwmem.h>
64 
65 static int fwmem_speed = 2, fwmem_debug = 0;
66 static struct fw_eui64 fwmem_eui64;
67 SYSCTL_DECL(_hw_firewire);
68 static SYSCTL_NODE(_hw_firewire, OID_AUTO, fwmem,
69     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
70     "FireWire Memory Access");
71 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW,
72 	&fwmem_eui64.hi, 0, "Fwmem target EUI64 high");
73 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW,
74 	&fwmem_eui64.lo, 0, "Fwmem target EUI64 low");
75 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0,
76 	"Fwmem link speed");
77 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0,
78 	"Fwmem driver debug flag");
79 
80 static MALLOC_DEFINE(M_FWMEM, "fwmem", "fwmem/FireWire");
81 
82 #define MAXLEN (512 << fwmem_speed)
83 
84 struct fwmem_softc {
85 	struct fw_eui64 eui;
86 	struct firewire_softc *sc;
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->hand = hand;
112 	xfer->sc = sc;
113 	xfer->send.pay_len = slen;
114 	xfer->recv.pay_len = rlen;
115 
116 	return xfer;
117 }
118 
119 struct fw_xfer *
120 fwmem_read_quad(
121 	struct fw_device *fwdev,
122 	caddr_t	sc,
123 	uint8_t spd,
124 	uint16_t dst_hi,
125 	uint32_t dst_lo,
126 	void *data,
127 	void (*hand)(struct fw_xfer *))
128 {
129 	struct fw_xfer *xfer;
130 	struct fw_pkt *fp;
131 
132 	xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 4, hand);
133 	if (xfer == NULL) {
134 		return NULL;
135 	}
136 
137 	fp = &xfer->send.hdr;
138 	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
139 	fp->mode.rreqq.dest_hi = dst_hi;
140 	fp->mode.rreqq.dest_lo = dst_lo;
141 
142 	xfer->send.payload = NULL;
143 	xfer->recv.payload = (uint32_t *)data;
144 
145 	if (fwmem_debug)
146 		printf("fwmem_read_quad: %d %04x:%08x\n", fwdev->dst,
147 		    dst_hi, dst_lo);
148 
149 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
150 		return xfer;
151 
152 	fw_xfer_free(xfer);
153 	return NULL;
154 }
155 
156 struct fw_xfer *
157 fwmem_write_quad(
158 	struct fw_device *fwdev,
159 	caddr_t	sc,
160 	uint8_t spd,
161 	uint16_t dst_hi,
162 	uint32_t dst_lo,
163 	void *data,
164 	void (*hand)(struct fw_xfer *))
165 {
166 	struct fw_xfer *xfer;
167 	struct fw_pkt *fp;
168 
169 	xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 0, hand);
170 	if (xfer == NULL)
171 		return NULL;
172 
173 	fp = &xfer->send.hdr;
174 	fp->mode.wreqq.tcode = FWTCODE_WREQQ;
175 	fp->mode.wreqq.dest_hi = dst_hi;
176 	fp->mode.wreqq.dest_lo = dst_lo;
177 	fp->mode.wreqq.data = *(uint32_t *)data;
178 
179 	xfer->send.payload = xfer->recv.payload = NULL;
180 
181 	if (fwmem_debug)
182 		printf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst,
183 		    dst_hi, dst_lo, *(uint32_t *)data);
184 
185 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
186 		return xfer;
187 
188 	fw_xfer_free(xfer);
189 	return NULL;
190 }
191 
192 struct fw_xfer *
193 fwmem_read_block(
194 	struct fw_device *fwdev,
195 	caddr_t	sc,
196 	uint8_t spd,
197 	uint16_t dst_hi,
198 	uint32_t dst_lo,
199 	int len,
200 	void *data,
201 	void (*hand)(struct fw_xfer *))
202 {
203 	struct fw_xfer *xfer;
204 	struct fw_pkt *fp;
205 
206 	xfer = fwmem_xfer_req(fwdev, sc, spd, 0, roundup2(len, 4), hand);
207 	if (xfer == NULL)
208 		return NULL;
209 
210 	fp = &xfer->send.hdr;
211 	fp->mode.rreqb.tcode = FWTCODE_RREQB;
212 	fp->mode.rreqb.dest_hi = dst_hi;
213 	fp->mode.rreqb.dest_lo = dst_lo;
214 	fp->mode.rreqb.len = len;
215 	fp->mode.rreqb.extcode = 0;
216 
217 	xfer->send.payload = NULL;
218 	xfer->recv.payload = data;
219 
220 	if (fwmem_debug)
221 		printf("fwmem_read_block: %d %04x:%08x %d\n", fwdev->dst,
222 		    dst_hi, dst_lo, len);
223 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
224 		return xfer;
225 
226 	fw_xfer_free(xfer);
227 	return NULL;
228 }
229 
230 struct fw_xfer *
231 fwmem_write_block(
232 	struct fw_device *fwdev,
233 	caddr_t	sc,
234 	uint8_t spd,
235 	uint16_t dst_hi,
236 	uint32_t dst_lo,
237 	int len,
238 	void *data,
239 	void (*hand)(struct fw_xfer *))
240 {
241 	struct fw_xfer *xfer;
242 	struct fw_pkt *fp;
243 
244 	xfer = fwmem_xfer_req(fwdev, sc, spd, len, 0, hand);
245 	if (xfer == NULL)
246 		return NULL;
247 
248 	fp = &xfer->send.hdr;
249 	fp->mode.wreqb.tcode = FWTCODE_WREQB;
250 	fp->mode.wreqb.dest_hi = dst_hi;
251 	fp->mode.wreqb.dest_lo = dst_lo;
252 	fp->mode.wreqb.len = len;
253 	fp->mode.wreqb.extcode = 0;
254 
255 	xfer->send.payload = data;
256 	xfer->recv.payload = NULL;
257 
258 	if (fwmem_debug)
259 		printf("fwmem_write_block: %d %04x:%08x %d\n", fwdev->dst,
260 				dst_hi, dst_lo, len);
261 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
262 		return xfer;
263 
264 	fw_xfer_free(xfer);
265 	return NULL;
266 }
267 
268 int
269 fwmem_open(struct cdev *dev, int flags, int fmt, fw_proc *td)
270 {
271 	struct fwmem_softc *fms;
272 	struct firewire_softc *sc;
273 	int unit = DEV2UNIT(dev);
274 
275 	sc = devclass_get_softc(firewire_devclass, unit);
276 	if (sc == NULL)
277 		return (ENXIO);
278 
279 	FW_GLOCK(sc->fc);
280 	if (dev->si_drv1 != NULL) {
281 		if ((flags & FWRITE) != 0) {
282 			FW_GUNLOCK(sc->fc);
283 			return (EBUSY);
284 		}
285 		FW_GUNLOCK(sc->fc);
286 		fms = dev->si_drv1;
287 		fms->refcount++;
288 	} else {
289 		dev->si_drv1 = (void *)-1;
290 		FW_GUNLOCK(sc->fc);
291 		dev->si_drv1 = malloc(sizeof(struct fwmem_softc),
292 		    M_FWMEM, M_WAITOK);
293 		dev->si_iosize_max = DFLTPHYS;
294 		fms = dev->si_drv1;
295 		bcopy(&fwmem_eui64, &fms->eui, sizeof(struct fw_eui64));
296 		fms->sc = sc;
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 cdev *dev, int flags, int fmt, fw_proc *td)
307 {
308 	struct fwmem_softc *fms;
309 
310 	fms = dev->si_drv1;
311 
312 	FW_GLOCK(fms->sc->fc);
313 	fms->refcount--;
314 	FW_GUNLOCK(fms->sc->fc);
315 	if (fwmem_debug)
316 		printf("%s: refcount=%d\n", __func__, fms->refcount);
317 	if (fms->refcount < 1) {
318 		free(dev->si_drv1, M_FWMEM);
319 		dev->si_drv1 = NULL;
320 	}
321 
322 	return (0);
323 }
324 
325 
326 static void
327 fwmem_biodone(struct fw_xfer *xfer)
328 {
329 	struct bio *bp;
330 
331 	bp = (struct bio *)xfer->sc;
332 	bp->bio_error = xfer->resp;
333 
334 	if (bp->bio_error != 0) {
335 		if (fwmem_debug)
336 			printf("%s: err=%d\n", __func__, bp->bio_error);
337 		bp->bio_flags |= BIO_ERROR;
338 		bp->bio_resid = bp->bio_bcount;
339 	}
340 
341 	fw_xfer_free(xfer);
342 	biodone(bp);
343 }
344 
345 void
346 fwmem_strategy(struct bio *bp)
347 {
348 	struct fwmem_softc *fms;
349 	struct fw_device *fwdev;
350 	struct fw_xfer *xfer;
351 	struct cdev *dev;
352 	int err = 0, iolen;
353 
354 	dev = bp->bio_dev;
355 	/* XXX check request length */
356 
357 	fms = dev->si_drv1;
358 	fwdev = fw_noderesolve_eui64(fms->sc->fc, &fms->eui);
359 	if (fwdev == NULL) {
360 		if (fwmem_debug)
361 			printf("fwmem: no such device ID:%08x%08x\n",
362 			    fms->eui.hi, fms->eui.lo);
363 		err = EINVAL;
364 		goto error;
365 	}
366 
367 	iolen = MIN(bp->bio_bcount, MAXLEN);
368 	if (bp->bio_cmd == BIO_READ) {
369 		if (iolen == 4 && (bp->bio_offset & 3) == 0)
370 			xfer = fwmem_read_quad(fwdev,
371 			    (void *)bp, fwmem_speed,
372 			    bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
373 			    bp->bio_data, fwmem_biodone);
374 		else
375 			xfer = fwmem_read_block(fwdev,
376 			    (void *)bp, fwmem_speed,
377 			    bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
378 			    iolen, bp->bio_data, fwmem_biodone);
379 	} else {
380 		if (iolen == 4 && (bp->bio_offset & 3) == 0)
381 			xfer = fwmem_write_quad(fwdev,
382 			    (void *)bp, fwmem_speed,
383 			    bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
384 			    bp->bio_data, fwmem_biodone);
385 		else
386 			xfer = fwmem_write_block(fwdev,
387 			    (void *)bp, fwmem_speed,
388 			    bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
389 			    iolen, bp->bio_data, fwmem_biodone);
390 	}
391 	if (xfer == NULL) {
392 		err = EIO;
393 		goto error;
394 	}
395 	/* XXX */
396 	bp->bio_resid = bp->bio_bcount - iolen;
397 error:
398 	if (err != 0) {
399 		if (fwmem_debug)
400 			printf("%s: err=%d\n", __func__, err);
401 		bp->bio_error = err;
402 		bp->bio_flags |= BIO_ERROR;
403 		bp->bio_resid = bp->bio_bcount;
404 		biodone(bp);
405 	}
406 }
407 
408 int
409 fwmem_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
410 {
411 	struct fwmem_softc *fms;
412 	int err = 0;
413 
414 	fms = dev->si_drv1;
415 	switch (cmd) {
416 	case FW_SDEUI64:
417 		bcopy(data, &fms->eui, sizeof(struct fw_eui64));
418 		break;
419 	case FW_GDEUI64:
420 		bcopy(&fms->eui, data, sizeof(struct fw_eui64));
421 		break;
422 	default:
423 		err = EINVAL;
424 	}
425 	return (err);
426 }
427 
428 int
429 fwmem_poll(struct cdev *dev, int events, fw_proc *td)
430 {
431 	return EINVAL;
432 }
433 
434 int
435 fwmem_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
436     int nproto, vm_memattr_t *memattr)
437 {
438 	return EINVAL;
439 }
440