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
 */

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>
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

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

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

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

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

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);
}

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;
};

74 75 76 77 78
#define fwnet_get_hdr_lf(h)		(((h)->w0 & 0xc0000000) >> 30)
#define fwnet_get_hdr_ether_type(h)	(((h)->w0 & 0x0000ffff))
#define fwnet_get_hdr_dg_size(h)	(((h)->w0 & 0x0fff0000) >> 16)
#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 81 82 83
#define fwnet_set_hdr_lf(lf)		((lf)  << 30)
#define fwnet_set_hdr_ether_type(et)	(et)
#define fwnet_set_hdr_dg_size(dgs)	((dgs) << 16)
#define fwnet_set_hdr_fg_off(fgo)	(fgo)
Jay Fenlason's avatar
Jay Fenlason committed
84

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

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

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

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 */
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;
};

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

133
struct fwnet_device {
134
	struct list_head dev_link;
Jay Fenlason's avatar
Jay Fenlason committed
135
	spinlock_t lock;
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;

	/*
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.  */
188
struct fwnet_packet_task {
Jay Fenlason's avatar
Jay Fenlason committed
189
	struct fw_transaction transaction;
190
	struct rfc2734_header hdr;
Jay Fenlason's avatar
Jay Fenlason committed
191
	struct sk_buff *skb;
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);
}

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

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

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

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

	if (daddr) {
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
	}

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

240
static int fwnet_header_rebuild(struct sk_buff *skb)
Jay Fenlason's avatar
Jay Fenlason committed
241
{
242
	struct fwnet_header *h = (struct fwnet_header *)skb->data;
Jay Fenlason's avatar
Jay Fenlason committed
243

244 245
	if (get_unaligned_be16(&h->h_proto) == ETH_P_IP)
		return arp_find((unsigned char *)&h->h_dest, skb);
Jay Fenlason's avatar
Jay Fenlason committed
246

247 248
	dev_notice(&skb->dev->dev, "unable to resolve type %04x addresses\n",
		   be16_to_cpu(h->h_proto));
Jay Fenlason's avatar
Jay Fenlason committed
249 250 251
	return 0;
}

252
static int fwnet_header_cache(const struct neighbour *neigh,
253
			      struct hh_cache *hh, __be16 type)
254 255 256
{
	struct net_device *net;
	struct fwnet_header *h;
Jay Fenlason's avatar
Jay Fenlason committed
257

258
	if (type == cpu_to_be16(ETH_P_802_3))
Jay Fenlason's avatar
Jay Fenlason committed
259
		return -1;
260
	net = neigh->dev;
261
	h = (struct fwnet_header *)((u8 *)hh->hh_data + HH_DATA_OFF(sizeof(*h)));
262
	h->h_proto = type;
263 264
	memcpy(h->h_dest, neigh->ha, net->addr_len);
	hh->hh_len = FWNET_HLEN;
Jay Fenlason's avatar
Jay Fenlason committed
265 266 267 268 269

	return 0;
}

/* Called by Address Resolution module to notify changes in address. */
270 271 272
static void fwnet_header_cache_update(struct hh_cache *hh,
		const struct net_device *net, const unsigned char *haddr)
{
273
	memcpy((u8 *)hh->hh_data + HH_DATA_OFF(FWNET_HLEN), haddr, net->addr_len);
Jay Fenlason's avatar
Jay Fenlason committed
274 275
}

276 277 278 279 280
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
281 282
}

283 284 285 286 287 288
static const struct header_ops fwnet_header_ops = {
	.create         = fwnet_header_create,
	.rebuild        = fwnet_header_rebuild,
	.cache		= fwnet_header_cache,
	.cache_update	= fwnet_header_cache_update,
	.parse          = fwnet_header_parse,
Jay Fenlason's avatar
Jay Fenlason committed
289 290 291
};

/* FIXME: is this correct for all cases? */
292 293
static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd,
			       unsigned offset, unsigned len)
Jay Fenlason's avatar
Jay Fenlason committed
294
{
295
	struct fwnet_fragment_info *fi;
Jay Fenlason's avatar
Jay Fenlason committed
296 297
	unsigned end = offset + len;

298 299
	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
300
			return true;
301

Jay Fenlason's avatar
Jay Fenlason committed
302 303 304 305
	return false;
}

/* Assumes that new fragment does not overlap any existing fragments */
306 307 308 309
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
310 311
	struct list_head *list;

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

Jay Fenlason's avatar
Jay Fenlason committed
328 329 330 331 332
			return fi;
		}
		if (offset + len == fi->offset) {
			/* The new fragment can be tacked on to the beginning */
			/* Did the new fragment plug a hole? */
333 334
			fi2 = list_entry(fi->fi_link.prev,
					 struct fwnet_fragment_info, fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
335 336 337
			if (fi2->offset + fi2->len == fi->offset) {
				/* glue fragments together */
				fi2->len += fi->len + len;
338
				list_del(&fi->fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
339
				kfree(fi);
340

Jay Fenlason's avatar
Jay Fenlason committed
341 342 343 344
				return fi2;
			}
			fi->offset = offset;
			fi->len += len;
345

Jay Fenlason's avatar
Jay Fenlason committed
346 347 348
			return fi;
		}
		if (offset > fi->offset + fi->len) {
349
			list = &fi->fi_link;
Jay Fenlason's avatar
Jay Fenlason committed
350 351 352
			break;
		}
		if (offset + len < fi->offset) {
353
			list = fi->fi_link.prev;
Jay Fenlason's avatar
Jay Fenlason committed
354 355 356 357 358
			break;
		}
	}

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
359
	if (!new)
Jay Fenlason's avatar
Jay Fenlason committed
360 361 362 363
		return NULL;

	new->offset = offset;
	new->len = len;
364 365
	list_add(&new->fi_link, list);

Jay Fenlason's avatar
Jay Fenlason committed
366 367 368
	return new;
}

369 370 371 372 373 374
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
375 376 377 378

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
	if (!new)
		goto fail;
379 380 381 382

	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
383
		goto fail_w_new;
384

Jay Fenlason's avatar
Jay Fenlason committed
385 386
	new->datagram_label = datagram_label;
	new->datagram_size = dg_size;
387
	new->skb = dev_alloc_skb(dg_size + LL_RESERVED_SPACE(net));
388
	if (new->skb == NULL)
Jay Fenlason's avatar
Jay Fenlason committed
389
		goto fail_w_fi;
390

391
	skb_reserve(new->skb, LL_RESERVED_SPACE(net));
Jay Fenlason's avatar
Jay Fenlason committed
392 393
	new->pbuf = skb_put(new->skb, dg_size);
	memcpy(new->pbuf + frag_off, frag_buf, frag_len);
394 395
	list_add_tail(&new->pd_link, &peer->pd_list);

Jay Fenlason's avatar
Jay Fenlason committed
396 397 398 399 400 401 402 403 404 405
	return new;

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

406 407 408 409
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
410

411 412
	list_for_each_entry(pd, &peer->pd_list, pd_link)
		if (pd->datagram_label == datagram_label)
Jay Fenlason's avatar
Jay Fenlason committed
413
			return pd;
414

Jay Fenlason's avatar
Jay Fenlason committed
415 416 417 418
	return NULL;
}


419 420 421
static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
{
	struct fwnet_fragment_info *fi, *n;
Jay Fenlason's avatar
Jay Fenlason committed
422

423
	list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
Jay Fenlason's avatar
Jay Fenlason committed
424
		kfree(fi);
425 426

	list_del(&old->pd_link);
Jay Fenlason's avatar
Jay Fenlason committed
427 428 429 430
	dev_kfree_skb_any(old->skb);
	kfree(old);
}

431 432 433 434 435
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
436
		return false;
437

Jay Fenlason's avatar
Jay Fenlason committed
438 439 440
	memcpy(pd->pbuf + frag_off, frag_buf, frag_len);

	/*
Lucas De Marchi's avatar
Lucas De Marchi committed
441
	 * Move list entry to beginning of list so that oldest partial
Jay Fenlason's avatar
Jay Fenlason committed
442 443
	 * datagrams percolate to the end of the list
	 */
444 445
	list_move_tail(&pd->pd_link, &peer->pd_list);

Jay Fenlason's avatar
Jay Fenlason committed
446 447 448
	return true;
}

449 450 451
static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
{
	struct fwnet_fragment_info *fi;
Jay Fenlason's avatar
Jay Fenlason committed
452

453
	fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
454

455
	return fi->len == pd->datagram_size;
Jay Fenlason's avatar
Jay Fenlason committed
456 457
}

458
/* caller must hold dev->lock */
459 460 461
static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
						  u64 guid)
{
462
	struct fwnet_peer *peer;
Jay Fenlason's avatar
Jay Fenlason committed
463

464 465 466
	list_for_each_entry(peer, &dev->peer_list, peer_link)
		if (peer->guid == guid)
			return peer;
Jay Fenlason's avatar
Jay Fenlason committed
467

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

471
/* caller must hold dev->lock */
472
static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
473
						int node_id, int generation)
474
{
475
	struct fwnet_peer *peer;
Jay Fenlason's avatar
Jay Fenlason committed
476

477 478 479 480
	list_for_each_entry(peer, &dev->peer_list, peer_link)
		if (peer->node_id    == node_id &&
		    peer->generation == generation)
			return peer;
481

482
	return NULL;
Jay Fenlason's avatar
Jay Fenlason committed
483 484
}

485 486
/* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */
static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed)
487
{
488
	max_rec = min(max_rec, speed + 8);
489
	max_rec = clamp(max_rec, 8U, 11U); /* 512...4096 */
490 491

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

494

495 496 497 498 499
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
500
	int status;
501
	__be64 guid;
Jay Fenlason's avatar
Jay Fenlason committed
502

503 504 505
	switch (ether_type) {
	case ETH_P_ARP:
	case ETH_P_IP:
506 507 508
#if IS_ENABLED(CONFIG_IPV6)
	case ETH_P_IPV6:
#endif
509 510 511 512 513
		break;
	default:
		goto err;
	}

514
	dev = netdev_priv(net);
Jay Fenlason's avatar
Jay Fenlason committed
515
	/* Write metadata, and then pass to the receive level */
516
	skb->dev = net;
517
	skb->ip_summed = CHECKSUM_NONE;
Jay Fenlason's avatar
Jay Fenlason committed
518 519 520

	/*
	 * Parse the encapsulation header. This actually does the job of
521
	 * converting to an ethernet-like pseudo frame header.
Jay Fenlason's avatar
Jay Fenlason committed
522
	 */
523 524
	guid = cpu_to_be64(dev->card->guid);
	if (dev_hard_header(skb, net, ether_type,
525
			   is_broadcast ? net->broadcast : net->dev_addr,
526 527
			   NULL, skb->len) >= 0) {
		struct fwnet_header *eth;
Jay Fenlason's avatar
Jay Fenlason committed
528 529 530 531 532
		u16 *rawp;
		__be16 protocol;

		skb_reset_mac_header(skb);
		skb_pull(skb, sizeof(*eth));
533
		eth = (struct fwnet_header *)skb_mac_header(skb);
534
		if (fwnet_hwaddr_is_multicast(eth->h_dest)) {
535 536
			if (memcmp(eth->h_dest, net->broadcast,
				   net->addr_len) == 0)
Jay Fenlason's avatar
Jay Fenlason committed
537 538 539 540 541 542
				skb->pkt_type = PACKET_BROADCAST;
#if 0
			else
				skb->pkt_type = PACKET_MULTICAST;
#endif
		} else {
543
			if (memcmp(eth->h_dest, net->dev_addr, net->addr_len))
Jay Fenlason's avatar
Jay Fenlason committed
544 545
				skb->pkt_type = PACKET_OTHERHOST;
		}
Simon Horman's avatar
Simon Horman committed
546
		if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) {
Jay Fenlason's avatar
Jay Fenlason committed
547 548 549
			protocol = eth->h_proto;
		} else {
			rawp = (u16 *)skb->data;
550
			if (*rawp == 0xffff)
Jay Fenlason's avatar
Jay Fenlason committed
551
				protocol = htons(ETH_P_802_3);
552
			else
Jay Fenlason's avatar
Jay Fenlason committed
553 554 555 556 557
				protocol = htons(ETH_P_802_2);
		}
		skb->protocol = protocol;
	}
	status = netif_rx(skb);
558 559 560
	if (status == NET_RX_DROP) {
		net->stats.rx_errors++;
		net->stats.rx_dropped++;
Jay Fenlason's avatar
Jay Fenlason committed
561
	} else {
562 563
		net->stats.rx_packets++;
		net->stats.rx_bytes += skb->len;
Jay Fenlason's avatar
Jay Fenlason committed
564
	}
565

Jay Fenlason's avatar
Jay Fenlason committed
566 567
	return 0;

568
 err:
569 570 571
	net->stats.rx_errors++;
	net->stats.rx_dropped++;

Jay Fenlason's avatar
Jay Fenlason committed
572
	dev_kfree_skb_any(skb);
573

574
	return -ENOENT;
Jay Fenlason's avatar
Jay Fenlason committed
575 576
}

577
static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
578 579
				 int source_node_id, int generation,
				 bool is_broadcast)
580
{
Jay Fenlason's avatar
Jay Fenlason committed
581
	struct sk_buff *skb;
582
	struct net_device *net = dev->netdev;
583
	struct rfc2734_header hdr;
Jay Fenlason's avatar
Jay Fenlason committed
584 585
	unsigned lf;
	unsigned long flags;
586 587
	struct fwnet_peer *peer;
	struct fwnet_partial_datagram *pd;
Jay Fenlason's avatar
Jay Fenlason committed
588 589 590 591 592 593
	int fg_off;
	int dg_size;
	u16 datagram_label;
	int retval;
	u16 ether_type;

594 595 596
	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
597 598 599 600 601
		/*
		 * 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.
		 */
602
		ether_type = fwnet_get_hdr_ether_type(&hdr);
Jay Fenlason's avatar
Jay Fenlason committed
603
		buf++;
604
		len -= RFC2374_UNFRAG_HDR_SIZE;
Jay Fenlason's avatar
Jay Fenlason committed
605

606
		skb = dev_alloc_skb(len + LL_RESERVED_SPACE(net));
Jay Fenlason's avatar
Jay Fenlason committed
607
		if (unlikely(!skb)) {
608 609
			net->stats.rx_dropped++;

610
			return -ENOMEM;
Jay Fenlason's avatar
Jay Fenlason committed
611
		}
612
		skb_reserve(skb, LL_RESERVED_SPACE(net));
613 614 615 616
		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
617 618 619
	}
	/* A datagram fragment has been received, now the fun begins. */
	hdr.w1 = ntohl(buf[1]);
620 621 622 623
	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
624 625
		fg_off = 0;
	} else {
626 627
		ether_type = 0;
		fg_off = fwnet_get_hdr_fg_off(&hdr);
Jay Fenlason's avatar
Jay Fenlason committed
628
	}
629 630 631
	datagram_label = fwnet_get_hdr_dgl(&hdr);
	dg_size = fwnet_get_hdr_dg_size(&hdr); /* ??? + 1 */

632 633 634
	spin_lock_irqsave(&dev->lock, flags);

	peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation);
635 636 637 638
	if (!peer) {
		retval = -ENOENT;
		goto fail;
	}
639 640

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

685
	if (lf == RFC2374_HDR_FIRSTFRAG)
Jay Fenlason's avatar
Jay Fenlason committed
686
		pd->ether_type = ether_type;
687 688

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

694
		spin_unlock_irqrestore(&dev->lock, flags);
695 696 697

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

707
	return retval;
Jay Fenlason's avatar
Jay Fenlason committed
708 709
}

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

718 719
	if (destination == IEEE1394_ALL_NODES) {
		kfree(r);
720

Jay Fenlason's avatar
Jay Fenlason committed
721 722
		return;
	}
723

724 725 726 727 728 729
	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) {
730
		dev_err(&dev->netdev->dev, "incoming packet failure\n");
731 732 733
		rcode = RCODE_CONFLICT_ERROR;
	} else
		rcode = RCODE_COMPLETE;
734

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

738 739 740 741
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
742
	struct fw_iso_packet packet;
743 744
	__be16 *hdr_ptr;
	__be32 *buf_ptr;
Jay Fenlason's avatar
Jay Fenlason committed
745 746 747 748 749 750 751 752
	int retval;
	u32 length;
	u16 source_node_id;
	u32 specifier_id;
	u32 ver;
	unsigned long offset;
	unsigned long flags;

753
	dev = data;
Jay Fenlason's avatar
Jay Fenlason committed
754
	hdr_ptr = header;
755 756 757 758 759 760 761 762 763 764
	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
765 766 767

	specifier_id =    (be32_to_cpu(buf_ptr[0]) & 0xffff) << 8
			| (be32_to_cpu(buf_ptr[1]) & 0xff000000) >> 24;
768
	ver = be32_to_cpu(buf_ptr[1]) & 0xffffff;
Jay Fenlason's avatar
Jay Fenlason committed
769
	source_node_id = be32_to_cpu(buf_ptr[0]) >> 16;
770

771 772 773 774 775 776
	if (specifier_id == IANA_SPECIFIER_ID &&
	    (ver == RFC2734_SW_VERSION
#if IS_ENABLED(CONFIG_IPV6)
	     || ver == RFC3146_SW_VERSION
#endif
	    )) {
Jay Fenlason's avatar
Jay Fenlason committed
777
		buf_ptr += 2;
778
		length -= IEEE1394_GASP_HDR_SIZE;
779 780
		fwnet_incoming_packet(dev, buf_ptr, length, source_node_id,
				      context->card->generation, true);
781 782 783
	}

	packet.payload_length = dev->rcv_buffer_size;
Jay Fenlason's avatar
Jay Fenlason committed
784 785 786 787
	packet.interrupt = 1;
	packet.skip = 0;
	packet.tag = 3;
	packet.sy = 0;
788 789 790
	packet.header_length = IEEE1394_GASP_HDR_SIZE;

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

792 793 794 795 796
	retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet,
				      &dev->broadcast_rcv_buffer, offset);

	spin_unlock_irqrestore(&dev->lock, flags);

797 798 799
	if (retval >= 0)
		fw_iso_context_queue_flush(dev->broadcast_rcv_context);
	else
800
		dev_err(&dev->netdev->dev, "requeue failed\n");
Jay Fenlason's avatar
Jay Fenlason committed
801 802
}

803 804
static struct kmem_cache *fwnet_packet_task_cache;

805 806 807 808 809 810
static void fwnet_free_ptask(struct fwnet_packet_task *ptask)
{
	dev_kfree_skb_any(ptask->skb);
	kmem_cache_free(fwnet_packet_task_cache, ptask);
}

811 812 813 814 815 816 817
/* 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);
}

818 819 820 821
static int fwnet_send_packet(struct fwnet_packet_task *ptask);

static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask)
{
822
	struct fwnet_device *dev = ptask->dev;
823
	struct sk_buff *skb = ptask->skb;
Jay Fenlason's avatar
Jay Fenlason committed
824
	unsigned long flags;
825
	bool free;
826 827 828

	spin_lock_irqsave(&dev->lock, flags);

829 830 831
	ptask->outstanding_pkts--;

	/* Check whether we or the networking TX soft-IRQ is last user. */
832
	free = (ptask->outstanding_pkts == 0 && ptask->enqueued);
833
	if (free)
834
		dec_queued_datagrams(dev);
835

836 837 838 839
	if (ptask->outstanding_pkts == 0) {
		dev->netdev->stats.tx_packets++;
		dev->netdev->stats.tx_bytes += skb->len;
	}
840 841

	spin_unlock_irqrestore(&dev->lock, flags);
842 843

	if (ptask->outstanding_pkts > 0) {
Jay Fenlason's avatar
Jay Fenlason committed
844 845 846 847 848 849
		u16 dg_size;
		u16 fg_off;
		u16 datagram_label;
		u16 lf;

		/* Update the ptask to point to the next fragment and send it */
850
		lf = fwnet_get_hdr_lf(&ptask->hdr);
Jay Fenlason's avatar
Jay Fenlason committed
851
		switch (lf) {
852 853
		case RFC2374_HDR_LASTFRAG:
		case RFC2374_HDR_UNFRAG:
Jay Fenlason's avatar
Jay Fenlason committed
854
		default:
855 856 857 858
			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
859 860
			BUG();

861
		case RFC2374_HDR_FIRSTFRAG:
Jay Fenlason's avatar
Jay Fenlason committed
862
			/* Set frag type here for future interior fragments */
863 864 865
			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
866 867
			break;

868 869 870 871 872
		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
873 874
			break;
		}
875

876 877 878 879 880 881
		if (ptask->dest_node == IEEE1394_ALL_NODES) {
			skb_pull(skb,
				 ptask->max_payload + IEEE1394_GASP_HDR_SIZE);
		} else {
			skb_pull(skb, ptask->max_payload);
		}
882 883 884
		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
885
		} else {
886 887 888
			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
889
		}
890
		fwnet_send_packet(ptask);
Jay Fenlason's avatar
Jay Fenlason committed
891
	}
892 893 894

	if (free)
		fwnet_free_ptask(ptask);
Jay Fenlason's avatar
Jay Fenlason committed
895 896
}

897 898 899 900 901 902 903 904 905 906 907 908
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. */
909
	free = ptask->enqueued;
910
	if (free)
911
		dec_queued_datagrams(dev);
912 913 914 915 916 917 918 919 920 921

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

	spin_unlock_irqrestore(&dev->lock, flags);

	if (free)
		fwnet_free_ptask(ptask);
}

922 923 924
static void fwnet_write_complete(struct fw_card *card, int rcode,
				 void *payload, size_t length, void *data)
{
925 926 927
	struct fwnet_packet_task *ptask = data;
	static unsigned long j;
	static int last_rcode, errors_skipped;
Jay Fenlason's avatar
Jay Fenlason committed
928

929
	if (rcode == RCODE_COMPLETE) {
930
		fwnet_transmit_packet_done(ptask);
931
	} else {
932
		if (printk_timed_ratelimit(&j,  1000) || rcode != last_rcode) {
933 934 935
			dev_err(&ptask->dev->netdev->dev,
				"fwnet_write_complete failed: %x (skipped %d)\n",
				rcode, errors_skipped);
936 937 938

			errors_skipped = 0;
			last_rcode = rcode;
939
		} else {
940
			errors_skipped++;
941 942
		}
		fwnet_transmit_packet_failed(ptask);
943
	}
Jay Fenlason's avatar
Jay Fenlason committed
944 945
}

946 947 948
static int fwnet_send_packet(struct fwnet_packet_task *ptask)
{
	struct fwnet_device *dev;
Jay Fenlason's avatar
Jay Fenlason committed
949
	unsigned tx_len;
950
	struct rfc2734_header *bufhdr;
Jay Fenlason's avatar
Jay Fenlason committed
951
	unsigned long flags;
952
	bool free;
Jay Fenlason's avatar
Jay Fenlason committed
953

954
	dev = ptask->dev;
Jay Fenlason's avatar
Jay Fenlason committed
955
	tx_len = ptask->max_payload;
956 957 958 959 960
	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
961 962
		break;

963 964 965 966 967 968 969
	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
970 971 972 973 974
		break;

	default:
		BUG();
	}
975 976
	if (ptask->dest_node == IEEE1394_ALL_NODES) {
		u8 *p;
Jay Fenlason's avatar
Jay Fenlason committed
977
		int generation;
978
		int node_id;
979
		unsigned int sw_version;
Jay Fenlason's avatar
Jay Fenlason committed
980 981

		/* ptask->generation may not have been set yet */
982
		generation = dev->card->generation;
Jay Fenlason's avatar
Jay Fenlason committed
983
		smp_rmb();
984 985
		node_id = dev->card->node_id;

986 987 988 989 990 991 992 993 994 995
		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
		}

996
		p = skb_push(ptask->skb, IEEE1394_GASP_HDR_SIZE);
997 998
		put_unaligned_be32(node_id << 16 | IANA_SPECIFIER_ID >> 8, p);
		put_unaligned_be32((IANA_SPECIFIER_ID & 0xff) << 24
999
						| sw_version, &p[4]);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

		/* We should not transmit if broadcast_channel.valid == 0. */
		fw_send_request(dev->card, &ptask->transaction,
				TCODE_STREAM_DATA,
				fw_stream_packet_destination_id(3,
						IEEE1394_BROADCAST_CHANNEL, 0),
				generation, SCODE_100, 0ULL, ptask->skb->data,
				tx_len + 8, fwnet_write_complete, ptask);

		spin_lock_irqsave(&dev->lock, flags);
1010 1011

		/* If the AT tasklet already ran, we may be last user. */
1012
		free = (ptask->outstanding_pkts == 0 && !ptask->enqueued);
1013
		if (!free)
1014 1015
			ptask->enqueued = true;
		else
1016
			dec_queued_datagrams(dev);
1017

1018 1019
		spin_unlock_irqrestore(&dev->lock, flags);

1020
		goto out;
Jay Fenlason's avatar
Jay Fenlason committed
1021
	}
1022 1023 1024 1025 1026 1027 1028

	fw_send_request(dev->card, &ptask->transaction,
			TCODE_WRITE_BLOCK_REQUEST, ptask->dest_node,
			ptask->generation, ptask->speed, ptask->fifo_addr,
			ptask->skb->data, tx_len, fwnet_write_complete, ptask);

	spin_lock_irqsave(&dev->lock, flags);
1029 1030

	/* If the AT tasklet already ran, we may be last user. */
1031
	free = (ptask->outstanding_pkts == 0 && !ptask->enqueued);
1032
	if (!free)
1033 1034
		ptask->enqueued = true;
	else
1035
		dec_queued_datagrams(dev);
1036

1037 1038
	spin_unlock_irqrestore(&dev->lock, flags);

1039
	dev->netdev->trans_start = jiffies;
1040 1041 1042
 out:
	if (free)
		fwnet_free_ptask(ptask);
1043

Jay Fenlason's avatar
Jay Fenlason committed
1044 1045 1046
	return 0;
}

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
static void fwnet_fifo_stop(struct fwnet_device *dev)
{
	if (dev->local_fifo == FWNET_NO_FIFO_ADDR)
		return;

	fw_core_remove_address_handler(&dev->handler);
	dev->local_fifo = FWNET_NO_FIFO_ADDR;
}

static int fwnet_fifo_start(struct fwnet_device *dev)
{
	int retval;

	if (dev->local_fifo != FWNET_NO_FIFO_ADDR)
		return 0;

	dev->handler.length = 4096;
	dev->handler.address_callback = fwnet_receive_packet;
	dev->handler.callback_data = dev;

	retval = fw_core_add_address_handler(&dev->handler,
					     &fw_high_memory_region);
	if (retval < 0)
		return retval;

	dev->local_fifo = dev->handler.offset;

	return 0;
}

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
static void __fwnet_broadcast_stop(struct fwnet_device *dev)
{
	unsigned u;

	if (dev->broadcast_state != FWNET_BROADCAST_ERROR) {
		for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++)
			kunmap(dev->broadcast_rcv_buffer.pages[u]);
		fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, dev->card);
	}
	if (dev->broadcast_rcv_context) {
		fw_iso_context_destroy(dev->broadcast_rcv_context);
		dev->broadcast_rcv_context = NULL;
	}
	kfree(dev->broadcast_rcv_buffer_ptrs);
	dev->broadcast_rcv_buffer_ptrs = NULL;
	dev->broadcast_state = FWNET_BROADCAST_ERROR;
}

static void fwnet_broadcast_stop(struct fwnet_device *dev)
{
	if (dev->broadcast_state == FWNET_BROADCAST_ERROR)
		return;
	fw_iso_context_stop(dev->broadcast_rcv_context);
	__fwnet_broadcast_stop(dev);
}

1103 1104
static int fwnet_broadcast_start(struct fwnet_device *dev)
{
Jay Fenlason's avatar
Jay Fenlason committed
1105 1106 1107 1108 1109 1110
	struct fw_iso_context *context;
	int retval;
	unsigned num_packets;
	unsigned max_receive;
	struct fw_iso_packet packet;
	unsigned long offset;
1111
	void **ptrptr;
Jay Fenlason's avatar
Jay Fenlason committed
1112 1113
	unsigned u;

1114 1115 1116
	if (dev->broadcast_state != FWNET_BROADCAST_ERROR)
		return 0;

1117 1118 1119
	max_receive = 1U << (dev->card->max_receive + 1);
	num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive;

1120 1121 1122
	ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL);
	if (!ptrptr) {
		retval = -ENOMEM;
1123
		goto failed;
1124 1125 1126
	}
	dev->broadcast_rcv_buffer_ptrs = ptrptr;

1127 1128 1129 1130 1131 1132
	context = fw_iso_context_create(dev->card, FW_ISO_CONTEXT_RECEIVE,
					IEEE1394_BROADCAST_CHANNEL,
					dev->card->link_speed, 8,
					fwnet_receive_broadcast, dev);
	if (IS_ERR(context)) {
		retval = PTR_ERR(context);
1133
		goto failed;
1134
	}
1135

1136 1137 1138
	retval = fw_iso_buffer_init(&dev->broadcast_rcv_buffer, dev->card,
				    FWNET_ISO_PAGE_COUNT, DMA_FROM_DEVICE);
	if (retval < 0)
1139 1140 1141
		goto failed;

	dev->broadcast_state = FWNET_BROADCAST_STOPPED;
1142

1143 1144 1145
	for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) {
		void *ptr;
		unsigned v;
Jay Fenlason's avatar
Jay Fenlason committed
1146

1147 1148 1149
		ptr = kmap(dev->broadcast_rcv_buffer.pages[u]);
		for (v = 0; v < num_packets / FWNET_ISO_PAGE_COUNT; v++)
			*ptrptr++ = (void *) ((char *)ptr + v * max_receive);
1150
	}
1151
	dev->broadcast_rcv_context = context;
Jay Fenlason's avatar
Jay Fenlason committed
1152 1153 1154 1155 1156 1157

	packet.payload_length = max_receive;
	packet.interrupt = 1;
	packet.skip = 0;
	packet.tag = 3;
	packet.sy = 0;
1158
	packet.header_length = IEEE1394_GASP_HDR_SIZE;
Jay Fenlason's avatar
Jay Fenlason committed
1159
	offset = 0;
1160 1161 1162 1163 1164

	for (u = 0; u < num_packets; u++) {
		retval = fw_iso_context_queue(context, &packet,
				&dev->broadcast_rcv_buffer, offset);
		if (retval < 0)
1165
			goto failed;
1166

Jay Fenlason's avatar
Jay Fenlason committed
1167 1168
		offset += max_receive;
	}
1169 1170 1171 1172 1173 1174
	dev->num_broadcast_rcv_ptrs = num_packets;
	dev->rcv_buffer_size = max_receive;
	dev->broadcast_rcv_next_ptr = 0U;
	retval = fw_iso_context_start(context, -1, 0,
			FW_ISO_CONTEXT_MATCH_ALL_TAGS); /* ??? sync */
	if (retval < 0)
1175
		goto failed;
1176 1177 1178 1179 1180 1181

	/* FIXME: adjust it according to the min. speed of all known peers? */
	dev->broadcast_xmt_max_payload = IEEE1394_MAX_PAYLOAD_S100
			- IEEE1394_GASP_HDR_SIZE - RFC2374_UNFRAG_HDR_SIZE;
	dev->broadcast_state = FWNET_BROADCAST_RUNNING;

Jay Fenlason's avatar
Jay Fenlason committed
1182 1183
	return 0;

1184 1185
 failed:
	__fwnet_broadcast_stop(dev);
Jay Fenlason's avatar
Jay Fenlason committed
1186 1187 1188
	return retval;
}

1189 1190 1191 1192 1193 1194 1195 1196
static void set_carrier_state(struct fwnet_device *dev)
{
	if (dev->peer_count > 1)
		netif_carrier_on(dev->netdev);
	else
		netif_carrier_off(dev->netdev);
}

1197 1198 1199 1200
/* ifup */
static int fwnet_open(struct net_device *net)
{
	struct fwnet_device *dev = netdev_priv(net);
Jay Fenlason's avatar
Jay Fenlason committed
1201 1202
	int ret;

1203 1204
	ret = fwnet_broadcast_start(dev);
	if (ret)
1205
		return ret;
1206

1207 1208
	netif_start_queue(net);

1209 1210 1211 1212
	spin_lock_irq(&dev->lock);
	set_carrier_state(dev);
	spin_unlock_irq(&dev->lock);

Jay Fenlason's avatar
Jay Fenlason committed
1213 1214 1215
	return 0;
}

1216 1217
/* ifdown */
static int fwnet_stop(struct net_device *net)
Jay Fenlason's avatar
Jay Fenlason committed
1218
{
1219 1220
	struct fwnet_device *dev = netdev_priv(net);

1221
	netif_stop_queue(net);
1222
	fwnet_broadcast_stop(dev);
Jay Fenlason's avatar
Jay Fenlason committed
1223 1224 1225 1226

	return 0;
}

1227
static netdev_tx_t fwnet_tx(struct sk_buff *skb, struct net_device *net)
Jay Fenlason's avatar
Jay Fenlason committed
1228
{
1229 1230
	struct fwnet_header hdr_buf;
	struct fwnet_device *dev = netdev_priv(net);
Jay Fenlason's avatar
Jay Fenlason committed
1231 1232 1233 1234 1235
	__be16 proto;
	u16 dest_node;
	unsigned max_payload;
	u16 dg_size;
	u16 *datagram_label_ptr;
1236
	struct fwnet_packet_task *ptask;
1237 1238
	struct fwnet_peer *peer;
	unsigned long flags;
Jay Fenlason's avatar
Jay Fenlason committed
1239

1240 1241 1242 1243 1244 1245 1246 1247 1248
	spin_lock_irqsave(&dev->lock, flags);

	/* Can this happen? */
	if (netif_queue_stopped(dev->netdev)) {
		spin_unlock_irqrestore(&dev->lock, flags);

		return NETDEV_TX_BUSY;
	}

1249
	ptask = kmem_cache_alloc(fwnet_packet_task_cache, GFP_ATOMIC);
Jay Fenlason's avatar
Jay Fenlason committed
1250 1251 1252 1253 1254 1255 1256 1257
	if (ptask == NULL)
		goto fail;

	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
		goto fail;

	/*
1258
	 * Make a copy of the driver-specific header.
Jay Fenlason's avatar
Jay Fenlason committed
1259 1260 1261 1262
	 * We might need to rebuild the header on tx failure.
	 */
	memcpy(&hdr_buf, skb->data, sizeof(hdr_buf));
	proto = hdr_buf.h_proto;
1263 1264 1265 1266

	switch (proto) {
	case htons(ETH_P_ARP):
	case htons(ETH_P_IP):
1267 1268 1269
#if IS_ENABLED(CONFIG_IPV6)
	case htons(ETH_P_IPV6):
#endif
1270 1271 1272 1273 1274 1275
		break;
	default:
		goto fail;
	}

	skb_pull(skb, sizeof(hdr_buf));
Jay Fenlason's avatar
Jay Fenlason committed
1276 1277 1278 1279 1280 1281
	dg_size = skb->len;

	/*
	 * Set the transmission type for the packet.  ARP packets and IP
	 * broadcast packets are sent via GASP.
	 */
1282
	if (fwnet_hwaddr_is_multicast(hdr_buf.h_dest)) {
1283
		max_payload        = dev->broadcast_xmt_max_payload;
1284 1285
		datagram_label_ptr = &dev->broadcast_xmt_datagramlabel;

1286 1287 1288 1289
		ptask->fifo_addr   = FWNET_NO_FIFO_ADDR;
		ptask->generation  = 0;
		ptask->dest_node   = IEEE1394_ALL_NODES;
		ptask->speed       = SCODE_100;
Jay Fenlason's avatar
Jay Fenlason committed
1290
	} else {
1291 1292
		union fwnet_hwaddr *ha = (union fwnet_hwaddr *)hdr_buf.h_dest;
		__be64 guid = get_unaligned(&ha->uc.uniq_id);
Jay Fenlason's avatar
Jay Fenlason committed
1293 1294
		u8 generation;

1295
		peer = fwnet_peer_find_by_guid(dev, be64_to_cpu(guid));
1296
		if (!peer)
1297
			goto fail;
Jay Fenlason's avatar
Jay Fenlason committed
1298

1299 1300 1301
		generation         = peer->generation;
		dest_node          = peer->node_id;
		max_payload        = peer->max_payload;
1302
		datagram_label_ptr = &peer->datagram_label;
Jay Fenlason's avatar
Jay Fenlason committed
1303

1304
		ptask->fifo_addr   = fwnet_hwaddr_fifo(ha);
1305 1306 1307
		ptask->generation  = generation;
		ptask->dest_node   = dest_node;
		ptask->speed       = peer->speed;
Jay Fenlason's avatar
Jay Fenlason committed
1308 1309 1310 1311 1312
	}

	ptask->hdr.w0 = 0;
	ptask->hdr.w1 = 0;
	ptask->skb = skb;
1313 1314
	ptask->dev = dev;

Jay Fenlason's avatar
Jay Fenlason committed
1315
	/* Does it all fit in one packet? */
1316 1317
	if (dg_size <= max_payload) {
		fwnet_make_uf_hdr(&ptask->hdr, ntohs(proto));
Jay Fenlason's avatar
Jay Fenlason committed
1318
		ptask->outstanding_pkts = 1;
1319
		max_payload = dg_size + RFC2374_UNFRAG_HDR_SIZE;
Jay Fenlason's avatar
Jay Fenlason committed
1320 1321 1322
	} else {
		u16 datagram_label;

1323
		max_payload -= RFC2374_FRAG_OVERHEAD;
Jay Fenlason's avatar
Jay Fenlason committed
1324
		datagram_label = (*datagram_label_ptr)++;
1325 1326
		fwnet_make_ff_hdr(&ptask->hdr, ntohs(proto), dg_size,
				  datagram_label);
Jay Fenlason's avatar
Jay Fenlason committed
1327
		ptask->outstanding_pkts = DIV_ROUND_UP(dg_size, max_payload);
1328
		max_payload += RFC2374_FRAG_HDR_SIZE;
Jay Fenlason's avatar
Jay Fenlason committed
1329
	}
1330

1331 1332
	if (++dev->queued_datagrams == FWNET_MAX_QUEUED_DATAGRAMS)
		netif_stop_queue(dev->netdev);
1333

1334 1335
	spin_unlock_irqrestore(&dev->lock, flags);

Jay Fenlason's avatar
Jay Fenlason committed
1336
	ptask->max_payload = max_payload;
1337
	ptask->enqueued    = 0;
1338

1339 1340
	fwnet_send_packet(ptask);

Jay Fenlason's avatar
Jay Fenlason committed
1341 1342 1343
	return NETDEV_TX_OK;

 fail:
1344 1345
	spin_unlock_irqrestore(&dev->lock, flags);

Jay Fenlason's avatar
Jay Fenlason committed
1346
	if (ptask)
1347
		kmem_cache_free(fwnet_packet_task_cache, ptask);
Jay Fenlason's avatar
Jay Fenlason committed
1348 1349 1350 1351

	if (skb != NULL)
		dev_kfree_skb(skb);

1352 1353
	net->stats.tx_dropped++;
	net->stats.tx_errors++;
Jay Fenlason's avatar
Jay Fenlason committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

	/*
	 * FIXME: According to a patch from 2003-02-26, "returning non-zero
	 * causes serious problems" here, allegedly.  Before that patch,
	 * -ERRNO was returned which is not appropriate under Linux 2.6.
	 * Perhaps more needs to be done?  Stop the queue in serious
	 * conditions and restart it elsewhere?
	 */
	return NETDEV_TX_OK;
}

1365 1366
static int fwnet_change_mtu(struct net_device *net, int new_mtu)
{
Jay Fenlason's avatar
Jay Fenlason committed
1367 1368 1369
	if (new_mtu < 68)
		return -EINVAL;

1370
	net->mtu = new_mtu;
Jay Fenlason's avatar
Jay Fenlason committed
1371 1372 1373
	return 0;
}

1374 1375 1376 1377
static const struct ethtool_ops fwnet_ethtool_ops = {
	.get_link	= ethtool_op_get_link,
};

1378 1379 1380 1381 1382
static const struct net_device_ops fwnet_netdev_ops = {
	.ndo_open       = fwnet_open,
	.ndo_stop	= fwnet_stop,
	.ndo_start_xmit = fwnet_tx,
	.ndo_change_mtu = fwnet_change_mtu,
Jay Fenlason's avatar
Jay Fenlason committed
1383 1384
};

1385 1386 1387 1388
static void fwnet_init_dev(struct net_device *net)
{
	net->header_ops		= &fwnet_header_ops;
	net->netdev_ops		= &fwnet_netdev_ops;
1389
	net->watchdog_timeo	= 2 * HZ;
1390 1391 1392 1393 1394
	net->flags		= IFF_BROADCAST | IFF_MULTICAST;
	net->features		= NETIF_F_HIGHDMA;
	net->addr_len		= FWNET_ALEN;
	net->hard_header_len	= FWNET_HLEN;
	net->type		= ARPHRD_IEEE1394;
1395
	net->tx_queue_len	= FWNET_TX_QUEUE_LEN;
1396
	net->ethtool_ops	= &fwnet_ethtool_ops;
Jay Fenlason's avatar
Jay Fenlason committed
1397 1398
}

1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
/* caller must hold fwnet_device_mutex */
static struct fwnet_device *fwnet_dev_find(struct fw_card *card)
{
	struct fwnet_device *dev;

	list_for_each_entry(dev, &fwnet_device_list, dev_link)
		if (dev->card == card)
			return dev;

	return NULL;
}

static int fwnet_add_peer(struct fwnet_device *dev,
			  struct fw_unit *unit, struct fw_device *device)
{
	struct fwnet_peer *peer;

	peer = kmalloc(sizeof(*peer), GFP_KERNEL);
	if (!peer)
		return -ENOMEM;

1420 1421
	dev_set_drvdata(&unit->device, peer);

1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
	peer->dev = dev;
	peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
	INIT_LIST_HEAD(&peer->pd_list);
	peer->pdg_size = 0;
	peer->datagram_label = 0;
	peer->speed = device->max_speed;
	peer->max_payload = fwnet_max_payload(device->max_rec, peer->speed);

	peer->generation = device->generation;
	smp_rmb();
	peer->node_id = device->node_id;

	spin_lock_irq(&dev->lock);
	list_add_tail(&peer->peer_link, &dev->peer_list);
1436
	dev->peer_count++;
1437
	set_carrier_state(dev);
1438 1439 1440 1441 1442
	spin_unlock_irq(&dev->lock);

	return 0;
}

1443 1444
static int fwnet_probe(struct fw_unit *unit,
		       const struct ieee1394_device_id *id)
1445 1446 1447 1448
{
	struct fw_device *device = fw_parent_device(unit);
	struct fw_card *card = device->card;
	struct net_device *net;
1449
	bool allocated_netdev = false;
1450
	struct fwnet_device *dev;
Jay Fenlason's avatar
Jay Fenlason committed
1451
	unsigned max_mtu;
1452
	int ret;
1453
	union fwnet_hwaddr *ha;
Jay Fenlason's avatar
Jay Fenlason committed
1454

1455
	mutex_lock(&fwnet_device_mutex);
Jay Fenlason's avatar
Jay Fenlason committed
1456

1457 1458 1459 1460
	dev = fwnet_dev_find(card);
	if (dev) {
		net = dev->netdev;
		goto have_dev;
Jay Fenlason's avatar
Jay Fenlason committed
1461
	}
1462

1463 1464
	net = alloc_netdev(sizeof(*dev), "firewire%d", fwnet_init_dev);
	if (net == NULL) {
1465
		ret = -ENOMEM;
Jay Fenlason's avatar
Jay Fenlason committed
1466 1467 1468
		goto out;
	}

1469
	allocated_netdev = true;
1470 1471
	SET_NETDEV_DEV(net, card->device);
	dev = netdev_priv(net);
Jay Fenlason's avatar
Jay Fenlason committed
1472

1473 1474 1475 1476 1477 1478
	spin_lock_init(&dev->lock);
	dev->broadcast_state = FWNET_BROADCAST_ERROR;
	dev->broadcast_rcv_context = NULL;
	dev->broadcast_xmt_max_payload = 0;
	dev->broadcast_xmt_datagramlabel = 0;
	dev->local_fifo = FWNET_NO_FIFO_ADDR;
1479
	dev->queued_datagrams = 0;
1480
	INIT_LIST_HEAD(&dev->peer_list);
1481
	dev->card = card;
1482
	dev->netdev = net;
Jay Fenlason's avatar
Jay Fenlason committed
1483

1484 1485 1486 1487 1488
	ret = fwnet_fifo_start(dev);
	if (ret < 0)
		goto out;
	dev->local_fifo = dev->handler.offset;

Jay Fenlason's avatar
Jay Fenlason committed
1489 1490 1491 1492 1493
	/*
	 * Use the RFC 2734 default 1500 octets or the maximum payload
	 * as initial MTU
	 */
	max_mtu = (1 << (card->max_receive + 1))
1494 1495
		  - sizeof(struct rfc2734_header) - IEEE1394_GASP_HDR_SIZE;
	net->mtu = min(1500U, max_mtu);
Jay Fenlason's avatar
Jay Fenlason committed
1496 1497

	/* Set our hardware address while we're at it */
1498 1499 1500 1501 1502 1503 1504 1505 1506
	ha = (union fwnet_hwaddr *)net->dev_addr;
	put_unaligned_be64(card->guid, &ha->uc.uniq_id);
	ha->uc.max_rec = dev->card->max_receive;
	ha->uc.sspd = dev->card->link_speed;
	put_unaligned_be16(dev->local_fifo >> 32, &ha->uc.fifo_hi);
	put_unaligned_be32(dev->local_fifo & 0xffffffff, &ha->uc.fifo_lo);

	memset(net->broadcast, -1, net->addr_len);

1507
	ret = register_netdev(net);
1508
	if (ret)
Jay Fenlason's avatar
Jay Fenlason committed
1509 1510
		goto out;

1511
	list_add_tail(&dev->dev_link, &fwnet_device_list);
1512
	dev_notice(&net->dev, "IP over IEEE 1394 on card %s\n",
1513
		   dev_name(card->device));
1514 1515
 have_dev:
	ret = fwnet_add_peer(dev, unit, device);
1516
	if (ret && allocated_netdev) {
1517 1518
		unregister_netdev(net);
		list_del(&dev->dev_link);
Jay Fenlason's avatar
Jay Fenlason committed
1519
 out:
1520
		fwnet_fifo_stop(dev);
1521
		free_netdev(net);
1522
	}
1523

1524 1525 1526 1527 1528
	mutex_unlock(&fwnet_device_mutex);

	return ret;
}

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
/*
 * FIXME abort partially sent fragmented datagrams,
 * discard partially received fragmented datagrams
 */
static void fwnet_update(struct fw_unit *unit)
{
	struct fw_device *device = fw_parent_device(unit);
	struct fwnet_peer *peer = dev_get_drvdata(&unit->device);
	int generation;

	generation = device->generation;

	spin_lock_irq(&peer->dev->lock);
	peer->node_id    = device->node_id;
	peer->generation = generation;
	spin_unlock_irq(&peer->dev->lock);
}

1547
static void fwnet_remove_peer(struct fwnet_peer *peer, struct fwnet_device *dev)
1548 1549 1550
{
	struct fwnet_partial_datagram *pd, *pd_next;

1551
	spin_lock_irq(&dev->lock);
1552
	list_del(&peer->peer_link);
1553 1554 1555
	dev->peer_count--;
	set_carrier_state(dev);
	spin_unlock_irq(&dev->lock);
1556 1557 1558 1559 1560

	list_for_each_entry_safe(pd, pd_next, &peer->pd_list, pd_link)
		fwnet_pd_delete(pd);

	kfree(peer);
Jay Fenlason's avatar
Jay Fenlason committed
1561 1562
}

1563
static void fwnet_remove(struct fw_unit *unit)
1564
{
1565
	struct fwnet_peer *peer = dev_get_drvdata(&unit->device);
1566
	struct fwnet_device *dev = peer->dev;
1567
	struct net_device *net;
1568
	int i;
1569

1570
	mutex_lock(&fwnet_device_mutex);
Jay Fenlason's avatar
Jay Fenlason committed
1571

1572 1573
	net = dev->netdev;

1574
	fwnet_remove_peer(peer, dev);
1575

1576
	if (list_empty(&dev->peer_list)) {
1577 1578
		unregister_netdev(net);

1579 1580
		fwnet_fifo_stop(dev);

1581 1582 1583
		for (i = 0; dev->queued_datagrams && i < 5; i++)
			ssleep(1);
		WARN_ON(dev->queued_datagrams);
1584 1585
		list_del(&dev->dev_link);

1586
		free_netdev(net);
Jay Fenlason's avatar
Jay Fenlason committed
1587
	}
1588

1589
	mutex_unlock(&fwnet_device_mutex);
Jay Fenlason's avatar
Jay Fenlason committed
1590 1591
}

1592 1593 1594 1595 1596 1597 1598
static const struct ieee1394_device_id fwnet_id_table[] = {
	{
		.match_flags  = IEEE1394_MATCH_SPECIFIER_ID |
				IEEE1394_MATCH_VERSION,
		.specifier_id = IANA_SPECIFIER_ID,
		.version      = RFC2734_SW_VERSION,
	},
1599 1600 1601 1602 1603 1604 1605 1606
#if IS_ENABLED(CONFIG_IPV6)
	{
		.match_flags  = IEEE1394_MATCH_SPECIFIER_ID |
				IEEE1394_MATCH_VERSION,
		.specifier_id = IANA_SPECIFIER_ID,
		.version      = RFC3146_SW_VERSION,
	},
#endif
1607 1608 1609 1610
	{ }
};

static struct fw_driver fwnet_driver = {
Jay Fenlason's avatar
Jay Fenlason committed
1611
	.driver = {
1612
		.owner  = THIS_MODULE,
1613
		.name   = KBUILD_MODNAME,
1614
		.bus    = &fw_bus_type,
Jay Fenlason's avatar
Jay Fenlason committed
1615
	},
1616
	.probe    = fwnet_probe,
1617
	.update   = fwnet_update,
1618
	.remove   = fwnet_remove,
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	.id_table = fwnet_id_table,
};

static const u32 rfc2374_unit_directory_data[] = {
	0x00040000,	/* directory_length		*/
	0x1200005e,	/* unit_specifier_id: IANA	*/
	0x81000003,	/* textual descriptor offset	*/
	0x13000001,	/* unit_sw_version: RFC 2734	*/
	0x81000005,	/* textual descriptor offset	*/
	0x00030000,	/* descriptor_length		*/
	0x00000000,	/* text				*/
	0x00000000,	/* minimal ASCII, en		*/
	0x49414e41,	/* I A N A			*/
	0x00030000,	/* descriptor_length		*/
	0x00000000,	/* text				*/
	0x00000000,	/* minimal ASCII, en		*/
	0x49507634,	/* I P v 4			*/
};

static struct fw_descriptor rfc2374_unit_directory = {
	.length = ARRAY_SIZE(rfc2374_unit_directory_data),
	.key    = (CSR_DIRECTORY | CSR_UNIT) << 24,
	.data   = rfc2374_unit_directory_data
Jay Fenlason's avatar
Jay Fenlason committed
1642 1643
};

1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
#if IS_ENABLED(CONFIG_IPV6)
static const u32 rfc3146_unit_directory_data[] = {
	0x00040000,	/* directory_length		*/
	0x1200005e,	/* unit_specifier_id: IANA	*/
	0x81000003,	/* textual descriptor offset	*/
	0x13000002,	/* unit_sw_version: RFC 3146	*/
	0x81000005,	/* textual descriptor offset	*/
	0x00030000,	/* descriptor_length		*/
	0x00000000,	/* text				*/
	0x00000000,	/* minimal ASCII, en		*/
	0x49414e41,	/* I A N A			*/
	0x00030000,	/* descriptor_length		*/
	0x00000000,	/* text				*/
	0x00000000,	/* minimal ASCII, en		*/
	0x49507636,	/* I P v 6			*/
};

static struct fw_descriptor rfc3146_unit_directory = {
	.length = ARRAY_SIZE(rfc3146_unit_directory_data),
	.key    = (CSR_DIRECTORY | CSR_UNIT) << 24,
	.data   = rfc3146_unit_directory_data
};
#endif

1668 1669 1670 1671 1672 1673 1674
static int __init fwnet_init(void)
{
	int err;

	err = fw_core_add_descriptor(&rfc2374_unit_directory);
	if (err)
		return err;
Jay Fenlason's avatar
Jay Fenlason committed
1675

1676 1677 1678 1679 1680 1681
#if IS_ENABLED(CONFIG_IPV6)
	err = fw_core_add_descriptor(&rfc3146_unit_directory);
	if (err)
		goto out;
#endif

1682 1683 1684 1685
	fwnet_packet_task_cache = kmem_cache_create("packet_task",
			sizeof(struct fwnet_packet_task), 0, 0, NULL);
	if (!fwnet_packet_task_cache) {
		err = -ENOMEM;
1686
		goto out2;
1687 1688 1689 1690 1691 1692 1693
	}

	err = driver_register(&fwnet_driver.driver);
	if (!err)
		return 0;

	kmem_cache_destroy(fwnet_packet_task_cache);
1694 1695 1696
out2:
#if IS_ENABLED(CONFIG_IPV6)
	fw_core_remove_descriptor(&rfc3146_unit_directory);
1697
out:
1698
#endif
1699 1700 1701
	fw_core_remove_descriptor(&rfc2374_unit_directory);

	return err;
Jay Fenlason's avatar
Jay Fenlason committed
1702
}
1703
module_init(fwnet_init);
Jay Fenlason's avatar
Jay Fenlason committed
1704

1705 1706 1707 1708
static void __exit fwnet_cleanup(void)
{
	driver_unregister(&fwnet_driver.driver);
	kmem_cache_destroy(fwnet_packet_task_cache);
1709 1710 1711
#if IS_ENABLED(CONFIG_IPV6)
	fw_core_remove_descriptor(&rfc3146_unit_directory);
#endif
1712
	fw_core_remove_descriptor(&rfc2374_unit_directory);
Jay Fenlason's avatar
Jay Fenlason committed
1713
}
1714
module_exit(fwnet_cleanup);
Jay Fenlason's avatar
Jay Fenlason committed
1715

1716
MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>");
1717
MODULE_DESCRIPTION("IP over IEEE1394 as per RFC 2734/3146");
1718 1719
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table);