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