aboutsummaryrefslogtreecommitdiffstats
path: root/xenolinux-2.4.21-pre4-sparse/arch/xeno/drivers/network/network.c
blob: 7e85bf5abe0dd8996790d7cf6a5b3980c685db42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/******************************************************************************
 * network.c
 * 
 * Virtual network driver for XenoLinux.
 * 
 * Copyright (c) 2002, K A Fraser
 */

#include <linux/config.h>
#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/errno.h>

#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>

#include <asm/io.h>
#include <net/sock.h>
#include <net/pkt_sched.h>

#define NET_TX_IRQ _EVENT_NET_TX
#define NET_RX_IRQ _EVENT_NET_RX

#define TX_MAX_ENTRIES (TX_RING_SIZE - 2)
#define RX_MAX_ENTRIES (RX_RING_SIZE - 2)

#define TX_RING_INC(_i)    (((_i)+1) & (TX_RING_SIZE-1))
#define RX_RING_INC(_i)    (((_i)+1) & (RX_RING_SIZE-1))
#define TX_RING_ADD(_i,_j) (((_i)+(_j)) & (TX_RING_SIZE-1))
#define RX_RING_ADD(_i,_j) (((_i)+(_j)) & (RX_RING_SIZE-1))

#define RX_BUF_SIZE ((PAGE_SIZE/2)+1) /* Fool the slab allocator :-) */

static void network_rx_int(int irq, void *dev_id, struct pt_regs *ptregs);
static void network_tx_int(int irq, void *dev_id, struct pt_regs *ptregs);
static void network_tx_buf_gc(struct net_device *dev);
static void network_alloc_rx_buffers(struct net_device *dev);
static void network_free_rx_buffers(struct net_device *dev);
static void cleanup_module(void);

static struct list_head dev_list;

/*
 * RX RING:   RX_IDX <= rx_cons <= rx_prod
 * TX RING:   TX_IDX <= tx_cons <= tx_prod
 * (*_IDX allocated privately here, *_cons & *_prod shared with hypervisor)
 */
struct net_private
{
    struct list_head list;
    struct net_device *dev;

    struct net_device_stats stats;
    struct sk_buff **tx_skb_ring;
    struct sk_buff **rx_skb_ring;
    atomic_t tx_entries;
    unsigned int rx_idx, tx_idx, tx_full;
    net_ring_t *net_ring;
    spinlock_t tx_lock;
};


static void dbg_network_int(int irq, void *dev_id, struct pt_regs *ptregs)
{
    struct net_device *dev = (struct net_device *)dev_id;
    struct net_private *np = dev->priv;
    printk(KERN_ALERT "tx_full = %d, tx_entries = %d, tx_idx = %d,"
           " tx_cons = %d, tx_prod = %d, tx_event = %d, state=%d\n",
           np->tx_full, atomic_read(&np->tx_entries), np->tx_idx, 
           np->net_ring->tx_cons, np->net_ring->tx_prod, 
           np->net_ring->tx_event,
           test_bit(__LINK_STATE_XOFF, &dev->state));
}


static int network_open(struct net_device *dev)
{
    struct net_private *np = dev->priv;
    int error = 0;

    np->rx_idx = np->tx_idx = np->tx_full = 0;

    memset(&np->stats, 0, sizeof(np->stats));

    spin_lock_init(&np->tx_lock);

    atomic_set(&np->tx_entries, 0);

    np->net_ring->tx_prod = np->net_ring->tx_cons = np->net_ring->tx_event = 0;
    np->net_ring->rx_prod = np->net_ring->rx_cons = np->net_ring->rx_event = 0;
    np->net_ring->tx_ring = NULL;
    np->net_ring->rx_ring = NULL;

    np->tx_skb_ring = kmalloc(TX_RING_SIZE * sizeof(struct sk_buff *),
                              GFP_KERNEL);
    np->rx_skb_ring = kmalloc(RX_RING_SIZE * sizeof(struct sk_buff *),
                              GFP_KERNEL);
    np->net_ring->tx_ring = kmalloc(TX_RING_SIZE * sizeof(tx_entry_t), 
                                    GFP_KERNEL);
    np->net_ring->rx_ring = kmalloc(RX_RING_SIZE * sizeof(rx_entry_t), 
                                    GFP_KERNEL);
    if ( (np->tx_skb_ring == NULL) || (np->rx_skb_ring == NULL) ||
         (np->net_ring->tx_ring == NULL) || (np->net_ring->rx_ring == NULL) )
    {
        printk(KERN_WARNING "%s; Could not allocate ring memory\n", dev->name);
        error = -ENOBUFS;
        goto fail;
    }

    network_alloc_rx_buffers(dev);

    error = request_irq(NET_RX_IRQ, network_rx_int, 0, 
                        "net-rx", dev);
    if ( error )
    {
        printk(KERN_WARNING "%s: Could not allocate receive interrupt\n",
               dev->name);
        network_free_rx_buffers(dev);
        goto fail;
    }

    error = request_irq(NET_TX_IRQ, network_tx_int, 0, 
                        "net-tx", dev);
    if ( error )
    {
        printk(KERN_WARNING "%s: Could not allocate transmit interrupt\n",
               dev->name);
        free_irq(NET_RX_IRQ, dev);
        network_free_rx_buffers(dev);
        goto fail;
    }

    error = request_irq(_EVENT_DEBUG, dbg_network_int, SA_SHIRQ,
                        "debug", dev);
    if ( error )
    {
        printk(KERN_WARNING "%s: Non-fatal error -- no debug interrupt\n",
               dev->name);
    }

    printk("XenoLinux Virtual Network Driver installed as %s\n", dev->name);

    netif_start_queue(dev);

    MOD_INC_USE_COUNT;

    return 0;

 fail:
    if ( np->net_ring->rx_ring ) kfree(np->net_ring->rx_ring);
    if ( np->net_ring->tx_ring ) kfree(np->net_ring->tx_ring);
    if ( np->rx_skb_ring ) kfree(np->rx_skb_ring);
    if ( np->tx_skb_ring ) kfree(np->tx_skb_ring);
    kfree(np);
    return error;
}


static void network_tx_buf_gc(struct net_device *dev)
{
    unsigned int i;
    struct net_private *np = dev->priv;
    struct sk_buff *skb;
    unsigned long flags;
    unsigned int cons;

    spin_lock_irqsave(&np->tx_lock, flags);

    do {
        cons = np->net_ring->tx_cons;

        for ( i = np->tx_idx; i != cons; i = TX_RING_INC(i) )
        {
            skb = np->tx_skb_ring[i];
            dev_kfree_skb_any(skb);
            atomic_dec(&np->tx_entries);
        }
        
        np->tx_idx = i;
        
        /* Set a new event, then check for race with update of tx_cons. */
        np->net_ring->tx_event =
            TX_RING_ADD(cons, (atomic_read(&np->tx_entries)>>1) + 1);
        smp_mb();
    }
    while ( cons != np->net_ring->tx_cons );

    if ( np->tx_full && (atomic_read(&np->tx_entries) < TX_MAX_ENTRIES) )
    {
        np->tx_full = 0;
        netif_wake_queue(dev);
    }

    spin_unlock_irqrestore(&np->tx_lock, flags);
}

inline unsigned long get_ppte(unsigned long addr)
{
    unsigned long ppte;
    pgd_t *pgd; pmd_t *pmd; pte_t *ptep;
    pgd = pgd_offset_k(addr);

    if ( pgd_none(*pgd) || pgd_bad(*pgd) ) BUG();
        
    pmd = pmd_offset(pgd, addr);
    if ( pmd_none(*pmd) || pmd_bad(*pmd) ) BUG(); 
        
    ptep = pte_offset(pmd, addr);
    ppte = (unsigned long)phys_to_machine(virt_to_phys(ptep));

    return ppte;
}

static void network_alloc_rx_buffers(struct net_device *dev)
{
    unsigned int i;
    struct net_private *np = dev->priv;
    struct sk_buff *skb;
    unsigned int end = RX_RING_ADD(np->rx_idx, RX_MAX_ENTRIES);    

    for ( i = np->net_ring->rx_prod; i != end; i = RX_RING_INC(i) )
    {
        skb = dev_alloc_skb(RX_BUF_SIZE);
        if ( skb == NULL ) break;
        skb->dev = dev;
        skb_reserve(skb, 2); /* word align the IP header */
        np->rx_skb_ring[i] = skb;
        np->net_ring->rx_ring[i].addr = get_ppte((unsigned long)skb->head); 
        np->net_ring->rx_ring[i].size = RX_BUF_SIZE - 16; /* arbitrary */
    }

    np->net_ring->rx_prod = i;

    np->net_ring->rx_event = RX_RING_INC(np->rx_idx);

    /*
     * We may have allocated buffers which have entries outstanding in
     * the page update queue -- make sure we flush those first!
     */
    flush_page_update_queue();
    HYPERVISOR_net_update();
}


static void network_free_rx_buffers(struct net_device *dev)
{
    unsigned int i;
    struct net_private *np = dev->priv;
    struct sk_buff *skb;    

    for ( i = np->rx_idx; i != np->net_ring->rx_prod; i = RX_RING_INC(i) )
    {
        skb = np->rx_skb_ring[i];
        dev_kfree_skb_any(skb);
    }
}

static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
    unsigned int i;
    struct net_private *np = (struct net_private *)dev->priv;

    if ( np->tx_full )
    {
        printk(KERN_ALERT "%s: full queue wasn't stopped!\n", dev->name);
        netif_stop_queue(dev);
        return -ENOBUFS;
    }
    i = np->net_ring->tx_prod;

    if ( (((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >= PAGE_SIZE )
    {
        struct sk_buff *new_skb = dev_alloc_skb(RX_BUF_SIZE);
        if ( new_skb == NULL ) return 1;
        skb_put(new_skb, skb->len);
        memcpy(new_skb->data, skb->data, skb->len);
        dev_kfree_skb(skb);
        skb = new_skb;
    }   
    
    np->tx_skb_ring[i] = skb;
    np->net_ring->tx_ring[i].addr =
        (unsigned long)phys_to_machine(virt_to_phys(skb->data));
    np->net_ring->tx_ring[i].size = skb->len;
    np->net_ring->tx_prod = TX_RING_INC(i);
    atomic_inc(&np->tx_entries);

    np->stats.tx_bytes += skb->len;
    np->stats.tx_packets++;

    spin_lock_irq(&np->tx_lock);
    if ( atomic_read(&np->tx_entries) >= TX_MAX_ENTRIES )
    {
        np->tx_full = 1;
        netif_stop_queue(dev);
    }
    spin_unlock_irq(&np->tx_lock);

    network_tx_buf_gc(dev);

    HYPERVISOR_net_update();

    return 0;
}


static void network_rx_int(int irq, void *dev_id, struct pt_regs *ptregs)
{
    unsigned int i;
    struct net_device *dev = (struct net_device *)dev_id;
    struct net_private *np = dev->priv;
    struct sk_buff *skb;
    
 again:
    for ( i = np->rx_idx; i != np->net_ring->rx_cons; i = RX_RING_INC(i) )
    {
        if (np->net_ring->rx_ring[i].status != RING_STATUS_OK)
        {
            printk(KERN_ALERT "bad buffer on RX ring!(%d)\n", 
                   np->net_ring->rx_ring[i].status);
            continue;
        }
        skb = np->rx_skb_ring[i];

        phys_to_machine_mapping[virt_to_phys(skb->head) >> PAGE_SHIFT] =
            (*(unsigned long *)phys_to_virt(
                machine_to_phys(np->net_ring->rx_ring[i].addr))
                ) >> PAGE_SHIFT;

        skb_put(skb, np->net_ring->rx_ring[i].size);
        skb->protocol = eth_type_trans(skb, dev);

        /*
         * Set up shinfo -- from alloc_skb This was particularily nasty:  the
         * shared info is hidden at the back of the data area (presumably so it
         * can be shared), but on page flip it gets very spunked.
         */
        atomic_set(&(skb_shinfo(skb)->dataref), 1);
        skb_shinfo(skb)->nr_frags = 0;
        skb_shinfo(skb)->frag_list = NULL;
                                
        np->stats.rx_packets++;

        np->stats.rx_bytes += np->net_ring->rx_ring[i].size;
        netif_rx(skb);
        dev->last_rx = jiffies;
    }

    np->rx_idx = i;

    network_alloc_rx_buffers(dev);
    
    /* Deal with hypervisor racing our resetting of rx_event. */
    smp_mb();
    if ( np->net_ring->rx_cons != i ) goto again;
}


static void network_tx_int(int irq, void *dev_id, struct pt_regs *ptregs)
{
    struct net_device *dev = (struct net_device *)dev_id;
    network_tx_buf_gc(dev);
}


int network_close(struct net_device *dev)
{
    struct net_private *np = dev->priv;

    netif_stop_queue(dev);

    free_irq(NET_RX_IRQ, dev);
    free_irq(NET_TX_IRQ, dev);

    /*
     * XXXX This cannot be done safely until be have a proper interface
     * for setting up and tearing down virtual interfaces on the fly.
     * Currently the receive buffers are locked down by Xen and we have
     * no sensible way of retrieving them.
     */
#if 0
    network_free_rx_buffers(dev);
    kfree(np->net_ring->rx_ring);
    kfree(np->net_ring->tx_ring);
#endif

    kfree(np->rx_skb_ring);
    kfree(np->tx_skb_ring);

    MOD_DEC_USE_COUNT;

    return 0;
}


static struct net_device_stats *network_get_stats(struct net_device *dev)
{
    struct net_private *np = (struct net_private *)dev->priv;
    return &np->stats;
}


int __init init_module(void)
{
    int i, err;
    struct net_device *dev;
    struct net_private *np;

    INIT_LIST_HEAD(&dev_list);

    for ( i = 0; i < start_info.num_net_rings; i++ )
    {
        dev = alloc_etherdev(sizeof(struct net_private));
        if ( dev == NULL )
        {
            err = -ENOMEM;
            goto fail;
        }

        np = dev->priv;
        np->net_ring = start_info.net_rings + i;

        SET_MODULE_OWNER(dev);
        dev->open            = network_open;
        dev->hard_start_xmit = network_start_xmit;
        dev->stop            = network_close;
        dev->get_stats       = network_get_stats;

        memset(dev->dev_addr, 0, ETH_ALEN);
        *(unsigned int *)(dev->dev_addr + 1) = i;

        if ( (err = register_netdev(dev)) != 0 )
        {
            kfree(dev);
            goto fail;
        }

        np->dev = dev;
        list_add(&np->list, &dev_list);
    }

    return 0;

 fail:
    cleanup_module();
    return err;
}


static void cleanup_module(void)
{
    struct net_private *np;
    struct net_device *dev;

    while ( !list_empty(&dev_list) )
    {
        np = list_entry(dev_list.next, struct net_private, list);
        list_del(&np->list);
        dev = np->dev;
        unregister_netdev(dev);
        kfree(dev);
    }
}


module_init(init_module);
module_exit(cleanup_module);