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