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