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