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