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