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