1
2
3
4
5#include "iavf.h"
6
7#include <linux/uaccess.h>
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
33struct iavf_stats {
34 char stat_string[ETH_GSTRING_LEN];
35 int sizeof_stat;
36 int stat_offset;
37};
38
39
40
41
42
43#define IAVF_STAT(_type, _name, _stat) { \
44 .stat_string = _name, \
45 .sizeof_stat = sizeof_field(_type, _stat), \
46 .stat_offset = offsetof(_type, _stat) \
47}
48
49
50#define IAVF_QUEUE_STAT(_name, _stat) \
51 IAVF_STAT(struct iavf_ring, _name, _stat)
52
53
54static const struct iavf_stats iavf_gstrings_queue_stats[] = {
55 IAVF_QUEUE_STAT("%s-%u.packets", stats.packets),
56 IAVF_QUEUE_STAT("%s-%u.bytes", stats.bytes),
57};
58
59
60
61
62
63
64
65
66
67
68
69static void
70iavf_add_one_ethtool_stat(u64 *data, void *pointer,
71 const struct iavf_stats *stat)
72{
73 char *p;
74
75 if (!pointer) {
76
77
78
79 *data = 0;
80 return;
81 }
82
83 p = (char *)pointer + stat->stat_offset;
84 switch (stat->sizeof_stat) {
85 case sizeof(u64):
86 *data = *((u64 *)p);
87 break;
88 case sizeof(u32):
89 *data = *((u32 *)p);
90 break;
91 case sizeof(u16):
92 *data = *((u16 *)p);
93 break;
94 case sizeof(u8):
95 *data = *((u8 *)p);
96 break;
97 default:
98 WARN_ONCE(1, "unexpected stat size for %s",
99 stat->stat_string);
100 *data = 0;
101 }
102}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117static void
118__iavf_add_ethtool_stats(u64 **data, void *pointer,
119 const struct iavf_stats stats[],
120 const unsigned int size)
121{
122 unsigned int i;
123
124 for (i = 0; i < size; i++)
125 iavf_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
126}
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141#define iavf_add_ethtool_stats(data, pointer, stats) \
142 __iavf_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158static void
159iavf_add_queue_stats(u64 **data, struct iavf_ring *ring)
160{
161 const unsigned int size = ARRAY_SIZE(iavf_gstrings_queue_stats);
162 const struct iavf_stats *stats = iavf_gstrings_queue_stats;
163 unsigned int start;
164 unsigned int i;
165
166
167
168
169
170
171 do {
172 start = !ring ? 0 : u64_stats_fetch_begin_irq(&ring->syncp);
173 for (i = 0; i < size; i++)
174 iavf_add_one_ethtool_stat(&(*data)[i], ring, &stats[i]);
175 } while (ring && u64_stats_fetch_retry_irq(&ring->syncp, start));
176
177
178 *data += size;
179}
180
181
182
183
184
185
186
187
188
189
190static void __iavf_add_stat_strings(u8 **p, const struct iavf_stats stats[],
191 const unsigned int size, ...)
192{
193 unsigned int i;
194
195 for (i = 0; i < size; i++) {
196 va_list args;
197
198 va_start(args, size);
199 vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
200 *p += ETH_GSTRING_LEN;
201 va_end(args);
202 }
203}
204
205
206
207
208
209
210
211
212
213
214
215
216
217#define iavf_add_stat_strings(p, stats, ...) \
218 __iavf_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
219
220#define VF_STAT(_name, _stat) \
221 IAVF_STAT(struct iavf_adapter, _name, _stat)
222
223static const struct iavf_stats iavf_gstrings_stats[] = {
224 VF_STAT("rx_bytes", current_stats.rx_bytes),
225 VF_STAT("rx_unicast", current_stats.rx_unicast),
226 VF_STAT("rx_multicast", current_stats.rx_multicast),
227 VF_STAT("rx_broadcast", current_stats.rx_broadcast),
228 VF_STAT("rx_discards", current_stats.rx_discards),
229 VF_STAT("rx_unknown_protocol", current_stats.rx_unknown_protocol),
230 VF_STAT("tx_bytes", current_stats.tx_bytes),
231 VF_STAT("tx_unicast", current_stats.tx_unicast),
232 VF_STAT("tx_multicast", current_stats.tx_multicast),
233 VF_STAT("tx_broadcast", current_stats.tx_broadcast),
234 VF_STAT("tx_discards", current_stats.tx_discards),
235 VF_STAT("tx_errors", current_stats.tx_errors),
236};
237
238#define IAVF_STATS_LEN ARRAY_SIZE(iavf_gstrings_stats)
239
240#define IAVF_QUEUE_STATS_LEN ARRAY_SIZE(iavf_gstrings_queue_stats)
241
242
243
244
245
246
247struct iavf_priv_flags {
248 char flag_string[ETH_GSTRING_LEN];
249 u32 flag;
250 bool read_only;
251};
252
253#define IAVF_PRIV_FLAG(_name, _flag, _read_only) { \
254 .flag_string = _name, \
255 .flag = _flag, \
256 .read_only = _read_only, \
257}
258
259static const struct iavf_priv_flags iavf_gstrings_priv_flags[] = {
260 IAVF_PRIV_FLAG("legacy-rx", IAVF_FLAG_LEGACY_RX, 0),
261};
262
263#define IAVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(iavf_gstrings_priv_flags)
264
265
266
267
268
269
270
271
272
273static int iavf_get_link_ksettings(struct net_device *netdev,
274 struct ethtool_link_ksettings *cmd)
275{
276 struct iavf_adapter *adapter = netdev_priv(netdev);
277
278 ethtool_link_ksettings_zero_link_mode(cmd, supported);
279 cmd->base.autoneg = AUTONEG_DISABLE;
280 cmd->base.port = PORT_NONE;
281 cmd->base.duplex = DUPLEX_FULL;
282
283 if (ADV_LINK_SUPPORT(adapter)) {
284 if (adapter->link_speed_mbps &&
285 adapter->link_speed_mbps < U32_MAX)
286 cmd->base.speed = adapter->link_speed_mbps;
287 else
288 cmd->base.speed = SPEED_UNKNOWN;
289
290 return 0;
291 }
292
293 switch (adapter->link_speed) {
294 case VIRTCHNL_LINK_SPEED_40GB:
295 cmd->base.speed = SPEED_40000;
296 break;
297 case VIRTCHNL_LINK_SPEED_25GB:
298 cmd->base.speed = SPEED_25000;
299 break;
300 case VIRTCHNL_LINK_SPEED_20GB:
301 cmd->base.speed = SPEED_20000;
302 break;
303 case VIRTCHNL_LINK_SPEED_10GB:
304 cmd->base.speed = SPEED_10000;
305 break;
306 case VIRTCHNL_LINK_SPEED_5GB:
307 cmd->base.speed = SPEED_5000;
308 break;
309 case VIRTCHNL_LINK_SPEED_2_5GB:
310 cmd->base.speed = SPEED_2500;
311 break;
312 case VIRTCHNL_LINK_SPEED_1GB:
313 cmd->base.speed = SPEED_1000;
314 break;
315 case VIRTCHNL_LINK_SPEED_100MB:
316 cmd->base.speed = SPEED_100;
317 break;
318 default:
319 break;
320 }
321
322 return 0;
323}
324
325
326
327
328
329
330
331
332static int iavf_get_sset_count(struct net_device *netdev, int sset)
333{
334 if (sset == ETH_SS_STATS)
335 return IAVF_STATS_LEN +
336 (IAVF_QUEUE_STATS_LEN * 2 * IAVF_MAX_REQ_QUEUES);
337 else if (sset == ETH_SS_PRIV_FLAGS)
338 return IAVF_PRIV_FLAGS_STR_LEN;
339 else
340 return -EINVAL;
341}
342
343
344
345
346
347
348
349
350
351static void iavf_get_ethtool_stats(struct net_device *netdev,
352 struct ethtool_stats *stats, u64 *data)
353{
354 struct iavf_adapter *adapter = netdev_priv(netdev);
355 unsigned int i;
356
357 iavf_add_ethtool_stats(&data, adapter, iavf_gstrings_stats);
358
359 rcu_read_lock();
360 for (i = 0; i < IAVF_MAX_REQ_QUEUES; i++) {
361 struct iavf_ring *ring;
362
363
364 ring = (i < adapter->num_active_queues ?
365 &adapter->tx_rings[i] : NULL);
366 iavf_add_queue_stats(&data, ring);
367
368
369 ring = (i < adapter->num_active_queues ?
370 &adapter->rx_rings[i] : NULL);
371 iavf_add_queue_stats(&data, ring);
372 }
373 rcu_read_unlock();
374}
375
376
377
378
379
380
381
382
383static void iavf_get_priv_flag_strings(struct net_device *netdev, u8 *data)
384{
385 unsigned int i;
386
387 for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
388 snprintf(data, ETH_GSTRING_LEN, "%s",
389 iavf_gstrings_priv_flags[i].flag_string);
390 data += ETH_GSTRING_LEN;
391 }
392}
393
394
395
396
397
398
399
400
401static void iavf_get_stat_strings(struct net_device *netdev, u8 *data)
402{
403 unsigned int i;
404
405 iavf_add_stat_strings(&data, iavf_gstrings_stats);
406
407
408
409
410 for (i = 0; i < netdev->num_tx_queues; i++) {
411 iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
412 "tx", i);
413 iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
414 "rx", i);
415 }
416}
417
418
419
420
421
422
423
424
425
426static void iavf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
427{
428 switch (sset) {
429 case ETH_SS_STATS:
430 iavf_get_stat_strings(netdev, data);
431 break;
432 case ETH_SS_PRIV_FLAGS:
433 iavf_get_priv_flag_strings(netdev, data);
434 break;
435 default:
436 break;
437 }
438}
439
440
441
442
443
444
445
446
447
448
449
450static u32 iavf_get_priv_flags(struct net_device *netdev)
451{
452 struct iavf_adapter *adapter = netdev_priv(netdev);
453 u32 i, ret_flags = 0;
454
455 for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
456 const struct iavf_priv_flags *priv_flags;
457
458 priv_flags = &iavf_gstrings_priv_flags[i];
459
460 if (priv_flags->flag & adapter->flags)
461 ret_flags |= BIT(i);
462 }
463
464 return ret_flags;
465}
466
467
468
469
470
471
472static int iavf_set_priv_flags(struct net_device *netdev, u32 flags)
473{
474 struct iavf_adapter *adapter = netdev_priv(netdev);
475 u32 orig_flags, new_flags, changed_flags;
476 u32 i;
477
478 orig_flags = READ_ONCE(adapter->flags);
479 new_flags = orig_flags;
480
481 for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
482 const struct iavf_priv_flags *priv_flags;
483
484 priv_flags = &iavf_gstrings_priv_flags[i];
485
486 if (flags & BIT(i))
487 new_flags |= priv_flags->flag;
488 else
489 new_flags &= ~(priv_flags->flag);
490
491 if (priv_flags->read_only &&
492 ((orig_flags ^ new_flags) & ~BIT(i)))
493 return -EOPNOTSUPP;
494 }
495
496
497
498
499
500
501
502
503
504
505
506
507 if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
508 dev_warn(&adapter->pdev->dev,
509 "Unable to update adapter->flags as it was modified by another thread...\n");
510 return -EAGAIN;
511 }
512
513 changed_flags = orig_flags ^ new_flags;
514
515
516
517
518
519
520
521 if (changed_flags & IAVF_FLAG_LEGACY_RX) {
522 if (netif_running(netdev)) {
523 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
524 queue_work(iavf_wq, &adapter->reset_task);
525 }
526 }
527
528 return 0;
529}
530
531
532
533
534
535
536
537static u32 iavf_get_msglevel(struct net_device *netdev)
538{
539 struct iavf_adapter *adapter = netdev_priv(netdev);
540
541 return adapter->msg_enable;
542}
543
544
545
546
547
548
549
550
551
552static void iavf_set_msglevel(struct net_device *netdev, u32 data)
553{
554 struct iavf_adapter *adapter = netdev_priv(netdev);
555
556 if (IAVF_DEBUG_USER & data)
557 adapter->hw.debug_mask = data;
558 adapter->msg_enable = data;
559}
560
561
562
563
564
565
566
567
568static void iavf_get_drvinfo(struct net_device *netdev,
569 struct ethtool_drvinfo *drvinfo)
570{
571 struct iavf_adapter *adapter = netdev_priv(netdev);
572
573 strlcpy(drvinfo->driver, iavf_driver_name, 32);
574 strlcpy(drvinfo->fw_version, "N/A", 4);
575 strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
576 drvinfo->n_priv_flags = IAVF_PRIV_FLAGS_STR_LEN;
577}
578
579
580
581
582
583
584
585
586
587static void iavf_get_ringparam(struct net_device *netdev,
588 struct ethtool_ringparam *ring)
589{
590 struct iavf_adapter *adapter = netdev_priv(netdev);
591
592 ring->rx_max_pending = IAVF_MAX_RXD;
593 ring->tx_max_pending = IAVF_MAX_TXD;
594 ring->rx_pending = adapter->rx_desc_count;
595 ring->tx_pending = adapter->tx_desc_count;
596}
597
598
599
600
601
602
603
604
605
606static int iavf_set_ringparam(struct net_device *netdev,
607 struct ethtool_ringparam *ring)
608{
609 struct iavf_adapter *adapter = netdev_priv(netdev);
610 u32 new_rx_count, new_tx_count;
611
612 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
613 return -EINVAL;
614
615 new_tx_count = clamp_t(u32, ring->tx_pending,
616 IAVF_MIN_TXD,
617 IAVF_MAX_TXD);
618 new_tx_count = ALIGN(new_tx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
619
620 new_rx_count = clamp_t(u32, ring->rx_pending,
621 IAVF_MIN_RXD,
622 IAVF_MAX_RXD);
623 new_rx_count = ALIGN(new_rx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
624
625
626 if ((new_tx_count == adapter->tx_desc_count) &&
627 (new_rx_count == adapter->rx_desc_count))
628 return 0;
629
630 adapter->tx_desc_count = new_tx_count;
631 adapter->rx_desc_count = new_rx_count;
632
633 if (netif_running(netdev)) {
634 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
635 queue_work(iavf_wq, &adapter->reset_task);
636 }
637
638 return 0;
639}
640
641
642
643
644
645
646
647
648
649
650
651static int __iavf_get_coalesce(struct net_device *netdev,
652 struct ethtool_coalesce *ec, int queue)
653{
654 struct iavf_adapter *adapter = netdev_priv(netdev);
655 struct iavf_vsi *vsi = &adapter->vsi;
656 struct iavf_ring *rx_ring, *tx_ring;
657
658 ec->tx_max_coalesced_frames = vsi->work_limit;
659 ec->rx_max_coalesced_frames = vsi->work_limit;
660
661
662
663
664 if (queue < 0)
665 queue = 0;
666 else if (queue >= adapter->num_active_queues)
667 return -EINVAL;
668
669 rx_ring = &adapter->rx_rings[queue];
670 tx_ring = &adapter->tx_rings[queue];
671
672 if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
673 ec->use_adaptive_rx_coalesce = 1;
674
675 if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
676 ec->use_adaptive_tx_coalesce = 1;
677
678 ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
679 ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
680
681 return 0;
682}
683
684
685
686
687
688
689
690
691
692
693
694static int iavf_get_coalesce(struct net_device *netdev,
695 struct ethtool_coalesce *ec)
696{
697 return __iavf_get_coalesce(netdev, ec, -1);
698}
699
700
701
702
703
704
705
706
707
708static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
709 struct ethtool_coalesce *ec)
710{
711 return __iavf_get_coalesce(netdev, ec, queue);
712}
713
714
715
716
717
718
719
720
721
722static void iavf_set_itr_per_queue(struct iavf_adapter *adapter,
723 struct ethtool_coalesce *ec, int queue)
724{
725 struct iavf_ring *rx_ring = &adapter->rx_rings[queue];
726 struct iavf_ring *tx_ring = &adapter->tx_rings[queue];
727 struct iavf_q_vector *q_vector;
728
729 rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
730 tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
731
732 rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
733 if (!ec->use_adaptive_rx_coalesce)
734 rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
735
736 tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
737 if (!ec->use_adaptive_tx_coalesce)
738 tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
739
740 q_vector = rx_ring->q_vector;
741 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
742
743 q_vector = tx_ring->q_vector;
744 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
745
746
747
748
749
750}
751
752
753
754
755
756
757
758
759
760static int __iavf_set_coalesce(struct net_device *netdev,
761 struct ethtool_coalesce *ec, int queue)
762{
763 struct iavf_adapter *adapter = netdev_priv(netdev);
764 struct iavf_vsi *vsi = &adapter->vsi;
765 int i;
766
767 if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
768 vsi->work_limit = ec->tx_max_coalesced_frames_irq;
769
770 if (ec->rx_coalesce_usecs == 0) {
771 if (ec->use_adaptive_rx_coalesce)
772 netif_info(adapter, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
773 } else if ((ec->rx_coalesce_usecs < IAVF_MIN_ITR) ||
774 (ec->rx_coalesce_usecs > IAVF_MAX_ITR)) {
775 netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
776 return -EINVAL;
777 } else if (ec->tx_coalesce_usecs == 0) {
778 if (ec->use_adaptive_tx_coalesce)
779 netif_info(adapter, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
780 } else if ((ec->tx_coalesce_usecs < IAVF_MIN_ITR) ||
781 (ec->tx_coalesce_usecs > IAVF_MAX_ITR)) {
782 netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
783 return -EINVAL;
784 }
785
786
787
788
789 if (queue < 0) {
790 for (i = 0; i < adapter->num_active_queues; i++)
791 iavf_set_itr_per_queue(adapter, ec, i);
792 } else if (queue < adapter->num_active_queues) {
793 iavf_set_itr_per_queue(adapter, ec, queue);
794 } else {
795 netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
796 adapter->num_active_queues - 1);
797 return -EINVAL;
798 }
799
800 return 0;
801}
802
803
804
805
806
807
808
809
810static int iavf_set_coalesce(struct net_device *netdev,
811 struct ethtool_coalesce *ec)
812{
813 return __iavf_set_coalesce(netdev, ec, -1);
814}
815
816
817
818
819
820
821
822
823
824static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
825 struct ethtool_coalesce *ec)
826{
827 return __iavf_set_coalesce(netdev, ec, queue);
828}
829
830
831
832
833
834
835
836
837static int iavf_fltr_to_ethtool_flow(enum iavf_fdir_flow_type flow)
838{
839 switch (flow) {
840 case IAVF_FDIR_FLOW_IPV4_TCP:
841 return TCP_V4_FLOW;
842 case IAVF_FDIR_FLOW_IPV4_UDP:
843 return UDP_V4_FLOW;
844 case IAVF_FDIR_FLOW_IPV4_SCTP:
845 return SCTP_V4_FLOW;
846 case IAVF_FDIR_FLOW_IPV4_AH:
847 return AH_V4_FLOW;
848 case IAVF_FDIR_FLOW_IPV4_ESP:
849 return ESP_V4_FLOW;
850 case IAVF_FDIR_FLOW_IPV4_OTHER:
851 return IPV4_USER_FLOW;
852 case IAVF_FDIR_FLOW_IPV6_TCP:
853 return TCP_V6_FLOW;
854 case IAVF_FDIR_FLOW_IPV6_UDP:
855 return UDP_V6_FLOW;
856 case IAVF_FDIR_FLOW_IPV6_SCTP:
857 return SCTP_V6_FLOW;
858 case IAVF_FDIR_FLOW_IPV6_AH:
859 return AH_V6_FLOW;
860 case IAVF_FDIR_FLOW_IPV6_ESP:
861 return ESP_V6_FLOW;
862 case IAVF_FDIR_FLOW_IPV6_OTHER:
863 return IPV6_USER_FLOW;
864 case IAVF_FDIR_FLOW_NON_IP_L2:
865 return ETHER_FLOW;
866 default:
867
868 return 0;
869 }
870}
871
872
873
874
875
876
877
878static enum iavf_fdir_flow_type iavf_ethtool_flow_to_fltr(int eth)
879{
880 switch (eth) {
881 case TCP_V4_FLOW:
882 return IAVF_FDIR_FLOW_IPV4_TCP;
883 case UDP_V4_FLOW:
884 return IAVF_FDIR_FLOW_IPV4_UDP;
885 case SCTP_V4_FLOW:
886 return IAVF_FDIR_FLOW_IPV4_SCTP;
887 case AH_V4_FLOW:
888 return IAVF_FDIR_FLOW_IPV4_AH;
889 case ESP_V4_FLOW:
890 return IAVF_FDIR_FLOW_IPV4_ESP;
891 case IPV4_USER_FLOW:
892 return IAVF_FDIR_FLOW_IPV4_OTHER;
893 case TCP_V6_FLOW:
894 return IAVF_FDIR_FLOW_IPV6_TCP;
895 case UDP_V6_FLOW:
896 return IAVF_FDIR_FLOW_IPV6_UDP;
897 case SCTP_V6_FLOW:
898 return IAVF_FDIR_FLOW_IPV6_SCTP;
899 case AH_V6_FLOW:
900 return IAVF_FDIR_FLOW_IPV6_AH;
901 case ESP_V6_FLOW:
902 return IAVF_FDIR_FLOW_IPV6_ESP;
903 case IPV6_USER_FLOW:
904 return IAVF_FDIR_FLOW_IPV6_OTHER;
905 case ETHER_FLOW:
906 return IAVF_FDIR_FLOW_NON_IP_L2;
907 default:
908 return IAVF_FDIR_FLOW_NONE;
909 }
910}
911
912
913
914
915
916
917
918
919
920static bool iavf_is_mask_valid(u64 mask, u64 field)
921{
922 return (mask & field) == field;
923}
924
925
926
927
928
929
930
931
932static int
933iavf_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
934 struct iavf_fdir_fltr *fltr)
935{
936 struct iavf_flex_word *flex;
937 int i, cnt = 0;
938
939 if (!(fsp->flow_type & FLOW_EXT))
940 return 0;
941
942 for (i = 0; i < IAVF_FLEX_WORD_NUM; i++) {
943#define IAVF_USERDEF_FLEX_WORD_M GENMASK(15, 0)
944#define IAVF_USERDEF_FLEX_OFFS_S 16
945#define IAVF_USERDEF_FLEX_OFFS_M GENMASK(31, IAVF_USERDEF_FLEX_OFFS_S)
946#define IAVF_USERDEF_FLEX_FLTR_M GENMASK(31, 0)
947 u32 value = be32_to_cpu(fsp->h_ext.data[i]);
948 u32 mask = be32_to_cpu(fsp->m_ext.data[i]);
949
950 if (!value || !mask)
951 continue;
952
953 if (!iavf_is_mask_valid(mask, IAVF_USERDEF_FLEX_FLTR_M))
954 return -EINVAL;
955
956
957
958
959#define IAVF_USERDEF_FLEX_MAX_OFFS_VAL 504
960 flex = &fltr->flex_words[cnt++];
961 flex->word = value & IAVF_USERDEF_FLEX_WORD_M;
962 flex->offset = (value & IAVF_USERDEF_FLEX_OFFS_M) >>
963 IAVF_USERDEF_FLEX_OFFS_S;
964 if (flex->offset > IAVF_USERDEF_FLEX_MAX_OFFS_VAL)
965 return -EINVAL;
966 }
967
968 fltr->flex_cnt = cnt;
969
970 return 0;
971}
972
973
974
975
976
977
978static void
979iavf_fill_rx_flow_ext_data(struct ethtool_rx_flow_spec *fsp,
980 struct iavf_fdir_fltr *fltr)
981{
982 if (!fltr->ext_mask.usr_def[0] && !fltr->ext_mask.usr_def[1])
983 return;
984
985 fsp->flow_type |= FLOW_EXT;
986
987 memcpy(fsp->h_ext.data, fltr->ext_data.usr_def, sizeof(fsp->h_ext.data));
988 memcpy(fsp->m_ext.data, fltr->ext_mask.usr_def, sizeof(fsp->m_ext.data));
989}
990
991
992
993
994
995
996
997
998static int
999iavf_get_ethtool_fdir_entry(struct iavf_adapter *adapter,
1000 struct ethtool_rxnfc *cmd)
1001{
1002 struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1003 struct iavf_fdir_fltr *rule = NULL;
1004 int ret = 0;
1005
1006 if (!FDIR_FLTR_SUPPORT(adapter))
1007 return -EOPNOTSUPP;
1008
1009 spin_lock_bh(&adapter->fdir_fltr_lock);
1010
1011 rule = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1012 if (!rule) {
1013 ret = -EINVAL;
1014 goto release_lock;
1015 }
1016
1017 fsp->flow_type = iavf_fltr_to_ethtool_flow(rule->flow_type);
1018
1019 memset(&fsp->m_u, 0, sizeof(fsp->m_u));
1020 memset(&fsp->m_ext, 0, sizeof(fsp->m_ext));
1021
1022 switch (fsp->flow_type) {
1023 case TCP_V4_FLOW:
1024 case UDP_V4_FLOW:
1025 case SCTP_V4_FLOW:
1026 fsp->h_u.tcp_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1027 fsp->h_u.tcp_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1028 fsp->h_u.tcp_ip4_spec.psrc = rule->ip_data.src_port;
1029 fsp->h_u.tcp_ip4_spec.pdst = rule->ip_data.dst_port;
1030 fsp->h_u.tcp_ip4_spec.tos = rule->ip_data.tos;
1031 fsp->m_u.tcp_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1032 fsp->m_u.tcp_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1033 fsp->m_u.tcp_ip4_spec.psrc = rule->ip_mask.src_port;
1034 fsp->m_u.tcp_ip4_spec.pdst = rule->ip_mask.dst_port;
1035 fsp->m_u.tcp_ip4_spec.tos = rule->ip_mask.tos;
1036 break;
1037 case AH_V4_FLOW:
1038 case ESP_V4_FLOW:
1039 fsp->h_u.ah_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1040 fsp->h_u.ah_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1041 fsp->h_u.ah_ip4_spec.spi = rule->ip_data.spi;
1042 fsp->h_u.ah_ip4_spec.tos = rule->ip_data.tos;
1043 fsp->m_u.ah_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1044 fsp->m_u.ah_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1045 fsp->m_u.ah_ip4_spec.spi = rule->ip_mask.spi;
1046 fsp->m_u.ah_ip4_spec.tos = rule->ip_mask.tos;
1047 break;
1048 case IPV4_USER_FLOW:
1049 fsp->h_u.usr_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1050 fsp->h_u.usr_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1051 fsp->h_u.usr_ip4_spec.l4_4_bytes = rule->ip_data.l4_header;
1052 fsp->h_u.usr_ip4_spec.tos = rule->ip_data.tos;
1053 fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
1054 fsp->h_u.usr_ip4_spec.proto = rule->ip_data.proto;
1055 fsp->m_u.usr_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1056 fsp->m_u.usr_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1057 fsp->m_u.usr_ip4_spec.l4_4_bytes = rule->ip_mask.l4_header;
1058 fsp->m_u.usr_ip4_spec.tos = rule->ip_mask.tos;
1059 fsp->m_u.usr_ip4_spec.ip_ver = 0xFF;
1060 fsp->m_u.usr_ip4_spec.proto = rule->ip_mask.proto;
1061 break;
1062 case TCP_V6_FLOW:
1063 case UDP_V6_FLOW:
1064 case SCTP_V6_FLOW:
1065 memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1066 sizeof(struct in6_addr));
1067 memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1068 sizeof(struct in6_addr));
1069 fsp->h_u.tcp_ip6_spec.psrc = rule->ip_data.src_port;
1070 fsp->h_u.tcp_ip6_spec.pdst = rule->ip_data.dst_port;
1071 fsp->h_u.tcp_ip6_spec.tclass = rule->ip_data.tclass;
1072 memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1073 sizeof(struct in6_addr));
1074 memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1075 sizeof(struct in6_addr));
1076 fsp->m_u.tcp_ip6_spec.psrc = rule->ip_mask.src_port;
1077 fsp->m_u.tcp_ip6_spec.pdst = rule->ip_mask.dst_port;
1078 fsp->m_u.tcp_ip6_spec.tclass = rule->ip_mask.tclass;
1079 break;
1080 case AH_V6_FLOW:
1081 case ESP_V6_FLOW:
1082 memcpy(fsp->h_u.ah_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1083 sizeof(struct in6_addr));
1084 memcpy(fsp->h_u.ah_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1085 sizeof(struct in6_addr));
1086 fsp->h_u.ah_ip6_spec.spi = rule->ip_data.spi;
1087 fsp->h_u.ah_ip6_spec.tclass = rule->ip_data.tclass;
1088 memcpy(fsp->m_u.ah_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1089 sizeof(struct in6_addr));
1090 memcpy(fsp->m_u.ah_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1091 sizeof(struct in6_addr));
1092 fsp->m_u.ah_ip6_spec.spi = rule->ip_mask.spi;
1093 fsp->m_u.ah_ip6_spec.tclass = rule->ip_mask.tclass;
1094 break;
1095 case IPV6_USER_FLOW:
1096 memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1097 sizeof(struct in6_addr));
1098 memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1099 sizeof(struct in6_addr));
1100 fsp->h_u.usr_ip6_spec.l4_4_bytes = rule->ip_data.l4_header;
1101 fsp->h_u.usr_ip6_spec.tclass = rule->ip_data.tclass;
1102 fsp->h_u.usr_ip6_spec.l4_proto = rule->ip_data.proto;
1103 memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1104 sizeof(struct in6_addr));
1105 memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1106 sizeof(struct in6_addr));
1107 fsp->m_u.usr_ip6_spec.l4_4_bytes = rule->ip_mask.l4_header;
1108 fsp->m_u.usr_ip6_spec.tclass = rule->ip_mask.tclass;
1109 fsp->m_u.usr_ip6_spec.l4_proto = rule->ip_mask.proto;
1110 break;
1111 case ETHER_FLOW:
1112 fsp->h_u.ether_spec.h_proto = rule->eth_data.etype;
1113 fsp->m_u.ether_spec.h_proto = rule->eth_mask.etype;
1114 break;
1115 default:
1116 ret = -EINVAL;
1117 break;
1118 }
1119
1120 iavf_fill_rx_flow_ext_data(fsp, rule);
1121
1122 if (rule->action == VIRTCHNL_ACTION_DROP)
1123 fsp->ring_cookie = RX_CLS_FLOW_DISC;
1124 else
1125 fsp->ring_cookie = rule->q_index;
1126
1127release_lock:
1128 spin_unlock_bh(&adapter->fdir_fltr_lock);
1129 return ret;
1130}
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140static int
1141iavf_get_fdir_fltr_ids(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd,
1142 u32 *rule_locs)
1143{
1144 struct iavf_fdir_fltr *fltr;
1145 unsigned int cnt = 0;
1146 int val = 0;
1147
1148 if (!FDIR_FLTR_SUPPORT(adapter))
1149 return -EOPNOTSUPP;
1150
1151 cmd->data = IAVF_MAX_FDIR_FILTERS;
1152
1153 spin_lock_bh(&adapter->fdir_fltr_lock);
1154
1155 list_for_each_entry(fltr, &adapter->fdir_list_head, list) {
1156 if (cnt == cmd->rule_cnt) {
1157 val = -EMSGSIZE;
1158 goto release_lock;
1159 }
1160 rule_locs[cnt] = fltr->loc;
1161 cnt++;
1162 }
1163
1164release_lock:
1165 spin_unlock_bh(&adapter->fdir_fltr_lock);
1166 if (!val)
1167 cmd->rule_cnt = cnt;
1168
1169 return val;
1170}
1171
1172
1173
1174
1175
1176
1177
1178static int
1179iavf_add_fdir_fltr_info(struct iavf_adapter *adapter, struct ethtool_rx_flow_spec *fsp,
1180 struct iavf_fdir_fltr *fltr)
1181{
1182 u32 flow_type, q_index = 0;
1183 enum virtchnl_action act;
1184 int err;
1185
1186 if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
1187 act = VIRTCHNL_ACTION_DROP;
1188 } else {
1189 q_index = fsp->ring_cookie;
1190 if (q_index >= adapter->num_active_queues)
1191 return -EINVAL;
1192
1193 act = VIRTCHNL_ACTION_QUEUE;
1194 }
1195
1196 fltr->action = act;
1197 fltr->loc = fsp->location;
1198 fltr->q_index = q_index;
1199
1200 if (fsp->flow_type & FLOW_EXT) {
1201 memcpy(fltr->ext_data.usr_def, fsp->h_ext.data,
1202 sizeof(fltr->ext_data.usr_def));
1203 memcpy(fltr->ext_mask.usr_def, fsp->m_ext.data,
1204 sizeof(fltr->ext_mask.usr_def));
1205 }
1206
1207 flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
1208 fltr->flow_type = iavf_ethtool_flow_to_fltr(flow_type);
1209
1210 switch (flow_type) {
1211 case TCP_V4_FLOW:
1212 case UDP_V4_FLOW:
1213 case SCTP_V4_FLOW:
1214 fltr->ip_data.v4_addrs.src_ip = fsp->h_u.tcp_ip4_spec.ip4src;
1215 fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
1216 fltr->ip_data.src_port = fsp->h_u.tcp_ip4_spec.psrc;
1217 fltr->ip_data.dst_port = fsp->h_u.tcp_ip4_spec.pdst;
1218 fltr->ip_data.tos = fsp->h_u.tcp_ip4_spec.tos;
1219 fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.tcp_ip4_spec.ip4src;
1220 fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.tcp_ip4_spec.ip4dst;
1221 fltr->ip_mask.src_port = fsp->m_u.tcp_ip4_spec.psrc;
1222 fltr->ip_mask.dst_port = fsp->m_u.tcp_ip4_spec.pdst;
1223 fltr->ip_mask.tos = fsp->m_u.tcp_ip4_spec.tos;
1224 break;
1225 case AH_V4_FLOW:
1226 case ESP_V4_FLOW:
1227 fltr->ip_data.v4_addrs.src_ip = fsp->h_u.ah_ip4_spec.ip4src;
1228 fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.ah_ip4_spec.ip4dst;
1229 fltr->ip_data.spi = fsp->h_u.ah_ip4_spec.spi;
1230 fltr->ip_data.tos = fsp->h_u.ah_ip4_spec.tos;
1231 fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.ah_ip4_spec.ip4src;
1232 fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.ah_ip4_spec.ip4dst;
1233 fltr->ip_mask.spi = fsp->m_u.ah_ip4_spec.spi;
1234 fltr->ip_mask.tos = fsp->m_u.ah_ip4_spec.tos;
1235 break;
1236 case IPV4_USER_FLOW:
1237 fltr->ip_data.v4_addrs.src_ip = fsp->h_u.usr_ip4_spec.ip4src;
1238 fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.usr_ip4_spec.ip4dst;
1239 fltr->ip_data.l4_header = fsp->h_u.usr_ip4_spec.l4_4_bytes;
1240 fltr->ip_data.tos = fsp->h_u.usr_ip4_spec.tos;
1241 fltr->ip_data.proto = fsp->h_u.usr_ip4_spec.proto;
1242 fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.usr_ip4_spec.ip4src;
1243 fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.usr_ip4_spec.ip4dst;
1244 fltr->ip_mask.l4_header = fsp->m_u.usr_ip4_spec.l4_4_bytes;
1245 fltr->ip_mask.tos = fsp->m_u.usr_ip4_spec.tos;
1246 fltr->ip_mask.proto = fsp->m_u.usr_ip4_spec.proto;
1247 break;
1248 case TCP_V6_FLOW:
1249 case UDP_V6_FLOW:
1250 case SCTP_V6_FLOW:
1251 memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1252 sizeof(struct in6_addr));
1253 memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1254 sizeof(struct in6_addr));
1255 fltr->ip_data.src_port = fsp->h_u.tcp_ip6_spec.psrc;
1256 fltr->ip_data.dst_port = fsp->h_u.tcp_ip6_spec.pdst;
1257 fltr->ip_data.tclass = fsp->h_u.tcp_ip6_spec.tclass;
1258 memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1259 sizeof(struct in6_addr));
1260 memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1261 sizeof(struct in6_addr));
1262 fltr->ip_mask.src_port = fsp->m_u.tcp_ip6_spec.psrc;
1263 fltr->ip_mask.dst_port = fsp->m_u.tcp_ip6_spec.pdst;
1264 fltr->ip_mask.tclass = fsp->m_u.tcp_ip6_spec.tclass;
1265 break;
1266 case AH_V6_FLOW:
1267 case ESP_V6_FLOW:
1268 memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.ah_ip6_spec.ip6src,
1269 sizeof(struct in6_addr));
1270 memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.ah_ip6_spec.ip6dst,
1271 sizeof(struct in6_addr));
1272 fltr->ip_data.spi = fsp->h_u.ah_ip6_spec.spi;
1273 fltr->ip_data.tclass = fsp->h_u.ah_ip6_spec.tclass;
1274 memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.ah_ip6_spec.ip6src,
1275 sizeof(struct in6_addr));
1276 memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.ah_ip6_spec.ip6dst,
1277 sizeof(struct in6_addr));
1278 fltr->ip_mask.spi = fsp->m_u.ah_ip6_spec.spi;
1279 fltr->ip_mask.tclass = fsp->m_u.ah_ip6_spec.tclass;
1280 break;
1281 case IPV6_USER_FLOW:
1282 memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1283 sizeof(struct in6_addr));
1284 memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1285 sizeof(struct in6_addr));
1286 fltr->ip_data.l4_header = fsp->h_u.usr_ip6_spec.l4_4_bytes;
1287 fltr->ip_data.tclass = fsp->h_u.usr_ip6_spec.tclass;
1288 fltr->ip_data.proto = fsp->h_u.usr_ip6_spec.l4_proto;
1289 memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1290 sizeof(struct in6_addr));
1291 memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1292 sizeof(struct in6_addr));
1293 fltr->ip_mask.l4_header = fsp->m_u.usr_ip6_spec.l4_4_bytes;
1294 fltr->ip_mask.tclass = fsp->m_u.usr_ip6_spec.tclass;
1295 fltr->ip_mask.proto = fsp->m_u.usr_ip6_spec.l4_proto;
1296 break;
1297 case ETHER_FLOW:
1298 fltr->eth_data.etype = fsp->h_u.ether_spec.h_proto;
1299 fltr->eth_mask.etype = fsp->m_u.ether_spec.h_proto;
1300 break;
1301 default:
1302
1303 return -EINVAL;
1304 }
1305
1306 if (iavf_fdir_is_dup_fltr(adapter, fltr))
1307 return -EEXIST;
1308
1309 err = iavf_parse_rx_flow_user_data(fsp, fltr);
1310 if (err)
1311 return err;
1312
1313 return iavf_fill_fdir_add_msg(adapter, fltr);
1314}
1315
1316
1317
1318
1319
1320
1321
1322
1323static int iavf_add_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1324{
1325 struct ethtool_rx_flow_spec *fsp = &cmd->fs;
1326 struct iavf_fdir_fltr *fltr;
1327 int count = 50;
1328 int err;
1329
1330 if (!FDIR_FLTR_SUPPORT(adapter))
1331 return -EOPNOTSUPP;
1332
1333 if (fsp->flow_type & FLOW_MAC_EXT)
1334 return -EINVAL;
1335
1336 if (adapter->fdir_active_fltr >= IAVF_MAX_FDIR_FILTERS) {
1337 dev_err(&adapter->pdev->dev,
1338 "Unable to add Flow Director filter because VF reached the limit of max allowed filters (%u)\n",
1339 IAVF_MAX_FDIR_FILTERS);
1340 return -ENOSPC;
1341 }
1342
1343 spin_lock_bh(&adapter->fdir_fltr_lock);
1344 if (iavf_find_fdir_fltr_by_loc(adapter, fsp->location)) {
1345 dev_err(&adapter->pdev->dev, "Failed to add Flow Director filter, it already exists\n");
1346 spin_unlock_bh(&adapter->fdir_fltr_lock);
1347 return -EEXIST;
1348 }
1349 spin_unlock_bh(&adapter->fdir_fltr_lock);
1350
1351 fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1352 if (!fltr)
1353 return -ENOMEM;
1354
1355 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
1356 &adapter->crit_section)) {
1357 if (--count == 0) {
1358 kfree(fltr);
1359 return -EINVAL;
1360 }
1361 udelay(1);
1362 }
1363
1364 err = iavf_add_fdir_fltr_info(adapter, fsp, fltr);
1365 if (err)
1366 goto ret;
1367
1368 spin_lock_bh(&adapter->fdir_fltr_lock);
1369 iavf_fdir_list_add_fltr(adapter, fltr);
1370 adapter->fdir_active_fltr++;
1371 fltr->state = IAVF_FDIR_FLTR_ADD_REQUEST;
1372 adapter->aq_required |= IAVF_FLAG_AQ_ADD_FDIR_FILTER;
1373 spin_unlock_bh(&adapter->fdir_fltr_lock);
1374
1375 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1376
1377ret:
1378 if (err && fltr)
1379 kfree(fltr);
1380
1381 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1382 return err;
1383}
1384
1385
1386
1387
1388
1389
1390
1391
1392static int iavf_del_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1393{
1394 struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1395 struct iavf_fdir_fltr *fltr = NULL;
1396 int err = 0;
1397
1398 if (!FDIR_FLTR_SUPPORT(adapter))
1399 return -EOPNOTSUPP;
1400
1401 spin_lock_bh(&adapter->fdir_fltr_lock);
1402 fltr = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1403 if (fltr) {
1404 if (fltr->state == IAVF_FDIR_FLTR_ACTIVE) {
1405 fltr->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1406 adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1407 } else {
1408 err = -EBUSY;
1409 }
1410 } else if (adapter->fdir_active_fltr) {
1411 err = -EINVAL;
1412 }
1413 spin_unlock_bh(&adapter->fdir_fltr_lock);
1414
1415 if (fltr && fltr->state == IAVF_FDIR_FLTR_DEL_REQUEST)
1416 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1417
1418 return err;
1419}
1420
1421
1422
1423
1424
1425
1426
1427
1428static u32 iavf_adv_rss_parse_hdrs(struct ethtool_rxnfc *cmd)
1429{
1430 u32 hdrs = IAVF_ADV_RSS_FLOW_SEG_HDR_NONE;
1431
1432 switch (cmd->flow_type) {
1433 case TCP_V4_FLOW:
1434 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1435 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1436 break;
1437 case UDP_V4_FLOW:
1438 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1439 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1440 break;
1441 case SCTP_V4_FLOW:
1442 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1443 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1444 break;
1445 case TCP_V6_FLOW:
1446 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1447 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1448 break;
1449 case UDP_V6_FLOW:
1450 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1451 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1452 break;
1453 case SCTP_V6_FLOW:
1454 hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1455 IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1456 break;
1457 default:
1458 break;
1459 }
1460
1461 return hdrs;
1462}
1463
1464
1465
1466
1467
1468
1469
1470
1471static u64 iavf_adv_rss_parse_hash_flds(struct ethtool_rxnfc *cmd)
1472{
1473 u64 hfld = IAVF_ADV_RSS_HASH_INVALID;
1474
1475 if (cmd->data & RXH_IP_SRC || cmd->data & RXH_IP_DST) {
1476 switch (cmd->flow_type) {
1477 case TCP_V4_FLOW:
1478 case UDP_V4_FLOW:
1479 case SCTP_V4_FLOW:
1480 if (cmd->data & RXH_IP_SRC)
1481 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_SA;
1482 if (cmd->data & RXH_IP_DST)
1483 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_DA;
1484 break;
1485 case TCP_V6_FLOW:
1486 case UDP_V6_FLOW:
1487 case SCTP_V6_FLOW:
1488 if (cmd->data & RXH_IP_SRC)
1489 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_SA;
1490 if (cmd->data & RXH_IP_DST)
1491 hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_DA;
1492 break;
1493 default:
1494 break;
1495 }
1496 }
1497
1498 if (cmd->data & RXH_L4_B_0_1 || cmd->data & RXH_L4_B_2_3) {
1499 switch (cmd->flow_type) {
1500 case TCP_V4_FLOW:
1501 case TCP_V6_FLOW:
1502 if (cmd->data & RXH_L4_B_0_1)
1503 hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT;
1504 if (cmd->data & RXH_L4_B_2_3)
1505 hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT;
1506 break;
1507 case UDP_V4_FLOW:
1508 case UDP_V6_FLOW:
1509 if (cmd->data & RXH_L4_B_0_1)
1510 hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT;
1511 if (cmd->data & RXH_L4_B_2_3)
1512 hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT;
1513 break;
1514 case SCTP_V4_FLOW:
1515 case SCTP_V6_FLOW:
1516 if (cmd->data & RXH_L4_B_0_1)
1517 hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT;
1518 if (cmd->data & RXH_L4_B_2_3)
1519 hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT;
1520 break;
1521 default:
1522 break;
1523 }
1524 }
1525
1526 return hfld;
1527}
1528
1529
1530
1531
1532
1533
1534
1535
1536static int
1537iavf_set_adv_rss_hash_opt(struct iavf_adapter *adapter,
1538 struct ethtool_rxnfc *cmd)
1539{
1540 struct iavf_adv_rss *rss_old, *rss_new;
1541 bool rss_new_add = false;
1542 int count = 50, err = 0;
1543 u64 hash_flds;
1544 u32 hdrs;
1545
1546 if (!ADV_RSS_SUPPORT(adapter))
1547 return -EOPNOTSUPP;
1548
1549 hdrs = iavf_adv_rss_parse_hdrs(cmd);
1550 if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1551 return -EINVAL;
1552
1553 hash_flds = iavf_adv_rss_parse_hash_flds(cmd);
1554 if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1555 return -EINVAL;
1556
1557 rss_new = kzalloc(sizeof(*rss_new), GFP_KERNEL);
1558 if (!rss_new)
1559 return -ENOMEM;
1560
1561 if (iavf_fill_adv_rss_cfg_msg(&rss_new->cfg_msg, hdrs, hash_flds)) {
1562 kfree(rss_new);
1563 return -EINVAL;
1564 }
1565
1566 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
1567 &adapter->crit_section)) {
1568 if (--count == 0) {
1569 kfree(rss_new);
1570 return -EINVAL;
1571 }
1572
1573 udelay(1);
1574 }
1575
1576 spin_lock_bh(&adapter->adv_rss_lock);
1577 rss_old = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1578 if (rss_old) {
1579 if (rss_old->state != IAVF_ADV_RSS_ACTIVE) {
1580 err = -EBUSY;
1581 } else if (rss_old->hash_flds != hash_flds) {
1582 rss_old->state = IAVF_ADV_RSS_ADD_REQUEST;
1583 rss_old->hash_flds = hash_flds;
1584 memcpy(&rss_old->cfg_msg, &rss_new->cfg_msg,
1585 sizeof(rss_new->cfg_msg));
1586 adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1587 } else {
1588 err = -EEXIST;
1589 }
1590 } else {
1591 rss_new_add = true;
1592 rss_new->state = IAVF_ADV_RSS_ADD_REQUEST;
1593 rss_new->packet_hdrs = hdrs;
1594 rss_new->hash_flds = hash_flds;
1595 list_add_tail(&rss_new->list, &adapter->adv_rss_list_head);
1596 adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1597 }
1598 spin_unlock_bh(&adapter->adv_rss_lock);
1599
1600 if (!err)
1601 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1602
1603 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1604
1605 if (!rss_new_add)
1606 kfree(rss_new);
1607
1608 return err;
1609}
1610
1611
1612
1613
1614
1615
1616
1617
1618static int
1619iavf_get_adv_rss_hash_opt(struct iavf_adapter *adapter,
1620 struct ethtool_rxnfc *cmd)
1621{
1622 struct iavf_adv_rss *rss;
1623 u64 hash_flds;
1624 u32 hdrs;
1625
1626 if (!ADV_RSS_SUPPORT(adapter))
1627 return -EOPNOTSUPP;
1628
1629 cmd->data = 0;
1630
1631 hdrs = iavf_adv_rss_parse_hdrs(cmd);
1632 if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1633 return -EINVAL;
1634
1635 spin_lock_bh(&adapter->adv_rss_lock);
1636 rss = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1637 if (rss)
1638 hash_flds = rss->hash_flds;
1639 else
1640 hash_flds = IAVF_ADV_RSS_HASH_INVALID;
1641 spin_unlock_bh(&adapter->adv_rss_lock);
1642
1643 if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1644 return -EINVAL;
1645
1646 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_SA |
1647 IAVF_ADV_RSS_HASH_FLD_IPV6_SA))
1648 cmd->data |= (u64)RXH_IP_SRC;
1649
1650 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_DA |
1651 IAVF_ADV_RSS_HASH_FLD_IPV6_DA))
1652 cmd->data |= (u64)RXH_IP_DST;
1653
1654 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT |
1655 IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT |
1656 IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT))
1657 cmd->data |= (u64)RXH_L4_B_0_1;
1658
1659 if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT |
1660 IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT |
1661 IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT))
1662 cmd->data |= (u64)RXH_L4_B_2_3;
1663
1664 return 0;
1665}
1666
1667
1668
1669
1670
1671
1672
1673
1674static int iavf_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1675{
1676 struct iavf_adapter *adapter = netdev_priv(netdev);
1677 int ret = -EOPNOTSUPP;
1678
1679 switch (cmd->cmd) {
1680 case ETHTOOL_SRXCLSRLINS:
1681 ret = iavf_add_fdir_ethtool(adapter, cmd);
1682 break;
1683 case ETHTOOL_SRXCLSRLDEL:
1684 ret = iavf_del_fdir_ethtool(adapter, cmd);
1685 break;
1686 case ETHTOOL_SRXFH:
1687 ret = iavf_set_adv_rss_hash_opt(adapter, cmd);
1688 break;
1689 default:
1690 break;
1691 }
1692
1693 return ret;
1694}
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1705 u32 *rule_locs)
1706{
1707 struct iavf_adapter *adapter = netdev_priv(netdev);
1708 int ret = -EOPNOTSUPP;
1709
1710 switch (cmd->cmd) {
1711 case ETHTOOL_GRXRINGS:
1712 cmd->data = adapter->num_active_queues;
1713 ret = 0;
1714 break;
1715 case ETHTOOL_GRXCLSRLCNT:
1716 if (!FDIR_FLTR_SUPPORT(adapter))
1717 break;
1718 cmd->rule_cnt = adapter->fdir_active_fltr;
1719 cmd->data = IAVF_MAX_FDIR_FILTERS;
1720 ret = 0;
1721 break;
1722 case ETHTOOL_GRXCLSRULE:
1723 ret = iavf_get_ethtool_fdir_entry(adapter, cmd);
1724 break;
1725 case ETHTOOL_GRXCLSRLALL:
1726 ret = iavf_get_fdir_fltr_ids(adapter, cmd, (u32 *)rule_locs);
1727 break;
1728 case ETHTOOL_GRXFH:
1729 ret = iavf_get_adv_rss_hash_opt(adapter, cmd);
1730 break;
1731 default:
1732 break;
1733 }
1734
1735 return ret;
1736}
1737
1738
1739
1740
1741
1742
1743
1744
1745static void iavf_get_channels(struct net_device *netdev,
1746 struct ethtool_channels *ch)
1747{
1748 struct iavf_adapter *adapter = netdev_priv(netdev);
1749
1750
1751 ch->max_combined = adapter->vsi_res->num_queue_pairs;
1752
1753 ch->max_other = NONQ_VECS;
1754 ch->other_count = NONQ_VECS;
1755
1756 ch->combined_count = adapter->num_active_queues;
1757}
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768static int iavf_set_channels(struct net_device *netdev,
1769 struct ethtool_channels *ch)
1770{
1771 struct iavf_adapter *adapter = netdev_priv(netdev);
1772 u32 num_req = ch->combined_count;
1773
1774 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1775 adapter->num_tc) {
1776 dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
1777 return -EINVAL;
1778 }
1779
1780
1781
1782
1783 if (num_req > adapter->vsi_res->num_queue_pairs)
1784 return -EINVAL;
1785
1786 if (num_req == adapter->num_active_queues)
1787 return 0;
1788
1789 if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
1790 return -EINVAL;
1791
1792 adapter->num_req_queues = num_req;
1793 adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1794 iavf_schedule_reset(adapter);
1795 return 0;
1796}
1797
1798
1799
1800
1801
1802
1803
1804static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
1805{
1806 struct iavf_adapter *adapter = netdev_priv(netdev);
1807
1808 return adapter->rss_key_size;
1809}
1810
1811
1812
1813
1814
1815
1816
1817static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
1818{
1819 struct iavf_adapter *adapter = netdev_priv(netdev);
1820
1821 return adapter->rss_lut_size;
1822}
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1834 u8 *hfunc)
1835{
1836 struct iavf_adapter *adapter = netdev_priv(netdev);
1837 u16 i;
1838
1839 if (hfunc)
1840 *hfunc = ETH_RSS_HASH_TOP;
1841 if (!indir)
1842 return 0;
1843
1844 memcpy(key, adapter->rss_key, adapter->rss_key_size);
1845
1846
1847 for (i = 0; i < adapter->rss_lut_size; i++)
1848 indir[i] = (u32)adapter->rss_lut[i];
1849
1850 return 0;
1851}
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
1864 const u8 *key, const u8 hfunc)
1865{
1866 struct iavf_adapter *adapter = netdev_priv(netdev);
1867 u16 i;
1868
1869
1870 if (key ||
1871 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1872 return -EOPNOTSUPP;
1873 if (!indir)
1874 return 0;
1875
1876 if (key)
1877 memcpy(adapter->rss_key, key, adapter->rss_key_size);
1878
1879
1880 for (i = 0; i < adapter->rss_lut_size; i++)
1881 adapter->rss_lut[i] = (u8)(indir[i]);
1882
1883 return iavf_config_rss(adapter);
1884}
1885
1886static const struct ethtool_ops iavf_ethtool_ops = {
1887 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1888 ETHTOOL_COALESCE_MAX_FRAMES |
1889 ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
1890 ETHTOOL_COALESCE_USE_ADAPTIVE,
1891 .get_drvinfo = iavf_get_drvinfo,
1892 .get_link = ethtool_op_get_link,
1893 .get_ringparam = iavf_get_ringparam,
1894 .set_ringparam = iavf_set_ringparam,
1895 .get_strings = iavf_get_strings,
1896 .get_ethtool_stats = iavf_get_ethtool_stats,
1897 .get_sset_count = iavf_get_sset_count,
1898 .get_priv_flags = iavf_get_priv_flags,
1899 .set_priv_flags = iavf_set_priv_flags,
1900 .get_msglevel = iavf_get_msglevel,
1901 .set_msglevel = iavf_set_msglevel,
1902 .get_coalesce = iavf_get_coalesce,
1903 .set_coalesce = iavf_set_coalesce,
1904 .get_per_queue_coalesce = iavf_get_per_queue_coalesce,
1905 .set_per_queue_coalesce = iavf_set_per_queue_coalesce,
1906 .set_rxnfc = iavf_set_rxnfc,
1907 .get_rxnfc = iavf_get_rxnfc,
1908 .get_rxfh_indir_size = iavf_get_rxfh_indir_size,
1909 .get_rxfh = iavf_get_rxfh,
1910 .set_rxfh = iavf_set_rxfh,
1911 .get_channels = iavf_get_channels,
1912 .set_channels = iavf_set_channels,
1913 .get_rxfh_key_size = iavf_get_rxfh_key_size,
1914 .get_link_ksettings = iavf_get_link_ksettings,
1915};
1916
1917
1918
1919
1920
1921
1922
1923
1924void iavf_set_ethtool_ops(struct net_device *netdev)
1925{
1926 netdev->ethtool_ops = &iavf_ethtool_ops;
1927}
1928