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