25d79b5c4ed9bc80bf3635dd1edbada7c9831e14
[dpdk.git] / drivers / net / ice / base / ice_flex_pipe.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2020 Intel Corporation
3  */
4
5 #include "ice_common.h"
6 #include "ice_flex_pipe.h"
7 #include "ice_protocol_type.h"
8 #include "ice_flow.h"
9
10 /* To support tunneling entries by PF, the package will append the PF number to
11  * the label; for example TNL_VXLAN_PF0, TNL_VXLAN_PF1, TNL_VXLAN_PF2, etc.
12  */
13 static const struct ice_tunnel_type_scan tnls[] = {
14         { TNL_VXLAN,            "TNL_VXLAN_PF" },
15         { TNL_GENEVE,           "TNL_GENEVE_PF" },
16         { TNL_LAST,             "" }
17 };
18
19 static const u32 ice_sect_lkup[ICE_BLK_COUNT][ICE_SECT_COUNT] = {
20         /* SWITCH */
21         {
22                 ICE_SID_XLT0_SW,
23                 ICE_SID_XLT_KEY_BUILDER_SW,
24                 ICE_SID_XLT1_SW,
25                 ICE_SID_XLT2_SW,
26                 ICE_SID_PROFID_TCAM_SW,
27                 ICE_SID_PROFID_REDIR_SW,
28                 ICE_SID_FLD_VEC_SW,
29                 ICE_SID_CDID_KEY_BUILDER_SW,
30                 ICE_SID_CDID_REDIR_SW
31         },
32
33         /* ACL */
34         {
35                 ICE_SID_XLT0_ACL,
36                 ICE_SID_XLT_KEY_BUILDER_ACL,
37                 ICE_SID_XLT1_ACL,
38                 ICE_SID_XLT2_ACL,
39                 ICE_SID_PROFID_TCAM_ACL,
40                 ICE_SID_PROFID_REDIR_ACL,
41                 ICE_SID_FLD_VEC_ACL,
42                 ICE_SID_CDID_KEY_BUILDER_ACL,
43                 ICE_SID_CDID_REDIR_ACL
44         },
45
46         /* FD */
47         {
48                 ICE_SID_XLT0_FD,
49                 ICE_SID_XLT_KEY_BUILDER_FD,
50                 ICE_SID_XLT1_FD,
51                 ICE_SID_XLT2_FD,
52                 ICE_SID_PROFID_TCAM_FD,
53                 ICE_SID_PROFID_REDIR_FD,
54                 ICE_SID_FLD_VEC_FD,
55                 ICE_SID_CDID_KEY_BUILDER_FD,
56                 ICE_SID_CDID_REDIR_FD
57         },
58
59         /* RSS */
60         {
61                 ICE_SID_XLT0_RSS,
62                 ICE_SID_XLT_KEY_BUILDER_RSS,
63                 ICE_SID_XLT1_RSS,
64                 ICE_SID_XLT2_RSS,
65                 ICE_SID_PROFID_TCAM_RSS,
66                 ICE_SID_PROFID_REDIR_RSS,
67                 ICE_SID_FLD_VEC_RSS,
68                 ICE_SID_CDID_KEY_BUILDER_RSS,
69                 ICE_SID_CDID_REDIR_RSS
70         },
71
72         /* PE */
73         {
74                 ICE_SID_XLT0_PE,
75                 ICE_SID_XLT_KEY_BUILDER_PE,
76                 ICE_SID_XLT1_PE,
77                 ICE_SID_XLT2_PE,
78                 ICE_SID_PROFID_TCAM_PE,
79                 ICE_SID_PROFID_REDIR_PE,
80                 ICE_SID_FLD_VEC_PE,
81                 ICE_SID_CDID_KEY_BUILDER_PE,
82                 ICE_SID_CDID_REDIR_PE
83         }
84 };
85
86 /**
87  * ice_sect_id - returns section ID
88  * @blk: block type
89  * @sect: section type
90  *
91  * This helper function returns the proper section ID given a block type and a
92  * section type.
93  */
94 static u32 ice_sect_id(enum ice_block blk, enum ice_sect sect)
95 {
96         return ice_sect_lkup[blk][sect];
97 }
98
99 /**
100  * ice_pkg_val_buf
101  * @buf: pointer to the ice buffer
102  *
103  * This helper function validates a buffer's header.
104  */
105 static struct ice_buf_hdr *ice_pkg_val_buf(struct ice_buf *buf)
106 {
107         struct ice_buf_hdr *hdr;
108         u16 section_count;
109         u16 data_end;
110
111         hdr = (struct ice_buf_hdr *)buf->buf;
112         /* verify data */
113         section_count = LE16_TO_CPU(hdr->section_count);
114         if (section_count < ICE_MIN_S_COUNT || section_count > ICE_MAX_S_COUNT)
115                 return NULL;
116
117         data_end = LE16_TO_CPU(hdr->data_end);
118         if (data_end < ICE_MIN_S_DATA_END || data_end > ICE_MAX_S_DATA_END)
119                 return NULL;
120
121         return hdr;
122 }
123
124 /**
125  * ice_find_buf_table
126  * @ice_seg: pointer to the ice segment
127  *
128  * Returns the address of the buffer table within the ice segment.
129  */
130 static struct ice_buf_table *ice_find_buf_table(struct ice_seg *ice_seg)
131 {
132         struct ice_nvm_table *nvms;
133
134         nvms = (struct ice_nvm_table *)
135                 (ice_seg->device_table +
136                  LE32_TO_CPU(ice_seg->device_table_count));
137
138         return (_FORCE_ struct ice_buf_table *)
139                 (nvms->vers + LE32_TO_CPU(nvms->table_count));
140 }
141
142 /**
143  * ice_pkg_enum_buf
144  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
145  * @state: pointer to the enum state
146  *
147  * This function will enumerate all the buffers in the ice segment. The first
148  * call is made with the ice_seg parameter non-NULL; on subsequent calls,
149  * ice_seg is set to NULL which continues the enumeration. When the function
150  * returns a NULL pointer, then the end of the buffers has been reached, or an
151  * unexpected value has been detected (for example an invalid section count or
152  * an invalid buffer end value).
153  */
154 static struct ice_buf_hdr *
155 ice_pkg_enum_buf(struct ice_seg *ice_seg, struct ice_pkg_enum *state)
156 {
157         if (ice_seg) {
158                 state->buf_table = ice_find_buf_table(ice_seg);
159                 if (!state->buf_table)
160                         return NULL;
161
162                 state->buf_idx = 0;
163                 return ice_pkg_val_buf(state->buf_table->buf_array);
164         }
165
166         if (++state->buf_idx < LE32_TO_CPU(state->buf_table->buf_count))
167                 return ice_pkg_val_buf(state->buf_table->buf_array +
168                                        state->buf_idx);
169         else
170                 return NULL;
171 }
172
173 /**
174  * ice_pkg_advance_sect
175  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
176  * @state: pointer to the enum state
177  *
178  * This helper function will advance the section within the ice segment,
179  * also advancing the buffer if needed.
180  */
181 static bool
182 ice_pkg_advance_sect(struct ice_seg *ice_seg, struct ice_pkg_enum *state)
183 {
184         if (!ice_seg && !state->buf)
185                 return false;
186
187         if (!ice_seg && state->buf)
188                 if (++state->sect_idx < LE16_TO_CPU(state->buf->section_count))
189                         return true;
190
191         state->buf = ice_pkg_enum_buf(ice_seg, state);
192         if (!state->buf)
193                 return false;
194
195         /* start of new buffer, reset section index */
196         state->sect_idx = 0;
197         return true;
198 }
199
200 /**
201  * ice_pkg_enum_section
202  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
203  * @state: pointer to the enum state
204  * @sect_type: section type to enumerate
205  *
206  * This function will enumerate all the sections of a particular type in the
207  * ice segment. The first call is made with the ice_seg parameter non-NULL;
208  * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
209  * When the function returns a NULL pointer, then the end of the matching
210  * sections has been reached.
211  */
212 static void *
213 ice_pkg_enum_section(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
214                      u32 sect_type)
215 {
216         u16 offset, size;
217
218         if (ice_seg)
219                 state->type = sect_type;
220
221         if (!ice_pkg_advance_sect(ice_seg, state))
222                 return NULL;
223
224         /* scan for next matching section */
225         while (state->buf->section_entry[state->sect_idx].type !=
226                CPU_TO_LE32(state->type))
227                 if (!ice_pkg_advance_sect(NULL, state))
228                         return NULL;
229
230         /* validate section */
231         offset = LE16_TO_CPU(state->buf->section_entry[state->sect_idx].offset);
232         if (offset < ICE_MIN_S_OFF || offset > ICE_MAX_S_OFF)
233                 return NULL;
234
235         size = LE16_TO_CPU(state->buf->section_entry[state->sect_idx].size);
236         if (size < ICE_MIN_S_SZ || size > ICE_MAX_S_SZ)
237                 return NULL;
238
239         /* make sure the section fits in the buffer */
240         if (offset + size > ICE_PKG_BUF_SIZE)
241                 return NULL;
242
243         state->sect_type =
244                 LE32_TO_CPU(state->buf->section_entry[state->sect_idx].type);
245
246         /* calc pointer to this section */
247         state->sect = ((u8 *)state->buf) +
248                 LE16_TO_CPU(state->buf->section_entry[state->sect_idx].offset);
249
250         return state->sect;
251 }
252
253 /**
254  * ice_pkg_enum_entry
255  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
256  * @state: pointer to the enum state
257  * @sect_type: section type to enumerate
258  * @offset: pointer to variable that receives the offset in the table (optional)
259  * @handler: function that handles access to the entries into the section type
260  *
261  * This function will enumerate all the entries in particular section type in
262  * the ice segment. The first call is made with the ice_seg parameter non-NULL;
263  * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
264  * When the function returns a NULL pointer, then the end of the entries has
265  * been reached.
266  *
267  * Since each section may have a different header and entry size, the handler
268  * function is needed to determine the number and location entries in each
269  * section.
270  *
271  * The offset parameter is optional, but should be used for sections that
272  * contain an offset for each section table. For such cases, the section handler
273  * function must return the appropriate offset + index to give the absolution
274  * offset for each entry. For example, if the base for a section's header
275  * indicates a base offset of 10, and the index for the entry is 2, then
276  * section handler function should set the offset to 10 + 2 = 12.
277  */
278 static void *
279 ice_pkg_enum_entry(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
280                    u32 sect_type, u32 *offset,
281                    void *(*handler)(u32 sect_type, void *section,
282                                     u32 index, u32 *offset))
283 {
284         void *entry;
285
286         if (ice_seg) {
287                 if (!handler)
288                         return NULL;
289
290                 if (!ice_pkg_enum_section(ice_seg, state, sect_type))
291                         return NULL;
292
293                 state->entry_idx = 0;
294                 state->handler = handler;
295         } else {
296                 state->entry_idx++;
297         }
298
299         if (!state->handler)
300                 return NULL;
301
302         /* get entry */
303         entry = state->handler(state->sect_type, state->sect, state->entry_idx,
304                                offset);
305         if (!entry) {
306                 /* end of a section, look for another section of this type */
307                 if (!ice_pkg_enum_section(NULL, state, 0))
308                         return NULL;
309
310                 state->entry_idx = 0;
311                 entry = state->handler(state->sect_type, state->sect,
312                                        state->entry_idx, offset);
313         }
314
315         return entry;
316 }
317
318 /**
319  * ice_boost_tcam_handler
320  * @sect_type: section type
321  * @section: pointer to section
322  * @index: index of the boost TCAM entry to be returned
323  * @offset: pointer to receive absolute offset, always 0 for boost TCAM sections
324  *
325  * This is a callback function that can be passed to ice_pkg_enum_entry.
326  * Handles enumeration of individual boost TCAM entries.
327  */
328 static void *
329 ice_boost_tcam_handler(u32 sect_type, void *section, u32 index, u32 *offset)
330 {
331         struct ice_boost_tcam_section *boost;
332
333         if (!section)
334                 return NULL;
335
336         if (sect_type != ICE_SID_RXPARSER_BOOST_TCAM)
337                 return NULL;
338
339         if (index > ICE_MAX_BST_TCAMS_IN_BUF)
340                 return NULL;
341
342         if (offset)
343                 *offset = 0;
344
345         boost = (struct ice_boost_tcam_section *)section;
346         if (index >= LE16_TO_CPU(boost->count))
347                 return NULL;
348
349         return boost->tcam + index;
350 }
351
352 /**
353  * ice_find_boost_entry
354  * @ice_seg: pointer to the ice segment (non-NULL)
355  * @addr: Boost TCAM address of entry to search for
356  * @entry: returns pointer to the entry
357  *
358  * Finds a particular Boost TCAM entry and returns a pointer to that entry
359  * if it is found. The ice_seg parameter must not be NULL since the first call
360  * to ice_pkg_enum_entry requires a pointer to an actual ice_segment structure.
361  */
362 static enum ice_status
363 ice_find_boost_entry(struct ice_seg *ice_seg, u16 addr,
364                      struct ice_boost_tcam_entry **entry)
365 {
366         struct ice_boost_tcam_entry *tcam;
367         struct ice_pkg_enum state;
368
369         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
370
371         if (!ice_seg)
372                 return ICE_ERR_PARAM;
373
374         do {
375                 tcam = (struct ice_boost_tcam_entry *)
376                        ice_pkg_enum_entry(ice_seg, &state,
377                                           ICE_SID_RXPARSER_BOOST_TCAM, NULL,
378                                           ice_boost_tcam_handler);
379                 if (tcam && LE16_TO_CPU(tcam->addr) == addr) {
380                         *entry = tcam;
381                         return ICE_SUCCESS;
382                 }
383
384                 ice_seg = NULL;
385         } while (tcam);
386
387         *entry = NULL;
388         return ICE_ERR_CFG;
389 }
390
391 /**
392  * ice_label_enum_handler
393  * @sect_type: section type
394  * @section: pointer to section
395  * @index: index of the label entry to be returned
396  * @offset: pointer to receive absolute offset, always zero for label sections
397  *
398  * This is a callback function that can be passed to ice_pkg_enum_entry.
399  * Handles enumeration of individual label entries.
400  */
401 static void *
402 ice_label_enum_handler(u32 __ALWAYS_UNUSED sect_type, void *section, u32 index,
403                        u32 *offset)
404 {
405         struct ice_label_section *labels;
406
407         if (!section)
408                 return NULL;
409
410         if (index > ICE_MAX_LABELS_IN_BUF)
411                 return NULL;
412
413         if (offset)
414                 *offset = 0;
415
416         labels = (struct ice_label_section *)section;
417         if (index >= LE16_TO_CPU(labels->count))
418                 return NULL;
419
420         return labels->label + index;
421 }
422
423 /**
424  * ice_enum_labels
425  * @ice_seg: pointer to the ice segment (NULL on subsequent calls)
426  * @type: the section type that will contain the label (0 on subsequent calls)
427  * @state: ice_pkg_enum structure that will hold the state of the enumeration
428  * @value: pointer to a value that will return the label's value if found
429  *
430  * Enumerates a list of labels in the package. The caller will call
431  * ice_enum_labels(ice_seg, type, ...) to start the enumeration, then call
432  * ice_enum_labels(NULL, 0, ...) to continue. When the function returns a NULL
433  * the end of the list has been reached.
434  */
435 static char *
436 ice_enum_labels(struct ice_seg *ice_seg, u32 type, struct ice_pkg_enum *state,
437                 u16 *value)
438 {
439         struct ice_label *label;
440
441         /* Check for valid label section on first call */
442         if (type && !(type >= ICE_SID_LBL_FIRST && type <= ICE_SID_LBL_LAST))
443                 return NULL;
444
445         label = (struct ice_label *)ice_pkg_enum_entry(ice_seg, state, type,
446                                                        NULL,
447                                                        ice_label_enum_handler);
448         if (!label)
449                 return NULL;
450
451         *value = LE16_TO_CPU(label->value);
452         return label->name;
453 }
454
455 /**
456  * ice_init_pkg_hints
457  * @hw: pointer to the HW structure
458  * @ice_seg: pointer to the segment of the package scan (non-NULL)
459  *
460  * This function will scan the package and save off relevant information
461  * (hints or metadata) for driver use. The ice_seg parameter must not be NULL
462  * since the first call to ice_enum_labels requires a pointer to an actual
463  * ice_seg structure.
464  */
465 static void ice_init_pkg_hints(struct ice_hw *hw, struct ice_seg *ice_seg)
466 {
467         struct ice_pkg_enum state;
468         char *label_name;
469         u16 val;
470         int i;
471
472         ice_memset(&hw->tnl, 0, sizeof(hw->tnl), ICE_NONDMA_MEM);
473         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
474
475         if (!ice_seg)
476                 return;
477
478         label_name = ice_enum_labels(ice_seg, ICE_SID_LBL_RXPARSER_TMEM, &state,
479                                      &val);
480
481         while (label_name && hw->tnl.count < ICE_TUNNEL_MAX_ENTRIES) {
482                 for (i = 0; tnls[i].type != TNL_LAST; i++) {
483                         size_t len = strlen(tnls[i].label_prefix);
484
485                         /* Look for matching label start, before continuing */
486                         if (strncmp(label_name, tnls[i].label_prefix, len))
487                                 continue;
488
489                         /* Make sure this label matches our PF. Note that the PF
490                          * character ('0' - '7') will be located where our
491                          * prefix string's null terminator is located.
492                          */
493                         if ((label_name[len] - '0') == hw->pf_id) {
494                                 hw->tnl.tbl[hw->tnl.count].type = tnls[i].type;
495                                 hw->tnl.tbl[hw->tnl.count].valid = false;
496                                 hw->tnl.tbl[hw->tnl.count].in_use = false;
497                                 hw->tnl.tbl[hw->tnl.count].marked = false;
498                                 hw->tnl.tbl[hw->tnl.count].boost_addr = val;
499                                 hw->tnl.tbl[hw->tnl.count].port = 0;
500                                 hw->tnl.count++;
501                                 break;
502                         }
503                 }
504
505                 label_name = ice_enum_labels(NULL, 0, &state, &val);
506         }
507
508         /* Cache the appropriate boost TCAM entry pointers */
509         for (i = 0; i < hw->tnl.count; i++) {
510                 ice_find_boost_entry(ice_seg, hw->tnl.tbl[i].boost_addr,
511                                      &hw->tnl.tbl[i].boost_entry);
512                 if (hw->tnl.tbl[i].boost_entry)
513                         hw->tnl.tbl[i].valid = true;
514         }
515 }
516
517 /* Key creation */
518
519 #define ICE_DC_KEY      0x1     /* don't care */
520 #define ICE_DC_KEYINV   0x1
521 #define ICE_NM_KEY      0x0     /* never match */
522 #define ICE_NM_KEYINV   0x0
523 #define ICE_0_KEY       0x1     /* match 0 */
524 #define ICE_0_KEYINV    0x0
525 #define ICE_1_KEY       0x0     /* match 1 */
526 #define ICE_1_KEYINV    0x1
527
528 /**
529  * ice_gen_key_word - generate 16-bits of a key/mask word
530  * @val: the value
531  * @valid: valid bits mask (change only the valid bits)
532  * @dont_care: don't care mask
533  * @nvr_mtch: never match mask
534  * @key: pointer to an array of where the resulting key portion
535  * @key_inv: pointer to an array of where the resulting key invert portion
536  *
537  * This function generates 16-bits from a 8-bit value, an 8-bit don't care mask
538  * and an 8-bit never match mask. The 16-bits of output are divided into 8 bits
539  * of key and 8 bits of key invert.
540  *
541  *     '0' =    b01, always match a 0 bit
542  *     '1' =    b10, always match a 1 bit
543  *     '?' =    b11, don't care bit (always matches)
544  *     '~' =    b00, never match bit
545  *
546  * Input:
547  *          val:         b0  1  0  1  0  1
548  *          dont_care:   b0  0  1  1  0  0
549  *          never_mtch:  b0  0  0  0  1  1
550  *          ------------------------------
551  * Result:  key:        b01 10 11 11 00 00
552  */
553 static enum ice_status
554 ice_gen_key_word(u8 val, u8 valid, u8 dont_care, u8 nvr_mtch, u8 *key,
555                  u8 *key_inv)
556 {
557         u8 in_key = *key, in_key_inv = *key_inv;
558         u8 i;
559
560         /* 'dont_care' and 'nvr_mtch' masks cannot overlap */
561         if ((dont_care ^ nvr_mtch) != (dont_care | nvr_mtch))
562                 return ICE_ERR_CFG;
563
564         *key = 0;
565         *key_inv = 0;
566
567         /* encode the 8 bits into 8-bit key and 8-bit key invert */
568         for (i = 0; i < 8; i++) {
569                 *key >>= 1;
570                 *key_inv >>= 1;
571
572                 if (!(valid & 0x1)) { /* change only valid bits */
573                         *key |= (in_key & 0x1) << 7;
574                         *key_inv |= (in_key_inv & 0x1) << 7;
575                 } else if (dont_care & 0x1) { /* don't care bit */
576                         *key |= ICE_DC_KEY << 7;
577                         *key_inv |= ICE_DC_KEYINV << 7;
578                 } else if (nvr_mtch & 0x1) { /* never match bit */
579                         *key |= ICE_NM_KEY << 7;
580                         *key_inv |= ICE_NM_KEYINV << 7;
581                 } else if (val & 0x01) { /* exact 1 match */
582                         *key |= ICE_1_KEY << 7;
583                         *key_inv |= ICE_1_KEYINV << 7;
584                 } else { /* exact 0 match */
585                         *key |= ICE_0_KEY << 7;
586                         *key_inv |= ICE_0_KEYINV << 7;
587                 }
588
589                 dont_care >>= 1;
590                 nvr_mtch >>= 1;
591                 valid >>= 1;
592                 val >>= 1;
593                 in_key >>= 1;
594                 in_key_inv >>= 1;
595         }
596
597         return ICE_SUCCESS;
598 }
599
600 /**
601  * ice_bits_max_set - determine if the number of bits set is within a maximum
602  * @mask: pointer to the byte array which is the mask
603  * @size: the number of bytes in the mask
604  * @max: the max number of set bits
605  *
606  * This function determines if there are at most 'max' number of bits set in an
607  * array. Returns true if the number for bits set is <= max or will return false
608  * otherwise.
609  */
610 static bool ice_bits_max_set(const u8 *mask, u16 size, u16 max)
611 {
612         u16 count = 0;
613         u16 i;
614
615         /* check each byte */
616         for (i = 0; i < size; i++) {
617                 /* if 0, go to next byte */
618                 if (!mask[i])
619                         continue;
620
621                 /* We know there is at least one set bit in this byte because of
622                  * the above check; if we already have found 'max' number of
623                  * bits set, then we can return failure now.
624                  */
625                 if (count == max)
626                         return false;
627
628                 /* count the bits in this byte, checking threshold */
629                 count += ice_hweight8(mask[i]);
630                 if (count > max)
631                         return false;
632         }
633
634         return true;
635 }
636
637 /**
638  * ice_set_key - generate a variable sized key with multiples of 16-bits
639  * @key: pointer to where the key will be stored
640  * @size: the size of the complete key in bytes (must be even)
641  * @val: array of 8-bit values that makes up the value portion of the key
642  * @upd: array of 8-bit masks that determine what key portion to update
643  * @dc: array of 8-bit masks that make up the don't care mask
644  * @nm: array of 8-bit masks that make up the never match mask
645  * @off: the offset of the first byte in the key to update
646  * @len: the number of bytes in the key update
647  *
648  * This function generates a key from a value, a don't care mask and a never
649  * match mask.
650  * upd, dc, and nm are optional parameters, and can be NULL:
651  *      upd == NULL --> upd mask is all 1's (update all bits)
652  *      dc == NULL --> dc mask is all 0's (no don't care bits)
653  *      nm == NULL --> nm mask is all 0's (no never match bits)
654  */
655 enum ice_status
656 ice_set_key(u8 *key, u16 size, u8 *val, u8 *upd, u8 *dc, u8 *nm, u16 off,
657             u16 len)
658 {
659         u16 half_size;
660         u16 i;
661
662         /* size must be a multiple of 2 bytes. */
663         if (size % 2)
664                 return ICE_ERR_CFG;
665         half_size = size / 2;
666
667         if (off + len > half_size)
668                 return ICE_ERR_CFG;
669
670         /* Make sure at most one bit is set in the never match mask. Having more
671          * than one never match mask bit set will cause HW to consume excessive
672          * power otherwise; this is a power management efficiency check.
673          */
674 #define ICE_NVR_MTCH_BITS_MAX   1
675         if (nm && !ice_bits_max_set(nm, len, ICE_NVR_MTCH_BITS_MAX))
676                 return ICE_ERR_CFG;
677
678         for (i = 0; i < len; i++)
679                 if (ice_gen_key_word(val[i], upd ? upd[i] : 0xff,
680                                      dc ? dc[i] : 0, nm ? nm[i] : 0,
681                                      key + off + i, key + half_size + off + i))
682                         return ICE_ERR_CFG;
683
684         return ICE_SUCCESS;
685 }
686
687 /**
688  * ice_acquire_global_cfg_lock
689  * @hw: pointer to the HW structure
690  * @access: access type (read or write)
691  *
692  * This function will request ownership of the global config lock for reading
693  * or writing of the package. When attempting to obtain write access, the
694  * caller must check for the following two return values:
695  *
696  * ICE_SUCCESS        - Means the caller has acquired the global config lock
697  *                      and can perform writing of the package.
698  * ICE_ERR_AQ_NO_WORK - Indicates another driver has already written the
699  *                      package or has found that no update was necessary; in
700  *                      this case, the caller can just skip performing any
701  *                      update of the package.
702  */
703 static enum ice_status
704 ice_acquire_global_cfg_lock(struct ice_hw *hw,
705                             enum ice_aq_res_access_type access)
706 {
707         enum ice_status status;
708
709         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
710
711         status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, access,
712                                  ICE_GLOBAL_CFG_LOCK_TIMEOUT);
713
714         if (status == ICE_ERR_AQ_NO_WORK)
715                 ice_debug(hw, ICE_DBG_PKG,
716                           "Global config lock: No work to do\n");
717
718         return status;
719 }
720
721 /**
722  * ice_release_global_cfg_lock
723  * @hw: pointer to the HW structure
724  *
725  * This function will release the global config lock.
726  */
727 static void ice_release_global_cfg_lock(struct ice_hw *hw)
728 {
729         ice_release_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID);
730 }
731
732 /**
733  * ice_acquire_change_lock
734  * @hw: pointer to the HW structure
735  * @access: access type (read or write)
736  *
737  * This function will request ownership of the change lock.
738  */
739 enum ice_status
740 ice_acquire_change_lock(struct ice_hw *hw, enum ice_aq_res_access_type access)
741 {
742         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
743
744         return ice_acquire_res(hw, ICE_CHANGE_LOCK_RES_ID, access,
745                                ICE_CHANGE_LOCK_TIMEOUT);
746 }
747
748 /**
749  * ice_release_change_lock
750  * @hw: pointer to the HW structure
751  *
752  * This function will release the change lock using the proper Admin Command.
753  */
754 void ice_release_change_lock(struct ice_hw *hw)
755 {
756         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
757
758         ice_release_res(hw, ICE_CHANGE_LOCK_RES_ID);
759 }
760
761 /**
762  * ice_aq_download_pkg
763  * @hw: pointer to the hardware structure
764  * @pkg_buf: the package buffer to transfer
765  * @buf_size: the size of the package buffer
766  * @last_buf: last buffer indicator
767  * @error_offset: returns error offset
768  * @error_info: returns error information
769  * @cd: pointer to command details structure or NULL
770  *
771  * Download Package (0x0C40)
772  */
773 static enum ice_status
774 ice_aq_download_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
775                     u16 buf_size, bool last_buf, u32 *error_offset,
776                     u32 *error_info, struct ice_sq_cd *cd)
777 {
778         struct ice_aqc_download_pkg *cmd;
779         struct ice_aq_desc desc;
780         enum ice_status status;
781
782         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
783
784         if (error_offset)
785                 *error_offset = 0;
786         if (error_info)
787                 *error_info = 0;
788
789         cmd = &desc.params.download_pkg;
790         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_download_pkg);
791         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
792
793         if (last_buf)
794                 cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
795
796         status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
797         if (status == ICE_ERR_AQ_ERROR) {
798                 /* Read error from buffer only when the FW returned an error */
799                 struct ice_aqc_download_pkg_resp *resp;
800
801                 resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
802                 if (error_offset)
803                         *error_offset = LE32_TO_CPU(resp->error_offset);
804                 if (error_info)
805                         *error_info = LE32_TO_CPU(resp->error_info);
806         }
807
808         return status;
809 }
810
811 /**
812  * ice_aq_update_pkg
813  * @hw: pointer to the hardware structure
814  * @pkg_buf: the package cmd buffer
815  * @buf_size: the size of the package cmd buffer
816  * @last_buf: last buffer indicator
817  * @error_offset: returns error offset
818  * @error_info: returns error information
819  * @cd: pointer to command details structure or NULL
820  *
821  * Update Package (0x0C42)
822  */
823 static enum ice_status
824 ice_aq_update_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf, u16 buf_size,
825                   bool last_buf, u32 *error_offset, u32 *error_info,
826                   struct ice_sq_cd *cd)
827 {
828         struct ice_aqc_download_pkg *cmd;
829         struct ice_aq_desc desc;
830         enum ice_status status;
831
832         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
833
834         if (error_offset)
835                 *error_offset = 0;
836         if (error_info)
837                 *error_info = 0;
838
839         cmd = &desc.params.download_pkg;
840         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_pkg);
841         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
842
843         if (last_buf)
844                 cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
845
846         status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
847         if (status == ICE_ERR_AQ_ERROR) {
848                 /* Read error from buffer only when the FW returned an error */
849                 struct ice_aqc_download_pkg_resp *resp;
850
851                 resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
852                 if (error_offset)
853                         *error_offset = LE32_TO_CPU(resp->error_offset);
854                 if (error_info)
855                         *error_info = LE32_TO_CPU(resp->error_info);
856         }
857
858         return status;
859 }
860
861 /**
862  * ice_find_seg_in_pkg
863  * @hw: pointer to the hardware structure
864  * @seg_type: the segment type to search for (i.e., SEGMENT_TYPE_CPK)
865  * @pkg_hdr: pointer to the package header to be searched
866  *
867  * This function searches a package file for a particular segment type. On
868  * success it returns a pointer to the segment header, otherwise it will
869  * return NULL.
870  */
871 static struct ice_generic_seg_hdr *
872 ice_find_seg_in_pkg(struct ice_hw *hw, u32 seg_type,
873                     struct ice_pkg_hdr *pkg_hdr)
874 {
875         u32 i;
876
877         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
878         ice_debug(hw, ICE_DBG_PKG, "Package format version: %d.%d.%d.%d\n",
879                   pkg_hdr->pkg_format_ver.major, pkg_hdr->pkg_format_ver.minor,
880                   pkg_hdr->pkg_format_ver.update,
881                   pkg_hdr->pkg_format_ver.draft);
882
883         /* Search all package segments for the requested segment type */
884         for (i = 0; i < LE32_TO_CPU(pkg_hdr->seg_count); i++) {
885                 struct ice_generic_seg_hdr *seg;
886
887                 seg = (struct ice_generic_seg_hdr *)
888                         ((u8 *)pkg_hdr + LE32_TO_CPU(pkg_hdr->seg_offset[i]));
889
890                 if (LE32_TO_CPU(seg->seg_type) == seg_type)
891                         return seg;
892         }
893
894         return NULL;
895 }
896
897 /**
898  * ice_update_pkg
899  * @hw: pointer to the hardware structure
900  * @bufs: pointer to an array of buffers
901  * @count: the number of buffers in the array
902  *
903  * Obtains change lock and updates package.
904  */
905 enum ice_status
906 ice_update_pkg(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
907 {
908         enum ice_status status;
909         u32 offset, info, i;
910
911         status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
912         if (status)
913                 return status;
914
915         for (i = 0; i < count; i++) {
916                 struct ice_buf_hdr *bh = (struct ice_buf_hdr *)(bufs + i);
917                 bool last = ((i + 1) == count);
918
919                 status = ice_aq_update_pkg(hw, bh, LE16_TO_CPU(bh->data_end),
920                                            last, &offset, &info, NULL);
921
922                 if (status) {
923                         ice_debug(hw, ICE_DBG_PKG,
924                                   "Update pkg failed: err %d off %d inf %d\n",
925                                   status, offset, info);
926                         break;
927                 }
928         }
929
930         ice_release_change_lock(hw);
931
932         return status;
933 }
934
935 /**
936  * ice_dwnld_cfg_bufs
937  * @hw: pointer to the hardware structure
938  * @bufs: pointer to an array of buffers
939  * @count: the number of buffers in the array
940  *
941  * Obtains global config lock and downloads the package configuration buffers
942  * to the firmware. Metadata buffers are skipped, and the first metadata buffer
943  * found indicates that the rest of the buffers are all metadata buffers.
944  */
945 static enum ice_status
946 ice_dwnld_cfg_bufs(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
947 {
948         enum ice_status status;
949         struct ice_buf_hdr *bh;
950         u32 offset, info, i;
951
952         if (!bufs || !count)
953                 return ICE_ERR_PARAM;
954
955         /* If the first buffer's first section has its metadata bit set
956          * then there are no buffers to be downloaded, and the operation is
957          * considered a success.
958          */
959         bh = (struct ice_buf_hdr *)bufs;
960         if (LE32_TO_CPU(bh->section_entry[0].type) & ICE_METADATA_BUF)
961                 return ICE_SUCCESS;
962
963         /* reset pkg_dwnld_status in case this function is called in the
964          * reset/rebuild flow
965          */
966         hw->pkg_dwnld_status = ICE_AQ_RC_OK;
967
968         status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
969         if (status) {
970                 if (status == ICE_ERR_AQ_NO_WORK)
971                         hw->pkg_dwnld_status = ICE_AQ_RC_EEXIST;
972                 else
973                         hw->pkg_dwnld_status = hw->adminq.sq_last_status;
974                 return status;
975         }
976
977         for (i = 0; i < count; i++) {
978                 bool last = ((i + 1) == count);
979
980                 if (!last) {
981                         /* check next buffer for metadata flag */
982                         bh = (struct ice_buf_hdr *)(bufs + i + 1);
983
984                         /* A set metadata flag in the next buffer will signal
985                          * that the current buffer will be the last buffer
986                          * downloaded
987                          */
988                         if (LE16_TO_CPU(bh->section_count))
989                                 if (LE32_TO_CPU(bh->section_entry[0].type) &
990                                     ICE_METADATA_BUF)
991                                         last = true;
992                 }
993
994                 bh = (struct ice_buf_hdr *)(bufs + i);
995
996                 status = ice_aq_download_pkg(hw, bh, ICE_PKG_BUF_SIZE, last,
997                                              &offset, &info, NULL);
998
999                 /* Save AQ status from download package */
1000                 hw->pkg_dwnld_status = hw->adminq.sq_last_status;
1001                 if (status) {
1002                         ice_debug(hw, ICE_DBG_PKG,
1003                                   "Pkg download failed: err %d off %d inf %d\n",
1004                                   status, offset, info);
1005                         break;
1006                 }
1007
1008                 if (last)
1009                         break;
1010         }
1011
1012         ice_release_global_cfg_lock(hw);
1013
1014         return status;
1015 }
1016
1017 /**
1018  * ice_aq_get_pkg_info_list
1019  * @hw: pointer to the hardware structure
1020  * @pkg_info: the buffer which will receive the information list
1021  * @buf_size: the size of the pkg_info information buffer
1022  * @cd: pointer to command details structure or NULL
1023  *
1024  * Get Package Info List (0x0C43)
1025  */
1026 static enum ice_status
1027 ice_aq_get_pkg_info_list(struct ice_hw *hw,
1028                          struct ice_aqc_get_pkg_info_resp *pkg_info,
1029                          u16 buf_size, struct ice_sq_cd *cd)
1030 {
1031         struct ice_aq_desc desc;
1032
1033         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1034         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_pkg_info_list);
1035
1036         return ice_aq_send_cmd(hw, &desc, pkg_info, buf_size, cd);
1037 }
1038
1039 /**
1040  * ice_download_pkg
1041  * @hw: pointer to the hardware structure
1042  * @ice_seg: pointer to the segment of the package to be downloaded
1043  *
1044  * Handles the download of a complete package.
1045  */
1046 static enum ice_status
1047 ice_download_pkg(struct ice_hw *hw, struct ice_seg *ice_seg)
1048 {
1049         struct ice_buf_table *ice_buf_tbl;
1050
1051         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1052         ice_debug(hw, ICE_DBG_PKG, "Segment format version: %d.%d.%d.%d\n",
1053                   ice_seg->hdr.seg_format_ver.major,
1054                   ice_seg->hdr.seg_format_ver.minor,
1055                   ice_seg->hdr.seg_format_ver.update,
1056                   ice_seg->hdr.seg_format_ver.draft);
1057
1058         ice_debug(hw, ICE_DBG_PKG, "Seg: type 0x%X, size %d, name %s\n",
1059                   LE32_TO_CPU(ice_seg->hdr.seg_type),
1060                   LE32_TO_CPU(ice_seg->hdr.seg_size), ice_seg->hdr.seg_id);
1061
1062         ice_buf_tbl = ice_find_buf_table(ice_seg);
1063
1064         ice_debug(hw, ICE_DBG_PKG, "Seg buf count: %d\n",
1065                   LE32_TO_CPU(ice_buf_tbl->buf_count));
1066
1067         return ice_dwnld_cfg_bufs(hw, ice_buf_tbl->buf_array,
1068                                   LE32_TO_CPU(ice_buf_tbl->buf_count));
1069 }
1070
1071 /**
1072  * ice_init_pkg_info
1073  * @hw: pointer to the hardware structure
1074  * @pkg_hdr: pointer to the driver's package hdr
1075  *
1076  * Saves off the package details into the HW structure.
1077  */
1078 static enum ice_status
1079 ice_init_pkg_info(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1080 {
1081         struct ice_global_metadata_seg *meta_seg;
1082         struct ice_generic_seg_hdr *seg_hdr;
1083
1084         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1085         if (!pkg_hdr)
1086                 return ICE_ERR_PARAM;
1087
1088         meta_seg = (struct ice_global_metadata_seg *)
1089                    ice_find_seg_in_pkg(hw, SEGMENT_TYPE_METADATA, pkg_hdr);
1090         if (meta_seg) {
1091                 hw->pkg_ver = meta_seg->pkg_ver;
1092                 ice_memcpy(hw->pkg_name, meta_seg->pkg_name,
1093                            sizeof(hw->pkg_name), ICE_NONDMA_TO_NONDMA);
1094
1095                 ice_debug(hw, ICE_DBG_PKG, "Pkg: %d.%d.%d.%d, %s\n",
1096                           meta_seg->pkg_ver.major, meta_seg->pkg_ver.minor,
1097                           meta_seg->pkg_ver.update, meta_seg->pkg_ver.draft,
1098                           meta_seg->pkg_name);
1099         } else {
1100                 ice_debug(hw, ICE_DBG_INIT,
1101                           "Did not find metadata segment in driver package\n");
1102                 return ICE_ERR_CFG;
1103         }
1104
1105         seg_hdr = ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE, pkg_hdr);
1106         if (seg_hdr) {
1107                 hw->ice_pkg_ver = seg_hdr->seg_format_ver;
1108                 ice_memcpy(hw->ice_pkg_name, seg_hdr->seg_id,
1109                            sizeof(hw->ice_pkg_name), ICE_NONDMA_TO_NONDMA);
1110
1111                 ice_debug(hw, ICE_DBG_PKG, "Ice Seg: %d.%d.%d.%d, %s\n",
1112                           seg_hdr->seg_format_ver.major,
1113                           seg_hdr->seg_format_ver.minor,
1114                           seg_hdr->seg_format_ver.update,
1115                           seg_hdr->seg_format_ver.draft,
1116                           seg_hdr->seg_id);
1117         } else {
1118                 ice_debug(hw, ICE_DBG_INIT,
1119                           "Did not find ice segment in driver package\n");
1120                 return ICE_ERR_CFG;
1121         }
1122
1123         return ICE_SUCCESS;
1124 }
1125
1126 /**
1127  * ice_get_pkg_info
1128  * @hw: pointer to the hardware structure
1129  *
1130  * Store details of the package currently loaded in HW into the HW structure.
1131  */
1132 static enum ice_status ice_get_pkg_info(struct ice_hw *hw)
1133 {
1134         struct ice_aqc_get_pkg_info_resp *pkg_info;
1135         enum ice_status status;
1136         u16 size;
1137         u32 i;
1138
1139         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1140
1141         size = ice_struct_size(pkg_info, pkg_info, ICE_PKG_CNT);
1142         pkg_info = (struct ice_aqc_get_pkg_info_resp *)ice_malloc(hw, size);
1143         if (!pkg_info)
1144                 return ICE_ERR_NO_MEMORY;
1145
1146         status = ice_aq_get_pkg_info_list(hw, pkg_info, size, NULL);
1147         if (status)
1148                 goto init_pkg_free_alloc;
1149
1150         for (i = 0; i < LE32_TO_CPU(pkg_info->count); i++) {
1151 #define ICE_PKG_FLAG_COUNT      4
1152                 char flags[ICE_PKG_FLAG_COUNT + 1] = { 0 };
1153                 u8 place = 0;
1154
1155                 if (pkg_info->pkg_info[i].is_active) {
1156                         flags[place++] = 'A';
1157                         hw->active_pkg_ver = pkg_info->pkg_info[i].ver;
1158                         hw->active_track_id =
1159                                 LE32_TO_CPU(pkg_info->pkg_info[i].track_id);
1160                         ice_memcpy(hw->active_pkg_name,
1161                                    pkg_info->pkg_info[i].name,
1162                                    sizeof(pkg_info->pkg_info[i].name),
1163                                    ICE_NONDMA_TO_NONDMA);
1164                         hw->active_pkg_in_nvm = pkg_info->pkg_info[i].is_in_nvm;
1165                 }
1166                 if (pkg_info->pkg_info[i].is_active_at_boot)
1167                         flags[place++] = 'B';
1168                 if (pkg_info->pkg_info[i].is_modified)
1169                         flags[place++] = 'M';
1170                 if (pkg_info->pkg_info[i].is_in_nvm)
1171                         flags[place++] = 'N';
1172
1173                 ice_debug(hw, ICE_DBG_PKG, "Pkg[%d]: %d.%d.%d.%d,%s,%s\n",
1174                           i, pkg_info->pkg_info[i].ver.major,
1175                           pkg_info->pkg_info[i].ver.minor,
1176                           pkg_info->pkg_info[i].ver.update,
1177                           pkg_info->pkg_info[i].ver.draft,
1178                           pkg_info->pkg_info[i].name, flags);
1179         }
1180
1181 init_pkg_free_alloc:
1182         ice_free(hw, pkg_info);
1183
1184         return status;
1185 }
1186
1187 /**
1188  * ice_verify_pkg - verify package
1189  * @pkg: pointer to the package buffer
1190  * @len: size of the package buffer
1191  *
1192  * Verifies various attributes of the package file, including length, format
1193  * version, and the requirement of at least one segment.
1194  */
1195 static enum ice_status ice_verify_pkg(struct ice_pkg_hdr *pkg, u32 len)
1196 {
1197         u32 seg_count;
1198         u32 i;
1199
1200         if (len < ice_struct_size(pkg, seg_offset, 1))
1201                 return ICE_ERR_BUF_TOO_SHORT;
1202
1203         if (pkg->pkg_format_ver.major != ICE_PKG_FMT_VER_MAJ ||
1204             pkg->pkg_format_ver.minor != ICE_PKG_FMT_VER_MNR ||
1205             pkg->pkg_format_ver.update != ICE_PKG_FMT_VER_UPD ||
1206             pkg->pkg_format_ver.draft != ICE_PKG_FMT_VER_DFT)
1207                 return ICE_ERR_CFG;
1208
1209         /* pkg must have at least one segment */
1210         seg_count = LE32_TO_CPU(pkg->seg_count);
1211         if (seg_count < 1)
1212                 return ICE_ERR_CFG;
1213
1214         /* make sure segment array fits in package length */
1215         if (len < ice_struct_size(pkg, seg_offset, seg_count))
1216                 return ICE_ERR_BUF_TOO_SHORT;
1217
1218         /* all segments must fit within length */
1219         for (i = 0; i < seg_count; i++) {
1220                 u32 off = LE32_TO_CPU(pkg->seg_offset[i]);
1221                 struct ice_generic_seg_hdr *seg;
1222
1223                 /* segment header must fit */
1224                 if (len < off + sizeof(*seg))
1225                         return ICE_ERR_BUF_TOO_SHORT;
1226
1227                 seg = (struct ice_generic_seg_hdr *)((u8 *)pkg + off);
1228
1229                 /* segment body must fit */
1230                 if (len < off + LE32_TO_CPU(seg->seg_size))
1231                         return ICE_ERR_BUF_TOO_SHORT;
1232         }
1233
1234         return ICE_SUCCESS;
1235 }
1236
1237 /**
1238  * ice_free_seg - free package segment pointer
1239  * @hw: pointer to the hardware structure
1240  *
1241  * Frees the package segment pointer in the proper manner, depending on if the
1242  * segment was allocated or just the passed in pointer was stored.
1243  */
1244 void ice_free_seg(struct ice_hw *hw)
1245 {
1246         if (hw->pkg_copy) {
1247                 ice_free(hw, hw->pkg_copy);
1248                 hw->pkg_copy = NULL;
1249                 hw->pkg_size = 0;
1250         }
1251         hw->seg = NULL;
1252 }
1253
1254 /**
1255  * ice_init_pkg_regs - initialize additional package registers
1256  * @hw: pointer to the hardware structure
1257  */
1258 static void ice_init_pkg_regs(struct ice_hw *hw)
1259 {
1260 #define ICE_SW_BLK_INP_MASK_L 0xFFFFFFFF
1261 #define ICE_SW_BLK_INP_MASK_H 0x0000FFFF
1262 #define ICE_SW_BLK_IDX  0
1263         if (hw->dcf_enabled)
1264                 return;
1265
1266         /* setup Switch block input mask, which is 48-bits in two parts */
1267         wr32(hw, GL_PREEXT_L2_PMASK0(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_L);
1268         wr32(hw, GL_PREEXT_L2_PMASK1(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_H);
1269 }
1270
1271 /**
1272  * ice_chk_pkg_version - check package version for compatibility with driver
1273  * @pkg_ver: pointer to a version structure to check
1274  *
1275  * Check to make sure that the package about to be downloaded is compatible with
1276  * the driver. To be compatible, the major and minor components of the package
1277  * version must match our ICE_PKG_SUPP_VER_MAJ and ICE_PKG_SUPP_VER_MNR
1278  * definitions.
1279  */
1280 static enum ice_status ice_chk_pkg_version(struct ice_pkg_ver *pkg_ver)
1281 {
1282         if (pkg_ver->major != ICE_PKG_SUPP_VER_MAJ ||
1283             pkg_ver->minor != ICE_PKG_SUPP_VER_MNR)
1284                 return ICE_ERR_NOT_SUPPORTED;
1285
1286         return ICE_SUCCESS;
1287 }
1288
1289 /**
1290  * ice_chk_pkg_compat
1291  * @hw: pointer to the hardware structure
1292  * @ospkg: pointer to the package hdr
1293  * @seg: pointer to the package segment hdr
1294  *
1295  * This function checks the package version compatibility with driver and NVM
1296  */
1297 static enum ice_status
1298 ice_chk_pkg_compat(struct ice_hw *hw, struct ice_pkg_hdr *ospkg,
1299                    struct ice_seg **seg)
1300 {
1301         struct ice_aqc_get_pkg_info_resp *pkg;
1302         enum ice_status status;
1303         u16 size;
1304         u32 i;
1305
1306         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1307
1308         /* Check package version compatibility */
1309         status = ice_chk_pkg_version(&hw->pkg_ver);
1310         if (status) {
1311                 ice_debug(hw, ICE_DBG_INIT, "Package version check failed.\n");
1312                 return status;
1313         }
1314
1315         /* find ICE segment in given package */
1316         *seg = (struct ice_seg *)ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE,
1317                                                      ospkg);
1318         if (!*seg) {
1319                 ice_debug(hw, ICE_DBG_INIT, "no ice segment in package.\n");
1320                 return ICE_ERR_CFG;
1321         }
1322
1323         /* Check if FW is compatible with the OS package */
1324         size = ice_struct_size(pkg, pkg_info, ICE_PKG_CNT);
1325         pkg = (struct ice_aqc_get_pkg_info_resp *)ice_malloc(hw, size);
1326         if (!pkg)
1327                 return ICE_ERR_NO_MEMORY;
1328
1329         status = ice_aq_get_pkg_info_list(hw, pkg, size, NULL);
1330         if (status)
1331                 goto fw_ddp_compat_free_alloc;
1332
1333         for (i = 0; i < LE32_TO_CPU(pkg->count); i++) {
1334                 /* loop till we find the NVM package */
1335                 if (!pkg->pkg_info[i].is_in_nvm)
1336                         continue;
1337                 if ((*seg)->hdr.seg_format_ver.major !=
1338                         pkg->pkg_info[i].ver.major ||
1339                     (*seg)->hdr.seg_format_ver.minor >
1340                         pkg->pkg_info[i].ver.minor) {
1341                         status = ICE_ERR_FW_DDP_MISMATCH;
1342                         ice_debug(hw, ICE_DBG_INIT,
1343                                   "OS package is not compatible with NVM.\n");
1344                 }
1345                 /* done processing NVM package so break */
1346                 break;
1347         }
1348 fw_ddp_compat_free_alloc:
1349         ice_free(hw, pkg);
1350         return status;
1351 }
1352
1353 /**
1354  * ice_init_pkg - initialize/download package
1355  * @hw: pointer to the hardware structure
1356  * @buf: pointer to the package buffer
1357  * @len: size of the package buffer
1358  *
1359  * This function initializes a package. The package contains HW tables
1360  * required to do packet processing. First, the function extracts package
1361  * information such as version. Then it finds the ice configuration segment
1362  * within the package; this function then saves a copy of the segment pointer
1363  * within the supplied package buffer. Next, the function will cache any hints
1364  * from the package, followed by downloading the package itself. Note, that if
1365  * a previous PF driver has already downloaded the package successfully, then
1366  * the current driver will not have to download the package again.
1367  *
1368  * The local package contents will be used to query default behavior and to
1369  * update specific sections of the HW's version of the package (e.g. to update
1370  * the parse graph to understand new protocols).
1371  *
1372  * This function stores a pointer to the package buffer memory, and it is
1373  * expected that the supplied buffer will not be freed immediately. If the
1374  * package buffer needs to be freed, such as when read from a file, use
1375  * ice_copy_and_init_pkg() instead of directly calling ice_init_pkg() in this
1376  * case.
1377  */
1378 enum ice_status ice_init_pkg(struct ice_hw *hw, u8 *buf, u32 len)
1379 {
1380         struct ice_pkg_hdr *pkg;
1381         enum ice_status status;
1382         struct ice_seg *seg;
1383
1384         if (!buf || !len)
1385                 return ICE_ERR_PARAM;
1386
1387         pkg = (struct ice_pkg_hdr *)buf;
1388         status = ice_verify_pkg(pkg, len);
1389         if (status) {
1390                 ice_debug(hw, ICE_DBG_INIT, "failed to verify pkg (err: %d)\n",
1391                           status);
1392                 return status;
1393         }
1394
1395         /* initialize package info */
1396         status = ice_init_pkg_info(hw, pkg);
1397         if (status)
1398                 return status;
1399
1400         /* before downloading the package, check package version for
1401          * compatibility with driver
1402          */
1403         status = ice_chk_pkg_compat(hw, pkg, &seg);
1404         if (status)
1405                 return status;
1406
1407         /* initialize package hints and then download package */
1408         ice_init_pkg_hints(hw, seg);
1409         status = ice_download_pkg(hw, seg);
1410         if (status == ICE_ERR_AQ_NO_WORK) {
1411                 ice_debug(hw, ICE_DBG_INIT,
1412                           "package previously loaded - no work.\n");
1413                 status = ICE_SUCCESS;
1414         }
1415
1416         /* Get information on the package currently loaded in HW, then make sure
1417          * the driver is compatible with this version.
1418          */
1419         if (!status) {
1420                 status = ice_get_pkg_info(hw);
1421                 if (!status)
1422                         status = ice_chk_pkg_version(&hw->active_pkg_ver);
1423         }
1424
1425         if (!status) {
1426                 hw->seg = seg;
1427                 /* on successful package download update other required
1428                  * registers to support the package and fill HW tables
1429                  * with package content.
1430                  */
1431                 ice_init_pkg_regs(hw);
1432                 ice_fill_blk_tbls(hw);
1433         } else {
1434                 ice_debug(hw, ICE_DBG_INIT, "package load failed, %d\n",
1435                           status);
1436         }
1437
1438         return status;
1439 }
1440
1441 /**
1442  * ice_copy_and_init_pkg - initialize/download a copy of the package
1443  * @hw: pointer to the hardware structure
1444  * @buf: pointer to the package buffer
1445  * @len: size of the package buffer
1446  *
1447  * This function copies the package buffer, and then calls ice_init_pkg() to
1448  * initialize the copied package contents.
1449  *
1450  * The copying is necessary if the package buffer supplied is constant, or if
1451  * the memory may disappear shortly after calling this function.
1452  *
1453  * If the package buffer resides in the data segment and can be modified, the
1454  * caller is free to use ice_init_pkg() instead of ice_copy_and_init_pkg().
1455  *
1456  * However, if the package buffer needs to be copied first, such as when being
1457  * read from a file, the caller should use ice_copy_and_init_pkg().
1458  *
1459  * This function will first copy the package buffer, before calling
1460  * ice_init_pkg(). The caller is free to immediately destroy the original
1461  * package buffer, as the new copy will be managed by this function and
1462  * related routines.
1463  */
1464 enum ice_status ice_copy_and_init_pkg(struct ice_hw *hw, const u8 *buf, u32 len)
1465 {
1466         enum ice_status status;
1467         u8 *buf_copy;
1468
1469         if (!buf || !len)
1470                 return ICE_ERR_PARAM;
1471
1472         buf_copy = (u8 *)ice_memdup(hw, buf, len, ICE_NONDMA_TO_NONDMA);
1473
1474         status = ice_init_pkg(hw, buf_copy, len);
1475         if (status) {
1476                 /* Free the copy, since we failed to initialize the package */
1477                 ice_free(hw, buf_copy);
1478         } else {
1479                 /* Track the copied pkg so we can free it later */
1480                 hw->pkg_copy = buf_copy;
1481                 hw->pkg_size = len;
1482         }
1483
1484         return status;
1485 }
1486
1487 /**
1488  * ice_pkg_buf_alloc
1489  * @hw: pointer to the HW structure
1490  *
1491  * Allocates a package buffer and returns a pointer to the buffer header.
1492  * Note: all package contents must be in Little Endian form.
1493  */
1494 static struct ice_buf_build *ice_pkg_buf_alloc(struct ice_hw *hw)
1495 {
1496         struct ice_buf_build *bld;
1497         struct ice_buf_hdr *buf;
1498
1499         bld = (struct ice_buf_build *)ice_malloc(hw, sizeof(*bld));
1500         if (!bld)
1501                 return NULL;
1502
1503         buf = (struct ice_buf_hdr *)bld;
1504         buf->data_end = CPU_TO_LE16(offsetof(struct ice_buf_hdr,
1505                                              section_entry));
1506         return bld;
1507 }
1508
1509 /**
1510  * ice_sw_fv_handler
1511  * @sect_type: section type
1512  * @section: pointer to section
1513  * @index: index of the field vector entry to be returned
1514  * @offset: ptr to variable that receives the offset in the field vector table
1515  *
1516  * This is a callback function that can be passed to ice_pkg_enum_entry.
1517  * This function treats the given section as of type ice_sw_fv_section and
1518  * enumerates offset field. "offset" is an index into the field vector
1519  * vector table.
1520  */
1521 static void *
1522 ice_sw_fv_handler(u32 sect_type, void *section, u32 index, u32 *offset)
1523 {
1524         struct ice_sw_fv_section *fv_section =
1525                 (struct ice_sw_fv_section *)section;
1526
1527         if (!section || sect_type != ICE_SID_FLD_VEC_SW)
1528                 return NULL;
1529         if (index >= LE16_TO_CPU(fv_section->count))
1530                 return NULL;
1531         if (offset)
1532                 /* "index" passed in to this function is relative to a given
1533                  * 4k block. To get to the true index into the field vector
1534                  * table need to add the relative index to the base_offset
1535                  * field of this section
1536                  */
1537                 *offset = LE16_TO_CPU(fv_section->base_offset) + index;
1538         return fv_section->fv + index;
1539 }
1540
1541 /**
1542  * ice_get_sw_prof_type - determine switch profile type
1543  * @hw: pointer to the HW structure
1544  * @fv: pointer to the switch field vector
1545  */
1546 static enum ice_prof_type
1547 ice_get_sw_prof_type(struct ice_hw *hw, struct ice_fv *fv)
1548 {
1549         u16 i;
1550
1551         for (i = 0; i < hw->blk[ICE_BLK_SW].es.fvw; i++) {
1552                 /* UDP tunnel will have UDP_OF protocol ID and VNI offset */
1553                 if (fv->ew[i].prot_id == (u8)ICE_PROT_UDP_OF &&
1554                     fv->ew[i].off == ICE_VNI_OFFSET)
1555                         return ICE_PROF_TUN_UDP;
1556
1557                 /* GRE tunnel will have GRE protocol */
1558                 if (fv->ew[i].prot_id == (u8)ICE_PROT_GRE_OF)
1559                         return ICE_PROF_TUN_GRE;
1560
1561                 /* PPPOE tunnel will have PPPOE protocol */
1562                 if (fv->ew[i].prot_id == (u8)ICE_PROT_PPPOE)
1563                         return ICE_PROF_TUN_PPPOE;
1564         }
1565
1566         return ICE_PROF_NON_TUN;
1567 }
1568
1569 /**
1570  * ice_get_sw_fv_bitmap - Get switch field vector bitmap based on profile type
1571  * @hw: pointer to hardware structure
1572  * @req_profs: type of profiles requested
1573  * @bm: pointer to memory for returning the bitmap of field vectors
1574  */
1575 void
1576 ice_get_sw_fv_bitmap(struct ice_hw *hw, enum ice_prof_type req_profs,
1577                      ice_bitmap_t *bm)
1578 {
1579         struct ice_pkg_enum state;
1580         struct ice_seg *ice_seg;
1581         struct ice_fv *fv;
1582
1583         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1584
1585         if (req_profs == ICE_PROF_ALL) {
1586                 u16 i;
1587
1588                 for (i = 0; i < ICE_MAX_NUM_PROFILES; i++)
1589                         ice_set_bit(i, bm);
1590                 return;
1591         }
1592
1593         ice_zero_bitmap(bm, ICE_MAX_NUM_PROFILES);
1594
1595         ice_seg = hw->seg;
1596         do {
1597                 enum ice_prof_type prof_type;
1598                 u32 offset;
1599
1600                 fv = (struct ice_fv *)
1601                         ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1602                                            &offset, ice_sw_fv_handler);
1603                 ice_seg = NULL;
1604
1605                 if (fv) {
1606                         /* Determine field vector type */
1607                         prof_type = ice_get_sw_prof_type(hw, fv);
1608
1609                         if (req_profs & prof_type)
1610                                 ice_set_bit((u16)offset, bm);
1611                 }
1612         } while (fv);
1613 }
1614
1615 /**
1616  * ice_get_sw_fv_list
1617  * @hw: pointer to the HW structure
1618  * @prot_ids: field vector to search for with a given protocol ID
1619  * @ids_cnt: lookup/protocol count
1620  * @bm: bitmap of field vectors to consider
1621  * @fv_list: Head of a list
1622  *
1623  * Finds all the field vector entries from switch block that contain
1624  * a given protocol ID and returns a list of structures of type
1625  * "ice_sw_fv_list_entry". Every structure in the list has a field vector
1626  * definition and profile ID information
1627  * NOTE: The caller of the function is responsible for freeing the memory
1628  * allocated for every list entry.
1629  */
1630 enum ice_status
1631 ice_get_sw_fv_list(struct ice_hw *hw, u8 *prot_ids, u16 ids_cnt,
1632                    ice_bitmap_t *bm, struct LIST_HEAD_TYPE *fv_list)
1633 {
1634         struct ice_sw_fv_list_entry *fvl;
1635         struct ice_sw_fv_list_entry *tmp;
1636         struct ice_pkg_enum state;
1637         struct ice_seg *ice_seg;
1638         struct ice_fv *fv;
1639         u32 offset;
1640
1641         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1642
1643         if (!ids_cnt || !hw->seg)
1644                 return ICE_ERR_PARAM;
1645
1646         ice_seg = hw->seg;
1647         do {
1648                 u16 i;
1649
1650                 fv = (struct ice_fv *)
1651                         ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1652                                            &offset, ice_sw_fv_handler);
1653                 if (!fv)
1654                         break;
1655                 ice_seg = NULL;
1656
1657                 /* If field vector is not in the bitmap list, then skip this
1658                  * profile.
1659                  */
1660                 if (!ice_is_bit_set(bm, (u16)offset))
1661                         continue;
1662
1663                 for (i = 0; i < ids_cnt; i++) {
1664                         int j;
1665
1666                         /* This code assumes that if a switch field vector line
1667                          * has a matching protocol, then this line will contain
1668                          * the entries necessary to represent every field in
1669                          * that protocol header.
1670                          */
1671                         for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
1672                                 if (fv->ew[j].prot_id == prot_ids[i])
1673                                         break;
1674                         if (j >= hw->blk[ICE_BLK_SW].es.fvw)
1675                                 break;
1676                         if (i + 1 == ids_cnt) {
1677                                 fvl = (struct ice_sw_fv_list_entry *)
1678                                         ice_malloc(hw, sizeof(*fvl));
1679                                 if (!fvl)
1680                                         goto err;
1681                                 fvl->fv_ptr = fv;
1682                                 fvl->profile_id = offset;
1683                                 LIST_ADD(&fvl->list_entry, fv_list);
1684                                 break;
1685                         }
1686                 }
1687         } while (fv);
1688         if (LIST_EMPTY(fv_list))
1689                 return ICE_ERR_CFG;
1690         return ICE_SUCCESS;
1691
1692 err:
1693         LIST_FOR_EACH_ENTRY_SAFE(fvl, tmp, fv_list, ice_sw_fv_list_entry,
1694                                  list_entry) {
1695                 LIST_DEL(&fvl->list_entry);
1696                 ice_free(hw, fvl);
1697         }
1698
1699         return ICE_ERR_NO_MEMORY;
1700 }
1701
1702 /**
1703  * ice_init_prof_result_bm - Initialize the profile result index bitmap
1704  * @hw: pointer to hardware structure
1705  */
1706 void ice_init_prof_result_bm(struct ice_hw *hw)
1707 {
1708         struct ice_pkg_enum state;
1709         struct ice_seg *ice_seg;
1710         struct ice_fv *fv;
1711
1712         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1713
1714         if (!hw->seg)
1715                 return;
1716
1717         ice_seg = hw->seg;
1718         do {
1719                 u32 off;
1720                 u16 i;
1721
1722                 fv = (struct ice_fv *)
1723                         ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1724                                            &off, ice_sw_fv_handler);
1725                 ice_seg = NULL;
1726                 if (!fv)
1727                         break;
1728
1729                 ice_zero_bitmap(hw->switch_info->prof_res_bm[off],
1730                                 ICE_MAX_FV_WORDS);
1731
1732                 /* Determine empty field vector indices, these can be
1733                  * used for recipe results. Skip index 0, since it is
1734                  * always used for Switch ID.
1735                  */
1736                 for (i = 1; i < ICE_MAX_FV_WORDS; i++)
1737                         if (fv->ew[i].prot_id == ICE_PROT_INVALID &&
1738                             fv->ew[i].off == ICE_FV_OFFSET_INVAL)
1739                                 ice_set_bit(i,
1740                                             hw->switch_info->prof_res_bm[off]);
1741         } while (fv);
1742 }
1743
1744 /**
1745  * ice_pkg_buf_free
1746  * @hw: pointer to the HW structure
1747  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1748  *
1749  * Frees a package buffer
1750  */
1751 static void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
1752 {
1753         ice_free(hw, bld);
1754 }
1755
1756 /**
1757  * ice_pkg_buf_reserve_section
1758  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1759  * @count: the number of sections to reserve
1760  *
1761  * Reserves one or more section table entries in a package buffer. This routine
1762  * can be called multiple times as long as they are made before calling
1763  * ice_pkg_buf_alloc_section(). Once ice_pkg_buf_alloc_section()
1764  * is called once, the number of sections that can be allocated will not be able
1765  * to be increased; not using all reserved sections is fine, but this will
1766  * result in some wasted space in the buffer.
1767  * Note: all package contents must be in Little Endian form.
1768  */
1769 static enum ice_status
1770 ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
1771 {
1772         struct ice_buf_hdr *buf;
1773         u16 section_count;
1774         u16 data_end;
1775
1776         if (!bld)
1777                 return ICE_ERR_PARAM;
1778
1779         buf = (struct ice_buf_hdr *)&bld->buf;
1780
1781         /* already an active section, can't increase table size */
1782         section_count = LE16_TO_CPU(buf->section_count);
1783         if (section_count > 0)
1784                 return ICE_ERR_CFG;
1785
1786         if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
1787                 return ICE_ERR_CFG;
1788         bld->reserved_section_table_entries += count;
1789
1790         data_end = LE16_TO_CPU(buf->data_end) +
1791                    (count * sizeof(buf->section_entry[0]));
1792         buf->data_end = CPU_TO_LE16(data_end);
1793
1794         return ICE_SUCCESS;
1795 }
1796
1797 /**
1798  * ice_pkg_buf_alloc_section
1799  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1800  * @type: the section type value
1801  * @size: the size of the section to reserve (in bytes)
1802  *
1803  * Reserves memory in the buffer for a section's content and updates the
1804  * buffers' status accordingly. This routine returns a pointer to the first
1805  * byte of the section start within the buffer, which is used to fill in the
1806  * section contents.
1807  * Note: all package contents must be in Little Endian form.
1808  */
1809 static void *
1810 ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
1811 {
1812         struct ice_buf_hdr *buf;
1813         u16 sect_count;
1814         u16 data_end;
1815
1816         if (!bld || !type || !size)
1817                 return NULL;
1818
1819         buf = (struct ice_buf_hdr *)&bld->buf;
1820
1821         /* check for enough space left in buffer */
1822         data_end = LE16_TO_CPU(buf->data_end);
1823
1824         /* section start must align on 4 byte boundary */
1825         data_end = ICE_ALIGN(data_end, 4);
1826
1827         if ((data_end + size) > ICE_MAX_S_DATA_END)
1828                 return NULL;
1829
1830         /* check for more available section table entries */
1831         sect_count = LE16_TO_CPU(buf->section_count);
1832         if (sect_count < bld->reserved_section_table_entries) {
1833                 void *section_ptr = ((u8 *)buf) + data_end;
1834
1835                 buf->section_entry[sect_count].offset = CPU_TO_LE16(data_end);
1836                 buf->section_entry[sect_count].size = CPU_TO_LE16(size);
1837                 buf->section_entry[sect_count].type = CPU_TO_LE32(type);
1838
1839                 data_end += size;
1840                 buf->data_end = CPU_TO_LE16(data_end);
1841
1842                 buf->section_count = CPU_TO_LE16(sect_count + 1);
1843                 return section_ptr;
1844         }
1845
1846         /* no free section table entries */
1847         return NULL;
1848 }
1849
1850 /**
1851  * ice_pkg_buf_get_active_sections
1852  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1853  *
1854  * Returns the number of active sections. Before using the package buffer
1855  * in an update package command, the caller should make sure that there is at
1856  * least one active section - otherwise, the buffer is not legal and should
1857  * not be used.
1858  * Note: all package contents must be in Little Endian form.
1859  */
1860 static u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
1861 {
1862         struct ice_buf_hdr *buf;
1863
1864         if (!bld)
1865                 return 0;
1866
1867         buf = (struct ice_buf_hdr *)&bld->buf;
1868         return LE16_TO_CPU(buf->section_count);
1869 }
1870
1871 /**
1872  * ice_pkg_buf
1873  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1874  *
1875  * Return a pointer to the buffer's header
1876  */
1877 static struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
1878 {
1879         if (!bld)
1880                 return NULL;
1881
1882         return &bld->buf;
1883 }
1884
1885 /**
1886  * ice_tunnel_port_in_use_hlpr - helper function to determine tunnel usage
1887  * @hw: pointer to the HW structure
1888  * @port: port to search for
1889  * @index: optionally returns index
1890  *
1891  * Returns whether a port is already in use as a tunnel, and optionally its
1892  * index
1893  */
1894 static bool ice_tunnel_port_in_use_hlpr(struct ice_hw *hw, u16 port, u16 *index)
1895 {
1896         u16 i;
1897
1898         for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1899                 if (hw->tnl.tbl[i].in_use && hw->tnl.tbl[i].port == port) {
1900                         if (index)
1901                                 *index = i;
1902                         return true;
1903                 }
1904
1905         return false;
1906 }
1907
1908 /**
1909  * ice_tunnel_port_in_use
1910  * @hw: pointer to the HW structure
1911  * @port: port to search for
1912  * @index: optionally returns index
1913  *
1914  * Returns whether a port is already in use as a tunnel, and optionally its
1915  * index
1916  */
1917 bool ice_tunnel_port_in_use(struct ice_hw *hw, u16 port, u16 *index)
1918 {
1919         bool res;
1920
1921         ice_acquire_lock(&hw->tnl_lock);
1922         res = ice_tunnel_port_in_use_hlpr(hw, port, index);
1923         ice_release_lock(&hw->tnl_lock);
1924
1925         return res;
1926 }
1927
1928 /**
1929  * ice_tunnel_get_type
1930  * @hw: pointer to the HW structure
1931  * @port: port to search for
1932  * @type: returns tunnel index
1933  *
1934  * For a given port number, will return the type of tunnel.
1935  */
1936 bool
1937 ice_tunnel_get_type(struct ice_hw *hw, u16 port, enum ice_tunnel_type *type)
1938 {
1939         bool res = false;
1940         u16 i;
1941
1942         ice_acquire_lock(&hw->tnl_lock);
1943
1944         for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1945                 if (hw->tnl.tbl[i].in_use && hw->tnl.tbl[i].port == port) {
1946                         *type = hw->tnl.tbl[i].type;
1947                         res = true;
1948                         break;
1949                 }
1950
1951         ice_release_lock(&hw->tnl_lock);
1952
1953         return res;
1954 }
1955
1956 /**
1957  * ice_find_free_tunnel_entry
1958  * @hw: pointer to the HW structure
1959  * @type: tunnel type
1960  * @index: optionally returns index
1961  *
1962  * Returns whether there is a free tunnel entry, and optionally its index
1963  */
1964 static bool
1965 ice_find_free_tunnel_entry(struct ice_hw *hw, enum ice_tunnel_type type,
1966                            u16 *index)
1967 {
1968         u16 i;
1969
1970         for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1971                 if (hw->tnl.tbl[i].valid && !hw->tnl.tbl[i].in_use &&
1972                     hw->tnl.tbl[i].type == type) {
1973                         if (index)
1974                                 *index = i;
1975                         return true;
1976                 }
1977
1978         return false;
1979 }
1980
1981 /**
1982  * ice_get_open_tunnel_port - retrieve an open tunnel port
1983  * @hw: pointer to the HW structure
1984  * @type: tunnel type (TNL_ALL will return any open port)
1985  * @port: returns open port
1986  */
1987 bool
1988 ice_get_open_tunnel_port(struct ice_hw *hw, enum ice_tunnel_type type,
1989                          u16 *port)
1990 {
1991         bool res = false;
1992         u16 i;
1993
1994         ice_acquire_lock(&hw->tnl_lock);
1995
1996         for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1997                 if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
1998                     (type == TNL_ALL || hw->tnl.tbl[i].type == type)) {
1999                         *port = hw->tnl.tbl[i].port;
2000                         res = true;
2001                         break;
2002                 }
2003
2004         ice_release_lock(&hw->tnl_lock);
2005
2006         return res;
2007 }
2008
2009 /**
2010  * ice_create_tunnel
2011  * @hw: pointer to the HW structure
2012  * @type: type of tunnel
2013  * @port: port of tunnel to create
2014  *
2015  * Create a tunnel by updating the parse graph in the parser. We do that by
2016  * creating a package buffer with the tunnel info and issuing an update package
2017  * command.
2018  */
2019 enum ice_status
2020 ice_create_tunnel(struct ice_hw *hw, enum ice_tunnel_type type, u16 port)
2021 {
2022         struct ice_boost_tcam_section *sect_rx, *sect_tx;
2023         enum ice_status status = ICE_ERR_MAX_LIMIT;
2024         struct ice_buf_build *bld;
2025         u16 index;
2026
2027         ice_acquire_lock(&hw->tnl_lock);
2028
2029         if (ice_tunnel_port_in_use_hlpr(hw, port, &index)) {
2030                 hw->tnl.tbl[index].ref++;
2031                 status = ICE_SUCCESS;
2032                 goto ice_create_tunnel_end;
2033         }
2034
2035         if (!ice_find_free_tunnel_entry(hw, type, &index)) {
2036                 status = ICE_ERR_OUT_OF_RANGE;
2037                 goto ice_create_tunnel_end;
2038         }
2039
2040         bld = ice_pkg_buf_alloc(hw);
2041         if (!bld) {
2042                 status = ICE_ERR_NO_MEMORY;
2043                 goto ice_create_tunnel_end;
2044         }
2045
2046         /* allocate 2 sections, one for Rx parser, one for Tx parser */
2047         if (ice_pkg_buf_reserve_section(bld, 2))
2048                 goto ice_create_tunnel_err;
2049
2050         sect_rx = (struct ice_boost_tcam_section *)
2051                 ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2052                                           ice_struct_size(sect_rx, tcam, 1));
2053         if (!sect_rx)
2054                 goto ice_create_tunnel_err;
2055         sect_rx->count = CPU_TO_LE16(1);
2056
2057         sect_tx = (struct ice_boost_tcam_section *)
2058                 ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2059                                           ice_struct_size(sect_tx, tcam, 1));
2060         if (!sect_tx)
2061                 goto ice_create_tunnel_err;
2062         sect_tx->count = CPU_TO_LE16(1);
2063
2064         /* copy original boost entry to update package buffer */
2065         ice_memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
2066                    sizeof(*sect_rx->tcam), ICE_NONDMA_TO_NONDMA);
2067
2068         /* over-write the never-match dest port key bits with the encoded port
2069          * bits
2070          */
2071         ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
2072                     (u8 *)&port, NULL, NULL, NULL,
2073                     (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key),
2074                     sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key));
2075
2076         /* exact copy of entry to Tx section entry */
2077         ice_memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam),
2078                    ICE_NONDMA_TO_NONDMA);
2079
2080         status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
2081         if (!status) {
2082                 hw->tnl.tbl[index].port = port;
2083                 hw->tnl.tbl[index].in_use = true;
2084                 hw->tnl.tbl[index].ref = 1;
2085         }
2086
2087 ice_create_tunnel_err:
2088         ice_pkg_buf_free(hw, bld);
2089
2090 ice_create_tunnel_end:
2091         ice_release_lock(&hw->tnl_lock);
2092
2093         return status;
2094 }
2095
2096 /**
2097  * ice_destroy_tunnel
2098  * @hw: pointer to the HW structure
2099  * @port: port of tunnel to destroy (ignored if the all parameter is true)
2100  * @all: flag that states to destroy all tunnels
2101  *
2102  * Destroys a tunnel or all tunnels by creating an update package buffer
2103  * targeting the specific updates requested and then performing an update
2104  * package.
2105  */
2106 enum ice_status ice_destroy_tunnel(struct ice_hw *hw, u16 port, bool all)
2107 {
2108         struct ice_boost_tcam_section *sect_rx, *sect_tx;
2109         enum ice_status status = ICE_ERR_MAX_LIMIT;
2110         struct ice_buf_build *bld;
2111         u16 count = 0;
2112         u16 index;
2113         u16 size;
2114         u16 i;
2115
2116         ice_acquire_lock(&hw->tnl_lock);
2117
2118         if (!all && ice_tunnel_port_in_use_hlpr(hw, port, &index))
2119                 if (hw->tnl.tbl[index].ref > 1) {
2120                         hw->tnl.tbl[index].ref--;
2121                         status = ICE_SUCCESS;
2122                         goto ice_destroy_tunnel_end;
2123                 }
2124
2125         /* determine count */
2126         for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2127                 if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
2128                     (all || hw->tnl.tbl[i].port == port))
2129                         count++;
2130
2131         if (!count) {
2132                 status = ICE_ERR_PARAM;
2133                 goto ice_destroy_tunnel_end;
2134         }
2135
2136         /* size of section - there is at least one entry */
2137         size = ice_struct_size(sect_rx, tcam, count);
2138
2139         bld = ice_pkg_buf_alloc(hw);
2140         if (!bld) {
2141                 status = ICE_ERR_NO_MEMORY;
2142                 goto ice_destroy_tunnel_end;
2143         }
2144
2145         /* allocate 2 sections, one for Rx parser, one for Tx parser */
2146         if (ice_pkg_buf_reserve_section(bld, 2))
2147                 goto ice_destroy_tunnel_err;
2148
2149         sect_rx = (struct ice_boost_tcam_section *)
2150                 ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2151                                           size);
2152         if (!sect_rx)
2153                 goto ice_destroy_tunnel_err;
2154         sect_rx->count = CPU_TO_LE16(1);
2155
2156         sect_tx = (struct ice_boost_tcam_section *)
2157                 ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2158                                           size);
2159         if (!sect_tx)
2160                 goto ice_destroy_tunnel_err;
2161         sect_tx->count = CPU_TO_LE16(1);
2162
2163         /* copy original boost entry to update package buffer, one copy to Rx
2164          * section, another copy to the Tx section
2165          */
2166         for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2167                 if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
2168                     (all || hw->tnl.tbl[i].port == port)) {
2169                         ice_memcpy(sect_rx->tcam + i,
2170                                    hw->tnl.tbl[i].boost_entry,
2171                                    sizeof(*sect_rx->tcam),
2172                                    ICE_NONDMA_TO_NONDMA);
2173                         ice_memcpy(sect_tx->tcam + i,
2174                                    hw->tnl.tbl[i].boost_entry,
2175                                    sizeof(*sect_tx->tcam),
2176                                    ICE_NONDMA_TO_NONDMA);
2177                         hw->tnl.tbl[i].marked = true;
2178                 }
2179
2180         status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
2181         if (!status)
2182                 for (i = 0; i < hw->tnl.count &&
2183                      i < ICE_TUNNEL_MAX_ENTRIES; i++)
2184                         if (hw->tnl.tbl[i].marked) {
2185                                 hw->tnl.tbl[i].ref = 0;
2186                                 hw->tnl.tbl[i].port = 0;
2187                                 hw->tnl.tbl[i].in_use = false;
2188                                 hw->tnl.tbl[i].marked = false;
2189                         }
2190
2191 ice_destroy_tunnel_err:
2192         ice_pkg_buf_free(hw, bld);
2193
2194 ice_destroy_tunnel_end:
2195         ice_release_lock(&hw->tnl_lock);
2196
2197         return status;
2198 }
2199
2200 /**
2201  * ice_find_prot_off - find prot ID and offset pair, based on prof and FV index
2202  * @hw: pointer to the hardware structure
2203  * @blk: hardware block
2204  * @prof: profile ID
2205  * @fv_idx: field vector word index
2206  * @prot: variable to receive the protocol ID
2207  * @off: variable to receive the protocol offset
2208  */
2209 enum ice_status
2210 ice_find_prot_off(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 fv_idx,
2211                   u8 *prot, u16 *off)
2212 {
2213         struct ice_fv_word *fv_ext;
2214
2215         if (prof >= hw->blk[blk].es.count)
2216                 return ICE_ERR_PARAM;
2217
2218         if (fv_idx >= hw->blk[blk].es.fvw)
2219                 return ICE_ERR_PARAM;
2220
2221         fv_ext = hw->blk[blk].es.t + (prof * hw->blk[blk].es.fvw);
2222
2223         *prot = fv_ext[fv_idx].prot_id;
2224         *off = fv_ext[fv_idx].off;
2225
2226         return ICE_SUCCESS;
2227 }
2228
2229 /* PTG Management */
2230
2231 /**
2232  * ice_ptg_find_ptype - Search for packet type group using packet type (ptype)
2233  * @hw: pointer to the hardware structure
2234  * @blk: HW block
2235  * @ptype: the ptype to search for
2236  * @ptg: pointer to variable that receives the PTG
2237  *
2238  * This function will search the PTGs for a particular ptype, returning the
2239  * PTG ID that contains it through the PTG parameter, with the value of
2240  * ICE_DEFAULT_PTG (0) meaning it is part the default PTG.
2241  */
2242 static enum ice_status
2243 ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg)
2244 {
2245         if (ptype >= ICE_XLT1_CNT || !ptg)
2246                 return ICE_ERR_PARAM;
2247
2248         *ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg;
2249         return ICE_SUCCESS;
2250 }
2251
2252 /**
2253  * ice_ptg_alloc_val - Allocates a new packet type group ID by value
2254  * @hw: pointer to the hardware structure
2255  * @blk: HW block
2256  * @ptg: the PTG to allocate
2257  *
2258  * This function allocates a given packet type group ID specified by the PTG
2259  * parameter.
2260  */
2261 static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg)
2262 {
2263         hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true;
2264 }
2265
2266 /**
2267  * ice_ptg_remove_ptype - Removes ptype from a particular packet type group
2268  * @hw: pointer to the hardware structure
2269  * @blk: HW block
2270  * @ptype: the ptype to remove
2271  * @ptg: the PTG to remove the ptype from
2272  *
2273  * This function will remove the ptype from the specific PTG, and move it to
2274  * the default PTG (ICE_DEFAULT_PTG).
2275  */
2276 static enum ice_status
2277 ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
2278 {
2279         struct ice_ptg_ptype **ch;
2280         struct ice_ptg_ptype *p;
2281
2282         if (ptype > ICE_XLT1_CNT - 1)
2283                 return ICE_ERR_PARAM;
2284
2285         if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use)
2286                 return ICE_ERR_DOES_NOT_EXIST;
2287
2288         /* Should not happen if .in_use is set, bad config */
2289         if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype)
2290                 return ICE_ERR_CFG;
2291
2292         /* find the ptype within this PTG, and bypass the link over it */
2293         p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2294         ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2295         while (p) {
2296                 if (ptype == (p - hw->blk[blk].xlt1.ptypes)) {
2297                         *ch = p->next_ptype;
2298                         break;
2299                 }
2300
2301                 ch = &p->next_ptype;
2302                 p = p->next_ptype;
2303         }
2304
2305         hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG;
2306         hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL;
2307
2308         return ICE_SUCCESS;
2309 }
2310
2311 /**
2312  * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group
2313  * @hw: pointer to the hardware structure
2314  * @blk: HW block
2315  * @ptype: the ptype to add or move
2316  * @ptg: the PTG to add or move the ptype to
2317  *
2318  * This function will either add or move a ptype to a particular PTG depending
2319  * on if the ptype is already part of another group. Note that using a
2320  * a destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the
2321  * default PTG.
2322  */
2323 static enum ice_status
2324 ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
2325 {
2326         enum ice_status status;
2327         u8 original_ptg;
2328
2329         if (ptype > ICE_XLT1_CNT - 1)
2330                 return ICE_ERR_PARAM;
2331
2332         if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG)
2333                 return ICE_ERR_DOES_NOT_EXIST;
2334
2335         status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg);
2336         if (status)
2337                 return status;
2338
2339         /* Is ptype already in the correct PTG? */
2340         if (original_ptg == ptg)
2341                 return ICE_SUCCESS;
2342
2343         /* Remove from original PTG and move back to the default PTG */
2344         if (original_ptg != ICE_DEFAULT_PTG)
2345                 ice_ptg_remove_ptype(hw, blk, ptype, original_ptg);
2346
2347         /* Moving to default PTG? Then we're done with this request */
2348         if (ptg == ICE_DEFAULT_PTG)
2349                 return ICE_SUCCESS;
2350
2351         /* Add ptype to PTG at beginning of list */
2352         hw->blk[blk].xlt1.ptypes[ptype].next_ptype =
2353                 hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2354         hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype =
2355                 &hw->blk[blk].xlt1.ptypes[ptype];
2356
2357         hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg;
2358         hw->blk[blk].xlt1.t[ptype] = ptg;
2359
2360         return ICE_SUCCESS;
2361 }
2362
2363 /* Block / table size info */
2364 struct ice_blk_size_details {
2365         u16 xlt1;                       /* # XLT1 entries */
2366         u16 xlt2;                       /* # XLT2 entries */
2367         u16 prof_tcam;                  /* # profile ID TCAM entries */
2368         u16 prof_id;                    /* # profile IDs */
2369         u8 prof_cdid_bits;              /* # CDID one-hot bits used in key */
2370         u16 prof_redir;                 /* # profile redirection entries */
2371         u16 es;                         /* # extraction sequence entries */
2372         u16 fvw;                        /* # field vector words */
2373         u8 overwrite;                   /* overwrite existing entries allowed */
2374         u8 reverse;                     /* reverse FV order */
2375 };
2376
2377 static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = {
2378         /**
2379          * Table Definitions
2380          * XLT1 - Number of entries in XLT1 table
2381          * XLT2 - Number of entries in XLT2 table
2382          * TCAM - Number of entries Profile ID TCAM table
2383          * CDID - Control Domain ID of the hardware block
2384          * PRED - Number of entries in the Profile Redirection Table
2385          * FV   - Number of entries in the Field Vector
2386          * FVW  - Width (in WORDs) of the Field Vector
2387          * OVR  - Overwrite existing table entries
2388          * REV  - Reverse FV
2389          */
2390         /*          XLT1        , XLT2        ,TCAM, PID,CDID,PRED,   FV, FVW */
2391         /*          Overwrite   , Reverse FV */
2392         /* SW  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256,   0,  256, 256,  48,
2393                     false, false },
2394         /* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  32,
2395                     false, false },
2396         /* FD  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2397                     false, true  },
2398         /* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2399                     true,  true  },
2400         /* PE  */ { ICE_XLT1_CNT, ICE_XLT2_CNT,  64,  32,   0,   32,  32,  24,
2401                     false, false },
2402 };
2403
2404 enum ice_sid_all {
2405         ICE_SID_XLT1_OFF = 0,
2406         ICE_SID_XLT2_OFF,
2407         ICE_SID_PR_OFF,
2408         ICE_SID_PR_REDIR_OFF,
2409         ICE_SID_ES_OFF,
2410         ICE_SID_OFF_COUNT,
2411 };
2412
2413 /* Characteristic handling */
2414
2415 /**
2416  * ice_match_prop_lst - determine if properties of two lists match
2417  * @list1: first properties list
2418  * @list2: second properties list
2419  *
2420  * Count, cookies and the order must match in order to be considered equivalent.
2421  */
2422 static bool
2423 ice_match_prop_lst(struct LIST_HEAD_TYPE *list1, struct LIST_HEAD_TYPE *list2)
2424 {
2425         struct ice_vsig_prof *tmp1;
2426         struct ice_vsig_prof *tmp2;
2427         u16 chk_count = 0;
2428         u16 count = 0;
2429
2430         /* compare counts */
2431         LIST_FOR_EACH_ENTRY(tmp1, list1, ice_vsig_prof, list)
2432                 count++;
2433         LIST_FOR_EACH_ENTRY(tmp2, list2, ice_vsig_prof, list)
2434                 chk_count++;
2435         if (!count || count != chk_count)
2436                 return false;
2437
2438         tmp1 = LIST_FIRST_ENTRY(list1, struct ice_vsig_prof, list);
2439         tmp2 = LIST_FIRST_ENTRY(list2, struct ice_vsig_prof, list);
2440
2441         /* profile cookies must compare, and in the exact same order to take
2442          * into account priority
2443          */
2444         while (count--) {
2445                 if (tmp2->profile_cookie != tmp1->profile_cookie)
2446                         return false;
2447
2448                 tmp1 = LIST_NEXT_ENTRY(tmp1, struct ice_vsig_prof, list);
2449                 tmp2 = LIST_NEXT_ENTRY(tmp2, struct ice_vsig_prof, list);
2450         }
2451
2452         return true;
2453 }
2454
2455 /* VSIG Management */
2456
2457 /**
2458  * ice_vsig_find_vsi - find a VSIG that contains a specified VSI
2459  * @hw: pointer to the hardware structure
2460  * @blk: HW block
2461  * @vsi: VSI of interest
2462  * @vsig: pointer to receive the VSI group
2463  *
2464  * This function will lookup the VSI entry in the XLT2 list and return
2465  * the VSI group its associated with.
2466  */
2467 enum ice_status
2468 ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig)
2469 {
2470         if (!vsig || vsi >= ICE_MAX_VSI)
2471                 return ICE_ERR_PARAM;
2472
2473         /* As long as there's a default or valid VSIG associated with the input
2474          * VSI, the functions returns a success. Any handling of VSIG will be
2475          * done by the following add, update or remove functions.
2476          */
2477         *vsig = hw->blk[blk].xlt2.vsis[vsi].vsig;
2478
2479         return ICE_SUCCESS;
2480 }
2481
2482 /**
2483  * ice_vsig_alloc_val - allocate a new VSIG by value
2484  * @hw: pointer to the hardware structure
2485  * @blk: HW block
2486  * @vsig: the VSIG to allocate
2487  *
2488  * This function will allocate a given VSIG specified by the VSIG parameter.
2489  */
2490 static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2491 {
2492         u16 idx = vsig & ICE_VSIG_IDX_M;
2493
2494         if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) {
2495                 INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2496                 hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true;
2497         }
2498
2499         return ICE_VSIG_VALUE(idx, hw->pf_id);
2500 }
2501
2502 /**
2503  * ice_vsig_alloc - Finds a free entry and allocates a new VSIG
2504  * @hw: pointer to the hardware structure
2505  * @blk: HW block
2506  *
2507  * This function will iterate through the VSIG list and mark the first
2508  * unused entry for the new VSIG entry as used and return that value.
2509  */
2510 static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk)
2511 {
2512         u16 i;
2513
2514         for (i = 1; i < ICE_MAX_VSIGS; i++)
2515                 if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use)
2516                         return ice_vsig_alloc_val(hw, blk, i);
2517
2518         return ICE_DEFAULT_VSIG;
2519 }
2520
2521 /**
2522  * ice_find_dup_props_vsig - find VSI group with a specified set of properties
2523  * @hw: pointer to the hardware structure
2524  * @blk: HW block
2525  * @chs: characteristic list
2526  * @vsig: returns the VSIG with the matching profiles, if found
2527  *
2528  * Each VSIG is associated with a characteristic set; i.e. all VSIs under
2529  * a group have the same characteristic set. To check if there exists a VSIG
2530  * which has the same characteristics as the input characteristics; this
2531  * function will iterate through the XLT2 list and return the VSIG that has a
2532  * matching configuration. In order to make sure that priorities are accounted
2533  * for, the list must match exactly, including the order in which the
2534  * characteristics are listed.
2535  */
2536 static enum ice_status
2537 ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk,
2538                         struct LIST_HEAD_TYPE *chs, u16 *vsig)
2539 {
2540         struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2;
2541         u16 i;
2542
2543         for (i = 0; i < xlt2->count; i++)
2544                 if (xlt2->vsig_tbl[i].in_use &&
2545                     ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) {
2546                         *vsig = ICE_VSIG_VALUE(i, hw->pf_id);
2547                         return ICE_SUCCESS;
2548                 }
2549
2550         return ICE_ERR_DOES_NOT_EXIST;
2551 }
2552
2553 /**
2554  * ice_vsig_free - free VSI group
2555  * @hw: pointer to the hardware structure
2556  * @blk: HW block
2557  * @vsig: VSIG to remove
2558  *
2559  * The function will remove all VSIs associated with the input VSIG and move
2560  * them to the DEFAULT_VSIG and mark the VSIG available.
2561  */
2562 static enum ice_status
2563 ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2564 {
2565         struct ice_vsig_prof *dtmp, *del;
2566         struct ice_vsig_vsi *vsi_cur;
2567         u16 idx;
2568
2569         idx = vsig & ICE_VSIG_IDX_M;
2570         if (idx >= ICE_MAX_VSIGS)
2571                 return ICE_ERR_PARAM;
2572
2573         if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2574                 return ICE_ERR_DOES_NOT_EXIST;
2575
2576         hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false;
2577
2578         vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2579         /* If the VSIG has at least 1 VSI then iterate through the
2580          * list and remove the VSIs before deleting the group.
2581          */
2582         if (vsi_cur) {
2583                 /* remove all vsis associated with this VSIG XLT2 entry */
2584                 do {
2585                         struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
2586
2587                         vsi_cur->vsig = ICE_DEFAULT_VSIG;
2588                         vsi_cur->changed = 1;
2589                         vsi_cur->next_vsi = NULL;
2590                         vsi_cur = tmp;
2591                 } while (vsi_cur);
2592
2593                 /* NULL terminate head of VSI list */
2594                 hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL;
2595         }
2596
2597         /* free characteristic list */
2598         LIST_FOR_EACH_ENTRY_SAFE(del, dtmp,
2599                                  &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
2600                                  ice_vsig_prof, list) {
2601                 LIST_DEL(&del->list);
2602                 ice_free(hw, del);
2603         }
2604
2605         /* if VSIG characteristic list was cleared for reset
2606          * re-initialize the list head
2607          */
2608         INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2609
2610         return ICE_SUCCESS;
2611 }
2612
2613 /**
2614  * ice_vsig_remove_vsi - remove VSI from VSIG
2615  * @hw: pointer to the hardware structure
2616  * @blk: HW block
2617  * @vsi: VSI to remove
2618  * @vsig: VSI group to remove from
2619  *
2620  * The function will remove the input VSI from its VSI group and move it
2621  * to the DEFAULT_VSIG.
2622  */
2623 static enum ice_status
2624 ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
2625 {
2626         struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt;
2627         u16 idx;
2628
2629         idx = vsig & ICE_VSIG_IDX_M;
2630
2631         if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
2632                 return ICE_ERR_PARAM;
2633
2634         if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2635                 return ICE_ERR_DOES_NOT_EXIST;
2636
2637         /* entry already in default VSIG, don't have to remove */
2638         if (idx == ICE_DEFAULT_VSIG)
2639                 return ICE_SUCCESS;
2640
2641         vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2642         if (!(*vsi_head))
2643                 return ICE_ERR_CFG;
2644
2645         vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi];
2646         vsi_cur = (*vsi_head);
2647
2648         /* iterate the VSI list, skip over the entry to be removed */
2649         while (vsi_cur) {
2650                 if (vsi_tgt == vsi_cur) {
2651                         (*vsi_head) = vsi_cur->next_vsi;
2652                         break;
2653                 }
2654                 vsi_head = &vsi_cur->next_vsi;
2655                 vsi_cur = vsi_cur->next_vsi;
2656         }
2657
2658         /* verify if VSI was removed from group list */
2659         if (!vsi_cur)
2660                 return ICE_ERR_DOES_NOT_EXIST;
2661
2662         vsi_cur->vsig = ICE_DEFAULT_VSIG;
2663         vsi_cur->changed = 1;
2664         vsi_cur->next_vsi = NULL;
2665
2666         return ICE_SUCCESS;
2667 }
2668
2669 /**
2670  * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group
2671  * @hw: pointer to the hardware structure
2672  * @blk: HW block
2673  * @vsi: VSI to move
2674  * @vsig: destination VSI group
2675  *
2676  * This function will move or add the input VSI to the target VSIG.
2677  * The function will find the original VSIG the VSI belongs to and
2678  * move the entry to the DEFAULT_VSIG, update the original VSIG and
2679  * then move entry to the new VSIG.
2680  */
2681 static enum ice_status
2682 ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
2683 {
2684         struct ice_vsig_vsi *tmp;
2685         enum ice_status status;
2686         u16 orig_vsig, idx;
2687
2688         idx = vsig & ICE_VSIG_IDX_M;
2689
2690         if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
2691                 return ICE_ERR_PARAM;
2692
2693         /* if VSIG not in use and VSIG is not default type this VSIG
2694          * doesn't exist.
2695          */
2696         if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use &&
2697             vsig != ICE_DEFAULT_VSIG)
2698                 return ICE_ERR_DOES_NOT_EXIST;
2699
2700         status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
2701         if (status)
2702                 return status;
2703
2704         /* no update required if vsigs match */
2705         if (orig_vsig == vsig)
2706                 return ICE_SUCCESS;
2707
2708         if (orig_vsig != ICE_DEFAULT_VSIG) {
2709                 /* remove entry from orig_vsig and add to default VSIG */
2710                 status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig);
2711                 if (status)
2712                         return status;
2713         }
2714
2715         if (idx == ICE_DEFAULT_VSIG)
2716                 return ICE_SUCCESS;
2717
2718         /* Create VSI entry and add VSIG and prop_mask values */
2719         hw->blk[blk].xlt2.vsis[vsi].vsig = vsig;
2720         hw->blk[blk].xlt2.vsis[vsi].changed = 1;
2721
2722         /* Add new entry to the head of the VSIG list */
2723         tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2724         hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi =
2725                 &hw->blk[blk].xlt2.vsis[vsi];
2726         hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp;
2727         hw->blk[blk].xlt2.t[vsi] = vsig;
2728
2729         return ICE_SUCCESS;
2730 }
2731
2732 /**
2733  * ice_prof_has_mask_idx - determine if profile index masking is identical
2734  * @hw: pointer to the hardware structure
2735  * @blk: HW block
2736  * @prof: profile to check
2737  * @idx: profile index to check
2738  * @mask: mask to match
2739  */
2740 static bool
2741 ice_prof_has_mask_idx(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 idx,
2742                       u16 mask)
2743 {
2744         bool expect_no_mask = false;
2745         bool found = false;
2746         bool match = false;
2747         u16 i;
2748
2749         /* If mask is 0x0000 or 0xffff, then there is no masking */
2750         if (mask == 0 || mask == 0xffff)
2751                 expect_no_mask = true;
2752
2753         /* Scan the enabled masks on this profile, for the specified idx */
2754         for (i = hw->blk[blk].masks.first; i < hw->blk[blk].masks.first +
2755              hw->blk[blk].masks.count; i++)
2756                 if (hw->blk[blk].es.mask_ena[prof] & BIT(i))
2757                         if (hw->blk[blk].masks.masks[i].in_use &&
2758                             hw->blk[blk].masks.masks[i].idx == idx) {
2759                                 found = true;
2760                                 if (hw->blk[blk].masks.masks[i].mask == mask)
2761                                         match = true;
2762                                 break;
2763                         }
2764
2765         if (expect_no_mask) {
2766                 if (found)
2767                         return false;
2768         } else {
2769                 if (!match)
2770                         return false;
2771         }
2772
2773         return true;
2774 }
2775
2776 /**
2777  * ice_prof_has_mask - determine if profile masking is identical
2778  * @hw: pointer to the hardware structure
2779  * @blk: HW block
2780  * @prof: profile to check
2781  * @masks: masks to match
2782  */
2783 static bool
2784 ice_prof_has_mask(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 *masks)
2785 {
2786         u16 i;
2787
2788         /* es->mask_ena[prof] will have the mask */
2789         for (i = 0; i < hw->blk[blk].es.fvw; i++)
2790                 if (!ice_prof_has_mask_idx(hw, blk, prof, i, masks[i]))
2791                         return false;
2792
2793         return true;
2794 }
2795
2796 /**
2797  * ice_find_prof_id_with_mask - find profile ID for a given field vector
2798  * @hw: pointer to the hardware structure
2799  * @blk: HW block
2800  * @fv: field vector to search for
2801  * @masks: masks for fv
2802  * @prof_id: receives the profile ID
2803  */
2804 static enum ice_status
2805 ice_find_prof_id_with_mask(struct ice_hw *hw, enum ice_block blk,
2806                            struct ice_fv_word *fv, u16 *masks, u8 *prof_id)
2807 {
2808         struct ice_es *es = &hw->blk[blk].es;
2809         u8 i;
2810
2811         /* For FD and RSS, we don't want to re-use an existed profile with the
2812          * same field vector and mask. This will cause rule interference.
2813          */
2814         if (blk == ICE_BLK_FD || blk == ICE_BLK_RSS)
2815                 return ICE_ERR_DOES_NOT_EXIST;
2816
2817         for (i = 0; i < (u8)es->count; i++) {
2818                 u16 off = i * es->fvw;
2819
2820                 if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv)))
2821                         continue;
2822
2823                 /* check if masks settings are the same for this profile */
2824                 if (masks && !ice_prof_has_mask(hw, blk, i, masks))
2825                         continue;
2826
2827                 *prof_id = i;
2828                 return ICE_SUCCESS;
2829         }
2830
2831         return ICE_ERR_DOES_NOT_EXIST;
2832 }
2833
2834 /**
2835  * ice_prof_id_rsrc_type - get profile ID resource type for a block type
2836  * @blk: the block type
2837  * @rsrc_type: pointer to variable to receive the resource type
2838  */
2839 static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type)
2840 {
2841         switch (blk) {
2842         case ICE_BLK_SW:
2843                 *rsrc_type = ICE_AQC_RES_TYPE_SWITCH_PROF_BLDR_PROFID;
2844                 break;
2845         case ICE_BLK_ACL:
2846                 *rsrc_type = ICE_AQC_RES_TYPE_ACL_PROF_BLDR_PROFID;
2847                 break;
2848         case ICE_BLK_FD:
2849                 *rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID;
2850                 break;
2851         case ICE_BLK_RSS:
2852                 *rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID;
2853                 break;
2854         case ICE_BLK_PE:
2855                 *rsrc_type = ICE_AQC_RES_TYPE_QHASH_PROF_BLDR_PROFID;
2856                 break;
2857         default:
2858                 return false;
2859         }
2860         return true;
2861 }
2862
2863 /**
2864  * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type
2865  * @blk: the block type
2866  * @rsrc_type: pointer to variable to receive the resource type
2867  */
2868 static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type)
2869 {
2870         switch (blk) {
2871         case ICE_BLK_SW:
2872                 *rsrc_type = ICE_AQC_RES_TYPE_SWITCH_PROF_BLDR_TCAM;
2873                 break;
2874         case ICE_BLK_ACL:
2875                 *rsrc_type = ICE_AQC_RES_TYPE_ACL_PROF_BLDR_TCAM;
2876                 break;
2877         case ICE_BLK_FD:
2878                 *rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM;
2879                 break;
2880         case ICE_BLK_RSS:
2881                 *rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM;
2882                 break;
2883         case ICE_BLK_PE:
2884                 *rsrc_type = ICE_AQC_RES_TYPE_QHASH_PROF_BLDR_TCAM;
2885                 break;
2886         default:
2887                 return false;
2888         }
2889         return true;
2890 }
2891
2892 /**
2893  * ice_alloc_tcam_ent - allocate hardware TCAM entry
2894  * @hw: pointer to the HW struct
2895  * @blk: the block to allocate the TCAM for
2896  * @btm: true to allocate from bottom of table, false to allocate from top
2897  * @tcam_idx: pointer to variable to receive the TCAM entry
2898  *
2899  * This function allocates a new entry in a Profile ID TCAM for a specific
2900  * block.
2901  */
2902 static enum ice_status
2903 ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, bool btm,
2904                    u16 *tcam_idx)
2905 {
2906         u16 res_type;
2907
2908         if (!ice_tcam_ent_rsrc_type(blk, &res_type))
2909                 return ICE_ERR_PARAM;
2910
2911         return ice_alloc_hw_res(hw, res_type, 1, btm, tcam_idx);
2912 }
2913
2914 /**
2915  * ice_free_tcam_ent - free hardware TCAM entry
2916  * @hw: pointer to the HW struct
2917  * @blk: the block from which to free the TCAM entry
2918  * @tcam_idx: the TCAM entry to free
2919  *
2920  * This function frees an entry in a Profile ID TCAM for a specific block.
2921  */
2922 static enum ice_status
2923 ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx)
2924 {
2925         u16 res_type;
2926
2927         if (!ice_tcam_ent_rsrc_type(blk, &res_type))
2928                 return ICE_ERR_PARAM;
2929
2930         return ice_free_hw_res(hw, res_type, 1, &tcam_idx);
2931 }
2932
2933 /**
2934  * ice_alloc_prof_id - allocate profile ID
2935  * @hw: pointer to the HW struct
2936  * @blk: the block to allocate the profile ID for
2937  * @prof_id: pointer to variable to receive the profile ID
2938  *
2939  * This function allocates a new profile ID, which also corresponds to a Field
2940  * Vector (Extraction Sequence) entry.
2941  */
2942 static enum ice_status
2943 ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id)
2944 {
2945         enum ice_status status;
2946         u16 res_type;
2947         u16 get_prof;
2948
2949         if (!ice_prof_id_rsrc_type(blk, &res_type))
2950                 return ICE_ERR_PARAM;
2951
2952         status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof);
2953         if (!status)
2954                 *prof_id = (u8)get_prof;
2955
2956         return status;
2957 }
2958
2959 /**
2960  * ice_free_prof_id - free profile ID
2961  * @hw: pointer to the HW struct
2962  * @blk: the block from which to free the profile ID
2963  * @prof_id: the profile ID to free
2964  *
2965  * This function frees a profile ID, which also corresponds to a Field Vector.
2966  */
2967 static enum ice_status
2968 ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
2969 {
2970         u16 tmp_prof_id = (u16)prof_id;
2971         u16 res_type;
2972
2973         if (!ice_prof_id_rsrc_type(blk, &res_type))
2974                 return ICE_ERR_PARAM;
2975
2976         return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id);
2977 }
2978
2979 /**
2980  * ice_prof_inc_ref - increment reference count for profile
2981  * @hw: pointer to the HW struct
2982  * @blk: the block from which to free the profile ID
2983  * @prof_id: the profile ID for which to increment the reference count
2984  */
2985 static enum ice_status
2986 ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
2987 {
2988         if (prof_id > hw->blk[blk].es.count)
2989                 return ICE_ERR_PARAM;
2990
2991         hw->blk[blk].es.ref_count[prof_id]++;
2992
2993         return ICE_SUCCESS;
2994 }
2995
2996 /**
2997  * ice_write_prof_mask_reg - write profile mask register
2998  * @hw: pointer to the HW struct
2999  * @blk: hardware block
3000  * @mask_idx: mask index
3001  * @idx: index of the FV which will use the mask
3002  * @mask: the 16-bit mask
3003  */
3004 static void
3005 ice_write_prof_mask_reg(struct ice_hw *hw, enum ice_block blk, u16 mask_idx,
3006                         u16 idx, u16 mask)
3007 {
3008         u32 offset;
3009         u32 val;
3010
3011         switch (blk) {
3012         case ICE_BLK_RSS:
3013                 offset = GLQF_HMASK(mask_idx);
3014                 val = (idx << GLQF_HMASK_MSK_INDEX_S) &
3015                         GLQF_HMASK_MSK_INDEX_M;
3016                 val |= (mask << GLQF_HMASK_MASK_S) & GLQF_HMASK_MASK_M;
3017                 break;
3018         case ICE_BLK_FD:
3019                 offset = GLQF_FDMASK(mask_idx);
3020                 val = (idx << GLQF_FDMASK_MSK_INDEX_S) &
3021                         GLQF_FDMASK_MSK_INDEX_M;
3022                 val |= (mask << GLQF_FDMASK_MASK_S) &
3023                         GLQF_FDMASK_MASK_M;
3024                 break;
3025         default:
3026                 ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
3027                           blk);
3028                 return;
3029         }
3030
3031         wr32(hw, offset, val);
3032         ice_debug(hw, ICE_DBG_PKG, "write mask, blk %d (%d): %x = %x\n",
3033                   blk, idx, offset, val);
3034 }
3035
3036 /**
3037  * ice_write_prof_mask_enable_res - write profile mask enable register
3038  * @hw: pointer to the HW struct
3039  * @blk: hardware block
3040  * @prof_id: profile ID
3041  * @enable_mask: enable mask
3042  */
3043 static void
3044 ice_write_prof_mask_enable_res(struct ice_hw *hw, enum ice_block blk,
3045                                u16 prof_id, u32 enable_mask)
3046 {
3047         u32 offset;
3048
3049         switch (blk) {
3050         case ICE_BLK_RSS:
3051                 offset = GLQF_HMASK_SEL(prof_id);
3052                 break;
3053         case ICE_BLK_FD:
3054                 offset = GLQF_FDMASK_SEL(prof_id);
3055                 break;
3056         default:
3057                 ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
3058                           blk);
3059                 return;
3060         }
3061
3062         wr32(hw, offset, enable_mask);
3063         ice_debug(hw, ICE_DBG_PKG, "write mask enable, blk %d (%d): %x = %x\n",
3064                   blk, prof_id, offset, enable_mask);
3065 }
3066
3067 /**
3068  * ice_init_prof_masks - initial prof masks
3069  * @hw: pointer to the HW struct
3070  * @blk: hardware block
3071  */
3072 static void ice_init_prof_masks(struct ice_hw *hw, enum ice_block blk)
3073 {
3074         u16 per_pf;
3075         u16 i;
3076
3077         ice_init_lock(&hw->blk[blk].masks.lock);
3078
3079         per_pf = ICE_PROF_MASK_COUNT / hw->dev_caps.num_funcs;
3080
3081         hw->blk[blk].masks.count = per_pf;
3082         hw->blk[blk].masks.first = hw->pf_id * per_pf;
3083
3084         ice_memset(hw->blk[blk].masks.masks, 0,
3085                    sizeof(hw->blk[blk].masks.masks), ICE_NONDMA_MEM);
3086
3087         for (i = hw->blk[blk].masks.first;
3088              i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
3089                 ice_write_prof_mask_reg(hw, blk, i, 0, 0);
3090 }
3091
3092 /**
3093  * ice_init_all_prof_masks - initial all prof masks
3094  * @hw: pointer to the HW struct
3095  */
3096 void ice_init_all_prof_masks(struct ice_hw *hw)
3097 {
3098         ice_init_prof_masks(hw, ICE_BLK_RSS);
3099         ice_init_prof_masks(hw, ICE_BLK_FD);
3100 }
3101
3102 /**
3103  * ice_alloc_prof_mask - allocate profile mask
3104  * @hw: pointer to the HW struct
3105  * @blk: hardware block
3106  * @idx: index of FV which will use the mask
3107  * @mask: the 16-bit mask
3108  * @mask_idx: variable to receive the mask index
3109  */
3110 static enum ice_status
3111 ice_alloc_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 idx, u16 mask,
3112                     u16 *mask_idx)
3113 {
3114         bool found_unused = false, found_copy = false;
3115         enum ice_status status = ICE_ERR_MAX_LIMIT;
3116         u16 unused_idx = 0, copy_idx = 0;
3117         u16 i;
3118
3119         if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3120                 return ICE_ERR_PARAM;
3121
3122         ice_acquire_lock(&hw->blk[blk].masks.lock);
3123
3124         for (i = hw->blk[blk].masks.first;
3125              i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
3126                 if (hw->blk[blk].masks.masks[i].in_use) {
3127                         /* if mask is in use and it exactly duplicates the
3128                          * desired mask and index, then in can be reused
3129                          */
3130                         if (hw->blk[blk].masks.masks[i].mask == mask &&
3131                             hw->blk[blk].masks.masks[i].idx == idx) {
3132                                 found_copy = true;
3133                                 copy_idx = i;
3134                                 break;
3135                         }
3136                 } else {
3137                         /* save off unused index, but keep searching in case
3138                          * there is an exact match later on
3139                          */
3140                         if (!found_unused) {
3141                                 found_unused = true;
3142                                 unused_idx = i;
3143                         }
3144                 }
3145
3146         if (found_copy)
3147                 i = copy_idx;
3148         else if (found_unused)
3149                 i = unused_idx;
3150         else
3151                 goto err_ice_alloc_prof_mask;
3152
3153         /* update mask for a new entry */
3154         if (found_unused) {
3155                 hw->blk[blk].masks.masks[i].in_use = true;
3156                 hw->blk[blk].masks.masks[i].mask = mask;
3157                 hw->blk[blk].masks.masks[i].idx = idx;
3158                 hw->blk[blk].masks.masks[i].ref = 0;
3159                 ice_write_prof_mask_reg(hw, blk, i, idx, mask);
3160         }
3161
3162         hw->blk[blk].masks.masks[i].ref++;
3163         *mask_idx = i;
3164         status = ICE_SUCCESS;
3165
3166 err_ice_alloc_prof_mask:
3167         ice_release_lock(&hw->blk[blk].masks.lock);
3168
3169         return status;
3170 }
3171
3172 /**
3173  * ice_free_prof_mask - free profile mask
3174  * @hw: pointer to the HW struct
3175  * @blk: hardware block
3176  * @mask_idx: index of mask
3177  */
3178 static enum ice_status
3179 ice_free_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 mask_idx)
3180 {
3181         if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3182                 return ICE_ERR_PARAM;
3183
3184         if (!(mask_idx >= hw->blk[blk].masks.first &&
3185               mask_idx < hw->blk[blk].masks.first + hw->blk[blk].masks.count))
3186                 return ICE_ERR_DOES_NOT_EXIST;
3187
3188         ice_acquire_lock(&hw->blk[blk].masks.lock);
3189
3190         if (!hw->blk[blk].masks.masks[mask_idx].in_use)
3191                 goto exit_ice_free_prof_mask;
3192
3193         if (hw->blk[blk].masks.masks[mask_idx].ref > 1) {
3194                 hw->blk[blk].masks.masks[mask_idx].ref--;
3195                 goto exit_ice_free_prof_mask;
3196         }
3197
3198         /* remove mask */
3199         hw->blk[blk].masks.masks[mask_idx].in_use = false;
3200         hw->blk[blk].masks.masks[mask_idx].mask = 0;
3201         hw->blk[blk].masks.masks[mask_idx].idx = 0;
3202
3203         /* update mask as unused entry */
3204         ice_debug(hw, ICE_DBG_PKG, "Free mask, blk %d, mask %d\n", blk,
3205                   mask_idx);
3206         ice_write_prof_mask_reg(hw, blk, mask_idx, 0, 0);
3207
3208 exit_ice_free_prof_mask:
3209         ice_release_lock(&hw->blk[blk].masks.lock);
3210
3211         return ICE_SUCCESS;
3212 }
3213
3214 /**
3215  * ice_free_prof_masks - free all profile masks for a profile
3216  * @hw: pointer to the HW struct
3217  * @blk: hardware block
3218  * @prof_id: profile ID
3219  */
3220 static enum ice_status
3221 ice_free_prof_masks(struct ice_hw *hw, enum ice_block blk, u16 prof_id)
3222 {
3223         u32 mask_bm;
3224         u16 i;
3225
3226         if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3227                 return ICE_ERR_PARAM;
3228
3229         mask_bm = hw->blk[blk].es.mask_ena[prof_id];
3230         for (i = 0; i < BITS_PER_BYTE * sizeof(mask_bm); i++)
3231                 if (mask_bm & BIT(i))
3232                         ice_free_prof_mask(hw, blk, i);
3233
3234         return ICE_SUCCESS;
3235 }
3236
3237 /**
3238  * ice_shutdown_prof_masks - releases lock for masking
3239  * @hw: pointer to the HW struct
3240  * @blk: hardware block
3241  *
3242  * This should be called before unloading the driver
3243  */
3244 static void ice_shutdown_prof_masks(struct ice_hw *hw, enum ice_block blk)
3245 {
3246         u16 i;
3247
3248         ice_acquire_lock(&hw->blk[blk].masks.lock);
3249
3250         for (i = hw->blk[blk].masks.first;
3251              i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) {
3252                 ice_write_prof_mask_reg(hw, blk, i, 0, 0);
3253
3254                 hw->blk[blk].masks.masks[i].in_use = false;
3255                 hw->blk[blk].masks.masks[i].idx = 0;
3256                 hw->blk[blk].masks.masks[i].mask = 0;
3257         }
3258
3259         ice_release_lock(&hw->blk[blk].masks.lock);
3260         ice_destroy_lock(&hw->blk[blk].masks.lock);
3261 }
3262
3263 /**
3264  * ice_shutdown_all_prof_masks - releases all locks for masking
3265  * @hw: pointer to the HW struct
3266  *
3267  * This should be called before unloading the driver
3268  */
3269 void ice_shutdown_all_prof_masks(struct ice_hw *hw)
3270 {
3271         ice_shutdown_prof_masks(hw, ICE_BLK_RSS);
3272         ice_shutdown_prof_masks(hw, ICE_BLK_FD);
3273 }
3274
3275 /**
3276  * ice_update_prof_masking - set registers according to masking
3277  * @hw: pointer to the HW struct
3278  * @blk: hardware block
3279  * @prof_id: profile ID
3280  * @masks: masks
3281  */
3282 static enum ice_status
3283 ice_update_prof_masking(struct ice_hw *hw, enum ice_block blk, u16 prof_id,
3284                         u16 *masks)
3285 {
3286         bool err = false;
3287         u32 ena_mask = 0;
3288         u16 idx;
3289         u16 i;
3290
3291         /* Only support FD and RSS masking, otherwise nothing to be done */
3292         if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3293                 return ICE_SUCCESS;
3294
3295         for (i = 0; i < hw->blk[blk].es.fvw; i++)
3296                 if (masks[i] && masks[i] != 0xFFFF) {
3297                         if (!ice_alloc_prof_mask(hw, blk, i, masks[i], &idx)) {
3298                                 ena_mask |= BIT(idx);
3299                         } else {
3300                                 /* not enough bitmaps */
3301                                 err = true;
3302                                 break;
3303                         }
3304                 }
3305
3306         if (err) {
3307                 /* free any bitmaps we have allocated */
3308                 for (i = 0; i < BITS_PER_BYTE * sizeof(ena_mask); i++)
3309                         if (ena_mask & BIT(i))
3310                                 ice_free_prof_mask(hw, blk, i);
3311
3312                 return ICE_ERR_OUT_OF_RANGE;
3313         }
3314
3315         /* enable the masks for this profile */
3316         ice_write_prof_mask_enable_res(hw, blk, prof_id, ena_mask);
3317
3318         /* store enabled masks with profile so that they can be freed later */
3319         hw->blk[blk].es.mask_ena[prof_id] = ena_mask;
3320
3321         return ICE_SUCCESS;
3322 }
3323
3324 /**
3325  * ice_write_es - write an extraction sequence to hardware
3326  * @hw: pointer to the HW struct
3327  * @blk: the block in which to write the extraction sequence
3328  * @prof_id: the profile ID to write
3329  * @fv: pointer to the extraction sequence to write - NULL to clear extraction
3330  */
3331 static void
3332 ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id,
3333              struct ice_fv_word *fv)
3334 {
3335         u16 off;
3336
3337         off = prof_id * hw->blk[blk].es.fvw;
3338         if (!fv) {
3339                 ice_memset(&hw->blk[blk].es.t[off], 0, hw->blk[blk].es.fvw *
3340                            sizeof(*fv), ICE_NONDMA_MEM);
3341                 hw->blk[blk].es.written[prof_id] = false;
3342         } else {
3343                 ice_memcpy(&hw->blk[blk].es.t[off], fv, hw->blk[blk].es.fvw *
3344                            sizeof(*fv), ICE_NONDMA_TO_NONDMA);
3345         }
3346 }
3347
3348 /**
3349  * ice_prof_dec_ref - decrement reference count for profile
3350  * @hw: pointer to the HW struct
3351  * @blk: the block from which to free the profile ID
3352  * @prof_id: the profile ID for which to decrement the reference count
3353  */
3354 static enum ice_status
3355 ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
3356 {
3357         if (prof_id > hw->blk[blk].es.count)
3358                 return ICE_ERR_PARAM;
3359
3360         if (hw->blk[blk].es.ref_count[prof_id] > 0) {
3361                 if (!--hw->blk[blk].es.ref_count[prof_id]) {
3362                         ice_write_es(hw, blk, prof_id, NULL);
3363                         ice_free_prof_masks(hw, blk, prof_id);
3364                         return ice_free_prof_id(hw, blk, prof_id);
3365                 }
3366         }
3367
3368         return ICE_SUCCESS;
3369 }
3370
3371 /* Block / table section IDs */
3372 static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = {
3373         /* SWITCH */
3374         {       ICE_SID_XLT1_SW,
3375                 ICE_SID_XLT2_SW,
3376                 ICE_SID_PROFID_TCAM_SW,
3377                 ICE_SID_PROFID_REDIR_SW,
3378                 ICE_SID_FLD_VEC_SW
3379         },
3380
3381         /* ACL */
3382         {       ICE_SID_XLT1_ACL,
3383                 ICE_SID_XLT2_ACL,
3384                 ICE_SID_PROFID_TCAM_ACL,
3385                 ICE_SID_PROFID_REDIR_ACL,
3386                 ICE_SID_FLD_VEC_ACL
3387         },
3388
3389         /* FD */
3390         {       ICE_SID_XLT1_FD,
3391                 ICE_SID_XLT2_FD,
3392                 ICE_SID_PROFID_TCAM_FD,
3393                 ICE_SID_PROFID_REDIR_FD,
3394                 ICE_SID_FLD_VEC_FD
3395         },
3396
3397         /* RSS */
3398         {       ICE_SID_XLT1_RSS,
3399                 ICE_SID_XLT2_RSS,
3400                 ICE_SID_PROFID_TCAM_RSS,
3401                 ICE_SID_PROFID_REDIR_RSS,
3402                 ICE_SID_FLD_VEC_RSS
3403         },
3404
3405         /* PE */
3406         {       ICE_SID_XLT1_PE,
3407                 ICE_SID_XLT2_PE,
3408                 ICE_SID_PROFID_TCAM_PE,
3409                 ICE_SID_PROFID_REDIR_PE,
3410                 ICE_SID_FLD_VEC_PE
3411         }
3412 };
3413
3414 /**
3415  * ice_init_sw_xlt1_db - init software XLT1 database from HW tables
3416  * @hw: pointer to the hardware structure
3417  * @blk: the HW block to initialize
3418  */
3419 static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk)
3420 {
3421         u16 pt;
3422
3423         for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) {
3424                 u8 ptg;
3425
3426                 ptg = hw->blk[blk].xlt1.t[pt];
3427                 if (ptg != ICE_DEFAULT_PTG) {
3428                         ice_ptg_alloc_val(hw, blk, ptg);
3429                         ice_ptg_add_mv_ptype(hw, blk, pt, ptg);
3430                 }
3431         }
3432 }
3433
3434 /**
3435  * ice_init_sw_xlt2_db - init software XLT2 database from HW tables
3436  * @hw: pointer to the hardware structure
3437  * @blk: the HW block to initialize
3438  */
3439 static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk)
3440 {
3441         u16 vsi;
3442
3443         for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) {
3444                 u16 vsig;
3445
3446                 vsig = hw->blk[blk].xlt2.t[vsi];
3447                 if (vsig) {
3448                         ice_vsig_alloc_val(hw, blk, vsig);
3449                         ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
3450                         /* no changes at this time, since this has been
3451                          * initialized from the original package
3452                          */
3453                         hw->blk[blk].xlt2.vsis[vsi].changed = 0;
3454                 }
3455         }
3456 }
3457
3458 /**
3459  * ice_init_sw_db - init software database from HW tables
3460  * @hw: pointer to the hardware structure
3461  */
3462 static void ice_init_sw_db(struct ice_hw *hw)
3463 {
3464         u16 i;
3465
3466         for (i = 0; i < ICE_BLK_COUNT; i++) {
3467                 ice_init_sw_xlt1_db(hw, (enum ice_block)i);
3468                 ice_init_sw_xlt2_db(hw, (enum ice_block)i);
3469         }
3470 }
3471
3472 /**
3473  * ice_fill_tbl - Reads content of a single table type into database
3474  * @hw: pointer to the hardware structure
3475  * @block_id: Block ID of the table to copy
3476  * @sid: Section ID of the table to copy
3477  *
3478  * Will attempt to read the entire content of a given table of a single block
3479  * into the driver database. We assume that the buffer will always
3480  * be as large or larger than the data contained in the package. If
3481  * this condition is not met, there is most likely an error in the package
3482  * contents.
3483  */
3484 static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid)
3485 {
3486         u32 dst_len, sect_len, offset = 0;
3487         struct ice_prof_redir_section *pr;
3488         struct ice_prof_id_section *pid;
3489         struct ice_xlt1_section *xlt1;
3490         struct ice_xlt2_section *xlt2;
3491         struct ice_sw_fv_section *es;
3492         struct ice_pkg_enum state;
3493         u8 *src, *dst;
3494         void *sect;
3495
3496         /* if the HW segment pointer is null then the first iteration of
3497          * ice_pkg_enum_section() will fail. In this case the HW tables will
3498          * not be filled and return success.
3499          */
3500         if (!hw->seg) {
3501                 ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n");
3502                 return;
3503         }
3504
3505         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
3506
3507         sect = ice_pkg_enum_section(hw->seg, &state, sid);
3508
3509         while (sect) {
3510                 switch (sid) {
3511                 case ICE_SID_XLT1_SW:
3512                 case ICE_SID_XLT1_FD:
3513                 case ICE_SID_XLT1_RSS:
3514                 case ICE_SID_XLT1_ACL:
3515                 case ICE_SID_XLT1_PE:
3516                         xlt1 = (struct ice_xlt1_section *)sect;
3517                         src = xlt1->value;
3518                         sect_len = LE16_TO_CPU(xlt1->count) *
3519                                 sizeof(*hw->blk[block_id].xlt1.t);
3520                         dst = hw->blk[block_id].xlt1.t;
3521                         dst_len = hw->blk[block_id].xlt1.count *
3522                                 sizeof(*hw->blk[block_id].xlt1.t);
3523                         break;
3524                 case ICE_SID_XLT2_SW:
3525                 case ICE_SID_XLT2_FD:
3526                 case ICE_SID_XLT2_RSS:
3527                 case ICE_SID_XLT2_ACL:
3528                 case ICE_SID_XLT2_PE:
3529                         xlt2 = (struct ice_xlt2_section *)sect;
3530                         src = (_FORCE_ u8 *)xlt2->value;
3531                         sect_len = LE16_TO_CPU(xlt2->count) *
3532                                 sizeof(*hw->blk[block_id].xlt2.t);
3533                         dst = (u8 *)hw->blk[block_id].xlt2.t;
3534                         dst_len = hw->blk[block_id].xlt2.count *
3535                                 sizeof(*hw->blk[block_id].xlt2.t);
3536                         break;
3537                 case ICE_SID_PROFID_TCAM_SW:
3538                 case ICE_SID_PROFID_TCAM_FD:
3539                 case ICE_SID_PROFID_TCAM_RSS:
3540                 case ICE_SID_PROFID_TCAM_ACL:
3541                 case ICE_SID_PROFID_TCAM_PE:
3542                         pid = (struct ice_prof_id_section *)sect;
3543                         src = (u8 *)pid->entry;
3544                         sect_len = LE16_TO_CPU(pid->count) *
3545                                 sizeof(*hw->blk[block_id].prof.t);
3546                         dst = (u8 *)hw->blk[block_id].prof.t;
3547                         dst_len = hw->blk[block_id].prof.count *
3548                                 sizeof(*hw->blk[block_id].prof.t);
3549                         break;
3550                 case ICE_SID_PROFID_REDIR_SW:
3551                 case ICE_SID_PROFID_REDIR_FD:
3552                 case ICE_SID_PROFID_REDIR_RSS:
3553                 case ICE_SID_PROFID_REDIR_ACL:
3554                 case ICE_SID_PROFID_REDIR_PE:
3555                         pr = (struct ice_prof_redir_section *)sect;
3556                         src = pr->redir_value;
3557                         sect_len = LE16_TO_CPU(pr->count) *
3558                                 sizeof(*hw->blk[block_id].prof_redir.t);
3559                         dst = hw->blk[block_id].prof_redir.t;
3560                         dst_len = hw->blk[block_id].prof_redir.count *
3561                                 sizeof(*hw->blk[block_id].prof_redir.t);
3562                         break;
3563                 case ICE_SID_FLD_VEC_SW:
3564                 case ICE_SID_FLD_VEC_FD:
3565                 case ICE_SID_FLD_VEC_RSS:
3566                 case ICE_SID_FLD_VEC_ACL:
3567                 case ICE_SID_FLD_VEC_PE:
3568                         es = (struct ice_sw_fv_section *)sect;
3569                         src = (u8 *)es->fv;
3570                         sect_len = (u32)(LE16_TO_CPU(es->count) *
3571                                          hw->blk[block_id].es.fvw) *
3572                                 sizeof(*hw->blk[block_id].es.t);
3573                         dst = (u8 *)hw->blk[block_id].es.t;
3574                         dst_len = (u32)(hw->blk[block_id].es.count *
3575                                         hw->blk[block_id].es.fvw) *
3576                                 sizeof(*hw->blk[block_id].es.t);
3577                         break;
3578                 default:
3579                         return;
3580                 }
3581
3582                 /* if the section offset exceeds destination length, terminate
3583                  * table fill.
3584                  */
3585                 if (offset > dst_len)
3586                         return;
3587
3588                 /* if the sum of section size and offset exceed destination size
3589                  * then we are out of bounds of the HW table size for that PF.
3590                  * Changing section length to fill the remaining table space
3591                  * of that PF.
3592                  */
3593                 if ((offset + sect_len) > dst_len)
3594                         sect_len = dst_len - offset;
3595
3596                 ice_memcpy(dst + offset, src, sect_len, ICE_NONDMA_TO_NONDMA);
3597                 offset += sect_len;
3598                 sect = ice_pkg_enum_section(NULL, &state, sid);
3599         }
3600 }
3601
3602 /**
3603  * ice_fill_blk_tbls - Read package context for tables
3604  * @hw: pointer to the hardware structure
3605  *
3606  * Reads the current package contents and populates the driver
3607  * database with the data iteratively for all advanced feature
3608  * blocks. Assume that the HW tables have been allocated.
3609  */
3610 void ice_fill_blk_tbls(struct ice_hw *hw)
3611 {
3612         u8 i;
3613
3614         for (i = 0; i < ICE_BLK_COUNT; i++) {
3615                 enum ice_block blk_id = (enum ice_block)i;
3616
3617                 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid);
3618                 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid);
3619                 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid);
3620                 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid);
3621                 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid);
3622         }
3623
3624         ice_init_sw_db(hw);
3625 }
3626
3627 /**
3628  * ice_free_prof_map - free profile map
3629  * @hw: pointer to the hardware structure
3630  * @blk_idx: HW block index
3631  */
3632 static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx)
3633 {
3634         struct ice_es *es = &hw->blk[blk_idx].es;
3635         struct ice_prof_map *del, *tmp;
3636
3637         ice_acquire_lock(&es->prof_map_lock);
3638         LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &es->prof_map,
3639                                  ice_prof_map, list) {
3640                 LIST_DEL(&del->list);
3641                 ice_free(hw, del);
3642         }
3643         INIT_LIST_HEAD(&es->prof_map);
3644         ice_release_lock(&es->prof_map_lock);
3645 }
3646
3647 /**
3648  * ice_free_flow_profs - free flow profile entries
3649  * @hw: pointer to the hardware structure
3650  * @blk_idx: HW block index
3651  */
3652 static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx)
3653 {
3654         struct ice_flow_prof *p, *tmp;
3655
3656         ice_acquire_lock(&hw->fl_profs_locks[blk_idx]);
3657         LIST_FOR_EACH_ENTRY_SAFE(p, tmp, &hw->fl_profs[blk_idx],
3658                                  ice_flow_prof, l_entry) {
3659                 struct ice_flow_entry *e, *t;
3660
3661                 LIST_FOR_EACH_ENTRY_SAFE(e, t, &p->entries,
3662                                          ice_flow_entry, l_entry)
3663                         ice_flow_rem_entry(hw, (enum ice_block)blk_idx,
3664                                            ICE_FLOW_ENTRY_HNDL(e));
3665
3666                 LIST_DEL(&p->l_entry);
3667                 if (p->acts)
3668                         ice_free(hw, p->acts);
3669
3670                 ice_destroy_lock(&p->entries_lock);
3671                 ice_free(hw, p);
3672         }
3673         ice_release_lock(&hw->fl_profs_locks[blk_idx]);
3674
3675         /* if driver is in reset and tables are being cleared
3676          * re-initialize the flow profile list heads
3677          */
3678         INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
3679 }
3680
3681 /**
3682  * ice_free_vsig_tbl - free complete VSIG table entries
3683  * @hw: pointer to the hardware structure
3684  * @blk: the HW block on which to free the VSIG table entries
3685  */
3686 static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk)
3687 {
3688         u16 i;
3689
3690         if (!hw->blk[blk].xlt2.vsig_tbl)
3691                 return;
3692
3693         for (i = 1; i < ICE_MAX_VSIGS; i++)
3694                 if (hw->blk[blk].xlt2.vsig_tbl[i].in_use)
3695                         ice_vsig_free(hw, blk, i);
3696 }
3697
3698 /**
3699  * ice_free_hw_tbls - free hardware table memory
3700  * @hw: pointer to the hardware structure
3701  */
3702 void ice_free_hw_tbls(struct ice_hw *hw)
3703 {
3704         struct ice_rss_cfg *r, *rt;
3705         u8 i;
3706
3707         for (i = 0; i < ICE_BLK_COUNT; i++) {
3708                 if (hw->blk[i].is_list_init) {
3709                         struct ice_es *es = &hw->blk[i].es;
3710
3711                         ice_free_prof_map(hw, i);
3712                         ice_destroy_lock(&es->prof_map_lock);
3713                         ice_free_flow_profs(hw, i);
3714                         ice_destroy_lock(&hw->fl_profs_locks[i]);
3715
3716                         hw->blk[i].is_list_init = false;
3717                 }
3718                 ice_free_vsig_tbl(hw, (enum ice_block)i);
3719                 ice_free(hw, hw->blk[i].xlt1.ptypes);
3720                 ice_free(hw, hw->blk[i].xlt1.ptg_tbl);
3721                 ice_free(hw, hw->blk[i].xlt1.t);
3722                 ice_free(hw, hw->blk[i].xlt2.t);
3723                 ice_free(hw, hw->blk[i].xlt2.vsig_tbl);
3724                 ice_free(hw, hw->blk[i].xlt2.vsis);
3725                 ice_free(hw, hw->blk[i].prof.t);
3726                 ice_free(hw, hw->blk[i].prof_redir.t);
3727                 ice_free(hw, hw->blk[i].es.t);
3728                 ice_free(hw, hw->blk[i].es.ref_count);
3729                 ice_free(hw, hw->blk[i].es.written);
3730                 ice_free(hw, hw->blk[i].es.mask_ena);
3731         }
3732
3733         LIST_FOR_EACH_ENTRY_SAFE(r, rt, &hw->rss_list_head,
3734                                  ice_rss_cfg, l_entry) {
3735                 LIST_DEL(&r->l_entry);
3736                 ice_free(hw, r);
3737         }
3738         ice_destroy_lock(&hw->rss_locks);
3739         if (!hw->dcf_enabled)
3740                 ice_shutdown_all_prof_masks(hw);
3741         ice_memset(hw->blk, 0, sizeof(hw->blk), ICE_NONDMA_MEM);
3742 }
3743
3744 /**
3745  * ice_init_flow_profs - init flow profile locks and list heads
3746  * @hw: pointer to the hardware structure
3747  * @blk_idx: HW block index
3748  */
3749 static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx)
3750 {
3751         ice_init_lock(&hw->fl_profs_locks[blk_idx]);
3752         INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
3753 }
3754
3755 /**
3756  * ice_clear_hw_tbls - clear HW tables and flow profiles
3757  * @hw: pointer to the hardware structure
3758  */
3759 void ice_clear_hw_tbls(struct ice_hw *hw)
3760 {
3761         u8 i;
3762
3763         for (i = 0; i < ICE_BLK_COUNT; i++) {
3764                 struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
3765                 struct ice_prof_tcam *prof = &hw->blk[i].prof;
3766                 struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
3767                 struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
3768                 struct ice_es *es = &hw->blk[i].es;
3769
3770                 if (hw->blk[i].is_list_init) {
3771                         ice_free_prof_map(hw, i);
3772                         ice_free_flow_profs(hw, i);
3773                 }
3774
3775                 ice_free_vsig_tbl(hw, (enum ice_block)i);
3776
3777                 ice_memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes),
3778                            ICE_NONDMA_MEM);
3779                 ice_memset(xlt1->ptg_tbl, 0,
3780                            ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl),
3781                            ICE_NONDMA_MEM);
3782                 ice_memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t),
3783                            ICE_NONDMA_MEM);
3784
3785                 ice_memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis),
3786                            ICE_NONDMA_MEM);
3787                 ice_memset(xlt2->vsig_tbl, 0,
3788                            xlt2->count * sizeof(*xlt2->vsig_tbl),
3789                            ICE_NONDMA_MEM);
3790                 ice_memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t),
3791                            ICE_NONDMA_MEM);
3792
3793                 ice_memset(prof->t, 0, prof->count * sizeof(*prof->t),
3794                            ICE_NONDMA_MEM);
3795                 ice_memset(prof_redir->t, 0,
3796                            prof_redir->count * sizeof(*prof_redir->t),
3797                            ICE_NONDMA_MEM);
3798
3799                 ice_memset(es->t, 0, es->count * sizeof(*es->t) * es->fvw,
3800                            ICE_NONDMA_MEM);
3801                 ice_memset(es->ref_count, 0, es->count * sizeof(*es->ref_count),
3802                            ICE_NONDMA_MEM);
3803                 ice_memset(es->written, 0, es->count * sizeof(*es->written),
3804                            ICE_NONDMA_MEM);
3805                 ice_memset(es->mask_ena, 0, es->count * sizeof(*es->mask_ena),
3806                            ICE_NONDMA_MEM);
3807         }
3808 }
3809
3810 /**
3811  * ice_init_hw_tbls - init hardware table memory
3812  * @hw: pointer to the hardware structure
3813  */
3814 enum ice_status ice_init_hw_tbls(struct ice_hw *hw)
3815 {
3816         u8 i;
3817
3818         ice_init_lock(&hw->rss_locks);
3819         INIT_LIST_HEAD(&hw->rss_list_head);
3820         if (!hw->dcf_enabled)
3821                 ice_init_all_prof_masks(hw);
3822         for (i = 0; i < ICE_BLK_COUNT; i++) {
3823                 struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
3824                 struct ice_prof_tcam *prof = &hw->blk[i].prof;
3825                 struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
3826                 struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
3827                 struct ice_es *es = &hw->blk[i].es;
3828                 u16 j;
3829
3830                 if (hw->blk[i].is_list_init)
3831                         continue;
3832
3833                 ice_init_flow_profs(hw, i);
3834                 ice_init_lock(&es->prof_map_lock);
3835                 INIT_LIST_HEAD(&es->prof_map);
3836                 hw->blk[i].is_list_init = true;
3837
3838                 hw->blk[i].overwrite = blk_sizes[i].overwrite;
3839                 es->reverse = blk_sizes[i].reverse;
3840
3841                 xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF];
3842                 xlt1->count = blk_sizes[i].xlt1;
3843
3844                 xlt1->ptypes = (struct ice_ptg_ptype *)
3845                         ice_calloc(hw, xlt1->count, sizeof(*xlt1->ptypes));
3846
3847                 if (!xlt1->ptypes)
3848                         goto err;
3849
3850                 xlt1->ptg_tbl = (struct ice_ptg_entry *)
3851                         ice_calloc(hw, ICE_MAX_PTGS, sizeof(*xlt1->ptg_tbl));
3852
3853                 if (!xlt1->ptg_tbl)
3854                         goto err;
3855
3856                 xlt1->t = (u8 *)ice_calloc(hw, xlt1->count, sizeof(*xlt1->t));
3857                 if (!xlt1->t)
3858                         goto err;
3859
3860                 xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF];
3861                 xlt2->count = blk_sizes[i].xlt2;
3862
3863                 xlt2->vsis = (struct ice_vsig_vsi *)
3864                         ice_calloc(hw, xlt2->count, sizeof(*xlt2->vsis));
3865
3866                 if (!xlt2->vsis)
3867                         goto err;
3868
3869                 xlt2->vsig_tbl = (struct ice_vsig_entry *)
3870                         ice_calloc(hw, xlt2->count, sizeof(*xlt2->vsig_tbl));
3871                 if (!xlt2->vsig_tbl)
3872                         goto err;
3873
3874                 for (j = 0; j < xlt2->count; j++)
3875                         INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst);
3876
3877                 xlt2->t = (u16 *)ice_calloc(hw, xlt2->count, sizeof(*xlt2->t));
3878                 if (!xlt2->t)
3879                         goto err;
3880
3881                 prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF];
3882                 prof->count = blk_sizes[i].prof_tcam;
3883                 prof->max_prof_id = blk_sizes[i].prof_id;
3884                 prof->cdid_bits = blk_sizes[i].prof_cdid_bits;
3885                 prof->t = (struct ice_prof_tcam_entry *)
3886                         ice_calloc(hw, prof->count, sizeof(*prof->t));
3887
3888                 if (!prof->t)
3889                         goto err;
3890
3891                 prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF];
3892                 prof_redir->count = blk_sizes[i].prof_redir;
3893                 prof_redir->t = (u8 *)ice_calloc(hw, prof_redir->count,
3894                                                  sizeof(*prof_redir->t));
3895
3896                 if (!prof_redir->t)
3897                         goto err;
3898
3899                 es->sid = ice_blk_sids[i][ICE_SID_ES_OFF];
3900                 es->count = blk_sizes[i].es;
3901                 es->fvw = blk_sizes[i].fvw;
3902                 es->t = (struct ice_fv_word *)
3903                         ice_calloc(hw, (u32)(es->count * es->fvw),
3904                                    sizeof(*es->t));
3905                 if (!es->t)
3906                         goto err;
3907
3908                 es->ref_count = (u16 *)
3909                         ice_calloc(hw, es->count, sizeof(*es->ref_count));
3910
3911                 if (!es->ref_count)
3912                         goto err;
3913
3914                 es->written = (u8 *)
3915                         ice_calloc(hw, es->count, sizeof(*es->written));
3916
3917                 if (!es->written)
3918                         goto err;
3919
3920                 es->mask_ena = (u32 *)
3921                         ice_calloc(hw, es->count, sizeof(*es->mask_ena));
3922
3923                 if (!es->mask_ena)
3924                         goto err;
3925         }
3926         return ICE_SUCCESS;
3927
3928 err:
3929         ice_free_hw_tbls(hw);
3930         return ICE_ERR_NO_MEMORY;
3931 }
3932
3933 /**
3934  * ice_prof_gen_key - generate profile ID key
3935  * @hw: pointer to the HW struct
3936  * @blk: the block in which to write profile ID to
3937  * @ptg: packet type group (PTG) portion of key
3938  * @vsig: VSIG portion of key
3939  * @cdid: CDID portion of key
3940  * @flags: flag portion of key
3941  * @vl_msk: valid mask
3942  * @dc_msk: don't care mask
3943  * @nm_msk: never match mask
3944  * @key: output of profile ID key
3945  */
3946 static enum ice_status
3947 ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig,
3948                  u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
3949                  u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],
3950                  u8 key[ICE_TCAM_KEY_SZ])
3951 {
3952         struct ice_prof_id_key inkey;
3953
3954         inkey.xlt1 = ptg;
3955         inkey.xlt2_cdid = CPU_TO_LE16(vsig);
3956         inkey.flags = CPU_TO_LE16(flags);
3957
3958         switch (hw->blk[blk].prof.cdid_bits) {
3959         case 0:
3960                 break;
3961         case 2:
3962 #define ICE_CD_2_M 0xC000U
3963 #define ICE_CD_2_S 14
3964                 inkey.xlt2_cdid &= ~CPU_TO_LE16(ICE_CD_2_M);
3965                 inkey.xlt2_cdid |= CPU_TO_LE16(BIT(cdid) << ICE_CD_2_S);
3966                 break;
3967         case 4:
3968 #define ICE_CD_4_M 0xF000U
3969 #define ICE_CD_4_S 12
3970                 inkey.xlt2_cdid &= ~CPU_TO_LE16(ICE_CD_4_M);
3971                 inkey.xlt2_cdid |= CPU_TO_LE16(BIT(cdid) << ICE_CD_4_S);
3972                 break;
3973         case 8:
3974 #define ICE_CD_8_M 0xFF00U
3975 #define ICE_CD_8_S 16
3976                 inkey.xlt2_cdid &= ~CPU_TO_LE16(ICE_CD_8_M);
3977                 inkey.xlt2_cdid |= CPU_TO_LE16(BIT(cdid) << ICE_CD_8_S);
3978                 break;
3979         default:
3980                 ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n");
3981                 break;
3982         }
3983
3984         return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk,
3985                            nm_msk, 0, ICE_TCAM_KEY_SZ / 2);
3986 }
3987
3988 /**
3989  * ice_tcam_write_entry - write TCAM entry
3990  * @hw: pointer to the HW struct
3991  * @blk: the block in which to write profile ID to
3992  * @idx: the entry index to write to
3993  * @prof_id: profile ID
3994  * @ptg: packet type group (PTG) portion of key
3995  * @vsig: VSIG portion of key
3996  * @cdid: CDID portion of key
3997  * @flags: flag portion of key
3998  * @vl_msk: valid mask
3999  * @dc_msk: don't care mask
4000  * @nm_msk: never match mask
4001  */
4002 static enum ice_status
4003 ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx,
4004                      u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags,
4005                      u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
4006                      u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],
4007                      u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])
4008 {
4009         struct ice_prof_tcam_entry;
4010         enum ice_status status;
4011
4012         status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk,
4013                                   dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key);
4014         if (!status) {
4015                 hw->blk[blk].prof.t[idx].addr = CPU_TO_LE16(idx);
4016                 hw->blk[blk].prof.t[idx].prof_id = prof_id;
4017         }
4018
4019         return status;
4020 }
4021
4022 /**
4023  * ice_vsig_get_ref - returns number of VSIs belong to a VSIG
4024  * @hw: pointer to the hardware structure
4025  * @blk: HW block
4026  * @vsig: VSIG to query
4027  * @refs: pointer to variable to receive the reference count
4028  */
4029 static enum ice_status
4030 ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs)
4031 {
4032         u16 idx = vsig & ICE_VSIG_IDX_M;
4033         struct ice_vsig_vsi *ptr;
4034
4035         *refs = 0;
4036
4037         if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
4038                 return ICE_ERR_DOES_NOT_EXIST;
4039
4040         ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
4041         while (ptr) {
4042                 (*refs)++;
4043                 ptr = ptr->next_vsi;
4044         }
4045
4046         return ICE_SUCCESS;
4047 }
4048
4049 /**
4050  * ice_has_prof_vsig - check to see if VSIG has a specific profile
4051  * @hw: pointer to the hardware structure
4052  * @blk: HW block
4053  * @vsig: VSIG to check against
4054  * @hdl: profile handle
4055  */
4056 static bool
4057 ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl)
4058 {
4059         u16 idx = vsig & ICE_VSIG_IDX_M;
4060         struct ice_vsig_prof *ent;
4061
4062         LIST_FOR_EACH_ENTRY(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4063                             ice_vsig_prof, list)
4064                 if (ent->profile_cookie == hdl)
4065                         return true;
4066
4067         ice_debug(hw, ICE_DBG_INIT,
4068                   "Characteristic list for VSI group %d not found.\n",
4069                   vsig);
4070         return false;
4071 }
4072
4073 /**
4074  * ice_prof_bld_es - build profile ID extraction sequence changes
4075  * @hw: pointer to the HW struct
4076  * @blk: hardware block
4077  * @bld: the update package buffer build to add to
4078  * @chgs: the list of changes to make in hardware
4079  */
4080 static enum ice_status
4081 ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk,
4082                 struct ice_buf_build *bld, struct LIST_HEAD_TYPE *chgs)
4083 {
4084         u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word);
4085         struct ice_chs_chg *tmp;
4086
4087         LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry)
4088                 if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) {
4089                         u16 off = tmp->prof_id * hw->blk[blk].es.fvw;
4090                         struct ice_pkg_es *p;
4091                         u32 id;
4092
4093                         id = ice_sect_id(blk, ICE_VEC_TBL);
4094                         p = (struct ice_pkg_es *)
4095                                 ice_pkg_buf_alloc_section(bld, id,
4096                                                           ice_struct_size(p, es,
4097                                                                           1) +
4098                                                           vec_size -
4099                                                           sizeof(p->es[0]));
4100
4101                         if (!p)
4102                                 return ICE_ERR_MAX_LIMIT;
4103
4104                         p->count = CPU_TO_LE16(1);
4105                         p->offset = CPU_TO_LE16(tmp->prof_id);
4106
4107                         ice_memcpy(p->es, &hw->blk[blk].es.t[off], vec_size,
4108                                    ICE_NONDMA_TO_NONDMA);
4109                 }
4110
4111         return ICE_SUCCESS;
4112 }
4113
4114 /**
4115  * ice_prof_bld_tcam - build profile ID TCAM changes
4116  * @hw: pointer to the HW struct
4117  * @blk: hardware block
4118  * @bld: the update package buffer build to add to
4119  * @chgs: the list of changes to make in hardware
4120  */
4121 static enum ice_status
4122 ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk,
4123                   struct ice_buf_build *bld, struct LIST_HEAD_TYPE *chgs)
4124 {
4125         struct ice_chs_chg *tmp;
4126
4127         LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry)
4128                 if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) {
4129                         struct ice_prof_id_section *p;
4130                         u32 id;
4131
4132                         id = ice_sect_id(blk, ICE_PROF_TCAM);
4133                         p = (struct ice_prof_id_section *)
4134                                 ice_pkg_buf_alloc_section(bld, id,
4135                                                           ice_struct_size(p,
4136                                                                           entry,
4137                                                                           1));
4138
4139                         if (!p)
4140                                 return ICE_ERR_MAX_LIMIT;
4141
4142                         p->count = CPU_TO_LE16(1);
4143                         p->entry[0].addr = CPU_TO_LE16(tmp->tcam_idx);
4144                         p->entry[0].prof_id = tmp->prof_id;
4145
4146                         ice_memcpy(p->entry[0].key,
4147                                    &hw->blk[blk].prof.t[tmp->tcam_idx].key,
4148                                    sizeof(hw->blk[blk].prof.t->key),
4149                                    ICE_NONDMA_TO_NONDMA);
4150                 }
4151
4152         return ICE_SUCCESS;
4153 }
4154
4155 /**
4156  * ice_prof_bld_xlt1 - build XLT1 changes
4157  * @blk: hardware block
4158  * @bld: the update package buffer build to add to
4159  * @chgs: the list of changes to make in hardware
4160  */
4161 static enum ice_status
4162 ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld,
4163                   struct LIST_HEAD_TYPE *chgs)
4164 {
4165         struct ice_chs_chg *tmp;
4166
4167         LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry)
4168                 if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) {
4169                         struct ice_xlt1_section *p;
4170                         u32 id;
4171
4172                         id = ice_sect_id(blk, ICE_XLT1);
4173                         p = (struct ice_xlt1_section *)
4174                                 ice_pkg_buf_alloc_section(bld, id,
4175                                                           ice_struct_size(p,
4176                                                                           value,
4177                                                                           1));
4178
4179                         if (!p)
4180                                 return ICE_ERR_MAX_LIMIT;
4181
4182                         p->count = CPU_TO_LE16(1);
4183                         p->offset = CPU_TO_LE16(tmp->ptype);
4184                         p->value[0] = tmp->ptg;
4185                 }
4186
4187         return ICE_SUCCESS;
4188 }
4189
4190 /**
4191  * ice_prof_bld_xlt2 - build XLT2 changes
4192  * @blk: hardware block
4193  * @bld: the update package buffer build to add to
4194  * @chgs: the list of changes to make in hardware
4195  */
4196 static enum ice_status
4197 ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld,
4198                   struct LIST_HEAD_TYPE *chgs)
4199 {
4200         struct ice_chs_chg *tmp;
4201
4202         LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry) {
4203                 struct ice_xlt2_section *p;
4204                 u32 id;
4205
4206                 switch (tmp->type) {
4207                 case ICE_VSIG_ADD:
4208                 case ICE_VSI_MOVE:
4209                 case ICE_VSIG_REM:
4210                         id = ice_sect_id(blk, ICE_XLT2);
4211                         p = (struct ice_xlt2_section *)
4212                                 ice_pkg_buf_alloc_section(bld, id,
4213                                                           ice_struct_size(p,
4214                                                                           value,
4215                                                                           1));
4216
4217                         if (!p)
4218                                 return ICE_ERR_MAX_LIMIT;
4219
4220                         p->count = CPU_TO_LE16(1);
4221                         p->offset = CPU_TO_LE16(tmp->vsi);
4222                         p->value[0] = CPU_TO_LE16(tmp->vsig);
4223                         break;
4224                 default:
4225                         break;
4226                 }
4227         }
4228
4229         return ICE_SUCCESS;
4230 }
4231
4232 /**
4233  * ice_upd_prof_hw - update hardware using the change list
4234  * @hw: pointer to the HW struct
4235  * @blk: hardware block
4236  * @chgs: the list of changes to make in hardware
4237  */
4238 static enum ice_status
4239 ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk,
4240                 struct LIST_HEAD_TYPE *chgs)
4241 {
4242         struct ice_buf_build *b;
4243         struct ice_chs_chg *tmp;
4244         enum ice_status status;
4245         u16 pkg_sects;
4246         u16 xlt1 = 0;
4247         u16 xlt2 = 0;
4248         u16 tcam = 0;
4249         u16 es = 0;
4250         u16 sects;
4251
4252         /* count number of sections we need */
4253         LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry) {
4254                 switch (tmp->type) {
4255                 case ICE_PTG_ES_ADD:
4256                         if (tmp->add_ptg)
4257                                 xlt1++;
4258                         if (tmp->add_prof)
4259                                 es++;
4260                         break;
4261                 case ICE_TCAM_ADD:
4262                         tcam++;
4263                         break;
4264                 case ICE_VSIG_ADD:
4265                 case ICE_VSI_MOVE:
4266                 case ICE_VSIG_REM:
4267                         xlt2++;
4268                         break;
4269                 default:
4270                         break;
4271                 }
4272         }
4273         sects = xlt1 + xlt2 + tcam + es;
4274
4275         if (!sects)
4276                 return ICE_SUCCESS;
4277
4278         /* Build update package buffer */
4279         b = ice_pkg_buf_alloc(hw);
4280         if (!b)
4281                 return ICE_ERR_NO_MEMORY;
4282
4283         status = ice_pkg_buf_reserve_section(b, sects);
4284         if (status)
4285                 goto error_tmp;
4286
4287         /* Preserve order of table update: ES, TCAM, PTG, VSIG */
4288         if (es) {
4289                 status = ice_prof_bld_es(hw, blk, b, chgs);
4290                 if (status)
4291                         goto error_tmp;
4292         }
4293
4294         if (tcam) {
4295                 status = ice_prof_bld_tcam(hw, blk, b, chgs);
4296                 if (status)
4297                         goto error_tmp;
4298         }
4299
4300         if (xlt1) {
4301                 status = ice_prof_bld_xlt1(blk, b, chgs);
4302                 if (status)
4303                         goto error_tmp;
4304         }
4305
4306         if (xlt2) {
4307                 status = ice_prof_bld_xlt2(blk, b, chgs);
4308                 if (status)
4309                         goto error_tmp;
4310         }
4311
4312         /* After package buffer build check if the section count in buffer is
4313          * non-zero and matches the number of sections detected for package
4314          * update.
4315          */
4316         pkg_sects = ice_pkg_buf_get_active_sections(b);
4317         if (!pkg_sects || pkg_sects != sects) {
4318                 status = ICE_ERR_INVAL_SIZE;
4319                 goto error_tmp;
4320         }
4321
4322         /* update package */
4323         status = ice_update_pkg(hw, ice_pkg_buf(b), 1);
4324         if (status == ICE_ERR_AQ_ERROR)
4325                 ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n");
4326
4327 error_tmp:
4328         ice_pkg_buf_free(hw, b);
4329         return status;
4330 }
4331
4332 /**
4333  * ice_update_fd_mask - set Flow Director Field Vector mask for a profile
4334  * @hw: pointer to the HW struct
4335  * @prof_id: profile ID
4336  * @mask_sel: mask select
4337  *
4338  * This function enable any of the masks selected by the mask select parameter
4339  * for the profile specified.
4340  */
4341 static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel)
4342 {
4343         wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel);
4344
4345         ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id,
4346                   GLQF_FDMASK_SEL(prof_id), mask_sel);
4347 }
4348
4349 struct ice_fd_src_dst_pair {
4350         u8 prot_id;
4351         u8 count;
4352         u16 off;
4353 };
4354
4355 static const struct ice_fd_src_dst_pair ice_fd_pairs[] = {
4356         /* These are defined in pairs */
4357         { ICE_PROT_IPV4_OF_OR_S, 2, 12 },
4358         { ICE_PROT_IPV4_OF_OR_S, 2, 16 },
4359
4360         { ICE_PROT_IPV4_IL, 2, 12 },
4361         { ICE_PROT_IPV4_IL, 2, 16 },
4362
4363         { ICE_PROT_IPV6_OF_OR_S, 8, 8 },
4364         { ICE_PROT_IPV6_OF_OR_S, 8, 24 },
4365
4366         { ICE_PROT_IPV6_IL, 8, 8 },
4367         { ICE_PROT_IPV6_IL, 8, 24 },
4368
4369         { ICE_PROT_TCP_IL, 1, 0 },
4370         { ICE_PROT_TCP_IL, 1, 2 },
4371
4372         { ICE_PROT_UDP_OF, 1, 0 },
4373         { ICE_PROT_UDP_OF, 1, 2 },
4374
4375         { ICE_PROT_UDP_IL_OR_S, 1, 0 },
4376         { ICE_PROT_UDP_IL_OR_S, 1, 2 },
4377
4378         { ICE_PROT_SCTP_IL, 1, 0 },
4379         { ICE_PROT_SCTP_IL, 1, 2 }
4380 };
4381
4382 #define ICE_FD_SRC_DST_PAIR_COUNT       ARRAY_SIZE(ice_fd_pairs)
4383
4384 /**
4385  * ice_update_fd_swap - set register appropriately for a FD FV extraction
4386  * @hw: pointer to the HW struct
4387  * @prof_id: profile ID
4388  * @es: extraction sequence (length of array is determined by the block)
4389  */
4390 static enum ice_status
4391 ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es)
4392 {
4393         ice_declare_bitmap(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
4394         u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 };
4395 #define ICE_FD_FV_NOT_FOUND (-2)
4396         s8 first_free = ICE_FD_FV_NOT_FOUND;
4397         u8 used[ICE_MAX_FV_WORDS] = { 0 };
4398         s8 orig_free, si;
4399         u32 mask_sel = 0;
4400         u8 i, j, k;
4401
4402         ice_zero_bitmap(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
4403
4404         /* This code assumes that the Flow Director field vectors are assigned
4405          * from the end of the FV indexes working towards the zero index, that
4406          * only complete fields will be included and will be consecutive, and
4407          * that there are no gaps between valid indexes.
4408          */
4409
4410         /* Determine swap fields present */
4411         for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) {
4412                 /* Find the first free entry, assuming right to left population.
4413                  * This is where we can start adding additional pairs if needed.
4414                  */
4415                 if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id !=
4416                     ICE_PROT_INVALID)
4417                         first_free = i - 1;
4418
4419                 for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
4420                         if (es[i].prot_id == ice_fd_pairs[j].prot_id &&
4421                             es[i].off == ice_fd_pairs[j].off) {
4422                                 ice_set_bit(j, pair_list);
4423                                 pair_start[j] = i;
4424                         }
4425         }
4426
4427         orig_free = first_free;
4428
4429         /* determine missing swap fields that need to be added */
4430         for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) {
4431                 u8 bit1 = ice_is_bit_set(pair_list, i + 1);
4432                 u8 bit0 = ice_is_bit_set(pair_list, i);
4433
4434                 if (bit0 ^ bit1) {
4435                         u8 index;
4436
4437                         /* add the appropriate 'paired' entry */
4438                         if (!bit0)
4439                                 index = i;
4440                         else
4441                                 index = i + 1;
4442
4443                         /* check for room */
4444                         if (first_free + 1 < (s8)ice_fd_pairs[index].count)
4445                                 return ICE_ERR_MAX_LIMIT;
4446
4447                         /* place in extraction sequence */
4448                         for (k = 0; k < ice_fd_pairs[index].count; k++) {
4449                                 es[first_free - k].prot_id =
4450                                         ice_fd_pairs[index].prot_id;
4451                                 es[first_free - k].off =
4452                                         ice_fd_pairs[index].off + (k * 2);
4453
4454                                 if (k > first_free)
4455                                         return ICE_ERR_OUT_OF_RANGE;
4456
4457                                 /* keep track of non-relevant fields */
4458                                 mask_sel |= BIT(first_free - k);
4459                         }
4460
4461                         pair_start[index] = first_free;
4462                         first_free -= ice_fd_pairs[index].count;
4463                 }
4464         }
4465
4466         /* fill in the swap array */
4467         si = hw->blk[ICE_BLK_FD].es.fvw - 1;
4468         while (si >= 0) {
4469                 u8 indexes_used = 1;
4470
4471                 /* assume flat at this index */
4472 #define ICE_SWAP_VALID  0x80
4473                 used[si] = si | ICE_SWAP_VALID;
4474
4475                 if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) {
4476                         si -= indexes_used;
4477                         continue;
4478                 }
4479
4480                 /* check for a swap location */
4481                 for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
4482                         if (es[si].prot_id == ice_fd_pairs[j].prot_id &&
4483                             es[si].off == ice_fd_pairs[j].off) {
4484                                 u8 idx;
4485
4486                                 /* determine the appropriate matching field */
4487                                 idx = j + ((j % 2) ? -1 : 1);
4488
4489                                 indexes_used = ice_fd_pairs[idx].count;
4490                                 for (k = 0; k < indexes_used; k++) {
4491                                         used[si - k] = (pair_start[idx] - k) |
4492                                                 ICE_SWAP_VALID;
4493                                 }
4494
4495                                 break;
4496                         }
4497
4498                 si -= indexes_used;
4499         }
4500
4501         /* for each set of 4 swap and 4 inset indexes, write the appropriate
4502          * register
4503          */
4504         for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) {
4505                 u32 raw_swap = 0;
4506                 u32 raw_in = 0;
4507
4508                 for (k = 0; k < 4; k++) {
4509                         u8 idx;
4510
4511                         idx = (j * 4) + k;
4512                         if (used[idx] && !(mask_sel & BIT(idx))) {
4513                                 raw_swap |= used[idx] << (k * BITS_PER_BYTE);
4514 #define ICE_INSET_DFLT 0x9f
4515                                 raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE);
4516                         }
4517                 }
4518
4519                 /* write the appropriate swap register set */
4520                 wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap);
4521
4522                 ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n",
4523                           prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap);
4524
4525                 /* write the appropriate inset register set */
4526                 wr32(hw, GLQF_FDINSET(prof_id, j), raw_in);
4527
4528                 ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n",
4529                           prof_id, j, GLQF_FDINSET(prof_id, j), raw_in);
4530         }
4531
4532         /* initially clear the mask select for this profile */
4533         ice_update_fd_mask(hw, prof_id, 0);
4534
4535         return ICE_SUCCESS;
4536 }
4537
4538 /* The entries here needs to match the order of enum ice_ptype_attrib */
4539 static const struct ice_ptype_attrib_info ice_ptype_attributes[] = {
4540         { ICE_GTP_PDU_EH,       ICE_GTP_PDU_FLAG_MASK },
4541         { ICE_GTP_SESSION,      ICE_GTP_FLAGS_MASK },
4542         { ICE_GTP_DOWNLINK,     ICE_GTP_FLAGS_MASK },
4543         { ICE_GTP_UPLINK,       ICE_GTP_FLAGS_MASK },
4544 };
4545
4546 /**
4547  * ice_get_ptype_attrib_info - get ptype attribute information
4548  * @type: attribute type
4549  * @info: pointer to variable to the attribute information
4550  */
4551 static void
4552 ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type,
4553                           struct ice_ptype_attrib_info *info)
4554 {
4555         *info = ice_ptype_attributes[type];
4556 }
4557
4558 /**
4559  * ice_add_prof_attrib - add any PTG with attributes to profile
4560  * @prof: pointer to the profile to which PTG entries will be added
4561  * @ptg: PTG to be added
4562  * @ptype: PTYPE that needs to be looked up
4563  * @attr: array of attributes that will be considered
4564  * @attr_cnt: number of elements in the attribute array
4565  */
4566 static enum ice_status
4567 ice_add_prof_attrib(struct ice_prof_map *prof, u8 ptg, u16 ptype,
4568                     const struct ice_ptype_attributes *attr, u16 attr_cnt)
4569 {
4570         bool found = false;
4571         u16 i;
4572
4573         for (i = 0; i < attr_cnt; i++) {
4574                 if (attr[i].ptype == ptype) {
4575                         found = true;
4576
4577                         prof->ptg[prof->ptg_cnt] = ptg;
4578                         ice_get_ptype_attrib_info(attr[i].attrib,
4579                                                   &prof->attr[prof->ptg_cnt]);
4580
4581                         if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
4582                                 return ICE_ERR_MAX_LIMIT;
4583                 }
4584         }
4585
4586         if (!found)
4587                 return ICE_ERR_DOES_NOT_EXIST;
4588
4589         return ICE_SUCCESS;
4590 }
4591
4592 /**
4593  * ice_add_prof - add profile
4594  * @hw: pointer to the HW struct
4595  * @blk: hardware block
4596  * @id: profile tracking ID
4597  * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits)
4598  * @attr: array of attributes
4599  * @attr_cnt: number of elements in attrib array
4600  * @es: extraction sequence (length of array is determined by the block)
4601  * @masks: mask for extraction sequence
4602  *
4603  * This function registers a profile, which matches a set of PTYPES with a
4604  * particular extraction sequence. While the hardware profile is allocated
4605  * it will not be written until the first call to ice_add_flow that specifies
4606  * the ID value used here.
4607  */
4608 enum ice_status
4609 ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, u8 ptypes[],
4610              const struct ice_ptype_attributes *attr, u16 attr_cnt,
4611              struct ice_fv_word *es, u16 *masks)
4612 {
4613         u32 bytes = DIVIDE_AND_ROUND_UP(ICE_FLOW_PTYPE_MAX, BITS_PER_BYTE);
4614         ice_declare_bitmap(ptgs_used, ICE_XLT1_CNT);
4615         struct ice_prof_map *prof;
4616         enum ice_status status;
4617         u8 byte = 0;
4618         u8 prof_id;
4619
4620         ice_zero_bitmap(ptgs_used, ICE_XLT1_CNT);
4621
4622         ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
4623
4624         /* search for existing profile */
4625         status = ice_find_prof_id_with_mask(hw, blk, es, masks, &prof_id);
4626         if (status) {
4627                 /* allocate profile ID */
4628                 status = ice_alloc_prof_id(hw, blk, &prof_id);
4629                 if (status)
4630                         goto err_ice_add_prof;
4631                 if (blk == ICE_BLK_FD) {
4632                         /* For Flow Director block, the extraction sequence may
4633                          * need to be altered in the case where there are paired
4634                          * fields that have no match. This is necessary because
4635                          * for Flow Director, src and dest fields need to paired
4636                          * for filter programming and these values are swapped
4637                          * during Tx.
4638                          */
4639                         status = ice_update_fd_swap(hw, prof_id, es);
4640                         if (status)
4641                                 goto err_ice_add_prof;
4642                 }
4643                 status = ice_update_prof_masking(hw, blk, prof_id, masks);
4644                 if (status)
4645                         goto err_ice_add_prof;
4646
4647                 /* and write new es */
4648                 ice_write_es(hw, blk, prof_id, es);
4649         }
4650
4651         ice_prof_inc_ref(hw, blk, prof_id);
4652
4653         /* add profile info */
4654
4655         prof = (struct ice_prof_map *)ice_malloc(hw, sizeof(*prof));
4656         if (!prof)
4657                 goto err_ice_add_prof;
4658
4659         prof->profile_cookie = id;
4660         prof->prof_id = prof_id;
4661         prof->ptg_cnt = 0;
4662         prof->context = 0;
4663
4664         /* build list of ptgs */
4665         while (bytes && prof->ptg_cnt < ICE_MAX_PTG_PER_PROFILE) {
4666                 u8 bit;
4667
4668                 if (!ptypes[byte]) {
4669                         bytes--;
4670                         byte++;
4671                         continue;
4672                 }
4673                 /* Examine 8 bits per byte */
4674                 for (bit = 0; bit < 8; bit++) {
4675                         if (ptypes[byte] & BIT(bit)) {
4676                                 u16 ptype;
4677                                 u8 ptg;
4678                                 u8 m;
4679
4680                                 ptype = byte * BITS_PER_BYTE + bit;
4681
4682                                 /* The package should place all ptypes in a
4683                                  * non-zero PTG, so the following call should
4684                                  * never fail.
4685                                  */
4686                                 if (ice_ptg_find_ptype(hw, blk, ptype, &ptg))
4687                                         continue;
4688
4689                                 /* If PTG is already added, skip and continue */
4690                                 if (ice_is_bit_set(ptgs_used, ptg))
4691                                         continue;
4692
4693                                 ice_set_bit(ptg, ptgs_used);
4694                                 /* Check to see there are any attributes for
4695                                  * this ptype, and add them if found.
4696                                  */
4697                                 status = ice_add_prof_attrib(prof, ptg, ptype,
4698                                                              attr, attr_cnt);
4699                                 if (status == ICE_ERR_MAX_LIMIT)
4700                                         break;
4701                                 if (status) {
4702                                         /* This is simple a ptype/PTG with no
4703                                          * attribute
4704                                          */
4705                                         prof->ptg[prof->ptg_cnt] = ptg;
4706                                         prof->attr[prof->ptg_cnt].flags = 0;
4707                                         prof->attr[prof->ptg_cnt].mask = 0;
4708
4709                                         if (++prof->ptg_cnt >=
4710                                             ICE_MAX_PTG_PER_PROFILE)
4711                                                 break;
4712                                 }
4713
4714                                 /* nothing left in byte, then exit */
4715                                 m = ~(u8)((1 << (bit + 1)) - 1);
4716                                 if (!(ptypes[byte] & m))
4717                                         break;
4718                         }
4719                 }
4720
4721                 bytes--;
4722                 byte++;
4723         }
4724
4725         LIST_ADD(&prof->list, &hw->blk[blk].es.prof_map);
4726         status = ICE_SUCCESS;
4727
4728 err_ice_add_prof:
4729         ice_release_lock(&hw->blk[blk].es.prof_map_lock);
4730         return status;
4731 }
4732
4733 /**
4734  * ice_search_prof_id - Search for a profile tracking ID
4735  * @hw: pointer to the HW struct
4736  * @blk: hardware block
4737  * @id: profile tracking ID
4738  *
4739  * This will search for a profile tracking ID which was previously added.
4740  * The profile map lock should be held before calling this function.
4741  */
4742 struct ice_prof_map *
4743 ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id)
4744 {
4745         struct ice_prof_map *entry = NULL;
4746         struct ice_prof_map *map;
4747
4748         LIST_FOR_EACH_ENTRY(map, &hw->blk[blk].es.prof_map, ice_prof_map, list)
4749                 if (map->profile_cookie == id) {
4750                         entry = map;
4751                         break;
4752                 }
4753
4754         return entry;
4755 }
4756
4757 /**
4758  * ice_vsig_prof_id_count - count profiles in a VSIG
4759  * @hw: pointer to the HW struct
4760  * @blk: hardware block
4761  * @vsig: VSIG to remove the profile from
4762  */
4763 static u16
4764 ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig)
4765 {
4766         u16 idx = vsig & ICE_VSIG_IDX_M, count = 0;
4767         struct ice_vsig_prof *p;
4768
4769         LIST_FOR_EACH_ENTRY(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4770                             ice_vsig_prof, list)
4771                 count++;
4772
4773         return count;
4774 }
4775
4776 /**
4777  * ice_rel_tcam_idx - release a TCAM index
4778  * @hw: pointer to the HW struct
4779  * @blk: hardware block
4780  * @idx: the index to release
4781  */
4782 static enum ice_status
4783 ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx)
4784 {
4785         /* Masks to invoke a never match entry */
4786         u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4787         u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF };
4788         u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
4789         enum ice_status status;
4790
4791         /* write the TCAM entry */
4792         status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk,
4793                                       dc_msk, nm_msk);
4794         if (status)
4795                 return status;
4796
4797         /* release the TCAM entry */
4798         status = ice_free_tcam_ent(hw, blk, idx);
4799
4800         return status;
4801 }
4802
4803 /**
4804  * ice_rem_prof_id - remove one profile from a VSIG
4805  * @hw: pointer to the HW struct
4806  * @blk: hardware block
4807  * @prof: pointer to profile structure to remove
4808  */
4809 static enum ice_status
4810 ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk,
4811                 struct ice_vsig_prof *prof)
4812 {
4813         enum ice_status status;
4814         u16 i;
4815
4816         for (i = 0; i < prof->tcam_count; i++)
4817                 if (prof->tcam[i].in_use) {
4818                         prof->tcam[i].in_use = false;
4819                         status = ice_rel_tcam_idx(hw, blk,
4820                                                   prof->tcam[i].tcam_idx);
4821                         if (status)
4822                                 return ICE_ERR_HW_TABLE;
4823                 }
4824
4825         return ICE_SUCCESS;
4826 }
4827
4828 /**
4829  * ice_rem_vsig - remove VSIG
4830  * @hw: pointer to the HW struct
4831  * @blk: hardware block
4832  * @vsig: the VSIG to remove
4833  * @chg: the change list
4834  */
4835 static enum ice_status
4836 ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
4837              struct LIST_HEAD_TYPE *chg)
4838 {
4839         u16 idx = vsig & ICE_VSIG_IDX_M;
4840         struct ice_vsig_vsi *vsi_cur;
4841         struct ice_vsig_prof *d, *t;
4842         enum ice_status status;
4843
4844         /* remove TCAM entries */
4845         LIST_FOR_EACH_ENTRY_SAFE(d, t,
4846                                  &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4847                                  ice_vsig_prof, list) {
4848                 status = ice_rem_prof_id(hw, blk, d);
4849                 if (status)
4850                         return status;
4851
4852                 LIST_DEL(&d->list);
4853                 ice_free(hw, d);
4854         }
4855
4856         /* Move all VSIS associated with this VSIG to the default VSIG */
4857         vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
4858         /* If the VSIG has at least 1 VSI then iterate through the list
4859          * and remove the VSIs before deleting the group.
4860          */
4861         if (vsi_cur)
4862                 do {
4863                         struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
4864                         struct ice_chs_chg *p;
4865
4866                         p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
4867                         if (!p)
4868                                 return ICE_ERR_NO_MEMORY;
4869
4870                         p->type = ICE_VSIG_REM;
4871                         p->orig_vsig = vsig;
4872                         p->vsig = ICE_DEFAULT_VSIG;
4873                         p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis;
4874
4875                         LIST_ADD(&p->list_entry, chg);
4876
4877                         vsi_cur = tmp;
4878                 } while (vsi_cur);
4879
4880         return ice_vsig_free(hw, blk, vsig);
4881 }
4882
4883 /**
4884  * ice_rem_prof_id_vsig - remove a specific profile from a VSIG
4885  * @hw: pointer to the HW struct
4886  * @blk: hardware block
4887  * @vsig: VSIG to remove the profile from
4888  * @hdl: profile handle indicating which profile to remove
4889  * @chg: list to receive a record of changes
4890  */
4891 static enum ice_status
4892 ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
4893                      struct LIST_HEAD_TYPE *chg)
4894 {
4895         u16 idx = vsig & ICE_VSIG_IDX_M;
4896         struct ice_vsig_prof *p, *t;
4897         enum ice_status status;
4898
4899         LIST_FOR_EACH_ENTRY_SAFE(p, t,
4900                                  &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4901                                  ice_vsig_prof, list)
4902                 if (p->profile_cookie == hdl) {
4903                         if (ice_vsig_prof_id_count(hw, blk, vsig) == 1)
4904                                 /* this is the last profile, remove the VSIG */
4905                                 return ice_rem_vsig(hw, blk, vsig, chg);
4906
4907                         status = ice_rem_prof_id(hw, blk, p);
4908                         if (!status) {
4909                                 LIST_DEL(&p->list);
4910                                 ice_free(hw, p);
4911                         }
4912                         return status;
4913                 }
4914
4915         return ICE_ERR_DOES_NOT_EXIST;
4916 }
4917
4918 /**
4919  * ice_rem_flow_all - remove all flows with a particular profile
4920  * @hw: pointer to the HW struct
4921  * @blk: hardware block
4922  * @id: profile tracking ID
4923  */
4924 static enum ice_status
4925 ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id)
4926 {
4927         struct ice_chs_chg *del, *tmp;
4928         struct LIST_HEAD_TYPE chg;
4929         enum ice_status status;
4930         u16 i;
4931
4932         INIT_LIST_HEAD(&chg);
4933
4934         for (i = 1; i < ICE_MAX_VSIGS; i++)
4935                 if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) {
4936                         if (ice_has_prof_vsig(hw, blk, i, id)) {
4937                                 status = ice_rem_prof_id_vsig(hw, blk, i, id,
4938                                                               &chg);
4939                                 if (status)
4940                                         goto err_ice_rem_flow_all;
4941                         }
4942                 }
4943
4944         status = ice_upd_prof_hw(hw, blk, &chg);
4945
4946 err_ice_rem_flow_all:
4947         LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
4948                 LIST_DEL(&del->list_entry);
4949                 ice_free(hw, del);
4950         }
4951
4952         return status;
4953 }
4954
4955 /**
4956  * ice_rem_prof - remove profile
4957  * @hw: pointer to the HW struct
4958  * @blk: hardware block
4959  * @id: profile tracking ID
4960  *
4961  * This will remove the profile specified by the ID parameter, which was
4962  * previously created through ice_add_prof. If any existing entries
4963  * are associated with this profile, they will be removed as well.
4964  */
4965 enum ice_status ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id)
4966 {
4967         struct ice_prof_map *pmap;
4968         enum ice_status status;
4969
4970         ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
4971
4972         pmap = ice_search_prof_id(hw, blk, id);
4973         if (!pmap) {
4974                 status = ICE_ERR_DOES_NOT_EXIST;
4975                 goto err_ice_rem_prof;
4976         }
4977
4978         /* remove all flows with this profile */
4979         status = ice_rem_flow_all(hw, blk, pmap->profile_cookie);
4980         if (status)
4981                 goto err_ice_rem_prof;
4982
4983         /* dereference profile, and possibly remove */
4984         ice_prof_dec_ref(hw, blk, pmap->prof_id);
4985
4986         LIST_DEL(&pmap->list);
4987         ice_free(hw, pmap);
4988
4989 err_ice_rem_prof:
4990         ice_release_lock(&hw->blk[blk].es.prof_map_lock);
4991         return status;
4992 }
4993
4994 /**
4995  * ice_get_prof - get profile
4996  * @hw: pointer to the HW struct
4997  * @blk: hardware block
4998  * @hdl: profile handle
4999  * @chg: change list
5000  */
5001 static enum ice_status
5002 ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl,
5003              struct LIST_HEAD_TYPE *chg)
5004 {
5005         enum ice_status status = ICE_SUCCESS;
5006         struct ice_prof_map *map;
5007         struct ice_chs_chg *p;
5008         u16 i;
5009
5010         ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
5011         /* Get the details on the profile specified by the handle ID */
5012         map = ice_search_prof_id(hw, blk, hdl);
5013         if (!map) {
5014                 status = ICE_ERR_DOES_NOT_EXIST;
5015                 goto err_ice_get_prof;
5016         }
5017
5018         for (i = 0; i < map->ptg_cnt; i++)
5019                 if (!hw->blk[blk].es.written[map->prof_id]) {
5020                         /* add ES to change list */
5021                         p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5022                         if (!p) {
5023                                 status = ICE_ERR_NO_MEMORY;
5024                                 goto err_ice_get_prof;
5025                         }
5026
5027                         p->type = ICE_PTG_ES_ADD;
5028                         p->ptype = 0;
5029                         p->ptg = map->ptg[i];
5030                         p->attr = map->attr[i];
5031                         p->add_ptg = 0;
5032
5033                         p->add_prof = 1;
5034                         p->prof_id = map->prof_id;
5035
5036                         hw->blk[blk].es.written[map->prof_id] = true;
5037
5038                         LIST_ADD(&p->list_entry, chg);
5039                 }
5040
5041 err_ice_get_prof:
5042         ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5043         /* let caller clean up the change list */
5044         return status;
5045 }
5046
5047 /**
5048  * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG
5049  * @hw: pointer to the HW struct
5050  * @blk: hardware block
5051  * @vsig: VSIG from which to copy the list
5052  * @lst: output list
5053  *
5054  * This routine makes a copy of the list of profiles in the specified VSIG.
5055  */
5056 static enum ice_status
5057 ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5058                    struct LIST_HEAD_TYPE *lst)
5059 {
5060         struct ice_vsig_prof *ent1, *ent2;
5061         u16 idx = vsig & ICE_VSIG_IDX_M;
5062
5063         LIST_FOR_EACH_ENTRY(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5064                             ice_vsig_prof, list) {
5065                 struct ice_vsig_prof *p;
5066
5067                 /* copy to the input list */
5068                 p = (struct ice_vsig_prof *)ice_memdup(hw, ent1, sizeof(*p),
5069                                                        ICE_NONDMA_TO_NONDMA);
5070                 if (!p)
5071                         goto err_ice_get_profs_vsig;
5072
5073                 LIST_ADD_TAIL(&p->list, lst);
5074         }
5075
5076         return ICE_SUCCESS;
5077
5078 err_ice_get_profs_vsig:
5079         LIST_FOR_EACH_ENTRY_SAFE(ent1, ent2, lst, ice_vsig_prof, list) {
5080                 LIST_DEL(&ent1->list);
5081                 ice_free(hw, ent1);
5082         }
5083
5084         return ICE_ERR_NO_MEMORY;
5085 }
5086
5087 /**
5088  * ice_add_prof_to_lst - add profile entry to a list
5089  * @hw: pointer to the HW struct
5090  * @blk: hardware block
5091  * @lst: the list to be added to
5092  * @hdl: profile handle of entry to add
5093  */
5094 static enum ice_status
5095 ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk,
5096                     struct LIST_HEAD_TYPE *lst, u64 hdl)
5097 {
5098         enum ice_status status = ICE_SUCCESS;
5099         struct ice_prof_map *map;
5100         struct ice_vsig_prof *p;
5101         u16 i;
5102
5103         ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
5104         map = ice_search_prof_id(hw, blk, hdl);
5105         if (!map) {
5106                 status = ICE_ERR_DOES_NOT_EXIST;
5107                 goto err_ice_add_prof_to_lst;
5108         }
5109
5110         p = (struct ice_vsig_prof *)ice_malloc(hw, sizeof(*p));
5111         if (!p) {
5112                 status = ICE_ERR_NO_MEMORY;
5113                 goto err_ice_add_prof_to_lst;
5114         }
5115
5116         p->profile_cookie = map->profile_cookie;
5117         p->prof_id = map->prof_id;
5118         p->tcam_count = map->ptg_cnt;
5119
5120         for (i = 0; i < map->ptg_cnt; i++) {
5121                 p->tcam[i].prof_id = map->prof_id;
5122                 p->tcam[i].tcam_idx = ICE_INVALID_TCAM;
5123                 p->tcam[i].ptg = map->ptg[i];
5124                 p->tcam[i].attr = map->attr[i];
5125         }
5126
5127         LIST_ADD(&p->list, lst);
5128
5129 err_ice_add_prof_to_lst:
5130         ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5131         return status;
5132 }
5133
5134 /**
5135  * ice_move_vsi - move VSI to another VSIG
5136  * @hw: pointer to the HW struct
5137  * @blk: hardware block
5138  * @vsi: the VSI to move
5139  * @vsig: the VSIG to move the VSI to
5140  * @chg: the change list
5141  */
5142 static enum ice_status
5143 ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig,
5144              struct LIST_HEAD_TYPE *chg)
5145 {
5146         enum ice_status status;
5147         struct ice_chs_chg *p;
5148         u16 orig_vsig;
5149
5150         p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5151         if (!p)
5152                 return ICE_ERR_NO_MEMORY;
5153
5154         status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
5155         if (!status)
5156                 status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
5157
5158         if (status) {
5159                 ice_free(hw, p);
5160                 return status;
5161         }
5162
5163         p->type = ICE_VSI_MOVE;
5164         p->vsi = vsi;
5165         p->orig_vsig = orig_vsig;
5166         p->vsig = vsig;
5167
5168         LIST_ADD(&p->list_entry, chg);
5169
5170         return ICE_SUCCESS;
5171 }
5172
5173 /**
5174  * ice_set_tcam_flags - set TCAM flag don't care mask
5175  * @mask: mask for flags
5176  * @dc_mask: pointer to the don't care mask
5177  */
5178 static void ice_set_tcam_flags(u16 mask, u8 dc_mask[ICE_TCAM_KEY_VAL_SZ])
5179 {
5180         u16 *flag_word;
5181
5182         /* flags are lowest u16 */
5183         flag_word = (u16 *)dc_mask;
5184         *flag_word = ~mask;
5185 }
5186
5187 /**
5188  * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list
5189  * @hw: pointer to the HW struct
5190  * @idx: the index of the TCAM entry to remove
5191  * @chg: the list of change structures to search
5192  */
5193 static void
5194 ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct LIST_HEAD_TYPE *chg)
5195 {
5196         struct ice_chs_chg *pos, *tmp;
5197
5198         LIST_FOR_EACH_ENTRY_SAFE(tmp, pos, chg, ice_chs_chg, list_entry)
5199                 if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) {
5200                         LIST_DEL(&tmp->list_entry);
5201                         ice_free(hw, tmp);
5202                 }
5203 }
5204
5205 /**
5206  * ice_prof_tcam_ena_dis - add enable or disable TCAM change
5207  * @hw: pointer to the HW struct
5208  * @blk: hardware block
5209  * @enable: true to enable, false to disable
5210  * @vsig: the VSIG of the TCAM entry
5211  * @tcam: pointer the TCAM info structure of the TCAM to disable
5212  * @chg: the change list
5213  *
5214  * This function appends an enable or disable TCAM entry in the change log
5215  */
5216 static enum ice_status
5217 ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable,
5218                       u16 vsig, struct ice_tcam_inf *tcam,
5219                       struct LIST_HEAD_TYPE *chg)
5220 {
5221         enum ice_status status;
5222         struct ice_chs_chg *p;
5223
5224         u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5225         u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
5226         u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
5227
5228         /* if disabling, free the TCAM */
5229         if (!enable) {
5230                 status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx);
5231
5232                 /* if we have already created a change for this TCAM entry, then
5233                  * we need to remove that entry, in order to prevent writing to
5234                  * a TCAM entry we no longer will have ownership of.
5235                  */
5236                 ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg);
5237                 tcam->tcam_idx = 0;
5238                 tcam->in_use = 0;
5239                 return status;
5240         }
5241
5242         /* for re-enabling, reallocate a TCAM */
5243         /* for entries with empty attribute masks, allocate entry from
5244          * the bottom of the tcam table; otherwise, allocate from the
5245          * top of the table in order to give it higher priority
5246          */
5247         status = ice_alloc_tcam_ent(hw, blk, tcam->attr.mask == 0,
5248                                     &tcam->tcam_idx);
5249         if (status)
5250                 return status;
5251
5252         /* add TCAM to change list */
5253         p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5254         if (!p)
5255                 return ICE_ERR_NO_MEMORY;
5256
5257         /* set don't care masks for TCAM flags */
5258         ice_set_tcam_flags(tcam->attr.mask, dc_msk);
5259
5260         status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id,
5261                                       tcam->ptg, vsig, 0, tcam->attr.flags,
5262                                       vl_msk, dc_msk, nm_msk);
5263         if (status)
5264                 goto err_ice_prof_tcam_ena_dis;
5265
5266         tcam->in_use = 1;
5267
5268         p->type = ICE_TCAM_ADD;
5269         p->add_tcam_idx = true;
5270         p->prof_id = tcam->prof_id;
5271         p->ptg = tcam->ptg;
5272         p->vsig = 0;
5273         p->tcam_idx = tcam->tcam_idx;
5274
5275         /* log change */
5276         LIST_ADD(&p->list_entry, chg);
5277
5278         return ICE_SUCCESS;
5279
5280 err_ice_prof_tcam_ena_dis:
5281         ice_free(hw, p);
5282         return status;
5283 }
5284
5285 /**
5286  * ice_ptg_attr_in_use - determine if PTG and attribute pair is in use
5287  * @ptg_attr: pointer to the PTG and attribute pair to check
5288  * @ptgs_used: bitmap that denotes which PTGs are in use
5289  * @attr_used: array of PTG and attributes pairs already used
5290  * @attr_cnt: count of entries in the attr_used array
5291  */
5292 static bool
5293 ice_ptg_attr_in_use(struct ice_tcam_inf *ptg_attr, ice_bitmap_t *ptgs_used,
5294                     struct ice_tcam_inf *attr_used[], u16 attr_cnt)
5295 {
5296         u16 i;
5297
5298         if (!ice_is_bit_set(ptgs_used, ptg_attr->ptg))
5299                 return false;
5300
5301         /* the PTG is used, so now look for correct attributes */
5302         for (i = 0; i < attr_cnt; i++)
5303                 if (attr_used[i]->ptg == ptg_attr->ptg &&
5304                     attr_used[i]->attr.flags == ptg_attr->attr.flags &&
5305                     attr_used[i]->attr.mask == ptg_attr->attr.mask)
5306                         return true;
5307
5308         return false;
5309 }
5310
5311 /**
5312  * ice_adj_prof_priorities - adjust profile based on priorities
5313  * @hw: pointer to the HW struct
5314  * @blk: hardware block
5315  * @vsig: the VSIG for which to adjust profile priorities
5316  * @chg: the change list
5317  */
5318 static enum ice_status
5319 ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5320                         struct LIST_HEAD_TYPE *chg)
5321 {
5322         ice_declare_bitmap(ptgs_used, ICE_XLT1_CNT);
5323         struct ice_tcam_inf **attr_used;
5324         enum ice_status status = ICE_SUCCESS;
5325         struct ice_vsig_prof *t;
5326         u16 attr_used_cnt = 0;
5327         u16 idx;
5328
5329 #define ICE_MAX_PTG_ATTRS       1024
5330         attr_used = (struct ice_tcam_inf **)ice_calloc(hw, ICE_MAX_PTG_ATTRS,
5331                                                        sizeof(*attr_used));
5332         if (!attr_used)
5333                 return ICE_ERR_NO_MEMORY;
5334
5335         ice_zero_bitmap(ptgs_used, ICE_XLT1_CNT);
5336         idx = vsig & ICE_VSIG_IDX_M;
5337
5338         /* Priority is based on the order in which the profiles are added. The
5339          * newest added profile has highest priority and the oldest added
5340          * profile has the lowest priority. Since the profile property list for
5341          * a VSIG is sorted from newest to oldest, this code traverses the list
5342          * in order and enables the first of each PTG that it finds (that is not
5343          * already enabled); it also disables any duplicate PTGs that it finds
5344          * in the older profiles (that are currently enabled).
5345          */
5346
5347         LIST_FOR_EACH_ENTRY(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5348                             ice_vsig_prof, list) {
5349                 u16 i;
5350
5351                 for (i = 0; i < t->tcam_count; i++) {
5352                         bool used;
5353
5354                         /* Scan the priorities from newest to oldest.
5355                          * Make sure that the newest profiles take priority.
5356                          */
5357                         used = ice_ptg_attr_in_use(&t->tcam[i], ptgs_used,
5358                                                    attr_used, attr_used_cnt);
5359
5360                         if (used && t->tcam[i].in_use) {
5361                                 /* need to mark this PTG as never match, as it
5362                                  * was already in use and therefore duplicate
5363                                  * (and lower priority)
5364                                  */
5365                                 status = ice_prof_tcam_ena_dis(hw, blk, false,
5366                                                                vsig,
5367                                                                &t->tcam[i],
5368                                                                chg);
5369                                 if (status)
5370                                         goto err_ice_adj_prof_priorities;
5371                         } else if (!used && !t->tcam[i].in_use) {
5372                                 /* need to enable this PTG, as it in not in use
5373                                  * and not enabled (highest priority)
5374                                  */
5375                                 status = ice_prof_tcam_ena_dis(hw, blk, true,
5376                                                                vsig,
5377                                                                &t->tcam[i],
5378                                                                chg);
5379                                 if (status)
5380                                         goto err_ice_adj_prof_priorities;
5381                         }
5382
5383                         /* keep track of used ptgs */
5384                         ice_set_bit(t->tcam[i].ptg, ptgs_used);
5385                         if (attr_used_cnt < ICE_MAX_PTG_ATTRS)
5386                                 attr_used[attr_used_cnt++] = &t->tcam[i];
5387                         else
5388                                 ice_debug(hw, ICE_DBG_INIT,
5389                                           "Warn: ICE_MAX_PTG_ATTRS exceeded\n");
5390                 }
5391         }
5392
5393 err_ice_adj_prof_priorities:
5394         ice_free(hw, attr_used);
5395         return status;
5396 }
5397
5398 /**
5399  * ice_add_prof_id_vsig - add profile to VSIG
5400  * @hw: pointer to the HW struct
5401  * @blk: hardware block
5402  * @vsig: the VSIG to which this profile is to be added
5403  * @hdl: the profile handle indicating the profile to add
5404  * @rev: true to add entries to the end of the list
5405  * @chg: the change list
5406  */
5407 static enum ice_status
5408 ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
5409                      bool rev, struct LIST_HEAD_TYPE *chg)
5410 {
5411         /* Masks that ignore flags */
5412         u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5413         u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
5414         u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
5415         enum ice_status status = ICE_SUCCESS;
5416         struct ice_prof_map *map;
5417         struct ice_vsig_prof *t;
5418         struct ice_chs_chg *p;
5419         u16 vsig_idx, i;
5420
5421         /* Error, if this VSIG already has this profile */
5422         if (ice_has_prof_vsig(hw, blk, vsig, hdl))
5423                 return ICE_ERR_ALREADY_EXISTS;
5424
5425         /* new VSIG profile structure */
5426         t = (struct ice_vsig_prof *)ice_malloc(hw, sizeof(*t));
5427         if (!t)
5428                 return ICE_ERR_NO_MEMORY;
5429
5430         ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
5431         /* Get the details on the profile specified by the handle ID */
5432         map = ice_search_prof_id(hw, blk, hdl);
5433         if (!map) {
5434                 status = ICE_ERR_DOES_NOT_EXIST;
5435                 goto err_ice_add_prof_id_vsig;
5436         }
5437
5438         t->profile_cookie = map->profile_cookie;
5439         t->prof_id = map->prof_id;
5440         t->tcam_count = map->ptg_cnt;
5441
5442         /* create TCAM entries */
5443         for (i = 0; i < map->ptg_cnt; i++) {
5444                 u16 tcam_idx;
5445
5446                 /* add TCAM to change list */
5447                 p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5448                 if (!p) {
5449                         status = ICE_ERR_NO_MEMORY;
5450                         goto err_ice_add_prof_id_vsig;
5451                 }
5452
5453                 /* allocate the TCAM entry index */
5454                 /* for entries with empty attribute masks, allocate entry from
5455                  * the bottom of the tcam table; otherwise, allocate from the
5456                  * top of the table in order to give it higher priority
5457                  */
5458                 status = ice_alloc_tcam_ent(hw, blk, map->attr[i].mask == 0,
5459                                             &tcam_idx);
5460                 if (status) {
5461                         ice_free(hw, p);
5462                         goto err_ice_add_prof_id_vsig;
5463                 }
5464
5465                 t->tcam[i].ptg = map->ptg[i];
5466                 t->tcam[i].prof_id = map->prof_id;
5467                 t->tcam[i].tcam_idx = tcam_idx;
5468                 t->tcam[i].attr = map->attr[i];
5469                 t->tcam[i].in_use = true;
5470
5471                 p->type = ICE_TCAM_ADD;
5472                 p->add_tcam_idx = true;
5473                 p->prof_id = t->tcam[i].prof_id;
5474                 p->ptg = t->tcam[i].ptg;
5475                 p->vsig = vsig;
5476                 p->tcam_idx = t->tcam[i].tcam_idx;
5477
5478                 /* set don't care masks for TCAM flags */
5479                 ice_set_tcam_flags(t->tcam[i].attr.mask, dc_msk);
5480
5481                 /* write the TCAM entry */
5482                 status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx,
5483                                               t->tcam[i].prof_id,
5484                                               t->tcam[i].ptg, vsig, 0,
5485                                               t->tcam[i].attr.flags, vl_msk,
5486                                               dc_msk, nm_msk);
5487                 if (status) {
5488                         ice_free(hw, p);
5489                         goto err_ice_add_prof_id_vsig;
5490                 }
5491
5492                 /* log change */
5493                 LIST_ADD(&p->list_entry, chg);
5494         }
5495
5496         /* add profile to VSIG */
5497         vsig_idx = vsig & ICE_VSIG_IDX_M;
5498         if (rev)
5499                 LIST_ADD_TAIL(&t->list,
5500                               &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
5501         else
5502                 LIST_ADD(&t->list,
5503                          &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
5504
5505         ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5506         return status;
5507
5508 err_ice_add_prof_id_vsig:
5509         ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5510         /* let caller clean up the change list */
5511         ice_free(hw, t);
5512         return status;
5513 }
5514
5515 /**
5516  * ice_create_prof_id_vsig - add a new VSIG with a single profile
5517  * @hw: pointer to the HW struct
5518  * @blk: hardware block
5519  * @vsi: the initial VSI that will be in VSIG
5520  * @hdl: the profile handle of the profile that will be added to the VSIG
5521  * @chg: the change list
5522  */
5523 static enum ice_status
5524 ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl,
5525                         struct LIST_HEAD_TYPE *chg)
5526 {
5527         enum ice_status status;
5528         struct ice_chs_chg *p;
5529         u16 new_vsig;
5530
5531         p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5532         if (!p)
5533                 return ICE_ERR_NO_MEMORY;
5534
5535         new_vsig = ice_vsig_alloc(hw, blk);
5536         if (!new_vsig) {
5537                 status = ICE_ERR_HW_TABLE;
5538                 goto err_ice_create_prof_id_vsig;
5539         }
5540
5541         status = ice_move_vsi(hw, blk, vsi, new_vsig, chg);
5542         if (status)
5543                 goto err_ice_create_prof_id_vsig;
5544
5545         status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg);
5546         if (status)
5547                 goto err_ice_create_prof_id_vsig;
5548
5549         p->type = ICE_VSIG_ADD;
5550         p->vsi = vsi;
5551         p->orig_vsig = ICE_DEFAULT_VSIG;
5552         p->vsig = new_vsig;
5553
5554         LIST_ADD(&p->list_entry, chg);
5555
5556         return ICE_SUCCESS;
5557
5558 err_ice_create_prof_id_vsig:
5559         /* let caller clean up the change list */
5560         ice_free(hw, p);
5561         return status;
5562 }
5563
5564 /**
5565  * ice_create_vsig_from_lst - create a new VSIG with a list of profiles
5566  * @hw: pointer to the HW struct
5567  * @blk: hardware block
5568  * @vsi: the initial VSI that will be in VSIG
5569  * @lst: the list of profile that will be added to the VSIG
5570  * @new_vsig: return of new VSIG
5571  * @chg: the change list
5572  */
5573 static enum ice_status
5574 ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi,
5575                          struct LIST_HEAD_TYPE *lst, u16 *new_vsig,
5576                          struct LIST_HEAD_TYPE *chg)
5577 {
5578         struct ice_vsig_prof *t;
5579         enum ice_status status;
5580         u16 vsig;
5581
5582         vsig = ice_vsig_alloc(hw, blk);
5583         if (!vsig)
5584                 return ICE_ERR_HW_TABLE;
5585
5586         status = ice_move_vsi(hw, blk, vsi, vsig, chg);
5587         if (status)
5588                 return status;
5589
5590         LIST_FOR_EACH_ENTRY(t, lst, ice_vsig_prof, list) {
5591                 /* Reverse the order here since we are copying the list */
5592                 status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie,
5593                                               true, chg);
5594                 if (status)
5595                         return status;
5596         }
5597
5598         *new_vsig = vsig;
5599
5600         return ICE_SUCCESS;
5601 }
5602
5603 /**
5604  * ice_find_prof_vsig - find a VSIG with a specific profile handle
5605  * @hw: pointer to the HW struct
5606  * @blk: hardware block
5607  * @hdl: the profile handle of the profile to search for
5608  * @vsig: returns the VSIG with the matching profile
5609  */
5610 static bool
5611 ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig)
5612 {
5613         struct ice_vsig_prof *t;
5614         struct LIST_HEAD_TYPE lst;
5615         enum ice_status status;
5616
5617         INIT_LIST_HEAD(&lst);
5618
5619         t = (struct ice_vsig_prof *)ice_malloc(hw, sizeof(*t));
5620         if (!t)
5621                 return false;
5622
5623         t->profile_cookie = hdl;
5624         LIST_ADD(&t->list, &lst);
5625
5626         status = ice_find_dup_props_vsig(hw, blk, &lst, vsig);
5627
5628         LIST_DEL(&t->list);
5629         ice_free(hw, t);
5630
5631         return status == ICE_SUCCESS;
5632 }
5633
5634 /**
5635  * ice_add_vsi_flow - add VSI flow
5636  * @hw: pointer to the HW struct
5637  * @blk: hardware block
5638  * @vsi: input VSI
5639  * @vsig: target VSIG to include the input VSI
5640  *
5641  * Calling this function will add the VSI to a given VSIG and
5642  * update the HW tables accordingly. This call can be used to
5643  * add multiple VSIs to a VSIG if we know beforehand that those
5644  * VSIs have the same characteristics of the VSIG. This will
5645  * save time in generating a new VSIG and TCAMs till a match is
5646  * found and subsequent rollback when a matching VSIG is found.
5647  */
5648 enum ice_status
5649 ice_add_vsi_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
5650 {
5651         struct ice_chs_chg *tmp, *del;
5652         struct LIST_HEAD_TYPE chg;
5653         enum ice_status status;
5654
5655         /* if target VSIG is default the move is invalid */
5656         if ((vsig & ICE_VSIG_IDX_M) == ICE_DEFAULT_VSIG)
5657                 return ICE_ERR_PARAM;
5658
5659         INIT_LIST_HEAD(&chg);
5660
5661         /* move VSI to the VSIG that matches */
5662         status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
5663         /* update hardware if success */
5664         if (!status)
5665                 status = ice_upd_prof_hw(hw, blk, &chg);
5666
5667         LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
5668                 LIST_DEL(&del->list_entry);
5669                 ice_free(hw, del);
5670         }
5671
5672         return status;
5673 }
5674
5675 /**
5676  * ice_add_prof_id_flow - add profile flow
5677  * @hw: pointer to the HW struct
5678  * @blk: hardware block
5679  * @vsi: the VSI to enable with the profile specified by ID
5680  * @hdl: profile handle
5681  *
5682  * Calling this function will update the hardware tables to enable the
5683  * profile indicated by the ID parameter for the VSIs specified in the VSI
5684  * array. Once successfully called, the flow will be enabled.
5685  */
5686 enum ice_status
5687 ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
5688 {
5689         struct ice_vsig_prof *tmp1, *del1;
5690         struct LIST_HEAD_TYPE union_lst;
5691         struct ice_chs_chg *tmp, *del;
5692         struct LIST_HEAD_TYPE chg;
5693         enum ice_status status;
5694         u16 vsig;
5695
5696         INIT_LIST_HEAD(&union_lst);
5697         INIT_LIST_HEAD(&chg);
5698
5699         /* Get profile */
5700         status = ice_get_prof(hw, blk, hdl, &chg);
5701         if (status)
5702                 return status;
5703
5704         /* determine if VSI is already part of a VSIG */
5705         status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
5706         if (!status && vsig) {
5707                 bool only_vsi;
5708                 u16 or_vsig;
5709                 u16 ref;
5710
5711                 /* found in VSIG */
5712                 or_vsig = vsig;
5713
5714                 /* make sure that there is no overlap/conflict between the new
5715                  * characteristics and the existing ones; we don't support that
5716                  * scenario
5717                  */
5718                 if (ice_has_prof_vsig(hw, blk, vsig, hdl)) {
5719                         status = ICE_ERR_ALREADY_EXISTS;
5720                         goto err_ice_add_prof_id_flow;
5721                 }
5722
5723                 /* last VSI in the VSIG? */
5724                 status = ice_vsig_get_ref(hw, blk, vsig, &ref);
5725                 if (status)
5726                         goto err_ice_add_prof_id_flow;
5727                 only_vsi = (ref == 1);
5728
5729                 /* create a union of the current profiles and the one being
5730                  * added
5731                  */
5732                 status = ice_get_profs_vsig(hw, blk, vsig, &union_lst);
5733                 if (status)
5734                         goto err_ice_add_prof_id_flow;
5735
5736                 status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl);
5737                 if (status)
5738                         goto err_ice_add_prof_id_flow;
5739
5740                 /* search for an existing VSIG with an exact charc match */
5741                 status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig);
5742                 if (!status) {
5743                         /* move VSI to the VSIG that matches */
5744                         status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
5745                         if (status)
5746                                 goto err_ice_add_prof_id_flow;
5747
5748                         /* VSI has been moved out of or_vsig. If the or_vsig had
5749                          * only that VSI it is now empty and can be removed.
5750                          */
5751                         if (only_vsi) {
5752                                 status = ice_rem_vsig(hw, blk, or_vsig, &chg);
5753                                 if (status)
5754                                         goto err_ice_add_prof_id_flow;
5755                         }
5756                 } else if (only_vsi) {
5757                         /* If the original VSIG only contains one VSI, then it
5758                          * will be the requesting VSI. In this case the VSI is
5759                          * not sharing entries and we can simply add the new
5760                          * profile to the VSIG.
5761                          */
5762                         status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false,
5763                                                       &chg);
5764                         if (status)
5765                                 goto err_ice_add_prof_id_flow;
5766
5767                         /* Adjust priorities */
5768                         status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
5769                         if (status)
5770                                 goto err_ice_add_prof_id_flow;
5771                 } else {
5772                         /* No match, so we need a new VSIG */
5773                         status = ice_create_vsig_from_lst(hw, blk, vsi,
5774                                                           &union_lst, &vsig,
5775                                                           &chg);
5776                         if (status)
5777                                 goto err_ice_add_prof_id_flow;
5778
5779                         /* Adjust priorities */
5780                         status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
5781                         if (status)
5782                                 goto err_ice_add_prof_id_flow;
5783                 }
5784         } else {
5785                 /* need to find or add a VSIG */
5786                 /* search for an existing VSIG with an exact charc match */
5787                 if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) {
5788                         /* found an exact match */
5789                         /* add or move VSI to the VSIG that matches */
5790                         status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
5791                         if (status)
5792                                 goto err_ice_add_prof_id_flow;
5793                 } else {
5794                         /* we did not find an exact match */
5795                         /* we need to add a VSIG */
5796                         status = ice_create_prof_id_vsig(hw, blk, vsi, hdl,
5797                                                          &chg);
5798                         if (status)
5799                                 goto err_ice_add_prof_id_flow;
5800                 }
5801         }
5802
5803         /* update hardware */
5804         if (!status)
5805                 status = ice_upd_prof_hw(hw, blk, &chg);
5806
5807 err_ice_add_prof_id_flow:
5808         LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
5809                 LIST_DEL(&del->list_entry);
5810                 ice_free(hw, del);
5811         }
5812
5813         LIST_FOR_EACH_ENTRY_SAFE(del1, tmp1, &union_lst, ice_vsig_prof, list) {
5814                 LIST_DEL(&del1->list);
5815                 ice_free(hw, del1);
5816         }
5817
5818         return status;
5819 }
5820
5821 /**
5822  * ice_rem_prof_from_list - remove a profile from list
5823  * @hw: pointer to the HW struct
5824  * @lst: list to remove the profile from
5825  * @hdl: the profile handle indicating the profile to remove
5826  */
5827 static enum ice_status
5828 ice_rem_prof_from_list(struct ice_hw *hw, struct LIST_HEAD_TYPE *lst, u64 hdl)
5829 {
5830         struct ice_vsig_prof *ent, *tmp;
5831
5832         LIST_FOR_EACH_ENTRY_SAFE(ent, tmp, lst, ice_vsig_prof, list)
5833                 if (ent->profile_cookie == hdl) {
5834                         LIST_DEL(&ent->list);
5835                         ice_free(hw, ent);
5836                         return ICE_SUCCESS;
5837                 }
5838
5839         return ICE_ERR_DOES_NOT_EXIST;
5840 }
5841
5842 /**
5843  * ice_rem_prof_id_flow - remove flow
5844  * @hw: pointer to the HW struct
5845  * @blk: hardware block
5846  * @vsi: the VSI from which to remove the profile specified by ID
5847  * @hdl: profile tracking handle
5848  *
5849  * Calling this function will update the hardware tables to remove the
5850  * profile indicated by the ID parameter for the VSIs specified in the VSI
5851  * array. Once successfully called, the flow will be disabled.
5852  */
5853 enum ice_status
5854 ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
5855 {
5856         struct ice_vsig_prof *tmp1, *del1;
5857         struct LIST_HEAD_TYPE chg, copy;
5858         struct ice_chs_chg *tmp, *del;
5859         enum ice_status status;
5860         u16 vsig;
5861
5862         INIT_LIST_HEAD(&copy);
5863         INIT_LIST_HEAD(&chg);
5864
5865         /* determine if VSI is already part of a VSIG */
5866         status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
5867         if (!status && vsig) {
5868                 bool last_profile;
5869                 bool only_vsi;
5870                 u16 ref;
5871
5872                 /* found in VSIG */
5873                 last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1;
5874                 status = ice_vsig_get_ref(hw, blk, vsig, &ref);
5875                 if (status)
5876                         goto err_ice_rem_prof_id_flow;
5877                 only_vsi = (ref == 1);
5878
5879                 if (only_vsi) {
5880                         /* If the original VSIG only contains one reference,
5881                          * which will be the requesting VSI, then the VSI is not
5882                          * sharing entries and we can simply remove the specific
5883                          * characteristics from the VSIG.
5884                          */
5885
5886                         if (last_profile) {
5887                                 /* If there are no profiles left for this VSIG,
5888                                  * then simply remove the the VSIG.
5889                                  */
5890                                 status = ice_rem_vsig(hw, blk, vsig, &chg);
5891                                 if (status)
5892                                         goto err_ice_rem_prof_id_flow;
5893                         } else {
5894                                 status = ice_rem_prof_id_vsig(hw, blk, vsig,
5895                                                               hdl, &chg);
5896                                 if (status)
5897                                         goto err_ice_rem_prof_id_flow;
5898
5899                                 /* Adjust priorities */
5900                                 status = ice_adj_prof_priorities(hw, blk, vsig,
5901                                                                  &chg);
5902                                 if (status)
5903                                         goto err_ice_rem_prof_id_flow;
5904                         }
5905
5906                 } else {
5907                         /* Make a copy of the VSIG's list of Profiles */
5908                         status = ice_get_profs_vsig(hw, blk, vsig, &copy);
5909                         if (status)
5910                                 goto err_ice_rem_prof_id_flow;
5911
5912                         /* Remove specified profile entry from the list */
5913                         status = ice_rem_prof_from_list(hw, &copy, hdl);
5914                         if (status)
5915                                 goto err_ice_rem_prof_id_flow;
5916
5917                         if (LIST_EMPTY(&copy)) {
5918                                 status = ice_move_vsi(hw, blk, vsi,
5919                                                       ICE_DEFAULT_VSIG, &chg);
5920                                 if (status)
5921                                         goto err_ice_rem_prof_id_flow;
5922
5923                         } else if (!ice_find_dup_props_vsig(hw, blk, &copy,
5924                                                             &vsig)) {
5925                                 /* found an exact match */
5926                                 /* add or move VSI to the VSIG that matches */
5927                                 /* Search for a VSIG with a matching profile
5928                                  * list
5929                                  */
5930
5931                                 /* Found match, move VSI to the matching VSIG */
5932                                 status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
5933                                 if (status)
5934                                         goto err_ice_rem_prof_id_flow;
5935                         } else {
5936                                 /* since no existing VSIG supports this
5937                                  * characteristic pattern, we need to create a
5938                                  * new VSIG and TCAM entries
5939                                  */
5940                                 status = ice_create_vsig_from_lst(hw, blk, vsi,
5941                                                                   &copy, &vsig,
5942                                                                   &chg);
5943                                 if (status)
5944                                         goto err_ice_rem_prof_id_flow;
5945
5946                                 /* Adjust priorities */
5947                                 status = ice_adj_prof_priorities(hw, blk, vsig,
5948                                                                  &chg);
5949                                 if (status)
5950                                         goto err_ice_rem_prof_id_flow;
5951                         }
5952                 }
5953         } else {
5954                 status = ICE_ERR_DOES_NOT_EXIST;
5955         }
5956
5957         /* update hardware tables */
5958         if (!status)
5959                 status = ice_upd_prof_hw(hw, blk, &chg);
5960
5961 err_ice_rem_prof_id_flow:
5962         LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
5963                 LIST_DEL(&del->list_entry);
5964                 ice_free(hw, del);
5965         }
5966
5967         LIST_FOR_EACH_ENTRY_SAFE(del1, tmp1, &copy, ice_vsig_prof, list) {
5968                 LIST_DEL(&del1->list);
5969                 ice_free(hw, del1);
5970         }
5971
5972         return status;
5973 }