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