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