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