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