rtdev.c 20.7 KB
Newer Older
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
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
/***
 *
 *  stack/rtdev.c - NIC device driver layer
 *
 *  Copyright (C) 1999       Lineo, Inc
 *                1999, 2002 David A. Schleef <ds@schleef.org>
 *                2002       Ulrich Marx <marx@kammer.uni-hannover.de>
 *                2003-2005  Jan Kiszka <jan.kiszka@web.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/spinlock.h>
#include <linux/if.h>
#include <linux/if_arp.h> /* ARPHRD_ETHER */
#include <linux/netdevice.h>
#include <linux/moduleparam.h>

#include <rtnet_internal.h>
#include <rtskb.h>
#include <ethernet/eth.h>
#include <rtmac/rtmac_disc.h>
36
#include <rtnet_port.h>
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
37
38
39
40
41


static unsigned int device_rtskbs = DEFAULT_DEVICE_RTSKBS;
module_param(device_rtskbs, uint, 0444);
MODULE_PARM_DESC(device_rtskbs, "Number of additional global realtime socket "
42
		 "buffers per network adapter");
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
43
44
45
46
47
48
49
50
51
52
53

struct rtnet_device         *rtnet_devices[MAX_RT_DEVICES];
static struct rtnet_device  *loopback_device;
static DEFINE_RTDM_LOCK(rtnet_devices_rt_lock);

LIST_HEAD(event_hook_list);
LIST_HEAD(rtskb_list);
DEFINE_MUTEX(rtnet_devices_nrt_lock);

static int rtdev_locked_xmit(struct rtskb *skb, struct rtnet_device *rtdev);

54
55
56
57
58
59
int rtdev_reference(struct rtnet_device *rtdev)
{
    smp_mb__before_atomic();
    if (rtdev->rt_owner && __atomic_add_unless(&rtdev->refcount, 1, 0) == 0) {
	if (!try_module_get(rtdev->rt_owner))
	    return 0;
60
61
	if (atomic_inc_return(&rtdev->refcount) != 1)
	    module_put(rtdev->rt_owner);
62
63
64
    }
    return 1;
}
65
EXPORT_SYMBOL_GPL(rtdev_reference);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
66

67
68
struct rtskb *rtnetdev_alloc_rtskb(struct rtnet_device *rtdev, unsigned int size)
{
69
    struct rtskb *rtskb = alloc_rtskb(size, &rtdev->dev_pool);
70
71
72
73
74
    if (rtskb)
	rtskb->rtdev = rtdev;
    return rtskb;
}
EXPORT_SYMBOL_GPL(rtnetdev_alloc_rtskb);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
75
76
77
78
79
80
81
82
83
84
85
86
87

/***
 *  __rtdev_get_by_name - find a rtnet_device by its name
 *  @name: name to find
 *  @note: caller must hold rtnet_devices_nrt_lock
 */
static struct rtnet_device *__rtdev_get_by_name(const char *name)
{
    int                 i;
    struct rtnet_device *rtdev;


    for (i = 0; i < MAX_RT_DEVICES; i++) {
88
89
90
	rtdev = rtnet_devices[i];
	if ((rtdev != NULL) && (strncmp(rtdev->name, name, IFNAMSIZ) == 0))
	    return rtdev;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    }
    return NULL;
}


/***
 *  rtdev_get_by_name - find and lock a rtnet_device by its name
 *  @name: name to find
 */
struct rtnet_device *rtdev_get_by_name(const char *name)
{
    struct rtnet_device *rtdev;
    rtdm_lockctx_t      context;


    rtdm_lock_get_irqsave(&rtnet_devices_rt_lock, context);

    rtdev = __rtdev_get_by_name(name);
109
110
    if (rtdev != NULL && !rtdev_reference(rtdev))
	    rtdev = NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

    rtdm_lock_put_irqrestore(&rtnet_devices_rt_lock, context);

    return rtdev;
}



/***
 *  rtdev_get_by_index - find and lock a rtnet_device by its ifindex
 *  @ifindex: index of device
 */
struct rtnet_device *rtdev_get_by_index(int ifindex)
{
    struct rtnet_device *rtdev;
    rtdm_lockctx_t      context;


    if ((ifindex <= 0) || (ifindex > MAX_RT_DEVICES))
130
	return NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
131
132
133
134

    rtdm_lock_get_irqsave(&rtnet_devices_rt_lock, context);

    rtdev = __rtdev_get_by_index(ifindex);
135
136
    if (rtdev != NULL && !rtdev_reference(rtdev))
	    rtdev = NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    rtdm_lock_put_irqrestore(&rtnet_devices_rt_lock, context);

    return rtdev;
}



/***
 *  __rtdev_get_by_hwaddr - find a rtnetdevice by its mac-address
 *  @type:          Type of the net_device (may be ARPHRD_ETHER)
 *  @hw_addr:       MAC-Address
 */
static inline struct rtnet_device *__rtdev_get_by_hwaddr(unsigned short type, char *hw_addr)
{
    int                 i;
    struct rtnet_device *rtdev;


    for (i = 0; i < MAX_RT_DEVICES; i++) {
157
158
159
160
161
	rtdev = rtnet_devices[i];
	if ((rtdev != NULL) && (rtdev->type == type) &&
	    (!memcmp(rtdev->dev_addr, hw_addr, rtdev->addr_len))) {
	    return rtdev;
	}
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    }
    return NULL;
}



/***
 *  rtdev_get_by_hwaddr - find and lock a rtnetdevice by its mac-address
 *  @type:          Type of the net_device (may be ARPHRD_ETHER)
 *  @hw_addr:       MAC-Address
 */
struct rtnet_device *rtdev_get_by_hwaddr(unsigned short type, char *hw_addr)
{
    struct rtnet_device *rtdev;
    rtdm_lockctx_t      context;


    rtdm_lock_get_irqsave(&rtnet_devices_rt_lock, context);

    rtdev = __rtdev_get_by_hwaddr(type, hw_addr);
182
183
    if (rtdev != NULL && !rtdev_reference(rtdev))
	    rtdev = NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

    rtdm_lock_put_irqrestore(&rtnet_devices_rt_lock, context);

    return rtdev;
}



/***
 *  rtdev_get_by_hwaddr - find and lock the loopback device if available
 */
struct rtnet_device *rtdev_get_loopback(void)
{
    struct rtnet_device *rtdev;
    rtdm_lockctx_t      context;


    rtdm_lock_get_irqsave(&rtnet_devices_rt_lock, context);

    rtdev = loopback_device;
204
205
    if (rtdev != NULL && !rtdev_reference(rtdev))
	    rtdev = NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

    rtdm_lock_put_irqrestore(&rtnet_devices_rt_lock, context);

    return rtdev;
}



/***
 *  rtdev_alloc_name - allocate a name for the rtnet_device
 *  @rtdev:         the rtnet_device
 *  @name_mask:     a name mask (e.g. "rteth%d" for ethernet)
 *
 *  This function have to be called from the driver probe function.
 */
void rtdev_alloc_name(struct rtnet_device *rtdev, const char *mask)
{
    char                buf[IFNAMSIZ];
    int                 i;
    struct rtnet_device *tmp;


    for (i = 0; i < MAX_RT_DEVICES; i++) {
229
230
231
232
233
234
235
	snprintf(buf, IFNAMSIZ, mask, i);
	if ((tmp = rtdev_get_by_name(buf)) == NULL) {
	    strncpy(rtdev->name, buf, IFNAMSIZ);
	    break;
	}
	else
	    rtdev_dereference(tmp);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
236
237
238
    }
}

239
static int rtdev_pool_trylock(void *cookie)
240
241
242
243
{
    return rtdev_reference(cookie);
}

244
static void rtdev_pool_unlock(void *cookie)
245
{
246
    rtdev_dereference(cookie);
247
}
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
248

249
static const struct rtskb_pool_lock_ops rtdev_ops = {
250
251
    .trylock = rtdev_pool_trylock,
    .unlock = rtdev_pool_unlock,
252
};
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
253
254
255
256
257
258
259

/***
 *  rtdev_alloc
 *  @int sizeof_priv:
 *
 *  allocate memory for a new rt-network-adapter
 */
260
struct rtnet_device *rtdev_alloc(unsigned sizeof_priv, unsigned dev_pool_size)
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
261
262
{
    struct rtnet_device *rtdev;
263
264
    unsigned            alloc_size;
    int                 ret;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
265
266
267
268
269
270
271


    /* ensure 32-byte alignment of the private area */
    alloc_size = sizeof (*rtdev) + sizeof_priv + 31;

    rtdev = (struct rtnet_device *)kmalloc(alloc_size, GFP_KERNEL);
    if (rtdev == NULL) {
272
273
274
275
	printk(KERN_ERR "RTnet: cannot allocate rtnet device\n");
	return NULL;
    }

276
277
    memset(rtdev, 0, alloc_size);

278
279
280
281
    ret = rtskb_pool_init(&rtdev->dev_pool, dev_pool_size, &rtdev_ops, rtdev);
    if (ret < dev_pool_size) {
	printk(KERN_ERR "RTnet: cannot allocate rtnet device pool\n");
	rtskb_pool_release(&rtdev->dev_pool);
282
283
	kfree(rtdev);
	return NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
284
285
286
287
288
289
290
291
292
293
294
295
    }

    rtdm_mutex_init(&rtdev->xmit_mutex);
    rtdm_lock_init(&rtdev->rtdev_lock);
    mutex_init(&rtdev->nrt_lock);

    atomic_set(&rtdev->refcount, 0);

    /* scale global rtskb pool */
    rtdev->add_rtskbs = rtskb_pool_extend(&global_pool, device_rtskbs);

    if (sizeof_priv)
296
	rtdev->priv = (void *)(((long)(rtdev + 1) + 31) & ~31);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
297
298
299
300
301
302
303
304
305
306
307
308

    return rtdev;
}



/***
 *  rtdev_free
 */
void rtdev_free (struct rtnet_device *rtdev)
{
    if (rtdev != NULL) {
309
	rtskb_pool_release(&rtdev->dev_pool);
310
311
312
313
	rtskb_pool_shrink(&global_pool, rtdev->add_rtskbs);
	rtdev->stack_event = NULL;
	rtdm_mutex_destroy(&rtdev->xmit_mutex);
	kfree(rtdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
314
315
316
317
318
319
320
321
322
    }
}



/**
 * rtalloc_etherdev - Allocates and sets up an ethernet device
 * @sizeof_priv: size of additional driver-private structure to
 *               be allocated for this ethernet device
323
 * @dev_pool_size: size of the rx pool
324
 * @module: module creating the deivce
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
325
326
327
328
329
330
 *
 * Fill in the fields of the device structure with ethernet-generic
 * values. Basically does everything except registering the device.
 *
 * A 32-byte alignment is enforced for the private data area.
 */
331
struct rtnet_device *__rt_alloc_etherdev(unsigned sizeof_priv,
332
					unsigned dev_pool_size,
333
					struct module *module)
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
334
335
336
{
    struct rtnet_device *rtdev;

337
    rtdev = rtdev_alloc(sizeof_priv, dev_pool_size);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
338
    if (!rtdev)
339
	return NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
340
341
342
343
344
345
346
347

    rtdev->hard_header     = rt_eth_header;
    rtdev->type            = ARPHRD_ETHER;
    rtdev->hard_header_len = ETH_HLEN;
    rtdev->mtu             = 1500; /* eth_mtu */
    rtdev->addr_len        = ETH_ALEN;
    rtdev->flags           = IFF_BROADCAST; /* TODO: IFF_MULTICAST; */
    rtdev->get_mtu         = rt_hard_mtu;
348
    rtdev->rt_owner	   = module;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

    memset(rtdev->broadcast, 0xFF, ETH_ALEN);
    strcpy(rtdev->name, "rteth%d");

    return rtdev;
}



static inline int __rtdev_new_index(void)
{
    int i;


    for (i = 0; i < MAX_RT_DEVICES; i++)
364
365
	if (rtnet_devices[i] == NULL)
	     return i+1;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
366
367
368
369
370
371
372
373
374
375
376
377
378

    return -ENOMEM;
}



static int rtskb_map(struct rtnet_device *rtdev, struct rtskb *skb)
{
    dma_addr_t addr;

    addr = rtdev->map_rtskb(rtdev, skb);

    if (WARN_ON(addr == RTSKB_UNMAPPED))
379
	return -ENOMEM;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
380
381

    if (skb->buf_dma_addr != RTSKB_UNMAPPED &&
382
383
384
385
386
	addr != skb->buf_dma_addr) {
	printk("RTnet: device %s maps skb differently than others. "
	       "Different IOMMU domain?\nThis is not supported.\n",
	       rtdev->name);
	return -EACCES;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    }

    skb->buf_dma_addr = addr;

    return 0;
}



int rtdev_map_rtskb(struct rtskb *skb)
{
    struct rtnet_device *rtdev;
    int err = 0;
    int i;

    skb->buf_dma_addr = RTSKB_UNMAPPED;

    mutex_lock(&rtnet_devices_nrt_lock);

    for (i = 0; i < MAX_RT_DEVICES; i++) {
407
408
409
410
411
412
	rtdev = rtnet_devices[i];
	if (rtdev && rtdev->map_rtskb) {
	    err = rtskb_map(rtdev, skb);
	    if (err)
		break;
	}
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
413
414
415
    }

    if (!err)
416
	list_add(&skb->entry, &rtskb_list);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430

    mutex_unlock(&rtnet_devices_nrt_lock);

    return err;
}



static int rtdev_map_all_rtskbs(struct rtnet_device *rtdev)
{
    struct rtskb *skb;
    int err = 0;

    if (!rtdev->map_rtskb)
431
	return 0;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
432
433

    list_for_each_entry(skb, &rtskb_list, entry) {
434
435
436
	err = rtskb_map(rtdev, skb);
	if (err)
	   break;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
437
438
439
440
441
442
443
444
445
446
447
448
449
    }

    return err;
}



void rtdev_unmap_rtskb(struct rtskb *skb)
{
    struct rtnet_device *rtdev;
    int i;

    if (skb->buf_dma_addr == RTSKB_UNMAPPED)
450
	return;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
451
452
453
454
455
456

    mutex_lock(&rtnet_devices_nrt_lock);

    list_del(&skb->entry);

    for (i = 0; i < MAX_RT_DEVICES; i++) {
457
458
459
460
	rtdev = rtnet_devices[i];
	if (rtdev && rtdev->unmap_rtskb) {
	    rtdev->unmap_rtskb(rtdev, skb);
	}
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
    }

    skb->buf_dma_addr = RTSKB_UNMAPPED;

    mutex_unlock(&rtnet_devices_nrt_lock);
}



static void rtdev_unmap_all_rtskbs(struct rtnet_device *rtdev)
{
    struct rtskb *skb;

    if (!rtdev->unmap_rtskb)
475
	return;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
476
477

    list_for_each_entry(skb, &rtskb_list, entry) {
478
	rtdev->unmap_rtskb(rtdev, skb);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
    }
}



/***
 * rt_register_rtnetdev: register a new rtnet_device (linux-like)
 * @rtdev:               the device
 */
int rt_register_rtnetdev(struct rtnet_device *rtdev)
{
    struct list_head        *entry;
    struct rtdev_event_hook *hook;
    rtdm_lockctx_t          context;
    int                     ifindex;
    int                     err;


    /* requires at least driver layer version 2.0 */
    if (rtdev->vers < RTDEV_VERS_2_0)
499
	return -EINVAL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
500
501

    if (rtdev->features & NETIF_F_LLTX)
502
	rtdev->start_xmit = rtdev->hard_start_xmit;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
503
    else
504
	rtdev->start_xmit = rtdev_locked_xmit;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
505
506
507
508
509

    mutex_lock(&rtnet_devices_nrt_lock);

    ifindex = __rtdev_new_index();
    if (ifindex < 0) {
510
511
	    err = ifindex;
	    goto fail;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
512
513
514
515
    }
    rtdev->ifindex = ifindex;

    if (strchr(rtdev->name,'%') != NULL)
516
	rtdev_alloc_name(rtdev, rtdev->name);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
517
518

    if (__rtdev_get_by_name(rtdev->name) != NULL) {
519
520
	    err = -EEXIST;
	    goto fail;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
521
522
    }

523
524
525
526
527
528
529
530
531
532
533
534
    rtdev->sysdev = device_create(rtnet_class, NULL,
		  MKDEV(0, rtdev->ifindex), rtdev, rtdev->name);
    if (rtdev->sysdev == NULL) {
	    err = PTR_ERR(rtdev->sysdev);
	    goto fail;
    }

    if (rtdev->sysbind) {
	    err = sysfs_create_link(&rtdev->sysdev->kobj,
				    &rtdev->sysbind->kobj, "adapter");
	    if (err)
		    goto fail_link;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
535
    }
536
537
538
539
   
    err = rtdev_map_all_rtskbs(rtdev);
    if (err)
	    goto fail_map;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
540
541
542
543

    rtdm_lock_get_irqsave(&rtnet_devices_rt_lock, context);

    if (rtdev->flags & IFF_LOOPBACK) {
544
545
546
	/* allow only one loopback device */
	if (loopback_device) {
	    rtdm_lock_put_irqrestore(&rtnet_devices_rt_lock, context);
547
548
	    err = -EEXIST;
	    goto fail_loopback;
549
550
	}
	loopback_device = rtdev;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
551
552
553
554
555
556
    }
    rtnet_devices[rtdev->ifindex-1] = rtdev;

    rtdm_lock_put_irqrestore(&rtnet_devices_rt_lock, context);

    list_for_each(entry, &event_hook_list) {
557
558
559
	hook = list_entry(entry, struct rtdev_event_hook, entry);
	if (hook->register_device)
	    hook->register_device(rtdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
560
561
562
563
564
565
566
567
568
569
    }

    mutex_unlock(&rtnet_devices_nrt_lock);

    /* Default state at registration is that the device is present. */
    set_bit(__RTNET_LINK_STATE_PRESENT, &rtdev->link_state);

    printk("RTnet: registered %s\n", rtdev->name);

    return 0;
570
571
572
573
574
575
576
577
578
579
580
581

fail_loopback:
    rtdev_unmap_all_rtskbs(rtdev);
fail_map:
    if (rtdev->sysbind)
	    sysfs_remove_link(&rtdev->sysdev->kobj, "adapter");
fail_link:
    device_destroy(rtnet_class, MKDEV(0, rtdev->ifindex));
fail:
    mutex_unlock(&rtnet_devices_nrt_lock);

    return err;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
}



/***
 * rt_unregister_rtnetdev: unregister a rtnet_device
 * @rtdev:                 the device
 */
int rt_unregister_rtnetdev(struct rtnet_device *rtdev)
{
    struct list_head        *entry;
    struct rtdev_event_hook *hook;
    rtdm_lockctx_t          context;

    RTNET_ASSERT(rtdev->ifindex != 0,
597
598
	printk("RTnet: device %s/%p was not registered\n", rtdev->name, rtdev);
	return -ENODEV;);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
599

600
601
602
603
604
    if (rtdev->sysbind)
	    sysfs_remove_link(&rtdev->sysdev->kobj, "adapter");

    device_destroy(rtnet_class, MKDEV(0, rtdev->ifindex));

Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
605
606
607
    mutex_lock(&rtnet_devices_nrt_lock);
    rtdm_lock_get_irqsave(&rtnet_devices_rt_lock, context);

608
    RTNET_ASSERT(atomic_read(&rtdev->refcount == 0), BUG());
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
609
610
    rtnet_devices[rtdev->ifindex-1] = NULL;
    if (rtdev->flags & IFF_LOOPBACK)
611
	loopback_device = NULL;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
612
613
614
615

    rtdm_lock_put_irqrestore(&rtnet_devices_rt_lock, context);

    list_for_each(entry, &event_hook_list) {
616
617
618
	hook = list_entry(entry, struct rtdev_event_hook, entry);
	if (hook->unregister_device)
	    hook->unregister_device(rtdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
619
620
621
622
623
624
625
626
627
    }

    rtdev_unmap_all_rtskbs(rtdev);

    mutex_unlock(&rtnet_devices_nrt_lock);

    clear_bit(__RTNET_LINK_STATE_PRESENT, &rtdev->link_state);

    RTNET_ASSERT(atomic_read(&rtdev->refcount) == 0,
628
	   printk("RTnet: rtdev reference counter < 0!\n"););
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

    printk("RTnet: unregistered %s\n", rtdev->name);

    return 0;
}



void rtdev_add_event_hook(struct rtdev_event_hook *hook)
{
    mutex_lock(&rtnet_devices_nrt_lock);
    list_add(&hook->entry, &event_hook_list);
    mutex_unlock(&rtnet_devices_nrt_lock);
}



void rtdev_del_event_hook(struct rtdev_event_hook *hook)
{
    mutex_lock(&rtnet_devices_nrt_lock);
    list_del(&hook->entry);
    mutex_unlock(&rtnet_devices_nrt_lock);
}

653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
int rtdev_up(struct rtnet_device *rtdev, struct rtnet_core_cmd *cmd)
{
	struct list_head        *entry;
	struct rtdev_event_hook *hook;
	int ret = 0;

	if (mutex_lock_interruptible(&rtdev->nrt_lock))
		return -ERESTARTSYS;

	/* We cannot change the promisc flag or the hardware address if
	   the device is already up. */
	if ((rtdev->flags & IFF_UP) &&
	    (((cmd->args.up.set_dev_flags | cmd->args.up.clear_dev_flags) &
	      IFF_PROMISC) ||
	     (cmd->args.up.dev_addr_type != ARPHRD_VOID))) {
		ret = -EBUSY;
		goto up_out;
	}

	rtdev->flags |= cmd->args.up.set_dev_flags;
	rtdev->flags &= ~cmd->args.up.clear_dev_flags;

	if (cmd->args.up.dev_addr_type != ARPHRD_VOID) {
		if (cmd->args.up.dev_addr_type != rtdev->type) {
			ret = -EINVAL;
			goto up_out;
		}
		memcpy(rtdev->dev_addr, cmd->args.up.dev_addr, MAX_ADDR_LEN);
	}

	set_bit(PRIV_FLAG_UP, &rtdev->priv_flags);

	ret = rtdev_open(rtdev);    /* also == 0 if rtdev is already up */

	if (ret == 0) {
		mutex_lock(&rtnet_devices_nrt_lock);

		list_for_each(entry, &event_hook_list) {
			hook = list_entry(entry, struct rtdev_event_hook, entry);
			if (hook->ifup)
				hook->ifup(rtdev, cmd);
		}

		mutex_unlock(&rtnet_devices_nrt_lock);
	} else
		clear_bit(PRIV_FLAG_UP, &rtdev->priv_flags);

up_out:
	mutex_unlock(&rtdev->nrt_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(rtdev_up);

int rtdev_down(struct rtnet_device *rtdev)
{
	struct list_head        *entry;
	struct rtdev_event_hook *hook;
	rtdm_lockctx_t          context;
	int ret = 0;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
713

714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
	if (mutex_lock_interruptible(&rtdev->nrt_lock))
		return -ERESTARTSYS;

	/* spin lock required for sync with routing code */
	rtdm_lock_get_irqsave(&rtdev->rtdev_lock, context);

	if (test_bit(PRIV_FLAG_ADDING_ROUTE, &rtdev->priv_flags)) {
		rtdm_lock_put_irqrestore(&rtdev->rtdev_lock, context);

		mutex_unlock(&rtdev->nrt_lock);
		return -EBUSY;
	}
	clear_bit(PRIV_FLAG_UP, &rtdev->priv_flags);

	rtdm_lock_put_irqrestore(&rtdev->rtdev_lock, context);

	if (rtdev->mac_detach != NULL)
		ret = rtdev->mac_detach(rtdev);

	if (ret == 0) {
		mutex_lock(&rtnet_devices_nrt_lock);

		list_for_each(entry, &event_hook_list) {
			hook = list_entry(entry, struct rtdev_event_hook, entry);
			if (hook->ifdown)
				hook->ifdown(rtdev);
		}

		mutex_unlock(&rtnet_devices_nrt_lock);

		ret = rtdev_close(rtdev);
	}

	mutex_unlock(&rtdev->nrt_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(rtdev_down);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
752
753
754
755
756
757
758
759
760
761
762
763

/***
 *  rtdev_open
 *
 *  Prepare an interface for use.
 */
int rtdev_open(struct rtnet_device *rtdev)
{
    int ret = 0;


    if (rtdev->flags & IFF_UP)              /* Is it already up?                */
764
765
766
767
	return 0;

    if (!rtdev_reference(rtdev))
	return -EIDRM;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
768
769

    if (rtdev->open)                        /* Call device private open method  */
770
	ret = rtdev->open(rtdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
771
772

    if ( !ret )  {
773
	rtdev->flags |= IFF_UP;
774
775
776
	set_bit(__RTNET_LINK_STATE_START, &rtdev->link_state);
    } else
	rtdev_dereference(rtdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791

    return ret;
}



/***
 *  rtdev_close
 */
int rtdev_close(struct rtnet_device *rtdev)
{
    int ret = 0;


    if ( !(rtdev->flags & IFF_UP) )
792
	return 0;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
793
794

    if (rtdev->stop)
795
	ret = rtdev->stop(rtdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
796
797
798
799

    rtdev->flags &= ~(IFF_UP|IFF_RUNNING);
    clear_bit(__RTNET_LINK_STATE_START, &rtdev->link_state);

800
801
802
    if (ret == 0)
	rtdev_dereference(rtdev);

Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
    return ret;
}



static int rtdev_locked_xmit(struct rtskb *skb, struct rtnet_device *rtdev)
{
    int ret;


    rtdm_mutex_lock(&rtdev->xmit_mutex);
    ret = rtdev->hard_start_xmit(skb, rtdev);
    rtdm_mutex_unlock(&rtdev->xmit_mutex);

    return ret;
}



/***
 *  rtdev_xmit - send real-time packet
 */
int rtdev_xmit(struct rtskb *rtskb)
{
    struct rtnet_device *rtdev;
    int                 err;


    RTNET_ASSERT(rtskb != NULL, return -EINVAL;);

    rtdev = rtskb->rtdev;

835
836
837
838
839
840
    if (!rtnetif_carrier_ok(rtdev)) {
	err = -EAGAIN;
	kfree_rtskb(rtskb);
	return err;
    }

841
842
843
844
845
846
    if (rtskb_acquire(rtskb, &rtdev->dev_pool) != 0) {
	err = -ENOBUFS;
	kfree_rtskb(rtskb);
	return err;
    }

Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
847
848
849
850
    RTNET_ASSERT(rtdev != NULL, return -EINVAL;);

    err = rtdev->start_xmit(rtskb, rtdev);
    if (err) {
851
852
	/* on error we must free the rtskb here */
	kfree_rtskb(rtskb);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
853

854
	rtdm_printk("hard_start_xmit returned %d\n", err);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
855
856
857
858
859
860
861
    }

    return err;
}



Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
862
#if IS_ENABLED(CONFIG_XENO_DRIVERS_NET_ADDON_PROXY)
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/***
 *      rtdev_xmit_proxy - send rtproxy packet
 */
int rtdev_xmit_proxy(struct rtskb *rtskb)
{
    struct rtnet_device *rtdev;
    int                 err;


    RTNET_ASSERT(rtskb != NULL, return -EINVAL;);

    rtdev = rtskb->rtdev;

    RTNET_ASSERT(rtdev != NULL, return -EINVAL;);

    /* TODO: make these lines race-condition-safe */
    if (rtdev->mac_disc) {
880
	RTNET_ASSERT(rtdev->mac_disc->nrt_packet_tx != NULL, return -EINVAL;);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
881

882
	err = rtdev->mac_disc->nrt_packet_tx(rtskb);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
883
    } else {
884
885
886
887
	err = rtdev->start_xmit(rtskb, rtdev);
	if (err) {
	    /* on error we must free the rtskb here */
	    kfree_rtskb(rtskb);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
888

889
890
	    rtdm_printk("hard_start_xmit returned %d\n", err);
	}
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
891
892
893
894
895
896
897
898
899
900
901
902
903
904
    }

    return err;
}
#endif /* CONFIG_XENO_DRIVERS_NET_ADDON_PROXY */



unsigned int rt_hard_mtu(struct rtnet_device *rtdev, unsigned int priority)
{
    return rtdev->mtu;
}


905
EXPORT_SYMBOL_GPL(__rt_alloc_etherdev);
906
EXPORT_SYMBOL_GPL(rtdev_free);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
907

908
EXPORT_SYMBOL_GPL(rtdev_alloc_name);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
909

910
911
EXPORT_SYMBOL_GPL(rt_register_rtnetdev);
EXPORT_SYMBOL_GPL(rt_unregister_rtnetdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
912

913
914
EXPORT_SYMBOL_GPL(rtdev_add_event_hook);
EXPORT_SYMBOL_GPL(rtdev_del_event_hook);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
915

916
917
918
919
EXPORT_SYMBOL_GPL(rtdev_get_by_name);
EXPORT_SYMBOL_GPL(rtdev_get_by_index);
EXPORT_SYMBOL_GPL(rtdev_get_by_hwaddr);
EXPORT_SYMBOL_GPL(rtdev_get_loopback);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
920

921
EXPORT_SYMBOL_GPL(rtdev_xmit);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
922

Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
923
#if IS_ENABLED(CONFIG_XENO_DRIVERS_NET_ADDON_PROXY)
924
EXPORT_SYMBOL_GPL(rtdev_xmit_proxy);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
925
926
#endif

927
EXPORT_SYMBOL_GPL(rt_hard_mtu);