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