net/ice/base: fix type-mismatch
[dpdk.git] / drivers / net / ice / base / ice_sched.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2019
3  */
4
5 #include "ice_sched.h"
6
7
8 /**
9  * ice_sched_add_root_node - Insert the Tx scheduler root node in SW DB
10  * @pi: port information structure
11  * @info: Scheduler element information from firmware
12  *
13  * This function inserts the root node of the scheduling tree topology
14  * to the SW DB.
15  */
16 static enum ice_status
17 ice_sched_add_root_node(struct ice_port_info *pi,
18                         struct ice_aqc_txsched_elem_data *info)
19 {
20         struct ice_sched_node *root;
21         struct ice_hw *hw;
22
23         if (!pi)
24                 return ICE_ERR_PARAM;
25
26         hw = pi->hw;
27
28         root = (struct ice_sched_node *)ice_malloc(hw, sizeof(*root));
29         if (!root)
30                 return ICE_ERR_NO_MEMORY;
31
32         /* coverity[suspicious_sizeof] */
33         root->children = (struct ice_sched_node **)
34                 ice_calloc(hw, hw->max_children[0], sizeof(*root));
35         if (!root->children) {
36                 ice_free(hw, root);
37                 return ICE_ERR_NO_MEMORY;
38         }
39
40         ice_memcpy(&root->info, info, sizeof(*info), ICE_DMA_TO_NONDMA);
41         pi->root = root;
42         return ICE_SUCCESS;
43 }
44
45 /**
46  * ice_sched_find_node_by_teid - Find the Tx scheduler node in SW DB
47  * @start_node: pointer to the starting ice_sched_node struct in a sub-tree
48  * @teid: node TEID to search
49  *
50  * This function searches for a node matching the TEID in the scheduling tree
51  * from the SW DB. The search is recursive and is restricted by the number of
52  * layers it has searched through; stopping at the max supported layer.
53  *
54  * This function needs to be called when holding the port_info->sched_lock
55  */
56 struct ice_sched_node *
57 ice_sched_find_node_by_teid(struct ice_sched_node *start_node, u32 teid)
58 {
59         u16 i;
60
61         /* The TEID is same as that of the start_node */
62         if (ICE_TXSCHED_GET_NODE_TEID(start_node) == teid)
63                 return start_node;
64
65         /* The node has no children or is at the max layer */
66         if (!start_node->num_children ||
67             start_node->tx_sched_layer >= ICE_AQC_TOPO_MAX_LEVEL_NUM ||
68             start_node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF)
69                 return NULL;
70
71         /* Check if TEID matches to any of the children nodes */
72         for (i = 0; i < start_node->num_children; i++)
73                 if (ICE_TXSCHED_GET_NODE_TEID(start_node->children[i]) == teid)
74                         return start_node->children[i];
75
76         /* Search within each child's sub-tree */
77         for (i = 0; i < start_node->num_children; i++) {
78                 struct ice_sched_node *tmp;
79
80                 tmp = ice_sched_find_node_by_teid(start_node->children[i],
81                                                   teid);
82                 if (tmp)
83                         return tmp;
84         }
85
86         return NULL;
87 }
88
89 /**
90  * ice_aqc_send_sched_elem_cmd - send scheduling elements cmd
91  * @hw: pointer to the HW struct
92  * @cmd_opc: cmd opcode
93  * @elems_req: number of elements to request
94  * @buf: pointer to buffer
95  * @buf_size: buffer size in bytes
96  * @elems_resp: returns total number of elements response
97  * @cd: pointer to command details structure or NULL
98  *
99  * This function sends a scheduling elements cmd (cmd_opc)
100  */
101 static enum ice_status
102 ice_aqc_send_sched_elem_cmd(struct ice_hw *hw, enum ice_adminq_opc cmd_opc,
103                             u16 elems_req, void *buf, u16 buf_size,
104                             u16 *elems_resp, struct ice_sq_cd *cd)
105 {
106         struct ice_aqc_sched_elem_cmd *cmd;
107         struct ice_aq_desc desc;
108         enum ice_status status;
109
110         cmd = &desc.params.sched_elem_cmd;
111         ice_fill_dflt_direct_cmd_desc(&desc, cmd_opc);
112         cmd->num_elem_req = CPU_TO_LE16(elems_req);
113         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
114         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
115         if (!status && elems_resp)
116                 *elems_resp = LE16_TO_CPU(cmd->num_elem_resp);
117
118         return status;
119 }
120
121 /**
122  * ice_aq_query_sched_elems - query scheduler elements
123  * @hw: pointer to the HW struct
124  * @elems_req: number of elements to query
125  * @buf: pointer to buffer
126  * @buf_size: buffer size in bytes
127  * @elems_ret: returns total number of elements returned
128  * @cd: pointer to command details structure or NULL
129  *
130  * Query scheduling elements (0x0404)
131  */
132 enum ice_status
133 ice_aq_query_sched_elems(struct ice_hw *hw, u16 elems_req,
134                          struct ice_aqc_get_elem *buf, u16 buf_size,
135                          u16 *elems_ret, struct ice_sq_cd *cd)
136 {
137         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_get_sched_elems,
138                                            elems_req, (void *)buf, buf_size,
139                                            elems_ret, cd);
140 }
141
142 /**
143  * ice_sched_add_node - Insert the Tx scheduler node in SW DB
144  * @pi: port information structure
145  * @layer: Scheduler layer of the node
146  * @info: Scheduler element information from firmware
147  *
148  * This function inserts a scheduler node to the SW DB.
149  */
150 enum ice_status
151 ice_sched_add_node(struct ice_port_info *pi, u8 layer,
152                    struct ice_aqc_txsched_elem_data *info)
153 {
154         struct ice_sched_node *parent;
155         struct ice_aqc_get_elem elem;
156         struct ice_sched_node *node;
157         enum ice_status status;
158         struct ice_hw *hw;
159
160         if (!pi)
161                 return ICE_ERR_PARAM;
162
163         hw = pi->hw;
164
165         /* A valid parent node should be there */
166         parent = ice_sched_find_node_by_teid(pi->root,
167                                              LE32_TO_CPU(info->parent_teid));
168         if (!parent) {
169                 ice_debug(hw, ICE_DBG_SCHED,
170                           "Parent Node not found for parent_teid=0x%x\n",
171                           LE32_TO_CPU(info->parent_teid));
172                 return ICE_ERR_PARAM;
173         }
174
175         /* query the current node information from FW  before additing it
176          * to the SW DB
177          */
178         status = ice_sched_query_elem(hw, LE32_TO_CPU(info->node_teid), &elem);
179         if (status)
180                 return status;
181         node = (struct ice_sched_node *)ice_malloc(hw, sizeof(*node));
182         if (!node)
183                 return ICE_ERR_NO_MEMORY;
184         if (hw->max_children[layer]) {
185                 /* coverity[suspicious_sizeof] */
186                 node->children = (struct ice_sched_node **)
187                         ice_calloc(hw, hw->max_children[layer], sizeof(*node));
188                 if (!node->children) {
189                         ice_free(hw, node);
190                         return ICE_ERR_NO_MEMORY;
191                 }
192         }
193
194         node->in_use = true;
195         node->parent = parent;
196         node->tx_sched_layer = layer;
197         parent->children[parent->num_children++] = node;
198         node->info = elem.generic[0];
199         return ICE_SUCCESS;
200 }
201
202 /**
203  * ice_aq_delete_sched_elems - delete scheduler elements
204  * @hw: pointer to the HW struct
205  * @grps_req: number of groups to delete
206  * @buf: pointer to buffer
207  * @buf_size: buffer size in bytes
208  * @grps_del: returns total number of elements deleted
209  * @cd: pointer to command details structure or NULL
210  *
211  * Delete scheduling elements (0x040F)
212  */
213 static enum ice_status
214 ice_aq_delete_sched_elems(struct ice_hw *hw, u16 grps_req,
215                           struct ice_aqc_delete_elem *buf, u16 buf_size,
216                           u16 *grps_del, struct ice_sq_cd *cd)
217 {
218         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_delete_sched_elems,
219                                            grps_req, (void *)buf, buf_size,
220                                            grps_del, cd);
221 }
222
223 /**
224  * ice_sched_remove_elems - remove nodes from HW
225  * @hw: pointer to the HW struct
226  * @parent: pointer to the parent node
227  * @num_nodes: number of nodes
228  * @node_teids: array of node teids to be deleted
229  *
230  * This function remove nodes from HW
231  */
232 static enum ice_status
233 ice_sched_remove_elems(struct ice_hw *hw, struct ice_sched_node *parent,
234                        u16 num_nodes, u32 *node_teids)
235 {
236         struct ice_aqc_delete_elem *buf;
237         u16 i, num_groups_removed = 0;
238         enum ice_status status;
239         u16 buf_size;
240
241         buf_size = sizeof(*buf) + sizeof(u32) * (num_nodes - 1);
242         buf = (struct ice_aqc_delete_elem *)ice_malloc(hw, buf_size);
243         if (!buf)
244                 return ICE_ERR_NO_MEMORY;
245
246         buf->hdr.parent_teid = parent->info.node_teid;
247         buf->hdr.num_elems = CPU_TO_LE16(num_nodes);
248         for (i = 0; i < num_nodes; i++)
249                 buf->teid[i] = CPU_TO_LE32(node_teids[i]);
250
251         status = ice_aq_delete_sched_elems(hw, 1, buf, buf_size,
252                                            &num_groups_removed, NULL);
253         if (status != ICE_SUCCESS || num_groups_removed != 1)
254                 ice_debug(hw, ICE_DBG_SCHED, "remove node failed FW error %d\n",
255                           hw->adminq.sq_last_status);
256
257         ice_free(hw, buf);
258         return status;
259 }
260
261 /**
262  * ice_sched_get_first_node - get the first node of the given layer
263  * @pi: port information structure
264  * @parent: pointer the base node of the subtree
265  * @layer: layer number
266  *
267  * This function retrieves the first node of the given layer from the subtree
268  */
269 static struct ice_sched_node *
270 ice_sched_get_first_node(struct ice_port_info *pi,
271                          struct ice_sched_node *parent, u8 layer)
272 {
273         return pi->sib_head[parent->tc_num][layer];
274 }
275
276 /**
277  * ice_sched_get_tc_node - get pointer to TC node
278  * @pi: port information structure
279  * @tc: TC number
280  *
281  * This function returns the TC node pointer
282  */
283 struct ice_sched_node *ice_sched_get_tc_node(struct ice_port_info *pi, u8 tc)
284 {
285         u8 i;
286
287         if (!pi || !pi->root)
288                 return NULL;
289         for (i = 0; i < pi->root->num_children; i++)
290                 if (pi->root->children[i]->tc_num == tc)
291                         return pi->root->children[i];
292         return NULL;
293 }
294
295 /**
296  * ice_free_sched_node - Free a Tx scheduler node from SW DB
297  * @pi: port information structure
298  * @node: pointer to the ice_sched_node struct
299  *
300  * This function frees up a node from SW DB as well as from HW
301  *
302  * This function needs to be called with the port_info->sched_lock held
303  */
304 void ice_free_sched_node(struct ice_port_info *pi, struct ice_sched_node *node)
305 {
306         struct ice_sched_node *parent;
307         struct ice_hw *hw = pi->hw;
308         u8 i, j;
309
310         /* Free the children before freeing up the parent node
311          * The parent array is updated below and that shifts the nodes
312          * in the array. So always pick the first child if num children > 0
313          */
314         while (node->num_children)
315                 ice_free_sched_node(pi, node->children[0]);
316
317         /* Leaf, TC and root nodes can't be deleted by SW */
318         if (node->tx_sched_layer >= hw->sw_entry_point_layer &&
319             node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
320             node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT &&
321             node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF) {
322                 u32 teid = LE32_TO_CPU(node->info.node_teid);
323
324                 ice_sched_remove_elems(hw, node->parent, 1, &teid);
325         }
326         parent = node->parent;
327         /* root has no parent */
328         if (parent) {
329                 struct ice_sched_node *p;
330
331                 /* update the parent */
332                 for (i = 0; i < parent->num_children; i++)
333                         if (parent->children[i] == node) {
334                                 for (j = i + 1; j < parent->num_children; j++)
335                                         parent->children[j - 1] =
336                                                 parent->children[j];
337                                 parent->num_children--;
338                                 break;
339                         }
340
341                 p = ice_sched_get_first_node(pi, node, node->tx_sched_layer);
342                 while (p) {
343                         if (p->sibling == node) {
344                                 p->sibling = node->sibling;
345                                 break;
346                         }
347                         p = p->sibling;
348                 }
349
350                 /* update the sibling head if head is getting removed */
351                 if (pi->sib_head[node->tc_num][node->tx_sched_layer] == node)
352                         pi->sib_head[node->tc_num][node->tx_sched_layer] =
353                                 node->sibling;
354         }
355
356         /* leaf nodes have no children */
357         if (node->children)
358                 ice_free(hw, node->children);
359         ice_free(hw, node);
360 }
361
362 /**
363  * ice_aq_get_dflt_topo - gets default scheduler topology
364  * @hw: pointer to the HW struct
365  * @lport: logical port number
366  * @buf: pointer to buffer
367  * @buf_size: buffer size in bytes
368  * @num_branches: returns total number of queue to port branches
369  * @cd: pointer to command details structure or NULL
370  *
371  * Get default scheduler topology (0x400)
372  */
373 static enum ice_status
374 ice_aq_get_dflt_topo(struct ice_hw *hw, u8 lport,
375                      struct ice_aqc_get_topo_elem *buf, u16 buf_size,
376                      u8 *num_branches, struct ice_sq_cd *cd)
377 {
378         struct ice_aqc_get_topo *cmd;
379         struct ice_aq_desc desc;
380         enum ice_status status;
381
382         cmd = &desc.params.get_topo;
383         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_dflt_topo);
384         cmd->port_num = lport;
385         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
386         if (!status && num_branches)
387                 *num_branches = cmd->num_branches;
388
389         return status;
390 }
391
392 /**
393  * ice_aq_add_sched_elems - adds scheduling element
394  * @hw: pointer to the HW struct
395  * @grps_req: the number of groups that are requested to be added
396  * @buf: pointer to buffer
397  * @buf_size: buffer size in bytes
398  * @grps_added: returns total number of groups added
399  * @cd: pointer to command details structure or NULL
400  *
401  * Add scheduling elements (0x0401)
402  */
403 static enum ice_status
404 ice_aq_add_sched_elems(struct ice_hw *hw, u16 grps_req,
405                        struct ice_aqc_add_elem *buf, u16 buf_size,
406                        u16 *grps_added, struct ice_sq_cd *cd)
407 {
408         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_add_sched_elems,
409                                            grps_req, (void *)buf, buf_size,
410                                            grps_added, cd);
411 }
412
413 /**
414  * ice_aq_cfg_sched_elems - configures scheduler elements
415  * @hw: pointer to the HW struct
416  * @elems_req: number of elements to configure
417  * @buf: pointer to buffer
418  * @buf_size: buffer size in bytes
419  * @elems_cfgd: returns total number of elements configured
420  * @cd: pointer to command details structure or NULL
421  *
422  * Configure scheduling elements (0x0403)
423  */
424 static enum ice_status
425 ice_aq_cfg_sched_elems(struct ice_hw *hw, u16 elems_req,
426                        struct ice_aqc_conf_elem *buf, u16 buf_size,
427                        u16 *elems_cfgd, struct ice_sq_cd *cd)
428 {
429         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_cfg_sched_elems,
430                                            elems_req, (void *)buf, buf_size,
431                                            elems_cfgd, cd);
432 }
433
434 /**
435  * ice_aq_move_sched_elems - move scheduler elements
436  * @hw: pointer to the HW struct
437  * @grps_req: number of groups to move
438  * @buf: pointer to buffer
439  * @buf_size: buffer size in bytes
440  * @grps_movd: returns total number of groups moved
441  * @cd: pointer to command details structure or NULL
442  *
443  * Move scheduling elements (0x0408)
444  */
445 static enum ice_status
446 ice_aq_move_sched_elems(struct ice_hw *hw, u16 grps_req,
447                         struct ice_aqc_move_elem *buf, u16 buf_size,
448                         u16 *grps_movd, struct ice_sq_cd *cd)
449 {
450         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_move_sched_elems,
451                                            grps_req, (void *)buf, buf_size,
452                                            grps_movd, cd);
453 }
454
455 /**
456  * ice_aq_suspend_sched_elems - suspend scheduler elements
457  * @hw: pointer to the HW struct
458  * @elems_req: number of elements to suspend
459  * @buf: pointer to buffer
460  * @buf_size: buffer size in bytes
461  * @elems_ret: returns total number of elements suspended
462  * @cd: pointer to command details structure or NULL
463  *
464  * Suspend scheduling elements (0x0409)
465  */
466 static enum ice_status
467 ice_aq_suspend_sched_elems(struct ice_hw *hw, u16 elems_req,
468                            struct ice_aqc_suspend_resume_elem *buf,
469                            u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
470 {
471         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_suspend_sched_elems,
472                                            elems_req, (void *)buf, buf_size,
473                                            elems_ret, cd);
474 }
475
476 /**
477  * ice_aq_resume_sched_elems - resume scheduler elements
478  * @hw: pointer to the HW struct
479  * @elems_req: number of elements to resume
480  * @buf: pointer to buffer
481  * @buf_size: buffer size in bytes
482  * @elems_ret: returns total number of elements resumed
483  * @cd: pointer to command details structure or NULL
484  *
485  * resume scheduling elements (0x040A)
486  */
487 static enum ice_status
488 ice_aq_resume_sched_elems(struct ice_hw *hw, u16 elems_req,
489                           struct ice_aqc_suspend_resume_elem *buf,
490                           u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
491 {
492         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_resume_sched_elems,
493                                            elems_req, (void *)buf, buf_size,
494                                            elems_ret, cd);
495 }
496
497 /**
498  * ice_aq_query_sched_res - query scheduler resource
499  * @hw: pointer to the HW struct
500  * @buf_size: buffer size in bytes
501  * @buf: pointer to buffer
502  * @cd: pointer to command details structure or NULL
503  *
504  * Query scheduler resource allocation (0x0412)
505  */
506 static enum ice_status
507 ice_aq_query_sched_res(struct ice_hw *hw, u16 buf_size,
508                        struct ice_aqc_query_txsched_res_resp *buf,
509                        struct ice_sq_cd *cd)
510 {
511         struct ice_aq_desc desc;
512
513         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_sched_res);
514         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
515 }
516
517 /**
518  * ice_sched_suspend_resume_elems - suspend or resume HW nodes
519  * @hw: pointer to the HW struct
520  * @num_nodes: number of nodes
521  * @node_teids: array of node teids to be suspended or resumed
522  * @suspend: true means suspend / false means resume
523  *
524  * This function suspends or resumes HW nodes
525  */
526 static enum ice_status
527 ice_sched_suspend_resume_elems(struct ice_hw *hw, u8 num_nodes, u32 *node_teids,
528                                bool suspend)
529 {
530         struct ice_aqc_suspend_resume_elem *buf;
531         u16 i, buf_size, num_elem_ret = 0;
532         enum ice_status status;
533
534         buf_size = sizeof(*buf) * num_nodes;
535         buf = (struct ice_aqc_suspend_resume_elem *)
536                 ice_malloc(hw, buf_size);
537         if (!buf)
538                 return ICE_ERR_NO_MEMORY;
539
540         for (i = 0; i < num_nodes; i++)
541                 buf->teid[i] = CPU_TO_LE32(node_teids[i]);
542
543         if (suspend)
544                 status = ice_aq_suspend_sched_elems(hw, num_nodes, buf,
545                                                     buf_size, &num_elem_ret,
546                                                     NULL);
547         else
548                 status = ice_aq_resume_sched_elems(hw, num_nodes, buf,
549                                                    buf_size, &num_elem_ret,
550                                                    NULL);
551         if (status != ICE_SUCCESS || num_elem_ret != num_nodes)
552                 ice_debug(hw, ICE_DBG_SCHED, "suspend/resume failed\n");
553
554         ice_free(hw, buf);
555         return status;
556 }
557
558 /**
559  * ice_alloc_lan_q_ctx - allocate LAN queue contexts for the given VSI and TC
560  * @hw: pointer to the HW struct
561  * @vsi_handle: VSI handle
562  * @tc: TC number
563  * @new_numqs: number of queues
564  */
565 static enum ice_status
566 ice_alloc_lan_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 new_numqs)
567 {
568         struct ice_vsi_ctx *vsi_ctx;
569         struct ice_q_ctx *q_ctx;
570
571         vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
572         if (!vsi_ctx)
573                 return ICE_ERR_PARAM;
574         /* allocate LAN queue contexts */
575         if (!vsi_ctx->lan_q_ctx[tc]) {
576                 vsi_ctx->lan_q_ctx[tc] = (struct ice_q_ctx *)
577                         ice_calloc(hw, new_numqs, sizeof(*q_ctx));
578                 if (!vsi_ctx->lan_q_ctx[tc])
579                         return ICE_ERR_NO_MEMORY;
580                 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
581                 return ICE_SUCCESS;
582         }
583         /* num queues are increased, update the queue contexts */
584         if (new_numqs > vsi_ctx->num_lan_q_entries[tc]) {
585                 u16 prev_num = vsi_ctx->num_lan_q_entries[tc];
586
587                 q_ctx = (struct ice_q_ctx *)
588                         ice_calloc(hw, new_numqs, sizeof(*q_ctx));
589                 if (!q_ctx)
590                         return ICE_ERR_NO_MEMORY;
591                 ice_memcpy(q_ctx, vsi_ctx->lan_q_ctx[tc],
592                            prev_num * sizeof(*q_ctx), ICE_DMA_TO_NONDMA);
593                 ice_free(hw, vsi_ctx->lan_q_ctx[tc]);
594                 vsi_ctx->lan_q_ctx[tc] = q_ctx;
595                 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
596         }
597         return ICE_SUCCESS;
598 }
599
600 /**
601  * ice_aq_rl_profile - performs a rate limiting task
602  * @hw: pointer to the HW struct
603  * @opcode:opcode for add, query, or remove profile(s)
604  * @num_profiles: the number of profiles
605  * @buf: pointer to buffer
606  * @buf_size: buffer size in bytes
607  * @num_processed: number of processed add or remove profile(s) to return
608  * @cd: pointer to command details structure
609  *
610  * Rl profile function to add, query, or remove profile(s)
611  */
612 static enum ice_status
613 ice_aq_rl_profile(struct ice_hw *hw, enum ice_adminq_opc opcode,
614                   u16 num_profiles, struct ice_aqc_rl_profile_generic_elem *buf,
615                   u16 buf_size, u16 *num_processed, struct ice_sq_cd *cd)
616 {
617         struct ice_aqc_rl_profile *cmd;
618         struct ice_aq_desc desc;
619         enum ice_status status;
620
621         cmd = &desc.params.rl_profile;
622
623         ice_fill_dflt_direct_cmd_desc(&desc, opcode);
624         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
625         cmd->num_profiles = CPU_TO_LE16(num_profiles);
626         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
627         if (!status && num_processed)
628                 *num_processed = LE16_TO_CPU(cmd->num_processed);
629         return status;
630 }
631
632 /**
633  * ice_aq_add_rl_profile - adds rate limiting profile(s)
634  * @hw: pointer to the HW struct
635  * @num_profiles: the number of profile(s) to be add
636  * @buf: pointer to buffer
637  * @buf_size: buffer size in bytes
638  * @num_profiles_added: total number of profiles added to return
639  * @cd: pointer to command details structure
640  *
641  * Add RL profile (0x0410)
642  */
643 static enum ice_status
644 ice_aq_add_rl_profile(struct ice_hw *hw, u16 num_profiles,
645                       struct ice_aqc_rl_profile_generic_elem *buf,
646                       u16 buf_size, u16 *num_profiles_added,
647                       struct ice_sq_cd *cd)
648 {
649         return ice_aq_rl_profile(hw, ice_aqc_opc_add_rl_profiles,
650                                  num_profiles, buf,
651                                  buf_size, num_profiles_added, cd);
652 }
653
654 /**
655  * ice_aq_query_rl_profile - query rate limiting profile(s)
656  * @hw: pointer to the HW struct
657  * @num_profiles: the number of profile(s) to query
658  * @buf: pointer to buffer
659  * @buf_size: buffer size in bytes
660  * @cd: pointer to command details structure
661  *
662  * Query RL profile (0x0411)
663  */
664 enum ice_status
665 ice_aq_query_rl_profile(struct ice_hw *hw, u16 num_profiles,
666                         struct ice_aqc_rl_profile_generic_elem *buf,
667                         u16 buf_size, struct ice_sq_cd *cd)
668 {
669         return ice_aq_rl_profile(hw, ice_aqc_opc_query_rl_profiles,
670                                  num_profiles, buf, buf_size, NULL, cd);
671 }
672
673 /**
674  * ice_aq_remove_rl_profile - removes RL profile(s)
675  * @hw: pointer to the HW struct
676  * @num_profiles: the number of profile(s) to remove
677  * @buf: pointer to buffer
678  * @buf_size: buffer size in bytes
679  * @num_profiles_removed: total number of profiles removed to return
680  * @cd: pointer to command details structure or NULL
681  *
682  * Remove RL profile (0x0415)
683  */
684 static enum ice_status
685 ice_aq_remove_rl_profile(struct ice_hw *hw, u16 num_profiles,
686                          struct ice_aqc_rl_profile_generic_elem *buf,
687                          u16 buf_size, u16 *num_profiles_removed,
688                          struct ice_sq_cd *cd)
689 {
690         return ice_aq_rl_profile(hw, ice_aqc_opc_remove_rl_profiles,
691                                  num_profiles, buf,
692                                  buf_size, num_profiles_removed, cd);
693 }
694
695 /**
696  * ice_sched_del_rl_profile - remove RL profile
697  * @hw: pointer to the HW struct
698  * @rl_info: rate limit profile information
699  *
700  * If the profile ID is not referenced anymore, it removes profile ID with
701  * its associated parameters from HW DB,and locally. The caller needs to
702  * hold scheduler lock.
703  */
704 static enum ice_status
705 ice_sched_del_rl_profile(struct ice_hw *hw,
706                          struct ice_aqc_rl_profile_info *rl_info)
707 {
708         struct ice_aqc_rl_profile_generic_elem *buf;
709         u16 num_profiles_removed;
710         enum ice_status status;
711         u16 num_profiles = 1;
712
713         if (rl_info->prof_id_ref != 0)
714                 return ICE_ERR_IN_USE;
715
716         /* Safe to remove profile ID */
717         buf = (struct ice_aqc_rl_profile_generic_elem *)
718                 &rl_info->profile;
719         status = ice_aq_remove_rl_profile(hw, num_profiles, buf, sizeof(*buf),
720                                           &num_profiles_removed, NULL);
721         if (status || num_profiles_removed != num_profiles)
722                 return ICE_ERR_CFG;
723
724         /* Delete stale entry now */
725         LIST_DEL(&rl_info->list_entry);
726         ice_free(hw, rl_info);
727         return status;
728 }
729
730 /**
731  * ice_sched_clear_rl_prof - clears RL prof entries
732  * @pi: port information structure
733  *
734  * This function removes all RL profile from HW as well as from SW DB.
735  */
736 static void ice_sched_clear_rl_prof(struct ice_port_info *pi)
737 {
738         u16 ln;
739
740         for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
741                 struct ice_aqc_rl_profile_info *rl_prof_elem;
742                 struct ice_aqc_rl_profile_info *rl_prof_tmp;
743
744                 LIST_FOR_EACH_ENTRY_SAFE(rl_prof_elem, rl_prof_tmp,
745                                          &pi->rl_prof_list[ln],
746                                          ice_aqc_rl_profile_info, list_entry) {
747                         struct ice_hw *hw = pi->hw;
748                         enum ice_status status;
749
750                         rl_prof_elem->prof_id_ref = 0;
751                         status = ice_sched_del_rl_profile(hw, rl_prof_elem);
752                         if (status) {
753                                 ice_debug(hw, ICE_DBG_SCHED,
754                                           "Remove rl profile failed\n");
755                                 /* On error, free mem required */
756                                 LIST_DEL(&rl_prof_elem->list_entry);
757                                 ice_free(hw, rl_prof_elem);
758                         }
759                 }
760         }
761 }
762
763 /**
764  * ice_sched_clear_agg - clears the aggregator related information
765  * @hw: pointer to the hardware structure
766  *
767  * This function removes aggregator list and free up aggregator related memory
768  * previously allocated.
769  */
770 void ice_sched_clear_agg(struct ice_hw *hw)
771 {
772         struct ice_sched_agg_info *agg_info;
773         struct ice_sched_agg_info *atmp;
774
775         LIST_FOR_EACH_ENTRY_SAFE(agg_info, atmp, &hw->agg_list,
776                                  ice_sched_agg_info,
777                                  list_entry) {
778                 struct ice_sched_agg_vsi_info *agg_vsi_info;
779                 struct ice_sched_agg_vsi_info *vtmp;
780
781                 LIST_FOR_EACH_ENTRY_SAFE(agg_vsi_info, vtmp,
782                                          &agg_info->agg_vsi_list,
783                                          ice_sched_agg_vsi_info, list_entry) {
784                         LIST_DEL(&agg_vsi_info->list_entry);
785                         ice_free(hw, agg_vsi_info);
786                 }
787                 LIST_DEL(&agg_info->list_entry);
788                 ice_free(hw, agg_info);
789         }
790 }
791
792 /**
793  * ice_sched_clear_tx_topo - clears the schduler tree nodes
794  * @pi: port information structure
795  *
796  * This function removes all the nodes from HW as well as from SW DB.
797  */
798 static void ice_sched_clear_tx_topo(struct ice_port_info *pi)
799 {
800         if (!pi)
801                 return;
802         /* remove RL profiles related lists */
803         ice_sched_clear_rl_prof(pi);
804         if (pi->root) {
805                 ice_free_sched_node(pi, pi->root);
806                 pi->root = NULL;
807         }
808 }
809
810 /**
811  * ice_sched_clear_port - clear the scheduler elements from SW DB for a port
812  * @pi: port information structure
813  *
814  * Cleanup scheduling elements from SW DB
815  */
816 void ice_sched_clear_port(struct ice_port_info *pi)
817 {
818         if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
819                 return;
820
821         pi->port_state = ICE_SCHED_PORT_STATE_INIT;
822         ice_acquire_lock(&pi->sched_lock);
823         ice_sched_clear_tx_topo(pi);
824         ice_release_lock(&pi->sched_lock);
825         ice_destroy_lock(&pi->sched_lock);
826 }
827
828 /**
829  * ice_sched_cleanup_all - cleanup scheduler elements from SW DB for all ports
830  * @hw: pointer to the HW struct
831  *
832  * Cleanup scheduling elements from SW DB for all the ports
833  */
834 void ice_sched_cleanup_all(struct ice_hw *hw)
835 {
836         if (!hw)
837                 return;
838
839         if (hw->layer_info) {
840                 ice_free(hw, hw->layer_info);
841                 hw->layer_info = NULL;
842         }
843
844         if (hw->port_info)
845                 ice_sched_clear_port(hw->port_info);
846
847         hw->num_tx_sched_layers = 0;
848         hw->num_tx_sched_phys_layers = 0;
849         hw->flattened_layers = 0;
850         hw->max_cgds = 0;
851 }
852
853 /**
854  * ice_aq_cfg_l2_node_cgd - configures L2 node to CGD mapping
855  * @hw: pointer to the HW struct
856  * @num_l2_nodes: the number of L2 nodes whose CGDs to configure
857  * @buf: pointer to buffer
858  * @buf_size: buffer size in bytes
859  * @cd: pointer to command details structure or NULL
860  *
861  * Configure L2 Node CGD (0x0414)
862  */
863 enum ice_status
864 ice_aq_cfg_l2_node_cgd(struct ice_hw *hw, u16 num_l2_nodes,
865                        struct ice_aqc_cfg_l2_node_cgd_data *buf,
866                        u16 buf_size, struct ice_sq_cd *cd)
867 {
868         struct ice_aqc_cfg_l2_node_cgd *cmd;
869         struct ice_aq_desc desc;
870
871         cmd = &desc.params.cfg_l2_node_cgd;
872         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_cfg_l2_node_cgd);
873         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
874
875         cmd->num_l2_nodes = CPU_TO_LE16(num_l2_nodes);
876         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
877 }
878
879
880 /**
881  * ice_sched_add_elems - add nodes to HW and SW DB
882  * @pi: port information structure
883  * @tc_node: pointer to the branch node
884  * @parent: pointer to the parent node
885  * @layer: layer number to add nodes
886  * @num_nodes: number of nodes
887  * @num_nodes_added: pointer to num nodes added
888  * @first_node_teid: if new nodes are added then return the TEID of first node
889  *
890  * This function add nodes to HW as well as to SW DB for a given layer
891  */
892 static enum ice_status
893 ice_sched_add_elems(struct ice_port_info *pi, struct ice_sched_node *tc_node,
894                     struct ice_sched_node *parent, u8 layer, u16 num_nodes,
895                     u16 *num_nodes_added, u32 *first_node_teid)
896 {
897         struct ice_sched_node *prev, *new_node;
898         struct ice_aqc_add_elem *buf;
899         u16 i, num_groups_added = 0;
900         enum ice_status status = ICE_SUCCESS;
901         struct ice_hw *hw = pi->hw;
902         u16 buf_size;
903         u32 teid;
904
905         buf_size = sizeof(*buf) + sizeof(*buf->generic) * (num_nodes - 1);
906         buf = (struct ice_aqc_add_elem *)ice_malloc(hw, buf_size);
907         if (!buf)
908                 return ICE_ERR_NO_MEMORY;
909
910         buf->hdr.parent_teid = parent->info.node_teid;
911         buf->hdr.num_elems = CPU_TO_LE16(num_nodes);
912         for (i = 0; i < num_nodes; i++) {
913                 buf->generic[i].parent_teid = parent->info.node_teid;
914                 buf->generic[i].data.elem_type = ICE_AQC_ELEM_TYPE_SE_GENERIC;
915                 buf->generic[i].data.valid_sections =
916                         ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
917                         ICE_AQC_ELEM_VALID_EIR;
918                 buf->generic[i].data.generic = 0;
919                 buf->generic[i].data.cir_bw.bw_profile_idx =
920                         CPU_TO_LE16(ICE_SCHED_DFLT_RL_PROF_ID);
921                 buf->generic[i].data.cir_bw.bw_alloc =
922                         CPU_TO_LE16(ICE_SCHED_DFLT_BW_WT);
923                 buf->generic[i].data.eir_bw.bw_profile_idx =
924                         CPU_TO_LE16(ICE_SCHED_DFLT_RL_PROF_ID);
925                 buf->generic[i].data.eir_bw.bw_alloc =
926                         CPU_TO_LE16(ICE_SCHED_DFLT_BW_WT);
927         }
928
929         status = ice_aq_add_sched_elems(hw, 1, buf, buf_size,
930                                         &num_groups_added, NULL);
931         if (status != ICE_SUCCESS || num_groups_added != 1) {
932                 ice_debug(hw, ICE_DBG_SCHED, "add node failed FW Error %d\n",
933                           hw->adminq.sq_last_status);
934                 ice_free(hw, buf);
935                 return ICE_ERR_CFG;
936         }
937
938         *num_nodes_added = num_nodes;
939         /* add nodes to the SW DB */
940         for (i = 0; i < num_nodes; i++) {
941                 status = ice_sched_add_node(pi, layer, &buf->generic[i]);
942                 if (status != ICE_SUCCESS) {
943                         ice_debug(hw, ICE_DBG_SCHED,
944                                   "add nodes in SW DB failed status =%d\n",
945                                   status);
946                         break;
947                 }
948
949                 teid = LE32_TO_CPU(buf->generic[i].node_teid);
950                 new_node = ice_sched_find_node_by_teid(parent, teid);
951                 if (!new_node) {
952                         ice_debug(hw, ICE_DBG_SCHED,
953                                   "Node is missing for teid =%d\n", teid);
954                         break;
955                 }
956
957                 new_node->sibling = NULL;
958                 new_node->tc_num = tc_node->tc_num;
959
960                 /* add it to previous node sibling pointer */
961                 /* Note: siblings are not linked across branches */
962                 prev = ice_sched_get_first_node(pi, tc_node, layer);
963                 if (prev && prev != new_node) {
964                         while (prev->sibling)
965                                 prev = prev->sibling;
966                         prev->sibling = new_node;
967                 }
968
969                 /* initialize the sibling head */
970                 if (!pi->sib_head[tc_node->tc_num][layer])
971                         pi->sib_head[tc_node->tc_num][layer] = new_node;
972
973                 if (i == 0)
974                         *first_node_teid = teid;
975         }
976
977         ice_free(hw, buf);
978         return status;
979 }
980
981 /**
982  * ice_sched_add_nodes_to_layer - Add nodes to a given layer
983  * @pi: port information structure
984  * @tc_node: pointer to TC node
985  * @parent: pointer to parent node
986  * @layer: layer number to add nodes
987  * @num_nodes: number of nodes to be added
988  * @first_node_teid: pointer to the first node TEID
989  * @num_nodes_added: pointer to number of nodes added
990  *
991  * This function add nodes to a given layer.
992  */
993 static enum ice_status
994 ice_sched_add_nodes_to_layer(struct ice_port_info *pi,
995                              struct ice_sched_node *tc_node,
996                              struct ice_sched_node *parent, u8 layer,
997                              u16 num_nodes, u32 *first_node_teid,
998                              u16 *num_nodes_added)
999 {
1000         u32 *first_teid_ptr = first_node_teid;
1001         u16 new_num_nodes, max_child_nodes;
1002         enum ice_status status = ICE_SUCCESS;
1003         struct ice_hw *hw = pi->hw;
1004         u16 num_added = 0;
1005         u32 temp;
1006
1007         *num_nodes_added = 0;
1008
1009         if (!num_nodes)
1010                 return status;
1011
1012         if (!parent || layer < hw->sw_entry_point_layer)
1013                 return ICE_ERR_PARAM;
1014
1015         /* max children per node per layer */
1016         max_child_nodes = hw->max_children[parent->tx_sched_layer];
1017
1018         /* current number of children + required nodes exceed max children ? */
1019         if ((parent->num_children + num_nodes) > max_child_nodes) {
1020                 /* Fail if the parent is a TC node */
1021                 if (parent == tc_node)
1022                         return ICE_ERR_CFG;
1023
1024                 /* utilize all the spaces if the parent is not full */
1025                 if (parent->num_children < max_child_nodes) {
1026                         new_num_nodes = max_child_nodes - parent->num_children;
1027                         /* this recursion is intentional, and wouldn't
1028                          * go more than 2 calls
1029                          */
1030                         status = ice_sched_add_nodes_to_layer(pi, tc_node,
1031                                                               parent, layer,
1032                                                               new_num_nodes,
1033                                                               first_node_teid,
1034                                                               &num_added);
1035                         if (status != ICE_SUCCESS)
1036                                 return status;
1037
1038                         *num_nodes_added += num_added;
1039                 }
1040                 /* Don't modify the first node TEID memory if the first node was
1041                  * added already in the above call. Instead send some temp
1042                  * memory for all other recursive calls.
1043                  */
1044                 if (num_added)
1045                         first_teid_ptr = &temp;
1046
1047                 new_num_nodes = num_nodes - num_added;
1048
1049                 /* This parent is full, try the next sibling */
1050                 parent = parent->sibling;
1051
1052                 /* this recursion is intentional, for 1024 queues
1053                  * per VSI, it goes max of 16 iterations.
1054                  * 1024 / 8 = 128 layer 8 nodes
1055                  * 128 /8 = 16 (add 8 nodes per iteration)
1056                  */
1057                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
1058                                                       layer, new_num_nodes,
1059                                                       first_teid_ptr,
1060                                                       &num_added);
1061                 *num_nodes_added += num_added;
1062                 return status;
1063         }
1064
1065         status = ice_sched_add_elems(pi, tc_node, parent, layer, num_nodes,
1066                                      num_nodes_added, first_node_teid);
1067         return status;
1068 }
1069
1070 /**
1071  * ice_sched_get_qgrp_layer - get the current queue group layer number
1072  * @hw: pointer to the HW struct
1073  *
1074  * This function returns the current queue group layer number
1075  */
1076 static u8 ice_sched_get_qgrp_layer(struct ice_hw *hw)
1077 {
1078         /* It's always total layers - 1, the array is 0 relative so -2 */
1079         return hw->num_tx_sched_layers - ICE_QGRP_LAYER_OFFSET;
1080 }
1081
1082 /**
1083  * ice_sched_get_vsi_layer - get the current VSI layer number
1084  * @hw: pointer to the HW struct
1085  *
1086  * This function returns the current VSI layer number
1087  */
1088 static u8 ice_sched_get_vsi_layer(struct ice_hw *hw)
1089 {
1090         /* Num Layers       VSI layer
1091          *     9               6
1092          *     7               4
1093          *     5 or less       sw_entry_point_layer
1094          */
1095         /* calculate the VSI layer based on number of layers. */
1096         if (hw->num_tx_sched_layers > ICE_VSI_LAYER_OFFSET + 1) {
1097                 u8 layer = hw->num_tx_sched_layers - ICE_VSI_LAYER_OFFSET;
1098
1099                 if (layer > hw->sw_entry_point_layer)
1100                         return layer;
1101         }
1102         return hw->sw_entry_point_layer;
1103 }
1104
1105 /**
1106  * ice_sched_get_agg_layer - get the current aggregator layer number
1107  * @hw: pointer to the HW struct
1108  *
1109  * This function returns the current aggregator layer number
1110  */
1111 static u8 ice_sched_get_agg_layer(struct ice_hw *hw)
1112 {
1113         /* Num Layers       aggregator layer
1114          *     9               4
1115          *     7 or less       sw_entry_point_layer
1116          */
1117         /* calculate the aggregator layer based on number of layers. */
1118         if (hw->num_tx_sched_layers > ICE_AGG_LAYER_OFFSET + 1) {
1119                 u8 layer = hw->num_tx_sched_layers - ICE_AGG_LAYER_OFFSET;
1120
1121                 if (layer > hw->sw_entry_point_layer)
1122                         return layer;
1123         }
1124         return hw->sw_entry_point_layer;
1125 }
1126
1127 /**
1128  * ice_rm_dflt_leaf_node - remove the default leaf node in the tree
1129  * @pi: port information structure
1130  *
1131  * This function removes the leaf node that was created by the FW
1132  * during initialization
1133  */
1134 static void ice_rm_dflt_leaf_node(struct ice_port_info *pi)
1135 {
1136         struct ice_sched_node *node;
1137
1138         node = pi->root;
1139         while (node) {
1140                 if (!node->num_children)
1141                         break;
1142                 node = node->children[0];
1143         }
1144         if (node && node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF) {
1145                 u32 teid = LE32_TO_CPU(node->info.node_teid);
1146                 enum ice_status status;
1147
1148                 /* remove the default leaf node */
1149                 status = ice_sched_remove_elems(pi->hw, node->parent, 1, &teid);
1150                 if (!status)
1151                         ice_free_sched_node(pi, node);
1152         }
1153 }
1154
1155 /**
1156  * ice_sched_rm_dflt_nodes - free the default nodes in the tree
1157  * @pi: port information structure
1158  *
1159  * This function frees all the nodes except root and TC that were created by
1160  * the FW during initialization
1161  */
1162 static void ice_sched_rm_dflt_nodes(struct ice_port_info *pi)
1163 {
1164         struct ice_sched_node *node;
1165
1166         ice_rm_dflt_leaf_node(pi);
1167
1168         /* remove the default nodes except TC and root nodes */
1169         node = pi->root;
1170         while (node) {
1171                 if (node->tx_sched_layer >= pi->hw->sw_entry_point_layer &&
1172                     node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
1173                     node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT) {
1174                         ice_free_sched_node(pi, node);
1175                         break;
1176                 }
1177
1178                 if (!node->num_children)
1179                         break;
1180                 node = node->children[0];
1181         }
1182 }
1183
1184 /**
1185  * ice_sched_init_port - Initialize scheduler by querying information from FW
1186  * @pi: port info structure for the tree to cleanup
1187  *
1188  * This function is the initial call to find the total number of Tx scheduler
1189  * resources, default topology created by firmware and storing the information
1190  * in SW DB.
1191  */
1192 enum ice_status ice_sched_init_port(struct ice_port_info *pi)
1193 {
1194         struct ice_aqc_get_topo_elem *buf;
1195         enum ice_status status;
1196         struct ice_hw *hw;
1197         u8 num_branches;
1198         u16 num_elems;
1199         u8 i, j;
1200
1201         if (!pi)
1202                 return ICE_ERR_PARAM;
1203         hw = pi->hw;
1204
1205         /* Query the Default Topology from FW */
1206         buf = (struct ice_aqc_get_topo_elem *)ice_malloc(hw,
1207                                                          ICE_AQ_MAX_BUF_LEN);
1208         if (!buf)
1209                 return ICE_ERR_NO_MEMORY;
1210
1211         /* Query default scheduling tree topology */
1212         status = ice_aq_get_dflt_topo(hw, pi->lport, buf, ICE_AQ_MAX_BUF_LEN,
1213                                       &num_branches, NULL);
1214         if (status)
1215                 goto err_init_port;
1216
1217         /* num_branches should be between 1-8 */
1218         if (num_branches < 1 || num_branches > ICE_TXSCHED_MAX_BRANCHES) {
1219                 ice_debug(hw, ICE_DBG_SCHED, "num_branches unexpected %d\n",
1220                           num_branches);
1221                 status = ICE_ERR_PARAM;
1222                 goto err_init_port;
1223         }
1224
1225         /* get the number of elements on the default/first branch */
1226         num_elems = LE16_TO_CPU(buf[0].hdr.num_elems);
1227
1228         /* num_elems should always be between 1-9 */
1229         if (num_elems < 1 || num_elems > ICE_AQC_TOPO_MAX_LEVEL_NUM) {
1230                 ice_debug(hw, ICE_DBG_SCHED, "num_elems unexpected %d\n",
1231                           num_elems);
1232                 status = ICE_ERR_PARAM;
1233                 goto err_init_port;
1234         }
1235
1236         /* If the last node is a leaf node then the index of the queue group
1237          * layer is two less than the number of elements.
1238          */
1239         if (num_elems > 2 && buf[0].generic[num_elems - 1].data.elem_type ==
1240             ICE_AQC_ELEM_TYPE_LEAF)
1241                 pi->last_node_teid =
1242                         LE32_TO_CPU(buf[0].generic[num_elems - 2].node_teid);
1243         else
1244                 pi->last_node_teid =
1245                         LE32_TO_CPU(buf[0].generic[num_elems - 1].node_teid);
1246
1247         /* Insert the Tx Sched root node */
1248         status = ice_sched_add_root_node(pi, &buf[0].generic[0]);
1249         if (status)
1250                 goto err_init_port;
1251
1252         /* Parse the default tree and cache the information */
1253         for (i = 0; i < num_branches; i++) {
1254                 num_elems = LE16_TO_CPU(buf[i].hdr.num_elems);
1255
1256                 /* Skip root element as already inserted */
1257                 for (j = 1; j < num_elems; j++) {
1258                         /* update the sw entry point */
1259                         if (buf[0].generic[j].data.elem_type ==
1260                             ICE_AQC_ELEM_TYPE_ENTRY_POINT)
1261                                 hw->sw_entry_point_layer = j;
1262
1263                         status = ice_sched_add_node(pi, j, &buf[i].generic[j]);
1264                         if (status)
1265                                 goto err_init_port;
1266                 }
1267         }
1268
1269         /* Remove the default nodes. */
1270         if (pi->root)
1271                 ice_sched_rm_dflt_nodes(pi);
1272
1273         /* initialize the port for handling the scheduler tree */
1274         pi->port_state = ICE_SCHED_PORT_STATE_READY;
1275         ice_init_lock(&pi->sched_lock);
1276         for (i = 0; i < ICE_AQC_TOPO_MAX_LEVEL_NUM; i++)
1277                 INIT_LIST_HEAD(&pi->rl_prof_list[i]);
1278
1279 err_init_port:
1280         if (status && pi->root) {
1281                 ice_free_sched_node(pi, pi->root);
1282                 pi->root = NULL;
1283         }
1284
1285         ice_free(hw, buf);
1286         return status;
1287 }
1288
1289 /**
1290  * ice_sched_get_node - Get the struct ice_sched_node for given TEID
1291  * @pi: port information structure
1292  * @teid: Scheduler node TEID
1293  *
1294  * This function retrieves the ice_sched_node struct for given TEID from
1295  * the SW DB and returns it to the caller.
1296  */
1297 struct ice_sched_node *ice_sched_get_node(struct ice_port_info *pi, u32 teid)
1298 {
1299         struct ice_sched_node *node;
1300
1301         if (!pi)
1302                 return NULL;
1303
1304         /* Find the node starting from root */
1305         ice_acquire_lock(&pi->sched_lock);
1306         node = ice_sched_find_node_by_teid(pi->root, teid);
1307         ice_release_lock(&pi->sched_lock);
1308
1309         if (!node)
1310                 ice_debug(pi->hw, ICE_DBG_SCHED,
1311                           "Node not found for teid=0x%x\n", teid);
1312
1313         return node;
1314 }
1315
1316 /**
1317  * ice_sched_query_res_alloc - query the FW for num of logical sched layers
1318  * @hw: pointer to the HW struct
1319  *
1320  * query FW for allocated scheduler resources and store in HW struct
1321  */
1322 enum ice_status ice_sched_query_res_alloc(struct ice_hw *hw)
1323 {
1324         struct ice_aqc_query_txsched_res_resp *buf;
1325         enum ice_status status = ICE_SUCCESS;
1326         __le16 max_sibl;
1327         u8 i;
1328
1329         if (hw->layer_info)
1330                 return status;
1331
1332         buf = (struct ice_aqc_query_txsched_res_resp *)
1333                 ice_malloc(hw, sizeof(*buf));
1334         if (!buf)
1335                 return ICE_ERR_NO_MEMORY;
1336
1337         status = ice_aq_query_sched_res(hw, sizeof(*buf), buf, NULL);
1338         if (status)
1339                 goto sched_query_out;
1340
1341         hw->num_tx_sched_layers = LE16_TO_CPU(buf->sched_props.logical_levels);
1342         hw->num_tx_sched_phys_layers =
1343                 LE16_TO_CPU(buf->sched_props.phys_levels);
1344         hw->flattened_layers = buf->sched_props.flattening_bitmap;
1345         hw->max_cgds = buf->sched_props.max_pf_cgds;
1346
1347         /* max sibling group size of current layer refers to the max children
1348          * of the below layer node.
1349          * layer 1 node max children will be layer 2 max sibling group size
1350          * layer 2 node max children will be layer 3 max sibling group size
1351          * and so on. This array will be populated from root (index 0) to
1352          * qgroup layer 7. Leaf node has no children.
1353          */
1354         for (i = 0; i < hw->num_tx_sched_layers - 1; i++) {
1355                 max_sibl = buf->layer_props[i + 1].max_sibl_grp_sz;
1356                 hw->max_children[i] = LE16_TO_CPU(max_sibl);
1357         }
1358
1359         hw->layer_info = (struct ice_aqc_layer_props *)
1360                          ice_memdup(hw, buf->layer_props,
1361                                     (hw->num_tx_sched_layers *
1362                                      sizeof(*hw->layer_info)),
1363                                     ICE_DMA_TO_DMA);
1364         if (!hw->layer_info) {
1365                 status = ICE_ERR_NO_MEMORY;
1366                 goto sched_query_out;
1367         }
1368
1369
1370 sched_query_out:
1371         ice_free(hw, buf);
1372         return status;
1373 }
1374
1375 /**
1376  * ice_sched_find_node_in_subtree - Find node in part of base node subtree
1377  * @hw: pointer to the HW struct
1378  * @base: pointer to the base node
1379  * @node: pointer to the node to search
1380  *
1381  * This function checks whether a given node is part of the base node
1382  * subtree or not
1383  */
1384 bool
1385 ice_sched_find_node_in_subtree(struct ice_hw *hw, struct ice_sched_node *base,
1386                                struct ice_sched_node *node)
1387 {
1388         u8 i;
1389
1390         for (i = 0; i < base->num_children; i++) {
1391                 struct ice_sched_node *child = base->children[i];
1392
1393                 if (node == child)
1394                         return true;
1395
1396                 if (child->tx_sched_layer > node->tx_sched_layer)
1397                         return false;
1398
1399                 /* this recursion is intentional, and wouldn't
1400                  * go more than 8 calls
1401                  */
1402                 if (ice_sched_find_node_in_subtree(hw, child, node))
1403                         return true;
1404         }
1405         return false;
1406 }
1407
1408 /**
1409  * ice_sched_get_free_qparent - Get a free LAN or RDMA queue group node
1410  * @pi: port information structure
1411  * @vsi_handle: software VSI handle
1412  * @tc: branch number
1413  * @owner: LAN or RDMA
1414  *
1415  * This function retrieves a free LAN or RDMA queue group node
1416  */
1417 struct ice_sched_node *
1418 ice_sched_get_free_qparent(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
1419                            u8 owner)
1420 {
1421         struct ice_sched_node *vsi_node, *qgrp_node = NULL;
1422         struct ice_vsi_ctx *vsi_ctx;
1423         u16 max_children;
1424         u8 qgrp_layer;
1425
1426         qgrp_layer = ice_sched_get_qgrp_layer(pi->hw);
1427         max_children = pi->hw->max_children[qgrp_layer];
1428
1429         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1430         if (!vsi_ctx)
1431                 return NULL;
1432         vsi_node = vsi_ctx->sched.vsi_node[tc];
1433         /* validate invalid VSI ID */
1434         if (!vsi_node)
1435                 goto lan_q_exit;
1436
1437         /* get the first queue group node from VSI sub-tree */
1438         qgrp_node = ice_sched_get_first_node(pi, vsi_node, qgrp_layer);
1439         while (qgrp_node) {
1440                 /* make sure the qgroup node is part of the VSI subtree */
1441                 if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1442                         if (qgrp_node->num_children < max_children &&
1443                             qgrp_node->owner == owner)
1444                                 break;
1445                 qgrp_node = qgrp_node->sibling;
1446         }
1447
1448 lan_q_exit:
1449         return qgrp_node;
1450 }
1451
1452 /**
1453  * ice_sched_get_vsi_node - Get a VSI node based on VSI ID
1454  * @pi: pointer to the port information structure
1455  * @tc_node: pointer to the TC node
1456  * @vsi_handle: software VSI handle
1457  *
1458  * This function retrieves a VSI node for a given VSI ID from a given
1459  * TC branch
1460  */
1461 struct ice_sched_node *
1462 ice_sched_get_vsi_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1463                        u16 vsi_handle)
1464 {
1465         struct ice_sched_node *node;
1466         u8 vsi_layer;
1467
1468         vsi_layer = ice_sched_get_vsi_layer(pi->hw);
1469         node = ice_sched_get_first_node(pi, tc_node, vsi_layer);
1470
1471         /* Check whether it already exists */
1472         while (node) {
1473                 if (node->vsi_handle == vsi_handle)
1474                         return node;
1475                 node = node->sibling;
1476         }
1477
1478         return node;
1479 }
1480
1481 /**
1482  * ice_sched_get_agg_node - Get an aggregator node based on aggregator ID
1483  * @pi: pointer to the port information structure
1484  * @tc_node: pointer to the TC node
1485  * @agg_id: aggregator ID
1486  *
1487  * This function retrieves an aggregator node for a given aggregator ID from
1488  * a given TC branch
1489  */
1490 static struct ice_sched_node *
1491 ice_sched_get_agg_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1492                        u32 agg_id)
1493 {
1494         struct ice_sched_node *node;
1495         struct ice_hw *hw = pi->hw;
1496         u8 agg_layer;
1497
1498         if (!hw)
1499                 return NULL;
1500         agg_layer = ice_sched_get_agg_layer(hw);
1501         node = ice_sched_get_first_node(pi, tc_node, agg_layer);
1502
1503         /* Check whether it already exists */
1504         while (node) {
1505                 if (node->agg_id == agg_id)
1506                         return node;
1507                 node = node->sibling;
1508         }
1509
1510         return node;
1511 }
1512
1513 /**
1514  * ice_sched_check_node - Compare node parameters between SW DB and HW DB
1515  * @hw: pointer to the HW struct
1516  * @node: pointer to the ice_sched_node struct
1517  *
1518  * This function queries and compares the HW element with SW DB node parameters
1519  */
1520 static bool ice_sched_check_node(struct ice_hw *hw, struct ice_sched_node *node)
1521 {
1522         struct ice_aqc_get_elem buf;
1523         enum ice_status status;
1524         u32 node_teid;
1525
1526         node_teid = LE32_TO_CPU(node->info.node_teid);
1527         status = ice_sched_query_elem(hw, node_teid, &buf);
1528         if (status != ICE_SUCCESS)
1529                 return false;
1530
1531         if (memcmp(buf.generic, &node->info, sizeof(*buf.generic))) {
1532                 ice_debug(hw, ICE_DBG_SCHED, "Node mismatch for teid=0x%x\n",
1533                           node_teid);
1534                 return false;
1535         }
1536
1537         return true;
1538 }
1539
1540 /**
1541  * ice_sched_calc_vsi_child_nodes - calculate number of VSI child nodes
1542  * @hw: pointer to the HW struct
1543  * @num_qs: number of queues
1544  * @num_nodes: num nodes array
1545  *
1546  * This function calculates the number of VSI child nodes based on the
1547  * number of queues.
1548  */
1549 static void
1550 ice_sched_calc_vsi_child_nodes(struct ice_hw *hw, u16 num_qs, u16 *num_nodes)
1551 {
1552         u16 num = num_qs;
1553         u8 i, qgl, vsil;
1554
1555         qgl = ice_sched_get_qgrp_layer(hw);
1556         vsil = ice_sched_get_vsi_layer(hw);
1557
1558         /* calculate num nodes from queue group to VSI layer */
1559         for (i = qgl; i > vsil; i--) {
1560                 /* round to the next integer if there is a remainder */
1561                 num = DIVIDE_AND_ROUND_UP(num, hw->max_children[i]);
1562
1563                 /* need at least one node */
1564                 num_nodes[i] = num ? num : 1;
1565         }
1566 }
1567
1568 /**
1569  * ice_sched_add_vsi_child_nodes - add VSI child nodes to tree
1570  * @pi: port information structure
1571  * @vsi_handle: software VSI handle
1572  * @tc_node: pointer to the TC node
1573  * @num_nodes: pointer to the num nodes that needs to be added per layer
1574  * @owner: node owner (LAN or RDMA)
1575  *
1576  * This function adds the VSI child nodes to tree. It gets called for
1577  * LAN and RDMA separately.
1578  */
1579 static enum ice_status
1580 ice_sched_add_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1581                               struct ice_sched_node *tc_node, u16 *num_nodes,
1582                               u8 owner)
1583 {
1584         struct ice_sched_node *parent, *node;
1585         struct ice_hw *hw = pi->hw;
1586         enum ice_status status;
1587         u32 first_node_teid;
1588         u16 num_added = 0;
1589         u8 i, qgl, vsil;
1590
1591         qgl = ice_sched_get_qgrp_layer(hw);
1592         vsil = ice_sched_get_vsi_layer(hw);
1593         parent = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1594         for (i = vsil + 1; i <= qgl; i++) {
1595                 if (!parent)
1596                         return ICE_ERR_CFG;
1597
1598                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
1599                                                       num_nodes[i],
1600                                                       &first_node_teid,
1601                                                       &num_added);
1602                 if (status != ICE_SUCCESS || num_nodes[i] != num_added)
1603                         return ICE_ERR_CFG;
1604
1605                 /* The newly added node can be a new parent for the next
1606                  * layer nodes
1607                  */
1608                 if (num_added) {
1609                         parent = ice_sched_find_node_by_teid(tc_node,
1610                                                              first_node_teid);
1611                         node = parent;
1612                         while (node) {
1613                                 node->owner = owner;
1614                                 node = node->sibling;
1615                         }
1616                 } else {
1617                         parent = parent->children[0];
1618                 }
1619         }
1620
1621         return ICE_SUCCESS;
1622 }
1623
1624 /**
1625  * ice_sched_calc_vsi_support_nodes - calculate number of VSI support nodes
1626  * @pi: pointer to the port info structure
1627  * @tc_node: pointer to TC node
1628  * @num_nodes: pointer to num nodes array
1629  *
1630  * This function calculates the number of supported nodes needed to add this
1631  * VSI into Tx tree including the VSI, parent and intermediate nodes in below
1632  * layers
1633  */
1634 static void
1635 ice_sched_calc_vsi_support_nodes(struct ice_port_info *pi,
1636                                  struct ice_sched_node *tc_node, u16 *num_nodes)
1637 {
1638         struct ice_sched_node *node;
1639         u8 vsil;
1640         int i;
1641
1642         vsil = ice_sched_get_vsi_layer(pi->hw);
1643         for (i = vsil; i >= pi->hw->sw_entry_point_layer; i--)
1644                 /* Add intermediate nodes if TC has no children and
1645                  * need at least one node for VSI
1646                  */
1647                 if (!tc_node->num_children || i == vsil) {
1648                         num_nodes[i]++;
1649                 } else {
1650                         /* If intermediate nodes are reached max children
1651                          * then add a new one.
1652                          */
1653                         node = ice_sched_get_first_node(pi, tc_node, (u8)i);
1654                         /* scan all the siblings */
1655                         while (node) {
1656                                 if (node->num_children <
1657                                     pi->hw->max_children[i])
1658                                         break;
1659                                 node = node->sibling;
1660                         }
1661
1662                         /* tree has one intermediate node to add this new VSI.
1663                          * So no need to calculate supported nodes for below
1664                          * layers.
1665                          */
1666                         if (node)
1667                                 break;
1668                         /* all the nodes are full, allocate a new one */
1669                         num_nodes[i]++;
1670                 }
1671 }
1672
1673 /**
1674  * ice_sched_add_vsi_support_nodes - add VSI supported nodes into Tx tree
1675  * @pi: port information structure
1676  * @vsi_handle: software VSI handle
1677  * @tc_node: pointer to TC node
1678  * @num_nodes: pointer to num nodes array
1679  *
1680  * This function adds the VSI supported nodes into Tx tree including the
1681  * VSI, its parent and intermediate nodes in below layers
1682  */
1683 static enum ice_status
1684 ice_sched_add_vsi_support_nodes(struct ice_port_info *pi, u16 vsi_handle,
1685                                 struct ice_sched_node *tc_node, u16 *num_nodes)
1686 {
1687         struct ice_sched_node *parent = tc_node;
1688         enum ice_status status;
1689         u32 first_node_teid;
1690         u16 num_added = 0;
1691         u8 i, vsil;
1692
1693         if (!pi)
1694                 return ICE_ERR_PARAM;
1695
1696         vsil = ice_sched_get_vsi_layer(pi->hw);
1697         for (i = pi->hw->sw_entry_point_layer; i <= vsil; i++) {
1698                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
1699                                                       i, num_nodes[i],
1700                                                       &first_node_teid,
1701                                                       &num_added);
1702                 if (status != ICE_SUCCESS || num_nodes[i] != num_added)
1703                         return ICE_ERR_CFG;
1704
1705                 /* The newly added node can be a new parent for the next
1706                  * layer nodes
1707                  */
1708                 if (num_added)
1709                         parent = ice_sched_find_node_by_teid(tc_node,
1710                                                              first_node_teid);
1711                 else
1712                         parent = parent->children[0];
1713
1714                 if (!parent)
1715                         return ICE_ERR_CFG;
1716
1717                 if (i == vsil)
1718                         parent->vsi_handle = vsi_handle;
1719         }
1720
1721         return ICE_SUCCESS;
1722 }
1723
1724 /**
1725  * ice_sched_add_vsi_to_topo - add a new VSI into tree
1726  * @pi: port information structure
1727  * @vsi_handle: software VSI handle
1728  * @tc: TC number
1729  *
1730  * This function adds a new VSI into scheduler tree
1731  */
1732 static enum ice_status
1733 ice_sched_add_vsi_to_topo(struct ice_port_info *pi, u16 vsi_handle, u8 tc)
1734 {
1735         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1736         struct ice_sched_node *tc_node;
1737
1738         tc_node = ice_sched_get_tc_node(pi, tc);
1739         if (!tc_node)
1740                 return ICE_ERR_PARAM;
1741
1742         /* calculate number of supported nodes needed for this VSI */
1743         ice_sched_calc_vsi_support_nodes(pi, tc_node, num_nodes);
1744
1745         /* add VSI supported nodes to TC subtree */
1746         return ice_sched_add_vsi_support_nodes(pi, vsi_handle, tc_node,
1747                                                num_nodes);
1748 }
1749
1750 /**
1751  * ice_sched_update_vsi_child_nodes - update VSI child nodes
1752  * @pi: port information structure
1753  * @vsi_handle: software VSI handle
1754  * @tc: TC number
1755  * @new_numqs: new number of max queues
1756  * @owner: owner of this subtree
1757  *
1758  * This function updates the VSI child nodes based on the number of queues
1759  */
1760 static enum ice_status
1761 ice_sched_update_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1762                                  u8 tc, u16 new_numqs, u8 owner)
1763 {
1764         u16 new_num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1765         struct ice_sched_node *vsi_node;
1766         struct ice_sched_node *tc_node;
1767         struct ice_vsi_ctx *vsi_ctx;
1768         enum ice_status status = ICE_SUCCESS;
1769         struct ice_hw *hw = pi->hw;
1770         u16 prev_numqs;
1771
1772         tc_node = ice_sched_get_tc_node(pi, tc);
1773         if (!tc_node)
1774                 return ICE_ERR_CFG;
1775
1776         vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1777         if (!vsi_node)
1778                 return ICE_ERR_CFG;
1779
1780         vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1781         if (!vsi_ctx)
1782                 return ICE_ERR_PARAM;
1783
1784         prev_numqs = vsi_ctx->sched.max_lanq[tc];
1785         /* num queues are not changed or less than the previous number */
1786         if (new_numqs <= prev_numqs)
1787                 return status;
1788         status = ice_alloc_lan_q_ctx(hw, vsi_handle, tc, new_numqs);
1789         if (status)
1790                 return status;
1791
1792         if (new_numqs)
1793                 ice_sched_calc_vsi_child_nodes(hw, new_numqs, new_num_nodes);
1794         /* Keep the max number of queue configuration all the time. Update the
1795          * tree only if number of queues > previous number of queues. This may
1796          * leave some extra nodes in the tree if number of queues < previous
1797          * number but that wouldn't harm anything. Removing those extra nodes
1798          * may complicate the code if those nodes are part of SRL or
1799          * individually rate limited.
1800          */
1801         status = ice_sched_add_vsi_child_nodes(pi, vsi_handle, tc_node,
1802                                                new_num_nodes, owner);
1803         if (status)
1804                 return status;
1805         vsi_ctx->sched.max_lanq[tc] = new_numqs;
1806
1807         return ICE_SUCCESS;
1808 }
1809
1810 /**
1811  * ice_sched_cfg_vsi - configure the new/existing VSI
1812  * @pi: port information structure
1813  * @vsi_handle: software VSI handle
1814  * @tc: TC number
1815  * @maxqs: max number of queues
1816  * @owner: LAN or RDMA
1817  * @enable: TC enabled or disabled
1818  *
1819  * This function adds/updates VSI nodes based on the number of queues. If TC is
1820  * enabled and VSI is in suspended state then resume the VSI back. If TC is
1821  * disabled then suspend the VSI if it is not already.
1822  */
1823 enum ice_status
1824 ice_sched_cfg_vsi(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 maxqs,
1825                   u8 owner, bool enable)
1826 {
1827         struct ice_sched_node *vsi_node, *tc_node;
1828         struct ice_vsi_ctx *vsi_ctx;
1829         enum ice_status status = ICE_SUCCESS;
1830         struct ice_hw *hw = pi->hw;
1831
1832         ice_debug(pi->hw, ICE_DBG_SCHED, "add/config VSI %d\n", vsi_handle);
1833         tc_node = ice_sched_get_tc_node(pi, tc);
1834         if (!tc_node)
1835                 return ICE_ERR_PARAM;
1836         vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1837         if (!vsi_ctx)
1838                 return ICE_ERR_PARAM;
1839         vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1840
1841         /* suspend the VSI if TC is not enabled */
1842         if (!enable) {
1843                 if (vsi_node && vsi_node->in_use) {
1844                         u32 teid = LE32_TO_CPU(vsi_node->info.node_teid);
1845
1846                         status = ice_sched_suspend_resume_elems(hw, 1, &teid,
1847                                                                 true);
1848                         if (!status)
1849                                 vsi_node->in_use = false;
1850                 }
1851                 return status;
1852         }
1853
1854         /* TC is enabled, if it is a new VSI then add it to the tree */
1855         if (!vsi_node) {
1856                 status = ice_sched_add_vsi_to_topo(pi, vsi_handle, tc);
1857                 if (status)
1858                         return status;
1859
1860                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1861                 if (!vsi_node)
1862                         return ICE_ERR_CFG;
1863
1864                 vsi_ctx->sched.vsi_node[tc] = vsi_node;
1865                 vsi_node->in_use = true;
1866                 /* invalidate the max queues whenever VSI gets added first time
1867                  * into the scheduler tree (boot or after reset). We need to
1868                  * recreate the child nodes all the time in these cases.
1869                  */
1870                 vsi_ctx->sched.max_lanq[tc] = 0;
1871         }
1872
1873         /* update the VSI child nodes */
1874         status = ice_sched_update_vsi_child_nodes(pi, vsi_handle, tc, maxqs,
1875                                                   owner);
1876         if (status)
1877                 return status;
1878
1879         /* TC is enabled, resume the VSI if it is in the suspend state */
1880         if (!vsi_node->in_use) {
1881                 u32 teid = LE32_TO_CPU(vsi_node->info.node_teid);
1882
1883                 status = ice_sched_suspend_resume_elems(hw, 1, &teid, false);
1884                 if (!status)
1885                         vsi_node->in_use = true;
1886         }
1887
1888         return status;
1889 }
1890
1891 /**
1892  * ice_sched_rm_agg_vsi_entry - remove aggregator related VSI info entry
1893  * @pi: port information structure
1894  * @vsi_handle: software VSI handle
1895  *
1896  * This function removes single aggregator VSI info entry from
1897  * aggregator list.
1898  */
1899 static void
1900 ice_sched_rm_agg_vsi_info(struct ice_port_info *pi, u16 vsi_handle)
1901 {
1902         struct ice_sched_agg_info *agg_info;
1903         struct ice_sched_agg_info *atmp;
1904
1905         LIST_FOR_EACH_ENTRY_SAFE(agg_info, atmp, &pi->hw->agg_list,
1906                                  ice_sched_agg_info,
1907                                  list_entry) {
1908                 struct ice_sched_agg_vsi_info *agg_vsi_info;
1909                 struct ice_sched_agg_vsi_info *vtmp;
1910
1911                 LIST_FOR_EACH_ENTRY_SAFE(agg_vsi_info, vtmp,
1912                                          &agg_info->agg_vsi_list,
1913                                          ice_sched_agg_vsi_info, list_entry)
1914                         if (agg_vsi_info->vsi_handle == vsi_handle) {
1915                                 LIST_DEL(&agg_vsi_info->list_entry);
1916                                 ice_free(pi->hw, agg_vsi_info);
1917                                 return;
1918                         }
1919         }
1920 }
1921
1922 /**
1923  * ice_sched_is_leaf_node_present - check for a leaf node in the sub-tree
1924  * @node: pointer to the sub-tree node
1925  *
1926  * This function checks for a leaf node presence in a given sub-tree node.
1927  */
1928 static bool ice_sched_is_leaf_node_present(struct ice_sched_node *node)
1929 {
1930         u8 i;
1931
1932         for (i = 0; i < node->num_children; i++)
1933                 if (ice_sched_is_leaf_node_present(node->children[i]))
1934                         return true;
1935         /* check for a leaf node */
1936         return (node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF);
1937 }
1938
1939 /**
1940  * ice_sched_rm_vsi_cfg - remove the VSI and its children nodes
1941  * @pi: port information structure
1942  * @vsi_handle: software VSI handle
1943  * @owner: LAN or RDMA
1944  *
1945  * This function removes the VSI and its LAN or RDMA children nodes from the
1946  * scheduler tree.
1947  */
1948 static enum ice_status
1949 ice_sched_rm_vsi_cfg(struct ice_port_info *pi, u16 vsi_handle, u8 owner)
1950 {
1951         enum ice_status status = ICE_ERR_PARAM;
1952         struct ice_vsi_ctx *vsi_ctx;
1953         u8 i;
1954
1955         ice_debug(pi->hw, ICE_DBG_SCHED, "removing VSI %d\n", vsi_handle);
1956         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
1957                 return status;
1958         ice_acquire_lock(&pi->sched_lock);
1959         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1960         if (!vsi_ctx)
1961                 goto exit_sched_rm_vsi_cfg;
1962
1963         ice_for_each_traffic_class(i) {
1964                 struct ice_sched_node *vsi_node, *tc_node;
1965                 u8 j = 0;
1966
1967                 tc_node = ice_sched_get_tc_node(pi, i);
1968                 if (!tc_node)
1969                         continue;
1970
1971                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1972                 if (!vsi_node)
1973                         continue;
1974
1975                 if (ice_sched_is_leaf_node_present(vsi_node)) {
1976                         ice_debug(pi->hw, ICE_DBG_SCHED,
1977                                   "VSI has leaf nodes in TC %d\n", i);
1978                         status = ICE_ERR_IN_USE;
1979                         goto exit_sched_rm_vsi_cfg;
1980                 }
1981                 while (j < vsi_node->num_children) {
1982                         if (vsi_node->children[j]->owner == owner) {
1983                                 ice_free_sched_node(pi, vsi_node->children[j]);
1984
1985                                 /* reset the counter again since the num
1986                                  * children will be updated after node removal
1987                                  */
1988                                 j = 0;
1989                         } else {
1990                                 j++;
1991                         }
1992                 }
1993                 /* remove the VSI if it has no children */
1994                 if (!vsi_node->num_children) {
1995                         ice_free_sched_node(pi, vsi_node);
1996                         vsi_ctx->sched.vsi_node[i] = NULL;
1997
1998                         /* clean up aggregator related VSI info if any */
1999                         ice_sched_rm_agg_vsi_info(pi, vsi_handle);
2000                 }
2001                 if (owner == ICE_SCHED_NODE_OWNER_LAN)
2002                         vsi_ctx->sched.max_lanq[i] = 0;
2003         }
2004         status = ICE_SUCCESS;
2005
2006 exit_sched_rm_vsi_cfg:
2007         ice_release_lock(&pi->sched_lock);
2008         return status;
2009 }
2010
2011 /**
2012  * ice_rm_vsi_lan_cfg - remove VSI and its LAN children nodes
2013  * @pi: port information structure
2014  * @vsi_handle: software VSI handle
2015  *
2016  * This function clears the VSI and its LAN children nodes from scheduler tree
2017  * for all TCs.
2018  */
2019 enum ice_status ice_rm_vsi_lan_cfg(struct ice_port_info *pi, u16 vsi_handle)
2020 {
2021         return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_LAN);
2022 }
2023
2024
2025 /**
2026  * ice_sched_is_tree_balanced - Check tree nodes are identical or not
2027  * @hw: pointer to the HW struct
2028  * @node: pointer to the ice_sched_node struct
2029  *
2030  * This function compares all the nodes for a given tree against HW DB nodes
2031  * This function needs to be called with the port_info->sched_lock held
2032  */
2033 bool ice_sched_is_tree_balanced(struct ice_hw *hw, struct ice_sched_node *node)
2034 {
2035         u8 i;
2036
2037         /* start from the leaf node */
2038         for (i = 0; i < node->num_children; i++)
2039                 /* Fail if node doesn't match with the SW DB
2040                  * this recursion is intentional, and wouldn't
2041                  * go more than 9 calls
2042                  */
2043                 if (!ice_sched_is_tree_balanced(hw, node->children[i]))
2044                         return false;
2045
2046         return ice_sched_check_node(hw, node);
2047 }
2048
2049 /**
2050  * ice_aq_query_node_to_root - retrieve the tree topology for a given node TEID
2051  * @hw: pointer to the HW struct
2052  * @node_teid: node TEID
2053  * @buf: pointer to buffer
2054  * @buf_size: buffer size in bytes
2055  * @cd: pointer to command details structure or NULL
2056  *
2057  * This function retrieves the tree topology from the firmware for a given
2058  * node TEID to the root node.
2059  */
2060 enum ice_status
2061 ice_aq_query_node_to_root(struct ice_hw *hw, u32 node_teid,
2062                           struct ice_aqc_get_elem *buf, u16 buf_size,
2063                           struct ice_sq_cd *cd)
2064 {
2065         struct ice_aqc_query_node_to_root *cmd;
2066         struct ice_aq_desc desc;
2067
2068         cmd = &desc.params.query_node_to_root;
2069         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_node_to_root);
2070         cmd->teid = CPU_TO_LE32(node_teid);
2071         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2072 }
2073
2074 /**
2075  * ice_get_agg_info - get the aggregator ID
2076  * @hw: pointer to the hardware structure
2077  * @agg_id: aggregator ID
2078  *
2079  * This function validates aggregator ID. The function returns info if
2080  * aggregator ID is present in list otherwise it returns null.
2081  */
2082 static struct ice_sched_agg_info*
2083 ice_get_agg_info(struct ice_hw *hw, u32 agg_id)
2084 {
2085         struct ice_sched_agg_info *agg_info;
2086
2087         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
2088                             list_entry)
2089                 if (agg_info->agg_id == agg_id)
2090                         return agg_info;
2091
2092         return NULL;
2093 }
2094
2095 /**
2096  * ice_sched_get_free_vsi_parent - Find a free parent node in aggregator subtree
2097  * @hw: pointer to the HW struct
2098  * @node: pointer to a child node
2099  * @num_nodes: num nodes count array
2100  *
2101  * This function walks through the aggregator subtree to find a free parent
2102  * node
2103  */
2104 static struct ice_sched_node *
2105 ice_sched_get_free_vsi_parent(struct ice_hw *hw, struct ice_sched_node *node,
2106                               u16 *num_nodes)
2107 {
2108         u8 l = node->tx_sched_layer;
2109         u8 vsil, i;
2110
2111         vsil = ice_sched_get_vsi_layer(hw);
2112
2113         /* Is it VSI parent layer ? */
2114         if (l == vsil - 1)
2115                 return (node->num_children < hw->max_children[l]) ? node : NULL;
2116
2117         /* We have intermediate nodes. Let's walk through the subtree. If the
2118          * intermediate node has space to add a new node then clear the count
2119          */
2120         if (node->num_children < hw->max_children[l])
2121                 num_nodes[l] = 0;
2122         /* The below recursive call is intentional and wouldn't go more than
2123          * 2 or 3 iterations.
2124          */
2125
2126         for (i = 0; i < node->num_children; i++) {
2127                 struct ice_sched_node *parent;
2128
2129                 parent = ice_sched_get_free_vsi_parent(hw, node->children[i],
2130                                                        num_nodes);
2131                 if (parent)
2132                         return parent;
2133         }
2134
2135         return NULL;
2136 }
2137
2138 /**
2139  * ice_sched_update_parent - update the new parent in SW DB
2140  * @new_parent: pointer to a new parent node
2141  * @node: pointer to a child node
2142  *
2143  * This function removes the child from the old parent and adds it to a new
2144  * parent
2145  */
2146 static void
2147 ice_sched_update_parent(struct ice_sched_node *new_parent,
2148                         struct ice_sched_node *node)
2149 {
2150         struct ice_sched_node *old_parent;
2151         u8 i, j;
2152
2153         old_parent = node->parent;
2154
2155         /* update the old parent children */
2156         for (i = 0; i < old_parent->num_children; i++)
2157                 if (old_parent->children[i] == node) {
2158                         for (j = i + 1; j < old_parent->num_children; j++)
2159                                 old_parent->children[j - 1] =
2160                                         old_parent->children[j];
2161                         old_parent->num_children--;
2162                         break;
2163                 }
2164
2165         /* now move the node to a new parent */
2166         new_parent->children[new_parent->num_children++] = node;
2167         node->parent = new_parent;
2168         node->info.parent_teid = new_parent->info.node_teid;
2169 }
2170
2171 /**
2172  * ice_sched_move_nodes - move child nodes to a given parent
2173  * @pi: port information structure
2174  * @parent: pointer to parent node
2175  * @num_items: number of child nodes to be moved
2176  * @list: pointer to child node teids
2177  *
2178  * This function move the child nodes to a given parent.
2179  */
2180 static enum ice_status
2181 ice_sched_move_nodes(struct ice_port_info *pi, struct ice_sched_node *parent,
2182                      u16 num_items, u32 *list)
2183 {
2184         enum ice_status status = ICE_SUCCESS;
2185         struct ice_aqc_move_elem *buf;
2186         struct ice_sched_node *node;
2187         u16 i, grps_movd = 0;
2188         struct ice_hw *hw;
2189
2190         hw = pi->hw;
2191
2192         if (!parent || !num_items)
2193                 return ICE_ERR_PARAM;
2194
2195         /* Does parent have enough space */
2196         if (parent->num_children + num_items >=
2197             hw->max_children[parent->tx_sched_layer])
2198                 return ICE_ERR_AQ_FULL;
2199
2200         buf = (struct ice_aqc_move_elem *)ice_malloc(hw, sizeof(*buf));
2201         if (!buf)
2202                 return ICE_ERR_NO_MEMORY;
2203
2204         for (i = 0; i < num_items; i++) {
2205                 node = ice_sched_find_node_by_teid(pi->root, list[i]);
2206                 if (!node) {
2207                         status = ICE_ERR_PARAM;
2208                         goto move_err_exit;
2209                 }
2210
2211                 buf->hdr.src_parent_teid = node->info.parent_teid;
2212                 buf->hdr.dest_parent_teid = parent->info.node_teid;
2213                 buf->teid[0] = node->info.node_teid;
2214                 buf->hdr.num_elems = CPU_TO_LE16(1);
2215                 status = ice_aq_move_sched_elems(hw, 1, buf, sizeof(*buf),
2216                                                  &grps_movd, NULL);
2217                 if (status && grps_movd != 1) {
2218                         status = ICE_ERR_CFG;
2219                         goto move_err_exit;
2220                 }
2221
2222                 /* update the SW DB */
2223                 ice_sched_update_parent(parent, node);
2224         }
2225
2226 move_err_exit:
2227         ice_free(hw, buf);
2228         return status;
2229 }
2230
2231 /**
2232  * ice_sched_move_vsi_to_agg - move VSI to aggregator node
2233  * @pi: port information structure
2234  * @vsi_handle: software VSI handle
2235  * @agg_id: aggregator ID
2236  * @tc: TC number
2237  *
2238  * This function moves a VSI to an aggregator node or its subtree.
2239  * Intermediate nodes may be created if required.
2240  */
2241 static enum ice_status
2242 ice_sched_move_vsi_to_agg(struct ice_port_info *pi, u16 vsi_handle, u32 agg_id,
2243                           u8 tc)
2244 {
2245         struct ice_sched_node *vsi_node, *agg_node, *tc_node, *parent;
2246         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2247         u32 first_node_teid, vsi_teid;
2248         enum ice_status status;
2249         u16 num_nodes_added;
2250         u8 aggl, vsil, i;
2251
2252         tc_node = ice_sched_get_tc_node(pi, tc);
2253         if (!tc_node)
2254                 return ICE_ERR_CFG;
2255
2256         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2257         if (!agg_node)
2258                 return ICE_ERR_DOES_NOT_EXIST;
2259
2260         vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2261         if (!vsi_node)
2262                 return ICE_ERR_DOES_NOT_EXIST;
2263
2264         aggl = ice_sched_get_agg_layer(pi->hw);
2265         vsil = ice_sched_get_vsi_layer(pi->hw);
2266
2267         /* set intermediate node count to 1 between aggregator and VSI layers */
2268         for (i = aggl + 1; i < vsil; i++)
2269                 num_nodes[i] = 1;
2270
2271         /* Check if the aggregator subtree has any free node to add the VSI */
2272         for (i = 0; i < agg_node->num_children; i++) {
2273                 parent = ice_sched_get_free_vsi_parent(pi->hw,
2274                                                        agg_node->children[i],
2275                                                        num_nodes);
2276                 if (parent)
2277                         goto move_nodes;
2278         }
2279
2280         /* add new nodes */
2281         parent = agg_node;
2282         for (i = aggl + 1; i < vsil; i++) {
2283                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2284                                                       num_nodes[i],
2285                                                       &first_node_teid,
2286                                                       &num_nodes_added);
2287                 if (status != ICE_SUCCESS || num_nodes[i] != num_nodes_added)
2288                         return ICE_ERR_CFG;
2289
2290                 /* The newly added node can be a new parent for the next
2291                  * layer nodes
2292                  */
2293                 if (num_nodes_added)
2294                         parent = ice_sched_find_node_by_teid(tc_node,
2295                                                              first_node_teid);
2296                 else
2297                         parent = parent->children[0];
2298
2299                 if (!parent)
2300                         return ICE_ERR_CFG;
2301         }
2302
2303 move_nodes:
2304         vsi_teid = LE32_TO_CPU(vsi_node->info.node_teid);
2305         return ice_sched_move_nodes(pi, parent, 1, &vsi_teid);
2306 }
2307
2308 /**
2309  * ice_move_all_vsi_to_dflt_agg - move all VSI(s) to default aggregator
2310  * @pi: port information structure
2311  * @agg_info: aggregator info
2312  * @tc: traffic class number
2313  * @rm_vsi_info: true or false
2314  *
2315  * This function move all the VSI(s) to the default aggregator and delete
2316  * aggregator VSI info based on passed in boolean parameter rm_vsi_info. The
2317  * caller holds the scheduler lock.
2318  */
2319 static enum ice_status
2320 ice_move_all_vsi_to_dflt_agg(struct ice_port_info *pi,
2321                              struct ice_sched_agg_info *agg_info, u8 tc,
2322                              bool rm_vsi_info)
2323 {
2324         struct ice_sched_agg_vsi_info *agg_vsi_info;
2325         struct ice_sched_agg_vsi_info *tmp;
2326         enum ice_status status = ICE_SUCCESS;
2327
2328         LIST_FOR_EACH_ENTRY_SAFE(agg_vsi_info, tmp, &agg_info->agg_vsi_list,
2329                                  ice_sched_agg_vsi_info, list_entry) {
2330                 u16 vsi_handle = agg_vsi_info->vsi_handle;
2331
2332                 /* Move VSI to default aggregator */
2333                 if (!ice_is_tc_ena(agg_vsi_info->tc_bitmap[0], tc))
2334                         continue;
2335
2336                 status = ice_sched_move_vsi_to_agg(pi, vsi_handle,
2337                                                    ICE_DFLT_AGG_ID, tc);
2338                 if (status)
2339                         break;
2340
2341                 ice_clear_bit(tc, agg_vsi_info->tc_bitmap);
2342                 if (rm_vsi_info && !agg_vsi_info->tc_bitmap[0]) {
2343                         LIST_DEL(&agg_vsi_info->list_entry);
2344                         ice_free(pi->hw, agg_vsi_info);
2345                 }
2346         }
2347
2348         return status;
2349 }
2350
2351 /**
2352  * ice_sched_is_agg_inuse - check whether the aggregator is in use or not
2353  * @pi: port information structure
2354  * @node: node pointer
2355  *
2356  * This function checks whether the aggregator is attached with any VSI or not.
2357  */
2358 static bool
2359 ice_sched_is_agg_inuse(struct ice_port_info *pi, struct ice_sched_node *node)
2360 {
2361         u8 vsil, i;
2362
2363         vsil = ice_sched_get_vsi_layer(pi->hw);
2364         if (node->tx_sched_layer < vsil - 1) {
2365                 for (i = 0; i < node->num_children; i++)
2366                         if (ice_sched_is_agg_inuse(pi, node->children[i]))
2367                                 return true;
2368                 return false;
2369         } else {
2370                 return node->num_children ? true : false;
2371         }
2372 }
2373
2374 /**
2375  * ice_sched_rm_agg_cfg - remove the aggregator node
2376  * @pi: port information structure
2377  * @agg_id: aggregator ID
2378  * @tc: TC number
2379  *
2380  * This function removes the aggregator node and intermediate nodes if any
2381  * from the given TC
2382  */
2383 static enum ice_status
2384 ice_sched_rm_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2385 {
2386         struct ice_sched_node *tc_node, *agg_node;
2387         struct ice_hw *hw = pi->hw;
2388
2389         tc_node = ice_sched_get_tc_node(pi, tc);
2390         if (!tc_node)
2391                 return ICE_ERR_CFG;
2392
2393         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2394         if (!agg_node)
2395                 return ICE_ERR_DOES_NOT_EXIST;
2396
2397         /* Can't remove the aggregator node if it has children */
2398         if (ice_sched_is_agg_inuse(pi, agg_node))
2399                 return ICE_ERR_IN_USE;
2400
2401         /* need to remove the whole subtree if aggregator node is the
2402          * only child.
2403          */
2404         while (agg_node->tx_sched_layer > hw->sw_entry_point_layer) {
2405                 struct ice_sched_node *parent = agg_node->parent;
2406
2407                 if (!parent)
2408                         return ICE_ERR_CFG;
2409
2410                 if (parent->num_children > 1)
2411                         break;
2412
2413                 agg_node = parent;
2414         }
2415
2416         ice_free_sched_node(pi, agg_node);
2417         return ICE_SUCCESS;
2418 }
2419
2420 /**
2421  * ice_rm_agg_cfg_tc - remove aggregator configuration for TC
2422  * @pi: port information structure
2423  * @agg_info: aggregator ID
2424  * @tc: TC number
2425  * @rm_vsi_info: bool value true or false
2426  *
2427  * This function removes aggregator reference to VSI of given TC. It removes
2428  * the aggregator configuration completely for requested TC. The caller needs
2429  * to hold the scheduler lock.
2430  */
2431 static enum ice_status
2432 ice_rm_agg_cfg_tc(struct ice_port_info *pi, struct ice_sched_agg_info *agg_info,
2433                   u8 tc, bool rm_vsi_info)
2434 {
2435         enum ice_status status = ICE_SUCCESS;
2436
2437         /* If nothing to remove - return success */
2438         if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2439                 goto exit_rm_agg_cfg_tc;
2440
2441         status = ice_move_all_vsi_to_dflt_agg(pi, agg_info, tc, rm_vsi_info);
2442         if (status)
2443                 goto exit_rm_agg_cfg_tc;
2444
2445         /* Delete aggregator node(s) */
2446         status = ice_sched_rm_agg_cfg(pi, agg_info->agg_id, tc);
2447         if (status)
2448                 goto exit_rm_agg_cfg_tc;
2449
2450         ice_clear_bit(tc, agg_info->tc_bitmap);
2451 exit_rm_agg_cfg_tc:
2452         return status;
2453 }
2454
2455 /**
2456  * ice_save_agg_tc_bitmap - save aggregator TC bitmap
2457  * @pi: port information structure
2458  * @agg_id: aggregator ID
2459  * @tc_bitmap: 8 bits TC bitmap
2460  *
2461  * Save aggregator TC bitmap. This function needs to be called with scheduler
2462  * lock held.
2463  */
2464 static enum ice_status
2465 ice_save_agg_tc_bitmap(struct ice_port_info *pi, u32 agg_id,
2466                        ice_bitmap_t *tc_bitmap)
2467 {
2468         struct ice_sched_agg_info *agg_info;
2469
2470         agg_info = ice_get_agg_info(pi->hw, agg_id);
2471         if (!agg_info)
2472                 return ICE_ERR_PARAM;
2473         ice_cp_bitmap(agg_info->replay_tc_bitmap, tc_bitmap,
2474                       ICE_MAX_TRAFFIC_CLASS);
2475         return ICE_SUCCESS;
2476 }
2477
2478 /**
2479  * ice_sched_add_agg_cfg - create an aggregator node
2480  * @pi: port information structure
2481  * @agg_id: aggregator ID
2482  * @tc: TC number
2483  *
2484  * This function creates an aggregator node and intermediate nodes if required
2485  * for the given TC
2486  */
2487 static enum ice_status
2488 ice_sched_add_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2489 {
2490         struct ice_sched_node *parent, *agg_node, *tc_node;
2491         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2492         enum ice_status status = ICE_SUCCESS;
2493         struct ice_hw *hw = pi->hw;
2494         u32 first_node_teid;
2495         u16 num_nodes_added;
2496         u8 i, aggl;
2497
2498         tc_node = ice_sched_get_tc_node(pi, tc);
2499         if (!tc_node)
2500                 return ICE_ERR_CFG;
2501
2502         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2503         /* Does Agg node already exist ? */
2504         if (agg_node)
2505                 return status;
2506
2507         aggl = ice_sched_get_agg_layer(hw);
2508
2509         /* need one node in Agg layer */
2510         num_nodes[aggl] = 1;
2511
2512         /* Check whether the intermediate nodes have space to add the
2513          * new aggregator. If they are full, then SW needs to allocate a new
2514          * intermediate node on those layers
2515          */
2516         for (i = hw->sw_entry_point_layer; i < aggl; i++) {
2517                 parent = ice_sched_get_first_node(pi, tc_node, i);
2518
2519                 /* scan all the siblings */
2520                 while (parent) {
2521                         if (parent->num_children < hw->max_children[i])
2522                                 break;
2523                         parent = parent->sibling;
2524                 }
2525
2526                 /* all the nodes are full, reserve one for this layer */
2527                 if (!parent)
2528                         num_nodes[i]++;
2529         }
2530
2531         /* add the aggregator node */
2532         parent = tc_node;
2533         for (i = hw->sw_entry_point_layer; i <= aggl; i++) {
2534                 if (!parent)
2535                         return ICE_ERR_CFG;
2536
2537                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2538                                                       num_nodes[i],
2539                                                       &first_node_teid,
2540                                                       &num_nodes_added);
2541                 if (status != ICE_SUCCESS || num_nodes[i] != num_nodes_added)
2542                         return ICE_ERR_CFG;
2543
2544                 /* The newly added node can be a new parent for the next
2545                  * layer nodes
2546                  */
2547                 if (num_nodes_added) {
2548                         parent = ice_sched_find_node_by_teid(tc_node,
2549                                                              first_node_teid);
2550                         /* register aggregator ID with the aggregator node */
2551                         if (parent && i == aggl)
2552                                 parent->agg_id = agg_id;
2553                 } else {
2554                         parent = parent->children[0];
2555                 }
2556         }
2557
2558         return ICE_SUCCESS;
2559 }
2560
2561 /**
2562  * ice_sched_cfg_agg - configure aggregator node
2563  * @pi: port information structure
2564  * @agg_id: aggregator ID
2565  * @agg_type: aggregator type queue, VSI, or aggregator group
2566  * @tc_bitmap: bits TC bitmap
2567  *
2568  * It registers a unique aggregator node into scheduler services. It
2569  * allows a user to register with a unique ID to track it's resources.
2570  * The aggregator type determines if this is a queue group, VSI group
2571  * or aggregator group. It then creates the aggregator node(s) for requested
2572  * TC(s) or removes an existing aggregator node including its configuration
2573  * if indicated via tc_bitmap. Call ice_rm_agg_cfg to release aggregator
2574  * resources and remove aggregator ID.
2575  * This function needs to be called with scheduler lock held.
2576  */
2577 static enum ice_status
2578 ice_sched_cfg_agg(struct ice_port_info *pi, u32 agg_id,
2579                   enum ice_agg_type agg_type, ice_bitmap_t *tc_bitmap)
2580 {
2581         struct ice_sched_agg_info *agg_info;
2582         enum ice_status status = ICE_SUCCESS;
2583         struct ice_hw *hw = pi->hw;
2584         u8 tc;
2585
2586         agg_info = ice_get_agg_info(hw, agg_id);
2587         if (!agg_info) {
2588                 /* Create new entry for new aggregator ID */
2589                 agg_info = (struct ice_sched_agg_info *)
2590                         ice_malloc(hw, sizeof(*agg_info));
2591                 if (!agg_info) {
2592                         status = ICE_ERR_NO_MEMORY;
2593                         goto exit_reg_agg;
2594                 }
2595                 agg_info->agg_id = agg_id;
2596                 agg_info->agg_type = agg_type;
2597                 agg_info->tc_bitmap[0] = 0;
2598
2599                 /* Initialize the aggregator VSI list head */
2600                 INIT_LIST_HEAD(&agg_info->agg_vsi_list);
2601
2602                 /* Add new entry in aggregator list */
2603                 LIST_ADD(&agg_info->list_entry, &hw->agg_list);
2604         }
2605         /* Create aggregator node(s) for requested TC(s) */
2606         ice_for_each_traffic_class(tc) {
2607                 if (!ice_is_tc_ena(*tc_bitmap, tc)) {
2608                         /* Delete aggregator cfg TC if it exists previously */
2609                         status = ice_rm_agg_cfg_tc(pi, agg_info, tc, false);
2610                         if (status)
2611                                 break;
2612                         continue;
2613                 }
2614
2615                 /* Check if aggregator node for TC already exists */
2616                 if (ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2617                         continue;
2618
2619                 /* Create new aggregator node for TC */
2620                 status = ice_sched_add_agg_cfg(pi, agg_id, tc);
2621                 if (status)
2622                         break;
2623
2624                 /* Save aggregator node's TC information */
2625                 ice_set_bit(tc, agg_info->tc_bitmap);
2626         }
2627 exit_reg_agg:
2628         return status;
2629 }
2630
2631 /**
2632  * ice_cfg_agg - config aggregator node
2633  * @pi: port information structure
2634  * @agg_id: aggregator ID
2635  * @agg_type: aggregator type queue, VSI, or aggregator group
2636  * @tc_bitmap: bits TC bitmap
2637  *
2638  * This function configures aggregator node(s).
2639  */
2640 enum ice_status
2641 ice_cfg_agg(struct ice_port_info *pi, u32 agg_id, enum ice_agg_type agg_type,
2642             u8 tc_bitmap)
2643 {
2644         ice_bitmap_t bitmap = tc_bitmap;
2645         enum ice_status status;
2646
2647         ice_acquire_lock(&pi->sched_lock);
2648         status = ice_sched_cfg_agg(pi, agg_id, agg_type,
2649                                    (ice_bitmap_t *)&bitmap);
2650         if (!status)
2651                 status = ice_save_agg_tc_bitmap(pi, agg_id,
2652                                                 (ice_bitmap_t *)&bitmap);
2653         ice_release_lock(&pi->sched_lock);
2654         return status;
2655 }
2656
2657 /**
2658  * ice_get_agg_vsi_info - get the aggregator ID
2659  * @agg_info: aggregator info
2660  * @vsi_handle: software VSI handle
2661  *
2662  * The function returns aggregator VSI info based on VSI handle. This function
2663  * needs to be called with scheduler lock held.
2664  */
2665 static struct ice_sched_agg_vsi_info*
2666 ice_get_agg_vsi_info(struct ice_sched_agg_info *agg_info, u16 vsi_handle)
2667 {
2668         struct ice_sched_agg_vsi_info *agg_vsi_info;
2669
2670         LIST_FOR_EACH_ENTRY(agg_vsi_info, &agg_info->agg_vsi_list,
2671                             ice_sched_agg_vsi_info, list_entry)
2672                 if (agg_vsi_info->vsi_handle == vsi_handle)
2673                         return agg_vsi_info;
2674
2675         return NULL;
2676 }
2677
2678 /**
2679  * ice_get_vsi_agg_info - get the aggregator info of VSI
2680  * @hw: pointer to the hardware structure
2681  * @vsi_handle: Sw VSI handle
2682  *
2683  * The function returns aggregator info of VSI represented via vsi_handle. The
2684  * VSI has in this case a different aggregator than the default one. This
2685  * function needs to be called with scheduler lock held.
2686  */
2687 static struct ice_sched_agg_info*
2688 ice_get_vsi_agg_info(struct ice_hw *hw, u16 vsi_handle)
2689 {
2690         struct ice_sched_agg_info *agg_info;
2691
2692         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
2693                             list_entry) {
2694                 struct ice_sched_agg_vsi_info *agg_vsi_info;
2695
2696                 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2697                 if (agg_vsi_info)
2698                         return agg_info;
2699         }
2700         return NULL;
2701 }
2702
2703 /**
2704  * ice_save_agg_vsi_tc_bitmap - save aggregator VSI TC bitmap
2705  * @pi: port information structure
2706  * @agg_id: aggregator ID
2707  * @vsi_handle: software VSI handle
2708  * @tc_bitmap: TC bitmap of enabled TC(s)
2709  *
2710  * Save VSI to aggregator TC bitmap. This function needs to call with scheduler
2711  * lock held.
2712  */
2713 static enum ice_status
2714 ice_save_agg_vsi_tc_bitmap(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2715                            ice_bitmap_t *tc_bitmap)
2716 {
2717         struct ice_sched_agg_vsi_info *agg_vsi_info;
2718         struct ice_sched_agg_info *agg_info;
2719
2720         agg_info = ice_get_agg_info(pi->hw, agg_id);
2721         if (!agg_info)
2722                 return ICE_ERR_PARAM;
2723         /* check if entry already exist */
2724         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2725         if (!agg_vsi_info)
2726                 return ICE_ERR_PARAM;
2727         ice_cp_bitmap(agg_vsi_info->replay_tc_bitmap, tc_bitmap,
2728                       ICE_MAX_TRAFFIC_CLASS);
2729         return ICE_SUCCESS;
2730 }
2731
2732 /**
2733  * ice_sched_assoc_vsi_to_agg - associate/move VSI to new/default aggregator
2734  * @pi: port information structure
2735  * @agg_id: aggregator ID
2736  * @vsi_handle: software VSI handle
2737  * @tc_bitmap: TC bitmap of enabled TC(s)
2738  *
2739  * This function moves VSI to a new or default aggregator node. If VSI is
2740  * already associated to the aggregator node then no operation is performed on
2741  * the tree. This function needs to be called with scheduler lock held.
2742  */
2743 static enum ice_status
2744 ice_sched_assoc_vsi_to_agg(struct ice_port_info *pi, u32 agg_id,
2745                            u16 vsi_handle, ice_bitmap_t *tc_bitmap)
2746 {
2747         struct ice_sched_agg_vsi_info *agg_vsi_info;
2748         struct ice_sched_agg_info *agg_info;
2749         enum ice_status status = ICE_SUCCESS;
2750         struct ice_hw *hw = pi->hw;
2751         u8 tc;
2752
2753         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2754                 return ICE_ERR_PARAM;
2755         agg_info = ice_get_agg_info(hw, agg_id);
2756         if (!agg_info)
2757                 return ICE_ERR_PARAM;
2758         /* check if entry already exist */
2759         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2760         if (!agg_vsi_info) {
2761                 /* Create new entry for VSI under aggregator list */
2762                 agg_vsi_info = (struct ice_sched_agg_vsi_info *)
2763                         ice_malloc(hw, sizeof(*agg_vsi_info));
2764                 if (!agg_vsi_info)
2765                         return ICE_ERR_PARAM;
2766
2767                 /* add VSI ID into the aggregator list */
2768                 agg_vsi_info->vsi_handle = vsi_handle;
2769                 LIST_ADD(&agg_vsi_info->list_entry, &agg_info->agg_vsi_list);
2770         }
2771         /* Move VSI node to new aggregator node for requested TC(s) */
2772         ice_for_each_traffic_class(tc) {
2773                 if (!ice_is_tc_ena(*tc_bitmap, tc))
2774                         continue;
2775
2776                 /* Move VSI to new aggregator */
2777                 status = ice_sched_move_vsi_to_agg(pi, vsi_handle, agg_id, tc);
2778                 if (status)
2779                         break;
2780
2781                 if (agg_id != ICE_DFLT_AGG_ID)
2782                         ice_set_bit(tc, agg_vsi_info->tc_bitmap);
2783                 else
2784                         ice_clear_bit(tc, agg_vsi_info->tc_bitmap);
2785         }
2786         /* If VSI moved back to default aggregator, delete agg_vsi_info. */
2787         if (!ice_is_any_bit_set(agg_vsi_info->tc_bitmap,
2788                                 ICE_MAX_TRAFFIC_CLASS)) {
2789                 LIST_DEL(&agg_vsi_info->list_entry);
2790                 ice_free(hw, agg_vsi_info);
2791         }
2792         return status;
2793 }
2794
2795 /**
2796  * ice_sched_rm_unused_rl_prof - remove unused RL profile
2797  * @pi: port information structure
2798  *
2799  * This function removes unused rate limit profiles from the HW and
2800  * SW DB. The caller needs to hold scheduler lock.
2801  */
2802 static void ice_sched_rm_unused_rl_prof(struct ice_port_info *pi)
2803 {
2804         u16 ln;
2805
2806         for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
2807                 struct ice_aqc_rl_profile_info *rl_prof_elem;
2808                 struct ice_aqc_rl_profile_info *rl_prof_tmp;
2809
2810                 LIST_FOR_EACH_ENTRY_SAFE(rl_prof_elem, rl_prof_tmp,
2811                                          &pi->rl_prof_list[ln],
2812                                          ice_aqc_rl_profile_info, list_entry) {
2813                         if (!ice_sched_del_rl_profile(pi->hw, rl_prof_elem))
2814                                 ice_debug(pi->hw, ICE_DBG_SCHED,
2815                                           "Removed rl profile\n");
2816                 }
2817         }
2818 }
2819
2820 /**
2821  * ice_sched_update_elem - update element
2822  * @hw: pointer to the HW struct
2823  * @node: pointer to node
2824  * @info: node info to update
2825  *
2826  * It updates the HW DB, and local SW DB of node. It updates the scheduling
2827  * parameters of node from argument info data buffer (Info->data buf) and
2828  * returns success or error on config sched element failure. The caller
2829  * needs to hold scheduler lock.
2830  */
2831 static enum ice_status
2832 ice_sched_update_elem(struct ice_hw *hw, struct ice_sched_node *node,
2833                       struct ice_aqc_txsched_elem_data *info)
2834 {
2835         struct ice_aqc_conf_elem buf;
2836         enum ice_status status;
2837         u16 elem_cfgd = 0;
2838         u16 num_elems = 1;
2839
2840         buf.generic[0] = *info;
2841         /* Parent TEID is reserved field in this aq call */
2842         buf.generic[0].parent_teid = 0;
2843         /* Element type is reserved field in this aq call */
2844         buf.generic[0].data.elem_type = 0;
2845         /* Flags is reserved field in this aq call */
2846         buf.generic[0].data.flags = 0;
2847
2848         /* Update HW DB */
2849         /* Configure element node */
2850         status = ice_aq_cfg_sched_elems(hw, num_elems, &buf, sizeof(buf),
2851                                         &elem_cfgd, NULL);
2852         if (status || elem_cfgd != num_elems) {
2853                 ice_debug(hw, ICE_DBG_SCHED, "Config sched elem error\n");
2854                 return ICE_ERR_CFG;
2855         }
2856
2857         /* Config success case */
2858         /* Now update local SW DB */
2859         /* Only copy the data portion of info buffer */
2860         node->info.data = info->data;
2861         return status;
2862 }
2863
2864 /**
2865  * ice_sched_cfg_node_bw_alloc - configure node BW weight/alloc params
2866  * @hw: pointer to the HW struct
2867  * @node: sched node to configure
2868  * @rl_type: rate limit type CIR, EIR, or shared
2869  * @bw_alloc: BW weight/allocation
2870  *
2871  * This function configures node element's BW allocation.
2872  */
2873 static enum ice_status
2874 ice_sched_cfg_node_bw_alloc(struct ice_hw *hw, struct ice_sched_node *node,
2875                             enum ice_rl_type rl_type, u8 bw_alloc)
2876 {
2877         struct ice_aqc_txsched_elem_data buf;
2878         struct ice_aqc_txsched_elem *data;
2879         enum ice_status status;
2880
2881         buf = node->info;
2882         data = &buf.data;
2883         if (rl_type == ICE_MIN_BW) {
2884                 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
2885                 data->cir_bw.bw_alloc = CPU_TO_LE16(bw_alloc);
2886         } else if (rl_type == ICE_MAX_BW) {
2887                 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
2888                 data->eir_bw.bw_alloc = CPU_TO_LE16(bw_alloc);
2889         } else {
2890                 return ICE_ERR_PARAM;
2891         }
2892
2893         /* Configure element */
2894         status = ice_sched_update_elem(hw, node, &buf);
2895         return status;
2896 }
2897
2898 /**
2899  * ice_move_vsi_to_agg - moves VSI to new or default aggregator
2900  * @pi: port information structure
2901  * @agg_id: aggregator ID
2902  * @vsi_handle: software VSI handle
2903  * @tc_bitmap: TC bitmap of enabled TC(s)
2904  *
2905  * Move or associate VSI to a new or default aggregator node.
2906  */
2907 enum ice_status
2908 ice_move_vsi_to_agg(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2909                     u8 tc_bitmap)
2910 {
2911         ice_bitmap_t bitmap = tc_bitmap;
2912         enum ice_status status;
2913
2914         ice_acquire_lock(&pi->sched_lock);
2915         status = ice_sched_assoc_vsi_to_agg(pi, agg_id, vsi_handle,
2916                                             (ice_bitmap_t *)&bitmap);
2917         if (!status)
2918                 status = ice_save_agg_vsi_tc_bitmap(pi, agg_id, vsi_handle,
2919                                                     (ice_bitmap_t *)&bitmap);
2920         ice_release_lock(&pi->sched_lock);
2921         return status;
2922 }
2923
2924 /**
2925  * ice_rm_agg_cfg - remove aggregator configuration
2926  * @pi: port information structure
2927  * @agg_id: aggregator ID
2928  *
2929  * This function removes aggregator reference to VSI and delete aggregator ID
2930  * info. It removes the aggregator configuration completely.
2931  */
2932 enum ice_status ice_rm_agg_cfg(struct ice_port_info *pi, u32 agg_id)
2933 {
2934         struct ice_sched_agg_info *agg_info;
2935         enum ice_status status = ICE_SUCCESS;
2936         u8 tc;
2937
2938         ice_acquire_lock(&pi->sched_lock);
2939         agg_info = ice_get_agg_info(pi->hw, agg_id);
2940         if (!agg_info) {
2941                 status = ICE_ERR_DOES_NOT_EXIST;
2942                 goto exit_ice_rm_agg_cfg;
2943         }
2944
2945         ice_for_each_traffic_class(tc) {
2946                 status = ice_rm_agg_cfg_tc(pi, agg_info, tc, true);
2947                 if (status)
2948                         goto exit_ice_rm_agg_cfg;
2949         }
2950
2951         if (ice_is_any_bit_set(agg_info->tc_bitmap, ICE_MAX_TRAFFIC_CLASS)) {
2952                 status = ICE_ERR_IN_USE;
2953                 goto exit_ice_rm_agg_cfg;
2954         }
2955
2956         /* Safe to delete entry now */
2957         LIST_DEL(&agg_info->list_entry);
2958         ice_free(pi->hw, agg_info);
2959
2960         /* Remove unused RL profile IDs from HW and SW DB */
2961         ice_sched_rm_unused_rl_prof(pi);
2962
2963 exit_ice_rm_agg_cfg:
2964         ice_release_lock(&pi->sched_lock);
2965         return status;
2966 }
2967
2968 /**
2969  * ice_set_clear_cir_bw_alloc - set or clear CIR BW alloc information
2970  * @bw_t_info: bandwidth type information structure
2971  * @bw_alloc: Bandwidth allocation information
2972  *
2973  * Save or clear CIR BW alloc information (bw_alloc) in the passed param
2974  * bw_t_info.
2975  */
2976 static void
2977 ice_set_clear_cir_bw_alloc(struct ice_bw_type_info *bw_t_info, u16 bw_alloc)
2978 {
2979         bw_t_info->cir_bw.bw_alloc = bw_alloc;
2980         if (bw_t_info->cir_bw.bw_alloc)
2981                 ice_set_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap);
2982         else
2983                 ice_clear_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap);
2984 }
2985
2986 /**
2987  * ice_set_clear_eir_bw_alloc - set or clear EIR BW alloc information
2988  * @bw_t_info: bandwidth type information structure
2989  * @bw_alloc: Bandwidth allocation information
2990  *
2991  * Save or clear EIR BW alloc information (bw_alloc) in the passed param
2992  * bw_t_info.
2993  */
2994 static void
2995 ice_set_clear_eir_bw_alloc(struct ice_bw_type_info *bw_t_info, u16 bw_alloc)
2996 {
2997         bw_t_info->eir_bw.bw_alloc = bw_alloc;
2998         if (bw_t_info->eir_bw.bw_alloc)
2999                 ice_set_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap);
3000         else
3001                 ice_clear_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap);
3002 }
3003
3004 /**
3005  * ice_sched_save_vsi_bw_alloc - save VSI node's BW alloc information
3006  * @pi: port information structure
3007  * @vsi_handle: sw VSI handle
3008  * @tc: traffic class
3009  * @rl_type: rate limit type min or max
3010  * @bw_alloc: Bandwidth allocation information
3011  *
3012  * Save BW alloc information of VSI type node for post replay use.
3013  */
3014 static enum ice_status
3015 ice_sched_save_vsi_bw_alloc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3016                             enum ice_rl_type rl_type, u16 bw_alloc)
3017 {
3018         struct ice_vsi_ctx *vsi_ctx;
3019
3020         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3021                 return ICE_ERR_PARAM;
3022         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3023         if (!vsi_ctx)
3024                 return ICE_ERR_PARAM;
3025         switch (rl_type) {
3026         case ICE_MIN_BW:
3027                 ice_set_clear_cir_bw_alloc(&vsi_ctx->sched.bw_t_info[tc],
3028                                            bw_alloc);
3029                 break;
3030         case ICE_MAX_BW:
3031                 ice_set_clear_eir_bw_alloc(&vsi_ctx->sched.bw_t_info[tc],
3032                                            bw_alloc);
3033                 break;
3034         default:
3035                 return ICE_ERR_PARAM;
3036         }
3037         return ICE_SUCCESS;
3038 }
3039
3040 /**
3041  * ice_set_clear_cir_bw - set or clear CIR BW
3042  * @bw_t_info: bandwidth type information structure
3043  * @bw: bandwidth in Kbps - Kilo bits per sec
3044  *
3045  * Save or clear CIR bandwidth (BW) in the passed param bw_t_info.
3046  */
3047 static void
3048 ice_set_clear_cir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3049 {
3050         if (bw == ICE_SCHED_DFLT_BW) {
3051                 ice_clear_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
3052                 bw_t_info->cir_bw.bw = 0;
3053         } else {
3054                 /* Save type of BW information */
3055                 ice_set_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
3056                 bw_t_info->cir_bw.bw = bw;
3057         }
3058 }
3059
3060 /**
3061  * ice_set_clear_eir_bw - set or clear EIR BW
3062  * @bw_t_info: bandwidth type information structure
3063  * @bw: bandwidth in Kbps - Kilo bits per sec
3064  *
3065  * Save or clear EIR bandwidth (BW) in the passed param bw_t_info.
3066  */
3067 static void
3068 ice_set_clear_eir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3069 {
3070         if (bw == ICE_SCHED_DFLT_BW) {
3071                 ice_clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3072                 bw_t_info->eir_bw.bw = 0;
3073         } else {
3074                 /* EIR BW and Shared BW profiles are mutually exclusive and
3075                  * hence only one of them may be set for any given element.
3076                  * First clear earlier saved shared BW information.
3077                  */
3078                 ice_clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3079                 bw_t_info->shared_bw = 0;
3080                 /* save EIR BW information */
3081                 ice_set_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3082                 bw_t_info->eir_bw.bw = bw;
3083         }
3084 }
3085
3086 /**
3087  * ice_set_clear_shared_bw - set or clear shared BW
3088  * @bw_t_info: bandwidth type information structure
3089  * @bw: bandwidth in Kbps - Kilo bits per sec
3090  *
3091  * Save or clear shared bandwidth (BW) in the passed param bw_t_info.
3092  */
3093 static void
3094 ice_set_clear_shared_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3095 {
3096         if (bw == ICE_SCHED_DFLT_BW) {
3097                 ice_clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3098                 bw_t_info->shared_bw = 0;
3099         } else {
3100                 /* EIR BW and Shared BW profiles are mutually exclusive and
3101                  * hence only one of them may be set for any given element.
3102                  * First clear earlier saved EIR BW information.
3103                  */
3104                 ice_clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3105                 bw_t_info->eir_bw.bw = 0;
3106                 /* save shared BW information */
3107                 ice_set_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3108                 bw_t_info->shared_bw = bw;
3109         }
3110 }
3111
3112 /**
3113  * ice_sched_save_vsi_bw - save VSI node's BW information
3114  * @pi: port information structure
3115  * @vsi_handle: sw VSI handle
3116  * @tc: traffic class
3117  * @rl_type: rate limit type min, max, or shared
3118  * @bw: bandwidth in Kbps - Kilo bits per sec
3119  *
3120  * Save BW information of VSI type node for post replay use.
3121  */
3122 static enum ice_status
3123 ice_sched_save_vsi_bw(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3124                       enum ice_rl_type rl_type, u32 bw)
3125 {
3126         struct ice_vsi_ctx *vsi_ctx;
3127
3128         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3129                 return ICE_ERR_PARAM;
3130         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3131         if (!vsi_ctx)
3132                 return ICE_ERR_PARAM;
3133         switch (rl_type) {
3134         case ICE_MIN_BW:
3135                 ice_set_clear_cir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3136                 break;
3137         case ICE_MAX_BW:
3138                 ice_set_clear_eir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3139                 break;
3140         case ICE_SHARED_BW:
3141                 ice_set_clear_shared_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3142                 break;
3143         default:
3144                 return ICE_ERR_PARAM;
3145         }
3146         return ICE_SUCCESS;
3147 }
3148
3149 /**
3150  * ice_set_clear_prio - set or clear priority information
3151  * @bw_t_info: bandwidth type information structure
3152  * @prio: priority to save
3153  *
3154  * Save or clear priority (prio) in the passed param bw_t_info.
3155  */
3156 static void
3157 ice_set_clear_prio(struct ice_bw_type_info *bw_t_info, u8 prio)
3158 {
3159         bw_t_info->generic = prio;
3160         if (bw_t_info->generic)
3161                 ice_set_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap);
3162         else
3163                 ice_clear_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap);
3164 }
3165
3166 /**
3167  * ice_sched_save_vsi_prio - save VSI node's priority information
3168  * @pi: port information structure
3169  * @vsi_handle: Software VSI handle
3170  * @tc: traffic class
3171  * @prio: priority to save
3172  *
3173  * Save priority information of VSI type node for post replay use.
3174  */
3175 static enum ice_status
3176 ice_sched_save_vsi_prio(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3177                         u8 prio)
3178 {
3179         struct ice_vsi_ctx *vsi_ctx;
3180
3181         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3182                 return ICE_ERR_PARAM;
3183         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3184         if (!vsi_ctx)
3185                 return ICE_ERR_PARAM;
3186         if (tc >= ICE_MAX_TRAFFIC_CLASS)
3187                 return ICE_ERR_PARAM;
3188         ice_set_clear_prio(&vsi_ctx->sched.bw_t_info[tc], prio);
3189         return ICE_SUCCESS;
3190 }
3191
3192 /**
3193  * ice_sched_save_agg_bw_alloc - save aggregator node's BW alloc information
3194  * @pi: port information structure
3195  * @agg_id: node aggregator ID
3196  * @tc: traffic class
3197  * @rl_type: rate limit type min or max
3198  * @bw_alloc: bandwidth alloc information
3199  *
3200  * Save BW alloc information of AGG type node for post replay use.
3201  */
3202 static enum ice_status
3203 ice_sched_save_agg_bw_alloc(struct ice_port_info *pi, u32 agg_id, u8 tc,
3204                             enum ice_rl_type rl_type, u16 bw_alloc)
3205 {
3206         struct ice_sched_agg_info *agg_info;
3207
3208         agg_info = ice_get_agg_info(pi->hw, agg_id);
3209         if (!agg_info)
3210                 return ICE_ERR_PARAM;
3211         if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
3212                 return ICE_ERR_PARAM;
3213         switch (rl_type) {
3214         case ICE_MIN_BW:
3215                 ice_set_clear_cir_bw_alloc(&agg_info->bw_t_info[tc], bw_alloc);
3216                 break;
3217         case ICE_MAX_BW:
3218                 ice_set_clear_eir_bw_alloc(&agg_info->bw_t_info[tc], bw_alloc);
3219                 break;
3220         default:
3221                 return ICE_ERR_PARAM;
3222         }
3223         return ICE_SUCCESS;
3224 }
3225
3226 /**
3227  * ice_sched_save_agg_bw - save aggregator node's BW information
3228  * @pi: port information structure
3229  * @agg_id: node aggregator ID
3230  * @tc: traffic class
3231  * @rl_type: rate limit type min, max, or shared
3232  * @bw: bandwidth in Kbps - Kilo bits per sec
3233  *
3234  * Save BW information of AGG type node for post replay use.
3235  */
3236 static enum ice_status
3237 ice_sched_save_agg_bw(struct ice_port_info *pi, u32 agg_id, u8 tc,
3238                       enum ice_rl_type rl_type, u32 bw)
3239 {
3240         struct ice_sched_agg_info *agg_info;
3241
3242         agg_info = ice_get_agg_info(pi->hw, agg_id);
3243         if (!agg_info)
3244                 return ICE_ERR_PARAM;
3245         if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
3246                 return ICE_ERR_PARAM;
3247         switch (rl_type) {
3248         case ICE_MIN_BW:
3249                 ice_set_clear_cir_bw(&agg_info->bw_t_info[tc], bw);
3250                 break;
3251         case ICE_MAX_BW:
3252                 ice_set_clear_eir_bw(&agg_info->bw_t_info[tc], bw);
3253                 break;
3254         case ICE_SHARED_BW:
3255                 ice_set_clear_shared_bw(&agg_info->bw_t_info[tc], bw);
3256                 break;
3257         default:
3258                 return ICE_ERR_PARAM;
3259         }
3260         return ICE_SUCCESS;
3261 }
3262
3263 /**
3264  * ice_cfg_vsi_bw_lmt_per_tc - configure VSI BW limit per TC
3265  * @pi: port information structure
3266  * @vsi_handle: software VSI handle
3267  * @tc: traffic class
3268  * @rl_type: min or max
3269  * @bw: bandwidth in Kbps
3270  *
3271  * This function configures BW limit of VSI scheduling node based on TC
3272  * information.
3273  */
3274 enum ice_status
3275 ice_cfg_vsi_bw_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3276                           enum ice_rl_type rl_type, u32 bw)
3277 {
3278         enum ice_status status;
3279
3280         status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
3281                                                   ICE_AGG_TYPE_VSI,
3282                                                   tc, rl_type, bw);
3283         if (!status) {
3284                 ice_acquire_lock(&pi->sched_lock);
3285                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type, bw);
3286                 ice_release_lock(&pi->sched_lock);
3287         }
3288         return status;
3289 }
3290
3291 /**
3292  * ice_cfg_dflt_vsi_bw_lmt_per_tc - configure default VSI BW limit per TC
3293  * @pi: port information structure
3294  * @vsi_handle: software VSI handle
3295  * @tc: traffic class
3296  * @rl_type: min or max
3297  *
3298  * This function configures default BW limit of VSI scheduling node based on TC
3299  * information.
3300  */
3301 enum ice_status
3302 ice_cfg_vsi_bw_dflt_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3303                                enum ice_rl_type rl_type)
3304 {
3305         enum ice_status status;
3306
3307         status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
3308                                                   ICE_AGG_TYPE_VSI,
3309                                                   tc, rl_type,
3310                                                   ICE_SCHED_DFLT_BW);
3311         if (!status) {
3312                 ice_acquire_lock(&pi->sched_lock);
3313                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type,
3314                                                ICE_SCHED_DFLT_BW);
3315                 ice_release_lock(&pi->sched_lock);
3316         }
3317         return status;
3318 }
3319
3320 /**
3321  * ice_cfg_agg_bw_lmt_per_tc - configure aggregator BW limit per TC
3322  * @pi: port information structure
3323  * @agg_id: aggregator ID
3324  * @tc: traffic class
3325  * @rl_type: min or max
3326  * @bw: bandwidth in Kbps
3327  *
3328  * This function applies BW limit to aggregator scheduling node based on TC
3329  * information.
3330  */
3331 enum ice_status
3332 ice_cfg_agg_bw_lmt_per_tc(struct ice_port_info *pi, u32 agg_id, u8 tc,
3333                           enum ice_rl_type rl_type, u32 bw)
3334 {
3335         enum ice_status status;
3336
3337         status = ice_sched_set_node_bw_lmt_per_tc(pi, agg_id, ICE_AGG_TYPE_AGG,
3338                                                   tc, rl_type, bw);
3339         if (!status) {
3340                 ice_acquire_lock(&pi->sched_lock);
3341                 status = ice_sched_save_agg_bw(pi, agg_id, tc, rl_type, bw);
3342                 ice_release_lock(&pi->sched_lock);
3343         }
3344         return status;
3345 }
3346
3347 /**
3348  * ice_cfg_agg_bw_dflt_lmt_per_tc - configure aggregator BW default limit per TC
3349  * @pi: port information structure
3350  * @agg_id: aggregator ID
3351  * @tc: traffic class
3352  * @rl_type: min or max
3353  *
3354  * This function applies default BW limit to aggregator scheduling node based
3355  * on TC information.
3356  */
3357 enum ice_status
3358 ice_cfg_agg_bw_dflt_lmt_per_tc(struct ice_port_info *pi, u32 agg_id, u8 tc,
3359                                enum ice_rl_type rl_type)
3360 {
3361         enum ice_status status;
3362
3363         status = ice_sched_set_node_bw_lmt_per_tc(pi, agg_id, ICE_AGG_TYPE_AGG,
3364                                                   tc, rl_type,
3365                                                   ICE_SCHED_DFLT_BW);
3366         if (!status) {
3367                 ice_acquire_lock(&pi->sched_lock);
3368                 status = ice_sched_save_agg_bw(pi, agg_id, tc, rl_type,
3369                                                ICE_SCHED_DFLT_BW);
3370                 ice_release_lock(&pi->sched_lock);
3371         }
3372         return status;
3373 }
3374
3375 /**
3376  * ice_cfg_vsi_bw_shared_lmt - configure VSI BW shared limit
3377  * @pi: port information structure
3378  * @vsi_handle: software VSI handle
3379  * @bw: bandwidth in Kbps
3380  *
3381  * This function Configures shared rate limiter(SRL) of all VSI type nodes
3382  * across all traffic classes for VSI matching handle.
3383  */
3384 enum ice_status
3385 ice_cfg_vsi_bw_shared_lmt(struct ice_port_info *pi, u16 vsi_handle, u32 bw)
3386 {
3387         return ice_sched_set_vsi_bw_shared_lmt(pi, vsi_handle, bw);
3388 }
3389
3390 /**
3391  * ice_cfg_vsi_bw_no_shared_lmt - configure VSI BW for no shared limiter
3392  * @pi: port information structure
3393  * @vsi_handle: software VSI handle
3394  *
3395  * This function removes the shared rate limiter(SRL) of all VSI type nodes
3396  * across all traffic classes for VSI matching handle.
3397  */
3398 enum ice_status
3399 ice_cfg_vsi_bw_no_shared_lmt(struct ice_port_info *pi, u16 vsi_handle)
3400 {
3401         return ice_sched_set_vsi_bw_shared_lmt(pi, vsi_handle,
3402                                                ICE_SCHED_DFLT_BW);
3403 }
3404
3405 /**
3406  * ice_cfg_agg_bw_shared_lmt - configure aggregator BW shared limit
3407  * @pi: port information structure
3408  * @agg_id: aggregator ID
3409  * @bw: bandwidth in Kbps
3410  *
3411  * This function configures the shared rate limiter(SRL) of all aggregator type
3412  * nodes across all traffic classes for aggregator matching agg_id.
3413  */
3414 enum ice_status
3415 ice_cfg_agg_bw_shared_lmt(struct ice_port_info *pi, u32 agg_id, u32 bw)
3416 {
3417         return ice_sched_set_agg_bw_shared_lmt(pi, agg_id, bw);
3418 }
3419
3420 /**
3421  * ice_cfg_agg_bw_no_shared_lmt - configure aggregator BW for no shared limiter
3422  * @pi: port information structure
3423  * @agg_id: aggregator ID
3424  *
3425  * This function removes the shared rate limiter(SRL) of all aggregator type
3426  * nodes across all traffic classes for aggregator matching agg_id.
3427  */
3428 enum ice_status
3429 ice_cfg_agg_bw_no_shared_lmt(struct ice_port_info *pi, u32 agg_id)
3430 {
3431         return ice_sched_set_agg_bw_shared_lmt(pi, agg_id, ICE_SCHED_DFLT_BW);
3432 }
3433
3434 /**
3435  * ice_config_vsi_queue_priority - config VSI queue priority of node
3436  * @pi: port information structure
3437  * @num_qs: number of VSI queues
3438  * @q_ids: queue IDs array
3439  * @q_ids: queue IDs array
3440  * @q_prio: queue priority array
3441  *
3442  * This function configures the queue node priority (Sibling Priority) of the
3443  * passed in VSI's queue(s) for a given traffic class (TC).
3444  */
3445 enum ice_status
3446 ice_cfg_vsi_q_priority(struct ice_port_info *pi, u16 num_qs, u32 *q_ids,
3447                        u8 *q_prio)
3448 {
3449         enum ice_status status = ICE_ERR_PARAM;
3450         u16 i;
3451
3452         ice_acquire_lock(&pi->sched_lock);
3453
3454         for (i = 0; i < num_qs; i++) {
3455                 struct ice_sched_node *node;
3456
3457                 node = ice_sched_find_node_by_teid(pi->root, q_ids[i]);
3458                 if (!node || node->info.data.elem_type !=
3459                     ICE_AQC_ELEM_TYPE_LEAF) {
3460                         status = ICE_ERR_PARAM;
3461                         break;
3462                 }
3463                 /* Configure Priority */
3464                 status = ice_sched_cfg_sibl_node_prio(pi, node, q_prio[i]);
3465                 if (status)
3466                         break;
3467         }
3468
3469         ice_release_lock(&pi->sched_lock);
3470         return status;
3471 }
3472
3473 /**
3474  * ice_cfg_agg_vsi_priority_per_tc - config aggregator's VSI priority per TC
3475  * @pi: port information structure
3476  * @agg_id: Aggregator ID
3477  * @num_vsis: number of VSI(s)
3478  * @vsi_handle_arr: array of software VSI handles
3479  * @node_prio: pointer to node priority
3480  * @tc: traffic class
3481  *
3482  * This function configures the node priority (Sibling Priority) of the
3483  * passed in VSI's for a given traffic class (TC) of an Aggregator ID.
3484  */
3485 enum ice_status
3486 ice_cfg_agg_vsi_priority_per_tc(struct ice_port_info *pi, u32 agg_id,
3487                                 u16 num_vsis, u16 *vsi_handle_arr,
3488                                 u8 *node_prio, u8 tc)
3489 {
3490         struct ice_sched_agg_vsi_info *agg_vsi_info;
3491         struct ice_sched_node *tc_node, *agg_node;
3492         enum ice_status status = ICE_ERR_PARAM;
3493         struct ice_sched_agg_info *agg_info;
3494         bool agg_id_present = false;
3495         struct ice_hw *hw = pi->hw;
3496         u16 i;
3497
3498         ice_acquire_lock(&pi->sched_lock);
3499         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
3500                             list_entry)
3501                 if (agg_info->agg_id == agg_id) {
3502                         agg_id_present = true;
3503                         break;
3504                 }
3505         if (!agg_id_present)
3506                 goto exit_agg_priority_per_tc;
3507
3508         tc_node = ice_sched_get_tc_node(pi, tc);
3509         if (!tc_node)
3510                 goto exit_agg_priority_per_tc;
3511
3512         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
3513         if (!agg_node)
3514                 goto exit_agg_priority_per_tc;
3515
3516         if (num_vsis > hw->max_children[agg_node->tx_sched_layer])
3517                 goto exit_agg_priority_per_tc;
3518
3519         for (i = 0; i < num_vsis; i++) {
3520                 struct ice_sched_node *vsi_node;
3521                 bool vsi_handle_valid = false;
3522                 u16 vsi_handle;
3523
3524                 status = ICE_ERR_PARAM;
3525                 vsi_handle = vsi_handle_arr[i];
3526                 if (!ice_is_vsi_valid(hw, vsi_handle))
3527                         goto exit_agg_priority_per_tc;
3528                 /* Verify child nodes before applying settings */
3529                 LIST_FOR_EACH_ENTRY(agg_vsi_info, &agg_info->agg_vsi_list,
3530                                     ice_sched_agg_vsi_info, list_entry)
3531                         if (agg_vsi_info->vsi_handle == vsi_handle) {
3532                                 /* cppcheck-suppress unreadVariable */
3533                                 vsi_handle_valid = true;
3534                                 break;
3535                         }
3536
3537                 if (!vsi_handle_valid)
3538                         goto exit_agg_priority_per_tc;
3539
3540                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
3541                 if (!vsi_node)
3542                         goto exit_agg_priority_per_tc;
3543
3544                 if (ice_sched_find_node_in_subtree(hw, agg_node, vsi_node)) {
3545                         /* Configure Priority */
3546                         status = ice_sched_cfg_sibl_node_prio(pi, vsi_node,
3547                                                               node_prio[i]);
3548                         if (status)
3549                                 break;
3550                         status = ice_sched_save_vsi_prio(pi, vsi_handle, tc,
3551                                                          node_prio[i]);
3552                         if (status)
3553                                 break;
3554                 }
3555         }
3556
3557 exit_agg_priority_per_tc:
3558         ice_release_lock(&pi->sched_lock);
3559         return status;
3560 }
3561
3562 /**
3563  * ice_cfg_vsi_bw_alloc - config VSI BW alloc per TC
3564  * @pi: port information structure
3565  * @vsi_handle: software VSI handle
3566  * @ena_tcmap: enabled TC map
3567  * @rl_type: Rate limit type CIR/EIR
3568  * @bw_alloc: Array of BW alloc
3569  *
3570  * This function configures the BW allocation of the passed in VSI's
3571  * node(s) for enabled traffic class.
3572  */
3573 enum ice_status
3574 ice_cfg_vsi_bw_alloc(struct ice_port_info *pi, u16 vsi_handle, u8 ena_tcmap,
3575                      enum ice_rl_type rl_type, u8 *bw_alloc)
3576 {
3577         enum ice_status status = ICE_SUCCESS;
3578         u8 tc;
3579
3580         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3581                 return ICE_ERR_PARAM;
3582
3583         ice_acquire_lock(&pi->sched_lock);
3584
3585         /* Return success if no nodes are present across TC */
3586         ice_for_each_traffic_class(tc) {
3587                 struct ice_sched_node *tc_node, *vsi_node;
3588
3589                 if (!ice_is_tc_ena(ena_tcmap, tc))
3590                         continue;
3591
3592                 tc_node = ice_sched_get_tc_node(pi, tc);
3593                 if (!tc_node)
3594                         continue;
3595
3596                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
3597                 if (!vsi_node)
3598                         continue;
3599
3600                 status = ice_sched_cfg_node_bw_alloc(pi->hw, vsi_node, rl_type,
3601                                                      bw_alloc[tc]);
3602                 if (status)
3603                         break;
3604                 status = ice_sched_save_vsi_bw_alloc(pi, vsi_handle, tc,
3605                                                      rl_type, bw_alloc[tc]);
3606                 if (status)
3607                         break;
3608         }
3609
3610         ice_release_lock(&pi->sched_lock);
3611         return status;
3612 }
3613
3614 /**
3615  * ice_cfg_agg_bw_alloc - config aggregator BW alloc
3616  * @pi: port information structure
3617  * @agg_id: aggregator ID
3618  * @ena_tcmap: enabled TC map
3619  * @rl_type: rate limit type CIR/EIR
3620  * @bw_alloc: array of BW alloc
3621  *
3622  * This function configures the BW allocation of passed in aggregator for
3623  * enabled traffic class(s).
3624  */
3625 enum ice_status
3626 ice_cfg_agg_bw_alloc(struct ice_port_info *pi, u32 agg_id, u8 ena_tcmap,
3627                      enum ice_rl_type rl_type, u8 *bw_alloc)
3628 {
3629         struct ice_sched_agg_info *agg_info;
3630         bool agg_id_present = false;
3631         enum ice_status status = ICE_SUCCESS;
3632         struct ice_hw *hw = pi->hw;
3633         u8 tc;
3634
3635         ice_acquire_lock(&pi->sched_lock);
3636         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
3637                             list_entry)
3638                 if (agg_info->agg_id == agg_id) {
3639                         agg_id_present = true;
3640                         break;
3641                 }
3642         if (!agg_id_present) {
3643                 status = ICE_ERR_PARAM;
3644                 goto exit_cfg_agg_bw_alloc;
3645         }
3646
3647         /* Return success if no nodes are present across TC */
3648         ice_for_each_traffic_class(tc) {
3649                 struct ice_sched_node *tc_node, *agg_node;
3650
3651                 if (!ice_is_tc_ena(ena_tcmap, tc))
3652                         continue;
3653
3654                 tc_node = ice_sched_get_tc_node(pi, tc);
3655                 if (!tc_node)
3656                         continue;
3657
3658                 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
3659                 if (!agg_node)
3660                         continue;
3661
3662                 status = ice_sched_cfg_node_bw_alloc(hw, agg_node, rl_type,
3663                                                      bw_alloc[tc]);
3664                 if (status)
3665                         break;
3666                 status = ice_sched_save_agg_bw_alloc(pi, agg_id, tc, rl_type,
3667                                                      bw_alloc[tc]);
3668                 if (status)
3669                         break;
3670         }
3671
3672 exit_cfg_agg_bw_alloc:
3673         ice_release_lock(&pi->sched_lock);
3674         return status;
3675 }
3676
3677 /**
3678  * ice_sched_calc_wakeup - calculate RL profile wakeup parameter
3679  * @bw: bandwidth in Kbps
3680  *
3681  * This function calculates the wakeup parameter of RL profile.
3682  */
3683 static u16 ice_sched_calc_wakeup(s32 bw)
3684 {
3685         s64 bytes_per_sec, wakeup_int, wakeup_a, wakeup_b, wakeup_f;
3686         s32 wakeup_f_int;
3687         u16 wakeup = 0;
3688
3689         /* Get the wakeup integer value */
3690         bytes_per_sec = DIV_64BIT(((s64)bw * 1000), BITS_PER_BYTE);
3691         wakeup_int = DIV_64BIT(ICE_RL_PROF_FREQUENCY, bytes_per_sec);
3692         if (wakeup_int > 63) {
3693                 wakeup = (u16)((1 << 15) | wakeup_int);
3694         } else {
3695                 /* Calculate fraction value up to 4 decimals
3696                  * Convert Integer value to a constant multiplier
3697                  */
3698                 wakeup_b = (s64)ICE_RL_PROF_MULTIPLIER * wakeup_int;
3699                 wakeup_a = DIV_64BIT((s64)ICE_RL_PROF_MULTIPLIER *
3700                                      ICE_RL_PROF_FREQUENCY, bytes_per_sec);
3701
3702                 /* Get Fraction value */
3703                 wakeup_f = wakeup_a - wakeup_b;
3704
3705                 /* Round up the Fractional value via Ceil(Fractional value) */
3706                 if (wakeup_f > DIV_64BIT(ICE_RL_PROF_MULTIPLIER, 2))
3707                         wakeup_f += 1;
3708
3709                 wakeup_f_int = (s32)DIV_64BIT(wakeup_f * ICE_RL_PROF_FRACTION,
3710                                               ICE_RL_PROF_MULTIPLIER);
3711                 wakeup |= (u16)(wakeup_int << 9);
3712                 wakeup |= (u16)(0x1ff & wakeup_f_int);
3713         }
3714
3715         return wakeup;
3716 }
3717
3718 /**
3719  * ice_sched_bw_to_rl_profile - convert BW to profile parameters
3720  * @bw: bandwidth in Kbps
3721  * @profile: profile parameters to return
3722  *
3723  * This function converts the BW to profile structure format.
3724  */
3725 static enum ice_status
3726 ice_sched_bw_to_rl_profile(u32 bw, struct ice_aqc_rl_profile_elem *profile)
3727 {
3728         enum ice_status status = ICE_ERR_PARAM;
3729         s64 bytes_per_sec, ts_rate, mv_tmp;
3730         bool found = false;
3731         s32 encode = 0;
3732         s64 mv = 0;
3733         s32 i;
3734
3735         /* Bw settings range is from 0.5Mb/sec to 100Gb/sec */
3736         if (bw < ICE_SCHED_MIN_BW || bw > ICE_SCHED_MAX_BW)
3737                 return status;
3738
3739         /* Bytes per second from Kbps */
3740         bytes_per_sec = DIV_64BIT(((s64)bw * 1000), BITS_PER_BYTE);
3741
3742         /* encode is 6 bits but really useful are 5 bits */
3743         for (i = 0; i < 64; i++) {
3744                 u64 pow_result = BIT_ULL(i);
3745
3746                 ts_rate = DIV_64BIT((s64)ICE_RL_PROF_FREQUENCY,
3747                                     pow_result * ICE_RL_PROF_TS_MULTIPLIER);
3748                 if (ts_rate <= 0)
3749                         continue;
3750
3751                 /* Multiplier value */
3752                 mv_tmp = DIV_64BIT(bytes_per_sec * ICE_RL_PROF_MULTIPLIER,
3753                                    ts_rate);
3754
3755                 /* Round to the nearest ICE_RL_PROF_MULTIPLIER */
3756                 mv = round_up_64bit(mv_tmp, ICE_RL_PROF_MULTIPLIER);
3757
3758                 /* First multiplier value greater than the given
3759                  * accuracy bytes
3760                  */
3761                 if (mv > ICE_RL_PROF_ACCURACY_BYTES) {
3762                         encode = i;
3763                         found = true;
3764                         break;
3765                 }
3766         }
3767         if (found) {
3768                 u16 wm;
3769
3770                 wm = ice_sched_calc_wakeup(bw);
3771                 profile->rl_multiply = CPU_TO_LE16(mv);
3772                 profile->wake_up_calc = CPU_TO_LE16(wm);
3773                 profile->rl_encode = CPU_TO_LE16(encode);
3774                 status = ICE_SUCCESS;
3775         } else {
3776                 status = ICE_ERR_DOES_NOT_EXIST;
3777         }
3778
3779         return status;
3780 }
3781
3782 /**
3783  * ice_sched_add_rl_profile - add RL profile
3784  * @pi: port information structure
3785  * @rl_type: type of rate limit BW - min, max, or shared
3786  * @bw: bandwidth in Kbps - Kilo bits per sec
3787  * @layer_num: specifies in which layer to create profile
3788  *
3789  * This function first checks the existing list for corresponding BW
3790  * parameter. If it exists, it returns the associated profile otherwise
3791  * it creates a new rate limit profile for requested BW, and adds it to
3792  * the HW DB and local list. It returns the new profile or null on error.
3793  * The caller needs to hold the scheduler lock.
3794  */
3795 static struct ice_aqc_rl_profile_info *
3796 ice_sched_add_rl_profile(struct ice_port_info *pi,
3797                          enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3798 {
3799         struct ice_aqc_rl_profile_generic_elem *buf;
3800         struct ice_aqc_rl_profile_info *rl_prof_elem;
3801         u16 profiles_added = 0, num_profiles = 1;
3802         enum ice_status status = ICE_ERR_PARAM;
3803         struct ice_hw *hw;
3804         u8 profile_type;
3805
3806         switch (rl_type) {
3807         case ICE_MIN_BW:
3808                 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3809                 break;
3810         case ICE_MAX_BW:
3811                 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3812                 break;
3813         case ICE_SHARED_BW:
3814                 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3815                 break;
3816         default:
3817                 return NULL;
3818         }
3819
3820         if (!pi)
3821                 return NULL;
3822         hw = pi->hw;
3823         LIST_FOR_EACH_ENTRY(rl_prof_elem, &pi->rl_prof_list[layer_num],
3824                             ice_aqc_rl_profile_info, list_entry)
3825                 if (rl_prof_elem->profile.flags == profile_type &&
3826                     rl_prof_elem->bw == bw)
3827                         /* Return existing profile ID info */
3828                         return rl_prof_elem;
3829
3830         /* Create new profile ID */
3831         rl_prof_elem = (struct ice_aqc_rl_profile_info *)
3832                 ice_malloc(hw, sizeof(*rl_prof_elem));
3833
3834         if (!rl_prof_elem)
3835                 return NULL;
3836
3837         status = ice_sched_bw_to_rl_profile(bw, &rl_prof_elem->profile);
3838         if (status != ICE_SUCCESS)
3839                 goto exit_add_rl_prof;
3840
3841         rl_prof_elem->bw = bw;
3842         /* layer_num is zero relative, and fw expects level from 1 to 9 */
3843         rl_prof_elem->profile.level = layer_num + 1;
3844         rl_prof_elem->profile.flags = profile_type;
3845         rl_prof_elem->profile.max_burst_size = CPU_TO_LE16(hw->max_burst_size);
3846
3847         /* Create new entry in HW DB */
3848         buf = (struct ice_aqc_rl_profile_generic_elem *)
3849                 &rl_prof_elem->profile;
3850         status = ice_aq_add_rl_profile(hw, num_profiles, buf, sizeof(*buf),
3851                                        &profiles_added, NULL);
3852         if (status || profiles_added != num_profiles)
3853                 goto exit_add_rl_prof;
3854
3855         /* Good entry - add in the list */
3856         rl_prof_elem->prof_id_ref = 0;
3857         LIST_ADD(&rl_prof_elem->list_entry, &pi->rl_prof_list[layer_num]);
3858         return rl_prof_elem;
3859
3860 exit_add_rl_prof:
3861         ice_free(hw, rl_prof_elem);
3862         return NULL;
3863 }
3864
3865 /**
3866  * ice_sched_cfg_node_bw_lmt - configure node sched params
3867  * @hw: pointer to the HW struct
3868  * @node: sched node to configure
3869  * @rl_type: rate limit type CIR, EIR, or shared
3870  * @rl_prof_id: rate limit profile ID
3871  *
3872  * This function configures node element's BW limit.
3873  */
3874 static enum ice_status
3875 ice_sched_cfg_node_bw_lmt(struct ice_hw *hw, struct ice_sched_node *node,
3876                           enum ice_rl_type rl_type, u16 rl_prof_id)
3877 {
3878         struct ice_aqc_txsched_elem_data buf;
3879         struct ice_aqc_txsched_elem *data;
3880
3881         buf = node->info;
3882         data = &buf.data;
3883         switch (rl_type) {
3884         case ICE_MIN_BW:
3885                 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
3886                 data->cir_bw.bw_profile_idx = CPU_TO_LE16(rl_prof_id);
3887                 break;
3888         case ICE_MAX_BW:
3889                 /* EIR BW and Shared BW profiles are mutually exclusive and
3890                  * hence only one of them may be set for any given element
3891                  */
3892                 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3893                         return ICE_ERR_CFG;
3894                 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3895                 data->eir_bw.bw_profile_idx = CPU_TO_LE16(rl_prof_id);
3896                 break;
3897         case ICE_SHARED_BW:
3898                 /* Check for removing shared BW */
3899                 if (rl_prof_id == ICE_SCHED_NO_SHARED_RL_PROF_ID) {
3900                         /* remove shared profile */
3901                         data->valid_sections &= ~ICE_AQC_ELEM_VALID_SHARED;
3902                         data->srl_id = 0; /* clear SRL field */
3903
3904                         /* enable back EIR to default profile */
3905                         data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3906                         data->eir_bw.bw_profile_idx =
3907                                 CPU_TO_LE16(ICE_SCHED_DFLT_RL_PROF_ID);
3908                         break;
3909                 }
3910                 /* EIR BW and Shared BW profiles are mutually exclusive and
3911                  * hence only one of them may be set for any given element
3912                  */
3913                 if ((data->valid_sections & ICE_AQC_ELEM_VALID_EIR) &&
3914                     (LE16_TO_CPU(data->eir_bw.bw_profile_idx) !=
3915                             ICE_SCHED_DFLT_RL_PROF_ID))
3916                         return ICE_ERR_CFG;
3917                 /* EIR BW is set to default, disable it */
3918                 data->valid_sections &= ~ICE_AQC_ELEM_VALID_EIR;
3919                 /* Okay to enable shared BW now */
3920                 data->valid_sections |= ICE_AQC_ELEM_VALID_SHARED;
3921                 data->srl_id = CPU_TO_LE16(rl_prof_id);
3922                 break;
3923         default:
3924                 /* Unknown rate limit type */
3925                 return ICE_ERR_PARAM;
3926         }
3927
3928         /* Configure element */
3929         return ice_sched_update_elem(hw, node, &buf);
3930 }
3931
3932 /**
3933  * ice_sched_get_node_rl_prof_id - get node's rate limit profile ID
3934  * @node: sched node
3935  * @rl_type: rate limit type
3936  *
3937  * If existing profile matches, it returns the corresponding rate
3938  * limit profile ID, otherwise it returns an invalid ID as error.
3939  */
3940 static u16
3941 ice_sched_get_node_rl_prof_id(struct ice_sched_node *node,
3942                               enum ice_rl_type rl_type)
3943 {
3944         u16 rl_prof_id = ICE_SCHED_INVAL_PROF_ID;
3945         struct ice_aqc_txsched_elem *data;
3946
3947         data = &node->info.data;
3948         switch (rl_type) {
3949         case ICE_MIN_BW:
3950                 if (data->valid_sections & ICE_AQC_ELEM_VALID_CIR)
3951                         rl_prof_id = LE16_TO_CPU(data->cir_bw.bw_profile_idx);
3952                 break;
3953         case ICE_MAX_BW:
3954                 if (data->valid_sections & ICE_AQC_ELEM_VALID_EIR)
3955                         rl_prof_id = LE16_TO_CPU(data->eir_bw.bw_profile_idx);
3956                 break;
3957         case ICE_SHARED_BW:
3958                 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3959                         rl_prof_id = LE16_TO_CPU(data->srl_id);
3960                 break;
3961         default:
3962                 break;
3963         }
3964
3965         return rl_prof_id;
3966 }
3967
3968 /**
3969  * ice_sched_get_rl_prof_layer - selects rate limit profile creation layer
3970  * @pi: port information structure
3971  * @rl_type: type of rate limit BW - min, max, or shared
3972  * @layer_index: layer index
3973  *
3974  * This function returns requested profile creation layer.
3975  */
3976 static u8
3977 ice_sched_get_rl_prof_layer(struct ice_port_info *pi, enum ice_rl_type rl_type,
3978                             u8 layer_index)
3979 {
3980         struct ice_hw *hw = pi->hw;
3981
3982         if (layer_index >= hw->num_tx_sched_layers)
3983                 return ICE_SCHED_INVAL_LAYER_NUM;
3984         switch (rl_type) {
3985         case ICE_MIN_BW:
3986                 if (hw->layer_info[layer_index].max_cir_rl_profiles)
3987                         return layer_index;
3988                 break;
3989         case ICE_MAX_BW:
3990                 if (hw->layer_info[layer_index].max_eir_rl_profiles)
3991                         return layer_index;
3992                 break;
3993         case ICE_SHARED_BW:
3994                 /* if current layer doesn't support SRL profile creation
3995                  * then try a layer up or down.
3996                  */
3997                 if (hw->layer_info[layer_index].max_srl_profiles)
3998                         return layer_index;
3999                 else if (layer_index < hw->num_tx_sched_layers - 1 &&
4000                          hw->layer_info[layer_index + 1].max_srl_profiles)
4001                         return layer_index + 1;
4002                 else if (layer_index > 0 &&
4003                          hw->layer_info[layer_index - 1].max_srl_profiles)
4004                         return layer_index - 1;
4005                 break;
4006         default:
4007                 break;
4008         }
4009         return ICE_SCHED_INVAL_LAYER_NUM;
4010 }
4011
4012 /**
4013  * ice_sched_get_srl_node - get shared rate limit node
4014  * @node: tree node
4015  * @srl_layer: shared rate limit layer
4016  *
4017  * This function returns SRL node to be used for shared rate limit purpose.
4018  * The caller needs to hold scheduler lock.
4019  */
4020 static struct ice_sched_node *
4021 ice_sched_get_srl_node(struct ice_sched_node *node, u8 srl_layer)
4022 {
4023         if (srl_layer > node->tx_sched_layer)
4024                 return node->children[0];
4025         else if (srl_layer < node->tx_sched_layer)
4026                 /* Node can't be created without a parent. It will always
4027                  * have a valid parent except root node.
4028                  */
4029                 return node->parent;
4030         else
4031                 return node;
4032 }
4033
4034 /**
4035  * ice_sched_rm_rl_profile - remove RL profile ID
4036  * @pi: port information structure
4037  * @layer_num: layer number where profiles are saved
4038  * @profile_type: profile type like EIR, CIR, or SRL
4039  * @profile_id: profile ID to remove
4040  *
4041  * This function removes rate limit profile from layer 'layer_num' of type
4042  * 'profile_type' and profile ID as 'profile_id'. The caller needs to hold
4043  * scheduler lock.
4044  */
4045 static enum ice_status
4046 ice_sched_rm_rl_profile(struct ice_port_info *pi, u8 layer_num, u8 profile_type,
4047                         u16 profile_id)
4048 {
4049         struct ice_aqc_rl_profile_info *rl_prof_elem;
4050         enum ice_status status = ICE_SUCCESS;
4051
4052         /* Check the existing list for RL profile */
4053         LIST_FOR_EACH_ENTRY(rl_prof_elem, &pi->rl_prof_list[layer_num],
4054                             ice_aqc_rl_profile_info, list_entry)
4055                 if (rl_prof_elem->profile.flags == profile_type &&
4056                     LE16_TO_CPU(rl_prof_elem->profile.profile_id) ==
4057                     profile_id) {
4058                         if (rl_prof_elem->prof_id_ref)
4059                                 rl_prof_elem->prof_id_ref--;
4060
4061                         /* Remove old profile ID from database */
4062                         status = ice_sched_del_rl_profile(pi->hw, rl_prof_elem);
4063                         if (status && status != ICE_ERR_IN_USE)
4064                                 ice_debug(pi->hw, ICE_DBG_SCHED,
4065                                           "Remove rl profile failed\n");
4066                         break;
4067                 }
4068         if (status == ICE_ERR_IN_USE)
4069                 status = ICE_SUCCESS;
4070         return status;
4071 }
4072
4073 /**
4074  * ice_sched_set_node_bw_dflt - set node's bandwidth limit to default
4075  * @pi: port information structure
4076  * @node: pointer to node structure
4077  * @rl_type: rate limit type min, max, or shared
4078  * @layer_num: layer number where RL profiles are saved
4079  *
4080  * This function configures node element's BW rate limit profile ID of
4081  * type CIR, EIR, or SRL to default. This function needs to be called
4082  * with the scheduler lock held.
4083  */
4084 static enum ice_status
4085 ice_sched_set_node_bw_dflt(struct ice_port_info *pi,
4086                            struct ice_sched_node *node,
4087                            enum ice_rl_type rl_type, u8 layer_num)
4088 {
4089         enum ice_status status;
4090         struct ice_hw *hw;
4091         u8 profile_type;
4092         u16 rl_prof_id;
4093         u16 old_id;
4094
4095         hw = pi->hw;
4096         switch (rl_type) {
4097         case ICE_MIN_BW:
4098                 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
4099                 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
4100                 break;
4101         case ICE_MAX_BW:
4102                 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
4103                 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
4104                 break;
4105         case ICE_SHARED_BW:
4106                 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
4107                 /* No SRL is configured for default case */
4108                 rl_prof_id = ICE_SCHED_NO_SHARED_RL_PROF_ID;
4109                 break;
4110         default:
4111                 return ICE_ERR_PARAM;
4112         }
4113         /* Save existing RL prof ID for later clean up */
4114         old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
4115         /* Configure BW scheduling parameters */
4116         status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
4117         if (status)
4118                 return status;
4119
4120         /* Remove stale RL profile ID */
4121         if (old_id == ICE_SCHED_DFLT_RL_PROF_ID ||
4122             old_id == ICE_SCHED_INVAL_PROF_ID)
4123                 return ICE_SUCCESS;
4124
4125         return ice_sched_rm_rl_profile(pi, layer_num, profile_type, old_id);
4126 }
4127
4128 /**
4129  * ice_sched_set_eir_srl_excl - set EIR/SRL exclusiveness
4130  * @pi: port information structure
4131  * @node: pointer to node structure
4132  * @layer_num: layer number where rate limit profiles are saved
4133  * @rl_type: rate limit type min, max, or shared
4134  * @bw: bandwidth value
4135  *
4136  * This function prepares node element's bandwidth to SRL or EIR exclusively.
4137  * EIR BW and Shared BW profiles are mutually exclusive and hence only one of
4138  * them may be set for any given element. This function needs to be called
4139  * with the scheduler lock held.
4140  */
4141 static enum ice_status
4142 ice_sched_set_eir_srl_excl(struct ice_port_info *pi,
4143                            struct ice_sched_node *node,
4144                            u8 layer_num, enum ice_rl_type rl_type, u32 bw)
4145 {
4146         if (rl_type == ICE_SHARED_BW) {
4147                 /* SRL node passed in this case, it may be different node */
4148                 if (bw == ICE_SCHED_DFLT_BW)
4149                         /* SRL being removed, ice_sched_cfg_node_bw_lmt()
4150                          * enables EIR to default. EIR is not set in this
4151                          * case, so no additional action is required.
4152                          */
4153                         return ICE_SUCCESS;
4154
4155                 /* SRL being configured, set EIR to default here.
4156                  * ice_sched_cfg_node_bw_lmt() disables EIR when it
4157                  * configures SRL
4158                  */
4159                 return ice_sched_set_node_bw_dflt(pi, node, ICE_MAX_BW,
4160                                                   layer_num);
4161         } else if (rl_type == ICE_MAX_BW &&
4162                    node->info.data.valid_sections & ICE_AQC_ELEM_VALID_SHARED) {
4163                 /* Remove Shared profile. Set default shared BW call
4164                  * removes shared profile for a node.
4165                  */
4166                 return ice_sched_set_node_bw_dflt(pi, node,
4167                                                   ICE_SHARED_BW,
4168                                                   layer_num);
4169         }
4170         return ICE_SUCCESS;
4171 }
4172
4173 /**
4174  * ice_sched_set_node_bw - set node's bandwidth
4175  * @pi: port information structure
4176  * @node: tree node
4177  * @rl_type: rate limit type min, max, or shared
4178  * @bw: bandwidth in Kbps - Kilo bits per sec
4179  * @layer_num: layer number
4180  *
4181  * This function adds new profile corresponding to requested BW, configures
4182  * node's RL profile ID of type CIR, EIR, or SRL, and removes old profile
4183  * ID from local database. The caller needs to hold scheduler lock.
4184  */
4185 static enum ice_status
4186 ice_sched_set_node_bw(struct ice_port_info *pi, struct ice_sched_node *node,
4187                       enum ice_rl_type rl_type, u32 bw, u8 layer_num)
4188 {
4189         struct ice_aqc_rl_profile_info *rl_prof_info;
4190         enum ice_status status = ICE_ERR_PARAM;
4191         struct ice_hw *hw = pi->hw;
4192         u16 old_id, rl_prof_id;
4193
4194         rl_prof_info = ice_sched_add_rl_profile(pi, rl_type, bw, layer_num);
4195         if (!rl_prof_info)
4196                 return status;
4197
4198         rl_prof_id = LE16_TO_CPU(rl_prof_info->profile.profile_id);
4199
4200         /* Save existing RL prof ID for later clean up */
4201         old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
4202         /* Configure BW scheduling parameters */
4203         status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
4204         if (status)
4205                 return status;
4206
4207         /* New changes has been applied */
4208         /* Increment the profile ID reference count */
4209         rl_prof_info->prof_id_ref++;
4210
4211         /* Check for old ID removal */
4212         if ((old_id == ICE_SCHED_DFLT_RL_PROF_ID && rl_type != ICE_SHARED_BW) ||
4213             old_id == ICE_SCHED_INVAL_PROF_ID || old_id == rl_prof_id)
4214                 return ICE_SUCCESS;
4215
4216         return ice_sched_rm_rl_profile(pi, layer_num,
4217                                        rl_prof_info->profile.flags,
4218                                        old_id);
4219 }
4220
4221 /**
4222  * ice_sched_set_node_bw_lmt - set node's BW limit
4223  * @pi: port information structure
4224  * @node: tree node
4225  * @rl_type: rate limit type min, max, or shared
4226  * @bw: bandwidth in Kbps - Kilo bits per sec
4227  *
4228  * It updates node's BW limit parameters like BW RL profile ID of type CIR,
4229  * EIR, or SRL. The caller needs to hold scheduler lock.
4230  */
4231 static enum ice_status
4232 ice_sched_set_node_bw_lmt(struct ice_port_info *pi, struct ice_sched_node *node,
4233                           enum ice_rl_type rl_type, u32 bw)
4234 {
4235         struct ice_sched_node *cfg_node = node;
4236         enum ice_status status;
4237
4238         struct ice_hw *hw;
4239         u8 layer_num;
4240
4241         if (!pi)
4242                 return ICE_ERR_PARAM;
4243         hw = pi->hw;
4244         /* Remove unused RL profile IDs from HW and SW DB */
4245         ice_sched_rm_unused_rl_prof(pi);
4246         layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
4247                                                 node->tx_sched_layer);
4248         if (layer_num >= hw->num_tx_sched_layers)
4249                 return ICE_ERR_PARAM;
4250
4251         if (rl_type == ICE_SHARED_BW) {
4252                 /* SRL node may be different */
4253                 cfg_node = ice_sched_get_srl_node(node, layer_num);
4254                 if (!cfg_node)
4255                         return ICE_ERR_CFG;
4256         }
4257         /* EIR BW and Shared BW profiles are mutually exclusive and
4258          * hence only one of them may be set for any given element
4259          */
4260         status = ice_sched_set_eir_srl_excl(pi, cfg_node, layer_num, rl_type,
4261                                             bw);
4262         if (status)
4263                 return status;
4264         if (bw == ICE_SCHED_DFLT_BW)
4265                 return ice_sched_set_node_bw_dflt(pi, cfg_node, rl_type,
4266                                                   layer_num);
4267         return ice_sched_set_node_bw(pi, cfg_node, rl_type, bw, layer_num);
4268 }
4269
4270 /**
4271  * ice_sched_set_node_bw_dflt_lmt - set node's BW limit to default
4272  * @pi: port information structure
4273  * @node: pointer to node structure
4274  * @rl_type: rate limit type min, max, or shared
4275  *
4276  * This function configures node element's BW rate limit profile ID of
4277  * type CIR, EIR, or SRL to default. This function needs to be called
4278  * with the scheduler lock held.
4279  */
4280 static enum ice_status
4281 ice_sched_set_node_bw_dflt_lmt(struct ice_port_info *pi,
4282                                struct ice_sched_node *node,
4283                                enum ice_rl_type rl_type)
4284 {
4285         return ice_sched_set_node_bw_lmt(pi, node, rl_type,
4286                                          ICE_SCHED_DFLT_BW);
4287 }
4288
4289 /**
4290  * ice_sched_validate_srl_node - Check node for SRL applicability
4291  * @node: sched node to configure
4292  * @sel_layer: selected SRL layer
4293  *
4294  * This function checks if the SRL can be applied to a selceted layer node on
4295  * behalf of the requested node (first argument). This function needs to be
4296  * called with scheduler lock held.
4297  */
4298 static enum ice_status
4299 ice_sched_validate_srl_node(struct ice_sched_node *node, u8 sel_layer)
4300 {
4301         /* SRL profiles are not available on all layers. Check if the
4302          * SRL profile can be applied to a node above or below the
4303          * requested node. SRL configuration is possible only if the
4304          * selected layer's node has single child.
4305          */
4306         if (sel_layer == node->tx_sched_layer ||
4307             ((sel_layer == node->tx_sched_layer + 1) &&
4308             node->num_children == 1) ||
4309             ((sel_layer == node->tx_sched_layer - 1) &&
4310             (node->parent && node->parent->num_children == 1)))
4311                 return ICE_SUCCESS;
4312
4313         return ICE_ERR_CFG;
4314 }
4315
4316 /**
4317  * ice_sched_save_q_bw - save queue node's BW information
4318  * @q_ctx: queue context structure
4319  * @rl_type: rate limit type min, max, or shared
4320  * @bw: bandwidth in Kbps - Kilo bits per sec
4321  *
4322  * Save BW information of queue type node for post replay use.
4323  */
4324 static enum ice_status
4325 ice_sched_save_q_bw(struct ice_q_ctx *q_ctx, enum ice_rl_type rl_type, u32 bw)
4326 {
4327         switch (rl_type) {
4328         case ICE_MIN_BW:
4329                 ice_set_clear_cir_bw(&q_ctx->bw_t_info, bw);
4330                 break;
4331         case ICE_MAX_BW:
4332                 ice_set_clear_eir_bw(&q_ctx->bw_t_info, bw);
4333                 break;
4334         case ICE_SHARED_BW:
4335                 ice_set_clear_shared_bw(&q_ctx->bw_t_info, bw);
4336                 break;
4337         default:
4338                 return ICE_ERR_PARAM;
4339         }
4340         return ICE_SUCCESS;
4341 }
4342
4343 /**
4344  * ice_sched_set_q_bw_lmt - sets queue BW limit
4345  * @pi: port information structure
4346  * @vsi_handle: sw VSI handle
4347  * @tc: traffic class
4348  * @q_handle: software queue handle
4349  * @rl_type: min, max, or shared
4350  * @bw: bandwidth in Kbps
4351  *
4352  * This function sets BW limit of queue scheduling node.
4353  */
4354 static enum ice_status
4355 ice_sched_set_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4356                        u16 q_handle, enum ice_rl_type rl_type, u32 bw)
4357 {
4358         enum ice_status status = ICE_ERR_PARAM;
4359         struct ice_sched_node *node;
4360         struct ice_q_ctx *q_ctx;
4361
4362         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4363                 return ICE_ERR_PARAM;
4364         ice_acquire_lock(&pi->sched_lock);
4365         q_ctx = ice_get_lan_q_ctx(pi->hw, vsi_handle, tc, q_handle);
4366         if (!q_ctx)
4367                 goto exit_q_bw_lmt;
4368         node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
4369         if (!node) {
4370                 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong q_teid\n");
4371                 goto exit_q_bw_lmt;
4372         }
4373
4374         /* Return error if it is not a leaf node */
4375         if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF)
4376                 goto exit_q_bw_lmt;
4377
4378         /* SRL bandwidth layer selection */
4379         if (rl_type == ICE_SHARED_BW) {
4380                 u8 sel_layer; /* selected layer */
4381
4382                 sel_layer = ice_sched_get_rl_prof_layer(pi, rl_type,
4383                                                         node->tx_sched_layer);
4384                 if (sel_layer >= pi->hw->num_tx_sched_layers) {
4385                         status = ICE_ERR_PARAM;
4386                         goto exit_q_bw_lmt;
4387                 }
4388                 status = ice_sched_validate_srl_node(node, sel_layer);
4389                 if (status)
4390                         goto exit_q_bw_lmt;
4391         }
4392
4393         if (bw == ICE_SCHED_DFLT_BW)
4394                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
4395         else
4396                 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
4397
4398         if (!status)
4399                 status = ice_sched_save_q_bw(q_ctx, rl_type, bw);
4400
4401 exit_q_bw_lmt:
4402         ice_release_lock(&pi->sched_lock);
4403         return status;
4404 }
4405
4406 /**
4407  * ice_cfg_q_bw_lmt - configure queue BW limit
4408  * @pi: port information structure
4409  * @vsi_handle: sw VSI handle
4410  * @tc: traffic class
4411  * @q_handle: software queue handle
4412  * @rl_type: min, max, or shared
4413  * @bw: bandwidth in Kbps
4414  *
4415  * This function configures BW limit of queue scheduling node.
4416  */
4417 enum ice_status
4418 ice_cfg_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4419                  u16 q_handle, enum ice_rl_type rl_type, u32 bw)
4420 {
4421         return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
4422                                       bw);
4423 }
4424
4425 /**
4426  * ice_cfg_q_bw_dflt_lmt - configure queue BW default limit
4427  * @pi: port information structure
4428  * @vsi_handle: sw VSI handle
4429  * @tc: traffic class
4430  * @q_handle: software queue handle
4431  * @rl_type: min, max, or shared
4432  *
4433  * This function configures BW default limit of queue scheduling node.
4434  */
4435 enum ice_status
4436 ice_cfg_q_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4437                       u16 q_handle, enum ice_rl_type rl_type)
4438 {
4439         return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
4440                                       ICE_SCHED_DFLT_BW);
4441 }
4442
4443 /**
4444  * ice_sched_save_tc_node_bw - save TC node BW limit
4445  * @pi: port information structure
4446  * @tc: TC number
4447  * @rl_type: min or max
4448  * @bw: bandwidth in Kbps
4449  *
4450  * This function saves the modified values of bandwidth settings for later
4451  * replay purpose (restore) after reset.
4452  */
4453 static enum ice_status
4454 ice_sched_save_tc_node_bw(struct ice_port_info *pi, u8 tc,
4455                           enum ice_rl_type rl_type, u32 bw)
4456 {
4457         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4458                 return ICE_ERR_PARAM;
4459         switch (rl_type) {
4460         case ICE_MIN_BW:
4461                 ice_set_clear_cir_bw(&pi->tc_node_bw_t_info[tc], bw);
4462                 break;
4463         case ICE_MAX_BW:
4464                 ice_set_clear_eir_bw(&pi->tc_node_bw_t_info[tc], bw);
4465                 break;
4466         case ICE_SHARED_BW:
4467                 ice_set_clear_shared_bw(&pi->tc_node_bw_t_info[tc], bw);
4468                 break;
4469         default:
4470                 return ICE_ERR_PARAM;
4471         }
4472         return ICE_SUCCESS;
4473 }
4474
4475 /**
4476  * ice_sched_set_tc_node_bw_lmt - sets TC node BW limit
4477  * @pi: port information structure
4478  * @tc: TC number
4479  * @rl_type: min or max
4480  * @bw: bandwidth in Kbps
4481  *
4482  * This function configures bandwidth limit of TC node.
4483  */
4484 static enum ice_status
4485 ice_sched_set_tc_node_bw_lmt(struct ice_port_info *pi, u8 tc,
4486                              enum ice_rl_type rl_type, u32 bw)
4487 {
4488         enum ice_status status = ICE_ERR_PARAM;
4489         struct ice_sched_node *tc_node;
4490
4491         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4492                 return status;
4493         ice_acquire_lock(&pi->sched_lock);
4494         tc_node = ice_sched_get_tc_node(pi, tc);
4495         if (!tc_node)
4496                 goto exit_set_tc_node_bw;
4497         if (bw == ICE_SCHED_DFLT_BW)
4498                 status = ice_sched_set_node_bw_dflt_lmt(pi, tc_node, rl_type);
4499         else
4500                 status = ice_sched_set_node_bw_lmt(pi, tc_node, rl_type, bw);
4501         if (!status)
4502                 status = ice_sched_save_tc_node_bw(pi, tc, rl_type, bw);
4503
4504 exit_set_tc_node_bw:
4505         ice_release_lock(&pi->sched_lock);
4506         return status;
4507 }
4508
4509 /**
4510  * ice_cfg_tc_node_bw_lmt - configure TC node BW limit
4511  * @pi: port information structure
4512  * @tc: TC number
4513  * @rl_type: min or max
4514  * @bw: bandwidth in Kbps
4515  *
4516  * This function configures BW limit of TC node.
4517  * Note: The minimum guaranteed reservation is done via DCBX.
4518  */
4519 enum ice_status
4520 ice_cfg_tc_node_bw_lmt(struct ice_port_info *pi, u8 tc,
4521                        enum ice_rl_type rl_type, u32 bw)
4522 {
4523         return ice_sched_set_tc_node_bw_lmt(pi, tc, rl_type, bw);
4524 }
4525
4526 /**
4527  * ice_cfg_tc_node_bw_dflt_lmt - configure TC node BW default limit
4528  * @pi: port information structure
4529  * @tc: TC number
4530  * @rl_type: min or max
4531  *
4532  * This function configures BW default limit of TC node.
4533  */
4534 enum ice_status
4535 ice_cfg_tc_node_bw_dflt_lmt(struct ice_port_info *pi, u8 tc,
4536                             enum ice_rl_type rl_type)
4537 {
4538         return ice_sched_set_tc_node_bw_lmt(pi, tc, rl_type, ICE_SCHED_DFLT_BW);
4539 }
4540
4541 /**
4542  * ice_sched_save_tc_node_bw_alloc - save TC node's BW alloc information
4543  * @pi: port information structure
4544  * @tc: traffic class
4545  * @rl_type: rate limit type min or max
4546  * @bw_alloc: Bandwidth allocation information
4547  *
4548  * Save BW alloc information of VSI type node for post replay use.
4549  */
4550 static enum ice_status
4551 ice_sched_save_tc_node_bw_alloc(struct ice_port_info *pi, u8 tc,
4552                                 enum ice_rl_type rl_type, u16 bw_alloc)
4553 {
4554         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4555                 return ICE_ERR_PARAM;
4556         switch (rl_type) {
4557         case ICE_MIN_BW:
4558                 ice_set_clear_cir_bw_alloc(&pi->tc_node_bw_t_info[tc],
4559                                            bw_alloc);
4560                 break;
4561         case ICE_MAX_BW:
4562                 ice_set_clear_eir_bw_alloc(&pi->tc_node_bw_t_info[tc],
4563                                            bw_alloc);
4564                 break;
4565         default:
4566                 return ICE_ERR_PARAM;
4567         }
4568         return ICE_SUCCESS;
4569 }
4570
4571 /**
4572  * ice_sched_set_tc_node_bw_alloc - set TC node BW alloc
4573  * @pi: port information structure
4574  * @tc: TC number
4575  * @rl_type: min or max
4576  * @bw_alloc: bandwidth alloc
4577  *
4578  * This function configures bandwidth alloc of TC node, also saves the
4579  * changed settings for replay purpose, and return success if it succeeds
4580  * in modifying bandwidth alloc setting.
4581  */
4582 static enum ice_status
4583 ice_sched_set_tc_node_bw_alloc(struct ice_port_info *pi, u8 tc,
4584                                enum ice_rl_type rl_type, u8 bw_alloc)
4585 {
4586         enum ice_status status = ICE_ERR_PARAM;
4587         struct ice_sched_node *tc_node;
4588
4589         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4590                 return status;
4591         ice_acquire_lock(&pi->sched_lock);
4592         tc_node = ice_sched_get_tc_node(pi, tc);
4593         if (!tc_node)
4594                 goto exit_set_tc_node_bw_alloc;
4595         status = ice_sched_cfg_node_bw_alloc(pi->hw, tc_node, rl_type,
4596                                              bw_alloc);
4597         if (status)
4598                 goto exit_set_tc_node_bw_alloc;
4599         status = ice_sched_save_tc_node_bw_alloc(pi, tc, rl_type, bw_alloc);
4600
4601 exit_set_tc_node_bw_alloc:
4602         ice_release_lock(&pi->sched_lock);
4603         return status;
4604 }
4605
4606 /**
4607  * ice_cfg_tc_node_bw_alloc - configure TC node BW alloc
4608  * @pi: port information structure
4609  * @tc: TC number
4610  * @rl_type: min or max
4611  * @bw_alloc: bandwidth alloc
4612  *
4613  * This function configures BW limit of TC node.
4614  * Note: The minimum guaranteed reservation is done via DCBX.
4615  */
4616 enum ice_status
4617 ice_cfg_tc_node_bw_alloc(struct ice_port_info *pi, u8 tc,
4618                          enum ice_rl_type rl_type, u8 bw_alloc)
4619 {
4620         return ice_sched_set_tc_node_bw_alloc(pi, tc, rl_type, bw_alloc);
4621 }
4622
4623 /**
4624  * ice_sched_set_agg_bw_dflt_lmt - set aggregator node's BW limit to default
4625  * @pi: port information structure
4626  * @vsi_handle: software VSI handle
4627  *
4628  * This function retrieves the aggregator ID based on VSI ID and TC,
4629  * and sets node's BW limit to default. This function needs to be
4630  * called with the scheduler lock held.
4631  */
4632 enum ice_status
4633 ice_sched_set_agg_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle)
4634 {
4635         struct ice_vsi_ctx *vsi_ctx;
4636         enum ice_status status = ICE_SUCCESS;
4637         u8 tc;
4638
4639         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4640                 return ICE_ERR_PARAM;
4641         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
4642         if (!vsi_ctx)
4643                 return ICE_ERR_PARAM;
4644
4645         ice_for_each_traffic_class(tc) {
4646                 struct ice_sched_node *node;
4647
4648                 node = vsi_ctx->sched.ag_node[tc];
4649                 if (!node)
4650                         continue;
4651
4652                 /* Set min profile to default */
4653                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, ICE_MIN_BW);
4654                 if (status)
4655                         break;
4656
4657                 /* Set max profile to default */
4658                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, ICE_MAX_BW);
4659                 if (status)
4660                         break;
4661
4662                 /* Remove shared profile, if there is one */
4663                 status = ice_sched_set_node_bw_dflt_lmt(pi, node,
4664                                                         ICE_SHARED_BW);
4665                 if (status)
4666                         break;
4667         }
4668
4669         return status;
4670 }
4671
4672 /**
4673  * ice_sched_get_node_by_id_type - get node from ID type
4674  * @pi: port information structure
4675  * @id: identifier
4676  * @agg_type: type of aggregator
4677  * @tc: traffic class
4678  *
4679  * This function returns node identified by ID of type aggregator, and
4680  * based on traffic class (TC). This function needs to be called with
4681  * the scheduler lock held.
4682  */
4683 static struct ice_sched_node *
4684 ice_sched_get_node_by_id_type(struct ice_port_info *pi, u32 id,
4685                               enum ice_agg_type agg_type, u8 tc)
4686 {
4687         struct ice_sched_node *node = NULL;
4688         struct ice_sched_node *child_node;
4689
4690         switch (agg_type) {
4691         case ICE_AGG_TYPE_VSI: {
4692                 struct ice_vsi_ctx *vsi_ctx;
4693                 u16 vsi_handle = (u16)id;
4694
4695                 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4696                         break;
4697                 /* Get sched_vsi_info */
4698                 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
4699                 if (!vsi_ctx)
4700                         break;
4701                 node = vsi_ctx->sched.vsi_node[tc];
4702                 break;
4703         }
4704
4705         case ICE_AGG_TYPE_AGG: {
4706                 struct ice_sched_node *tc_node;
4707
4708                 tc_node = ice_sched_get_tc_node(pi, tc);
4709                 if (tc_node)
4710                         node = ice_sched_get_agg_node(pi, tc_node, id);
4711                 break;
4712         }
4713
4714         case ICE_AGG_TYPE_Q:
4715                 /* The current implementation allows single queue to modify */
4716                 node = ice_sched_get_node(pi, id);
4717                 break;
4718
4719         case ICE_AGG_TYPE_QG:
4720                 /* The current implementation allows single qg to modify */
4721                 child_node = ice_sched_get_node(pi, id);
4722                 if (!child_node)
4723                         break;
4724                 node = child_node->parent;
4725                 break;
4726
4727         default:
4728                 break;
4729         }
4730
4731         return node;
4732 }
4733
4734 /**
4735  * ice_sched_set_node_bw_lmt_per_tc - set node BW limit per TC
4736  * @pi: port information structure
4737  * @id: ID (software VSI handle or AGG ID)
4738  * @agg_type: aggregator type (VSI or AGG type node)
4739  * @tc: traffic class
4740  * @rl_type: min or max
4741  * @bw: bandwidth in Kbps
4742  *
4743  * This function sets BW limit of VSI or Aggregator scheduling node
4744  * based on TC information from passed in argument BW.
4745  */
4746 enum ice_status
4747 ice_sched_set_node_bw_lmt_per_tc(struct ice_port_info *pi, u32 id,
4748                                  enum ice_agg_type agg_type, u8 tc,
4749                                  enum ice_rl_type rl_type, u32 bw)
4750 {
4751         enum ice_status status = ICE_ERR_PARAM;
4752         struct ice_sched_node *node;
4753
4754         if (!pi)
4755                 return status;
4756
4757         if (rl_type == ICE_UNKNOWN_BW)
4758                 return status;
4759
4760         ice_acquire_lock(&pi->sched_lock);
4761         node = ice_sched_get_node_by_id_type(pi, id, agg_type, tc);
4762         if (!node) {
4763                 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong id, agg type, or tc\n");
4764                 goto exit_set_node_bw_lmt_per_tc;
4765         }
4766         if (bw == ICE_SCHED_DFLT_BW)
4767                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
4768         else
4769                 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
4770
4771 exit_set_node_bw_lmt_per_tc:
4772         ice_release_lock(&pi->sched_lock);
4773         return status;
4774 }
4775
4776 /**
4777  * ice_sched_validate_vsi_srl_node - validate VSI SRL node
4778  * @pi: port information structure
4779  * @vsi_handle: software VSI handle
4780  *
4781  * This function validates SRL node of the VSI node if available SRL layer is
4782  * different than the VSI node layer on all TC(s).This function needs to be
4783  * called with scheduler lock held.
4784  */
4785 static enum ice_status
4786 ice_sched_validate_vsi_srl_node(struct ice_port_info *pi, u16 vsi_handle)
4787 {
4788         u8 sel_layer = ICE_SCHED_INVAL_LAYER_NUM;
4789         u8 tc;
4790
4791         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4792                 return ICE_ERR_PARAM;
4793
4794         /* Return success if no nodes are present across TC */
4795         ice_for_each_traffic_class(tc) {
4796                 struct ice_sched_node *tc_node, *vsi_node;
4797                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4798                 enum ice_status status;
4799
4800                 tc_node = ice_sched_get_tc_node(pi, tc);
4801                 if (!tc_node)
4802                         continue;
4803
4804                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
4805                 if (!vsi_node)
4806                         continue;
4807
4808                 /* SRL bandwidth layer selection */
4809                 if (sel_layer == ICE_SCHED_INVAL_LAYER_NUM) {
4810                         u8 node_layer = vsi_node->tx_sched_layer;
4811                         u8 layer_num;
4812
4813                         layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
4814                                                                 node_layer);
4815                         if (layer_num >= pi->hw->num_tx_sched_layers)
4816                                 return ICE_ERR_PARAM;
4817                         sel_layer = layer_num;
4818                 }
4819
4820                 status = ice_sched_validate_srl_node(vsi_node, sel_layer);
4821                 if (status)
4822                         return status;
4823         }
4824         return ICE_SUCCESS;
4825 }
4826
4827 /**
4828  * ice_sched_set_vsi_bw_shared_lmt - set VSI BW shared limit
4829  * @pi: port information structure
4830  * @vsi_handle: software VSI handle
4831  * @bw: bandwidth in Kbps
4832  *
4833  * This function Configures shared rate limiter(SRL) of all VSI type nodes
4834  * across all traffic classes for VSI matching handle. When BW value of
4835  * ICE_SCHED_DFLT_BW is passed, it removes the SRL from the node.
4836  */
4837 enum ice_status
4838 ice_sched_set_vsi_bw_shared_lmt(struct ice_port_info *pi, u16 vsi_handle,
4839                                 u32 bw)
4840 {
4841         enum ice_status status = ICE_SUCCESS;
4842         u8 tc;
4843
4844         if (!pi)
4845                 return ICE_ERR_PARAM;
4846
4847         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4848                 return ICE_ERR_PARAM;
4849
4850         ice_acquire_lock(&pi->sched_lock);
4851         status = ice_sched_validate_vsi_srl_node(pi, vsi_handle);
4852         if (status)
4853                 goto exit_set_vsi_bw_shared_lmt;
4854         /* Return success if no nodes are present across TC */
4855         ice_for_each_traffic_class(tc) {
4856                 struct ice_sched_node *tc_node, *vsi_node;
4857                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4858
4859                 tc_node = ice_sched_get_tc_node(pi, tc);
4860                 if (!tc_node)
4861                         continue;
4862
4863                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
4864                 if (!vsi_node)
4865                         continue;
4866
4867                 if (bw == ICE_SCHED_DFLT_BW)
4868                         /* It removes existing SRL from the node */
4869                         status = ice_sched_set_node_bw_dflt_lmt(pi, vsi_node,
4870                                                                 rl_type);
4871                 else
4872                         status = ice_sched_set_node_bw_lmt(pi, vsi_node,
4873                                                            rl_type, bw);
4874                 if (status)
4875                         break;
4876                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type, bw);
4877                 if (status)
4878                         break;
4879         }
4880
4881 exit_set_vsi_bw_shared_lmt:
4882         ice_release_lock(&pi->sched_lock);
4883         return status;
4884 }
4885
4886 /**
4887  * ice_sched_validate_agg_srl_node - validate AGG SRL node
4888  * @pi: port information structure
4889  * @agg_id: aggregator ID
4890  *
4891  * This function validates SRL node of the AGG node if available SRL layer is
4892  * different than the AGG node layer on all TC(s).This function needs to be
4893  * called with scheduler lock held.
4894  */
4895 static enum ice_status
4896 ice_sched_validate_agg_srl_node(struct ice_port_info *pi, u32 agg_id)
4897 {
4898         u8 sel_layer = ICE_SCHED_INVAL_LAYER_NUM;
4899         struct ice_sched_agg_info *agg_info;
4900         bool agg_id_present = false;
4901         enum ice_status status = ICE_SUCCESS;
4902         u8 tc;
4903
4904         LIST_FOR_EACH_ENTRY(agg_info, &pi->hw->agg_list, ice_sched_agg_info,
4905                             list_entry)
4906                 if (agg_info->agg_id == agg_id) {
4907                         agg_id_present = true;
4908                         break;
4909                 }
4910         if (!agg_id_present)
4911                 return ICE_ERR_PARAM;
4912         /* Return success if no nodes are present across TC */
4913         ice_for_each_traffic_class(tc) {
4914                 struct ice_sched_node *tc_node, *agg_node;
4915                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4916
4917                 tc_node = ice_sched_get_tc_node(pi, tc);
4918                 if (!tc_node)
4919                         continue;
4920
4921                 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
4922                 if (!agg_node)
4923                         continue;
4924                 /* SRL bandwidth layer selection */
4925                 if (sel_layer == ICE_SCHED_INVAL_LAYER_NUM) {
4926                         u8 node_layer = agg_node->tx_sched_layer;
4927                         u8 layer_num;
4928
4929                         layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
4930                                                                 node_layer);
4931                         if (layer_num >= pi->hw->num_tx_sched_layers)
4932                                 return ICE_ERR_PARAM;
4933                         sel_layer = layer_num;
4934                 }
4935
4936                 status = ice_sched_validate_srl_node(agg_node, sel_layer);
4937                 if (status)
4938                         break;
4939         }
4940         return status;
4941 }
4942
4943 /**
4944  * ice_sched_set_agg_bw_shared_lmt - set aggregator BW shared limit
4945  * @pi: port information structure
4946  * @agg_id: aggregator ID
4947  * @bw: bandwidth in Kbps
4948  *
4949  * This function configures the shared rate limiter(SRL) of all aggregator type
4950  * nodes across all traffic classes for aggregator matching agg_id. When
4951  * BW value of ICE_SCHED_DFLT_BW is passed, it removes SRL from the
4952  * node(s).
4953  */
4954 enum ice_status
4955 ice_sched_set_agg_bw_shared_lmt(struct ice_port_info *pi, u32 agg_id, u32 bw)
4956 {
4957         struct ice_sched_agg_info *agg_info;
4958         struct ice_sched_agg_info *tmp;
4959         bool agg_id_present = false;
4960         enum ice_status status = ICE_SUCCESS;
4961         u8 tc;
4962
4963         if (!pi)
4964                 return ICE_ERR_PARAM;
4965
4966         ice_acquire_lock(&pi->sched_lock);
4967         status = ice_sched_validate_agg_srl_node(pi, agg_id);
4968         if (status)
4969                 goto exit_agg_bw_shared_lmt;
4970
4971         LIST_FOR_EACH_ENTRY_SAFE(agg_info, tmp, &pi->hw->agg_list,
4972                                  ice_sched_agg_info, list_entry)
4973                 if (agg_info->agg_id == agg_id) {
4974                         agg_id_present = true;
4975                         break;
4976                 }
4977
4978         if (!agg_id_present) {
4979                 status = ICE_ERR_PARAM;
4980                 goto exit_agg_bw_shared_lmt;
4981         }
4982
4983         /* Return success if no nodes are present across TC */
4984         ice_for_each_traffic_class(tc) {
4985                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4986                 struct ice_sched_node *tc_node, *agg_node;
4987
4988                 tc_node = ice_sched_get_tc_node(pi, tc);
4989                 if (!tc_node)
4990                         continue;
4991
4992                 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
4993                 if (!agg_node)
4994                         continue;
4995
4996                 if (bw == ICE_SCHED_DFLT_BW)
4997                         /* It removes existing SRL from the node */
4998                         status = ice_sched_set_node_bw_dflt_lmt(pi, agg_node,
4999                                                                 rl_type);
5000                 else
5001                         status = ice_sched_set_node_bw_lmt(pi, agg_node,
5002                                                            rl_type, bw);
5003                 if (status)
5004                         break;
5005                 status = ice_sched_save_agg_bw(pi, agg_id, tc, rl_type, bw);
5006                 if (status)
5007                         break;
5008         }
5009
5010 exit_agg_bw_shared_lmt:
5011         ice_release_lock(&pi->sched_lock);
5012         return status;
5013 }
5014
5015 /**
5016  * ice_sched_cfg_sibl_node_prio - configure node sibling priority
5017  * @pi: port information structure
5018  * @node: sched node to configure
5019  * @priority: sibling priority
5020  *
5021  * This function configures node element's sibling priority only. This
5022  * function needs to be called with scheduler lock held.
5023  */
5024 enum ice_status
5025 ice_sched_cfg_sibl_node_prio(struct ice_port_info *pi,
5026                              struct ice_sched_node *node, u8 priority)
5027 {
5028         struct ice_aqc_txsched_elem_data buf;
5029         struct ice_aqc_txsched_elem *data;
5030         struct ice_hw *hw = pi->hw;
5031         enum ice_status status;
5032
5033         if (!hw)
5034                 return ICE_ERR_PARAM;
5035         buf = node->info;
5036         data = &buf.data;
5037         data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
5038         priority = (priority << ICE_AQC_ELEM_GENERIC_PRIO_S) &
5039                    ICE_AQC_ELEM_GENERIC_PRIO_M;
5040         data->generic &= ~ICE_AQC_ELEM_GENERIC_PRIO_M;
5041         data->generic |= priority;
5042
5043         /* Configure element */
5044         status = ice_sched_update_elem(hw, node, &buf);
5045         return status;
5046 }
5047
5048 /**
5049  * ice_cfg_rl_burst_size - Set burst size value
5050  * @hw: pointer to the HW struct
5051  * @bytes: burst size in bytes
5052  *
5053  * This function configures/set the burst size to requested new value. The new
5054  * burst size value is used for future rate limit calls. It doesn't change the
5055  * existing or previously created RL profiles.
5056  */
5057 enum ice_status ice_cfg_rl_burst_size(struct ice_hw *hw, u32 bytes)
5058 {
5059         u16 burst_size_to_prog;
5060
5061         if (bytes < ICE_MIN_BURST_SIZE_ALLOWED ||
5062             bytes > ICE_MAX_BURST_SIZE_ALLOWED)
5063                 return ICE_ERR_PARAM;
5064         if (ice_round_to_num(bytes, 64) <=
5065             ICE_MAX_BURST_SIZE_64_BYTE_GRANULARITY) {
5066                 /* 64 byte granularity case */
5067                 /* Disable MSB granularity bit */
5068                 burst_size_to_prog = ICE_64_BYTE_GRANULARITY;
5069                 /* round number to nearest 64 byte granularity */
5070                 bytes = ice_round_to_num(bytes, 64);
5071                 /* The value is in 64 byte chunks */
5072                 burst_size_to_prog |= (u16)(bytes / 64);
5073         } else {
5074                 /* k bytes granularity case */
5075                 /* Enable MSB granularity bit */
5076                 burst_size_to_prog = ICE_KBYTE_GRANULARITY;
5077                 /* round number to nearest 1024 granularity */
5078                 bytes = ice_round_to_num(bytes, 1024);
5079                 /* check rounding doesn't go beyond allowed */
5080                 if (bytes > ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY)
5081                         bytes = ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY;
5082                 /* The value is in k bytes */
5083                 burst_size_to_prog |= (u16)(bytes / 1024);
5084         }
5085         hw->max_burst_size = burst_size_to_prog;
5086         return ICE_SUCCESS;
5087 }
5088
5089 /*
5090  * ice_sched_replay_node_prio - re-configure node priority
5091  * @hw: pointer to the HW struct
5092  * @node: sched node to configure
5093  * @priority: priority value
5094  *
5095  * This function configures node element's priority value. It
5096  * needs to be called with scheduler lock held.
5097  */
5098 static enum ice_status
5099 ice_sched_replay_node_prio(struct ice_hw *hw, struct ice_sched_node *node,
5100                            u8 priority)
5101 {
5102         struct ice_aqc_txsched_elem_data buf;
5103         struct ice_aqc_txsched_elem *data;
5104         enum ice_status status;
5105
5106         buf = node->info;
5107         data = &buf.data;
5108         data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
5109         data->generic = priority;
5110
5111         /* Configure element */
5112         status = ice_sched_update_elem(hw, node, &buf);
5113         return status;
5114 }
5115
5116 /**
5117  * ice_sched_replay_node_bw - replay node(s) BW
5118  * @hw: pointer to the HW struct
5119  * @node: sched node to configure
5120  * @bw_t_info: BW type information
5121  *
5122  * This function restores node's BW from bw_t_info. The caller needs
5123  * to hold the scheduler lock.
5124  */
5125 static enum ice_status
5126 ice_sched_replay_node_bw(struct ice_hw *hw, struct ice_sched_node *node,
5127                          struct ice_bw_type_info *bw_t_info)
5128 {
5129         struct ice_port_info *pi = hw->port_info;
5130         enum ice_status status = ICE_ERR_PARAM;
5131         u16 bw_alloc;
5132
5133         if (!node)
5134                 return status;
5135         if (!ice_is_any_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CNT))
5136                 return ICE_SUCCESS;
5137         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_PRIO)) {
5138                 status = ice_sched_replay_node_prio(hw, node,
5139                                                     bw_t_info->generic);
5140                 if (status)
5141                         return status;
5142         }
5143         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CIR)) {
5144                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW,
5145                                                    bw_t_info->cir_bw.bw);
5146                 if (status)
5147                         return status;
5148         }
5149         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CIR_WT)) {
5150                 bw_alloc = bw_t_info->cir_bw.bw_alloc;
5151                 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MIN_BW,
5152                                                      bw_alloc);
5153                 if (status)
5154                         return status;
5155         }
5156         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_EIR)) {
5157                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW,
5158                                                    bw_t_info->eir_bw.bw);
5159                 if (status)
5160                         return status;
5161         }
5162         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_EIR_WT)) {
5163                 bw_alloc = bw_t_info->eir_bw.bw_alloc;
5164                 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MAX_BW,
5165                                                      bw_alloc);
5166                 if (status)
5167                         return status;
5168         }
5169         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_SHARED))
5170                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_SHARED_BW,
5171                                                    bw_t_info->shared_bw);
5172         return status;
5173 }
5174
5175 /**
5176  * ice_sched_replay_agg_bw - replay aggregator node(s) BW
5177  * @hw: pointer to the HW struct
5178  * @agg_info: aggregator data structure
5179  *
5180  * This function re-creates aggregator type nodes. The caller needs to hold
5181  * the scheduler lock.
5182  */
5183 static enum ice_status
5184 ice_sched_replay_agg_bw(struct ice_hw *hw, struct ice_sched_agg_info *agg_info)
5185 {
5186         struct ice_sched_node *tc_node, *agg_node;
5187         enum ice_status status = ICE_SUCCESS;
5188         u8 tc;
5189
5190         if (!agg_info)
5191                 return ICE_ERR_PARAM;
5192         ice_for_each_traffic_class(tc) {
5193                 if (!ice_is_any_bit_set(agg_info->bw_t_info[tc].bw_t_bitmap,
5194                                         ICE_BW_TYPE_CNT))
5195                         continue;
5196                 tc_node = ice_sched_get_tc_node(hw->port_info, tc);
5197                 if (!tc_node) {
5198                         status = ICE_ERR_PARAM;
5199                         break;
5200                 }
5201                 agg_node = ice_sched_get_agg_node(hw->port_info, tc_node,
5202                                                   agg_info->agg_id);
5203                 if (!agg_node) {
5204                         status = ICE_ERR_PARAM;
5205                         break;
5206                 }
5207                 status = ice_sched_replay_node_bw(hw, agg_node,
5208                                                   &agg_info->bw_t_info[tc]);
5209                 if (status)
5210                         break;
5211         }
5212         return status;
5213 }
5214
5215 /**
5216  * ice_sched_get_ena_tc_bitmap - get enabled TC bitmap
5217  * @pi: port info struct
5218  * @tc_bitmap: 8 bits TC bitmap to check
5219  * @ena_tc_bitmap: 8 bits enabled TC bitmap to return
5220  *
5221  * This function returns enabled TC bitmap in variable ena_tc_bitmap. Some TCs
5222  * may be missing, it returns enabled TCs. This function needs to be called with
5223  * scheduler lock held.
5224  */
5225 static void
5226 ice_sched_get_ena_tc_bitmap(struct ice_port_info *pi, ice_bitmap_t *tc_bitmap,
5227                             ice_bitmap_t *ena_tc_bitmap)
5228 {
5229         u8 tc;
5230
5231         /* Some TC(s) may be missing after reset, adjust for replay */
5232         ice_for_each_traffic_class(tc)
5233                 if (ice_is_tc_ena(*tc_bitmap, tc) &&
5234                     (ice_sched_get_tc_node(pi, tc)))
5235                         ice_set_bit(tc, ena_tc_bitmap);
5236 }
5237
5238 /**
5239  * ice_sched_replay_agg - recreate aggregator node(s)
5240  * @hw: pointer to the HW struct
5241  *
5242  * This function recreate aggregator type nodes which are not replayed earlier.
5243  * It also replay aggregator BW information. These aggregator nodes are not
5244  * associated with VSI type node yet.
5245  */
5246 void ice_sched_replay_agg(struct ice_hw *hw)
5247 {
5248         struct ice_port_info *pi = hw->port_info;
5249         struct ice_sched_agg_info *agg_info;
5250
5251         ice_acquire_lock(&pi->sched_lock);
5252         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
5253                             list_entry) {
5254                 /* replay aggregator (re-create aggregator node) */
5255                 if (!ice_cmp_bitmap(agg_info->tc_bitmap,
5256                                     agg_info->replay_tc_bitmap,
5257                                     ICE_MAX_TRAFFIC_CLASS)) {
5258                         ice_declare_bitmap(replay_bitmap,
5259                                            ICE_MAX_TRAFFIC_CLASS);
5260                         enum ice_status status;
5261
5262                         ice_zero_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5263                         ice_sched_get_ena_tc_bitmap(pi,
5264                                                     agg_info->replay_tc_bitmap,
5265                                                     replay_bitmap);
5266                         status = ice_sched_cfg_agg(hw->port_info,
5267                                                    agg_info->agg_id,
5268                                                    ICE_AGG_TYPE_AGG,
5269                                                    replay_bitmap);
5270                         if (status) {
5271                                 ice_info(hw, "Replay agg id[%d] failed\n",
5272                                          agg_info->agg_id);
5273                                 /* Move on to next one */
5274                                 continue;
5275                         }
5276                         /* Replay aggregator node BW (restore aggregator BW) */
5277                         status = ice_sched_replay_agg_bw(hw, agg_info);
5278                         if (status)
5279                                 ice_info(hw, "Replay agg bw [id=%d] failed\n",
5280                                          agg_info->agg_id);
5281                 }
5282         }
5283         ice_release_lock(&pi->sched_lock);
5284 }
5285
5286 /**
5287  * ice_sched_replay_agg_vsi_preinit - Agg/VSI replay pre initialization
5288  * @hw: pointer to the HW struct
5289  *
5290  * This function initialize aggregator(s) TC bitmap to zero. A required
5291  * preinit step for replaying aggregators.
5292  */
5293 void ice_sched_replay_agg_vsi_preinit(struct ice_hw *hw)
5294 {
5295         struct ice_port_info *pi = hw->port_info;
5296         struct ice_sched_agg_info *agg_info;
5297
5298         ice_acquire_lock(&pi->sched_lock);
5299         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
5300                             list_entry) {
5301                 struct ice_sched_agg_vsi_info *agg_vsi_info;
5302
5303                 agg_info->tc_bitmap[0] = 0;
5304                 LIST_FOR_EACH_ENTRY(agg_vsi_info, &agg_info->agg_vsi_list,
5305                                     ice_sched_agg_vsi_info, list_entry)
5306                         agg_vsi_info->tc_bitmap[0] = 0;
5307         }
5308         ice_release_lock(&pi->sched_lock);
5309 }
5310
5311 /**
5312  * ice_sched_replay_tc_node_bw - replay TC node(s) BW
5313  * @pi: port information structure
5314  *
5315  * This function replay TC nodes.
5316  */
5317 enum ice_status
5318 ice_sched_replay_tc_node_bw(struct ice_port_info *pi)
5319 {
5320         enum ice_status status = ICE_SUCCESS;
5321         u8 tc;
5322
5323         if (!pi->hw)
5324                 return ICE_ERR_PARAM;
5325         ice_acquire_lock(&pi->sched_lock);
5326         ice_for_each_traffic_class(tc) {
5327                 struct ice_sched_node *tc_node;
5328
5329                 tc_node = ice_sched_get_tc_node(pi, tc);
5330                 if (!tc_node)
5331                         continue; /* TC not present */
5332                 status = ice_sched_replay_node_bw(pi->hw, tc_node,
5333                                                   &pi->tc_node_bw_t_info[tc]);
5334                 if (status)
5335                         break;
5336         }
5337         ice_release_lock(&pi->sched_lock);
5338         return status;
5339 }
5340
5341 /**
5342  * ice_sched_replay_vsi_bw - replay VSI type node(s) BW
5343  * @hw: pointer to the HW struct
5344  * @vsi_handle: software VSI handle
5345  * @tc_bitmap: 8 bits TC bitmap
5346  *
5347  * This function replays VSI type nodes bandwidth. This function needs to be
5348  * called with scheduler lock held.
5349  */
5350 static enum ice_status
5351 ice_sched_replay_vsi_bw(struct ice_hw *hw, u16 vsi_handle,
5352                         ice_bitmap_t *tc_bitmap)
5353 {
5354         struct ice_sched_node *vsi_node, *tc_node;
5355         struct ice_port_info *pi = hw->port_info;
5356         struct ice_bw_type_info *bw_t_info;
5357         struct ice_vsi_ctx *vsi_ctx;
5358         enum ice_status status = ICE_SUCCESS;
5359         u8 tc;
5360
5361         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
5362         if (!vsi_ctx)
5363                 return ICE_ERR_PARAM;
5364         ice_for_each_traffic_class(tc) {
5365                 if (!ice_is_tc_ena(*tc_bitmap, tc))
5366                         continue;
5367                 tc_node = ice_sched_get_tc_node(pi, tc);
5368                 if (!tc_node)
5369                         continue;
5370                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
5371                 if (!vsi_node)
5372                         continue;
5373                 bw_t_info = &vsi_ctx->sched.bw_t_info[tc];
5374                 status = ice_sched_replay_node_bw(hw, vsi_node, bw_t_info);
5375                 if (status)
5376                         break;
5377         }
5378         return status;
5379 }
5380
5381 /**
5382  * ice_sched_replay_vsi_agg - replay aggregator & VSI to aggregator node(s)
5383  * @hw: pointer to the HW struct
5384  * @vsi_handle: software VSI handle
5385  *
5386  * This function replays aggregator node, VSI to aggregator type nodes, and
5387  * their node bandwidth information. This function needs to be called with
5388  * scheduler lock held.
5389  */
5390 static enum ice_status
5391 ice_sched_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
5392 {
5393         ice_declare_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5394         struct ice_sched_agg_vsi_info *agg_vsi_info;
5395         struct ice_port_info *pi = hw->port_info;
5396         struct ice_sched_agg_info *agg_info;
5397         enum ice_status status;
5398
5399         ice_zero_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5400         if (!ice_is_vsi_valid(hw, vsi_handle))
5401                 return ICE_ERR_PARAM;
5402         agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
5403         if (!agg_info)
5404                 return ICE_SUCCESS; /* Not present in list - default Agg case */
5405         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
5406         if (!agg_vsi_info)
5407                 return ICE_SUCCESS; /* Not present in list - default Agg case */
5408         ice_sched_get_ena_tc_bitmap(pi, agg_info->replay_tc_bitmap,
5409                                     replay_bitmap);
5410         /* Replay aggregator node associated to vsi_handle */
5411         status = ice_sched_cfg_agg(hw->port_info, agg_info->agg_id,
5412                                    ICE_AGG_TYPE_AGG, replay_bitmap);
5413         if (status)
5414                 return status;
5415         /* Replay aggregator node BW (restore aggregator BW) */
5416         status = ice_sched_replay_agg_bw(hw, agg_info);
5417         if (status)
5418                 return status;
5419
5420         ice_zero_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5421         ice_sched_get_ena_tc_bitmap(pi, agg_vsi_info->replay_tc_bitmap,
5422                                     replay_bitmap);
5423         /* Move this VSI (vsi_handle) to above aggregator */
5424         status = ice_sched_assoc_vsi_to_agg(pi, agg_info->agg_id, vsi_handle,
5425                                             replay_bitmap);
5426         if (status)
5427                 return status;
5428         /* Replay VSI BW (restore VSI BW) */
5429         return ice_sched_replay_vsi_bw(hw, vsi_handle,
5430                                        agg_vsi_info->tc_bitmap);
5431 }
5432
5433 /**
5434  * ice_replay_vsi_agg - replay VSI to aggregator node
5435  * @hw: pointer to the HW struct
5436  * @vsi_handle: software VSI handle
5437  *
5438  * This function replays association of VSI to aggregator type nodes, and
5439  * node bandwidth information.
5440  */
5441 enum ice_status
5442 ice_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
5443 {
5444         struct ice_port_info *pi = hw->port_info;
5445         enum ice_status status;
5446
5447         ice_acquire_lock(&pi->sched_lock);
5448         status = ice_sched_replay_vsi_agg(hw, vsi_handle);
5449         ice_release_lock(&pi->sched_lock);
5450         return status;
5451 }
5452
5453 /**
5454  * ice_sched_replay_q_bw - replay queue type node BW
5455  * @pi: port information structure
5456  * @q_ctx: queue context structure
5457  *
5458  * This function replays queue type node bandwidth. This function needs to be
5459  * called with scheduler lock held.
5460  */
5461 enum ice_status
5462 ice_sched_replay_q_bw(struct ice_port_info *pi, struct ice_q_ctx *q_ctx)
5463 {
5464         struct ice_sched_node *q_node;
5465
5466         /* Following also checks the presence of node in tree */
5467         q_node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
5468         if (!q_node)
5469                 return ICE_ERR_PARAM;
5470         return ice_sched_replay_node_bw(pi->hw, q_node, &q_ctx->bw_t_info);
5471 }