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