Skip to content
Snippets Groups Projects
net.c 43.3 KiB
Newer Older
Jay Fenlason's avatar
Jay Fenlason committed
/*
 * IPv4 over IEEE 1394, per RFC 2734
 *
 * Copyright (C) 2009 Jay Fenlason <fenlason@redhat.com>
 *
 * based on eth1394 by Ben Collins et al
 */

#include <linux/bug.h>
#include <linux/delay.h>
Jay Fenlason's avatar
Jay Fenlason committed
#include <linux/device.h>
#include <linux/ethtool.h>
Jay Fenlason's avatar
Jay Fenlason committed
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/highmem.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/jiffies.h>
Jay Fenlason's avatar
Jay Fenlason committed
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
Jay Fenlason's avatar
Jay Fenlason committed
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
Jay Fenlason's avatar
Jay Fenlason committed

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

/* 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 /* ? */
#define IEEE1394_BROADCAST_CHANNEL	31
#define IEEE1394_ALL_NODES		(0xffc0 | 0x003f)
#define IEEE1394_MAX_PAYLOAD_S100	512
#define FWNET_NO_FIFO_ADDR		(~0ULL)
#define IANA_SPECIFIER_ID		0x00005eU
#define RFC2734_SW_VERSION		0x000001U
#define IEEE1394_GASP_HDR_SIZE	8
#define RFC2374_UNFRAG_HDR_SIZE	4
#define RFC2374_FRAG_HDR_SIZE	8
#define RFC2374_FRAG_OVERHEAD	4
#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	*/
#define RFC2734_HW_ADDR_LEN	16
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 */
	__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	*/
/* 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 */
/* IPv4 and IPv6 encapsulation header */
struct rfc2734_header {
Jay Fenlason's avatar
Jay Fenlason committed
	u32 w0;
	u32 w1;
};

#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)
#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)
#define fwnet_set_hdr_dgl(dgl)		((dgl) << 16)
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);
}
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);
}
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

/* This list keeps track of what parts of the datagram have been filled in */
struct fwnet_fragment_info {
	struct list_head fi_link;
Jay Fenlason's avatar
Jay Fenlason committed
	u16 offset;
	u16 len;
};

struct fwnet_partial_datagram {
	struct list_head pd_link;
	struct list_head fi_list;
Jay Fenlason's avatar
Jay Fenlason committed
	struct sk_buff *skb;
	/* FIXME Why not use skb->data? */
	char *pbuf;
	u16 datagram_label;
	u16 ether_type;
	u16 datagram_size;
};

static DEFINE_MUTEX(fwnet_device_mutex);
static LIST_HEAD(fwnet_device_list);
struct fwnet_device {
	struct list_head dev_link;
Jay Fenlason's avatar
Jay Fenlason committed
	spinlock_t lock;
	enum {
		FWNET_BROADCAST_ERROR,
		FWNET_BROADCAST_RUNNING,
		FWNET_BROADCAST_STOPPED,
	} broadcast_state;
Jay Fenlason's avatar
Jay Fenlason committed
	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;

	/*
	 * The CSR address that remote nodes must send datagrams to for us to
Jay Fenlason's avatar
Jay Fenlason committed
	 * receive them.
	 */
	struct fw_address_handler handler;
	u64 local_fifo;

	/* Number of tx datagrams that have been queued but not yet acked */
	int queued_datagrams;
	int peer_count;
	struct list_head peer_list;
Jay Fenlason's avatar
Jay Fenlason committed
	struct fw_card *card;
	struct net_device *netdev;
};

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

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

	u16 datagram_label;       /* outgoing datagram label */
	u16 max_payload;          /* includes RFC2374_FRAG_HDR_SIZE overhead */
	int node_id;
	int generation;
	unsigned speed;
Jay Fenlason's avatar
Jay Fenlason committed
};

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

Jay Fenlason's avatar
Jay Fenlason committed
	int outstanding_pkts;
	u64 fifo_addr;
	u16 dest_node;
	u16 max_payload;
Jay Fenlason's avatar
Jay Fenlason committed
	u8 generation;
	u8 speed;
/*
 * 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;
	h = (struct fwnet_header *)skb_push(skb, sizeof(*h));
	put_unaligned_be16(type, &h->h_proto);
	if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) {
		memset(h->h_dest, 0, net->addr_len);
		return net->hard_header_len;
Jay Fenlason's avatar
Jay Fenlason committed
	}

	if (daddr) {
		memcpy(h->h_dest, daddr, net->addr_len);

		return net->hard_header_len;
	return -net->hard_header_len;
static int fwnet_header_rebuild(struct sk_buff *skb)
	struct fwnet_header *h = (struct fwnet_header *)skb->data;
	if (get_unaligned_be16(&h->h_proto) == ETH_P_IP)
		return arp_find((unsigned char *)&h->h_dest, skb);
	fw_notify("%s: unable to resolve type %04x addresses\n",
		  skb->dev->name, be16_to_cpu(h->h_proto));
static int fwnet_header_cache(const struct neighbour *neigh,
			      struct hh_cache *hh, __be16 type)
{
	struct net_device *net;
	struct fwnet_header *h;
	if (type == cpu_to_be16(ETH_P_802_3))
Jay Fenlason's avatar
Jay Fenlason committed
		return -1;
	net = neigh->dev;
	h = (struct fwnet_header *)((u8 *)hh->hh_data + 16 - sizeof(*h));
	memcpy(h->h_dest, neigh->ha, net->addr_len);
	hh->hh_len = FWNET_HLEN;
Jay Fenlason's avatar
Jay Fenlason committed

	return 0;
}

/* Called by Address Resolution module to notify changes in address. */
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);
static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{
	memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN);

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

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

	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
			return true;
Jay Fenlason's avatar
Jay Fenlason committed
	return false;
}

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

	list = &pd->fi_list;
	list_for_each_entry(fi, &pd->fi_list, fi_link) {
Jay Fenlason's avatar
Jay Fenlason committed
		if (fi->offset + fi->len == offset) {
			/* The new fragment can be tacked on to the end */
			/* Did the new fragment plug a hole? */
			fi2 = list_entry(fi->fi_link.next,
					 struct fwnet_fragment_info, fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
			if (fi->offset + fi->len == fi2->offset) {
				/* glue fragments together */
				fi->len += len + fi2->len;
				list_del(&fi2->fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
				kfree(fi2);
			} else {
				fi->len += len;
			}
Jay Fenlason's avatar
Jay Fenlason committed
			return fi;
		}
		if (offset + len == fi->offset) {
			/* The new fragment can be tacked on to the beginning */
			/* Did the new fragment plug a hole? */
			fi2 = list_entry(fi->fi_link.prev,
					 struct fwnet_fragment_info, fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
			if (fi2->offset + fi2->len == fi->offset) {
				/* glue fragments together */
				fi2->len += fi->len + len;
				list_del(&fi->fi_link);
Jay Fenlason's avatar
Jay Fenlason committed
				kfree(fi);
Jay Fenlason's avatar
Jay Fenlason committed
				return fi2;
			}
			fi->offset = offset;
			fi->len += len;
Jay Fenlason's avatar
Jay Fenlason committed
			return fi;
		}
		if (offset > fi->offset + fi->len) {
			list = &fi->fi_link;
Jay Fenlason's avatar
Jay Fenlason committed
			break;
		}
		if (offset + len < fi->offset) {
			list = fi->fi_link.prev;
Jay Fenlason's avatar
Jay Fenlason committed
			break;
		}
	}

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
	if (!new) {
		fw_error("out of memory\n");
Jay Fenlason's avatar
Jay Fenlason committed
		return NULL;
	}

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

Jay Fenlason's avatar
Jay Fenlason committed
	return new;
}

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

	new = kmalloc(sizeof(*new), GFP_ATOMIC);
	if (!new)
		goto fail;

	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
		goto fail_w_new;
Jay Fenlason's avatar
Jay Fenlason committed
	new->datagram_label = datagram_label;
	new->datagram_size = dg_size;
	new->skb = dev_alloc_skb(dg_size + net->hard_header_len + 15);
	if (new->skb == NULL)
Jay Fenlason's avatar
Jay Fenlason committed
		goto fail_w_fi;

	skb_reserve(new->skb, (net->hard_header_len + 15) & ~15);
Jay Fenlason's avatar
Jay Fenlason committed
	new->pbuf = skb_put(new->skb, dg_size);
	memcpy(new->pbuf + frag_off, frag_buf, frag_len);
	list_add_tail(&new->pd_link, &peer->pd_list);

Jay Fenlason's avatar
Jay Fenlason committed
	return new;

fail_w_fi:
	kfree(fi);
fail_w_new:
	kfree(new);
fail:
	fw_error("out of memory\n");

Jay Fenlason's avatar
Jay Fenlason committed
	return NULL;
}

static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer,
						    u16 datagram_label)
{
	struct fwnet_partial_datagram *pd;
	list_for_each_entry(pd, &peer->pd_list, pd_link)
		if (pd->datagram_label == datagram_label)
Jay Fenlason's avatar
Jay Fenlason committed
			return pd;
static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
{
	struct fwnet_fragment_info *fi, *n;
	list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
Jay Fenlason's avatar
Jay Fenlason committed
		kfree(fi);

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

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
		return false;
Jay Fenlason's avatar
Jay Fenlason committed
	memcpy(pd->pbuf + frag_off, frag_buf, frag_len);

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

Jay Fenlason's avatar
Jay Fenlason committed
	return true;
}

static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
{
	struct fwnet_fragment_info *fi;
	fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
	return fi->len == pd->datagram_size;
/* caller must hold dev->lock */
static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
						  u64 guid)
{
	struct fwnet_peer *peer;
	list_for_each_entry(peer, &dev->peer_list, peer_link)
		if (peer->guid == guid)
			return peer;
/* caller must hold dev->lock */
static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
						int node_id, int generation)
	struct fwnet_peer *peer;
	list_for_each_entry(peer, &dev->peer_list, peer_link)
		if (peer->node_id    == node_id &&
		    peer->generation == generation)
			return peer;
/* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */
static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed)
	max_rec = min(max_rec, speed + 8);
	max_rec = min(max_rec, 0xbU); /* <= 4096 */
	if (max_rec < 8) {
		fw_notify("max_rec %x out of range\n", max_rec);
		max_rec = 8;

	return (1 << (max_rec + 1)) - RFC2374_FRAG_HDR_SIZE;
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);
Jay Fenlason's avatar
Jay Fenlason committed
	int status;
	__be64 guid;
	dev = netdev_priv(net);
Jay Fenlason's avatar
Jay Fenlason committed
	/* Write metadata, and then pass to the receive level */
	skb->dev = net;
Jay Fenlason's avatar
Jay Fenlason committed
	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) {
		struct rfc2734_arp *arp1394;
Jay Fenlason's avatar
Jay Fenlason committed
		struct arphdr *arp;
		unsigned char *arp_ptr;
		u64 fifo_addr;
		u64 peer_guid;
Jay Fenlason's avatar
Jay Fenlason committed
		u16 max_payload;
		struct fwnet_peer *peer;
		unsigned long flags;

		arp1394   = (struct rfc2734_arp *)skb->data;
		arp       = (struct arphdr *)skb->data;
		arp_ptr   = (unsigned char *)(arp + 1);
		peer_guid = get_unaligned_be64(&arp1394->s_uniq_id);
		fifo_addr = (u64)get_unaligned_be16(&arp1394->fifo_hi) << 32
				| get_unaligned_be32(&arp1394->fifo_lo);
Jay Fenlason's avatar
Jay Fenlason committed

		sspd = arp1394->sspd;
		/* Sanity check.  OS X 10.3 PPC reportedly sends 131. */
		if (sspd > SCODE_3200) {
			fw_notify("sspd %x out of range\n", sspd);
		max_payload = fwnet_max_payload(arp1394->max_rec, sspd);
		spin_lock_irqsave(&dev->lock, flags);
		peer = fwnet_peer_find_by_guid(dev, peer_guid);
		if (peer) {
			peer->fifo = fifo_addr;

			if (peer->speed > sspd)
				peer->speed = sspd;
			if (peer->max_payload > max_payload)
				peer->max_payload = max_payload;
		}
		spin_unlock_irqrestore(&dev->lock, flags);

		if (!peer) {
			fw_notify("No peer for ARP packet from %016llx\n",
				  (unsigned long long)peer_guid);
Jay Fenlason's avatar
Jay Fenlason committed
		/*
		 * 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;
		/* 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;
Jay Fenlason's avatar
Jay Fenlason committed

		if (arp->ar_op == htons(ARPOP_REQUEST))
			memset(arp_ptr, 0, sizeof(u64));
		else
			memcpy(arp_ptr, net->dev_addr, sizeof(u64));
Jay Fenlason's avatar
Jay Fenlason committed
	}

	/* Now add the ethernet header. */
	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;
Jay Fenlason's avatar
Jay Fenlason committed
		u16 *rawp;
		__be16 protocol;

		skb_reset_mac_header(skb);
		skb_pull(skb, sizeof(*eth));
		eth = (struct fwnet_header *)skb_mac_header(skb);
Jay Fenlason's avatar
Jay Fenlason committed
		if (*eth->h_dest & 1) {
			if (memcmp(eth->h_dest, net->broadcast,
				   net->addr_len) == 0)
Jay Fenlason's avatar
Jay Fenlason committed
				skb->pkt_type = PACKET_BROADCAST;
#if 0
			else
				skb->pkt_type = PACKET_MULTICAST;
#endif
		} else {
			if (memcmp(eth->h_dest, net->dev_addr, net->addr_len))
Jay Fenlason's avatar
Jay Fenlason committed
				skb->pkt_type = PACKET_OTHERHOST;
		}
		if (ntohs(eth->h_proto) >= 1536) {
			protocol = eth->h_proto;
		} else {
			rawp = (u16 *)skb->data;
			if (*rawp == 0xffff)
Jay Fenlason's avatar
Jay Fenlason committed
				protocol = htons(ETH_P_802_3);
Jay Fenlason's avatar
Jay Fenlason committed
				protocol = htons(ETH_P_802_2);
		}
		skb->protocol = protocol;
	}
	status = netif_rx(skb);
	if (status == NET_RX_DROP) {
		net->stats.rx_errors++;
		net->stats.rx_dropped++;
Jay Fenlason's avatar
Jay Fenlason committed
	} else {
		net->stats.rx_packets++;
		net->stats.rx_bytes += skb->len;
Jay Fenlason's avatar
Jay Fenlason committed
	return 0;

	net->stats.rx_errors++;
	net->stats.rx_dropped++;

Jay Fenlason's avatar
Jay Fenlason committed
	dev_kfree_skb_any(skb);
static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
				 int source_node_id, int generation,
				 bool is_broadcast)
Jay Fenlason's avatar
Jay Fenlason committed
	struct sk_buff *skb;
	struct net_device *net = dev->netdev;
	struct rfc2734_header hdr;
Jay Fenlason's avatar
Jay Fenlason committed
	unsigned lf;
	unsigned long flags;
	struct fwnet_peer *peer;
	struct fwnet_partial_datagram *pd;
Jay Fenlason's avatar
Jay Fenlason committed
	int fg_off;
	int dg_size;
	u16 datagram_label;
	int retval;
	u16 ether_type;

	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
		/*
		 * 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.
		 */
		ether_type = fwnet_get_hdr_ether_type(&hdr);
Jay Fenlason's avatar
Jay Fenlason committed
		buf++;
		len -= RFC2374_UNFRAG_HDR_SIZE;
		skb = dev_alloc_skb(len + net->hard_header_len + 15);
Jay Fenlason's avatar
Jay Fenlason committed
		if (unlikely(!skb)) {
			fw_error("out of memory\n");
			net->stats.rx_dropped++;

		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);
Jay Fenlason's avatar
Jay Fenlason committed
	}
	/* A datagram fragment has been received, now the fun begins. */
	hdr.w1 = ntohl(buf[1]);
	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
		fg_off = 0;
	} else {
		ether_type = 0;
		fg_off = fwnet_get_hdr_fg_off(&hdr);
	datagram_label = fwnet_get_hdr_dgl(&hdr);
	dg_size = fwnet_get_hdr_dg_size(&hdr); /* ??? + 1 */

	spin_lock_irqsave(&dev->lock, flags);

	peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation);
	if (!peer) {
		retval = -ENOENT;
		goto fail;
	}

	pd = fwnet_pd_find(peer, datagram_label);
Jay Fenlason's avatar
Jay Fenlason committed
	if (pd == NULL) {
		while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) {
Jay Fenlason's avatar
Jay Fenlason committed
			/* remove the oldest */
			fwnet_pd_delete(list_first_entry(&peer->pd_list,
				struct fwnet_partial_datagram, pd_link));
			peer->pdg_size--;
		pd = fwnet_pd_new(net, peer, datagram_label,
				  dg_size, buf, fg_off, len);
		if (pd == NULL) {
Jay Fenlason's avatar
Jay Fenlason committed
			retval = -ENOMEM;
		peer->pdg_size++;
Jay Fenlason's avatar
Jay Fenlason committed
	} else {
		if (fwnet_frag_overlap(pd, fg_off, len) ||
		    pd->datagram_size != dg_size) {
Jay Fenlason's avatar
Jay Fenlason committed
			/*
			 * Differing datagram sizes or overlapping fragments,
			 * discard old datagram and start a new one.
			fwnet_pd_delete(pd);
			pd = fwnet_pd_new(net, peer, datagram_label,
					  dg_size, buf, fg_off, len);
			if (pd == NULL) {
				peer->pdg_size--;
Jay Fenlason's avatar
Jay Fenlason committed
			}
		} else {
			if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) {
Jay Fenlason's avatar
Jay Fenlason committed
				/*
				 * Couldn't save off fragment anyway
				 * so might as well obliterate the
				 * datagram now.
				 */
				fwnet_pd_delete(pd);
				peer->pdg_size--;
Jay Fenlason's avatar
Jay Fenlason committed
			}
		}
	} /* new datagram or add to existing one */

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

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

		spin_unlock_irqrestore(&dev->lock, flags);

		return fwnet_finish_incoming_packet(net, skb, source_node_id,
						    false, ether_type);
Jay Fenlason's avatar
Jay Fenlason committed
	}
	/*
	 * Datagram is not complete, we're done for the
	 * moment.
	 */
	spin_unlock_irqrestore(&dev->lock, flags);
static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r,
		int tcode, int destination, int source, int generation,
		unsigned long long offset, void *payload, size_t length,
		void *callback_data)
	struct fwnet_device *dev = callback_data;
	int rcode;
	if (destination == IEEE1394_ALL_NODES) {
		kfree(r);
Jay Fenlason's avatar
Jay Fenlason committed
		return;
	}
	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) {
		fw_error("Incoming packet failure\n");
		rcode = RCODE_CONFLICT_ERROR;
	} else
		rcode = RCODE_COMPLETE;
	fw_send_response(card, r, rcode);
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
	struct fw_iso_packet packet;
	struct fw_card *card;
	__be16 *hdr_ptr;
	__be32 *buf_ptr;
Jay Fenlason's avatar
Jay Fenlason committed
	int retval;
	u32 length;
	u16 source_node_id;
	u32 specifier_id;
	u32 ver;
	unsigned long offset;
	unsigned long flags;

	dev = data;
	card = dev->card;
Jay Fenlason's avatar
Jay Fenlason committed
	hdr_ptr = header;
	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

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

	if (specifier_id == IANA_SPECIFIER_ID && ver == RFC2734_SW_VERSION) {
Jay Fenlason's avatar
Jay Fenlason committed
		buf_ptr += 2;
		length -= IEEE1394_GASP_HDR_SIZE;
		fwnet_incoming_packet(dev, buf_ptr, length,
				      source_node_id, -1, true);
	}

	packet.payload_length = dev->rcv_buffer_size;
Jay Fenlason's avatar
Jay Fenlason committed
	packet.interrupt = 1;
	packet.skip = 0;
	packet.tag = 3;
	packet.sy = 0;
	packet.header_length = IEEE1394_GASP_HDR_SIZE;

	spin_lock_irqsave(&dev->lock, flags);
	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_iso_context_queue_flush(dev->broadcast_rcv_context);
	else
		fw_error("requeue failed\n");
static struct kmem_cache *fwnet_packet_task_cache;

static void fwnet_free_ptask(struct fwnet_packet_task *ptask)
{
	dev_kfree_skb_any(ptask->skb);
	kmem_cache_free(fwnet_packet_task_cache, ptask);
}

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

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 = ptask->dev;
	struct sk_buff *skb = ptask->skb;
Jay Fenlason's avatar
Jay Fenlason committed
	unsigned long flags;

	spin_lock_irqsave(&dev->lock, flags);

	ptask->outstanding_pkts--;

	/* Check whether we or the networking TX soft-IRQ is last user. */
	free = (ptask->outstanding_pkts == 0 && ptask->enqueued);
	if (free)
	if (ptask->outstanding_pkts == 0) {
		dev->netdev->stats.tx_packets++;
		dev->netdev->stats.tx_bytes += skb->len;
	}

	spin_unlock_irqrestore(&dev->lock, flags);

	if (ptask->outstanding_pkts > 0) {
Jay Fenlason's avatar
Jay Fenlason committed
		u16 dg_size;
		u16 fg_off;
		u16 datagram_label;
		u16 lf;

		/* Update the ptask to point to the next fragment and send it */
		lf = fwnet_get_hdr_lf(&ptask->hdr);
Jay Fenlason's avatar
Jay Fenlason committed
		switch (lf) {
		case RFC2374_HDR_LASTFRAG:
		case RFC2374_HDR_UNFRAG:
Jay Fenlason's avatar
Jay Fenlason committed
		default:
			fw_error("Outstanding packet %x lf %x, header %x,%x\n",
				 ptask->outstanding_pkts, lf, ptask->hdr.w0,
				 ptask->hdr.w1);
		case RFC2374_HDR_FIRSTFRAG:
Jay Fenlason's avatar
Jay Fenlason committed
			/* Set frag type here for future interior fragments */
			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);
		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
			break;
		}
		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);
Jay Fenlason's avatar
Jay Fenlason committed
		} else {
			fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG,
					  dg_size, fg_off, datagram_label);
			ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE;
		fwnet_send_packet(ptask);

	if (free)
		fwnet_free_ptask(ptask);
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. */
	free = ptask->enqueued;
	if (free)

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

	spin_unlock_irqrestore(&dev->lock, flags);

	if (free)