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