]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/firewire/net.c
Merge branch 'for-rmk' of git://git.pengutronix.de/git/imx/linux-2.6
[net-next-2.6.git] / drivers / firewire / net.c
1 /*
2  * IPv4 over IEEE 1394, per RFC 2734
3  *
4  * Copyright (C) 2009 Jay Fenlason <fenlason@redhat.com>
5  *
6  * based on eth1394 by Ben Collins et al
7  */
8
9 #include <linux/bug.h>
10 #include <linux/device.h>
11 #include <linux/ethtool.h>
12 #include <linux/firewire.h>
13 #include <linux/firewire-constants.h>
14 #include <linux/highmem.h>
15 #include <linux/in.h>
16 #include <linux/ip.h>
17 #include <linux/jiffies.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/mutex.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26
27 #include <asm/unaligned.h>
28 #include <net/arp.h>
29
30 #define FWNET_MAX_FRAGMENTS     25      /* arbitrary limit */
31 #define FWNET_ISO_PAGE_COUNT    (PAGE_SIZE < 16 * 1024 ? 4 : 2)
32
33 #define IEEE1394_BROADCAST_CHANNEL      31
34 #define IEEE1394_ALL_NODES              (0xffc0 | 0x003f)
35 #define IEEE1394_MAX_PAYLOAD_S100       512
36 #define FWNET_NO_FIFO_ADDR              (~0ULL)
37
38 #define IANA_SPECIFIER_ID               0x00005eU
39 #define RFC2734_SW_VERSION              0x000001U
40
41 #define IEEE1394_GASP_HDR_SIZE  8
42
43 #define RFC2374_UNFRAG_HDR_SIZE 4
44 #define RFC2374_FRAG_HDR_SIZE   8
45 #define RFC2374_FRAG_OVERHEAD   4
46
47 #define RFC2374_HDR_UNFRAG      0       /* unfragmented         */
48 #define RFC2374_HDR_FIRSTFRAG   1       /* first fragment       */
49 #define RFC2374_HDR_LASTFRAG    2       /* last fragment        */
50 #define RFC2374_HDR_INTFRAG     3       /* interior fragment    */
51
52 #define RFC2734_HW_ADDR_LEN     16
53
54 struct rfc2734_arp {
55         __be16 hw_type;         /* 0x0018       */
56         __be16 proto_type;      /* 0x0806       */
57         u8 hw_addr_len;         /* 16           */
58         u8 ip_addr_len;         /* 4            */
59         __be16 opcode;          /* ARP Opcode   */
60         /* Above is exactly the same format as struct arphdr */
61
62         __be64 s_uniq_id;       /* Sender's 64bit EUI                   */
63         u8 max_rec;             /* Sender's max packet size             */
64         u8 sspd;                /* Sender's max speed                   */
65         __be16 fifo_hi;         /* hi 16bits of sender's FIFO addr      */
66         __be32 fifo_lo;         /* lo 32bits of sender's FIFO addr      */
67         __be32 sip;             /* Sender's IP Address                  */
68         __be32 tip;             /* IP Address of requested hw addr      */
69 } __attribute__((packed));
70
71 /* This header format is specific to this driver implementation. */
72 #define FWNET_ALEN      8
73 #define FWNET_HLEN      10
74 struct fwnet_header {
75         u8 h_dest[FWNET_ALEN];  /* destination address */
76         __be16 h_proto;         /* packet type ID field */
77 } __attribute__((packed));
78
79 /* IPv4 and IPv6 encapsulation header */
80 struct rfc2734_header {
81         u32 w0;
82         u32 w1;
83 };
84
85 #define fwnet_get_hdr_lf(h)             (((h)->w0 & 0xc0000000) >> 30)
86 #define fwnet_get_hdr_ether_type(h)     (((h)->w0 & 0x0000ffff))
87 #define fwnet_get_hdr_dg_size(h)        (((h)->w0 & 0x0fff0000) >> 16)
88 #define fwnet_get_hdr_fg_off(h)         (((h)->w0 & 0x00000fff))
89 #define fwnet_get_hdr_dgl(h)            (((h)->w1 & 0xffff0000) >> 16)
90
91 #define fwnet_set_hdr_lf(lf)            ((lf)  << 30)
92 #define fwnet_set_hdr_ether_type(et)    (et)
93 #define fwnet_set_hdr_dg_size(dgs)      ((dgs) << 16)
94 #define fwnet_set_hdr_fg_off(fgo)       (fgo)
95
96 #define fwnet_set_hdr_dgl(dgl)          ((dgl) << 16)
97
98 static inline void fwnet_make_uf_hdr(struct rfc2734_header *hdr,
99                 unsigned ether_type)
100 {
101         hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_UNFRAG)
102                   | fwnet_set_hdr_ether_type(ether_type);
103 }
104
105 static inline void fwnet_make_ff_hdr(struct rfc2734_header *hdr,
106                 unsigned ether_type, unsigned dg_size, unsigned dgl)
107 {
108         hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_FIRSTFRAG)
109                   | fwnet_set_hdr_dg_size(dg_size)
110                   | fwnet_set_hdr_ether_type(ether_type);
111         hdr->w1 = fwnet_set_hdr_dgl(dgl);
112 }
113
114 static inline void fwnet_make_sf_hdr(struct rfc2734_header *hdr,
115                 unsigned lf, unsigned dg_size, unsigned fg_off, unsigned dgl)
116 {
117         hdr->w0 = fwnet_set_hdr_lf(lf)
118                   | fwnet_set_hdr_dg_size(dg_size)
119                   | fwnet_set_hdr_fg_off(fg_off);
120         hdr->w1 = fwnet_set_hdr_dgl(dgl);
121 }
122
123 /* This list keeps track of what parts of the datagram have been filled in */
124 struct fwnet_fragment_info {
125         struct list_head fi_link;
126         u16 offset;
127         u16 len;
128 };
129
130 struct fwnet_partial_datagram {
131         struct list_head pd_link;
132         struct list_head fi_list;
133         struct sk_buff *skb;
134         /* FIXME Why not use skb->data? */
135         char *pbuf;
136         u16 datagram_label;
137         u16 ether_type;
138         u16 datagram_size;
139 };
140
141 static DEFINE_MUTEX(fwnet_device_mutex);
142 static LIST_HEAD(fwnet_device_list);
143
144 struct fwnet_device {
145         struct list_head dev_link;
146         spinlock_t lock;
147         enum {
148                 FWNET_BROADCAST_ERROR,
149                 FWNET_BROADCAST_RUNNING,
150                 FWNET_BROADCAST_STOPPED,
151         } broadcast_state;
152         struct fw_iso_context *broadcast_rcv_context;
153         struct fw_iso_buffer broadcast_rcv_buffer;
154         void **broadcast_rcv_buffer_ptrs;
155         unsigned broadcast_rcv_next_ptr;
156         unsigned num_broadcast_rcv_ptrs;
157         unsigned rcv_buffer_size;
158         /*
159          * This value is the maximum unfragmented datagram size that can be
160          * sent by the hardware.  It already has the GASP overhead and the
161          * unfragmented datagram header overhead calculated into it.
162          */
163         unsigned broadcast_xmt_max_payload;
164         u16 broadcast_xmt_datagramlabel;
165
166         /*
167          * The CSR address that remote nodes must send datagrams to for us to
168          * receive them.
169          */
170         struct fw_address_handler handler;
171         u64 local_fifo;
172
173         /* List of packets to be sent */
174         struct list_head packet_list;
175         /*
176          * List of packets that were broadcasted.  When we get an ISO interrupt
177          * one of them has been sent
178          */
179         struct list_head broadcasted_list;
180         /* List of packets that have been sent but not yet acked */
181         struct list_head sent_list;
182
183         struct list_head peer_list;
184         struct fw_card *card;
185         struct net_device *netdev;
186 };
187
188 struct fwnet_peer {
189         struct list_head peer_link;
190         struct fwnet_device *dev;
191         u64 guid;
192         u64 fifo;
193
194         /* guarded by dev->lock */
195         struct list_head pd_list; /* received partial datagrams */
196         unsigned pdg_size;        /* pd_list size */
197
198         u16 datagram_label;       /* outgoing datagram label */
199         unsigned max_payload;     /* includes RFC2374_FRAG_HDR_SIZE overhead */
200         int node_id;
201         int generation;
202         unsigned speed;
203 };
204
205 /* This is our task struct. It's used for the packet complete callback.  */
206 struct fwnet_packet_task {
207         /*
208          * ptask can actually be on dev->packet_list, dev->broadcasted_list,
209          * or dev->sent_list depending on its current state.
210          */
211         struct list_head pt_link;
212         struct fw_transaction transaction;
213         struct rfc2734_header hdr;
214         struct sk_buff *skb;
215         struct fwnet_device *dev;
216
217         int outstanding_pkts;
218         unsigned max_payload;
219         u64 fifo_addr;
220         u16 dest_node;
221         u8 generation;
222         u8 speed;
223 };
224
225 /*
226  * saddr == NULL means use device source address.
227  * daddr == NULL means leave destination address (eg unresolved arp).
228  */
229 static int fwnet_header_create(struct sk_buff *skb, struct net_device *net,
230                         unsigned short type, const void *daddr,
231                         const void *saddr, unsigned len)
232 {
233         struct fwnet_header *h;
234
235         h = (struct fwnet_header *)skb_push(skb, sizeof(*h));
236         put_unaligned_be16(type, &h->h_proto);
237
238         if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) {
239                 memset(h->h_dest, 0, net->addr_len);
240
241                 return net->hard_header_len;
242         }
243
244         if (daddr) {
245                 memcpy(h->h_dest, daddr, net->addr_len);
246
247                 return net->hard_header_len;
248         }
249
250         return -net->hard_header_len;
251 }
252
253 static int fwnet_header_rebuild(struct sk_buff *skb)
254 {
255         struct fwnet_header *h = (struct fwnet_header *)skb->data;
256
257         if (get_unaligned_be16(&h->h_proto) == ETH_P_IP)
258                 return arp_find((unsigned char *)&h->h_dest, skb);
259
260         fw_notify("%s: unable to resolve type %04x addresses\n",
261                   skb->dev->name, be16_to_cpu(h->h_proto));
262         return 0;
263 }
264
265 static int fwnet_header_cache(const struct neighbour *neigh,
266                               struct hh_cache *hh)
267 {
268         struct net_device *net;
269         struct fwnet_header *h;
270
271         if (hh->hh_type == cpu_to_be16(ETH_P_802_3))
272                 return -1;
273         net = neigh->dev;
274         h = (struct fwnet_header *)((u8 *)hh->hh_data + 16 - sizeof(*h));
275         h->h_proto = hh->hh_type;
276         memcpy(h->h_dest, neigh->ha, net->addr_len);
277         hh->hh_len = FWNET_HLEN;
278
279         return 0;
280 }
281
282 /* Called by Address Resolution module to notify changes in address. */
283 static void fwnet_header_cache_update(struct hh_cache *hh,
284                 const struct net_device *net, const unsigned char *haddr)
285 {
286         memcpy((u8 *)hh->hh_data + 16 - FWNET_HLEN, haddr, net->addr_len);
287 }
288
289 static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr)
290 {
291         memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN);
292
293         return FWNET_ALEN;
294 }
295
296 static const struct header_ops fwnet_header_ops = {
297         .create         = fwnet_header_create,
298         .rebuild        = fwnet_header_rebuild,
299         .cache          = fwnet_header_cache,
300         .cache_update   = fwnet_header_cache_update,
301         .parse          = fwnet_header_parse,
302 };
303
304 /* FIXME: is this correct for all cases? */
305 static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd,
306                                unsigned offset, unsigned len)
307 {
308         struct fwnet_fragment_info *fi;
309         unsigned end = offset + len;
310
311         list_for_each_entry(fi, &pd->fi_list, fi_link)
312                 if (offset < fi->offset + fi->len && end > fi->offset)
313                         return true;
314
315         return false;
316 }
317
318 /* Assumes that new fragment does not overlap any existing fragments */
319 static struct fwnet_fragment_info *fwnet_frag_new(
320         struct fwnet_partial_datagram *pd, unsigned offset, unsigned len)
321 {
322         struct fwnet_fragment_info *fi, *fi2, *new;
323         struct list_head *list;
324
325         list = &pd->fi_list;
326         list_for_each_entry(fi, &pd->fi_list, fi_link) {
327                 if (fi->offset + fi->len == offset) {
328                         /* The new fragment can be tacked on to the end */
329                         /* Did the new fragment plug a hole? */
330                         fi2 = list_entry(fi->fi_link.next,
331                                          struct fwnet_fragment_info, fi_link);
332                         if (fi->offset + fi->len == fi2->offset) {
333                                 /* glue fragments together */
334                                 fi->len += len + fi2->len;
335                                 list_del(&fi2->fi_link);
336                                 kfree(fi2);
337                         } else {
338                                 fi->len += len;
339                         }
340
341                         return fi;
342                 }
343                 if (offset + len == fi->offset) {
344                         /* The new fragment can be tacked on to the beginning */
345                         /* Did the new fragment plug a hole? */
346                         fi2 = list_entry(fi->fi_link.prev,
347                                          struct fwnet_fragment_info, fi_link);
348                         if (fi2->offset + fi2->len == fi->offset) {
349                                 /* glue fragments together */
350                                 fi2->len += fi->len + len;
351                                 list_del(&fi->fi_link);
352                                 kfree(fi);
353
354                                 return fi2;
355                         }
356                         fi->offset = offset;
357                         fi->len += len;
358
359                         return fi;
360                 }
361                 if (offset > fi->offset + fi->len) {
362                         list = &fi->fi_link;
363                         break;
364                 }
365                 if (offset + len < fi->offset) {
366                         list = fi->fi_link.prev;
367                         break;
368                 }
369         }
370
371         new = kmalloc(sizeof(*new), GFP_ATOMIC);
372         if (!new) {
373                 fw_error("out of memory\n");
374                 return NULL;
375         }
376
377         new->offset = offset;
378         new->len = len;
379         list_add(&new->fi_link, list);
380
381         return new;
382 }
383
384 static struct fwnet_partial_datagram *fwnet_pd_new(struct net_device *net,
385                 struct fwnet_peer *peer, u16 datagram_label, unsigned dg_size,
386                 void *frag_buf, unsigned frag_off, unsigned frag_len)
387 {
388         struct fwnet_partial_datagram *new;
389         struct fwnet_fragment_info *fi;
390
391         new = kmalloc(sizeof(*new), GFP_ATOMIC);
392         if (!new)
393                 goto fail;
394
395         INIT_LIST_HEAD(&new->fi_list);
396         fi = fwnet_frag_new(new, frag_off, frag_len);
397         if (fi == NULL)
398                 goto fail_w_new;
399
400         new->datagram_label = datagram_label;
401         new->datagram_size = dg_size;
402         new->skb = dev_alloc_skb(dg_size + net->hard_header_len + 15);
403         if (new->skb == NULL)
404                 goto fail_w_fi;
405
406         skb_reserve(new->skb, (net->hard_header_len + 15) & ~15);
407         new->pbuf = skb_put(new->skb, dg_size);
408         memcpy(new->pbuf + frag_off, frag_buf, frag_len);
409         list_add_tail(&new->pd_link, &peer->pd_list);
410
411         return new;
412
413 fail_w_fi:
414         kfree(fi);
415 fail_w_new:
416         kfree(new);
417 fail:
418         fw_error("out of memory\n");
419
420         return NULL;
421 }
422
423 static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer,
424                                                     u16 datagram_label)
425 {
426         struct fwnet_partial_datagram *pd;
427
428         list_for_each_entry(pd, &peer->pd_list, pd_link)
429                 if (pd->datagram_label == datagram_label)
430                         return pd;
431
432         return NULL;
433 }
434
435
436 static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
437 {
438         struct fwnet_fragment_info *fi, *n;
439
440         list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
441                 kfree(fi);
442
443         list_del(&old->pd_link);
444         dev_kfree_skb_any(old->skb);
445         kfree(old);
446 }
447
448 static bool fwnet_pd_update(struct fwnet_peer *peer,
449                 struct fwnet_partial_datagram *pd, void *frag_buf,
450                 unsigned frag_off, unsigned frag_len)
451 {
452         if (fwnet_frag_new(pd, frag_off, frag_len) == NULL)
453                 return false;
454
455         memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
456
457         /*
458          * Move list entry to beginnig of list so that oldest partial
459          * datagrams percolate to the end of the list
460          */
461         list_move_tail(&pd->pd_link, &peer->pd_list);
462
463         return true;
464 }
465
466 static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
467 {
468         struct fwnet_fragment_info *fi;
469
470         fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
471
472         return fi->len == pd->datagram_size;
473 }
474
475 /* caller must hold dev->lock */
476 static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
477                                                   u64 guid)
478 {
479         struct fwnet_peer *peer;
480
481         list_for_each_entry(peer, &dev->peer_list, peer_link)
482                 if (peer->guid == guid)
483                         return peer;
484
485         return NULL;
486 }
487
488 /* caller must hold dev->lock */
489 static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
490                                                 int node_id, int generation)
491 {
492         struct fwnet_peer *peer;
493
494         list_for_each_entry(peer, &dev->peer_list, peer_link)
495                 if (peer->node_id    == node_id &&
496                     peer->generation == generation)
497                         return peer;
498
499         return NULL;
500 }
501
502 /* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */
503 static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed)
504 {
505         max_rec = min(max_rec, speed + 8);
506         max_rec = min(max_rec, 0xbU); /* <= 4096 */
507         if (max_rec < 8) {
508                 fw_notify("max_rec %x out of range\n", max_rec);
509                 max_rec = 8;
510         }
511
512         return (1 << (max_rec + 1)) - RFC2374_FRAG_HDR_SIZE;
513 }
514
515
516 static int fwnet_finish_incoming_packet(struct net_device *net,
517                                         struct sk_buff *skb, u16 source_node_id,
518                                         bool is_broadcast, u16 ether_type)
519 {
520         struct fwnet_device *dev;
521         static const __be64 broadcast_hw = cpu_to_be64(~0ULL);
522         int status;
523         __be64 guid;
524
525         dev = netdev_priv(net);
526         /* Write metadata, and then pass to the receive level */
527         skb->dev = net;
528         skb->ip_summed = CHECKSUM_UNNECESSARY;  /* don't check it */
529
530         /*
531          * Parse the encapsulation header. This actually does the job of
532          * converting to an ethernet frame header, as well as arp
533          * conversion if needed. ARP conversion is easier in this
534          * direction, since we are using ethernet as our backend.
535          */
536         /*
537          * If this is an ARP packet, convert it. First, we want to make
538          * use of some of the fields, since they tell us a little bit
539          * about the sending machine.
540          */
541         if (ether_type == ETH_P_ARP) {
542                 struct rfc2734_arp *arp1394;
543                 struct arphdr *arp;
544                 unsigned char *arp_ptr;
545                 u64 fifo_addr;
546                 u64 peer_guid;
547                 unsigned sspd;
548                 u16 max_payload;
549                 struct fwnet_peer *peer;
550                 unsigned long flags;
551
552                 arp1394   = (struct rfc2734_arp *)skb->data;
553                 arp       = (struct arphdr *)skb->data;
554                 arp_ptr   = (unsigned char *)(arp + 1);
555                 peer_guid = get_unaligned_be64(&arp1394->s_uniq_id);
556                 fifo_addr = (u64)get_unaligned_be16(&arp1394->fifo_hi) << 32
557                                 | get_unaligned_be32(&arp1394->fifo_lo);
558
559                 sspd = arp1394->sspd;
560                 /* Sanity check.  OS X 10.3 PPC reportedly sends 131. */
561                 if (sspd > SCODE_3200) {
562                         fw_notify("sspd %x out of range\n", sspd);
563                         sspd = SCODE_3200;
564                 }
565                 max_payload = fwnet_max_payload(arp1394->max_rec, sspd);
566
567                 spin_lock_irqsave(&dev->lock, flags);
568                 peer = fwnet_peer_find_by_guid(dev, peer_guid);
569                 if (peer) {
570                         peer->fifo = fifo_addr;
571
572                         if (peer->speed > sspd)
573                                 peer->speed = sspd;
574                         if (peer->max_payload > max_payload)
575                                 peer->max_payload = max_payload;
576                 }
577                 spin_unlock_irqrestore(&dev->lock, flags);
578
579                 if (!peer) {
580                         fw_notify("No peer for ARP packet from %016llx\n",
581                                   (unsigned long long)peer_guid);
582                         goto no_peer;
583                 }
584
585                 /*
586                  * Now that we're done with the 1394 specific stuff, we'll
587                  * need to alter some of the data.  Believe it or not, all
588                  * that needs to be done is sender_IP_address needs to be
589                  * moved, the destination hardware address get stuffed
590                  * in and the hardware address length set to 8.
591                  *
592                  * IMPORTANT: The code below overwrites 1394 specific data
593                  * needed above so keep the munging of the data for the
594                  * higher level IP stack last.
595                  */
596
597                 arp->ar_hln = 8;
598                 /* skip over sender unique id */
599                 arp_ptr += arp->ar_hln;
600                 /* move sender IP addr */
601                 put_unaligned(arp1394->sip, (u32 *)arp_ptr);
602                 /* skip over sender IP addr */
603                 arp_ptr += arp->ar_pln;
604
605                 if (arp->ar_op == htons(ARPOP_REQUEST))
606                         memset(arp_ptr, 0, sizeof(u64));
607                 else
608                         memcpy(arp_ptr, net->dev_addr, sizeof(u64));
609         }
610
611         /* Now add the ethernet header. */
612         guid = cpu_to_be64(dev->card->guid);
613         if (dev_hard_header(skb, net, ether_type,
614                            is_broadcast ? &broadcast_hw : &guid,
615                            NULL, skb->len) >= 0) {
616                 struct fwnet_header *eth;
617                 u16 *rawp;
618                 __be16 protocol;
619
620                 skb_reset_mac_header(skb);
621                 skb_pull(skb, sizeof(*eth));
622                 eth = (struct fwnet_header *)skb_mac_header(skb);
623                 if (*eth->h_dest & 1) {
624                         if (memcmp(eth->h_dest, net->broadcast,
625                                    net->addr_len) == 0)
626                                 skb->pkt_type = PACKET_BROADCAST;
627 #if 0
628                         else
629                                 skb->pkt_type = PACKET_MULTICAST;
630 #endif
631                 } else {
632                         if (memcmp(eth->h_dest, net->dev_addr, net->addr_len))
633                                 skb->pkt_type = PACKET_OTHERHOST;
634                 }
635                 if (ntohs(eth->h_proto) >= 1536) {
636                         protocol = eth->h_proto;
637                 } else {
638                         rawp = (u16 *)skb->data;
639                         if (*rawp == 0xffff)
640                                 protocol = htons(ETH_P_802_3);
641                         else
642                                 protocol = htons(ETH_P_802_2);
643                 }
644                 skb->protocol = protocol;
645         }
646         status = netif_rx(skb);
647         if (status == NET_RX_DROP) {
648                 net->stats.rx_errors++;
649                 net->stats.rx_dropped++;
650         } else {
651                 net->stats.rx_packets++;
652                 net->stats.rx_bytes += skb->len;
653         }
654         if (netif_queue_stopped(net))
655                 netif_wake_queue(net);
656
657         return 0;
658
659  no_peer:
660         net->stats.rx_errors++;
661         net->stats.rx_dropped++;
662
663         dev_kfree_skb_any(skb);
664         if (netif_queue_stopped(net))
665                 netif_wake_queue(net);
666
667         return -ENOENT;
668 }
669
670 static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
671                                  int source_node_id, int generation,
672                                  bool is_broadcast)
673 {
674         struct sk_buff *skb;
675         struct net_device *net = dev->netdev;
676         struct rfc2734_header hdr;
677         unsigned lf;
678         unsigned long flags;
679         struct fwnet_peer *peer;
680         struct fwnet_partial_datagram *pd;
681         int fg_off;
682         int dg_size;
683         u16 datagram_label;
684         int retval;
685         u16 ether_type;
686
687         hdr.w0 = be32_to_cpu(buf[0]);
688         lf = fwnet_get_hdr_lf(&hdr);
689         if (lf == RFC2374_HDR_UNFRAG) {
690                 /*
691                  * An unfragmented datagram has been received by the ieee1394
692                  * bus. Build an skbuff around it so we can pass it to the
693                  * high level network layer.
694                  */
695                 ether_type = fwnet_get_hdr_ether_type(&hdr);
696                 buf++;
697                 len -= RFC2374_UNFRAG_HDR_SIZE;
698
699                 skb = dev_alloc_skb(len + net->hard_header_len + 15);
700                 if (unlikely(!skb)) {
701                         fw_error("out of memory\n");
702                         net->stats.rx_dropped++;
703
704                         return -ENOMEM;
705                 }
706                 skb_reserve(skb, (net->hard_header_len + 15) & ~15);
707                 memcpy(skb_put(skb, len), buf, len);
708
709                 return fwnet_finish_incoming_packet(net, skb, source_node_id,
710                                                     is_broadcast, ether_type);
711         }
712         /* A datagram fragment has been received, now the fun begins. */
713         hdr.w1 = ntohl(buf[1]);
714         buf += 2;
715         len -= RFC2374_FRAG_HDR_SIZE;
716         if (lf == RFC2374_HDR_FIRSTFRAG) {
717                 ether_type = fwnet_get_hdr_ether_type(&hdr);
718                 fg_off = 0;
719         } else {
720                 ether_type = 0;
721                 fg_off = fwnet_get_hdr_fg_off(&hdr);
722         }
723         datagram_label = fwnet_get_hdr_dgl(&hdr);
724         dg_size = fwnet_get_hdr_dg_size(&hdr); /* ??? + 1 */
725
726         spin_lock_irqsave(&dev->lock, flags);
727
728         peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation);
729         if (!peer) {
730                 retval = -ENOENT;
731                 goto fail;
732         }
733
734         pd = fwnet_pd_find(peer, datagram_label);
735         if (pd == NULL) {
736                 while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) {
737                         /* remove the oldest */
738                         fwnet_pd_delete(list_first_entry(&peer->pd_list,
739                                 struct fwnet_partial_datagram, pd_link));
740                         peer->pdg_size--;
741                 }
742                 pd = fwnet_pd_new(net, peer, datagram_label,
743                                   dg_size, buf, fg_off, len);
744                 if (pd == NULL) {
745                         retval = -ENOMEM;
746                         goto fail;
747                 }
748                 peer->pdg_size++;
749         } else {
750                 if (fwnet_frag_overlap(pd, fg_off, len) ||
751                     pd->datagram_size != dg_size) {
752                         /*
753                          * Differing datagram sizes or overlapping fragments,
754                          * discard old datagram and start a new one.
755                          */
756                         fwnet_pd_delete(pd);
757                         pd = fwnet_pd_new(net, peer, datagram_label,
758                                           dg_size, buf, fg_off, len);
759                         if (pd == NULL) {
760                                 peer->pdg_size--;
761                                 retval = -ENOMEM;
762                                 goto fail;
763                         }
764                 } else {
765                         if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) {
766                                 /*
767                                  * Couldn't save off fragment anyway
768                                  * so might as well obliterate the
769                                  * datagram now.
770                                  */
771                                 fwnet_pd_delete(pd);
772                                 peer->pdg_size--;
773                                 retval = -ENOMEM;
774                                 goto fail;
775                         }
776                 }
777         } /* new datagram or add to existing one */
778
779         if (lf == RFC2374_HDR_FIRSTFRAG)
780                 pd->ether_type = ether_type;
781
782         if (fwnet_pd_is_complete(pd)) {
783                 ether_type = pd->ether_type;
784                 peer->pdg_size--;
785                 skb = skb_get(pd->skb);
786                 fwnet_pd_delete(pd);
787
788                 spin_unlock_irqrestore(&dev->lock, flags);
789
790                 return fwnet_finish_incoming_packet(net, skb, source_node_id,
791                                                     false, ether_type);
792         }
793         /*
794          * Datagram is not complete, we're done for the
795          * moment.
796          */
797         spin_unlock_irqrestore(&dev->lock, flags);
798
799         return 0;
800  fail:
801         spin_unlock_irqrestore(&dev->lock, flags);
802
803         if (netif_queue_stopped(net))
804                 netif_wake_queue(net);
805
806         return retval;
807 }
808
809 static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r,
810                 int tcode, int destination, int source, int generation,
811                 unsigned long long offset, void *payload, size_t length,
812                 void *callback_data)
813 {
814         struct fwnet_device *dev = callback_data;
815         int rcode;
816
817         if (destination == IEEE1394_ALL_NODES) {
818                 kfree(r);
819
820                 return;
821         }
822
823         if (offset != dev->handler.offset)
824                 rcode = RCODE_ADDRESS_ERROR;
825         else if (tcode != TCODE_WRITE_BLOCK_REQUEST)
826                 rcode = RCODE_TYPE_ERROR;
827         else if (fwnet_incoming_packet(dev, payload, length,
828                                        source, generation, false) != 0) {
829                 fw_error("Incoming packet failure\n");
830                 rcode = RCODE_CONFLICT_ERROR;
831         } else
832                 rcode = RCODE_COMPLETE;
833
834         fw_send_response(card, r, rcode);
835 }
836
837 static void fwnet_receive_broadcast(struct fw_iso_context *context,
838                 u32 cycle, size_t header_length, void *header, void *data)
839 {
840         struct fwnet_device *dev;
841         struct fw_iso_packet packet;
842         struct fw_card *card;
843         __be16 *hdr_ptr;
844         __be32 *buf_ptr;
845         int retval;
846         u32 length;
847         u16 source_node_id;
848         u32 specifier_id;
849         u32 ver;
850         unsigned long offset;
851         unsigned long flags;
852
853         dev = data;
854         card = dev->card;
855         hdr_ptr = header;
856         length = be16_to_cpup(hdr_ptr);
857
858         spin_lock_irqsave(&dev->lock, flags);
859
860         offset = dev->rcv_buffer_size * dev->broadcast_rcv_next_ptr;
861         buf_ptr = dev->broadcast_rcv_buffer_ptrs[dev->broadcast_rcv_next_ptr++];
862         if (dev->broadcast_rcv_next_ptr == dev->num_broadcast_rcv_ptrs)
863                 dev->broadcast_rcv_next_ptr = 0;
864
865         spin_unlock_irqrestore(&dev->lock, flags);
866
867         specifier_id =    (be32_to_cpu(buf_ptr[0]) & 0xffff) << 8
868                         | (be32_to_cpu(buf_ptr[1]) & 0xff000000) >> 24;
869         ver = be32_to_cpu(buf_ptr[1]) & 0xffffff;
870         source_node_id = be32_to_cpu(buf_ptr[0]) >> 16;
871
872         if (specifier_id == IANA_SPECIFIER_ID && ver == RFC2734_SW_VERSION) {
873                 buf_ptr += 2;
874                 length -= IEEE1394_GASP_HDR_SIZE;
875                 fwnet_incoming_packet(dev, buf_ptr, length,
876                                       source_node_id, -1, true);
877         }
878
879         packet.payload_length = dev->rcv_buffer_size;
880         packet.interrupt = 1;
881         packet.skip = 0;
882         packet.tag = 3;
883         packet.sy = 0;
884         packet.header_length = IEEE1394_GASP_HDR_SIZE;
885
886         spin_lock_irqsave(&dev->lock, flags);
887
888         retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet,
889                                       &dev->broadcast_rcv_buffer, offset);
890
891         spin_unlock_irqrestore(&dev->lock, flags);
892
893         if (retval < 0)
894                 fw_error("requeue failed\n");
895 }
896
897 static struct kmem_cache *fwnet_packet_task_cache;
898
899 static void fwnet_free_ptask(struct fwnet_packet_task *ptask)
900 {
901         dev_kfree_skb_any(ptask->skb);
902         kmem_cache_free(fwnet_packet_task_cache, ptask);
903 }
904
905 static int fwnet_send_packet(struct fwnet_packet_task *ptask);
906
907 static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask)
908 {
909         struct fwnet_device *dev = ptask->dev;
910         unsigned long flags;
911         bool free;
912
913         spin_lock_irqsave(&dev->lock, flags);
914
915         ptask->outstanding_pkts--;
916
917         /* Check whether we or the networking TX soft-IRQ is last user. */
918         free = (ptask->outstanding_pkts == 0 && !list_empty(&ptask->pt_link));
919
920         if (ptask->outstanding_pkts == 0)
921                 list_del(&ptask->pt_link);
922
923         spin_unlock_irqrestore(&dev->lock, flags);
924
925         if (ptask->outstanding_pkts > 0) {
926                 u16 dg_size;
927                 u16 fg_off;
928                 u16 datagram_label;
929                 u16 lf;
930                 struct sk_buff *skb;
931
932                 /* Update the ptask to point to the next fragment and send it */
933                 lf = fwnet_get_hdr_lf(&ptask->hdr);
934                 switch (lf) {
935                 case RFC2374_HDR_LASTFRAG:
936                 case RFC2374_HDR_UNFRAG:
937                 default:
938                         fw_error("Outstanding packet %x lf %x, header %x,%x\n",
939                                  ptask->outstanding_pkts, lf, ptask->hdr.w0,
940                                  ptask->hdr.w1);
941                         BUG();
942
943                 case RFC2374_HDR_FIRSTFRAG:
944                         /* Set frag type here for future interior fragments */
945                         dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
946                         fg_off = ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
947                         datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
948                         break;
949
950                 case RFC2374_HDR_INTFRAG:
951                         dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
952                         fg_off = fwnet_get_hdr_fg_off(&ptask->hdr)
953                                   + ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
954                         datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
955                         break;
956                 }
957                 skb = ptask->skb;
958                 skb_pull(skb, ptask->max_payload);
959                 if (ptask->outstanding_pkts > 1) {
960                         fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG,
961                                           dg_size, fg_off, datagram_label);
962                 } else {
963                         fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG,
964                                           dg_size, fg_off, datagram_label);
965                         ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE;
966                 }
967                 fwnet_send_packet(ptask);
968         }
969
970         if (free)
971                 fwnet_free_ptask(ptask);
972 }
973
974 static void fwnet_write_complete(struct fw_card *card, int rcode,
975                                  void *payload, size_t length, void *data)
976 {
977         struct fwnet_packet_task *ptask;
978
979         ptask = data;
980
981         if (rcode == RCODE_COMPLETE)
982                 fwnet_transmit_packet_done(ptask);
983         else
984                 fw_error("fwnet_write_complete: failed: %x\n", rcode);
985                 /* ??? error recovery */
986 }
987
988 static int fwnet_send_packet(struct fwnet_packet_task *ptask)
989 {
990         struct fwnet_device *dev;
991         unsigned tx_len;
992         struct rfc2734_header *bufhdr;
993         unsigned long flags;
994         bool free;
995
996         dev = ptask->dev;
997         tx_len = ptask->max_payload;
998         switch (fwnet_get_hdr_lf(&ptask->hdr)) {
999         case RFC2374_HDR_UNFRAG:
1000                 bufhdr = (struct rfc2734_header *)
1001                                 skb_push(ptask->skb, RFC2374_UNFRAG_HDR_SIZE);
1002                 put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
1003                 break;
1004
1005         case RFC2374_HDR_FIRSTFRAG:
1006         case RFC2374_HDR_INTFRAG:
1007         case RFC2374_HDR_LASTFRAG:
1008                 bufhdr = (struct rfc2734_header *)
1009                                 skb_push(ptask->skb, RFC2374_FRAG_HDR_SIZE);
1010                 put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
1011                 put_unaligned_be32(ptask->hdr.w1, &bufhdr->w1);
1012                 break;
1013
1014         default:
1015                 BUG();
1016         }
1017         if (ptask->dest_node == IEEE1394_ALL_NODES) {
1018                 u8 *p;
1019                 int generation;
1020                 int node_id;
1021
1022                 /* ptask->generation may not have been set yet */
1023                 generation = dev->card->generation;
1024                 smp_rmb();
1025                 node_id = dev->card->node_id;
1026
1027                 p = skb_push(ptask->skb, 8);
1028                 put_unaligned_be32(node_id << 16 | IANA_SPECIFIER_ID >> 8, p);
1029                 put_unaligned_be32((IANA_SPECIFIER_ID & 0xff) << 24
1030                                                 | RFC2734_SW_VERSION, &p[4]);
1031
1032                 /* We should not transmit if broadcast_channel.valid == 0. */
1033                 fw_send_request(dev->card, &ptask->transaction,
1034                                 TCODE_STREAM_DATA,
1035                                 fw_stream_packet_destination_id(3,
1036                                                 IEEE1394_BROADCAST_CHANNEL, 0),
1037                                 generation, SCODE_100, 0ULL, ptask->skb->data,
1038                                 tx_len + 8, fwnet_write_complete, ptask);
1039
1040                 spin_lock_irqsave(&dev->lock, flags);
1041
1042                 /* If the AT tasklet already ran, we may be last user. */
1043                 free = (ptask->outstanding_pkts == 0 && list_empty(&ptask->pt_link));
1044                 if (!free)
1045                         list_add_tail(&ptask->pt_link, &dev->broadcasted_list);
1046
1047                 spin_unlock_irqrestore(&dev->lock, flags);
1048
1049                 goto out;
1050         }
1051
1052         fw_send_request(dev->card, &ptask->transaction,
1053                         TCODE_WRITE_BLOCK_REQUEST, ptask->dest_node,
1054                         ptask->generation, ptask->speed, ptask->fifo_addr,
1055                         ptask->skb->data, tx_len, fwnet_write_complete, ptask);
1056
1057         spin_lock_irqsave(&dev->lock, flags);
1058
1059         /* If the AT tasklet already ran, we may be last user. */
1060         free = (ptask->outstanding_pkts == 0 && list_empty(&ptask->pt_link));
1061         if (!free)
1062                 list_add_tail(&ptask->pt_link, &dev->sent_list);
1063
1064         spin_unlock_irqrestore(&dev->lock, flags);
1065
1066         dev->netdev->trans_start = jiffies;
1067  out:
1068         if (free)
1069                 fwnet_free_ptask(ptask);
1070
1071         return 0;
1072 }
1073
1074 static int fwnet_broadcast_start(struct fwnet_device *dev)
1075 {
1076         struct fw_iso_context *context;
1077         int retval;
1078         unsigned num_packets;
1079         unsigned max_receive;
1080         struct fw_iso_packet packet;
1081         unsigned long offset;
1082         unsigned u;
1083
1084         if (dev->local_fifo == FWNET_NO_FIFO_ADDR) {
1085                 /* outside OHCI posted write area? */
1086                 static const struct fw_address_region region = {
1087                         .start = 0xffff00000000ULL,
1088                         .end   = CSR_REGISTER_BASE,
1089                 };
1090
1091                 dev->handler.length = 4096;
1092                 dev->handler.address_callback = fwnet_receive_packet;
1093                 dev->handler.callback_data = dev;
1094
1095                 retval = fw_core_add_address_handler(&dev->handler, &region);
1096                 if (retval < 0)
1097                         goto failed_initial;
1098
1099                 dev->local_fifo = dev->handler.offset;
1100         }
1101
1102         max_receive = 1U << (dev->card->max_receive + 1);
1103         num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive;
1104
1105         if (!dev->broadcast_rcv_context) {
1106                 void **ptrptr;
1107
1108                 context = fw_iso_context_create(dev->card,
1109                     FW_ISO_CONTEXT_RECEIVE, IEEE1394_BROADCAST_CHANNEL,
1110                     dev->card->link_speed, 8, fwnet_receive_broadcast, dev);
1111                 if (IS_ERR(context)) {
1112                         retval = PTR_ERR(context);
1113                         goto failed_context_create;
1114                 }
1115
1116                 retval = fw_iso_buffer_init(&dev->broadcast_rcv_buffer,
1117                     dev->card, FWNET_ISO_PAGE_COUNT, DMA_FROM_DEVICE);
1118                 if (retval < 0)
1119                         goto failed_buffer_init;
1120
1121                 ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL);
1122                 if (!ptrptr) {
1123                         retval = -ENOMEM;
1124                         goto failed_ptrs_alloc;
1125                 }
1126
1127                 dev->broadcast_rcv_buffer_ptrs = ptrptr;
1128                 for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) {
1129                         void *ptr;
1130                         unsigned v;
1131
1132                         ptr = kmap(dev->broadcast_rcv_buffer.pages[u]);
1133                         for (v = 0; v < num_packets / FWNET_ISO_PAGE_COUNT; v++)
1134                                 *ptrptr++ = (void *)
1135                                                 ((char *)ptr + v * max_receive);
1136                 }
1137                 dev->broadcast_rcv_context = context;
1138         } else {
1139                 context = dev->broadcast_rcv_context;
1140         }
1141
1142         packet.payload_length = max_receive;
1143         packet.interrupt = 1;
1144         packet.skip = 0;
1145         packet.tag = 3;
1146         packet.sy = 0;
1147         packet.header_length = IEEE1394_GASP_HDR_SIZE;
1148         offset = 0;
1149
1150         for (u = 0; u < num_packets; u++) {
1151                 retval = fw_iso_context_queue(context, &packet,
1152                                 &dev->broadcast_rcv_buffer, offset);
1153                 if (retval < 0)
1154                         goto failed_rcv_queue;
1155
1156                 offset += max_receive;
1157         }
1158         dev->num_broadcast_rcv_ptrs = num_packets;
1159         dev->rcv_buffer_size = max_receive;
1160         dev->broadcast_rcv_next_ptr = 0U;
1161         retval = fw_iso_context_start(context, -1, 0,
1162                         FW_ISO_CONTEXT_MATCH_ALL_TAGS); /* ??? sync */
1163         if (retval < 0)
1164                 goto failed_rcv_queue;
1165
1166         /* FIXME: adjust it according to the min. speed of all known peers? */
1167         dev->broadcast_xmt_max_payload = IEEE1394_MAX_PAYLOAD_S100
1168                         - IEEE1394_GASP_HDR_SIZE - RFC2374_UNFRAG_HDR_SIZE;
1169         dev->broadcast_state = FWNET_BROADCAST_RUNNING;
1170
1171         return 0;
1172
1173  failed_rcv_queue:
1174         kfree(dev->broadcast_rcv_buffer_ptrs);
1175         dev->broadcast_rcv_buffer_ptrs = NULL;
1176  failed_ptrs_alloc:
1177         fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, dev->card);
1178  failed_buffer_init:
1179         fw_iso_context_destroy(context);
1180         dev->broadcast_rcv_context = NULL;
1181  failed_context_create:
1182         fw_core_remove_address_handler(&dev->handler);
1183  failed_initial:
1184         dev->local_fifo = FWNET_NO_FIFO_ADDR;
1185
1186         return retval;
1187 }
1188
1189 /* ifup */
1190 static int fwnet_open(struct net_device *net)
1191 {
1192         struct fwnet_device *dev = netdev_priv(net);
1193         int ret;
1194
1195         if (dev->broadcast_state == FWNET_BROADCAST_ERROR) {
1196                 ret = fwnet_broadcast_start(dev);
1197                 if (ret)
1198                         return ret;
1199         }
1200         netif_start_queue(net);
1201
1202         return 0;
1203 }
1204
1205 /* ifdown */
1206 static int fwnet_stop(struct net_device *net)
1207 {
1208         netif_stop_queue(net);
1209
1210         /* Deallocate iso context for use by other applications? */
1211
1212         return 0;
1213 }
1214
1215 static netdev_tx_t fwnet_tx(struct sk_buff *skb, struct net_device *net)
1216 {
1217         struct fwnet_header hdr_buf;
1218         struct fwnet_device *dev = netdev_priv(net);
1219         __be16 proto;
1220         u16 dest_node;
1221         unsigned max_payload;
1222         u16 dg_size;
1223         u16 *datagram_label_ptr;
1224         struct fwnet_packet_task *ptask;
1225         struct fwnet_peer *peer;
1226         unsigned long flags;
1227
1228         ptask = kmem_cache_alloc(fwnet_packet_task_cache, GFP_ATOMIC);
1229         if (ptask == NULL)
1230                 goto fail;
1231
1232         skb = skb_share_check(skb, GFP_ATOMIC);
1233         if (!skb)
1234                 goto fail;
1235
1236         /*
1237          * Make a copy of the driver-specific header.
1238          * We might need to rebuild the header on tx failure.
1239          */
1240         memcpy(&hdr_buf, skb->data, sizeof(hdr_buf));
1241         skb_pull(skb, sizeof(hdr_buf));
1242
1243         proto = hdr_buf.h_proto;
1244         dg_size = skb->len;
1245
1246         /* serialize access to peer, including peer->datagram_label */
1247         spin_lock_irqsave(&dev->lock, flags);
1248
1249         /*
1250          * Set the transmission type for the packet.  ARP packets and IP
1251          * broadcast packets are sent via GASP.
1252          */
1253         if (memcmp(hdr_buf.h_dest, net->broadcast, FWNET_ALEN) == 0
1254             || proto == htons(ETH_P_ARP)
1255             || (proto == htons(ETH_P_IP)
1256                 && IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
1257                 max_payload        = dev->broadcast_xmt_max_payload;
1258                 datagram_label_ptr = &dev->broadcast_xmt_datagramlabel;
1259
1260                 ptask->fifo_addr   = FWNET_NO_FIFO_ADDR;
1261                 ptask->generation  = 0;
1262                 ptask->dest_node   = IEEE1394_ALL_NODES;
1263                 ptask->speed       = SCODE_100;
1264         } else {
1265                 __be64 guid = get_unaligned((__be64 *)hdr_buf.h_dest);
1266                 u8 generation;
1267
1268                 peer = fwnet_peer_find_by_guid(dev, be64_to_cpu(guid));
1269                 if (!peer || peer->fifo == FWNET_NO_FIFO_ADDR)
1270                         goto fail_unlock;
1271
1272                 generation         = peer->generation;
1273                 dest_node          = peer->node_id;
1274                 max_payload        = peer->max_payload;
1275                 datagram_label_ptr = &peer->datagram_label;
1276
1277                 ptask->fifo_addr   = peer->fifo;
1278                 ptask->generation  = generation;
1279                 ptask->dest_node   = dest_node;
1280                 ptask->speed       = peer->speed;
1281         }
1282
1283         /* If this is an ARP packet, convert it */
1284         if (proto == htons(ETH_P_ARP)) {
1285                 struct arphdr *arp = (struct arphdr *)skb->data;
1286                 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
1287                 struct rfc2734_arp *arp1394 = (struct rfc2734_arp *)skb->data;
1288                 __be32 ipaddr;
1289
1290                 ipaddr = get_unaligned((__be32 *)(arp_ptr + FWNET_ALEN));
1291
1292                 arp1394->hw_addr_len    = RFC2734_HW_ADDR_LEN;
1293                 arp1394->max_rec        = dev->card->max_receive;
1294                 arp1394->sspd           = dev->card->link_speed;
1295
1296                 put_unaligned_be16(dev->local_fifo >> 32,
1297                                    &arp1394->fifo_hi);
1298                 put_unaligned_be32(dev->local_fifo & 0xffffffff,
1299                                    &arp1394->fifo_lo);
1300                 put_unaligned(ipaddr, &arp1394->sip);
1301         }
1302
1303         ptask->hdr.w0 = 0;
1304         ptask->hdr.w1 = 0;
1305         ptask->skb = skb;
1306         ptask->dev = dev;
1307
1308         /* Does it all fit in one packet? */
1309         if (dg_size <= max_payload) {
1310                 fwnet_make_uf_hdr(&ptask->hdr, ntohs(proto));
1311                 ptask->outstanding_pkts = 1;
1312                 max_payload = dg_size + RFC2374_UNFRAG_HDR_SIZE;
1313         } else {
1314                 u16 datagram_label;
1315
1316                 max_payload -= RFC2374_FRAG_OVERHEAD;
1317                 datagram_label = (*datagram_label_ptr)++;
1318                 fwnet_make_ff_hdr(&ptask->hdr, ntohs(proto), dg_size,
1319                                   datagram_label);
1320                 ptask->outstanding_pkts = DIV_ROUND_UP(dg_size, max_payload);
1321                 max_payload += RFC2374_FRAG_HDR_SIZE;
1322         }
1323
1324         spin_unlock_irqrestore(&dev->lock, flags);
1325
1326         ptask->max_payload = max_payload;
1327         INIT_LIST_HEAD(&ptask->pt_link);
1328
1329         fwnet_send_packet(ptask);
1330
1331         return NETDEV_TX_OK;
1332
1333  fail_unlock:
1334         spin_unlock_irqrestore(&dev->lock, flags);
1335  fail:
1336         if (ptask)
1337                 kmem_cache_free(fwnet_packet_task_cache, ptask);
1338
1339         if (skb != NULL)
1340                 dev_kfree_skb(skb);
1341
1342         net->stats.tx_dropped++;
1343         net->stats.tx_errors++;
1344
1345         /*
1346          * FIXME: According to a patch from 2003-02-26, "returning non-zero
1347          * causes serious problems" here, allegedly.  Before that patch,
1348          * -ERRNO was returned which is not appropriate under Linux 2.6.
1349          * Perhaps more needs to be done?  Stop the queue in serious
1350          * conditions and restart it elsewhere?
1351          */
1352         return NETDEV_TX_OK;
1353 }
1354
1355 static int fwnet_change_mtu(struct net_device *net, int new_mtu)
1356 {
1357         if (new_mtu < 68)
1358                 return -EINVAL;
1359
1360         net->mtu = new_mtu;
1361         return 0;
1362 }
1363
1364 static void fwnet_get_drvinfo(struct net_device *net,
1365                               struct ethtool_drvinfo *info)
1366 {
1367         strcpy(info->driver, KBUILD_MODNAME);
1368         strcpy(info->bus_info, "ieee1394");
1369 }
1370
1371 static const struct ethtool_ops fwnet_ethtool_ops = {
1372         .get_drvinfo = fwnet_get_drvinfo,
1373 };
1374
1375 static const struct net_device_ops fwnet_netdev_ops = {
1376         .ndo_open       = fwnet_open,
1377         .ndo_stop       = fwnet_stop,
1378         .ndo_start_xmit = fwnet_tx,
1379         .ndo_change_mtu = fwnet_change_mtu,
1380 };
1381
1382 static void fwnet_init_dev(struct net_device *net)
1383 {
1384         net->header_ops         = &fwnet_header_ops;
1385         net->netdev_ops         = &fwnet_netdev_ops;
1386         net->watchdog_timeo     = 2 * HZ;
1387         net->flags              = IFF_BROADCAST | IFF_MULTICAST;
1388         net->features           = NETIF_F_HIGHDMA;
1389         net->addr_len           = FWNET_ALEN;
1390         net->hard_header_len    = FWNET_HLEN;
1391         net->type               = ARPHRD_IEEE1394;
1392         net->tx_queue_len       = 10;
1393         SET_ETHTOOL_OPS(net, &fwnet_ethtool_ops);
1394 }
1395
1396 /* caller must hold fwnet_device_mutex */
1397 static struct fwnet_device *fwnet_dev_find(struct fw_card *card)
1398 {
1399         struct fwnet_device *dev;
1400
1401         list_for_each_entry(dev, &fwnet_device_list, dev_link)
1402                 if (dev->card == card)
1403                         return dev;
1404
1405         return NULL;
1406 }
1407
1408 static int fwnet_add_peer(struct fwnet_device *dev,
1409                           struct fw_unit *unit, struct fw_device *device)
1410 {
1411         struct fwnet_peer *peer;
1412
1413         peer = kmalloc(sizeof(*peer), GFP_KERNEL);
1414         if (!peer)
1415                 return -ENOMEM;
1416
1417         dev_set_drvdata(&unit->device, peer);
1418
1419         peer->dev = dev;
1420         peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
1421         peer->fifo = FWNET_NO_FIFO_ADDR;
1422         INIT_LIST_HEAD(&peer->pd_list);
1423         peer->pdg_size = 0;
1424         peer->datagram_label = 0;
1425         peer->speed = device->max_speed;
1426         peer->max_payload = fwnet_max_payload(device->max_rec, peer->speed);
1427
1428         peer->generation = device->generation;
1429         smp_rmb();
1430         peer->node_id = device->node_id;
1431
1432         spin_lock_irq(&dev->lock);
1433         list_add_tail(&peer->peer_link, &dev->peer_list);
1434         spin_unlock_irq(&dev->lock);
1435
1436         return 0;
1437 }
1438
1439 static int fwnet_probe(struct device *_dev)
1440 {
1441         struct fw_unit *unit = fw_unit(_dev);
1442         struct fw_device *device = fw_parent_device(unit);
1443         struct fw_card *card = device->card;
1444         struct net_device *net;
1445         bool allocated_netdev = false;
1446         struct fwnet_device *dev;
1447         unsigned max_mtu;
1448         int ret;
1449
1450         mutex_lock(&fwnet_device_mutex);
1451
1452         dev = fwnet_dev_find(card);
1453         if (dev) {
1454                 net = dev->netdev;
1455                 goto have_dev;
1456         }
1457
1458         net = alloc_netdev(sizeof(*dev), "firewire%d", fwnet_init_dev);
1459         if (net == NULL) {
1460                 ret = -ENOMEM;
1461                 goto out;
1462         }
1463
1464         allocated_netdev = true;
1465         SET_NETDEV_DEV(net, card->device);
1466         dev = netdev_priv(net);
1467
1468         spin_lock_init(&dev->lock);
1469         dev->broadcast_state = FWNET_BROADCAST_ERROR;
1470         dev->broadcast_rcv_context = NULL;
1471         dev->broadcast_xmt_max_payload = 0;
1472         dev->broadcast_xmt_datagramlabel = 0;
1473
1474         dev->local_fifo = FWNET_NO_FIFO_ADDR;
1475
1476         INIT_LIST_HEAD(&dev->packet_list);
1477         INIT_LIST_HEAD(&dev->broadcasted_list);
1478         INIT_LIST_HEAD(&dev->sent_list);
1479         INIT_LIST_HEAD(&dev->peer_list);
1480
1481         dev->card = card;
1482         dev->netdev = net;
1483
1484         /*
1485          * Use the RFC 2734 default 1500 octets or the maximum payload
1486          * as initial MTU
1487          */
1488         max_mtu = (1 << (card->max_receive + 1))
1489                   - sizeof(struct rfc2734_header) - IEEE1394_GASP_HDR_SIZE;
1490         net->mtu = min(1500U, max_mtu);
1491
1492         /* Set our hardware address while we're at it */
1493         put_unaligned_be64(card->guid, net->dev_addr);
1494         put_unaligned_be64(~0ULL, net->broadcast);
1495         ret = register_netdev(net);
1496         if (ret) {
1497                 fw_error("Cannot register the driver\n");
1498                 goto out;
1499         }
1500
1501         list_add_tail(&dev->dev_link, &fwnet_device_list);
1502         fw_notify("%s: IPv4 over FireWire on device %016llx\n",
1503                   net->name, (unsigned long long)card->guid);
1504  have_dev:
1505         ret = fwnet_add_peer(dev, unit, device);
1506         if (ret && allocated_netdev) {
1507                 unregister_netdev(net);
1508                 list_del(&dev->dev_link);
1509         }
1510  out:
1511         if (ret && allocated_netdev)
1512                 free_netdev(net);
1513
1514         mutex_unlock(&fwnet_device_mutex);
1515
1516         return ret;
1517 }
1518
1519 static void fwnet_remove_peer(struct fwnet_peer *peer)
1520 {
1521         struct fwnet_partial_datagram *pd, *pd_next;
1522
1523         spin_lock_irq(&peer->dev->lock);
1524         list_del(&peer->peer_link);
1525         spin_unlock_irq(&peer->dev->lock);
1526
1527         list_for_each_entry_safe(pd, pd_next, &peer->pd_list, pd_link)
1528                 fwnet_pd_delete(pd);
1529
1530         kfree(peer);
1531 }
1532
1533 static int fwnet_remove(struct device *_dev)
1534 {
1535         struct fwnet_peer *peer = dev_get_drvdata(_dev);
1536         struct fwnet_device *dev = peer->dev;
1537         struct net_device *net;
1538         struct fwnet_packet_task *ptask, *pt_next;
1539
1540         mutex_lock(&fwnet_device_mutex);
1541
1542         fwnet_remove_peer(peer);
1543
1544         if (list_empty(&dev->peer_list)) {
1545                 net = dev->netdev;
1546                 unregister_netdev(net);
1547
1548                 if (dev->local_fifo != FWNET_NO_FIFO_ADDR)
1549                         fw_core_remove_address_handler(&dev->handler);
1550                 if (dev->broadcast_rcv_context) {
1551                         fw_iso_context_stop(dev->broadcast_rcv_context);
1552                         fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer,
1553                                               dev->card);
1554                         fw_iso_context_destroy(dev->broadcast_rcv_context);
1555                 }
1556                 list_for_each_entry_safe(ptask, pt_next,
1557                                          &dev->packet_list, pt_link) {
1558                         dev_kfree_skb_any(ptask->skb);
1559                         kmem_cache_free(fwnet_packet_task_cache, ptask);
1560                 }
1561                 list_for_each_entry_safe(ptask, pt_next,
1562                                          &dev->broadcasted_list, pt_link) {
1563                         dev_kfree_skb_any(ptask->skb);
1564                         kmem_cache_free(fwnet_packet_task_cache, ptask);
1565                 }
1566                 list_for_each_entry_safe(ptask, pt_next,
1567                                          &dev->sent_list, pt_link) {
1568                         dev_kfree_skb_any(ptask->skb);
1569                         kmem_cache_free(fwnet_packet_task_cache, ptask);
1570                 }
1571                 list_del(&dev->dev_link);
1572
1573                 free_netdev(net);
1574         }
1575
1576         mutex_unlock(&fwnet_device_mutex);
1577
1578         return 0;
1579 }
1580
1581 /*
1582  * FIXME abort partially sent fragmented datagrams,
1583  * discard partially received fragmented datagrams
1584  */
1585 static void fwnet_update(struct fw_unit *unit)
1586 {
1587         struct fw_device *device = fw_parent_device(unit);
1588         struct fwnet_peer *peer = dev_get_drvdata(&unit->device);
1589         int generation;
1590
1591         generation = device->generation;
1592
1593         spin_lock_irq(&peer->dev->lock);
1594         peer->node_id    = device->node_id;
1595         peer->generation = generation;
1596         spin_unlock_irq(&peer->dev->lock);
1597 }
1598
1599 static const struct ieee1394_device_id fwnet_id_table[] = {
1600         {
1601                 .match_flags  = IEEE1394_MATCH_SPECIFIER_ID |
1602                                 IEEE1394_MATCH_VERSION,
1603                 .specifier_id = IANA_SPECIFIER_ID,
1604                 .version      = RFC2734_SW_VERSION,
1605         },
1606         { }
1607 };
1608
1609 static struct fw_driver fwnet_driver = {
1610         .driver = {
1611                 .owner  = THIS_MODULE,
1612                 .name   = "net",
1613                 .bus    = &fw_bus_type,
1614                 .probe  = fwnet_probe,
1615                 .remove = fwnet_remove,
1616         },
1617         .update   = fwnet_update,
1618         .id_table = fwnet_id_table,
1619 };
1620
1621 static const u32 rfc2374_unit_directory_data[] = {
1622         0x00040000,     /* directory_length             */
1623         0x1200005e,     /* unit_specifier_id: IANA      */
1624         0x81000003,     /* textual descriptor offset    */
1625         0x13000001,     /* unit_sw_version: RFC 2734    */
1626         0x81000005,     /* textual descriptor offset    */
1627         0x00030000,     /* descriptor_length            */
1628         0x00000000,     /* text                         */
1629         0x00000000,     /* minimal ASCII, en            */
1630         0x49414e41,     /* I A N A                      */
1631         0x00030000,     /* descriptor_length            */
1632         0x00000000,     /* text                         */
1633         0x00000000,     /* minimal ASCII, en            */
1634         0x49507634,     /* I P v 4                      */
1635 };
1636
1637 static struct fw_descriptor rfc2374_unit_directory = {
1638         .length = ARRAY_SIZE(rfc2374_unit_directory_data),
1639         .key    = (CSR_DIRECTORY | CSR_UNIT) << 24,
1640         .data   = rfc2374_unit_directory_data
1641 };
1642
1643 static int __init fwnet_init(void)
1644 {
1645         int err;
1646
1647         err = fw_core_add_descriptor(&rfc2374_unit_directory);
1648         if (err)
1649                 return err;
1650
1651         fwnet_packet_task_cache = kmem_cache_create("packet_task",
1652                         sizeof(struct fwnet_packet_task), 0, 0, NULL);
1653         if (!fwnet_packet_task_cache) {
1654                 err = -ENOMEM;
1655                 goto out;
1656         }
1657
1658         err = driver_register(&fwnet_driver.driver);
1659         if (!err)
1660                 return 0;
1661
1662         kmem_cache_destroy(fwnet_packet_task_cache);
1663 out:
1664         fw_core_remove_descriptor(&rfc2374_unit_directory);
1665
1666         return err;
1667 }
1668 module_init(fwnet_init);
1669
1670 static void __exit fwnet_cleanup(void)
1671 {
1672         driver_unregister(&fwnet_driver.driver);
1673         kmem_cache_destroy(fwnet_packet_task_cache);
1674         fw_core_remove_descriptor(&rfc2374_unit_directory);
1675 }
1676 module_exit(fwnet_cleanup);
1677
1678 MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>");
1679 MODULE_DESCRIPTION("IPv4 over IEEE1394 as per RFC 2734");
1680 MODULE_LICENSE("GPL");
1681 MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table);