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