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