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