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