Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[linux-2.6] / drivers / ieee1394 / eth1394.c
1 /*
2  * eth1394.c -- IPv4 driver for Linux IEEE-1394 Subsystem
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
25 /*
26  * This driver intends to support RFC 2734, which describes a method for
27  * transporting IPv4 datagrams over IEEE-1394 serial busses.
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
42 #include <linux/module.h>
43
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>
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>
63 #include <asm/unaligned.h>
64 #include <net/arp.h>
65
66 #include "config_roms.h"
67 #include "csr1212.h"
68 #include "eth1394.h"
69 #include "highlevel.h"
70 #include "ieee1394.h"
71 #include "ieee1394_core.h"
72 #include "ieee1394_hotplug.h"
73 #include "ieee1394_transactions.h"
74 #include "ieee1394_types.h"
75 #include "iso.h"
76 #include "nodemgr.h"
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
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 {
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                */
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 {
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      */
122 };
123
124 static const char driver_name[] = "eth1394";
125
126 static struct kmem_cache *packet_task_cache;
127
128 static struct hpsb_highlevel eth1394_highlevel;
129
130 /* Use common.lf to determine header len */
131 static const int hdr_type_len[] = {
132         sizeof(struct eth1394_uf_hdr),
133         sizeof(struct eth1394_ff_hdr),
134         sizeof(struct eth1394_sf_hdr),
135         sizeof(struct eth1394_sf_hdr)
136 };
137
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
147 /*
148  * The max_partial_datagrams parameter is the maximum number of fragmented
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,
168                                           unsigned char *haddr);
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);
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);
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
193 static int ether1394_recv_init(struct eth1394_priv *priv)
194 {
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)));
200
201         priv->iso = hpsb_iso_recv_init(priv->host,
202                                        ETHER1394_GASP_BUFFERS * iso_buf_size,
203                                        ETHER1394_GASP_BUFFERS,
204                                        priv->broadcast_channel,
205                                        HPSB_ISO_DMA_PACKET_PER_BUFFER,
206                                        1, ether1394_iso);
207         if (priv->iso == NULL) {
208                 ETH1394_PRINT_G(KERN_ERR, "Failed to allocate IR context\n");
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
220 /* This is called after an "ifup" */
221 static int ether1394_open(struct net_device *dev)
222 {
223         struct eth1394_priv *priv = netdev_priv(dev);
224         int ret;
225
226         if (priv->bc_state == ETHER1394_BC_ERROR) {
227                 ret = ether1394_recv_init(priv);
228                 if (ret)
229                         return ret;
230         }
231         netif_start_queue(dev);
232         return 0;
233 }
234
235 /* This is called after an "ifdown" */
236 static int ether1394_stop(struct net_device *dev)
237 {
238         netif_stop_queue(dev);
239         return 0;
240 }
241
242 /* Return statistics to the caller */
243 static struct net_device_stats *ether1394_stats(struct net_device *dev)
244 {
245         return &(((struct eth1394_priv *)netdev_priv(dev))->stats);
246 }
247
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)
251 {
252         struct hpsb_host *host =
253                         ((struct eth1394_priv *)netdev_priv(dev))->host;
254
255         ETH1394_PRINT(KERN_ERR, dev->name, "Timeout, resetting host\n");
256         ether1394_host_reset(host);
257 }
258
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
265 static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
266 {
267         int max_mtu;
268
269         if (new_mtu < 68)
270                 return -EINVAL;
271
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
280         dev->mtu = new_mtu;
281         return 0;
282 }
283
284 static void purge_partial_datagram(struct list_head *old)
285 {
286         struct partial_datagram *pd;
287         struct list_head *lh, *n;
288         struct fragment_info *fi;
289
290         pd = list_entry(old, struct partial_datagram, list);
291
292         list_for_each_safe(lh, n, &pd->frag_info) {
293                 fi = list_entry(lh, struct fragment_info, list);
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;
334
335         list_for_each_entry(node, inl, list)
336                 if (node->ud->ne->nodeid == nodeid)
337                         return node;
338
339         return NULL;
340 }
341
342 static int eth1394_new_node(struct eth1394_host_info *hi,
343                             struct unit_directory *ud)
344 {
345         struct eth1394_priv *priv;
346         struct eth1394_node_ref *new_node;
347         struct eth1394_node_info *node_info;
348
349         new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
350         if (!new_node)
351                 return -ENOMEM;
352
353         node_info = kmalloc(sizeof(*node_info), GFP_KERNEL);
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;
362         node_info->fifo = CSR1212_INVALID_ADDR_SPACE;
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);
369         return 0;
370 }
371
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(&eth1394_highlevel, ud->ne->host);
379         if (!hi)
380                 return -ENOENT;
381
382         return eth1394_new_node(hi, ud);
383 }
384
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(&eth1394_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);
403         if (!old_node)
404                 return 0;
405
406         list_del(&old_node->list);
407         kfree(old_node);
408
409         node_info = (struct eth1394_node_info*)ud->device.driver_data;
410
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);
417
418         kfree(node_info);
419         ud->device.driver_data = NULL;
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;
428
429         hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
430         if (!hi)
431                 return -ENOENT;
432
433         priv = netdev_priv(hi->dev);
434         node = eth1394_find_node(&priv->ip_node_list, ud);
435         if (node)
436                 return 0;
437
438         return eth1394_new_node(hi, ud);
439 }
440
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 = {
454         .name           = driver_name,
455         .id_table       = eth1394_id_table,
456         .update         = eth1394_update,
457         .driver         = {
458                 .probe          = eth1394_probe,
459                 .remove         = eth1394_remove,
460         },
461 };
462
463 static void ether1394_reset_priv(struct net_device *dev, int set_mtu)
464 {
465         unsigned long flags;
466         int i;
467         struct eth1394_priv *priv = netdev_priv(dev);
468         struct hpsb_host *host = priv->host;
469         u64 guid = get_unaligned((u64 *)&(host->csr.rom->bus_info_data[3]));
470         int max_speed = IEEE1394_SPEED_MAX;
471
472         spin_lock_irqsave(&priv->lock, flags);
473
474         memset(priv->ud_list, 0, sizeof(priv->ud_list));
475         priv->bc_maxpayload = 512;
476
477         /* Determine speed limit */
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                 }
487                 if (max_speed > host->speed[i])
488                         max_speed = host->speed[i];
489         }
490         priv->bc_sspd = max_speed;
491
492         if (set_mtu) {
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));
496
497                 /* Set our hardware address while we're at it */
498                 memcpy(dev->dev_addr, &guid, sizeof(u64));
499                 memset(dev->broadcast, 0xff, sizeof(u64));
500         }
501
502         spin_unlock_irqrestore(&priv->lock, flags);
503 }
504
505 static void ether1394_init_dev(struct net_device *dev)
506 {
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;
519
520         SET_ETHTOOL_OPS(dev, &ethtool_ops);
521
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
529         /* FIXME: This value was copied from ether_setup(). Is it too much? */
530         dev->tx_queue_len       = 1000;
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  */
538 static void ether1394_add_host(struct hpsb_host *host)
539 {
540         struct eth1394_host_info *hi = NULL;
541         struct net_device *dev = NULL;
542         struct eth1394_priv *priv;
543         u64 fifo_addr;
544
545         if (hpsb_config_rom_ip1394_add(host) != 0) {
546                 ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n");
547                 return;
548         }
549
550         fifo_addr = hpsb_allocate_and_register_addrspace(
551                         &eth1394_highlevel, host, &addr_ops,
552                         ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN,
553                         CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE);
554         if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
555                 ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n");
556                 hpsb_config_rom_ip1394_remove(host);
557                 return;
558         }
559
560         dev = alloc_netdev(sizeof(*priv), "eth%d", ether1394_init_dev);
561         if (dev == NULL) {
562                 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
563                 goto out;
564         }
565
566         SET_MODULE_OWNER(dev);
567 #if 0
568         /* FIXME - Is this the correct parent device anyway? */
569         SET_NETDEV_DEV(dev, &host->device);
570 #endif
571
572         priv = netdev_priv(dev);
573         INIT_LIST_HEAD(&priv->ip_node_list);
574         spin_lock_init(&priv->lock);
575         priv->host = host;
576         priv->local_fifo = fifo_addr;
577
578         hi = hpsb_create_hostinfo(&eth1394_highlevel, host, sizeof(*hi));
579         if (hi == NULL) {
580                 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
581                 goto out;
582         }
583
584         ether1394_reset_priv(dev, 1);
585
586         if (register_netdev(dev)) {
587                 ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n");
588                 goto out;
589         }
590
591         ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n",
592                       host->id);
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
601         ether1394_recv_init(priv);
602         return;
603 out:
604         if (dev)
605                 free_netdev(dev);
606         if (hi)
607                 hpsb_destroy_hostinfo(&eth1394_highlevel, host);
608         hpsb_unregister_addrspace(&eth1394_highlevel, host, fifo_addr);
609         hpsb_config_rom_ip1394_remove(host);
610 }
611
612 /* Remove a card from our list */
613 static void ether1394_remove_host(struct hpsb_host *host)
614 {
615         struct eth1394_host_info *hi;
616         struct eth1394_priv *priv;
617
618         hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
619         if (!hi)
620                 return;
621         priv = netdev_priv(hi->dev);
622         hpsb_unregister_addrspace(&eth1394_highlevel, host, priv->local_fifo);
623         hpsb_config_rom_ip1394_remove(host);
624         if (priv->iso)
625                 hpsb_iso_shutdown(priv->iso);
626         unregister_netdev(hi->dev);
627         free_netdev(hi->dev);
628 }
629
630 /* A bus reset happened */
631 static void ether1394_host_reset(struct hpsb_host *host)
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(&eth1394_highlevel, host);
642
643         /* This can happen for hosts that we don't use */
644         if (!hi)
645                 return;
646
647         dev = hi->dev;
648         priv = netdev_priv(dev);
649
650         /* Reset our private host data, but not our MTU */
651         netif_stop_queue(dev);
652         ether1394_reset_priv(dev, 0);
653
654         list_for_each_entry(node, &priv->ip_node_list, list) {
655                 node_info = node->ud->device.driver_data;
656
657                 spin_lock_irqsave(&node_info->pdg.lock, flags);
658
659                 list_for_each_safe(lh, n, &node_info->pdg.list)
660                         purge_partial_datagram(lh);
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
668         netif_wake_queue(dev);
669 }
670
671 /******************************************
672  * HW Header net device functions
673  ******************************************/
674 /* These functions have been adapted from net/ethernet/eth.c */
675
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 {
683         struct eth1394hdr *eth =
684                         (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN);
685
686         eth->h_proto = htons(type);
687
688         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
689                 memset(eth->h_dest, 0, dev->addr_len);
690                 return dev->hard_header_len;
691         }
692
693         if (daddr) {
694                 memcpy(eth->h_dest, daddr, dev->addr_len);
695                 return dev->hard_header_len;
696         }
697
698         return -dev->hard_header_len;
699 }
700
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;
711
712         if (eth->h_proto == htons(ETH_P_IP))
713                 return arp_find((unsigned char *)&eth->h_dest, skb);
714
715         ETH1394_PRINT(KERN_DEBUG, skb->dev->name,
716                       "unable to resolve type %04x addresses\n",
717                       ntohs(eth->h_proto));
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;
724
725         memcpy(haddr, dev->dev_addr, ETH1394_ALEN);
726         return ETH1394_ALEN;
727 }
728
729 static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh)
730 {
731         unsigned short type = hh->hh_type;
732         struct net_device *dev = neigh->dev;
733         struct eth1394hdr *eth =
734                 (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN);
735
736         if (type == htons(ETH_P_802_3))
737                 return -1;
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 {
751         memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len);
752 }
753
754 /******************************************
755  * Datagram reception code
756  ******************************************/
757
758 /* Copied from net/ethernet/eth.c */
759 static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
760 {
761         struct eth1394hdr *eth;
762         unsigned char *rawp;
763
764         skb_reset_mac_header(skb);
765         skb_pull(skb, ETH1394_HLEN);
766         eth = eth1394_hdr(skb);
767
768         if (*eth->h_dest & 1) {
769                 if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0)
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;
778         }
779
780         if (ntohs(eth->h_proto) >= 1536)
781                 return eth->h_proto;
782
783         rawp = skb->data;
784
785         if (*(unsigned short *)rawp == 0xFFFF)
786                 return htons(ETH_P_802_3);
787
788         return htons(ETH_P_802_2);
789 }
790
791 /* Parse an encapsulated IP1394 header into an ethernet frame packet.
792  * We also perform ARP translation here, if need be.  */
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)
796 {
797         struct eth1394_priv *priv = netdev_priv(dev);
798         u64 dest_hw;
799         unsigned short ret = 0;
800
801         /* Setup our hw addresses. We use these to build the ethernet header. */
802         if (destid == (LOCAL_BUS | ALL_NODES))
803                 dest_hw = ~0ULL;  /* broadcast */
804         else
805                 dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 |
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.  */
811         if (ether_type == htons(ETH_P_ARP)) {
812                 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
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 |
816                                            ntohl(arp1394->fifo_lo);
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;
823                 __be64 guid;
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
830                 maxpayload = min(eth1394_speedto_maxpayload[sspd],
831                                  (u16)(1 << (max_rec + 1)));
832
833                 guid = get_unaligned(&arp1394->s_uniq_id);
834                 node = eth1394_find_node_guid(&priv->ip_node_list,
835                                               be64_to_cpu(guid));
836                 if (!node)
837                         return 0;
838
839                 node_info =
840                     (struct eth1394_node_info *)node->ud->device.driver_data;
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 */
859                 *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */
860                 arp_ptr += arp->ar_pln;         /* skip over sender IP addr */
861
862                 if (arp->ar_op == htons(ARPOP_REQUEST))
863                         memset(arp_ptr, 0, sizeof(u64));
864                 else
865                         memcpy(arp_ptr, dev->dev_addr, sizeof(u64));
866         }
867
868         /* Now add the ethernet header. */
869         if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL,
870                              skb->len) >= 0)
871                 ret = ether1394_type_trans(skb, dev);
872
873         return ret;
874 }
875
876 static int fragment_overlap(struct list_head *frag_list, int offset, int len)
877 {
878         struct fragment_info *fi;
879         int end = offset + len;
880
881         list_for_each_entry(fi, frag_list, list)
882                 if (offset < fi->offset + fi->len && end > fi->offset)
883                         return 1;
884
885         return 0;
886 }
887
888 static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl)
889 {
890         struct partial_datagram *pd;
891
892         list_for_each_entry(pd, pdgl, list)
893                 if (pd->dgl == dgl)
894                         return &pd->list;
895
896         return NULL;
897 }
898
899 /* Assumes that new fragment does not overlap any existing fragments */
900 static int new_fragment(struct list_head *frag_info, int offset, int len)
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);
907                 if (fi->offset + fi->len == offset) {
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);
912                         if (fi->offset + fi->len == fi2->offset) {
913                                 /* glue fragments together */
914                                 fi->len += fi2->len;
915                                 list_del(lh->next);
916                                 kfree(fi2);
917                         }
918                         return 0;
919                 } else if (offset + len == fi->offset) {
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);
925                         if (fi2->offset + fi2->len == fi->offset) {
926                                 /* glue fragments together */
927                                 fi2->len += fi->len;
928                                 list_del(lh);
929                                 kfree(fi);
930                         }
931                         return 0;
932                 } else if (offset > fi->offset + fi->len) {
933                         break;
934                 } else if (offset + len < fi->offset) {
935                         lh = lh->prev;
936                         break;
937                 }
938         }
939
940         new = kmalloc(sizeof(*new), GFP_ATOMIC);
941         if (!new)
942                 return -ENOMEM;
943
944         new->offset = offset;
945         new->len = len;
946
947         list_add(&new->list, lh);
948         return 0;
949 }
950
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)
954 {
955         struct partial_datagram *new;
956
957         new = kmalloc(sizeof(*new), GFP_ATOMIC);
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);
986         return 0;
987 }
988
989 static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh,
990                                    char *frag_buf, int frag_off, int frag_len)
991 {
992         struct partial_datagram *pd =
993                         list_entry(lh, struct partial_datagram, list);
994
995         if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0)
996                 return -ENOMEM;
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 */
1002         list_move(lh, pdgl);
1003         return 0;
1004 }
1005
1006 static int is_datagram_complete(struct list_head *lh, int dg_size)
1007 {
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);
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);
1035                 if (unlikely(!node)) {
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
1047         node_info = (struct eth1394_node_info *)ud->device.driver_data;
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);
1060                 if (unlikely(!skb)) {
1061                         ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
1062                         priv->stats.rx_dropped++;
1063                         return -1;
1064                 }
1065                 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
1066                 memcpy(skb_put(skb, len - hdr_len), buf + hdr_len,
1067                        len - hdr_len);
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
1152                 if (hdr->common.lf == ETH1394_HDR_LF_FF)
1153                         pd->ether_type = ether_type;
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
1180         spin_lock_irqsave(&priv->lock, flags);
1181
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(&eth1394_highlevel, host);
1215         if (unlikely(!hi)) {
1216                 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1217                                 host->id);
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(&eth1394_highlevel, iso->host);
1241         if (unlikely(!hi)) {
1242                 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1243                                 iso->host->id);
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];
1253                 data = (quadlet_t *)(iso->data_buf.kvirt + info->offset);
1254
1255                 /* skip over GASP header */
1256                 buf = (char *)data + 8;
1257                 len = info->len - 8;
1258
1259                 specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 |
1260                                (be32_to_cpu(data[1]) & 0xff000000) >> 24;
1261                 source_id = be32_to_cpu(data[0]) >> 16;
1262
1263                 priv = netdev_priv(dev);
1264
1265                 if (info->channel != (iso->host->csr.broadcast_channel & 0x3f)
1266                     || specifier_id != ETHER1394_GASP_SPECIFIER_ID) {
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  */
1294 static void ether1394_arp_to_1394arp(struct sk_buff *skb,
1295                                      struct net_device *dev)
1296 {
1297         struct eth1394_priv *priv = netdev_priv(dev);
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
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;
1306         arp1394->fifo_hi        = htons(priv->local_fifo >> 32);
1307         arp1394->fifo_lo        = htonl(priv->local_fifo & ~0x0);
1308 }
1309
1310 /* We need to encapsulate the standard header with our own. We use the
1311  * ethernet header's proto for our own. */
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)
1316 {
1317         unsigned int adj_max_payload =
1318                                 max_payload - hdr_type_len[ETH1394_HDR_LF_UF];
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         }
1331         return (dg_size + adj_max_payload - 1) / adj_max_payload;
1332 }
1333
1334 static unsigned int ether1394_encapsulate(struct sk_buff *skb,
1335                                           unsigned int max_payload,
1336                                           union eth1394_hdr *hdr)
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
1343         switch (ftype) {
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         }
1372         return min(max_payload, skb->len);
1373 }
1374
1375 static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host)
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
1388 static int ether1394_prep_write_packet(struct hpsb_packet *p,
1389                                        struct hpsb_host *host, nodeid_t node,
1390                                        u64 addr, void *data, int tx_len)
1391 {
1392         p->node_id = node;
1393         p->data = NULL;
1394
1395         p->tcode = TCODE_WRITEB;
1396         p->header[1] = host->node_id << 16 | addr >> 32;
1397         p->header[2] = addr & 0xffffffff;
1398
1399         p->header_size = 16;
1400         p->expect_response = 1;
1401
1402         if (hpsb_get_tlabel(p)) {
1403                 ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n");
1404                 return -1;
1405         }
1406         p->header[0] =
1407                 p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4;
1408
1409         p->header[3] = tx_len << 16;
1410         p->data_size = (tx_len + 3) & ~3;
1411         p->data = data;
1412
1413         return 0;
1414 }
1415
1416 static void ether1394_prep_gasp_packet(struct hpsb_packet *p,
1417                                        struct eth1394_priv *priv,
1418                                        struct sk_buff *skb, int length)
1419 {
1420         p->header_size = 4;
1421         p->tcode = TCODE_STREAM_DATA;
1422
1423         p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 |
1424                        TCODE_STREAM_DATA << 4;
1425         p->data_size = length;
1426         p->data = (quadlet_t *)skb->data - 2;
1427         p->data[0] = cpu_to_be32(priv->host->node_id << 16 |
1428                                  ETHER1394_GASP_SPECIFIER_ID_HI);
1429         p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 |
1430                                  ETHER1394_GASP_VERSION);
1431
1432         p->speed_code = priv->bc_sspd;
1433
1434         /* prevent hpsb_send_packet() from overriding our speed code */
1435         p->node_id = LOCAL_BUS | ALL_NODES;
1436 }
1437
1438 static void ether1394_free_packet(struct hpsb_packet *packet)
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) {
1457                 int length = tx_len + 2 * sizeof(quadlet_t);
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
1480 /* Task function to be run when a datagram transmission is completed */
1481 static void ether1394_dg_complete(struct packet_task *ptask, int fail)
1482 {
1483         struct sk_buff *skb = ptask->skb;
1484         struct eth1394_priv *priv = netdev_priv(skb->dev);
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
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
1529 /* Transmit a packet (called by kernel) */
1530 static int ether1394_tx(struct sk_buff *skb, struct net_device *dev)
1531 {
1532         struct eth1394hdr *eth;
1533         struct eth1394_priv *priv = netdev_priv(dev);
1534         __be16 proto;
1535         unsigned long flags;
1536         nodeid_t dest_node;
1537         eth1394_tx_type tx_type;
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
1546         ptask = kmem_cache_alloc(packet_task_cache, GFP_ATOMIC);
1547         if (ptask == NULL)
1548                 goto fail;
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
1554         if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000)
1555                 goto fail;
1556 #endif
1557
1558         skb = skb_share_check(skb, GFP_ATOMIC);
1559         if (!skb)
1560                 goto fail;
1561
1562         /* Get rid of the fake eth1394 header, but save a pointer */
1563         eth = (struct eth1394hdr *)skb->data;
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 ||
1572             proto == htons(ETH_P_ARP) ||
1573             (proto == htons(ETH_P_IP) &&
1574              IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
1575                 tx_type = ETH1394_GASP;
1576                 dest_node = LOCAL_BUS | ALL_NODES;
1577                 max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD;
1578                 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
1579                 dgl = priv->bc_dgl;
1580                 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1581                         priv->bc_dgl++;
1582         } else {
1583                 __be64 guid = get_unaligned((u64 *)eth->h_dest);
1584
1585                 node = eth1394_find_node_guid(&priv->ip_node_list,
1586                                               be64_to_cpu(guid));
1587                 if (!node)
1588                         goto fail;
1589
1590                 node_info =
1591                     (struct eth1394_node_info *)node->ud->device.driver_data;
1592                 if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE)
1593                         goto fail;
1594
1595                 dest_node = node->ud->ne->nodeid;
1596                 max_payload = node_info->maxpayload;
1597                 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
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 */
1606         if (proto == htons(ETH_P_ARP))
1607                 ether1394_arp_to_1394arp(skb, dev);
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;
1630         ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload,
1631                                         proto, &ptask->hdr, dg_size, dgl);
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
1639         netif_wake_queue(dev);
1640         return NETDEV_TX_OK;
1641 fail:
1642         if (ptask)
1643                 kmem_cache_free(packet_task_cache, ptask);
1644
1645         if (skb != NULL)
1646                 dev_kfree_skb(skb);
1647
1648         spin_lock_irqsave(&priv->lock, flags);
1649         priv->stats.tx_dropped++;
1650         priv->stats.tx_errors++;
1651         spin_unlock_irqrestore(&priv->lock, flags);
1652
1653         if (netif_queue_stopped(dev))
1654                 netif_wake_queue(dev);
1655
1656         /*
1657          * FIXME: According to a patch from 2003-02-26, "returning non-zero
1658          * causes serious problems" here, allegedly.  Before that patch,
1659          * -ERRNO was returned which is not appropriate under Linux 2.6.
1660          * Perhaps more needs to be done?  Stop the queue in serious
1661          * conditions and restart it elsewhere?
1662          */
1663         /* return NETDEV_TX_BUSY; */
1664         return NETDEV_TX_OK;
1665 }
1666
1667 static void ether1394_get_drvinfo(struct net_device *dev,
1668                                   struct ethtool_drvinfo *info)
1669 {
1670         strcpy(info->driver, driver_name);
1671         strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */
1672 }
1673
1674 static struct ethtool_ops ethtool_ops = {
1675         .get_drvinfo = ether1394_get_drvinfo
1676 };
1677
1678 static int __init ether1394_init_module(void)
1679 {
1680         int err;
1681
1682         packet_task_cache = kmem_cache_create("packet_task",
1683                                               sizeof(struct packet_task),
1684                                               0, 0, NULL, NULL);
1685         if (!packet_task_cache)
1686                 return -ENOMEM;
1687
1688         hpsb_register_highlevel(&eth1394_highlevel);
1689         err = hpsb_register_protocol(&eth1394_proto_driver);
1690         if (err) {
1691                 hpsb_unregister_highlevel(&eth1394_highlevel);
1692                 kmem_cache_destroy(packet_task_cache);
1693         }
1694         return err;
1695 }
1696
1697 static void __exit ether1394_exit_module(void)
1698 {
1699         hpsb_unregister_protocol(&eth1394_proto_driver);
1700         hpsb_unregister_highlevel(&eth1394_highlevel);
1701         kmem_cache_destroy(packet_task_cache);
1702 }
1703
1704 module_init(ether1394_init_module);
1705 module_exit(ether1394_exit_module);