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