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