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