6c0a5301a83dc37c0bd1721860d1bffe3c119605
[dpdk.git] / lib / librte_pmd_ixgbe / ixgbe_fdir.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdarg.h>
37 #include <errno.h>
38 #include <sys/queue.h>
39
40 #include <rte_interrupts.h>
41 #include <rte_log.h>
42 #include <rte_debug.h>
43 #include <rte_pci.h>
44 #include <rte_ether.h>
45 #include <rte_ethdev.h>
46
47 #include "ixgbe_logs.h"
48 #include "ixgbe/ixgbe_api.h"
49 #include "ixgbe/ixgbe_common.h"
50 #include "ixgbe_ethdev.h"
51
52 /* To get PBALLOC (Packet Buffer Allocation) bits from FDIRCTRL value */
53 #define FDIRCTRL_PBALLOC_MASK           0x03
54
55 /* For calculating memory required for FDIR filters */
56 #define PBALLOC_SIZE_SHIFT              15
57
58 /* Number of bits used to mask bucket hash for different pballoc sizes */
59 #define PERFECT_BUCKET_64KB_HASH_MASK   0x07FF  /* 11 bits */
60 #define PERFECT_BUCKET_128KB_HASH_MASK  0x0FFF  /* 12 bits */
61 #define PERFECT_BUCKET_256KB_HASH_MASK  0x1FFF  /* 13 bits */
62 #define SIG_BUCKET_64KB_HASH_MASK       0x1FFF  /* 13 bits */
63 #define SIG_BUCKET_128KB_HASH_MASK      0x3FFF  /* 14 bits */
64 #define SIG_BUCKET_256KB_HASH_MASK      0x7FFF  /* 15 bits */
65
66 /**
67  * This function is based on ixgbe_fdir_enable_82599() in ixgbe/ixgbe_82599.c.
68  * It adds extra configuration of fdirctrl that is common for all filter types.
69  *
70  *  Initialize Flow Director control registers
71  *  @hw: pointer to hardware structure
72  *  @fdirctrl: value to write to flow director control register
73  **/
74 static void fdir_enable_82599(struct ixgbe_hw *hw, u32 fdirctrl)
75 {
76         int i;
77
78         PMD_INIT_FUNC_TRACE();
79
80         /* Prime the keys for hashing */
81         IXGBE_WRITE_REG(hw, IXGBE_FDIRHKEY, IXGBE_ATR_BUCKET_HASH_KEY);
82         IXGBE_WRITE_REG(hw, IXGBE_FDIRSKEY, IXGBE_ATR_SIGNATURE_HASH_KEY);
83
84         /*
85          * Continue setup of fdirctrl register bits:
86          *  Set the maximum length per hash bucket to 0xA filters
87          *  Send interrupt when 64 filters are left
88          */
89         fdirctrl |= (0xA << IXGBE_FDIRCTRL_MAX_LENGTH_SHIFT) |
90                     (4 << IXGBE_FDIRCTRL_FULL_THRESH_SHIFT);
91
92         /*
93          * Poll init-done after we write the register.  Estimated times:
94          *      10G: PBALLOC = 11b, timing is 60us
95          *       1G: PBALLOC = 11b, timing is 600us
96          *     100M: PBALLOC = 11b, timing is 6ms
97          *
98          *     Multiple these timings by 4 if under full Rx load
99          *
100          * So we'll poll for IXGBE_FDIR_INIT_DONE_POLL times, sleeping for
101          * 1 msec per poll time.  If we're at line rate and drop to 100M, then
102          * this might not finish in our poll time, but we can live with that
103          * for now.
104          */
105         IXGBE_WRITE_REG(hw, IXGBE_FDIRCTRL, fdirctrl);
106         IXGBE_WRITE_FLUSH(hw);
107         for (i = 0; i < IXGBE_FDIR_INIT_DONE_POLL; i++) {
108                 if (IXGBE_READ_REG(hw, IXGBE_FDIRCTRL) &
109                                    IXGBE_FDIRCTRL_INIT_DONE)
110                         break;
111                 msec_delay(1);
112         }
113
114         if (i >= IXGBE_FDIR_INIT_DONE_POLL)
115                 PMD_INIT_LOG(WARNING, "Flow Director poll time exceeded!\n");
116 }
117
118 /*
119  * Set appropriate bits in fdirctrl for: variable reporting levels, moving
120  * flexbytes matching field, and drop queue (only for perfect matching mode).
121  */
122 static int
123 configure_fdir_flags(struct rte_fdir_conf *conf, uint32_t *fdirctrl)
124 {
125         *fdirctrl = 0;
126
127         switch (conf->pballoc) {
128         case RTE_FDIR_PBALLOC_64K:
129                 /* 8k - 1 signature filters */
130                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_64K;
131                 break;
132         case RTE_FDIR_PBALLOC_128K:
133                 /* 16k - 1 signature filters */
134                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_128K;
135                 break;
136         case RTE_FDIR_PBALLOC_256K:
137                 /* 32k - 1 signature filters */
138                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_256K;
139                 break;
140         default:
141                 /* bad value */
142                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->pballoc value");
143                 return -EINVAL;
144         };
145
146         /* status flags: write hash & swindex in the rx descriptor */
147         switch (conf->status) {
148         case RTE_FDIR_NO_REPORT_STATUS:
149                 /* do nothing, default mode */
150                 break;
151         case RTE_FDIR_REPORT_STATUS:
152                 /* report status when the packet matches a fdir rule */
153                 *fdirctrl |= IXGBE_FDIRCTRL_REPORT_STATUS;
154                 break;
155         case RTE_FDIR_REPORT_STATUS_ALWAYS:
156                 /* always report status */
157                 *fdirctrl |= IXGBE_FDIRCTRL_REPORT_STATUS_ALWAYS;
158                 break;
159         default:
160                 /* bad value */
161                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->status value");
162                 return -EINVAL;
163         };
164
165         *fdirctrl |= (conf->flexbytes_offset << IXGBE_FDIRCTRL_FLEX_SHIFT);
166
167         if (conf->mode == RTE_FDIR_MODE_PERFECT) {
168                 *fdirctrl |= IXGBE_FDIRCTRL_PERFECT_MATCH;
169                 *fdirctrl |= (conf->drop_queue << IXGBE_FDIRCTRL_DROP_Q_SHIFT);
170         }
171
172         return 0;
173 }
174
175 int
176 ixgbe_fdir_configure(struct rte_eth_dev *dev)
177 {
178         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
179         int err;
180         uint32_t fdirctrl, pbsize;
181         int i;
182
183         PMD_INIT_FUNC_TRACE();
184
185         if (hw->mac.type != ixgbe_mac_82599EB && hw->mac.type !=ixgbe_mac_X540)
186                 return -ENOSYS;
187
188         err = configure_fdir_flags(&dev->data->dev_conf.fdir_conf, &fdirctrl);
189         if (err)
190                 return err;
191
192         /*
193          * Before enabling Flow Director, the Rx Packet Buffer size
194          * must be reduced.  The new value is the current size minus
195          * flow director memory usage size.
196          */
197         pbsize = (1 << (PBALLOC_SIZE_SHIFT + (fdirctrl & FDIRCTRL_PBALLOC_MASK)));
198         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0),
199             (IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) - pbsize));
200
201         /*
202          * The defaults in the HW for RX PB 1-7 are not zero and so should be
203          * intialized to zero for non DCB mode otherwise actual total RX PB
204          * would be bigger than programmed and filter space would run into
205          * the PB 0 region.
206          */
207         for (i = 1; i < 8; i++)
208                 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
209
210         fdir_enable_82599(hw, fdirctrl);
211         return 0;
212 }
213
214 /*
215  * The below function is taken from the FreeBSD IXGBE drivers release
216  * 2.3.8. The only change is not to mask hash_result with IXGBE_ATR_HASH_MASK
217  * before returning, as the signature hash can use 16bits.
218  *
219  * The newer driver has optimised functions for calculating bucket and
220  * signature hashes. However they don't support IPv6 type packets for signature
221  * filters so are not used here.
222  *
223  * Note that the bkt_hash field in the ixgbe_atr_input structure is also never
224  * set.
225  *
226  * Compute the hashes for SW ATR
227  *  @stream: input bitstream to compute the hash on
228  *  @key: 32-bit hash key
229  **/
230 static u32
231 ixgbe_atr_compute_hash_82599(union ixgbe_atr_input *atr_input,
232                                  u32 key)
233 {
234         /*
235          * The algorithm is as follows:
236          *    Hash[15:0] = Sum { S[n] x K[n+16] }, n = 0...350
237          *    where Sum {A[n]}, n = 0...n is bitwise XOR of A[0], A[1]...A[n]
238          *    and A[n] x B[n] is bitwise AND between same length strings
239          *
240          *    K[n] is 16 bits, defined as:
241          *       for n modulo 32 >= 15, K[n] = K[n % 32 : (n % 32) - 15]
242          *       for n modulo 32 < 15, K[n] =
243          *             K[(n % 32:0) | (31:31 - (14 - (n % 32)))]
244          *
245          *    S[n] is 16 bits, defined as:
246          *       for n >= 15, S[n] = S[n:n - 15]
247          *       for n < 15, S[n] = S[(n:0) | (350:350 - (14 - n))]
248          *
249          *    To simplify for programming, the algorithm is implemented
250          *    in software this way:
251          *
252          *    key[31:0], hi_hash_dword[31:0], lo_hash_dword[31:0], hash[15:0]
253          *
254          *    for (i = 0; i < 352; i+=32)
255          *        hi_hash_dword[31:0] ^= Stream[(i+31):i];
256          *
257          *    lo_hash_dword[15:0]  ^= Stream[15:0];
258          *    lo_hash_dword[15:0]  ^= hi_hash_dword[31:16];
259          *    lo_hash_dword[31:16] ^= hi_hash_dword[15:0];
260          *
261          *    hi_hash_dword[31:0]  ^= Stream[351:320];
262          *
263          *    if(key[0])
264          *        hash[15:0] ^= Stream[15:0];
265          *
266          *    for (i = 0; i < 16; i++) {
267          *        if (key[i])
268          *            hash[15:0] ^= lo_hash_dword[(i+15):i];
269          *        if (key[i + 16])
270          *            hash[15:0] ^= hi_hash_dword[(i+15):i];
271          *    }
272          *
273          */
274         __be32 common_hash_dword = 0;
275         u32 hi_hash_dword, lo_hash_dword, flow_vm_vlan;
276         u32 hash_result = 0;
277         u8 i;
278
279         /* record the flow_vm_vlan bits as they are a key part to the hash */
280         flow_vm_vlan = IXGBE_NTOHL(atr_input->dword_stream[0]);
281
282         /* generate common hash dword */
283         for (i = 1; i <= 13; i++)
284                 common_hash_dword ^= atr_input->dword_stream[i];
285
286         hi_hash_dword = IXGBE_NTOHL(common_hash_dword);
287
288         /* low dword is word swapped version of common */
289         lo_hash_dword = (hi_hash_dword >> 16) | (hi_hash_dword << 16);
290
291         /* apply flow ID/VM pool/VLAN ID bits to hash words */
292         hi_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan >> 16);
293
294         /* Process bits 0 and 16 */
295         if (key & 0x0001) hash_result ^= lo_hash_dword;
296         if (key & 0x00010000) hash_result ^= hi_hash_dword;
297
298         /*
299          * apply flow ID/VM pool/VLAN ID bits to lo hash dword, we had to
300          * delay this because bit 0 of the stream should not be processed
301          * so we do not add the vlan until after bit 0 was processed
302          */
303         lo_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan << 16);
304
305
306         /* process the remaining 30 bits in the key 2 bits at a time */
307         for (i = 15; i; i-- ) {
308                 if (key & (0x0001 << i)) hash_result ^= lo_hash_dword >> i;
309                 if (key & (0x00010000 << i)) hash_result ^= hi_hash_dword >> i;
310         }
311
312         return hash_result;
313 }
314
315 /*
316  * Calculate the hash value needed for signature-match filters. In the FreeBSD
317  * driver, this is done by the optimised function
318  * ixgbe_atr_compute_sig_hash_82599(). However that can't be used here as it
319  * doesn't support calculating a hash for an IPv6 filter.
320  */
321 static uint32_t
322 atr_compute_sig_hash_82599(union ixgbe_atr_input *input,
323                 enum rte_fdir_pballoc_type pballoc)
324 {
325         uint32_t bucket_hash, sig_hash;
326
327         if (pballoc == RTE_FDIR_PBALLOC_256K)
328                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
329                                 IXGBE_ATR_BUCKET_HASH_KEY) &
330                                 SIG_BUCKET_256KB_HASH_MASK;
331         else if (pballoc == RTE_FDIR_PBALLOC_128K)
332                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
333                                 IXGBE_ATR_BUCKET_HASH_KEY) &
334                                 SIG_BUCKET_128KB_HASH_MASK;
335         else
336                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
337                                 IXGBE_ATR_BUCKET_HASH_KEY) &
338                                 SIG_BUCKET_64KB_HASH_MASK;
339
340         sig_hash = ixgbe_atr_compute_hash_82599(input,
341                         IXGBE_ATR_SIGNATURE_HASH_KEY);
342
343         return (sig_hash << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT) | bucket_hash;
344 }
345
346 /**
347  * This function is based on ixgbe_atr_add_signature_filter_82599() in
348  * ixgbe/ixgbe_82599.c, but uses a pre-calculated hash value. It also supports
349  * setting extra fields in the FDIRCMD register, and removes the code that was
350  * verifying the flow_type field. According to the documentation, a flow type of
351  * 00 (i.e. not TCP, UDP, or SCTP) is not supported, however it appears to
352  * work ok...
353  *
354  *  Adds a signature hash filter
355  *  @hw: pointer to hardware structure
356  *  @input: unique input dword
357  *  @queue: queue index to direct traffic to
358  *  @fdircmd: any extra flags to set in fdircmd register
359  *  @fdirhash: pre-calculated hash value for the filter
360  **/
361 static void
362 fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
363                 union ixgbe_atr_input *input, u8 queue, u32 fdircmd,
364                 u32 fdirhash)
365 {
366         u64  fdirhashcmd;
367
368         PMD_INIT_FUNC_TRACE();
369
370         /* configure FDIRCMD register */
371         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
372                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
373         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
374         fdircmd |= (u32)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
375
376         /*
377          * The lower 32-bits of fdirhashcmd is for FDIRHASH, the upper 32-bits
378          * is for FDIRCMD.  Then do a 64-bit register write from FDIRHASH.
379          */
380         fdirhashcmd = (u64)fdircmd << 32;
381         fdirhashcmd |= fdirhash;
382         IXGBE_WRITE_REG64(hw, IXGBE_FDIRHASH, fdirhashcmd);
383
384         PMD_INIT_LOG(DEBUG, "Tx Queue=%x hash=%x\n", queue, (u32)fdirhashcmd);
385 }
386
387 /*
388  * Convert DPDK rte_fdir_filter struct to ixgbe_atr_input union that is used
389  * by the IXGBE driver code.
390  */
391 static int
392 fdir_filter_to_atr_input(struct rte_fdir_filter *fdir_filter,
393                 union ixgbe_atr_input *input)
394 {
395         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP ||
396                         fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE) &&
397                         (fdir_filter->port_src || fdir_filter->port_dst)) {
398                 PMD_INIT_LOG(ERR, "Invalid fdir_filter");
399                 return -EINVAL;
400         }
401
402         memset(input, 0, sizeof(*input));
403
404         input->formatted.vlan_id = fdir_filter->vlan_id;
405         input->formatted.src_port = fdir_filter->port_src;
406         input->formatted.dst_port = fdir_filter->port_dst;
407         input->formatted.flex_bytes = fdir_filter->flex_bytes;
408
409         switch (fdir_filter->l4type) {
410         case RTE_FDIR_L4TYPE_TCP:
411                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV4;
412                 break;
413         case RTE_FDIR_L4TYPE_UDP:
414                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV4;
415                 break;
416         case RTE_FDIR_L4TYPE_SCTP:
417                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV4;
418                 break;
419         case RTE_FDIR_L4TYPE_NONE:
420                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV4;
421                 break;
422         default:
423                 PMD_INIT_LOG(ERR, " Error on l4type input");
424                 return -EINVAL;
425         }
426
427         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6) {
428                 input->formatted.flow_type |= IXGBE_ATR_L4TYPE_IPV6_MASK;
429
430                 input->formatted.src_ip[0] = fdir_filter->ip_src.ipv6_addr[0];
431                 input->formatted.src_ip[1] = fdir_filter->ip_src.ipv6_addr[1];
432                 input->formatted.src_ip[2] = fdir_filter->ip_src.ipv6_addr[2];
433                 input->formatted.src_ip[3] = fdir_filter->ip_src.ipv6_addr[3];
434
435                 input->formatted.dst_ip[0] = fdir_filter->ip_dst.ipv6_addr[0];
436                 input->formatted.dst_ip[1] = fdir_filter->ip_dst.ipv6_addr[1];
437                 input->formatted.dst_ip[2] = fdir_filter->ip_dst.ipv6_addr[2];
438                 input->formatted.dst_ip[3] = fdir_filter->ip_dst.ipv6_addr[3];
439
440         } else {
441                 input->formatted.src_ip[0] = fdir_filter->ip_src.ipv4_addr;
442                 input->formatted.dst_ip[0] = fdir_filter->ip_dst.ipv4_addr;
443         }
444
445         return 0;
446 }
447
448 /*
449  * Adds or updates a signature filter.
450  *
451  * dev: ethernet device to add filter to
452  * fdir_filter: filter details
453  * queue: queue index to direct traffic to
454  * update: 0 to add a new filter, otherwise update existing.
455  */
456 static int
457 fdir_add_update_signature_filter(struct rte_eth_dev *dev,
458                 struct rte_fdir_filter *fdir_filter, uint8_t queue, int update)
459 {
460         struct ixgbe_hw *hw= IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
461         uint32_t fdircmd_flags = (update) ? IXGBE_FDIRCMD_FILTER_UPDATE : 0;
462         uint32_t fdirhash;
463         union ixgbe_atr_input input;
464         int err;
465
466         if (hw->mac.type != ixgbe_mac_82599EB && hw->mac.type !=ixgbe_mac_X540)
467                 return -ENOSYS;
468
469         err = fdir_filter_to_atr_input(fdir_filter, &input);
470         if (err)
471                 return err;
472
473         fdirhash = atr_compute_sig_hash_82599(&input,
474                         dev->data->dev_conf.fdir_conf.pballoc);
475         fdir_add_signature_filter_82599(hw, &input, queue, fdircmd_flags,
476                         fdirhash);
477         return 0;
478 }
479
480 int
481 ixgbe_fdir_add_signature_filter(struct rte_eth_dev *dev,
482                 struct rte_fdir_filter *fdir_filter, uint8_t queue)
483 {
484         PMD_INIT_FUNC_TRACE();
485         return fdir_add_update_signature_filter(dev, fdir_filter, queue, 0);
486 }
487
488 int
489 ixgbe_fdir_update_signature_filter(struct rte_eth_dev *dev,
490                 struct rte_fdir_filter *fdir_filter, uint8_t queue)
491 {
492         PMD_INIT_FUNC_TRACE();
493         return fdir_add_update_signature_filter(dev, fdir_filter, queue, 1);
494 }
495
496 /*
497  * This is based on ixgbe_fdir_erase_perfect_filter_82599() in
498  * ixgbe/ixgbe_82599.c. It is modified to take in the hash as a parameter so
499  * that it can be used for removing signature and perfect filters.
500  */
501 static s32
502 fdir_erase_filter_82599(struct ixgbe_hw *hw,
503         __rte_unused union ixgbe_atr_input *input, uint32_t fdirhash)
504 {
505         u32 fdircmd = 0;
506         u32 retry_count;
507         s32 err = 0;
508
509         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
510
511         /* flush hash to HW */
512         IXGBE_WRITE_FLUSH(hw);
513
514         /* Query if filter is present */
515         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, IXGBE_FDIRCMD_CMD_QUERY_REM_FILT);
516
517         for (retry_count = 10; retry_count; retry_count--) {
518                 /* allow 10us for query to process */
519                 usec_delay(10);
520                 /* verify query completed successfully */
521                 fdircmd = IXGBE_READ_REG(hw, IXGBE_FDIRCMD);
522                 if (!(fdircmd & IXGBE_FDIRCMD_CMD_MASK))
523                         break;
524         }
525
526         if (!retry_count) {
527                 PMD_INIT_LOG(ERR, "Timeout querying for flow director filter");
528                 err = -EIO;
529         }
530
531         /* if filter exists in hardware then remove it */
532         if (fdircmd & IXGBE_FDIRCMD_FILTER_VALID) {
533                 IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
534                 IXGBE_WRITE_FLUSH(hw);
535                 IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
536                                 IXGBE_FDIRCMD_CMD_REMOVE_FLOW);
537         }
538
539         return err;
540 }
541
542 int
543 ixgbe_fdir_remove_signature_filter(struct rte_eth_dev *dev,
544                 struct rte_fdir_filter *fdir_filter)
545 {
546         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
547         union ixgbe_atr_input input;
548         int err;
549
550         PMD_INIT_FUNC_TRACE();
551
552         if (hw->mac.type != ixgbe_mac_82599EB && hw->mac.type !=ixgbe_mac_X540)
553                 return -ENOSYS;
554
555         err = fdir_filter_to_atr_input(fdir_filter, &input);
556         if (err)
557                 return err;
558
559         return fdir_erase_filter_82599(hw, &input,
560                         atr_compute_sig_hash_82599(&input,
561                         dev->data->dev_conf.fdir_conf.pballoc));
562 }
563
564 /**
565  * Reverse the bits in FDIR registers that store 2 x 16 bit masks.
566  *
567  *  @hi_dword: Bits 31:16 mask to be bit swapped.
568  *  @lo_dword: Bits 15:0  mask to be bit swapped.
569  *
570  *  Flow director uses several registers to store 2 x 16 bit masks with the
571  *  bits reversed such as FDIRTCPM, FDIRUDPM and FDIRIP6M. The LS bit of the
572  *  mask affects the MS bit/byte of the target. This function reverses the
573  *  bits in these masks.
574  *  **/
575 static uint32_t
576 reverse_fdir_bitmasks(uint16_t hi_dword, uint16_t lo_dword)
577 {
578         u32 mask = hi_dword << 16;
579         mask |= lo_dword;
580         mask = ((mask & 0x55555555) << 1) | ((mask & 0xAAAAAAAA) >> 1);
581         mask = ((mask & 0x33333333) << 2) | ((mask & 0xCCCCCCCC) >> 2);
582         mask = ((mask & 0x0F0F0F0F) << 4) | ((mask & 0xF0F0F0F0) >> 4);
583         return ((mask & 0x00FF00FF) << 8) | ((mask & 0xFF00FF00) >> 8);
584 }
585
586 /*
587  * This macro exists in ixgbe/ixgbe_82599.c, however in that file it reverses
588  * the bytes, and then reverses them again. So here it does nothing.
589  */
590 #define IXGBE_WRITE_REG_BE32 IXGBE_WRITE_REG
591
592 /*
593  * This is based on ixgbe_fdir_set_input_mask_82599() in ixgbe/ixgbe_82599.c,
594  * but makes use of the rte_fdir_masks structure to see which bits to set.
595  */
596 static int
597 fdir_set_input_mask_82599(struct ixgbe_hw *hw,
598                 struct rte_fdir_masks *input_mask)
599 {
600         /* mask VM pool since it is currently not supported */
601         u32 fdirm = IXGBE_FDIRM_POOL;
602         u32 fdirtcpm;  /* TCP source and destination port masks. */
603         u32 fdiripv6m; /* IPv6 source and destination masks. */
604
605         PMD_INIT_FUNC_TRACE();
606
607         /*
608          * Program the relevant mask registers.  If src/dst_port or src/dst_addr
609          * are zero, then assume a full mask for that field. Also assume that
610          * a VLAN of 0 is unspecified, so mask that out as well.  L4type
611          * cannot be masked out in this implementation.
612          */
613         if (input_mask->only_ip_flow) {
614                 /* use the L4 protocol mask for raw IPv4/IPv6 traffic */
615                 fdirm |= IXGBE_FDIRM_L4P;
616                 if (input_mask->dst_port_mask || input_mask->src_port_mask) {
617                         PMD_INIT_LOG(ERR, " Error on src/dst port mask\n");
618                         return -EINVAL;
619                 }
620         }
621
622         if (!input_mask->comp_ipv6_dst)
623                 /* mask DIPV6 */
624                 fdirm |= IXGBE_FDIRM_DIPv6;
625
626         if (!input_mask->vlan_id)
627                 /* mask VLAN ID*/
628                 fdirm |= IXGBE_FDIRM_VLANID;
629
630         if (!input_mask->vlan_prio)
631                 /* mask VLAN priority */
632                 fdirm |= IXGBE_FDIRM_VLANP;
633
634         if (!input_mask->flexbytes)
635                 /* Mask Flex Bytes */
636                 fdirm |= IXGBE_FDIRM_FLEX;
637
638         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
639
640         /* store the TCP/UDP port masks, bit reversed from port layout */
641         fdirtcpm = reverse_fdir_bitmasks(input_mask->dst_port_mask,
642                                          input_mask->src_port_mask);
643
644         /* write both the same so that UDP and TCP use the same mask */
645         IXGBE_WRITE_REG(hw, IXGBE_FDIRTCPM, ~fdirtcpm);
646         IXGBE_WRITE_REG(hw, IXGBE_FDIRUDPM, ~fdirtcpm);
647
648         if (!input_mask->set_ipv6_mask) {
649                 /* Store source and destination IPv4 masks (big-endian) */
650                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIP4M,
651                                 IXGBE_NTOHL(~input_mask->src_ipv4_mask));
652                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRDIP4M,
653                                 IXGBE_NTOHL(~input_mask->dst_ipv4_mask));
654         }
655         else {
656                 /* Store source and destination IPv6 masks (bit reversed) */
657                 fdiripv6m = reverse_fdir_bitmasks(input_mask->dst_ipv6_mask,
658                                                   input_mask->src_ipv6_mask);
659
660                 IXGBE_WRITE_REG(hw, IXGBE_FDIRIP6M, ~fdiripv6m);
661         }
662
663         return IXGBE_SUCCESS;
664 }
665
666 int
667 ixgbe_fdir_set_masks(struct rte_eth_dev *dev, struct rte_fdir_masks *fdir_masks)
668 {
669         struct ixgbe_hw *hw;
670         int err;
671
672         PMD_INIT_FUNC_TRACE();
673
674         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
675
676         if (hw->mac.type != ixgbe_mac_82599EB && hw->mac.type !=ixgbe_mac_X540)
677                 return -ENOSYS;
678
679         err = ixgbe_reinit_fdir_tables_82599(hw);
680         if (err) {
681                 PMD_INIT_LOG(ERR, "reinit of fdir tables failed");
682                 return -EIO;
683         }
684
685         return fdir_set_input_mask_82599(hw, fdir_masks);
686 }
687
688 static uint32_t
689 atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
690                 enum rte_fdir_pballoc_type pballoc)
691 {
692         if (pballoc == RTE_FDIR_PBALLOC_256K)
693                 return ixgbe_atr_compute_hash_82599(input,
694                                 IXGBE_ATR_BUCKET_HASH_KEY) &
695                                 PERFECT_BUCKET_256KB_HASH_MASK;
696         else if (pballoc == RTE_FDIR_PBALLOC_128K)
697                 return ixgbe_atr_compute_hash_82599(input,
698                                 IXGBE_ATR_BUCKET_HASH_KEY) &
699                                 PERFECT_BUCKET_128KB_HASH_MASK;
700         else
701                 return ixgbe_atr_compute_hash_82599(input,
702                                 IXGBE_ATR_BUCKET_HASH_KEY) &
703                                 PERFECT_BUCKET_64KB_HASH_MASK;
704 }
705
706 /*
707  * This is based on ixgbe_fdir_write_perfect_filter_82599() in
708  * ixgbe/ixgbe_82599.c, with the ability to set extra flags in FDIRCMD register
709  * added, and IPv6 support also added. The hash value is also pre-calculated
710  * as the pballoc value is needed to do it.
711  */
712 static void
713 fdir_write_perfect_filter_82599(struct ixgbe_hw *hw, union ixgbe_atr_input *input,
714                 uint16_t soft_id, uint8_t queue, uint32_t fdircmd,
715                 uint32_t fdirhash)
716 {
717         u32 fdirport, fdirvlan;
718
719         /* record the source address (big-endian) */
720         if (input->formatted.flow_type & IXGBE_ATR_L4TYPE_IPV6_MASK) {
721                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(0), input->formatted.src_ip[0]);
722                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(1), input->formatted.src_ip[1]);
723                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(2), input->formatted.src_ip[2]);
724                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPSA, input->formatted.src_ip[3]);
725         }
726         else {
727                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPSA, input->formatted.src_ip[0]);
728         }
729
730         /* record the first 32 bits of the destination address (big-endian) */
731         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPDA, input->formatted.dst_ip[0]);
732
733         /* record source and destination port (little-endian)*/
734         fdirport = IXGBE_NTOHS(input->formatted.dst_port);
735         fdirport <<= IXGBE_FDIRPORT_DESTINATION_SHIFT;
736         fdirport |= IXGBE_NTOHS(input->formatted.src_port);
737         IXGBE_WRITE_REG(hw, IXGBE_FDIRPORT, fdirport);
738
739         /* record vlan (little-endian) and flex_bytes(big-endian) */
740         fdirvlan = input->formatted.flex_bytes;
741         fdirvlan <<= IXGBE_FDIRVLAN_FLEX_SHIFT;
742         fdirvlan |= IXGBE_NTOHS(input->formatted.vlan_id);
743         IXGBE_WRITE_REG(hw, IXGBE_FDIRVLAN, fdirvlan);
744
745         /* configure FDIRHASH register */
746         fdirhash |= soft_id << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
747         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
748
749         /*
750          * flush all previous writes to make certain registers are
751          * programmed prior to issuing the command
752          */
753         IXGBE_WRITE_FLUSH(hw);
754
755         /* configure FDIRCMD register */
756         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
757                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
758         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
759         fdircmd |= (u32)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
760         fdircmd |= (u32)input->formatted.vm_pool << IXGBE_FDIRCMD_VT_POOL_SHIFT;
761
762         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
763 }
764
765 /*
766  * Adds or updates a perfect filter.
767  *
768  * dev: ethernet device to add filter to
769  * fdir_filter: filter details
770  * soft_id: software index for the filters
771  * queue: queue index to direct traffic to
772  * drop: non-zero if packets should be sent to the drop queue
773  * update: 0 to add a new filter, otherwise update existing.
774  */
775 static int
776 fdir_add_update_perfect_filter(struct rte_eth_dev *dev,
777                 struct rte_fdir_filter *fdir_filter, uint16_t soft_id,
778                 uint8_t queue, int drop, int update)
779 {
780         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
781         uint32_t fdircmd_flags = (update) ? IXGBE_FDIRCMD_FILTER_UPDATE : 0;
782         uint32_t fdirhash;
783         union ixgbe_atr_input input;
784         int err;
785
786         if (hw->mac.type != ixgbe_mac_82599EB && hw->mac.type !=ixgbe_mac_X540)
787                 return -ENOSYS;
788
789         err = fdir_filter_to_atr_input(fdir_filter, &input);
790         if (err)
791                 return err;
792
793         if (drop) {
794                 queue = dev->data->dev_conf.fdir_conf.drop_queue;
795                 fdircmd_flags |= IXGBE_FDIRCMD_DROP;
796         }
797
798         fdirhash = atr_compute_perfect_hash_82599(&input,
799                         dev->data->dev_conf.fdir_conf.pballoc);
800
801         fdir_write_perfect_filter_82599(hw, &input, soft_id, queue,
802                         fdircmd_flags, fdirhash);
803         return 0;
804 }
805
806 int
807 ixgbe_fdir_add_perfect_filter(struct rte_eth_dev *dev,
808                 struct rte_fdir_filter *fdir_filter, uint16_t soft_id,
809                 uint8_t queue, uint8_t drop)
810 {
811         PMD_INIT_FUNC_TRACE();
812         return fdir_add_update_perfect_filter(dev, fdir_filter, soft_id, queue,
813                         drop, 0);
814 }
815
816 int
817 ixgbe_fdir_update_perfect_filter(struct rte_eth_dev *dev,
818                 struct rte_fdir_filter *fdir_filter, uint16_t soft_id,
819                 uint8_t queue, uint8_t drop)
820 {
821         PMD_INIT_FUNC_TRACE();
822         return fdir_add_update_perfect_filter(dev, fdir_filter, soft_id, queue,
823                         drop, 1);
824 }
825
826 int
827 ixgbe_fdir_remove_perfect_filter(struct rte_eth_dev *dev,
828                 struct rte_fdir_filter *fdir_filter,
829                 uint16_t soft_id)
830 {
831         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
832         union ixgbe_atr_input input;
833         uint32_t fdirhash;
834         int err;
835
836         PMD_INIT_FUNC_TRACE();
837
838         if (hw->mac.type != ixgbe_mac_82599EB && hw->mac.type !=ixgbe_mac_X540)
839                 return -ENOSYS;
840
841         err = fdir_filter_to_atr_input(fdir_filter, &input);
842         if (err)
843                 return err;
844
845         /* configure FDIRHASH register */
846         fdirhash = atr_compute_perfect_hash_82599(&input,
847                         dev->data->dev_conf.fdir_conf.pballoc);
848         fdirhash |= soft_id << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
849
850         return fdir_erase_filter_82599(hw, &input, fdirhash);
851 }
852
853 void
854 ixgbe_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir *fdir)
855 {
856         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
857         struct ixgbe_hw_fdir_info *info =
858                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
859         uint32_t reg;
860
861         if (hw->mac.type != ixgbe_mac_82599EB && hw->mac.type !=ixgbe_mac_X540)
862                 return;
863
864         /* Get the information from registers */
865         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFREE);
866         info->collision = (uint16_t)((reg & IXGBE_FDIRFREE_COLL_MASK) >>
867                                         IXGBE_FDIRFREE_COLL_SHIFT);
868         info->free = (uint16_t)((reg & IXGBE_FDIRFREE_FREE_MASK) >>
869                                    IXGBE_FDIRFREE_FREE_SHIFT);
870
871         reg = IXGBE_READ_REG(hw, IXGBE_FDIRLEN);
872         info->maxhash = (uint16_t)((reg & IXGBE_FDIRLEN_MAXHASH_MASK) >>
873                                       IXGBE_FDIRLEN_MAXHASH_SHIFT);
874         info->maxlen  = (uint8_t)((reg & IXGBE_FDIRLEN_MAXLEN_MASK) >>
875                                      IXGBE_FDIRLEN_MAXLEN_SHIFT);
876
877         reg = IXGBE_READ_REG(hw, IXGBE_FDIRUSTAT);
878         info->remove += (reg & IXGBE_FDIRUSTAT_REMOVE_MASK) >>
879                 IXGBE_FDIRUSTAT_REMOVE_SHIFT;
880         info->add += (reg & IXGBE_FDIRUSTAT_ADD_MASK) >>
881                 IXGBE_FDIRUSTAT_ADD_SHIFT;
882
883         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFSTAT) & 0xFFFF;
884         info->f_remove += (reg & IXGBE_FDIRFSTAT_FREMOVE_MASK) >>
885                 IXGBE_FDIRFSTAT_FREMOVE_SHIFT;
886         info->f_add += (reg & IXGBE_FDIRFSTAT_FADD_MASK) >>
887                 IXGBE_FDIRFSTAT_FADD_SHIFT;
888
889         /*  Copy the new information in the fdir parameter */
890         fdir->collision = info->collision;
891         fdir->free = info->free;
892         fdir->maxhash = info->maxhash;
893         fdir->maxlen = info->maxlen;
894         fdir->remove = info->remove;
895         fdir->add = info->add;
896         fdir->f_remove = info->f_remove;
897         fdir->f_add = info->f_add;
898 }