OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [ieee1394/] [eth1394.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.