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