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