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