blob: 7142eeec8074374d87109ec98d65f78d28bd0e74 [file] [log] [blame]
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001/*
2 * IPv4 over IEEE 1394, per RFC 2734
3 *
4 * Copyright (C) 2009 Jay Fenlason <fenlason@redhat.com>
5 *
6 * based on eth1394 by Ben Collins et al
7 */
8
Stefan Richterf91e3bd2009-06-07 22:57:53 +02009#include <linux/bug.h>
Jay Fenlasonc76acec2009-05-18 13:08:06 -040010#include <linux/device.h>
11#include <linux/ethtool.h>
12#include <linux/firewire.h>
13#include <linux/firewire-constants.h>
14#include <linux/highmem.h>
15#include <linux/in.h>
16#include <linux/ip.h>
Stefan Richterf91e3bd2009-06-07 22:57:53 +020017#include <linux/jiffies.h>
Jay Fenlasonc76acec2009-05-18 13:08:06 -040018#include <linux/mod_devicetable.h>
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Stefan Richter5a124d32009-06-14 11:45:27 +020021#include <linux/mutex.h>
Jay Fenlasonc76acec2009-05-18 13:08:06 -040022#include <linux/netdevice.h>
23#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Stefan Richter5a124d32009-06-14 11:45:27 +020025#include <linux/spinlock.h>
Jay Fenlasonc76acec2009-05-18 13:08:06 -040026
27#include <asm/unaligned.h>
28#include <net/arp.h>
29
Stefan Richterf91e3bd2009-06-07 22:57:53 +020030#define FWNET_MAX_FRAGMENTS 25 /* arbitrary limit */
31#define FWNET_ISO_PAGE_COUNT (PAGE_SIZE < 16 * 1024 ? 4 : 2)
Jay Fenlasonc76acec2009-05-18 13:08:06 -040032
Stefan Richterf91e3bd2009-06-07 22:57:53 +020033#define IEEE1394_BROADCAST_CHANNEL 31
34#define IEEE1394_ALL_NODES (0xffc0 | 0x003f)
35#define IEEE1394_MAX_PAYLOAD_S100 512
36#define FWNET_NO_FIFO_ADDR (~0ULL)
Jay Fenlasonc76acec2009-05-18 13:08:06 -040037
Stefan Richterf91e3bd2009-06-07 22:57:53 +020038#define IANA_SPECIFIER_ID 0x00005eU
39#define RFC2734_SW_VERSION 0x000001U
Jay Fenlasonc76acec2009-05-18 13:08:06 -040040
Stefan Richterf91e3bd2009-06-07 22:57:53 +020041#define IEEE1394_GASP_HDR_SIZE 8
Jay Fenlasonc76acec2009-05-18 13:08:06 -040042
Stefan Richterf91e3bd2009-06-07 22:57:53 +020043#define RFC2374_UNFRAG_HDR_SIZE 4
44#define RFC2374_FRAG_HDR_SIZE 8
45#define RFC2374_FRAG_OVERHEAD 4
Jay Fenlasonc76acec2009-05-18 13:08:06 -040046
Stefan Richterf91e3bd2009-06-07 22:57:53 +020047#define RFC2374_HDR_UNFRAG 0 /* unfragmented */
48#define RFC2374_HDR_FIRSTFRAG 1 /* first fragment */
49#define RFC2374_HDR_LASTFRAG 2 /* last fragment */
50#define RFC2374_HDR_INTFRAG 3 /* interior fragment */
Jay Fenlasonc76acec2009-05-18 13:08:06 -040051
Stefan Richterf91e3bd2009-06-07 22:57:53 +020052#define RFC2734_HW_ADDR_LEN 16
Jay Fenlasonc76acec2009-05-18 13:08:06 -040053
Stefan Richterf91e3bd2009-06-07 22:57:53 +020054struct rfc2734_arp {
55 __be16 hw_type; /* 0x0018 */
56 __be16 proto_type; /* 0x0806 */
Jay Fenlasonc76acec2009-05-18 13:08:06 -040057 u8 hw_addr_len; /* 16 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +020058 u8 ip_addr_len; /* 4 */
59 __be16 opcode; /* ARP Opcode */
Jay Fenlasonc76acec2009-05-18 13:08:06 -040060 /* Above is exactly the same format as struct arphdr */
61
Stefan Richterf91e3bd2009-06-07 22:57:53 +020062 __be64 s_uniq_id; /* Sender's 64bit EUI */
63 u8 max_rec; /* Sender's max packet size */
Jay Fenlasonc76acec2009-05-18 13:08:06 -040064 u8 sspd; /* Sender's max speed */
Stefan Richterf91e3bd2009-06-07 22:57:53 +020065 __be16 fifo_hi; /* hi 16bits of sender's FIFO addr */
66 __be32 fifo_lo; /* lo 32bits of sender's FIFO addr */
67 __be32 sip; /* Sender's IP Address */
68 __be32 tip; /* IP Address of requested hw addr */
Jay Fenlasonc76acec2009-05-18 13:08:06 -040069} __attribute__((packed));
70
Stefan Richterf91e3bd2009-06-07 22:57:53 +020071/* This header format is specific to this driver implementation. */
72#define FWNET_ALEN 8
73#define FWNET_HLEN 10
74struct fwnet_header {
75 u8 h_dest[FWNET_ALEN]; /* destination address */
76 __be16 h_proto; /* packet type ID field */
77} __attribute__((packed));
Jay Fenlasonc76acec2009-05-18 13:08:06 -040078
Stefan Richterf91e3bd2009-06-07 22:57:53 +020079/* IPv4 and IPv6 encapsulation header */
80struct rfc2734_header {
Jay Fenlasonc76acec2009-05-18 13:08:06 -040081 u32 w0;
82 u32 w1;
83};
84
Stefan Richterf91e3bd2009-06-07 22:57:53 +020085#define fwnet_get_hdr_lf(h) (((h)->w0 & 0xc0000000) >> 30)
86#define fwnet_get_hdr_ether_type(h) (((h)->w0 & 0x0000ffff))
87#define fwnet_get_hdr_dg_size(h) (((h)->w0 & 0x0fff0000) >> 16)
88#define fwnet_get_hdr_fg_off(h) (((h)->w0 & 0x00000fff))
89#define fwnet_get_hdr_dgl(h) (((h)->w1 & 0xffff0000) >> 16)
90
91#define fwnet_set_hdr_lf(lf) ((lf) << 30)
92#define fwnet_set_hdr_ether_type(et) (et)
93#define fwnet_set_hdr_dg_size(dgs) ((dgs) << 16)
94#define fwnet_set_hdr_fg_off(fgo) (fgo)
95
96#define fwnet_set_hdr_dgl(dgl) ((dgl) << 16)
97
98static inline void fwnet_make_uf_hdr(struct rfc2734_header *hdr,
99 unsigned ether_type)
100{
101 hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_UNFRAG)
102 | fwnet_set_hdr_ether_type(ether_type);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400103}
104
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200105static inline void fwnet_make_ff_hdr(struct rfc2734_header *hdr,
106 unsigned ether_type, unsigned dg_size, unsigned dgl)
107{
108 hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_FIRSTFRAG)
109 | fwnet_set_hdr_dg_size(dg_size)
110 | fwnet_set_hdr_ether_type(ether_type);
111 hdr->w1 = fwnet_set_hdr_dgl(dgl);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400112}
113
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200114static inline void fwnet_make_sf_hdr(struct rfc2734_header *hdr,
115 unsigned lf, unsigned dg_size, unsigned fg_off, unsigned dgl)
116{
117 hdr->w0 = fwnet_set_hdr_lf(lf)
118 | fwnet_set_hdr_dg_size(dg_size)
119 | fwnet_set_hdr_fg_off(fg_off);
120 hdr->w1 = fwnet_set_hdr_dgl(dgl);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400121}
122
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400123/* This list keeps track of what parts of the datagram have been filled in */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200124struct fwnet_fragment_info {
125 struct list_head fi_link;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400126 u16 offset;
127 u16 len;
128};
129
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200130struct fwnet_partial_datagram {
131 struct list_head pd_link;
132 struct list_head fi_list;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400133 struct sk_buff *skb;
134 /* FIXME Why not use skb->data? */
135 char *pbuf;
136 u16 datagram_label;
137 u16 ether_type;
138 u16 datagram_size;
139};
140
Stefan Richter5a124d32009-06-14 11:45:27 +0200141static DEFINE_MUTEX(fwnet_device_mutex);
142static LIST_HEAD(fwnet_device_list);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400143
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200144struct fwnet_device {
Stefan Richter5a124d32009-06-14 11:45:27 +0200145 struct list_head dev_link;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400146 spinlock_t lock;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200147 enum {
148 FWNET_BROADCAST_ERROR,
149 FWNET_BROADCAST_RUNNING,
150 FWNET_BROADCAST_STOPPED,
151 } broadcast_state;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400152 struct fw_iso_context *broadcast_rcv_context;
153 struct fw_iso_buffer broadcast_rcv_buffer;
154 void **broadcast_rcv_buffer_ptrs;
155 unsigned broadcast_rcv_next_ptr;
156 unsigned num_broadcast_rcv_ptrs;
157 unsigned rcv_buffer_size;
158 /*
159 * This value is the maximum unfragmented datagram size that can be
160 * sent by the hardware. It already has the GASP overhead and the
161 * unfragmented datagram header overhead calculated into it.
162 */
163 unsigned broadcast_xmt_max_payload;
164 u16 broadcast_xmt_datagramlabel;
165
166 /*
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200167 * The CSR address that remote nodes must send datagrams to for us to
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400168 * receive them.
169 */
170 struct fw_address_handler handler;
171 u64 local_fifo;
172
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400173 /* List of packets to be sent */
174 struct list_head packet_list;
175 /*
176 * List of packets that were broadcasted. When we get an ISO interrupt
177 * one of them has been sent
178 */
179 struct list_head broadcasted_list;
180 /* List of packets that have been sent but not yet acked */
181 struct list_head sent_list;
182
Stefan Richter5a124d32009-06-14 11:45:27 +0200183 struct list_head peer_list;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400184 struct fw_card *card;
Stefan Richter5a124d32009-06-14 11:45:27 +0200185 struct net_device *netdev;
186};
187
188struct fwnet_peer {
189 struct list_head peer_link;
190 struct fwnet_device *dev;
191 u64 guid;
192 u64 fifo;
193
194 /* guarded by dev->lock */
195 struct list_head pd_list; /* received partial datagrams */
196 unsigned pdg_size; /* pd_list size */
197
198 u16 datagram_label; /* outgoing datagram label */
199 unsigned max_payload; /* includes RFC2374_FRAG_HDR_SIZE overhead */
200 int node_id;
201 int generation;
202 unsigned speed;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400203};
204
205/* This is our task struct. It's used for the packet complete callback. */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200206struct fwnet_packet_task {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400207 /*
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200208 * ptask can actually be on dev->packet_list, dev->broadcasted_list,
209 * or dev->sent_list depending on its current state.
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400210 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200211 struct list_head pt_link;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400212 struct fw_transaction transaction;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200213 struct rfc2734_header hdr;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400214 struct sk_buff *skb;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200215 struct fwnet_device *dev;
216
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400217 int outstanding_pkts;
218 unsigned max_payload;
219 u64 fifo_addr;
220 u16 dest_node;
221 u8 generation;
222 u8 speed;
223};
224
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200225/*
226 * saddr == NULL means use device source address.
227 * daddr == NULL means leave destination address (eg unresolved arp).
228 */
229static int fwnet_header_create(struct sk_buff *skb, struct net_device *net,
230 unsigned short type, const void *daddr,
231 const void *saddr, unsigned len)
232{
233 struct fwnet_header *h;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400234
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200235 h = (struct fwnet_header *)skb_push(skb, sizeof(*h));
236 put_unaligned_be16(type, &h->h_proto);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400237
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200238 if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) {
239 memset(h->h_dest, 0, net->addr_len);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400240
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200241 return net->hard_header_len;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400242 }
243
244 if (daddr) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200245 memcpy(h->h_dest, daddr, net->addr_len);
246
247 return net->hard_header_len;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400248 }
249
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200250 return -net->hard_header_len;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400251}
252
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200253static int fwnet_header_rebuild(struct sk_buff *skb)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400254{
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200255 struct fwnet_header *h = (struct fwnet_header *)skb->data;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400256
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200257 if (get_unaligned_be16(&h->h_proto) == ETH_P_IP)
258 return arp_find((unsigned char *)&h->h_dest, skb);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400259
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200260 fw_notify("%s: unable to resolve type %04x addresses\n",
261 skb->dev->name, be16_to_cpu(h->h_proto));
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400262 return 0;
263}
264
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200265static int fwnet_header_cache(const struct neighbour *neigh,
266 struct hh_cache *hh)
267{
268 struct net_device *net;
269 struct fwnet_header *h;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400270
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200271 if (hh->hh_type == cpu_to_be16(ETH_P_802_3))
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400272 return -1;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200273 net = neigh->dev;
274 h = (struct fwnet_header *)((u8 *)hh->hh_data + 16 - sizeof(*h));
275 h->h_proto = hh->hh_type;
276 memcpy(h->h_dest, neigh->ha, net->addr_len);
277 hh->hh_len = FWNET_HLEN;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400278
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400279 return 0;
280}
281
282/* Called by Address Resolution module to notify changes in address. */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200283static void fwnet_header_cache_update(struct hh_cache *hh,
284 const struct net_device *net, const unsigned char *haddr)
285{
286 memcpy((u8 *)hh->hh_data + 16 - FWNET_HLEN, haddr, net->addr_len);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400287}
288
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200289static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr)
290{
291 memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN);
292
293 return FWNET_ALEN;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400294}
295
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200296static const struct header_ops fwnet_header_ops = {
297 .create = fwnet_header_create,
298 .rebuild = fwnet_header_rebuild,
299 .cache = fwnet_header_cache,
300 .cache_update = fwnet_header_cache_update,
301 .parse = fwnet_header_parse,
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400302};
303
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400304/* FIXME: is this correct for all cases? */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200305static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd,
306 unsigned offset, unsigned len)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400307{
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200308 struct fwnet_fragment_info *fi;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400309 unsigned end = offset + len;
310
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200311 list_for_each_entry(fi, &pd->fi_list, fi_link)
312 if (offset < fi->offset + fi->len && end > fi->offset)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400313 return true;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200314
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400315 return false;
316}
317
318/* Assumes that new fragment does not overlap any existing fragments */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200319static struct fwnet_fragment_info *fwnet_frag_new(
320 struct fwnet_partial_datagram *pd, unsigned offset, unsigned len)
321{
322 struct fwnet_fragment_info *fi, *fi2, *new;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400323 struct list_head *list;
324
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200325 list = &pd->fi_list;
326 list_for_each_entry(fi, &pd->fi_list, fi_link) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400327 if (fi->offset + fi->len == offset) {
328 /* The new fragment can be tacked on to the end */
329 /* Did the new fragment plug a hole? */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200330 fi2 = list_entry(fi->fi_link.next,
331 struct fwnet_fragment_info, fi_link);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400332 if (fi->offset + fi->len == fi2->offset) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400333 /* glue fragments together */
334 fi->len += len + fi2->len;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200335 list_del(&fi2->fi_link);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400336 kfree(fi2);
337 } else {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400338 fi->len += len;
339 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200340
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400341 return fi;
342 }
343 if (offset + len == fi->offset) {
344 /* The new fragment can be tacked on to the beginning */
345 /* Did the new fragment plug a hole? */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200346 fi2 = list_entry(fi->fi_link.prev,
347 struct fwnet_fragment_info, fi_link);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400348 if (fi2->offset + fi2->len == fi->offset) {
349 /* glue fragments together */
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400350 fi2->len += fi->len + len;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200351 list_del(&fi->fi_link);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400352 kfree(fi);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200353
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400354 return fi2;
355 }
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400356 fi->offset = offset;
357 fi->len += len;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200358
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400359 return fi;
360 }
361 if (offset > fi->offset + fi->len) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200362 list = &fi->fi_link;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400363 break;
364 }
365 if (offset + len < fi->offset) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200366 list = fi->fi_link.prev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400367 break;
368 }
369 }
370
371 new = kmalloc(sizeof(*new), GFP_ATOMIC);
372 if (!new) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200373 fw_error("out of memory\n");
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400374 return NULL;
375 }
376
377 new->offset = offset;
378 new->len = len;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200379 list_add(&new->fi_link, list);
380
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400381 return new;
382}
383
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200384static struct fwnet_partial_datagram *fwnet_pd_new(struct net_device *net,
385 struct fwnet_peer *peer, u16 datagram_label, unsigned dg_size,
386 void *frag_buf, unsigned frag_off, unsigned frag_len)
387{
388 struct fwnet_partial_datagram *new;
389 struct fwnet_fragment_info *fi;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400390
391 new = kmalloc(sizeof(*new), GFP_ATOMIC);
392 if (!new)
393 goto fail;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200394
395 INIT_LIST_HEAD(&new->fi_list);
396 fi = fwnet_frag_new(new, frag_off, frag_len);
397 if (fi == NULL)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400398 goto fail_w_new;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200399
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400400 new->datagram_label = datagram_label;
401 new->datagram_size = dg_size;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200402 new->skb = dev_alloc_skb(dg_size + net->hard_header_len + 15);
403 if (new->skb == NULL)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400404 goto fail_w_fi;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200405
406 skb_reserve(new->skb, (net->hard_header_len + 15) & ~15);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400407 new->pbuf = skb_put(new->skb, dg_size);
408 memcpy(new->pbuf + frag_off, frag_buf, frag_len);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200409 list_add_tail(&new->pd_link, &peer->pd_list);
410
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400411 return new;
412
413fail_w_fi:
414 kfree(fi);
415fail_w_new:
416 kfree(new);
417fail:
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200418 fw_error("out of memory\n");
419
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400420 return NULL;
421}
422
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200423static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer,
424 u16 datagram_label)
425{
426 struct fwnet_partial_datagram *pd;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400427
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200428 list_for_each_entry(pd, &peer->pd_list, pd_link)
429 if (pd->datagram_label == datagram_label)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400430 return pd;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200431
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400432 return NULL;
433}
434
435
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200436static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
437{
438 struct fwnet_fragment_info *fi, *n;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400439
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200440 list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400441 kfree(fi);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200442
443 list_del(&old->pd_link);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400444 dev_kfree_skb_any(old->skb);
445 kfree(old);
446}
447
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200448static bool fwnet_pd_update(struct fwnet_peer *peer,
449 struct fwnet_partial_datagram *pd, void *frag_buf,
450 unsigned frag_off, unsigned frag_len)
451{
452 if (fwnet_frag_new(pd, frag_off, frag_len) == NULL)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400453 return false;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200454
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400455 memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
456
457 /*
458 * Move list entry to beginnig of list so that oldest partial
459 * datagrams percolate to the end of the list
460 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200461 list_move_tail(&pd->pd_link, &peer->pd_list);
462
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400463 return true;
464}
465
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200466static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
467{
468 struct fwnet_fragment_info *fi;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400469
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200470 fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400471
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200472 return fi->len == pd->datagram_size;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400473}
474
Stefan Richter5a124d32009-06-14 11:45:27 +0200475/* caller must hold dev->lock */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200476static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
477 u64 guid)
478{
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200479 struct fwnet_peer *peer;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400480
Stefan Richter5a124d32009-06-14 11:45:27 +0200481 list_for_each_entry(peer, &dev->peer_list, peer_link)
482 if (peer->guid == guid)
483 return peer;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200484
Stefan Richter5a124d32009-06-14 11:45:27 +0200485 return NULL;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400486}
487
Stefan Richter5a124d32009-06-14 11:45:27 +0200488/* caller must hold dev->lock */
489static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
490 int node_id, int generation)
491{
492 struct fwnet_peer *peer;
493
494 list_for_each_entry(peer, &dev->peer_list, peer_link)
495 if (peer->node_id == node_id &&
496 peer->generation == generation)
497 return peer;
498
499 return NULL;
500}
501
502/* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */
503static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed)
504{
505 max_rec = min(max_rec, speed + 8);
506 max_rec = min(max_rec, 0xbU); /* <= 4096 */
507 if (max_rec < 8) {
508 fw_notify("max_rec %x out of range\n", max_rec);
509 max_rec = 8;
510 }
511
512 return (1 << (max_rec + 1)) - RFC2374_FRAG_HDR_SIZE;
513}
514
515
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200516static int fwnet_finish_incoming_packet(struct net_device *net,
517 struct sk_buff *skb, u16 source_node_id,
518 bool is_broadcast, u16 ether_type)
519{
520 struct fwnet_device *dev;
521 static const __be64 broadcast_hw = cpu_to_be64(~0ULL);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400522 int status;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200523 __be64 guid;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400524
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200525 dev = netdev_priv(net);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400526 /* Write metadata, and then pass to the receive level */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200527 skb->dev = net;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400528 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
529
530 /*
531 * Parse the encapsulation header. This actually does the job of
532 * converting to an ethernet frame header, as well as arp
533 * conversion if needed. ARP conversion is easier in this
534 * direction, since we are using ethernet as our backend.
535 */
536 /*
537 * If this is an ARP packet, convert it. First, we want to make
538 * use of some of the fields, since they tell us a little bit
539 * about the sending machine.
540 */
541 if (ether_type == ETH_P_ARP) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200542 struct rfc2734_arp *arp1394;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400543 struct arphdr *arp;
544 unsigned char *arp_ptr;
545 u64 fifo_addr;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200546 u64 peer_guid;
Stefan Richter5a124d32009-06-14 11:45:27 +0200547 unsigned sspd;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400548 u16 max_payload;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200549 struct fwnet_peer *peer;
Stefan Richter5a124d32009-06-14 11:45:27 +0200550 unsigned long flags;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400551
Stefan Richter5a124d32009-06-14 11:45:27 +0200552 arp1394 = (struct rfc2734_arp *)skb->data;
553 arp = (struct arphdr *)skb->data;
554 arp_ptr = (unsigned char *)(arp + 1);
555 peer_guid = get_unaligned_be64(&arp1394->s_uniq_id);
556 fifo_addr = (u64)get_unaligned_be16(&arp1394->fifo_hi) << 32
557 | get_unaligned_be32(&arp1394->fifo_lo);
558
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400559 sspd = arp1394->sspd;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200560 /* Sanity check. OS X 10.3 PPC reportedly sends 131. */
561 if (sspd > SCODE_3200) {
562 fw_notify("sspd %x out of range\n", sspd);
Stefan Richter5a124d32009-06-14 11:45:27 +0200563 sspd = SCODE_3200;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400564 }
Stefan Richter5a124d32009-06-14 11:45:27 +0200565 max_payload = fwnet_max_payload(arp1394->max_rec, sspd);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400566
Stefan Richter5a124d32009-06-14 11:45:27 +0200567 spin_lock_irqsave(&dev->lock, flags);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200568 peer = fwnet_peer_find_by_guid(dev, peer_guid);
Stefan Richter5a124d32009-06-14 11:45:27 +0200569 if (peer) {
570 peer->fifo = fifo_addr;
571
572 if (peer->speed > sspd)
573 peer->speed = sspd;
574 if (peer->max_payload > max_payload)
575 peer->max_payload = max_payload;
576 }
577 spin_unlock_irqrestore(&dev->lock, flags);
578
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200579 if (!peer) {
580 fw_notify("No peer for ARP packet from %016llx\n",
581 (unsigned long long)peer_guid);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400582 goto failed_proto;
583 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200584
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400585 /*
586 * Now that we're done with the 1394 specific stuff, we'll
587 * need to alter some of the data. Believe it or not, all
588 * that needs to be done is sender_IP_address needs to be
589 * moved, the destination hardware address get stuffed
590 * in and the hardware address length set to 8.
591 *
592 * IMPORTANT: The code below overwrites 1394 specific data
593 * needed above so keep the munging of the data for the
594 * higher level IP stack last.
595 */
596
597 arp->ar_hln = 8;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200598 /* skip over sender unique id */
599 arp_ptr += arp->ar_hln;
600 /* move sender IP addr */
601 put_unaligned(arp1394->sip, (u32 *)arp_ptr);
602 /* skip over sender IP addr */
603 arp_ptr += arp->ar_pln;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400604
605 if (arp->ar_op == htons(ARPOP_REQUEST))
606 memset(arp_ptr, 0, sizeof(u64));
607 else
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200608 memcpy(arp_ptr, net->dev_addr, sizeof(u64));
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400609 }
610
611 /* Now add the ethernet header. */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200612 guid = cpu_to_be64(dev->card->guid);
613 if (dev_hard_header(skb, net, ether_type,
614 is_broadcast ? &broadcast_hw : &guid,
615 NULL, skb->len) >= 0) {
616 struct fwnet_header *eth;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400617 u16 *rawp;
618 __be16 protocol;
619
620 skb_reset_mac_header(skb);
621 skb_pull(skb, sizeof(*eth));
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200622 eth = (struct fwnet_header *)skb_mac_header(skb);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400623 if (*eth->h_dest & 1) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200624 if (memcmp(eth->h_dest, net->broadcast,
625 net->addr_len) == 0)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400626 skb->pkt_type = PACKET_BROADCAST;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400627#if 0
628 else
629 skb->pkt_type = PACKET_MULTICAST;
630#endif
631 } else {
Stefan Richter156ce862009-06-14 11:46:57 +0200632 if (memcmp(eth->h_dest, net->dev_addr, net->addr_len))
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400633 skb->pkt_type = PACKET_OTHERHOST;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400634 }
635 if (ntohs(eth->h_proto) >= 1536) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400636 protocol = eth->h_proto;
637 } else {
638 rawp = (u16 *)skb->data;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200639 if (*rawp == 0xffff)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400640 protocol = htons(ETH_P_802_3);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200641 else
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400642 protocol = htons(ETH_P_802_2);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400643 }
644 skb->protocol = protocol;
645 }
646 status = netif_rx(skb);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200647 if (status == NET_RX_DROP) {
648 net->stats.rx_errors++;
649 net->stats.rx_dropped++;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400650 } else {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200651 net->stats.rx_packets++;
652 net->stats.rx_bytes += skb->len;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400653 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200654 if (netif_queue_stopped(net))
655 netif_wake_queue(net);
656
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400657 return 0;
658
659 failed_proto:
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200660 net->stats.rx_errors++;
661 net->stats.rx_dropped++;
662
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400663 dev_kfree_skb_any(skb);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200664 if (netif_queue_stopped(net))
665 netif_wake_queue(net);
666
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400667 return 0;
668}
669
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200670static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
Stefan Richter5a124d32009-06-14 11:45:27 +0200671 int source_node_id, int generation,
672 bool is_broadcast)
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200673{
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400674 struct sk_buff *skb;
Stefan Richter5a124d32009-06-14 11:45:27 +0200675 struct net_device *net = dev->netdev;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200676 struct rfc2734_header hdr;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400677 unsigned lf;
678 unsigned long flags;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200679 struct fwnet_peer *peer;
680 struct fwnet_partial_datagram *pd;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400681 int fg_off;
682 int dg_size;
683 u16 datagram_label;
684 int retval;
685 u16 ether_type;
686
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200687 hdr.w0 = be32_to_cpu(buf[0]);
688 lf = fwnet_get_hdr_lf(&hdr);
689 if (lf == RFC2374_HDR_UNFRAG) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400690 /*
691 * An unfragmented datagram has been received by the ieee1394
692 * bus. Build an skbuff around it so we can pass it to the
693 * high level network layer.
694 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200695 ether_type = fwnet_get_hdr_ether_type(&hdr);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400696 buf++;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200697 len -= RFC2374_UNFRAG_HDR_SIZE;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400698
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200699 skb = dev_alloc_skb(len + net->hard_header_len + 15);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400700 if (unlikely(!skb)) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200701 fw_error("out of memory\n");
702 net->stats.rx_dropped++;
703
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400704 return -1;
705 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200706 skb_reserve(skb, (net->hard_header_len + 15) & ~15);
707 memcpy(skb_put(skb, len), buf, len);
708
709 return fwnet_finish_incoming_packet(net, skb, source_node_id,
710 is_broadcast, ether_type);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400711 }
712 /* A datagram fragment has been received, now the fun begins. */
713 hdr.w1 = ntohl(buf[1]);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200714 buf += 2;
715 len -= RFC2374_FRAG_HDR_SIZE;
716 if (lf == RFC2374_HDR_FIRSTFRAG) {
717 ether_type = fwnet_get_hdr_ether_type(&hdr);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400718 fg_off = 0;
719 } else {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200720 ether_type = 0;
721 fg_off = fwnet_get_hdr_fg_off(&hdr);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400722 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200723 datagram_label = fwnet_get_hdr_dgl(&hdr);
724 dg_size = fwnet_get_hdr_dg_size(&hdr); /* ??? + 1 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200725
Stefan Richter5a124d32009-06-14 11:45:27 +0200726 spin_lock_irqsave(&dev->lock, flags);
727
728 peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation);
729 if (!peer)
730 goto bad_proto;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200731
732 pd = fwnet_pd_find(peer, datagram_label);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400733 if (pd == NULL) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200734 while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400735 /* remove the oldest */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200736 fwnet_pd_delete(list_first_entry(&peer->pd_list,
737 struct fwnet_partial_datagram, pd_link));
738 peer->pdg_size--;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400739 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200740 pd = fwnet_pd_new(net, peer, datagram_label,
741 dg_size, buf, fg_off, len);
742 if (pd == NULL) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400743 retval = -ENOMEM;
744 goto bad_proto;
745 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200746 peer->pdg_size++;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400747 } else {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200748 if (fwnet_frag_overlap(pd, fg_off, len) ||
749 pd->datagram_size != dg_size) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400750 /*
751 * Differing datagram sizes or overlapping fragments,
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200752 * discard old datagram and start a new one.
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400753 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200754 fwnet_pd_delete(pd);
755 pd = fwnet_pd_new(net, peer, datagram_label,
756 dg_size, buf, fg_off, len);
757 if (pd == NULL) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400758 retval = -ENOMEM;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200759 peer->pdg_size--;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400760 goto bad_proto;
761 }
762 } else {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200763 if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400764 /*
765 * Couldn't save off fragment anyway
766 * so might as well obliterate the
767 * datagram now.
768 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200769 fwnet_pd_delete(pd);
770 peer->pdg_size--;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400771 goto bad_proto;
772 }
773 }
774 } /* new datagram or add to existing one */
775
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200776 if (lf == RFC2374_HDR_FIRSTFRAG)
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400777 pd->ether_type = ether_type;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200778
779 if (fwnet_pd_is_complete(pd)) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400780 ether_type = pd->ether_type;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200781 peer->pdg_size--;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400782 skb = skb_get(pd->skb);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200783 fwnet_pd_delete(pd);
784
Stefan Richter5a124d32009-06-14 11:45:27 +0200785 spin_unlock_irqrestore(&dev->lock, flags);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200786
787 return fwnet_finish_incoming_packet(net, skb, source_node_id,
788 false, ether_type);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400789 }
790 /*
791 * Datagram is not complete, we're done for the
792 * moment.
793 */
Stefan Richter5a124d32009-06-14 11:45:27 +0200794 spin_unlock_irqrestore(&dev->lock, flags);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200795
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400796 return 0;
797
798 bad_proto:
Stefan Richter5a124d32009-06-14 11:45:27 +0200799 spin_unlock_irqrestore(&dev->lock, flags);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200800
801 if (netif_queue_stopped(net))
802 netif_wake_queue(net);
803
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400804 return 0;
805}
806
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200807static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r,
808 int tcode, int destination, int source, int generation,
809 int speed, unsigned long long offset, void *payload,
810 size_t length, void *callback_data)
811{
Stefan Richter00635b82009-06-16 22:35:32 +0200812 struct fwnet_device *dev = callback_data;
813 int rcode;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400814
Stefan Richter00635b82009-06-16 22:35:32 +0200815 if (destination == IEEE1394_ALL_NODES) {
816 kfree(r);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200817
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400818 return;
819 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200820
Stefan Richter00635b82009-06-16 22:35:32 +0200821 if (offset != dev->handler.offset)
822 rcode = RCODE_ADDRESS_ERROR;
823 else if (tcode != TCODE_WRITE_BLOCK_REQUEST)
824 rcode = RCODE_TYPE_ERROR;
825 else if (fwnet_incoming_packet(dev, payload, length,
826 source, generation, false) != 0) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200827 fw_error("Incoming packet failure\n");
Stefan Richter00635b82009-06-16 22:35:32 +0200828 rcode = RCODE_CONFLICT_ERROR;
829 } else
830 rcode = RCODE_COMPLETE;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200831
Stefan Richter00635b82009-06-16 22:35:32 +0200832 fw_send_response(card, r, rcode);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400833}
834
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200835static void fwnet_receive_broadcast(struct fw_iso_context *context,
836 u32 cycle, size_t header_length, void *header, void *data)
837{
838 struct fwnet_device *dev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400839 struct fw_iso_packet packet;
840 struct fw_card *card;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200841 __be16 *hdr_ptr;
842 __be32 *buf_ptr;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400843 int retval;
844 u32 length;
845 u16 source_node_id;
846 u32 specifier_id;
847 u32 ver;
848 unsigned long offset;
849 unsigned long flags;
850
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200851 dev = data;
852 card = dev->card;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400853 hdr_ptr = header;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200854 length = be16_to_cpup(hdr_ptr);
855
856 spin_lock_irqsave(&dev->lock, flags);
857
858 offset = dev->rcv_buffer_size * dev->broadcast_rcv_next_ptr;
859 buf_ptr = dev->broadcast_rcv_buffer_ptrs[dev->broadcast_rcv_next_ptr++];
860 if (dev->broadcast_rcv_next_ptr == dev->num_broadcast_rcv_ptrs)
861 dev->broadcast_rcv_next_ptr = 0;
862
863 spin_unlock_irqrestore(&dev->lock, flags);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400864
865 specifier_id = (be32_to_cpu(buf_ptr[0]) & 0xffff) << 8
866 | (be32_to_cpu(buf_ptr[1]) & 0xff000000) >> 24;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200867 ver = be32_to_cpu(buf_ptr[1]) & 0xffffff;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400868 source_node_id = be32_to_cpu(buf_ptr[0]) >> 16;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200869
870 if (specifier_id == IANA_SPECIFIER_ID && ver == RFC2734_SW_VERSION) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400871 buf_ptr += 2;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200872 length -= IEEE1394_GASP_HDR_SIZE;
873 fwnet_incoming_packet(dev, buf_ptr, length,
Stefan Richter5a124d32009-06-14 11:45:27 +0200874 source_node_id, -1, true);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200875 }
876
877 packet.payload_length = dev->rcv_buffer_size;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400878 packet.interrupt = 1;
879 packet.skip = 0;
880 packet.tag = 3;
881 packet.sy = 0;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200882 packet.header_length = IEEE1394_GASP_HDR_SIZE;
883
884 spin_lock_irqsave(&dev->lock, flags);
885
886 retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet,
887 &dev->broadcast_rcv_buffer, offset);
888
889 spin_unlock_irqrestore(&dev->lock, flags);
890
891 if (retval < 0)
892 fw_error("requeue failed\n");
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400893}
894
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200895static struct kmem_cache *fwnet_packet_task_cache;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400896
Stefan Richter110f82d2010-01-18 22:36:49 +0100897static void fwnet_free_ptask(struct fwnet_packet_task *ptask)
898{
899 dev_kfree_skb_any(ptask->skb);
900 kmem_cache_free(fwnet_packet_task_cache, ptask);
901}
902
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200903static int fwnet_send_packet(struct fwnet_packet_task *ptask);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400904
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200905static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask)
906{
Stefan Richter110f82d2010-01-18 22:36:49 +0100907 struct fwnet_device *dev = ptask->dev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400908 unsigned long flags;
Stefan Richter110f82d2010-01-18 22:36:49 +0100909 bool free;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200910
911 spin_lock_irqsave(&dev->lock, flags);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200912
Stefan Richter110f82d2010-01-18 22:36:49 +0100913 ptask->outstanding_pkts--;
914
915 /* Check whether we or the networking TX soft-IRQ is last user. */
916 free = (ptask->outstanding_pkts == 0 && !list_empty(&ptask->pt_link));
917
918 if (ptask->outstanding_pkts == 0)
919 list_del(&ptask->pt_link);
920
921 spin_unlock_irqrestore(&dev->lock, flags);
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200922
923 if (ptask->outstanding_pkts > 0) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400924 u16 dg_size;
925 u16 fg_off;
926 u16 datagram_label;
927 u16 lf;
928 struct sk_buff *skb;
929
930 /* Update the ptask to point to the next fragment and send it */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200931 lf = fwnet_get_hdr_lf(&ptask->hdr);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400932 switch (lf) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200933 case RFC2374_HDR_LASTFRAG:
934 case RFC2374_HDR_UNFRAG:
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400935 default:
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200936 fw_error("Outstanding packet %x lf %x, header %x,%x\n",
937 ptask->outstanding_pkts, lf, ptask->hdr.w0,
938 ptask->hdr.w1);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400939 BUG();
940
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200941 case RFC2374_HDR_FIRSTFRAG:
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400942 /* Set frag type here for future interior fragments */
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200943 dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
944 fg_off = ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
945 datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400946 break;
947
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200948 case RFC2374_HDR_INTFRAG:
949 dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
950 fg_off = fwnet_get_hdr_fg_off(&ptask->hdr)
951 + ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
952 datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400953 break;
954 }
955 skb = ptask->skb;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200956 skb_pull(skb, ptask->max_payload);
957 if (ptask->outstanding_pkts > 1) {
958 fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG,
959 dg_size, fg_off, datagram_label);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400960 } else {
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200961 fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG,
962 dg_size, fg_off, datagram_label);
963 ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400964 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200965 fwnet_send_packet(ptask);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400966 }
Stefan Richter110f82d2010-01-18 22:36:49 +0100967
968 if (free)
969 fwnet_free_ptask(ptask);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400970}
971
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200972static void fwnet_write_complete(struct fw_card *card, int rcode,
973 void *payload, size_t length, void *data)
974{
975 struct fwnet_packet_task *ptask;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400976
977 ptask = data;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400978
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200979 if (rcode == RCODE_COMPLETE)
980 fwnet_transmit_packet_done(ptask);
981 else
982 fw_error("fwnet_write_complete: failed: %x\n", rcode);
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400983 /* ??? error recovery */
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400984}
985
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200986static int fwnet_send_packet(struct fwnet_packet_task *ptask)
987{
988 struct fwnet_device *dev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400989 unsigned tx_len;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200990 struct rfc2734_header *bufhdr;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400991 unsigned long flags;
Stefan Richter110f82d2010-01-18 22:36:49 +0100992 bool free;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400993
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200994 dev = ptask->dev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -0400995 tx_len = ptask->max_payload;
Stefan Richterf91e3bd2009-06-07 22:57:53 +0200996 switch (fwnet_get_hdr_lf(&ptask->hdr)) {
997 case RFC2374_HDR_UNFRAG:
998 bufhdr = (struct rfc2734_header *)
999 skb_push(ptask->skb, RFC2374_UNFRAG_HDR_SIZE);
1000 put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001001 break;
1002
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001003 case RFC2374_HDR_FIRSTFRAG:
1004 case RFC2374_HDR_INTFRAG:
1005 case RFC2374_HDR_LASTFRAG:
1006 bufhdr = (struct rfc2734_header *)
1007 skb_push(ptask->skb, RFC2374_FRAG_HDR_SIZE);
1008 put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
1009 put_unaligned_be32(ptask->hdr.w1, &bufhdr->w1);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001010 break;
1011
1012 default:
1013 BUG();
1014 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001015 if (ptask->dest_node == IEEE1394_ALL_NODES) {
1016 u8 *p;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001017 int generation;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001018 int node_id;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001019
1020 /* ptask->generation may not have been set yet */
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001021 generation = dev->card->generation;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001022 smp_rmb();
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001023 node_id = dev->card->node_id;
1024
1025 p = skb_push(ptask->skb, 8);
1026 put_unaligned_be32(node_id << 16 | IANA_SPECIFIER_ID >> 8, p);
1027 put_unaligned_be32((IANA_SPECIFIER_ID & 0xff) << 24
1028 | RFC2734_SW_VERSION, &p[4]);
1029
1030 /* We should not transmit if broadcast_channel.valid == 0. */
1031 fw_send_request(dev->card, &ptask->transaction,
1032 TCODE_STREAM_DATA,
1033 fw_stream_packet_destination_id(3,
1034 IEEE1394_BROADCAST_CHANNEL, 0),
1035 generation, SCODE_100, 0ULL, ptask->skb->data,
1036 tx_len + 8, fwnet_write_complete, ptask);
1037
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001038 spin_lock_irqsave(&dev->lock, flags);
Stefan Richter110f82d2010-01-18 22:36:49 +01001039
1040 /* If the AT tasklet already ran, we may be last user. */
1041 free = (ptask->outstanding_pkts == 0 && list_empty(&ptask->pt_link));
1042 if (!free)
1043 list_add_tail(&ptask->pt_link, &dev->broadcasted_list);
1044
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001045 spin_unlock_irqrestore(&dev->lock, flags);
1046
Stefan Richter110f82d2010-01-18 22:36:49 +01001047 goto out;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001048 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001049
1050 fw_send_request(dev->card, &ptask->transaction,
1051 TCODE_WRITE_BLOCK_REQUEST, ptask->dest_node,
1052 ptask->generation, ptask->speed, ptask->fifo_addr,
1053 ptask->skb->data, tx_len, fwnet_write_complete, ptask);
1054
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001055 spin_lock_irqsave(&dev->lock, flags);
Stefan Richter110f82d2010-01-18 22:36:49 +01001056
1057 /* If the AT tasklet already ran, we may be last user. */
1058 free = (ptask->outstanding_pkts == 0 && list_empty(&ptask->pt_link));
1059 if (!free)
1060 list_add_tail(&ptask->pt_link, &dev->sent_list);
1061
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001062 spin_unlock_irqrestore(&dev->lock, flags);
1063
Stefan Richter5a124d32009-06-14 11:45:27 +02001064 dev->netdev->trans_start = jiffies;
Stefan Richter110f82d2010-01-18 22:36:49 +01001065 out:
1066 if (free)
1067 fwnet_free_ptask(ptask);
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001068
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001069 return 0;
1070}
1071
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001072static int fwnet_broadcast_start(struct fwnet_device *dev)
1073{
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001074 struct fw_iso_context *context;
1075 int retval;
1076 unsigned num_packets;
1077 unsigned max_receive;
1078 struct fw_iso_packet packet;
1079 unsigned long offset;
1080 unsigned u;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001081
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001082 if (dev->local_fifo == FWNET_NO_FIFO_ADDR) {
1083 /* outside OHCI posted write area? */
1084 static const struct fw_address_region region = {
1085 .start = 0xffff00000000ULL,
1086 .end = CSR_REGISTER_BASE,
1087 };
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001088
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001089 dev->handler.length = 4096;
1090 dev->handler.address_callback = fwnet_receive_packet;
1091 dev->handler.callback_data = dev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001092
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001093 retval = fw_core_add_address_handler(&dev->handler, &region);
1094 if (retval < 0)
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001095 goto failed_initial;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001096
1097 dev->local_fifo = dev->handler.offset;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001098 }
1099
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001100 max_receive = 1U << (dev->card->max_receive + 1);
1101 num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive;
1102
1103 if (!dev->broadcast_rcv_context) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001104 void **ptrptr;
1105
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001106 context = fw_iso_context_create(dev->card,
1107 FW_ISO_CONTEXT_RECEIVE, IEEE1394_BROADCAST_CHANNEL,
1108 dev->card->link_speed, 8, fwnet_receive_broadcast, dev);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001109 if (IS_ERR(context)) {
1110 retval = PTR_ERR(context);
1111 goto failed_context_create;
1112 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001113
1114 retval = fw_iso_buffer_init(&dev->broadcast_rcv_buffer,
1115 dev->card, FWNET_ISO_PAGE_COUNT, DMA_FROM_DEVICE);
1116 if (retval < 0)
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001117 goto failed_buffer_init;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001118
1119 ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL);
1120 if (!ptrptr) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001121 retval = -ENOMEM;
1122 goto failed_ptrs_alloc;
1123 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001124
1125 dev->broadcast_rcv_buffer_ptrs = ptrptr;
1126 for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001127 void *ptr;
1128 unsigned v;
1129
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001130 ptr = kmap(dev->broadcast_rcv_buffer.pages[u]);
1131 for (v = 0; v < num_packets / FWNET_ISO_PAGE_COUNT; v++)
1132 *ptrptr++ = (void *)
1133 ((char *)ptr + v * max_receive);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001134 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001135 dev->broadcast_rcv_context = context;
1136 } else {
1137 context = dev->broadcast_rcv_context;
1138 }
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001139
1140 packet.payload_length = max_receive;
1141 packet.interrupt = 1;
1142 packet.skip = 0;
1143 packet.tag = 3;
1144 packet.sy = 0;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001145 packet.header_length = IEEE1394_GASP_HDR_SIZE;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001146 offset = 0;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001147
1148 for (u = 0; u < num_packets; u++) {
1149 retval = fw_iso_context_queue(context, &packet,
1150 &dev->broadcast_rcv_buffer, offset);
1151 if (retval < 0)
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001152 goto failed_rcv_queue;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001153
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001154 offset += max_receive;
1155 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001156 dev->num_broadcast_rcv_ptrs = num_packets;
1157 dev->rcv_buffer_size = max_receive;
1158 dev->broadcast_rcv_next_ptr = 0U;
1159 retval = fw_iso_context_start(context, -1, 0,
1160 FW_ISO_CONTEXT_MATCH_ALL_TAGS); /* ??? sync */
1161 if (retval < 0)
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001162 goto failed_rcv_queue;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001163
1164 /* FIXME: adjust it according to the min. speed of all known peers? */
1165 dev->broadcast_xmt_max_payload = IEEE1394_MAX_PAYLOAD_S100
1166 - IEEE1394_GASP_HDR_SIZE - RFC2374_UNFRAG_HDR_SIZE;
1167 dev->broadcast_state = FWNET_BROADCAST_RUNNING;
1168
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001169 return 0;
1170
1171 failed_rcv_queue:
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001172 kfree(dev->broadcast_rcv_buffer_ptrs);
1173 dev->broadcast_rcv_buffer_ptrs = NULL;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001174 failed_ptrs_alloc:
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001175 fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, dev->card);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001176 failed_buffer_init:
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001177 fw_iso_context_destroy(context);
1178 dev->broadcast_rcv_context = NULL;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001179 failed_context_create:
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001180 fw_core_remove_address_handler(&dev->handler);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001181 failed_initial:
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001182 dev->local_fifo = FWNET_NO_FIFO_ADDR;
1183
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001184 return retval;
1185}
1186
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001187/* ifup */
1188static int fwnet_open(struct net_device *net)
1189{
1190 struct fwnet_device *dev = netdev_priv(net);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001191 int ret;
1192
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001193 if (dev->broadcast_state == FWNET_BROADCAST_ERROR) {
1194 ret = fwnet_broadcast_start(dev);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001195 if (ret)
1196 return ret;
1197 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001198 netif_start_queue(net);
1199
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001200 return 0;
1201}
1202
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001203/* ifdown */
1204static int fwnet_stop(struct net_device *net)
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001205{
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001206 netif_stop_queue(net);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001207
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001208 /* Deallocate iso context for use by other applications? */
1209
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001210 return 0;
1211}
1212
Stephen Hemminger424efe92009-08-31 19:50:51 +00001213static netdev_tx_t fwnet_tx(struct sk_buff *skb, struct net_device *net)
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001214{
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001215 struct fwnet_header hdr_buf;
1216 struct fwnet_device *dev = netdev_priv(net);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001217 __be16 proto;
1218 u16 dest_node;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001219 unsigned max_payload;
1220 u16 dg_size;
1221 u16 *datagram_label_ptr;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001222 struct fwnet_packet_task *ptask;
Stefan Richter5a124d32009-06-14 11:45:27 +02001223 struct fwnet_peer *peer;
1224 unsigned long flags;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001225
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001226 ptask = kmem_cache_alloc(fwnet_packet_task_cache, GFP_ATOMIC);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001227 if (ptask == NULL)
1228 goto fail;
1229
1230 skb = skb_share_check(skb, GFP_ATOMIC);
1231 if (!skb)
1232 goto fail;
1233
1234 /*
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001235 * Make a copy of the driver-specific header.
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001236 * We might need to rebuild the header on tx failure.
1237 */
1238 memcpy(&hdr_buf, skb->data, sizeof(hdr_buf));
1239 skb_pull(skb, sizeof(hdr_buf));
1240
1241 proto = hdr_buf.h_proto;
1242 dg_size = skb->len;
1243
Stefan Richter5a124d32009-06-14 11:45:27 +02001244 /* serialize access to peer, including peer->datagram_label */
1245 spin_lock_irqsave(&dev->lock, flags);
1246
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001247 /*
1248 * Set the transmission type for the packet. ARP packets and IP
1249 * broadcast packets are sent via GASP.
1250 */
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001251 if (memcmp(hdr_buf.h_dest, net->broadcast, FWNET_ALEN) == 0
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001252 || proto == htons(ETH_P_ARP)
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001253 || (proto == htons(ETH_P_IP)
1254 && IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
Stefan Richter5a124d32009-06-14 11:45:27 +02001255 max_payload = dev->broadcast_xmt_max_payload;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001256 datagram_label_ptr = &dev->broadcast_xmt_datagramlabel;
1257
Stefan Richter5a124d32009-06-14 11:45:27 +02001258 ptask->fifo_addr = FWNET_NO_FIFO_ADDR;
1259 ptask->generation = 0;
1260 ptask->dest_node = IEEE1394_ALL_NODES;
1261 ptask->speed = SCODE_100;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001262 } else {
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001263 __be64 guid = get_unaligned((__be64 *)hdr_buf.h_dest);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001264 u8 generation;
1265
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001266 peer = fwnet_peer_find_by_guid(dev, be64_to_cpu(guid));
Stefan Richter5a124d32009-06-14 11:45:27 +02001267 if (!peer || peer->fifo == FWNET_NO_FIFO_ADDR)
1268 goto fail_unlock;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001269
Stefan Richter5a124d32009-06-14 11:45:27 +02001270 generation = peer->generation;
1271 dest_node = peer->node_id;
1272 max_payload = peer->max_payload;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001273 datagram_label_ptr = &peer->datagram_label;
1274
Stefan Richter5a124d32009-06-14 11:45:27 +02001275 ptask->fifo_addr = peer->fifo;
1276 ptask->generation = generation;
1277 ptask->dest_node = dest_node;
1278 ptask->speed = peer->speed;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001279 }
1280
1281 /* If this is an ARP packet, convert it */
1282 if (proto == htons(ETH_P_ARP)) {
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001283 struct arphdr *arp = (struct arphdr *)skb->data;
1284 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001285 struct rfc2734_arp *arp1394 = (struct rfc2734_arp *)skb->data;
1286 __be32 ipaddr;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001287
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001288 ipaddr = get_unaligned((__be32 *)(arp_ptr + FWNET_ALEN));
1289
1290 arp1394->hw_addr_len = RFC2734_HW_ADDR_LEN;
1291 arp1394->max_rec = dev->card->max_receive;
1292 arp1394->sspd = dev->card->link_speed;
1293
1294 put_unaligned_be16(dev->local_fifo >> 32,
1295 &arp1394->fifo_hi);
1296 put_unaligned_be32(dev->local_fifo & 0xffffffff,
1297 &arp1394->fifo_lo);
1298 put_unaligned(ipaddr, &arp1394->sip);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001299 }
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001300
1301 ptask->hdr.w0 = 0;
1302 ptask->hdr.w1 = 0;
1303 ptask->skb = skb;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001304 ptask->dev = dev;
1305
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001306 /* Does it all fit in one packet? */
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001307 if (dg_size <= max_payload) {
1308 fwnet_make_uf_hdr(&ptask->hdr, ntohs(proto));
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001309 ptask->outstanding_pkts = 1;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001310 max_payload = dg_size + RFC2374_UNFRAG_HDR_SIZE;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001311 } else {
1312 u16 datagram_label;
1313
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001314 max_payload -= RFC2374_FRAG_OVERHEAD;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001315 datagram_label = (*datagram_label_ptr)++;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001316 fwnet_make_ff_hdr(&ptask->hdr, ntohs(proto), dg_size,
1317 datagram_label);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001318 ptask->outstanding_pkts = DIV_ROUND_UP(dg_size, max_payload);
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001319 max_payload += RFC2374_FRAG_HDR_SIZE;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001320 }
Stefan Richter5a124d32009-06-14 11:45:27 +02001321
1322 spin_unlock_irqrestore(&dev->lock, flags);
1323
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001324 ptask->max_payload = max_payload;
Stefan Richter110f82d2010-01-18 22:36:49 +01001325 INIT_LIST_HEAD(&ptask->pt_link);
1326
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001327 fwnet_send_packet(ptask);
1328
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001329 return NETDEV_TX_OK;
1330
Stefan Richter5a124d32009-06-14 11:45:27 +02001331 fail_unlock:
1332 spin_unlock_irqrestore(&dev->lock, flags);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001333 fail:
1334 if (ptask)
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001335 kmem_cache_free(fwnet_packet_task_cache, ptask);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001336
1337 if (skb != NULL)
1338 dev_kfree_skb(skb);
1339
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001340 net->stats.tx_dropped++;
1341 net->stats.tx_errors++;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001342
1343 /*
1344 * FIXME: According to a patch from 2003-02-26, "returning non-zero
1345 * causes serious problems" here, allegedly. Before that patch,
1346 * -ERRNO was returned which is not appropriate under Linux 2.6.
1347 * Perhaps more needs to be done? Stop the queue in serious
1348 * conditions and restart it elsewhere?
1349 */
1350 return NETDEV_TX_OK;
1351}
1352
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001353static int fwnet_change_mtu(struct net_device *net, int new_mtu)
1354{
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001355 if (new_mtu < 68)
1356 return -EINVAL;
1357
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001358 net->mtu = new_mtu;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001359 return 0;
1360}
1361
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001362static void fwnet_get_drvinfo(struct net_device *net,
1363 struct ethtool_drvinfo *info)
1364{
1365 strcpy(info->driver, KBUILD_MODNAME);
1366 strcpy(info->bus_info, "ieee1394");
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001367}
1368
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001369static const struct ethtool_ops fwnet_ethtool_ops = {
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001370 .get_drvinfo = fwnet_get_drvinfo,
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001371};
1372
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001373static const struct net_device_ops fwnet_netdev_ops = {
1374 .ndo_open = fwnet_open,
1375 .ndo_stop = fwnet_stop,
1376 .ndo_start_xmit = fwnet_tx,
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001377 .ndo_change_mtu = fwnet_change_mtu,
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001378};
1379
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001380static void fwnet_init_dev(struct net_device *net)
1381{
1382 net->header_ops = &fwnet_header_ops;
1383 net->netdev_ops = &fwnet_netdev_ops;
Stefan Richter1337f852009-06-14 11:47:44 +02001384 net->watchdog_timeo = 2 * HZ;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001385 net->flags = IFF_BROADCAST | IFF_MULTICAST;
1386 net->features = NETIF_F_HIGHDMA;
1387 net->addr_len = FWNET_ALEN;
1388 net->hard_header_len = FWNET_HLEN;
1389 net->type = ARPHRD_IEEE1394;
Stefan Richter1337f852009-06-14 11:47:44 +02001390 net->tx_queue_len = 10;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001391 SET_ETHTOOL_OPS(net, &fwnet_ethtool_ops);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001392}
1393
Stefan Richter5a124d32009-06-14 11:45:27 +02001394/* caller must hold fwnet_device_mutex */
1395static struct fwnet_device *fwnet_dev_find(struct fw_card *card)
1396{
1397 struct fwnet_device *dev;
1398
1399 list_for_each_entry(dev, &fwnet_device_list, dev_link)
1400 if (dev->card == card)
1401 return dev;
1402
1403 return NULL;
1404}
1405
1406static int fwnet_add_peer(struct fwnet_device *dev,
1407 struct fw_unit *unit, struct fw_device *device)
1408{
1409 struct fwnet_peer *peer;
1410
1411 peer = kmalloc(sizeof(*peer), GFP_KERNEL);
1412 if (!peer)
1413 return -ENOMEM;
1414
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001415 dev_set_drvdata(&unit->device, peer);
1416
Stefan Richter5a124d32009-06-14 11:45:27 +02001417 peer->dev = dev;
1418 peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
1419 peer->fifo = FWNET_NO_FIFO_ADDR;
1420 INIT_LIST_HEAD(&peer->pd_list);
1421 peer->pdg_size = 0;
1422 peer->datagram_label = 0;
1423 peer->speed = device->max_speed;
1424 peer->max_payload = fwnet_max_payload(device->max_rec, peer->speed);
1425
1426 peer->generation = device->generation;
1427 smp_rmb();
1428 peer->node_id = device->node_id;
1429
1430 spin_lock_irq(&dev->lock);
1431 list_add_tail(&peer->peer_link, &dev->peer_list);
1432 spin_unlock_irq(&dev->lock);
1433
1434 return 0;
1435}
1436
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001437static int fwnet_probe(struct device *_dev)
1438{
1439 struct fw_unit *unit = fw_unit(_dev);
1440 struct fw_device *device = fw_parent_device(unit);
1441 struct fw_card *card = device->card;
1442 struct net_device *net;
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001443 bool allocated_netdev = false;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001444 struct fwnet_device *dev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001445 unsigned max_mtu;
Stefan Richter5a124d32009-06-14 11:45:27 +02001446 int ret;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001447
Stefan Richter5a124d32009-06-14 11:45:27 +02001448 mutex_lock(&fwnet_device_mutex);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001449
Stefan Richter5a124d32009-06-14 11:45:27 +02001450 dev = fwnet_dev_find(card);
1451 if (dev) {
Stefan Richter5a124d32009-06-14 11:45:27 +02001452 net = dev->netdev;
1453 goto have_dev;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001454 }
Stefan Richter5a124d32009-06-14 11:45:27 +02001455
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001456 net = alloc_netdev(sizeof(*dev), "firewire%d", fwnet_init_dev);
1457 if (net == NULL) {
Stefan Richter5a124d32009-06-14 11:45:27 +02001458 ret = -ENOMEM;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001459 goto out;
1460 }
1461
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001462 allocated_netdev = true;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001463 SET_NETDEV_DEV(net, card->device);
1464 dev = netdev_priv(net);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001465
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001466 spin_lock_init(&dev->lock);
1467 dev->broadcast_state = FWNET_BROADCAST_ERROR;
1468 dev->broadcast_rcv_context = NULL;
1469 dev->broadcast_xmt_max_payload = 0;
1470 dev->broadcast_xmt_datagramlabel = 0;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001471
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001472 dev->local_fifo = FWNET_NO_FIFO_ADDR;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001473
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001474 INIT_LIST_HEAD(&dev->packet_list);
1475 INIT_LIST_HEAD(&dev->broadcasted_list);
1476 INIT_LIST_HEAD(&dev->sent_list);
Stefan Richter5a124d32009-06-14 11:45:27 +02001477 INIT_LIST_HEAD(&dev->peer_list);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001478
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001479 dev->card = card;
Stefan Richter5a124d32009-06-14 11:45:27 +02001480 dev->netdev = net;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001481
1482 /*
1483 * Use the RFC 2734 default 1500 octets or the maximum payload
1484 * as initial MTU
1485 */
1486 max_mtu = (1 << (card->max_receive + 1))
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001487 - sizeof(struct rfc2734_header) - IEEE1394_GASP_HDR_SIZE;
1488 net->mtu = min(1500U, max_mtu);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001489
1490 /* Set our hardware address while we're at it */
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001491 put_unaligned_be64(card->guid, net->dev_addr);
1492 put_unaligned_be64(~0ULL, net->broadcast);
Stefan Richter5a124d32009-06-14 11:45:27 +02001493 ret = register_netdev(net);
1494 if (ret) {
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001495 fw_error("Cannot register the driver\n");
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001496 goto out;
1497 }
1498
Stefan Richter5a124d32009-06-14 11:45:27 +02001499 list_add_tail(&dev->dev_link, &fwnet_device_list);
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001500 fw_notify("%s: IPv4 over FireWire on device %016llx\n",
1501 net->name, (unsigned long long)card->guid);
Stefan Richter5a124d32009-06-14 11:45:27 +02001502 have_dev:
1503 ret = fwnet_add_peer(dev, unit, device);
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001504 if (ret && allocated_netdev) {
Stefan Richter5a124d32009-06-14 11:45:27 +02001505 unregister_netdev(net);
1506 list_del(&dev->dev_link);
1507 }
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001508 out:
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001509 if (ret && allocated_netdev)
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001510 free_netdev(net);
1511
Stefan Richter5a124d32009-06-14 11:45:27 +02001512 mutex_unlock(&fwnet_device_mutex);
1513
1514 return ret;
1515}
1516
1517static void fwnet_remove_peer(struct fwnet_peer *peer)
1518{
1519 struct fwnet_partial_datagram *pd, *pd_next;
1520
1521 spin_lock_irq(&peer->dev->lock);
1522 list_del(&peer->peer_link);
1523 spin_unlock_irq(&peer->dev->lock);
1524
1525 list_for_each_entry_safe(pd, pd_next, &peer->pd_list, pd_link)
1526 fwnet_pd_delete(pd);
1527
1528 kfree(peer);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001529}
1530
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001531static int fwnet_remove(struct device *_dev)
1532{
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001533 struct fwnet_peer *peer = dev_get_drvdata(_dev);
Stefan Richter5a124d32009-06-14 11:45:27 +02001534 struct fwnet_device *dev = peer->dev;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001535 struct net_device *net;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001536 struct fwnet_packet_task *ptask, *pt_next;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001537
Stefan Richter5a124d32009-06-14 11:45:27 +02001538 mutex_lock(&fwnet_device_mutex);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001539
Stefan Richter5a124d32009-06-14 11:45:27 +02001540 fwnet_remove_peer(peer);
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001541
Stefan Richter5a124d32009-06-14 11:45:27 +02001542 if (list_empty(&dev->peer_list)) {
1543 net = dev->netdev;
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001544 unregister_netdev(net);
1545
1546 if (dev->local_fifo != FWNET_NO_FIFO_ADDR)
1547 fw_core_remove_address_handler(&dev->handler);
1548 if (dev->broadcast_rcv_context) {
1549 fw_iso_context_stop(dev->broadcast_rcv_context);
1550 fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer,
1551 dev->card);
1552 fw_iso_context_destroy(dev->broadcast_rcv_context);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001553 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001554 list_for_each_entry_safe(ptask, pt_next,
1555 &dev->packet_list, pt_link) {
1556 dev_kfree_skb_any(ptask->skb);
1557 kmem_cache_free(fwnet_packet_task_cache, ptask);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001558 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001559 list_for_each_entry_safe(ptask, pt_next,
1560 &dev->broadcasted_list, pt_link) {
1561 dev_kfree_skb_any(ptask->skb);
1562 kmem_cache_free(fwnet_packet_task_cache, ptask);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001563 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001564 list_for_each_entry_safe(ptask, pt_next,
1565 &dev->sent_list, pt_link) {
1566 dev_kfree_skb_any(ptask->skb);
1567 kmem_cache_free(fwnet_packet_task_cache, ptask);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001568 }
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001569 list_del(&dev->dev_link);
1570
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001571 free_netdev(net);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001572 }
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001573
Stefan Richter5a124d32009-06-14 11:45:27 +02001574 mutex_unlock(&fwnet_device_mutex);
1575
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001576 return 0;
1577}
1578
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001579/*
1580 * FIXME abort partially sent fragmented datagrams,
1581 * discard partially received fragmented datagrams
1582 */
1583static void fwnet_update(struct fw_unit *unit)
1584{
1585 struct fw_device *device = fw_parent_device(unit);
Stefan Richterb01b4ba2009-06-16 20:43:55 +02001586 struct fwnet_peer *peer = dev_get_drvdata(&unit->device);
Stefan Richter5a124d32009-06-14 11:45:27 +02001587 int generation;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001588
Stefan Richter5a124d32009-06-14 11:45:27 +02001589 generation = device->generation;
1590
1591 spin_lock_irq(&peer->dev->lock);
1592 peer->node_id = device->node_id;
1593 peer->generation = generation;
1594 spin_unlock_irq(&peer->dev->lock);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001595}
1596
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001597static const struct ieee1394_device_id fwnet_id_table[] = {
1598 {
1599 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
1600 IEEE1394_MATCH_VERSION,
1601 .specifier_id = IANA_SPECIFIER_ID,
1602 .version = RFC2734_SW_VERSION,
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001603 },
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001604 { }
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001605};
1606
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001607static struct fw_driver fwnet_driver = {
1608 .driver = {
1609 .owner = THIS_MODULE,
1610 .name = "net",
1611 .bus = &fw_bus_type,
1612 .probe = fwnet_probe,
1613 .remove = fwnet_remove,
1614 },
1615 .update = fwnet_update,
1616 .id_table = fwnet_id_table,
1617};
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001618
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001619static const u32 rfc2374_unit_directory_data[] = {
1620 0x00040000, /* directory_length */
1621 0x1200005e, /* unit_specifier_id: IANA */
1622 0x81000003, /* textual descriptor offset */
1623 0x13000001, /* unit_sw_version: RFC 2734 */
1624 0x81000005, /* textual descriptor offset */
1625 0x00030000, /* descriptor_length */
1626 0x00000000, /* text */
1627 0x00000000, /* minimal ASCII, en */
1628 0x49414e41, /* I A N A */
1629 0x00030000, /* descriptor_length */
1630 0x00000000, /* text */
1631 0x00000000, /* minimal ASCII, en */
1632 0x49507634, /* I P v 4 */
1633};
1634
1635static struct fw_descriptor rfc2374_unit_directory = {
1636 .length = ARRAY_SIZE(rfc2374_unit_directory_data),
1637 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
1638 .data = rfc2374_unit_directory_data
1639};
1640
1641static int __init fwnet_init(void)
1642{
1643 int err;
1644
1645 err = fw_core_add_descriptor(&rfc2374_unit_directory);
1646 if (err)
1647 return err;
1648
1649 fwnet_packet_task_cache = kmem_cache_create("packet_task",
1650 sizeof(struct fwnet_packet_task), 0, 0, NULL);
1651 if (!fwnet_packet_task_cache) {
1652 err = -ENOMEM;
1653 goto out;
1654 }
1655
1656 err = driver_register(&fwnet_driver.driver);
1657 if (!err)
1658 return 0;
1659
1660 kmem_cache_destroy(fwnet_packet_task_cache);
1661out:
1662 fw_core_remove_descriptor(&rfc2374_unit_directory);
1663
1664 return err;
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001665}
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001666module_init(fwnet_init);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001667
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001668static void __exit fwnet_cleanup(void)
1669{
1670 driver_unregister(&fwnet_driver.driver);
1671 kmem_cache_destroy(fwnet_packet_task_cache);
1672 fw_core_remove_descriptor(&rfc2374_unit_directory);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001673}
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001674module_exit(fwnet_cleanup);
Jay Fenlasonc76acec2009-05-18 13:08:06 -04001675
Stefan Richterf91e3bd2009-06-07 22:57:53 +02001676MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>");
1677MODULE_DESCRIPTION("IPv4 over IEEE1394 as per RFC 2734");
1678MODULE_LICENSE("GPL");
1679MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table);