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