Commit | Line | Data |
---|---|---|
c76acec6 JF |
1 | /* |
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 | ||
f91e3bd8 | 9 | #include <linux/bug.h> |
c76acec6 JF |
10 | #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> | |
f91e3bd8 | 17 | #include <linux/jiffies.h> |
c76acec6 JF |
18 | #include <linux/mod_devicetable.h> |
19 | #include <linux/module.h> | |
20 | #include <linux/moduleparam.h> | |
21 | #include <linux/netdevice.h> | |
22 | #include <linux/skbuff.h> | |
23 | ||
24 | #include <asm/unaligned.h> | |
25 | #include <net/arp.h> | |
26 | ||
f91e3bd8 SR |
27 | #define FWNET_MAX_FRAGMENTS 25 /* arbitrary limit */ |
28 | #define FWNET_ISO_PAGE_COUNT (PAGE_SIZE < 16 * 1024 ? 4 : 2) | |
c76acec6 | 29 | |
f91e3bd8 SR |
30 | #define IEEE1394_BROADCAST_CHANNEL 31 |
31 | #define IEEE1394_ALL_NODES (0xffc0 | 0x003f) | |
32 | #define IEEE1394_MAX_PAYLOAD_S100 512 | |
33 | #define FWNET_NO_FIFO_ADDR (~0ULL) | |
c76acec6 | 34 | |
f91e3bd8 SR |
35 | #define IANA_SPECIFIER_ID 0x00005eU |
36 | #define RFC2734_SW_VERSION 0x000001U | |
c76acec6 | 37 | |
f91e3bd8 | 38 | #define IEEE1394_GASP_HDR_SIZE 8 |
c76acec6 | 39 | |
f91e3bd8 SR |
40 | #define RFC2374_UNFRAG_HDR_SIZE 4 |
41 | #define RFC2374_FRAG_HDR_SIZE 8 | |
42 | #define RFC2374_FRAG_OVERHEAD 4 | |
c76acec6 | 43 | |
f91e3bd8 SR |
44 | #define RFC2374_HDR_UNFRAG 0 /* unfragmented */ |
45 | #define RFC2374_HDR_FIRSTFRAG 1 /* first fragment */ | |
46 | #define RFC2374_HDR_LASTFRAG 2 /* last fragment */ | |
47 | #define RFC2374_HDR_INTFRAG 3 /* interior fragment */ | |
c76acec6 | 48 | |
f91e3bd8 | 49 | #define RFC2734_HW_ADDR_LEN 16 |
c76acec6 | 50 | |
f91e3bd8 SR |
51 | struct rfc2734_arp { |
52 | __be16 hw_type; /* 0x0018 */ | |
53 | __be16 proto_type; /* 0x0806 */ | |
54 | u8 hw_addr_len; /* 16 */ | |
55 | u8 ip_addr_len; /* 4 */ | |
56 | __be16 opcode; /* ARP Opcode */ | |
57 | /* Above is exactly the same format as struct arphdr */ | |
c76acec6 | 58 | |
f91e3bd8 SR |
59 | __be64 s_uniq_id; /* Sender's 64bit EUI */ |
60 | u8 max_rec; /* Sender's max packet size */ | |
61 | u8 sspd; /* Sender's max speed */ | |
62 | __be16 fifo_hi; /* hi 16bits of sender's FIFO addr */ | |
63 | __be32 fifo_lo; /* lo 32bits of sender's FIFO addr */ | |
64 | __be32 sip; /* Sender's IP Address */ | |
65 | __be32 tip; /* IP Address of requested hw addr */ | |
66 | } __attribute__((packed)); | |
c76acec6 | 67 | |
f91e3bd8 SR |
68 | /* This header format is specific to this driver implementation. */ |
69 | #define FWNET_ALEN 8 | |
70 | #define FWNET_HLEN 10 | |
71 | struct fwnet_header { | |
72 | u8 h_dest[FWNET_ALEN]; /* destination address */ | |
73 | __be16 h_proto; /* packet type ID field */ | |
74 | } __attribute__((packed)); | |
c76acec6 | 75 | |
f91e3bd8 SR |
76 | /* IPv4 and IPv6 encapsulation header */ |
77 | struct rfc2734_header { | |
c76acec6 JF |
78 | u32 w0; |
79 | u32 w1; | |
80 | }; | |
81 | ||
f91e3bd8 SR |
82 | #define fwnet_get_hdr_lf(h) (((h)->w0 & 0xc0000000) >> 30) |
83 | #define fwnet_get_hdr_ether_type(h) (((h)->w0 & 0x0000ffff)) | |
84 | #define fwnet_get_hdr_dg_size(h) (((h)->w0 & 0x0fff0000) >> 16) | |
85 | #define fwnet_get_hdr_fg_off(h) (((h)->w0 & 0x00000fff)) | |
86 | #define fwnet_get_hdr_dgl(h) (((h)->w1 & 0xffff0000) >> 16) | |
c76acec6 | 87 | |
f91e3bd8 SR |
88 | #define fwnet_set_hdr_lf(lf) ((lf) << 30) |
89 | #define fwnet_set_hdr_ether_type(et) (et) | |
90 | #define fwnet_set_hdr_dg_size(dgs) ((dgs) << 16) | |
91 | #define fwnet_set_hdr_fg_off(fgo) (fgo) | |
c76acec6 | 92 | |
f91e3bd8 | 93 | #define fwnet_set_hdr_dgl(dgl) ((dgl) << 16) |
c76acec6 | 94 | |
f91e3bd8 SR |
95 | static inline void fwnet_make_uf_hdr(struct rfc2734_header *hdr, |
96 | unsigned ether_type) | |
97 | { | |
98 | hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_UNFRAG) | |
99 | | fwnet_set_hdr_ether_type(ether_type); | |
100 | } | |
c76acec6 | 101 | |
f91e3bd8 SR |
102 | static inline void fwnet_make_ff_hdr(struct rfc2734_header *hdr, |
103 | unsigned ether_type, unsigned dg_size, unsigned dgl) | |
104 | { | |
105 | hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_FIRSTFRAG) | |
106 | | fwnet_set_hdr_dg_size(dg_size) | |
107 | | fwnet_set_hdr_ether_type(ether_type); | |
108 | hdr->w1 = fwnet_set_hdr_dgl(dgl); | |
109 | } | |
c76acec6 | 110 | |
f91e3bd8 SR |
111 | static inline void fwnet_make_sf_hdr(struct rfc2734_header *hdr, |
112 | unsigned lf, unsigned dg_size, unsigned fg_off, unsigned dgl) | |
113 | { | |
114 | hdr->w0 = fwnet_set_hdr_lf(lf) | |
115 | | fwnet_set_hdr_dg_size(dg_size) | |
116 | | fwnet_set_hdr_fg_off(fg_off); | |
117 | hdr->w1 = fwnet_set_hdr_dgl(dgl); | |
118 | } | |
c76acec6 JF |
119 | |
120 | /* This list keeps track of what parts of the datagram have been filled in */ | |
f91e3bd8 SR |
121 | struct fwnet_fragment_info { |
122 | struct list_head fi_link; | |
c76acec6 JF |
123 | u16 offset; |
124 | u16 len; | |
125 | }; | |
126 | ||
f91e3bd8 SR |
127 | struct fwnet_partial_datagram { |
128 | struct list_head pd_link; | |
129 | struct list_head fi_list; | |
c76acec6 JF |
130 | struct sk_buff *skb; |
131 | /* FIXME Why not use skb->data? */ | |
132 | char *pbuf; | |
133 | u16 datagram_label; | |
134 | u16 ether_type; | |
135 | u16 datagram_size; | |
136 | }; | |
137 | ||
138 | /* | |
139 | * We keep one of these for each IPv4 capable device attached to a fw_card. | |
140 | * The list of them is stored in the fw_card structure rather than in the | |
f91e3bd8 SR |
141 | * fwnet_device because the remote IPv4 nodes may be probed before the card is, |
142 | * so we need a place to store them before the fwnet_device structure is | |
c76acec6 JF |
143 | * allocated. |
144 | */ | |
f91e3bd8 SR |
145 | struct fwnet_peer { |
146 | struct list_head peer_link; | |
147 | /* guid of the remote peer */ | |
c76acec6 | 148 | u64 guid; |
f91e3bd8 | 149 | /* FIFO address to transmit datagrams to, or FWNET_NO_FIFO_ADDR */ |
c76acec6 JF |
150 | u64 fifo; |
151 | ||
152 | spinlock_t pdg_lock; /* partial datagram lock */ | |
f91e3bd8 SR |
153 | /* List of partial datagrams received from this peer */ |
154 | struct list_head pd_list; | |
155 | /* Number of entries in pd_list at the moment */ | |
c76acec6 JF |
156 | unsigned pdg_size; |
157 | ||
f91e3bd8 SR |
158 | /* max payload to transmit to this remote peer */ |
159 | /* This already includes the RFC2374_FRAG_HDR_SIZE overhead */ | |
c76acec6 JF |
160 | u16 max_payload; |
161 | /* outgoing datagram label */ | |
162 | u16 datagram_label; | |
f91e3bd8 SR |
163 | /* Current node_id of the remote peer */ |
164 | u16 node_id; | |
165 | /* current generation of the remote peer */ | |
c76acec6 | 166 | u8 generation; |
f91e3bd8 | 167 | /* max speed that this peer can receive at */ |
c76acec6 JF |
168 | u8 xmt_speed; |
169 | }; | |
170 | ||
f91e3bd8 | 171 | struct fwnet_device { |
c76acec6 | 172 | spinlock_t lock; |
f91e3bd8 SR |
173 | enum { |
174 | FWNET_BROADCAST_ERROR, | |
175 | FWNET_BROADCAST_RUNNING, | |
176 | FWNET_BROADCAST_STOPPED, | |
177 | } broadcast_state; | |
c76acec6 JF |
178 | struct fw_iso_context *broadcast_rcv_context; |
179 | struct fw_iso_buffer broadcast_rcv_buffer; | |
180 | void **broadcast_rcv_buffer_ptrs; | |
181 | unsigned broadcast_rcv_next_ptr; | |
182 | unsigned num_broadcast_rcv_ptrs; | |
183 | unsigned rcv_buffer_size; | |
184 | /* | |
185 | * This value is the maximum unfragmented datagram size that can be | |
186 | * sent by the hardware. It already has the GASP overhead and the | |
187 | * unfragmented datagram header overhead calculated into it. | |
188 | */ | |
189 | unsigned broadcast_xmt_max_payload; | |
190 | u16 broadcast_xmt_datagramlabel; | |
191 | ||
192 | /* | |
f91e3bd8 | 193 | * The CSR address that remote nodes must send datagrams to for us to |
c76acec6 JF |
194 | * receive them. |
195 | */ | |
196 | struct fw_address_handler handler; | |
197 | u64 local_fifo; | |
198 | ||
c76acec6 JF |
199 | /* List of packets to be sent */ |
200 | struct list_head packet_list; | |
201 | /* | |
202 | * List of packets that were broadcasted. When we get an ISO interrupt | |
203 | * one of them has been sent | |
204 | */ | |
205 | struct list_head broadcasted_list; | |
206 | /* List of packets that have been sent but not yet acked */ | |
207 | struct list_head sent_list; | |
208 | ||
209 | struct fw_card *card; | |
210 | }; | |
211 | ||
212 | /* This is our task struct. It's used for the packet complete callback. */ | |
f91e3bd8 | 213 | struct fwnet_packet_task { |
c76acec6 | 214 | /* |
f91e3bd8 SR |
215 | * ptask can actually be on dev->packet_list, dev->broadcasted_list, |
216 | * or dev->sent_list depending on its current state. | |
c76acec6 | 217 | */ |
f91e3bd8 | 218 | struct list_head pt_link; |
c76acec6 | 219 | struct fw_transaction transaction; |
f91e3bd8 | 220 | struct rfc2734_header hdr; |
c76acec6 | 221 | struct sk_buff *skb; |
f91e3bd8 SR |
222 | struct fwnet_device *dev; |
223 | ||
c76acec6 JF |
224 | int outstanding_pkts; |
225 | unsigned max_payload; | |
226 | u64 fifo_addr; | |
227 | u16 dest_node; | |
228 | u8 generation; | |
229 | u8 speed; | |
230 | }; | |
231 | ||
f91e3bd8 SR |
232 | /* |
233 | * saddr == NULL means use device source address. | |
234 | * daddr == NULL means leave destination address (eg unresolved arp). | |
235 | */ | |
236 | static int fwnet_header_create(struct sk_buff *skb, struct net_device *net, | |
237 | unsigned short type, const void *daddr, | |
238 | const void *saddr, unsigned len) | |
239 | { | |
240 | struct fwnet_header *h; | |
c76acec6 | 241 | |
f91e3bd8 SR |
242 | h = (struct fwnet_header *)skb_push(skb, sizeof(*h)); |
243 | put_unaligned_be16(type, &h->h_proto); | |
c76acec6 | 244 | |
f91e3bd8 SR |
245 | if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) { |
246 | memset(h->h_dest, 0, net->addr_len); | |
c76acec6 | 247 | |
f91e3bd8 | 248 | return net->hard_header_len; |
c76acec6 JF |
249 | } |
250 | ||
251 | if (daddr) { | |
f91e3bd8 SR |
252 | memcpy(h->h_dest, daddr, net->addr_len); |
253 | ||
254 | return net->hard_header_len; | |
c76acec6 JF |
255 | } |
256 | ||
f91e3bd8 | 257 | return -net->hard_header_len; |
c76acec6 JF |
258 | } |
259 | ||
f91e3bd8 | 260 | static int fwnet_header_rebuild(struct sk_buff *skb) |
c76acec6 | 261 | { |
f91e3bd8 | 262 | struct fwnet_header *h = (struct fwnet_header *)skb->data; |
c76acec6 | 263 | |
f91e3bd8 SR |
264 | if (get_unaligned_be16(&h->h_proto) == ETH_P_IP) |
265 | return arp_find((unsigned char *)&h->h_dest, skb); | |
c76acec6 | 266 | |
f91e3bd8 SR |
267 | fw_notify("%s: unable to resolve type %04x addresses\n", |
268 | skb->dev->name, be16_to_cpu(h->h_proto)); | |
c76acec6 JF |
269 | return 0; |
270 | } | |
271 | ||
f91e3bd8 SR |
272 | static int fwnet_header_cache(const struct neighbour *neigh, |
273 | struct hh_cache *hh) | |
274 | { | |
275 | struct net_device *net; | |
276 | struct fwnet_header *h; | |
c76acec6 | 277 | |
f91e3bd8 | 278 | if (hh->hh_type == cpu_to_be16(ETH_P_802_3)) |
c76acec6 | 279 | return -1; |
f91e3bd8 SR |
280 | net = neigh->dev; |
281 | h = (struct fwnet_header *)((u8 *)hh->hh_data + 16 - sizeof(*h)); | |
282 | h->h_proto = hh->hh_type; | |
283 | memcpy(h->h_dest, neigh->ha, net->addr_len); | |
284 | hh->hh_len = FWNET_HLEN; | |
c76acec6 | 285 | |
c76acec6 JF |
286 | return 0; |
287 | } | |
288 | ||
289 | /* Called by Address Resolution module to notify changes in address. */ | |
f91e3bd8 SR |
290 | static void fwnet_header_cache_update(struct hh_cache *hh, |
291 | const struct net_device *net, const unsigned char *haddr) | |
292 | { | |
293 | memcpy((u8 *)hh->hh_data + 16 - FWNET_HLEN, haddr, net->addr_len); | |
c76acec6 JF |
294 | } |
295 | ||
f91e3bd8 SR |
296 | static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
297 | { | |
298 | memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN); | |
299 | ||
300 | return FWNET_ALEN; | |
c76acec6 JF |
301 | } |
302 | ||
f91e3bd8 SR |
303 | static const struct header_ops fwnet_header_ops = { |
304 | .create = fwnet_header_create, | |
305 | .rebuild = fwnet_header_rebuild, | |
306 | .cache = fwnet_header_cache, | |
307 | .cache_update = fwnet_header_cache_update, | |
308 | .parse = fwnet_header_parse, | |
c76acec6 JF |
309 | }; |
310 | ||
c76acec6 | 311 | /* FIXME: is this correct for all cases? */ |
f91e3bd8 SR |
312 | static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd, |
313 | unsigned offset, unsigned len) | |
c76acec6 | 314 | { |
f91e3bd8 | 315 | struct fwnet_fragment_info *fi; |
c76acec6 JF |
316 | unsigned end = offset + len; |
317 | ||
f91e3bd8 SR |
318 | list_for_each_entry(fi, &pd->fi_list, fi_link) |
319 | if (offset < fi->offset + fi->len && end > fi->offset) | |
c76acec6 | 320 | return true; |
f91e3bd8 | 321 | |
c76acec6 JF |
322 | return false; |
323 | } | |
324 | ||
325 | /* Assumes that new fragment does not overlap any existing fragments */ | |
f91e3bd8 SR |
326 | static struct fwnet_fragment_info *fwnet_frag_new( |
327 | struct fwnet_partial_datagram *pd, unsigned offset, unsigned len) | |
328 | { | |
329 | struct fwnet_fragment_info *fi, *fi2, *new; | |
c76acec6 JF |
330 | struct list_head *list; |
331 | ||
f91e3bd8 SR |
332 | list = &pd->fi_list; |
333 | list_for_each_entry(fi, &pd->fi_list, fi_link) { | |
c76acec6 JF |
334 | if (fi->offset + fi->len == offset) { |
335 | /* The new fragment can be tacked on to the end */ | |
336 | /* Did the new fragment plug a hole? */ | |
f91e3bd8 SR |
337 | fi2 = list_entry(fi->fi_link.next, |
338 | struct fwnet_fragment_info, fi_link); | |
c76acec6 | 339 | if (fi->offset + fi->len == fi2->offset) { |
c76acec6 JF |
340 | /* glue fragments together */ |
341 | fi->len += len + fi2->len; | |
f91e3bd8 | 342 | list_del(&fi2->fi_link); |
c76acec6 JF |
343 | kfree(fi2); |
344 | } else { | |
c76acec6 JF |
345 | fi->len += len; |
346 | } | |
f91e3bd8 | 347 | |
c76acec6 JF |
348 | return fi; |
349 | } | |
350 | if (offset + len == fi->offset) { | |
351 | /* The new fragment can be tacked on to the beginning */ | |
352 | /* Did the new fragment plug a hole? */ | |
f91e3bd8 SR |
353 | fi2 = list_entry(fi->fi_link.prev, |
354 | struct fwnet_fragment_info, fi_link); | |
c76acec6 JF |
355 | if (fi2->offset + fi2->len == fi->offset) { |
356 | /* glue fragments together */ | |
c76acec6 | 357 | fi2->len += fi->len + len; |
f91e3bd8 | 358 | list_del(&fi->fi_link); |
c76acec6 | 359 | kfree(fi); |
f91e3bd8 | 360 | |
c76acec6 JF |
361 | return fi2; |
362 | } | |
c76acec6 JF |
363 | fi->offset = offset; |
364 | fi->len += len; | |
f91e3bd8 | 365 | |
c76acec6 JF |
366 | return fi; |
367 | } | |
368 | if (offset > fi->offset + fi->len) { | |
f91e3bd8 | 369 | list = &fi->fi_link; |
c76acec6 JF |
370 | break; |
371 | } | |
372 | if (offset + len < fi->offset) { | |
f91e3bd8 | 373 | list = fi->fi_link.prev; |
c76acec6 JF |
374 | break; |
375 | } | |
376 | } | |
377 | ||
378 | new = kmalloc(sizeof(*new), GFP_ATOMIC); | |
379 | if (!new) { | |
f91e3bd8 | 380 | fw_error("out of memory\n"); |
c76acec6 JF |
381 | return NULL; |
382 | } | |
383 | ||
384 | new->offset = offset; | |
385 | new->len = len; | |
f91e3bd8 SR |
386 | list_add(&new->fi_link, list); |
387 | ||
c76acec6 JF |
388 | return new; |
389 | } | |
390 | ||
f91e3bd8 SR |
391 | static struct fwnet_partial_datagram *fwnet_pd_new(struct net_device *net, |
392 | struct fwnet_peer *peer, u16 datagram_label, unsigned dg_size, | |
393 | void *frag_buf, unsigned frag_off, unsigned frag_len) | |
394 | { | |
395 | struct fwnet_partial_datagram *new; | |
396 | struct fwnet_fragment_info *fi; | |
c76acec6 JF |
397 | |
398 | new = kmalloc(sizeof(*new), GFP_ATOMIC); | |
399 | if (!new) | |
400 | goto fail; | |
f91e3bd8 SR |
401 | |
402 | INIT_LIST_HEAD(&new->fi_list); | |
403 | fi = fwnet_frag_new(new, frag_off, frag_len); | |
404 | if (fi == NULL) | |
c76acec6 | 405 | goto fail_w_new; |
f91e3bd8 | 406 | |
c76acec6 JF |
407 | new->datagram_label = datagram_label; |
408 | new->datagram_size = dg_size; | |
f91e3bd8 SR |
409 | new->skb = dev_alloc_skb(dg_size + net->hard_header_len + 15); |
410 | if (new->skb == NULL) | |
c76acec6 | 411 | goto fail_w_fi; |
f91e3bd8 SR |
412 | |
413 | skb_reserve(new->skb, (net->hard_header_len + 15) & ~15); | |
c76acec6 JF |
414 | new->pbuf = skb_put(new->skb, dg_size); |
415 | memcpy(new->pbuf + frag_off, frag_buf, frag_len); | |
f91e3bd8 SR |
416 | list_add_tail(&new->pd_link, &peer->pd_list); |
417 | ||
c76acec6 JF |
418 | return new; |
419 | ||
420 | fail_w_fi: | |
421 | kfree(fi); | |
422 | fail_w_new: | |
423 | kfree(new); | |
424 | fail: | |
f91e3bd8 SR |
425 | fw_error("out of memory\n"); |
426 | ||
c76acec6 JF |
427 | return NULL; |
428 | } | |
429 | ||
f91e3bd8 SR |
430 | static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer, |
431 | u16 datagram_label) | |
432 | { | |
433 | struct fwnet_partial_datagram *pd; | |
c76acec6 | 434 | |
f91e3bd8 SR |
435 | list_for_each_entry(pd, &peer->pd_list, pd_link) |
436 | if (pd->datagram_label == datagram_label) | |
c76acec6 | 437 | return pd; |
f91e3bd8 | 438 | |
c76acec6 JF |
439 | return NULL; |
440 | } | |
441 | ||
442 | ||
f91e3bd8 SR |
443 | static void fwnet_pd_delete(struct fwnet_partial_datagram *old) |
444 | { | |
445 | struct fwnet_fragment_info *fi, *n; | |
c76acec6 | 446 | |
f91e3bd8 | 447 | list_for_each_entry_safe(fi, n, &old->fi_list, fi_link) |
c76acec6 | 448 | kfree(fi); |
f91e3bd8 SR |
449 | |
450 | list_del(&old->pd_link); | |
c76acec6 JF |
451 | dev_kfree_skb_any(old->skb); |
452 | kfree(old); | |
453 | } | |
454 | ||
f91e3bd8 SR |
455 | static bool fwnet_pd_update(struct fwnet_peer *peer, |
456 | struct fwnet_partial_datagram *pd, void *frag_buf, | |
457 | unsigned frag_off, unsigned frag_len) | |
458 | { | |
459 | if (fwnet_frag_new(pd, frag_off, frag_len) == NULL) | |
c76acec6 | 460 | return false; |
f91e3bd8 | 461 | |
c76acec6 JF |
462 | memcpy(pd->pbuf + frag_off, frag_buf, frag_len); |
463 | ||
464 | /* | |
465 | * Move list entry to beginnig of list so that oldest partial | |
466 | * datagrams percolate to the end of the list | |
467 | */ | |
f91e3bd8 SR |
468 | list_move_tail(&pd->pd_link, &peer->pd_list); |
469 | ||
c76acec6 JF |
470 | return true; |
471 | } | |
472 | ||
f91e3bd8 SR |
473 | static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd) |
474 | { | |
475 | struct fwnet_fragment_info *fi; | |
c76acec6 | 476 | |
f91e3bd8 | 477 | fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link); |
c76acec6 | 478 | |
f91e3bd8 | 479 | return fi->len == pd->datagram_size; |
c76acec6 JF |
480 | } |
481 | ||
f91e3bd8 SR |
482 | static int fwnet_peer_new(struct fw_card *card, struct fw_device *device) |
483 | { | |
484 | struct fwnet_peer *peer; | |
c76acec6 | 485 | |
f91e3bd8 SR |
486 | peer = kmalloc(sizeof(*peer), GFP_KERNEL); |
487 | if (!peer) { | |
488 | fw_error("out of memory\n"); | |
c76acec6 | 489 | |
c76acec6 JF |
490 | return -ENOMEM; |
491 | } | |
f91e3bd8 SR |
492 | peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4]; |
493 | peer->fifo = FWNET_NO_FIFO_ADDR; | |
494 | INIT_LIST_HEAD(&peer->pd_list); | |
495 | spin_lock_init(&peer->pdg_lock); | |
496 | peer->pdg_size = 0; | |
497 | peer->generation = device->generation; | |
c76acec6 | 498 | rmb(); |
f91e3bd8 | 499 | peer->node_id = device->node_id; |
c76acec6 | 500 | /* FIXME what should it really be? */ |
f91e3bd8 SR |
501 | peer->max_payload = IEEE1394_MAX_PAYLOAD_S100 - RFC2374_UNFRAG_HDR_SIZE; |
502 | peer->datagram_label = 0U; | |
503 | peer->xmt_speed = device->max_speed; | |
504 | list_add_tail(&peer->peer_link, &card->peer_list); | |
505 | ||
c76acec6 JF |
506 | return 0; |
507 | } | |
508 | ||
f91e3bd8 SR |
509 | /* FIXME caller must take the lock, or peer needs to be reference-counted */ |
510 | static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev, | |
511 | u64 guid) | |
512 | { | |
513 | struct fwnet_peer *p, *peer = NULL; | |
c76acec6 JF |
514 | unsigned long flags; |
515 | ||
f91e3bd8 SR |
516 | spin_lock_irqsave(&dev->lock, flags); |
517 | list_for_each_entry(p, &dev->card->peer_list, peer_link) | |
518 | if (p->guid == guid) { | |
519 | peer = p; | |
520 | break; | |
c76acec6 | 521 | } |
f91e3bd8 | 522 | spin_unlock_irqrestore(&dev->lock, flags); |
c76acec6 | 523 | |
f91e3bd8 | 524 | return peer; |
c76acec6 JF |
525 | } |
526 | ||
f91e3bd8 SR |
527 | /* FIXME caller must take the lock, or peer needs to be reference-counted */ |
528 | /* FIXME node_id doesn't mean anything without generation */ | |
529 | static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev, | |
530 | u16 node_id) | |
531 | { | |
532 | struct fwnet_peer *p, *peer = NULL; | |
c76acec6 JF |
533 | unsigned long flags; |
534 | ||
f91e3bd8 SR |
535 | spin_lock_irqsave(&dev->lock, flags); |
536 | list_for_each_entry(p, &dev->card->peer_list, peer_link) | |
537 | if (p->node_id == node_id) { | |
538 | peer = p; | |
539 | break; | |
c76acec6 | 540 | } |
f91e3bd8 SR |
541 | spin_unlock_irqrestore(&dev->lock, flags); |
542 | ||
543 | return peer; | |
c76acec6 JF |
544 | } |
545 | ||
f91e3bd8 SR |
546 | /* FIXME */ |
547 | static void fwnet_peer_delete(struct fw_card *card, struct fw_device *device) | |
548 | { | |
549 | struct net_device *net; | |
550 | struct fwnet_device *dev; | |
551 | struct fwnet_peer *peer; | |
c76acec6 JF |
552 | u64 guid; |
553 | unsigned long flags; | |
f91e3bd8 | 554 | struct fwnet_partial_datagram *pd, *pd_next; |
c76acec6 JF |
555 | |
556 | guid = (u64)device->config_rom[3] << 32 | device->config_rom[4]; | |
f91e3bd8 SR |
557 | net = card->netdev; |
558 | if (net) | |
559 | dev = netdev_priv(net); | |
c76acec6 | 560 | else |
f91e3bd8 SR |
561 | dev = NULL; |
562 | if (dev) | |
563 | spin_lock_irqsave(&dev->lock, flags); | |
564 | ||
565 | list_for_each_entry(peer, &card->peer_list, peer_link) { | |
566 | if (peer->guid == guid) { | |
567 | list_del(&peer->peer_link); | |
568 | list_for_each_entry_safe(pd, pd_next, &peer->pd_list, | |
569 | pd_link) | |
570 | fwnet_pd_delete(pd); | |
c76acec6 JF |
571 | break; |
572 | } | |
573 | } | |
f91e3bd8 SR |
574 | if (dev) |
575 | spin_unlock_irqrestore(&dev->lock, flags); | |
c76acec6 JF |
576 | } |
577 | ||
f91e3bd8 SR |
578 | static int fwnet_finish_incoming_packet(struct net_device *net, |
579 | struct sk_buff *skb, u16 source_node_id, | |
580 | bool is_broadcast, u16 ether_type) | |
581 | { | |
582 | struct fwnet_device *dev; | |
583 | static const __be64 broadcast_hw = cpu_to_be64(~0ULL); | |
c76acec6 | 584 | int status; |
f91e3bd8 | 585 | __be64 guid; |
c76acec6 | 586 | |
f91e3bd8 | 587 | dev = netdev_priv(net); |
c76acec6 | 588 | /* Write metadata, and then pass to the receive level */ |
f91e3bd8 | 589 | skb->dev = net; |
c76acec6 JF |
590 | skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ |
591 | ||
592 | /* | |
593 | * Parse the encapsulation header. This actually does the job of | |
594 | * converting to an ethernet frame header, as well as arp | |
595 | * conversion if needed. ARP conversion is easier in this | |
596 | * direction, since we are using ethernet as our backend. | |
597 | */ | |
598 | /* | |
599 | * If this is an ARP packet, convert it. First, we want to make | |
600 | * use of some of the fields, since they tell us a little bit | |
601 | * about the sending machine. | |
602 | */ | |
603 | if (ether_type == ETH_P_ARP) { | |
f91e3bd8 | 604 | struct rfc2734_arp *arp1394; |
c76acec6 JF |
605 | struct arphdr *arp; |
606 | unsigned char *arp_ptr; | |
607 | u64 fifo_addr; | |
f91e3bd8 | 608 | u64 peer_guid; |
c76acec6 JF |
609 | u8 max_rec; |
610 | u8 sspd; | |
611 | u16 max_payload; | |
f91e3bd8 SR |
612 | struct fwnet_peer *peer; |
613 | static const u16 fwnet_speed_to_max_payload[] = { | |
c76acec6 JF |
614 | /* S100, S200, S400, S800, S1600, S3200 */ |
615 | 512, 1024, 2048, 4096, 4096, 4096 | |
616 | }; | |
617 | ||
f91e3bd8 | 618 | arp1394 = (struct rfc2734_arp *)skb->data; |
c76acec6 JF |
619 | arp = (struct arphdr *)skb->data; |
620 | arp_ptr = (unsigned char *)(arp + 1); | |
f91e3bd8 SR |
621 | fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 |
622 | | ntohl(arp1394->fifo_lo); | |
623 | max_rec = dev->card->max_receive; | |
624 | if (arp1394->max_rec < max_rec) | |
c76acec6 JF |
625 | max_rec = arp1394->max_rec; |
626 | sspd = arp1394->sspd; | |
f91e3bd8 SR |
627 | /* Sanity check. OS X 10.3 PPC reportedly sends 131. */ |
628 | if (sspd > SCODE_3200) { | |
629 | fw_notify("sspd %x out of range\n", sspd); | |
c76acec6 JF |
630 | sspd = 0; |
631 | } | |
632 | ||
f91e3bd8 SR |
633 | max_payload = min(fwnet_speed_to_max_payload[sspd], |
634 | (u16)(1 << (max_rec + 1))) - RFC2374_UNFRAG_HDR_SIZE; | |
c76acec6 | 635 | |
f91e3bd8 SR |
636 | peer_guid = get_unaligned_be64(&arp1394->s_uniq_id); |
637 | peer = fwnet_peer_find_by_guid(dev, peer_guid); | |
638 | if (!peer) { | |
639 | fw_notify("No peer for ARP packet from %016llx\n", | |
640 | (unsigned long long)peer_guid); | |
c76acec6 JF |
641 | goto failed_proto; |
642 | } | |
f91e3bd8 SR |
643 | |
644 | /* FIXME don't use card->generation */ | |
645 | if (peer->node_id != source_node_id || | |
646 | peer->generation != dev->card->generation) { | |
647 | fw_notify("Internal error: peer->node_id (%x) != " | |
648 | "source_node_id (%x) or peer->generation (%x)" | |
649 | " != dev->card->generation(%x)\n", | |
650 | peer->node_id, source_node_id, | |
651 | peer->generation, dev->card->generation); | |
652 | peer->node_id = source_node_id; | |
653 | peer->generation = dev->card->generation; | |
c76acec6 JF |
654 | } |
655 | ||
656 | /* FIXME: for debugging */ | |
f91e3bd8 | 657 | if (sspd > SCODE_400) |
c76acec6 JF |
658 | sspd = SCODE_400; |
659 | /* Update our speed/payload/fifo_offset table */ | |
660 | /* | |
661 | * FIXME: this does not handle cases where two high-speed endpoints must use a slower speed because of | |
662 | * a lower speed hub between them. We need to look at the actual topology map here. | |
663 | */ | |
f91e3bd8 SR |
664 | peer->fifo = fifo_addr; |
665 | peer->max_payload = max_payload; | |
c76acec6 JF |
666 | /* |
667 | * Only allow speeds to go down from their initial value. | |
f91e3bd8 SR |
668 | * Otherwise a local peer that can only do S400 or slower may |
669 | * be told to transmit at S800 to a faster remote peer. | |
c76acec6 | 670 | */ |
f91e3bd8 SR |
671 | if (peer->xmt_speed > sspd) |
672 | peer->xmt_speed = sspd; | |
c76acec6 JF |
673 | |
674 | /* | |
675 | * Now that we're done with the 1394 specific stuff, we'll | |
676 | * need to alter some of the data. Believe it or not, all | |
677 | * that needs to be done is sender_IP_address needs to be | |
678 | * moved, the destination hardware address get stuffed | |
679 | * in and the hardware address length set to 8. | |
680 | * | |
681 | * IMPORTANT: The code below overwrites 1394 specific data | |
682 | * needed above so keep the munging of the data for the | |
683 | * higher level IP stack last. | |
684 | */ | |
685 | ||
686 | arp->ar_hln = 8; | |
f91e3bd8 SR |
687 | /* skip over sender unique id */ |
688 | arp_ptr += arp->ar_hln; | |
689 | /* move sender IP addr */ | |
690 | put_unaligned(arp1394->sip, (u32 *)arp_ptr); | |
691 | /* skip over sender IP addr */ | |
692 | arp_ptr += arp->ar_pln; | |
c76acec6 JF |
693 | |
694 | if (arp->ar_op == htons(ARPOP_REQUEST)) | |
695 | memset(arp_ptr, 0, sizeof(u64)); | |
696 | else | |
f91e3bd8 | 697 | memcpy(arp_ptr, net->dev_addr, sizeof(u64)); |
c76acec6 JF |
698 | } |
699 | ||
700 | /* Now add the ethernet header. */ | |
f91e3bd8 SR |
701 | guid = cpu_to_be64(dev->card->guid); |
702 | if (dev_hard_header(skb, net, ether_type, | |
703 | is_broadcast ? &broadcast_hw : &guid, | |
704 | NULL, skb->len) >= 0) { | |
705 | struct fwnet_header *eth; | |
c76acec6 JF |
706 | u16 *rawp; |
707 | __be16 protocol; | |
708 | ||
709 | skb_reset_mac_header(skb); | |
710 | skb_pull(skb, sizeof(*eth)); | |
f91e3bd8 | 711 | eth = (struct fwnet_header *)skb_mac_header(skb); |
c76acec6 | 712 | if (*eth->h_dest & 1) { |
f91e3bd8 SR |
713 | if (memcmp(eth->h_dest, net->broadcast, |
714 | net->addr_len) == 0) | |
c76acec6 | 715 | skb->pkt_type = PACKET_BROADCAST; |
c76acec6 JF |
716 | #if 0 |
717 | else | |
718 | skb->pkt_type = PACKET_MULTICAST; | |
719 | #endif | |
720 | } else { | |
f91e3bd8 | 721 | if (memcmp(eth->h_dest, net->dev_addr, net->addr_len)) { |
c76acec6 JF |
722 | u64 a1, a2; |
723 | ||
f91e3bd8 SR |
724 | memcpy(&a1, eth->h_dest, sizeof(u64)); |
725 | memcpy(&a2, net->dev_addr, sizeof(u64)); | |
c76acec6 JF |
726 | skb->pkt_type = PACKET_OTHERHOST; |
727 | } | |
728 | } | |
729 | if (ntohs(eth->h_proto) >= 1536) { | |
c76acec6 JF |
730 | protocol = eth->h_proto; |
731 | } else { | |
732 | rawp = (u16 *)skb->data; | |
f91e3bd8 | 733 | if (*rawp == 0xffff) |
c76acec6 | 734 | protocol = htons(ETH_P_802_3); |
f91e3bd8 | 735 | else |
c76acec6 | 736 | protocol = htons(ETH_P_802_2); |
c76acec6 JF |
737 | } |
738 | skb->protocol = protocol; | |
739 | } | |
740 | status = netif_rx(skb); | |
f91e3bd8 SR |
741 | if (status == NET_RX_DROP) { |
742 | net->stats.rx_errors++; | |
743 | net->stats.rx_dropped++; | |
c76acec6 | 744 | } else { |
f91e3bd8 SR |
745 | net->stats.rx_packets++; |
746 | net->stats.rx_bytes += skb->len; | |
c76acec6 | 747 | } |
f91e3bd8 SR |
748 | if (netif_queue_stopped(net)) |
749 | netif_wake_queue(net); | |
750 | ||
c76acec6 JF |
751 | return 0; |
752 | ||
753 | failed_proto: | |
f91e3bd8 SR |
754 | net->stats.rx_errors++; |
755 | net->stats.rx_dropped++; | |
756 | ||
c76acec6 | 757 | dev_kfree_skb_any(skb); |
f91e3bd8 SR |
758 | if (netif_queue_stopped(net)) |
759 | netif_wake_queue(net); | |
760 | ||
761 | net->last_rx = jiffies; | |
762 | ||
c76acec6 JF |
763 | return 0; |
764 | } | |
765 | ||
f91e3bd8 SR |
766 | static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len, |
767 | u16 source_node_id, bool is_broadcast) | |
768 | { | |
c76acec6 | 769 | struct sk_buff *skb; |
f91e3bd8 SR |
770 | struct net_device *net; |
771 | struct rfc2734_header hdr; | |
c76acec6 JF |
772 | unsigned lf; |
773 | unsigned long flags; | |
f91e3bd8 SR |
774 | struct fwnet_peer *peer; |
775 | struct fwnet_partial_datagram *pd; | |
c76acec6 JF |
776 | int fg_off; |
777 | int dg_size; | |
778 | u16 datagram_label; | |
779 | int retval; | |
780 | u16 ether_type; | |
781 | ||
f91e3bd8 | 782 | net = dev->card->netdev; |
c76acec6 | 783 | |
f91e3bd8 SR |
784 | hdr.w0 = be32_to_cpu(buf[0]); |
785 | lf = fwnet_get_hdr_lf(&hdr); | |
786 | if (lf == RFC2374_HDR_UNFRAG) { | |
c76acec6 JF |
787 | /* |
788 | * An unfragmented datagram has been received by the ieee1394 | |
789 | * bus. Build an skbuff around it so we can pass it to the | |
790 | * high level network layer. | |
791 | */ | |
f91e3bd8 | 792 | ether_type = fwnet_get_hdr_ether_type(&hdr); |
c76acec6 | 793 | buf++; |
f91e3bd8 | 794 | len -= RFC2374_UNFRAG_HDR_SIZE; |
c76acec6 | 795 | |
f91e3bd8 | 796 | skb = dev_alloc_skb(len + net->hard_header_len + 15); |
c76acec6 | 797 | if (unlikely(!skb)) { |
f91e3bd8 SR |
798 | fw_error("out of memory\n"); |
799 | net->stats.rx_dropped++; | |
800 | ||
c76acec6 JF |
801 | return -1; |
802 | } | |
f91e3bd8 SR |
803 | skb_reserve(skb, (net->hard_header_len + 15) & ~15); |
804 | memcpy(skb_put(skb, len), buf, len); | |
805 | ||
806 | return fwnet_finish_incoming_packet(net, skb, source_node_id, | |
807 | is_broadcast, ether_type); | |
c76acec6 JF |
808 | } |
809 | /* A datagram fragment has been received, now the fun begins. */ | |
810 | hdr.w1 = ntohl(buf[1]); | |
f91e3bd8 SR |
811 | buf += 2; |
812 | len -= RFC2374_FRAG_HDR_SIZE; | |
813 | if (lf == RFC2374_HDR_FIRSTFRAG) { | |
814 | ether_type = fwnet_get_hdr_ether_type(&hdr); | |
c76acec6 JF |
815 | fg_off = 0; |
816 | } else { | |
f91e3bd8 SR |
817 | ether_type = 0; |
818 | fg_off = fwnet_get_hdr_fg_off(&hdr); | |
c76acec6 | 819 | } |
f91e3bd8 SR |
820 | datagram_label = fwnet_get_hdr_dgl(&hdr); |
821 | dg_size = fwnet_get_hdr_dg_size(&hdr); /* ??? + 1 */ | |
822 | peer = fwnet_peer_find_by_node_id(dev, source_node_id); | |
823 | ||
824 | spin_lock_irqsave(&peer->pdg_lock, flags); | |
825 | ||
826 | pd = fwnet_pd_find(peer, datagram_label); | |
c76acec6 | 827 | if (pd == NULL) { |
f91e3bd8 | 828 | while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) { |
c76acec6 | 829 | /* remove the oldest */ |
f91e3bd8 SR |
830 | fwnet_pd_delete(list_first_entry(&peer->pd_list, |
831 | struct fwnet_partial_datagram, pd_link)); | |
832 | peer->pdg_size--; | |
c76acec6 | 833 | } |
f91e3bd8 SR |
834 | pd = fwnet_pd_new(net, peer, datagram_label, |
835 | dg_size, buf, fg_off, len); | |
836 | if (pd == NULL) { | |
c76acec6 JF |
837 | retval = -ENOMEM; |
838 | goto bad_proto; | |
839 | } | |
f91e3bd8 | 840 | peer->pdg_size++; |
c76acec6 | 841 | } else { |
f91e3bd8 SR |
842 | if (fwnet_frag_overlap(pd, fg_off, len) || |
843 | pd->datagram_size != dg_size) { | |
c76acec6 JF |
844 | /* |
845 | * Differing datagram sizes or overlapping fragments, | |
f91e3bd8 | 846 | * discard old datagram and start a new one. |
c76acec6 | 847 | */ |
f91e3bd8 SR |
848 | fwnet_pd_delete(pd); |
849 | pd = fwnet_pd_new(net, peer, datagram_label, | |
850 | dg_size, buf, fg_off, len); | |
851 | if (pd == NULL) { | |
c76acec6 | 852 | retval = -ENOMEM; |
f91e3bd8 | 853 | peer->pdg_size--; |
c76acec6 JF |
854 | goto bad_proto; |
855 | } | |
856 | } else { | |
f91e3bd8 | 857 | if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) { |
c76acec6 JF |
858 | /* |
859 | * Couldn't save off fragment anyway | |
860 | * so might as well obliterate the | |
861 | * datagram now. | |
862 | */ | |
f91e3bd8 SR |
863 | fwnet_pd_delete(pd); |
864 | peer->pdg_size--; | |
c76acec6 JF |
865 | goto bad_proto; |
866 | } | |
867 | } | |
868 | } /* new datagram or add to existing one */ | |
869 | ||
f91e3bd8 | 870 | if (lf == RFC2374_HDR_FIRSTFRAG) |
c76acec6 | 871 | pd->ether_type = ether_type; |
f91e3bd8 SR |
872 | |
873 | if (fwnet_pd_is_complete(pd)) { | |
c76acec6 | 874 | ether_type = pd->ether_type; |
f91e3bd8 | 875 | peer->pdg_size--; |
c76acec6 | 876 | skb = skb_get(pd->skb); |
f91e3bd8 SR |
877 | fwnet_pd_delete(pd); |
878 | ||
879 | spin_unlock_irqrestore(&peer->pdg_lock, flags); | |
880 | ||
881 | return fwnet_finish_incoming_packet(net, skb, source_node_id, | |
882 | false, ether_type); | |
c76acec6 JF |
883 | } |
884 | /* | |
885 | * Datagram is not complete, we're done for the | |
886 | * moment. | |
887 | */ | |
f91e3bd8 SR |
888 | spin_unlock_irqrestore(&peer->pdg_lock, flags); |
889 | ||
c76acec6 JF |
890 | return 0; |
891 | ||
892 | bad_proto: | |
f91e3bd8 SR |
893 | spin_unlock_irqrestore(&peer->pdg_lock, flags); |
894 | ||
895 | if (netif_queue_stopped(net)) | |
896 | netif_wake_queue(net); | |
897 | ||
c76acec6 JF |
898 | return 0; |
899 | } | |
900 | ||
f91e3bd8 SR |
901 | static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r, |
902 | int tcode, int destination, int source, int generation, | |
903 | int speed, unsigned long long offset, void *payload, | |
904 | size_t length, void *callback_data) | |
905 | { | |
906 | struct fwnet_device *dev; | |
c76acec6 JF |
907 | int status; |
908 | ||
f91e3bd8 SR |
909 | dev = callback_data; |
910 | if (tcode != TCODE_WRITE_BLOCK_REQUEST | |
911 | || destination != card->node_id /* <- FIXME */ | |
912 | || generation != card->generation /* <- FIXME */ | |
913 | || offset != dev->handler.offset) { | |
c76acec6 | 914 | fw_send_response(card, r, RCODE_CONFLICT_ERROR); |
f91e3bd8 | 915 | |
c76acec6 JF |
916 | return; |
917 | } | |
f91e3bd8 SR |
918 | |
919 | status = fwnet_incoming_packet(dev, payload, length, source, false); | |
920 | if (status != 0) { | |
921 | fw_error("Incoming packet failure\n"); | |
922 | fw_send_response(card, r, RCODE_CONFLICT_ERROR); | |
923 | ||
c76acec6 JF |
924 | return; |
925 | } | |
f91e3bd8 SR |
926 | |
927 | fw_send_response(card, r, RCODE_COMPLETE); | |
c76acec6 JF |
928 | } |
929 | ||
f91e3bd8 SR |
930 | static void fwnet_receive_broadcast(struct fw_iso_context *context, |
931 | u32 cycle, size_t header_length, void *header, void *data) | |
932 | { | |
933 | struct fwnet_device *dev; | |
c76acec6 JF |
934 | struct fw_iso_packet packet; |
935 | struct fw_card *card; | |
f91e3bd8 SR |
936 | __be16 *hdr_ptr; |
937 | __be32 *buf_ptr; | |
c76acec6 JF |
938 | int retval; |
939 | u32 length; | |
940 | u16 source_node_id; | |
941 | u32 specifier_id; | |
942 | u32 ver; | |
943 | unsigned long offset; | |
944 | unsigned long flags; | |
945 | ||
f91e3bd8 SR |
946 | dev = data; |
947 | card = dev->card; | |
c76acec6 | 948 | hdr_ptr = header; |
f91e3bd8 SR |
949 | length = be16_to_cpup(hdr_ptr); |
950 | ||
951 | spin_lock_irqsave(&dev->lock, flags); | |
952 | ||
953 | offset = dev->rcv_buffer_size * dev->broadcast_rcv_next_ptr; | |
954 | buf_ptr = dev->broadcast_rcv_buffer_ptrs[dev->broadcast_rcv_next_ptr++]; | |
955 | if (dev->broadcast_rcv_next_ptr == dev->num_broadcast_rcv_ptrs) | |
956 | dev->broadcast_rcv_next_ptr = 0; | |
957 | ||
958 | spin_unlock_irqrestore(&dev->lock, flags); | |
c76acec6 JF |
959 | |
960 | specifier_id = (be32_to_cpu(buf_ptr[0]) & 0xffff) << 8 | |
961 | | (be32_to_cpu(buf_ptr[1]) & 0xff000000) >> 24; | |
f91e3bd8 | 962 | ver = be32_to_cpu(buf_ptr[1]) & 0xffffff; |
c76acec6 | 963 | source_node_id = be32_to_cpu(buf_ptr[0]) >> 16; |
f91e3bd8 SR |
964 | |
965 | if (specifier_id == IANA_SPECIFIER_ID && ver == RFC2734_SW_VERSION) { | |
c76acec6 | 966 | buf_ptr += 2; |
f91e3bd8 SR |
967 | length -= IEEE1394_GASP_HDR_SIZE; |
968 | fwnet_incoming_packet(dev, buf_ptr, length, | |
969 | source_node_id, true); | |
970 | } | |
971 | ||
972 | packet.payload_length = dev->rcv_buffer_size; | |
c76acec6 JF |
973 | packet.interrupt = 1; |
974 | packet.skip = 0; | |
975 | packet.tag = 3; | |
976 | packet.sy = 0; | |
f91e3bd8 SR |
977 | packet.header_length = IEEE1394_GASP_HDR_SIZE; |
978 | ||
979 | spin_lock_irqsave(&dev->lock, flags); | |
c76acec6 | 980 | |
f91e3bd8 SR |
981 | retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet, |
982 | &dev->broadcast_rcv_buffer, offset); | |
983 | ||
984 | spin_unlock_irqrestore(&dev->lock, flags); | |
985 | ||
986 | if (retval < 0) | |
987 | fw_error("requeue failed\n"); | |
c76acec6 JF |
988 | } |
989 | ||
f91e3bd8 SR |
990 | static struct kmem_cache *fwnet_packet_task_cache; |
991 | ||
992 | static int fwnet_send_packet(struct fwnet_packet_task *ptask); | |
993 | ||
994 | static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask) | |
995 | { | |
996 | struct fwnet_device *dev; | |
c76acec6 JF |
997 | unsigned long flags; |
998 | ||
f91e3bd8 SR |
999 | dev = ptask->dev; |
1000 | ||
1001 | spin_lock_irqsave(&dev->lock, flags); | |
1002 | list_del(&ptask->pt_link); | |
1003 | spin_unlock_irqrestore(&dev->lock, flags); | |
1004 | ||
1005 | ptask->outstanding_pkts--; /* FIXME access inside lock */ | |
1006 | ||
1007 | if (ptask->outstanding_pkts > 0) { | |
c76acec6 JF |
1008 | u16 dg_size; |
1009 | u16 fg_off; | |
1010 | u16 datagram_label; | |
1011 | u16 lf; | |
1012 | struct sk_buff *skb; | |
1013 | ||
1014 | /* Update the ptask to point to the next fragment and send it */ | |
f91e3bd8 | 1015 | lf = fwnet_get_hdr_lf(&ptask->hdr); |
c76acec6 | 1016 | switch (lf) { |
f91e3bd8 SR |
1017 | case RFC2374_HDR_LASTFRAG: |
1018 | case RFC2374_HDR_UNFRAG: | |
c76acec6 | 1019 | default: |
f91e3bd8 SR |
1020 | fw_error("Outstanding packet %x lf %x, header %x,%x\n", |
1021 | ptask->outstanding_pkts, lf, ptask->hdr.w0, | |
1022 | ptask->hdr.w1); | |
c76acec6 JF |
1023 | BUG(); |
1024 | ||
f91e3bd8 | 1025 | case RFC2374_HDR_FIRSTFRAG: |
c76acec6 | 1026 | /* Set frag type here for future interior fragments */ |
f91e3bd8 SR |
1027 | dg_size = fwnet_get_hdr_dg_size(&ptask->hdr); |
1028 | fg_off = ptask->max_payload - RFC2374_FRAG_HDR_SIZE; | |
1029 | datagram_label = fwnet_get_hdr_dgl(&ptask->hdr); | |
c76acec6 JF |
1030 | break; |
1031 | ||
f91e3bd8 SR |
1032 | case RFC2374_HDR_INTFRAG: |
1033 | dg_size = fwnet_get_hdr_dg_size(&ptask->hdr); | |
1034 | fg_off = fwnet_get_hdr_fg_off(&ptask->hdr) | |
1035 | + ptask->max_payload - RFC2374_FRAG_HDR_SIZE; | |
1036 | datagram_label = fwnet_get_hdr_dgl(&ptask->hdr); | |
c76acec6 JF |
1037 | break; |
1038 | } | |
1039 | skb = ptask->skb; | |
f91e3bd8 SR |
1040 | skb_pull(skb, ptask->max_payload); |
1041 | if (ptask->outstanding_pkts > 1) { | |
1042 | fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG, | |
1043 | dg_size, fg_off, datagram_label); | |
c76acec6 | 1044 | } else { |
f91e3bd8 SR |
1045 | fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG, |
1046 | dg_size, fg_off, datagram_label); | |
1047 | ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE; | |
c76acec6 | 1048 | } |
f91e3bd8 | 1049 | fwnet_send_packet(ptask); |
c76acec6 | 1050 | } else { |
f91e3bd8 SR |
1051 | dev_kfree_skb_any(ptask->skb); |
1052 | kmem_cache_free(fwnet_packet_task_cache, ptask); | |
c76acec6 JF |
1053 | } |
1054 | } | |
1055 | ||
f91e3bd8 SR |
1056 | static void fwnet_write_complete(struct fw_card *card, int rcode, |
1057 | void *payload, size_t length, void *data) | |
1058 | { | |
1059 | struct fwnet_packet_task *ptask; | |
c76acec6 JF |
1060 | |
1061 | ptask = data; | |
c76acec6 | 1062 | |
f91e3bd8 SR |
1063 | if (rcode == RCODE_COMPLETE) |
1064 | fwnet_transmit_packet_done(ptask); | |
1065 | else | |
1066 | fw_error("fwnet_write_complete: failed: %x\n", rcode); | |
c76acec6 | 1067 | /* ??? error recovery */ |
c76acec6 JF |
1068 | } |
1069 | ||
f91e3bd8 SR |
1070 | static int fwnet_send_packet(struct fwnet_packet_task *ptask) |
1071 | { | |
1072 | struct fwnet_device *dev; | |
c76acec6 | 1073 | unsigned tx_len; |
f91e3bd8 | 1074 | struct rfc2734_header *bufhdr; |
c76acec6 | 1075 | unsigned long flags; |
f91e3bd8 | 1076 | struct net_device *net; |
c76acec6 | 1077 | |
f91e3bd8 | 1078 | dev = ptask->dev; |
c76acec6 | 1079 | tx_len = ptask->max_payload; |
f91e3bd8 SR |
1080 | switch (fwnet_get_hdr_lf(&ptask->hdr)) { |
1081 | case RFC2374_HDR_UNFRAG: | |
1082 | bufhdr = (struct rfc2734_header *) | |
1083 | skb_push(ptask->skb, RFC2374_UNFRAG_HDR_SIZE); | |
1084 | put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0); | |
c76acec6 JF |
1085 | break; |
1086 | ||
f91e3bd8 SR |
1087 | case RFC2374_HDR_FIRSTFRAG: |
1088 | case RFC2374_HDR_INTFRAG: | |
1089 | case RFC2374_HDR_LASTFRAG: | |
1090 | bufhdr = (struct rfc2734_header *) | |
1091 | skb_push(ptask->skb, RFC2374_FRAG_HDR_SIZE); | |
1092 | put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0); | |
1093 | put_unaligned_be32(ptask->hdr.w1, &bufhdr->w1); | |
c76acec6 JF |
1094 | break; |
1095 | ||
1096 | default: | |
1097 | BUG(); | |
1098 | } | |
f91e3bd8 SR |
1099 | if (ptask->dest_node == IEEE1394_ALL_NODES) { |
1100 | u8 *p; | |
c76acec6 | 1101 | int generation; |
f91e3bd8 | 1102 | int node_id; |
c76acec6 JF |
1103 | |
1104 | /* ptask->generation may not have been set yet */ | |
f91e3bd8 | 1105 | generation = dev->card->generation; |
c76acec6 | 1106 | smp_rmb(); |
f91e3bd8 SR |
1107 | node_id = dev->card->node_id; |
1108 | ||
1109 | p = skb_push(ptask->skb, 8); | |
1110 | put_unaligned_be32(node_id << 16 | IANA_SPECIFIER_ID >> 8, p); | |
1111 | put_unaligned_be32((IANA_SPECIFIER_ID & 0xff) << 24 | |
1112 | | RFC2734_SW_VERSION, &p[4]); | |
1113 | ||
1114 | /* We should not transmit if broadcast_channel.valid == 0. */ | |
1115 | fw_send_request(dev->card, &ptask->transaction, | |
1116 | TCODE_STREAM_DATA, | |
1117 | fw_stream_packet_destination_id(3, | |
1118 | IEEE1394_BROADCAST_CHANNEL, 0), | |
1119 | generation, SCODE_100, 0ULL, ptask->skb->data, | |
1120 | tx_len + 8, fwnet_write_complete, ptask); | |
1121 | ||
1122 | /* FIXME race? */ | |
1123 | spin_lock_irqsave(&dev->lock, flags); | |
1124 | list_add_tail(&ptask->pt_link, &dev->broadcasted_list); | |
1125 | spin_unlock_irqrestore(&dev->lock, flags); | |
1126 | ||
c76acec6 | 1127 | return 0; |
c76acec6 | 1128 | } |
f91e3bd8 SR |
1129 | |
1130 | fw_send_request(dev->card, &ptask->transaction, | |
1131 | TCODE_WRITE_BLOCK_REQUEST, ptask->dest_node, | |
1132 | ptask->generation, ptask->speed, ptask->fifo_addr, | |
1133 | ptask->skb->data, tx_len, fwnet_write_complete, ptask); | |
1134 | ||
1135 | /* FIXME race? */ | |
1136 | spin_lock_irqsave(&dev->lock, flags); | |
1137 | list_add_tail(&ptask->pt_link, &dev->sent_list); | |
1138 | spin_unlock_irqrestore(&dev->lock, flags); | |
1139 | ||
1140 | net = dev->card->netdev; | |
1141 | net->trans_start = jiffies; | |
1142 | ||
c76acec6 JF |
1143 | return 0; |
1144 | } | |
1145 | ||
f91e3bd8 SR |
1146 | static int fwnet_broadcast_start(struct fwnet_device *dev) |
1147 | { | |
c76acec6 JF |
1148 | struct fw_iso_context *context; |
1149 | int retval; | |
1150 | unsigned num_packets; | |
1151 | unsigned max_receive; | |
1152 | struct fw_iso_packet packet; | |
1153 | unsigned long offset; | |
1154 | unsigned u; | |
c76acec6 | 1155 | |
f91e3bd8 SR |
1156 | if (dev->local_fifo == FWNET_NO_FIFO_ADDR) { |
1157 | /* outside OHCI posted write area? */ | |
1158 | static const struct fw_address_region region = { | |
1159 | .start = 0xffff00000000ULL, | |
1160 | .end = CSR_REGISTER_BASE, | |
1161 | }; | |
1162 | ||
1163 | dev->handler.length = 4096; | |
1164 | dev->handler.address_callback = fwnet_receive_packet; | |
1165 | dev->handler.callback_data = dev; | |
1166 | ||
1167 | retval = fw_core_add_address_handler(&dev->handler, ®ion); | |
1168 | if (retval < 0) | |
c76acec6 | 1169 | goto failed_initial; |
f91e3bd8 SR |
1170 | |
1171 | dev->local_fifo = dev->handler.offset; | |
c76acec6 JF |
1172 | } |
1173 | ||
f91e3bd8 SR |
1174 | max_receive = 1U << (dev->card->max_receive + 1); |
1175 | num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive; | |
1176 | ||
1177 | if (!dev->broadcast_rcv_context) { | |
c76acec6 JF |
1178 | void **ptrptr; |
1179 | ||
f91e3bd8 SR |
1180 | context = fw_iso_context_create(dev->card, |
1181 | FW_ISO_CONTEXT_RECEIVE, IEEE1394_BROADCAST_CHANNEL, | |
1182 | dev->card->link_speed, 8, fwnet_receive_broadcast, dev); | |
c76acec6 JF |
1183 | if (IS_ERR(context)) { |
1184 | retval = PTR_ERR(context); | |
1185 | goto failed_context_create; | |
1186 | } | |
f91e3bd8 SR |
1187 | |
1188 | retval = fw_iso_buffer_init(&dev->broadcast_rcv_buffer, | |
1189 | dev->card, FWNET_ISO_PAGE_COUNT, DMA_FROM_DEVICE); | |
1190 | if (retval < 0) | |
c76acec6 | 1191 | goto failed_buffer_init; |
f91e3bd8 SR |
1192 | |
1193 | ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL); | |
1194 | if (!ptrptr) { | |
c76acec6 JF |
1195 | retval = -ENOMEM; |
1196 | goto failed_ptrs_alloc; | |
1197 | } | |
f91e3bd8 SR |
1198 | |
1199 | dev->broadcast_rcv_buffer_ptrs = ptrptr; | |
1200 | for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) { | |
c76acec6 JF |
1201 | void *ptr; |
1202 | unsigned v; | |
1203 | ||
f91e3bd8 SR |
1204 | ptr = kmap(dev->broadcast_rcv_buffer.pages[u]); |
1205 | for (v = 0; v < num_packets / FWNET_ISO_PAGE_COUNT; v++) | |
1206 | *ptrptr++ = (void *) | |
1207 | ((char *)ptr + v * max_receive); | |
c76acec6 | 1208 | } |
f91e3bd8 SR |
1209 | dev->broadcast_rcv_context = context; |
1210 | } else { | |
1211 | context = dev->broadcast_rcv_context; | |
1212 | } | |
c76acec6 JF |
1213 | |
1214 | packet.payload_length = max_receive; | |
1215 | packet.interrupt = 1; | |
1216 | packet.skip = 0; | |
1217 | packet.tag = 3; | |
1218 | packet.sy = 0; | |
f91e3bd8 | 1219 | packet.header_length = IEEE1394_GASP_HDR_SIZE; |
c76acec6 | 1220 | offset = 0; |
f91e3bd8 SR |
1221 | |
1222 | for (u = 0; u < num_packets; u++) { | |
1223 | retval = fw_iso_context_queue(context, &packet, | |
1224 | &dev->broadcast_rcv_buffer, offset); | |
1225 | if (retval < 0) | |
c76acec6 | 1226 | goto failed_rcv_queue; |
f91e3bd8 | 1227 | |
c76acec6 JF |
1228 | offset += max_receive; |
1229 | } | |
f91e3bd8 SR |
1230 | dev->num_broadcast_rcv_ptrs = num_packets; |
1231 | dev->rcv_buffer_size = max_receive; | |
1232 | dev->broadcast_rcv_next_ptr = 0U; | |
1233 | retval = fw_iso_context_start(context, -1, 0, | |
1234 | FW_ISO_CONTEXT_MATCH_ALL_TAGS); /* ??? sync */ | |
1235 | if (retval < 0) | |
c76acec6 | 1236 | goto failed_rcv_queue; |
f91e3bd8 SR |
1237 | |
1238 | /* FIXME: adjust it according to the min. speed of all known peers? */ | |
1239 | dev->broadcast_xmt_max_payload = IEEE1394_MAX_PAYLOAD_S100 | |
1240 | - IEEE1394_GASP_HDR_SIZE - RFC2374_UNFRAG_HDR_SIZE; | |
1241 | dev->broadcast_state = FWNET_BROADCAST_RUNNING; | |
1242 | ||
c76acec6 JF |
1243 | return 0; |
1244 | ||
1245 | failed_rcv_queue: | |
f91e3bd8 SR |
1246 | kfree(dev->broadcast_rcv_buffer_ptrs); |
1247 | dev->broadcast_rcv_buffer_ptrs = NULL; | |
c76acec6 | 1248 | failed_ptrs_alloc: |
f91e3bd8 | 1249 | fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, dev->card); |
c76acec6 | 1250 | failed_buffer_init: |
f91e3bd8 SR |
1251 | fw_iso_context_destroy(context); |
1252 | dev->broadcast_rcv_context = NULL; | |
c76acec6 | 1253 | failed_context_create: |
f91e3bd8 | 1254 | fw_core_remove_address_handler(&dev->handler); |
c76acec6 | 1255 | failed_initial: |
f91e3bd8 SR |
1256 | dev->local_fifo = FWNET_NO_FIFO_ADDR; |
1257 | ||
c76acec6 JF |
1258 | return retval; |
1259 | } | |
1260 | ||
f91e3bd8 SR |
1261 | /* ifup */ |
1262 | static int fwnet_open(struct net_device *net) | |
1263 | { | |
1264 | struct fwnet_device *dev = netdev_priv(net); | |
c76acec6 JF |
1265 | int ret; |
1266 | ||
f91e3bd8 SR |
1267 | if (dev->broadcast_state == FWNET_BROADCAST_ERROR) { |
1268 | ret = fwnet_broadcast_start(dev); | |
c76acec6 JF |
1269 | if (ret) |
1270 | return ret; | |
1271 | } | |
f91e3bd8 SR |
1272 | netif_start_queue(net); |
1273 | ||
c76acec6 JF |
1274 | return 0; |
1275 | } | |
1276 | ||
f91e3bd8 SR |
1277 | /* ifdown */ |
1278 | static int fwnet_stop(struct net_device *net) | |
c76acec6 | 1279 | { |
f91e3bd8 SR |
1280 | netif_stop_queue(net); |
1281 | ||
1282 | /* Deallocate iso context for use by other applications? */ | |
c76acec6 | 1283 | |
c76acec6 JF |
1284 | return 0; |
1285 | } | |
1286 | ||
f91e3bd8 | 1287 | static int fwnet_tx(struct sk_buff *skb, struct net_device *net) |
c76acec6 | 1288 | { |
f91e3bd8 SR |
1289 | struct fwnet_header hdr_buf; |
1290 | struct fwnet_device *dev = netdev_priv(net); | |
c76acec6 JF |
1291 | __be16 proto; |
1292 | u16 dest_node; | |
c76acec6 JF |
1293 | unsigned max_payload; |
1294 | u16 dg_size; | |
1295 | u16 *datagram_label_ptr; | |
f91e3bd8 SR |
1296 | struct fwnet_packet_task *ptask; |
1297 | struct fwnet_peer *peer = NULL; | |
c76acec6 | 1298 | |
f91e3bd8 | 1299 | ptask = kmem_cache_alloc(fwnet_packet_task_cache, GFP_ATOMIC); |
c76acec6 JF |
1300 | if (ptask == NULL) |
1301 | goto fail; | |
1302 | ||
1303 | skb = skb_share_check(skb, GFP_ATOMIC); | |
1304 | if (!skb) | |
1305 | goto fail; | |
1306 | ||
1307 | /* | |
f91e3bd8 | 1308 | * Make a copy of the driver-specific header. |
c76acec6 JF |
1309 | * We might need to rebuild the header on tx failure. |
1310 | */ | |
1311 | memcpy(&hdr_buf, skb->data, sizeof(hdr_buf)); | |
1312 | skb_pull(skb, sizeof(hdr_buf)); | |
1313 | ||
1314 | proto = hdr_buf.h_proto; | |
1315 | dg_size = skb->len; | |
1316 | ||
1317 | /* | |
1318 | * Set the transmission type for the packet. ARP packets and IP | |
1319 | * broadcast packets are sent via GASP. | |
1320 | */ | |
f91e3bd8 | 1321 | if (memcmp(hdr_buf.h_dest, net->broadcast, FWNET_ALEN) == 0 |
c76acec6 | 1322 | || proto == htons(ETH_P_ARP) |
f91e3bd8 SR |
1323 | || (proto == htons(ETH_P_IP) |
1324 | && IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) { | |
1325 | max_payload = dev->broadcast_xmt_max_payload; | |
1326 | datagram_label_ptr = &dev->broadcast_xmt_datagramlabel; | |
1327 | ||
1328 | ptask->fifo_addr = FWNET_NO_FIFO_ADDR; | |
1329 | ptask->generation = 0; | |
1330 | ptask->dest_node = IEEE1394_ALL_NODES; | |
1331 | ptask->speed = SCODE_100; | |
c76acec6 | 1332 | } else { |
f91e3bd8 | 1333 | __be64 guid = get_unaligned((__be64 *)hdr_buf.h_dest); |
c76acec6 JF |
1334 | u8 generation; |
1335 | ||
f91e3bd8 SR |
1336 | peer = fwnet_peer_find_by_guid(dev, be64_to_cpu(guid)); |
1337 | if (!peer) | |
c76acec6 | 1338 | goto fail; |
c76acec6 | 1339 | |
f91e3bd8 | 1340 | if (peer->fifo == FWNET_NO_FIFO_ADDR) |
c76acec6 | 1341 | goto fail; |
c76acec6 | 1342 | |
f91e3bd8 SR |
1343 | generation = peer->generation; |
1344 | smp_rmb(); | |
1345 | dest_node = peer->node_id; | |
1346 | ||
1347 | max_payload = peer->max_payload; | |
1348 | datagram_label_ptr = &peer->datagram_label; | |
c76acec6 | 1349 | |
f91e3bd8 | 1350 | ptask->fifo_addr = peer->fifo; |
c76acec6 JF |
1351 | ptask->generation = generation; |
1352 | ptask->dest_node = dest_node; | |
f91e3bd8 | 1353 | ptask->speed = peer->xmt_speed; |
c76acec6 JF |
1354 | } |
1355 | ||
1356 | /* If this is an ARP packet, convert it */ | |
1357 | if (proto == htons(ETH_P_ARP)) { | |
c76acec6 JF |
1358 | struct arphdr *arp = (struct arphdr *)skb->data; |
1359 | unsigned char *arp_ptr = (unsigned char *)(arp + 1); | |
f91e3bd8 SR |
1360 | struct rfc2734_arp *arp1394 = (struct rfc2734_arp *)skb->data; |
1361 | __be32 ipaddr; | |
1362 | ||
1363 | ipaddr = get_unaligned((__be32 *)(arp_ptr + FWNET_ALEN)); | |
1364 | ||
1365 | arp1394->hw_addr_len = RFC2734_HW_ADDR_LEN; | |
1366 | arp1394->max_rec = dev->card->max_receive; | |
1367 | arp1394->sspd = dev->card->link_speed; | |
1368 | ||
1369 | put_unaligned_be16(dev->local_fifo >> 32, | |
1370 | &arp1394->fifo_hi); | |
1371 | put_unaligned_be32(dev->local_fifo & 0xffffffff, | |
1372 | &arp1394->fifo_lo); | |
1373 | put_unaligned(ipaddr, &arp1394->sip); | |
c76acec6 | 1374 | } |
c76acec6 JF |
1375 | |
1376 | ptask->hdr.w0 = 0; | |
1377 | ptask->hdr.w1 = 0; | |
1378 | ptask->skb = skb; | |
f91e3bd8 SR |
1379 | ptask->dev = dev; |
1380 | ||
c76acec6 | 1381 | /* Does it all fit in one packet? */ |
f91e3bd8 SR |
1382 | if (dg_size <= max_payload) { |
1383 | fwnet_make_uf_hdr(&ptask->hdr, ntohs(proto)); | |
c76acec6 | 1384 | ptask->outstanding_pkts = 1; |
f91e3bd8 | 1385 | max_payload = dg_size + RFC2374_UNFRAG_HDR_SIZE; |
c76acec6 JF |
1386 | } else { |
1387 | u16 datagram_label; | |
1388 | ||
f91e3bd8 | 1389 | max_payload -= RFC2374_FRAG_OVERHEAD; |
c76acec6 | 1390 | datagram_label = (*datagram_label_ptr)++; |
f91e3bd8 SR |
1391 | fwnet_make_ff_hdr(&ptask->hdr, ntohs(proto), dg_size, |
1392 | datagram_label); | |
c76acec6 | 1393 | ptask->outstanding_pkts = DIV_ROUND_UP(dg_size, max_payload); |
f91e3bd8 | 1394 | max_payload += RFC2374_FRAG_HDR_SIZE; |
c76acec6 JF |
1395 | } |
1396 | ptask->max_payload = max_payload; | |
f91e3bd8 SR |
1397 | fwnet_send_packet(ptask); |
1398 | ||
c76acec6 JF |
1399 | return NETDEV_TX_OK; |
1400 | ||
1401 | fail: | |
1402 | if (ptask) | |
f91e3bd8 | 1403 | kmem_cache_free(fwnet_packet_task_cache, ptask); |
c76acec6 JF |
1404 | |
1405 | if (skb != NULL) | |
1406 | dev_kfree_skb(skb); | |
1407 | ||
f91e3bd8 SR |
1408 | net->stats.tx_dropped++; |
1409 | net->stats.tx_errors++; | |
c76acec6 JF |
1410 | |
1411 | /* | |
1412 | * FIXME: According to a patch from 2003-02-26, "returning non-zero | |
1413 | * causes serious problems" here, allegedly. Before that patch, | |
1414 | * -ERRNO was returned which is not appropriate under Linux 2.6. | |
1415 | * Perhaps more needs to be done? Stop the queue in serious | |
1416 | * conditions and restart it elsewhere? | |
1417 | */ | |
1418 | return NETDEV_TX_OK; | |
1419 | } | |
1420 | ||
f91e3bd8 SR |
1421 | static void fwnet_tx_timeout(struct net_device *net) |
1422 | { | |
1423 | fw_error("%s: timeout\n", net->name); | |
c76acec6 | 1424 | |
f91e3bd8 | 1425 | /* FIXME: What to do if we timeout? */ |
c76acec6 JF |
1426 | } |
1427 | ||
f91e3bd8 SR |
1428 | static int fwnet_change_mtu(struct net_device *net, int new_mtu) |
1429 | { | |
c76acec6 JF |
1430 | if (new_mtu < 68) |
1431 | return -EINVAL; | |
1432 | ||
f91e3bd8 | 1433 | net->mtu = new_mtu; |
c76acec6 JF |
1434 | return 0; |
1435 | } | |
1436 | ||
f91e3bd8 SR |
1437 | static void fwnet_get_drvinfo(struct net_device *net, |
1438 | struct ethtool_drvinfo *info) | |
1439 | { | |
1440 | strcpy(info->driver, KBUILD_MODNAME); | |
1441 | strcpy(info->bus_info, "ieee1394"); | |
c76acec6 JF |
1442 | } |
1443 | ||
f91e3bd8 SR |
1444 | static struct ethtool_ops fwnet_ethtool_ops = { |
1445 | .get_drvinfo = fwnet_get_drvinfo, | |
c76acec6 JF |
1446 | }; |
1447 | ||
f91e3bd8 SR |
1448 | static const struct net_device_ops fwnet_netdev_ops = { |
1449 | .ndo_open = fwnet_open, | |
1450 | .ndo_stop = fwnet_stop, | |
1451 | .ndo_start_xmit = fwnet_tx, | |
1452 | .ndo_tx_timeout = fwnet_tx_timeout, | |
1453 | .ndo_change_mtu = fwnet_change_mtu, | |
c76acec6 JF |
1454 | }; |
1455 | ||
f91e3bd8 SR |
1456 | static void fwnet_init_dev(struct net_device *net) |
1457 | { | |
1458 | net->header_ops = &fwnet_header_ops; | |
1459 | net->netdev_ops = &fwnet_netdev_ops; | |
1460 | net->watchdog_timeo = 100000; /* ? FIXME */ | |
1461 | net->flags = IFF_BROADCAST | IFF_MULTICAST; | |
1462 | net->features = NETIF_F_HIGHDMA; | |
1463 | net->addr_len = FWNET_ALEN; | |
1464 | net->hard_header_len = FWNET_HLEN; | |
1465 | net->type = ARPHRD_IEEE1394; | |
1466 | net->tx_queue_len = 1000; /* ? FIXME */ | |
1467 | SET_ETHTOOL_OPS(net, &fwnet_ethtool_ops); | |
c76acec6 JF |
1468 | } |
1469 | ||
f91e3bd8 SR |
1470 | /* FIXME create netdev upon first fw_unit of a card, not upon local fw_unit */ |
1471 | static int fwnet_probe(struct device *_dev) | |
1472 | { | |
1473 | struct fw_unit *unit = fw_unit(_dev); | |
1474 | struct fw_device *device = fw_parent_device(unit); | |
1475 | struct fw_card *card = device->card; | |
1476 | struct net_device *net; | |
1477 | struct fwnet_device *dev; | |
c76acec6 | 1478 | unsigned max_mtu; |
c76acec6 | 1479 | |
f91e3bd8 | 1480 | if (!device->is_local) { |
c76acec6 JF |
1481 | int added; |
1482 | ||
f91e3bd8 | 1483 | added = fwnet_peer_new(card, device); |
c76acec6 JF |
1484 | return added; |
1485 | } | |
f91e3bd8 SR |
1486 | net = alloc_netdev(sizeof(*dev), "firewire%d", fwnet_init_dev); |
1487 | if (net == NULL) { | |
1488 | fw_error("out of memory\n"); | |
c76acec6 JF |
1489 | goto out; |
1490 | } | |
1491 | ||
f91e3bd8 SR |
1492 | SET_NETDEV_DEV(net, card->device); |
1493 | dev = netdev_priv(net); | |
c76acec6 | 1494 | |
f91e3bd8 SR |
1495 | spin_lock_init(&dev->lock); |
1496 | dev->broadcast_state = FWNET_BROADCAST_ERROR; | |
1497 | dev->broadcast_rcv_context = NULL; | |
1498 | dev->broadcast_xmt_max_payload = 0; | |
1499 | dev->broadcast_xmt_datagramlabel = 0; | |
c76acec6 | 1500 | |
f91e3bd8 | 1501 | dev->local_fifo = FWNET_NO_FIFO_ADDR; |
c76acec6 | 1502 | |
f91e3bd8 SR |
1503 | /* INIT_WORK(&dev->wake, fwnet_handle_queue);*/ |
1504 | INIT_LIST_HEAD(&dev->packet_list); | |
1505 | INIT_LIST_HEAD(&dev->broadcasted_list); | |
1506 | INIT_LIST_HEAD(&dev->sent_list); | |
c76acec6 | 1507 | |
f91e3bd8 | 1508 | dev->card = card; |
c76acec6 JF |
1509 | |
1510 | /* | |
1511 | * Use the RFC 2734 default 1500 octets or the maximum payload | |
1512 | * as initial MTU | |
1513 | */ | |
1514 | max_mtu = (1 << (card->max_receive + 1)) | |
f91e3bd8 SR |
1515 | - sizeof(struct rfc2734_header) - IEEE1394_GASP_HDR_SIZE; |
1516 | net->mtu = min(1500U, max_mtu); | |
c76acec6 JF |
1517 | |
1518 | /* Set our hardware address while we're at it */ | |
f91e3bd8 SR |
1519 | put_unaligned_be64(card->guid, net->dev_addr); |
1520 | put_unaligned_be64(~0ULL, net->broadcast); | |
1521 | if (register_netdev(net)) { | |
1522 | fw_error("Cannot register the driver\n"); | |
c76acec6 JF |
1523 | goto out; |
1524 | } | |
1525 | ||
f91e3bd8 SR |
1526 | fw_notify("%s: IPv4 over FireWire on device %016llx\n", |
1527 | net->name, (unsigned long long)card->guid); | |
1528 | card->netdev = net; | |
c76acec6 | 1529 | |
f91e3bd8 | 1530 | return 0; |
c76acec6 | 1531 | out: |
f91e3bd8 SR |
1532 | if (net) |
1533 | free_netdev(net); | |
1534 | ||
c76acec6 JF |
1535 | return -ENOENT; |
1536 | } | |
1537 | ||
f91e3bd8 SR |
1538 | static int fwnet_remove(struct device *_dev) |
1539 | { | |
1540 | struct fw_unit *unit = fw_unit(_dev); | |
1541 | struct fw_device *device = fw_parent_device(unit); | |
1542 | struct fw_card *card = device->card; | |
1543 | struct net_device *net; | |
1544 | struct fwnet_device *dev; | |
1545 | struct fwnet_peer *peer; | |
1546 | struct fwnet_partial_datagram *pd, *pd_next; | |
1547 | struct fwnet_packet_task *ptask, *pt_next; | |
1548 | ||
1549 | if (!device->is_local) { | |
1550 | fwnet_peer_delete(card, device); | |
c76acec6 | 1551 | |
c76acec6 JF |
1552 | return 0; |
1553 | } | |
f91e3bd8 SR |
1554 | |
1555 | net = card->netdev; | |
1556 | if (net) { | |
1557 | dev = netdev_priv(net); | |
1558 | unregister_netdev(net); | |
1559 | ||
1560 | if (dev->local_fifo != FWNET_NO_FIFO_ADDR) | |
1561 | fw_core_remove_address_handler(&dev->handler); | |
1562 | if (dev->broadcast_rcv_context) { | |
1563 | fw_iso_context_stop(dev->broadcast_rcv_context); | |
1564 | fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, | |
1565 | dev->card); | |
1566 | fw_iso_context_destroy(dev->broadcast_rcv_context); | |
c76acec6 | 1567 | } |
f91e3bd8 SR |
1568 | list_for_each_entry_safe(ptask, pt_next, |
1569 | &dev->packet_list, pt_link) { | |
1570 | dev_kfree_skb_any(ptask->skb); | |
1571 | kmem_cache_free(fwnet_packet_task_cache, ptask); | |
c76acec6 | 1572 | } |
f91e3bd8 SR |
1573 | list_for_each_entry_safe(ptask, pt_next, |
1574 | &dev->broadcasted_list, pt_link) { | |
1575 | dev_kfree_skb_any(ptask->skb); | |
1576 | kmem_cache_free(fwnet_packet_task_cache, ptask); | |
c76acec6 | 1577 | } |
f91e3bd8 SR |
1578 | list_for_each_entry_safe(ptask, pt_next, |
1579 | &dev->sent_list, pt_link) { | |
1580 | dev_kfree_skb_any(ptask->skb); | |
1581 | kmem_cache_free(fwnet_packet_task_cache, ptask); | |
c76acec6 | 1582 | } |
f91e3bd8 SR |
1583 | list_for_each_entry(peer, &card->peer_list, peer_link) { |
1584 | if (peer->pdg_size) { | |
1585 | list_for_each_entry_safe(pd, pd_next, | |
1586 | &peer->pd_list, pd_link) | |
1587 | fwnet_pd_delete(pd); | |
1588 | peer->pdg_size = 0; | |
c76acec6 | 1589 | } |
f91e3bd8 | 1590 | peer->fifo = FWNET_NO_FIFO_ADDR; |
c76acec6 | 1591 | } |
f91e3bd8 | 1592 | free_netdev(net); |
c76acec6 | 1593 | card->netdev = NULL; |
c76acec6 | 1594 | } |
f91e3bd8 | 1595 | |
c76acec6 JF |
1596 | return 0; |
1597 | } | |
1598 | ||
f91e3bd8 SR |
1599 | /* |
1600 | * FIXME abort partially sent fragmented datagrams, | |
1601 | * discard partially received fragmented datagrams | |
1602 | */ | |
1603 | static void fwnet_update(struct fw_unit *unit) | |
1604 | { | |
1605 | struct fw_device *device = fw_parent_device(unit); | |
1606 | struct net_device *net = device->card->netdev; | |
1607 | struct fwnet_device *dev; | |
1608 | struct fwnet_peer *peer; | |
1609 | u64 guid; | |
c76acec6 | 1610 | |
f91e3bd8 SR |
1611 | if (net && !device->is_local) { |
1612 | dev = netdev_priv(net); | |
1613 | guid = (u64)device->config_rom[3] << 32 | device->config_rom[4]; | |
1614 | peer = fwnet_peer_find_by_guid(dev, guid); | |
1615 | if (!peer) { | |
1616 | fw_error("fwnet_update: no peer for device %016llx\n", | |
1617 | (unsigned long long)guid); | |
1618 | return; | |
1619 | } | |
1620 | peer->generation = device->generation; | |
1621 | rmb(); | |
1622 | peer->node_id = device->node_id; | |
c76acec6 JF |
1623 | } |
1624 | } | |
1625 | ||
f91e3bd8 SR |
1626 | static const struct ieee1394_device_id fwnet_id_table[] = { |
1627 | { | |
1628 | .match_flags = IEEE1394_MATCH_SPECIFIER_ID | | |
1629 | IEEE1394_MATCH_VERSION, | |
1630 | .specifier_id = IANA_SPECIFIER_ID, | |
1631 | .version = RFC2734_SW_VERSION, | |
1632 | }, | |
1633 | { } | |
1634 | }; | |
1635 | ||
1636 | static struct fw_driver fwnet_driver = { | |
c76acec6 | 1637 | .driver = { |
f91e3bd8 SR |
1638 | .owner = THIS_MODULE, |
1639 | .name = "net", | |
1640 | .bus = &fw_bus_type, | |
1641 | .probe = fwnet_probe, | |
1642 | .remove = fwnet_remove, | |
c76acec6 | 1643 | }, |
f91e3bd8 SR |
1644 | .update = fwnet_update, |
1645 | .id_table = fwnet_id_table, | |
1646 | }; | |
1647 | ||
1648 | static const u32 rfc2374_unit_directory_data[] = { | |
1649 | 0x00040000, /* directory_length */ | |
1650 | 0x1200005e, /* unit_specifier_id: IANA */ | |
1651 | 0x81000003, /* textual descriptor offset */ | |
1652 | 0x13000001, /* unit_sw_version: RFC 2734 */ | |
1653 | 0x81000005, /* textual descriptor offset */ | |
1654 | 0x00030000, /* descriptor_length */ | |
1655 | 0x00000000, /* text */ | |
1656 | 0x00000000, /* minimal ASCII, en */ | |
1657 | 0x49414e41, /* I A N A */ | |
1658 | 0x00030000, /* descriptor_length */ | |
1659 | 0x00000000, /* text */ | |
1660 | 0x00000000, /* minimal ASCII, en */ | |
1661 | 0x49507634, /* I P v 4 */ | |
1662 | }; | |
1663 | ||
1664 | static struct fw_descriptor rfc2374_unit_directory = { | |
1665 | .length = ARRAY_SIZE(rfc2374_unit_directory_data), | |
1666 | .key = (CSR_DIRECTORY | CSR_UNIT) << 24, | |
1667 | .data = rfc2374_unit_directory_data | |
c76acec6 JF |
1668 | }; |
1669 | ||
f91e3bd8 SR |
1670 | static int __init fwnet_init(void) |
1671 | { | |
1672 | int err; | |
1673 | ||
1674 | err = fw_core_add_descriptor(&rfc2374_unit_directory); | |
1675 | if (err) | |
1676 | return err; | |
c76acec6 | 1677 | |
f91e3bd8 SR |
1678 | fwnet_packet_task_cache = kmem_cache_create("packet_task", |
1679 | sizeof(struct fwnet_packet_task), 0, 0, NULL); | |
1680 | if (!fwnet_packet_task_cache) { | |
1681 | err = -ENOMEM; | |
1682 | goto out; | |
1683 | } | |
1684 | ||
1685 | err = driver_register(&fwnet_driver.driver); | |
1686 | if (!err) | |
1687 | return 0; | |
1688 | ||
1689 | kmem_cache_destroy(fwnet_packet_task_cache); | |
1690 | out: | |
1691 | fw_core_remove_descriptor(&rfc2374_unit_directory); | |
1692 | ||
1693 | return err; | |
c76acec6 | 1694 | } |
f91e3bd8 | 1695 | module_init(fwnet_init); |
c76acec6 | 1696 | |
f91e3bd8 SR |
1697 | static void __exit fwnet_cleanup(void) |
1698 | { | |
1699 | driver_unregister(&fwnet_driver.driver); | |
1700 | kmem_cache_destroy(fwnet_packet_task_cache); | |
1701 | fw_core_remove_descriptor(&rfc2374_unit_directory); | |
c76acec6 | 1702 | } |
f91e3bd8 | 1703 | module_exit(fwnet_cleanup); |
c76acec6 | 1704 | |
f91e3bd8 SR |
1705 | MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>"); |
1706 | MODULE_DESCRIPTION("IPv4 over IEEE1394 as per RFC 2734"); | |
1707 | MODULE_LICENSE("GPL"); | |
1708 | MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table); |