Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
efbeccf1 | 2 | * eth1394.c -- IPv4 driver for Linux IEEE-1394 Subsystem |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2001-2003 Ben Collins <bcollins@debian.org> | |
5 | * 2000 Bonin Franck <boninf@free.fr> | |
6 | * 2003 Steve Kinneberg <kinnebergsteve@acmsystems.com> | |
7 | * | |
8 | * Mainly based on work by Emanuel Pirker and Andreas E. Bombe | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software Foundation, | |
22 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
23 | */ | |
24 | ||
efbeccf1 SR |
25 | /* |
26 | * This driver intends to support RFC 2734, which describes a method for | |
27 | * transporting IPv4 datagrams over IEEE-1394 serial busses. | |
1da177e4 LT |
28 | * |
29 | * TODO: | |
30 | * RFC 2734 related: | |
31 | * - Add MCAP. Limited Multicast exists only to 224.0.0.1 and 224.0.0.2. | |
32 | * | |
33 | * Non-RFC 2734 related: | |
34 | * - Handle fragmented skb's coming from the networking layer. | |
35 | * - Move generic GASP reception to core 1394 code | |
36 | * - Convert kmalloc/kfree for link fragments to use kmem_cache_* instead | |
37 | * - Stability improvements | |
38 | * - Performance enhancements | |
39 | * - Consider garbage collecting old partial datagrams after X amount of time | |
40 | */ | |
41 | ||
1da177e4 LT |
42 | #include <linux/module.h> |
43 | ||
1da177e4 LT |
44 | #include <linux/kernel.h> |
45 | #include <linux/slab.h> | |
46 | #include <linux/errno.h> | |
47 | #include <linux/types.h> | |
48 | #include <linux/delay.h> | |
49 | #include <linux/init.h> | |
50 | ||
51 | #include <linux/netdevice.h> | |
52 | #include <linux/inetdevice.h> | |
1da177e4 LT |
53 | #include <linux/if_arp.h> |
54 | #include <linux/if_ether.h> | |
55 | #include <linux/ip.h> | |
56 | #include <linux/in.h> | |
57 | #include <linux/tcp.h> | |
58 | #include <linux/skbuff.h> | |
59 | #include <linux/bitops.h> | |
60 | #include <linux/ethtool.h> | |
61 | #include <asm/uaccess.h> | |
62 | #include <asm/delay.h> | |
c20e3945 | 63 | #include <asm/unaligned.h> |
1da177e4 LT |
64 | #include <net/arp.h> |
65 | ||
de4394f1 | 66 | #include "config_roms.h" |
1da177e4 | 67 | #include "csr1212.h" |
de4394f1 SR |
68 | #include "eth1394.h" |
69 | #include "highlevel.h" | |
70 | #include "ieee1394.h" | |
1da177e4 | 71 | #include "ieee1394_core.h" |
de4394f1 | 72 | #include "ieee1394_hotplug.h" |
1da177e4 | 73 | #include "ieee1394_transactions.h" |
de4394f1 | 74 | #include "ieee1394_types.h" |
1da177e4 LT |
75 | #include "iso.h" |
76 | #include "nodemgr.h" | |
1da177e4 LT |
77 | |
78 | #define ETH1394_PRINT_G(level, fmt, args...) \ | |
79 | printk(level "%s: " fmt, driver_name, ## args) | |
80 | ||
81 | #define ETH1394_PRINT(level, dev_name, fmt, args...) \ | |
82 | printk(level "%s: %s: " fmt, driver_name, dev_name, ## args) | |
83 | ||
1da177e4 LT |
84 | struct fragment_info { |
85 | struct list_head list; | |
86 | int offset; | |
87 | int len; | |
88 | }; | |
89 | ||
90 | struct partial_datagram { | |
91 | struct list_head list; | |
92 | u16 dgl; | |
93 | u16 dg_size; | |
94 | u16 ether_type; | |
95 | struct sk_buff *skb; | |
96 | char *pbuf; | |
97 | struct list_head frag_info; | |
98 | }; | |
99 | ||
100 | struct pdg_list { | |
efbeccf1 SR |
101 | struct list_head list; /* partial datagram list per node */ |
102 | unsigned int sz; /* partial datagram list size per node */ | |
103 | spinlock_t lock; /* partial datagram lock */ | |
1da177e4 LT |
104 | }; |
105 | ||
106 | struct eth1394_host_info { | |
107 | struct hpsb_host *host; | |
108 | struct net_device *dev; | |
109 | }; | |
110 | ||
111 | struct eth1394_node_ref { | |
112 | struct unit_directory *ud; | |
113 | struct list_head list; | |
114 | }; | |
115 | ||
116 | struct eth1394_node_info { | |
efbeccf1 SR |
117 | u16 maxpayload; /* max payload */ |
118 | u8 sspd; /* max speed */ | |
119 | u64 fifo; /* FIFO address */ | |
120 | struct pdg_list pdg; /* partial RX datagram lists */ | |
121 | int dgl; /* outgoing datagram label */ | |
1da177e4 LT |
122 | }; |
123 | ||
efbeccf1 | 124 | static const char driver_name[] = "eth1394"; |
1da177e4 | 125 | |
e18b890b | 126 | static struct kmem_cache *packet_task_cache; |
1da177e4 LT |
127 | |
128 | static struct hpsb_highlevel eth1394_highlevel; | |
129 | ||
130 | /* Use common.lf to determine header len */ | |
131 | static const int hdr_type_len[] = { | |
efbeccf1 SR |
132 | sizeof(struct eth1394_uf_hdr), |
133 | sizeof(struct eth1394_ff_hdr), | |
134 | sizeof(struct eth1394_sf_hdr), | |
135 | sizeof(struct eth1394_sf_hdr) | |
1da177e4 LT |
136 | }; |
137 | ||
1da177e4 LT |
138 | static const u16 eth1394_speedto_maxpayload[] = { |
139 | /* S100, S200, S400, S800, S1600, S3200 */ | |
140 | 512, 1024, 2048, 4096, 4096, 4096 | |
141 | }; | |
142 | ||
143 | MODULE_AUTHOR("Ben Collins (bcollins@debian.org)"); | |
144 | MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)"); | |
145 | MODULE_LICENSE("GPL"); | |
146 | ||
efbeccf1 SR |
147 | /* |
148 | * The max_partial_datagrams parameter is the maximum number of fragmented | |
1da177e4 LT |
149 | * datagrams per node that eth1394 will keep in memory. Providing an upper |
150 | * bound allows us to limit the amount of memory that partial datagrams | |
151 | * consume in the event that some partial datagrams are never completed. | |
152 | */ | |
153 | static int max_partial_datagrams = 25; | |
154 | module_param(max_partial_datagrams, int, S_IRUGO | S_IWUSR); | |
155 | MODULE_PARM_DESC(max_partial_datagrams, | |
156 | "Maximum number of partially received fragmented datagrams " | |
157 | "(default = 25)."); | |
158 | ||
159 | ||
160 | static int ether1394_header(struct sk_buff *skb, struct net_device *dev, | |
161 | unsigned short type, void *daddr, void *saddr, | |
162 | unsigned len); | |
163 | static int ether1394_rebuild_header(struct sk_buff *skb); | |
164 | static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr); | |
165 | static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh); | |
166 | static void ether1394_header_cache_update(struct hh_cache *hh, | |
167 | struct net_device *dev, | |
efbeccf1 | 168 | unsigned char *haddr); |
1da177e4 LT |
169 | static int ether1394_tx(struct sk_buff *skb, struct net_device *dev); |
170 | static void ether1394_iso(struct hpsb_iso *iso); | |
171 | ||
172 | static struct ethtool_ops ethtool_ops; | |
173 | ||
174 | static int ether1394_write(struct hpsb_host *host, int srcid, int destid, | |
175 | quadlet_t *data, u64 addr, size_t len, u16 flags); | |
efbeccf1 SR |
176 | static void ether1394_add_host(struct hpsb_host *host); |
177 | static void ether1394_remove_host(struct hpsb_host *host); | |
178 | static void ether1394_host_reset(struct hpsb_host *host); | |
1da177e4 LT |
179 | |
180 | /* Function for incoming 1394 packets */ | |
181 | static struct hpsb_address_ops addr_ops = { | |
182 | .write = ether1394_write, | |
183 | }; | |
184 | ||
185 | /* Ieee1394 highlevel driver functions */ | |
186 | static struct hpsb_highlevel eth1394_highlevel = { | |
187 | .name = driver_name, | |
188 | .add_host = ether1394_add_host, | |
189 | .remove_host = ether1394_remove_host, | |
190 | .host_reset = ether1394_host_reset, | |
191 | }; | |
192 | ||
5009d269 | 193 | static int ether1394_recv_init(struct eth1394_priv *priv) |
09d7a96f | 194 | { |
efbeccf1 SR |
195 | unsigned int iso_buf_size; |
196 | ||
197 | /* FIXME: rawiso limits us to PAGE_SIZE */ | |
198 | iso_buf_size = min((unsigned int)PAGE_SIZE, | |
199 | 2 * (1U << (priv->host->csr.max_rec + 1))); | |
09d7a96f JD |
200 | |
201 | priv->iso = hpsb_iso_recv_init(priv->host, | |
efbeccf1 | 202 | ETHER1394_GASP_BUFFERS * iso_buf_size, |
09d7a96f JD |
203 | ETHER1394_GASP_BUFFERS, |
204 | priv->broadcast_channel, | |
205 | HPSB_ISO_DMA_PACKET_PER_BUFFER, | |
206 | 1, ether1394_iso); | |
207 | if (priv->iso == NULL) { | |
5009d269 | 208 | ETH1394_PRINT_G(KERN_ERR, "Failed to allocate IR context\n"); |
09d7a96f JD |
209 | priv->bc_state = ETHER1394_BC_ERROR; |
210 | return -EAGAIN; | |
211 | } | |
212 | ||
213 | if (hpsb_iso_recv_start(priv->iso, -1, (1 << 3), -1) < 0) | |
214 | priv->bc_state = ETHER1394_BC_STOPPED; | |
215 | else | |
216 | priv->bc_state = ETHER1394_BC_RUNNING; | |
217 | return 0; | |
218 | } | |
219 | ||
1da177e4 | 220 | /* This is called after an "ifup" */ |
09d7a96f | 221 | static int ether1394_open(struct net_device *dev) |
1da177e4 LT |
222 | { |
223 | struct eth1394_priv *priv = netdev_priv(dev); | |
09d7a96f | 224 | int ret; |
1da177e4 | 225 | |
1da177e4 | 226 | if (priv->bc_state == ETHER1394_BC_ERROR) { |
5009d269 | 227 | ret = ether1394_recv_init(priv); |
09d7a96f JD |
228 | if (ret) |
229 | return ret; | |
1da177e4 | 230 | } |
efbeccf1 | 231 | netif_start_queue(dev); |
1da177e4 LT |
232 | return 0; |
233 | } | |
234 | ||
235 | /* This is called after an "ifdown" */ | |
efbeccf1 | 236 | static int ether1394_stop(struct net_device *dev) |
1da177e4 | 237 | { |
efbeccf1 | 238 | netif_stop_queue(dev); |
1da177e4 LT |
239 | return 0; |
240 | } | |
241 | ||
242 | /* Return statistics to the caller */ | |
efbeccf1 | 243 | static struct net_device_stats *ether1394_stats(struct net_device *dev) |
1da177e4 LT |
244 | { |
245 | return &(((struct eth1394_priv *)netdev_priv(dev))->stats); | |
246 | } | |
247 | ||
efbeccf1 SR |
248 | /* FIXME: What to do if we timeout? I think a host reset is probably in order, |
249 | * so that's what we do. Should we increment the stat counters too? */ | |
250 | static void ether1394_tx_timeout(struct net_device *dev) | |
1da177e4 | 251 | { |
efbeccf1 SR |
252 | struct hpsb_host *host = |
253 | ((struct eth1394_priv *)netdev_priv(dev))->host; | |
1da177e4 | 254 | |
246a5fda SR |
255 | ETH1394_PRINT(KERN_ERR, dev->name, "Timeout, resetting host\n"); |
256 | ether1394_host_reset(host); | |
1da177e4 LT |
257 | } |
258 | ||
17bab407 SR |
259 | static inline int ether1394_max_mtu(struct hpsb_host* host) |
260 | { | |
261 | return (1 << (host->csr.max_rec + 1)) | |
262 | - sizeof(union eth1394_hdr) - ETHER1394_GASP_OVERHEAD; | |
263 | } | |
264 | ||
1da177e4 LT |
265 | static int ether1394_change_mtu(struct net_device *dev, int new_mtu) |
266 | { | |
17bab407 | 267 | int max_mtu; |
1da177e4 | 268 | |
17bab407 | 269 | if (new_mtu < 68) |
1da177e4 | 270 | return -EINVAL; |
efbeccf1 | 271 | |
17bab407 SR |
272 | max_mtu = ether1394_max_mtu( |
273 | ((struct eth1394_priv *)netdev_priv(dev))->host); | |
274 | if (new_mtu > max_mtu) { | |
275 | ETH1394_PRINT(KERN_INFO, dev->name, | |
276 | "Local node constrains MTU to %d\n", max_mtu); | |
277 | return -ERANGE; | |
278 | } | |
279 | ||
1da177e4 LT |
280 | dev->mtu = new_mtu; |
281 | return 0; | |
282 | } | |
283 | ||
284 | static void purge_partial_datagram(struct list_head *old) | |
285 | { | |
efbeccf1 | 286 | struct partial_datagram *pd; |
1da177e4 | 287 | struct list_head *lh, *n; |
efbeccf1 SR |
288 | struct fragment_info *fi; |
289 | ||
290 | pd = list_entry(old, struct partial_datagram, list); | |
1da177e4 LT |
291 | |
292 | list_for_each_safe(lh, n, &pd->frag_info) { | |
efbeccf1 | 293 | fi = list_entry(lh, struct fragment_info, list); |
1da177e4 LT |
294 | list_del(lh); |
295 | kfree(fi); | |
296 | } | |
297 | list_del(old); | |
298 | kfree_skb(pd->skb); | |
299 | kfree(pd); | |
300 | } | |
301 | ||
302 | /****************************************** | |
303 | * 1394 bus activity functions | |
304 | ******************************************/ | |
305 | ||
306 | static struct eth1394_node_ref *eth1394_find_node(struct list_head *inl, | |
307 | struct unit_directory *ud) | |
308 | { | |
309 | struct eth1394_node_ref *node; | |
310 | ||
311 | list_for_each_entry(node, inl, list) | |
312 | if (node->ud == ud) | |
313 | return node; | |
314 | ||
315 | return NULL; | |
316 | } | |
317 | ||
318 | static struct eth1394_node_ref *eth1394_find_node_guid(struct list_head *inl, | |
319 | u64 guid) | |
320 | { | |
321 | struct eth1394_node_ref *node; | |
322 | ||
323 | list_for_each_entry(node, inl, list) | |
324 | if (node->ud->ne->guid == guid) | |
325 | return node; | |
326 | ||
327 | return NULL; | |
328 | } | |
329 | ||
330 | static struct eth1394_node_ref *eth1394_find_node_nodeid(struct list_head *inl, | |
331 | nodeid_t nodeid) | |
332 | { | |
333 | struct eth1394_node_ref *node; | |
efbeccf1 SR |
334 | |
335 | list_for_each_entry(node, inl, list) | |
1da177e4 LT |
336 | if (node->ud->ne->nodeid == nodeid) |
337 | return node; | |
1da177e4 LT |
338 | |
339 | return NULL; | |
340 | } | |
341 | ||
d06c1dda SR |
342 | static int eth1394_new_node(struct eth1394_host_info *hi, |
343 | struct unit_directory *ud) | |
1da177e4 | 344 | { |
1da177e4 LT |
345 | struct eth1394_priv *priv; |
346 | struct eth1394_node_ref *new_node; | |
347 | struct eth1394_node_info *node_info; | |
348 | ||
5e7abccd | 349 | new_node = kmalloc(sizeof(*new_node), GFP_KERNEL); |
1da177e4 LT |
350 | if (!new_node) |
351 | return -ENOMEM; | |
352 | ||
5e7abccd | 353 | node_info = kmalloc(sizeof(*node_info), GFP_KERNEL); |
1da177e4 LT |
354 | if (!node_info) { |
355 | kfree(new_node); | |
356 | return -ENOMEM; | |
357 | } | |
358 | ||
359 | spin_lock_init(&node_info->pdg.lock); | |
360 | INIT_LIST_HEAD(&node_info->pdg.list); | |
361 | node_info->pdg.sz = 0; | |
6737231e | 362 | node_info->fifo = CSR1212_INVALID_ADDR_SPACE; |
1da177e4 LT |
363 | |
364 | ud->device.driver_data = node_info; | |
365 | new_node->ud = ud; | |
366 | ||
367 | priv = netdev_priv(hi->dev); | |
368 | list_add_tail(&new_node->list, &priv->ip_node_list); | |
1da177e4 LT |
369 | return 0; |
370 | } | |
371 | ||
d06c1dda SR |
372 | static int eth1394_probe(struct device *dev) |
373 | { | |
374 | struct unit_directory *ud; | |
375 | struct eth1394_host_info *hi; | |
376 | ||
377 | ud = container_of(dev, struct unit_directory, device); | |
378 | hi = hpsb_get_hostinfo(ð1394_highlevel, ud->ne->host); | |
379 | if (!hi) | |
380 | return -ENOENT; | |
381 | ||
382 | return eth1394_new_node(hi, ud); | |
383 | } | |
384 | ||
1da177e4 LT |
385 | static int eth1394_remove(struct device *dev) |
386 | { | |
387 | struct unit_directory *ud; | |
388 | struct eth1394_host_info *hi; | |
389 | struct eth1394_priv *priv; | |
390 | struct eth1394_node_ref *old_node; | |
391 | struct eth1394_node_info *node_info; | |
392 | struct list_head *lh, *n; | |
393 | unsigned long flags; | |
394 | ||
395 | ud = container_of(dev, struct unit_directory, device); | |
396 | hi = hpsb_get_hostinfo(ð1394_highlevel, ud->ne->host); | |
397 | if (!hi) | |
398 | return -ENOENT; | |
399 | ||
400 | priv = netdev_priv(hi->dev); | |
401 | ||
402 | old_node = eth1394_find_node(&priv->ip_node_list, ud); | |
efbeccf1 SR |
403 | if (!old_node) |
404 | return 0; | |
1da177e4 | 405 | |
efbeccf1 SR |
406 | list_del(&old_node->list); |
407 | kfree(old_node); | |
1da177e4 | 408 | |
efbeccf1 | 409 | node_info = (struct eth1394_node_info*)ud->device.driver_data; |
1da177e4 | 410 | |
efbeccf1 SR |
411 | spin_lock_irqsave(&node_info->pdg.lock, flags); |
412 | /* The partial datagram list should be empty, but we'll just | |
413 | * make sure anyway... */ | |
414 | list_for_each_safe(lh, n, &node_info->pdg.list) | |
415 | purge_partial_datagram(lh); | |
416 | spin_unlock_irqrestore(&node_info->pdg.lock, flags); | |
1da177e4 | 417 | |
efbeccf1 SR |
418 | kfree(node_info); |
419 | ud->device.driver_data = NULL; | |
1da177e4 LT |
420 | return 0; |
421 | } | |
422 | ||
423 | static int eth1394_update(struct unit_directory *ud) | |
424 | { | |
425 | struct eth1394_host_info *hi; | |
426 | struct eth1394_priv *priv; | |
427 | struct eth1394_node_ref *node; | |
1da177e4 LT |
428 | |
429 | hi = hpsb_get_hostinfo(ð1394_highlevel, ud->ne->host); | |
430 | if (!hi) | |
431 | return -ENOENT; | |
432 | ||
433 | priv = netdev_priv(hi->dev); | |
1da177e4 | 434 | node = eth1394_find_node(&priv->ip_node_list, ud); |
efbeccf1 SR |
435 | if (node) |
436 | return 0; | |
1da177e4 | 437 | |
d06c1dda | 438 | return eth1394_new_node(hi, ud); |
1da177e4 LT |
439 | } |
440 | ||
1da177e4 LT |
441 | static struct ieee1394_device_id eth1394_id_table[] = { |
442 | { | |
443 | .match_flags = (IEEE1394_MATCH_SPECIFIER_ID | | |
444 | IEEE1394_MATCH_VERSION), | |
445 | .specifier_id = ETHER1394_GASP_SPECIFIER_ID, | |
446 | .version = ETHER1394_GASP_VERSION, | |
447 | }, | |
448 | {} | |
449 | }; | |
450 | ||
451 | MODULE_DEVICE_TABLE(ieee1394, eth1394_id_table); | |
452 | ||
453 | static struct hpsb_protocol_driver eth1394_proto_driver = { | |
efbeccf1 | 454 | .name = driver_name, |
1da177e4 LT |
455 | .id_table = eth1394_id_table, |
456 | .update = eth1394_update, | |
457 | .driver = { | |
1da177e4 LT |
458 | .probe = eth1394_probe, |
459 | .remove = eth1394_remove, | |
460 | }, | |
461 | }; | |
462 | ||
efbeccf1 | 463 | static void ether1394_reset_priv(struct net_device *dev, int set_mtu) |
1da177e4 LT |
464 | { |
465 | unsigned long flags; | |
466 | int i; | |
467 | struct eth1394_priv *priv = netdev_priv(dev); | |
468 | struct hpsb_host *host = priv->host; | |
efbeccf1 | 469 | u64 guid = get_unaligned((u64 *)&(host->csr.rom->bus_info_data[3])); |
1da177e4 LT |
470 | int max_speed = IEEE1394_SPEED_MAX; |
471 | ||
efbeccf1 | 472 | spin_lock_irqsave(&priv->lock, flags); |
1da177e4 | 473 | |
027611b8 | 474 | memset(priv->ud_list, 0, sizeof(priv->ud_list)); |
1da177e4 LT |
475 | priv->bc_maxpayload = 512; |
476 | ||
477 | /* Determine speed limit */ | |
478 | for (i = 0; i < host->node_count; i++) | |
647dcb5f BC |
479 | if (max_speed > host->speed[i]) |
480 | max_speed = host->speed[i]; | |
1da177e4 LT |
481 | priv->bc_sspd = max_speed; |
482 | ||
1da177e4 | 483 | if (set_mtu) { |
17bab407 SR |
484 | /* Use the RFC 2734 default 1500 octets or the maximum payload |
485 | * as initial MTU */ | |
486 | dev->mtu = min(1500, ether1394_max_mtu(host)); | |
1da177e4 LT |
487 | |
488 | /* Set our hardware address while we're at it */ | |
c20e3945 DM |
489 | memcpy(dev->dev_addr, &guid, sizeof(u64)); |
490 | memset(dev->broadcast, 0xff, sizeof(u64)); | |
1da177e4 LT |
491 | } |
492 | ||
efbeccf1 | 493 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 LT |
494 | } |
495 | ||
efbeccf1 | 496 | static void ether1394_init_dev(struct net_device *dev) |
1da177e4 | 497 | { |
1da177e4 LT |
498 | dev->open = ether1394_open; |
499 | dev->stop = ether1394_stop; | |
500 | dev->hard_start_xmit = ether1394_tx; | |
501 | dev->get_stats = ether1394_stats; | |
502 | dev->tx_timeout = ether1394_tx_timeout; | |
503 | dev->change_mtu = ether1394_change_mtu; | |
504 | ||
505 | dev->hard_header = ether1394_header; | |
506 | dev->rebuild_header = ether1394_rebuild_header; | |
507 | dev->hard_header_cache = ether1394_header_cache; | |
508 | dev->header_cache_update= ether1394_header_cache_update; | |
509 | dev->hard_header_parse = ether1394_header_parse; | |
01590d20 | 510 | |
1da177e4 LT |
511 | SET_ETHTOOL_OPS(dev, ðtool_ops); |
512 | ||
1da177e4 LT |
513 | dev->watchdog_timeo = ETHER1394_TIMEOUT; |
514 | dev->flags = IFF_BROADCAST | IFF_MULTICAST; | |
515 | dev->features = NETIF_F_HIGHDMA; | |
516 | dev->addr_len = ETH1394_ALEN; | |
517 | dev->hard_header_len = ETH1394_HLEN; | |
518 | dev->type = ARPHRD_IEEE1394; | |
519 | ||
01590d20 SR |
520 | /* FIXME: This value was copied from ether_setup(). Is it too much? */ |
521 | dev->tx_queue_len = 1000; | |
1da177e4 LT |
522 | } |
523 | ||
524 | /* | |
525 | * This function is called every time a card is found. It is generally called | |
526 | * when the module is installed. This is where we add all of our ethernet | |
527 | * devices. One for each host. | |
528 | */ | |
efbeccf1 | 529 | static void ether1394_add_host(struct hpsb_host *host) |
1da177e4 LT |
530 | { |
531 | struct eth1394_host_info *hi = NULL; | |
532 | struct net_device *dev = NULL; | |
533 | struct eth1394_priv *priv; | |
1da177e4 LT |
534 | u64 fifo_addr; |
535 | ||
70093cfd SR |
536 | if (hpsb_config_rom_ip1394_add(host) != 0) { |
537 | ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n"); | |
1da177e4 | 538 | return; |
70093cfd | 539 | } |
1da177e4 | 540 | |
6737231e BC |
541 | fifo_addr = hpsb_allocate_and_register_addrspace( |
542 | ð1394_highlevel, host, &addr_ops, | |
543 | ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN, | |
544 | CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE); | |
157188cb SR |
545 | if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) { |
546 | ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n"); | |
70093cfd | 547 | hpsb_config_rom_ip1394_remove(host); |
157188cb SR |
548 | return; |
549 | } | |
1da177e4 | 550 | |
01590d20 | 551 | dev = alloc_netdev(sizeof(*priv), "eth%d", ether1394_init_dev); |
1da177e4 | 552 | if (dev == NULL) { |
5009d269 | 553 | ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); |
1da177e4 | 554 | goto out; |
efbeccf1 | 555 | } |
1da177e4 LT |
556 | |
557 | SET_MODULE_OWNER(dev); | |
7a9eeb2f SR |
558 | #if 0 |
559 | /* FIXME - Is this the correct parent device anyway? */ | |
1da177e4 | 560 | SET_NETDEV_DEV(dev, &host->device); |
7a9eeb2f | 561 | #endif |
1da177e4 LT |
562 | |
563 | priv = netdev_priv(dev); | |
1da177e4 | 564 | INIT_LIST_HEAD(&priv->ip_node_list); |
1da177e4 LT |
565 | spin_lock_init(&priv->lock); |
566 | priv->host = host; | |
567 | priv->local_fifo = fifo_addr; | |
568 | ||
569 | hi = hpsb_create_hostinfo(ð1394_highlevel, host, sizeof(*hi)); | |
1da177e4 | 570 | if (hi == NULL) { |
5009d269 | 571 | ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); |
1da177e4 | 572 | goto out; |
efbeccf1 | 573 | } |
1da177e4 | 574 | |
01590d20 | 575 | ether1394_reset_priv(dev, 1); |
1da177e4 | 576 | |
5009d269 SR |
577 | if (register_netdev(dev)) { |
578 | ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n"); | |
1da177e4 LT |
579 | goto out; |
580 | } | |
581 | ||
5009d269 SR |
582 | ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n", |
583 | host->id); | |
1da177e4 LT |
584 | |
585 | hi->host = host; | |
586 | hi->dev = dev; | |
587 | ||
588 | /* Ignore validity in hopes that it will be set in the future. It'll | |
589 | * be checked when the eth device is opened. */ | |
590 | priv->broadcast_channel = host->csr.broadcast_channel & 0x3f; | |
591 | ||
5009d269 | 592 | ether1394_recv_init(priv); |
1da177e4 | 593 | return; |
1da177e4 | 594 | out: |
157188cb | 595 | if (dev) |
1da177e4 LT |
596 | free_netdev(dev); |
597 | if (hi) | |
598 | hpsb_destroy_hostinfo(ð1394_highlevel, host); | |
157188cb | 599 | hpsb_unregister_addrspace(ð1394_highlevel, host, fifo_addr); |
70093cfd | 600 | hpsb_config_rom_ip1394_remove(host); |
1da177e4 LT |
601 | } |
602 | ||
603 | /* Remove a card from our list */ | |
efbeccf1 | 604 | static void ether1394_remove_host(struct hpsb_host *host) |
1da177e4 LT |
605 | { |
606 | struct eth1394_host_info *hi; | |
2cd556ae | 607 | struct eth1394_priv *priv; |
1da177e4 LT |
608 | |
609 | hi = hpsb_get_hostinfo(ð1394_highlevel, host); | |
2cd556ae SR |
610 | if (!hi) |
611 | return; | |
612 | priv = netdev_priv(hi->dev); | |
613 | hpsb_unregister_addrspace(ð1394_highlevel, host, priv->local_fifo); | |
70093cfd | 614 | hpsb_config_rom_ip1394_remove(host); |
2cd556ae SR |
615 | if (priv->iso) |
616 | hpsb_iso_shutdown(priv->iso); | |
617 | unregister_netdev(hi->dev); | |
618 | free_netdev(hi->dev); | |
1da177e4 LT |
619 | } |
620 | ||
efbeccf1 SR |
621 | /* A bus reset happened */ |
622 | static void ether1394_host_reset(struct hpsb_host *host) | |
1da177e4 LT |
623 | { |
624 | struct eth1394_host_info *hi; | |
625 | struct eth1394_priv *priv; | |
626 | struct net_device *dev; | |
627 | struct list_head *lh, *n; | |
628 | struct eth1394_node_ref *node; | |
629 | struct eth1394_node_info *node_info; | |
630 | unsigned long flags; | |
631 | ||
632 | hi = hpsb_get_hostinfo(ð1394_highlevel, host); | |
633 | ||
634 | /* This can happen for hosts that we don't use */ | |
2cd556ae | 635 | if (!hi) |
1da177e4 LT |
636 | return; |
637 | ||
638 | dev = hi->dev; | |
efbeccf1 | 639 | priv = netdev_priv(dev); |
1da177e4 | 640 | |
efbeccf1 SR |
641 | /* Reset our private host data, but not our MTU */ |
642 | netif_stop_queue(dev); | |
643 | ether1394_reset_priv(dev, 0); | |
1da177e4 LT |
644 | |
645 | list_for_each_entry(node, &priv->ip_node_list, list) { | |
efbeccf1 | 646 | node_info = node->ud->device.driver_data; |
1da177e4 LT |
647 | |
648 | spin_lock_irqsave(&node_info->pdg.lock, flags); | |
649 | ||
efbeccf1 | 650 | list_for_each_safe(lh, n, &node_info->pdg.list) |
1da177e4 | 651 | purge_partial_datagram(lh); |
1da177e4 LT |
652 | |
653 | INIT_LIST_HEAD(&(node_info->pdg.list)); | |
654 | node_info->pdg.sz = 0; | |
655 | ||
656 | spin_unlock_irqrestore(&node_info->pdg.lock, flags); | |
657 | } | |
658 | ||
efbeccf1 | 659 | netif_wake_queue(dev); |
1da177e4 LT |
660 | } |
661 | ||
662 | /****************************************** | |
663 | * HW Header net device functions | |
664 | ******************************************/ | |
665 | /* These functions have been adapted from net/ethernet/eth.c */ | |
666 | ||
1da177e4 LT |
667 | /* Create a fake MAC header for an arbitrary protocol layer. |
668 | * saddr=NULL means use device source address | |
669 | * daddr=NULL means leave destination address (eg unresolved arp). */ | |
670 | static int ether1394_header(struct sk_buff *skb, struct net_device *dev, | |
671 | unsigned short type, void *daddr, void *saddr, | |
672 | unsigned len) | |
673 | { | |
efbeccf1 SR |
674 | struct eth1394hdr *eth = |
675 | (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN); | |
1da177e4 LT |
676 | |
677 | eth->h_proto = htons(type); | |
678 | ||
efbeccf1 | 679 | if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) { |
1da177e4 | 680 | memset(eth->h_dest, 0, dev->addr_len); |
efbeccf1 | 681 | return dev->hard_header_len; |
1da177e4 LT |
682 | } |
683 | ||
684 | if (daddr) { | |
efbeccf1 | 685 | memcpy(eth->h_dest, daddr, dev->addr_len); |
1da177e4 LT |
686 | return dev->hard_header_len; |
687 | } | |
688 | ||
689 | return -dev->hard_header_len; | |
1da177e4 LT |
690 | } |
691 | ||
1da177e4 LT |
692 | /* Rebuild the faked MAC header. This is called after an ARP |
693 | * (or in future other address resolution) has completed on this | |
694 | * sk_buff. We now let ARP fill in the other fields. | |
695 | * | |
696 | * This routine CANNOT use cached dst->neigh! | |
697 | * Really, it is used only when dst->neigh is wrong. | |
698 | */ | |
699 | static int ether1394_rebuild_header(struct sk_buff *skb) | |
700 | { | |
701 | struct eth1394hdr *eth = (struct eth1394hdr *)skb->data; | |
1da177e4 | 702 | |
599bba96 | 703 | if (eth->h_proto == htons(ETH_P_IP)) |
efbeccf1 | 704 | return arp_find((unsigned char *)ð->h_dest, skb); |
1da177e4 | 705 | |
599bba96 SR |
706 | ETH1394_PRINT(KERN_DEBUG, skb->dev->name, |
707 | "unable to resolve type %04x addresses\n", | |
708 | ntohs(eth->h_proto)); | |
1da177e4 LT |
709 | return 0; |
710 | } | |
711 | ||
712 | static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr) | |
713 | { | |
714 | struct net_device *dev = skb->dev; | |
efbeccf1 | 715 | |
1da177e4 LT |
716 | memcpy(haddr, dev->dev_addr, ETH1394_ALEN); |
717 | return ETH1394_ALEN; | |
718 | } | |
719 | ||
1da177e4 LT |
720 | static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh) |
721 | { | |
722 | unsigned short type = hh->hh_type; | |
1da177e4 | 723 | struct net_device *dev = neigh->dev; |
efbeccf1 SR |
724 | struct eth1394hdr *eth = |
725 | (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN); | |
1da177e4 | 726 | |
7136b807 | 727 | if (type == htons(ETH_P_802_3)) |
1da177e4 | 728 | return -1; |
1da177e4 LT |
729 | |
730 | eth->h_proto = type; | |
731 | memcpy(eth->h_dest, neigh->ha, dev->addr_len); | |
732 | ||
733 | hh->hh_len = ETH1394_HLEN; | |
734 | return 0; | |
735 | } | |
736 | ||
737 | /* Called by Address Resolution module to notify changes in address. */ | |
738 | static void ether1394_header_cache_update(struct hh_cache *hh, | |
739 | struct net_device *dev, | |
740 | unsigned char * haddr) | |
741 | { | |
efbeccf1 | 742 | memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len); |
1da177e4 LT |
743 | } |
744 | ||
1da177e4 LT |
745 | /****************************************** |
746 | * Datagram reception code | |
747 | ******************************************/ | |
748 | ||
749 | /* Copied from net/ethernet/eth.c */ | |
e00f04a7 | 750 | static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
751 | { |
752 | struct eth1394hdr *eth; | |
753 | unsigned char *rawp; | |
754 | ||
459a98ed | 755 | skb_reset_mac_header(skb); |
efbeccf1 | 756 | skb_pull(skb, ETH1394_HLEN); |
1da177e4 LT |
757 | eth = eth1394_hdr(skb); |
758 | ||
759 | if (*eth->h_dest & 1) { | |
efbeccf1 | 760 | if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0) |
1da177e4 LT |
761 | skb->pkt_type = PACKET_BROADCAST; |
762 | #if 0 | |
763 | else | |
764 | skb->pkt_type = PACKET_MULTICAST; | |
765 | #endif | |
766 | } else { | |
767 | if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len)) | |
768 | skb->pkt_type = PACKET_OTHERHOST; | |
efbeccf1 | 769 | } |
1da177e4 | 770 | |
efbeccf1 | 771 | if (ntohs(eth->h_proto) >= 1536) |
1da177e4 LT |
772 | return eth->h_proto; |
773 | ||
774 | rawp = skb->data; | |
775 | ||
efbeccf1 SR |
776 | if (*(unsigned short *)rawp == 0xFFFF) |
777 | return htons(ETH_P_802_3); | |
1da177e4 | 778 | |
efbeccf1 | 779 | return htons(ETH_P_802_2); |
1da177e4 LT |
780 | } |
781 | ||
782 | /* Parse an encapsulated IP1394 header into an ethernet frame packet. | |
783 | * We also perform ARP translation here, if need be. */ | |
e00f04a7 SR |
784 | static u16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev, |
785 | nodeid_t srcid, nodeid_t destid, | |
786 | u16 ether_type) | |
1da177e4 LT |
787 | { |
788 | struct eth1394_priv *priv = netdev_priv(dev); | |
789 | u64 dest_hw; | |
790 | unsigned short ret = 0; | |
791 | ||
efbeccf1 | 792 | /* Setup our hw addresses. We use these to build the ethernet header. */ |
1da177e4 LT |
793 | if (destid == (LOCAL_BUS | ALL_NODES)) |
794 | dest_hw = ~0ULL; /* broadcast */ | |
795 | else | |
efbeccf1 | 796 | dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 | |
1da177e4 LT |
797 | priv->host->csr.guid_lo); |
798 | ||
799 | /* If this is an ARP packet, convert it. First, we want to make | |
800 | * use of some of the fields, since they tell us a little bit | |
801 | * about the sending machine. */ | |
7136b807 | 802 | if (ether_type == htons(ETH_P_ARP)) { |
efbeccf1 | 803 | struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data; |
1da177e4 LT |
804 | struct arphdr *arp = (struct arphdr *)skb->data; |
805 | unsigned char *arp_ptr = (unsigned char *)(arp + 1); | |
806 | u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 | | |
efbeccf1 | 807 | ntohl(arp1394->fifo_lo); |
1da177e4 LT |
808 | u8 max_rec = min(priv->host->csr.max_rec, |
809 | (u8)(arp1394->max_rec)); | |
810 | int sspd = arp1394->sspd; | |
811 | u16 maxpayload; | |
812 | struct eth1394_node_ref *node; | |
813 | struct eth1394_node_info *node_info; | |
c20e3945 | 814 | __be64 guid; |
1da177e4 LT |
815 | |
816 | /* Sanity check. MacOSX seems to be sending us 131 in this | |
817 | * field (atleast on my Panther G5). Not sure why. */ | |
818 | if (sspd > 5 || sspd < 0) | |
819 | sspd = 0; | |
820 | ||
efbeccf1 SR |
821 | maxpayload = min(eth1394_speedto_maxpayload[sspd], |
822 | (u16)(1 << (max_rec + 1))); | |
1da177e4 | 823 | |
c20e3945 | 824 | guid = get_unaligned(&arp1394->s_uniq_id); |
1da177e4 | 825 | node = eth1394_find_node_guid(&priv->ip_node_list, |
c20e3945 | 826 | be64_to_cpu(guid)); |
efbeccf1 | 827 | if (!node) |
1da177e4 | 828 | return 0; |
1da177e4 | 829 | |
efbeccf1 SR |
830 | node_info = |
831 | (struct eth1394_node_info *)node->ud->device.driver_data; | |
1da177e4 LT |
832 | |
833 | /* Update our speed/payload/fifo_offset table */ | |
834 | node_info->maxpayload = maxpayload; | |
835 | node_info->sspd = sspd; | |
836 | node_info->fifo = fifo_addr; | |
837 | ||
838 | /* Now that we're done with the 1394 specific stuff, we'll | |
839 | * need to alter some of the data. Believe it or not, all | |
840 | * that needs to be done is sender_IP_address needs to be | |
841 | * moved, the destination hardware address get stuffed | |
842 | * in and the hardware address length set to 8. | |
843 | * | |
844 | * IMPORTANT: The code below overwrites 1394 specific data | |
845 | * needed above so keep the munging of the data for the | |
846 | * higher level IP stack last. */ | |
847 | ||
848 | arp->ar_hln = 8; | |
849 | arp_ptr += arp->ar_hln; /* skip over sender unique id */ | |
efbeccf1 | 850 | *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */ |
1da177e4 LT |
851 | arp_ptr += arp->ar_pln; /* skip over sender IP addr */ |
852 | ||
02f4213f | 853 | if (arp->ar_op == htons(ARPOP_REQUEST)) |
c20e3945 | 854 | memset(arp_ptr, 0, sizeof(u64)); |
1da177e4 | 855 | else |
c20e3945 | 856 | memcpy(arp_ptr, dev->dev_addr, sizeof(u64)); |
1da177e4 LT |
857 | } |
858 | ||
859 | /* Now add the ethernet header. */ | |
7136b807 BC |
860 | if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL, |
861 | skb->len) >= 0) | |
1da177e4 LT |
862 | ret = ether1394_type_trans(skb, dev); |
863 | ||
864 | return ret; | |
865 | } | |
866 | ||
e00f04a7 | 867 | static int fragment_overlap(struct list_head *frag_list, int offset, int len) |
1da177e4 LT |
868 | { |
869 | struct fragment_info *fi; | |
2e2173df | 870 | int end = offset + len; |
1da177e4 | 871 | |
2e2173df SR |
872 | list_for_each_entry(fi, frag_list, list) |
873 | if (offset < fi->offset + fi->len && end > fi->offset) | |
1da177e4 | 874 | return 1; |
2e2173df | 875 | |
1da177e4 LT |
876 | return 0; |
877 | } | |
878 | ||
e00f04a7 | 879 | static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl) |
1da177e4 LT |
880 | { |
881 | struct partial_datagram *pd; | |
882 | ||
efbeccf1 | 883 | list_for_each_entry(pd, pdgl, list) |
1da177e4 LT |
884 | if (pd->dgl == dgl) |
885 | return &pd->list; | |
efbeccf1 | 886 | |
1da177e4 LT |
887 | return NULL; |
888 | } | |
889 | ||
890 | /* Assumes that new fragment does not overlap any existing fragments */ | |
e00f04a7 | 891 | static int new_fragment(struct list_head *frag_info, int offset, int len) |
1da177e4 LT |
892 | { |
893 | struct list_head *lh; | |
894 | struct fragment_info *fi, *fi2, *new; | |
895 | ||
896 | list_for_each(lh, frag_info) { | |
897 | fi = list_entry(lh, struct fragment_info, list); | |
efbeccf1 | 898 | if (fi->offset + fi->len == offset) { |
1da177e4 LT |
899 | /* The new fragment can be tacked on to the end */ |
900 | fi->len += len; | |
901 | /* Did the new fragment plug a hole? */ | |
902 | fi2 = list_entry(lh->next, struct fragment_info, list); | |
efbeccf1 | 903 | if (fi->offset + fi->len == fi2->offset) { |
1da177e4 LT |
904 | /* glue fragments together */ |
905 | fi->len += fi2->len; | |
906 | list_del(lh->next); | |
907 | kfree(fi2); | |
908 | } | |
909 | return 0; | |
efbeccf1 | 910 | } else if (offset + len == fi->offset) { |
1da177e4 LT |
911 | /* The new fragment can be tacked on to the beginning */ |
912 | fi->offset = offset; | |
913 | fi->len += len; | |
914 | /* Did the new fragment plug a hole? */ | |
915 | fi2 = list_entry(lh->prev, struct fragment_info, list); | |
efbeccf1 | 916 | if (fi2->offset + fi2->len == fi->offset) { |
1da177e4 LT |
917 | /* glue fragments together */ |
918 | fi2->len += fi->len; | |
919 | list_del(lh); | |
920 | kfree(fi); | |
921 | } | |
922 | return 0; | |
efbeccf1 | 923 | } else if (offset > fi->offset + fi->len) { |
1da177e4 | 924 | break; |
efbeccf1 | 925 | } else if (offset + len < fi->offset) { |
1da177e4 LT |
926 | lh = lh->prev; |
927 | break; | |
928 | } | |
929 | } | |
930 | ||
8551158a | 931 | new = kmalloc(sizeof(*new), GFP_ATOMIC); |
1da177e4 LT |
932 | if (!new) |
933 | return -ENOMEM; | |
934 | ||
935 | new->offset = offset; | |
936 | new->len = len; | |
937 | ||
938 | list_add(&new->list, lh); | |
1da177e4 LT |
939 | return 0; |
940 | } | |
941 | ||
e00f04a7 SR |
942 | static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl, |
943 | int dgl, int dg_size, char *frag_buf, | |
944 | int frag_off, int frag_len) | |
1da177e4 LT |
945 | { |
946 | struct partial_datagram *new; | |
947 | ||
8551158a | 948 | new = kmalloc(sizeof(*new), GFP_ATOMIC); |
1da177e4 LT |
949 | if (!new) |
950 | return -ENOMEM; | |
951 | ||
952 | INIT_LIST_HEAD(&new->frag_info); | |
953 | ||
954 | if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) { | |
955 | kfree(new); | |
956 | return -ENOMEM; | |
957 | } | |
958 | ||
959 | new->dgl = dgl; | |
960 | new->dg_size = dg_size; | |
961 | ||
962 | new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15); | |
963 | if (!new->skb) { | |
964 | struct fragment_info *fi = list_entry(new->frag_info.next, | |
965 | struct fragment_info, | |
966 | list); | |
967 | kfree(fi); | |
968 | kfree(new); | |
969 | return -ENOMEM; | |
970 | } | |
971 | ||
972 | skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15); | |
973 | new->pbuf = skb_put(new->skb, dg_size); | |
974 | memcpy(new->pbuf + frag_off, frag_buf, frag_len); | |
975 | ||
976 | list_add(&new->list, pdgl); | |
1da177e4 LT |
977 | return 0; |
978 | } | |
979 | ||
e00f04a7 SR |
980 | static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh, |
981 | char *frag_buf, int frag_off, int frag_len) | |
1da177e4 | 982 | { |
efbeccf1 SR |
983 | struct partial_datagram *pd = |
984 | list_entry(lh, struct partial_datagram, list); | |
1da177e4 | 985 | |
efbeccf1 | 986 | if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0) |
1da177e4 | 987 | return -ENOMEM; |
1da177e4 LT |
988 | |
989 | memcpy(pd->pbuf + frag_off, frag_buf, frag_len); | |
990 | ||
991 | /* Move list entry to beginnig of list so that oldest partial | |
992 | * datagrams percolate to the end of the list */ | |
179e0917 | 993 | list_move(lh, pdgl); |
1da177e4 LT |
994 | return 0; |
995 | } | |
996 | ||
e00f04a7 | 997 | static int is_datagram_complete(struct list_head *lh, int dg_size) |
1da177e4 | 998 | { |
e00f04a7 SR |
999 | struct partial_datagram *pd; |
1000 | struct fragment_info *fi; | |
1001 | ||
1002 | pd = list_entry(lh, struct partial_datagram, list); | |
1003 | fi = list_entry(pd->frag_info.next, struct fragment_info, list); | |
1da177e4 LT |
1004 | |
1005 | return (fi->len == dg_size); | |
1006 | } | |
1007 | ||
1008 | /* Packet reception. We convert the IP1394 encapsulation header to an | |
1009 | * ethernet header, and fill it with some of our other fields. This is | |
1010 | * an incoming packet from the 1394 bus. */ | |
1011 | static int ether1394_data_handler(struct net_device *dev, int srcid, int destid, | |
1012 | char *buf, int len) | |
1013 | { | |
1014 | struct sk_buff *skb; | |
1015 | unsigned long flags; | |
1016 | struct eth1394_priv *priv = netdev_priv(dev); | |
1017 | union eth1394_hdr *hdr = (union eth1394_hdr *)buf; | |
1018 | u16 ether_type = 0; /* initialized to clear warning */ | |
1019 | int hdr_len; | |
1020 | struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)]; | |
1021 | struct eth1394_node_info *node_info; | |
1022 | ||
1023 | if (!ud) { | |
1024 | struct eth1394_node_ref *node; | |
1025 | node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid); | |
09939871 | 1026 | if (unlikely(!node)) { |
1da177e4 LT |
1027 | HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid " |
1028 | "lookup failure: " NODE_BUS_FMT, | |
1029 | NODE_BUS_ARGS(priv->host, srcid)); | |
1030 | priv->stats.rx_dropped++; | |
1031 | return -1; | |
1032 | } | |
1033 | ud = node->ud; | |
1034 | ||
1035 | priv->ud_list[NODEID_TO_NODE(srcid)] = ud; | |
1036 | } | |
1037 | ||
efbeccf1 | 1038 | node_info = (struct eth1394_node_info *)ud->device.driver_data; |
1da177e4 LT |
1039 | |
1040 | /* First, did we receive a fragmented or unfragmented datagram? */ | |
1041 | hdr->words.word1 = ntohs(hdr->words.word1); | |
1042 | ||
1043 | hdr_len = hdr_type_len[hdr->common.lf]; | |
1044 | ||
1045 | if (hdr->common.lf == ETH1394_HDR_LF_UF) { | |
1046 | /* An unfragmented datagram has been received by the ieee1394 | |
1047 | * bus. Build an skbuff around it so we can pass it to the | |
1048 | * high level network layer. */ | |
1049 | ||
1050 | skb = dev_alloc_skb(len + dev->hard_header_len + 15); | |
09939871 | 1051 | if (unlikely(!skb)) { |
5009d269 | 1052 | ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); |
1da177e4 LT |
1053 | priv->stats.rx_dropped++; |
1054 | return -1; | |
1055 | } | |
1056 | skb_reserve(skb, (dev->hard_header_len + 15) & ~15); | |
efbeccf1 SR |
1057 | memcpy(skb_put(skb, len - hdr_len), buf + hdr_len, |
1058 | len - hdr_len); | |
1da177e4 LT |
1059 | ether_type = hdr->uf.ether_type; |
1060 | } else { | |
1061 | /* A datagram fragment has been received, now the fun begins. */ | |
1062 | ||
1063 | struct list_head *pdgl, *lh; | |
1064 | struct partial_datagram *pd; | |
1065 | int fg_off; | |
1066 | int fg_len = len - hdr_len; | |
1067 | int dg_size; | |
1068 | int dgl; | |
1069 | int retval; | |
1070 | struct pdg_list *pdg = &(node_info->pdg); | |
1071 | ||
1072 | hdr->words.word3 = ntohs(hdr->words.word3); | |
1073 | /* The 4th header word is reserved so no need to do ntohs() */ | |
1074 | ||
1075 | if (hdr->common.lf == ETH1394_HDR_LF_FF) { | |
1076 | ether_type = hdr->ff.ether_type; | |
1077 | dgl = hdr->ff.dgl; | |
1078 | dg_size = hdr->ff.dg_size + 1; | |
1079 | fg_off = 0; | |
1080 | } else { | |
1081 | hdr->words.word2 = ntohs(hdr->words.word2); | |
1082 | dgl = hdr->sf.dgl; | |
1083 | dg_size = hdr->sf.dg_size + 1; | |
1084 | fg_off = hdr->sf.fg_off; | |
1085 | } | |
1086 | spin_lock_irqsave(&pdg->lock, flags); | |
1087 | ||
1088 | pdgl = &(pdg->list); | |
1089 | lh = find_partial_datagram(pdgl, dgl); | |
1090 | ||
1091 | if (lh == NULL) { | |
1092 | while (pdg->sz >= max_partial_datagrams) { | |
1093 | /* remove the oldest */ | |
1094 | purge_partial_datagram(pdgl->prev); | |
1095 | pdg->sz--; | |
1096 | } | |
1097 | ||
1098 | retval = new_partial_datagram(dev, pdgl, dgl, dg_size, | |
1099 | buf + hdr_len, fg_off, | |
1100 | fg_len); | |
1101 | if (retval < 0) { | |
1102 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1103 | goto bad_proto; | |
1104 | } | |
1105 | pdg->sz++; | |
1106 | lh = find_partial_datagram(pdgl, dgl); | |
1107 | } else { | |
1108 | struct partial_datagram *pd; | |
1109 | ||
1110 | pd = list_entry(lh, struct partial_datagram, list); | |
1111 | ||
1112 | if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) { | |
1113 | /* Overlapping fragments, obliterate old | |
1114 | * datagram and start new one. */ | |
1115 | purge_partial_datagram(lh); | |
1116 | retval = new_partial_datagram(dev, pdgl, dgl, | |
1117 | dg_size, | |
1118 | buf + hdr_len, | |
1119 | fg_off, fg_len); | |
1120 | if (retval < 0) { | |
1121 | pdg->sz--; | |
1122 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1123 | goto bad_proto; | |
1124 | } | |
1125 | } else { | |
1126 | retval = update_partial_datagram(pdgl, lh, | |
1127 | buf + hdr_len, | |
1128 | fg_off, fg_len); | |
1129 | if (retval < 0) { | |
1130 | /* Couldn't save off fragment anyway | |
1131 | * so might as well obliterate the | |
1132 | * datagram now. */ | |
1133 | purge_partial_datagram(lh); | |
1134 | pdg->sz--; | |
1135 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1136 | goto bad_proto; | |
1137 | } | |
1138 | } /* fragment overlap */ | |
1139 | } /* new datagram or add to existing one */ | |
1140 | ||
1141 | pd = list_entry(lh, struct partial_datagram, list); | |
1142 | ||
efbeccf1 | 1143 | if (hdr->common.lf == ETH1394_HDR_LF_FF) |
1da177e4 | 1144 | pd->ether_type = ether_type; |
1da177e4 LT |
1145 | |
1146 | if (is_datagram_complete(lh, dg_size)) { | |
1147 | ether_type = pd->ether_type; | |
1148 | pdg->sz--; | |
1149 | skb = skb_get(pd->skb); | |
1150 | purge_partial_datagram(lh); | |
1151 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1152 | } else { | |
1153 | /* Datagram is not complete, we're done for the | |
1154 | * moment. */ | |
1155 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1156 | return 0; | |
1157 | } | |
1158 | } /* unframgented datagram or fragmented one */ | |
1159 | ||
1160 | /* Write metadata, and then pass to the receive level */ | |
1161 | skb->dev = dev; | |
1162 | skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ | |
1163 | ||
1164 | /* Parse the encapsulation header. This actually does the job of | |
1165 | * converting to an ethernet frame header, aswell as arp | |
1166 | * conversion if needed. ARP conversion is easier in this | |
1167 | * direction, since we are using ethernet as our backend. */ | |
1168 | skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid, | |
1169 | ether_type); | |
1170 | ||
1da177e4 | 1171 | spin_lock_irqsave(&priv->lock, flags); |
efbeccf1 | 1172 | |
1da177e4 LT |
1173 | if (!skb->protocol) { |
1174 | priv->stats.rx_errors++; | |
1175 | priv->stats.rx_dropped++; | |
1176 | dev_kfree_skb_any(skb); | |
1177 | goto bad_proto; | |
1178 | } | |
1179 | ||
1180 | if (netif_rx(skb) == NET_RX_DROP) { | |
1181 | priv->stats.rx_errors++; | |
1182 | priv->stats.rx_dropped++; | |
1183 | goto bad_proto; | |
1184 | } | |
1185 | ||
1186 | /* Statistics */ | |
1187 | priv->stats.rx_packets++; | |
1188 | priv->stats.rx_bytes += skb->len; | |
1189 | ||
1190 | bad_proto: | |
1191 | if (netif_queue_stopped(dev)) | |
1192 | netif_wake_queue(dev); | |
1193 | spin_unlock_irqrestore(&priv->lock, flags); | |
1194 | ||
1195 | dev->last_rx = jiffies; | |
1196 | ||
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | static int ether1394_write(struct hpsb_host *host, int srcid, int destid, | |
1201 | quadlet_t *data, u64 addr, size_t len, u16 flags) | |
1202 | { | |
1203 | struct eth1394_host_info *hi; | |
1204 | ||
1205 | hi = hpsb_get_hostinfo(ð1394_highlevel, host); | |
09939871 | 1206 | if (unlikely(!hi)) { |
5009d269 SR |
1207 | ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n", |
1208 | host->id); | |
1da177e4 LT |
1209 | return RCODE_ADDRESS_ERROR; |
1210 | } | |
1211 | ||
1212 | if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len)) | |
1213 | return RCODE_ADDRESS_ERROR; | |
1214 | else | |
1215 | return RCODE_COMPLETE; | |
1216 | } | |
1217 | ||
1218 | static void ether1394_iso(struct hpsb_iso *iso) | |
1219 | { | |
1220 | quadlet_t *data; | |
1221 | char *buf; | |
1222 | struct eth1394_host_info *hi; | |
1223 | struct net_device *dev; | |
1224 | struct eth1394_priv *priv; | |
1225 | unsigned int len; | |
1226 | u32 specifier_id; | |
1227 | u16 source_id; | |
1228 | int i; | |
1229 | int nready; | |
1230 | ||
1231 | hi = hpsb_get_hostinfo(ð1394_highlevel, iso->host); | |
09939871 | 1232 | if (unlikely(!hi)) { |
5009d269 SR |
1233 | ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n", |
1234 | iso->host->id); | |
1da177e4 LT |
1235 | return; |
1236 | } | |
1237 | ||
1238 | dev = hi->dev; | |
1239 | ||
1240 | nready = hpsb_iso_n_ready(iso); | |
1241 | for (i = 0; i < nready; i++) { | |
1242 | struct hpsb_iso_packet_info *info = | |
1243 | &iso->infos[(iso->first_packet + i) % iso->buf_packets]; | |
efbeccf1 | 1244 | data = (quadlet_t *)(iso->data_buf.kvirt + info->offset); |
1da177e4 LT |
1245 | |
1246 | /* skip over GASP header */ | |
1247 | buf = (char *)data + 8; | |
1248 | len = info->len - 8; | |
1249 | ||
efbeccf1 SR |
1250 | specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 | |
1251 | (be32_to_cpu(data[1]) & 0xff000000) >> 24; | |
1da177e4 LT |
1252 | source_id = be32_to_cpu(data[0]) >> 16; |
1253 | ||
1254 | priv = netdev_priv(dev); | |
1255 | ||
efbeccf1 SR |
1256 | if (info->channel != (iso->host->csr.broadcast_channel & 0x3f) |
1257 | || specifier_id != ETHER1394_GASP_SPECIFIER_ID) { | |
1da177e4 LT |
1258 | /* This packet is not for us */ |
1259 | continue; | |
1260 | } | |
1261 | ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES, | |
1262 | buf, len); | |
1263 | } | |
1264 | ||
1265 | hpsb_iso_recv_release_packets(iso, i); | |
1266 | ||
1267 | dev->last_rx = jiffies; | |
1268 | } | |
1269 | ||
1270 | /****************************************** | |
1271 | * Datagram transmission code | |
1272 | ******************************************/ | |
1273 | ||
1274 | /* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire | |
1275 | * arphdr) is the same format as the ip1394 header, so they overlap. The rest | |
1276 | * needs to be munged a bit. The remainder of the arphdr is formatted based | |
1277 | * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to | |
1278 | * judge. | |
1279 | * | |
1280 | * Now that the EUI is used for the hardware address all we need to do to make | |
1281 | * this work for 1394 is to insert 2 quadlets that contain max_rec size, | |
1282 | * speed, and unicast FIFO address information between the sender_unique_id | |
1283 | * and the IP addresses. | |
1284 | */ | |
e00f04a7 SR |
1285 | static void ether1394_arp_to_1394arp(struct sk_buff *skb, |
1286 | struct net_device *dev) | |
1da177e4 LT |
1287 | { |
1288 | struct eth1394_priv *priv = netdev_priv(dev); | |
1da177e4 LT |
1289 | struct arphdr *arp = (struct arphdr *)skb->data; |
1290 | unsigned char *arp_ptr = (unsigned char *)(arp + 1); | |
1291 | struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data; | |
1292 | ||
1da177e4 LT |
1293 | arp1394->hw_addr_len = 16; |
1294 | arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN); | |
1295 | arp1394->max_rec = priv->host->csr.max_rec; | |
1296 | arp1394->sspd = priv->host->csr.lnk_spd; | |
efbeccf1 SR |
1297 | arp1394->fifo_hi = htons(priv->local_fifo >> 32); |
1298 | arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0); | |
1da177e4 LT |
1299 | } |
1300 | ||
1301 | /* We need to encapsulate the standard header with our own. We use the | |
1302 | * ethernet header's proto for our own. */ | |
e00f04a7 SR |
1303 | static unsigned int ether1394_encapsulate_prep(unsigned int max_payload, |
1304 | __be16 proto, | |
1305 | union eth1394_hdr *hdr, | |
1306 | u16 dg_size, u16 dgl) | |
1da177e4 | 1307 | { |
efbeccf1 SR |
1308 | unsigned int adj_max_payload = |
1309 | max_payload - hdr_type_len[ETH1394_HDR_LF_UF]; | |
1da177e4 LT |
1310 | |
1311 | /* Does it all fit in one packet? */ | |
1312 | if (dg_size <= adj_max_payload) { | |
1313 | hdr->uf.lf = ETH1394_HDR_LF_UF; | |
1314 | hdr->uf.ether_type = proto; | |
1315 | } else { | |
1316 | hdr->ff.lf = ETH1394_HDR_LF_FF; | |
1317 | hdr->ff.ether_type = proto; | |
1318 | hdr->ff.dg_size = dg_size - 1; | |
1319 | hdr->ff.dgl = dgl; | |
1320 | adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF]; | |
1321 | } | |
efbeccf1 | 1322 | return (dg_size + adj_max_payload - 1) / adj_max_payload; |
1da177e4 LT |
1323 | } |
1324 | ||
e00f04a7 SR |
1325 | static unsigned int ether1394_encapsulate(struct sk_buff *skb, |
1326 | unsigned int max_payload, | |
1327 | union eth1394_hdr *hdr) | |
1da177e4 LT |
1328 | { |
1329 | union eth1394_hdr *bufhdr; | |
1330 | int ftype = hdr->common.lf; | |
1331 | int hdrsz = hdr_type_len[ftype]; | |
1332 | unsigned int adj_max_payload = max_payload - hdrsz; | |
1333 | ||
efbeccf1 | 1334 | switch (ftype) { |
1da177e4 LT |
1335 | case ETH1394_HDR_LF_UF: |
1336 | bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz); | |
1337 | bufhdr->words.word1 = htons(hdr->words.word1); | |
1338 | bufhdr->words.word2 = hdr->words.word2; | |
1339 | break; | |
1340 | ||
1341 | case ETH1394_HDR_LF_FF: | |
1342 | bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz); | |
1343 | bufhdr->words.word1 = htons(hdr->words.word1); | |
1344 | bufhdr->words.word2 = hdr->words.word2; | |
1345 | bufhdr->words.word3 = htons(hdr->words.word3); | |
1346 | bufhdr->words.word4 = 0; | |
1347 | ||
1348 | /* Set frag type here for future interior fragments */ | |
1349 | hdr->common.lf = ETH1394_HDR_LF_IF; | |
1350 | hdr->sf.fg_off = 0; | |
1351 | break; | |
1352 | ||
1353 | default: | |
1354 | hdr->sf.fg_off += adj_max_payload; | |
1355 | bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload); | |
1356 | if (max_payload >= skb->len) | |
1357 | hdr->common.lf = ETH1394_HDR_LF_LF; | |
1358 | bufhdr->words.word1 = htons(hdr->words.word1); | |
1359 | bufhdr->words.word2 = htons(hdr->words.word2); | |
1360 | bufhdr->words.word3 = htons(hdr->words.word3); | |
1361 | bufhdr->words.word4 = 0; | |
1362 | } | |
1da177e4 LT |
1363 | return min(max_payload, skb->len); |
1364 | } | |
1365 | ||
e00f04a7 | 1366 | static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host) |
1da177e4 LT |
1367 | { |
1368 | struct hpsb_packet *p; | |
1369 | ||
1370 | p = hpsb_alloc_packet(0); | |
1371 | if (p) { | |
1372 | p->host = host; | |
1373 | p->generation = get_hpsb_generation(host); | |
1374 | p->type = hpsb_async; | |
1375 | } | |
1376 | return p; | |
1377 | } | |
1378 | ||
e00f04a7 SR |
1379 | static int ether1394_prep_write_packet(struct hpsb_packet *p, |
1380 | struct hpsb_host *host, nodeid_t node, | |
efbeccf1 | 1381 | u64 addr, void *data, int tx_len) |
1da177e4 LT |
1382 | { |
1383 | p->node_id = node; | |
1384 | p->data = NULL; | |
1385 | ||
1386 | p->tcode = TCODE_WRITEB; | |
efbeccf1 | 1387 | p->header[1] = host->node_id << 16 | addr >> 32; |
1da177e4 LT |
1388 | p->header[2] = addr & 0xffffffff; |
1389 | ||
1390 | p->header_size = 16; | |
1391 | p->expect_response = 1; | |
1392 | ||
1393 | if (hpsb_get_tlabel(p)) { | |
5009d269 | 1394 | ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n"); |
1da177e4 LT |
1395 | return -1; |
1396 | } | |
efbeccf1 SR |
1397 | p->header[0] = |
1398 | p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4; | |
1da177e4 LT |
1399 | |
1400 | p->header[3] = tx_len << 16; | |
1401 | p->data_size = (tx_len + 3) & ~3; | |
efbeccf1 | 1402 | p->data = data; |
1da177e4 LT |
1403 | |
1404 | return 0; | |
1405 | } | |
1406 | ||
e00f04a7 SR |
1407 | static void ether1394_prep_gasp_packet(struct hpsb_packet *p, |
1408 | struct eth1394_priv *priv, | |
1409 | struct sk_buff *skb, int length) | |
1da177e4 LT |
1410 | { |
1411 | p->header_size = 4; | |
1412 | p->tcode = TCODE_STREAM_DATA; | |
1413 | ||
efbeccf1 SR |
1414 | p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 | |
1415 | TCODE_STREAM_DATA << 4; | |
1da177e4 | 1416 | p->data_size = length; |
efbeccf1 SR |
1417 | p->data = (quadlet_t *)skb->data - 2; |
1418 | p->data[0] = cpu_to_be32(priv->host->node_id << 16 | | |
1da177e4 | 1419 | ETHER1394_GASP_SPECIFIER_ID_HI); |
efbeccf1 | 1420 | p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 | |
7136b807 | 1421 | ETHER1394_GASP_VERSION); |
1da177e4 LT |
1422 | |
1423 | /* Setting the node id to ALL_NODES (not LOCAL_BUS | ALL_NODES) | |
1424 | * prevents hpsb_send_packet() from setting the speed to an arbitrary | |
1425 | * value based on packet->node_id if packet->node_id is not set. */ | |
1426 | p->node_id = ALL_NODES; | |
1427 | p->speed_code = priv->bc_sspd; | |
1428 | } | |
1429 | ||
e00f04a7 | 1430 | static void ether1394_free_packet(struct hpsb_packet *packet) |
1da177e4 LT |
1431 | { |
1432 | if (packet->tcode != TCODE_STREAM_DATA) | |
1433 | hpsb_free_tlabel(packet); | |
1434 | hpsb_free_packet(packet); | |
1435 | } | |
1436 | ||
1437 | static void ether1394_complete_cb(void *__ptask); | |
1438 | ||
1439 | static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len) | |
1440 | { | |
1441 | struct eth1394_priv *priv = ptask->priv; | |
1442 | struct hpsb_packet *packet = NULL; | |
1443 | ||
1444 | packet = ether1394_alloc_common_packet(priv->host); | |
1445 | if (!packet) | |
1446 | return -1; | |
1447 | ||
1448 | if (ptask->tx_type == ETH1394_GASP) { | |
efbeccf1 | 1449 | int length = tx_len + 2 * sizeof(quadlet_t); |
1da177e4 LT |
1450 | |
1451 | ether1394_prep_gasp_packet(packet, priv, ptask->skb, length); | |
1452 | } else if (ether1394_prep_write_packet(packet, priv->host, | |
1453 | ptask->dest_node, | |
1454 | ptask->addr, ptask->skb->data, | |
1455 | tx_len)) { | |
1456 | hpsb_free_packet(packet); | |
1457 | return -1; | |
1458 | } | |
1459 | ||
1460 | ptask->packet = packet; | |
1461 | hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb, | |
1462 | ptask); | |
1463 | ||
1464 | if (hpsb_send_packet(packet) < 0) { | |
1465 | ether1394_free_packet(packet); | |
1466 | return -1; | |
1467 | } | |
1468 | ||
1469 | return 0; | |
1470 | } | |
1471 | ||
1da177e4 | 1472 | /* Task function to be run when a datagram transmission is completed */ |
e00f04a7 | 1473 | static void ether1394_dg_complete(struct packet_task *ptask, int fail) |
1da177e4 LT |
1474 | { |
1475 | struct sk_buff *skb = ptask->skb; | |
efbeccf1 | 1476 | struct eth1394_priv *priv = netdev_priv(skb->dev); |
1da177e4 LT |
1477 | unsigned long flags; |
1478 | ||
1479 | /* Statistics */ | |
1480 | spin_lock_irqsave(&priv->lock, flags); | |
1481 | if (fail) { | |
1482 | priv->stats.tx_dropped++; | |
1483 | priv->stats.tx_errors++; | |
1484 | } else { | |
1485 | priv->stats.tx_bytes += skb->len; | |
1486 | priv->stats.tx_packets++; | |
1487 | } | |
1488 | spin_unlock_irqrestore(&priv->lock, flags); | |
1489 | ||
1490 | dev_kfree_skb_any(skb); | |
1491 | kmem_cache_free(packet_task_cache, ptask); | |
1492 | } | |
1493 | ||
1da177e4 LT |
1494 | /* Callback for when a packet has been sent and the status of that packet is |
1495 | * known */ | |
1496 | static void ether1394_complete_cb(void *__ptask) | |
1497 | { | |
1498 | struct packet_task *ptask = (struct packet_task *)__ptask; | |
1499 | struct hpsb_packet *packet = ptask->packet; | |
1500 | int fail = 0; | |
1501 | ||
1502 | if (packet->tcode != TCODE_STREAM_DATA) | |
1503 | fail = hpsb_packet_success(packet); | |
1504 | ||
1505 | ether1394_free_packet(packet); | |
1506 | ||
1507 | ptask->outstanding_pkts--; | |
1508 | if (ptask->outstanding_pkts > 0 && !fail) { | |
1509 | int tx_len; | |
1510 | ||
1511 | /* Add the encapsulation header to the fragment */ | |
1512 | tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload, | |
1513 | &ptask->hdr); | |
1514 | if (ether1394_send_packet(ptask, tx_len)) | |
1515 | ether1394_dg_complete(ptask, 1); | |
1516 | } else { | |
1517 | ether1394_dg_complete(ptask, fail); | |
1518 | } | |
1519 | } | |
1520 | ||
1da177e4 | 1521 | /* Transmit a packet (called by kernel) */ |
efbeccf1 | 1522 | static int ether1394_tx(struct sk_buff *skb, struct net_device *dev) |
1da177e4 | 1523 | { |
1da177e4 LT |
1524 | struct eth1394hdr *eth; |
1525 | struct eth1394_priv *priv = netdev_priv(dev); | |
02f4213f | 1526 | __be16 proto; |
1da177e4 LT |
1527 | unsigned long flags; |
1528 | nodeid_t dest_node; | |
1529 | eth1394_tx_type tx_type; | |
1da177e4 LT |
1530 | unsigned int tx_len; |
1531 | unsigned int max_payload; | |
1532 | u16 dg_size; | |
1533 | u16 dgl; | |
1534 | struct packet_task *ptask; | |
1535 | struct eth1394_node_ref *node; | |
1536 | struct eth1394_node_info *node_info = NULL; | |
1537 | ||
53f374e7 | 1538 | ptask = kmem_cache_alloc(packet_task_cache, GFP_ATOMIC); |
fdc0092b | 1539 | if (ptask == NULL) |
1da177e4 | 1540 | goto fail; |
1da177e4 LT |
1541 | |
1542 | /* XXX Ignore this for now. Noticed that when MacOSX is the IRM, | |
1543 | * it does not set our validity bit. We need to compensate for | |
1544 | * that somewhere else, but not in eth1394. */ | |
1545 | #if 0 | |
fdc0092b | 1546 | if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000) |
1da177e4 | 1547 | goto fail; |
1da177e4 LT |
1548 | #endif |
1549 | ||
53f374e7 | 1550 | skb = skb_share_check(skb, GFP_ATOMIC); |
fdc0092b | 1551 | if (!skb) |
1da177e4 | 1552 | goto fail; |
1da177e4 LT |
1553 | |
1554 | /* Get rid of the fake eth1394 header, but save a pointer */ | |
efbeccf1 | 1555 | eth = (struct eth1394hdr *)skb->data; |
1da177e4 LT |
1556 | skb_pull(skb, ETH1394_HLEN); |
1557 | ||
1558 | proto = eth->h_proto; | |
1559 | dg_size = skb->len; | |
1560 | ||
1561 | /* Set the transmission type for the packet. ARP packets and IP | |
1562 | * broadcast packets are sent via GASP. */ | |
1563 | if (memcmp(eth->h_dest, dev->broadcast, ETH1394_ALEN) == 0 || | |
7136b807 BC |
1564 | proto == htons(ETH_P_ARP) || |
1565 | (proto == htons(ETH_P_IP) && | |
eddc9ec5 | 1566 | IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) { |
1da177e4 LT |
1567 | tx_type = ETH1394_GASP; |
1568 | dest_node = LOCAL_BUS | ALL_NODES; | |
1569 | max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD; | |
efbeccf1 | 1570 | BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD); |
1da177e4 LT |
1571 | dgl = priv->bc_dgl; |
1572 | if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF]) | |
1573 | priv->bc_dgl++; | |
1574 | } else { | |
c20e3945 DM |
1575 | __be64 guid = get_unaligned((u64 *)eth->h_dest); |
1576 | ||
1da177e4 | 1577 | node = eth1394_find_node_guid(&priv->ip_node_list, |
c20e3945 | 1578 | be64_to_cpu(guid)); |
fdc0092b | 1579 | if (!node) |
1da177e4 | 1580 | goto fail; |
fdc0092b | 1581 | |
efbeccf1 SR |
1582 | node_info = |
1583 | (struct eth1394_node_info *)node->ud->device.driver_data; | |
fdc0092b | 1584 | if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE) |
1da177e4 | 1585 | goto fail; |
1da177e4 LT |
1586 | |
1587 | dest_node = node->ud->ne->nodeid; | |
1588 | max_payload = node_info->maxpayload; | |
efbeccf1 | 1589 | BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD); |
1da177e4 LT |
1590 | |
1591 | dgl = node_info->dgl; | |
1592 | if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF]) | |
1593 | node_info->dgl++; | |
1594 | tx_type = ETH1394_WRREQ; | |
1595 | } | |
1596 | ||
1597 | /* If this is an ARP packet, convert it */ | |
7136b807 | 1598 | if (proto == htons(ETH_P_ARP)) |
efbeccf1 | 1599 | ether1394_arp_to_1394arp(skb, dev); |
1da177e4 LT |
1600 | |
1601 | ptask->hdr.words.word1 = 0; | |
1602 | ptask->hdr.words.word2 = 0; | |
1603 | ptask->hdr.words.word3 = 0; | |
1604 | ptask->hdr.words.word4 = 0; | |
1605 | ptask->skb = skb; | |
1606 | ptask->priv = priv; | |
1607 | ptask->tx_type = tx_type; | |
1608 | ||
1609 | if (tx_type != ETH1394_GASP) { | |
1610 | u64 addr; | |
1611 | ||
1612 | spin_lock_irqsave(&priv->lock, flags); | |
1613 | addr = node_info->fifo; | |
1614 | spin_unlock_irqrestore(&priv->lock, flags); | |
1615 | ||
1616 | ptask->addr = addr; | |
1617 | ptask->dest_node = dest_node; | |
1618 | } | |
1619 | ||
1620 | ptask->tx_type = tx_type; | |
1621 | ptask->max_payload = max_payload; | |
efbeccf1 SR |
1622 | ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload, |
1623 | proto, &ptask->hdr, dg_size, dgl); | |
1da177e4 LT |
1624 | |
1625 | /* Add the encapsulation header to the fragment */ | |
1626 | tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr); | |
1627 | dev->trans_start = jiffies; | |
1628 | if (ether1394_send_packet(ptask, tx_len)) | |
1629 | goto fail; | |
1630 | ||
1631 | netif_wake_queue(dev); | |
fdc0092b | 1632 | return NETDEV_TX_OK; |
1da177e4 LT |
1633 | fail: |
1634 | if (ptask) | |
1635 | kmem_cache_free(packet_task_cache, ptask); | |
1636 | ||
1637 | if (skb != NULL) | |
1638 | dev_kfree_skb(skb); | |
1639 | ||
efbeccf1 | 1640 | spin_lock_irqsave(&priv->lock, flags); |
1da177e4 LT |
1641 | priv->stats.tx_dropped++; |
1642 | priv->stats.tx_errors++; | |
efbeccf1 | 1643 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 LT |
1644 | |
1645 | if (netif_queue_stopped(dev)) | |
1646 | netif_wake_queue(dev); | |
1647 | ||
fdc0092b SR |
1648 | /* |
1649 | * FIXME: According to a patch from 2003-02-26, "returning non-zero | |
1650 | * causes serious problems" here, allegedly. Before that patch, | |
1651 | * -ERRNO was returned which is not appropriate under Linux 2.6. | |
1652 | * Perhaps more needs to be done? Stop the queue in serious | |
1653 | * conditions and restart it elsewhere? | |
1654 | */ | |
1655 | /* return NETDEV_TX_BUSY; */ | |
1656 | return NETDEV_TX_OK; | |
1da177e4 LT |
1657 | } |
1658 | ||
efbeccf1 SR |
1659 | static void ether1394_get_drvinfo(struct net_device *dev, |
1660 | struct ethtool_drvinfo *info) | |
1da177e4 | 1661 | { |
efbeccf1 SR |
1662 | strcpy(info->driver, driver_name); |
1663 | strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */ | |
1da177e4 LT |
1664 | } |
1665 | ||
1666 | static struct ethtool_ops ethtool_ops = { | |
1667 | .get_drvinfo = ether1394_get_drvinfo | |
1668 | }; | |
1669 | ||
809e905c | 1670 | static int __init ether1394_init_module(void) |
1da177e4 | 1671 | { |
809e905c AM |
1672 | int err; |
1673 | ||
efbeccf1 SR |
1674 | packet_task_cache = kmem_cache_create("packet_task", |
1675 | sizeof(struct packet_task), | |
1da177e4 | 1676 | 0, 0, NULL, NULL); |
809e905c AM |
1677 | if (!packet_task_cache) |
1678 | return -ENOMEM; | |
1da177e4 | 1679 | |
1da177e4 | 1680 | hpsb_register_highlevel(ð1394_highlevel); |
809e905c AM |
1681 | err = hpsb_register_protocol(ð1394_proto_driver); |
1682 | if (err) { | |
1683 | hpsb_unregister_highlevel(ð1394_highlevel); | |
1684 | kmem_cache_destroy(packet_task_cache); | |
1685 | } | |
1686 | return err; | |
1da177e4 LT |
1687 | } |
1688 | ||
809e905c | 1689 | static void __exit ether1394_exit_module(void) |
1da177e4 LT |
1690 | { |
1691 | hpsb_unregister_protocol(ð1394_proto_driver); | |
1692 | hpsb_unregister_highlevel(ð1394_highlevel); | |
1693 | kmem_cache_destroy(packet_task_cache); | |
1694 | } | |
1695 | ||
1696 | module_init(ether1394_init_module); | |
1697 | module_exit(ether1394_exit_module); |