~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to net/core/ethtool.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <linux/uaccess.h>
22
22
#include <linux/vmalloc.h>
23
23
#include <linux/slab.h>
 
24
#include <linux/rtnetlink.h>
 
25
#include <linux/sched.h>
24
26
 
25
27
/*
26
28
 * Some useful ethtool_ops methods that're device independent.
34
36
}
35
37
EXPORT_SYMBOL(ethtool_op_get_link);
36
38
 
37
 
u32 ethtool_op_get_rx_csum(struct net_device *dev)
38
 
{
39
 
        return (dev->features & NETIF_F_ALL_CSUM) != 0;
40
 
}
41
 
EXPORT_SYMBOL(ethtool_op_get_rx_csum);
42
 
 
43
39
u32 ethtool_op_get_tx_csum(struct net_device *dev)
44
40
{
45
41
        return (dev->features & NETIF_F_ALL_CSUM) != 0;
55
51
 
56
52
        return 0;
57
53
}
 
54
EXPORT_SYMBOL(ethtool_op_set_tx_csum);
58
55
 
59
56
int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
60
57
{
186
183
 
187
184
/* Handlers for each ethtool command */
188
185
 
 
186
#define ETHTOOL_DEV_FEATURE_WORDS       1
 
187
 
 
188
static void ethtool_get_features_compat(struct net_device *dev,
 
189
        struct ethtool_get_features_block *features)
 
190
{
 
191
        if (!dev->ethtool_ops)
 
192
                return;
 
193
 
 
194
        /* getting RX checksum */
 
195
        if (dev->ethtool_ops->get_rx_csum)
 
196
                if (dev->ethtool_ops->get_rx_csum(dev))
 
197
                        features[0].active |= NETIF_F_RXCSUM;
 
198
 
 
199
        /* mark legacy-changeable features */
 
200
        if (dev->ethtool_ops->set_sg)
 
201
                features[0].available |= NETIF_F_SG;
 
202
        if (dev->ethtool_ops->set_tx_csum)
 
203
                features[0].available |= NETIF_F_ALL_CSUM;
 
204
        if (dev->ethtool_ops->set_tso)
 
205
                features[0].available |= NETIF_F_ALL_TSO;
 
206
        if (dev->ethtool_ops->set_rx_csum)
 
207
                features[0].available |= NETIF_F_RXCSUM;
 
208
        if (dev->ethtool_ops->set_flags)
 
209
                features[0].available |= flags_dup_features;
 
210
}
 
211
 
 
212
static int ethtool_set_feature_compat(struct net_device *dev,
 
213
        int (*legacy_set)(struct net_device *, u32),
 
214
        struct ethtool_set_features_block *features, u32 mask)
 
215
{
 
216
        u32 do_set;
 
217
 
 
218
        if (!legacy_set)
 
219
                return 0;
 
220
 
 
221
        if (!(features[0].valid & mask))
 
222
                return 0;
 
223
 
 
224
        features[0].valid &= ~mask;
 
225
 
 
226
        do_set = !!(features[0].requested & mask);
 
227
 
 
228
        if (legacy_set(dev, do_set) < 0)
 
229
                netdev_info(dev,
 
230
                        "Legacy feature change (%s) failed for 0x%08x\n",
 
231
                        do_set ? "set" : "clear", mask);
 
232
 
 
233
        return 1;
 
234
}
 
235
 
 
236
static int ethtool_set_flags_compat(struct net_device *dev,
 
237
        int (*legacy_set)(struct net_device *, u32),
 
238
        struct ethtool_set_features_block *features, u32 mask)
 
239
{
 
240
        u32 value;
 
241
 
 
242
        if (!legacy_set)
 
243
                return 0;
 
244
 
 
245
        if (!(features[0].valid & mask))
 
246
                return 0;
 
247
 
 
248
        value = dev->features & ~features[0].valid;
 
249
        value |= features[0].requested;
 
250
 
 
251
        features[0].valid &= ~mask;
 
252
 
 
253
        if (legacy_set(dev, value & mask) < 0)
 
254
                netdev_info(dev, "Legacy flags change failed\n");
 
255
 
 
256
        return 1;
 
257
}
 
258
 
 
259
static int ethtool_set_features_compat(struct net_device *dev,
 
260
        struct ethtool_set_features_block *features)
 
261
{
 
262
        int compat;
 
263
 
 
264
        if (!dev->ethtool_ops)
 
265
                return 0;
 
266
 
 
267
        compat  = ethtool_set_feature_compat(dev, dev->ethtool_ops->set_sg,
 
268
                features, NETIF_F_SG);
 
269
        compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tx_csum,
 
270
                features, NETIF_F_ALL_CSUM);
 
271
        compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tso,
 
272
                features, NETIF_F_ALL_TSO);
 
273
        compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_rx_csum,
 
274
                features, NETIF_F_RXCSUM);
 
275
        compat |= ethtool_set_flags_compat(dev, dev->ethtool_ops->set_flags,
 
276
                features, flags_dup_features);
 
277
 
 
278
        return compat;
 
279
}
 
280
 
 
281
static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
 
282
{
 
283
        struct ethtool_gfeatures cmd = {
 
284
                .cmd = ETHTOOL_GFEATURES,
 
285
                .size = ETHTOOL_DEV_FEATURE_WORDS,
 
286
        };
 
287
        struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
 
288
                {
 
289
                        .available = dev->hw_features,
 
290
                        .requested = dev->wanted_features,
 
291
                        .active = dev->features,
 
292
                        .never_changed = NETIF_F_NEVER_CHANGE,
 
293
                },
 
294
        };
 
295
        u32 __user *sizeaddr;
 
296
        u32 copy_size;
 
297
 
 
298
        ethtool_get_features_compat(dev, features);
 
299
 
 
300
        sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
 
301
        if (get_user(copy_size, sizeaddr))
 
302
                return -EFAULT;
 
303
 
 
304
        if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
 
305
                copy_size = ETHTOOL_DEV_FEATURE_WORDS;
 
306
 
 
307
        if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
 
308
                return -EFAULT;
 
309
        useraddr += sizeof(cmd);
 
310
        if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
 
311
                return -EFAULT;
 
312
 
 
313
        return 0;
 
314
}
 
315
 
 
316
static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
 
317
{
 
318
        struct ethtool_sfeatures cmd;
 
319
        struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
 
320
        int ret = 0;
 
321
 
 
322
        if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
 
323
                return -EFAULT;
 
324
        useraddr += sizeof(cmd);
 
325
 
 
326
        if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
 
327
                return -EINVAL;
 
328
 
 
329
        if (copy_from_user(features, useraddr, sizeof(features)))
 
330
                return -EFAULT;
 
331
 
 
332
        if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
 
333
                return -EINVAL;
 
334
 
 
335
        if (ethtool_set_features_compat(dev, features))
 
336
                ret |= ETHTOOL_F_COMPAT;
 
337
 
 
338
        if (features[0].valid & ~dev->hw_features) {
 
339
                features[0].valid &= dev->hw_features;
 
340
                ret |= ETHTOOL_F_UNSUPPORTED;
 
341
        }
 
342
 
 
343
        dev->wanted_features &= ~features[0].valid;
 
344
        dev->wanted_features |= features[0].valid & features[0].requested;
 
345
        __netdev_update_features(dev);
 
346
 
 
347
        if ((dev->wanted_features ^ dev->features) & features[0].valid)
 
348
                ret |= ETHTOOL_F_WISH;
 
349
 
 
350
        return ret;
 
351
}
 
352
 
 
353
static const char netdev_features_strings[ETHTOOL_DEV_FEATURE_WORDS * 32][ETH_GSTRING_LEN] = {
 
354
        /* NETIF_F_SG */              "tx-scatter-gather",
 
355
        /* NETIF_F_IP_CSUM */         "tx-checksum-ipv4",
 
356
        /* NETIF_F_NO_CSUM */         "tx-checksum-unneeded",
 
357
        /* NETIF_F_HW_CSUM */         "tx-checksum-ip-generic",
 
358
        /* NETIF_F_IPV6_CSUM */       "tx-checksum-ipv6",
 
359
        /* NETIF_F_HIGHDMA */         "highdma",
 
360
        /* NETIF_F_FRAGLIST */        "tx-scatter-gather-fraglist",
 
361
        /* NETIF_F_HW_VLAN_TX */      "tx-vlan-hw-insert",
 
362
 
 
363
        /* NETIF_F_HW_VLAN_RX */      "rx-vlan-hw-parse",
 
364
        /* NETIF_F_HW_VLAN_FILTER */  "rx-vlan-filter",
 
365
        /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
 
366
        /* NETIF_F_GSO */             "tx-generic-segmentation",
 
367
        /* NETIF_F_LLTX */            "tx-lockless",
 
368
        /* NETIF_F_NETNS_LOCAL */     "netns-local",
 
369
        /* NETIF_F_GRO */             "rx-gro",
 
370
        /* NETIF_F_LRO */             "rx-lro",
 
371
 
 
372
        /* NETIF_F_TSO */             "tx-tcp-segmentation",
 
373
        /* NETIF_F_UFO */             "tx-udp-fragmentation",
 
374
        /* NETIF_F_GSO_ROBUST */      "tx-gso-robust",
 
375
        /* NETIF_F_TSO_ECN */         "tx-tcp-ecn-segmentation",
 
376
        /* NETIF_F_TSO6 */            "tx-tcp6-segmentation",
 
377
        /* NETIF_F_FSO */             "tx-fcoe-segmentation",
 
378
        "",
 
379
        "",
 
380
 
 
381
        /* NETIF_F_FCOE_CRC */        "tx-checksum-fcoe-crc",
 
382
        /* NETIF_F_SCTP_CSUM */       "tx-checksum-sctp",
 
383
        /* NETIF_F_FCOE_MTU */        "fcoe-mtu",
 
384
        /* NETIF_F_NTUPLE */          "rx-ntuple-filter",
 
385
        /* NETIF_F_RXHASH */          "rx-hashing",
 
386
        /* NETIF_F_RXCSUM */          "rx-checksum",
 
387
        /* NETIF_F_NOCACHE_COPY */    "tx-nocache-copy",
 
388
        /* NETIF_F_LOOPBACK */        "loopback",
 
389
};
 
390
 
 
391
static int __ethtool_get_sset_count(struct net_device *dev, int sset)
 
392
{
 
393
        const struct ethtool_ops *ops = dev->ethtool_ops;
 
394
 
 
395
        if (sset == ETH_SS_FEATURES)
 
396
                return ARRAY_SIZE(netdev_features_strings);
 
397
 
 
398
        if (ops && ops->get_sset_count && ops->get_strings)
 
399
                return ops->get_sset_count(dev, sset);
 
400
        else
 
401
                return -EOPNOTSUPP;
 
402
}
 
403
 
 
404
static void __ethtool_get_strings(struct net_device *dev,
 
405
        u32 stringset, u8 *data)
 
406
{
 
407
        const struct ethtool_ops *ops = dev->ethtool_ops;
 
408
 
 
409
        if (stringset == ETH_SS_FEATURES)
 
410
                memcpy(data, netdev_features_strings,
 
411
                        sizeof(netdev_features_strings));
 
412
        else
 
413
                /* ops->get_strings is valid because checked earlier */
 
414
                ops->get_strings(dev, stringset, data);
 
415
}
 
416
 
 
417
static u32 ethtool_get_feature_mask(u32 eth_cmd)
 
418
{
 
419
        /* feature masks of legacy discrete ethtool ops */
 
420
 
 
421
        switch (eth_cmd) {
 
422
        case ETHTOOL_GTXCSUM:
 
423
        case ETHTOOL_STXCSUM:
 
424
                return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
 
425
        case ETHTOOL_GRXCSUM:
 
426
        case ETHTOOL_SRXCSUM:
 
427
                return NETIF_F_RXCSUM;
 
428
        case ETHTOOL_GSG:
 
429
        case ETHTOOL_SSG:
 
430
                return NETIF_F_SG;
 
431
        case ETHTOOL_GTSO:
 
432
        case ETHTOOL_STSO:
 
433
                return NETIF_F_ALL_TSO;
 
434
        case ETHTOOL_GUFO:
 
435
        case ETHTOOL_SUFO:
 
436
                return NETIF_F_UFO;
 
437
        case ETHTOOL_GGSO:
 
438
        case ETHTOOL_SGSO:
 
439
                return NETIF_F_GSO;
 
440
        case ETHTOOL_GGRO:
 
441
        case ETHTOOL_SGRO:
 
442
                return NETIF_F_GRO;
 
443
        default:
 
444
                BUG();
 
445
        }
 
446
}
 
447
 
 
448
static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
 
449
{
 
450
        const struct ethtool_ops *ops = dev->ethtool_ops;
 
451
 
 
452
        if (!ops)
 
453
                return NULL;
 
454
 
 
455
        switch (ethcmd) {
 
456
        case ETHTOOL_GTXCSUM:
 
457
                return ops->get_tx_csum;
 
458
        case ETHTOOL_GRXCSUM:
 
459
                return ops->get_rx_csum;
 
460
        case ETHTOOL_SSG:
 
461
                return ops->get_sg;
 
462
        case ETHTOOL_STSO:
 
463
                return ops->get_tso;
 
464
        case ETHTOOL_SUFO:
 
465
                return ops->get_ufo;
 
466
        default:
 
467
                return NULL;
 
468
        }
 
469
}
 
470
 
 
471
static u32 __ethtool_get_rx_csum_oldbug(struct net_device *dev)
 
472
{
 
473
        return !!(dev->features & NETIF_F_ALL_CSUM);
 
474
}
 
475
 
 
476
static int ethtool_get_one_feature(struct net_device *dev,
 
477
        char __user *useraddr, u32 ethcmd)
 
478
{
 
479
        u32 mask = ethtool_get_feature_mask(ethcmd);
 
480
        struct ethtool_value edata = {
 
481
                .cmd = ethcmd,
 
482
                .data = !!(dev->features & mask),
 
483
        };
 
484
 
 
485
        /* compatibility with discrete get_ ops */
 
486
        if (!(dev->hw_features & mask)) {
 
487
                u32 (*actor)(struct net_device *);
 
488
 
 
489
                actor = __ethtool_get_one_feature_actor(dev, ethcmd);
 
490
 
 
491
                /* bug compatibility with old get_rx_csum */
 
492
                if (ethcmd == ETHTOOL_GRXCSUM && !actor)
 
493
                        actor = __ethtool_get_rx_csum_oldbug;
 
494
 
 
495
                if (actor)
 
496
                        edata.data = actor(dev);
 
497
        }
 
498
 
 
499
        if (copy_to_user(useraddr, &edata, sizeof(edata)))
 
500
                return -EFAULT;
 
501
        return 0;
 
502
}
 
503
 
 
504
static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
 
505
static int __ethtool_set_rx_csum(struct net_device *dev, u32 data);
 
506
static int __ethtool_set_sg(struct net_device *dev, u32 data);
 
507
static int __ethtool_set_tso(struct net_device *dev, u32 data);
 
508
static int __ethtool_set_ufo(struct net_device *dev, u32 data);
 
509
 
 
510
static int ethtool_set_one_feature(struct net_device *dev,
 
511
        void __user *useraddr, u32 ethcmd)
 
512
{
 
513
        struct ethtool_value edata;
 
514
        u32 mask;
 
515
 
 
516
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
 
517
                return -EFAULT;
 
518
 
 
519
        mask = ethtool_get_feature_mask(ethcmd);
 
520
        mask &= dev->hw_features;
 
521
        if (mask) {
 
522
                if (edata.data)
 
523
                        dev->wanted_features |= mask;
 
524
                else
 
525
                        dev->wanted_features &= ~mask;
 
526
 
 
527
                __netdev_update_features(dev);
 
528
                return 0;
 
529
        }
 
530
 
 
531
        /* Driver is not converted to ndo_fix_features or does not
 
532
         * support changing this offload. In the latter case it won't
 
533
         * have corresponding ethtool_ops field set.
 
534
         *
 
535
         * Following part is to be removed after all drivers advertise
 
536
         * their changeable features in netdev->hw_features and stop
 
537
         * using discrete offload setting ops.
 
538
         */
 
539
 
 
540
        switch (ethcmd) {
 
541
        case ETHTOOL_STXCSUM:
 
542
                return __ethtool_set_tx_csum(dev, edata.data);
 
543
        case ETHTOOL_SRXCSUM:
 
544
                return __ethtool_set_rx_csum(dev, edata.data);
 
545
        case ETHTOOL_SSG:
 
546
                return __ethtool_set_sg(dev, edata.data);
 
547
        case ETHTOOL_STSO:
 
548
                return __ethtool_set_tso(dev, edata.data);
 
549
        case ETHTOOL_SUFO:
 
550
                return __ethtool_set_ufo(dev, edata.data);
 
551
        default:
 
552
                return -EOPNOTSUPP;
 
553
        }
 
554
}
 
555
 
 
556
int __ethtool_set_flags(struct net_device *dev, u32 data)
 
557
{
 
558
        u32 changed;
 
559
 
 
560
        if (data & ~flags_dup_features)
 
561
                return -EINVAL;
 
562
 
 
563
        /* legacy set_flags() op */
 
564
        if (dev->ethtool_ops->set_flags) {
 
565
                if (unlikely(dev->hw_features & flags_dup_features))
 
566
                        netdev_warn(dev,
 
567
                                "driver BUG: mixed hw_features and set_flags()\n");
 
568
                return dev->ethtool_ops->set_flags(dev, data);
 
569
        }
 
570
 
 
571
        /* allow changing only bits set in hw_features */
 
572
        changed = (data ^ dev->features) & flags_dup_features;
 
573
        if (changed & ~dev->hw_features)
 
574
                return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
 
575
 
 
576
        dev->wanted_features =
 
577
                (dev->wanted_features & ~changed) | (data & dev->hw_features);
 
578
 
 
579
        __netdev_update_features(dev);
 
580
 
 
581
        return 0;
 
582
}
 
583
 
189
584
static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
190
585
{
191
586
        struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
266
661
                                                    void __user *useraddr)
267
662
{
268
663
        struct ethtool_sset_info info;
269
 
        const struct ethtool_ops *ops = dev->ethtool_ops;
270
664
        u64 sset_mask;
271
665
        int i, idx = 0, n_bits = 0, ret, rc;
272
666
        u32 *info_buf = NULL;
273
667
 
274
 
        if (!ops->get_sset_count)
275
 
                return -EOPNOTSUPP;
276
 
 
277
668
        if (copy_from_user(&info, useraddr, sizeof(info)))
278
669
                return -EFAULT;
279
670
 
300
691
                if (!(sset_mask & (1ULL << i)))
301
692
                        continue;
302
693
 
303
 
                rc = ops->get_sset_count(dev, i);
 
694
                rc = __ethtool_get_sset_count(dev, i);
304
695
                if (rc >= 0) {
305
696
                        info.sset_mask |= (1ULL << i);
306
697
                        info_buf[idx++] = rc;
542
933
        struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
543
934
        int ret;
544
935
 
 
936
        if (!ops->set_rx_ntuple)
 
937
                return -EOPNOTSUPP;
 
938
 
545
939
        if (!(dev->features & NETIF_F_NTUPLE))
546
940
                return -EINVAL;
547
941
 
1075
1469
        return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1076
1470
}
1077
1471
 
 
1472
static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
 
1473
                                                   void __user *useraddr)
 
1474
{
 
1475
        struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
 
1476
 
 
1477
        if (!dev->ethtool_ops->get_channels)
 
1478
                return -EOPNOTSUPP;
 
1479
 
 
1480
        dev->ethtool_ops->get_channels(dev, &channels);
 
1481
 
 
1482
        if (copy_to_user(useraddr, &channels, sizeof(channels)))
 
1483
                return -EFAULT;
 
1484
        return 0;
 
1485
}
 
1486
 
 
1487
static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
 
1488
                                                   void __user *useraddr)
 
1489
{
 
1490
        struct ethtool_channels channels;
 
1491
 
 
1492
        if (!dev->ethtool_ops->set_channels)
 
1493
                return -EOPNOTSUPP;
 
1494
 
 
1495
        if (copy_from_user(&channels, useraddr, sizeof(channels)))
 
1496
                return -EFAULT;
 
1497
 
 
1498
        return dev->ethtool_ops->set_channels(dev, &channels);
 
1499
}
 
1500
 
1078
1501
static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1079
1502
{
1080
1503
        struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1106
1529
{
1107
1530
        int err;
1108
1531
 
 
1532
        if (!dev->ethtool_ops->set_sg)
 
1533
                return -EOPNOTSUPP;
 
1534
 
 
1535
        if (data && !(dev->features & NETIF_F_ALL_CSUM))
 
1536
                return -EINVAL;
 
1537
 
1109
1538
        if (!data && dev->ethtool_ops->set_tso) {
1110
1539
                err = dev->ethtool_ops->set_tso(dev, 0);
1111
1540
                if (err)
1120
1549
        return dev->ethtool_ops->set_sg(dev, data);
1121
1550
}
1122
1551
 
1123
 
static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
 
1552
static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
1124
1553
{
1125
 
        struct ethtool_value edata;
1126
1554
        int err;
1127
1555
 
1128
1556
        if (!dev->ethtool_ops->set_tx_csum)
1129
1557
                return -EOPNOTSUPP;
1130
1558
 
1131
 
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
1132
 
                return -EFAULT;
1133
 
 
1134
 
        if (!edata.data && dev->ethtool_ops->set_sg) {
 
1559
        if (!data && dev->ethtool_ops->set_sg) {
1135
1560
                err = __ethtool_set_sg(dev, 0);
1136
1561
                if (err)
1137
1562
                        return err;
1138
1563
        }
1139
1564
 
1140
 
        return dev->ethtool_ops->set_tx_csum(dev, edata.data);
 
1565
        return dev->ethtool_ops->set_tx_csum(dev, data);
1141
1566
}
1142
 
EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1143
1567
 
1144
 
static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
 
1568
static int __ethtool_set_rx_csum(struct net_device *dev, u32 data)
1145
1569
{
1146
 
        struct ethtool_value edata;
1147
 
 
1148
1570
        if (!dev->ethtool_ops->set_rx_csum)
1149
1571
                return -EOPNOTSUPP;
1150
1572
 
1151
 
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
1152
 
                return -EFAULT;
1153
 
 
1154
 
        if (!edata.data && dev->ethtool_ops->set_sg)
 
1573
        if (!data)
1155
1574
                dev->features &= ~NETIF_F_GRO;
1156
1575
 
1157
 
        return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1158
 
}
1159
 
 
1160
 
static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1161
 
{
1162
 
        struct ethtool_value edata;
1163
 
 
1164
 
        if (!dev->ethtool_ops->set_sg)
1165
 
                return -EOPNOTSUPP;
1166
 
 
1167
 
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
1168
 
                return -EFAULT;
1169
 
 
1170
 
        if (edata.data &&
1171
 
            !(dev->features & NETIF_F_ALL_CSUM))
1172
 
                return -EINVAL;
1173
 
 
1174
 
        return __ethtool_set_sg(dev, edata.data);
1175
 
}
1176
 
 
1177
 
static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1178
 
{
1179
 
        struct ethtool_value edata;
1180
 
 
 
1576
        return dev->ethtool_ops->set_rx_csum(dev, data);
 
1577
}
 
1578
 
 
1579
static int __ethtool_set_tso(struct net_device *dev, u32 data)
 
1580
{
1181
1581
        if (!dev->ethtool_ops->set_tso)
1182
1582
                return -EOPNOTSUPP;
1183
1583
 
1184
 
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
1185
 
                return -EFAULT;
1186
 
 
1187
 
        if (edata.data && !(dev->features & NETIF_F_SG))
 
1584
        if (data && !(dev->features & NETIF_F_SG))
1188
1585
                return -EINVAL;
1189
1586
 
1190
 
        return dev->ethtool_ops->set_tso(dev, edata.data);
 
1587
        return dev->ethtool_ops->set_tso(dev, data);
1191
1588
}
1192
1589
 
1193
 
static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
 
1590
static int __ethtool_set_ufo(struct net_device *dev, u32 data)
1194
1591
{
1195
 
        struct ethtool_value edata;
1196
 
 
1197
1592
        if (!dev->ethtool_ops->set_ufo)
1198
1593
                return -EOPNOTSUPP;
1199
 
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
1200
 
                return -EFAULT;
1201
 
        if (edata.data && !(dev->features & NETIF_F_SG))
 
1594
        if (data && !(dev->features & NETIF_F_SG))
1202
1595
                return -EINVAL;
1203
 
        if (edata.data && !((dev->features & NETIF_F_GEN_CSUM) ||
 
1596
        if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
1204
1597
                (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1205
1598
                        == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
1206
1599
                return -EINVAL;
1207
 
        return dev->ethtool_ops->set_ufo(dev, edata.data);
1208
 
}
1209
 
 
1210
 
static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1211
 
{
1212
 
        struct ethtool_value edata = { ETHTOOL_GGSO };
1213
 
 
1214
 
        edata.data = dev->features & NETIF_F_GSO;
1215
 
        if (copy_to_user(useraddr, &edata, sizeof(edata)))
1216
 
                return -EFAULT;
1217
 
        return 0;
1218
 
}
1219
 
 
1220
 
static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1221
 
{
1222
 
        struct ethtool_value edata;
1223
 
 
1224
 
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
1225
 
                return -EFAULT;
1226
 
        if (edata.data)
1227
 
                dev->features |= NETIF_F_GSO;
1228
 
        else
1229
 
                dev->features &= ~NETIF_F_GSO;
1230
 
        return 0;
1231
 
}
1232
 
 
1233
 
static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1234
 
{
1235
 
        struct ethtool_value edata = { ETHTOOL_GGRO };
1236
 
 
1237
 
        edata.data = dev->features & NETIF_F_GRO;
1238
 
        if (copy_to_user(useraddr, &edata, sizeof(edata)))
1239
 
                return -EFAULT;
1240
 
        return 0;
1241
 
}
1242
 
 
1243
 
static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1244
 
{
1245
 
        struct ethtool_value edata;
1246
 
 
1247
 
        if (copy_from_user(&edata, useraddr, sizeof(edata)))
1248
 
                return -EFAULT;
1249
 
 
1250
 
        if (edata.data) {
1251
 
                u32 rxcsum = dev->ethtool_ops->get_rx_csum ?
1252
 
                                dev->ethtool_ops->get_rx_csum(dev) :
1253
 
                                ethtool_op_get_rx_csum(dev);
1254
 
 
1255
 
                if (!rxcsum)
1256
 
                        return -EINVAL;
1257
 
                dev->features |= NETIF_F_GRO;
1258
 
        } else
1259
 
                dev->features &= ~NETIF_F_GRO;
1260
 
 
1261
 
        return 0;
 
1600
        return dev->ethtool_ops->set_ufo(dev, data);
1262
1601
}
1263
1602
 
1264
1603
static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1302
1641
static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1303
1642
{
1304
1643
        struct ethtool_gstrings gstrings;
1305
 
        const struct ethtool_ops *ops = dev->ethtool_ops;
1306
1644
        u8 *data;
1307
1645
        int ret;
1308
1646
 
1309
 
        if (!ops->get_strings || !ops->get_sset_count)
1310
 
                return -EOPNOTSUPP;
1311
 
 
1312
1647
        if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1313
1648
                return -EFAULT;
1314
1649
 
1315
 
        ret = ops->get_sset_count(dev, gstrings.string_set);
 
1650
        ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1316
1651
        if (ret < 0)
1317
1652
                return ret;
1318
1653
 
1322
1657
        if (!data)
1323
1658
                return -ENOMEM;
1324
1659
 
1325
 
        ops->get_strings(dev, gstrings.string_set, data);
 
1660
        __ethtool_get_strings(dev, gstrings.string_set, data);
1326
1661
 
1327
1662
        ret = -EFAULT;
1328
1663
        if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1332
1667
                goto out;
1333
1668
        ret = 0;
1334
1669
 
1335
 
 out:
 
1670
out:
1336
1671
        kfree(data);
1337
1672
        return ret;
1338
1673
}
1340
1675
static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1341
1676
{
1342
1677
        struct ethtool_value id;
 
1678
        static bool busy;
 
1679
        int rc;
1343
1680
 
1344
 
        if (!dev->ethtool_ops->phys_id)
 
1681
        if (!dev->ethtool_ops->set_phys_id)
1345
1682
                return -EOPNOTSUPP;
1346
1683
 
 
1684
        if (busy)
 
1685
                return -EBUSY;
 
1686
 
1347
1687
        if (copy_from_user(&id, useraddr, sizeof(id)))
1348
1688
                return -EFAULT;
1349
1689
 
1350
 
        return dev->ethtool_ops->phys_id(dev, id.data);
 
1690
        rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
 
1691
        if (rc < 0)
 
1692
                return rc;
 
1693
 
 
1694
        /* Drop the RTNL lock while waiting, but prevent reentry or
 
1695
         * removal of the device.
 
1696
         */
 
1697
        busy = true;
 
1698
        dev_hold(dev);
 
1699
        rtnl_unlock();
 
1700
 
 
1701
        if (rc == 0) {
 
1702
                /* Driver will handle this itself */
 
1703
                schedule_timeout_interruptible(
 
1704
                        id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
 
1705
        } else {
 
1706
                /* Driver expects to be called at twice the frequency in rc */
 
1707
                int n = rc * 2, i, interval = HZ / n;
 
1708
 
 
1709
                /* Count down seconds */
 
1710
                do {
 
1711
                        /* Count down iterations per second */
 
1712
                        i = n;
 
1713
                        do {
 
1714
                                rtnl_lock();
 
1715
                                rc = dev->ethtool_ops->set_phys_id(dev,
 
1716
                                    (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
 
1717
                                rtnl_unlock();
 
1718
                                if (rc)
 
1719
                                        break;
 
1720
                                schedule_timeout_interruptible(interval);
 
1721
                        } while (!signal_pending(current) && --i != 0);
 
1722
                } while (!signal_pending(current) &&
 
1723
                         (id.data == 0 || --id.data != 0));
 
1724
        }
 
1725
 
 
1726
        rtnl_lock();
 
1727
        dev_put(dev);
 
1728
        busy = false;
 
1729
 
 
1730
        (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
 
1731
        return rc;
1351
1732
}
1352
1733
 
1353
1734
static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1465
1846
        return dev->ethtool_ops->flash_device(dev, &efl);
1466
1847
}
1467
1848
 
 
1849
static int ethtool_set_dump(struct net_device *dev,
 
1850
                        void __user *useraddr)
 
1851
{
 
1852
        struct ethtool_dump dump;
 
1853
 
 
1854
        if (!dev->ethtool_ops->set_dump)
 
1855
                return -EOPNOTSUPP;
 
1856
 
 
1857
        if (copy_from_user(&dump, useraddr, sizeof(dump)))
 
1858
                return -EFAULT;
 
1859
 
 
1860
        return dev->ethtool_ops->set_dump(dev, &dump);
 
1861
}
 
1862
 
 
1863
static int ethtool_get_dump_flag(struct net_device *dev,
 
1864
                                void __user *useraddr)
 
1865
{
 
1866
        int ret;
 
1867
        struct ethtool_dump dump;
 
1868
        const struct ethtool_ops *ops = dev->ethtool_ops;
 
1869
 
 
1870
        if (!dev->ethtool_ops->get_dump_flag)
 
1871
                return -EOPNOTSUPP;
 
1872
 
 
1873
        if (copy_from_user(&dump, useraddr, sizeof(dump)))
 
1874
                return -EFAULT;
 
1875
 
 
1876
        ret = ops->get_dump_flag(dev, &dump);
 
1877
        if (ret)
 
1878
                return ret;
 
1879
 
 
1880
        if (copy_to_user(useraddr, &dump, sizeof(dump)))
 
1881
                return -EFAULT;
 
1882
        return 0;
 
1883
}
 
1884
 
 
1885
static int ethtool_get_dump_data(struct net_device *dev,
 
1886
                                void __user *useraddr)
 
1887
{
 
1888
        int ret;
 
1889
        __u32 len;
 
1890
        struct ethtool_dump dump, tmp;
 
1891
        const struct ethtool_ops *ops = dev->ethtool_ops;
 
1892
        void *data = NULL;
 
1893
 
 
1894
        if (!dev->ethtool_ops->get_dump_data ||
 
1895
                !dev->ethtool_ops->get_dump_flag)
 
1896
                return -EOPNOTSUPP;
 
1897
 
 
1898
        if (copy_from_user(&dump, useraddr, sizeof(dump)))
 
1899
                return -EFAULT;
 
1900
 
 
1901
        memset(&tmp, 0, sizeof(tmp));
 
1902
        tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
 
1903
        ret = ops->get_dump_flag(dev, &tmp);
 
1904
        if (ret)
 
1905
                return ret;
 
1906
 
 
1907
        len = (tmp.len > dump.len) ? dump.len : tmp.len;
 
1908
        if (!len)
 
1909
                return -EFAULT;
 
1910
 
 
1911
        data = vzalloc(tmp.len);
 
1912
        if (!data)
 
1913
                return -ENOMEM;
 
1914
        ret = ops->get_dump_data(dev, &dump, data);
 
1915
        if (ret)
 
1916
                goto out;
 
1917
 
 
1918
        if (copy_to_user(useraddr, &dump, sizeof(dump))) {
 
1919
                ret = -EFAULT;
 
1920
                goto out;
 
1921
        }
 
1922
        useraddr += offsetof(struct ethtool_dump, data);
 
1923
        if (copy_to_user(useraddr, data, len))
 
1924
                ret = -EFAULT;
 
1925
out:
 
1926
        vfree(data);
 
1927
        return ret;
 
1928
}
 
1929
 
1468
1930
/* The main entry point in this file.  Called from net/core/dev.c */
1469
1931
 
1470
1932
int dev_ethtool(struct net *net, struct ifreq *ifr)
1473
1935
        void __user *useraddr = ifr->ifr_data;
1474
1936
        u32 ethcmd;
1475
1937
        int rc;
1476
 
        unsigned long old_features;
 
1938
        u32 old_features;
1477
1939
 
1478
1940
        if (!dev || !netif_device_present(dev))
1479
1941
                return -ENODEV;
1515
1977
        case ETHTOOL_GRXCLSRLCNT:
1516
1978
        case ETHTOOL_GRXCLSRULE:
1517
1979
        case ETHTOOL_GRXCLSRLALL:
 
1980
        case ETHTOOL_GFEATURES:
1518
1981
                break;
1519
1982
        default:
1520
1983
                if (!capable(CAP_NET_ADMIN))
1585
2048
        case ETHTOOL_SPAUSEPARAM:
1586
2049
                rc = ethtool_set_pauseparam(dev, useraddr);
1587
2050
                break;
1588
 
        case ETHTOOL_GRXCSUM:
1589
 
                rc = ethtool_get_value(dev, useraddr, ethcmd,
1590
 
                                       (dev->ethtool_ops->get_rx_csum ?
1591
 
                                        dev->ethtool_ops->get_rx_csum :
1592
 
                                        ethtool_op_get_rx_csum));
1593
 
                break;
1594
 
        case ETHTOOL_SRXCSUM:
1595
 
                rc = ethtool_set_rx_csum(dev, useraddr);
1596
 
                break;
1597
 
        case ETHTOOL_GTXCSUM:
1598
 
                rc = ethtool_get_value(dev, useraddr, ethcmd,
1599
 
                                       (dev->ethtool_ops->get_tx_csum ?
1600
 
                                        dev->ethtool_ops->get_tx_csum :
1601
 
                                        ethtool_op_get_tx_csum));
1602
 
                break;
1603
 
        case ETHTOOL_STXCSUM:
1604
 
                rc = ethtool_set_tx_csum(dev, useraddr);
1605
 
                break;
1606
 
        case ETHTOOL_GSG:
1607
 
                rc = ethtool_get_value(dev, useraddr, ethcmd,
1608
 
                                       (dev->ethtool_ops->get_sg ?
1609
 
                                        dev->ethtool_ops->get_sg :
1610
 
                                        ethtool_op_get_sg));
1611
 
                break;
1612
 
        case ETHTOOL_SSG:
1613
 
                rc = ethtool_set_sg(dev, useraddr);
1614
 
                break;
1615
 
        case ETHTOOL_GTSO:
1616
 
                rc = ethtool_get_value(dev, useraddr, ethcmd,
1617
 
                                       (dev->ethtool_ops->get_tso ?
1618
 
                                        dev->ethtool_ops->get_tso :
1619
 
                                        ethtool_op_get_tso));
1620
 
                break;
1621
 
        case ETHTOOL_STSO:
1622
 
                rc = ethtool_set_tso(dev, useraddr);
1623
 
                break;
1624
2051
        case ETHTOOL_TEST:
1625
2052
                rc = ethtool_self_test(dev, useraddr);
1626
2053
                break;
1636
2063
        case ETHTOOL_GPERMADDR:
1637
2064
                rc = ethtool_get_perm_addr(dev, useraddr);
1638
2065
                break;
1639
 
        case ETHTOOL_GUFO:
1640
 
                rc = ethtool_get_value(dev, useraddr, ethcmd,
1641
 
                                       (dev->ethtool_ops->get_ufo ?
1642
 
                                        dev->ethtool_ops->get_ufo :
1643
 
                                        ethtool_op_get_ufo));
1644
 
                break;
1645
 
        case ETHTOOL_SUFO:
1646
 
                rc = ethtool_set_ufo(dev, useraddr);
1647
 
                break;
1648
 
        case ETHTOOL_GGSO:
1649
 
                rc = ethtool_get_gso(dev, useraddr);
1650
 
                break;
1651
 
        case ETHTOOL_SGSO:
1652
 
                rc = ethtool_set_gso(dev, useraddr);
1653
 
                break;
1654
2066
        case ETHTOOL_GFLAGS:
1655
2067
                rc = ethtool_get_value(dev, useraddr, ethcmd,
1656
2068
                                       (dev->ethtool_ops->get_flags ?
1658
2070
                                        ethtool_op_get_flags));
1659
2071
                break;
1660
2072
        case ETHTOOL_SFLAGS:
1661
 
                rc = ethtool_set_value(dev, useraddr,
1662
 
                                       dev->ethtool_ops->set_flags);
 
2073
                rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1663
2074
                break;
1664
2075
        case ETHTOOL_GPFLAGS:
1665
2076
                rc = ethtool_get_value(dev, useraddr, ethcmd,
1681
2092
        case ETHTOOL_SRXCLSRLINS:
1682
2093
                rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1683
2094
                break;
1684
 
        case ETHTOOL_GGRO:
1685
 
                rc = ethtool_get_gro(dev, useraddr);
1686
 
                break;
1687
 
        case ETHTOOL_SGRO:
1688
 
                rc = ethtool_set_gro(dev, useraddr);
1689
 
                break;
1690
2095
        case ETHTOOL_FLASHDEV:
1691
2096
                rc = ethtool_flash_device(dev, useraddr);
1692
2097
                break;
1708
2113
        case ETHTOOL_SRXFHINDIR:
1709
2114
                rc = ethtool_set_rxfh_indir(dev, useraddr);
1710
2115
                break;
 
2116
        case ETHTOOL_GFEATURES:
 
2117
                rc = ethtool_get_features(dev, useraddr);
 
2118
                break;
 
2119
        case ETHTOOL_SFEATURES:
 
2120
                rc = ethtool_set_features(dev, useraddr);
 
2121
                break;
 
2122
        case ETHTOOL_GTXCSUM:
 
2123
        case ETHTOOL_GRXCSUM:
 
2124
        case ETHTOOL_GSG:
 
2125
        case ETHTOOL_GTSO:
 
2126
        case ETHTOOL_GUFO:
 
2127
        case ETHTOOL_GGSO:
 
2128
        case ETHTOOL_GGRO:
 
2129
                rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
 
2130
                break;
 
2131
        case ETHTOOL_STXCSUM:
 
2132
        case ETHTOOL_SRXCSUM:
 
2133
        case ETHTOOL_SSG:
 
2134
        case ETHTOOL_STSO:
 
2135
        case ETHTOOL_SUFO:
 
2136
        case ETHTOOL_SGSO:
 
2137
        case ETHTOOL_SGRO:
 
2138
                rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
 
2139
                break;
 
2140
        case ETHTOOL_GCHANNELS:
 
2141
                rc = ethtool_get_channels(dev, useraddr);
 
2142
                break;
 
2143
        case ETHTOOL_SCHANNELS:
 
2144
                rc = ethtool_set_channels(dev, useraddr);
 
2145
                break;
 
2146
        case ETHTOOL_SET_DUMP:
 
2147
                rc = ethtool_set_dump(dev, useraddr);
 
2148
                break;
 
2149
        case ETHTOOL_GET_DUMP_FLAG:
 
2150
                rc = ethtool_get_dump_flag(dev, useraddr);
 
2151
                break;
 
2152
        case ETHTOOL_GET_DUMP_DATA:
 
2153
                rc = ethtool_get_dump_data(dev, useraddr);
 
2154
                break;
1711
2155
        default:
1712
2156
                rc = -EOPNOTSUPP;
1713
2157
        }