update Intel copyright years to 2014
[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)
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 = 10; i; i -= 2)
284                 common_hash_dword ^= atr_input->dword_stream[i] ^
285                                      atr_input->dword_stream[i - 1];
286
287         hi_hash_dword = IXGBE_NTOHL(common_hash_dword);
288
289         /* low dword is word swapped version of common */
290         lo_hash_dword = (hi_hash_dword >> 16) | (hi_hash_dword << 16);
291
292         /* apply flow ID/VM pool/VLAN ID bits to hash words */
293         hi_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan >> 16);
294
295         /* Process bits 0 and 16 */
296         if (key & 0x0001) hash_result ^= lo_hash_dword;
297         if (key & 0x00010000) hash_result ^= hi_hash_dword;
298
299         /*
300          * apply flow ID/VM pool/VLAN ID bits to lo hash dword, we had to
301          * delay this because bit 0 of the stream should not be processed
302          * so we do not add the vlan until after bit 0 was processed
303          */
304         lo_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan << 16);
305
306
307         /* process the remaining 30 bits in the key 2 bits at a time */
308         for (i = 15; i; i-- ) {
309                 if (key & (0x0001 << i)) hash_result ^= lo_hash_dword >> i;
310                 if (key & (0x00010000 << i)) hash_result ^= hi_hash_dword >> i;
311         }
312
313         return hash_result;
314 }
315
316 /*
317  * Calculate the hash value needed for signature-match filters. In the FreeBSD
318  * driver, this is done by the optimised function
319  * ixgbe_atr_compute_sig_hash_82599(). However that can't be used here as it
320  * doesn't support calculating a hash for an IPv6 filter.
321  */
322 static uint32_t
323 atr_compute_sig_hash_82599(union ixgbe_atr_input *input,
324                 enum rte_fdir_pballoc_type pballoc)
325 {
326         uint32_t bucket_hash, sig_hash;
327
328         if (pballoc == RTE_FDIR_PBALLOC_256K)
329                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
330                                 IXGBE_ATR_BUCKET_HASH_KEY) &
331                                 SIG_BUCKET_256KB_HASH_MASK;
332         else if (pballoc == RTE_FDIR_PBALLOC_128K)
333                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
334                                 IXGBE_ATR_BUCKET_HASH_KEY) &
335                                 SIG_BUCKET_128KB_HASH_MASK;
336         else
337                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
338                                 IXGBE_ATR_BUCKET_HASH_KEY) &
339                                 SIG_BUCKET_64KB_HASH_MASK;
340
341         sig_hash = ixgbe_atr_compute_hash_82599(input,
342                         IXGBE_ATR_SIGNATURE_HASH_KEY);
343
344         return (sig_hash << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT) | bucket_hash;
345 }
346
347 /**
348  * This function is based on ixgbe_atr_add_signature_filter_82599() in
349  * ixgbe/ixgbe_82599.c, but uses a pre-calculated hash value. It also supports
350  * setting extra fields in the FDIRCMD register, and removes the code that was
351  * verifying the flow_type field. According to the documentation, a flow type of
352  * 00 (i.e. not TCP, UDP, or SCTP) is not supported, however it appears to
353  * work ok...
354  *
355  *  Adds a signature hash filter
356  *  @hw: pointer to hardware structure
357  *  @input: unique input dword
358  *  @queue: queue index to direct traffic to
359  *  @fdircmd: any extra flags to set in fdircmd register
360  *  @fdirhash: pre-calculated hash value for the filter
361  **/
362 static void
363 fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
364                 union ixgbe_atr_input *input, u8 queue, u32 fdircmd,
365                 u32 fdirhash)
366 {
367         u64  fdirhashcmd;
368
369         PMD_INIT_FUNC_TRACE();
370
371         /* configure FDIRCMD register */
372         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
373                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
374         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
375         fdircmd |= (u32)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
376
377         /*
378          * The lower 32-bits of fdirhashcmd is for FDIRHASH, the upper 32-bits
379          * is for FDIRCMD.  Then do a 64-bit register write from FDIRHASH.
380          */
381         fdirhashcmd = (u64)fdircmd << 32;
382         fdirhashcmd |= fdirhash;
383         IXGBE_WRITE_REG64(hw, IXGBE_FDIRHASH, fdirhashcmd);
384
385         PMD_INIT_LOG(DEBUG, "Tx Queue=%x hash=%x\n", queue, (u32)fdirhashcmd);
386 }
387
388 /*
389  * Convert DPDK rte_fdir_filter struct to ixgbe_atr_input union that is used
390  * by the IXGBE driver code.
391  */
392 static int
393 fdir_filter_to_atr_input(struct rte_fdir_filter *fdir_filter,
394                 union ixgbe_atr_input *input)
395 {
396         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP ||
397                         fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE) &&
398                         (fdir_filter->port_src || fdir_filter->port_dst)) {
399                 PMD_INIT_LOG(ERR, "Invalid fdir_filter");
400                 return -EINVAL;
401         }
402
403         memset(input, 0, sizeof(*input));
404
405         input->formatted.vlan_id = fdir_filter->vlan_id;
406         input->formatted.src_port = fdir_filter->port_src;
407         input->formatted.dst_port = fdir_filter->port_dst;
408         input->formatted.flex_bytes = fdir_filter->flex_bytes;
409
410         switch (fdir_filter->l4type) {
411         case RTE_FDIR_L4TYPE_TCP:
412                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV4;
413                 break;
414         case RTE_FDIR_L4TYPE_UDP:
415                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV4;
416                 break;
417         case RTE_FDIR_L4TYPE_SCTP:
418                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV4;
419                 break;
420         case RTE_FDIR_L4TYPE_NONE:
421                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV4;
422                 break;
423         default:
424                 PMD_INIT_LOG(ERR, " Error on l4type input");
425                 return -EINVAL;
426         }
427
428         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6) {
429                 input->formatted.flow_type |= IXGBE_ATR_L4TYPE_IPV6_MASK;
430
431                 input->formatted.src_ip[0] = fdir_filter->ip_src.ipv6_addr[0];
432                 input->formatted.src_ip[1] = fdir_filter->ip_src.ipv6_addr[1];
433                 input->formatted.src_ip[2] = fdir_filter->ip_src.ipv6_addr[2];
434                 input->formatted.src_ip[3] = fdir_filter->ip_src.ipv6_addr[3];
435
436                 input->formatted.dst_ip[0] = fdir_filter->ip_dst.ipv6_addr[0];
437                 input->formatted.dst_ip[1] = fdir_filter->ip_dst.ipv6_addr[1];
438                 input->formatted.dst_ip[2] = fdir_filter->ip_dst.ipv6_addr[2];
439                 input->formatted.dst_ip[3] = fdir_filter->ip_dst.ipv6_addr[3];
440
441         } else {
442                 input->formatted.src_ip[0] = fdir_filter->ip_src.ipv4_addr;
443                 input->formatted.dst_ip[0] = fdir_filter->ip_dst.ipv4_addr;
444         }
445
446         return 0;
447 }
448
449 /*
450  * Adds or updates a signature filter.
451  *
452  * dev: ethernet device to add filter to
453  * fdir_filter: filter details
454  * queue: queue index to direct traffic to
455  * update: 0 to add a new filter, otherwise update existing.
456  */
457 static int
458 fdir_add_update_signature_filter(struct rte_eth_dev *dev,
459                 struct rte_fdir_filter *fdir_filter, uint8_t queue, int update)
460 {
461         struct ixgbe_hw *hw= IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
462         uint32_t fdircmd_flags = (update) ? IXGBE_FDIRCMD_FILTER_UPDATE : 0;
463         uint32_t fdirhash;
464         union ixgbe_atr_input input;
465         int err;
466
467         if (hw->mac.type != ixgbe_mac_82599EB)
468                 return -ENOSYS;
469
470         err = fdir_filter_to_atr_input(fdir_filter, &input);
471         if (err)
472                 return err;
473
474         fdirhash = atr_compute_sig_hash_82599(&input,
475                         dev->data->dev_conf.fdir_conf.pballoc);
476         fdir_add_signature_filter_82599(hw, &input, queue, fdircmd_flags,
477                         fdirhash);
478         return 0;
479 }
480
481 int
482 ixgbe_fdir_add_signature_filter(struct rte_eth_dev *dev,
483                 struct rte_fdir_filter *fdir_filter, uint8_t queue)
484 {
485         PMD_INIT_FUNC_TRACE();
486         return fdir_add_update_signature_filter(dev, fdir_filter, queue, 0);
487 }
488
489 int
490 ixgbe_fdir_update_signature_filter(struct rte_eth_dev *dev,
491                 struct rte_fdir_filter *fdir_filter, uint8_t queue)
492 {
493         PMD_INIT_FUNC_TRACE();
494         return fdir_add_update_signature_filter(dev, fdir_filter, queue, 1);
495 }
496
497 /*
498  * This is based on ixgbe_fdir_erase_perfect_filter_82599() in
499  * ixgbe/ixgbe_82599.c. It is modified to take in the hash as a parameter so
500  * that it can be used for removing signature and perfect filters.
501  */
502 static s32
503 fdir_erase_filter_82599(struct ixgbe_hw *hw,
504         __rte_unused union ixgbe_atr_input *input, uint32_t fdirhash)
505 {
506         u32 fdircmd = 0;
507         u32 retry_count;
508         s32 err = 0;
509
510         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
511
512         /* flush hash to HW */
513         IXGBE_WRITE_FLUSH(hw);
514
515         /* Query if filter is present */
516         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, IXGBE_FDIRCMD_CMD_QUERY_REM_FILT);
517
518         for (retry_count = 10; retry_count; retry_count--) {
519                 /* allow 10us for query to process */
520                 usec_delay(10);
521                 /* verify query completed successfully */
522                 fdircmd = IXGBE_READ_REG(hw, IXGBE_FDIRCMD);
523                 if (!(fdircmd & IXGBE_FDIRCMD_CMD_MASK))
524                         break;
525         }
526
527         if (!retry_count) {
528                 PMD_INIT_LOG(ERR, "Timeout querying for flow director filter");
529                 err = -EIO;
530         }
531
532         /* if filter exists in hardware then remove it */
533         if (fdircmd & IXGBE_FDIRCMD_FILTER_VALID) {
534                 IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
535                 IXGBE_WRITE_FLUSH(hw);
536                 IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
537                                 IXGBE_FDIRCMD_CMD_REMOVE_FLOW);
538         }
539
540         return err;
541 }
542
543 int
544 ixgbe_fdir_remove_signature_filter(struct rte_eth_dev *dev,
545                 struct rte_fdir_filter *fdir_filter)
546 {
547         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
548         union ixgbe_atr_input input;
549         int err;
550
551         PMD_INIT_FUNC_TRACE();
552
553         if (hw->mac.type != ixgbe_mac_82599EB)
554                 return -ENOSYS;
555
556         err = fdir_filter_to_atr_input(fdir_filter, &input);
557         if (err)
558                 return err;
559
560         return fdir_erase_filter_82599(hw, &input,
561                         atr_compute_sig_hash_82599(&input,
562                         dev->data->dev_conf.fdir_conf.pballoc));
563 }
564
565 /**
566  * Reverse the bits in FDIR registers that store 2 x 16 bit masks.
567  *
568  *  @hi_dword: Bits 31:16 mask to be bit swapped.
569  *  @lo_dword: Bits 15:0  mask to be bit swapped.
570  *
571  *  Flow director uses several registers to store 2 x 16 bit masks with the
572  *  bits reversed such as FDIRTCPM, FDIRUDPM and FDIRIP6M. The LS bit of the
573  *  mask affects the MS bit/byte of the target. This function reverses the
574  *  bits in these masks.
575  *  **/
576 static uint32_t
577 reverse_fdir_bitmasks(uint16_t hi_dword, uint16_t lo_dword)
578 {
579         u32 mask = hi_dword << 16;
580         mask |= lo_dword;
581         mask = ((mask & 0x55555555) << 1) | ((mask & 0xAAAAAAAA) >> 1);
582         mask = ((mask & 0x33333333) << 2) | ((mask & 0xCCCCCCCC) >> 2);
583         mask = ((mask & 0x0F0F0F0F) << 4) | ((mask & 0xF0F0F0F0) >> 4);
584         return ((mask & 0x00FF00FF) << 8) | ((mask & 0xFF00FF00) >> 8);
585 }
586
587 /*
588  * This macro exists in ixgbe/ixgbe_82599.c, however in that file it reverses
589  * the bytes, and then reverses them again. So here it does nothing.
590  */
591 #define IXGBE_WRITE_REG_BE32 IXGBE_WRITE_REG
592
593 /*
594  * This is based on ixgbe_fdir_set_input_mask_82599() in ixgbe/ixgbe_82599.c,
595  * but makes use of the rte_fdir_masks structure to see which bits to set.
596  */
597 static int
598 fdir_set_input_mask_82599(struct ixgbe_hw *hw,
599                 struct rte_fdir_masks *input_mask)
600 {
601         /* mask VM pool since it is currently not supported */
602         u32 fdirm = IXGBE_FDIRM_POOL;
603         u32 fdirtcpm;  /* TCP source and destination port masks. */
604         u32 fdiripv6m; /* IPv6 source and destination masks. */
605
606         PMD_INIT_FUNC_TRACE();
607
608         /*
609          * Program the relevant mask registers.  If src/dst_port or src/dst_addr
610          * are zero, then assume a full mask for that field. Also assume that
611          * a VLAN of 0 is unspecified, so mask that out as well.  L4type
612          * cannot be masked out in this implementation.
613          */
614         if (input_mask->only_ip_flow) {
615                 /* use the L4 protocol mask for raw IPv4/IPv6 traffic */
616                 fdirm |= IXGBE_FDIRM_L4P;
617                 if (input_mask->dst_port_mask || input_mask->src_port_mask) {
618                         PMD_INIT_LOG(ERR, " Error on src/dst port mask\n");
619                         return -EINVAL;
620                 }
621         }
622
623         if (!input_mask->comp_ipv6_dst)
624                 /* mask DIPV6 */
625                 fdirm |= IXGBE_FDIRM_DIPv6;
626
627         if (!input_mask->vlan_id)
628                 /* mask VLAN ID*/
629                 fdirm |= IXGBE_FDIRM_VLANID;
630
631         if (!input_mask->vlan_prio)
632                 /* mask VLAN priority */
633                 fdirm |= IXGBE_FDIRM_VLANP;
634
635         if (!input_mask->flexbytes)
636                 /* Mask Flex Bytes */
637                 fdirm |= IXGBE_FDIRM_FLEX;
638
639         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
640
641         /* store the TCP/UDP port masks, bit reversed from port layout */
642         fdirtcpm = reverse_fdir_bitmasks(input_mask->dst_port_mask,
643                                          input_mask->src_port_mask);
644
645         /* write both the same so that UDP and TCP use the same mask */
646         IXGBE_WRITE_REG(hw, IXGBE_FDIRTCPM, ~fdirtcpm);
647         IXGBE_WRITE_REG(hw, IXGBE_FDIRUDPM, ~fdirtcpm);
648
649         if (!input_mask->set_ipv6_mask) {
650                 /* Store source and destination IPv4 masks (big-endian) */
651                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIP4M,
652                                 IXGBE_NTOHL(~input_mask->src_ipv4_mask));
653                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRDIP4M,
654                                 IXGBE_NTOHL(~input_mask->dst_ipv4_mask));
655         }
656         else {
657                 /* Store source and destination IPv6 masks (bit reversed) */
658                 fdiripv6m = reverse_fdir_bitmasks(input_mask->dst_ipv6_mask,
659                                                   input_mask->src_ipv6_mask);
660
661                 IXGBE_WRITE_REG(hw, IXGBE_FDIRIP6M, ~fdiripv6m);
662         }
663
664         return IXGBE_SUCCESS;
665 }
666
667 int
668 ixgbe_fdir_set_masks(struct rte_eth_dev *dev, struct rte_fdir_masks *fdir_masks)
669 {
670         struct ixgbe_hw *hw;
671         int err;
672
673         PMD_INIT_FUNC_TRACE();
674
675         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
676
677         if (hw->mac.type != ixgbe_mac_82599EB)
678                 return -ENOSYS;
679
680         err = ixgbe_reinit_fdir_tables_82599(hw);
681         if (err) {
682                 PMD_INIT_LOG(ERR, "reinit of fdir tables failed");
683                 return -EIO;
684         }
685
686         return fdir_set_input_mask_82599(hw, fdir_masks);
687 }
688
689 static uint32_t
690 atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
691                 enum rte_fdir_pballoc_type pballoc)
692 {
693         if (pballoc == RTE_FDIR_PBALLOC_256K)
694                 return ixgbe_atr_compute_hash_82599(input,
695                                 IXGBE_ATR_BUCKET_HASH_KEY) &
696                                 PERFECT_BUCKET_256KB_HASH_MASK;
697         else if (pballoc == RTE_FDIR_PBALLOC_128K)
698                 return ixgbe_atr_compute_hash_82599(input,
699                                 IXGBE_ATR_BUCKET_HASH_KEY) &
700                                 PERFECT_BUCKET_128KB_HASH_MASK;
701         else
702                 return ixgbe_atr_compute_hash_82599(input,
703                                 IXGBE_ATR_BUCKET_HASH_KEY) &
704                                 PERFECT_BUCKET_64KB_HASH_MASK;
705 }
706
707 /*
708  * This is based on ixgbe_fdir_write_perfect_filter_82599() in
709  * ixgbe/ixgbe_82599.c, with the ability to set extra flags in FDIRCMD register
710  * added, and IPv6 support also added. The hash value is also pre-calculated
711  * as the pballoc value is needed to do it.
712  */
713 static void
714 fdir_write_perfect_filter_82599(struct ixgbe_hw *hw, union ixgbe_atr_input *input,
715                 uint16_t soft_id, uint8_t queue, uint32_t fdircmd,
716                 uint32_t fdirhash)
717 {
718         u32 fdirport, fdirvlan;
719
720         /* record the source address (big-endian) */
721         if (input->formatted.flow_type & IXGBE_ATR_L4TYPE_IPV6_MASK) {
722                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(0), input->formatted.src_ip[0]);
723                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(1), input->formatted.src_ip[1]);
724                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRSIPv6(2), input->formatted.src_ip[2]);
725                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPSA, input->formatted.src_ip[3]);
726         }
727         else {
728                 IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPSA, input->formatted.src_ip[0]);
729         }
730
731         /* record the first 32 bits of the destination address (big-endian) */
732         IXGBE_WRITE_REG_BE32(hw, IXGBE_FDIRIPDA, input->formatted.dst_ip[0]);
733
734         /* record source and destination port (little-endian)*/
735         fdirport = IXGBE_NTOHS(input->formatted.dst_port);
736         fdirport <<= IXGBE_FDIRPORT_DESTINATION_SHIFT;
737         fdirport |= IXGBE_NTOHS(input->formatted.src_port);
738         IXGBE_WRITE_REG(hw, IXGBE_FDIRPORT, fdirport);
739
740         /* record vlan (little-endian) and flex_bytes(big-endian) */
741         fdirvlan = input->formatted.flex_bytes;
742         fdirvlan <<= IXGBE_FDIRVLAN_FLEX_SHIFT;
743         fdirvlan |= IXGBE_NTOHS(input->formatted.vlan_id);
744         IXGBE_WRITE_REG(hw, IXGBE_FDIRVLAN, fdirvlan);
745
746         /* configure FDIRHASH register */
747         fdirhash |= soft_id << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
748         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
749
750         /*
751          * flush all previous writes to make certain registers are
752          * programmed prior to issuing the command
753          */
754         IXGBE_WRITE_FLUSH(hw);
755
756         /* configure FDIRCMD register */
757         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
758                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
759         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
760         fdircmd |= (u32)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
761         fdircmd |= (u32)input->formatted.vm_pool << IXGBE_FDIRCMD_VT_POOL_SHIFT;
762
763         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
764 }
765
766 /*
767  * Adds or updates a perfect filter.
768  *
769  * dev: ethernet device to add filter to
770  * fdir_filter: filter details
771  * soft_id: software index for the filters
772  * queue: queue index to direct traffic to
773  * drop: non-zero if packets should be sent to the drop queue
774  * update: 0 to add a new filter, otherwise update existing.
775  */
776 static int
777 fdir_add_update_perfect_filter(struct rte_eth_dev *dev,
778                 struct rte_fdir_filter *fdir_filter, uint16_t soft_id,
779                 uint8_t queue, int drop, int update)
780 {
781         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
782         uint32_t fdircmd_flags = (update) ? IXGBE_FDIRCMD_FILTER_UPDATE : 0;
783         uint32_t fdirhash;
784         union ixgbe_atr_input input;
785         int err;
786
787         if (hw->mac.type != ixgbe_mac_82599EB)
788                 return -ENOSYS;
789
790         err = fdir_filter_to_atr_input(fdir_filter, &input);
791         if (err)
792                 return err;
793
794         if (drop) {
795                 queue = dev->data->dev_conf.fdir_conf.drop_queue;
796                 fdircmd_flags |= IXGBE_FDIRCMD_DROP;
797         }
798
799         fdirhash = atr_compute_perfect_hash_82599(&input,
800                         dev->data->dev_conf.fdir_conf.pballoc);
801
802         fdir_write_perfect_filter_82599(hw, &input, soft_id, queue,
803                         fdircmd_flags, fdirhash);
804         return 0;
805 }
806
807 int
808 ixgbe_fdir_add_perfect_filter(struct rte_eth_dev *dev,
809                 struct rte_fdir_filter *fdir_filter, uint16_t soft_id,
810                 uint8_t queue, uint8_t drop)
811 {
812         PMD_INIT_FUNC_TRACE();
813         return fdir_add_update_perfect_filter(dev, fdir_filter, soft_id, queue,
814                         drop, 0);
815 }
816
817 int
818 ixgbe_fdir_update_perfect_filter(struct rte_eth_dev *dev,
819                 struct rte_fdir_filter *fdir_filter, uint16_t soft_id,
820                 uint8_t queue, uint8_t drop)
821 {
822         PMD_INIT_FUNC_TRACE();
823         return fdir_add_update_perfect_filter(dev, fdir_filter, soft_id, queue,
824                         drop, 1);
825 }
826
827 int
828 ixgbe_fdir_remove_perfect_filter(struct rte_eth_dev *dev,
829                 struct rte_fdir_filter *fdir_filter,
830                 uint16_t soft_id)
831 {
832         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
833         union ixgbe_atr_input input;
834         uint32_t fdirhash;
835         int err;
836
837         PMD_INIT_FUNC_TRACE();
838
839         if (hw->mac.type != ixgbe_mac_82599EB)
840                 return -ENOSYS;
841
842         err = fdir_filter_to_atr_input(fdir_filter, &input);
843         if (err)
844                 return err;
845
846         /* configure FDIRHASH register */
847         fdirhash = atr_compute_perfect_hash_82599(&input,
848                         dev->data->dev_conf.fdir_conf.pballoc);
849         fdirhash |= soft_id << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
850
851         return fdir_erase_filter_82599(hw, &input, fdirhash);
852 }
853
854 void
855 ixgbe_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir *fdir)
856 {
857         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
858         struct ixgbe_hw_fdir_info *info =
859                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
860         uint32_t reg;
861
862         if (hw->mac.type != ixgbe_mac_82599EB)
863                 return;
864
865         /* Get the information from registers */
866         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFREE);
867         info->collision = (uint16_t)((reg & IXGBE_FDIRFREE_COLL_MASK) >>
868                                         IXGBE_FDIRFREE_COLL_SHIFT);
869         info->free = (uint16_t)((reg & IXGBE_FDIRFREE_FREE_MASK) >>
870                                    IXGBE_FDIRFREE_FREE_SHIFT);
871
872         reg = IXGBE_READ_REG(hw, IXGBE_FDIRLEN);
873         info->maxhash = (uint16_t)((reg & IXGBE_FDIRLEN_MAXHASH_MASK) >>
874                                       IXGBE_FDIRLEN_MAXHASH_SHIFT);
875         info->maxlen  = (uint8_t)((reg & IXGBE_FDIRLEN_MAXLEN_MASK) >>
876                                      IXGBE_FDIRLEN_MAXLEN_SHIFT);
877
878         reg = IXGBE_READ_REG(hw, IXGBE_FDIRUSTAT);
879         info->remove += (reg & IXGBE_FDIRUSTAT_REMOVE_MASK) >>
880                 IXGBE_FDIRUSTAT_REMOVE_SHIFT;
881         info->add += (reg & IXGBE_FDIRUSTAT_ADD_MASK) >>
882                 IXGBE_FDIRUSTAT_ADD_SHIFT;
883
884         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFSTAT) & 0xFFFF;
885         info->f_remove += (reg & IXGBE_FDIRFSTAT_FREMOVE_MASK) >>
886                 IXGBE_FDIRFSTAT_FREMOVE_SHIFT;
887         info->f_add += (reg & IXGBE_FDIRFSTAT_FADD_MASK) >>
888                 IXGBE_FDIRFSTAT_FADD_SHIFT;
889
890         /*  Copy the new information in the fdir parameter */
891         fdir->collision = info->collision;
892         fdir->free = info->free;
893         fdir->maxhash = info->maxhash;
894         fdir->maxlen = info->maxlen;
895         fdir->remove = info->remove;
896         fdir->add = info->add;
897         fdir->f_remove = info->f_remove;
898         fdir->f_add = info->f_add;
899 }