xref: /netbsd-src/sys/dev/ieee1394/if_fwip.c (revision a7e090f70e491979434963c9a27df4020fe0a18b)
1 /*	$NetBSD: if_fwip.c,v 1.21 2010/03/11 04:00:37 mrg Exp $	*/
2 /*-
3  * Copyright (c) 2004
4  *	Doug Rabson
5  * Copyright (c) 2002-2003
6  * 	Hidetoshi Shimokawa. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *
19  *	This product includes software developed by Hidetoshi Shimokawa.
20  *
21  * 4. Neither the name of the author nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * $FreeBSD: src/sys/dev/firewire/if_fwip.c,v 1.16 2007/06/06 14:31:36 simokawa Exp $
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_fwip.c,v 1.21 2010/03/11 04:00:37 mrg Exp $");
42 
43 #ifdef HAVE_KERNEL_OPTION_HEADERS
44 #include "opt_device_polling.h"
45 #include "opt_inet.h"
46 #endif
47 
48 #if defined(__FreeBSD__)
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
57 #include <sys/taskqueue.h>
58 #include <sys/module.h>
59 #include <sys/bus.h>
60 #include <sys/bus.h>
61 
62 #include <net/bpf.h>
63 #include <net/if.h>
64 #include <net/firewire.h>
65 #include <net/if_arp.h>
66 #include <net/if_types.h>
67 #ifdef __DragonFly__
68 #include <bus/firewire/fw_port.h>
69 #include <bus/firewire/firewire.h>
70 #include <bus/firewire/firewirereg.h>
71 #include "if_fwipvar.h"
72 #else
73 #include <dev/firewire/fw_port.h>
74 #include <dev/firewire/firewire.h>
75 #include <dev/firewire/firewirereg.h>
76 #include <dev/firewire/iec13213.h>
77 #include <dev/firewire/if_fwipvar.h>
78 #endif
79 #elif defined(__NetBSD__)
80 #include <sys/param.h>
81 #include <sys/device.h>
82 #include <sys/errno.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/sysctl.h>
86 
87 #include <sys/bus.h>
88 
89 #include <net/if.h>
90 #include <net/if_ieee1394.h>
91 #include <net/if_types.h>
92 
93 #include <dev/ieee1394/fw_port.h>
94 #include <dev/ieee1394/firewire.h>
95 #include <dev/ieee1394/firewirereg.h>
96 #include <dev/ieee1394/iec13213.h>
97 #include <dev/ieee1394/if_fwipvar.h>
98 #endif
99 
100 /*
101  * We really need a mechanism for allocating regions in the FIFO
102  * address space. We pick a address in the OHCI controller's 'middle'
103  * address space. This means that the controller will automatically
104  * send responses for us, which is fine since we don't have any
105  * important information to put in the response anyway.
106  */
107 #define INET_FIFO	0xfffe00000000LL
108 
109 #if defined(__FreeBSD__)
110 #define FWIPDEBUG	if (fwipdebug) if_printf
111 #elif defined(__NetBSD__)
112 #define FWIPDEBUG	if (fwipdebug) aprint_debug_ifnet
113 #endif
114 #define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
115 
116 #if defined(__NetBSD__)
117 int fwipmatch (device_t, cfdata_t, void *);
118 void fwipattach (device_t, device_t, void *);
119 int fwipdetach (device_t, int);
120 int fwipactivate (device_t, enum devact);
121 
122 #endif
123 /* network interface */
124 static void fwip_start (struct ifnet *);
125 static int fwip_ioctl (struct ifnet *, u_long, void *);
126 #if defined(__FreeBSD__)
127 static void fwip_init(void *);
128 static void fwip_stop(struct fwip_softc *);
129 #elif defined(__NetBSD__)
130 static int fwip_init(struct ifnet *);
131 static void fwip_stop(struct ifnet *, int);
132 #endif
133 
134 static void fwip_post_busreset (void *);
135 static void fwip_output_callback (struct fw_xfer *);
136 static void fwip_async_output (struct fwip_softc *, struct ifnet *);
137 static void fwip_start_send (void *, int);
138 static void fwip_stream_input (struct fw_xferq *);
139 static void fwip_unicast_input(struct fw_xfer *);
140 
141 static int fwipdebug = 0;
142 static int broadcast_channel = 0xc0 | 0x1f; /*  tag | channel(XXX) */
143 static int tx_speed = 2;
144 static int rx_queue_len = FWMAXQUEUE;
145 
146 #if defined(__FreeBSD__)
147 MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface");
148 SYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, "");
149 SYSCTL_DECL(_hw_firewire);
150 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD, 0,
151 	"Firewire ip subsystem");
152 SYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
153 	0, "Length of the receive queue");
154 
155 TUNABLE_INT("hw.firewire.fwip.rx_queue_len", &rx_queue_len);
156 #elif defined(__NetBSD__)
157 MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over IEEE1394 interface");
158 /*
159  * Setup sysctl(3) MIB, hw.fwip.*
160  *
161  * TBD condition CTLFLAG_PERMANENT on being a module or not
162  */
163 SYSCTL_SETUP(sysctl_fwip, "sysctl fwip(4) subtree setup")
164 {
165 	int rc, fwip_node_num;
166 	const struct sysctlnode *node;
167 
168 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
169 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
170 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
171 		goto err;
172 	}
173 
174 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
175 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "fwip",
176 	    SYSCTL_DESCR("fwip controls"),
177 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
178 		goto err;
179 	}
180 	fwip_node_num = node->sysctl_num;
181 
182 	/* fwip RX queue length */
183 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
184 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
185 	    "rx_queue_len", SYSCTL_DESCR("Length of the receive queue"),
186 	    NULL, 0, &rx_queue_len,
187 	    0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
188 		goto err;
189 	}
190 
191 	/* fwip RX queue length */
192 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
193 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
194 	    "if_fwip_debug", SYSCTL_DESCR("fwip driver debug flag"),
195 	    NULL, 0, &fwipdebug,
196 	    0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
197 		goto err;
198 	}
199 
200 	return;
201 
202 err:
203 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
204 }
205 #endif
206 
207 #ifdef DEVICE_POLLING
208 static poll_handler_t fwip_poll;
209 
210 static void
211 fwip_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
212 {
213 	struct fwip_softc *fwip;
214 	struct firewire_comm *fc;
215 
216 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
217 		return;
218 
219 	fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
220 	fc = fwip->fd.fc;
221 	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
222 }
223 #endif /* DEVICE_POLLING */
224 #if defined(__FreeBSD__)
225 static void
226 fwip_identify(driver_t *driver, device_t parent)
227 {
228 	BUS_ADD_CHILD(parent, 0, "fwip", fw_get_unit(parent));
229 }
230 
231 static int
232 fwip_probe(device_t dev)
233 {
234 	device_t pa;
235 
236 	pa = device_get_parent(dev);
237 	if(fw_get_unit(dev) != fw_get_unit(pa)){
238 		return(ENXIO);
239 	}
240 
241 	device_set_desc(dev, "IP over FireWire");
242 	return (0);
243 }
244 #elif defined(__NetBSD__)
245 int
246 fwipmatch(device_t parent, cfdata_t cf, void *aux)
247 {
248 	struct fw_attach_args *fwa = aux;
249 
250 	if (strcmp(fwa->name, "fwip") == 0)
251 		return (1);
252 	return (0);
253 }
254 #endif
255 
256 FW_ATTACH(fwip)
257 {
258 	FW_ATTACH_START(fwip, fwip, fwa);
259 	FWIP_ATTACH_START;
260 	struct ifnet *ifp;
261 	int s;
262 
263 	FWIP_ATTACH_SETUP;
264 
265 	ifp = fwip->fw_softc.fwip_ifp;
266 	if (ifp == NULL)
267 		FW_ATTACH_RETURN(ENOSPC);
268 
269 #ifdef __NetBSD__
270 	aprint_naive("\n");
271 #endif
272 
273 	fw_mtx_init(&fwip->mtx, "fwip", NULL, MTX_DEF);
274 	/* XXX */
275 	fwip->dma_ch = -1;
276 
277 	fwip->fd.fc = fwa->fc;
278 	if (tx_speed < 0)
279 		tx_speed = fwip->fd.fc->speed;
280 
281 	fwip->fd.post_explore = NULL;
282 	fwip->fd.post_busreset = fwip_post_busreset;
283 	fwip->fw_softc.fwip = fwip;
284 	FW_TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip);
285 
286 	/*
287 	 * Encode our hardware the way that arp likes it.
288 	 */
289 	hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi);
290 	hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo);
291 	hwaddr->sender_max_rec = fwip->fd.fc->maxrec;
292 	hwaddr->sspd = fwip->fd.fc->speed;
293 	hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
294 	hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
295 
296 	/* fill the rest and attach interface */
297 	ifp->if_softc = &fwip->fw_softc;
298 
299 #if __FreeBSD_version >= 501113 || defined(__DragonFly__) || defined(__NetBSD__)
300 	IF_INITNAME(ifp, dev, unit);
301 #else
302 	ifp->if_unit = unit;
303 	ifp->if_name = "fwip";
304 #endif
305 #if defined(__NetBSD__)
306 	IFQ_SET_READY(&ifp->if_snd);
307 #endif
308 	SET_IFFUNC(ifp, fwip_start, fwip_ioctl, fwip_init, fwip_stop);
309 	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
310 	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
311 #ifdef DEVICE_POLLING
312 	ifp->if_capabilities |= IFCAP_POLLING;
313 #endif
314 
315 	s = splfwnet();
316 	FIREWIRE_IFATTACH(ifp, hwaddr);
317 	splx(s);
318 
319 #if defined(__NetBSD__)
320 	if (!pmf_device_register(self, NULL, NULL))
321 		aprint_error_dev(self, "couldn't establish power handler\n");
322 	else
323 		pmf_class_network_register(self, ifp);
324 #endif
325 
326 	FWIPDEBUG(ifp, "interface created\n");
327 	FW_ATTACH_RETURN(0);
328 }
329 
330 IF_STOP(fwip)
331 {
332 	IF_STOP_START(fwip, ifp, fwip);
333 	struct firewire_comm *fc;
334 	struct fw_xferq *xferq;
335 	struct fw_xfer *xfer, *next;
336 	int i;
337 
338 	fc = fwip->fd.fc;
339 
340 	if (fwip->dma_ch >= 0) {
341 		xferq = fc->ir[fwip->dma_ch];
342 
343 		if (xferq->flag & FWXFERQ_RUNNING)
344 			fc->irx_disable(fc, fwip->dma_ch);
345 		xferq->flag &=
346 			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
347 			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
348 		xferq->hand =  NULL;
349 
350 		for (i = 0; i < xferq->bnchunk; i ++)
351 			m_freem(xferq->bulkxfer[i].mbuf);
352 		free(xferq->bulkxfer, M_FWIP);
353 
354 		fw_bindremove(fc, &fwip->fwb);
355 		for (xfer = STAILQ_FIRST(&fwip->fwb.xferlist); xfer != NULL;
356 					xfer = next) {
357 			next = STAILQ_NEXT(xfer, link);
358 			fw_xfer_free(xfer);
359 		}
360 
361 		for (xfer = STAILQ_FIRST(&fwip->xferlist); xfer != NULL;
362 					xfer = next) {
363 			next = STAILQ_NEXT(xfer, link);
364 			fw_xfer_free(xfer);
365 		}
366 		STAILQ_INIT(&fwip->xferlist);
367 
368 		xferq->bulkxfer =  NULL;
369 		fwip->dma_ch = -1;
370 	}
371 
372 #if defined(__FreeBSD__)
373 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
374 #elif defined(__NetBSD__)
375 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
376 #endif
377 }
378 
379 FW_DETACH(fwip)
380 {
381 	IF_DETACH_START(fwip, fwip);
382 	struct ifnet *ifp;
383 	int s;
384 
385 	ifp = fwip->fw_softc.fwip_ifp;
386 
387 #ifdef DEVICE_POLLING
388 	if (ifp->if_capenable & IFCAP_POLLING)
389 		ether_poll_deregister(ifp);
390 #endif
391 
392 	s = splfwnet();
393 
394 	FWIP_STOP(fwip);
395 	FIREWIRE_IFDETACH(ifp);
396 	fw_mtx_destroy(&fwip->mtx);
397 
398 	splx(s);
399 	return 0;
400 }
401 
402 int
403 fwipactivate(device_t self, enum devact act)
404 {
405 	struct fwip_softc *fwip = device_private(self);
406 
407 	switch (act) {
408 	case DVACT_DEACTIVATE:
409 		if_deactivate(fwip->fw_softc.fwip_ifp);
410 		return 0;
411 	default:
412 		return EOPNOTSUPP;
413 	}
414 }
415 
416 IF_INIT(fwip)
417 {
418 	IF_INIT_START(fwip, fwip, ifp);
419 	struct firewire_comm *fc;
420 	struct fw_xferq *xferq;
421 	struct fw_xfer *xfer;
422 	struct mbuf *m;
423 	int i;
424 
425 	FWIPDEBUG(ifp, "initializing\n");
426 
427 	fc = fwip->fd.fc;
428 #define START 0
429 	if (fwip->dma_ch < 0) {
430 		fwip->dma_ch = fw_open_isodma(fc, /* tx */0);
431 		if (fwip->dma_ch < 0)
432 			IF_INIT_RETURN(ENXIO);
433 		xferq = fc->ir[fwip->dma_ch];
434 		xferq->flag |=
435 		    FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_STREAM;
436 		xferq->flag &= ~0xff;
437 		xferq->flag |= broadcast_channel & 0xff;
438 		/* register fwip_input handler */
439 		xferq->sc = (void *) fwip;
440 		xferq->hand = fwip_stream_input;
441 		xferq->bnchunk = rx_queue_len;
442 		xferq->bnpacket = 1;
443 		xferq->psize = MCLBYTES;
444 		xferq->queued = 0;
445 		xferq->buf = NULL;
446 		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
447 			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
448 							M_FWIP, M_WAITOK);
449 		if (xferq->bulkxfer == NULL) {
450 			printf("if_fwip: malloc failed\n");
451 			IF_INIT_RETURN(ENOMEM);
452 		}
453 		STAILQ_INIT(&xferq->stvalid);
454 		STAILQ_INIT(&xferq->stfree);
455 		STAILQ_INIT(&xferq->stdma);
456 		xferq->stproc = NULL;
457 		for (i = 0; i < xferq->bnchunk; i ++) {
458 			m =
459 #if defined(__DragonFly__) || __FreeBSD_version < 500000
460 				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
461 #else
462 				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
463 #endif
464 			xferq->bulkxfer[i].mbuf = m;
465 			if (m != NULL) {
466 				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
467 				STAILQ_INSERT_TAIL(&xferq->stfree,
468 						&xferq->bulkxfer[i], link);
469 			} else
470 				printf("fwip_as_input: m_getcl failed\n");
471 		}
472 
473 		fwip->fwb.start = INET_FIFO;
474 		fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
475 
476 		/* pre-allocate xfer */
477 		STAILQ_INIT(&fwip->fwb.xferlist);
478 		for (i = 0; i < rx_queue_len; i ++) {
479 			xfer = fw_xfer_alloc(M_FWIP);
480 			if (xfer == NULL)
481 				break;
482 			m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
483 			xfer->recv.payload = mtod(m, uint32_t *);
484 			xfer->recv.pay_len = MCLBYTES;
485 			xfer->hand = fwip_unicast_input;
486 			xfer->fc = fc;
487 			xfer->sc = (void *)fwip;
488 			xfer->mbuf = m;
489 			STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
490 		}
491 		fw_bindadd(fc, &fwip->fwb);
492 
493 		STAILQ_INIT(&fwip->xferlist);
494 		for (i = 0; i < TX_MAX_QUEUE; i++) {
495 			xfer = fw_xfer_alloc(M_FWIP);
496 			if (xfer == NULL)
497 				break;
498 			xfer->send.spd = tx_speed;
499 			xfer->fc = fwip->fd.fc;
500 			xfer->sc = (void *)fwip;
501 			xfer->hand = fwip_output_callback;
502 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
503 		}
504 	} else
505 		xferq = fc->ir[fwip->dma_ch];
506 
507 	fwip->last_dest.hi = 0;
508 	fwip->last_dest.lo = 0;
509 
510 	/* start dma */
511 	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
512 		fc->irx_enable(fc, fwip->dma_ch);
513 
514 #if defined(__FreeBSD__)
515 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
516 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
517 #elif defined(__NetBSD__)
518 	ifp->if_flags |= IFF_RUNNING;
519 	ifp->if_flags &= ~IFF_OACTIVE;
520 #endif
521 
522 #if 0
523 	/* attempt to start output */
524 	fwip_start(ifp);
525 #endif
526 	IF_INIT_RETURN(0);
527 }
528 
529 static int
530 fwip_ioctl(struct ifnet *ifp, u_long cmd, void *data)
531 {
532 	IF_IOCTL_START(fwip, fwip);
533 	int s, error = 0;
534 
535 	switch (cmd) {
536 	case SIOCSIFFLAGS:
537 		s = splfwnet();
538 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
539 			;
540 		else if (ifp->if_flags & IFF_UP) {
541 			if (!(ifp->if_flags & IFF_RUNNING))
542 				FWIP_INIT(fwip);
543 		} else {
544 			if (ifp->if_flags & IFF_RUNNING)
545 				FWIP_STOP(fwip);
546 		}
547 		splx(s);
548 		break;
549 	case SIOCADDMULTI:
550 	case SIOCDELMULTI:
551 		break;
552 	case SIOCSIFCAP:
553 		if ((error = FIREWIRE_IOCTL(ifp, cmd, data)) != ENETRESET)
554 			break;
555 		error = 0;
556 #ifdef DEVICE_POLLING
557 	    {
558 		struct ifreq *ifr = (struct ifreq *) data;
559 		struct firewire_comm *fc = fc = fwip->fd.fc;
560 
561 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
562 		    !(ifp->if_capenable & IFCAP_POLLING)) {
563 			error = ether_poll_register(fwip_poll, ifp);
564 			if (error)
565 				return(error);
566 			/* Disable interrupts */
567 			fc->set_intr(fc, 0);
568 			ifp->if_capenable |= IFCAP_POLLING;
569 			return (error);
570 
571 		}
572 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
573 		    ifp->if_capenable & IFCAP_POLLING) {
574 			error = ether_poll_deregister(ifp);
575 			/* Enable interrupts. */
576 			fc->set_intr(fc, 1);
577 			ifp->if_capenable &= ~IFCAP_POLLING;
578 			return (error);
579 		}
580 	    }
581 #endif /* DEVICE_POLLING */
582 		break;
583 
584 	default:
585 		s = splfwnet();
586 		error = FIREWIRE_IOCTL(ifp, cmd, data);
587 		splx(s);
588 		return (error);
589 	}
590 
591 	return error;
592 }
593 
594 static void
595 fwip_post_busreset(void *arg)
596 {
597 	struct fwip_softc *fwip = arg;
598 	struct crom_src *src;
599 	struct crom_chunk *root;
600 
601 	src = fwip->fd.fc->crom_src;
602 	root = fwip->fd.fc->crom_root;
603 
604 	/* RFC2734 IPv4 over IEEE1394 */
605 	memset(&fwip->unit4, 0, sizeof(struct crom_chunk));
606 	crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
607 	crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
608 	crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
609 	crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
610 	crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
611 
612 	/* RFC3146 IPv6 over IEEE1394 */
613 	memset(&fwip->unit6, 0, sizeof(struct crom_chunk));
614 	crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
615 	crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
616 	crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
617 	crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
618 	crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
619 
620 	fwip->last_dest.hi = 0;
621 	fwip->last_dest.lo = 0;
622 	FIREWIRE_BUSRESET(fwip->fw_softc.fwip_ifp);
623 }
624 
625 static void
626 fwip_output_callback(struct fw_xfer *xfer)
627 {
628 	struct fwip_softc *fwip;
629 	struct ifnet *ifp;
630 	int s;
631 
632 	fwip = (struct fwip_softc *)xfer->sc;
633 	ifp = fwip->fw_softc.fwip_ifp;
634 	/* XXX error check */
635 	FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
636 	if (xfer->resp != 0)
637 		ifp->if_oerrors ++;
638 
639 	m_freem(xfer->mbuf);
640 	fw_xfer_unload(xfer);
641 
642 	s = splfwnet();
643 	FWIP_LOCK(fwip);
644 	STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
645 	FWIP_UNLOCK(fwip);
646 	splx(s);
647 
648 	/* for queue full */
649 	if (ifp->if_snd.ifq_head != NULL) {
650 		fwip_start(ifp);
651 	}
652 }
653 
654 static void
655 fwip_start(struct ifnet *ifp)
656 {
657 	struct fwip_softc *fwip =
658 	    ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
659 	int s;
660 
661 	FWIPDEBUG(ifp, "starting\n");
662 
663 	if (fwip->dma_ch < 0) {
664 		struct mbuf	*m = NULL;
665 
666 		FWIPDEBUG(ifp, "not ready\n");
667 
668 		s = splfwnet();
669 		do {
670 			IF_DEQUEUE(&ifp->if_snd, m);
671 			if (m != NULL)
672 				m_freem(m);
673 			ifp->if_oerrors ++;
674 		} while (m != NULL);
675 		splx(s);
676 
677 		return;
678 	}
679 
680 	s = splfwnet();
681 #if defined(__FreeBSD__)
682 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
683 #elif defined(__NetBSD__)
684 	ifp->if_flags |= IFF_OACTIVE;
685 #endif
686 
687 	if (ifp->if_snd.ifq_len != 0)
688 		fwip_async_output(fwip, ifp);
689 
690 #if defined(__FreeBSD__)
691 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
692 #elif defined(__NetBSD__)
693 	ifp->if_flags &= ~IFF_OACTIVE;
694 #endif
695 	splx(s);
696 }
697 
698 /* Async. stream output */
699 static void
700 fwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
701 {
702 	struct firewire_comm *fc = fwip->fd.fc;
703 	struct mbuf *m;
704 	struct m_tag *mtag;
705 	struct fw_hwaddr *destfw;
706 	struct fw_xfer *xfer;
707 	struct fw_xferq *xferq;
708 	struct fw_pkt *fp;
709 	uint16_t nodeid;
710 	int error;
711 	int i = 0;
712 
713 	xfer = NULL;
714 	xferq = fc->atq;
715 	while ((xferq->queued < xferq->maxq - 1) &&
716 	    (ifp->if_snd.ifq_head != NULL)) {
717 		FWIP_LOCK(fwip);
718 		xfer = STAILQ_FIRST(&fwip->xferlist);
719 		if (xfer == NULL) {
720 			FWIP_UNLOCK(fwip);
721 #if 0
722 			printf("if_fwip: lack of xfer\n");
723 #endif
724 			break;
725 		}
726 		STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
727 		FWIP_UNLOCK(fwip);
728 
729 		IF_DEQUEUE(&ifp->if_snd, m);
730 		if (m == NULL) {
731 			FWIP_LOCK(fwip);
732 			STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link);
733 			FWIP_UNLOCK(fwip);
734 			break;
735 		}
736 
737 		/*
738 		 * Dig out the link-level address which
739 		 * firewire_output got via arp or neighbour
740 		 * discovery. If we don't have a link-level address,
741 		 * just stick the thing on the broadcast channel.
742 		 */
743 		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
744 		if (mtag == NULL)
745 			destfw = 0;
746 		else
747 			destfw = (struct fw_hwaddr *) (mtag + 1);
748 
749 		/*
750 		 * We don't do any bpf stuff here - the generic code
751 		 * in firewire_output gives the packet to bpf before
752 		 * it adds the link-level encapsulation.
753 		 */
754 
755 		/*
756 		 * Put the mbuf in the xfer early in case we hit an
757 		 * error case below - fwip_output_callback will free
758 		 * the mbuf.
759 		 */
760 		xfer->mbuf = m;
761 
762 		/*
763 		 * We use the arp result (if any) to add a suitable firewire
764 		 * packet header before handing off to the bus.
765 		 */
766 		fp = &xfer->send.hdr;
767 		nodeid = FWLOCALBUS | fc->nodeid;
768 		if ((m->m_flags & M_BCAST) || !destfw) {
769 			/*
770 			 * Broadcast packets are sent as GASP packets with
771 			 * specifier ID 0x00005e, version 1 on the broadcast
772 			 * channel. To be conservative, we send at the
773 			 * slowest possible speed.
774 			 */
775 			uint32_t *p;
776 
777 			M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
778 			p = mtod(m, uint32_t *);
779 			fp->mode.stream.len = m->m_pkthdr.len;
780 			fp->mode.stream.chtag = broadcast_channel;
781 			fp->mode.stream.tcode = FWTCODE_STREAM;
782 			fp->mode.stream.sy = 0;
783 			xfer->send.spd = 0;
784 			p[0] = htonl(nodeid << 16);
785 			p[1] = htonl((0x5e << 24) | 1);
786 		} else {
787 			/*
788 			 * Unicast packets are sent as block writes to the
789 			 * target's unicast fifo address. If we can't
790 			 * find the node address, we just give up. We
791 			 * could broadcast it but that might overflow
792 			 * the packet size limitations due to the
793 			 * extra GASP header. Note: the hardware
794 			 * address is stored in network byte order to
795 			 * make life easier for ARP.
796 			 */
797 			struct fw_device *fd;
798 			struct fw_eui64 eui;
799 
800 			eui.hi = ntohl(destfw->sender_unique_ID_hi);
801 			eui.lo = ntohl(destfw->sender_unique_ID_lo);
802 			if (fwip->last_dest.hi != eui.hi ||
803 			    fwip->last_dest.lo != eui.lo) {
804 				fd = fw_noderesolve_eui64(fc, &eui);
805 				if (!fd) {
806 					/* error */
807 					ifp->if_oerrors ++;
808 					/* XXX set error code */
809 					fwip_output_callback(xfer);
810 					continue;
811 
812 				}
813 				fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
814 				fwip->last_hdr.mode.wreqb.tlrt = 0;
815 				fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
816 				fwip->last_hdr.mode.wreqb.pri = 0;
817 				fwip->last_hdr.mode.wreqb.src = nodeid;
818 				fwip->last_hdr.mode.wreqb.dest_hi =
819 					ntohs(destfw->sender_unicast_FIFO_hi);
820 				fwip->last_hdr.mode.wreqb.dest_lo =
821 					ntohl(destfw->sender_unicast_FIFO_lo);
822 				fwip->last_hdr.mode.wreqb.extcode = 0;
823 				fwip->last_dest = eui;
824 			}
825 
826 			fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
827 			fp->mode.wreqb.len = m->m_pkthdr.len;
828 			xfer->send.spd = min(destfw->sspd, fc->speed);
829 		}
830 
831 		xfer->send.pay_len = m->m_pkthdr.len;
832 
833 		error = fw_asyreq(fc, -1, xfer);
834 		if (error == EAGAIN) {
835 			/*
836 			 * We ran out of tlabels - requeue the packet
837 			 * for later transmission.
838 			 */
839 			xfer->mbuf = 0;
840 			FWIP_LOCK(fwip);
841 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
842 			FWIP_UNLOCK(fwip);
843 			IF_PREPEND(&ifp->if_snd, m);
844 			break;
845 		}
846 		if (error) {
847 			/* error */
848 			ifp->if_oerrors ++;
849 			/* XXX set error code */
850 			fwip_output_callback(xfer);
851 			continue;
852 		} else {
853 			ifp->if_opackets ++;
854 			i++;
855 		}
856 	}
857 #if 0
858 	if (i > 1)
859 		printf("%d queued\n", i);
860 #endif
861 	if (i > 0)
862 		xferq->start(fc);
863 }
864 
865 static void
866 fwip_start_send (void *arg, int count)
867 {
868 	struct fwip_softc *fwip = arg;
869 
870 	fwip->fd.fc->atq->start(fwip->fd.fc);
871 }
872 
873 /* Async. stream output */
874 static void
875 fwip_stream_input(struct fw_xferq *xferq)
876 {
877 	struct mbuf *m, *m0;
878 	struct m_tag *mtag;
879 	struct ifnet *ifp;
880 	struct fwip_softc *fwip;
881 	struct fw_bulkxfer *sxfer;
882 	struct fw_pkt *fp;
883 	uint16_t src;
884 	uint32_t *p;
885 
886 	fwip = (struct fwip_softc *)xferq->sc;
887 	ifp = fwip->fw_softc.fwip_ifp;
888 	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
889 		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
890 		fp = mtod(sxfer->mbuf, struct fw_pkt *);
891 		if (fwip->fd.fc->irx_post != NULL)
892 			fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
893 		m = sxfer->mbuf;
894 
895 		/* insert new rbuf */
896 		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
897 		if (m0 != NULL) {
898 			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
899 			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
900 		} else
901 			printf("fwip_as_input: m_getcl failed\n");
902 
903 		/*
904 		 * We must have a GASP header - leave the
905 		 * encapsulation sanity checks to the generic
906 		 * code. Remeber that we also have the firewire async
907 		 * stream header even though that isn't accounted for
908 		 * in mode.stream.len.
909 		 */
910 		if (sxfer->resp != 0 || fp->mode.stream.len <
911 		    2*sizeof(uint32_t)) {
912 			m_freem(m);
913 			ifp->if_ierrors ++;
914 			continue;
915 		}
916 		m->m_len = m->m_pkthdr.len = fp->mode.stream.len
917 			+ sizeof(fp->mode.stream);
918 
919 		/*
920 		 * If we received the packet on the broadcast channel,
921 		 * mark it as broadcast, otherwise we assume it must
922 		 * be multicast.
923 		 */
924 		if (fp->mode.stream.chtag == broadcast_channel)
925 			m->m_flags |= M_BCAST;
926 		else
927 			m->m_flags |= M_MCAST;
928 
929 		/*
930 		 * Make sure we recognise the GASP specifier and
931 		 * version.
932 		 */
933 		p = mtod(m, uint32_t *);
934 		if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
935 		    || (ntohl(p[2]) & 0xffffff) != 1) {
936 			FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
937 			    ntohl(p[1]), ntohl(p[2]));
938 			m_freem(m);
939 			ifp->if_ierrors ++;
940 			continue;
941 		}
942 
943 		/*
944 		 * Record the sender ID for possible BPF usage.
945 		 */
946 		src = ntohl(p[1]) >> 16;
947 		if (bpf_peers_present(ifp->if_bpf)) {
948 			mtag = m_tag_alloc(MTAG_FIREWIRE,
949 			    MTAG_FIREWIRE_SENDER_EUID,
950 			    2*sizeof(uint32_t), M_NOWAIT);
951 			if (mtag) {
952 				/* bpf wants it in network byte order */
953 				struct fw_device *fd;
954 				uint32_t *p2 = (uint32_t *) (mtag + 1);
955 				fd = fw_noderesolve_nodeid(fwip->fd.fc,
956 				    src & 0x3f);
957 				if (fd) {
958 					p2[0] = htonl(fd->eui.hi);
959 					p2[1] = htonl(fd->eui.lo);
960 				} else {
961 					p2[0] = 0;
962 					p2[1] = 0;
963 				}
964 				m_tag_prepend(m, mtag);
965 			}
966 		}
967 
968 		/*
969 		 * Trim off the GASP header
970 		 */
971 		m_adj(m, 3*sizeof(uint32_t));
972 		m->m_pkthdr.rcvif = ifp;
973 		FIREWIRE_INPUT(ifp, m, src);
974 		ifp->if_ipackets ++;
975 	}
976 	if (STAILQ_FIRST(&xferq->stfree) != NULL)
977 		fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
978 }
979 
980 static inline void
981 fwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
982 {
983 	struct mbuf *m;
984 
985 	/*
986 	 * We have finished with a unicast xfer. Allocate a new
987 	 * cluster and stick it on the back of the input queue.
988 	 */
989 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
990 	if (m == NULL)
991 		printf("fwip_unicast_input_recycle: m_getcl failed\n");
992 	xfer->mbuf = m;
993 	xfer->recv.payload = mtod(m, uint32_t *);
994 	xfer->recv.pay_len = MCLBYTES;
995 	xfer->mbuf = m;
996 	STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
997 }
998 
999 static void
1000 fwip_unicast_input(struct fw_xfer *xfer)
1001 {
1002 	uint64_t address;
1003 	struct mbuf *m;
1004 	struct m_tag *mtag;
1005 	struct ifnet *ifp;
1006 	struct fwip_softc *fwip;
1007 	struct fw_pkt *fp;
1008 	//struct fw_pkt *sfp;
1009 	int rtcode;
1010 
1011 	fwip = (struct fwip_softc *)xfer->sc;
1012 	ifp = fwip->fw_softc.fwip_ifp;
1013 	m = xfer->mbuf;
1014 	xfer->mbuf = 0;
1015 	fp = &xfer->recv.hdr;
1016 
1017 	/*
1018 	 * Check the fifo address - we only accept addresses of
1019 	 * exactly INET_FIFO.
1020 	 */
1021 	address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
1022 		| fp->mode.wreqb.dest_lo;
1023 	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
1024 		rtcode = FWRCODE_ER_TYPE;
1025 	} else if (address != INET_FIFO) {
1026 		rtcode = FWRCODE_ER_ADDR;
1027 	} else {
1028 		rtcode = FWRCODE_COMPLETE;
1029 	}
1030 
1031 	/*
1032 	 * Pick up a new mbuf and stick it on the back of the receive
1033 	 * queue.
1034 	 */
1035 	fwip_unicast_input_recycle(fwip, xfer);
1036 
1037 	/*
1038 	 * If we've already rejected the packet, give up now.
1039 	 */
1040 	if (rtcode != FWRCODE_COMPLETE) {
1041 		m_freem(m);
1042 		ifp->if_ierrors ++;
1043 		return;
1044 	}
1045 
1046 	if (bpf_peers_present(ifp->if_bpf)) {
1047 		/*
1048 		 * Record the sender ID for possible BPF usage.
1049 		 */
1050 		mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
1051 		    2*sizeof(uint32_t), M_NOWAIT);
1052 		if (mtag) {
1053 			/* bpf wants it in network byte order */
1054 			struct fw_device *fd;
1055 			uint32_t *p = (uint32_t *) (mtag + 1);
1056 			fd = fw_noderesolve_nodeid(fwip->fd.fc,
1057 			    fp->mode.wreqb.src & 0x3f);
1058 			if (fd) {
1059 				p[0] = htonl(fd->eui.hi);
1060 				p[1] = htonl(fd->eui.lo);
1061 			} else {
1062 				p[0] = 0;
1063 				p[1] = 0;
1064 			}
1065 			m_tag_prepend(m, mtag);
1066 		}
1067 	}
1068 
1069 	/*
1070 	 * Hand off to the generic encapsulation code. We don't use
1071 	 * ifp->if_input so that we can pass the source nodeid as an
1072 	 * argument to facilitate link-level fragment reassembly.
1073 	 */
1074 	m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
1075 	m->m_pkthdr.rcvif = ifp;
1076 	FIREWIRE_INPUT(ifp, m, fp->mode.wreqb.src);
1077 	ifp->if_ipackets ++;
1078 }
1079 
1080 #if defined(__FreeBSD__)
1081 static devclass_t fwip_devclass;
1082 
1083 static device_method_t fwip_methods[] = {
1084 	/* device interface */
1085 	DEVMETHOD(device_identify,	fwip_identify),
1086 	DEVMETHOD(device_probe,		fwip_probe),
1087 	DEVMETHOD(device_attach,	fwip_attach),
1088 	DEVMETHOD(device_detach,	fwip_detach),
1089 	{ 0, 0 }
1090 };
1091 
1092 static driver_t fwip_driver = {
1093         "fwip",
1094 	fwip_methods,
1095 	sizeof(struct fwip_softc),
1096 };
1097 
1098 
1099 #ifdef __DragonFly__
1100 DECLARE_DUMMY_MODULE(fwip);
1101 #endif
1102 DRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
1103 MODULE_VERSION(fwip, 1);
1104 MODULE_DEPEND(fwip, firewire, 1, 1, 1);
1105 #elif defined(__NetBSD__)
1106 CFATTACH_DECL_NEW(fwip, sizeof(struct fwip_softc),
1107     fwipmatch, fwipattach, fwipdetach, NULL);
1108 #endif
1109