net/ice/base: check root pointer for validity
[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         u8 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         struct ice_aqc_move_elem *buf;
2185         struct ice_sched_node *node;
2186         enum ice_status status = ICE_SUCCESS;
2187         struct ice_hw *hw;
2188         u16 grps_movd = 0;
2189         u8 i;
2190
2191         hw = pi->hw;
2192
2193         if (!parent || !num_items)
2194                 return ICE_ERR_PARAM;
2195
2196         /* Does parent have enough space */
2197         if (parent->num_children + num_items >=
2198             hw->max_children[parent->tx_sched_layer])
2199                 return ICE_ERR_AQ_FULL;
2200
2201         buf = (struct ice_aqc_move_elem *)ice_malloc(hw, sizeof(*buf));
2202         if (!buf)
2203                 return ICE_ERR_NO_MEMORY;
2204
2205         for (i = 0; i < num_items; i++) {
2206                 node = ice_sched_find_node_by_teid(pi->root, list[i]);
2207                 if (!node) {
2208                         status = ICE_ERR_PARAM;
2209                         goto move_err_exit;
2210                 }
2211
2212                 buf->hdr.src_parent_teid = node->info.parent_teid;
2213                 buf->hdr.dest_parent_teid = parent->info.node_teid;
2214                 buf->teid[0] = node->info.node_teid;
2215                 buf->hdr.num_elems = CPU_TO_LE16(1);
2216                 status = ice_aq_move_sched_elems(hw, 1, buf, sizeof(*buf),
2217                                                  &grps_movd, NULL);
2218                 if (status && grps_movd != 1) {
2219                         status = ICE_ERR_CFG;
2220                         goto move_err_exit;
2221                 }
2222
2223                 /* update the SW DB */
2224                 ice_sched_update_parent(parent, node);
2225         }
2226
2227 move_err_exit:
2228         ice_free(hw, buf);
2229         return status;
2230 }
2231
2232 /**
2233  * ice_sched_move_vsi_to_agg - move VSI to aggregator node
2234  * @pi: port information structure
2235  * @vsi_handle: software VSI handle
2236  * @agg_id: aggregator ID
2237  * @tc: TC number
2238  *
2239  * This function moves a VSI to an aggregator node or its subtree.
2240  * Intermediate nodes may be created if required.
2241  */
2242 static enum ice_status
2243 ice_sched_move_vsi_to_agg(struct ice_port_info *pi, u16 vsi_handle, u32 agg_id,
2244                           u8 tc)
2245 {
2246         struct ice_sched_node *vsi_node, *agg_node, *tc_node, *parent;
2247         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2248         u32 first_node_teid, vsi_teid;
2249         enum ice_status status;
2250         u16 num_nodes_added;
2251         u8 aggl, vsil, i;
2252
2253         tc_node = ice_sched_get_tc_node(pi, tc);
2254         if (!tc_node)
2255                 return ICE_ERR_CFG;
2256
2257         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2258         if (!agg_node)
2259                 return ICE_ERR_DOES_NOT_EXIST;
2260
2261         vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2262         if (!vsi_node)
2263                 return ICE_ERR_DOES_NOT_EXIST;
2264
2265         aggl = ice_sched_get_agg_layer(pi->hw);
2266         vsil = ice_sched_get_vsi_layer(pi->hw);
2267
2268         /* set intermediate node count to 1 between aggregator and VSI layers */
2269         for (i = aggl + 1; i < vsil; i++)
2270                 num_nodes[i] = 1;
2271
2272         /* Check if the aggregator subtree has any free node to add the VSI */
2273         for (i = 0; i < agg_node->num_children; i++) {
2274                 parent = ice_sched_get_free_vsi_parent(pi->hw,
2275                                                        agg_node->children[i],
2276                                                        num_nodes);
2277                 if (parent)
2278                         goto move_nodes;
2279         }
2280
2281         /* add new nodes */
2282         parent = agg_node;
2283         for (i = aggl + 1; i < vsil; i++) {
2284                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2285                                                       num_nodes[i],
2286                                                       &first_node_teid,
2287                                                       &num_nodes_added);
2288                 if (status != ICE_SUCCESS || num_nodes[i] != num_nodes_added)
2289                         return ICE_ERR_CFG;
2290
2291                 /* The newly added node can be a new parent for the next
2292                  * layer nodes
2293                  */
2294                 if (num_nodes_added)
2295                         parent = ice_sched_find_node_by_teid(tc_node,
2296                                                              first_node_teid);
2297                 else
2298                         parent = parent->children[0];
2299
2300                 if (!parent)
2301                         return ICE_ERR_CFG;
2302         }
2303
2304 move_nodes:
2305         vsi_teid = LE32_TO_CPU(vsi_node->info.node_teid);
2306         return ice_sched_move_nodes(pi, parent, 1, &vsi_teid);
2307 }
2308
2309 /**
2310  * ice_move_all_vsi_to_dflt_agg - move all VSI(s) to default aggregator
2311  * @pi: port information structure
2312  * @agg_info: aggregator info
2313  * @tc: traffic class number
2314  * @rm_vsi_info: true or false
2315  *
2316  * This function move all the VSI(s) to the default aggregator and delete
2317  * aggregator VSI info based on passed in boolean parameter rm_vsi_info. The
2318  * caller holds the scheduler lock.
2319  */
2320 static enum ice_status
2321 ice_move_all_vsi_to_dflt_agg(struct ice_port_info *pi,
2322                              struct ice_sched_agg_info *agg_info, u8 tc,
2323                              bool rm_vsi_info)
2324 {
2325         struct ice_sched_agg_vsi_info *agg_vsi_info;
2326         struct ice_sched_agg_vsi_info *tmp;
2327         enum ice_status status = ICE_SUCCESS;
2328
2329         LIST_FOR_EACH_ENTRY_SAFE(agg_vsi_info, tmp, &agg_info->agg_vsi_list,
2330                                  ice_sched_agg_vsi_info, list_entry) {
2331                 u16 vsi_handle = agg_vsi_info->vsi_handle;
2332
2333                 /* Move VSI to default aggregator */
2334                 if (!ice_is_tc_ena(agg_vsi_info->tc_bitmap[0], tc))
2335                         continue;
2336
2337                 status = ice_sched_move_vsi_to_agg(pi, vsi_handle,
2338                                                    ICE_DFLT_AGG_ID, tc);
2339                 if (status)
2340                         break;
2341
2342                 ice_clear_bit(tc, agg_vsi_info->tc_bitmap);
2343                 if (rm_vsi_info && !agg_vsi_info->tc_bitmap[0]) {
2344                         LIST_DEL(&agg_vsi_info->list_entry);
2345                         ice_free(pi->hw, agg_vsi_info);
2346                 }
2347         }
2348
2349         return status;
2350 }
2351
2352 /**
2353  * ice_sched_is_agg_inuse - check whether the aggregator is in use or not
2354  * @pi: port information structure
2355  * @node: node pointer
2356  *
2357  * This function checks whether the aggregator is attached with any VSI or not.
2358  */
2359 static bool
2360 ice_sched_is_agg_inuse(struct ice_port_info *pi, struct ice_sched_node *node)
2361 {
2362         u8 vsil, i;
2363
2364         vsil = ice_sched_get_vsi_layer(pi->hw);
2365         if (node->tx_sched_layer < vsil - 1) {
2366                 for (i = 0; i < node->num_children; i++)
2367                         if (ice_sched_is_agg_inuse(pi, node->children[i]))
2368                                 return true;
2369                 return false;
2370         } else {
2371                 return node->num_children ? true : false;
2372         }
2373 }
2374
2375 /**
2376  * ice_sched_rm_agg_cfg - remove the aggregator node
2377  * @pi: port information structure
2378  * @agg_id: aggregator ID
2379  * @tc: TC number
2380  *
2381  * This function removes the aggregator node and intermediate nodes if any
2382  * from the given TC
2383  */
2384 static enum ice_status
2385 ice_sched_rm_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2386 {
2387         struct ice_sched_node *tc_node, *agg_node;
2388         struct ice_hw *hw = pi->hw;
2389
2390         tc_node = ice_sched_get_tc_node(pi, tc);
2391         if (!tc_node)
2392                 return ICE_ERR_CFG;
2393
2394         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2395         if (!agg_node)
2396                 return ICE_ERR_DOES_NOT_EXIST;
2397
2398         /* Can't remove the aggregator node if it has children */
2399         if (ice_sched_is_agg_inuse(pi, agg_node))
2400                 return ICE_ERR_IN_USE;
2401
2402         /* need to remove the whole subtree if aggregator node is the
2403          * only child.
2404          */
2405         while (agg_node->tx_sched_layer > hw->sw_entry_point_layer) {
2406                 struct ice_sched_node *parent = agg_node->parent;
2407
2408                 if (!parent)
2409                         return ICE_ERR_CFG;
2410
2411                 if (parent->num_children > 1)
2412                         break;
2413
2414                 agg_node = parent;
2415         }
2416
2417         ice_free_sched_node(pi, agg_node);
2418         return ICE_SUCCESS;
2419 }
2420
2421 /**
2422  * ice_rm_agg_cfg_tc - remove aggregator configuration for TC
2423  * @pi: port information structure
2424  * @agg_info: aggregator ID
2425  * @tc: TC number
2426  * @rm_vsi_info: bool value true or false
2427  *
2428  * This function removes aggregator reference to VSI of given TC. It removes
2429  * the aggregator configuration completely for requested TC. The caller needs
2430  * to hold the scheduler lock.
2431  */
2432 static enum ice_status
2433 ice_rm_agg_cfg_tc(struct ice_port_info *pi, struct ice_sched_agg_info *agg_info,
2434                   u8 tc, bool rm_vsi_info)
2435 {
2436         enum ice_status status = ICE_SUCCESS;
2437
2438         /* If nothing to remove - return success */
2439         if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2440                 goto exit_rm_agg_cfg_tc;
2441
2442         status = ice_move_all_vsi_to_dflt_agg(pi, agg_info, tc, rm_vsi_info);
2443         if (status)
2444                 goto exit_rm_agg_cfg_tc;
2445
2446         /* Delete aggregator node(s) */
2447         status = ice_sched_rm_agg_cfg(pi, agg_info->agg_id, tc);
2448         if (status)
2449                 goto exit_rm_agg_cfg_tc;
2450
2451         ice_clear_bit(tc, agg_info->tc_bitmap);
2452 exit_rm_agg_cfg_tc:
2453         return status;
2454 }
2455
2456 /**
2457  * ice_save_agg_tc_bitmap - save aggregator TC bitmap
2458  * @pi: port information structure
2459  * @agg_id: aggregator ID
2460  * @tc_bitmap: 8 bits TC bitmap
2461  *
2462  * Save aggregator TC bitmap. This function needs to be called with scheduler
2463  * lock held.
2464  */
2465 static enum ice_status
2466 ice_save_agg_tc_bitmap(struct ice_port_info *pi, u32 agg_id,
2467                        ice_bitmap_t *tc_bitmap)
2468 {
2469         struct ice_sched_agg_info *agg_info;
2470
2471         agg_info = ice_get_agg_info(pi->hw, agg_id);
2472         if (!agg_info)
2473                 return ICE_ERR_PARAM;
2474         ice_cp_bitmap(agg_info->replay_tc_bitmap, tc_bitmap,
2475                       ICE_MAX_TRAFFIC_CLASS);
2476         return ICE_SUCCESS;
2477 }
2478
2479 /**
2480  * ice_sched_add_agg_cfg - create an aggregator node
2481  * @pi: port information structure
2482  * @agg_id: aggregator ID
2483  * @tc: TC number
2484  *
2485  * This function creates an aggregator node and intermediate nodes if required
2486  * for the given TC
2487  */
2488 static enum ice_status
2489 ice_sched_add_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2490 {
2491         struct ice_sched_node *parent, *agg_node, *tc_node;
2492         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2493         enum ice_status status = ICE_SUCCESS;
2494         struct ice_hw *hw = pi->hw;
2495         u32 first_node_teid;
2496         u16 num_nodes_added;
2497         u8 i, aggl;
2498
2499         tc_node = ice_sched_get_tc_node(pi, tc);
2500         if (!tc_node)
2501                 return ICE_ERR_CFG;
2502
2503         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2504         /* Does Agg node already exist ? */
2505         if (agg_node)
2506                 return status;
2507
2508         aggl = ice_sched_get_agg_layer(hw);
2509
2510         /* need one node in Agg layer */
2511         num_nodes[aggl] = 1;
2512
2513         /* Check whether the intermediate nodes have space to add the
2514          * new aggregator. If they are full, then SW needs to allocate a new
2515          * intermediate node on those layers
2516          */
2517         for (i = hw->sw_entry_point_layer; i < aggl; i++) {
2518                 parent = ice_sched_get_first_node(pi, tc_node, i);
2519
2520                 /* scan all the siblings */
2521                 while (parent) {
2522                         if (parent->num_children < hw->max_children[i])
2523                                 break;
2524                         parent = parent->sibling;
2525                 }
2526
2527                 /* all the nodes are full, reserve one for this layer */
2528                 if (!parent)
2529                         num_nodes[i]++;
2530         }
2531
2532         /* add the aggregator node */
2533         parent = tc_node;
2534         for (i = hw->sw_entry_point_layer; i <= aggl; i++) {
2535                 if (!parent)
2536                         return ICE_ERR_CFG;
2537
2538                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2539                                                       num_nodes[i],
2540                                                       &first_node_teid,
2541                                                       &num_nodes_added);
2542                 if (status != ICE_SUCCESS || num_nodes[i] != num_nodes_added)
2543                         return ICE_ERR_CFG;
2544
2545                 /* The newly added node can be a new parent for the next
2546                  * layer nodes
2547                  */
2548                 if (num_nodes_added) {
2549                         parent = ice_sched_find_node_by_teid(tc_node,
2550                                                              first_node_teid);
2551                         /* register aggregator ID with the aggregator node */
2552                         if (parent && i == aggl)
2553                                 parent->agg_id = agg_id;
2554                 } else {
2555                         parent = parent->children[0];
2556                 }
2557         }
2558
2559         return ICE_SUCCESS;
2560 }
2561
2562 /**
2563  * ice_sched_cfg_agg - configure aggregator node
2564  * @pi: port information structure
2565  * @agg_id: aggregator ID
2566  * @agg_type: aggregator type queue, VSI, or aggregator group
2567  * @tc_bitmap: bits TC bitmap
2568  *
2569  * It registers a unique aggregator node into scheduler services. It
2570  * allows a user to register with a unique ID to track it's resources.
2571  * The aggregator type determines if this is a queue group, VSI group
2572  * or aggregator group. It then creates the aggregator node(s) for requested
2573  * TC(s) or removes an existing aggregator node including its configuration
2574  * if indicated via tc_bitmap. Call ice_rm_agg_cfg to release aggregator
2575  * resources and remove aggregator ID.
2576  * This function needs to be called with scheduler lock held.
2577  */
2578 static enum ice_status
2579 ice_sched_cfg_agg(struct ice_port_info *pi, u32 agg_id,
2580                   enum ice_agg_type agg_type, ice_bitmap_t *tc_bitmap)
2581 {
2582         struct ice_sched_agg_info *agg_info;
2583         enum ice_status status = ICE_SUCCESS;
2584         struct ice_hw *hw = pi->hw;
2585         u8 tc;
2586
2587         agg_info = ice_get_agg_info(hw, agg_id);
2588         if (!agg_info) {
2589                 /* Create new entry for new aggregator ID */
2590                 agg_info = (struct ice_sched_agg_info *)
2591                         ice_malloc(hw, sizeof(*agg_info));
2592                 if (!agg_info) {
2593                         status = ICE_ERR_NO_MEMORY;
2594                         goto exit_reg_agg;
2595                 }
2596                 agg_info->agg_id = agg_id;
2597                 agg_info->agg_type = agg_type;
2598                 agg_info->tc_bitmap[0] = 0;
2599
2600                 /* Initialize the aggregator VSI list head */
2601                 INIT_LIST_HEAD(&agg_info->agg_vsi_list);
2602
2603                 /* Add new entry in aggregator list */
2604                 LIST_ADD(&agg_info->list_entry, &hw->agg_list);
2605         }
2606         /* Create aggregator node(s) for requested TC(s) */
2607         ice_for_each_traffic_class(tc) {
2608                 if (!ice_is_tc_ena(*tc_bitmap, tc)) {
2609                         /* Delete aggregator cfg TC if it exists previously */
2610                         status = ice_rm_agg_cfg_tc(pi, agg_info, tc, false);
2611                         if (status)
2612                                 break;
2613                         continue;
2614                 }
2615
2616                 /* Check if aggregator node for TC already exists */
2617                 if (ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2618                         continue;
2619
2620                 /* Create new aggregator node for TC */
2621                 status = ice_sched_add_agg_cfg(pi, agg_id, tc);
2622                 if (status)
2623                         break;
2624
2625                 /* Save aggregator node's TC information */
2626                 ice_set_bit(tc, agg_info->tc_bitmap);
2627         }
2628 exit_reg_agg:
2629         return status;
2630 }
2631
2632 /**
2633  * ice_cfg_agg - config aggregator node
2634  * @pi: port information structure
2635  * @agg_id: aggregator ID
2636  * @agg_type: aggregator type queue, VSI, or aggregator group
2637  * @tc_bitmap: bits TC bitmap
2638  *
2639  * This function configures aggregator node(s).
2640  */
2641 enum ice_status
2642 ice_cfg_agg(struct ice_port_info *pi, u32 agg_id, enum ice_agg_type agg_type,
2643             u8 tc_bitmap)
2644 {
2645         ice_bitmap_t bitmap = tc_bitmap;
2646         enum ice_status status;
2647
2648         ice_acquire_lock(&pi->sched_lock);
2649         status = ice_sched_cfg_agg(pi, agg_id, agg_type,
2650                                    (ice_bitmap_t *)&bitmap);
2651         if (!status)
2652                 status = ice_save_agg_tc_bitmap(pi, agg_id,
2653                                                 (ice_bitmap_t *)&bitmap);
2654         ice_release_lock(&pi->sched_lock);
2655         return status;
2656 }
2657
2658 /**
2659  * ice_get_agg_vsi_info - get the aggregator ID
2660  * @agg_info: aggregator info
2661  * @vsi_handle: software VSI handle
2662  *
2663  * The function returns aggregator VSI info based on VSI handle. This function
2664  * needs to be called with scheduler lock held.
2665  */
2666 static struct ice_sched_agg_vsi_info*
2667 ice_get_agg_vsi_info(struct ice_sched_agg_info *agg_info, u16 vsi_handle)
2668 {
2669         struct ice_sched_agg_vsi_info *agg_vsi_info;
2670
2671         LIST_FOR_EACH_ENTRY(agg_vsi_info, &agg_info->agg_vsi_list,
2672                             ice_sched_agg_vsi_info, list_entry)
2673                 if (agg_vsi_info->vsi_handle == vsi_handle)
2674                         return agg_vsi_info;
2675
2676         return NULL;
2677 }
2678
2679 /**
2680  * ice_get_vsi_agg_info - get the aggregator info of VSI
2681  * @hw: pointer to the hardware structure
2682  * @vsi_handle: Sw VSI handle
2683  *
2684  * The function returns aggregator info of VSI represented via vsi_handle. The
2685  * VSI has in this case a different aggregator than the default one. This
2686  * function needs to be called with scheduler lock held.
2687  */
2688 static struct ice_sched_agg_info*
2689 ice_get_vsi_agg_info(struct ice_hw *hw, u16 vsi_handle)
2690 {
2691         struct ice_sched_agg_info *agg_info;
2692
2693         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
2694                             list_entry) {
2695                 struct ice_sched_agg_vsi_info *agg_vsi_info;
2696
2697                 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2698                 if (agg_vsi_info)
2699                         return agg_info;
2700         }
2701         return NULL;
2702 }
2703
2704 /**
2705  * ice_save_agg_vsi_tc_bitmap - save aggregator VSI TC bitmap
2706  * @pi: port information structure
2707  * @agg_id: aggregator ID
2708  * @vsi_handle: software VSI handle
2709  * @tc_bitmap: TC bitmap of enabled TC(s)
2710  *
2711  * Save VSI to aggregator TC bitmap. This function needs to call with scheduler
2712  * lock held.
2713  */
2714 static enum ice_status
2715 ice_save_agg_vsi_tc_bitmap(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2716                            ice_bitmap_t *tc_bitmap)
2717 {
2718         struct ice_sched_agg_vsi_info *agg_vsi_info;
2719         struct ice_sched_agg_info *agg_info;
2720
2721         agg_info = ice_get_agg_info(pi->hw, agg_id);
2722         if (!agg_info)
2723                 return ICE_ERR_PARAM;
2724         /* check if entry already exist */
2725         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2726         if (!agg_vsi_info)
2727                 return ICE_ERR_PARAM;
2728         ice_cp_bitmap(agg_vsi_info->replay_tc_bitmap, tc_bitmap,
2729                       ICE_MAX_TRAFFIC_CLASS);
2730         return ICE_SUCCESS;
2731 }
2732
2733 /**
2734  * ice_sched_assoc_vsi_to_agg - associate/move VSI to new/default aggregator
2735  * @pi: port information structure
2736  * @agg_id: aggregator ID
2737  * @vsi_handle: software VSI handle
2738  * @tc_bitmap: TC bitmap of enabled TC(s)
2739  *
2740  * This function moves VSI to a new or default aggregator node. If VSI is
2741  * already associated to the aggregator node then no operation is performed on
2742  * the tree. This function needs to be called with scheduler lock held.
2743  */
2744 static enum ice_status
2745 ice_sched_assoc_vsi_to_agg(struct ice_port_info *pi, u32 agg_id,
2746                            u16 vsi_handle, ice_bitmap_t *tc_bitmap)
2747 {
2748         struct ice_sched_agg_vsi_info *agg_vsi_info;
2749         struct ice_sched_agg_info *agg_info;
2750         enum ice_status status = ICE_SUCCESS;
2751         struct ice_hw *hw = pi->hw;
2752         u8 tc;
2753
2754         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2755                 return ICE_ERR_PARAM;
2756         agg_info = ice_get_agg_info(hw, agg_id);
2757         if (!agg_info)
2758                 return ICE_ERR_PARAM;
2759         /* check if entry already exist */
2760         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2761         if (!agg_vsi_info) {
2762                 /* Create new entry for VSI under aggregator list */
2763                 agg_vsi_info = (struct ice_sched_agg_vsi_info *)
2764                         ice_malloc(hw, sizeof(*agg_vsi_info));
2765                 if (!agg_vsi_info)
2766                         return ICE_ERR_PARAM;
2767
2768                 /* add VSI ID into the aggregator list */
2769                 agg_vsi_info->vsi_handle = vsi_handle;
2770                 LIST_ADD(&agg_vsi_info->list_entry, &agg_info->agg_vsi_list);
2771         }
2772         /* Move VSI node to new aggregator node for requested TC(s) */
2773         ice_for_each_traffic_class(tc) {
2774                 if (!ice_is_tc_ena(*tc_bitmap, tc))
2775                         continue;
2776
2777                 /* Move VSI to new aggregator */
2778                 status = ice_sched_move_vsi_to_agg(pi, vsi_handle, agg_id, tc);
2779                 if (status)
2780                         break;
2781
2782                 if (agg_id != ICE_DFLT_AGG_ID)
2783                         ice_set_bit(tc, agg_vsi_info->tc_bitmap);
2784                 else
2785                         ice_clear_bit(tc, agg_vsi_info->tc_bitmap);
2786         }
2787         /* If VSI moved back to default aggregator, delete agg_vsi_info. */
2788         if (!ice_is_any_bit_set(agg_vsi_info->tc_bitmap,
2789                                 ICE_MAX_TRAFFIC_CLASS)) {
2790                 LIST_DEL(&agg_vsi_info->list_entry);
2791                 ice_free(hw, agg_vsi_info);
2792         }
2793         return status;
2794 }
2795
2796 /**
2797  * ice_sched_rm_unused_rl_prof - remove unused RL profile
2798  * @pi: port information structure
2799  *
2800  * This function removes unused rate limit profiles from the HW and
2801  * SW DB. The caller needs to hold scheduler lock.
2802  */
2803 static void ice_sched_rm_unused_rl_prof(struct ice_port_info *pi)
2804 {
2805         u8 ln;
2806
2807         for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
2808                 struct ice_aqc_rl_profile_info *rl_prof_elem;
2809                 struct ice_aqc_rl_profile_info *rl_prof_tmp;
2810
2811                 LIST_FOR_EACH_ENTRY_SAFE(rl_prof_elem, rl_prof_tmp,
2812                                          &pi->rl_prof_list[ln],
2813                                          ice_aqc_rl_profile_info, list_entry) {
2814                         if (!ice_sched_del_rl_profile(pi->hw, rl_prof_elem))
2815                                 ice_debug(pi->hw, ICE_DBG_SCHED,
2816                                           "Removed rl profile\n");
2817                 }
2818         }
2819 }
2820
2821 /**
2822  * ice_sched_update_elem - update element
2823  * @hw: pointer to the HW struct
2824  * @node: pointer to node
2825  * @info: node info to update
2826  *
2827  * It updates the HW DB, and local SW DB of node. It updates the scheduling
2828  * parameters of node from argument info data buffer (Info->data buf) and
2829  * returns success or error on config sched element failure. The caller
2830  * needs to hold scheduler lock.
2831  */
2832 static enum ice_status
2833 ice_sched_update_elem(struct ice_hw *hw, struct ice_sched_node *node,
2834                       struct ice_aqc_txsched_elem_data *info)
2835 {
2836         struct ice_aqc_conf_elem buf;
2837         enum ice_status status;
2838         u16 elem_cfgd = 0;
2839         u16 num_elems = 1;
2840
2841         buf.generic[0] = *info;
2842         /* Parent TEID is reserved field in this aq call */
2843         buf.generic[0].parent_teid = 0;
2844         /* Element type is reserved field in this aq call */
2845         buf.generic[0].data.elem_type = 0;
2846         /* Flags is reserved field in this aq call */
2847         buf.generic[0].data.flags = 0;
2848
2849         /* Update HW DB */
2850         /* Configure element node */
2851         status = ice_aq_cfg_sched_elems(hw, num_elems, &buf, sizeof(buf),
2852                                         &elem_cfgd, NULL);
2853         if (status || elem_cfgd != num_elems) {
2854                 ice_debug(hw, ICE_DBG_SCHED, "Config sched elem error\n");
2855                 return ICE_ERR_CFG;
2856         }
2857
2858         /* Config success case */
2859         /* Now update local SW DB */
2860         /* Only copy the data portion of info buffer */
2861         node->info.data = info->data;
2862         return status;
2863 }
2864
2865 /**
2866  * ice_sched_cfg_node_bw_alloc - configure node BW weight/alloc params
2867  * @hw: pointer to the HW struct
2868  * @node: sched node to configure
2869  * @rl_type: rate limit type CIR, EIR, or shared
2870  * @bw_alloc: BW weight/allocation
2871  *
2872  * This function configures node element's BW allocation.
2873  */
2874 static enum ice_status
2875 ice_sched_cfg_node_bw_alloc(struct ice_hw *hw, struct ice_sched_node *node,
2876                             enum ice_rl_type rl_type, u8 bw_alloc)
2877 {
2878         struct ice_aqc_txsched_elem_data buf;
2879         struct ice_aqc_txsched_elem *data;
2880         enum ice_status status;
2881
2882         buf = node->info;
2883         data = &buf.data;
2884         if (rl_type == ICE_MIN_BW) {
2885                 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
2886                 data->cir_bw.bw_alloc = CPU_TO_LE16(bw_alloc);
2887         } else if (rl_type == ICE_MAX_BW) {
2888                 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
2889                 data->eir_bw.bw_alloc = CPU_TO_LE16(bw_alloc);
2890         } else {
2891                 return ICE_ERR_PARAM;
2892         }
2893
2894         /* Configure element */
2895         status = ice_sched_update_elem(hw, node, &buf);
2896         return status;
2897 }
2898
2899 /**
2900  * ice_move_vsi_to_agg - moves VSI to new or default aggregator
2901  * @pi: port information structure
2902  * @agg_id: aggregator ID
2903  * @vsi_handle: software VSI handle
2904  * @tc_bitmap: TC bitmap of enabled TC(s)
2905  *
2906  * Move or associate VSI to a new or default aggregator node.
2907  */
2908 enum ice_status
2909 ice_move_vsi_to_agg(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2910                     u8 tc_bitmap)
2911 {
2912         ice_bitmap_t bitmap = tc_bitmap;
2913         enum ice_status status;
2914
2915         ice_acquire_lock(&pi->sched_lock);
2916         status = ice_sched_assoc_vsi_to_agg(pi, agg_id, vsi_handle,
2917                                             (ice_bitmap_t *)&bitmap);
2918         if (!status)
2919                 status = ice_save_agg_vsi_tc_bitmap(pi, agg_id, vsi_handle,
2920                                                     (ice_bitmap_t *)&bitmap);
2921         ice_release_lock(&pi->sched_lock);
2922         return status;
2923 }
2924
2925 /**
2926  * ice_rm_agg_cfg - remove aggregator configuration
2927  * @pi: port information structure
2928  * @agg_id: aggregator ID
2929  *
2930  * This function removes aggregator reference to VSI and delete aggregator ID
2931  * info. It removes the aggregator configuration completely.
2932  */
2933 enum ice_status ice_rm_agg_cfg(struct ice_port_info *pi, u32 agg_id)
2934 {
2935         struct ice_sched_agg_info *agg_info;
2936         enum ice_status status = ICE_SUCCESS;
2937         u8 tc;
2938
2939         ice_acquire_lock(&pi->sched_lock);
2940         agg_info = ice_get_agg_info(pi->hw, agg_id);
2941         if (!agg_info) {
2942                 status = ICE_ERR_DOES_NOT_EXIST;
2943                 goto exit_ice_rm_agg_cfg;
2944         }
2945
2946         ice_for_each_traffic_class(tc) {
2947                 status = ice_rm_agg_cfg_tc(pi, agg_info, tc, true);
2948                 if (status)
2949                         goto exit_ice_rm_agg_cfg;
2950         }
2951
2952         if (ice_is_any_bit_set(agg_info->tc_bitmap, ICE_MAX_TRAFFIC_CLASS)) {
2953                 status = ICE_ERR_IN_USE;
2954                 goto exit_ice_rm_agg_cfg;
2955         }
2956
2957         /* Safe to delete entry now */
2958         LIST_DEL(&agg_info->list_entry);
2959         ice_free(pi->hw, agg_info);
2960
2961         /* Remove unused RL profile IDs from HW and SW DB */
2962         ice_sched_rm_unused_rl_prof(pi);
2963
2964 exit_ice_rm_agg_cfg:
2965         ice_release_lock(&pi->sched_lock);
2966         return status;
2967 }
2968
2969 /**
2970  * ice_set_clear_cir_bw_alloc - set or clear CIR BW alloc information
2971  * @bw_t_info: bandwidth type information structure
2972  * @bw_alloc: Bandwidth allocation information
2973  *
2974  * Save or clear CIR BW alloc information (bw_alloc) in the passed param
2975  * bw_t_info.
2976  */
2977 static void
2978 ice_set_clear_cir_bw_alloc(struct ice_bw_type_info *bw_t_info, u16 bw_alloc)
2979 {
2980         bw_t_info->cir_bw.bw_alloc = bw_alloc;
2981         if (bw_t_info->cir_bw.bw_alloc)
2982                 ice_set_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap);
2983         else
2984                 ice_clear_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap);
2985 }
2986
2987 /**
2988  * ice_set_clear_eir_bw_alloc - set or clear EIR BW alloc information
2989  * @bw_t_info: bandwidth type information structure
2990  * @bw_alloc: Bandwidth allocation information
2991  *
2992  * Save or clear EIR BW alloc information (bw_alloc) in the passed param
2993  * bw_t_info.
2994  */
2995 static void
2996 ice_set_clear_eir_bw_alloc(struct ice_bw_type_info *bw_t_info, u16 bw_alloc)
2997 {
2998         bw_t_info->eir_bw.bw_alloc = bw_alloc;
2999         if (bw_t_info->eir_bw.bw_alloc)
3000                 ice_set_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap);
3001         else
3002                 ice_clear_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap);
3003 }
3004
3005 /**
3006  * ice_sched_save_vsi_bw_alloc - save VSI node's BW alloc information
3007  * @pi: port information structure
3008  * @vsi_handle: sw VSI handle
3009  * @tc: traffic class
3010  * @rl_type: rate limit type min or max
3011  * @bw_alloc: Bandwidth allocation information
3012  *
3013  * Save BW alloc information of VSI type node for post replay use.
3014  */
3015 static enum ice_status
3016 ice_sched_save_vsi_bw_alloc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3017                             enum ice_rl_type rl_type, u16 bw_alloc)
3018 {
3019         struct ice_vsi_ctx *vsi_ctx;
3020
3021         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3022                 return ICE_ERR_PARAM;
3023         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3024         if (!vsi_ctx)
3025                 return ICE_ERR_PARAM;
3026         switch (rl_type) {
3027         case ICE_MIN_BW:
3028                 ice_set_clear_cir_bw_alloc(&vsi_ctx->sched.bw_t_info[tc],
3029                                            bw_alloc);
3030                 break;
3031         case ICE_MAX_BW:
3032                 ice_set_clear_eir_bw_alloc(&vsi_ctx->sched.bw_t_info[tc],
3033                                            bw_alloc);
3034                 break;
3035         default:
3036                 return ICE_ERR_PARAM;
3037         }
3038         return ICE_SUCCESS;
3039 }
3040
3041 /**
3042  * ice_set_clear_cir_bw - set or clear CIR BW
3043  * @bw_t_info: bandwidth type information structure
3044  * @bw: bandwidth in Kbps - Kilo bits per sec
3045  *
3046  * Save or clear CIR bandwidth (BW) in the passed param bw_t_info.
3047  */
3048 static void
3049 ice_set_clear_cir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3050 {
3051         if (bw == ICE_SCHED_DFLT_BW) {
3052                 ice_clear_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
3053                 bw_t_info->cir_bw.bw = 0;
3054         } else {
3055                 /* Save type of BW information */
3056                 ice_set_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
3057                 bw_t_info->cir_bw.bw = bw;
3058         }
3059 }
3060
3061 /**
3062  * ice_set_clear_eir_bw - set or clear EIR BW
3063  * @bw_t_info: bandwidth type information structure
3064  * @bw: bandwidth in Kbps - Kilo bits per sec
3065  *
3066  * Save or clear EIR bandwidth (BW) in the passed param bw_t_info.
3067  */
3068 static void
3069 ice_set_clear_eir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3070 {
3071         if (bw == ICE_SCHED_DFLT_BW) {
3072                 ice_clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3073                 bw_t_info->eir_bw.bw = 0;
3074         } else {
3075                 /* EIR BW and Shared BW profiles are mutually exclusive and
3076                  * hence only one of them may be set for any given element.
3077                  * First clear earlier saved shared BW information.
3078                  */
3079                 ice_clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3080                 bw_t_info->shared_bw = 0;
3081                 /* save EIR BW information */
3082                 ice_set_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3083                 bw_t_info->eir_bw.bw = bw;
3084         }
3085 }
3086
3087 /**
3088  * ice_set_clear_shared_bw - set or clear shared BW
3089  * @bw_t_info: bandwidth type information structure
3090  * @bw: bandwidth in Kbps - Kilo bits per sec
3091  *
3092  * Save or clear shared bandwidth (BW) in the passed param bw_t_info.
3093  */
3094 static void
3095 ice_set_clear_shared_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3096 {
3097         if (bw == ICE_SCHED_DFLT_BW) {
3098                 ice_clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3099                 bw_t_info->shared_bw = 0;
3100         } else {
3101                 /* EIR BW and Shared BW profiles are mutually exclusive and
3102                  * hence only one of them may be set for any given element.
3103                  * First clear earlier saved EIR BW information.
3104                  */
3105                 ice_clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3106                 bw_t_info->eir_bw.bw = 0;
3107                 /* save shared BW information */
3108                 ice_set_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3109                 bw_t_info->shared_bw = bw;
3110         }
3111 }
3112
3113 /**
3114  * ice_sched_save_vsi_bw - save VSI node's BW information
3115  * @pi: port information structure
3116  * @vsi_handle: sw VSI handle
3117  * @tc: traffic class
3118  * @rl_type: rate limit type min, max, or shared
3119  * @bw: bandwidth in Kbps - Kilo bits per sec
3120  *
3121  * Save BW information of VSI type node for post replay use.
3122  */
3123 static enum ice_status
3124 ice_sched_save_vsi_bw(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3125                       enum ice_rl_type rl_type, u32 bw)
3126 {
3127         struct ice_vsi_ctx *vsi_ctx;
3128
3129         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3130                 return ICE_ERR_PARAM;
3131         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3132         if (!vsi_ctx)
3133                 return ICE_ERR_PARAM;
3134         switch (rl_type) {
3135         case ICE_MIN_BW:
3136                 ice_set_clear_cir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3137                 break;
3138         case ICE_MAX_BW:
3139                 ice_set_clear_eir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3140                 break;
3141         case ICE_SHARED_BW:
3142                 ice_set_clear_shared_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3143                 break;
3144         default:
3145                 return ICE_ERR_PARAM;
3146         }
3147         return ICE_SUCCESS;
3148 }
3149
3150 /**
3151  * ice_set_clear_prio - set or clear priority information
3152  * @bw_t_info: bandwidth type information structure
3153  * @prio: priority to save
3154  *
3155  * Save or clear priority (prio) in the passed param bw_t_info.
3156  */
3157 static void
3158 ice_set_clear_prio(struct ice_bw_type_info *bw_t_info, u8 prio)
3159 {
3160         bw_t_info->generic = prio;
3161         if (bw_t_info->generic)
3162                 ice_set_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap);
3163         else
3164                 ice_clear_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap);
3165 }
3166
3167 /**
3168  * ice_sched_save_vsi_prio - save VSI node's priority information
3169  * @pi: port information structure
3170  * @vsi_handle: Software VSI handle
3171  * @tc: traffic class
3172  * @prio: priority to save
3173  *
3174  * Save priority information of VSI type node for post replay use.
3175  */
3176 static enum ice_status
3177 ice_sched_save_vsi_prio(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3178                         u8 prio)
3179 {
3180         struct ice_vsi_ctx *vsi_ctx;
3181
3182         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3183                 return ICE_ERR_PARAM;
3184         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3185         if (!vsi_ctx)
3186                 return ICE_ERR_PARAM;
3187         if (tc >= ICE_MAX_TRAFFIC_CLASS)
3188                 return ICE_ERR_PARAM;
3189         ice_set_clear_prio(&vsi_ctx->sched.bw_t_info[tc], prio);
3190         return ICE_SUCCESS;
3191 }
3192
3193 /**
3194  * ice_sched_save_agg_bw_alloc - save aggregator node's BW alloc information
3195  * @pi: port information structure
3196  * @agg_id: node aggregator ID
3197  * @tc: traffic class
3198  * @rl_type: rate limit type min or max
3199  * @bw_alloc: bandwidth alloc information
3200  *
3201  * Save BW alloc information of AGG type node for post replay use.
3202  */
3203 static enum ice_status
3204 ice_sched_save_agg_bw_alloc(struct ice_port_info *pi, u32 agg_id, u8 tc,
3205                             enum ice_rl_type rl_type, u16 bw_alloc)
3206 {
3207         struct ice_sched_agg_info *agg_info;
3208
3209         agg_info = ice_get_agg_info(pi->hw, agg_id);
3210         if (!agg_info)
3211                 return ICE_ERR_PARAM;
3212         if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
3213                 return ICE_ERR_PARAM;
3214         switch (rl_type) {
3215         case ICE_MIN_BW:
3216                 ice_set_clear_cir_bw_alloc(&agg_info->bw_t_info[tc], bw_alloc);
3217                 break;
3218         case ICE_MAX_BW:
3219                 ice_set_clear_eir_bw_alloc(&agg_info->bw_t_info[tc], bw_alloc);
3220                 break;
3221         default:
3222                 return ICE_ERR_PARAM;
3223         }
3224         return ICE_SUCCESS;
3225 }
3226
3227 /**
3228  * ice_sched_save_agg_bw - save aggregator node's BW information
3229  * @pi: port information structure
3230  * @agg_id: node aggregator ID
3231  * @tc: traffic class
3232  * @rl_type: rate limit type min, max, or shared
3233  * @bw: bandwidth in Kbps - Kilo bits per sec
3234  *
3235  * Save BW information of AGG type node for post replay use.
3236  */
3237 static enum ice_status
3238 ice_sched_save_agg_bw(struct ice_port_info *pi, u32 agg_id, u8 tc,
3239                       enum ice_rl_type rl_type, u32 bw)
3240 {
3241         struct ice_sched_agg_info *agg_info;
3242
3243         agg_info = ice_get_agg_info(pi->hw, agg_id);
3244         if (!agg_info)
3245                 return ICE_ERR_PARAM;
3246         if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
3247                 return ICE_ERR_PARAM;
3248         switch (rl_type) {
3249         case ICE_MIN_BW:
3250                 ice_set_clear_cir_bw(&agg_info->bw_t_info[tc], bw);
3251                 break;
3252         case ICE_MAX_BW:
3253                 ice_set_clear_eir_bw(&agg_info->bw_t_info[tc], bw);
3254                 break;
3255         case ICE_SHARED_BW:
3256                 ice_set_clear_shared_bw(&agg_info->bw_t_info[tc], bw);
3257                 break;
3258         default:
3259                 return ICE_ERR_PARAM;
3260         }
3261         return ICE_SUCCESS;
3262 }
3263
3264 /**
3265  * ice_cfg_vsi_bw_lmt_per_tc - configure VSI BW limit per TC
3266  * @pi: port information structure
3267  * @vsi_handle: software VSI handle
3268  * @tc: traffic class
3269  * @rl_type: min or max
3270  * @bw: bandwidth in Kbps
3271  *
3272  * This function configures BW limit of VSI scheduling node based on TC
3273  * information.
3274  */
3275 enum ice_status
3276 ice_cfg_vsi_bw_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3277                           enum ice_rl_type rl_type, u32 bw)
3278 {
3279         enum ice_status status;
3280
3281         status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
3282                                                   ICE_AGG_TYPE_VSI,
3283                                                   tc, rl_type, bw);
3284         if (!status) {
3285                 ice_acquire_lock(&pi->sched_lock);
3286                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type, bw);
3287                 ice_release_lock(&pi->sched_lock);
3288         }
3289         return status;
3290 }
3291
3292 /**
3293  * ice_cfg_dflt_vsi_bw_lmt_per_tc - configure default VSI BW limit per TC
3294  * @pi: port information structure
3295  * @vsi_handle: software VSI handle
3296  * @tc: traffic class
3297  * @rl_type: min or max
3298  *
3299  * This function configures default BW limit of VSI scheduling node based on TC
3300  * information.
3301  */
3302 enum ice_status
3303 ice_cfg_vsi_bw_dflt_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3304                                enum ice_rl_type rl_type)
3305 {
3306         enum ice_status status;
3307
3308         status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
3309                                                   ICE_AGG_TYPE_VSI,
3310                                                   tc, rl_type,
3311                                                   ICE_SCHED_DFLT_BW);
3312         if (!status) {
3313                 ice_acquire_lock(&pi->sched_lock);
3314                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type,
3315                                                ICE_SCHED_DFLT_BW);
3316                 ice_release_lock(&pi->sched_lock);
3317         }
3318         return status;
3319 }
3320
3321 /**
3322  * ice_cfg_agg_bw_lmt_per_tc - configure aggregator BW limit per TC
3323  * @pi: port information structure
3324  * @agg_id: aggregator ID
3325  * @tc: traffic class
3326  * @rl_type: min or max
3327  * @bw: bandwidth in Kbps
3328  *
3329  * This function applies BW limit to aggregator scheduling node based on TC
3330  * information.
3331  */
3332 enum ice_status
3333 ice_cfg_agg_bw_lmt_per_tc(struct ice_port_info *pi, u32 agg_id, u8 tc,
3334                           enum ice_rl_type rl_type, u32 bw)
3335 {
3336         enum ice_status status;
3337
3338         status = ice_sched_set_node_bw_lmt_per_tc(pi, agg_id, ICE_AGG_TYPE_AGG,
3339                                                   tc, rl_type, bw);
3340         if (!status) {
3341                 ice_acquire_lock(&pi->sched_lock);
3342                 status = ice_sched_save_agg_bw(pi, agg_id, tc, rl_type, bw);
3343                 ice_release_lock(&pi->sched_lock);
3344         }
3345         return status;
3346 }
3347
3348 /**
3349  * ice_cfg_agg_bw_dflt_lmt_per_tc - configure aggregator BW default limit per TC
3350  * @pi: port information structure
3351  * @agg_id: aggregator ID
3352  * @tc: traffic class
3353  * @rl_type: min or max
3354  *
3355  * This function applies default BW limit to aggregator scheduling node based
3356  * on TC information.
3357  */
3358 enum ice_status
3359 ice_cfg_agg_bw_dflt_lmt_per_tc(struct ice_port_info *pi, u32 agg_id, u8 tc,
3360                                enum ice_rl_type rl_type)
3361 {
3362         enum ice_status status;
3363
3364         status = ice_sched_set_node_bw_lmt_per_tc(pi, agg_id, ICE_AGG_TYPE_AGG,
3365                                                   tc, rl_type,
3366                                                   ICE_SCHED_DFLT_BW);
3367         if (!status) {
3368                 ice_acquire_lock(&pi->sched_lock);
3369                 status = ice_sched_save_agg_bw(pi, agg_id, tc, rl_type,
3370                                                ICE_SCHED_DFLT_BW);
3371                 ice_release_lock(&pi->sched_lock);
3372         }
3373         return status;
3374 }
3375
3376 /**
3377  * ice_cfg_vsi_bw_shared_lmt - configure VSI BW shared limit
3378  * @pi: port information structure
3379  * @vsi_handle: software VSI handle
3380  * @bw: bandwidth in Kbps
3381  *
3382  * This function Configures shared rate limiter(SRL) of all VSI type nodes
3383  * across all traffic classes for VSI matching handle.
3384  */
3385 enum ice_status
3386 ice_cfg_vsi_bw_shared_lmt(struct ice_port_info *pi, u16 vsi_handle, u32 bw)
3387 {
3388         return ice_sched_set_vsi_bw_shared_lmt(pi, vsi_handle, bw);
3389 }
3390
3391 /**
3392  * ice_cfg_vsi_bw_no_shared_lmt - configure VSI BW for no shared limiter
3393  * @pi: port information structure
3394  * @vsi_handle: software VSI handle
3395  *
3396  * This function removes the shared rate limiter(SRL) of all VSI type nodes
3397  * across all traffic classes for VSI matching handle.
3398  */
3399 enum ice_status
3400 ice_cfg_vsi_bw_no_shared_lmt(struct ice_port_info *pi, u16 vsi_handle)
3401 {
3402         return ice_sched_set_vsi_bw_shared_lmt(pi, vsi_handle,
3403                                                ICE_SCHED_DFLT_BW);
3404 }
3405
3406 /**
3407  * ice_cfg_agg_bw_shared_lmt - configure aggregator BW shared limit
3408  * @pi: port information structure
3409  * @agg_id: aggregator ID
3410  * @bw: bandwidth in Kbps
3411  *
3412  * This function configures the shared rate limiter(SRL) of all aggregator type
3413  * nodes across all traffic classes for aggregator matching agg_id.
3414  */
3415 enum ice_status
3416 ice_cfg_agg_bw_shared_lmt(struct ice_port_info *pi, u32 agg_id, u32 bw)
3417 {
3418         return ice_sched_set_agg_bw_shared_lmt(pi, agg_id, bw);
3419 }
3420
3421 /**
3422  * ice_cfg_agg_bw_no_shared_lmt - configure aggregator BW for no shared limiter
3423  * @pi: port information structure
3424  * @agg_id: aggregator ID
3425  *
3426  * This function removes the shared rate limiter(SRL) of all aggregator type
3427  * nodes across all traffic classes for aggregator matching agg_id.
3428  */
3429 enum ice_status
3430 ice_cfg_agg_bw_no_shared_lmt(struct ice_port_info *pi, u32 agg_id)
3431 {
3432         return ice_sched_set_agg_bw_shared_lmt(pi, agg_id, ICE_SCHED_DFLT_BW);
3433 }
3434
3435 /**
3436  * ice_config_vsi_queue_priority - config VSI queue priority of node
3437  * @pi: port information structure
3438  * @num_qs: number of VSI queues
3439  * @q_ids: queue IDs array
3440  * @q_ids: queue IDs array
3441  * @q_prio: queue priority array
3442  *
3443  * This function configures the queue node priority (Sibling Priority) of the
3444  * passed in VSI's queue(s) for a given traffic class (TC).
3445  */
3446 enum ice_status
3447 ice_cfg_vsi_q_priority(struct ice_port_info *pi, u16 num_qs, u32 *q_ids,
3448                        u8 *q_prio)
3449 {
3450         enum ice_status status = ICE_ERR_PARAM;
3451         u16 i;
3452
3453         ice_acquire_lock(&pi->sched_lock);
3454
3455         for (i = 0; i < num_qs; i++) {
3456                 struct ice_sched_node *node;
3457
3458                 node = ice_sched_find_node_by_teid(pi->root, q_ids[i]);
3459                 if (!node || node->info.data.elem_type !=
3460                     ICE_AQC_ELEM_TYPE_LEAF) {
3461                         status = ICE_ERR_PARAM;
3462                         break;
3463                 }
3464                 /* Configure Priority */
3465                 status = ice_sched_cfg_sibl_node_prio(pi, node, q_prio[i]);
3466                 if (status)
3467                         break;
3468         }
3469
3470         ice_release_lock(&pi->sched_lock);
3471         return status;
3472 }
3473
3474 /**
3475  * ice_cfg_agg_vsi_priority_per_tc - config aggregator's VSI priority per TC
3476  * @pi: port information structure
3477  * @agg_id: Aggregator ID
3478  * @num_vsis: number of VSI(s)
3479  * @vsi_handle_arr: array of software VSI handles
3480  * @node_prio: pointer to node priority
3481  * @tc: traffic class
3482  *
3483  * This function configures the node priority (Sibling Priority) of the
3484  * passed in VSI's for a given traffic class (TC) of an Aggregator ID.
3485  */
3486 enum ice_status
3487 ice_cfg_agg_vsi_priority_per_tc(struct ice_port_info *pi, u32 agg_id,
3488                                 u16 num_vsis, u16 *vsi_handle_arr,
3489                                 u8 *node_prio, u8 tc)
3490 {
3491         struct ice_sched_agg_vsi_info *agg_vsi_info;
3492         struct ice_sched_node *tc_node, *agg_node;
3493         enum ice_status status = ICE_ERR_PARAM;
3494         struct ice_sched_agg_info *agg_info;
3495         bool agg_id_present = false;
3496         struct ice_hw *hw = pi->hw;
3497         u16 i;
3498
3499         ice_acquire_lock(&pi->sched_lock);
3500         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
3501                             list_entry)
3502                 if (agg_info->agg_id == agg_id) {
3503                         agg_id_present = true;
3504                         break;
3505                 }
3506         if (!agg_id_present)
3507                 goto exit_agg_priority_per_tc;
3508
3509         tc_node = ice_sched_get_tc_node(pi, tc);
3510         if (!tc_node)
3511                 goto exit_agg_priority_per_tc;
3512
3513         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
3514         if (!agg_node)
3515                 goto exit_agg_priority_per_tc;
3516
3517         if (num_vsis > hw->max_children[agg_node->tx_sched_layer])
3518                 goto exit_agg_priority_per_tc;
3519
3520         for (i = 0; i < num_vsis; i++) {
3521                 struct ice_sched_node *vsi_node;
3522                 bool vsi_handle_valid = false;
3523                 u16 vsi_handle;
3524
3525                 status = ICE_ERR_PARAM;
3526                 vsi_handle = vsi_handle_arr[i];
3527                 if (!ice_is_vsi_valid(hw, vsi_handle))
3528                         goto exit_agg_priority_per_tc;
3529                 /* Verify child nodes before applying settings */
3530                 LIST_FOR_EACH_ENTRY(agg_vsi_info, &agg_info->agg_vsi_list,
3531                                     ice_sched_agg_vsi_info, list_entry)
3532                         if (agg_vsi_info->vsi_handle == vsi_handle) {
3533                                 /* cppcheck-suppress unreadVariable */
3534                                 vsi_handle_valid = true;
3535                                 break;
3536                         }
3537
3538                 if (!vsi_handle_valid)
3539                         goto exit_agg_priority_per_tc;
3540
3541                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
3542                 if (!vsi_node)
3543                         goto exit_agg_priority_per_tc;
3544
3545                 if (ice_sched_find_node_in_subtree(hw, agg_node, vsi_node)) {
3546                         /* Configure Priority */
3547                         status = ice_sched_cfg_sibl_node_prio(pi, vsi_node,
3548                                                               node_prio[i]);
3549                         if (status)
3550                                 break;
3551                         status = ice_sched_save_vsi_prio(pi, vsi_handle, tc,
3552                                                          node_prio[i]);
3553                         if (status)
3554                                 break;
3555                 }
3556         }
3557
3558 exit_agg_priority_per_tc:
3559         ice_release_lock(&pi->sched_lock);
3560         return status;
3561 }
3562
3563 /**
3564  * ice_cfg_vsi_bw_alloc - config VSI BW alloc per TC
3565  * @pi: port information structure
3566  * @vsi_handle: software VSI handle
3567  * @ena_tcmap: enabled TC map
3568  * @rl_type: Rate limit type CIR/EIR
3569  * @bw_alloc: Array of BW alloc
3570  *
3571  * This function configures the BW allocation of the passed in VSI's
3572  * node(s) for enabled traffic class.
3573  */
3574 enum ice_status
3575 ice_cfg_vsi_bw_alloc(struct ice_port_info *pi, u16 vsi_handle, u8 ena_tcmap,
3576                      enum ice_rl_type rl_type, u8 *bw_alloc)
3577 {
3578         enum ice_status status = ICE_SUCCESS;
3579         u8 tc;
3580
3581         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3582                 return ICE_ERR_PARAM;
3583
3584         ice_acquire_lock(&pi->sched_lock);
3585
3586         /* Return success if no nodes are present across TC */
3587         ice_for_each_traffic_class(tc) {
3588                 struct ice_sched_node *tc_node, *vsi_node;
3589
3590                 if (!ice_is_tc_ena(ena_tcmap, tc))
3591                         continue;
3592
3593                 tc_node = ice_sched_get_tc_node(pi, tc);
3594                 if (!tc_node)
3595                         continue;
3596
3597                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
3598                 if (!vsi_node)
3599                         continue;
3600
3601                 status = ice_sched_cfg_node_bw_alloc(pi->hw, vsi_node, rl_type,
3602                                                      bw_alloc[tc]);
3603                 if (status)
3604                         break;
3605                 status = ice_sched_save_vsi_bw_alloc(pi, vsi_handle, tc,
3606                                                      rl_type, bw_alloc[tc]);
3607                 if (status)
3608                         break;
3609         }
3610
3611         ice_release_lock(&pi->sched_lock);
3612         return status;
3613 }
3614
3615 /**
3616  * ice_cfg_agg_bw_alloc - config aggregator BW alloc
3617  * @pi: port information structure
3618  * @agg_id: aggregator ID
3619  * @ena_tcmap: enabled TC map
3620  * @rl_type: rate limit type CIR/EIR
3621  * @bw_alloc: array of BW alloc
3622  *
3623  * This function configures the BW allocation of passed in aggregator for
3624  * enabled traffic class(s).
3625  */
3626 enum ice_status
3627 ice_cfg_agg_bw_alloc(struct ice_port_info *pi, u32 agg_id, u8 ena_tcmap,
3628                      enum ice_rl_type rl_type, u8 *bw_alloc)
3629 {
3630         struct ice_sched_agg_info *agg_info;
3631         bool agg_id_present = false;
3632         enum ice_status status = ICE_SUCCESS;
3633         struct ice_hw *hw = pi->hw;
3634         u8 tc;
3635
3636         ice_acquire_lock(&pi->sched_lock);
3637         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
3638                             list_entry)
3639                 if (agg_info->agg_id == agg_id) {
3640                         agg_id_present = true;
3641                         break;
3642                 }
3643         if (!agg_id_present) {
3644                 status = ICE_ERR_PARAM;
3645                 goto exit_cfg_agg_bw_alloc;
3646         }
3647
3648         /* Return success if no nodes are present across TC */
3649         ice_for_each_traffic_class(tc) {
3650                 struct ice_sched_node *tc_node, *agg_node;
3651
3652                 if (!ice_is_tc_ena(ena_tcmap, tc))
3653                         continue;
3654
3655                 tc_node = ice_sched_get_tc_node(pi, tc);
3656                 if (!tc_node)
3657                         continue;
3658
3659                 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
3660                 if (!agg_node)
3661                         continue;
3662
3663                 status = ice_sched_cfg_node_bw_alloc(hw, agg_node, rl_type,
3664                                                      bw_alloc[tc]);
3665                 if (status)
3666                         break;
3667                 status = ice_sched_save_agg_bw_alloc(pi, agg_id, tc, rl_type,
3668                                                      bw_alloc[tc]);
3669                 if (status)
3670                         break;
3671         }
3672
3673 exit_cfg_agg_bw_alloc:
3674         ice_release_lock(&pi->sched_lock);
3675         return status;
3676 }
3677
3678 /**
3679  * ice_sched_calc_wakeup - calculate RL profile wakeup parameter
3680  * @bw: bandwidth in Kbps
3681  *
3682  * This function calculates the wakeup parameter of RL profile.
3683  */
3684 static u16 ice_sched_calc_wakeup(s32 bw)
3685 {
3686         s64 bytes_per_sec, wakeup_int, wakeup_a, wakeup_b, wakeup_f;
3687         s32 wakeup_f_int;
3688         u16 wakeup = 0;
3689
3690         /* Get the wakeup integer value */
3691         bytes_per_sec = DIV_64BIT(((s64)bw * 1000), BITS_PER_BYTE);
3692         wakeup_int = DIV_64BIT(ICE_RL_PROF_FREQUENCY, bytes_per_sec);
3693         if (wakeup_int > 63) {
3694                 wakeup = (u16)((1 << 15) | wakeup_int);
3695         } else {
3696                 /* Calculate fraction value up to 4 decimals
3697                  * Convert Integer value to a constant multiplier
3698                  */
3699                 wakeup_b = (s64)ICE_RL_PROF_MULTIPLIER * wakeup_int;
3700                 wakeup_a = DIV_64BIT((s64)ICE_RL_PROF_MULTIPLIER *
3701                                      ICE_RL_PROF_FREQUENCY, bytes_per_sec);
3702
3703                 /* Get Fraction value */
3704                 wakeup_f = wakeup_a - wakeup_b;
3705
3706                 /* Round up the Fractional value via Ceil(Fractional value) */
3707                 if (wakeup_f > DIV_64BIT(ICE_RL_PROF_MULTIPLIER, 2))
3708                         wakeup_f += 1;
3709
3710                 wakeup_f_int = (s32)DIV_64BIT(wakeup_f * ICE_RL_PROF_FRACTION,
3711                                               ICE_RL_PROF_MULTIPLIER);
3712                 wakeup |= (u16)(wakeup_int << 9);
3713                 wakeup |= (u16)(0x1ff & wakeup_f_int);
3714         }
3715
3716         return wakeup;
3717 }
3718
3719 /**
3720  * ice_sched_bw_to_rl_profile - convert BW to profile parameters
3721  * @bw: bandwidth in Kbps
3722  * @profile: profile parameters to return
3723  *
3724  * This function converts the BW to profile structure format.
3725  */
3726 static enum ice_status
3727 ice_sched_bw_to_rl_profile(u32 bw, struct ice_aqc_rl_profile_elem *profile)
3728 {
3729         enum ice_status status = ICE_ERR_PARAM;
3730         s64 bytes_per_sec, ts_rate, mv_tmp;
3731         bool found = false;
3732         s32 encode = 0;
3733         s64 mv = 0;
3734         s32 i;
3735
3736         /* Bw settings range is from 0.5Mb/sec to 100Gb/sec */
3737         if (bw < ICE_SCHED_MIN_BW || bw > ICE_SCHED_MAX_BW)
3738                 return status;
3739
3740         /* Bytes per second from Kbps */
3741         bytes_per_sec = DIV_64BIT(((s64)bw * 1000), BITS_PER_BYTE);
3742
3743         /* encode is 6 bits but really useful are 5 bits */
3744         for (i = 0; i < 64; i++) {
3745                 u64 pow_result = BIT_ULL(i);
3746
3747                 ts_rate = DIV_64BIT((s64)ICE_RL_PROF_FREQUENCY,
3748                                     pow_result * ICE_RL_PROF_TS_MULTIPLIER);
3749                 if (ts_rate <= 0)
3750                         continue;
3751
3752                 /* Multiplier value */
3753                 mv_tmp = DIV_64BIT(bytes_per_sec * ICE_RL_PROF_MULTIPLIER,
3754                                    ts_rate);
3755
3756                 /* Round to the nearest ICE_RL_PROF_MULTIPLIER */
3757                 mv = round_up_64bit(mv_tmp, ICE_RL_PROF_MULTIPLIER);
3758
3759                 /* First multiplier value greater than the given
3760                  * accuracy bytes
3761                  */
3762                 if (mv > ICE_RL_PROF_ACCURACY_BYTES) {
3763                         encode = i;
3764                         found = true;
3765                         break;
3766                 }
3767         }
3768         if (found) {
3769                 u16 wm;
3770
3771                 wm = ice_sched_calc_wakeup(bw);
3772                 profile->rl_multiply = CPU_TO_LE16(mv);
3773                 profile->wake_up_calc = CPU_TO_LE16(wm);
3774                 profile->rl_encode = CPU_TO_LE16(encode);
3775                 status = ICE_SUCCESS;
3776         } else {
3777                 status = ICE_ERR_DOES_NOT_EXIST;
3778         }
3779
3780         return status;
3781 }
3782
3783 /**
3784  * ice_sched_add_rl_profile - add RL profile
3785  * @pi: port information structure
3786  * @rl_type: type of rate limit BW - min, max, or shared
3787  * @bw: bandwidth in Kbps - Kilo bits per sec
3788  * @layer_num: specifies in which layer to create profile
3789  *
3790  * This function first checks the existing list for corresponding BW
3791  * parameter. If it exists, it returns the associated profile otherwise
3792  * it creates a new rate limit profile for requested BW, and adds it to
3793  * the HW DB and local list. It returns the new profile or null on error.
3794  * The caller needs to hold the scheduler lock.
3795  */
3796 static struct ice_aqc_rl_profile_info *
3797 ice_sched_add_rl_profile(struct ice_port_info *pi,
3798                          enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3799 {
3800         struct ice_aqc_rl_profile_generic_elem *buf;
3801         struct ice_aqc_rl_profile_info *rl_prof_elem;
3802         u16 profiles_added = 0, num_profiles = 1;
3803         enum ice_status status = ICE_ERR_PARAM;
3804         struct ice_hw *hw;
3805         u8 profile_type;
3806
3807         switch (rl_type) {
3808         case ICE_MIN_BW:
3809                 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3810                 break;
3811         case ICE_MAX_BW:
3812                 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3813                 break;
3814         case ICE_SHARED_BW:
3815                 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3816                 break;
3817         default:
3818                 return NULL;
3819         }
3820
3821         if (!pi)
3822                 return NULL;
3823         hw = pi->hw;
3824         LIST_FOR_EACH_ENTRY(rl_prof_elem, &pi->rl_prof_list[layer_num],
3825                             ice_aqc_rl_profile_info, list_entry)
3826                 if (rl_prof_elem->profile.flags == profile_type &&
3827                     rl_prof_elem->bw == bw)
3828                         /* Return existing profile ID info */
3829                         return rl_prof_elem;
3830
3831         /* Create new profile ID */
3832         rl_prof_elem = (struct ice_aqc_rl_profile_info *)
3833                 ice_malloc(hw, sizeof(*rl_prof_elem));
3834
3835         if (!rl_prof_elem)
3836                 return NULL;
3837
3838         status = ice_sched_bw_to_rl_profile(bw, &rl_prof_elem->profile);
3839         if (status != ICE_SUCCESS)
3840                 goto exit_add_rl_prof;
3841
3842         rl_prof_elem->bw = bw;
3843         /* layer_num is zero relative, and fw expects level from 1 to 9 */
3844         rl_prof_elem->profile.level = layer_num + 1;
3845         rl_prof_elem->profile.flags = profile_type;
3846         rl_prof_elem->profile.max_burst_size = CPU_TO_LE16(hw->max_burst_size);
3847
3848         /* Create new entry in HW DB */
3849         buf = (struct ice_aqc_rl_profile_generic_elem *)
3850                 &rl_prof_elem->profile;
3851         status = ice_aq_add_rl_profile(hw, num_profiles, buf, sizeof(*buf),
3852                                        &profiles_added, NULL);
3853         if (status || profiles_added != num_profiles)
3854                 goto exit_add_rl_prof;
3855
3856         /* Good entry - add in the list */
3857         rl_prof_elem->prof_id_ref = 0;
3858         LIST_ADD(&rl_prof_elem->list_entry, &pi->rl_prof_list[layer_num]);
3859         return rl_prof_elem;
3860
3861 exit_add_rl_prof:
3862         ice_free(hw, rl_prof_elem);
3863         return NULL;
3864 }
3865
3866 /**
3867  * ice_sched_cfg_node_bw_lmt - configure node sched params
3868  * @hw: pointer to the HW struct
3869  * @node: sched node to configure
3870  * @rl_type: rate limit type CIR, EIR, or shared
3871  * @rl_prof_id: rate limit profile ID
3872  *
3873  * This function configures node element's BW limit.
3874  */
3875 static enum ice_status
3876 ice_sched_cfg_node_bw_lmt(struct ice_hw *hw, struct ice_sched_node *node,
3877                           enum ice_rl_type rl_type, u16 rl_prof_id)
3878 {
3879         struct ice_aqc_txsched_elem_data buf;
3880         struct ice_aqc_txsched_elem *data;
3881
3882         buf = node->info;
3883         data = &buf.data;
3884         switch (rl_type) {
3885         case ICE_MIN_BW:
3886                 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
3887                 data->cir_bw.bw_profile_idx = CPU_TO_LE16(rl_prof_id);
3888                 break;
3889         case ICE_MAX_BW:
3890                 /* EIR BW and Shared BW profiles are mutually exclusive and
3891                  * hence only one of them may be set for any given element
3892                  */
3893                 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3894                         return ICE_ERR_CFG;
3895                 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3896                 data->eir_bw.bw_profile_idx = CPU_TO_LE16(rl_prof_id);
3897                 break;
3898         case ICE_SHARED_BW:
3899                 /* Check for removing shared BW */
3900                 if (rl_prof_id == ICE_SCHED_NO_SHARED_RL_PROF_ID) {
3901                         /* remove shared profile */
3902                         data->valid_sections &= ~ICE_AQC_ELEM_VALID_SHARED;
3903                         data->srl_id = 0; /* clear SRL field */
3904
3905                         /* enable back EIR to default profile */
3906                         data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3907                         data->eir_bw.bw_profile_idx =
3908                                 CPU_TO_LE16(ICE_SCHED_DFLT_RL_PROF_ID);
3909                         break;
3910                 }
3911                 /* EIR BW and Shared BW profiles are mutually exclusive and
3912                  * hence only one of them may be set for any given element
3913                  */
3914                 if ((data->valid_sections & ICE_AQC_ELEM_VALID_EIR) &&
3915                     (LE16_TO_CPU(data->eir_bw.bw_profile_idx) !=
3916                             ICE_SCHED_DFLT_RL_PROF_ID))
3917                         return ICE_ERR_CFG;
3918                 /* EIR BW is set to default, disable it */
3919                 data->valid_sections &= ~ICE_AQC_ELEM_VALID_EIR;
3920                 /* Okay to enable shared BW now */
3921                 data->valid_sections |= ICE_AQC_ELEM_VALID_SHARED;
3922                 data->srl_id = CPU_TO_LE16(rl_prof_id);
3923                 break;
3924         default:
3925                 /* Unknown rate limit type */
3926                 return ICE_ERR_PARAM;
3927         }
3928
3929         /* Configure element */
3930         return ice_sched_update_elem(hw, node, &buf);
3931 }
3932
3933 /**
3934  * ice_sched_get_node_rl_prof_id - get node's rate limit profile ID
3935  * @node: sched node
3936  * @rl_type: rate limit type
3937  *
3938  * If existing profile matches, it returns the corresponding rate
3939  * limit profile ID, otherwise it returns an invalid ID as error.
3940  */
3941 static u16
3942 ice_sched_get_node_rl_prof_id(struct ice_sched_node *node,
3943                               enum ice_rl_type rl_type)
3944 {
3945         u16 rl_prof_id = ICE_SCHED_INVAL_PROF_ID;
3946         struct ice_aqc_txsched_elem *data;
3947
3948         data = &node->info.data;
3949         switch (rl_type) {
3950         case ICE_MIN_BW:
3951                 if (data->valid_sections & ICE_AQC_ELEM_VALID_CIR)
3952                         rl_prof_id = LE16_TO_CPU(data->cir_bw.bw_profile_idx);
3953                 break;
3954         case ICE_MAX_BW:
3955                 if (data->valid_sections & ICE_AQC_ELEM_VALID_EIR)
3956                         rl_prof_id = LE16_TO_CPU(data->eir_bw.bw_profile_idx);
3957                 break;
3958         case ICE_SHARED_BW:
3959                 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3960                         rl_prof_id = LE16_TO_CPU(data->srl_id);
3961                 break;
3962         default:
3963                 break;
3964         }
3965
3966         return rl_prof_id;
3967 }
3968
3969 /**
3970  * ice_sched_get_rl_prof_layer - selects rate limit profile creation layer
3971  * @pi: port information structure
3972  * @rl_type: type of rate limit BW - min, max, or shared
3973  * @layer_index: layer index
3974  *
3975  * This function returns requested profile creation layer.
3976  */
3977 static u8
3978 ice_sched_get_rl_prof_layer(struct ice_port_info *pi, enum ice_rl_type rl_type,
3979                             u8 layer_index)
3980 {
3981         struct ice_hw *hw = pi->hw;
3982
3983         if (layer_index >= hw->num_tx_sched_layers)
3984                 return ICE_SCHED_INVAL_LAYER_NUM;
3985         switch (rl_type) {
3986         case ICE_MIN_BW:
3987                 if (hw->layer_info[layer_index].max_cir_rl_profiles)
3988                         return layer_index;
3989                 break;
3990         case ICE_MAX_BW:
3991                 if (hw->layer_info[layer_index].max_eir_rl_profiles)
3992                         return layer_index;
3993                 break;
3994         case ICE_SHARED_BW:
3995                 /* if current layer doesn't support SRL profile creation
3996                  * then try a layer up or down.
3997                  */
3998                 if (hw->layer_info[layer_index].max_srl_profiles)
3999                         return layer_index;
4000                 else if (layer_index < hw->num_tx_sched_layers - 1 &&
4001                          hw->layer_info[layer_index + 1].max_srl_profiles)
4002                         return layer_index + 1;
4003                 else if (layer_index > 0 &&
4004                          hw->layer_info[layer_index - 1].max_srl_profiles)
4005                         return layer_index - 1;
4006                 break;
4007         default:
4008                 break;
4009         }
4010         return ICE_SCHED_INVAL_LAYER_NUM;
4011 }
4012
4013 /**
4014  * ice_sched_get_srl_node - get shared rate limit node
4015  * @node: tree node
4016  * @srl_layer: shared rate limit layer
4017  *
4018  * This function returns SRL node to be used for shared rate limit purpose.
4019  * The caller needs to hold scheduler lock.
4020  */
4021 static struct ice_sched_node *
4022 ice_sched_get_srl_node(struct ice_sched_node *node, u8 srl_layer)
4023 {
4024         if (srl_layer > node->tx_sched_layer)
4025                 return node->children[0];
4026         else if (srl_layer < node->tx_sched_layer)
4027                 /* Node can't be created without a parent. It will always
4028                  * have a valid parent except root node.
4029                  */
4030                 return node->parent;
4031         else
4032                 return node;
4033 }
4034
4035 /**
4036  * ice_sched_rm_rl_profile - remove RL profile ID
4037  * @pi: port information structure
4038  * @layer_num: layer number where profiles are saved
4039  * @profile_type: profile type like EIR, CIR, or SRL
4040  * @profile_id: profile ID to remove
4041  *
4042  * This function removes rate limit profile from layer 'layer_num' of type
4043  * 'profile_type' and profile ID as 'profile_id'. The caller needs to hold
4044  * scheduler lock.
4045  */
4046 static enum ice_status
4047 ice_sched_rm_rl_profile(struct ice_port_info *pi, u8 layer_num, u8 profile_type,
4048                         u16 profile_id)
4049 {
4050         struct ice_aqc_rl_profile_info *rl_prof_elem;
4051         enum ice_status status = ICE_SUCCESS;
4052
4053         /* Check the existing list for RL profile */
4054         LIST_FOR_EACH_ENTRY(rl_prof_elem, &pi->rl_prof_list[layer_num],
4055                             ice_aqc_rl_profile_info, list_entry)
4056                 if (rl_prof_elem->profile.flags == profile_type &&
4057                     LE16_TO_CPU(rl_prof_elem->profile.profile_id) ==
4058                     profile_id) {
4059                         if (rl_prof_elem->prof_id_ref)
4060                                 rl_prof_elem->prof_id_ref--;
4061
4062                         /* Remove old profile ID from database */
4063                         status = ice_sched_del_rl_profile(pi->hw, rl_prof_elem);
4064                         if (status && status != ICE_ERR_IN_USE)
4065                                 ice_debug(pi->hw, ICE_DBG_SCHED,
4066                                           "Remove rl profile failed\n");
4067                         break;
4068                 }
4069         if (status == ICE_ERR_IN_USE)
4070                 status = ICE_SUCCESS;
4071         return status;
4072 }
4073
4074 /**
4075  * ice_sched_set_node_bw_dflt - set node's bandwidth limit to default
4076  * @pi: port information structure
4077  * @node: pointer to node structure
4078  * @rl_type: rate limit type min, max, or shared
4079  * @layer_num: layer number where RL profiles are saved
4080  *
4081  * This function configures node element's BW rate limit profile ID of
4082  * type CIR, EIR, or SRL to default. This function needs to be called
4083  * with the scheduler lock held.
4084  */
4085 static enum ice_status
4086 ice_sched_set_node_bw_dflt(struct ice_port_info *pi,
4087                            struct ice_sched_node *node,
4088                            enum ice_rl_type rl_type, u8 layer_num)
4089 {
4090         enum ice_status status;
4091         struct ice_hw *hw;
4092         u8 profile_type;
4093         u16 rl_prof_id;
4094         u16 old_id;
4095
4096         hw = pi->hw;
4097         switch (rl_type) {
4098         case ICE_MIN_BW:
4099                 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
4100                 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
4101                 break;
4102         case ICE_MAX_BW:
4103                 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
4104                 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
4105                 break;
4106         case ICE_SHARED_BW:
4107                 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
4108                 /* No SRL is configured for default case */
4109                 rl_prof_id = ICE_SCHED_NO_SHARED_RL_PROF_ID;
4110                 break;
4111         default:
4112                 return ICE_ERR_PARAM;
4113         }
4114         /* Save existing RL prof ID for later clean up */
4115         old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
4116         /* Configure BW scheduling parameters */
4117         status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
4118         if (status)
4119                 return status;
4120
4121         /* Remove stale RL profile ID */
4122         if (old_id == ICE_SCHED_DFLT_RL_PROF_ID ||
4123             old_id == ICE_SCHED_INVAL_PROF_ID)
4124                 return ICE_SUCCESS;
4125
4126         return ice_sched_rm_rl_profile(pi, layer_num, profile_type, old_id);
4127 }
4128
4129 /**
4130  * ice_sched_set_eir_srl_excl - set EIR/SRL exclusiveness
4131  * @pi: port information structure
4132  * @node: pointer to node structure
4133  * @layer_num: layer number where rate limit profiles are saved
4134  * @rl_type: rate limit type min, max, or shared
4135  * @bw: bandwidth value
4136  *
4137  * This function prepares node element's bandwidth to SRL or EIR exclusively.
4138  * EIR BW and Shared BW profiles are mutually exclusive and hence only one of
4139  * them may be set for any given element. This function needs to be called
4140  * with the scheduler lock held.
4141  */
4142 static enum ice_status
4143 ice_sched_set_eir_srl_excl(struct ice_port_info *pi,
4144                            struct ice_sched_node *node,
4145                            u8 layer_num, enum ice_rl_type rl_type, u32 bw)
4146 {
4147         if (rl_type == ICE_SHARED_BW) {
4148                 /* SRL node passed in this case, it may be different node */
4149                 if (bw == ICE_SCHED_DFLT_BW)
4150                         /* SRL being removed, ice_sched_cfg_node_bw_lmt()
4151                          * enables EIR to default. EIR is not set in this
4152                          * case, so no additional action is required.
4153                          */
4154                         return ICE_SUCCESS;
4155
4156                 /* SRL being configured, set EIR to default here.
4157                  * ice_sched_cfg_node_bw_lmt() disables EIR when it
4158                  * configures SRL
4159                  */
4160                 return ice_sched_set_node_bw_dflt(pi, node, ICE_MAX_BW,
4161                                                   layer_num);
4162         } else if (rl_type == ICE_MAX_BW &&
4163                    node->info.data.valid_sections & ICE_AQC_ELEM_VALID_SHARED) {
4164                 /* Remove Shared profile. Set default shared BW call
4165                  * removes shared profile for a node.
4166                  */
4167                 return ice_sched_set_node_bw_dflt(pi, node,
4168                                                   ICE_SHARED_BW,
4169                                                   layer_num);
4170         }
4171         return ICE_SUCCESS;
4172 }
4173
4174 /**
4175  * ice_sched_set_node_bw - set node's bandwidth
4176  * @pi: port information structure
4177  * @node: tree node
4178  * @rl_type: rate limit type min, max, or shared
4179  * @bw: bandwidth in Kbps - Kilo bits per sec
4180  * @layer_num: layer number
4181  *
4182  * This function adds new profile corresponding to requested BW, configures
4183  * node's RL profile ID of type CIR, EIR, or SRL, and removes old profile
4184  * ID from local database. The caller needs to hold scheduler lock.
4185  */
4186 static enum ice_status
4187 ice_sched_set_node_bw(struct ice_port_info *pi, struct ice_sched_node *node,
4188                       enum ice_rl_type rl_type, u32 bw, u8 layer_num)
4189 {
4190         struct ice_aqc_rl_profile_info *rl_prof_info;
4191         enum ice_status status = ICE_ERR_PARAM;
4192         struct ice_hw *hw = pi->hw;
4193         u16 old_id, rl_prof_id;
4194
4195         rl_prof_info = ice_sched_add_rl_profile(pi, rl_type, bw, layer_num);
4196         if (!rl_prof_info)
4197                 return status;
4198
4199         rl_prof_id = LE16_TO_CPU(rl_prof_info->profile.profile_id);
4200
4201         /* Save existing RL prof ID for later clean up */
4202         old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
4203         /* Configure BW scheduling parameters */
4204         status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
4205         if (status)
4206                 return status;
4207
4208         /* New changes has been applied */
4209         /* Increment the profile ID reference count */
4210         rl_prof_info->prof_id_ref++;
4211
4212         /* Check for old ID removal */
4213         if ((old_id == ICE_SCHED_DFLT_RL_PROF_ID && rl_type != ICE_SHARED_BW) ||
4214             old_id == ICE_SCHED_INVAL_PROF_ID || old_id == rl_prof_id)
4215                 return ICE_SUCCESS;
4216
4217         return ice_sched_rm_rl_profile(pi, layer_num,
4218                                        rl_prof_info->profile.flags,
4219                                        old_id);
4220 }
4221
4222 /**
4223  * ice_sched_set_node_bw_lmt - set node's BW limit
4224  * @pi: port information structure
4225  * @node: tree node
4226  * @rl_type: rate limit type min, max, or shared
4227  * @bw: bandwidth in Kbps - Kilo bits per sec
4228  *
4229  * It updates node's BW limit parameters like BW RL profile ID of type CIR,
4230  * EIR, or SRL. The caller needs to hold scheduler lock.
4231  */
4232 static enum ice_status
4233 ice_sched_set_node_bw_lmt(struct ice_port_info *pi, struct ice_sched_node *node,
4234                           enum ice_rl_type rl_type, u32 bw)
4235 {
4236         struct ice_sched_node *cfg_node = node;
4237         enum ice_status status;
4238
4239         struct ice_hw *hw;
4240         u8 layer_num;
4241
4242         if (!pi)
4243                 return ICE_ERR_PARAM;
4244         hw = pi->hw;
4245         /* Remove unused RL profile IDs from HW and SW DB */
4246         ice_sched_rm_unused_rl_prof(pi);
4247         layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
4248                                                 node->tx_sched_layer);
4249         if (layer_num >= hw->num_tx_sched_layers)
4250                 return ICE_ERR_PARAM;
4251
4252         if (rl_type == ICE_SHARED_BW) {
4253                 /* SRL node may be different */
4254                 cfg_node = ice_sched_get_srl_node(node, layer_num);
4255                 if (!cfg_node)
4256                         return ICE_ERR_CFG;
4257         }
4258         /* EIR BW and Shared BW profiles are mutually exclusive and
4259          * hence only one of them may be set for any given element
4260          */
4261         status = ice_sched_set_eir_srl_excl(pi, cfg_node, layer_num, rl_type,
4262                                             bw);
4263         if (status)
4264                 return status;
4265         if (bw == ICE_SCHED_DFLT_BW)
4266                 return ice_sched_set_node_bw_dflt(pi, cfg_node, rl_type,
4267                                                   layer_num);
4268         return ice_sched_set_node_bw(pi, cfg_node, rl_type, bw, layer_num);
4269 }
4270
4271 /**
4272  * ice_sched_set_node_bw_dflt_lmt - set node's BW limit to default
4273  * @pi: port information structure
4274  * @node: pointer to node structure
4275  * @rl_type: rate limit type min, max, or shared
4276  *
4277  * This function configures node element's BW rate limit profile ID of
4278  * type CIR, EIR, or SRL to default. This function needs to be called
4279  * with the scheduler lock held.
4280  */
4281 static enum ice_status
4282 ice_sched_set_node_bw_dflt_lmt(struct ice_port_info *pi,
4283                                struct ice_sched_node *node,
4284                                enum ice_rl_type rl_type)
4285 {
4286         return ice_sched_set_node_bw_lmt(pi, node, rl_type,
4287                                          ICE_SCHED_DFLT_BW);
4288 }
4289
4290 /**
4291  * ice_sched_validate_srl_node - Check node for SRL applicability
4292  * @node: sched node to configure
4293  * @sel_layer: selected SRL layer
4294  *
4295  * This function checks if the SRL can be applied to a selceted layer node on
4296  * behalf of the requested node (first argument). This function needs to be
4297  * called with scheduler lock held.
4298  */
4299 static enum ice_status
4300 ice_sched_validate_srl_node(struct ice_sched_node *node, u8 sel_layer)
4301 {
4302         /* SRL profiles are not available on all layers. Check if the
4303          * SRL profile can be applied to a node above or below the
4304          * requested node. SRL configuration is possible only if the
4305          * selected layer's node has single child.
4306          */
4307         if (sel_layer == node->tx_sched_layer ||
4308             ((sel_layer == node->tx_sched_layer + 1) &&
4309             node->num_children == 1) ||
4310             ((sel_layer == node->tx_sched_layer - 1) &&
4311             (node->parent && node->parent->num_children == 1)))
4312                 return ICE_SUCCESS;
4313
4314         return ICE_ERR_CFG;
4315 }
4316
4317 /**
4318  * ice_sched_save_q_bw - save queue node's BW information
4319  * @q_ctx: queue context structure
4320  * @rl_type: rate limit type min, max, or shared
4321  * @bw: bandwidth in Kbps - Kilo bits per sec
4322  *
4323  * Save BW information of queue type node for post replay use.
4324  */
4325 static enum ice_status
4326 ice_sched_save_q_bw(struct ice_q_ctx *q_ctx, enum ice_rl_type rl_type, u32 bw)
4327 {
4328         switch (rl_type) {
4329         case ICE_MIN_BW:
4330                 ice_set_clear_cir_bw(&q_ctx->bw_t_info, bw);
4331                 break;
4332         case ICE_MAX_BW:
4333                 ice_set_clear_eir_bw(&q_ctx->bw_t_info, bw);
4334                 break;
4335         case ICE_SHARED_BW:
4336                 ice_set_clear_shared_bw(&q_ctx->bw_t_info, bw);
4337                 break;
4338         default:
4339                 return ICE_ERR_PARAM;
4340         }
4341         return ICE_SUCCESS;
4342 }
4343
4344 /**
4345  * ice_sched_set_q_bw_lmt - sets queue BW limit
4346  * @pi: port information structure
4347  * @vsi_handle: sw VSI handle
4348  * @tc: traffic class
4349  * @q_handle: software queue handle
4350  * @rl_type: min, max, or shared
4351  * @bw: bandwidth in Kbps
4352  *
4353  * This function sets BW limit of queue scheduling node.
4354  */
4355 static enum ice_status
4356 ice_sched_set_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4357                        u16 q_handle, enum ice_rl_type rl_type, u32 bw)
4358 {
4359         enum ice_status status = ICE_ERR_PARAM;
4360         struct ice_sched_node *node;
4361         struct ice_q_ctx *q_ctx;
4362
4363         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4364                 return ICE_ERR_PARAM;
4365         ice_acquire_lock(&pi->sched_lock);
4366         q_ctx = ice_get_lan_q_ctx(pi->hw, vsi_handle, tc, q_handle);
4367         if (!q_ctx)
4368                 goto exit_q_bw_lmt;
4369         node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
4370         if (!node) {
4371                 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong q_teid\n");
4372                 goto exit_q_bw_lmt;
4373         }
4374
4375         /* Return error if it is not a leaf node */
4376         if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF)
4377                 goto exit_q_bw_lmt;
4378
4379         /* SRL bandwidth layer selection */
4380         if (rl_type == ICE_SHARED_BW) {
4381                 u8 sel_layer; /* selected layer */
4382
4383                 sel_layer = ice_sched_get_rl_prof_layer(pi, rl_type,
4384                                                         node->tx_sched_layer);
4385                 if (sel_layer >= pi->hw->num_tx_sched_layers) {
4386                         status = ICE_ERR_PARAM;
4387                         goto exit_q_bw_lmt;
4388                 }
4389                 status = ice_sched_validate_srl_node(node, sel_layer);
4390                 if (status)
4391                         goto exit_q_bw_lmt;
4392         }
4393
4394         if (bw == ICE_SCHED_DFLT_BW)
4395                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
4396         else
4397                 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
4398
4399         if (!status)
4400                 status = ice_sched_save_q_bw(q_ctx, rl_type, bw);
4401
4402 exit_q_bw_lmt:
4403         ice_release_lock(&pi->sched_lock);
4404         return status;
4405 }
4406
4407 /**
4408  * ice_cfg_q_bw_lmt - configure queue BW limit
4409  * @pi: port information structure
4410  * @vsi_handle: sw VSI handle
4411  * @tc: traffic class
4412  * @q_handle: software queue handle
4413  * @rl_type: min, max, or shared
4414  * @bw: bandwidth in Kbps
4415  *
4416  * This function configures BW limit of queue scheduling node.
4417  */
4418 enum ice_status
4419 ice_cfg_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4420                  u16 q_handle, enum ice_rl_type rl_type, u32 bw)
4421 {
4422         return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
4423                                       bw);
4424 }
4425
4426 /**
4427  * ice_cfg_q_bw_dflt_lmt - configure queue BW default limit
4428  * @pi: port information structure
4429  * @vsi_handle: sw VSI handle
4430  * @tc: traffic class
4431  * @q_handle: software queue handle
4432  * @rl_type: min, max, or shared
4433  *
4434  * This function configures BW default limit of queue scheduling node.
4435  */
4436 enum ice_status
4437 ice_cfg_q_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4438                       u16 q_handle, enum ice_rl_type rl_type)
4439 {
4440         return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
4441                                       ICE_SCHED_DFLT_BW);
4442 }
4443
4444 /**
4445  * ice_sched_save_tc_node_bw - save TC node BW limit
4446  * @pi: port information structure
4447  * @tc: TC number
4448  * @rl_type: min or max
4449  * @bw: bandwidth in Kbps
4450  *
4451  * This function saves the modified values of bandwidth settings for later
4452  * replay purpose (restore) after reset.
4453  */
4454 static enum ice_status
4455 ice_sched_save_tc_node_bw(struct ice_port_info *pi, u8 tc,
4456                           enum ice_rl_type rl_type, u32 bw)
4457 {
4458         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4459                 return ICE_ERR_PARAM;
4460         switch (rl_type) {
4461         case ICE_MIN_BW:
4462                 ice_set_clear_cir_bw(&pi->tc_node_bw_t_info[tc], bw);
4463                 break;
4464         case ICE_MAX_BW:
4465                 ice_set_clear_eir_bw(&pi->tc_node_bw_t_info[tc], bw);
4466                 break;
4467         case ICE_SHARED_BW:
4468                 ice_set_clear_shared_bw(&pi->tc_node_bw_t_info[tc], bw);
4469                 break;
4470         default:
4471                 return ICE_ERR_PARAM;
4472         }
4473         return ICE_SUCCESS;
4474 }
4475
4476 /**
4477  * ice_sched_set_tc_node_bw_lmt - sets TC node BW limit
4478  * @pi: port information structure
4479  * @tc: TC number
4480  * @rl_type: min or max
4481  * @bw: bandwidth in Kbps
4482  *
4483  * This function configures bandwidth limit of TC node.
4484  */
4485 static enum ice_status
4486 ice_sched_set_tc_node_bw_lmt(struct ice_port_info *pi, u8 tc,
4487                              enum ice_rl_type rl_type, u32 bw)
4488 {
4489         enum ice_status status = ICE_ERR_PARAM;
4490         struct ice_sched_node *tc_node;
4491
4492         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4493                 return status;
4494         ice_acquire_lock(&pi->sched_lock);
4495         tc_node = ice_sched_get_tc_node(pi, tc);
4496         if (!tc_node)
4497                 goto exit_set_tc_node_bw;
4498         if (bw == ICE_SCHED_DFLT_BW)
4499                 status = ice_sched_set_node_bw_dflt_lmt(pi, tc_node, rl_type);
4500         else
4501                 status = ice_sched_set_node_bw_lmt(pi, tc_node, rl_type, bw);
4502         if (!status)
4503                 status = ice_sched_save_tc_node_bw(pi, tc, rl_type, bw);
4504
4505 exit_set_tc_node_bw:
4506         ice_release_lock(&pi->sched_lock);
4507         return status;
4508 }
4509
4510 /**
4511  * ice_cfg_tc_node_bw_lmt - configure TC node BW limit
4512  * @pi: port information structure
4513  * @tc: TC number
4514  * @rl_type: min or max
4515  * @bw: bandwidth in Kbps
4516  *
4517  * This function configures BW limit of TC node.
4518  * Note: The minimum guaranteed reservation is done via DCBX.
4519  */
4520 enum ice_status
4521 ice_cfg_tc_node_bw_lmt(struct ice_port_info *pi, u8 tc,
4522                        enum ice_rl_type rl_type, u32 bw)
4523 {
4524         return ice_sched_set_tc_node_bw_lmt(pi, tc, rl_type, bw);
4525 }
4526
4527 /**
4528  * ice_cfg_tc_node_bw_dflt_lmt - configure TC node BW default limit
4529  * @pi: port information structure
4530  * @tc: TC number
4531  * @rl_type: min or max
4532  *
4533  * This function configures BW default limit of TC node.
4534  */
4535 enum ice_status
4536 ice_cfg_tc_node_bw_dflt_lmt(struct ice_port_info *pi, u8 tc,
4537                             enum ice_rl_type rl_type)
4538 {
4539         return ice_sched_set_tc_node_bw_lmt(pi, tc, rl_type, ICE_SCHED_DFLT_BW);
4540 }
4541
4542 /**
4543  * ice_sched_save_tc_node_bw_alloc - save TC node's BW alloc information
4544  * @pi: port information structure
4545  * @tc: traffic class
4546  * @rl_type: rate limit type min or max
4547  * @bw_alloc: Bandwidth allocation information
4548  *
4549  * Save BW alloc information of VSI type node for post replay use.
4550  */
4551 static enum ice_status
4552 ice_sched_save_tc_node_bw_alloc(struct ice_port_info *pi, u8 tc,
4553                                 enum ice_rl_type rl_type, u16 bw_alloc)
4554 {
4555         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4556                 return ICE_ERR_PARAM;
4557         switch (rl_type) {
4558         case ICE_MIN_BW:
4559                 ice_set_clear_cir_bw_alloc(&pi->tc_node_bw_t_info[tc],
4560                                            bw_alloc);
4561                 break;
4562         case ICE_MAX_BW:
4563                 ice_set_clear_eir_bw_alloc(&pi->tc_node_bw_t_info[tc],
4564                                            bw_alloc);
4565                 break;
4566         default:
4567                 return ICE_ERR_PARAM;
4568         }
4569         return ICE_SUCCESS;
4570 }
4571
4572 /**
4573  * ice_sched_set_tc_node_bw_alloc - set TC node BW alloc
4574  * @pi: port information structure
4575  * @tc: TC number
4576  * @rl_type: min or max
4577  * @bw_alloc: bandwidth alloc
4578  *
4579  * This function configures bandwidth alloc of TC node, also saves the
4580  * changed settings for replay purpose, and return success if it succeeds
4581  * in modifying bandwidth alloc setting.
4582  */
4583 static enum ice_status
4584 ice_sched_set_tc_node_bw_alloc(struct ice_port_info *pi, u8 tc,
4585                                enum ice_rl_type rl_type, u8 bw_alloc)
4586 {
4587         enum ice_status status = ICE_ERR_PARAM;
4588         struct ice_sched_node *tc_node;
4589
4590         if (tc >= ICE_MAX_TRAFFIC_CLASS)
4591                 return status;
4592         ice_acquire_lock(&pi->sched_lock);
4593         tc_node = ice_sched_get_tc_node(pi, tc);
4594         if (!tc_node)
4595                 goto exit_set_tc_node_bw_alloc;
4596         status = ice_sched_cfg_node_bw_alloc(pi->hw, tc_node, rl_type,
4597                                              bw_alloc);
4598         if (status)
4599                 goto exit_set_tc_node_bw_alloc;
4600         status = ice_sched_save_tc_node_bw_alloc(pi, tc, rl_type, bw_alloc);
4601
4602 exit_set_tc_node_bw_alloc:
4603         ice_release_lock(&pi->sched_lock);
4604         return status;
4605 }
4606
4607 /**
4608  * ice_cfg_tc_node_bw_alloc - configure TC node BW alloc
4609  * @pi: port information structure
4610  * @tc: TC number
4611  * @rl_type: min or max
4612  * @bw_alloc: bandwidth alloc
4613  *
4614  * This function configures BW limit of TC node.
4615  * Note: The minimum guaranteed reservation is done via DCBX.
4616  */
4617 enum ice_status
4618 ice_cfg_tc_node_bw_alloc(struct ice_port_info *pi, u8 tc,
4619                          enum ice_rl_type rl_type, u8 bw_alloc)
4620 {
4621         return ice_sched_set_tc_node_bw_alloc(pi, tc, rl_type, bw_alloc);
4622 }
4623
4624 /**
4625  * ice_sched_set_agg_bw_dflt_lmt - set aggregator node's BW limit to default
4626  * @pi: port information structure
4627  * @vsi_handle: software VSI handle
4628  *
4629  * This function retrieves the aggregator ID based on VSI ID and TC,
4630  * and sets node's BW limit to default. This function needs to be
4631  * called with the scheduler lock held.
4632  */
4633 enum ice_status
4634 ice_sched_set_agg_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle)
4635 {
4636         struct ice_vsi_ctx *vsi_ctx;
4637         enum ice_status status = ICE_SUCCESS;
4638         u8 tc;
4639
4640         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4641                 return ICE_ERR_PARAM;
4642         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
4643         if (!vsi_ctx)
4644                 return ICE_ERR_PARAM;
4645
4646         ice_for_each_traffic_class(tc) {
4647                 struct ice_sched_node *node;
4648
4649                 node = vsi_ctx->sched.ag_node[tc];
4650                 if (!node)
4651                         continue;
4652
4653                 /* Set min profile to default */
4654                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, ICE_MIN_BW);
4655                 if (status)
4656                         break;
4657
4658                 /* Set max profile to default */
4659                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, ICE_MAX_BW);
4660                 if (status)
4661                         break;
4662
4663                 /* Remove shared profile, if there is one */
4664                 status = ice_sched_set_node_bw_dflt_lmt(pi, node,
4665                                                         ICE_SHARED_BW);
4666                 if (status)
4667                         break;
4668         }
4669
4670         return status;
4671 }
4672
4673 /**
4674  * ice_sched_get_node_by_id_type - get node from ID type
4675  * @pi: port information structure
4676  * @id: identifier
4677  * @agg_type: type of aggregator
4678  * @tc: traffic class
4679  *
4680  * This function returns node identified by ID of type aggregator, and
4681  * based on traffic class (TC). This function needs to be called with
4682  * the scheduler lock held.
4683  */
4684 static struct ice_sched_node *
4685 ice_sched_get_node_by_id_type(struct ice_port_info *pi, u32 id,
4686                               enum ice_agg_type agg_type, u8 tc)
4687 {
4688         struct ice_sched_node *node = NULL;
4689         struct ice_sched_node *child_node;
4690
4691         switch (agg_type) {
4692         case ICE_AGG_TYPE_VSI: {
4693                 struct ice_vsi_ctx *vsi_ctx;
4694                 u16 vsi_handle = (u16)id;
4695
4696                 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4697                         break;
4698                 /* Get sched_vsi_info */
4699                 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
4700                 if (!vsi_ctx)
4701                         break;
4702                 node = vsi_ctx->sched.vsi_node[tc];
4703                 break;
4704         }
4705
4706         case ICE_AGG_TYPE_AGG: {
4707                 struct ice_sched_node *tc_node;
4708
4709                 tc_node = ice_sched_get_tc_node(pi, tc);
4710                 if (tc_node)
4711                         node = ice_sched_get_agg_node(pi, tc_node, id);
4712                 break;
4713         }
4714
4715         case ICE_AGG_TYPE_Q:
4716                 /* The current implementation allows single queue to modify */
4717                 node = ice_sched_get_node(pi, id);
4718                 break;
4719
4720         case ICE_AGG_TYPE_QG:
4721                 /* The current implementation allows single qg to modify */
4722                 child_node = ice_sched_get_node(pi, id);
4723                 if (!child_node)
4724                         break;
4725                 node = child_node->parent;
4726                 break;
4727
4728         default:
4729                 break;
4730         }
4731
4732         return node;
4733 }
4734
4735 /**
4736  * ice_sched_set_node_bw_lmt_per_tc - set node BW limit per TC
4737  * @pi: port information structure
4738  * @id: ID (software VSI handle or AGG ID)
4739  * @agg_type: aggregator type (VSI or AGG type node)
4740  * @tc: traffic class
4741  * @rl_type: min or max
4742  * @bw: bandwidth in Kbps
4743  *
4744  * This function sets BW limit of VSI or Aggregator scheduling node
4745  * based on TC information from passed in argument BW.
4746  */
4747 enum ice_status
4748 ice_sched_set_node_bw_lmt_per_tc(struct ice_port_info *pi, u32 id,
4749                                  enum ice_agg_type agg_type, u8 tc,
4750                                  enum ice_rl_type rl_type, u32 bw)
4751 {
4752         enum ice_status status = ICE_ERR_PARAM;
4753         struct ice_sched_node *node;
4754
4755         if (!pi)
4756                 return status;
4757
4758         if (rl_type == ICE_UNKNOWN_BW)
4759                 return status;
4760
4761         ice_acquire_lock(&pi->sched_lock);
4762         node = ice_sched_get_node_by_id_type(pi, id, agg_type, tc);
4763         if (!node) {
4764                 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong id, agg type, or tc\n");
4765                 goto exit_set_node_bw_lmt_per_tc;
4766         }
4767         if (bw == ICE_SCHED_DFLT_BW)
4768                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
4769         else
4770                 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
4771
4772 exit_set_node_bw_lmt_per_tc:
4773         ice_release_lock(&pi->sched_lock);
4774         return status;
4775 }
4776
4777 /**
4778  * ice_sched_validate_vsi_srl_node - validate VSI SRL node
4779  * @pi: port information structure
4780  * @vsi_handle: software VSI handle
4781  *
4782  * This function validates SRL node of the VSI node if available SRL layer is
4783  * different than the VSI node layer on all TC(s).This function needs to be
4784  * called with scheduler lock held.
4785  */
4786 static enum ice_status
4787 ice_sched_validate_vsi_srl_node(struct ice_port_info *pi, u16 vsi_handle)
4788 {
4789         u8 sel_layer = ICE_SCHED_INVAL_LAYER_NUM;
4790         u8 tc;
4791
4792         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4793                 return ICE_ERR_PARAM;
4794
4795         /* Return success if no nodes are present across TC */
4796         ice_for_each_traffic_class(tc) {
4797                 struct ice_sched_node *tc_node, *vsi_node;
4798                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4799                 enum ice_status status;
4800
4801                 tc_node = ice_sched_get_tc_node(pi, tc);
4802                 if (!tc_node)
4803                         continue;
4804
4805                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
4806                 if (!vsi_node)
4807                         continue;
4808
4809                 /* SRL bandwidth layer selection */
4810                 if (sel_layer == ICE_SCHED_INVAL_LAYER_NUM) {
4811                         u8 node_layer = vsi_node->tx_sched_layer;
4812                         u8 layer_num;
4813
4814                         layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
4815                                                                 node_layer);
4816                         if (layer_num >= pi->hw->num_tx_sched_layers)
4817                                 return ICE_ERR_PARAM;
4818                         sel_layer = layer_num;
4819                 }
4820
4821                 status = ice_sched_validate_srl_node(vsi_node, sel_layer);
4822                 if (status)
4823                         return status;
4824         }
4825         return ICE_SUCCESS;
4826 }
4827
4828 /**
4829  * ice_sched_set_vsi_bw_shared_lmt - set VSI BW shared limit
4830  * @pi: port information structure
4831  * @vsi_handle: software VSI handle
4832  * @bw: bandwidth in Kbps
4833  *
4834  * This function Configures shared rate limiter(SRL) of all VSI type nodes
4835  * across all traffic classes for VSI matching handle. When BW value of
4836  * ICE_SCHED_DFLT_BW is passed, it removes the SRL from the node.
4837  */
4838 enum ice_status
4839 ice_sched_set_vsi_bw_shared_lmt(struct ice_port_info *pi, u16 vsi_handle,
4840                                 u32 bw)
4841 {
4842         enum ice_status status = ICE_SUCCESS;
4843         u8 tc;
4844
4845         if (!pi)
4846                 return ICE_ERR_PARAM;
4847
4848         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4849                 return ICE_ERR_PARAM;
4850
4851         ice_acquire_lock(&pi->sched_lock);
4852         status = ice_sched_validate_vsi_srl_node(pi, vsi_handle);
4853         if (status)
4854                 goto exit_set_vsi_bw_shared_lmt;
4855         /* Return success if no nodes are present across TC */
4856         ice_for_each_traffic_class(tc) {
4857                 struct ice_sched_node *tc_node, *vsi_node;
4858                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4859
4860                 tc_node = ice_sched_get_tc_node(pi, tc);
4861                 if (!tc_node)
4862                         continue;
4863
4864                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
4865                 if (!vsi_node)
4866                         continue;
4867
4868                 if (bw == ICE_SCHED_DFLT_BW)
4869                         /* It removes existing SRL from the node */
4870                         status = ice_sched_set_node_bw_dflt_lmt(pi, vsi_node,
4871                                                                 rl_type);
4872                 else
4873                         status = ice_sched_set_node_bw_lmt(pi, vsi_node,
4874                                                            rl_type, bw);
4875                 if (status)
4876                         break;
4877                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type, bw);
4878                 if (status)
4879                         break;
4880         }
4881
4882 exit_set_vsi_bw_shared_lmt:
4883         ice_release_lock(&pi->sched_lock);
4884         return status;
4885 }
4886
4887 /**
4888  * ice_sched_validate_agg_srl_node - validate AGG SRL node
4889  * @pi: port information structure
4890  * @agg_id: aggregator ID
4891  *
4892  * This function validates SRL node of the AGG node if available SRL layer is
4893  * different than the AGG node layer on all TC(s).This function needs to be
4894  * called with scheduler lock held.
4895  */
4896 static enum ice_status
4897 ice_sched_validate_agg_srl_node(struct ice_port_info *pi, u32 agg_id)
4898 {
4899         u8 sel_layer = ICE_SCHED_INVAL_LAYER_NUM;
4900         struct ice_sched_agg_info *agg_info;
4901         bool agg_id_present = false;
4902         enum ice_status status = ICE_SUCCESS;
4903         u8 tc;
4904
4905         LIST_FOR_EACH_ENTRY(agg_info, &pi->hw->agg_list, ice_sched_agg_info,
4906                             list_entry)
4907                 if (agg_info->agg_id == agg_id) {
4908                         agg_id_present = true;
4909                         break;
4910                 }
4911         if (!agg_id_present)
4912                 return ICE_ERR_PARAM;
4913         /* Return success if no nodes are present across TC */
4914         ice_for_each_traffic_class(tc) {
4915                 struct ice_sched_node *tc_node, *agg_node;
4916                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4917
4918                 tc_node = ice_sched_get_tc_node(pi, tc);
4919                 if (!tc_node)
4920                         continue;
4921
4922                 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
4923                 if (!agg_node)
4924                         continue;
4925                 /* SRL bandwidth layer selection */
4926                 if (sel_layer == ICE_SCHED_INVAL_LAYER_NUM) {
4927                         u8 node_layer = agg_node->tx_sched_layer;
4928                         u8 layer_num;
4929
4930                         layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
4931                                                                 node_layer);
4932                         if (layer_num >= pi->hw->num_tx_sched_layers)
4933                                 return ICE_ERR_PARAM;
4934                         sel_layer = layer_num;
4935                 }
4936
4937                 status = ice_sched_validate_srl_node(agg_node, sel_layer);
4938                 if (status)
4939                         break;
4940         }
4941         return status;
4942 }
4943
4944 /**
4945  * ice_sched_set_agg_bw_shared_lmt - set aggregator BW shared limit
4946  * @pi: port information structure
4947  * @agg_id: aggregator ID
4948  * @bw: bandwidth in Kbps
4949  *
4950  * This function configures the shared rate limiter(SRL) of all aggregator type
4951  * nodes across all traffic classes for aggregator matching agg_id. When
4952  * BW value of ICE_SCHED_DFLT_BW is passed, it removes SRL from the
4953  * node(s).
4954  */
4955 enum ice_status
4956 ice_sched_set_agg_bw_shared_lmt(struct ice_port_info *pi, u32 agg_id, u32 bw)
4957 {
4958         struct ice_sched_agg_info *agg_info;
4959         struct ice_sched_agg_info *tmp;
4960         bool agg_id_present = false;
4961         enum ice_status status = ICE_SUCCESS;
4962         u8 tc;
4963
4964         if (!pi)
4965                 return ICE_ERR_PARAM;
4966
4967         ice_acquire_lock(&pi->sched_lock);
4968         status = ice_sched_validate_agg_srl_node(pi, agg_id);
4969         if (status)
4970                 goto exit_agg_bw_shared_lmt;
4971
4972         LIST_FOR_EACH_ENTRY_SAFE(agg_info, tmp, &pi->hw->agg_list,
4973                                  ice_sched_agg_info, list_entry)
4974                 if (agg_info->agg_id == agg_id) {
4975                         agg_id_present = true;
4976                         break;
4977                 }
4978
4979         if (!agg_id_present) {
4980                 status = ICE_ERR_PARAM;
4981                 goto exit_agg_bw_shared_lmt;
4982         }
4983
4984         /* Return success if no nodes are present across TC */
4985         ice_for_each_traffic_class(tc) {
4986                 enum ice_rl_type rl_type = ICE_SHARED_BW;
4987                 struct ice_sched_node *tc_node, *agg_node;
4988
4989                 tc_node = ice_sched_get_tc_node(pi, tc);
4990                 if (!tc_node)
4991                         continue;
4992
4993                 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
4994                 if (!agg_node)
4995                         continue;
4996
4997                 if (bw == ICE_SCHED_DFLT_BW)
4998                         /* It removes existing SRL from the node */
4999                         status = ice_sched_set_node_bw_dflt_lmt(pi, agg_node,
5000                                                                 rl_type);
5001                 else
5002                         status = ice_sched_set_node_bw_lmt(pi, agg_node,
5003                                                            rl_type, bw);
5004                 if (status)
5005                         break;
5006                 status = ice_sched_save_agg_bw(pi, agg_id, tc, rl_type, bw);
5007                 if (status)
5008                         break;
5009         }
5010
5011 exit_agg_bw_shared_lmt:
5012         ice_release_lock(&pi->sched_lock);
5013         return status;
5014 }
5015
5016 /**
5017  * ice_sched_cfg_sibl_node_prio - configure node sibling priority
5018  * @pi: port information structure
5019  * @node: sched node to configure
5020  * @priority: sibling priority
5021  *
5022  * This function configures node element's sibling priority only. This
5023  * function needs to be called with scheduler lock held.
5024  */
5025 enum ice_status
5026 ice_sched_cfg_sibl_node_prio(struct ice_port_info *pi,
5027                              struct ice_sched_node *node, u8 priority)
5028 {
5029         struct ice_aqc_txsched_elem_data buf;
5030         struct ice_aqc_txsched_elem *data;
5031         struct ice_hw *hw = pi->hw;
5032         enum ice_status status;
5033
5034         if (!hw)
5035                 return ICE_ERR_PARAM;
5036         buf = node->info;
5037         data = &buf.data;
5038         data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
5039         priority = (priority << ICE_AQC_ELEM_GENERIC_PRIO_S) &
5040                    ICE_AQC_ELEM_GENERIC_PRIO_M;
5041         data->generic &= ~ICE_AQC_ELEM_GENERIC_PRIO_M;
5042         data->generic |= priority;
5043
5044         /* Configure element */
5045         status = ice_sched_update_elem(hw, node, &buf);
5046         return status;
5047 }
5048
5049 /**
5050  * ice_cfg_rl_burst_size - Set burst size value
5051  * @hw: pointer to the HW struct
5052  * @bytes: burst size in bytes
5053  *
5054  * This function configures/set the burst size to requested new value. The new
5055  * burst size value is used for future rate limit calls. It doesn't change the
5056  * existing or previously created RL profiles.
5057  */
5058 enum ice_status ice_cfg_rl_burst_size(struct ice_hw *hw, u32 bytes)
5059 {
5060         u16 burst_size_to_prog;
5061
5062         if (bytes < ICE_MIN_BURST_SIZE_ALLOWED ||
5063             bytes > ICE_MAX_BURST_SIZE_ALLOWED)
5064                 return ICE_ERR_PARAM;
5065         if (ice_round_to_num(bytes, 64) <=
5066             ICE_MAX_BURST_SIZE_64_BYTE_GRANULARITY) {
5067                 /* 64 byte granularity case */
5068                 /* Disable MSB granularity bit */
5069                 burst_size_to_prog = ICE_64_BYTE_GRANULARITY;
5070                 /* round number to nearest 64 byte granularity */
5071                 bytes = ice_round_to_num(bytes, 64);
5072                 /* The value is in 64 byte chunks */
5073                 burst_size_to_prog |= (u16)(bytes / 64);
5074         } else {
5075                 /* k bytes granularity case */
5076                 /* Enable MSB granularity bit */
5077                 burst_size_to_prog = ICE_KBYTE_GRANULARITY;
5078                 /* round number to nearest 1024 granularity */
5079                 bytes = ice_round_to_num(bytes, 1024);
5080                 /* check rounding doesn't go beyond allowed */
5081                 if (bytes > ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY)
5082                         bytes = ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY;
5083                 /* The value is in k bytes */
5084                 burst_size_to_prog |= (u16)(bytes / 1024);
5085         }
5086         hw->max_burst_size = burst_size_to_prog;
5087         return ICE_SUCCESS;
5088 }
5089
5090 /*
5091  * ice_sched_replay_node_prio - re-configure node priority
5092  * @hw: pointer to the HW struct
5093  * @node: sched node to configure
5094  * @priority: priority value
5095  *
5096  * This function configures node element's priority value. It
5097  * needs to be called with scheduler lock held.
5098  */
5099 static enum ice_status
5100 ice_sched_replay_node_prio(struct ice_hw *hw, struct ice_sched_node *node,
5101                            u8 priority)
5102 {
5103         struct ice_aqc_txsched_elem_data buf;
5104         struct ice_aqc_txsched_elem *data;
5105         enum ice_status status;
5106
5107         buf = node->info;
5108         data = &buf.data;
5109         data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
5110         data->generic = priority;
5111
5112         /* Configure element */
5113         status = ice_sched_update_elem(hw, node, &buf);
5114         return status;
5115 }
5116
5117 /**
5118  * ice_sched_replay_node_bw - replay node(s) BW
5119  * @hw: pointer to the HW struct
5120  * @node: sched node to configure
5121  * @bw_t_info: BW type information
5122  *
5123  * This function restores node's BW from bw_t_info. The caller needs
5124  * to hold the scheduler lock.
5125  */
5126 static enum ice_status
5127 ice_sched_replay_node_bw(struct ice_hw *hw, struct ice_sched_node *node,
5128                          struct ice_bw_type_info *bw_t_info)
5129 {
5130         struct ice_port_info *pi = hw->port_info;
5131         enum ice_status status = ICE_ERR_PARAM;
5132         u16 bw_alloc;
5133
5134         if (!node)
5135                 return status;
5136         if (!ice_is_any_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CNT))
5137                 return ICE_SUCCESS;
5138         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_PRIO)) {
5139                 status = ice_sched_replay_node_prio(hw, node,
5140                                                     bw_t_info->generic);
5141                 if (status)
5142                         return status;
5143         }
5144         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CIR)) {
5145                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW,
5146                                                    bw_t_info->cir_bw.bw);
5147                 if (status)
5148                         return status;
5149         }
5150         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CIR_WT)) {
5151                 bw_alloc = bw_t_info->cir_bw.bw_alloc;
5152                 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MIN_BW,
5153                                                      bw_alloc);
5154                 if (status)
5155                         return status;
5156         }
5157         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_EIR)) {
5158                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW,
5159                                                    bw_t_info->eir_bw.bw);
5160                 if (status)
5161                         return status;
5162         }
5163         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_EIR_WT)) {
5164                 bw_alloc = bw_t_info->eir_bw.bw_alloc;
5165                 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MAX_BW,
5166                                                      bw_alloc);
5167                 if (status)
5168                         return status;
5169         }
5170         if (ice_is_bit_set(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_SHARED))
5171                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_SHARED_BW,
5172                                                    bw_t_info->shared_bw);
5173         return status;
5174 }
5175
5176 /**
5177  * ice_sched_replay_agg_bw - replay aggregator node(s) BW
5178  * @hw: pointer to the HW struct
5179  * @agg_info: aggregator data structure
5180  *
5181  * This function re-creates aggregator type nodes. The caller needs to hold
5182  * the scheduler lock.
5183  */
5184 static enum ice_status
5185 ice_sched_replay_agg_bw(struct ice_hw *hw, struct ice_sched_agg_info *agg_info)
5186 {
5187         struct ice_sched_node *tc_node, *agg_node;
5188         enum ice_status status = ICE_SUCCESS;
5189         u8 tc;
5190
5191         if (!agg_info)
5192                 return ICE_ERR_PARAM;
5193         ice_for_each_traffic_class(tc) {
5194                 if (!ice_is_any_bit_set(agg_info->bw_t_info[tc].bw_t_bitmap,
5195                                         ICE_BW_TYPE_CNT))
5196                         continue;
5197                 tc_node = ice_sched_get_tc_node(hw->port_info, tc);
5198                 if (!tc_node) {
5199                         status = ICE_ERR_PARAM;
5200                         break;
5201                 }
5202                 agg_node = ice_sched_get_agg_node(hw->port_info, tc_node,
5203                                                   agg_info->agg_id);
5204                 if (!agg_node) {
5205                         status = ICE_ERR_PARAM;
5206                         break;
5207                 }
5208                 status = ice_sched_replay_node_bw(hw, agg_node,
5209                                                   &agg_info->bw_t_info[tc]);
5210                 if (status)
5211                         break;
5212         }
5213         return status;
5214 }
5215
5216 /**
5217  * ice_sched_get_ena_tc_bitmap - get enabled TC bitmap
5218  * @pi: port info struct
5219  * @tc_bitmap: 8 bits TC bitmap to check
5220  * @ena_tc_bitmap: 8 bits enabled TC bitmap to return
5221  *
5222  * This function returns enabled TC bitmap in variable ena_tc_bitmap. Some TCs
5223  * may be missing, it returns enabled TCs. This function needs to be called with
5224  * scheduler lock held.
5225  */
5226 static void
5227 ice_sched_get_ena_tc_bitmap(struct ice_port_info *pi, ice_bitmap_t *tc_bitmap,
5228                             ice_bitmap_t *ena_tc_bitmap)
5229 {
5230         u8 tc;
5231
5232         /* Some TC(s) may be missing after reset, adjust for replay */
5233         ice_for_each_traffic_class(tc)
5234                 if (ice_is_tc_ena(*tc_bitmap, tc) &&
5235                     (ice_sched_get_tc_node(pi, tc)))
5236                         ice_set_bit(tc, ena_tc_bitmap);
5237 }
5238
5239 /**
5240  * ice_sched_replay_agg - recreate aggregator node(s)
5241  * @hw: pointer to the HW struct
5242  *
5243  * This function recreate aggregator type nodes which are not replayed earlier.
5244  * It also replay aggregator BW information. These aggregator nodes are not
5245  * associated with VSI type node yet.
5246  */
5247 void ice_sched_replay_agg(struct ice_hw *hw)
5248 {
5249         struct ice_port_info *pi = hw->port_info;
5250         struct ice_sched_agg_info *agg_info;
5251
5252         ice_acquire_lock(&pi->sched_lock);
5253         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
5254                             list_entry) {
5255                 /* replay aggregator (re-create aggregator node) */
5256                 if (!ice_cmp_bitmap(agg_info->tc_bitmap,
5257                                     agg_info->replay_tc_bitmap,
5258                                     ICE_MAX_TRAFFIC_CLASS)) {
5259                         ice_declare_bitmap(replay_bitmap,
5260                                            ICE_MAX_TRAFFIC_CLASS);
5261                         enum ice_status status;
5262
5263                         ice_zero_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5264                         ice_sched_get_ena_tc_bitmap(pi,
5265                                                     agg_info->replay_tc_bitmap,
5266                                                     replay_bitmap);
5267                         status = ice_sched_cfg_agg(hw->port_info,
5268                                                    agg_info->agg_id,
5269                                                    ICE_AGG_TYPE_AGG,
5270                                                    replay_bitmap);
5271                         if (status) {
5272                                 ice_info(hw, "Replay agg id[%d] failed\n",
5273                                          agg_info->agg_id);
5274                                 /* Move on to next one */
5275                                 continue;
5276                         }
5277                         /* Replay aggregator node BW (restore aggregator BW) */
5278                         status = ice_sched_replay_agg_bw(hw, agg_info);
5279                         if (status)
5280                                 ice_info(hw, "Replay agg bw [id=%d] failed\n",
5281                                          agg_info->agg_id);
5282                 }
5283         }
5284         ice_release_lock(&pi->sched_lock);
5285 }
5286
5287 /**
5288  * ice_sched_replay_agg_vsi_preinit - Agg/VSI replay pre initialization
5289  * @hw: pointer to the HW struct
5290  *
5291  * This function initialize aggregator(s) TC bitmap to zero. A required
5292  * preinit step for replaying aggregators.
5293  */
5294 void ice_sched_replay_agg_vsi_preinit(struct ice_hw *hw)
5295 {
5296         struct ice_port_info *pi = hw->port_info;
5297         struct ice_sched_agg_info *agg_info;
5298
5299         ice_acquire_lock(&pi->sched_lock);
5300         LIST_FOR_EACH_ENTRY(agg_info, &hw->agg_list, ice_sched_agg_info,
5301                             list_entry) {
5302                 struct ice_sched_agg_vsi_info *agg_vsi_info;
5303
5304                 agg_info->tc_bitmap[0] = 0;
5305                 LIST_FOR_EACH_ENTRY(agg_vsi_info, &agg_info->agg_vsi_list,
5306                                     ice_sched_agg_vsi_info, list_entry)
5307                         agg_vsi_info->tc_bitmap[0] = 0;
5308         }
5309         ice_release_lock(&pi->sched_lock);
5310 }
5311
5312 /**
5313  * ice_sched_replay_tc_node_bw - replay TC node(s) BW
5314  * @pi: port information structure
5315  *
5316  * This function replay TC nodes.
5317  */
5318 enum ice_status
5319 ice_sched_replay_tc_node_bw(struct ice_port_info *pi)
5320 {
5321         enum ice_status status = ICE_SUCCESS;
5322         u8 tc;
5323
5324         if (!pi->hw)
5325                 return ICE_ERR_PARAM;
5326         ice_acquire_lock(&pi->sched_lock);
5327         ice_for_each_traffic_class(tc) {
5328                 struct ice_sched_node *tc_node;
5329
5330                 tc_node = ice_sched_get_tc_node(pi, tc);
5331                 if (!tc_node)
5332                         continue; /* TC not present */
5333                 status = ice_sched_replay_node_bw(pi->hw, tc_node,
5334                                                   &pi->tc_node_bw_t_info[tc]);
5335                 if (status)
5336                         break;
5337         }
5338         ice_release_lock(&pi->sched_lock);
5339         return status;
5340 }
5341
5342 /**
5343  * ice_sched_replay_vsi_bw - replay VSI type node(s) BW
5344  * @hw: pointer to the HW struct
5345  * @vsi_handle: software VSI handle
5346  * @tc_bitmap: 8 bits TC bitmap
5347  *
5348  * This function replays VSI type nodes bandwidth. This function needs to be
5349  * called with scheduler lock held.
5350  */
5351 static enum ice_status
5352 ice_sched_replay_vsi_bw(struct ice_hw *hw, u16 vsi_handle,
5353                         ice_bitmap_t *tc_bitmap)
5354 {
5355         struct ice_sched_node *vsi_node, *tc_node;
5356         struct ice_port_info *pi = hw->port_info;
5357         struct ice_bw_type_info *bw_t_info;
5358         struct ice_vsi_ctx *vsi_ctx;
5359         enum ice_status status = ICE_SUCCESS;
5360         u8 tc;
5361
5362         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
5363         if (!vsi_ctx)
5364                 return ICE_ERR_PARAM;
5365         ice_for_each_traffic_class(tc) {
5366                 if (!ice_is_tc_ena(*tc_bitmap, tc))
5367                         continue;
5368                 tc_node = ice_sched_get_tc_node(pi, tc);
5369                 if (!tc_node)
5370                         continue;
5371                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
5372                 if (!vsi_node)
5373                         continue;
5374                 bw_t_info = &vsi_ctx->sched.bw_t_info[tc];
5375                 status = ice_sched_replay_node_bw(hw, vsi_node, bw_t_info);
5376                 if (status)
5377                         break;
5378         }
5379         return status;
5380 }
5381
5382 /**
5383  * ice_sched_replay_vsi_agg - replay aggregator & VSI to aggregator node(s)
5384  * @hw: pointer to the HW struct
5385  * @vsi_handle: software VSI handle
5386  *
5387  * This function replays aggregator node, VSI to aggregator type nodes, and
5388  * their node bandwidth information. This function needs to be called with
5389  * scheduler lock held.
5390  */
5391 static enum ice_status
5392 ice_sched_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
5393 {
5394         ice_declare_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5395         struct ice_sched_agg_vsi_info *agg_vsi_info;
5396         struct ice_port_info *pi = hw->port_info;
5397         struct ice_sched_agg_info *agg_info;
5398         enum ice_status status;
5399
5400         ice_zero_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5401         if (!ice_is_vsi_valid(hw, vsi_handle))
5402                 return ICE_ERR_PARAM;
5403         agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
5404         if (!agg_info)
5405                 return ICE_SUCCESS; /* Not present in list - default Agg case */
5406         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
5407         if (!agg_vsi_info)
5408                 return ICE_SUCCESS; /* Not present in list - default Agg case */
5409         ice_sched_get_ena_tc_bitmap(pi, agg_info->replay_tc_bitmap,
5410                                     replay_bitmap);
5411         /* Replay aggregator node associated to vsi_handle */
5412         status = ice_sched_cfg_agg(hw->port_info, agg_info->agg_id,
5413                                    ICE_AGG_TYPE_AGG, replay_bitmap);
5414         if (status)
5415                 return status;
5416         /* Replay aggregator node BW (restore aggregator BW) */
5417         status = ice_sched_replay_agg_bw(hw, agg_info);
5418         if (status)
5419                 return status;
5420
5421         ice_zero_bitmap(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
5422         ice_sched_get_ena_tc_bitmap(pi, agg_vsi_info->replay_tc_bitmap,
5423                                     replay_bitmap);
5424         /* Move this VSI (vsi_handle) to above aggregator */
5425         status = ice_sched_assoc_vsi_to_agg(pi, agg_info->agg_id, vsi_handle,
5426                                             replay_bitmap);
5427         if (status)
5428                 return status;
5429         /* Replay VSI BW (restore VSI BW) */
5430         return ice_sched_replay_vsi_bw(hw, vsi_handle,
5431                                        agg_vsi_info->tc_bitmap);
5432 }
5433
5434 /**
5435  * ice_replay_vsi_agg - replay VSI to aggregator node
5436  * @hw: pointer to the HW struct
5437  * @vsi_handle: software VSI handle
5438  *
5439  * This function replays association of VSI to aggregator type nodes, and
5440  * node bandwidth information.
5441  */
5442 enum ice_status
5443 ice_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
5444 {
5445         struct ice_port_info *pi = hw->port_info;
5446         enum ice_status status;
5447
5448         ice_acquire_lock(&pi->sched_lock);
5449         status = ice_sched_replay_vsi_agg(hw, vsi_handle);
5450         ice_release_lock(&pi->sched_lock);
5451         return status;
5452 }
5453
5454 /**
5455  * ice_sched_replay_q_bw - replay queue type node BW
5456  * @pi: port information structure
5457  * @q_ctx: queue context structure
5458  *
5459  * This function replays queue type node bandwidth. This function needs to be
5460  * called with scheduler lock held.
5461  */
5462 enum ice_status
5463 ice_sched_replay_q_bw(struct ice_port_info *pi, struct ice_q_ctx *q_ctx)
5464 {
5465         struct ice_sched_node *q_node;
5466
5467         /* Following also checks the presence of node in tree */
5468         q_node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
5469         if (!q_node)
5470                 return ICE_ERR_PARAM;
5471         return ice_sched_replay_node_bw(pi->hw, q_node, &q_ctx->bw_t_info);
5472 }