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