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