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 */ | |
21b2c564 SR |
478 | /* FIXME: This is broken for nodes with link speed < PHY speed, |
479 | * and it is suboptimal for S200B...S800B hardware. | |
480 | * The result of nodemgr's speed probe should be used somehow. */ | |
481 | for (i = 0; i < host->node_count; i++) { | |
482 | /* take care of S100B...S400B PHY ports */ | |
483 | if (host->speed[i] == SELFID_SPEED_UNKNOWN) { | |
484 | max_speed = IEEE1394_SPEED_100; | |
485 | break; | |
486 | } | |
647dcb5f BC |
487 | if (max_speed > host->speed[i]) |
488 | max_speed = host->speed[i]; | |
21b2c564 | 489 | } |
1da177e4 LT |
490 | priv->bc_sspd = max_speed; |
491 | ||
1da177e4 | 492 | if (set_mtu) { |
17bab407 SR |
493 | /* Use the RFC 2734 default 1500 octets or the maximum payload |
494 | * as initial MTU */ | |
495 | dev->mtu = min(1500, ether1394_max_mtu(host)); | |
1da177e4 LT |
496 | |
497 | /* Set our hardware address while we're at it */ | |
c20e3945 DM |
498 | memcpy(dev->dev_addr, &guid, sizeof(u64)); |
499 | memset(dev->broadcast, 0xff, sizeof(u64)); | |
1da177e4 LT |
500 | } |
501 | ||
efbeccf1 | 502 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 LT |
503 | } |
504 | ||
efbeccf1 | 505 | static void ether1394_init_dev(struct net_device *dev) |
1da177e4 | 506 | { |
1da177e4 LT |
507 | dev->open = ether1394_open; |
508 | dev->stop = ether1394_stop; | |
509 | dev->hard_start_xmit = ether1394_tx; | |
510 | dev->get_stats = ether1394_stats; | |
511 | dev->tx_timeout = ether1394_tx_timeout; | |
512 | dev->change_mtu = ether1394_change_mtu; | |
513 | ||
514 | dev->hard_header = ether1394_header; | |
515 | dev->rebuild_header = ether1394_rebuild_header; | |
516 | dev->hard_header_cache = ether1394_header_cache; | |
517 | dev->header_cache_update= ether1394_header_cache_update; | |
518 | dev->hard_header_parse = ether1394_header_parse; | |
01590d20 | 519 | |
1da177e4 LT |
520 | SET_ETHTOOL_OPS(dev, ðtool_ops); |
521 | ||
1da177e4 LT |
522 | dev->watchdog_timeo = ETHER1394_TIMEOUT; |
523 | dev->flags = IFF_BROADCAST | IFF_MULTICAST; | |
524 | dev->features = NETIF_F_HIGHDMA; | |
525 | dev->addr_len = ETH1394_ALEN; | |
526 | dev->hard_header_len = ETH1394_HLEN; | |
527 | dev->type = ARPHRD_IEEE1394; | |
528 | ||
01590d20 SR |
529 | /* FIXME: This value was copied from ether_setup(). Is it too much? */ |
530 | dev->tx_queue_len = 1000; | |
1da177e4 LT |
531 | } |
532 | ||
533 | /* | |
534 | * This function is called every time a card is found. It is generally called | |
535 | * when the module is installed. This is where we add all of our ethernet | |
536 | * devices. One for each host. | |
537 | */ | |
efbeccf1 | 538 | static void ether1394_add_host(struct hpsb_host *host) |
1da177e4 LT |
539 | { |
540 | struct eth1394_host_info *hi = NULL; | |
541 | struct net_device *dev = NULL; | |
542 | struct eth1394_priv *priv; | |
1da177e4 LT |
543 | u64 fifo_addr; |
544 | ||
70093cfd SR |
545 | if (hpsb_config_rom_ip1394_add(host) != 0) { |
546 | ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n"); | |
1da177e4 | 547 | return; |
70093cfd | 548 | } |
1da177e4 | 549 | |
6737231e BC |
550 | fifo_addr = hpsb_allocate_and_register_addrspace( |
551 | ð1394_highlevel, host, &addr_ops, | |
552 | ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN, | |
553 | CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE); | |
157188cb SR |
554 | if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) { |
555 | ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n"); | |
70093cfd | 556 | hpsb_config_rom_ip1394_remove(host); |
157188cb SR |
557 | return; |
558 | } | |
1da177e4 | 559 | |
01590d20 | 560 | dev = alloc_netdev(sizeof(*priv), "eth%d", ether1394_init_dev); |
1da177e4 | 561 | if (dev == NULL) { |
5009d269 | 562 | ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); |
1da177e4 | 563 | goto out; |
efbeccf1 | 564 | } |
1da177e4 LT |
565 | |
566 | SET_MODULE_OWNER(dev); | |
7a9eeb2f SR |
567 | #if 0 |
568 | /* FIXME - Is this the correct parent device anyway? */ | |
1da177e4 | 569 | SET_NETDEV_DEV(dev, &host->device); |
7a9eeb2f | 570 | #endif |
1da177e4 LT |
571 | |
572 | priv = netdev_priv(dev); | |
1da177e4 | 573 | INIT_LIST_HEAD(&priv->ip_node_list); |
1da177e4 LT |
574 | spin_lock_init(&priv->lock); |
575 | priv->host = host; | |
576 | priv->local_fifo = fifo_addr; | |
577 | ||
578 | hi = hpsb_create_hostinfo(ð1394_highlevel, host, sizeof(*hi)); | |
1da177e4 | 579 | if (hi == NULL) { |
5009d269 | 580 | ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); |
1da177e4 | 581 | goto out; |
efbeccf1 | 582 | } |
1da177e4 | 583 | |
01590d20 | 584 | ether1394_reset_priv(dev, 1); |
1da177e4 | 585 | |
5009d269 SR |
586 | if (register_netdev(dev)) { |
587 | ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n"); | |
1da177e4 LT |
588 | goto out; |
589 | } | |
590 | ||
5009d269 SR |
591 | ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n", |
592 | host->id); | |
1da177e4 LT |
593 | |
594 | hi->host = host; | |
595 | hi->dev = dev; | |
596 | ||
597 | /* Ignore validity in hopes that it will be set in the future. It'll | |
598 | * be checked when the eth device is opened. */ | |
599 | priv->broadcast_channel = host->csr.broadcast_channel & 0x3f; | |
600 | ||
5009d269 | 601 | ether1394_recv_init(priv); |
1da177e4 | 602 | return; |
1da177e4 | 603 | out: |
157188cb | 604 | if (dev) |
1da177e4 LT |
605 | free_netdev(dev); |
606 | if (hi) | |
607 | hpsb_destroy_hostinfo(ð1394_highlevel, host); | |
157188cb | 608 | hpsb_unregister_addrspace(ð1394_highlevel, host, fifo_addr); |
70093cfd | 609 | hpsb_config_rom_ip1394_remove(host); |
1da177e4 LT |
610 | } |
611 | ||
612 | /* Remove a card from our list */ | |
efbeccf1 | 613 | static void ether1394_remove_host(struct hpsb_host *host) |
1da177e4 LT |
614 | { |
615 | struct eth1394_host_info *hi; | |
2cd556ae | 616 | struct eth1394_priv *priv; |
1da177e4 LT |
617 | |
618 | hi = hpsb_get_hostinfo(ð1394_highlevel, host); | |
2cd556ae SR |
619 | if (!hi) |
620 | return; | |
621 | priv = netdev_priv(hi->dev); | |
622 | hpsb_unregister_addrspace(ð1394_highlevel, host, priv->local_fifo); | |
70093cfd | 623 | hpsb_config_rom_ip1394_remove(host); |
2cd556ae SR |
624 | if (priv->iso) |
625 | hpsb_iso_shutdown(priv->iso); | |
626 | unregister_netdev(hi->dev); | |
627 | free_netdev(hi->dev); | |
1da177e4 LT |
628 | } |
629 | ||
efbeccf1 SR |
630 | /* A bus reset happened */ |
631 | static void ether1394_host_reset(struct hpsb_host *host) | |
1da177e4 LT |
632 | { |
633 | struct eth1394_host_info *hi; | |
634 | struct eth1394_priv *priv; | |
635 | struct net_device *dev; | |
636 | struct list_head *lh, *n; | |
637 | struct eth1394_node_ref *node; | |
638 | struct eth1394_node_info *node_info; | |
639 | unsigned long flags; | |
640 | ||
641 | hi = hpsb_get_hostinfo(ð1394_highlevel, host); | |
642 | ||
643 | /* This can happen for hosts that we don't use */ | |
2cd556ae | 644 | if (!hi) |
1da177e4 LT |
645 | return; |
646 | ||
647 | dev = hi->dev; | |
efbeccf1 | 648 | priv = netdev_priv(dev); |
1da177e4 | 649 | |
efbeccf1 SR |
650 | /* Reset our private host data, but not our MTU */ |
651 | netif_stop_queue(dev); | |
652 | ether1394_reset_priv(dev, 0); | |
1da177e4 LT |
653 | |
654 | list_for_each_entry(node, &priv->ip_node_list, list) { | |
efbeccf1 | 655 | node_info = node->ud->device.driver_data; |
1da177e4 LT |
656 | |
657 | spin_lock_irqsave(&node_info->pdg.lock, flags); | |
658 | ||
efbeccf1 | 659 | list_for_each_safe(lh, n, &node_info->pdg.list) |
1da177e4 | 660 | purge_partial_datagram(lh); |
1da177e4 LT |
661 | |
662 | INIT_LIST_HEAD(&(node_info->pdg.list)); | |
663 | node_info->pdg.sz = 0; | |
664 | ||
665 | spin_unlock_irqrestore(&node_info->pdg.lock, flags); | |
666 | } | |
667 | ||
efbeccf1 | 668 | netif_wake_queue(dev); |
1da177e4 LT |
669 | } |
670 | ||
671 | /****************************************** | |
672 | * HW Header net device functions | |
673 | ******************************************/ | |
674 | /* These functions have been adapted from net/ethernet/eth.c */ | |
675 | ||
1da177e4 LT |
676 | /* Create a fake MAC header for an arbitrary protocol layer. |
677 | * saddr=NULL means use device source address | |
678 | * daddr=NULL means leave destination address (eg unresolved arp). */ | |
679 | static int ether1394_header(struct sk_buff *skb, struct net_device *dev, | |
680 | unsigned short type, void *daddr, void *saddr, | |
681 | unsigned len) | |
682 | { | |
efbeccf1 SR |
683 | struct eth1394hdr *eth = |
684 | (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN); | |
1da177e4 LT |
685 | |
686 | eth->h_proto = htons(type); | |
687 | ||
efbeccf1 | 688 | if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) { |
1da177e4 | 689 | memset(eth->h_dest, 0, dev->addr_len); |
efbeccf1 | 690 | return dev->hard_header_len; |
1da177e4 LT |
691 | } |
692 | ||
693 | if (daddr) { | |
efbeccf1 | 694 | memcpy(eth->h_dest, daddr, dev->addr_len); |
1da177e4 LT |
695 | return dev->hard_header_len; |
696 | } | |
697 | ||
698 | return -dev->hard_header_len; | |
1da177e4 LT |
699 | } |
700 | ||
1da177e4 LT |
701 | /* Rebuild the faked MAC header. This is called after an ARP |
702 | * (or in future other address resolution) has completed on this | |
703 | * sk_buff. We now let ARP fill in the other fields. | |
704 | * | |
705 | * This routine CANNOT use cached dst->neigh! | |
706 | * Really, it is used only when dst->neigh is wrong. | |
707 | */ | |
708 | static int ether1394_rebuild_header(struct sk_buff *skb) | |
709 | { | |
710 | struct eth1394hdr *eth = (struct eth1394hdr *)skb->data; | |
1da177e4 | 711 | |
599bba96 | 712 | if (eth->h_proto == htons(ETH_P_IP)) |
efbeccf1 | 713 | return arp_find((unsigned char *)ð->h_dest, skb); |
1da177e4 | 714 | |
599bba96 SR |
715 | ETH1394_PRINT(KERN_DEBUG, skb->dev->name, |
716 | "unable to resolve type %04x addresses\n", | |
717 | ntohs(eth->h_proto)); | |
1da177e4 LT |
718 | return 0; |
719 | } | |
720 | ||
721 | static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr) | |
722 | { | |
723 | struct net_device *dev = skb->dev; | |
efbeccf1 | 724 | |
1da177e4 LT |
725 | memcpy(haddr, dev->dev_addr, ETH1394_ALEN); |
726 | return ETH1394_ALEN; | |
727 | } | |
728 | ||
1da177e4 LT |
729 | static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh) |
730 | { | |
731 | unsigned short type = hh->hh_type; | |
1da177e4 | 732 | struct net_device *dev = neigh->dev; |
efbeccf1 SR |
733 | struct eth1394hdr *eth = |
734 | (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN); | |
1da177e4 | 735 | |
7136b807 | 736 | if (type == htons(ETH_P_802_3)) |
1da177e4 | 737 | return -1; |
1da177e4 LT |
738 | |
739 | eth->h_proto = type; | |
740 | memcpy(eth->h_dest, neigh->ha, dev->addr_len); | |
741 | ||
742 | hh->hh_len = ETH1394_HLEN; | |
743 | return 0; | |
744 | } | |
745 | ||
746 | /* Called by Address Resolution module to notify changes in address. */ | |
747 | static void ether1394_header_cache_update(struct hh_cache *hh, | |
748 | struct net_device *dev, | |
749 | unsigned char * haddr) | |
750 | { | |
efbeccf1 | 751 | memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len); |
1da177e4 LT |
752 | } |
753 | ||
1da177e4 LT |
754 | /****************************************** |
755 | * Datagram reception code | |
756 | ******************************************/ | |
757 | ||
758 | /* Copied from net/ethernet/eth.c */ | |
e00f04a7 | 759 | static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
760 | { |
761 | struct eth1394hdr *eth; | |
762 | unsigned char *rawp; | |
763 | ||
459a98ed | 764 | skb_reset_mac_header(skb); |
efbeccf1 | 765 | skb_pull(skb, ETH1394_HLEN); |
1da177e4 LT |
766 | eth = eth1394_hdr(skb); |
767 | ||
768 | if (*eth->h_dest & 1) { | |
efbeccf1 | 769 | if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0) |
1da177e4 LT |
770 | skb->pkt_type = PACKET_BROADCAST; |
771 | #if 0 | |
772 | else | |
773 | skb->pkt_type = PACKET_MULTICAST; | |
774 | #endif | |
775 | } else { | |
776 | if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len)) | |
777 | skb->pkt_type = PACKET_OTHERHOST; | |
efbeccf1 | 778 | } |
1da177e4 | 779 | |
efbeccf1 | 780 | if (ntohs(eth->h_proto) >= 1536) |
1da177e4 LT |
781 | return eth->h_proto; |
782 | ||
783 | rawp = skb->data; | |
784 | ||
efbeccf1 SR |
785 | if (*(unsigned short *)rawp == 0xFFFF) |
786 | return htons(ETH_P_802_3); | |
1da177e4 | 787 | |
efbeccf1 | 788 | return htons(ETH_P_802_2); |
1da177e4 LT |
789 | } |
790 | ||
791 | /* Parse an encapsulated IP1394 header into an ethernet frame packet. | |
792 | * We also perform ARP translation here, if need be. */ | |
e00f04a7 SR |
793 | static u16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev, |
794 | nodeid_t srcid, nodeid_t destid, | |
795 | u16 ether_type) | |
1da177e4 LT |
796 | { |
797 | struct eth1394_priv *priv = netdev_priv(dev); | |
798 | u64 dest_hw; | |
799 | unsigned short ret = 0; | |
800 | ||
efbeccf1 | 801 | /* Setup our hw addresses. We use these to build the ethernet header. */ |
1da177e4 LT |
802 | if (destid == (LOCAL_BUS | ALL_NODES)) |
803 | dest_hw = ~0ULL; /* broadcast */ | |
804 | else | |
efbeccf1 | 805 | dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 | |
1da177e4 LT |
806 | priv->host->csr.guid_lo); |
807 | ||
808 | /* If this is an ARP packet, convert it. First, we want to make | |
809 | * use of some of the fields, since they tell us a little bit | |
810 | * about the sending machine. */ | |
7136b807 | 811 | if (ether_type == htons(ETH_P_ARP)) { |
efbeccf1 | 812 | struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data; |
1da177e4 LT |
813 | struct arphdr *arp = (struct arphdr *)skb->data; |
814 | unsigned char *arp_ptr = (unsigned char *)(arp + 1); | |
815 | u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 | | |
efbeccf1 | 816 | ntohl(arp1394->fifo_lo); |
1da177e4 LT |
817 | u8 max_rec = min(priv->host->csr.max_rec, |
818 | (u8)(arp1394->max_rec)); | |
819 | int sspd = arp1394->sspd; | |
820 | u16 maxpayload; | |
821 | struct eth1394_node_ref *node; | |
822 | struct eth1394_node_info *node_info; | |
c20e3945 | 823 | __be64 guid; |
1da177e4 LT |
824 | |
825 | /* Sanity check. MacOSX seems to be sending us 131 in this | |
826 | * field (atleast on my Panther G5). Not sure why. */ | |
827 | if (sspd > 5 || sspd < 0) | |
828 | sspd = 0; | |
829 | ||
efbeccf1 SR |
830 | maxpayload = min(eth1394_speedto_maxpayload[sspd], |
831 | (u16)(1 << (max_rec + 1))); | |
1da177e4 | 832 | |
c20e3945 | 833 | guid = get_unaligned(&arp1394->s_uniq_id); |
1da177e4 | 834 | node = eth1394_find_node_guid(&priv->ip_node_list, |
c20e3945 | 835 | be64_to_cpu(guid)); |
efbeccf1 | 836 | if (!node) |
1da177e4 | 837 | return 0; |
1da177e4 | 838 | |
efbeccf1 SR |
839 | node_info = |
840 | (struct eth1394_node_info *)node->ud->device.driver_data; | |
1da177e4 LT |
841 | |
842 | /* Update our speed/payload/fifo_offset table */ | |
843 | node_info->maxpayload = maxpayload; | |
844 | node_info->sspd = sspd; | |
845 | node_info->fifo = fifo_addr; | |
846 | ||
847 | /* Now that we're done with the 1394 specific stuff, we'll | |
848 | * need to alter some of the data. Believe it or not, all | |
849 | * that needs to be done is sender_IP_address needs to be | |
850 | * moved, the destination hardware address get stuffed | |
851 | * in and the hardware address length set to 8. | |
852 | * | |
853 | * IMPORTANT: The code below overwrites 1394 specific data | |
854 | * needed above so keep the munging of the data for the | |
855 | * higher level IP stack last. */ | |
856 | ||
857 | arp->ar_hln = 8; | |
858 | arp_ptr += arp->ar_hln; /* skip over sender unique id */ | |
efbeccf1 | 859 | *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */ |
1da177e4 LT |
860 | arp_ptr += arp->ar_pln; /* skip over sender IP addr */ |
861 | ||
02f4213f | 862 | if (arp->ar_op == htons(ARPOP_REQUEST)) |
c20e3945 | 863 | memset(arp_ptr, 0, sizeof(u64)); |
1da177e4 | 864 | else |
c20e3945 | 865 | memcpy(arp_ptr, dev->dev_addr, sizeof(u64)); |
1da177e4 LT |
866 | } |
867 | ||
868 | /* Now add the ethernet header. */ | |
7136b807 BC |
869 | if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL, |
870 | skb->len) >= 0) | |
1da177e4 LT |
871 | ret = ether1394_type_trans(skb, dev); |
872 | ||
873 | return ret; | |
874 | } | |
875 | ||
e00f04a7 | 876 | static int fragment_overlap(struct list_head *frag_list, int offset, int len) |
1da177e4 LT |
877 | { |
878 | struct fragment_info *fi; | |
2e2173df | 879 | int end = offset + len; |
1da177e4 | 880 | |
2e2173df SR |
881 | list_for_each_entry(fi, frag_list, list) |
882 | if (offset < fi->offset + fi->len && end > fi->offset) | |
1da177e4 | 883 | return 1; |
2e2173df | 884 | |
1da177e4 LT |
885 | return 0; |
886 | } | |
887 | ||
e00f04a7 | 888 | static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl) |
1da177e4 LT |
889 | { |
890 | struct partial_datagram *pd; | |
891 | ||
efbeccf1 | 892 | list_for_each_entry(pd, pdgl, list) |
1da177e4 LT |
893 | if (pd->dgl == dgl) |
894 | return &pd->list; | |
efbeccf1 | 895 | |
1da177e4 LT |
896 | return NULL; |
897 | } | |
898 | ||
899 | /* Assumes that new fragment does not overlap any existing fragments */ | |
e00f04a7 | 900 | static int new_fragment(struct list_head *frag_info, int offset, int len) |
1da177e4 LT |
901 | { |
902 | struct list_head *lh; | |
903 | struct fragment_info *fi, *fi2, *new; | |
904 | ||
905 | list_for_each(lh, frag_info) { | |
906 | fi = list_entry(lh, struct fragment_info, list); | |
efbeccf1 | 907 | if (fi->offset + fi->len == offset) { |
1da177e4 LT |
908 | /* The new fragment can be tacked on to the end */ |
909 | fi->len += len; | |
910 | /* Did the new fragment plug a hole? */ | |
911 | fi2 = list_entry(lh->next, struct fragment_info, list); | |
efbeccf1 | 912 | if (fi->offset + fi->len == fi2->offset) { |
1da177e4 LT |
913 | /* glue fragments together */ |
914 | fi->len += fi2->len; | |
915 | list_del(lh->next); | |
916 | kfree(fi2); | |
917 | } | |
918 | return 0; | |
efbeccf1 | 919 | } else if (offset + len == fi->offset) { |
1da177e4 LT |
920 | /* The new fragment can be tacked on to the beginning */ |
921 | fi->offset = offset; | |
922 | fi->len += len; | |
923 | /* Did the new fragment plug a hole? */ | |
924 | fi2 = list_entry(lh->prev, struct fragment_info, list); | |
efbeccf1 | 925 | if (fi2->offset + fi2->len == fi->offset) { |
1da177e4 LT |
926 | /* glue fragments together */ |
927 | fi2->len += fi->len; | |
928 | list_del(lh); | |
929 | kfree(fi); | |
930 | } | |
931 | return 0; | |
efbeccf1 | 932 | } else if (offset > fi->offset + fi->len) { |
1da177e4 | 933 | break; |
efbeccf1 | 934 | } else if (offset + len < fi->offset) { |
1da177e4 LT |
935 | lh = lh->prev; |
936 | break; | |
937 | } | |
938 | } | |
939 | ||
8551158a | 940 | new = kmalloc(sizeof(*new), GFP_ATOMIC); |
1da177e4 LT |
941 | if (!new) |
942 | return -ENOMEM; | |
943 | ||
944 | new->offset = offset; | |
945 | new->len = len; | |
946 | ||
947 | list_add(&new->list, lh); | |
1da177e4 LT |
948 | return 0; |
949 | } | |
950 | ||
e00f04a7 SR |
951 | static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl, |
952 | int dgl, int dg_size, char *frag_buf, | |
953 | int frag_off, int frag_len) | |
1da177e4 LT |
954 | { |
955 | struct partial_datagram *new; | |
956 | ||
8551158a | 957 | new = kmalloc(sizeof(*new), GFP_ATOMIC); |
1da177e4 LT |
958 | if (!new) |
959 | return -ENOMEM; | |
960 | ||
961 | INIT_LIST_HEAD(&new->frag_info); | |
962 | ||
963 | if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) { | |
964 | kfree(new); | |
965 | return -ENOMEM; | |
966 | } | |
967 | ||
968 | new->dgl = dgl; | |
969 | new->dg_size = dg_size; | |
970 | ||
971 | new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15); | |
972 | if (!new->skb) { | |
973 | struct fragment_info *fi = list_entry(new->frag_info.next, | |
974 | struct fragment_info, | |
975 | list); | |
976 | kfree(fi); | |
977 | kfree(new); | |
978 | return -ENOMEM; | |
979 | } | |
980 | ||
981 | skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15); | |
982 | new->pbuf = skb_put(new->skb, dg_size); | |
983 | memcpy(new->pbuf + frag_off, frag_buf, frag_len); | |
984 | ||
985 | list_add(&new->list, pdgl); | |
1da177e4 LT |
986 | return 0; |
987 | } | |
988 | ||
e00f04a7 SR |
989 | static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh, |
990 | char *frag_buf, int frag_off, int frag_len) | |
1da177e4 | 991 | { |
efbeccf1 SR |
992 | struct partial_datagram *pd = |
993 | list_entry(lh, struct partial_datagram, list); | |
1da177e4 | 994 | |
efbeccf1 | 995 | if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0) |
1da177e4 | 996 | return -ENOMEM; |
1da177e4 LT |
997 | |
998 | memcpy(pd->pbuf + frag_off, frag_buf, frag_len); | |
999 | ||
1000 | /* Move list entry to beginnig of list so that oldest partial | |
1001 | * datagrams percolate to the end of the list */ | |
179e0917 | 1002 | list_move(lh, pdgl); |
1da177e4 LT |
1003 | return 0; |
1004 | } | |
1005 | ||
e00f04a7 | 1006 | static int is_datagram_complete(struct list_head *lh, int dg_size) |
1da177e4 | 1007 | { |
e00f04a7 SR |
1008 | struct partial_datagram *pd; |
1009 | struct fragment_info *fi; | |
1010 | ||
1011 | pd = list_entry(lh, struct partial_datagram, list); | |
1012 | fi = list_entry(pd->frag_info.next, struct fragment_info, list); | |
1da177e4 LT |
1013 | |
1014 | return (fi->len == dg_size); | |
1015 | } | |
1016 | ||
1017 | /* Packet reception. We convert the IP1394 encapsulation header to an | |
1018 | * ethernet header, and fill it with some of our other fields. This is | |
1019 | * an incoming packet from the 1394 bus. */ | |
1020 | static int ether1394_data_handler(struct net_device *dev, int srcid, int destid, | |
1021 | char *buf, int len) | |
1022 | { | |
1023 | struct sk_buff *skb; | |
1024 | unsigned long flags; | |
1025 | struct eth1394_priv *priv = netdev_priv(dev); | |
1026 | union eth1394_hdr *hdr = (union eth1394_hdr *)buf; | |
1027 | u16 ether_type = 0; /* initialized to clear warning */ | |
1028 | int hdr_len; | |
1029 | struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)]; | |
1030 | struct eth1394_node_info *node_info; | |
1031 | ||
1032 | if (!ud) { | |
1033 | struct eth1394_node_ref *node; | |
1034 | node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid); | |
09939871 | 1035 | if (unlikely(!node)) { |
1da177e4 LT |
1036 | HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid " |
1037 | "lookup failure: " NODE_BUS_FMT, | |
1038 | NODE_BUS_ARGS(priv->host, srcid)); | |
1039 | priv->stats.rx_dropped++; | |
1040 | return -1; | |
1041 | } | |
1042 | ud = node->ud; | |
1043 | ||
1044 | priv->ud_list[NODEID_TO_NODE(srcid)] = ud; | |
1045 | } | |
1046 | ||
efbeccf1 | 1047 | node_info = (struct eth1394_node_info *)ud->device.driver_data; |
1da177e4 LT |
1048 | |
1049 | /* First, did we receive a fragmented or unfragmented datagram? */ | |
1050 | hdr->words.word1 = ntohs(hdr->words.word1); | |
1051 | ||
1052 | hdr_len = hdr_type_len[hdr->common.lf]; | |
1053 | ||
1054 | if (hdr->common.lf == ETH1394_HDR_LF_UF) { | |
1055 | /* An unfragmented datagram has been received by the ieee1394 | |
1056 | * bus. Build an skbuff around it so we can pass it to the | |
1057 | * high level network layer. */ | |
1058 | ||
1059 | skb = dev_alloc_skb(len + dev->hard_header_len + 15); | |
09939871 | 1060 | if (unlikely(!skb)) { |
5009d269 | 1061 | ETH1394_PRINT_G(KERN_ERR, "Out of memory\n"); |
1da177e4 LT |
1062 | priv->stats.rx_dropped++; |
1063 | return -1; | |
1064 | } | |
1065 | skb_reserve(skb, (dev->hard_header_len + 15) & ~15); | |
efbeccf1 SR |
1066 | memcpy(skb_put(skb, len - hdr_len), buf + hdr_len, |
1067 | len - hdr_len); | |
1da177e4 LT |
1068 | ether_type = hdr->uf.ether_type; |
1069 | } else { | |
1070 | /* A datagram fragment has been received, now the fun begins. */ | |
1071 | ||
1072 | struct list_head *pdgl, *lh; | |
1073 | struct partial_datagram *pd; | |
1074 | int fg_off; | |
1075 | int fg_len = len - hdr_len; | |
1076 | int dg_size; | |
1077 | int dgl; | |
1078 | int retval; | |
1079 | struct pdg_list *pdg = &(node_info->pdg); | |
1080 | ||
1081 | hdr->words.word3 = ntohs(hdr->words.word3); | |
1082 | /* The 4th header word is reserved so no need to do ntohs() */ | |
1083 | ||
1084 | if (hdr->common.lf == ETH1394_HDR_LF_FF) { | |
1085 | ether_type = hdr->ff.ether_type; | |
1086 | dgl = hdr->ff.dgl; | |
1087 | dg_size = hdr->ff.dg_size + 1; | |
1088 | fg_off = 0; | |
1089 | } else { | |
1090 | hdr->words.word2 = ntohs(hdr->words.word2); | |
1091 | dgl = hdr->sf.dgl; | |
1092 | dg_size = hdr->sf.dg_size + 1; | |
1093 | fg_off = hdr->sf.fg_off; | |
1094 | } | |
1095 | spin_lock_irqsave(&pdg->lock, flags); | |
1096 | ||
1097 | pdgl = &(pdg->list); | |
1098 | lh = find_partial_datagram(pdgl, dgl); | |
1099 | ||
1100 | if (lh == NULL) { | |
1101 | while (pdg->sz >= max_partial_datagrams) { | |
1102 | /* remove the oldest */ | |
1103 | purge_partial_datagram(pdgl->prev); | |
1104 | pdg->sz--; | |
1105 | } | |
1106 | ||
1107 | retval = new_partial_datagram(dev, pdgl, dgl, dg_size, | |
1108 | buf + hdr_len, fg_off, | |
1109 | fg_len); | |
1110 | if (retval < 0) { | |
1111 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1112 | goto bad_proto; | |
1113 | } | |
1114 | pdg->sz++; | |
1115 | lh = find_partial_datagram(pdgl, dgl); | |
1116 | } else { | |
1117 | struct partial_datagram *pd; | |
1118 | ||
1119 | pd = list_entry(lh, struct partial_datagram, list); | |
1120 | ||
1121 | if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) { | |
1122 | /* Overlapping fragments, obliterate old | |
1123 | * datagram and start new one. */ | |
1124 | purge_partial_datagram(lh); | |
1125 | retval = new_partial_datagram(dev, pdgl, dgl, | |
1126 | dg_size, | |
1127 | buf + hdr_len, | |
1128 | fg_off, fg_len); | |
1129 | if (retval < 0) { | |
1130 | pdg->sz--; | |
1131 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1132 | goto bad_proto; | |
1133 | } | |
1134 | } else { | |
1135 | retval = update_partial_datagram(pdgl, lh, | |
1136 | buf + hdr_len, | |
1137 | fg_off, fg_len); | |
1138 | if (retval < 0) { | |
1139 | /* Couldn't save off fragment anyway | |
1140 | * so might as well obliterate the | |
1141 | * datagram now. */ | |
1142 | purge_partial_datagram(lh); | |
1143 | pdg->sz--; | |
1144 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1145 | goto bad_proto; | |
1146 | } | |
1147 | } /* fragment overlap */ | |
1148 | } /* new datagram or add to existing one */ | |
1149 | ||
1150 | pd = list_entry(lh, struct partial_datagram, list); | |
1151 | ||
efbeccf1 | 1152 | if (hdr->common.lf == ETH1394_HDR_LF_FF) |
1da177e4 | 1153 | pd->ether_type = ether_type; |
1da177e4 LT |
1154 | |
1155 | if (is_datagram_complete(lh, dg_size)) { | |
1156 | ether_type = pd->ether_type; | |
1157 | pdg->sz--; | |
1158 | skb = skb_get(pd->skb); | |
1159 | purge_partial_datagram(lh); | |
1160 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1161 | } else { | |
1162 | /* Datagram is not complete, we're done for the | |
1163 | * moment. */ | |
1164 | spin_unlock_irqrestore(&pdg->lock, flags); | |
1165 | return 0; | |
1166 | } | |
1167 | } /* unframgented datagram or fragmented one */ | |
1168 | ||
1169 | /* Write metadata, and then pass to the receive level */ | |
1170 | skb->dev = dev; | |
1171 | skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ | |
1172 | ||
1173 | /* Parse the encapsulation header. This actually does the job of | |
1174 | * converting to an ethernet frame header, aswell as arp | |
1175 | * conversion if needed. ARP conversion is easier in this | |
1176 | * direction, since we are using ethernet as our backend. */ | |
1177 | skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid, | |
1178 | ether_type); | |
1179 | ||
1da177e4 | 1180 | spin_lock_irqsave(&priv->lock, flags); |
efbeccf1 | 1181 | |
1da177e4 LT |
1182 | if (!skb->protocol) { |
1183 | priv->stats.rx_errors++; | |
1184 | priv->stats.rx_dropped++; | |
1185 | dev_kfree_skb_any(skb); | |
1186 | goto bad_proto; | |
1187 | } | |
1188 | ||
1189 | if (netif_rx(skb) == NET_RX_DROP) { | |
1190 | priv->stats.rx_errors++; | |
1191 | priv->stats.rx_dropped++; | |
1192 | goto bad_proto; | |
1193 | } | |
1194 | ||
1195 | /* Statistics */ | |
1196 | priv->stats.rx_packets++; | |
1197 | priv->stats.rx_bytes += skb->len; | |
1198 | ||
1199 | bad_proto: | |
1200 | if (netif_queue_stopped(dev)) | |
1201 | netif_wake_queue(dev); | |
1202 | spin_unlock_irqrestore(&priv->lock, flags); | |
1203 | ||
1204 | dev->last_rx = jiffies; | |
1205 | ||
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | static int ether1394_write(struct hpsb_host *host, int srcid, int destid, | |
1210 | quadlet_t *data, u64 addr, size_t len, u16 flags) | |
1211 | { | |
1212 | struct eth1394_host_info *hi; | |
1213 | ||
1214 | hi = hpsb_get_hostinfo(ð1394_highlevel, host); | |
09939871 | 1215 | if (unlikely(!hi)) { |
5009d269 SR |
1216 | ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n", |
1217 | host->id); | |
1da177e4 LT |
1218 | return RCODE_ADDRESS_ERROR; |
1219 | } | |
1220 | ||
1221 | if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len)) | |
1222 | return RCODE_ADDRESS_ERROR; | |
1223 | else | |
1224 | return RCODE_COMPLETE; | |
1225 | } | |
1226 | ||
1227 | static void ether1394_iso(struct hpsb_iso *iso) | |
1228 | { | |
1229 | quadlet_t *data; | |
1230 | char *buf; | |
1231 | struct eth1394_host_info *hi; | |
1232 | struct net_device *dev; | |
1233 | struct eth1394_priv *priv; | |
1234 | unsigned int len; | |
1235 | u32 specifier_id; | |
1236 | u16 source_id; | |
1237 | int i; | |
1238 | int nready; | |
1239 | ||
1240 | hi = hpsb_get_hostinfo(ð1394_highlevel, iso->host); | |
09939871 | 1241 | if (unlikely(!hi)) { |
5009d269 SR |
1242 | ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n", |
1243 | iso->host->id); | |
1da177e4 LT |
1244 | return; |
1245 | } | |
1246 | ||
1247 | dev = hi->dev; | |
1248 | ||
1249 | nready = hpsb_iso_n_ready(iso); | |
1250 | for (i = 0; i < nready; i++) { | |
1251 | struct hpsb_iso_packet_info *info = | |
1252 | &iso->infos[(iso->first_packet + i) % iso->buf_packets]; | |
efbeccf1 | 1253 | data = (quadlet_t *)(iso->data_buf.kvirt + info->offset); |
1da177e4 LT |
1254 | |
1255 | /* skip over GASP header */ | |
1256 | buf = (char *)data + 8; | |
1257 | len = info->len - 8; | |
1258 | ||
efbeccf1 SR |
1259 | specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 | |
1260 | (be32_to_cpu(data[1]) & 0xff000000) >> 24; | |
1da177e4 LT |
1261 | source_id = be32_to_cpu(data[0]) >> 16; |
1262 | ||
1263 | priv = netdev_priv(dev); | |
1264 | ||
efbeccf1 SR |
1265 | if (info->channel != (iso->host->csr.broadcast_channel & 0x3f) |
1266 | || specifier_id != ETHER1394_GASP_SPECIFIER_ID) { | |
1da177e4 LT |
1267 | /* This packet is not for us */ |
1268 | continue; | |
1269 | } | |
1270 | ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES, | |
1271 | buf, len); | |
1272 | } | |
1273 | ||
1274 | hpsb_iso_recv_release_packets(iso, i); | |
1275 | ||
1276 | dev->last_rx = jiffies; | |
1277 | } | |
1278 | ||
1279 | /****************************************** | |
1280 | * Datagram transmission code | |
1281 | ******************************************/ | |
1282 | ||
1283 | /* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire | |
1284 | * arphdr) is the same format as the ip1394 header, so they overlap. The rest | |
1285 | * needs to be munged a bit. The remainder of the arphdr is formatted based | |
1286 | * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to | |
1287 | * judge. | |
1288 | * | |
1289 | * Now that the EUI is used for the hardware address all we need to do to make | |
1290 | * this work for 1394 is to insert 2 quadlets that contain max_rec size, | |
1291 | * speed, and unicast FIFO address information between the sender_unique_id | |
1292 | * and the IP addresses. | |
1293 | */ | |
e00f04a7 SR |
1294 | static void ether1394_arp_to_1394arp(struct sk_buff *skb, |
1295 | struct net_device *dev) | |
1da177e4 LT |
1296 | { |
1297 | struct eth1394_priv *priv = netdev_priv(dev); | |
1da177e4 LT |
1298 | struct arphdr *arp = (struct arphdr *)skb->data; |
1299 | unsigned char *arp_ptr = (unsigned char *)(arp + 1); | |
1300 | struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data; | |
1301 | ||
1da177e4 LT |
1302 | arp1394->hw_addr_len = 16; |
1303 | arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN); | |
1304 | arp1394->max_rec = priv->host->csr.max_rec; | |
1305 | arp1394->sspd = priv->host->csr.lnk_spd; | |
efbeccf1 SR |
1306 | arp1394->fifo_hi = htons(priv->local_fifo >> 32); |
1307 | arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0); | |
1da177e4 LT |
1308 | } |
1309 | ||
1310 | /* We need to encapsulate the standard header with our own. We use the | |
1311 | * ethernet header's proto for our own. */ | |
e00f04a7 SR |
1312 | static unsigned int ether1394_encapsulate_prep(unsigned int max_payload, |
1313 | __be16 proto, | |
1314 | union eth1394_hdr *hdr, | |
1315 | u16 dg_size, u16 dgl) | |
1da177e4 | 1316 | { |
efbeccf1 SR |
1317 | unsigned int adj_max_payload = |
1318 | max_payload - hdr_type_len[ETH1394_HDR_LF_UF]; | |
1da177e4 LT |
1319 | |
1320 | /* Does it all fit in one packet? */ | |
1321 | if (dg_size <= adj_max_payload) { | |
1322 | hdr->uf.lf = ETH1394_HDR_LF_UF; | |
1323 | hdr->uf.ether_type = proto; | |
1324 | } else { | |
1325 | hdr->ff.lf = ETH1394_HDR_LF_FF; | |
1326 | hdr->ff.ether_type = proto; | |
1327 | hdr->ff.dg_size = dg_size - 1; | |
1328 | hdr->ff.dgl = dgl; | |
1329 | adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF]; | |
1330 | } | |
efbeccf1 | 1331 | return (dg_size + adj_max_payload - 1) / adj_max_payload; |
1da177e4 LT |
1332 | } |
1333 | ||
e00f04a7 SR |
1334 | static unsigned int ether1394_encapsulate(struct sk_buff *skb, |
1335 | unsigned int max_payload, | |
1336 | union eth1394_hdr *hdr) | |
1da177e4 LT |
1337 | { |
1338 | union eth1394_hdr *bufhdr; | |
1339 | int ftype = hdr->common.lf; | |
1340 | int hdrsz = hdr_type_len[ftype]; | |
1341 | unsigned int adj_max_payload = max_payload - hdrsz; | |
1342 | ||
efbeccf1 | 1343 | switch (ftype) { |
1da177e4 LT |
1344 | case ETH1394_HDR_LF_UF: |
1345 | bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz); | |
1346 | bufhdr->words.word1 = htons(hdr->words.word1); | |
1347 | bufhdr->words.word2 = hdr->words.word2; | |
1348 | break; | |
1349 | ||
1350 | case ETH1394_HDR_LF_FF: | |
1351 | bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz); | |
1352 | bufhdr->words.word1 = htons(hdr->words.word1); | |
1353 | bufhdr->words.word2 = hdr->words.word2; | |
1354 | bufhdr->words.word3 = htons(hdr->words.word3); | |
1355 | bufhdr->words.word4 = 0; | |
1356 | ||
1357 | /* Set frag type here for future interior fragments */ | |
1358 | hdr->common.lf = ETH1394_HDR_LF_IF; | |
1359 | hdr->sf.fg_off = 0; | |
1360 | break; | |
1361 | ||
1362 | default: | |
1363 | hdr->sf.fg_off += adj_max_payload; | |
1364 | bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload); | |
1365 | if (max_payload >= skb->len) | |
1366 | hdr->common.lf = ETH1394_HDR_LF_LF; | |
1367 | bufhdr->words.word1 = htons(hdr->words.word1); | |
1368 | bufhdr->words.word2 = htons(hdr->words.word2); | |
1369 | bufhdr->words.word3 = htons(hdr->words.word3); | |
1370 | bufhdr->words.word4 = 0; | |
1371 | } | |
1da177e4 LT |
1372 | return min(max_payload, skb->len); |
1373 | } | |
1374 | ||
e00f04a7 | 1375 | static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host) |
1da177e4 LT |
1376 | { |
1377 | struct hpsb_packet *p; | |
1378 | ||
1379 | p = hpsb_alloc_packet(0); | |
1380 | if (p) { | |
1381 | p->host = host; | |
1382 | p->generation = get_hpsb_generation(host); | |
1383 | p->type = hpsb_async; | |
1384 | } | |
1385 | return p; | |
1386 | } | |
1387 | ||
e00f04a7 SR |
1388 | static int ether1394_prep_write_packet(struct hpsb_packet *p, |
1389 | struct hpsb_host *host, nodeid_t node, | |
efbeccf1 | 1390 | u64 addr, void *data, int tx_len) |
1da177e4 LT |
1391 | { |
1392 | p->node_id = node; | |
1393 | p->data = NULL; | |
1394 | ||
1395 | p->tcode = TCODE_WRITEB; | |
efbeccf1 | 1396 | p->header[1] = host->node_id << 16 | addr >> 32; |
1da177e4 LT |
1397 | p->header[2] = addr & 0xffffffff; |
1398 | ||
1399 | p->header_size = 16; | |
1400 | p->expect_response = 1; | |
1401 | ||
1402 | if (hpsb_get_tlabel(p)) { | |
5009d269 | 1403 | ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n"); |
1da177e4 LT |
1404 | return -1; |
1405 | } | |
efbeccf1 SR |
1406 | p->header[0] = |
1407 | p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4; | |
1da177e4 LT |
1408 | |
1409 | p->header[3] = tx_len << 16; | |
1410 | p->data_size = (tx_len + 3) & ~3; | |
efbeccf1 | 1411 | p->data = data; |
1da177e4 LT |
1412 | |
1413 | return 0; | |
1414 | } | |
1415 | ||
e00f04a7 SR |
1416 | static void ether1394_prep_gasp_packet(struct hpsb_packet *p, |
1417 | struct eth1394_priv *priv, | |
1418 | struct sk_buff *skb, int length) | |
1da177e4 LT |
1419 | { |
1420 | p->header_size = 4; | |
1421 | p->tcode = TCODE_STREAM_DATA; | |
1422 | ||
efbeccf1 SR |
1423 | p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 | |
1424 | TCODE_STREAM_DATA << 4; | |
1da177e4 | 1425 | p->data_size = length; |
efbeccf1 SR |
1426 | p->data = (quadlet_t *)skb->data - 2; |
1427 | p->data[0] = cpu_to_be32(priv->host->node_id << 16 | | |
1da177e4 | 1428 | ETHER1394_GASP_SPECIFIER_ID_HI); |
efbeccf1 | 1429 | p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 | |
7136b807 | 1430 | ETHER1394_GASP_VERSION); |
1da177e4 | 1431 | |
1da177e4 | 1432 | p->speed_code = priv->bc_sspd; |
21b2c564 SR |
1433 | |
1434 | /* prevent hpsb_send_packet() from overriding our speed code */ | |
1435 | p->node_id = LOCAL_BUS | ALL_NODES; | |
1da177e4 LT |
1436 | } |
1437 | ||
e00f04a7 | 1438 | static void ether1394_free_packet(struct hpsb_packet *packet) |
1da177e4 LT |
1439 | { |
1440 | if (packet->tcode != TCODE_STREAM_DATA) | |
1441 | hpsb_free_tlabel(packet); | |
1442 | hpsb_free_packet(packet); | |
1443 | } | |
1444 | ||
1445 | static void ether1394_complete_cb(void *__ptask); | |
1446 | ||
1447 | static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len) | |
1448 | { | |
1449 | struct eth1394_priv *priv = ptask->priv; | |
1450 | struct hpsb_packet *packet = NULL; | |
1451 | ||
1452 | packet = ether1394_alloc_common_packet(priv->host); | |
1453 | if (!packet) | |
1454 | return -1; | |
1455 | ||
1456 | if (ptask->tx_type == ETH1394_GASP) { | |
efbeccf1 | 1457 | int length = tx_len + 2 * sizeof(quadlet_t); |
1da177e4 LT |
1458 | |
1459 | ether1394_prep_gasp_packet(packet, priv, ptask->skb, length); | |
1460 | } else if (ether1394_prep_write_packet(packet, priv->host, | |
1461 | ptask->dest_node, | |
1462 | ptask->addr, ptask->skb->data, | |
1463 | tx_len)) { | |
1464 | hpsb_free_packet(packet); | |
1465 | return -1; | |
1466 | } | |
1467 | ||
1468 | ptask->packet = packet; | |
1469 | hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb, | |
1470 | ptask); | |
1471 | ||
1472 | if (hpsb_send_packet(packet) < 0) { | |
1473 | ether1394_free_packet(packet); | |
1474 | return -1; | |
1475 | } | |
1476 | ||
1477 | return 0; | |
1478 | } | |
1479 | ||
1da177e4 | 1480 | /* Task function to be run when a datagram transmission is completed */ |
e00f04a7 | 1481 | static void ether1394_dg_complete(struct packet_task *ptask, int fail) |
1da177e4 LT |
1482 | { |
1483 | struct sk_buff *skb = ptask->skb; | |
efbeccf1 | 1484 | struct eth1394_priv *priv = netdev_priv(skb->dev); |
1da177e4 LT |
1485 | unsigned long flags; |
1486 | ||
1487 | /* Statistics */ | |
1488 | spin_lock_irqsave(&priv->lock, flags); | |
1489 | if (fail) { | |
1490 | priv->stats.tx_dropped++; | |
1491 | priv->stats.tx_errors++; | |
1492 | } else { | |
1493 | priv->stats.tx_bytes += skb->len; | |
1494 | priv->stats.tx_packets++; | |
1495 | } | |
1496 | spin_unlock_irqrestore(&priv->lock, flags); | |
1497 | ||
1498 | dev_kfree_skb_any(skb); | |
1499 | kmem_cache_free(packet_task_cache, ptask); | |
1500 | } | |
1501 | ||
1da177e4 LT |
1502 | /* Callback for when a packet has been sent and the status of that packet is |
1503 | * known */ | |
1504 | static void ether1394_complete_cb(void *__ptask) | |
1505 | { | |
1506 | struct packet_task *ptask = (struct packet_task *)__ptask; | |
1507 | struct hpsb_packet *packet = ptask->packet; | |
1508 | int fail = 0; | |
1509 | ||
1510 | if (packet->tcode != TCODE_STREAM_DATA) | |
1511 | fail = hpsb_packet_success(packet); | |
1512 | ||
1513 | ether1394_free_packet(packet); | |
1514 | ||
1515 | ptask->outstanding_pkts--; | |
1516 | if (ptask->outstanding_pkts > 0 && !fail) { | |
1517 | int tx_len; | |
1518 | ||
1519 | /* Add the encapsulation header to the fragment */ | |
1520 | tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload, | |
1521 | &ptask->hdr); | |
1522 | if (ether1394_send_packet(ptask, tx_len)) | |
1523 | ether1394_dg_complete(ptask, 1); | |
1524 | } else { | |
1525 | ether1394_dg_complete(ptask, fail); | |
1526 | } | |
1527 | } | |
1528 | ||
1da177e4 | 1529 | /* Transmit a packet (called by kernel) */ |
efbeccf1 | 1530 | static int ether1394_tx(struct sk_buff *skb, struct net_device *dev) |
1da177e4 | 1531 | { |
1da177e4 LT |
1532 | struct eth1394hdr *eth; |
1533 | struct eth1394_priv *priv = netdev_priv(dev); | |
02f4213f | 1534 | __be16 proto; |
1da177e4 LT |
1535 | unsigned long flags; |
1536 | nodeid_t dest_node; | |
1537 | eth1394_tx_type tx_type; | |
1da177e4 LT |
1538 | unsigned int tx_len; |
1539 | unsigned int max_payload; | |
1540 | u16 dg_size; | |
1541 | u16 dgl; | |
1542 | struct packet_task *ptask; | |
1543 | struct eth1394_node_ref *node; | |
1544 | struct eth1394_node_info *node_info = NULL; | |
1545 | ||
53f374e7 | 1546 | ptask = kmem_cache_alloc(packet_task_cache, GFP_ATOMIC); |
fdc0092b | 1547 | if (ptask == NULL) |
1da177e4 | 1548 | goto fail; |
1da177e4 LT |
1549 | |
1550 | /* XXX Ignore this for now. Noticed that when MacOSX is the IRM, | |
1551 | * it does not set our validity bit. We need to compensate for | |
1552 | * that somewhere else, but not in eth1394. */ | |
1553 | #if 0 | |
fdc0092b | 1554 | if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000) |
1da177e4 | 1555 | goto fail; |
1da177e4 LT |
1556 | #endif |
1557 | ||
53f374e7 | 1558 | skb = skb_share_check(skb, GFP_ATOMIC); |
fdc0092b | 1559 | if (!skb) |
1da177e4 | 1560 | goto fail; |
1da177e4 LT |
1561 | |
1562 | /* Get rid of the fake eth1394 header, but save a pointer */ | |
efbeccf1 | 1563 | eth = (struct eth1394hdr *)skb->data; |
1da177e4 LT |
1564 | skb_pull(skb, ETH1394_HLEN); |
1565 | ||
1566 | proto = eth->h_proto; | |
1567 | dg_size = skb->len; | |
1568 | ||
1569 | /* Set the transmission type for the packet. ARP packets and IP | |
1570 | * broadcast packets are sent via GASP. */ | |
1571 | if (memcmp(eth->h_dest, dev->broadcast, ETH1394_ALEN) == 0 || | |
7136b807 BC |
1572 | proto == htons(ETH_P_ARP) || |
1573 | (proto == htons(ETH_P_IP) && | |
eddc9ec5 | 1574 | IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) { |
1da177e4 LT |
1575 | tx_type = ETH1394_GASP; |
1576 | dest_node = LOCAL_BUS | ALL_NODES; | |
1577 | max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD; | |
efbeccf1 | 1578 | BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD); |
1da177e4 LT |
1579 | dgl = priv->bc_dgl; |
1580 | if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF]) | |
1581 | priv->bc_dgl++; | |
1582 | } else { | |
c20e3945 DM |
1583 | __be64 guid = get_unaligned((u64 *)eth->h_dest); |
1584 | ||
1da177e4 | 1585 | node = eth1394_find_node_guid(&priv->ip_node_list, |
c20e3945 | 1586 | be64_to_cpu(guid)); |
fdc0092b | 1587 | if (!node) |
1da177e4 | 1588 | goto fail; |
fdc0092b | 1589 | |
efbeccf1 SR |
1590 | node_info = |
1591 | (struct eth1394_node_info *)node->ud->device.driver_data; | |
fdc0092b | 1592 | if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE) |
1da177e4 | 1593 | goto fail; |
1da177e4 LT |
1594 | |
1595 | dest_node = node->ud->ne->nodeid; | |
1596 | max_payload = node_info->maxpayload; | |
efbeccf1 | 1597 | BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD); |
1da177e4 LT |
1598 | |
1599 | dgl = node_info->dgl; | |
1600 | if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF]) | |
1601 | node_info->dgl++; | |
1602 | tx_type = ETH1394_WRREQ; | |
1603 | } | |
1604 | ||
1605 | /* If this is an ARP packet, convert it */ | |
7136b807 | 1606 | if (proto == htons(ETH_P_ARP)) |
efbeccf1 | 1607 | ether1394_arp_to_1394arp(skb, dev); |
1da177e4 LT |
1608 | |
1609 | ptask->hdr.words.word1 = 0; | |
1610 | ptask->hdr.words.word2 = 0; | |
1611 | ptask->hdr.words.word3 = 0; | |
1612 | ptask->hdr.words.word4 = 0; | |
1613 | ptask->skb = skb; | |
1614 | ptask->priv = priv; | |
1615 | ptask->tx_type = tx_type; | |
1616 | ||
1617 | if (tx_type != ETH1394_GASP) { | |
1618 | u64 addr; | |
1619 | ||
1620 | spin_lock_irqsave(&priv->lock, flags); | |
1621 | addr = node_info->fifo; | |
1622 | spin_unlock_irqrestore(&priv->lock, flags); | |
1623 | ||
1624 | ptask->addr = addr; | |
1625 | ptask->dest_node = dest_node; | |
1626 | } | |
1627 | ||
1628 | ptask->tx_type = tx_type; | |
1629 | ptask->max_payload = max_payload; | |
efbeccf1 SR |
1630 | ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload, |
1631 | proto, &ptask->hdr, dg_size, dgl); | |
1da177e4 LT |
1632 | |
1633 | /* Add the encapsulation header to the fragment */ | |
1634 | tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr); | |
1635 | dev->trans_start = jiffies; | |
1636 | if (ether1394_send_packet(ptask, tx_len)) | |
1637 | goto fail; | |
1638 | ||
fdc0092b | 1639 | return NETDEV_TX_OK; |
1da177e4 LT |
1640 | fail: |
1641 | if (ptask) | |
1642 | kmem_cache_free(packet_task_cache, ptask); | |
1643 | ||
1644 | if (skb != NULL) | |
1645 | dev_kfree_skb(skb); | |
1646 | ||
efbeccf1 | 1647 | spin_lock_irqsave(&priv->lock, flags); |
1da177e4 LT |
1648 | priv->stats.tx_dropped++; |
1649 | priv->stats.tx_errors++; | |
efbeccf1 | 1650 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 | 1651 | |
fdc0092b SR |
1652 | /* |
1653 | * FIXME: According to a patch from 2003-02-26, "returning non-zero | |
1654 | * causes serious problems" here, allegedly. Before that patch, | |
1655 | * -ERRNO was returned which is not appropriate under Linux 2.6. | |
1656 | * Perhaps more needs to be done? Stop the queue in serious | |
1657 | * conditions and restart it elsewhere? | |
1658 | */ | |
1659 | /* return NETDEV_TX_BUSY; */ | |
1660 | return NETDEV_TX_OK; | |
1da177e4 LT |
1661 | } |
1662 | ||
efbeccf1 SR |
1663 | static void ether1394_get_drvinfo(struct net_device *dev, |
1664 | struct ethtool_drvinfo *info) | |
1da177e4 | 1665 | { |
efbeccf1 SR |
1666 | strcpy(info->driver, driver_name); |
1667 | strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */ | |
1da177e4 LT |
1668 | } |
1669 | ||
1670 | static struct ethtool_ops ethtool_ops = { | |
1671 | .get_drvinfo = ether1394_get_drvinfo | |
1672 | }; | |
1673 | ||
809e905c | 1674 | static int __init ether1394_init_module(void) |
1da177e4 | 1675 | { |
809e905c AM |
1676 | int err; |
1677 | ||
efbeccf1 SR |
1678 | packet_task_cache = kmem_cache_create("packet_task", |
1679 | sizeof(struct packet_task), | |
1da177e4 | 1680 | 0, 0, NULL, NULL); |
809e905c AM |
1681 | if (!packet_task_cache) |
1682 | return -ENOMEM; | |
1da177e4 | 1683 | |
1da177e4 | 1684 | hpsb_register_highlevel(ð1394_highlevel); |
809e905c AM |
1685 | err = hpsb_register_protocol(ð1394_proto_driver); |
1686 | if (err) { | |
1687 | hpsb_unregister_highlevel(ð1394_highlevel); | |
1688 | kmem_cache_destroy(packet_task_cache); | |
1689 | } | |
1690 | return err; | |
1da177e4 LT |
1691 | } |
1692 | ||
809e905c | 1693 | static void __exit ether1394_exit_module(void) |
1da177e4 LT |
1694 | { |
1695 | hpsb_unregister_protocol(ð1394_proto_driver); | |
1696 | hpsb_unregister_highlevel(ð1394_highlevel); | |
1697 | kmem_cache_destroy(packet_task_cache); | |
1698 | } | |
1699 | ||
1700 | module_init(ether1394_init_module); | |
1701 | module_exit(ether1394_exit_module); |