net.c 41.9 KB
Newer Older
Jay Fenlason's avatar
Jay Fenlason committed
1
2
/*
 * IPv4 over IEEE 1394, per RFC 2734
3
 * IPv6 over IEEE 1394, per RFC 3146
Jay Fenlason's avatar
Jay Fenlason committed
4
5
6
7
8
9
 *
 * Copyright (C) 2009 Jay Fenlason <fenlason@redhat.com>
 *
 * based on eth1394 by Ben Collins et al
 */

Stefan Richter's avatar
Stefan Richter committed
10
#include <linux/bug.h>
11
#include <linux/compiler.h>
12
#include <linux/delay.h>
Jay Fenlason's avatar
Jay Fenlason committed
13
#include <linux/device.h>
14
#include <linux/ethtool.h>
Jay Fenlason's avatar
Jay Fenlason committed
15
16
17
18
19
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/highmem.h>
#include <linux/in.h>
#include <linux/ip.h>
Stefan Richter's avatar
Stefan Richter committed
20
#include <linux/jiffies.h>
Jay Fenlason's avatar
Jay Fenlason committed
21
22
23
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
24
#include <linux/mutex.h>
Jay Fenlason's avatar
Jay Fenlason committed
25
26
#include <linux/netdevice.h>
#include <linux/skbuff.h>
27
#include <linux/slab.h>
28
#include <linux/spinlock.h>
Jay Fenlason's avatar
Jay Fenlason committed
29
30
31

#include <asm/unaligned.h>
#include <net/arp.h>
32
#include <net/firewire.h>
Jay Fenlason's avatar
Jay Fenlason committed
33

34
35
36
37
38
39
40
41
/* rx limits */
#define FWNET_MAX_FRAGMENTS		30 /* arbitrary, > TX queue depth */
#define FWNET_ISO_PAGE_COUNT		(PAGE_SIZE < 16*1024 ? 4 : 2)

/* tx limits */
#define FWNET_MAX_QUEUED_DATAGRAMS	20 /* < 64 = number of tlabels */
#define FWNET_MIN_QUEUED_DATAGRAMS	10 /* should keep AT DMA busy enough */
#define FWNET_TX_QUEUE_LEN		FWNET_MAX_QUEUED_DATAGRAMS /* ? */
Jay Fenlason's avatar
Jay Fenlason committed
42

Stefan Richter's avatar
Stefan Richter committed
43
44
45
46
#define IEEE1394_BROADCAST_CHANNEL	31
#define IEEE1394_ALL_NODES		(0xffc0 | 0x003f)
#define IEEE1394_MAX_PAYLOAD_S100	512
#define FWNET_NO_FIFO_ADDR		(~0ULL)
Jay Fenlason's avatar
Jay Fenlason committed
47

Stefan Richter's avatar
Stefan Richter committed
48
49
#define IANA_SPECIFIER_ID		0x00005eU
#define RFC2734_SW_VERSION		0x000001U
50
#define RFC3146_SW_VERSION		0x000002U
Jay Fenlason's avatar
Jay Fenlason committed
51

Stefan Richter's avatar
Stefan Richter committed
52
#define IEEE1394_GASP_HDR_SIZE	8
Jay Fenlason's avatar
Jay Fenlason committed
53

Stefan Richter's avatar
Stefan Richter committed
54
55
56
#define RFC2374_UNFRAG_HDR_SIZE	4
#define RFC2374_FRAG_HDR_SIZE	8
#define RFC2374_FRAG_OVERHEAD	4
Jay Fenlason's avatar
Jay Fenlason committed
57

Stefan Richter's avatar
Stefan Richter committed
58
59
60
61
#define RFC2374_HDR_UNFRAG	0	/* unfragmented		*/
#define RFC2374_HDR_FIRSTFRAG	1	/* first fragment	*/
#define RFC2374_HDR_LASTFRAG	2	/* last fragment	*/
#define RFC2374_HDR_INTFRAG	3	/* interior fragment	*/
Jay Fenlason's avatar
Jay Fenlason committed
62

63
64
65
66
67
static bool fwnet_hwaddr_is_multicast(u8 *ha)
{
	return !!(*ha & 1);
}

Stefan Richter's avatar
Stefan Richter committed
68
69
/* IPv4 and IPv6 encapsulation header */
struct rfc2734_header {
Jay Fenlason's avatar
Jay Fenlason committed
70
71
72
73
	u32 w0;
	u32 w1;
};

Stefan Richter's avatar
Stefan Richter committed
74
75
#define fwnet_get_hdr_lf(h)		(((h)->w0 & 0xc0000000) >> 30)
#define fwnet_get_hdr_ether_type(h)	(((h)->w0 & 0x0000ffff))
76
#define fwnet_get_hdr_dg_size(h)	((((h)->w0 & 0x0fff0000) >> 16) + 1)
Stefan Richter's avatar
Stefan Richter committed
77
78
#define fwnet_get_hdr_fg_off(h)		(((h)->w0 & 0x00000fff))
#define fwnet_get_hdr_dgl(h)		(((h)->w1 & 0xffff0000) >> 16)
Jay Fenlason's avatar
Jay Fenlason committed
79

80
#define fwnet_set_hdr_lf(lf)		((lf) << 30)
Stefan Richter's avatar
Stefan Richter committed
81
#define fwnet_set_hdr_ether_type(et)	(et)
82
#define fwnet_set_hdr_dg_size(dgs)	(((dgs) - 1) << 16)
Stefan Richter's avatar
Stefan Richter committed
83
#define fwnet_set_hdr_fg_off(fgo)	(fgo)
Jay Fenlason's avatar
Jay Fenlason committed
84

Stefan Richter's avatar
Stefan Richter committed
85
#define fwnet_set_hdr_dgl(dgl)		((dgl) << 16)
Jay Fenlason's avatar
Jay Fenlason committed
86

Stefan Richter's avatar
Stefan Richter committed
87
88
89
90
91
92
static inline void fwnet_make_uf_hdr(struct rfc2734_header *hdr,
		unsigned ether_type)
{
	hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_UNFRAG)
		  | fwnet_set_hdr_ether_type(ether_type);
}
Jay Fenlason's avatar
Jay Fenlason committed
93

Stefan Richter's avatar
Stefan Richter committed
94
95
96
97
98
99
100
101
static inline void fwnet_make_ff_hdr(struct rfc2734_header *hdr,
		unsigned ether_type, unsigned dg_size, unsigned dgl)
{
	hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_FIRSTFRAG)
		  | fwnet_set_hdr_dg_size(dg_size)
		  | fwnet_set_hdr_ether_type(ether_type);
	hdr->w1 = fwnet_set_hdr_dgl(dgl);
}
Jay Fenlason's avatar
Jay Fenlason committed
102

Stefan Richter's avatar
Stefan Richter committed
103
104
105
106
107
108
109
110
static inline void fwnet_make_sf_hdr(struct rfc2734_header *hdr,
		unsigned lf, unsigned dg_size, unsigned fg_off, unsigned dgl)
{
	hdr->w0 = fwnet_set_hdr_lf(lf)
		  | fwnet_set_hdr_dg_size(dg_size)
		  | fwnet_set_hdr_fg_off(fg_off);
	hdr->w1 = fwnet_set_hdr_dgl(dgl);
}
Jay Fenlason's avatar
Jay Fenlason committed
111
112

/* This list keeps track of what parts of the datagram have been filled in */
Stefan Richter's avatar
Stefan Richter committed
113
114
struct fwnet_fragment_info {
	struct list_head fi_link;
Jay Fenlason's avatar
Jay Fenlason committed
115
116
117
118
	u16 offset;
	u16 len;
};

Stefan Richter's avatar
Stefan Richter committed
119
120
121
struct fwnet_partial_datagram {
	struct list_head pd_link;
	struct list_head fi_list;
Jay Fenlason's avatar
Jay Fenlason committed
122
123
124
125
126
127
128
129
	struct sk_buff *skb;
	/* FIXME Why not use skb->data? */
	char *pbuf;
	u16 datagram_label;
	u16 ether_type;
	u16 datagram_size;
};

130
131
static DEFINE_MUTEX(fwnet_device_mutex);
static LIST_HEAD(fwnet_device_list);
Jay Fenlason's avatar
Jay Fenlason committed
132

Stefan Richter's avatar
Stefan Richter committed
133
struct fwnet_device {
134
	struct list_head dev_link;
Jay Fenlason's avatar
Jay Fenlason committed
135
	spinlock_t lock;
Stefan Richter's avatar
Stefan Richter committed
136
137
138
139
140
	enum {
		FWNET_BROADCAST_ERROR,
		FWNET_BROADCAST_RUNNING,
		FWNET_BROADCAST_STOPPED,
	} broadcast_state;
Jay Fenlason's avatar
Jay Fenlason committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
	struct fw_iso_context *broadcast_rcv_context;
	struct fw_iso_buffer broadcast_rcv_buffer;
	void **broadcast_rcv_buffer_ptrs;
	unsigned broadcast_rcv_next_ptr;
	unsigned num_broadcast_rcv_ptrs;
	unsigned rcv_buffer_size;
	/*
	 * This value is the maximum unfragmented datagram size that can be
	 * sent by the hardware.  It already has the GASP overhead and the
	 * unfragmented datagram header overhead calculated into it.
	 */
	unsigned broadcast_xmt_max_payload;
	u16 broadcast_xmt_datagramlabel;

	/*
Stefan Richter's avatar
Stefan Richter committed
156
	 * The CSR address that remote nodes must send datagrams to for us to
Jay Fenlason's avatar
Jay Fenlason committed
157
158
159
160
161
	 * receive them.
	 */
	struct fw_address_handler handler;
	u64 local_fifo;

162
163
	/* Number of tx datagrams that have been queued but not yet acked */
	int queued_datagrams;
Jay Fenlason's avatar
Jay Fenlason committed
164

165
	int peer_count;
166
	struct list_head peer_list;
Jay Fenlason's avatar
Jay Fenlason committed
167
	struct fw_card *card;
168
169
170
171
172
173
174
175
176
177
178
179
180
	struct net_device *netdev;
};

struct fwnet_peer {
	struct list_head peer_link;
	struct fwnet_device *dev;
	u64 guid;

	/* guarded by dev->lock */
	struct list_head pd_list; /* received partial datagrams */
	unsigned pdg_size;        /* pd_list size */

	u16 datagram_label;       /* outgoing datagram label */
181
	u16 max_payload;          /* includes RFC2374_FRAG_HDR_SIZE overhead */
182
183
184
	int node_id;
	int generation;
	unsigned speed;
Jay Fenlason's avatar
Jay Fenlason committed
185
186
187
};

/* This is our task struct. It's used for the packet complete callback.  */
Stefan Richter's avatar
Stefan Richter committed
188
struct fwnet_packet_task {
Jay Fenlason's avatar
Jay Fenlason committed
189
	struct fw_transaction transaction;
Stefan Richter's avatar
Stefan Richter committed
190
	struct rfc2734_header hdr;
Jay Fenlason's avatar
Jay Fenlason committed
191
	struct sk_buff *skb;
Stefan Richter's avatar
Stefan Richter committed
192
193
	struct fwnet_device *dev;

Jay Fenlason's avatar
Jay Fenlason committed
194
195
196
	int outstanding_pkts;
	u64 fifo_addr;
	u16 dest_node;
197
	u16 max_payload;
Jay Fenlason's avatar
Jay Fenlason committed
198
199
	u8 generation;
	u8 speed;
200
	u8 enqueued;
Jay Fenlason's avatar
Jay Fenlason committed
201
202
};

203
204
205
206
207
208
209
210
211
/*
 * Get fifo address embedded in hwaddr
 */
static __u64 fwnet_hwaddr_fifo(union fwnet_hwaddr *ha)
{
	return (u64)get_unaligned_be16(&ha->uc.fifo_hi) << 32
	       | get_unaligned_be32(&ha->uc.fifo_lo);
}

Stefan Richter's avatar
Stefan Richter committed
212
213
214
215
216
217
218
219
220
/*
 * saddr == NULL means use device source address.
 * daddr == NULL means leave destination address (eg unresolved arp).
 */
static int fwnet_header_create(struct sk_buff *skb, struct net_device *net,
			unsigned short type, const void *daddr,
			const void *saddr, unsigned len)
{
	struct fwnet_header *h;
Jay Fenlason's avatar
Jay Fenlason committed
221

Stefan Richter's avatar
Stefan Richter committed
222
223
	h = (struct fwnet_header *)skb_push(skb, sizeof(*h));
	put_unaligned_be16(type, &h->h_proto);
Jay Fenlason's avatar
Jay Fenlason committed
224

Stefan Richter's avatar
Stefan Richter committed
225
226
	if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) {
		memset(h->h_dest, 0, net->addr_len);
Jay Fenlason's avatar
Jay Fenlason committed
227

Stefan Richter's avatar
Stefan Richter committed
228
		return net->hard_header_len;
Jay Fenlason's avatar
Jay Fenlason committed
229
230
231
	}

	if (daddr) {
Stefan Richter's avatar
Stefan Richter committed
232
233
234
		memcpy(h->h_dest, daddr, net->addr_len);

		return net->hard_header_len;
Jay Fenlason's avatar
Jay Fenlason committed
235
236
	}

Stefan Richter's avatar
Stefan Richter committed
237
	return -net->hard_header_len;
Jay Fenlason's avatar
Jay Fenlason committed
238
239
}

Stefan Richter's avatar
Stefan Richter committed
240
static int fwnet_header_cache(const struct neighbour *neigh,
241
			      struct hh_cache *hh, __be16 type)
Stefan Richter's avatar
Stefan Richter committed
242
243
244
{
	struct net_device *net;
	struct fwnet_header *h;
Jay Fenlason's avatar
Jay Fenlason committed
245

246
	if (type == cpu_to_be16(ETH_P_802_3))
Jay Fenlason's avatar
Jay Fenlason committed
247
		return -1;
Stefan Richter's avatar
Stefan Richter committed
248
	net = neigh->dev;
249
	h = (struct fwnet_header *)((u8 *)hh->hh_data + HH_DATA_OFF(sizeof(*h)));
250
	h->h_proto = type;
Stefan Richter's avatar
Stefan Richter committed
251
252
	memcpy(h->h_dest, neigh->ha, net->addr_len);
	hh->hh_len = FWNET_HLEN;
Jay Fenlason's avatar
Jay Fenlason committed
253
254
255
256
257

	return 0;
}

/* Called by Address Resolution module to notify changes in address. */
Stefan Richter's avatar
Stefan Richter committed
258
259
260
static void fwnet_header_cache_update(struct hh_cache *hh,
		const struct net_device *net, const unsigned char *haddr)
{
261
	memcpy((u8 *)hh->hh_data + HH_DATA_OFF(FWNET_HLEN), haddr, net->addr_len);
Jay Fenlason's avatar
Jay Fenlason committed
262
263
}

Stefan Richter's avatar
Stefan Richter committed
264
265
266
267
268
static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{
	memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN);

	return FWNET_ALEN;
Jay Fenlason's avatar
Jay Fenlason committed
269
270
}

Stefan Richter's avatar
Stefan Richter committed
271
272
273
274
275
static const struct header_ops fwnet_header_ops = {
	.create         = fwnet_header_create,
	.cache		= fwnet_header_cache,
	.cache_update	= fwnet_header_cache_update,
	.parse          = fwnet_header_parse,
Jay Fenlason's avatar
Jay Fenlason committed
276
277
278
};

/* FIXME: is this correct for all cases? */
Stefan Richter's avatar
Stefan Richter committed
279
280
static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd,
			       unsigned offset, unsigned len)
Jay Fenlason's avatar
Jay Fenlason committed
281
{
Stefan Richter's avatar
Stefan Richter committed
282
	struct fwnet_fragment_info *fi;
Jay Fenlason's avatar
Jay Fenlason committed
283
284
	unsigned end = offset + len;

Stefan Richter's avatar
Stefan Richter committed
285
286
	list_for_each_entry(fi, &pd->fi_list, fi_link)
		if (offset < fi->offset + fi->len && end > fi->offset)
Jay Fenlason's avatar
Jay Fenlason committed
287
			return true;
Stefan Richter's avatar
Stefan Richter committed
288

Jay Fenlason's avatar
Jay Fenlason committed
289
290
291
292
	return false;
}

/* Assumes that new fragment does not overlap any existing fragments */
Stefan Richter's avatar
Stefan Richter committed
293
294
295
296
static struct fwnet_fragment_info *fwnet_frag_new(
	struct fwnet_partial_datagram *pd, unsigned offset, unsigned len)
{
	struct fwnet_fragment_info *fi, *fi2, *new;
Jay Fenlason's avatar
Jay Fenlason committed
297
298
	struct list_head *list;

Stefan Richter's avatar
Stefan Richter committed
299
300
	list = &pd->fi_list;
	list_for_each_entry(fi, &pd->fi_list, fi_link) {
Jay Fenlason's avatar
Jay Fenlason committed
301
302
303
		if (fi->offset + fi->len == offset) {
			/* The new fragment can be tacked on to the end */
			/* Did the new fragment plug a hole? */
Stefan Richter's avatar
Stefan Richter committed
304
305
			fi2 = list_entry(fi->fi_link.next,
					 struct fwnet_fragment_info, fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
306
307
308
			if (fi->offset + fi->len == fi2->offset) {
				/* glue fragments together */
				fi->len += len + fi2->len;
Stefan Richter's avatar
Stefan Richter committed
309
				list_del(&fi2->fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
310
311
312
313
				kfree(fi2);
			} else {
				fi->len += len;
			}
Stefan Richter's avatar
Stefan Richter committed
314

Jay Fenlason's avatar
Jay Fenlason committed
315
316
317
318
319
			return fi;
		}
		if (offset + len == fi->offset) {
			/* The new fragment can be tacked on to the beginning */
			/* Did the new fragment plug a hole? */
Stefan Richter's avatar
Stefan Richter committed
320
321
			fi2 = list_entry(fi->fi_link.prev,
					 struct fwnet_fragment_info, fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
322
323
324
			if (fi2->offset + fi2->len == fi->offset) {
				/* glue fragments together */
				fi2->len += fi->len + len;
Stefan Richter's avatar
Stefan Richter committed
325
				list_del(&fi->fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
326
				kfree(fi);
Stefan Richter's avatar
Stefan Richter committed
327

Jay Fenlason's avatar
Jay Fenlason committed
328
329
330
331
				return fi2;
			}
			fi->offset = offset;
			fi->len += len;
Stefan Richter's avatar
Stefan Richter committed
332

Jay Fenlason's avatar
Jay Fenlason committed
333
334
335
			return fi;
		}
		if (offset > fi->offset + fi->len) {
Stefan Richter's avatar
Stefan Richter committed
336
			list = &fi->fi_link;
Jay Fenlason's avatar
Jay Fenlason committed
337
338
339
			break;
		}
		if (offset + len < fi->offset) {
Stefan Richter's avatar
Stefan Richter committed
340
			list = fi->fi_link.prev;
Jay Fenlason's avatar
Jay Fenlason committed
341
342
343
344
345
			break;
		}
	}

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
346
	if (!new)
Jay Fenlason's avatar
Jay Fenlason committed
347
348
349
350
		return NULL;

	new->offset = offset;
	new->len = len;
Stefan Richter's avatar
Stefan Richter committed
351
352
	list_add(&new->fi_link, list);

Jay Fenlason's avatar
Jay Fenlason committed
353
354
355
	return new;
}

Stefan Richter's avatar
Stefan Richter committed
356
357
358
359
360
361
static struct fwnet_partial_datagram *fwnet_pd_new(struct net_device *net,
		struct fwnet_peer *peer, u16 datagram_label, unsigned dg_size,
		void *frag_buf, unsigned frag_off, unsigned frag_len)
{
	struct fwnet_partial_datagram *new;
	struct fwnet_fragment_info *fi;
Jay Fenlason's avatar
Jay Fenlason committed
362
363
364
365

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
	if (!new)
		goto fail;
Stefan Richter's avatar
Stefan Richter committed
366
367
368
369

	INIT_LIST_HEAD(&new->fi_list);
	fi = fwnet_frag_new(new, frag_off, frag_len);
	if (fi == NULL)
Jay Fenlason's avatar
Jay Fenlason committed
370
		goto fail_w_new;
Stefan Richter's avatar
Stefan Richter committed
371

Jay Fenlason's avatar
Jay Fenlason committed
372
373
	new->datagram_label = datagram_label;
	new->datagram_size = dg_size;
374
	new->skb = dev_alloc_skb(dg_size + LL_RESERVED_SPACE(net));
Stefan Richter's avatar
Stefan Richter committed
375
	if (new->skb == NULL)
Jay Fenlason's avatar
Jay Fenlason committed
376
		goto fail_w_fi;
Stefan Richter's avatar
Stefan Richter committed
377

378
	skb_reserve(new->skb, LL_RESERVED_SPACE(net));
Jay Fenlason's avatar
Jay Fenlason committed
379
380
	new->pbuf = skb_put(new->skb, dg_size);
	memcpy(new->pbuf + frag_off, frag_buf, frag_len);
Stefan Richter's avatar
Stefan Richter committed
381
382
	list_add_tail(&new->pd_link, &peer->pd_list);

Jay Fenlason's avatar
Jay Fenlason committed
383
384
385
386
387
388
389
390
391
392
	return new;

fail_w_fi:
	kfree(fi);
fail_w_new:
	kfree(new);
fail:
	return NULL;
}

Stefan Richter's avatar
Stefan Richter committed
393
394
395
396
static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer,
						    u16 datagram_label)
{
	struct fwnet_partial_datagram *pd;
Jay Fenlason's avatar
Jay Fenlason committed
397

Stefan Richter's avatar
Stefan Richter committed
398
399
	list_for_each_entry(pd, &peer->pd_list, pd_link)
		if (pd->datagram_label == datagram_label)
Jay Fenlason's avatar
Jay Fenlason committed
400
			return pd;
Stefan Richter's avatar
Stefan Richter committed
401

Jay Fenlason's avatar
Jay Fenlason committed
402
403
404
405
	return NULL;
}


Stefan Richter's avatar
Stefan Richter committed
406
407
408
static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
{
	struct fwnet_fragment_info *fi, *n;
Jay Fenlason's avatar
Jay Fenlason committed
409

Stefan Richter's avatar
Stefan Richter committed
410
	list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
Jay Fenlason's avatar
Jay Fenlason committed
411
		kfree(fi);
Stefan Richter's avatar
Stefan Richter committed
412
413

	list_del(&old->pd_link);
Jay Fenlason's avatar
Jay Fenlason committed
414
415
416
417
	dev_kfree_skb_any(old->skb);
	kfree(old);
}

Stefan Richter's avatar
Stefan Richter committed
418
419
420
421
422
static bool fwnet_pd_update(struct fwnet_peer *peer,
		struct fwnet_partial_datagram *pd, void *frag_buf,
		unsigned frag_off, unsigned frag_len)
{
	if (fwnet_frag_new(pd, frag_off, frag_len) == NULL)
Jay Fenlason's avatar
Jay Fenlason committed
423
		return false;
Stefan Richter's avatar
Stefan Richter committed
424

Jay Fenlason's avatar
Jay Fenlason committed
425
426
427
	memcpy(pd->pbuf + frag_off, frag_buf, frag_len);

	/*
Lucas De Marchi's avatar
Lucas De Marchi committed
428
	 * Move list entry to beginning of list so that oldest partial
Jay Fenlason's avatar
Jay Fenlason committed
429
430
	 * datagrams percolate to the end of the list
	 */
Stefan Richter's avatar
Stefan Richter committed
431
432
	list_move_tail(&pd->pd_link, &peer->pd_list);

Jay Fenlason's avatar
Jay Fenlason committed
433
434
435
	return true;
}

Stefan Richter's avatar
Stefan Richter committed
436
437
438
static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
{
	struct fwnet_fragment_info *fi;
Jay Fenlason's avatar
Jay Fenlason committed
439

Stefan Richter's avatar
Stefan Richter committed
440
	fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
441

Stefan Richter's avatar
Stefan Richter committed
442
	return fi->len == pd->datagram_size;
Jay Fenlason's avatar
Jay Fenlason committed
443
444
}

445
/* caller must hold dev->lock */
Stefan Richter's avatar
Stefan Richter committed
446
447
448
static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
						  u64 guid)
{
449
	struct fwnet_peer *peer;
Jay Fenlason's avatar
Jay Fenlason committed
450

451
452
453
	list_for_each_entry(peer, &dev->peer_list, peer_link)
		if (peer->guid == guid)
			return peer;
Jay Fenlason's avatar
Jay Fenlason committed
454

455
	return NULL;
Jay Fenlason's avatar
Jay Fenlason committed
456
457
}

458
/* caller must hold dev->lock */
Stefan Richter's avatar
Stefan Richter committed
459
static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
460
						int node_id, int generation)
Stefan Richter's avatar
Stefan Richter committed
461
{
462
	struct fwnet_peer *peer;
Jay Fenlason's avatar
Jay Fenlason committed
463

464
465
466
467
	list_for_each_entry(peer, &dev->peer_list, peer_link)
		if (peer->node_id    == node_id &&
		    peer->generation == generation)
			return peer;
Stefan Richter's avatar
Stefan Richter committed
468

469
	return NULL;
Jay Fenlason's avatar
Jay Fenlason committed
470
471
}

472
473
/* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */
static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed)
Stefan Richter's avatar
Stefan Richter committed
474
{
475
	max_rec = min(max_rec, speed + 8);
476
	max_rec = clamp(max_rec, 8U, 11U); /* 512...4096 */
477
478

	return (1 << (max_rec + 1)) - RFC2374_FRAG_HDR_SIZE;
Jay Fenlason's avatar
Jay Fenlason committed
479
480
}

481

Stefan Richter's avatar
Stefan Richter committed
482
483
484
485
486
static int fwnet_finish_incoming_packet(struct net_device *net,
					struct sk_buff *skb, u16 source_node_id,
					bool is_broadcast, u16 ether_type)
{
	struct fwnet_device *dev;
Jay Fenlason's avatar
Jay Fenlason committed
487
	int status;
Stefan Richter's avatar
Stefan Richter committed
488
	__be64 guid;
Jay Fenlason's avatar
Jay Fenlason committed
489

490
491
492
	switch (ether_type) {
	case ETH_P_ARP:
	case ETH_P_IP:
493
494
495
#if IS_ENABLED(CONFIG_IPV6)
	case ETH_P_IPV6:
#endif
496
497
498
499
500
		break;
	default:
		goto err;
	}

Stefan Richter's avatar
Stefan Richter committed
501
	dev = netdev_priv(net);
Jay Fenlason's avatar
Jay Fenlason committed
502
	/* Write metadata, and then pass to the receive level */
Stefan Richter's avatar
Stefan Richter committed
503
	skb->dev = net;
504
	skb->ip_summed = CHECKSUM_NONE;
Jay Fenlason's avatar
Jay Fenlason committed
505
506
507

	/*
	 * Parse the encapsulation header. This actually does the job of
508
	 * converting to an ethernet-like pseudo frame header.
Jay Fenlason's avatar
Jay Fenlason committed
509
	 */
Stefan Richter's avatar
Stefan Richter committed
510
511
	guid = cpu_to_be64(dev->card->guid);
	if (dev_hard_header(skb, net, ether_type,
512
			   is_broadcast ? net->broadcast : net->dev_addr,
Stefan Richter's avatar
Stefan Richter committed
513
514
			   NULL, skb->len) >= 0) {
		struct fwnet_header *eth;
Jay Fenlason's avatar
Jay Fenlason committed
515
516
517
518
519
		u16 *rawp;
		__be16 protocol;

		skb_reset_mac_header(skb);
		skb_pull(skb, sizeof(*eth));
Stefan Richter's avatar
Stefan Richter committed
520
		eth = (struct fwnet_header *)skb_mac_header(skb);
521
		if (fwnet_hwaddr_is_multicast(eth->h_dest)) {
Stefan Richter's avatar
Stefan Richter committed
522
523
			if (memcmp(eth->h_dest, net->broadcast,
				   net->addr_len) == 0)
Jay Fenlason's avatar
Jay Fenlason committed
524
525
526
527
528
529
				skb->pkt_type = PACKET_BROADCAST;
#if 0
			else
				skb->pkt_type = PACKET_MULTICAST;
#endif
		} else {
530
			if (memcmp(eth->h_dest, net->dev_addr, net->addr_len))
Jay Fenlason's avatar
Jay Fenlason committed
531
532
				skb->pkt_type = PACKET_OTHERHOST;
		}
Simon Horman's avatar
Simon Horman committed
533
		if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) {
Jay Fenlason's avatar
Jay Fenlason committed
534
535
536
			protocol = eth->h_proto;
		} else {
			rawp = (u16 *)skb->data;
Stefan Richter's avatar
Stefan Richter committed
537
			if (*rawp == 0xffff)
Jay Fenlason's avatar
Jay Fenlason committed
538
				protocol = htons(ETH_P_802_3);
Stefan Richter's avatar
Stefan Richter committed
539
			else
Jay Fenlason's avatar
Jay Fenlason committed
540
541
542
543
544
				protocol = htons(ETH_P_802_2);
		}
		skb->protocol = protocol;
	}
	status = netif_rx(skb);
Stefan Richter's avatar
Stefan Richter committed
545
546
547
	if (status == NET_RX_DROP) {
		net->stats.rx_errors++;
		net->stats.rx_dropped++;
Jay Fenlason's avatar
Jay Fenlason committed
548
	} else {
Stefan Richter's avatar
Stefan Richter committed
549
550
		net->stats.rx_packets++;
		net->stats.rx_bytes += skb->len;
Jay Fenlason's avatar
Jay Fenlason committed
551
	}
Stefan Richter's avatar
Stefan Richter committed
552

Jay Fenlason's avatar
Jay Fenlason committed
553
554
	return 0;

555
 err:
Stefan Richter's avatar
Stefan Richter committed
556
557
558
	net->stats.rx_errors++;
	net->stats.rx_dropped++;

Jay Fenlason's avatar
Jay Fenlason committed
559
	dev_kfree_skb_any(skb);
Stefan Richter's avatar
Stefan Richter committed
560

561
	return -ENOENT;
Jay Fenlason's avatar
Jay Fenlason committed
562
563
}

Stefan Richter's avatar
Stefan Richter committed
564
static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
565
566
				 int source_node_id, int generation,
				 bool is_broadcast)
Stefan Richter's avatar
Stefan Richter committed
567
{
Jay Fenlason's avatar
Jay Fenlason committed
568
	struct sk_buff *skb;
569
	struct net_device *net = dev->netdev;
Stefan Richter's avatar
Stefan Richter committed
570
	struct rfc2734_header hdr;
Jay Fenlason's avatar
Jay Fenlason committed
571
572
	unsigned lf;
	unsigned long flags;
Stefan Richter's avatar
Stefan Richter committed
573
574
	struct fwnet_peer *peer;
	struct fwnet_partial_datagram *pd;
Jay Fenlason's avatar
Jay Fenlason committed
575
576
577
578
579
580
	int fg_off;
	int dg_size;
	u16 datagram_label;
	int retval;
	u16 ether_type;

581
582
583
	if (len <= RFC2374_UNFRAG_HDR_SIZE)
		return 0;

Stefan Richter's avatar
Stefan Richter committed
584
585
586
	hdr.w0 = be32_to_cpu(buf[0]);
	lf = fwnet_get_hdr_lf(&hdr);
	if (lf == RFC2374_HDR_UNFRAG) {
Jay Fenlason's avatar
Jay Fenlason committed
587
588
589
590
591
		/*
		 * An unfragmented datagram has been received by the ieee1394
		 * bus. Build an skbuff around it so we can pass it to the
		 * high level network layer.
		 */
Stefan Richter's avatar
Stefan Richter committed
592
		ether_type = fwnet_get_hdr_ether_type(&hdr);
Jay Fenlason's avatar
Jay Fenlason committed
593
		buf++;
Stefan Richter's avatar
Stefan Richter committed
594
		len -= RFC2374_UNFRAG_HDR_SIZE;
Jay Fenlason's avatar
Jay Fenlason committed
595

596
		skb = dev_alloc_skb(len + LL_RESERVED_SPACE(net));
Jay Fenlason's avatar
Jay Fenlason committed
597
		if (unlikely(!skb)) {
Stefan Richter's avatar
Stefan Richter committed
598
599
			net->stats.rx_dropped++;

600
			return -ENOMEM;
Jay Fenlason's avatar
Jay Fenlason committed
601
		}
602
		skb_reserve(skb, LL_RESERVED_SPACE(net));
Stefan Richter's avatar
Stefan Richter committed
603
604
605
606
		memcpy(skb_put(skb, len), buf, len);

		return fwnet_finish_incoming_packet(net, skb, source_node_id,
						    is_broadcast, ether_type);
Jay Fenlason's avatar
Jay Fenlason committed
607
	}
608

Jay Fenlason's avatar
Jay Fenlason committed
609
	/* A datagram fragment has been received, now the fun begins. */
610
611
612
613

	if (len <= RFC2374_FRAG_HDR_SIZE)
		return 0;

Jay Fenlason's avatar
Jay Fenlason committed
614
	hdr.w1 = ntohl(buf[1]);
Stefan Richter's avatar
Stefan Richter committed
615
616
617
618
	buf += 2;
	len -= RFC2374_FRAG_HDR_SIZE;
	if (lf == RFC2374_HDR_FIRSTFRAG) {
		ether_type = fwnet_get_hdr_ether_type(&hdr);
Jay Fenlason's avatar
Jay Fenlason committed
619
620
		fg_off = 0;
	} else {
Stefan Richter's avatar
Stefan Richter committed
621
622
		ether_type = 0;
		fg_off = fwnet_get_hdr_fg_off(&hdr);
Jay Fenlason's avatar
Jay Fenlason committed
623
	}
Stefan Richter's avatar
Stefan Richter committed
624
	datagram_label = fwnet_get_hdr_dgl(&hdr);
625
	dg_size = fwnet_get_hdr_dg_size(&hdr);
Stefan Richter's avatar
Stefan Richter committed
626

627
628
629
	if (fg_off + len > dg_size)
		return 0;

630
631
632
	spin_lock_irqsave(&dev->lock, flags);

	peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation);
633
634
635
636
	if (!peer) {
		retval = -ENOENT;
		goto fail;
	}
Stefan Richter's avatar
Stefan Richter committed
637
638

	pd = fwnet_pd_find(peer, datagram_label);
Jay Fenlason's avatar
Jay Fenlason committed
639
	if (pd == NULL) {
Stefan Richter's avatar
Stefan Richter committed
640
		while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) {
Jay Fenlason's avatar
Jay Fenlason committed
641
			/* remove the oldest */
Stefan Richter's avatar
Stefan Richter committed
642
643
644
			fwnet_pd_delete(list_first_entry(&peer->pd_list,
				struct fwnet_partial_datagram, pd_link));
			peer->pdg_size--;
Jay Fenlason's avatar
Jay Fenlason committed
645
		}
Stefan Richter's avatar
Stefan Richter committed
646
647
648
		pd = fwnet_pd_new(net, peer, datagram_label,
				  dg_size, buf, fg_off, len);
		if (pd == NULL) {
Jay Fenlason's avatar
Jay Fenlason committed
649
			retval = -ENOMEM;
650
			goto fail;
Jay Fenlason's avatar
Jay Fenlason committed
651
		}
Stefan Richter's avatar
Stefan Richter committed
652
		peer->pdg_size++;
Jay Fenlason's avatar
Jay Fenlason committed
653
	} else {
Stefan Richter's avatar
Stefan Richter committed
654
655
		if (fwnet_frag_overlap(pd, fg_off, len) ||
		    pd->datagram_size != dg_size) {
Jay Fenlason's avatar
Jay Fenlason committed
656
657
			/*
			 * Differing datagram sizes or overlapping fragments,
Stefan Richter's avatar
Stefan Richter committed
658
			 * discard old datagram and start a new one.
Jay Fenlason's avatar
Jay Fenlason committed
659
			 */
Stefan Richter's avatar
Stefan Richter committed
660
661
662
663
664
			fwnet_pd_delete(pd);
			pd = fwnet_pd_new(net, peer, datagram_label,
					  dg_size, buf, fg_off, len);
			if (pd == NULL) {
				peer->pdg_size--;
665
666
				retval = -ENOMEM;
				goto fail;
Jay Fenlason's avatar
Jay Fenlason committed
667
668
			}
		} else {
Stefan Richter's avatar
Stefan Richter committed
669
			if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) {
Jay Fenlason's avatar
Jay Fenlason committed
670
671
672
673
674
				/*
				 * Couldn't save off fragment anyway
				 * so might as well obliterate the
				 * datagram now.
				 */
Stefan Richter's avatar
Stefan Richter committed
675
676
				fwnet_pd_delete(pd);
				peer->pdg_size--;
677
678
				retval = -ENOMEM;
				goto fail;
Jay Fenlason's avatar
Jay Fenlason committed
679
680
681
682
			}
		}
	} /* new datagram or add to existing one */

Stefan Richter's avatar
Stefan Richter committed
683
	if (lf == RFC2374_HDR_FIRSTFRAG)
Jay Fenlason's avatar
Jay Fenlason committed
684
		pd->ether_type = ether_type;
Stefan Richter's avatar
Stefan Richter committed
685
686

	if (fwnet_pd_is_complete(pd)) {
Jay Fenlason's avatar
Jay Fenlason committed
687
		ether_type = pd->ether_type;
Stefan Richter's avatar
Stefan Richter committed
688
		peer->pdg_size--;
Jay Fenlason's avatar
Jay Fenlason committed
689
		skb = skb_get(pd->skb);
Stefan Richter's avatar
Stefan Richter committed
690
691
		fwnet_pd_delete(pd);

692
		spin_unlock_irqrestore(&dev->lock, flags);
Stefan Richter's avatar
Stefan Richter committed
693
694
695

		return fwnet_finish_incoming_packet(net, skb, source_node_id,
						    false, ether_type);
Jay Fenlason's avatar
Jay Fenlason committed
696
697
698
699
700
	}
	/*
	 * Datagram is not complete, we're done for the
	 * moment.
	 */
701
	retval = 0;
702
 fail:
703
	spin_unlock_irqrestore(&dev->lock, flags);
Stefan Richter's avatar
Stefan Richter committed
704

705
	return retval;
Jay Fenlason's avatar
Jay Fenlason committed
706
707
}

Stefan Richter's avatar
Stefan Richter committed
708
709
static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r,
		int tcode, int destination, int source, int generation,
710
711
		unsigned long long offset, void *payload, size_t length,
		void *callback_data)
Stefan Richter's avatar
Stefan Richter committed
712
{
713
714
	struct fwnet_device *dev = callback_data;
	int rcode;
Jay Fenlason's avatar
Jay Fenlason committed
715

716
717
	if (destination == IEEE1394_ALL_NODES) {
		kfree(r);
Stefan Richter's avatar
Stefan Richter committed
718

Jay Fenlason's avatar
Jay Fenlason committed
719
720
		return;
	}
Stefan Richter's avatar
Stefan Richter committed
721

722
723
724
725
726
727
	if (offset != dev->handler.offset)
		rcode = RCODE_ADDRESS_ERROR;
	else if (tcode != TCODE_WRITE_BLOCK_REQUEST)
		rcode = RCODE_TYPE_ERROR;
	else if (fwnet_incoming_packet(dev, payload, length,
				       source, generation, false) != 0) {
728
		dev_err(&dev->netdev->dev, "incoming packet failure\n");
729
730
731
		rcode = RCODE_CONFLICT_ERROR;
	} else
		rcode = RCODE_COMPLETE;
Stefan Richter's avatar
Stefan Richter committed
732

733
	fw_send_response(card, r, rcode);
Jay Fenlason's avatar
Jay Fenlason committed
734
735
}

736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
static int gasp_source_id(__be32 *p)
{
	return be32_to_cpu(p[0]) >> 16;
}

static u32 gasp_specifier_id(__be32 *p)
{
	return (be32_to_cpu(p[0]) & 0xffff) << 8 |
	       (be32_to_cpu(p[1]) & 0xff000000) >> 24;
}

static u32 gasp_version(__be32 *p)
{
	return be32_to_cpu(p[1]) & 0xffffff;
}

Stefan Richter's avatar
Stefan Richter committed
752
753
754
755
static void fwnet_receive_broadcast(struct fw_iso_context *context,
		u32 cycle, size_t header_length, void *header, void *data)
{
	struct fwnet_device *dev;
Jay Fenlason's avatar
Jay Fenlason committed
756
	struct fw_iso_packet packet;
Stefan Richter's avatar
Stefan Richter committed
757
758
	__be16 *hdr_ptr;
	__be32 *buf_ptr;
Jay Fenlason's avatar
Jay Fenlason committed
759
760
761
762
763
	int retval;
	u32 length;
	unsigned long offset;
	unsigned long flags;

Stefan Richter's avatar
Stefan Richter committed
764
	dev = data;
Jay Fenlason's avatar
Jay Fenlason committed
765
	hdr_ptr = header;
Stefan Richter's avatar
Stefan Richter committed
766
767
768
769
770
771
772
773
774
775
	length = be16_to_cpup(hdr_ptr);

	spin_lock_irqsave(&dev->lock, flags);

	offset = dev->rcv_buffer_size * dev->broadcast_rcv_next_ptr;
	buf_ptr = dev->broadcast_rcv_buffer_ptrs[dev->broadcast_rcv_next_ptr++];
	if (dev->broadcast_rcv_next_ptr == dev->num_broadcast_rcv_ptrs)
		dev->broadcast_rcv_next_ptr = 0;

	spin_unlock_irqrestore(&dev->lock, flags);
Jay Fenlason's avatar
Jay Fenlason committed
776

777
778
779
	if (length > IEEE1394_GASP_HDR_SIZE &&
	    gasp_specifier_id(buf_ptr) == IANA_SPECIFIER_ID &&
	    (gasp_version(buf_ptr) == RFC2734_SW_VERSION
780
#if IS_ENABLED(CONFIG_IPV6)
781
	     || gasp_version(buf_ptr) == RFC3146_SW_VERSION
782
#endif
783
784
785
786
	    ))
		fwnet_incoming_packet(dev, buf_ptr + 2,
				      length - IEEE1394_GASP_HDR_SIZE,
				      gasp_source_id(buf_ptr),
787
				      context->card->generation, true);
Stefan Richter's avatar
Stefan Richter committed
788
789

	packet.payload_length = dev->rcv_buffer_size;
Jay Fenlason's avatar
Jay Fenlason committed
790
791
792
793
	packet.interrupt = 1;
	packet.skip = 0;
	packet.tag = 3;
	packet.sy = 0;
Stefan Richter's avatar
Stefan Richter committed
794
795
796
	packet.header_length = IEEE1394_GASP_HDR_SIZE;

	spin_lock_irqsave(&dev->lock, flags);
Jay Fenlason's avatar
Jay Fenlason committed
797

Stefan Richter's avatar
Stefan Richter committed
798
799
800
801
802
	retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet,
				      &dev->broadcast_rcv_buffer, offset);

	spin_unlock_irqrestore(&dev->lock, flags);

803
804
805
	if (retval >= 0)
		fw_iso_context_queue_flush(dev->broadcast_rcv_context);
	else
806
		dev_err(&dev->netdev->dev, "requeue failed\n");
Jay Fenlason's avatar
Jay Fenlason committed
807
808
}

Stefan Richter's avatar
Stefan Richter committed
809
810
static struct kmem_cache *fwnet_packet_task_cache;

811
812
813
814
815
816
static void fwnet_free_ptask(struct fwnet_packet_task *ptask)
{
	dev_kfree_skb_any(ptask->skb);
	kmem_cache_free(fwnet_packet_task_cache, ptask);
}

817
818
819
820
821
822
823
/* Caller must hold dev->lock. */
static void dec_queued_datagrams(struct fwnet_device *dev)
{
	if (--dev->queued_datagrams == FWNET_MIN_QUEUED_DATAGRAMS)
		netif_wake_queue(dev->netdev);
}

Stefan Richter's avatar
Stefan Richter committed
824
825
826
827
static int fwnet_send_packet(struct fwnet_packet_task *ptask);

static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask)
{
828
	struct fwnet_device *dev = ptask->dev;
829
	struct sk_buff *skb = ptask->skb;
Jay Fenlason's avatar
Jay Fenlason committed
830
	unsigned long flags;
831
	bool free;
Stefan Richter's avatar
Stefan Richter committed
832
833
834

	spin_lock_irqsave(&dev->lock, flags);

835
836
837
	ptask->outstanding_pkts--;

	/* Check whether we or the networking TX soft-IRQ is last user. */
838
	free = (ptask->outstanding_pkts == 0 && ptask->enqueued);
839
	if (free)
840
		dec_queued_datagrams(dev);
841

842
843
844
845
	if (ptask->outstanding_pkts == 0) {
		dev->netdev->stats.tx_packets++;
		dev->netdev->stats.tx_bytes += skb->len;
	}
846
847

	spin_unlock_irqrestore(&dev->lock, flags);
Stefan Richter's avatar
Stefan Richter committed
848
849

	if (ptask->outstanding_pkts > 0) {
Jay Fenlason's avatar
Jay Fenlason committed
850
851
852
853
854
855
		u16 dg_size;
		u16 fg_off;
		u16 datagram_label;
		u16 lf;

		/* Update the ptask to point to the next fragment and send it */
Stefan Richter's avatar
Stefan Richter committed
856
		lf = fwnet_get_hdr_lf(&ptask->hdr);
Jay Fenlason's avatar
Jay Fenlason committed
857
		switch (lf) {
Stefan Richter's avatar
Stefan Richter committed
858
859
		case RFC2374_HDR_LASTFRAG:
		case RFC2374_HDR_UNFRAG:
Jay Fenlason's avatar
Jay Fenlason committed
860
		default:
861
862
863
864
			dev_err(&dev->netdev->dev,
				"outstanding packet %x lf %x, header %x,%x\n",
				ptask->outstanding_pkts, lf, ptask->hdr.w0,
				ptask->hdr.w1);
Jay Fenlason's avatar
Jay Fenlason committed
865
866
			BUG();

Stefan Richter's avatar
Stefan Richter committed
867
		case RFC2374_HDR_FIRSTFRAG:
Jay Fenlason's avatar
Jay Fenlason committed
868
			/* Set frag type here for future interior fragments */
Stefan Richter's avatar
Stefan Richter committed
869
870
871
			dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
			fg_off = ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
			datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
Jay Fenlason's avatar
Jay Fenlason committed
872
873
			break;

Stefan Richter's avatar
Stefan Richter committed
874
875
876
877
878
		case RFC2374_HDR_INTFRAG:
			dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
			fg_off = fwnet_get_hdr_fg_off(&ptask->hdr)
				  + ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
			datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
Jay Fenlason's avatar
Jay Fenlason committed
879
880
			break;
		}
881

882
883
884
885
886
887
		if (ptask->dest_node == IEEE1394_ALL_NODES) {
			skb_pull(skb,
				 ptask->max_payload + IEEE1394_GASP_HDR_SIZE);
		} else {
			skb_pull(skb, ptask->max_payload);
		}
Stefan Richter's avatar
Stefan Richter committed
888
889
890
		if (ptask->outstanding_pkts > 1) {
			fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG,
					  dg_size, fg_off, datagram_label);
Jay Fenlason's avatar
Jay Fenlason committed
891
		} else {
Stefan Richter's avatar
Stefan Richter committed
892
893
894
			fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG,
					  dg_size, fg_off, datagram_label);
			ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE;
Jay Fenlason's avatar
Jay Fenlason committed
895
		}
Stefan Richter's avatar
Stefan Richter committed
896
		fwnet_send_packet(ptask);
Jay Fenlason's avatar
Jay Fenlason committed
897
	}
898
899
900

	if (free)
		fwnet_free_ptask(ptask);
Jay Fenlason's avatar
Jay Fenlason committed
901
902
}

903
904
905
906
907
908
909
910
911
912
913
914
static void fwnet_transmit_packet_failed(struct fwnet_packet_task *ptask)
{
	struct fwnet_device *dev = ptask->dev;
	unsigned long flags;
	bool free;

	spin_lock_irqsave(&dev->lock, flags);

	/* One fragment failed; don't try to send remaining fragments. */
	ptask->outstanding_pkts = 0;

	/* Check whether we or the networking TX soft-IRQ is last user. */
915
	free = ptask->enqueued;
916
	if (free)
917
		dec_queued_datagrams(dev);
918
919
920
921
922
923
924
925
926
927

	dev->netdev->stats.tx_dropped++;
	dev->netdev->stats.tx_errors++;

	spin_unlock_irqrestore(&dev->lock, flags);

	if (free)
		fwnet_free_ptask(ptask);
}

Stefan Richter's avatar
Stefan Richter committed
928
929
930
static void fwnet_write_complete(struct fw_card *card, int rcode,
				 void *payload, size_t length, void *data)
{
931
932
933
	struct fwnet_packet_task *ptask = data;
	static unsigned long j;
	static int last_rcode, errors_skipped;
Jay Fenlason's avatar
Jay Fenlason committed
934

935
	if (rcode == RCODE_COMPLETE) {
Stefan Richter's avatar
Stefan Richter committed
936
		fwnet_transmit_packet_done(ptask);
937
	} else {
938
		if (printk_timed_ratelimit(&j,  1000) || rcode != last_rcode) {
939
940
941
			dev_err(&ptask->dev->netdev->dev,
				"fwnet_write_complete failed: %x (skipped %d)\n",
				rcode, errors_skipped);
942
943
944

			errors_skipped = 0;
			last_rcode = rcode;
945
		} else {
946
			errors_skipped++;
947
948
		}
		fwnet_transmit_packet_failed(ptask);
949
	}
Jay Fenlason's avatar
Jay Fenlason committed
950
951
}

Stefan Richter's avatar
Stefan Richter committed
952
953
954
static int fwnet_send_packet(struct fwnet_packet_task *ptask)
{
	struct fwnet_device *dev;
Jay Fenlason's avatar
Jay Fenlason committed
955
	unsigned tx_len;
Stefan Richter's avatar
Stefan Richter committed
956
	struct rfc2734_header *bufhdr;
Jay Fenlason's avatar
Jay Fenlason committed
957
	unsigned long flags;
958
	bool free;
Jay Fenlason's avatar
Jay Fenlason committed
959

Stefan Richter's avatar
Stefan Richter committed
960
	dev = ptask->dev;
Jay Fenlason's avatar
Jay Fenlason committed
961
	tx_len = ptask->max_payload;
Stefan Richter's avatar
Stefan Richter committed
962
963
964
965
966
	switch (fwnet_get_hdr_lf(&ptask->hdr)) {
	case RFC2374_HDR_UNFRAG:
		bufhdr = (struct rfc2734_header *)
				skb_push(ptask->skb, RFC2374_UNFRAG_HDR_SIZE);
		put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
Jay Fenlason's avatar
Jay Fenlason committed
967
968
		break;

Stefan Richter's avatar
Stefan Richter committed
969
970
971
972
973
974
975
	case RFC2374_HDR_FIRSTFRAG:
	case RFC2374_HDR_INTFRAG:
	case RFC2374_HDR_LASTFRAG:
		bufhdr = (struct rfc2734_header *)
				skb_push(ptask->skb, RFC2374_FRAG_HDR_SIZE);
		put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
		put_unaligned_be32(ptask->hdr.w1, &bufhdr->w1);
Jay Fenlason's avatar
Jay Fenlason committed
976
977
978
979
980
		break;

	default:
		BUG();
	}
Stefan Richter's avatar
Stefan Richter committed
981
982
	if (ptask->dest_node == IEEE1394_ALL_NODES) {
		u8 *p;
Jay Fenlason's avatar
Jay Fenlason committed
983
		int generation;
Stefan Richter's avatar
Stefan Richter committed
984
		int node_id;
985
		unsigned int sw_version;
Jay Fenlason's avatar
Jay Fenlason committed
986
987

		/* ptask->generation may not have been set yet */
Stefan Richter's avatar
Stefan Richter committed
988
		generation = dev->card->generation;
Jay Fenlason's avatar
Jay Fenlason committed
989
		smp_rmb();
Stefan Richter's avatar
Stefan Richter committed
990
991
		node_id = dev->card->node_id;

992
993
994
995
996
997
998
999
1000
		switch (ptask->skb->protocol) {
		default:
			sw_version = RFC2734_SW_VERSION;
			break;
#if IS_ENABLED(CONFIG_IPV6)
		case htons(ETH_P_IPV6):
			sw_version = RFC3146_SW_VERSION;
#endif
		}