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