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