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