c319b7f264d20174c7edee5e707984b8cfc54c92
[dpdk.git] / drivers / net / txgbe / txgbe_fdir.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdarg.h>
8 #include <errno.h>
9 #include <sys/queue.h>
10
11
12 #include "txgbe_logs.h"
13 #include "base/txgbe.h"
14 #include "txgbe_ethdev.h"
15
16 #define TXGBE_DEFAULT_FLEXBYTES_OFFSET  12 /*default flexbytes offset in bytes*/
17 #define TXGBE_MAX_FLX_SOURCE_OFF        62
18
19 #define IPV6_ADDR_TO_MASK(ipaddr, ipv6m) do { \
20         uint8_t ipv6_addr[16]; \
21         uint8_t i; \
22         rte_memcpy(ipv6_addr, (ipaddr), sizeof(ipv6_addr));\
23         (ipv6m) = 0; \
24         for (i = 0; i < sizeof(ipv6_addr); i++) { \
25                 if (ipv6_addr[i] == UINT8_MAX) \
26                         (ipv6m) |= 1 << i; \
27                 else if (ipv6_addr[i] != 0) { \
28                         PMD_DRV_LOG(ERR, " invalid IPv6 address mask."); \
29                         return -EINVAL; \
30                 } \
31         } \
32 } while (0)
33
34 #define IPV6_MASK_TO_ADDR(ipv6m, ipaddr) do { \
35         uint8_t ipv6_addr[16]; \
36         uint8_t i; \
37         for (i = 0; i < sizeof(ipv6_addr); i++) { \
38                 if ((ipv6m) & (1 << i)) \
39                         ipv6_addr[i] = UINT8_MAX; \
40                 else \
41                         ipv6_addr[i] = 0; \
42         } \
43         rte_memcpy((ipaddr), ipv6_addr, sizeof(ipv6_addr));\
44 } while (0)
45
46 /**
47  *  Initialize Flow Director control registers
48  *  @hw: pointer to hardware structure
49  *  @fdirctrl: value to write to flow director control register
50  **/
51 static int
52 txgbe_fdir_enable(struct txgbe_hw *hw, uint32_t fdirctrl)
53 {
54         int i;
55
56         PMD_INIT_FUNC_TRACE();
57
58         /* Prime the keys for hashing */
59         wr32(hw, TXGBE_FDIRBKTHKEY, TXGBE_ATR_BUCKET_HASH_KEY);
60         wr32(hw, TXGBE_FDIRSIGHKEY, TXGBE_ATR_SIGNATURE_HASH_KEY);
61
62         /*
63          * Continue setup of fdirctrl register bits:
64          *  Set the maximum length per hash bucket to 0xA filters
65          *  Send interrupt when 64 filters are left
66          */
67         fdirctrl |= TXGBE_FDIRCTL_MAXLEN(0xA) |
68                     TXGBE_FDIRCTL_FULLTHR(4);
69
70         /*
71          * Poll init-done after we write the register.  Estimated times:
72          *      10G: PBALLOC = 11b, timing is 60us
73          *       1G: PBALLOC = 11b, timing is 600us
74          *     100M: PBALLOC = 11b, timing is 6ms
75          *
76          *     Multiple these timings by 4 if under full Rx load
77          *
78          * So we'll poll for TXGBE_FDIR_INIT_DONE_POLL times, sleeping for
79          * 1 msec per poll time.  If we're at line rate and drop to 100M, then
80          * this might not finish in our poll time, but we can live with that
81          * for now.
82          */
83         wr32(hw, TXGBE_FDIRCTL, fdirctrl);
84         txgbe_flush(hw);
85         for (i = 0; i < TXGBE_FDIR_INIT_DONE_POLL; i++) {
86                 if (rd32(hw, TXGBE_FDIRCTL) & TXGBE_FDIRCTL_INITDONE)
87                         break;
88                 msec_delay(1);
89         }
90
91         if (i >= TXGBE_FDIR_INIT_DONE_POLL) {
92                 PMD_INIT_LOG(ERR, "Flow Director poll time exceeded during enabling!");
93                 return -ETIMEDOUT;
94         }
95         return 0;
96 }
97
98 /*
99  * Set appropriate bits in fdirctrl for: variable reporting levels, moving
100  * flexbytes matching field, and drop queue (only for perfect matching mode).
101  */
102 static inline int
103 configure_fdir_flags(const struct rte_fdir_conf *conf,
104                      uint32_t *fdirctrl, uint32_t *flex)
105 {
106         *fdirctrl = 0;
107         *flex = 0;
108
109         switch (conf->pballoc) {
110         case RTE_FDIR_PBALLOC_64K:
111                 /* 8k - 1 signature filters */
112                 *fdirctrl |= TXGBE_FDIRCTL_BUF_64K;
113                 break;
114         case RTE_FDIR_PBALLOC_128K:
115                 /* 16k - 1 signature filters */
116                 *fdirctrl |= TXGBE_FDIRCTL_BUF_128K;
117                 break;
118         case RTE_FDIR_PBALLOC_256K:
119                 /* 32k - 1 signature filters */
120                 *fdirctrl |= TXGBE_FDIRCTL_BUF_256K;
121                 break;
122         default:
123                 /* bad value */
124                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->pballoc value");
125                 return -EINVAL;
126         };
127
128         /* status flags: write hash & swindex in the rx descriptor */
129         switch (conf->status) {
130         case RTE_FDIR_NO_REPORT_STATUS:
131                 /* do nothing, default mode */
132                 break;
133         case RTE_FDIR_REPORT_STATUS:
134                 /* report status when the packet matches a fdir rule */
135                 *fdirctrl |= TXGBE_FDIRCTL_REPORT_MATCH;
136                 break;
137         case RTE_FDIR_REPORT_STATUS_ALWAYS:
138                 /* always report status */
139                 *fdirctrl |= TXGBE_FDIRCTL_REPORT_ALWAYS;
140                 break;
141         default:
142                 /* bad value */
143                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->status value");
144                 return -EINVAL;
145         };
146
147         *flex |= TXGBE_FDIRFLEXCFG_BASE_MAC;
148         *flex |= TXGBE_FDIRFLEXCFG_OFST(TXGBE_DEFAULT_FLEXBYTES_OFFSET / 2);
149
150         switch (conf->mode) {
151         case RTE_FDIR_MODE_SIGNATURE:
152                 break;
153         case RTE_FDIR_MODE_PERFECT:
154                 *fdirctrl |= TXGBE_FDIRCTL_PERFECT;
155                 *fdirctrl |= TXGBE_FDIRCTL_DROPQP(conf->drop_queue);
156                 break;
157         default:
158                 /* bad value */
159                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->mode value");
160                 return -EINVAL;
161         }
162
163         return 0;
164 }
165
166 static inline uint32_t
167 reverse_fdir_bmks(uint16_t hi_dword, uint16_t lo_dword)
168 {
169         uint32_t mask = hi_dword << 16;
170
171         mask |= lo_dword;
172         mask = ((mask & 0x55555555) << 1) | ((mask & 0xAAAAAAAA) >> 1);
173         mask = ((mask & 0x33333333) << 2) | ((mask & 0xCCCCCCCC) >> 2);
174         mask = ((mask & 0x0F0F0F0F) << 4) | ((mask & 0xF0F0F0F0) >> 4);
175         return ((mask & 0x00FF00FF) << 8) | ((mask & 0xFF00FF00) >> 8);
176 }
177
178 int
179 txgbe_fdir_set_input_mask(struct rte_eth_dev *dev)
180 {
181         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
182         struct txgbe_hw_fdir_info *info = TXGBE_DEV_FDIR(dev);
183         enum rte_fdir_mode mode = dev->data->dev_conf.fdir_conf.mode;
184         /*
185          * mask VM pool and DIPv6 since there are currently not supported
186          * mask FLEX byte, it will be set in flex_conf
187          */
188         uint32_t fdirm = TXGBE_FDIRMSK_POOL;
189         uint32_t fdirtcpm;  /* TCP source and destination port masks. */
190         uint32_t fdiripv6m; /* IPv6 source and destination masks. */
191
192         PMD_INIT_FUNC_TRACE();
193
194         if (mode != RTE_FDIR_MODE_SIGNATURE &&
195             mode != RTE_FDIR_MODE_PERFECT) {
196                 PMD_DRV_LOG(ERR, "Not supported fdir mode - %d!", mode);
197                 return -ENOTSUP;
198         }
199
200         /*
201          * Program the relevant mask registers.  If src/dst_port or src/dst_addr
202          * are zero, then assume a full mask for that field. Also assume that
203          * a VLAN of 0 is unspecified, so mask that out as well.  L4type
204          * cannot be masked out in this implementation.
205          */
206         if (info->mask.dst_port_mask == 0 && info->mask.src_port_mask == 0) {
207                 /* use the L4 protocol mask for raw IPv4/IPv6 traffic */
208                 fdirm |= TXGBE_FDIRMSK_L4P;
209         }
210
211         /* TBD: don't support encapsulation yet */
212         wr32(hw, TXGBE_FDIRMSK, fdirm);
213
214         /* store the TCP/UDP port masks, bit reversed from port layout */
215         fdirtcpm = reverse_fdir_bmks(rte_be_to_cpu_16(info->mask.dst_port_mask),
216                         rte_be_to_cpu_16(info->mask.src_port_mask));
217
218         /* write all the same so that UDP, TCP and SCTP use the same mask
219          * (little-endian)
220          */
221         wr32(hw, TXGBE_FDIRTCPMSK, ~fdirtcpm);
222         wr32(hw, TXGBE_FDIRUDPMSK, ~fdirtcpm);
223         wr32(hw, TXGBE_FDIRSCTPMSK, ~fdirtcpm);
224
225         /* Store source and destination IPv4 masks (big-endian) */
226         wr32(hw, TXGBE_FDIRSIP4MSK, ~info->mask.src_ipv4_mask);
227         wr32(hw, TXGBE_FDIRDIP4MSK, ~info->mask.dst_ipv4_mask);
228
229         if (mode == RTE_FDIR_MODE_SIGNATURE) {
230                 /*
231                  * Store source and destination IPv6 masks (bit reversed)
232                  */
233                 fdiripv6m = TXGBE_FDIRIP6MSK_DST(info->mask.dst_ipv6_mask) |
234                             TXGBE_FDIRIP6MSK_SRC(info->mask.src_ipv6_mask);
235
236                 wr32(hw, TXGBE_FDIRIP6MSK, ~fdiripv6m);
237         }
238
239         return 0;
240 }
241
242 static int
243 txgbe_fdir_store_input_mask(struct rte_eth_dev *dev)
244 {
245         struct rte_eth_fdir_masks *input_mask =
246                                 &dev->data->dev_conf.fdir_conf.mask;
247         enum rte_fdir_mode mode = dev->data->dev_conf.fdir_conf.mode;
248         struct txgbe_hw_fdir_info *info = TXGBE_DEV_FDIR(dev);
249         uint16_t dst_ipv6m = 0;
250         uint16_t src_ipv6m = 0;
251
252         if (mode != RTE_FDIR_MODE_SIGNATURE &&
253             mode != RTE_FDIR_MODE_PERFECT) {
254                 PMD_DRV_LOG(ERR, "Not supported fdir mode - %d!", mode);
255                 return -ENOTSUP;
256         }
257
258         memset(&info->mask, 0, sizeof(struct txgbe_hw_fdir_mask));
259         info->mask.vlan_tci_mask = input_mask->vlan_tci_mask;
260         info->mask.src_port_mask = input_mask->src_port_mask;
261         info->mask.dst_port_mask = input_mask->dst_port_mask;
262         info->mask.src_ipv4_mask = input_mask->ipv4_mask.src_ip;
263         info->mask.dst_ipv4_mask = input_mask->ipv4_mask.dst_ip;
264         IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.src_ip, src_ipv6m);
265         IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.dst_ip, dst_ipv6m);
266         info->mask.src_ipv6_mask = src_ipv6m;
267         info->mask.dst_ipv6_mask = dst_ipv6m;
268
269         return 0;
270 }
271
272 int
273 txgbe_fdir_set_flexbytes_offset(struct rte_eth_dev *dev,
274                                 uint16_t offset)
275 {
276         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
277         int i;
278
279         for (i = 0; i < 64; i++) {
280                 uint32_t flexreg, flex;
281                 flexreg = rd32(hw, TXGBE_FDIRFLEXCFG(i / 4));
282                 flex = TXGBE_FDIRFLEXCFG_BASE_MAC;
283                 flex |= TXGBE_FDIRFLEXCFG_OFST(offset / 2);
284                 flexreg &= ~(TXGBE_FDIRFLEXCFG_ALL(~0UL, i % 4));
285                 flexreg |= TXGBE_FDIRFLEXCFG_ALL(flex, i % 4);
286                 wr32(hw, TXGBE_FDIRFLEXCFG(i / 4), flexreg);
287         }
288
289         txgbe_flush(hw);
290         for (i = 0; i < TXGBE_FDIR_INIT_DONE_POLL; i++) {
291                 if (rd32(hw, TXGBE_FDIRCTL) &
292                         TXGBE_FDIRCTL_INITDONE)
293                         break;
294                 msec_delay(1);
295         }
296         return 0;
297 }
298
299 /*
300  * txgbe_check_fdir_flex_conf -check if the flex payload and mask configuration
301  * arguments are valid
302  */
303 static int
304 txgbe_set_fdir_flex_conf(struct rte_eth_dev *dev, uint32_t flex)
305 {
306         const struct rte_eth_fdir_flex_conf *conf =
307                                 &dev->data->dev_conf.fdir_conf.flex_conf;
308         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
309         struct txgbe_hw_fdir_info *info = TXGBE_DEV_FDIR(dev);
310         const struct rte_eth_flex_payload_cfg *flex_cfg;
311         const struct rte_eth_fdir_flex_mask *flex_mask;
312         uint16_t flexbytes = 0;
313         uint16_t i;
314
315         if (conf == NULL) {
316                 PMD_DRV_LOG(ERR, "NULL pointer.");
317                 return -EINVAL;
318         }
319
320         flex |= TXGBE_FDIRFLEXCFG_DIA;
321
322         for (i = 0; i < conf->nb_payloads; i++) {
323                 flex_cfg = &conf->flex_set[i];
324                 if (flex_cfg->type != RTE_ETH_RAW_PAYLOAD) {
325                         PMD_DRV_LOG(ERR, "unsupported payload type.");
326                         return -EINVAL;
327                 }
328                 if (((flex_cfg->src_offset[0] & 0x1) == 0) &&
329                     (flex_cfg->src_offset[1] == flex_cfg->src_offset[0] + 1) &&
330                      flex_cfg->src_offset[0] <= TXGBE_MAX_FLX_SOURCE_OFF) {
331                         flex &= ~TXGBE_FDIRFLEXCFG_OFST_MASK;
332                         flex |=
333                             TXGBE_FDIRFLEXCFG_OFST(flex_cfg->src_offset[0] / 2);
334                 } else {
335                         PMD_DRV_LOG(ERR, "invalid flexbytes arguments.");
336                         return -EINVAL;
337                 }
338         }
339
340         for (i = 0; i < conf->nb_flexmasks; i++) {
341                 flex_mask = &conf->flex_mask[i];
342                 if (flex_mask->flow_type != RTE_ETH_FLOW_UNKNOWN) {
343                         PMD_DRV_LOG(ERR, "flexmask should be set globally.");
344                         return -EINVAL;
345                 }
346                 flexbytes = (uint16_t)(((flex_mask->mask[1] << 8) & 0xFF00) |
347                                         ((flex_mask->mask[0]) & 0xFF));
348                 if (flexbytes == UINT16_MAX) {
349                         flex &= ~TXGBE_FDIRFLEXCFG_DIA;
350                 } else if (flexbytes != 0) {
351                      /* TXGBE_FDIRFLEXCFG_DIA is set by default when set mask */
352                         PMD_DRV_LOG(ERR, " invalid flexbytes mask arguments.");
353                         return -EINVAL;
354                 }
355         }
356
357         info->mask.flex_bytes_mask = flexbytes ? UINT16_MAX : 0;
358         info->flex_bytes_offset = (uint8_t)(TXGBD_FDIRFLEXCFG_OFST(flex) * 2);
359
360         for (i = 0; i < 64; i++) {
361                 uint32_t flexreg;
362                 flexreg = rd32(hw, TXGBE_FDIRFLEXCFG(i / 4));
363                 flexreg &= ~(TXGBE_FDIRFLEXCFG_ALL(~0UL, i % 4));
364                 flexreg |= TXGBE_FDIRFLEXCFG_ALL(flex, i % 4);
365                 wr32(hw, TXGBE_FDIRFLEXCFG(i / 4), flexreg);
366         }
367         return 0;
368 }
369
370 int
371 txgbe_fdir_configure(struct rte_eth_dev *dev)
372 {
373         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
374         int err;
375         uint32_t fdirctrl, flex, pbsize;
376         int i;
377         enum rte_fdir_mode mode = dev->data->dev_conf.fdir_conf.mode;
378
379         PMD_INIT_FUNC_TRACE();
380
381         /* supports mac-vlan and tunnel mode */
382         if (mode != RTE_FDIR_MODE_SIGNATURE &&
383             mode != RTE_FDIR_MODE_PERFECT)
384                 return -ENOSYS;
385
386         err = configure_fdir_flags(&dev->data->dev_conf.fdir_conf,
387                                    &fdirctrl, &flex);
388         if (err)
389                 return err;
390
391         /*
392          * Before enabling Flow Director, the Rx Packet Buffer size
393          * must be reduced.  The new value is the current size minus
394          * flow director memory usage size.
395          */
396         pbsize = rd32(hw, TXGBE_PBRXSIZE(0));
397         pbsize -= TXGBD_FDIRCTL_BUF_BYTE(fdirctrl);
398         wr32(hw, TXGBE_PBRXSIZE(0), pbsize);
399
400         /*
401          * The defaults in the HW for RX PB 1-7 are not zero and so should be
402          * initialized to zero for non DCB mode otherwise actual total RX PB
403          * would be bigger than programmed and filter space would run into
404          * the PB 0 region.
405          */
406         for (i = 1; i < 8; i++)
407                 wr32(hw, TXGBE_PBRXSIZE(i), 0);
408
409         err = txgbe_fdir_store_input_mask(dev);
410         if (err < 0) {
411                 PMD_INIT_LOG(ERR, " Error on setting FD mask");
412                 return err;
413         }
414
415         err = txgbe_fdir_set_input_mask(dev);
416         if (err < 0) {
417                 PMD_INIT_LOG(ERR, " Error on setting FD mask");
418                 return err;
419         }
420
421         err = txgbe_set_fdir_flex_conf(dev, flex);
422         if (err < 0) {
423                 PMD_INIT_LOG(ERR, " Error on setting FD flexible arguments.");
424                 return err;
425         }
426
427         err = txgbe_fdir_enable(hw, fdirctrl);
428         if (err < 0) {
429                 PMD_INIT_LOG(ERR, " Error on enabling FD.");
430                 return err;
431         }
432         return 0;
433 }
434