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