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