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