net.c 44.1 KB
Newer Older
J
Jay Fenlason 已提交
1 2 3 4 5 6 7 8
/*
 * IPv4 over IEEE 1394, per RFC 2734
 *
 * Copyright (C) 2009 Jay Fenlason <fenlason@redhat.com>
 *
 * based on eth1394 by Ben Collins et al
 */

S
Stefan Richter 已提交
9
#include <linux/bug.h>
J
Jay Fenlason 已提交
10 11 12 13 14 15 16
#include <linux/device.h>
#include <linux/ethtool.h>
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/highmem.h>
#include <linux/in.h>
#include <linux/ip.h>
S
Stefan Richter 已提交
17
#include <linux/jiffies.h>
J
Jay Fenlason 已提交
18 19 20 21 22 23 24 25 26
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>

#include <asm/unaligned.h>
#include <net/arp.h>

S
Stefan Richter 已提交
27 28
#define FWNET_MAX_FRAGMENTS	25	/* arbitrary limit */
#define FWNET_ISO_PAGE_COUNT	(PAGE_SIZE < 16 * 1024 ? 4 : 2)
J
Jay Fenlason 已提交
29

S
Stefan Richter 已提交
30 31 32 33
#define IEEE1394_BROADCAST_CHANNEL	31
#define IEEE1394_ALL_NODES		(0xffc0 | 0x003f)
#define IEEE1394_MAX_PAYLOAD_S100	512
#define FWNET_NO_FIFO_ADDR		(~0ULL)
J
Jay Fenlason 已提交
34

S
Stefan Richter 已提交
35 36
#define IANA_SPECIFIER_ID		0x00005eU
#define RFC2734_SW_VERSION		0x000001U
J
Jay Fenlason 已提交
37

S
Stefan Richter 已提交
38
#define IEEE1394_GASP_HDR_SIZE	8
J
Jay Fenlason 已提交
39

S
Stefan Richter 已提交
40 41 42
#define RFC2374_UNFRAG_HDR_SIZE	4
#define RFC2374_FRAG_HDR_SIZE	8
#define RFC2374_FRAG_OVERHEAD	4
J
Jay Fenlason 已提交
43

S
Stefan Richter 已提交
44 45 46 47
#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	*/
J
Jay Fenlason 已提交
48

S
Stefan Richter 已提交
49
#define RFC2734_HW_ADDR_LEN	16
J
Jay Fenlason 已提交
50

S
Stefan Richter 已提交
51 52 53 54 55 56 57
struct rfc2734_arp {
	__be16 hw_type;		/* 0x0018	*/
	__be16 proto_type;	/* 0x0806       */
	u8 hw_addr_len;		/* 16		*/
	u8 ip_addr_len;		/* 4		*/
	__be16 opcode;		/* ARP Opcode	*/
	/* Above is exactly the same format as struct arphdr */
J
Jay Fenlason 已提交
58

S
Stefan Richter 已提交
59 60 61 62 63 64 65 66
	__be64 s_uniq_id;	/* Sender's 64bit EUI			*/
	u8 max_rec;		/* Sender's max packet size		*/
	u8 sspd;		/* Sender's max speed			*/
	__be16 fifo_hi;		/* hi 16bits of sender's FIFO addr	*/
	__be32 fifo_lo;		/* lo 32bits of sender's FIFO addr	*/
	__be32 sip;		/* Sender's IP Address			*/
	__be32 tip;		/* IP Address of requested hw addr	*/
} __attribute__((packed));
J
Jay Fenlason 已提交
67

S
Stefan Richter 已提交
68 69 70 71 72 73 74
/* This header format is specific to this driver implementation. */
#define FWNET_ALEN	8
#define FWNET_HLEN	10
struct fwnet_header {
	u8 h_dest[FWNET_ALEN];	/* destination address */
	__be16 h_proto;		/* packet type ID field */
} __attribute__((packed));
J
Jay Fenlason 已提交
75

S
Stefan Richter 已提交
76 77
/* IPv4 and IPv6 encapsulation header */
struct rfc2734_header {
J
Jay Fenlason 已提交
78 79 80 81
	u32 w0;
	u32 w1;
};

S
Stefan Richter 已提交
82 83 84 85 86
#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)
J
Jay Fenlason 已提交
87

S
Stefan Richter 已提交
88 89 90 91
#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)
J
Jay Fenlason 已提交
92

S
Stefan Richter 已提交
93
#define fwnet_set_hdr_dgl(dgl)		((dgl) << 16)
J
Jay Fenlason 已提交
94

S
Stefan Richter 已提交
95 96 97 98 99 100
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);
}
J
Jay Fenlason 已提交
101

S
Stefan Richter 已提交
102 103 104 105 106 107 108 109
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);
}
J
Jay Fenlason 已提交
110

S
Stefan Richter 已提交
111 112 113 114 115 116 117 118
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);
}
J
Jay Fenlason 已提交
119 120

/* This list keeps track of what parts of the datagram have been filled in */
S
Stefan Richter 已提交
121 122
struct fwnet_fragment_info {
	struct list_head fi_link;
J
Jay Fenlason 已提交
123 124 125 126
	u16 offset;
	u16 len;
};

S
Stefan Richter 已提交
127 128 129
struct fwnet_partial_datagram {
	struct list_head pd_link;
	struct list_head fi_list;
J
Jay Fenlason 已提交
130 131 132 133 134 135 136 137 138 139 140
	struct sk_buff *skb;
	/* FIXME Why not use skb->data? */
	char *pbuf;
	u16 datagram_label;
	u16 ether_type;
	u16 datagram_size;
};

/*
 * We keep one of these for each IPv4 capable device attached to a fw_card.
 * The list of them is stored in the fw_card structure rather than in the
S
Stefan Richter 已提交
141 142
 * fwnet_device because the remote IPv4 nodes may be probed before the card is,
 * so we need a place to store them before the fwnet_device structure is
J
Jay Fenlason 已提交
143 144
 * allocated.
 */
S
Stefan Richter 已提交
145 146 147
struct fwnet_peer {
	struct list_head peer_link;
	/* guid of the remote peer */
J
Jay Fenlason 已提交
148
	u64 guid;
S
Stefan Richter 已提交
149
	/* FIFO address to transmit datagrams to, or FWNET_NO_FIFO_ADDR */
J
Jay Fenlason 已提交
150 151 152
	u64 fifo;

	spinlock_t pdg_lock;	/* partial datagram lock		*/
S
Stefan Richter 已提交
153 154 155
	/* List of partial datagrams received from this peer */
	struct list_head pd_list;
	/* Number of entries in pd_list at the moment */
J
Jay Fenlason 已提交
156 157
	unsigned pdg_size;

S
Stefan Richter 已提交
158 159
	/* max payload to transmit to this remote peer */
	/* This already includes the RFC2374_FRAG_HDR_SIZE overhead */
J
Jay Fenlason 已提交
160 161 162
	u16 max_payload;
	/* outgoing datagram label */
	u16 datagram_label;
S
Stefan Richter 已提交
163 164 165
	/* Current node_id of the remote peer */
	u16 node_id;
	/* current generation of the remote peer */
J
Jay Fenlason 已提交
166
	u8 generation;
S
Stefan Richter 已提交
167
	/* max speed that this peer can receive at */
J
Jay Fenlason 已提交
168 169 170
	u8 xmt_speed;
};

S
Stefan Richter 已提交
171
struct fwnet_device {
J
Jay Fenlason 已提交
172
	spinlock_t lock;
S
Stefan Richter 已提交
173 174 175 176 177
	enum {
		FWNET_BROADCAST_ERROR,
		FWNET_BROADCAST_RUNNING,
		FWNET_BROADCAST_STOPPED,
	} broadcast_state;
J
Jay Fenlason 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	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;

	/*
S
Stefan Richter 已提交
193
	 * The CSR address that remote nodes must send datagrams to for us to
J
Jay Fenlason 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	 * receive them.
	 */
	struct fw_address_handler handler;
	u64 local_fifo;

	/* List of packets to be sent */
	struct list_head packet_list;
	/*
	 * List of packets that were broadcasted.  When we get an ISO interrupt
	 * one of them has been sent
	 */
	struct list_head broadcasted_list;
	/* List of packets that have been sent but not yet acked */
	struct list_head sent_list;

	struct fw_card *card;
};

/* This is our task struct. It's used for the packet complete callback.  */
S
Stefan Richter 已提交
213
struct fwnet_packet_task {
J
Jay Fenlason 已提交
214
	/*
S
Stefan Richter 已提交
215 216
	 * ptask can actually be on dev->packet_list, dev->broadcasted_list,
	 * or dev->sent_list depending on its current state.
J
Jay Fenlason 已提交
217
	 */
S
Stefan Richter 已提交
218
	struct list_head pt_link;
J
Jay Fenlason 已提交
219
	struct fw_transaction transaction;
S
Stefan Richter 已提交
220
	struct rfc2734_header hdr;
J
Jay Fenlason 已提交
221
	struct sk_buff *skb;
S
Stefan Richter 已提交
222 223
	struct fwnet_device *dev;

J
Jay Fenlason 已提交
224 225 226 227 228 229 230 231
	int outstanding_pkts;
	unsigned max_payload;
	u64 fifo_addr;
	u16 dest_node;
	u8 generation;
	u8 speed;
};

S
Stefan Richter 已提交
232 233 234 235 236 237 238 239 240
/*
 * 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;
J
Jay Fenlason 已提交
241

S
Stefan Richter 已提交
242 243
	h = (struct fwnet_header *)skb_push(skb, sizeof(*h));
	put_unaligned_be16(type, &h->h_proto);
J
Jay Fenlason 已提交
244

S
Stefan Richter 已提交
245 246
	if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) {
		memset(h->h_dest, 0, net->addr_len);
J
Jay Fenlason 已提交
247

S
Stefan Richter 已提交
248
		return net->hard_header_len;
J
Jay Fenlason 已提交
249 250 251
	}

	if (daddr) {
S
Stefan Richter 已提交
252 253 254
		memcpy(h->h_dest, daddr, net->addr_len);

		return net->hard_header_len;
J
Jay Fenlason 已提交
255 256
	}

S
Stefan Richter 已提交
257
	return -net->hard_header_len;
J
Jay Fenlason 已提交
258 259
}

S
Stefan Richter 已提交
260
static int fwnet_header_rebuild(struct sk_buff *skb)
J
Jay Fenlason 已提交
261
{
S
Stefan Richter 已提交
262
	struct fwnet_header *h = (struct fwnet_header *)skb->data;
J
Jay Fenlason 已提交
263

S
Stefan Richter 已提交
264 265
	if (get_unaligned_be16(&h->h_proto) == ETH_P_IP)
		return arp_find((unsigned char *)&h->h_dest, skb);
J
Jay Fenlason 已提交
266

S
Stefan Richter 已提交
267 268
	fw_notify("%s: unable to resolve type %04x addresses\n",
		  skb->dev->name, be16_to_cpu(h->h_proto));
J
Jay Fenlason 已提交
269 270 271
	return 0;
}

S
Stefan Richter 已提交
272 273 274 275 276
static int fwnet_header_cache(const struct neighbour *neigh,
			      struct hh_cache *hh)
{
	struct net_device *net;
	struct fwnet_header *h;
J
Jay Fenlason 已提交
277

S
Stefan Richter 已提交
278
	if (hh->hh_type == cpu_to_be16(ETH_P_802_3))
J
Jay Fenlason 已提交
279
		return -1;
S
Stefan Richter 已提交
280 281 282 283 284
	net = neigh->dev;
	h = (struct fwnet_header *)((u8 *)hh->hh_data + 16 - sizeof(*h));
	h->h_proto = hh->hh_type;
	memcpy(h->h_dest, neigh->ha, net->addr_len);
	hh->hh_len = FWNET_HLEN;
J
Jay Fenlason 已提交
285 286 287 288 289

	return 0;
}

/* Called by Address Resolution module to notify changes in address. */
S
Stefan Richter 已提交
290 291 292 293
static void fwnet_header_cache_update(struct hh_cache *hh,
		const struct net_device *net, const unsigned char *haddr)
{
	memcpy((u8 *)hh->hh_data + 16 - FWNET_HLEN, haddr, net->addr_len);
J
Jay Fenlason 已提交
294 295
}

S
Stefan Richter 已提交
296 297 298 299 300
static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{
	memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN);

	return FWNET_ALEN;
J
Jay Fenlason 已提交
301 302
}

S
Stefan Richter 已提交
303 304 305 306 307 308
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,
J
Jay Fenlason 已提交
309 310 311
};

/* FIXME: is this correct for all cases? */
S
Stefan Richter 已提交
312 313
static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd,
			       unsigned offset, unsigned len)
J
Jay Fenlason 已提交
314
{
S
Stefan Richter 已提交
315
	struct fwnet_fragment_info *fi;
J
Jay Fenlason 已提交
316 317
	unsigned end = offset + len;

S
Stefan Richter 已提交
318 319
	list_for_each_entry(fi, &pd->fi_list, fi_link)
		if (offset < fi->offset + fi->len && end > fi->offset)
J
Jay Fenlason 已提交
320
			return true;
S
Stefan Richter 已提交
321

J
Jay Fenlason 已提交
322 323 324 325
	return false;
}

/* Assumes that new fragment does not overlap any existing fragments */
S
Stefan Richter 已提交
326 327 328 329
static struct fwnet_fragment_info *fwnet_frag_new(
	struct fwnet_partial_datagram *pd, unsigned offset, unsigned len)
{
	struct fwnet_fragment_info *fi, *fi2, *new;
J
Jay Fenlason 已提交
330 331
	struct list_head *list;

S
Stefan Richter 已提交
332 333
	list = &pd->fi_list;
	list_for_each_entry(fi, &pd->fi_list, fi_link) {
J
Jay Fenlason 已提交
334 335 336
		if (fi->offset + fi->len == offset) {
			/* The new fragment can be tacked on to the end */
			/* Did the new fragment plug a hole? */
S
Stefan Richter 已提交
337 338
			fi2 = list_entry(fi->fi_link.next,
					 struct fwnet_fragment_info, fi_link);
J
Jay Fenlason 已提交
339 340 341
			if (fi->offset + fi->len == fi2->offset) {
				/* glue fragments together */
				fi->len += len + fi2->len;
S
Stefan Richter 已提交
342
				list_del(&fi2->fi_link);
J
Jay Fenlason 已提交
343 344 345 346
				kfree(fi2);
			} else {
				fi->len += len;
			}
S
Stefan Richter 已提交
347

J
Jay Fenlason 已提交
348 349 350 351 352
			return fi;
		}
		if (offset + len == fi->offset) {
			/* The new fragment can be tacked on to the beginning */
			/* Did the new fragment plug a hole? */
S
Stefan Richter 已提交
353 354
			fi2 = list_entry(fi->fi_link.prev,
					 struct fwnet_fragment_info, fi_link);
J
Jay Fenlason 已提交
355 356 357
			if (fi2->offset + fi2->len == fi->offset) {
				/* glue fragments together */
				fi2->len += fi->len + len;
S
Stefan Richter 已提交
358
				list_del(&fi->fi_link);
J
Jay Fenlason 已提交
359
				kfree(fi);
S
Stefan Richter 已提交
360

J
Jay Fenlason 已提交
361 362 363 364
				return fi2;
			}
			fi->offset = offset;
			fi->len += len;
S
Stefan Richter 已提交
365

J
Jay Fenlason 已提交
366 367 368
			return fi;
		}
		if (offset > fi->offset + fi->len) {
S
Stefan Richter 已提交
369
			list = &fi->fi_link;
J
Jay Fenlason 已提交
370 371 372
			break;
		}
		if (offset + len < fi->offset) {
S
Stefan Richter 已提交
373
			list = fi->fi_link.prev;
J
Jay Fenlason 已提交
374 375 376 377 378 379
			break;
		}
	}

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
	if (!new) {
S
Stefan Richter 已提交
380
		fw_error("out of memory\n");
J
Jay Fenlason 已提交
381 382 383 384 385
		return NULL;
	}

	new->offset = offset;
	new->len = len;
S
Stefan Richter 已提交
386 387
	list_add(&new->fi_link, list);

J
Jay Fenlason 已提交
388 389 390
	return new;
}

S
Stefan Richter 已提交
391 392 393 394 395 396
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;
J
Jay Fenlason 已提交
397 398 399 400

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
	if (!new)
		goto fail;
S
Stefan Richter 已提交
401 402 403 404

	INIT_LIST_HEAD(&new->fi_list);
	fi = fwnet_frag_new(new, frag_off, frag_len);
	if (fi == NULL)
J
Jay Fenlason 已提交
405
		goto fail_w_new;
S
Stefan Richter 已提交
406

J
Jay Fenlason 已提交
407 408
	new->datagram_label = datagram_label;
	new->datagram_size = dg_size;
S
Stefan Richter 已提交
409 410
	new->skb = dev_alloc_skb(dg_size + net->hard_header_len + 15);
	if (new->skb == NULL)
J
Jay Fenlason 已提交
411
		goto fail_w_fi;
S
Stefan Richter 已提交
412 413

	skb_reserve(new->skb, (net->hard_header_len + 15) & ~15);
J
Jay Fenlason 已提交
414 415
	new->pbuf = skb_put(new->skb, dg_size);
	memcpy(new->pbuf + frag_off, frag_buf, frag_len);
S
Stefan Richter 已提交
416 417
	list_add_tail(&new->pd_link, &peer->pd_list);

J
Jay Fenlason 已提交
418 419 420 421 422 423 424
	return new;

fail_w_fi:
	kfree(fi);
fail_w_new:
	kfree(new);
fail:
S
Stefan Richter 已提交
425 426
	fw_error("out of memory\n");

J
Jay Fenlason 已提交
427 428 429
	return NULL;
}

S
Stefan Richter 已提交
430 431 432 433
static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer,
						    u16 datagram_label)
{
	struct fwnet_partial_datagram *pd;
J
Jay Fenlason 已提交
434

S
Stefan Richter 已提交
435 436
	list_for_each_entry(pd, &peer->pd_list, pd_link)
		if (pd->datagram_label == datagram_label)
J
Jay Fenlason 已提交
437
			return pd;
S
Stefan Richter 已提交
438

J
Jay Fenlason 已提交
439 440 441 442
	return NULL;
}


S
Stefan Richter 已提交
443 444 445
static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
{
	struct fwnet_fragment_info *fi, *n;
J
Jay Fenlason 已提交
446

S
Stefan Richter 已提交
447
	list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
J
Jay Fenlason 已提交
448
		kfree(fi);
S
Stefan Richter 已提交
449 450

	list_del(&old->pd_link);
J
Jay Fenlason 已提交
451 452 453 454
	dev_kfree_skb_any(old->skb);
	kfree(old);
}

S
Stefan Richter 已提交
455 456 457 458 459
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)
J
Jay Fenlason 已提交
460
		return false;
S
Stefan Richter 已提交
461

J
Jay Fenlason 已提交
462 463 464 465 466 467
	memcpy(pd->pbuf + frag_off, frag_buf, frag_len);

	/*
	 * Move list entry to beginnig of list so that oldest partial
	 * datagrams percolate to the end of the list
	 */
S
Stefan Richter 已提交
468 469
	list_move_tail(&pd->pd_link, &peer->pd_list);

J
Jay Fenlason 已提交
470 471 472
	return true;
}

S
Stefan Richter 已提交
473 474 475
static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
{
	struct fwnet_fragment_info *fi;
J
Jay Fenlason 已提交
476

S
Stefan Richter 已提交
477
	fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
J
Jay Fenlason 已提交
478

S
Stefan Richter 已提交
479
	return fi->len == pd->datagram_size;
J
Jay Fenlason 已提交
480 481
}

S
Stefan Richter 已提交
482 483 484
static int fwnet_peer_new(struct fw_card *card, struct fw_device *device)
{
	struct fwnet_peer *peer;
J
Jay Fenlason 已提交
485

S
Stefan Richter 已提交
486 487 488
	peer = kmalloc(sizeof(*peer), GFP_KERNEL);
	if (!peer) {
		fw_error("out of memory\n");
J
Jay Fenlason 已提交
489 490 491

		return -ENOMEM;
	}
S
Stefan Richter 已提交
492 493 494 495 496 497
	peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
	peer->fifo = FWNET_NO_FIFO_ADDR;
	INIT_LIST_HEAD(&peer->pd_list);
	spin_lock_init(&peer->pdg_lock);
	peer->pdg_size = 0;
	peer->generation = device->generation;
J
Jay Fenlason 已提交
498
	rmb();
S
Stefan Richter 已提交
499
	peer->node_id = device->node_id;
J
Jay Fenlason 已提交
500
	 /* FIXME what should it really be? */
S
Stefan Richter 已提交
501 502 503 504 505
	peer->max_payload = IEEE1394_MAX_PAYLOAD_S100 - RFC2374_UNFRAG_HDR_SIZE;
	peer->datagram_label = 0U;
	peer->xmt_speed = device->max_speed;
	list_add_tail(&peer->peer_link, &card->peer_list);

J
Jay Fenlason 已提交
506 507 508
	return 0;
}

S
Stefan Richter 已提交
509 510 511 512 513
/* FIXME caller must take the lock, or peer needs to be reference-counted */
static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
						  u64 guid)
{
	struct fwnet_peer *p, *peer = NULL;
J
Jay Fenlason 已提交
514 515
	unsigned long flags;

S
Stefan Richter 已提交
516 517 518 519 520
	spin_lock_irqsave(&dev->lock, flags);
	list_for_each_entry(p, &dev->card->peer_list, peer_link)
		if (p->guid == guid) {
			peer = p;
			break;
J
Jay Fenlason 已提交
521
		}
S
Stefan Richter 已提交
522
	spin_unlock_irqrestore(&dev->lock, flags);
J
Jay Fenlason 已提交
523

S
Stefan Richter 已提交
524
	return peer;
J
Jay Fenlason 已提交
525 526
}

S
Stefan Richter 已提交
527 528 529 530 531 532
/* FIXME caller must take the lock, or peer needs to be reference-counted */
/* FIXME node_id doesn't mean anything without generation */
static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
						     u16 node_id)
{
	struct fwnet_peer *p, *peer = NULL;
J
Jay Fenlason 已提交
533 534
	unsigned long flags;

S
Stefan Richter 已提交
535 536 537 538 539
	spin_lock_irqsave(&dev->lock, flags);
	list_for_each_entry(p, &dev->card->peer_list, peer_link)
		if (p->node_id == node_id) {
			peer = p;
			break;
J
Jay Fenlason 已提交
540
		}
S
Stefan Richter 已提交
541 542 543
	spin_unlock_irqrestore(&dev->lock, flags);

	return peer;
J
Jay Fenlason 已提交
544 545
}

S
Stefan Richter 已提交
546 547 548 549 550 551
/* FIXME */
static void fwnet_peer_delete(struct fw_card *card, struct fw_device *device)
{
	struct net_device *net;
	struct fwnet_device *dev;
	struct fwnet_peer *peer;
J
Jay Fenlason 已提交
552 553
	u64 guid;
	unsigned long flags;
S
Stefan Richter 已提交
554
	struct fwnet_partial_datagram *pd, *pd_next;
J
Jay Fenlason 已提交
555 556

	guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
S
Stefan Richter 已提交
557 558 559
	net = card->netdev;
	if (net)
		dev = netdev_priv(net);
J
Jay Fenlason 已提交
560
	else
S
Stefan Richter 已提交
561 562 563 564 565 566 567 568 569 570
		dev = NULL;
	if (dev)
		spin_lock_irqsave(&dev->lock, flags);

	list_for_each_entry(peer, &card->peer_list, peer_link) {
		if (peer->guid == guid) {
			list_del(&peer->peer_link);
			list_for_each_entry_safe(pd, pd_next, &peer->pd_list,
						 pd_link)
				fwnet_pd_delete(pd);
J
Jay Fenlason 已提交
571 572 573
			break;
		}
	}
S
Stefan Richter 已提交
574 575
	if (dev)
		spin_unlock_irqrestore(&dev->lock, flags);
J
Jay Fenlason 已提交
576 577
}

S
Stefan Richter 已提交
578 579 580 581 582 583
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;
	static const __be64 broadcast_hw = cpu_to_be64(~0ULL);
J
Jay Fenlason 已提交
584
	int status;
S
Stefan Richter 已提交
585
	__be64 guid;
J
Jay Fenlason 已提交
586

S
Stefan Richter 已提交
587
	dev = netdev_priv(net);
J
Jay Fenlason 已提交
588
	/* Write metadata, and then pass to the receive level */
S
Stefan Richter 已提交
589
	skb->dev = net;
J
Jay Fenlason 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603
	skb->ip_summed = CHECKSUM_UNNECESSARY;  /* don't check it */

	/*
	 * Parse the encapsulation header. This actually does the job of
	 * converting to an ethernet frame header, as well as arp
	 * conversion if needed. ARP conversion is easier in this
	 * direction, since we are using ethernet as our backend.
	 */
	/*
	 * If this is an ARP packet, convert it. First, we want to make
	 * use of some of the fields, since they tell us a little bit
	 * about the sending machine.
	 */
	if (ether_type == ETH_P_ARP) {
S
Stefan Richter 已提交
604
		struct rfc2734_arp *arp1394;
J
Jay Fenlason 已提交
605 606 607
		struct arphdr *arp;
		unsigned char *arp_ptr;
		u64 fifo_addr;
S
Stefan Richter 已提交
608
		u64 peer_guid;
J
Jay Fenlason 已提交
609 610 611
		u8 max_rec;
		u8 sspd;
		u16 max_payload;
S
Stefan Richter 已提交
612 613
		struct fwnet_peer *peer;
		static const u16 fwnet_speed_to_max_payload[] = {
J
Jay Fenlason 已提交
614 615 616 617
			/* S100, S200, S400, S800, S1600, S3200 */
			    512, 1024, 2048, 4096,  4096,  4096
		};

S
Stefan Richter 已提交
618
		arp1394 = (struct rfc2734_arp *)skb->data;
J
Jay Fenlason 已提交
619 620
		arp = (struct arphdr *)skb->data;
		arp_ptr = (unsigned char *)(arp + 1);
S
Stefan Richter 已提交
621 622 623 624
		fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32
				| ntohl(arp1394->fifo_lo);
		max_rec = dev->card->max_receive;
		if (arp1394->max_rec < max_rec)
J
Jay Fenlason 已提交
625 626
			max_rec = arp1394->max_rec;
		sspd = arp1394->sspd;
S
Stefan Richter 已提交
627 628 629
		/* Sanity check.  OS X 10.3 PPC reportedly sends 131. */
		if (sspd > SCODE_3200) {
			fw_notify("sspd %x out of range\n", sspd);
J
Jay Fenlason 已提交
630 631 632
			sspd = 0;
		}

S
Stefan Richter 已提交
633 634
		max_payload = min(fwnet_speed_to_max_payload[sspd],
			(u16)(1 << (max_rec + 1))) - RFC2374_UNFRAG_HDR_SIZE;
J
Jay Fenlason 已提交
635

S
Stefan Richter 已提交
636 637 638 639 640
		peer_guid = get_unaligned_be64(&arp1394->s_uniq_id);
		peer = fwnet_peer_find_by_guid(dev, peer_guid);
		if (!peer) {
			fw_notify("No peer for ARP packet from %016llx\n",
				  (unsigned long long)peer_guid);
J
Jay Fenlason 已提交
641 642
			goto failed_proto;
		}
S
Stefan Richter 已提交
643 644 645 646 647 648 649 650 651 652 653

		/* FIXME don't use card->generation */
		if (peer->node_id != source_node_id ||
		    peer->generation != dev->card->generation) {
			fw_notify("Internal error: peer->node_id (%x) != "
				  "source_node_id (%x) or peer->generation (%x)"
				  " != dev->card->generation(%x)\n",
				  peer->node_id, source_node_id,
				  peer->generation, dev->card->generation);
			peer->node_id = source_node_id;
			peer->generation = dev->card->generation;
J
Jay Fenlason 已提交
654 655 656
		}

		/* FIXME: for debugging */
S
Stefan Richter 已提交
657
		if (sspd > SCODE_400)
J
Jay Fenlason 已提交
658 659 660 661 662 663
			sspd = SCODE_400;
		/* Update our speed/payload/fifo_offset table */
		/*
		 * FIXME: this does not handle cases where two high-speed endpoints must use a slower speed because of
		 * a lower speed hub between them.  We need to look at the actual topology map here.
		 */
S
Stefan Richter 已提交
664 665
		peer->fifo = fifo_addr;
		peer->max_payload = max_payload;
J
Jay Fenlason 已提交
666 667
		/*
		 * Only allow speeds to go down from their initial value.
S
Stefan Richter 已提交
668 669
		 * Otherwise a local peer that can only do S400 or slower may
		 * be told to transmit at S800 to a faster remote peer.
J
Jay Fenlason 已提交
670
		 */
S
Stefan Richter 已提交
671 672
		if (peer->xmt_speed > sspd)
			peer->xmt_speed = sspd;
J
Jay Fenlason 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686

		/*
		 * Now that we're done with the 1394 specific stuff, we'll
		 * need to alter some of the data.  Believe it or not, all
		 * that needs to be done is sender_IP_address needs to be
		 * moved, the destination hardware address get stuffed
		 * in and the hardware address length set to 8.
		 *
		 * IMPORTANT: The code below overwrites 1394 specific data
		 * needed above so keep the munging of the data for the
		 * higher level IP stack last.
		 */

		arp->ar_hln = 8;
S
Stefan Richter 已提交
687 688 689 690 691 692
		/* skip over sender unique id */
		arp_ptr += arp->ar_hln;
		/* move sender IP addr */
		put_unaligned(arp1394->sip, (u32 *)arp_ptr);
		/* skip over sender IP addr */
		arp_ptr += arp->ar_pln;
J
Jay Fenlason 已提交
693 694 695 696

		if (arp->ar_op == htons(ARPOP_REQUEST))
			memset(arp_ptr, 0, sizeof(u64));
		else
S
Stefan Richter 已提交
697
			memcpy(arp_ptr, net->dev_addr, sizeof(u64));
J
Jay Fenlason 已提交
698 699 700
	}

	/* Now add the ethernet header. */
S
Stefan Richter 已提交
701 702 703 704 705
	guid = cpu_to_be64(dev->card->guid);
	if (dev_hard_header(skb, net, ether_type,
			   is_broadcast ? &broadcast_hw : &guid,
			   NULL, skb->len) >= 0) {
		struct fwnet_header *eth;
J
Jay Fenlason 已提交
706 707 708 709 710
		u16 *rawp;
		__be16 protocol;

		skb_reset_mac_header(skb);
		skb_pull(skb, sizeof(*eth));
S
Stefan Richter 已提交
711
		eth = (struct fwnet_header *)skb_mac_header(skb);
J
Jay Fenlason 已提交
712
		if (*eth->h_dest & 1) {
S
Stefan Richter 已提交
713 714
			if (memcmp(eth->h_dest, net->broadcast,
				   net->addr_len) == 0)
J
Jay Fenlason 已提交
715 716 717 718 719 720
				skb->pkt_type = PACKET_BROADCAST;
#if 0
			else
				skb->pkt_type = PACKET_MULTICAST;
#endif
		} else {
S
Stefan Richter 已提交
721
			if (memcmp(eth->h_dest, net->dev_addr, net->addr_len)) {
J
Jay Fenlason 已提交
722 723
				u64 a1, a2;

S
Stefan Richter 已提交
724 725
				memcpy(&a1, eth->h_dest, sizeof(u64));
				memcpy(&a2, net->dev_addr, sizeof(u64));
J
Jay Fenlason 已提交
726 727 728 729 730 731 732
				skb->pkt_type = PACKET_OTHERHOST;
			}
		}
		if (ntohs(eth->h_proto) >= 1536) {
			protocol = eth->h_proto;
		} else {
			rawp = (u16 *)skb->data;
S
Stefan Richter 已提交
733
			if (*rawp == 0xffff)
J
Jay Fenlason 已提交
734
				protocol = htons(ETH_P_802_3);
S
Stefan Richter 已提交
735
			else
J
Jay Fenlason 已提交
736 737 738 739 740
				protocol = htons(ETH_P_802_2);
		}
		skb->protocol = protocol;
	}
	status = netif_rx(skb);
S
Stefan Richter 已提交
741 742 743
	if (status == NET_RX_DROP) {
		net->stats.rx_errors++;
		net->stats.rx_dropped++;
J
Jay Fenlason 已提交
744
	} else {
S
Stefan Richter 已提交
745 746
		net->stats.rx_packets++;
		net->stats.rx_bytes += skb->len;
J
Jay Fenlason 已提交
747
	}
S
Stefan Richter 已提交
748 749 750
	if (netif_queue_stopped(net))
		netif_wake_queue(net);

J
Jay Fenlason 已提交
751 752 753
	return 0;

 failed_proto:
S
Stefan Richter 已提交
754 755 756
	net->stats.rx_errors++;
	net->stats.rx_dropped++;

J
Jay Fenlason 已提交
757
	dev_kfree_skb_any(skb);
S
Stefan Richter 已提交
758 759 760 761 762
	if (netif_queue_stopped(net))
		netif_wake_queue(net);

	net->last_rx = jiffies;

J
Jay Fenlason 已提交
763 764 765
	return 0;
}

S
Stefan Richter 已提交
766 767 768
static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
				 u16 source_node_id, bool is_broadcast)
{
J
Jay Fenlason 已提交
769
	struct sk_buff *skb;
S
Stefan Richter 已提交
770 771
	struct net_device *net;
	struct rfc2734_header hdr;
J
Jay Fenlason 已提交
772 773
	unsigned lf;
	unsigned long flags;
S
Stefan Richter 已提交
774 775
	struct fwnet_peer *peer;
	struct fwnet_partial_datagram *pd;
J
Jay Fenlason 已提交
776 777 778 779 780 781
	int fg_off;
	int dg_size;
	u16 datagram_label;
	int retval;
	u16 ether_type;

S
Stefan Richter 已提交
782
	net = dev->card->netdev;
J
Jay Fenlason 已提交
783

S
Stefan Richter 已提交
784 785 786
	hdr.w0 = be32_to_cpu(buf[0]);
	lf = fwnet_get_hdr_lf(&hdr);
	if (lf == RFC2374_HDR_UNFRAG) {
J
Jay Fenlason 已提交
787 788 789 790 791
		/*
		 * 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.
		 */
S
Stefan Richter 已提交
792
		ether_type = fwnet_get_hdr_ether_type(&hdr);
J
Jay Fenlason 已提交
793
		buf++;
S
Stefan Richter 已提交
794
		len -= RFC2374_UNFRAG_HDR_SIZE;
J
Jay Fenlason 已提交
795

S
Stefan Richter 已提交
796
		skb = dev_alloc_skb(len + net->hard_header_len + 15);
J
Jay Fenlason 已提交
797
		if (unlikely(!skb)) {
S
Stefan Richter 已提交
798 799 800
			fw_error("out of memory\n");
			net->stats.rx_dropped++;

J
Jay Fenlason 已提交
801 802
			return -1;
		}
S
Stefan Richter 已提交
803 804 805 806 807
		skb_reserve(skb, (net->hard_header_len + 15) & ~15);
		memcpy(skb_put(skb, len), buf, len);

		return fwnet_finish_incoming_packet(net, skb, source_node_id,
						    is_broadcast, ether_type);
J
Jay Fenlason 已提交
808 809 810
	}
	/* A datagram fragment has been received, now the fun begins. */
	hdr.w1 = ntohl(buf[1]);
S
Stefan Richter 已提交
811 812 813 814
	buf += 2;
	len -= RFC2374_FRAG_HDR_SIZE;
	if (lf == RFC2374_HDR_FIRSTFRAG) {
		ether_type = fwnet_get_hdr_ether_type(&hdr);
J
Jay Fenlason 已提交
815 816
		fg_off = 0;
	} else {
S
Stefan Richter 已提交
817 818
		ether_type = 0;
		fg_off = fwnet_get_hdr_fg_off(&hdr);
J
Jay Fenlason 已提交
819
	}
S
Stefan Richter 已提交
820 821 822 823 824 825 826
	datagram_label = fwnet_get_hdr_dgl(&hdr);
	dg_size = fwnet_get_hdr_dg_size(&hdr); /* ??? + 1 */
	peer = fwnet_peer_find_by_node_id(dev, source_node_id);

	spin_lock_irqsave(&peer->pdg_lock, flags);

	pd = fwnet_pd_find(peer, datagram_label);
J
Jay Fenlason 已提交
827
	if (pd == NULL) {
S
Stefan Richter 已提交
828
		while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) {
J
Jay Fenlason 已提交
829
			/* remove the oldest */
S
Stefan Richter 已提交
830 831 832
			fwnet_pd_delete(list_first_entry(&peer->pd_list,
				struct fwnet_partial_datagram, pd_link));
			peer->pdg_size--;
J
Jay Fenlason 已提交
833
		}
S
Stefan Richter 已提交
834 835 836
		pd = fwnet_pd_new(net, peer, datagram_label,
				  dg_size, buf, fg_off, len);
		if (pd == NULL) {
J
Jay Fenlason 已提交
837 838 839
			retval = -ENOMEM;
			goto bad_proto;
		}
S
Stefan Richter 已提交
840
		peer->pdg_size++;
J
Jay Fenlason 已提交
841
	} else {
S
Stefan Richter 已提交
842 843
		if (fwnet_frag_overlap(pd, fg_off, len) ||
		    pd->datagram_size != dg_size) {
J
Jay Fenlason 已提交
844 845
			/*
			 * Differing datagram sizes or overlapping fragments,
S
Stefan Richter 已提交
846
			 * discard old datagram and start a new one.
J
Jay Fenlason 已提交
847
			 */
S
Stefan Richter 已提交
848 849 850 851
			fwnet_pd_delete(pd);
			pd = fwnet_pd_new(net, peer, datagram_label,
					  dg_size, buf, fg_off, len);
			if (pd == NULL) {
J
Jay Fenlason 已提交
852
				retval = -ENOMEM;
S
Stefan Richter 已提交
853
				peer->pdg_size--;
J
Jay Fenlason 已提交
854 855 856
				goto bad_proto;
			}
		} else {
S
Stefan Richter 已提交
857
			if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) {
J
Jay Fenlason 已提交
858 859 860 861 862
				/*
				 * Couldn't save off fragment anyway
				 * so might as well obliterate the
				 * datagram now.
				 */
S
Stefan Richter 已提交
863 864
				fwnet_pd_delete(pd);
				peer->pdg_size--;
J
Jay Fenlason 已提交
865 866 867 868 869
				goto bad_proto;
			}
		}
	} /* new datagram or add to existing one */

S
Stefan Richter 已提交
870
	if (lf == RFC2374_HDR_FIRSTFRAG)
J
Jay Fenlason 已提交
871
		pd->ether_type = ether_type;
S
Stefan Richter 已提交
872 873

	if (fwnet_pd_is_complete(pd)) {
J
Jay Fenlason 已提交
874
		ether_type = pd->ether_type;
S
Stefan Richter 已提交
875
		peer->pdg_size--;
J
Jay Fenlason 已提交
876
		skb = skb_get(pd->skb);
S
Stefan Richter 已提交
877 878 879 880 881 882
		fwnet_pd_delete(pd);

		spin_unlock_irqrestore(&peer->pdg_lock, flags);

		return fwnet_finish_incoming_packet(net, skb, source_node_id,
						    false, ether_type);
J
Jay Fenlason 已提交
883 884 885 886 887
	}
	/*
	 * Datagram is not complete, we're done for the
	 * moment.
	 */
S
Stefan Richter 已提交
888 889
	spin_unlock_irqrestore(&peer->pdg_lock, flags);

J
Jay Fenlason 已提交
890 891 892
	return 0;

 bad_proto:
S
Stefan Richter 已提交
893 894 895 896 897
	spin_unlock_irqrestore(&peer->pdg_lock, flags);

	if (netif_queue_stopped(net))
		netif_wake_queue(net);

J
Jay Fenlason 已提交
898 899 900
	return 0;
}

S
Stefan Richter 已提交
901 902 903 904 905 906
static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r,
		int tcode, int destination, int source, int generation,
		int speed, unsigned long long offset, void *payload,
		size_t length, void *callback_data)
{
	struct fwnet_device *dev;
J
Jay Fenlason 已提交
907 908
	int status;

S
Stefan Richter 已提交
909 910 911 912 913
	dev = callback_data;
	if (tcode != TCODE_WRITE_BLOCK_REQUEST
	    || destination != card->node_id	/* <- FIXME */
	    || generation != card->generation	/* <- FIXME */
	    || offset != dev->handler.offset) {
J
Jay Fenlason 已提交
914
		fw_send_response(card, r, RCODE_CONFLICT_ERROR);
S
Stefan Richter 已提交
915

J
Jay Fenlason 已提交
916 917
		return;
	}
S
Stefan Richter 已提交
918 919 920 921 922 923

	status = fwnet_incoming_packet(dev, payload, length, source, false);
	if (status != 0) {
		fw_error("Incoming packet failure\n");
		fw_send_response(card, r, RCODE_CONFLICT_ERROR);

J
Jay Fenlason 已提交
924 925
		return;
	}
S
Stefan Richter 已提交
926 927

	fw_send_response(card, r, RCODE_COMPLETE);
J
Jay Fenlason 已提交
928 929
}

S
Stefan Richter 已提交
930 931 932 933
static void fwnet_receive_broadcast(struct fw_iso_context *context,
		u32 cycle, size_t header_length, void *header, void *data)
{
	struct fwnet_device *dev;
J
Jay Fenlason 已提交
934 935
	struct fw_iso_packet packet;
	struct fw_card *card;
S
Stefan Richter 已提交
936 937
	__be16 *hdr_ptr;
	__be32 *buf_ptr;
J
Jay Fenlason 已提交
938 939 940 941 942 943 944 945
	int retval;
	u32 length;
	u16 source_node_id;
	u32 specifier_id;
	u32 ver;
	unsigned long offset;
	unsigned long flags;

S
Stefan Richter 已提交
946 947
	dev = data;
	card = dev->card;
J
Jay Fenlason 已提交
948
	hdr_ptr = header;
S
Stefan Richter 已提交
949 950 951 952 953 954 955 956 957 958
	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);
J
Jay Fenlason 已提交
959 960 961

	specifier_id =    (be32_to_cpu(buf_ptr[0]) & 0xffff) << 8
			| (be32_to_cpu(buf_ptr[1]) & 0xff000000) >> 24;
S
Stefan Richter 已提交
962
	ver = be32_to_cpu(buf_ptr[1]) & 0xffffff;
J
Jay Fenlason 已提交
963
	source_node_id = be32_to_cpu(buf_ptr[0]) >> 16;
S
Stefan Richter 已提交
964 965

	if (specifier_id == IANA_SPECIFIER_ID && ver == RFC2734_SW_VERSION) {
J
Jay Fenlason 已提交
966
		buf_ptr += 2;
S
Stefan Richter 已提交
967 968 969 970 971 972
		length -= IEEE1394_GASP_HDR_SIZE;
		fwnet_incoming_packet(dev, buf_ptr, length,
				      source_node_id, true);
	}

	packet.payload_length = dev->rcv_buffer_size;
J
Jay Fenlason 已提交
973 974 975 976
	packet.interrupt = 1;
	packet.skip = 0;
	packet.tag = 3;
	packet.sy = 0;
S
Stefan Richter 已提交
977 978 979
	packet.header_length = IEEE1394_GASP_HDR_SIZE;

	spin_lock_irqsave(&dev->lock, flags);
J
Jay Fenlason 已提交
980

S
Stefan Richter 已提交
981 982 983 984 985 986 987
	retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet,
				      &dev->broadcast_rcv_buffer, offset);

	spin_unlock_irqrestore(&dev->lock, flags);

	if (retval < 0)
		fw_error("requeue failed\n");
J
Jay Fenlason 已提交
988 989
}

S
Stefan Richter 已提交
990 991 992 993 994 995 996
static struct kmem_cache *fwnet_packet_task_cache;

static int fwnet_send_packet(struct fwnet_packet_task *ptask);

static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask)
{
	struct fwnet_device *dev;
J
Jay Fenlason 已提交
997 998
	unsigned long flags;

S
Stefan Richter 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007
	dev = ptask->dev;

	spin_lock_irqsave(&dev->lock, flags);
	list_del(&ptask->pt_link);
	spin_unlock_irqrestore(&dev->lock, flags);

	ptask->outstanding_pkts--; /* FIXME access inside lock */

	if (ptask->outstanding_pkts > 0) {
J
Jay Fenlason 已提交
1008 1009 1010 1011 1012 1013 1014
		u16 dg_size;
		u16 fg_off;
		u16 datagram_label;
		u16 lf;
		struct sk_buff *skb;

		/* Update the ptask to point to the next fragment and send it */
S
Stefan Richter 已提交
1015
		lf = fwnet_get_hdr_lf(&ptask->hdr);
J
Jay Fenlason 已提交
1016
		switch (lf) {
S
Stefan Richter 已提交
1017 1018
		case RFC2374_HDR_LASTFRAG:
		case RFC2374_HDR_UNFRAG:
J
Jay Fenlason 已提交
1019
		default:
S
Stefan Richter 已提交
1020 1021 1022
			fw_error("Outstanding packet %x lf %x, header %x,%x\n",
				 ptask->outstanding_pkts, lf, ptask->hdr.w0,
				 ptask->hdr.w1);
J
Jay Fenlason 已提交
1023 1024
			BUG();

S
Stefan Richter 已提交
1025
		case RFC2374_HDR_FIRSTFRAG:
J
Jay Fenlason 已提交
1026
			/* Set frag type here for future interior fragments */
S
Stefan Richter 已提交
1027 1028 1029
			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);
J
Jay Fenlason 已提交
1030 1031
			break;

S
Stefan Richter 已提交
1032 1033 1034 1035 1036
		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);
J
Jay Fenlason 已提交
1037 1038 1039
			break;
		}
		skb = ptask->skb;
S
Stefan Richter 已提交
1040 1041 1042 1043
		skb_pull(skb, ptask->max_payload);
		if (ptask->outstanding_pkts > 1) {
			fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG,
					  dg_size, fg_off, datagram_label);
J
Jay Fenlason 已提交
1044
		} else {
S
Stefan Richter 已提交
1045 1046 1047
			fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG,
					  dg_size, fg_off, datagram_label);
			ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE;
J
Jay Fenlason 已提交
1048
		}
S
Stefan Richter 已提交
1049
		fwnet_send_packet(ptask);
J
Jay Fenlason 已提交
1050
	} else {
S
Stefan Richter 已提交
1051 1052
		dev_kfree_skb_any(ptask->skb);
		kmem_cache_free(fwnet_packet_task_cache, ptask);
J
Jay Fenlason 已提交
1053 1054 1055
	}
}

S
Stefan Richter 已提交
1056 1057 1058 1059
static void fwnet_write_complete(struct fw_card *card, int rcode,
				 void *payload, size_t length, void *data)
{
	struct fwnet_packet_task *ptask;
J
Jay Fenlason 已提交
1060 1061 1062

	ptask = data;

S
Stefan Richter 已提交
1063 1064 1065 1066
	if (rcode == RCODE_COMPLETE)
		fwnet_transmit_packet_done(ptask);
	else
		fw_error("fwnet_write_complete: failed: %x\n", rcode);
J
Jay Fenlason 已提交
1067 1068 1069
		/* ??? error recovery */
}

S
Stefan Richter 已提交
1070 1071 1072
static int fwnet_send_packet(struct fwnet_packet_task *ptask)
{
	struct fwnet_device *dev;
J
Jay Fenlason 已提交
1073
	unsigned tx_len;
S
Stefan Richter 已提交
1074
	struct rfc2734_header *bufhdr;
J
Jay Fenlason 已提交
1075
	unsigned long flags;
S
Stefan Richter 已提交
1076
	struct net_device *net;
J
Jay Fenlason 已提交
1077

S
Stefan Richter 已提交
1078
	dev = ptask->dev;
J
Jay Fenlason 已提交
1079
	tx_len = ptask->max_payload;
S
Stefan Richter 已提交
1080 1081 1082 1083 1084
	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);
J
Jay Fenlason 已提交
1085 1086
		break;

S
Stefan Richter 已提交
1087 1088 1089 1090 1091 1092 1093
	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);
J
Jay Fenlason 已提交
1094 1095 1096 1097 1098
		break;

	default:
		BUG();
	}
S
Stefan Richter 已提交
1099 1100
	if (ptask->dest_node == IEEE1394_ALL_NODES) {
		u8 *p;
J
Jay Fenlason 已提交
1101
		int generation;
S
Stefan Richter 已提交
1102
		int node_id;
J
Jay Fenlason 已提交
1103 1104

		/* ptask->generation may not have been set yet */
S
Stefan Richter 已提交
1105
		generation = dev->card->generation;
J
Jay Fenlason 已提交
1106
		smp_rmb();
S
Stefan Richter 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
		node_id = dev->card->node_id;

		p = skb_push(ptask->skb, 8);
		put_unaligned_be32(node_id << 16 | IANA_SPECIFIER_ID >> 8, p);
		put_unaligned_be32((IANA_SPECIFIER_ID & 0xff) << 24
						| RFC2734_SW_VERSION, &p[4]);

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

		/* FIXME race? */
		spin_lock_irqsave(&dev->lock, flags);
		list_add_tail(&ptask->pt_link, &dev->broadcasted_list);
		spin_unlock_irqrestore(&dev->lock, flags);

J
Jay Fenlason 已提交
1127 1128
		return 0;
	}
S
Stefan Richter 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

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

	/* FIXME race? */
	spin_lock_irqsave(&dev->lock, flags);
	list_add_tail(&ptask->pt_link, &dev->sent_list);
	spin_unlock_irqrestore(&dev->lock, flags);

	net = dev->card->netdev;
	net->trans_start = jiffies;

J
Jay Fenlason 已提交
1143 1144 1145
	return 0;
}

S
Stefan Richter 已提交
1146 1147
static int fwnet_broadcast_start(struct fwnet_device *dev)
{
J
Jay Fenlason 已提交
1148 1149 1150 1151 1152 1153 1154 1155
	struct fw_iso_context *context;
	int retval;
	unsigned num_packets;
	unsigned max_receive;
	struct fw_iso_packet packet;
	unsigned long offset;
	unsigned u;

S
Stefan Richter 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	if (dev->local_fifo == FWNET_NO_FIFO_ADDR) {
		/* outside OHCI posted write area? */
		static const struct fw_address_region region = {
			.start = 0xffff00000000ULL,
			.end   = CSR_REGISTER_BASE,
		};

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

		retval = fw_core_add_address_handler(&dev->handler, &region);
		if (retval < 0)
J
Jay Fenlason 已提交
1169
			goto failed_initial;
S
Stefan Richter 已提交
1170 1171

		dev->local_fifo = dev->handler.offset;
J
Jay Fenlason 已提交
1172 1173
	}

S
Stefan Richter 已提交
1174 1175 1176 1177
	max_receive = 1U << (dev->card->max_receive + 1);
	num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive;

	if (!dev->broadcast_rcv_context) {
J
Jay Fenlason 已提交
1178 1179
		void **ptrptr;

S
Stefan Richter 已提交
1180 1181 1182
		context = fw_iso_context_create(dev->card,
		    FW_ISO_CONTEXT_RECEIVE, IEEE1394_BROADCAST_CHANNEL,
		    dev->card->link_speed, 8, fwnet_receive_broadcast, dev);
J
Jay Fenlason 已提交
1183 1184 1185 1186
		if (IS_ERR(context)) {
			retval = PTR_ERR(context);
			goto failed_context_create;
		}
S
Stefan Richter 已提交
1187 1188 1189 1190

		retval = fw_iso_buffer_init(&dev->broadcast_rcv_buffer,
		    dev->card, FWNET_ISO_PAGE_COUNT, DMA_FROM_DEVICE);
		if (retval < 0)
J
Jay Fenlason 已提交
1191
			goto failed_buffer_init;
S
Stefan Richter 已提交
1192 1193 1194

		ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL);
		if (!ptrptr) {
J
Jay Fenlason 已提交
1195 1196 1197
			retval = -ENOMEM;
			goto failed_ptrs_alloc;
		}
S
Stefan Richter 已提交
1198 1199 1200

		dev->broadcast_rcv_buffer_ptrs = ptrptr;
		for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) {
J
Jay Fenlason 已提交
1201 1202 1203
			void *ptr;
			unsigned v;

S
Stefan Richter 已提交
1204 1205 1206 1207
			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);
J
Jay Fenlason 已提交
1208
		}
S
Stefan Richter 已提交
1209 1210 1211 1212
		dev->broadcast_rcv_context = context;
	} else {
		context = dev->broadcast_rcv_context;
	}
J
Jay Fenlason 已提交
1213 1214 1215 1216 1217 1218

	packet.payload_length = max_receive;
	packet.interrupt = 1;
	packet.skip = 0;
	packet.tag = 3;
	packet.sy = 0;
S
Stefan Richter 已提交
1219
	packet.header_length = IEEE1394_GASP_HDR_SIZE;
J
Jay Fenlason 已提交
1220
	offset = 0;
S
Stefan Richter 已提交
1221 1222 1223 1224 1225

	for (u = 0; u < num_packets; u++) {
		retval = fw_iso_context_queue(context, &packet,
				&dev->broadcast_rcv_buffer, offset);
		if (retval < 0)
J
Jay Fenlason 已提交
1226
			goto failed_rcv_queue;
S
Stefan Richter 已提交
1227

J
Jay Fenlason 已提交
1228 1229
		offset += max_receive;
	}
S
Stefan Richter 已提交
1230 1231 1232 1233 1234 1235
	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)
J
Jay Fenlason 已提交
1236
		goto failed_rcv_queue;
S
Stefan Richter 已提交
1237 1238 1239 1240 1241 1242

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

J
Jay Fenlason 已提交
1243 1244 1245
	return 0;

 failed_rcv_queue:
S
Stefan Richter 已提交
1246 1247
	kfree(dev->broadcast_rcv_buffer_ptrs);
	dev->broadcast_rcv_buffer_ptrs = NULL;
J
Jay Fenlason 已提交
1248
 failed_ptrs_alloc:
S
Stefan Richter 已提交
1249
	fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, dev->card);
J
Jay Fenlason 已提交
1250
 failed_buffer_init:
S
Stefan Richter 已提交
1251 1252
	fw_iso_context_destroy(context);
	dev->broadcast_rcv_context = NULL;
J
Jay Fenlason 已提交
1253
 failed_context_create:
S
Stefan Richter 已提交
1254
	fw_core_remove_address_handler(&dev->handler);
J
Jay Fenlason 已提交
1255
 failed_initial:
S
Stefan Richter 已提交
1256 1257
	dev->local_fifo = FWNET_NO_FIFO_ADDR;

J
Jay Fenlason 已提交
1258 1259 1260
	return retval;
}

S
Stefan Richter 已提交
1261 1262 1263 1264
/* ifup */
static int fwnet_open(struct net_device *net)
{
	struct fwnet_device *dev = netdev_priv(net);
J
Jay Fenlason 已提交
1265 1266
	int ret;

S
Stefan Richter 已提交
1267 1268
	if (dev->broadcast_state == FWNET_BROADCAST_ERROR) {
		ret = fwnet_broadcast_start(dev);
J
Jay Fenlason 已提交
1269 1270 1271
		if (ret)
			return ret;
	}
S
Stefan Richter 已提交
1272 1273
	netif_start_queue(net);

J
Jay Fenlason 已提交
1274 1275 1276
	return 0;
}

S
Stefan Richter 已提交
1277 1278
/* ifdown */
static int fwnet_stop(struct net_device *net)
J
Jay Fenlason 已提交
1279
{
S
Stefan Richter 已提交
1280 1281 1282
	netif_stop_queue(net);

	/* Deallocate iso context for use by other applications? */
J
Jay Fenlason 已提交
1283 1284 1285 1286

	return 0;
}

S
Stefan Richter 已提交
1287
static int fwnet_tx(struct sk_buff *skb, struct net_device *net)
J
Jay Fenlason 已提交
1288
{
S
Stefan Richter 已提交
1289 1290
	struct fwnet_header hdr_buf;
	struct fwnet_device *dev = netdev_priv(net);
J
Jay Fenlason 已提交
1291 1292 1293 1294 1295
	__be16 proto;
	u16 dest_node;
	unsigned max_payload;
	u16 dg_size;
	u16 *datagram_label_ptr;
S
Stefan Richter 已提交
1296 1297
	struct fwnet_packet_task *ptask;
	struct fwnet_peer *peer = NULL;
J
Jay Fenlason 已提交
1298

S
Stefan Richter 已提交
1299
	ptask = kmem_cache_alloc(fwnet_packet_task_cache, GFP_ATOMIC);
J
Jay Fenlason 已提交
1300 1301 1302 1303 1304 1305 1306 1307
	if (ptask == NULL)
		goto fail;

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

	/*
S
Stefan Richter 已提交
1308
	 * Make a copy of the driver-specific header.
J
Jay Fenlason 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	 * We might need to rebuild the header on tx failure.
	 */
	memcpy(&hdr_buf, skb->data, sizeof(hdr_buf));
	skb_pull(skb, sizeof(hdr_buf));

	proto = hdr_buf.h_proto;
	dg_size = skb->len;

	/*
	 * Set the transmission type for the packet.  ARP packets and IP
	 * broadcast packets are sent via GASP.
	 */
S
Stefan Richter 已提交
1321
	if (memcmp(hdr_buf.h_dest, net->broadcast, FWNET_ALEN) == 0
J
Jay Fenlason 已提交
1322
	    || proto == htons(ETH_P_ARP)
S
Stefan Richter 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331
	    || (proto == htons(ETH_P_IP)
		&& IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
		max_payload = dev->broadcast_xmt_max_payload;
		datagram_label_ptr = &dev->broadcast_xmt_datagramlabel;

		ptask->fifo_addr = FWNET_NO_FIFO_ADDR;
		ptask->generation = 0;
		ptask->dest_node = IEEE1394_ALL_NODES;
		ptask->speed = SCODE_100;
J
Jay Fenlason 已提交
1332
	} else {
S
Stefan Richter 已提交
1333
		__be64 guid = get_unaligned((__be64 *)hdr_buf.h_dest);
J
Jay Fenlason 已提交
1334 1335
		u8 generation;

S
Stefan Richter 已提交
1336 1337
		peer = fwnet_peer_find_by_guid(dev, be64_to_cpu(guid));
		if (!peer)
J
Jay Fenlason 已提交
1338 1339
			goto fail;

S
Stefan Richter 已提交
1340
		if (peer->fifo == FWNET_NO_FIFO_ADDR)
J
Jay Fenlason 已提交
1341 1342
			goto fail;

S
Stefan Richter 已提交
1343 1344 1345 1346 1347 1348
		generation = peer->generation;
		smp_rmb();
		dest_node = peer->node_id;

		max_payload = peer->max_payload;
		datagram_label_ptr = &peer->datagram_label;
J
Jay Fenlason 已提交
1349

S
Stefan Richter 已提交
1350
		ptask->fifo_addr = peer->fifo;
J
Jay Fenlason 已提交
1351 1352
		ptask->generation = generation;
		ptask->dest_node = dest_node;
S
Stefan Richter 已提交
1353
		ptask->speed = peer->xmt_speed;
J
Jay Fenlason 已提交
1354 1355 1356 1357 1358 1359
	}

	/* If this is an ARP packet, convert it */
	if (proto == htons(ETH_P_ARP)) {
		struct arphdr *arp = (struct arphdr *)skb->data;
		unsigned char *arp_ptr = (unsigned char *)(arp + 1);
S
Stefan Richter 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
		struct rfc2734_arp *arp1394 = (struct rfc2734_arp *)skb->data;
		__be32 ipaddr;

		ipaddr = get_unaligned((__be32 *)(arp_ptr + FWNET_ALEN));

		arp1394->hw_addr_len    = RFC2734_HW_ADDR_LEN;
		arp1394->max_rec        = dev->card->max_receive;
		arp1394->sspd		= dev->card->link_speed;

		put_unaligned_be16(dev->local_fifo >> 32,
				   &arp1394->fifo_hi);
		put_unaligned_be32(dev->local_fifo & 0xffffffff,
				   &arp1394->fifo_lo);
		put_unaligned(ipaddr, &arp1394->sip);
J
Jay Fenlason 已提交
1374 1375 1376 1377 1378
	}

	ptask->hdr.w0 = 0;
	ptask->hdr.w1 = 0;
	ptask->skb = skb;
S
Stefan Richter 已提交
1379 1380
	ptask->dev = dev;

J
Jay Fenlason 已提交
1381
	/* Does it all fit in one packet? */
S
Stefan Richter 已提交
1382 1383
	if (dg_size <= max_payload) {
		fwnet_make_uf_hdr(&ptask->hdr, ntohs(proto));
J
Jay Fenlason 已提交
1384
		ptask->outstanding_pkts = 1;
S
Stefan Richter 已提交
1385
		max_payload = dg_size + RFC2374_UNFRAG_HDR_SIZE;
J
Jay Fenlason 已提交
1386 1387 1388
	} else {
		u16 datagram_label;

S
Stefan Richter 已提交
1389
		max_payload -= RFC2374_FRAG_OVERHEAD;
J
Jay Fenlason 已提交
1390
		datagram_label = (*datagram_label_ptr)++;
S
Stefan Richter 已提交
1391 1392
		fwnet_make_ff_hdr(&ptask->hdr, ntohs(proto), dg_size,
				  datagram_label);
J
Jay Fenlason 已提交
1393
		ptask->outstanding_pkts = DIV_ROUND_UP(dg_size, max_payload);
S
Stefan Richter 已提交
1394
		max_payload += RFC2374_FRAG_HDR_SIZE;
J
Jay Fenlason 已提交
1395 1396
	}
	ptask->max_payload = max_payload;
S
Stefan Richter 已提交
1397 1398
	fwnet_send_packet(ptask);

J
Jay Fenlason 已提交
1399 1400 1401 1402
	return NETDEV_TX_OK;

 fail:
	if (ptask)
S
Stefan Richter 已提交
1403
		kmem_cache_free(fwnet_packet_task_cache, ptask);
J
Jay Fenlason 已提交
1404 1405 1406 1407

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

S
Stefan Richter 已提交
1408 1409
	net->stats.tx_dropped++;
	net->stats.tx_errors++;
J
Jay Fenlason 已提交
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420

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

S
Stefan Richter 已提交
1421 1422 1423
static void fwnet_tx_timeout(struct net_device *net)
{
	fw_error("%s: timeout\n", net->name);
J
Jay Fenlason 已提交
1424

S
Stefan Richter 已提交
1425
	/* FIXME: What to do if we timeout? */
J
Jay Fenlason 已提交
1426 1427
}

S
Stefan Richter 已提交
1428 1429
static int fwnet_change_mtu(struct net_device *net, int new_mtu)
{
J
Jay Fenlason 已提交
1430 1431 1432
	if (new_mtu < 68)
		return -EINVAL;

S
Stefan Richter 已提交
1433
	net->mtu = new_mtu;
J
Jay Fenlason 已提交
1434 1435 1436
	return 0;
}

S
Stefan Richter 已提交
1437 1438 1439 1440 1441
static void fwnet_get_drvinfo(struct net_device *net,
			      struct ethtool_drvinfo *info)
{
	strcpy(info->driver, KBUILD_MODNAME);
	strcpy(info->bus_info, "ieee1394");
J
Jay Fenlason 已提交
1442 1443
}

S
Stefan Richter 已提交
1444 1445
static struct ethtool_ops fwnet_ethtool_ops = {
	.get_drvinfo = fwnet_get_drvinfo,
J
Jay Fenlason 已提交
1446 1447
};

S
Stefan Richter 已提交
1448 1449 1450 1451 1452 1453
static const struct net_device_ops fwnet_netdev_ops = {
	.ndo_open       = fwnet_open,
	.ndo_stop	= fwnet_stop,
	.ndo_start_xmit = fwnet_tx,
	.ndo_tx_timeout = fwnet_tx_timeout,
	.ndo_change_mtu = fwnet_change_mtu,
J
Jay Fenlason 已提交
1454 1455
};

S
Stefan Richter 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
static void fwnet_init_dev(struct net_device *net)
{
	net->header_ops		= &fwnet_header_ops;
	net->netdev_ops		= &fwnet_netdev_ops;
	net->watchdog_timeo	= 100000; /* ? FIXME */
	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;
	net->tx_queue_len	= 1000; /* ? FIXME */
	SET_ETHTOOL_OPS(net, &fwnet_ethtool_ops);
J
Jay Fenlason 已提交
1468 1469
}

S
Stefan Richter 已提交
1470 1471 1472 1473 1474 1475 1476 1477
/* FIXME create netdev upon first fw_unit of a card, not upon local fw_unit */
static int fwnet_probe(struct device *_dev)
{
	struct fw_unit *unit = fw_unit(_dev);
	struct fw_device *device = fw_parent_device(unit);
	struct fw_card *card = device->card;
	struct net_device *net;
	struct fwnet_device *dev;
J
Jay Fenlason 已提交
1478 1479
	unsigned max_mtu;

S
Stefan Richter 已提交
1480
	if (!device->is_local) {
J
Jay Fenlason 已提交
1481 1482
		int added;

S
Stefan Richter 已提交
1483
		added = fwnet_peer_new(card, device);
J
Jay Fenlason 已提交
1484 1485
		return added;
	}
S
Stefan Richter 已提交
1486 1487 1488
	net = alloc_netdev(sizeof(*dev), "firewire%d", fwnet_init_dev);
	if (net == NULL) {
		fw_error("out of memory\n");
J
Jay Fenlason 已提交
1489 1490 1491
		goto out;
	}

S
Stefan Richter 已提交
1492 1493
	SET_NETDEV_DEV(net, card->device);
	dev = netdev_priv(net);
J
Jay Fenlason 已提交
1494

S
Stefan Richter 已提交
1495 1496 1497 1498 1499
	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;
J
Jay Fenlason 已提交
1500

S
Stefan Richter 已提交
1501
	dev->local_fifo = FWNET_NO_FIFO_ADDR;
J
Jay Fenlason 已提交
1502

S
Stefan Richter 已提交
1503 1504 1505 1506
	/* INIT_WORK(&dev->wake, fwnet_handle_queue);*/
	INIT_LIST_HEAD(&dev->packet_list);
	INIT_LIST_HEAD(&dev->broadcasted_list);
	INIT_LIST_HEAD(&dev->sent_list);
J
Jay Fenlason 已提交
1507

S
Stefan Richter 已提交
1508
	dev->card = card;
J
Jay Fenlason 已提交
1509 1510 1511 1512 1513 1514

	/*
	 * Use the RFC 2734 default 1500 octets or the maximum payload
	 * as initial MTU
	 */
	max_mtu = (1 << (card->max_receive + 1))
S
Stefan Richter 已提交
1515 1516
		  - sizeof(struct rfc2734_header) - IEEE1394_GASP_HDR_SIZE;
	net->mtu = min(1500U, max_mtu);
J
Jay Fenlason 已提交
1517 1518

	/* Set our hardware address while we're at it */
S
Stefan Richter 已提交
1519 1520 1521 1522
	put_unaligned_be64(card->guid, net->dev_addr);
	put_unaligned_be64(~0ULL, net->broadcast);
	if (register_netdev(net)) {
		fw_error("Cannot register the driver\n");
J
Jay Fenlason 已提交
1523 1524 1525
		goto out;
	}

S
Stefan Richter 已提交
1526 1527 1528
	fw_notify("%s: IPv4 over FireWire on device %016llx\n",
		  net->name, (unsigned long long)card->guid);
	card->netdev = net;
J
Jay Fenlason 已提交
1529

S
Stefan Richter 已提交
1530
	return 0;
J
Jay Fenlason 已提交
1531
 out:
S
Stefan Richter 已提交
1532 1533 1534
	if (net)
		free_netdev(net);

J
Jay Fenlason 已提交
1535 1536 1537
	return -ENOENT;
}

S
Stefan Richter 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
static int fwnet_remove(struct device *_dev)
{
	struct fw_unit *unit = fw_unit(_dev);
	struct fw_device *device = fw_parent_device(unit);
	struct fw_card *card = device->card;
	struct net_device *net;
	struct fwnet_device *dev;
	struct fwnet_peer *peer;
	struct fwnet_partial_datagram *pd, *pd_next;
	struct fwnet_packet_task *ptask, *pt_next;

	if (!device->is_local) {
		fwnet_peer_delete(card, device);
J
Jay Fenlason 已提交
1551 1552 1553

		return 0;
	}
S
Stefan Richter 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566

	net = card->netdev;
	if (net) {
		dev = netdev_priv(net);
		unregister_netdev(net);

		if (dev->local_fifo != FWNET_NO_FIFO_ADDR)
			fw_core_remove_address_handler(&dev->handler);
		if (dev->broadcast_rcv_context) {
			fw_iso_context_stop(dev->broadcast_rcv_context);
			fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer,
					      dev->card);
			fw_iso_context_destroy(dev->broadcast_rcv_context);
J
Jay Fenlason 已提交
1567
		}
S
Stefan Richter 已提交
1568 1569 1570 1571
		list_for_each_entry_safe(ptask, pt_next,
					 &dev->packet_list, pt_link) {
			dev_kfree_skb_any(ptask->skb);
			kmem_cache_free(fwnet_packet_task_cache, ptask);
J
Jay Fenlason 已提交
1572
		}
S
Stefan Richter 已提交
1573 1574 1575 1576
		list_for_each_entry_safe(ptask, pt_next,
					 &dev->broadcasted_list, pt_link) {
			dev_kfree_skb_any(ptask->skb);
			kmem_cache_free(fwnet_packet_task_cache, ptask);
J
Jay Fenlason 已提交
1577
		}
S
Stefan Richter 已提交
1578 1579 1580 1581
		list_for_each_entry_safe(ptask, pt_next,
					 &dev->sent_list, pt_link) {
			dev_kfree_skb_any(ptask->skb);
			kmem_cache_free(fwnet_packet_task_cache, ptask);
J
Jay Fenlason 已提交
1582
		}
S
Stefan Richter 已提交
1583 1584 1585 1586 1587 1588
		list_for_each_entry(peer, &card->peer_list, peer_link) {
			if (peer->pdg_size) {
				list_for_each_entry_safe(pd, pd_next,
						&peer->pd_list, pd_link)
					fwnet_pd_delete(pd);
				peer->pdg_size = 0;
J
Jay Fenlason 已提交
1589
			}
S
Stefan Richter 已提交
1590
			peer->fifo = FWNET_NO_FIFO_ADDR;
J
Jay Fenlason 已提交
1591
		}
S
Stefan Richter 已提交
1592
		free_netdev(net);
J
Jay Fenlason 已提交
1593 1594
		card->netdev = NULL;
	}
S
Stefan Richter 已提交
1595

J
Jay Fenlason 已提交
1596 1597 1598
	return 0;
}

S
Stefan Richter 已提交
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
/*
 * 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 net_device *net = device->card->netdev;
	struct fwnet_device *dev;
	struct fwnet_peer *peer;
	u64 guid;
J
Jay Fenlason 已提交
1610

S
Stefan Richter 已提交
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
	if (net && !device->is_local) {
		dev = netdev_priv(net);
		guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
		peer = fwnet_peer_find_by_guid(dev, guid);
		if (!peer) {
			fw_error("fwnet_update: no peer for device %016llx\n",
				 (unsigned long long)guid);
			return;
		}
		peer->generation = device->generation;
		rmb();
		peer->node_id = device->node_id;
J
Jay Fenlason 已提交
1623 1624 1625
	}
}

S
Stefan Richter 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
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,
	},
	{ }
};

static struct fw_driver fwnet_driver = {
J
Jay Fenlason 已提交
1637
	.driver = {
S
Stefan Richter 已提交
1638 1639 1640 1641 1642
		.owner  = THIS_MODULE,
		.name   = "net",
		.bus    = &fw_bus_type,
		.probe  = fwnet_probe,
		.remove = fwnet_remove,
J
Jay Fenlason 已提交
1643
	},
S
Stefan Richter 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	.update   = fwnet_update,
	.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
J
Jay Fenlason 已提交
1668 1669
};

S
Stefan Richter 已提交
1670 1671 1672 1673 1674 1675 1676
static int __init fwnet_init(void)
{
	int err;

	err = fw_core_add_descriptor(&rfc2374_unit_directory);
	if (err)
		return err;
J
Jay Fenlason 已提交
1677

S
Stefan Richter 已提交
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
	fwnet_packet_task_cache = kmem_cache_create("packet_task",
			sizeof(struct fwnet_packet_task), 0, 0, NULL);
	if (!fwnet_packet_task_cache) {
		err = -ENOMEM;
		goto out;
	}

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

	kmem_cache_destroy(fwnet_packet_task_cache);
out:
	fw_core_remove_descriptor(&rfc2374_unit_directory);

	return err;
J
Jay Fenlason 已提交
1694
}
S
Stefan Richter 已提交
1695
module_init(fwnet_init);
J
Jay Fenlason 已提交
1696

S
Stefan Richter 已提交
1697 1698 1699 1700 1701
static void __exit fwnet_cleanup(void)
{
	driver_unregister(&fwnet_driver.driver);
	kmem_cache_destroy(fwnet_packet_task_cache);
	fw_core_remove_descriptor(&rfc2374_unit_directory);
J
Jay Fenlason 已提交
1702
}
S
Stefan Richter 已提交
1703
module_exit(fwnet_cleanup);
J
Jay Fenlason 已提交
1704

S
Stefan Richter 已提交
1705 1706 1707 1708
MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>");
MODULE_DESCRIPTION("IPv4 over IEEE1394 as per RFC 2734");
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table);