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