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