]> Git Repo - J-linux.git/blob - drivers/net/ethernet/intel/ice/ice_sched.c
Merge tag 'trace-tools-v6.5' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[J-linux.git] / drivers / net / ethernet / intel / ice / ice_sched.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 #include <net/devlink.h>
5 #include "ice_sched.h"
6
7 /**
8  * ice_sched_add_root_node - Insert the Tx scheduler root node in SW DB
9  * @pi: port information structure
10  * @info: Scheduler element information from firmware
11  *
12  * This function inserts the root node of the scheduling tree topology
13  * to the SW DB.
14  */
15 static int
16 ice_sched_add_root_node(struct ice_port_info *pi,
17                         struct ice_aqc_txsched_elem_data *info)
18 {
19         struct ice_sched_node *root;
20         struct ice_hw *hw;
21
22         if (!pi)
23                 return -EINVAL;
24
25         hw = pi->hw;
26
27         root = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*root), GFP_KERNEL);
28         if (!root)
29                 return -ENOMEM;
30
31         /* coverity[suspicious_sizeof] */
32         root->children = devm_kcalloc(ice_hw_to_dev(hw), hw->max_children[0],
33                                       sizeof(*root), GFP_KERNEL);
34         if (!root->children) {
35                 devm_kfree(ice_hw_to_dev(hw), root);
36                 return -ENOMEM;
37         }
38
39         memcpy(&root->info, info, sizeof(*info));
40         pi->root = root;
41         return 0;
42 }
43
44 /**
45  * ice_sched_find_node_by_teid - Find the Tx scheduler node in SW DB
46  * @start_node: pointer to the starting ice_sched_node struct in a sub-tree
47  * @teid: node TEID to search
48  *
49  * This function searches for a node matching the TEID in the scheduling tree
50  * from the SW DB. The search is recursive and is restricted by the number of
51  * layers it has searched through; stopping at the max supported layer.
52  *
53  * This function needs to be called when holding the port_info->sched_lock
54  */
55 struct ice_sched_node *
56 ice_sched_find_node_by_teid(struct ice_sched_node *start_node, u32 teid)
57 {
58         u16 i;
59
60         /* The TEID is same as that of the start_node */
61         if (ICE_TXSCHED_GET_NODE_TEID(start_node) == teid)
62                 return start_node;
63
64         /* The node has no children or is at the max layer */
65         if (!start_node->num_children ||
66             start_node->tx_sched_layer >= ICE_AQC_TOPO_MAX_LEVEL_NUM ||
67             start_node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF)
68                 return NULL;
69
70         /* Check if TEID matches to any of the children nodes */
71         for (i = 0; i < start_node->num_children; i++)
72                 if (ICE_TXSCHED_GET_NODE_TEID(start_node->children[i]) == teid)
73                         return start_node->children[i];
74
75         /* Search within each child's sub-tree */
76         for (i = 0; i < start_node->num_children; i++) {
77                 struct ice_sched_node *tmp;
78
79                 tmp = ice_sched_find_node_by_teid(start_node->children[i],
80                                                   teid);
81                 if (tmp)
82                         return tmp;
83         }
84
85         return NULL;
86 }
87
88 /**
89  * ice_aqc_send_sched_elem_cmd - send scheduling elements cmd
90  * @hw: pointer to the HW struct
91  * @cmd_opc: cmd opcode
92  * @elems_req: number of elements to request
93  * @buf: pointer to buffer
94  * @buf_size: buffer size in bytes
95  * @elems_resp: returns total number of elements response
96  * @cd: pointer to command details structure or NULL
97  *
98  * This function sends a scheduling elements cmd (cmd_opc)
99  */
100 static int
101 ice_aqc_send_sched_elem_cmd(struct ice_hw *hw, enum ice_adminq_opc cmd_opc,
102                             u16 elems_req, void *buf, u16 buf_size,
103                             u16 *elems_resp, struct ice_sq_cd *cd)
104 {
105         struct ice_aqc_sched_elem_cmd *cmd;
106         struct ice_aq_desc desc;
107         int status;
108
109         cmd = &desc.params.sched_elem_cmd;
110         ice_fill_dflt_direct_cmd_desc(&desc, cmd_opc);
111         cmd->num_elem_req = cpu_to_le16(elems_req);
112         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
113         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
114         if (!status && elems_resp)
115                 *elems_resp = le16_to_cpu(cmd->num_elem_resp);
116
117         return status;
118 }
119
120 /**
121  * ice_aq_query_sched_elems - query scheduler elements
122  * @hw: pointer to the HW struct
123  * @elems_req: number of elements to query
124  * @buf: pointer to buffer
125  * @buf_size: buffer size in bytes
126  * @elems_ret: returns total number of elements returned
127  * @cd: pointer to command details structure or NULL
128  *
129  * Query scheduling elements (0x0404)
130  */
131 int
132 ice_aq_query_sched_elems(struct ice_hw *hw, u16 elems_req,
133                          struct ice_aqc_txsched_elem_data *buf, u16 buf_size,
134                          u16 *elems_ret, struct ice_sq_cd *cd)
135 {
136         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_get_sched_elems,
137                                            elems_req, (void *)buf, buf_size,
138                                            elems_ret, cd);
139 }
140
141 /**
142  * ice_sched_add_node - Insert the Tx scheduler node in SW DB
143  * @pi: port information structure
144  * @layer: Scheduler layer of the node
145  * @info: Scheduler element information from firmware
146  * @prealloc_node: preallocated ice_sched_node struct for SW DB
147  *
148  * This function inserts a scheduler node to the SW DB.
149  */
150 int
151 ice_sched_add_node(struct ice_port_info *pi, u8 layer,
152                    struct ice_aqc_txsched_elem_data *info,
153                    struct ice_sched_node *prealloc_node)
154 {
155         struct ice_aqc_txsched_elem_data elem;
156         struct ice_sched_node *parent;
157         struct ice_sched_node *node;
158         struct ice_hw *hw;
159         int status;
160
161         if (!pi)
162                 return -EINVAL;
163
164         hw = pi->hw;
165
166         /* A valid parent node should be there */
167         parent = ice_sched_find_node_by_teid(pi->root,
168                                              le32_to_cpu(info->parent_teid));
169         if (!parent) {
170                 ice_debug(hw, ICE_DBG_SCHED, "Parent Node not found for parent_teid=0x%x\n",
171                           le32_to_cpu(info->parent_teid));
172                 return -EINVAL;
173         }
174
175         /* query the current node information from FW before adding 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
182         if (prealloc_node)
183                 node = prealloc_node;
184         else
185                 node = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*node), GFP_KERNEL);
186         if (!node)
187                 return -ENOMEM;
188         if (hw->max_children[layer]) {
189                 /* coverity[suspicious_sizeof] */
190                 node->children = devm_kcalloc(ice_hw_to_dev(hw),
191                                               hw->max_children[layer],
192                                               sizeof(*node), GFP_KERNEL);
193                 if (!node->children) {
194                         devm_kfree(ice_hw_to_dev(hw), node);
195                         return -ENOMEM;
196                 }
197         }
198
199         node->in_use = true;
200         node->parent = parent;
201         node->tx_sched_layer = layer;
202         parent->children[parent->num_children++] = node;
203         node->info = elem;
204         return 0;
205 }
206
207 /**
208  * ice_aq_delete_sched_elems - delete scheduler elements
209  * @hw: pointer to the HW struct
210  * @grps_req: number of groups to delete
211  * @buf: pointer to buffer
212  * @buf_size: buffer size in bytes
213  * @grps_del: returns total number of elements deleted
214  * @cd: pointer to command details structure or NULL
215  *
216  * Delete scheduling elements (0x040F)
217  */
218 static int
219 ice_aq_delete_sched_elems(struct ice_hw *hw, u16 grps_req,
220                           struct ice_aqc_delete_elem *buf, u16 buf_size,
221                           u16 *grps_del, struct ice_sq_cd *cd)
222 {
223         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_delete_sched_elems,
224                                            grps_req, (void *)buf, buf_size,
225                                            grps_del, cd);
226 }
227
228 /**
229  * ice_sched_remove_elems - remove nodes from HW
230  * @hw: pointer to the HW struct
231  * @parent: pointer to the parent node
232  * @num_nodes: number of nodes
233  * @node_teids: array of node teids to be deleted
234  *
235  * This function remove nodes from HW
236  */
237 static int
238 ice_sched_remove_elems(struct ice_hw *hw, struct ice_sched_node *parent,
239                        u16 num_nodes, u32 *node_teids)
240 {
241         struct ice_aqc_delete_elem *buf;
242         u16 i, num_groups_removed = 0;
243         u16 buf_size;
244         int status;
245
246         buf_size = struct_size(buf, teid, num_nodes);
247         buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
248         if (!buf)
249                 return -ENOMEM;
250
251         buf->hdr.parent_teid = parent->info.node_teid;
252         buf->hdr.num_elems = cpu_to_le16(num_nodes);
253         for (i = 0; i < num_nodes; i++)
254                 buf->teid[i] = cpu_to_le32(node_teids[i]);
255
256         status = ice_aq_delete_sched_elems(hw, 1, buf, buf_size,
257                                            &num_groups_removed, NULL);
258         if (status || num_groups_removed != 1)
259                 ice_debug(hw, ICE_DBG_SCHED, "remove node failed FW error %d\n",
260                           hw->adminq.sq_last_status);
261
262         devm_kfree(ice_hw_to_dev(hw), buf);
263         return status;
264 }
265
266 /**
267  * ice_sched_get_first_node - get the first node of the given layer
268  * @pi: port information structure
269  * @parent: pointer the base node of the subtree
270  * @layer: layer number
271  *
272  * This function retrieves the first node of the given layer from the subtree
273  */
274 static struct ice_sched_node *
275 ice_sched_get_first_node(struct ice_port_info *pi,
276                          struct ice_sched_node *parent, u8 layer)
277 {
278         return pi->sib_head[parent->tc_num][layer];
279 }
280
281 /**
282  * ice_sched_get_tc_node - get pointer to TC node
283  * @pi: port information structure
284  * @tc: TC number
285  *
286  * This function returns the TC node pointer
287  */
288 struct ice_sched_node *ice_sched_get_tc_node(struct ice_port_info *pi, u8 tc)
289 {
290         u8 i;
291
292         if (!pi || !pi->root)
293                 return NULL;
294         for (i = 0; i < pi->root->num_children; i++)
295                 if (pi->root->children[i]->tc_num == tc)
296                         return pi->root->children[i];
297         return NULL;
298 }
299
300 /**
301  * ice_free_sched_node - Free a Tx scheduler node from SW DB
302  * @pi: port information structure
303  * @node: pointer to the ice_sched_node struct
304  *
305  * This function frees up a node from SW DB as well as from HW
306  *
307  * This function needs to be called with the port_info->sched_lock held
308  */
309 void ice_free_sched_node(struct ice_port_info *pi, struct ice_sched_node *node)
310 {
311         struct ice_sched_node *parent;
312         struct ice_hw *hw = pi->hw;
313         u8 i, j;
314
315         /* Free the children before freeing up the parent node
316          * The parent array is updated below and that shifts the nodes
317          * in the array. So always pick the first child if num children > 0
318          */
319         while (node->num_children)
320                 ice_free_sched_node(pi, node->children[0]);
321
322         /* Leaf, TC and root nodes can't be deleted by SW */
323         if (node->tx_sched_layer >= hw->sw_entry_point_layer &&
324             node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
325             node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT &&
326             node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF) {
327                 u32 teid = le32_to_cpu(node->info.node_teid);
328
329                 ice_sched_remove_elems(hw, node->parent, 1, &teid);
330         }
331         parent = node->parent;
332         /* root has no parent */
333         if (parent) {
334                 struct ice_sched_node *p;
335
336                 /* update the parent */
337                 for (i = 0; i < parent->num_children; i++)
338                         if (parent->children[i] == node) {
339                                 for (j = i + 1; j < parent->num_children; j++)
340                                         parent->children[j - 1] =
341                                                 parent->children[j];
342                                 parent->num_children--;
343                                 break;
344                         }
345
346                 p = ice_sched_get_first_node(pi, node, node->tx_sched_layer);
347                 while (p) {
348                         if (p->sibling == node) {
349                                 p->sibling = node->sibling;
350                                 break;
351                         }
352                         p = p->sibling;
353                 }
354
355                 /* update the sibling head if head is getting removed */
356                 if (pi->sib_head[node->tc_num][node->tx_sched_layer] == node)
357                         pi->sib_head[node->tc_num][node->tx_sched_layer] =
358                                 node->sibling;
359         }
360
361         devm_kfree(ice_hw_to_dev(hw), node->children);
362         kfree(node->name);
363         xa_erase(&pi->sched_node_ids, node->id);
364         devm_kfree(ice_hw_to_dev(hw), node);
365 }
366
367 /**
368  * ice_aq_get_dflt_topo - gets default scheduler topology
369  * @hw: pointer to the HW struct
370  * @lport: logical port number
371  * @buf: pointer to buffer
372  * @buf_size: buffer size in bytes
373  * @num_branches: returns total number of queue to port branches
374  * @cd: pointer to command details structure or NULL
375  *
376  * Get default scheduler topology (0x400)
377  */
378 static int
379 ice_aq_get_dflt_topo(struct ice_hw *hw, u8 lport,
380                      struct ice_aqc_get_topo_elem *buf, u16 buf_size,
381                      u8 *num_branches, struct ice_sq_cd *cd)
382 {
383         struct ice_aqc_get_topo *cmd;
384         struct ice_aq_desc desc;
385         int status;
386
387         cmd = &desc.params.get_topo;
388         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_dflt_topo);
389         cmd->port_num = lport;
390         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
391         if (!status && num_branches)
392                 *num_branches = cmd->num_branches;
393
394         return status;
395 }
396
397 /**
398  * ice_aq_add_sched_elems - adds scheduling element
399  * @hw: pointer to the HW struct
400  * @grps_req: the number of groups that are requested to be added
401  * @buf: pointer to buffer
402  * @buf_size: buffer size in bytes
403  * @grps_added: returns total number of groups added
404  * @cd: pointer to command details structure or NULL
405  *
406  * Add scheduling elements (0x0401)
407  */
408 static int
409 ice_aq_add_sched_elems(struct ice_hw *hw, u16 grps_req,
410                        struct ice_aqc_add_elem *buf, u16 buf_size,
411                        u16 *grps_added, struct ice_sq_cd *cd)
412 {
413         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_add_sched_elems,
414                                            grps_req, (void *)buf, buf_size,
415                                            grps_added, cd);
416 }
417
418 /**
419  * ice_aq_cfg_sched_elems - configures scheduler elements
420  * @hw: pointer to the HW struct
421  * @elems_req: number of elements to configure
422  * @buf: pointer to buffer
423  * @buf_size: buffer size in bytes
424  * @elems_cfgd: returns total number of elements configured
425  * @cd: pointer to command details structure or NULL
426  *
427  * Configure scheduling elements (0x0403)
428  */
429 static int
430 ice_aq_cfg_sched_elems(struct ice_hw *hw, u16 elems_req,
431                        struct ice_aqc_txsched_elem_data *buf, u16 buf_size,
432                        u16 *elems_cfgd, struct ice_sq_cd *cd)
433 {
434         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_cfg_sched_elems,
435                                            elems_req, (void *)buf, buf_size,
436                                            elems_cfgd, cd);
437 }
438
439 /**
440  * ice_aq_move_sched_elems - move scheduler elements
441  * @hw: pointer to the HW struct
442  * @grps_req: number of groups to move
443  * @buf: pointer to buffer
444  * @buf_size: buffer size in bytes
445  * @grps_movd: returns total number of groups moved
446  * @cd: pointer to command details structure or NULL
447  *
448  * Move scheduling elements (0x0408)
449  */
450 static int
451 ice_aq_move_sched_elems(struct ice_hw *hw, u16 grps_req,
452                         struct ice_aqc_move_elem *buf, u16 buf_size,
453                         u16 *grps_movd, struct ice_sq_cd *cd)
454 {
455         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_move_sched_elems,
456                                            grps_req, (void *)buf, buf_size,
457                                            grps_movd, cd);
458 }
459
460 /**
461  * ice_aq_suspend_sched_elems - suspend scheduler elements
462  * @hw: pointer to the HW struct
463  * @elems_req: number of elements to suspend
464  * @buf: pointer to buffer
465  * @buf_size: buffer size in bytes
466  * @elems_ret: returns total number of elements suspended
467  * @cd: pointer to command details structure or NULL
468  *
469  * Suspend scheduling elements (0x0409)
470  */
471 static int
472 ice_aq_suspend_sched_elems(struct ice_hw *hw, u16 elems_req, __le32 *buf,
473                            u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
474 {
475         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_suspend_sched_elems,
476                                            elems_req, (void *)buf, buf_size,
477                                            elems_ret, cd);
478 }
479
480 /**
481  * ice_aq_resume_sched_elems - resume scheduler elements
482  * @hw: pointer to the HW struct
483  * @elems_req: number of elements to resume
484  * @buf: pointer to buffer
485  * @buf_size: buffer size in bytes
486  * @elems_ret: returns total number of elements resumed
487  * @cd: pointer to command details structure or NULL
488  *
489  * resume scheduling elements (0x040A)
490  */
491 static int
492 ice_aq_resume_sched_elems(struct ice_hw *hw, u16 elems_req, __le32 *buf,
493                           u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
494 {
495         return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_resume_sched_elems,
496                                            elems_req, (void *)buf, buf_size,
497                                            elems_ret, cd);
498 }
499
500 /**
501  * ice_aq_query_sched_res - query scheduler resource
502  * @hw: pointer to the HW struct
503  * @buf_size: buffer size in bytes
504  * @buf: pointer to buffer
505  * @cd: pointer to command details structure or NULL
506  *
507  * Query scheduler resource allocation (0x0412)
508  */
509 static int
510 ice_aq_query_sched_res(struct ice_hw *hw, u16 buf_size,
511                        struct ice_aqc_query_txsched_res_resp *buf,
512                        struct ice_sq_cd *cd)
513 {
514         struct ice_aq_desc desc;
515
516         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_sched_res);
517         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
518 }
519
520 /**
521  * ice_sched_suspend_resume_elems - suspend or resume HW nodes
522  * @hw: pointer to the HW struct
523  * @num_nodes: number of nodes
524  * @node_teids: array of node teids to be suspended or resumed
525  * @suspend: true means suspend / false means resume
526  *
527  * This function suspends or resumes HW nodes
528  */
529 static int
530 ice_sched_suspend_resume_elems(struct ice_hw *hw, u8 num_nodes, u32 *node_teids,
531                                bool suspend)
532 {
533         u16 i, buf_size, num_elem_ret = 0;
534         __le32 *buf;
535         int status;
536
537         buf_size = sizeof(*buf) * num_nodes;
538         buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
539         if (!buf)
540                 return -ENOMEM;
541
542         for (i = 0; i < num_nodes; i++)
543                 buf[i] = cpu_to_le32(node_teids[i]);
544
545         if (suspend)
546                 status = ice_aq_suspend_sched_elems(hw, num_nodes, buf,
547                                                     buf_size, &num_elem_ret,
548                                                     NULL);
549         else
550                 status = ice_aq_resume_sched_elems(hw, num_nodes, buf,
551                                                    buf_size, &num_elem_ret,
552                                                    NULL);
553         if (status || num_elem_ret != num_nodes)
554                 ice_debug(hw, ICE_DBG_SCHED, "suspend/resume failed\n");
555
556         devm_kfree(ice_hw_to_dev(hw), buf);
557         return status;
558 }
559
560 /**
561  * ice_alloc_lan_q_ctx - allocate LAN queue contexts for the given VSI and TC
562  * @hw: pointer to the HW struct
563  * @vsi_handle: VSI handle
564  * @tc: TC number
565  * @new_numqs: number of queues
566  */
567 static int
568 ice_alloc_lan_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 new_numqs)
569 {
570         struct ice_vsi_ctx *vsi_ctx;
571         struct ice_q_ctx *q_ctx;
572
573         vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
574         if (!vsi_ctx)
575                 return -EINVAL;
576         /* allocate LAN queue contexts */
577         if (!vsi_ctx->lan_q_ctx[tc]) {
578                 vsi_ctx->lan_q_ctx[tc] = devm_kcalloc(ice_hw_to_dev(hw),
579                                                       new_numqs,
580                                                       sizeof(*q_ctx),
581                                                       GFP_KERNEL);
582                 if (!vsi_ctx->lan_q_ctx[tc])
583                         return -ENOMEM;
584                 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
585                 return 0;
586         }
587         /* num queues are increased, update the queue contexts */
588         if (new_numqs > vsi_ctx->num_lan_q_entries[tc]) {
589                 u16 prev_num = vsi_ctx->num_lan_q_entries[tc];
590
591                 q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
592                                      sizeof(*q_ctx), GFP_KERNEL);
593                 if (!q_ctx)
594                         return -ENOMEM;
595                 memcpy(q_ctx, vsi_ctx->lan_q_ctx[tc],
596                        prev_num * sizeof(*q_ctx));
597                 devm_kfree(ice_hw_to_dev(hw), vsi_ctx->lan_q_ctx[tc]);
598                 vsi_ctx->lan_q_ctx[tc] = q_ctx;
599                 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
600         }
601         return 0;
602 }
603
604 /**
605  * ice_alloc_rdma_q_ctx - allocate RDMA queue contexts for the given VSI and TC
606  * @hw: pointer to the HW struct
607  * @vsi_handle: VSI handle
608  * @tc: TC number
609  * @new_numqs: number of queues
610  */
611 static int
612 ice_alloc_rdma_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 new_numqs)
613 {
614         struct ice_vsi_ctx *vsi_ctx;
615         struct ice_q_ctx *q_ctx;
616
617         vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
618         if (!vsi_ctx)
619                 return -EINVAL;
620         /* allocate RDMA queue contexts */
621         if (!vsi_ctx->rdma_q_ctx[tc]) {
622                 vsi_ctx->rdma_q_ctx[tc] = devm_kcalloc(ice_hw_to_dev(hw),
623                                                        new_numqs,
624                                                        sizeof(*q_ctx),
625                                                        GFP_KERNEL);
626                 if (!vsi_ctx->rdma_q_ctx[tc])
627                         return -ENOMEM;
628                 vsi_ctx->num_rdma_q_entries[tc] = new_numqs;
629                 return 0;
630         }
631         /* num queues are increased, update the queue contexts */
632         if (new_numqs > vsi_ctx->num_rdma_q_entries[tc]) {
633                 u16 prev_num = vsi_ctx->num_rdma_q_entries[tc];
634
635                 q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
636                                      sizeof(*q_ctx), GFP_KERNEL);
637                 if (!q_ctx)
638                         return -ENOMEM;
639                 memcpy(q_ctx, vsi_ctx->rdma_q_ctx[tc],
640                        prev_num * sizeof(*q_ctx));
641                 devm_kfree(ice_hw_to_dev(hw), vsi_ctx->rdma_q_ctx[tc]);
642                 vsi_ctx->rdma_q_ctx[tc] = q_ctx;
643                 vsi_ctx->num_rdma_q_entries[tc] = new_numqs;
644         }
645         return 0;
646 }
647
648 /**
649  * ice_aq_rl_profile - performs a rate limiting task
650  * @hw: pointer to the HW struct
651  * @opcode: opcode for add, query, or remove profile(s)
652  * @num_profiles: the number of profiles
653  * @buf: pointer to buffer
654  * @buf_size: buffer size in bytes
655  * @num_processed: number of processed add or remove profile(s) to return
656  * @cd: pointer to command details structure
657  *
658  * RL profile function to add, query, or remove profile(s)
659  */
660 static int
661 ice_aq_rl_profile(struct ice_hw *hw, enum ice_adminq_opc opcode,
662                   u16 num_profiles, struct ice_aqc_rl_profile_elem *buf,
663                   u16 buf_size, u16 *num_processed, struct ice_sq_cd *cd)
664 {
665         struct ice_aqc_rl_profile *cmd;
666         struct ice_aq_desc desc;
667         int status;
668
669         cmd = &desc.params.rl_profile;
670
671         ice_fill_dflt_direct_cmd_desc(&desc, opcode);
672         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
673         cmd->num_profiles = cpu_to_le16(num_profiles);
674         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
675         if (!status && num_processed)
676                 *num_processed = le16_to_cpu(cmd->num_processed);
677         return status;
678 }
679
680 /**
681  * ice_aq_add_rl_profile - adds rate limiting profile(s)
682  * @hw: pointer to the HW struct
683  * @num_profiles: the number of profile(s) to be add
684  * @buf: pointer to buffer
685  * @buf_size: buffer size in bytes
686  * @num_profiles_added: total number of profiles added to return
687  * @cd: pointer to command details structure
688  *
689  * Add RL profile (0x0410)
690  */
691 static int
692 ice_aq_add_rl_profile(struct ice_hw *hw, u16 num_profiles,
693                       struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
694                       u16 *num_profiles_added, struct ice_sq_cd *cd)
695 {
696         return ice_aq_rl_profile(hw, ice_aqc_opc_add_rl_profiles, num_profiles,
697                                  buf, buf_size, num_profiles_added, cd);
698 }
699
700 /**
701  * ice_aq_remove_rl_profile - removes RL profile(s)
702  * @hw: pointer to the HW struct
703  * @num_profiles: the number of profile(s) to remove
704  * @buf: pointer to buffer
705  * @buf_size: buffer size in bytes
706  * @num_profiles_removed: total number of profiles removed to return
707  * @cd: pointer to command details structure or NULL
708  *
709  * Remove RL profile (0x0415)
710  */
711 static int
712 ice_aq_remove_rl_profile(struct ice_hw *hw, u16 num_profiles,
713                          struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
714                          u16 *num_profiles_removed, struct ice_sq_cd *cd)
715 {
716         return ice_aq_rl_profile(hw, ice_aqc_opc_remove_rl_profiles,
717                                  num_profiles, buf, buf_size,
718                                  num_profiles_removed, cd);
719 }
720
721 /**
722  * ice_sched_del_rl_profile - remove RL profile
723  * @hw: pointer to the HW struct
724  * @rl_info: rate limit profile information
725  *
726  * If the profile ID is not referenced anymore, it removes profile ID with
727  * its associated parameters from HW DB,and locally. The caller needs to
728  * hold scheduler lock.
729  */
730 static int
731 ice_sched_del_rl_profile(struct ice_hw *hw,
732                          struct ice_aqc_rl_profile_info *rl_info)
733 {
734         struct ice_aqc_rl_profile_elem *buf;
735         u16 num_profiles_removed;
736         u16 num_profiles = 1;
737         int status;
738
739         if (rl_info->prof_id_ref != 0)
740                 return -EBUSY;
741
742         /* Safe to remove profile ID */
743         buf = &rl_info->profile;
744         status = ice_aq_remove_rl_profile(hw, num_profiles, buf, sizeof(*buf),
745                                           &num_profiles_removed, NULL);
746         if (status || num_profiles_removed != num_profiles)
747                 return -EIO;
748
749         /* Delete stale entry now */
750         list_del(&rl_info->list_entry);
751         devm_kfree(ice_hw_to_dev(hw), rl_info);
752         return status;
753 }
754
755 /**
756  * ice_sched_clear_rl_prof - clears RL prof entries
757  * @pi: port information structure
758  *
759  * This function removes all RL profile from HW as well as from SW DB.
760  */
761 static void ice_sched_clear_rl_prof(struct ice_port_info *pi)
762 {
763         u16 ln;
764
765         for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
766                 struct ice_aqc_rl_profile_info *rl_prof_elem;
767                 struct ice_aqc_rl_profile_info *rl_prof_tmp;
768
769                 list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
770                                          &pi->rl_prof_list[ln], list_entry) {
771                         struct ice_hw *hw = pi->hw;
772                         int status;
773
774                         rl_prof_elem->prof_id_ref = 0;
775                         status = ice_sched_del_rl_profile(hw, rl_prof_elem);
776                         if (status) {
777                                 ice_debug(hw, ICE_DBG_SCHED, "Remove rl profile failed\n");
778                                 /* On error, free mem required */
779                                 list_del(&rl_prof_elem->list_entry);
780                                 devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
781                         }
782                 }
783         }
784 }
785
786 /**
787  * ice_sched_clear_agg - clears the aggregator related information
788  * @hw: pointer to the hardware structure
789  *
790  * This function removes aggregator list and free up aggregator related memory
791  * previously allocated.
792  */
793 void ice_sched_clear_agg(struct ice_hw *hw)
794 {
795         struct ice_sched_agg_info *agg_info;
796         struct ice_sched_agg_info *atmp;
797
798         list_for_each_entry_safe(agg_info, atmp, &hw->agg_list, list_entry) {
799                 struct ice_sched_agg_vsi_info *agg_vsi_info;
800                 struct ice_sched_agg_vsi_info *vtmp;
801
802                 list_for_each_entry_safe(agg_vsi_info, vtmp,
803                                          &agg_info->agg_vsi_list, list_entry) {
804                         list_del(&agg_vsi_info->list_entry);
805                         devm_kfree(ice_hw_to_dev(hw), agg_vsi_info);
806                 }
807                 list_del(&agg_info->list_entry);
808                 devm_kfree(ice_hw_to_dev(hw), agg_info);
809         }
810 }
811
812 /**
813  * ice_sched_clear_tx_topo - clears the scheduler tree nodes
814  * @pi: port information structure
815  *
816  * This function removes all the nodes from HW as well as from SW DB.
817  */
818 static void ice_sched_clear_tx_topo(struct ice_port_info *pi)
819 {
820         if (!pi)
821                 return;
822         /* remove RL profiles related lists */
823         ice_sched_clear_rl_prof(pi);
824         if (pi->root) {
825                 ice_free_sched_node(pi, pi->root);
826                 pi->root = NULL;
827         }
828 }
829
830 /**
831  * ice_sched_clear_port - clear the scheduler elements from SW DB for a port
832  * @pi: port information structure
833  *
834  * Cleanup scheduling elements from SW DB
835  */
836 void ice_sched_clear_port(struct ice_port_info *pi)
837 {
838         if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
839                 return;
840
841         pi->port_state = ICE_SCHED_PORT_STATE_INIT;
842         mutex_lock(&pi->sched_lock);
843         ice_sched_clear_tx_topo(pi);
844         mutex_unlock(&pi->sched_lock);
845         mutex_destroy(&pi->sched_lock);
846 }
847
848 /**
849  * ice_sched_cleanup_all - cleanup scheduler elements from SW DB for all ports
850  * @hw: pointer to the HW struct
851  *
852  * Cleanup scheduling elements from SW DB for all the ports
853  */
854 void ice_sched_cleanup_all(struct ice_hw *hw)
855 {
856         if (!hw)
857                 return;
858
859         devm_kfree(ice_hw_to_dev(hw), hw->layer_info);
860         hw->layer_info = NULL;
861
862         ice_sched_clear_port(hw->port_info);
863
864         hw->num_tx_sched_layers = 0;
865         hw->num_tx_sched_phys_layers = 0;
866         hw->flattened_layers = 0;
867         hw->max_cgds = 0;
868 }
869
870 /**
871  * ice_sched_add_elems - add nodes to HW and SW DB
872  * @pi: port information structure
873  * @tc_node: pointer to the branch node
874  * @parent: pointer to the parent node
875  * @layer: layer number to add nodes
876  * @num_nodes: number of nodes
877  * @num_nodes_added: pointer to num nodes added
878  * @first_node_teid: if new nodes are added then return the TEID of first node
879  * @prealloc_nodes: preallocated nodes struct for software DB
880  *
881  * This function add nodes to HW as well as to SW DB for a given layer
882  */
883 int
884 ice_sched_add_elems(struct ice_port_info *pi, struct ice_sched_node *tc_node,
885                     struct ice_sched_node *parent, u8 layer, u16 num_nodes,
886                     u16 *num_nodes_added, u32 *first_node_teid,
887                     struct ice_sched_node **prealloc_nodes)
888 {
889         struct ice_sched_node *prev, *new_node;
890         struct ice_aqc_add_elem *buf;
891         u16 i, num_groups_added = 0;
892         struct ice_hw *hw = pi->hw;
893         size_t buf_size;
894         int status = 0;
895         u32 teid;
896
897         buf_size = struct_size(buf, generic, num_nodes);
898         buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
899         if (!buf)
900                 return -ENOMEM;
901
902         buf->hdr.parent_teid = parent->info.node_teid;
903         buf->hdr.num_elems = cpu_to_le16(num_nodes);
904         for (i = 0; i < num_nodes; i++) {
905                 buf->generic[i].parent_teid = parent->info.node_teid;
906                 buf->generic[i].data.elem_type = ICE_AQC_ELEM_TYPE_SE_GENERIC;
907                 buf->generic[i].data.valid_sections =
908                         ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
909                         ICE_AQC_ELEM_VALID_EIR;
910                 buf->generic[i].data.generic = 0;
911                 buf->generic[i].data.cir_bw.bw_profile_idx =
912                         cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
913                 buf->generic[i].data.cir_bw.bw_alloc =
914                         cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
915                 buf->generic[i].data.eir_bw.bw_profile_idx =
916                         cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
917                 buf->generic[i].data.eir_bw.bw_alloc =
918                         cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
919         }
920
921         status = ice_aq_add_sched_elems(hw, 1, buf, buf_size,
922                                         &num_groups_added, NULL);
923         if (status || num_groups_added != 1) {
924                 ice_debug(hw, ICE_DBG_SCHED, "add node failed FW Error %d\n",
925                           hw->adminq.sq_last_status);
926                 devm_kfree(ice_hw_to_dev(hw), buf);
927                 return -EIO;
928         }
929
930         *num_nodes_added = num_nodes;
931         /* add nodes to the SW DB */
932         for (i = 0; i < num_nodes; i++) {
933                 if (prealloc_nodes)
934                         status = ice_sched_add_node(pi, layer, &buf->generic[i], prealloc_nodes[i]);
935                 else
936                         status = ice_sched_add_node(pi, layer, &buf->generic[i], NULL);
937
938                 if (status) {
939                         ice_debug(hw, ICE_DBG_SCHED, "add nodes in SW DB failed status =%d\n",
940                                   status);
941                         break;
942                 }
943
944                 teid = le32_to_cpu(buf->generic[i].node_teid);
945                 new_node = ice_sched_find_node_by_teid(parent, teid);
946                 if (!new_node) {
947                         ice_debug(hw, ICE_DBG_SCHED, "Node is missing for teid =%d\n", teid);
948                         break;
949                 }
950
951                 new_node->sibling = NULL;
952                 new_node->tc_num = tc_node->tc_num;
953                 new_node->tx_weight = ICE_SCHED_DFLT_BW_WT;
954                 new_node->tx_share = ICE_SCHED_DFLT_BW;
955                 new_node->tx_max = ICE_SCHED_DFLT_BW;
956                 new_node->name = kzalloc(SCHED_NODE_NAME_MAX_LEN, GFP_KERNEL);
957                 if (!new_node->name)
958                         return -ENOMEM;
959
960                 status = xa_alloc(&pi->sched_node_ids, &new_node->id, NULL, XA_LIMIT(0, UINT_MAX),
961                                   GFP_KERNEL);
962                 if (status) {
963                         ice_debug(hw, ICE_DBG_SCHED, "xa_alloc failed for sched node status =%d\n",
964                                   status);
965                         break;
966                 }
967
968                 snprintf(new_node->name, SCHED_NODE_NAME_MAX_LEN, "node_%u", new_node->id);
969
970                 /* add it to previous node sibling pointer */
971                 /* Note: siblings are not linked across branches */
972                 prev = ice_sched_get_first_node(pi, tc_node, layer);
973                 if (prev && prev != new_node) {
974                         while (prev->sibling)
975                                 prev = prev->sibling;
976                         prev->sibling = new_node;
977                 }
978
979                 /* initialize the sibling head */
980                 if (!pi->sib_head[tc_node->tc_num][layer])
981                         pi->sib_head[tc_node->tc_num][layer] = new_node;
982
983                 if (i == 0)
984                         *first_node_teid = teid;
985         }
986
987         devm_kfree(ice_hw_to_dev(hw), buf);
988         return status;
989 }
990
991 /**
992  * ice_sched_add_nodes_to_hw_layer - Add nodes to HW layer
993  * @pi: port information structure
994  * @tc_node: pointer to TC node
995  * @parent: pointer to parent node
996  * @layer: layer number to add nodes
997  * @num_nodes: number of nodes to be added
998  * @first_node_teid: pointer to the first node TEID
999  * @num_nodes_added: pointer to number of nodes added
1000  *
1001  * Add nodes into specific HW layer.
1002  */
1003 static int
1004 ice_sched_add_nodes_to_hw_layer(struct ice_port_info *pi,
1005                                 struct ice_sched_node *tc_node,
1006                                 struct ice_sched_node *parent, u8 layer,
1007                                 u16 num_nodes, u32 *first_node_teid,
1008                                 u16 *num_nodes_added)
1009 {
1010         u16 max_child_nodes;
1011
1012         *num_nodes_added = 0;
1013
1014         if (!num_nodes)
1015                 return 0;
1016
1017         if (!parent || layer < pi->hw->sw_entry_point_layer)
1018                 return -EINVAL;
1019
1020         /* max children per node per layer */
1021         max_child_nodes = pi->hw->max_children[parent->tx_sched_layer];
1022
1023         /* current number of children + required nodes exceed max children */
1024         if ((parent->num_children + num_nodes) > max_child_nodes) {
1025                 /* Fail if the parent is a TC node */
1026                 if (parent == tc_node)
1027                         return -EIO;
1028                 return -ENOSPC;
1029         }
1030
1031         return ice_sched_add_elems(pi, tc_node, parent, layer, num_nodes,
1032                                    num_nodes_added, first_node_teid, NULL);
1033 }
1034
1035 /**
1036  * ice_sched_add_nodes_to_layer - Add nodes to a given layer
1037  * @pi: port information structure
1038  * @tc_node: pointer to TC node
1039  * @parent: pointer to parent node
1040  * @layer: layer number to add nodes
1041  * @num_nodes: number of nodes to be added
1042  * @first_node_teid: pointer to the first node TEID
1043  * @num_nodes_added: pointer to number of nodes added
1044  *
1045  * This function add nodes to a given layer.
1046  */
1047 static int
1048 ice_sched_add_nodes_to_layer(struct ice_port_info *pi,
1049                              struct ice_sched_node *tc_node,
1050                              struct ice_sched_node *parent, u8 layer,
1051                              u16 num_nodes, u32 *first_node_teid,
1052                              u16 *num_nodes_added)
1053 {
1054         u32 *first_teid_ptr = first_node_teid;
1055         u16 new_num_nodes = num_nodes;
1056         int status = 0;
1057
1058         *num_nodes_added = 0;
1059         while (*num_nodes_added < num_nodes) {
1060                 u16 max_child_nodes, num_added = 0;
1061                 u32 temp;
1062
1063                 status = ice_sched_add_nodes_to_hw_layer(pi, tc_node, parent,
1064                                                          layer, new_num_nodes,
1065                                                          first_teid_ptr,
1066                                                          &num_added);
1067                 if (!status)
1068                         *num_nodes_added += num_added;
1069                 /* added more nodes than requested ? */
1070                 if (*num_nodes_added > num_nodes) {
1071                         ice_debug(pi->hw, ICE_DBG_SCHED, "added extra nodes %d %d\n", num_nodes,
1072                                   *num_nodes_added);
1073                         status = -EIO;
1074                         break;
1075                 }
1076                 /* break if all the nodes are added successfully */
1077                 if (!status && (*num_nodes_added == num_nodes))
1078                         break;
1079                 /* break if the error is not max limit */
1080                 if (status && status != -ENOSPC)
1081                         break;
1082                 /* Exceeded the max children */
1083                 max_child_nodes = pi->hw->max_children[parent->tx_sched_layer];
1084                 /* utilize all the spaces if the parent is not full */
1085                 if (parent->num_children < max_child_nodes) {
1086                         new_num_nodes = max_child_nodes - parent->num_children;
1087                 } else {
1088                         /* This parent is full, try the next sibling */
1089                         parent = parent->sibling;
1090                         /* Don't modify the first node TEID memory if the
1091                          * first node was added already in the above call.
1092                          * Instead send some temp memory for all other
1093                          * recursive calls.
1094                          */
1095                         if (num_added)
1096                                 first_teid_ptr = &temp;
1097
1098                         new_num_nodes = num_nodes - *num_nodes_added;
1099                 }
1100         }
1101         return status;
1102 }
1103
1104 /**
1105  * ice_sched_get_qgrp_layer - get the current queue group layer number
1106  * @hw: pointer to the HW struct
1107  *
1108  * This function returns the current queue group layer number
1109  */
1110 static u8 ice_sched_get_qgrp_layer(struct ice_hw *hw)
1111 {
1112         /* It's always total layers - 1, the array is 0 relative so -2 */
1113         return hw->num_tx_sched_layers - ICE_QGRP_LAYER_OFFSET;
1114 }
1115
1116 /**
1117  * ice_sched_get_vsi_layer - get the current VSI layer number
1118  * @hw: pointer to the HW struct
1119  *
1120  * This function returns the current VSI layer number
1121  */
1122 static u8 ice_sched_get_vsi_layer(struct ice_hw *hw)
1123 {
1124         /* Num Layers       VSI layer
1125          *     9               6
1126          *     7               4
1127          *     5 or less       sw_entry_point_layer
1128          */
1129         /* calculate the VSI layer based on number of layers. */
1130         if (hw->num_tx_sched_layers > ICE_VSI_LAYER_OFFSET + 1) {
1131                 u8 layer = hw->num_tx_sched_layers - ICE_VSI_LAYER_OFFSET;
1132
1133                 if (layer > hw->sw_entry_point_layer)
1134                         return layer;
1135         }
1136         return hw->sw_entry_point_layer;
1137 }
1138
1139 /**
1140  * ice_sched_get_agg_layer - get the current aggregator layer number
1141  * @hw: pointer to the HW struct
1142  *
1143  * This function returns the current aggregator layer number
1144  */
1145 static u8 ice_sched_get_agg_layer(struct ice_hw *hw)
1146 {
1147         /* Num Layers       aggregator layer
1148          *     9               4
1149          *     7 or less       sw_entry_point_layer
1150          */
1151         /* calculate the aggregator layer based on number of layers. */
1152         if (hw->num_tx_sched_layers > ICE_AGG_LAYER_OFFSET + 1) {
1153                 u8 layer = hw->num_tx_sched_layers - ICE_AGG_LAYER_OFFSET;
1154
1155                 if (layer > hw->sw_entry_point_layer)
1156                         return layer;
1157         }
1158         return hw->sw_entry_point_layer;
1159 }
1160
1161 /**
1162  * ice_rm_dflt_leaf_node - remove the default leaf node in the tree
1163  * @pi: port information structure
1164  *
1165  * This function removes the leaf node that was created by the FW
1166  * during initialization
1167  */
1168 static void ice_rm_dflt_leaf_node(struct ice_port_info *pi)
1169 {
1170         struct ice_sched_node *node;
1171
1172         node = pi->root;
1173         while (node) {
1174                 if (!node->num_children)
1175                         break;
1176                 node = node->children[0];
1177         }
1178         if (node && node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF) {
1179                 u32 teid = le32_to_cpu(node->info.node_teid);
1180                 int status;
1181
1182                 /* remove the default leaf node */
1183                 status = ice_sched_remove_elems(pi->hw, node->parent, 1, &teid);
1184                 if (!status)
1185                         ice_free_sched_node(pi, node);
1186         }
1187 }
1188
1189 /**
1190  * ice_sched_rm_dflt_nodes - free the default nodes in the tree
1191  * @pi: port information structure
1192  *
1193  * This function frees all the nodes except root and TC that were created by
1194  * the FW during initialization
1195  */
1196 static void ice_sched_rm_dflt_nodes(struct ice_port_info *pi)
1197 {
1198         struct ice_sched_node *node;
1199
1200         ice_rm_dflt_leaf_node(pi);
1201
1202         /* remove the default nodes except TC and root nodes */
1203         node = pi->root;
1204         while (node) {
1205                 if (node->tx_sched_layer >= pi->hw->sw_entry_point_layer &&
1206                     node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
1207                     node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT) {
1208                         ice_free_sched_node(pi, node);
1209                         break;
1210                 }
1211
1212                 if (!node->num_children)
1213                         break;
1214                 node = node->children[0];
1215         }
1216 }
1217
1218 /**
1219  * ice_sched_init_port - Initialize scheduler by querying information from FW
1220  * @pi: port info structure for the tree to cleanup
1221  *
1222  * This function is the initial call to find the total number of Tx scheduler
1223  * resources, default topology created by firmware and storing the information
1224  * in SW DB.
1225  */
1226 int ice_sched_init_port(struct ice_port_info *pi)
1227 {
1228         struct ice_aqc_get_topo_elem *buf;
1229         struct ice_hw *hw;
1230         u8 num_branches;
1231         u16 num_elems;
1232         int status;
1233         u8 i, j;
1234
1235         if (!pi)
1236                 return -EINVAL;
1237         hw = pi->hw;
1238
1239         /* Query the Default Topology from FW */
1240         buf = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
1241         if (!buf)
1242                 return -ENOMEM;
1243
1244         /* Query default scheduling tree topology */
1245         status = ice_aq_get_dflt_topo(hw, pi->lport, buf, ICE_AQ_MAX_BUF_LEN,
1246                                       &num_branches, NULL);
1247         if (status)
1248                 goto err_init_port;
1249
1250         /* num_branches should be between 1-8 */
1251         if (num_branches < 1 || num_branches > ICE_TXSCHED_MAX_BRANCHES) {
1252                 ice_debug(hw, ICE_DBG_SCHED, "num_branches unexpected %d\n",
1253                           num_branches);
1254                 status = -EINVAL;
1255                 goto err_init_port;
1256         }
1257
1258         /* get the number of elements on the default/first branch */
1259         num_elems = le16_to_cpu(buf[0].hdr.num_elems);
1260
1261         /* num_elems should always be between 1-9 */
1262         if (num_elems < 1 || num_elems > ICE_AQC_TOPO_MAX_LEVEL_NUM) {
1263                 ice_debug(hw, ICE_DBG_SCHED, "num_elems unexpected %d\n",
1264                           num_elems);
1265                 status = -EINVAL;
1266                 goto err_init_port;
1267         }
1268
1269         /* If the last node is a leaf node then the index of the queue group
1270          * layer is two less than the number of elements.
1271          */
1272         if (num_elems > 2 && buf[0].generic[num_elems - 1].data.elem_type ==
1273             ICE_AQC_ELEM_TYPE_LEAF)
1274                 pi->last_node_teid =
1275                         le32_to_cpu(buf[0].generic[num_elems - 2].node_teid);
1276         else
1277                 pi->last_node_teid =
1278                         le32_to_cpu(buf[0].generic[num_elems - 1].node_teid);
1279
1280         /* Insert the Tx Sched root node */
1281         status = ice_sched_add_root_node(pi, &buf[0].generic[0]);
1282         if (status)
1283                 goto err_init_port;
1284
1285         /* Parse the default tree and cache the information */
1286         for (i = 0; i < num_branches; i++) {
1287                 num_elems = le16_to_cpu(buf[i].hdr.num_elems);
1288
1289                 /* Skip root element as already inserted */
1290                 for (j = 1; j < num_elems; j++) {
1291                         /* update the sw entry point */
1292                         if (buf[0].generic[j].data.elem_type ==
1293                             ICE_AQC_ELEM_TYPE_ENTRY_POINT)
1294                                 hw->sw_entry_point_layer = j;
1295
1296                         status = ice_sched_add_node(pi, j, &buf[i].generic[j], NULL);
1297                         if (status)
1298                                 goto err_init_port;
1299                 }
1300         }
1301
1302         /* Remove the default nodes. */
1303         if (pi->root)
1304                 ice_sched_rm_dflt_nodes(pi);
1305
1306         /* initialize the port for handling the scheduler tree */
1307         pi->port_state = ICE_SCHED_PORT_STATE_READY;
1308         mutex_init(&pi->sched_lock);
1309         for (i = 0; i < ICE_AQC_TOPO_MAX_LEVEL_NUM; i++)
1310                 INIT_LIST_HEAD(&pi->rl_prof_list[i]);
1311
1312 err_init_port:
1313         if (status && pi->root) {
1314                 ice_free_sched_node(pi, pi->root);
1315                 pi->root = NULL;
1316         }
1317
1318         kfree(buf);
1319         return status;
1320 }
1321
1322 /**
1323  * ice_sched_query_res_alloc - query the FW for num of logical sched layers
1324  * @hw: pointer to the HW struct
1325  *
1326  * query FW for allocated scheduler resources and store in HW struct
1327  */
1328 int ice_sched_query_res_alloc(struct ice_hw *hw)
1329 {
1330         struct ice_aqc_query_txsched_res_resp *buf;
1331         __le16 max_sibl;
1332         int status = 0;
1333         u16 i;
1334
1335         if (hw->layer_info)
1336                 return status;
1337
1338         buf = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*buf), GFP_KERNEL);
1339         if (!buf)
1340                 return -ENOMEM;
1341
1342         status = ice_aq_query_sched_res(hw, sizeof(*buf), buf, NULL);
1343         if (status)
1344                 goto sched_query_out;
1345
1346         hw->num_tx_sched_layers = le16_to_cpu(buf->sched_props.logical_levels);
1347         hw->num_tx_sched_phys_layers =
1348                 le16_to_cpu(buf->sched_props.phys_levels);
1349         hw->flattened_layers = buf->sched_props.flattening_bitmap;
1350         hw->max_cgds = buf->sched_props.max_pf_cgds;
1351
1352         /* max sibling group size of current layer refers to the max children
1353          * of the below layer node.
1354          * layer 1 node max children will be layer 2 max sibling group size
1355          * layer 2 node max children will be layer 3 max sibling group size
1356          * and so on. This array will be populated from root (index 0) to
1357          * qgroup layer 7. Leaf node has no children.
1358          */
1359         for (i = 0; i < hw->num_tx_sched_layers - 1; i++) {
1360                 max_sibl = buf->layer_props[i + 1].max_sibl_grp_sz;
1361                 hw->max_children[i] = le16_to_cpu(max_sibl);
1362         }
1363
1364         hw->layer_info = devm_kmemdup(ice_hw_to_dev(hw), buf->layer_props,
1365                                       (hw->num_tx_sched_layers *
1366                                        sizeof(*hw->layer_info)),
1367                                       GFP_KERNEL);
1368         if (!hw->layer_info) {
1369                 status = -ENOMEM;
1370                 goto sched_query_out;
1371         }
1372
1373 sched_query_out:
1374         devm_kfree(ice_hw_to_dev(hw), buf);
1375         return status;
1376 }
1377
1378 /**
1379  * ice_sched_get_psm_clk_freq - determine the PSM clock frequency
1380  * @hw: pointer to the HW struct
1381  *
1382  * Determine the PSM clock frequency and store in HW struct
1383  */
1384 void ice_sched_get_psm_clk_freq(struct ice_hw *hw)
1385 {
1386         u32 val, clk_src;
1387
1388         val = rd32(hw, GLGEN_CLKSTAT_SRC);
1389         clk_src = (val & GLGEN_CLKSTAT_SRC_PSM_CLK_SRC_M) >>
1390                 GLGEN_CLKSTAT_SRC_PSM_CLK_SRC_S;
1391
1392 #define PSM_CLK_SRC_367_MHZ 0x0
1393 #define PSM_CLK_SRC_416_MHZ 0x1
1394 #define PSM_CLK_SRC_446_MHZ 0x2
1395 #define PSM_CLK_SRC_390_MHZ 0x3
1396
1397         switch (clk_src) {
1398         case PSM_CLK_SRC_367_MHZ:
1399                 hw->psm_clk_freq = ICE_PSM_CLK_367MHZ_IN_HZ;
1400                 break;
1401         case PSM_CLK_SRC_416_MHZ:
1402                 hw->psm_clk_freq = ICE_PSM_CLK_416MHZ_IN_HZ;
1403                 break;
1404         case PSM_CLK_SRC_446_MHZ:
1405                 hw->psm_clk_freq = ICE_PSM_CLK_446MHZ_IN_HZ;
1406                 break;
1407         case PSM_CLK_SRC_390_MHZ:
1408                 hw->psm_clk_freq = ICE_PSM_CLK_390MHZ_IN_HZ;
1409                 break;
1410         default:
1411                 ice_debug(hw, ICE_DBG_SCHED, "PSM clk_src unexpected %u\n",
1412                           clk_src);
1413                 /* fall back to a safe default */
1414                 hw->psm_clk_freq = ICE_PSM_CLK_446MHZ_IN_HZ;
1415         }
1416 }
1417
1418 /**
1419  * ice_sched_find_node_in_subtree - Find node in part of base node subtree
1420  * @hw: pointer to the HW struct
1421  * @base: pointer to the base node
1422  * @node: pointer to the node to search
1423  *
1424  * This function checks whether a given node is part of the base node
1425  * subtree or not
1426  */
1427 static bool
1428 ice_sched_find_node_in_subtree(struct ice_hw *hw, struct ice_sched_node *base,
1429                                struct ice_sched_node *node)
1430 {
1431         u8 i;
1432
1433         for (i = 0; i < base->num_children; i++) {
1434                 struct ice_sched_node *child = base->children[i];
1435
1436                 if (node == child)
1437                         return true;
1438
1439                 if (child->tx_sched_layer > node->tx_sched_layer)
1440                         return false;
1441
1442                 /* this recursion is intentional, and wouldn't
1443                  * go more than 8 calls
1444                  */
1445                 if (ice_sched_find_node_in_subtree(hw, child, node))
1446                         return true;
1447         }
1448         return false;
1449 }
1450
1451 /**
1452  * ice_sched_get_free_qgrp - Scan all queue group siblings and find a free node
1453  * @pi: port information structure
1454  * @vsi_node: software VSI handle
1455  * @qgrp_node: first queue group node identified for scanning
1456  * @owner: LAN or RDMA
1457  *
1458  * This function retrieves a free LAN or RDMA queue group node by scanning
1459  * qgrp_node and its siblings for the queue group with the fewest number
1460  * of queues currently assigned.
1461  */
1462 static struct ice_sched_node *
1463 ice_sched_get_free_qgrp(struct ice_port_info *pi,
1464                         struct ice_sched_node *vsi_node,
1465                         struct ice_sched_node *qgrp_node, u8 owner)
1466 {
1467         struct ice_sched_node *min_qgrp;
1468         u8 min_children;
1469
1470         if (!qgrp_node)
1471                 return qgrp_node;
1472         min_children = qgrp_node->num_children;
1473         if (!min_children)
1474                 return qgrp_node;
1475         min_qgrp = qgrp_node;
1476         /* scan all queue groups until find a node which has less than the
1477          * minimum number of children. This way all queue group nodes get
1478          * equal number of shares and active. The bandwidth will be equally
1479          * distributed across all queues.
1480          */
1481         while (qgrp_node) {
1482                 /* make sure the qgroup node is part of the VSI subtree */
1483                 if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1484                         if (qgrp_node->num_children < min_children &&
1485                             qgrp_node->owner == owner) {
1486                                 /* replace the new min queue group node */
1487                                 min_qgrp = qgrp_node;
1488                                 min_children = min_qgrp->num_children;
1489                                 /* break if it has no children, */
1490                                 if (!min_children)
1491                                         break;
1492                         }
1493                 qgrp_node = qgrp_node->sibling;
1494         }
1495         return min_qgrp;
1496 }
1497
1498 /**
1499  * ice_sched_get_free_qparent - Get a free LAN or RDMA queue group node
1500  * @pi: port information structure
1501  * @vsi_handle: software VSI handle
1502  * @tc: branch number
1503  * @owner: LAN or RDMA
1504  *
1505  * This function retrieves a free LAN or RDMA queue group node
1506  */
1507 struct ice_sched_node *
1508 ice_sched_get_free_qparent(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
1509                            u8 owner)
1510 {
1511         struct ice_sched_node *vsi_node, *qgrp_node;
1512         struct ice_vsi_ctx *vsi_ctx;
1513         u16 max_children;
1514         u8 qgrp_layer;
1515
1516         qgrp_layer = ice_sched_get_qgrp_layer(pi->hw);
1517         max_children = pi->hw->max_children[qgrp_layer];
1518
1519         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1520         if (!vsi_ctx)
1521                 return NULL;
1522         vsi_node = vsi_ctx->sched.vsi_node[tc];
1523         /* validate invalid VSI ID */
1524         if (!vsi_node)
1525                 return NULL;
1526
1527         /* get the first queue group node from VSI sub-tree */
1528         qgrp_node = ice_sched_get_first_node(pi, vsi_node, qgrp_layer);
1529         while (qgrp_node) {
1530                 /* make sure the qgroup node is part of the VSI subtree */
1531                 if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1532                         if (qgrp_node->num_children < max_children &&
1533                             qgrp_node->owner == owner)
1534                                 break;
1535                 qgrp_node = qgrp_node->sibling;
1536         }
1537
1538         /* Select the best queue group */
1539         return ice_sched_get_free_qgrp(pi, vsi_node, qgrp_node, owner);
1540 }
1541
1542 /**
1543  * ice_sched_get_vsi_node - Get a VSI node based on VSI ID
1544  * @pi: pointer to the port information structure
1545  * @tc_node: pointer to the TC node
1546  * @vsi_handle: software VSI handle
1547  *
1548  * This function retrieves a VSI node for a given VSI ID from a given
1549  * TC branch
1550  */
1551 static struct ice_sched_node *
1552 ice_sched_get_vsi_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1553                        u16 vsi_handle)
1554 {
1555         struct ice_sched_node *node;
1556         u8 vsi_layer;
1557
1558         vsi_layer = ice_sched_get_vsi_layer(pi->hw);
1559         node = ice_sched_get_first_node(pi, tc_node, vsi_layer);
1560
1561         /* Check whether it already exists */
1562         while (node) {
1563                 if (node->vsi_handle == vsi_handle)
1564                         return node;
1565                 node = node->sibling;
1566         }
1567
1568         return node;
1569 }
1570
1571 /**
1572  * ice_sched_get_agg_node - Get an aggregator node based on aggregator ID
1573  * @pi: pointer to the port information structure
1574  * @tc_node: pointer to the TC node
1575  * @agg_id: aggregator ID
1576  *
1577  * This function retrieves an aggregator node for a given aggregator ID from
1578  * a given TC branch
1579  */
1580 static struct ice_sched_node *
1581 ice_sched_get_agg_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1582                        u32 agg_id)
1583 {
1584         struct ice_sched_node *node;
1585         struct ice_hw *hw = pi->hw;
1586         u8 agg_layer;
1587
1588         if (!hw)
1589                 return NULL;
1590         agg_layer = ice_sched_get_agg_layer(hw);
1591         node = ice_sched_get_first_node(pi, tc_node, agg_layer);
1592
1593         /* Check whether it already exists */
1594         while (node) {
1595                 if (node->agg_id == agg_id)
1596                         return node;
1597                 node = node->sibling;
1598         }
1599
1600         return node;
1601 }
1602
1603 /**
1604  * ice_sched_calc_vsi_child_nodes - calculate number of VSI child nodes
1605  * @hw: pointer to the HW struct
1606  * @num_qs: number of queues
1607  * @num_nodes: num nodes array
1608  *
1609  * This function calculates the number of VSI child nodes based on the
1610  * number of queues.
1611  */
1612 static void
1613 ice_sched_calc_vsi_child_nodes(struct ice_hw *hw, u16 num_qs, u16 *num_nodes)
1614 {
1615         u16 num = num_qs;
1616         u8 i, qgl, vsil;
1617
1618         qgl = ice_sched_get_qgrp_layer(hw);
1619         vsil = ice_sched_get_vsi_layer(hw);
1620
1621         /* calculate num nodes from queue group to VSI layer */
1622         for (i = qgl; i > vsil; i--) {
1623                 /* round to the next integer if there is a remainder */
1624                 num = DIV_ROUND_UP(num, hw->max_children[i]);
1625
1626                 /* need at least one node */
1627                 num_nodes[i] = num ? num : 1;
1628         }
1629 }
1630
1631 /**
1632  * ice_sched_add_vsi_child_nodes - add VSI child nodes to tree
1633  * @pi: port information structure
1634  * @vsi_handle: software VSI handle
1635  * @tc_node: pointer to the TC node
1636  * @num_nodes: pointer to the num nodes that needs to be added per layer
1637  * @owner: node owner (LAN or RDMA)
1638  *
1639  * This function adds the VSI child nodes to tree. It gets called for
1640  * LAN and RDMA separately.
1641  */
1642 static int
1643 ice_sched_add_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1644                               struct ice_sched_node *tc_node, u16 *num_nodes,
1645                               u8 owner)
1646 {
1647         struct ice_sched_node *parent, *node;
1648         struct ice_hw *hw = pi->hw;
1649         u32 first_node_teid;
1650         u16 num_added = 0;
1651         u8 i, qgl, vsil;
1652
1653         qgl = ice_sched_get_qgrp_layer(hw);
1654         vsil = ice_sched_get_vsi_layer(hw);
1655         parent = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1656         for (i = vsil + 1; i <= qgl; i++) {
1657                 int status;
1658
1659                 if (!parent)
1660                         return -EIO;
1661
1662                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
1663                                                       num_nodes[i],
1664                                                       &first_node_teid,
1665                                                       &num_added);
1666                 if (status || num_nodes[i] != num_added)
1667                         return -EIO;
1668
1669                 /* The newly added node can be a new parent for the next
1670                  * layer nodes
1671                  */
1672                 if (num_added) {
1673                         parent = ice_sched_find_node_by_teid(tc_node,
1674                                                              first_node_teid);
1675                         node = parent;
1676                         while (node) {
1677                                 node->owner = owner;
1678                                 node = node->sibling;
1679                         }
1680                 } else {
1681                         parent = parent->children[0];
1682                 }
1683         }
1684
1685         return 0;
1686 }
1687
1688 /**
1689  * ice_sched_calc_vsi_support_nodes - calculate number of VSI support nodes
1690  * @pi: pointer to the port info structure
1691  * @tc_node: pointer to TC node
1692  * @num_nodes: pointer to num nodes array
1693  *
1694  * This function calculates the number of supported nodes needed to add this
1695  * VSI into Tx tree including the VSI, parent and intermediate nodes in below
1696  * layers
1697  */
1698 static void
1699 ice_sched_calc_vsi_support_nodes(struct ice_port_info *pi,
1700                                  struct ice_sched_node *tc_node, u16 *num_nodes)
1701 {
1702         struct ice_sched_node *node;
1703         u8 vsil;
1704         int i;
1705
1706         vsil = ice_sched_get_vsi_layer(pi->hw);
1707         for (i = vsil; i >= pi->hw->sw_entry_point_layer; i--)
1708                 /* Add intermediate nodes if TC has no children and
1709                  * need at least one node for VSI
1710                  */
1711                 if (!tc_node->num_children || i == vsil) {
1712                         num_nodes[i]++;
1713                 } else {
1714                         /* If intermediate nodes are reached max children
1715                          * then add a new one.
1716                          */
1717                         node = ice_sched_get_first_node(pi, tc_node, (u8)i);
1718                         /* scan all the siblings */
1719                         while (node) {
1720                                 if (node->num_children < pi->hw->max_children[i])
1721                                         break;
1722                                 node = node->sibling;
1723                         }
1724
1725                         /* tree has one intermediate node to add this new VSI.
1726                          * So no need to calculate supported nodes for below
1727                          * layers.
1728                          */
1729                         if (node)
1730                                 break;
1731                         /* all the nodes are full, allocate a new one */
1732                         num_nodes[i]++;
1733                 }
1734 }
1735
1736 /**
1737  * ice_sched_add_vsi_support_nodes - add VSI supported nodes into Tx tree
1738  * @pi: port information structure
1739  * @vsi_handle: software VSI handle
1740  * @tc_node: pointer to TC node
1741  * @num_nodes: pointer to num nodes array
1742  *
1743  * This function adds the VSI supported nodes into Tx tree including the
1744  * VSI, its parent and intermediate nodes in below layers
1745  */
1746 static int
1747 ice_sched_add_vsi_support_nodes(struct ice_port_info *pi, u16 vsi_handle,
1748                                 struct ice_sched_node *tc_node, u16 *num_nodes)
1749 {
1750         struct ice_sched_node *parent = tc_node;
1751         u32 first_node_teid;
1752         u16 num_added = 0;
1753         u8 i, vsil;
1754
1755         if (!pi)
1756                 return -EINVAL;
1757
1758         vsil = ice_sched_get_vsi_layer(pi->hw);
1759         for (i = pi->hw->sw_entry_point_layer; i <= vsil; i++) {
1760                 int status;
1761
1762                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
1763                                                       i, num_nodes[i],
1764                                                       &first_node_teid,
1765                                                       &num_added);
1766                 if (status || num_nodes[i] != num_added)
1767                         return -EIO;
1768
1769                 /* The newly added node can be a new parent for the next
1770                  * layer nodes
1771                  */
1772                 if (num_added)
1773                         parent = ice_sched_find_node_by_teid(tc_node,
1774                                                              first_node_teid);
1775                 else
1776                         parent = parent->children[0];
1777
1778                 if (!parent)
1779                         return -EIO;
1780
1781                 if (i == vsil)
1782                         parent->vsi_handle = vsi_handle;
1783         }
1784
1785         return 0;
1786 }
1787
1788 /**
1789  * ice_sched_add_vsi_to_topo - add a new VSI into tree
1790  * @pi: port information structure
1791  * @vsi_handle: software VSI handle
1792  * @tc: TC number
1793  *
1794  * This function adds a new VSI into scheduler tree
1795  */
1796 static int
1797 ice_sched_add_vsi_to_topo(struct ice_port_info *pi, u16 vsi_handle, u8 tc)
1798 {
1799         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1800         struct ice_sched_node *tc_node;
1801
1802         tc_node = ice_sched_get_tc_node(pi, tc);
1803         if (!tc_node)
1804                 return -EINVAL;
1805
1806         /* calculate number of supported nodes needed for this VSI */
1807         ice_sched_calc_vsi_support_nodes(pi, tc_node, num_nodes);
1808
1809         /* add VSI supported nodes to TC subtree */
1810         return ice_sched_add_vsi_support_nodes(pi, vsi_handle, tc_node,
1811                                                num_nodes);
1812 }
1813
1814 /**
1815  * ice_sched_update_vsi_child_nodes - update VSI child nodes
1816  * @pi: port information structure
1817  * @vsi_handle: software VSI handle
1818  * @tc: TC number
1819  * @new_numqs: new number of max queues
1820  * @owner: owner of this subtree
1821  *
1822  * This function updates the VSI child nodes based on the number of queues
1823  */
1824 static int
1825 ice_sched_update_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1826                                  u8 tc, u16 new_numqs, u8 owner)
1827 {
1828         u16 new_num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1829         struct ice_sched_node *vsi_node;
1830         struct ice_sched_node *tc_node;
1831         struct ice_vsi_ctx *vsi_ctx;
1832         struct ice_hw *hw = pi->hw;
1833         u16 prev_numqs;
1834         int status = 0;
1835
1836         tc_node = ice_sched_get_tc_node(pi, tc);
1837         if (!tc_node)
1838                 return -EIO;
1839
1840         vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1841         if (!vsi_node)
1842                 return -EIO;
1843
1844         vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1845         if (!vsi_ctx)
1846                 return -EINVAL;
1847
1848         if (owner == ICE_SCHED_NODE_OWNER_LAN)
1849                 prev_numqs = vsi_ctx->sched.max_lanq[tc];
1850         else
1851                 prev_numqs = vsi_ctx->sched.max_rdmaq[tc];
1852         /* num queues are not changed or less than the previous number */
1853         if (new_numqs <= prev_numqs)
1854                 return status;
1855         if (owner == ICE_SCHED_NODE_OWNER_LAN) {
1856                 status = ice_alloc_lan_q_ctx(hw, vsi_handle, tc, new_numqs);
1857                 if (status)
1858                         return status;
1859         } else {
1860                 status = ice_alloc_rdma_q_ctx(hw, vsi_handle, tc, new_numqs);
1861                 if (status)
1862                         return status;
1863         }
1864
1865         if (new_numqs)
1866                 ice_sched_calc_vsi_child_nodes(hw, new_numqs, new_num_nodes);
1867         /* Keep the max number of queue configuration all the time. Update the
1868          * tree only if number of queues > previous number of queues. This may
1869          * leave some extra nodes in the tree if number of queues < previous
1870          * number but that wouldn't harm anything. Removing those extra nodes
1871          * may complicate the code if those nodes are part of SRL or
1872          * individually rate limited.
1873          */
1874         status = ice_sched_add_vsi_child_nodes(pi, vsi_handle, tc_node,
1875                                                new_num_nodes, owner);
1876         if (status)
1877                 return status;
1878         if (owner == ICE_SCHED_NODE_OWNER_LAN)
1879                 vsi_ctx->sched.max_lanq[tc] = new_numqs;
1880         else
1881                 vsi_ctx->sched.max_rdmaq[tc] = new_numqs;
1882
1883         return 0;
1884 }
1885
1886 /**
1887  * ice_sched_cfg_vsi - configure the new/existing VSI
1888  * @pi: port information structure
1889  * @vsi_handle: software VSI handle
1890  * @tc: TC number
1891  * @maxqs: max number of queues
1892  * @owner: LAN or RDMA
1893  * @enable: TC enabled or disabled
1894  *
1895  * This function adds/updates VSI nodes based on the number of queues. If TC is
1896  * enabled and VSI is in suspended state then resume the VSI back. If TC is
1897  * disabled then suspend the VSI if it is not already.
1898  */
1899 int
1900 ice_sched_cfg_vsi(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 maxqs,
1901                   u8 owner, bool enable)
1902 {
1903         struct ice_sched_node *vsi_node, *tc_node;
1904         struct ice_vsi_ctx *vsi_ctx;
1905         struct ice_hw *hw = pi->hw;
1906         int status = 0;
1907
1908         ice_debug(pi->hw, ICE_DBG_SCHED, "add/config VSI %d\n", vsi_handle);
1909         tc_node = ice_sched_get_tc_node(pi, tc);
1910         if (!tc_node)
1911                 return -EINVAL;
1912         vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1913         if (!vsi_ctx)
1914                 return -EINVAL;
1915         vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1916
1917         /* suspend the VSI if TC is not enabled */
1918         if (!enable) {
1919                 if (vsi_node && vsi_node->in_use) {
1920                         u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1921
1922                         status = ice_sched_suspend_resume_elems(hw, 1, &teid,
1923                                                                 true);
1924                         if (!status)
1925                                 vsi_node->in_use = false;
1926                 }
1927                 return status;
1928         }
1929
1930         /* TC is enabled, if it is a new VSI then add it to the tree */
1931         if (!vsi_node) {
1932                 status = ice_sched_add_vsi_to_topo(pi, vsi_handle, tc);
1933                 if (status)
1934                         return status;
1935
1936                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1937                 if (!vsi_node)
1938                         return -EIO;
1939
1940                 vsi_ctx->sched.vsi_node[tc] = vsi_node;
1941                 vsi_node->in_use = true;
1942                 /* invalidate the max queues whenever VSI gets added first time
1943                  * into the scheduler tree (boot or after reset). We need to
1944                  * recreate the child nodes all the time in these cases.
1945                  */
1946                 vsi_ctx->sched.max_lanq[tc] = 0;
1947                 vsi_ctx->sched.max_rdmaq[tc] = 0;
1948         }
1949
1950         /* update the VSI child nodes */
1951         status = ice_sched_update_vsi_child_nodes(pi, vsi_handle, tc, maxqs,
1952                                                   owner);
1953         if (status)
1954                 return status;
1955
1956         /* TC is enabled, resume the VSI if it is in the suspend state */
1957         if (!vsi_node->in_use) {
1958                 u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1959
1960                 status = ice_sched_suspend_resume_elems(hw, 1, &teid, false);
1961                 if (!status)
1962                         vsi_node->in_use = true;
1963         }
1964
1965         return status;
1966 }
1967
1968 /**
1969  * ice_sched_rm_agg_vsi_info - remove aggregator related VSI info entry
1970  * @pi: port information structure
1971  * @vsi_handle: software VSI handle
1972  *
1973  * This function removes single aggregator VSI info entry from
1974  * aggregator list.
1975  */
1976 static void ice_sched_rm_agg_vsi_info(struct ice_port_info *pi, u16 vsi_handle)
1977 {
1978         struct ice_sched_agg_info *agg_info;
1979         struct ice_sched_agg_info *atmp;
1980
1981         list_for_each_entry_safe(agg_info, atmp, &pi->hw->agg_list,
1982                                  list_entry) {
1983                 struct ice_sched_agg_vsi_info *agg_vsi_info;
1984                 struct ice_sched_agg_vsi_info *vtmp;
1985
1986                 list_for_each_entry_safe(agg_vsi_info, vtmp,
1987                                          &agg_info->agg_vsi_list, list_entry)
1988                         if (agg_vsi_info->vsi_handle == vsi_handle) {
1989                                 list_del(&agg_vsi_info->list_entry);
1990                                 devm_kfree(ice_hw_to_dev(pi->hw),
1991                                            agg_vsi_info);
1992                                 return;
1993                         }
1994         }
1995 }
1996
1997 /**
1998  * ice_sched_is_leaf_node_present - check for a leaf node in the sub-tree
1999  * @node: pointer to the sub-tree node
2000  *
2001  * This function checks for a leaf node presence in a given sub-tree node.
2002  */
2003 static bool ice_sched_is_leaf_node_present(struct ice_sched_node *node)
2004 {
2005         u8 i;
2006
2007         for (i = 0; i < node->num_children; i++)
2008                 if (ice_sched_is_leaf_node_present(node->children[i]))
2009                         return true;
2010         /* check for a leaf node */
2011         return (node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF);
2012 }
2013
2014 /**
2015  * ice_sched_rm_vsi_cfg - remove the VSI and its children nodes
2016  * @pi: port information structure
2017  * @vsi_handle: software VSI handle
2018  * @owner: LAN or RDMA
2019  *
2020  * This function removes the VSI and its LAN or RDMA children nodes from the
2021  * scheduler tree.
2022  */
2023 static int
2024 ice_sched_rm_vsi_cfg(struct ice_port_info *pi, u16 vsi_handle, u8 owner)
2025 {
2026         struct ice_vsi_ctx *vsi_ctx;
2027         int status = -EINVAL;
2028         u8 i;
2029
2030         ice_debug(pi->hw, ICE_DBG_SCHED, "removing VSI %d\n", vsi_handle);
2031         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2032                 return status;
2033         mutex_lock(&pi->sched_lock);
2034         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
2035         if (!vsi_ctx)
2036                 goto exit_sched_rm_vsi_cfg;
2037
2038         ice_for_each_traffic_class(i) {
2039                 struct ice_sched_node *vsi_node, *tc_node;
2040                 u8 j = 0;
2041
2042                 tc_node = ice_sched_get_tc_node(pi, i);
2043                 if (!tc_node)
2044                         continue;
2045
2046                 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2047                 if (!vsi_node)
2048                         continue;
2049
2050                 if (ice_sched_is_leaf_node_present(vsi_node)) {
2051                         ice_debug(pi->hw, ICE_DBG_SCHED, "VSI has leaf nodes in TC %d\n", i);
2052                         status = -EBUSY;
2053                         goto exit_sched_rm_vsi_cfg;
2054                 }
2055                 while (j < vsi_node->num_children) {
2056                         if (vsi_node->children[j]->owner == owner) {
2057                                 ice_free_sched_node(pi, vsi_node->children[j]);
2058
2059                                 /* reset the counter again since the num
2060                                  * children will be updated after node removal
2061                                  */
2062                                 j = 0;
2063                         } else {
2064                                 j++;
2065                         }
2066                 }
2067                 /* remove the VSI if it has no children */
2068                 if (!vsi_node->num_children) {
2069                         ice_free_sched_node(pi, vsi_node);
2070                         vsi_ctx->sched.vsi_node[i] = NULL;
2071
2072                         /* clean up aggregator related VSI info if any */
2073                         ice_sched_rm_agg_vsi_info(pi, vsi_handle);
2074                 }
2075                 if (owner == ICE_SCHED_NODE_OWNER_LAN)
2076                         vsi_ctx->sched.max_lanq[i] = 0;
2077                 else
2078                         vsi_ctx->sched.max_rdmaq[i] = 0;
2079         }
2080         status = 0;
2081
2082 exit_sched_rm_vsi_cfg:
2083         mutex_unlock(&pi->sched_lock);
2084         return status;
2085 }
2086
2087 /**
2088  * ice_rm_vsi_lan_cfg - remove VSI and its LAN children nodes
2089  * @pi: port information structure
2090  * @vsi_handle: software VSI handle
2091  *
2092  * This function clears the VSI and its LAN children nodes from scheduler tree
2093  * for all TCs.
2094  */
2095 int ice_rm_vsi_lan_cfg(struct ice_port_info *pi, u16 vsi_handle)
2096 {
2097         return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_LAN);
2098 }
2099
2100 /**
2101  * ice_rm_vsi_rdma_cfg - remove VSI and its RDMA children nodes
2102  * @pi: port information structure
2103  * @vsi_handle: software VSI handle
2104  *
2105  * This function clears the VSI and its RDMA children nodes from scheduler tree
2106  * for all TCs.
2107  */
2108 int ice_rm_vsi_rdma_cfg(struct ice_port_info *pi, u16 vsi_handle)
2109 {
2110         return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_RDMA);
2111 }
2112
2113 /**
2114  * ice_get_agg_info - get the aggregator ID
2115  * @hw: pointer to the hardware structure
2116  * @agg_id: aggregator ID
2117  *
2118  * This function validates aggregator ID. The function returns info if
2119  * aggregator ID is present in list otherwise it returns null.
2120  */
2121 static struct ice_sched_agg_info *
2122 ice_get_agg_info(struct ice_hw *hw, u32 agg_id)
2123 {
2124         struct ice_sched_agg_info *agg_info;
2125
2126         list_for_each_entry(agg_info, &hw->agg_list, list_entry)
2127                 if (agg_info->agg_id == agg_id)
2128                         return agg_info;
2129
2130         return NULL;
2131 }
2132
2133 /**
2134  * ice_sched_get_free_vsi_parent - Find a free parent node in aggregator subtree
2135  * @hw: pointer to the HW struct
2136  * @node: pointer to a child node
2137  * @num_nodes: num nodes count array
2138  *
2139  * This function walks through the aggregator subtree to find a free parent
2140  * node
2141  */
2142 static struct ice_sched_node *
2143 ice_sched_get_free_vsi_parent(struct ice_hw *hw, struct ice_sched_node *node,
2144                               u16 *num_nodes)
2145 {
2146         u8 l = node->tx_sched_layer;
2147         u8 vsil, i;
2148
2149         vsil = ice_sched_get_vsi_layer(hw);
2150
2151         /* Is it VSI parent layer ? */
2152         if (l == vsil - 1)
2153                 return (node->num_children < hw->max_children[l]) ? node : NULL;
2154
2155         /* We have intermediate nodes. Let's walk through the subtree. If the
2156          * intermediate node has space to add a new node then clear the count
2157          */
2158         if (node->num_children < hw->max_children[l])
2159                 num_nodes[l] = 0;
2160         /* The below recursive call is intentional and wouldn't go more than
2161          * 2 or 3 iterations.
2162          */
2163
2164         for (i = 0; i < node->num_children; i++) {
2165                 struct ice_sched_node *parent;
2166
2167                 parent = ice_sched_get_free_vsi_parent(hw, node->children[i],
2168                                                        num_nodes);
2169                 if (parent)
2170                         return parent;
2171         }
2172
2173         return NULL;
2174 }
2175
2176 /**
2177  * ice_sched_update_parent - update the new parent in SW DB
2178  * @new_parent: pointer to a new parent node
2179  * @node: pointer to a child node
2180  *
2181  * This function removes the child from the old parent and adds it to a new
2182  * parent
2183  */
2184 void
2185 ice_sched_update_parent(struct ice_sched_node *new_parent,
2186                         struct ice_sched_node *node)
2187 {
2188         struct ice_sched_node *old_parent;
2189         u8 i, j;
2190
2191         old_parent = node->parent;
2192
2193         /* update the old parent children */
2194         for (i = 0; i < old_parent->num_children; i++)
2195                 if (old_parent->children[i] == node) {
2196                         for (j = i + 1; j < old_parent->num_children; j++)
2197                                 old_parent->children[j - 1] =
2198                                         old_parent->children[j];
2199                         old_parent->num_children--;
2200                         break;
2201                 }
2202
2203         /* now move the node to a new parent */
2204         new_parent->children[new_parent->num_children++] = node;
2205         node->parent = new_parent;
2206         node->info.parent_teid = new_parent->info.node_teid;
2207 }
2208
2209 /**
2210  * ice_sched_move_nodes - move child nodes to a given parent
2211  * @pi: port information structure
2212  * @parent: pointer to parent node
2213  * @num_items: number of child nodes to be moved
2214  * @list: pointer to child node teids
2215  *
2216  * This function move the child nodes to a given parent.
2217  */
2218 int
2219 ice_sched_move_nodes(struct ice_port_info *pi, struct ice_sched_node *parent,
2220                      u16 num_items, u32 *list)
2221 {
2222         struct ice_aqc_move_elem *buf;
2223         struct ice_sched_node *node;
2224         u16 i, grps_movd = 0;
2225         struct ice_hw *hw;
2226         int status = 0;
2227         u16 buf_len;
2228
2229         hw = pi->hw;
2230
2231         if (!parent || !num_items)
2232                 return -EINVAL;
2233
2234         /* Does parent have enough space */
2235         if (parent->num_children + num_items >
2236             hw->max_children[parent->tx_sched_layer])
2237                 return -ENOSPC;
2238
2239         buf_len = struct_size(buf, teid, 1);
2240         buf = kzalloc(buf_len, GFP_KERNEL);
2241         if (!buf)
2242                 return -ENOMEM;
2243
2244         for (i = 0; i < num_items; i++) {
2245                 node = ice_sched_find_node_by_teid(pi->root, list[i]);
2246                 if (!node) {
2247                         status = -EINVAL;
2248                         goto move_err_exit;
2249                 }
2250
2251                 buf->hdr.src_parent_teid = node->info.parent_teid;
2252                 buf->hdr.dest_parent_teid = parent->info.node_teid;
2253                 buf->teid[0] = node->info.node_teid;
2254                 buf->hdr.num_elems = cpu_to_le16(1);
2255                 status = ice_aq_move_sched_elems(hw, 1, buf, buf_len,
2256                                                  &grps_movd, NULL);
2257                 if (status && grps_movd != 1) {
2258                         status = -EIO;
2259                         goto move_err_exit;
2260                 }
2261
2262                 /* update the SW DB */
2263                 ice_sched_update_parent(parent, node);
2264         }
2265
2266 move_err_exit:
2267         kfree(buf);
2268         return status;
2269 }
2270
2271 /**
2272  * ice_sched_move_vsi_to_agg - move VSI to aggregator node
2273  * @pi: port information structure
2274  * @vsi_handle: software VSI handle
2275  * @agg_id: aggregator ID
2276  * @tc: TC number
2277  *
2278  * This function moves a VSI to an aggregator node or its subtree.
2279  * Intermediate nodes may be created if required.
2280  */
2281 static int
2282 ice_sched_move_vsi_to_agg(struct ice_port_info *pi, u16 vsi_handle, u32 agg_id,
2283                           u8 tc)
2284 {
2285         struct ice_sched_node *vsi_node, *agg_node, *tc_node, *parent;
2286         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2287         u32 first_node_teid, vsi_teid;
2288         u16 num_nodes_added;
2289         u8 aggl, vsil, i;
2290         int status;
2291
2292         tc_node = ice_sched_get_tc_node(pi, tc);
2293         if (!tc_node)
2294                 return -EIO;
2295
2296         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2297         if (!agg_node)
2298                 return -ENOENT;
2299
2300         vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2301         if (!vsi_node)
2302                 return -ENOENT;
2303
2304         /* Is this VSI already part of given aggregator? */
2305         if (ice_sched_find_node_in_subtree(pi->hw, agg_node, vsi_node))
2306                 return 0;
2307
2308         aggl = ice_sched_get_agg_layer(pi->hw);
2309         vsil = ice_sched_get_vsi_layer(pi->hw);
2310
2311         /* set intermediate node count to 1 between aggregator and VSI layers */
2312         for (i = aggl + 1; i < vsil; i++)
2313                 num_nodes[i] = 1;
2314
2315         /* Check if the aggregator subtree has any free node to add the VSI */
2316         for (i = 0; i < agg_node->num_children; i++) {
2317                 parent = ice_sched_get_free_vsi_parent(pi->hw,
2318                                                        agg_node->children[i],
2319                                                        num_nodes);
2320                 if (parent)
2321                         goto move_nodes;
2322         }
2323
2324         /* add new nodes */
2325         parent = agg_node;
2326         for (i = aggl + 1; i < vsil; i++) {
2327                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2328                                                       num_nodes[i],
2329                                                       &first_node_teid,
2330                                                       &num_nodes_added);
2331                 if (status || num_nodes[i] != num_nodes_added)
2332                         return -EIO;
2333
2334                 /* The newly added node can be a new parent for the next
2335                  * layer nodes
2336                  */
2337                 if (num_nodes_added)
2338                         parent = ice_sched_find_node_by_teid(tc_node,
2339                                                              first_node_teid);
2340                 else
2341                         parent = parent->children[0];
2342
2343                 if (!parent)
2344                         return -EIO;
2345         }
2346
2347 move_nodes:
2348         vsi_teid = le32_to_cpu(vsi_node->info.node_teid);
2349         return ice_sched_move_nodes(pi, parent, 1, &vsi_teid);
2350 }
2351
2352 /**
2353  * ice_move_all_vsi_to_dflt_agg - move all VSI(s) to default aggregator
2354  * @pi: port information structure
2355  * @agg_info: aggregator info
2356  * @tc: traffic class number
2357  * @rm_vsi_info: true or false
2358  *
2359  * This function move all the VSI(s) to the default aggregator and delete
2360  * aggregator VSI info based on passed in boolean parameter rm_vsi_info. The
2361  * caller holds the scheduler lock.
2362  */
2363 static int
2364 ice_move_all_vsi_to_dflt_agg(struct ice_port_info *pi,
2365                              struct ice_sched_agg_info *agg_info, u8 tc,
2366                              bool rm_vsi_info)
2367 {
2368         struct ice_sched_agg_vsi_info *agg_vsi_info;
2369         struct ice_sched_agg_vsi_info *tmp;
2370         int status = 0;
2371
2372         list_for_each_entry_safe(agg_vsi_info, tmp, &agg_info->agg_vsi_list,
2373                                  list_entry) {
2374                 u16 vsi_handle = agg_vsi_info->vsi_handle;
2375
2376                 /* Move VSI to default aggregator */
2377                 if (!ice_is_tc_ena(agg_vsi_info->tc_bitmap[0], tc))
2378                         continue;
2379
2380                 status = ice_sched_move_vsi_to_agg(pi, vsi_handle,
2381                                                    ICE_DFLT_AGG_ID, tc);
2382                 if (status)
2383                         break;
2384
2385                 clear_bit(tc, agg_vsi_info->tc_bitmap);
2386                 if (rm_vsi_info && !agg_vsi_info->tc_bitmap[0]) {
2387                         list_del(&agg_vsi_info->list_entry);
2388                         devm_kfree(ice_hw_to_dev(pi->hw), agg_vsi_info);
2389                 }
2390         }
2391
2392         return status;
2393 }
2394
2395 /**
2396  * ice_sched_is_agg_inuse - check whether the aggregator is in use or not
2397  * @pi: port information structure
2398  * @node: node pointer
2399  *
2400  * This function checks whether the aggregator is attached with any VSI or not.
2401  */
2402 static bool
2403 ice_sched_is_agg_inuse(struct ice_port_info *pi, struct ice_sched_node *node)
2404 {
2405         u8 vsil, i;
2406
2407         vsil = ice_sched_get_vsi_layer(pi->hw);
2408         if (node->tx_sched_layer < vsil - 1) {
2409                 for (i = 0; i < node->num_children; i++)
2410                         if (ice_sched_is_agg_inuse(pi, node->children[i]))
2411                                 return true;
2412                 return false;
2413         } else {
2414                 return node->num_children ? true : false;
2415         }
2416 }
2417
2418 /**
2419  * ice_sched_rm_agg_cfg - remove the aggregator node
2420  * @pi: port information structure
2421  * @agg_id: aggregator ID
2422  * @tc: TC number
2423  *
2424  * This function removes the aggregator node and intermediate nodes if any
2425  * from the given TC
2426  */
2427 static int
2428 ice_sched_rm_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2429 {
2430         struct ice_sched_node *tc_node, *agg_node;
2431         struct ice_hw *hw = pi->hw;
2432
2433         tc_node = ice_sched_get_tc_node(pi, tc);
2434         if (!tc_node)
2435                 return -EIO;
2436
2437         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2438         if (!agg_node)
2439                 return -ENOENT;
2440
2441         /* Can't remove the aggregator node if it has children */
2442         if (ice_sched_is_agg_inuse(pi, agg_node))
2443                 return -EBUSY;
2444
2445         /* need to remove the whole subtree if aggregator node is the
2446          * only child.
2447          */
2448         while (agg_node->tx_sched_layer > hw->sw_entry_point_layer) {
2449                 struct ice_sched_node *parent = agg_node->parent;
2450
2451                 if (!parent)
2452                         return -EIO;
2453
2454                 if (parent->num_children > 1)
2455                         break;
2456
2457                 agg_node = parent;
2458         }
2459
2460         ice_free_sched_node(pi, agg_node);
2461         return 0;
2462 }
2463
2464 /**
2465  * ice_rm_agg_cfg_tc - remove aggregator configuration for TC
2466  * @pi: port information structure
2467  * @agg_info: aggregator ID
2468  * @tc: TC number
2469  * @rm_vsi_info: bool value true or false
2470  *
2471  * This function removes aggregator reference to VSI of given TC. It removes
2472  * the aggregator configuration completely for requested TC. The caller needs
2473  * to hold the scheduler lock.
2474  */
2475 static int
2476 ice_rm_agg_cfg_tc(struct ice_port_info *pi, struct ice_sched_agg_info *agg_info,
2477                   u8 tc, bool rm_vsi_info)
2478 {
2479         int status = 0;
2480
2481         /* If nothing to remove - return success */
2482         if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2483                 goto exit_rm_agg_cfg_tc;
2484
2485         status = ice_move_all_vsi_to_dflt_agg(pi, agg_info, tc, rm_vsi_info);
2486         if (status)
2487                 goto exit_rm_agg_cfg_tc;
2488
2489         /* Delete aggregator node(s) */
2490         status = ice_sched_rm_agg_cfg(pi, agg_info->agg_id, tc);
2491         if (status)
2492                 goto exit_rm_agg_cfg_tc;
2493
2494         clear_bit(tc, agg_info->tc_bitmap);
2495 exit_rm_agg_cfg_tc:
2496         return status;
2497 }
2498
2499 /**
2500  * ice_save_agg_tc_bitmap - save aggregator TC bitmap
2501  * @pi: port information structure
2502  * @agg_id: aggregator ID
2503  * @tc_bitmap: 8 bits TC bitmap
2504  *
2505  * Save aggregator TC bitmap. This function needs to be called with scheduler
2506  * lock held.
2507  */
2508 static int
2509 ice_save_agg_tc_bitmap(struct ice_port_info *pi, u32 agg_id,
2510                        unsigned long *tc_bitmap)
2511 {
2512         struct ice_sched_agg_info *agg_info;
2513
2514         agg_info = ice_get_agg_info(pi->hw, agg_id);
2515         if (!agg_info)
2516                 return -EINVAL;
2517         bitmap_copy(agg_info->replay_tc_bitmap, tc_bitmap,
2518                     ICE_MAX_TRAFFIC_CLASS);
2519         return 0;
2520 }
2521
2522 /**
2523  * ice_sched_add_agg_cfg - create an aggregator node
2524  * @pi: port information structure
2525  * @agg_id: aggregator ID
2526  * @tc: TC number
2527  *
2528  * This function creates an aggregator node and intermediate nodes if required
2529  * for the given TC
2530  */
2531 static int
2532 ice_sched_add_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2533 {
2534         struct ice_sched_node *parent, *agg_node, *tc_node;
2535         u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2536         struct ice_hw *hw = pi->hw;
2537         u32 first_node_teid;
2538         u16 num_nodes_added;
2539         int status = 0;
2540         u8 i, aggl;
2541
2542         tc_node = ice_sched_get_tc_node(pi, tc);
2543         if (!tc_node)
2544                 return -EIO;
2545
2546         agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2547         /* Does Agg node already exist ? */
2548         if (agg_node)
2549                 return status;
2550
2551         aggl = ice_sched_get_agg_layer(hw);
2552
2553         /* need one node in Agg layer */
2554         num_nodes[aggl] = 1;
2555
2556         /* Check whether the intermediate nodes have space to add the
2557          * new aggregator. If they are full, then SW needs to allocate a new
2558          * intermediate node on those layers
2559          */
2560         for (i = hw->sw_entry_point_layer; i < aggl; i++) {
2561                 parent = ice_sched_get_first_node(pi, tc_node, i);
2562
2563                 /* scan all the siblings */
2564                 while (parent) {
2565                         if (parent->num_children < hw->max_children[i])
2566                                 break;
2567                         parent = parent->sibling;
2568                 }
2569
2570                 /* all the nodes are full, reserve one for this layer */
2571                 if (!parent)
2572                         num_nodes[i]++;
2573         }
2574
2575         /* add the aggregator node */
2576         parent = tc_node;
2577         for (i = hw->sw_entry_point_layer; i <= aggl; i++) {
2578                 if (!parent)
2579                         return -EIO;
2580
2581                 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2582                                                       num_nodes[i],
2583                                                       &first_node_teid,
2584                                                       &num_nodes_added);
2585                 if (status || num_nodes[i] != num_nodes_added)
2586                         return -EIO;
2587
2588                 /* The newly added node can be a new parent for the next
2589                  * layer nodes
2590                  */
2591                 if (num_nodes_added) {
2592                         parent = ice_sched_find_node_by_teid(tc_node,
2593                                                              first_node_teid);
2594                         /* register aggregator ID with the aggregator node */
2595                         if (parent && i == aggl)
2596                                 parent->agg_id = agg_id;
2597                 } else {
2598                         parent = parent->children[0];
2599                 }
2600         }
2601
2602         return 0;
2603 }
2604
2605 /**
2606  * ice_sched_cfg_agg - configure aggregator node
2607  * @pi: port information structure
2608  * @agg_id: aggregator ID
2609  * @agg_type: aggregator type queue, VSI, or aggregator group
2610  * @tc_bitmap: bits TC bitmap
2611  *
2612  * It registers a unique aggregator node into scheduler services. It
2613  * allows a user to register with a unique ID to track it's resources.
2614  * The aggregator type determines if this is a queue group, VSI group
2615  * or aggregator group. It then creates the aggregator node(s) for requested
2616  * TC(s) or removes an existing aggregator node including its configuration
2617  * if indicated via tc_bitmap. Call ice_rm_agg_cfg to release aggregator
2618  * resources and remove aggregator ID.
2619  * This function needs to be called with scheduler lock held.
2620  */
2621 static int
2622 ice_sched_cfg_agg(struct ice_port_info *pi, u32 agg_id,
2623                   enum ice_agg_type agg_type, unsigned long *tc_bitmap)
2624 {
2625         struct ice_sched_agg_info *agg_info;
2626         struct ice_hw *hw = pi->hw;
2627         int status = 0;
2628         u8 tc;
2629
2630         agg_info = ice_get_agg_info(hw, agg_id);
2631         if (!agg_info) {
2632                 /* Create new entry for new aggregator ID */
2633                 agg_info = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*agg_info),
2634                                         GFP_KERNEL);
2635                 if (!agg_info)
2636                         return -ENOMEM;
2637
2638                 agg_info->agg_id = agg_id;
2639                 agg_info->agg_type = agg_type;
2640                 agg_info->tc_bitmap[0] = 0;
2641
2642                 /* Initialize the aggregator VSI list head */
2643                 INIT_LIST_HEAD(&agg_info->agg_vsi_list);
2644
2645                 /* Add new entry in aggregator list */
2646                 list_add(&agg_info->list_entry, &hw->agg_list);
2647         }
2648         /* Create aggregator node(s) for requested TC(s) */
2649         ice_for_each_traffic_class(tc) {
2650                 if (!ice_is_tc_ena(*tc_bitmap, tc)) {
2651                         /* Delete aggregator cfg TC if it exists previously */
2652                         status = ice_rm_agg_cfg_tc(pi, agg_info, tc, false);
2653                         if (status)
2654                                 break;
2655                         continue;
2656                 }
2657
2658                 /* Check if aggregator node for TC already exists */
2659                 if (ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2660                         continue;
2661
2662                 /* Create new aggregator node for TC */
2663                 status = ice_sched_add_agg_cfg(pi, agg_id, tc);
2664                 if (status)
2665                         break;
2666
2667                 /* Save aggregator node's TC information */
2668                 set_bit(tc, agg_info->tc_bitmap);
2669         }
2670
2671         return status;
2672 }
2673
2674 /**
2675  * ice_cfg_agg - config aggregator node
2676  * @pi: port information structure
2677  * @agg_id: aggregator ID
2678  * @agg_type: aggregator type queue, VSI, or aggregator group
2679  * @tc_bitmap: bits TC bitmap
2680  *
2681  * This function configures aggregator node(s).
2682  */
2683 int
2684 ice_cfg_agg(struct ice_port_info *pi, u32 agg_id, enum ice_agg_type agg_type,
2685             u8 tc_bitmap)
2686 {
2687         unsigned long bitmap = tc_bitmap;
2688         int status;
2689
2690         mutex_lock(&pi->sched_lock);
2691         status = ice_sched_cfg_agg(pi, agg_id, agg_type, &bitmap);
2692         if (!status)
2693                 status = ice_save_agg_tc_bitmap(pi, agg_id, &bitmap);
2694         mutex_unlock(&pi->sched_lock);
2695         return status;
2696 }
2697
2698 /**
2699  * ice_get_agg_vsi_info - get the aggregator ID
2700  * @agg_info: aggregator info
2701  * @vsi_handle: software VSI handle
2702  *
2703  * The function returns aggregator VSI info based on VSI handle. This function
2704  * needs to be called with scheduler lock held.
2705  */
2706 static struct ice_sched_agg_vsi_info *
2707 ice_get_agg_vsi_info(struct ice_sched_agg_info *agg_info, u16 vsi_handle)
2708 {
2709         struct ice_sched_agg_vsi_info *agg_vsi_info;
2710
2711         list_for_each_entry(agg_vsi_info, &agg_info->agg_vsi_list, list_entry)
2712                 if (agg_vsi_info->vsi_handle == vsi_handle)
2713                         return agg_vsi_info;
2714
2715         return NULL;
2716 }
2717
2718 /**
2719  * ice_get_vsi_agg_info - get the aggregator info of VSI
2720  * @hw: pointer to the hardware structure
2721  * @vsi_handle: Sw VSI handle
2722  *
2723  * The function returns aggregator info of VSI represented via vsi_handle. The
2724  * VSI has in this case a different aggregator than the default one. This
2725  * function needs to be called with scheduler lock held.
2726  */
2727 static struct ice_sched_agg_info *
2728 ice_get_vsi_agg_info(struct ice_hw *hw, u16 vsi_handle)
2729 {
2730         struct ice_sched_agg_info *agg_info;
2731
2732         list_for_each_entry(agg_info, &hw->agg_list, list_entry) {
2733                 struct ice_sched_agg_vsi_info *agg_vsi_info;
2734
2735                 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2736                 if (agg_vsi_info)
2737                         return agg_info;
2738         }
2739         return NULL;
2740 }
2741
2742 /**
2743  * ice_save_agg_vsi_tc_bitmap - save aggregator VSI TC bitmap
2744  * @pi: port information structure
2745  * @agg_id: aggregator ID
2746  * @vsi_handle: software VSI handle
2747  * @tc_bitmap: TC bitmap of enabled TC(s)
2748  *
2749  * Save VSI to aggregator TC bitmap. This function needs to call with scheduler
2750  * lock held.
2751  */
2752 static int
2753 ice_save_agg_vsi_tc_bitmap(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2754                            unsigned long *tc_bitmap)
2755 {
2756         struct ice_sched_agg_vsi_info *agg_vsi_info;
2757         struct ice_sched_agg_info *agg_info;
2758
2759         agg_info = ice_get_agg_info(pi->hw, agg_id);
2760         if (!agg_info)
2761                 return -EINVAL;
2762         /* check if entry already exist */
2763         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2764         if (!agg_vsi_info)
2765                 return -EINVAL;
2766         bitmap_copy(agg_vsi_info->replay_tc_bitmap, tc_bitmap,
2767                     ICE_MAX_TRAFFIC_CLASS);
2768         return 0;
2769 }
2770
2771 /**
2772  * ice_sched_assoc_vsi_to_agg - associate/move VSI to new/default aggregator
2773  * @pi: port information structure
2774  * @agg_id: aggregator ID
2775  * @vsi_handle: software VSI handle
2776  * @tc_bitmap: TC bitmap of enabled TC(s)
2777  *
2778  * This function moves VSI to a new or default aggregator node. If VSI is
2779  * already associated to the aggregator node then no operation is performed on
2780  * the tree. This function needs to be called with scheduler lock held.
2781  */
2782 static int
2783 ice_sched_assoc_vsi_to_agg(struct ice_port_info *pi, u32 agg_id,
2784                            u16 vsi_handle, unsigned long *tc_bitmap)
2785 {
2786         struct ice_sched_agg_vsi_info *agg_vsi_info, *iter, *old_agg_vsi_info = NULL;
2787         struct ice_sched_agg_info *agg_info, *old_agg_info;
2788         struct ice_hw *hw = pi->hw;
2789         int status = 0;
2790         u8 tc;
2791
2792         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2793                 return -EINVAL;
2794         agg_info = ice_get_agg_info(hw, agg_id);
2795         if (!agg_info)
2796                 return -EINVAL;
2797         /* If the VSI is already part of another aggregator then update
2798          * its VSI info list
2799          */
2800         old_agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
2801         if (old_agg_info && old_agg_info != agg_info) {
2802                 struct ice_sched_agg_vsi_info *vtmp;
2803
2804                 list_for_each_entry_safe(iter, vtmp,
2805                                          &old_agg_info->agg_vsi_list,
2806                                          list_entry)
2807                         if (iter->vsi_handle == vsi_handle) {
2808                                 old_agg_vsi_info = iter;
2809                                 break;
2810                         }
2811         }
2812
2813         /* check if entry already exist */
2814         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2815         if (!agg_vsi_info) {
2816                 /* Create new entry for VSI under aggregator list */
2817                 agg_vsi_info = devm_kzalloc(ice_hw_to_dev(hw),
2818                                             sizeof(*agg_vsi_info), GFP_KERNEL);
2819                 if (!agg_vsi_info)
2820                         return -EINVAL;
2821
2822                 /* add VSI ID into the aggregator list */
2823                 agg_vsi_info->vsi_handle = vsi_handle;
2824                 list_add(&agg_vsi_info->list_entry, &agg_info->agg_vsi_list);
2825         }
2826         /* Move VSI node to new aggregator node for requested TC(s) */
2827         ice_for_each_traffic_class(tc) {
2828                 if (!ice_is_tc_ena(*tc_bitmap, tc))
2829                         continue;
2830
2831                 /* Move VSI to new aggregator */
2832                 status = ice_sched_move_vsi_to_agg(pi, vsi_handle, agg_id, tc);
2833                 if (status)
2834                         break;
2835
2836                 set_bit(tc, agg_vsi_info->tc_bitmap);
2837                 if (old_agg_vsi_info)
2838                         clear_bit(tc, old_agg_vsi_info->tc_bitmap);
2839         }
2840         if (old_agg_vsi_info && !old_agg_vsi_info->tc_bitmap[0]) {
2841                 list_del(&old_agg_vsi_info->list_entry);
2842                 devm_kfree(ice_hw_to_dev(pi->hw), old_agg_vsi_info);
2843         }
2844         return status;
2845 }
2846
2847 /**
2848  * ice_sched_rm_unused_rl_prof - remove unused RL profile
2849  * @pi: port information structure
2850  *
2851  * This function removes unused rate limit profiles from the HW and
2852  * SW DB. The caller needs to hold scheduler lock.
2853  */
2854 static void ice_sched_rm_unused_rl_prof(struct ice_port_info *pi)
2855 {
2856         u16 ln;
2857
2858         for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
2859                 struct ice_aqc_rl_profile_info *rl_prof_elem;
2860                 struct ice_aqc_rl_profile_info *rl_prof_tmp;
2861
2862                 list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
2863                                          &pi->rl_prof_list[ln], list_entry) {
2864                         if (!ice_sched_del_rl_profile(pi->hw, rl_prof_elem))
2865                                 ice_debug(pi->hw, ICE_DBG_SCHED, "Removed rl profile\n");
2866                 }
2867         }
2868 }
2869
2870 /**
2871  * ice_sched_update_elem - update element
2872  * @hw: pointer to the HW struct
2873  * @node: pointer to node
2874  * @info: node info to update
2875  *
2876  * Update the HW DB, and local SW DB of node. Update the scheduling
2877  * parameters of node from argument info data buffer (Info->data buf) and
2878  * returns success or error on config sched element failure. The caller
2879  * needs to hold scheduler lock.
2880  */
2881 static int
2882 ice_sched_update_elem(struct ice_hw *hw, struct ice_sched_node *node,
2883                       struct ice_aqc_txsched_elem_data *info)
2884 {
2885         struct ice_aqc_txsched_elem_data buf;
2886         u16 elem_cfgd = 0;
2887         u16 num_elems = 1;
2888         int status;
2889
2890         buf = *info;
2891         /* Parent TEID is reserved field in this aq call */
2892         buf.parent_teid = 0;
2893         /* Element type is reserved field in this aq call */
2894         buf.data.elem_type = 0;
2895         /* Flags is reserved field in this aq call */
2896         buf.data.flags = 0;
2897
2898         /* Update HW DB */
2899         /* Configure element node */
2900         status = ice_aq_cfg_sched_elems(hw, num_elems, &buf, sizeof(buf),
2901                                         &elem_cfgd, NULL);
2902         if (status || elem_cfgd != num_elems) {
2903                 ice_debug(hw, ICE_DBG_SCHED, "Config sched elem error\n");
2904                 return -EIO;
2905         }
2906
2907         /* Config success case */
2908         /* Now update local SW DB */
2909         /* Only copy the data portion of info buffer */
2910         node->info.data = info->data;
2911         return status;
2912 }
2913
2914 /**
2915  * ice_sched_cfg_node_bw_alloc - configure node BW weight/alloc params
2916  * @hw: pointer to the HW struct
2917  * @node: sched node to configure
2918  * @rl_type: rate limit type CIR, EIR, or shared
2919  * @bw_alloc: BW weight/allocation
2920  *
2921  * This function configures node element's BW allocation.
2922  */
2923 static int
2924 ice_sched_cfg_node_bw_alloc(struct ice_hw *hw, struct ice_sched_node *node,
2925                             enum ice_rl_type rl_type, u16 bw_alloc)
2926 {
2927         struct ice_aqc_txsched_elem_data buf;
2928         struct ice_aqc_txsched_elem *data;
2929
2930         buf = node->info;
2931         data = &buf.data;
2932         if (rl_type == ICE_MIN_BW) {
2933                 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
2934                 data->cir_bw.bw_alloc = cpu_to_le16(bw_alloc);
2935         } else if (rl_type == ICE_MAX_BW) {
2936                 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
2937                 data->eir_bw.bw_alloc = cpu_to_le16(bw_alloc);
2938         } else {
2939                 return -EINVAL;
2940         }
2941
2942         /* Configure element */
2943         return ice_sched_update_elem(hw, node, &buf);
2944 }
2945
2946 /**
2947  * ice_move_vsi_to_agg - moves VSI to new or default aggregator
2948  * @pi: port information structure
2949  * @agg_id: aggregator ID
2950  * @vsi_handle: software VSI handle
2951  * @tc_bitmap: TC bitmap of enabled TC(s)
2952  *
2953  * Move or associate VSI to a new or default aggregator node.
2954  */
2955 int
2956 ice_move_vsi_to_agg(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2957                     u8 tc_bitmap)
2958 {
2959         unsigned long bitmap = tc_bitmap;
2960         int status;
2961
2962         mutex_lock(&pi->sched_lock);
2963         status = ice_sched_assoc_vsi_to_agg(pi, agg_id, vsi_handle,
2964                                             (unsigned long *)&bitmap);
2965         if (!status)
2966                 status = ice_save_agg_vsi_tc_bitmap(pi, agg_id, vsi_handle,
2967                                                     (unsigned long *)&bitmap);
2968         mutex_unlock(&pi->sched_lock);
2969         return status;
2970 }
2971
2972 /**
2973  * ice_set_clear_cir_bw - set or clear CIR BW
2974  * @bw_t_info: bandwidth type information structure
2975  * @bw: bandwidth in Kbps - Kilo bits per sec
2976  *
2977  * Save or clear CIR bandwidth (BW) in the passed param bw_t_info.
2978  */
2979 static void ice_set_clear_cir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
2980 {
2981         if (bw == ICE_SCHED_DFLT_BW) {
2982                 clear_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
2983                 bw_t_info->cir_bw.bw = 0;
2984         } else {
2985                 /* Save type of BW information */
2986                 set_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
2987                 bw_t_info->cir_bw.bw = bw;
2988         }
2989 }
2990
2991 /**
2992  * ice_set_clear_eir_bw - set or clear EIR BW
2993  * @bw_t_info: bandwidth type information structure
2994  * @bw: bandwidth in Kbps - Kilo bits per sec
2995  *
2996  * Save or clear EIR bandwidth (BW) in the passed param bw_t_info.
2997  */
2998 static void ice_set_clear_eir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
2999 {
3000         if (bw == ICE_SCHED_DFLT_BW) {
3001                 clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3002                 bw_t_info->eir_bw.bw = 0;
3003         } else {
3004                 /* EIR BW and Shared BW profiles are mutually exclusive and
3005                  * hence only one of them may be set for any given element.
3006                  * First clear earlier saved shared BW information.
3007                  */
3008                 clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3009                 bw_t_info->shared_bw = 0;
3010                 /* save EIR BW information */
3011                 set_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3012                 bw_t_info->eir_bw.bw = bw;
3013         }
3014 }
3015
3016 /**
3017  * ice_set_clear_shared_bw - set or clear shared BW
3018  * @bw_t_info: bandwidth type information structure
3019  * @bw: bandwidth in Kbps - Kilo bits per sec
3020  *
3021  * Save or clear shared bandwidth (BW) in the passed param bw_t_info.
3022  */
3023 static void ice_set_clear_shared_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3024 {
3025         if (bw == ICE_SCHED_DFLT_BW) {
3026                 clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3027                 bw_t_info->shared_bw = 0;
3028         } else {
3029                 /* EIR BW and Shared BW profiles are mutually exclusive and
3030                  * hence only one of them may be set for any given element.
3031                  * First clear earlier saved EIR BW information.
3032                  */
3033                 clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3034                 bw_t_info->eir_bw.bw = 0;
3035                 /* save shared BW information */
3036                 set_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3037                 bw_t_info->shared_bw = bw;
3038         }
3039 }
3040
3041 /**
3042  * ice_sched_save_vsi_bw - save VSI node's BW information
3043  * @pi: port information structure
3044  * @vsi_handle: sw VSI handle
3045  * @tc: traffic class
3046  * @rl_type: rate limit type min, max, or shared
3047  * @bw: bandwidth in Kbps - Kilo bits per sec
3048  *
3049  * Save BW information of VSI type node for post replay use.
3050  */
3051 static int
3052 ice_sched_save_vsi_bw(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3053                       enum ice_rl_type rl_type, u32 bw)
3054 {
3055         struct ice_vsi_ctx *vsi_ctx;
3056
3057         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3058                 return -EINVAL;
3059         vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3060         if (!vsi_ctx)
3061                 return -EINVAL;
3062         switch (rl_type) {
3063         case ICE_MIN_BW:
3064                 ice_set_clear_cir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3065                 break;
3066         case ICE_MAX_BW:
3067                 ice_set_clear_eir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3068                 break;
3069         case ICE_SHARED_BW:
3070                 ice_set_clear_shared_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3071                 break;
3072         default:
3073                 return -EINVAL;
3074         }
3075         return 0;
3076 }
3077
3078 /**
3079  * ice_sched_calc_wakeup - calculate RL profile wakeup parameter
3080  * @hw: pointer to the HW struct
3081  * @bw: bandwidth in Kbps
3082  *
3083  * This function calculates the wakeup parameter of RL profile.
3084  */
3085 static u16 ice_sched_calc_wakeup(struct ice_hw *hw, s32 bw)
3086 {
3087         s64 bytes_per_sec, wakeup_int, wakeup_a, wakeup_b, wakeup_f;
3088         s32 wakeup_f_int;
3089         u16 wakeup = 0;
3090
3091         /* Get the wakeup integer value */
3092         bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
3093         wakeup_int = div64_long(hw->psm_clk_freq, bytes_per_sec);
3094         if (wakeup_int > 63) {
3095                 wakeup = (u16)((1 << 15) | wakeup_int);
3096         } else {
3097                 /* Calculate fraction value up to 4 decimals
3098                  * Convert Integer value to a constant multiplier
3099                  */
3100                 wakeup_b = (s64)ICE_RL_PROF_MULTIPLIER * wakeup_int;
3101                 wakeup_a = div64_long((s64)ICE_RL_PROF_MULTIPLIER *
3102                                            hw->psm_clk_freq, bytes_per_sec);
3103
3104                 /* Get Fraction value */
3105                 wakeup_f = wakeup_a - wakeup_b;
3106
3107                 /* Round up the Fractional value via Ceil(Fractional value) */
3108                 if (wakeup_f > div64_long(ICE_RL_PROF_MULTIPLIER, 2))
3109                         wakeup_f += 1;
3110
3111                 wakeup_f_int = (s32)div64_long(wakeup_f * ICE_RL_PROF_FRACTION,
3112                                                ICE_RL_PROF_MULTIPLIER);
3113                 wakeup |= (u16)(wakeup_int << 9);
3114                 wakeup |= (u16)(0x1ff & wakeup_f_int);
3115         }
3116
3117         return wakeup;
3118 }
3119
3120 /**
3121  * ice_sched_bw_to_rl_profile - convert BW to profile parameters
3122  * @hw: pointer to the HW struct
3123  * @bw: bandwidth in Kbps
3124  * @profile: profile parameters to return
3125  *
3126  * This function converts the BW to profile structure format.
3127  */
3128 static int
3129 ice_sched_bw_to_rl_profile(struct ice_hw *hw, u32 bw,
3130                            struct ice_aqc_rl_profile_elem *profile)
3131 {
3132         s64 bytes_per_sec, ts_rate, mv_tmp;
3133         int status = -EINVAL;
3134         bool found = false;
3135         s32 encode = 0;
3136         s64 mv = 0;
3137         s32 i;
3138
3139         /* Bw settings range is from 0.5Mb/sec to 100Gb/sec */
3140         if (bw < ICE_SCHED_MIN_BW || bw > ICE_SCHED_MAX_BW)
3141                 return status;
3142
3143         /* Bytes per second from Kbps */
3144         bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
3145
3146         /* encode is 6 bits but really useful are 5 bits */
3147         for (i = 0; i < 64; i++) {
3148                 u64 pow_result = BIT_ULL(i);
3149
3150                 ts_rate = div64_long((s64)hw->psm_clk_freq,
3151                                      pow_result * ICE_RL_PROF_TS_MULTIPLIER);
3152                 if (ts_rate <= 0)
3153                         continue;
3154
3155                 /* Multiplier value */
3156                 mv_tmp = div64_long(bytes_per_sec * ICE_RL_PROF_MULTIPLIER,
3157                                     ts_rate);
3158
3159                 /* Round to the nearest ICE_RL_PROF_MULTIPLIER */
3160                 mv = round_up_64bit(mv_tmp, ICE_RL_PROF_MULTIPLIER);
3161
3162                 /* First multiplier value greater than the given
3163                  * accuracy bytes
3164                  */
3165                 if (mv > ICE_RL_PROF_ACCURACY_BYTES) {
3166                         encode = i;
3167                         found = true;
3168                         break;
3169                 }
3170         }
3171         if (found) {
3172                 u16 wm;
3173
3174                 wm = ice_sched_calc_wakeup(hw, bw);
3175                 profile->rl_multiply = cpu_to_le16(mv);
3176                 profile->wake_up_calc = cpu_to_le16(wm);
3177                 profile->rl_encode = cpu_to_le16(encode);
3178                 status = 0;
3179         } else {
3180                 status = -ENOENT;
3181         }
3182
3183         return status;
3184 }
3185
3186 /**
3187  * ice_sched_add_rl_profile - add RL profile
3188  * @pi: port information structure
3189  * @rl_type: type of rate limit BW - min, max, or shared
3190  * @bw: bandwidth in Kbps - Kilo bits per sec
3191  * @layer_num: specifies in which layer to create profile
3192  *
3193  * This function first checks the existing list for corresponding BW
3194  * parameter. If it exists, it returns the associated profile otherwise
3195  * it creates a new rate limit profile for requested BW, and adds it to
3196  * the HW DB and local list. It returns the new profile or null on error.
3197  * The caller needs to hold the scheduler lock.
3198  */
3199 static struct ice_aqc_rl_profile_info *
3200 ice_sched_add_rl_profile(struct ice_port_info *pi,
3201                          enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3202 {
3203         struct ice_aqc_rl_profile_info *rl_prof_elem;
3204         u16 profiles_added = 0, num_profiles = 1;
3205         struct ice_aqc_rl_profile_elem *buf;
3206         struct ice_hw *hw;
3207         u8 profile_type;
3208         int status;
3209
3210         if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
3211                 return NULL;
3212         switch (rl_type) {
3213         case ICE_MIN_BW:
3214                 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3215                 break;
3216         case ICE_MAX_BW:
3217                 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3218                 break;
3219         case ICE_SHARED_BW:
3220                 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3221                 break;
3222         default:
3223                 return NULL;
3224         }
3225
3226         if (!pi)
3227                 return NULL;
3228         hw = pi->hw;
3229         list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
3230                             list_entry)
3231                 if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
3232                     profile_type && rl_prof_elem->bw == bw)
3233                         /* Return existing profile ID info */
3234                         return rl_prof_elem;
3235
3236         /* Create new profile ID */
3237         rl_prof_elem = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*rl_prof_elem),
3238                                     GFP_KERNEL);
3239
3240         if (!rl_prof_elem)
3241                 return NULL;
3242
3243         status = ice_sched_bw_to_rl_profile(hw, bw, &rl_prof_elem->profile);
3244         if (status)
3245                 goto exit_add_rl_prof;
3246
3247         rl_prof_elem->bw = bw;
3248         /* layer_num is zero relative, and fw expects level from 1 to 9 */
3249         rl_prof_elem->profile.level = layer_num + 1;
3250         rl_prof_elem->profile.flags = profile_type;
3251         rl_prof_elem->profile.max_burst_size = cpu_to_le16(hw->max_burst_size);
3252
3253         /* Create new entry in HW DB */
3254         buf = &rl_prof_elem->profile;
3255         status = ice_aq_add_rl_profile(hw, num_profiles, buf, sizeof(*buf),
3256                                        &profiles_added, NULL);
3257         if (status || profiles_added != num_profiles)
3258                 goto exit_add_rl_prof;
3259
3260         /* Good entry - add in the list */
3261         rl_prof_elem->prof_id_ref = 0;
3262         list_add(&rl_prof_elem->list_entry, &pi->rl_prof_list[layer_num]);
3263         return rl_prof_elem;
3264
3265 exit_add_rl_prof:
3266         devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
3267         return NULL;
3268 }
3269
3270 /**
3271  * ice_sched_cfg_node_bw_lmt - configure node sched params
3272  * @hw: pointer to the HW struct
3273  * @node: sched node to configure
3274  * @rl_type: rate limit type CIR, EIR, or shared
3275  * @rl_prof_id: rate limit profile ID
3276  *
3277  * This function configures node element's BW limit.
3278  */
3279 static int
3280 ice_sched_cfg_node_bw_lmt(struct ice_hw *hw, struct ice_sched_node *node,
3281                           enum ice_rl_type rl_type, u16 rl_prof_id)
3282 {
3283         struct ice_aqc_txsched_elem_data buf;
3284         struct ice_aqc_txsched_elem *data;
3285
3286         buf = node->info;
3287         data = &buf.data;
3288         switch (rl_type) {
3289         case ICE_MIN_BW:
3290                 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
3291                 data->cir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
3292                 break;
3293         case ICE_MAX_BW:
3294                 /* EIR BW and Shared BW profiles are mutually exclusive and
3295                  * hence only one of them may be set for any given element
3296                  */
3297                 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3298                         return -EIO;
3299                 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3300                 data->eir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
3301                 break;
3302         case ICE_SHARED_BW:
3303                 /* Check for removing shared BW */
3304                 if (rl_prof_id == ICE_SCHED_NO_SHARED_RL_PROF_ID) {
3305                         /* remove shared profile */
3306                         data->valid_sections &= ~ICE_AQC_ELEM_VALID_SHARED;
3307                         data->srl_id = 0; /* clear SRL field */
3308
3309                         /* enable back EIR to default profile */
3310                         data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3311                         data->eir_bw.bw_profile_idx =
3312                                 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
3313                         break;
3314                 }
3315                 /* EIR BW and Shared BW profiles are mutually exclusive and
3316                  * hence only one of them may be set for any given element
3317                  */
3318                 if ((data->valid_sections & ICE_AQC_ELEM_VALID_EIR) &&
3319                     (le16_to_cpu(data->eir_bw.bw_profile_idx) !=
3320                             ICE_SCHED_DFLT_RL_PROF_ID))
3321                         return -EIO;
3322                 /* EIR BW is set to default, disable it */
3323                 data->valid_sections &= ~ICE_AQC_ELEM_VALID_EIR;
3324                 /* Okay to enable shared BW now */
3325                 data->valid_sections |= ICE_AQC_ELEM_VALID_SHARED;
3326                 data->srl_id = cpu_to_le16(rl_prof_id);
3327                 break;
3328         default:
3329                 /* Unknown rate limit type */
3330                 return -EINVAL;
3331         }
3332
3333         /* Configure element */
3334         return ice_sched_update_elem(hw, node, &buf);
3335 }
3336
3337 /**
3338  * ice_sched_get_node_rl_prof_id - get node's rate limit profile ID
3339  * @node: sched node
3340  * @rl_type: rate limit type
3341  *
3342  * If existing profile matches, it returns the corresponding rate
3343  * limit profile ID, otherwise it returns an invalid ID as error.
3344  */
3345 static u16
3346 ice_sched_get_node_rl_prof_id(struct ice_sched_node *node,
3347                               enum ice_rl_type rl_type)
3348 {
3349         u16 rl_prof_id = ICE_SCHED_INVAL_PROF_ID;
3350         struct ice_aqc_txsched_elem *data;
3351
3352         data = &node->info.data;
3353         switch (rl_type) {
3354         case ICE_MIN_BW:
3355                 if (data->valid_sections & ICE_AQC_ELEM_VALID_CIR)
3356                         rl_prof_id = le16_to_cpu(data->cir_bw.bw_profile_idx);
3357                 break;
3358         case ICE_MAX_BW:
3359                 if (data->valid_sections & ICE_AQC_ELEM_VALID_EIR)
3360                         rl_prof_id = le16_to_cpu(data->eir_bw.bw_profile_idx);
3361                 break;
3362         case ICE_SHARED_BW:
3363                 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3364                         rl_prof_id = le16_to_cpu(data->srl_id);
3365                 break;
3366         default:
3367                 break;
3368         }
3369
3370         return rl_prof_id;
3371 }
3372
3373 /**
3374  * ice_sched_get_rl_prof_layer - selects rate limit profile creation layer
3375  * @pi: port information structure
3376  * @rl_type: type of rate limit BW - min, max, or shared
3377  * @layer_index: layer index
3378  *
3379  * This function returns requested profile creation layer.
3380  */
3381 static u8
3382 ice_sched_get_rl_prof_layer(struct ice_port_info *pi, enum ice_rl_type rl_type,
3383                             u8 layer_index)
3384 {
3385         struct ice_hw *hw = pi->hw;
3386
3387         if (layer_index >= hw->num_tx_sched_layers)
3388                 return ICE_SCHED_INVAL_LAYER_NUM;
3389         switch (rl_type) {
3390         case ICE_MIN_BW:
3391                 if (hw->layer_info[layer_index].max_cir_rl_profiles)
3392                         return layer_index;
3393                 break;
3394         case ICE_MAX_BW:
3395                 if (hw->layer_info[layer_index].max_eir_rl_profiles)
3396                         return layer_index;
3397                 break;
3398         case ICE_SHARED_BW:
3399                 /* if current layer doesn't support SRL profile creation
3400                  * then try a layer up or down.
3401                  */
3402                 if (hw->layer_info[layer_index].max_srl_profiles)
3403                         return layer_index;
3404                 else if (layer_index < hw->num_tx_sched_layers - 1 &&
3405                          hw->layer_info[layer_index + 1].max_srl_profiles)
3406                         return layer_index + 1;
3407                 else if (layer_index > 0 &&
3408                          hw->layer_info[layer_index - 1].max_srl_profiles)
3409                         return layer_index - 1;
3410                 break;
3411         default:
3412                 break;
3413         }
3414         return ICE_SCHED_INVAL_LAYER_NUM;
3415 }
3416
3417 /**
3418  * ice_sched_get_srl_node - get shared rate limit node
3419  * @node: tree node
3420  * @srl_layer: shared rate limit layer
3421  *
3422  * This function returns SRL node to be used for shared rate limit purpose.
3423  * The caller needs to hold scheduler lock.
3424  */
3425 static struct ice_sched_node *
3426 ice_sched_get_srl_node(struct ice_sched_node *node, u8 srl_layer)
3427 {
3428         if (srl_layer > node->tx_sched_layer)
3429                 return node->children[0];
3430         else if (srl_layer < node->tx_sched_layer)
3431                 /* Node can't be created without a parent. It will always
3432                  * have a valid parent except root node.
3433                  */
3434                 return node->parent;
3435         else
3436                 return node;
3437 }
3438
3439 /**
3440  * ice_sched_rm_rl_profile - remove RL profile ID
3441  * @pi: port information structure
3442  * @layer_num: layer number where profiles are saved
3443  * @profile_type: profile type like EIR, CIR, or SRL
3444  * @profile_id: profile ID to remove
3445  *
3446  * This function removes rate limit profile from layer 'layer_num' of type
3447  * 'profile_type' and profile ID as 'profile_id'. The caller needs to hold
3448  * scheduler lock.
3449  */
3450 static int
3451 ice_sched_rm_rl_profile(struct ice_port_info *pi, u8 layer_num, u8 profile_type,
3452                         u16 profile_id)
3453 {
3454         struct ice_aqc_rl_profile_info *rl_prof_elem;
3455         int status = 0;
3456
3457         if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
3458                 return -EINVAL;
3459         /* Check the existing list for RL profile */
3460         list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
3461                             list_entry)
3462                 if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
3463                     profile_type &&
3464                     le16_to_cpu(rl_prof_elem->profile.profile_id) ==
3465                     profile_id) {
3466                         if (rl_prof_elem->prof_id_ref)
3467                                 rl_prof_elem->prof_id_ref--;
3468
3469                         /* Remove old profile ID from database */
3470                         status = ice_sched_del_rl_profile(pi->hw, rl_prof_elem);
3471                         if (status && status != -EBUSY)
3472                                 ice_debug(pi->hw, ICE_DBG_SCHED, "Remove rl profile failed\n");
3473                         break;
3474                 }
3475         if (status == -EBUSY)
3476                 status = 0;
3477         return status;
3478 }
3479
3480 /**
3481  * ice_sched_set_node_bw_dflt - set node's bandwidth limit to default
3482  * @pi: port information structure
3483  * @node: pointer to node structure
3484  * @rl_type: rate limit type min, max, or shared
3485  * @layer_num: layer number where RL profiles are saved
3486  *
3487  * This function configures node element's BW rate limit profile ID of
3488  * type CIR, EIR, or SRL to default. This function needs to be called
3489  * with the scheduler lock held.
3490  */
3491 static int
3492 ice_sched_set_node_bw_dflt(struct ice_port_info *pi,
3493                            struct ice_sched_node *node,
3494                            enum ice_rl_type rl_type, u8 layer_num)
3495 {
3496         struct ice_hw *hw;
3497         u8 profile_type;
3498         u16 rl_prof_id;
3499         u16 old_id;
3500         int status;
3501
3502         hw = pi->hw;
3503         switch (rl_type) {
3504         case ICE_MIN_BW:
3505                 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3506                 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
3507                 break;
3508         case ICE_MAX_BW:
3509                 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3510                 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
3511                 break;
3512         case ICE_SHARED_BW:
3513                 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3514                 /* No SRL is configured for default case */
3515                 rl_prof_id = ICE_SCHED_NO_SHARED_RL_PROF_ID;
3516                 break;
3517         default:
3518                 return -EINVAL;
3519         }
3520         /* Save existing RL prof ID for later clean up */
3521         old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
3522         /* Configure BW scheduling parameters */
3523         status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
3524         if (status)
3525                 return status;
3526
3527         /* Remove stale RL profile ID */
3528         if (old_id == ICE_SCHED_DFLT_RL_PROF_ID ||
3529             old_id == ICE_SCHED_INVAL_PROF_ID)
3530                 return 0;
3531
3532         return ice_sched_rm_rl_profile(pi, layer_num, profile_type, old_id);
3533 }
3534
3535 /**
3536  * ice_sched_set_eir_srl_excl - set EIR/SRL exclusiveness
3537  * @pi: port information structure
3538  * @node: pointer to node structure
3539  * @layer_num: layer number where rate limit profiles are saved
3540  * @rl_type: rate limit type min, max, or shared
3541  * @bw: bandwidth value
3542  *
3543  * This function prepares node element's bandwidth to SRL or EIR exclusively.
3544  * EIR BW and Shared BW profiles are mutually exclusive and hence only one of
3545  * them may be set for any given element. This function needs to be called
3546  * with the scheduler lock held.
3547  */
3548 static int
3549 ice_sched_set_eir_srl_excl(struct ice_port_info *pi,
3550                            struct ice_sched_node *node,
3551                            u8 layer_num, enum ice_rl_type rl_type, u32 bw)
3552 {
3553         if (rl_type == ICE_SHARED_BW) {
3554                 /* SRL node passed in this case, it may be different node */
3555                 if (bw == ICE_SCHED_DFLT_BW)
3556                         /* SRL being removed, ice_sched_cfg_node_bw_lmt()
3557                          * enables EIR to default. EIR is not set in this
3558                          * case, so no additional action is required.
3559                          */
3560                         return 0;
3561
3562                 /* SRL being configured, set EIR to default here.
3563                  * ice_sched_cfg_node_bw_lmt() disables EIR when it
3564                  * configures SRL
3565                  */
3566                 return ice_sched_set_node_bw_dflt(pi, node, ICE_MAX_BW,
3567                                                   layer_num);
3568         } else if (rl_type == ICE_MAX_BW &&
3569                    node->info.data.valid_sections & ICE_AQC_ELEM_VALID_SHARED) {
3570                 /* Remove Shared profile. Set default shared BW call
3571                  * removes shared profile for a node.
3572                  */
3573                 return ice_sched_set_node_bw_dflt(pi, node,
3574                                                   ICE_SHARED_BW,
3575                                                   layer_num);
3576         }
3577         return 0;
3578 }
3579
3580 /**
3581  * ice_sched_set_node_bw - set node's bandwidth
3582  * @pi: port information structure
3583  * @node: tree node
3584  * @rl_type: rate limit type min, max, or shared
3585  * @bw: bandwidth in Kbps - Kilo bits per sec
3586  * @layer_num: layer number
3587  *
3588  * This function adds new profile corresponding to requested BW, configures
3589  * node's RL profile ID of type CIR, EIR, or SRL, and removes old profile
3590  * ID from local database. The caller needs to hold scheduler lock.
3591  */
3592 int
3593 ice_sched_set_node_bw(struct ice_port_info *pi, struct ice_sched_node *node,
3594                       enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3595 {
3596         struct ice_aqc_rl_profile_info *rl_prof_info;
3597         struct ice_hw *hw = pi->hw;
3598         u16 old_id, rl_prof_id;
3599         int status = -EINVAL;
3600
3601         rl_prof_info = ice_sched_add_rl_profile(pi, rl_type, bw, layer_num);
3602         if (!rl_prof_info)
3603                 return status;
3604
3605         rl_prof_id = le16_to_cpu(rl_prof_info->profile.profile_id);
3606
3607         /* Save existing RL prof ID for later clean up */
3608         old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
3609         /* Configure BW scheduling parameters */
3610         status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
3611         if (status)
3612                 return status;
3613
3614         /* New changes has been applied */
3615         /* Increment the profile ID reference count */
3616         rl_prof_info->prof_id_ref++;
3617
3618         /* Check for old ID removal */
3619         if ((old_id == ICE_SCHED_DFLT_RL_PROF_ID && rl_type != ICE_SHARED_BW) ||
3620             old_id == ICE_SCHED_INVAL_PROF_ID || old_id == rl_prof_id)
3621                 return 0;
3622
3623         return ice_sched_rm_rl_profile(pi, layer_num,
3624                                        rl_prof_info->profile.flags &
3625                                        ICE_AQC_RL_PROFILE_TYPE_M, old_id);
3626 }
3627
3628 /**
3629  * ice_sched_set_node_priority - set node's priority
3630  * @pi: port information structure
3631  * @node: tree node
3632  * @priority: number 0-7 representing priority among siblings
3633  *
3634  * This function sets priority of a node among it's siblings.
3635  */
3636 int
3637 ice_sched_set_node_priority(struct ice_port_info *pi, struct ice_sched_node *node,
3638                             u16 priority)
3639 {
3640         struct ice_aqc_txsched_elem_data buf;
3641         struct ice_aqc_txsched_elem *data;
3642
3643         buf = node->info;
3644         data = &buf.data;
3645
3646         data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
3647         data->generic |= FIELD_PREP(ICE_AQC_ELEM_GENERIC_PRIO_M, priority);
3648
3649         return ice_sched_update_elem(pi->hw, node, &buf);
3650 }
3651
3652 /**
3653  * ice_sched_set_node_weight - set node's weight
3654  * @pi: port information structure
3655  * @node: tree node
3656  * @weight: number 1-200 representing weight for WFQ
3657  *
3658  * This function sets weight of the node for WFQ algorithm.
3659  */
3660 int
3661 ice_sched_set_node_weight(struct ice_port_info *pi, struct ice_sched_node *node, u16 weight)
3662 {
3663         struct ice_aqc_txsched_elem_data buf;
3664         struct ice_aqc_txsched_elem *data;
3665
3666         buf = node->info;
3667         data = &buf.data;
3668
3669         data->valid_sections = ICE_AQC_ELEM_VALID_CIR | ICE_AQC_ELEM_VALID_EIR |
3670                                ICE_AQC_ELEM_VALID_GENERIC;
3671         data->cir_bw.bw_alloc = cpu_to_le16(weight);
3672         data->eir_bw.bw_alloc = cpu_to_le16(weight);
3673
3674         data->generic |= FIELD_PREP(ICE_AQC_ELEM_GENERIC_SP_M, 0x0);
3675
3676         return ice_sched_update_elem(pi->hw, node, &buf);
3677 }
3678
3679 /**
3680  * ice_sched_set_node_bw_lmt - set node's BW limit
3681  * @pi: port information structure
3682  * @node: tree node
3683  * @rl_type: rate limit type min, max, or shared
3684  * @bw: bandwidth in Kbps - Kilo bits per sec
3685  *
3686  * It updates node's BW limit parameters like BW RL profile ID of type CIR,
3687  * EIR, or SRL. The caller needs to hold scheduler lock.
3688  */
3689 int
3690 ice_sched_set_node_bw_lmt(struct ice_port_info *pi, struct ice_sched_node *node,
3691                           enum ice_rl_type rl_type, u32 bw)
3692 {
3693         struct ice_sched_node *cfg_node = node;
3694         int status;
3695
3696         struct ice_hw *hw;
3697         u8 layer_num;
3698
3699         if (!pi)
3700                 return -EINVAL;
3701         hw = pi->hw;
3702         /* Remove unused RL profile IDs from HW and SW DB */
3703         ice_sched_rm_unused_rl_prof(pi);
3704         layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
3705                                                 node->tx_sched_layer);
3706         if (layer_num >= hw->num_tx_sched_layers)
3707                 return -EINVAL;
3708
3709         if (rl_type == ICE_SHARED_BW) {
3710                 /* SRL node may be different */
3711                 cfg_node = ice_sched_get_srl_node(node, layer_num);
3712                 if (!cfg_node)
3713                         return -EIO;
3714         }
3715         /* EIR BW and Shared BW profiles are mutually exclusive and
3716          * hence only one of them may be set for any given element
3717          */
3718         status = ice_sched_set_eir_srl_excl(pi, cfg_node, layer_num, rl_type,
3719                                             bw);
3720         if (status)
3721                 return status;
3722         if (bw == ICE_SCHED_DFLT_BW)
3723                 return ice_sched_set_node_bw_dflt(pi, cfg_node, rl_type,
3724                                                   layer_num);
3725         return ice_sched_set_node_bw(pi, cfg_node, rl_type, bw, layer_num);
3726 }
3727
3728 /**
3729  * ice_sched_set_node_bw_dflt_lmt - set node's BW limit to default
3730  * @pi: port information structure
3731  * @node: pointer to node structure
3732  * @rl_type: rate limit type min, max, or shared
3733  *
3734  * This function configures node element's BW rate limit profile ID of
3735  * type CIR, EIR, or SRL to default. This function needs to be called
3736  * with the scheduler lock held.
3737  */
3738 static int
3739 ice_sched_set_node_bw_dflt_lmt(struct ice_port_info *pi,
3740                                struct ice_sched_node *node,
3741                                enum ice_rl_type rl_type)
3742 {
3743         return ice_sched_set_node_bw_lmt(pi, node, rl_type,
3744                                          ICE_SCHED_DFLT_BW);
3745 }
3746
3747 /**
3748  * ice_sched_validate_srl_node - Check node for SRL applicability
3749  * @node: sched node to configure
3750  * @sel_layer: selected SRL layer
3751  *
3752  * This function checks if the SRL can be applied to a selected layer node on
3753  * behalf of the requested node (first argument). This function needs to be
3754  * called with scheduler lock held.
3755  */
3756 static int
3757 ice_sched_validate_srl_node(struct ice_sched_node *node, u8 sel_layer)
3758 {
3759         /* SRL profiles are not available on all layers. Check if the
3760          * SRL profile can be applied to a node above or below the
3761          * requested node. SRL configuration is possible only if the
3762          * selected layer's node has single child.
3763          */
3764         if (sel_layer == node->tx_sched_layer ||
3765             ((sel_layer == node->tx_sched_layer + 1) &&
3766             node->num_children == 1) ||
3767             ((sel_layer == node->tx_sched_layer - 1) &&
3768             (node->parent && node->parent->num_children == 1)))
3769                 return 0;
3770
3771         return -EIO;
3772 }
3773
3774 /**
3775  * ice_sched_save_q_bw - save queue node's BW information
3776  * @q_ctx: queue context structure
3777  * @rl_type: rate limit type min, max, or shared
3778  * @bw: bandwidth in Kbps - Kilo bits per sec
3779  *
3780  * Save BW information of queue type node for post replay use.
3781  */
3782 static int
3783 ice_sched_save_q_bw(struct ice_q_ctx *q_ctx, enum ice_rl_type rl_type, u32 bw)
3784 {
3785         switch (rl_type) {
3786         case ICE_MIN_BW:
3787                 ice_set_clear_cir_bw(&q_ctx->bw_t_info, bw);
3788                 break;
3789         case ICE_MAX_BW:
3790                 ice_set_clear_eir_bw(&q_ctx->bw_t_info, bw);
3791                 break;
3792         case ICE_SHARED_BW:
3793                 ice_set_clear_shared_bw(&q_ctx->bw_t_info, bw);
3794                 break;
3795         default:
3796                 return -EINVAL;
3797         }
3798         return 0;
3799 }
3800
3801 /**
3802  * ice_sched_set_q_bw_lmt - sets queue BW limit
3803  * @pi: port information structure
3804  * @vsi_handle: sw VSI handle
3805  * @tc: traffic class
3806  * @q_handle: software queue handle
3807  * @rl_type: min, max, or shared
3808  * @bw: bandwidth in Kbps
3809  *
3810  * This function sets BW limit of queue scheduling node.
3811  */
3812 static int
3813 ice_sched_set_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3814                        u16 q_handle, enum ice_rl_type rl_type, u32 bw)
3815 {
3816         struct ice_sched_node *node;
3817         struct ice_q_ctx *q_ctx;
3818         int status = -EINVAL;
3819
3820         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3821                 return -EINVAL;
3822         mutex_lock(&pi->sched_lock);
3823         q_ctx = ice_get_lan_q_ctx(pi->hw, vsi_handle, tc, q_handle);
3824         if (!q_ctx)
3825                 goto exit_q_bw_lmt;
3826         node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
3827         if (!node) {
3828                 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong q_teid\n");
3829                 goto exit_q_bw_lmt;
3830         }
3831
3832         /* Return error if it is not a leaf node */
3833         if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF)
3834                 goto exit_q_bw_lmt;
3835
3836         /* SRL bandwidth layer selection */
3837         if (rl_type == ICE_SHARED_BW) {
3838                 u8 sel_layer; /* selected layer */
3839
3840                 sel_layer = ice_sched_get_rl_prof_layer(pi, rl_type,
3841                                                         node->tx_sched_layer);
3842                 if (sel_layer >= pi->hw->num_tx_sched_layers) {
3843                         status = -EINVAL;
3844                         goto exit_q_bw_lmt;
3845                 }
3846                 status = ice_sched_validate_srl_node(node, sel_layer);
3847                 if (status)
3848                         goto exit_q_bw_lmt;
3849         }
3850
3851         if (bw == ICE_SCHED_DFLT_BW)
3852                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
3853         else
3854                 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
3855
3856         if (!status)
3857                 status = ice_sched_save_q_bw(q_ctx, rl_type, bw);
3858
3859 exit_q_bw_lmt:
3860         mutex_unlock(&pi->sched_lock);
3861         return status;
3862 }
3863
3864 /**
3865  * ice_cfg_q_bw_lmt - configure queue BW limit
3866  * @pi: port information structure
3867  * @vsi_handle: sw VSI handle
3868  * @tc: traffic class
3869  * @q_handle: software queue handle
3870  * @rl_type: min, max, or shared
3871  * @bw: bandwidth in Kbps
3872  *
3873  * This function configures BW limit of queue scheduling node.
3874  */
3875 int
3876 ice_cfg_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3877                  u16 q_handle, enum ice_rl_type rl_type, u32 bw)
3878 {
3879         return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
3880                                       bw);
3881 }
3882
3883 /**
3884  * ice_cfg_q_bw_dflt_lmt - configure queue BW default limit
3885  * @pi: port information structure
3886  * @vsi_handle: sw VSI handle
3887  * @tc: traffic class
3888  * @q_handle: software queue handle
3889  * @rl_type: min, max, or shared
3890  *
3891  * This function configures BW default limit of queue scheduling node.
3892  */
3893 int
3894 ice_cfg_q_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3895                       u16 q_handle, enum ice_rl_type rl_type)
3896 {
3897         return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
3898                                       ICE_SCHED_DFLT_BW);
3899 }
3900
3901 /**
3902  * ice_sched_get_node_by_id_type - get node from ID type
3903  * @pi: port information structure
3904  * @id: identifier
3905  * @agg_type: type of aggregator
3906  * @tc: traffic class
3907  *
3908  * This function returns node identified by ID of type aggregator, and
3909  * based on traffic class (TC). This function needs to be called with
3910  * the scheduler lock held.
3911  */
3912 static struct ice_sched_node *
3913 ice_sched_get_node_by_id_type(struct ice_port_info *pi, u32 id,
3914                               enum ice_agg_type agg_type, u8 tc)
3915 {
3916         struct ice_sched_node *node = NULL;
3917
3918         switch (agg_type) {
3919         case ICE_AGG_TYPE_VSI: {
3920                 struct ice_vsi_ctx *vsi_ctx;
3921                 u16 vsi_handle = (u16)id;
3922
3923                 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3924                         break;
3925                 /* Get sched_vsi_info */
3926                 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3927                 if (!vsi_ctx)
3928                         break;
3929                 node = vsi_ctx->sched.vsi_node[tc];
3930                 break;
3931         }
3932
3933         case ICE_AGG_TYPE_AGG: {
3934                 struct ice_sched_node *tc_node;
3935
3936                 tc_node = ice_sched_get_tc_node(pi, tc);
3937                 if (tc_node)
3938                         node = ice_sched_get_agg_node(pi, tc_node, id);
3939                 break;
3940         }
3941
3942         default:
3943                 break;
3944         }
3945
3946         return node;
3947 }
3948
3949 /**
3950  * ice_sched_set_node_bw_lmt_per_tc - set node BW limit per TC
3951  * @pi: port information structure
3952  * @id: ID (software VSI handle or AGG ID)
3953  * @agg_type: aggregator type (VSI or AGG type node)
3954  * @tc: traffic class
3955  * @rl_type: min or max
3956  * @bw: bandwidth in Kbps
3957  *
3958  * This function sets BW limit of VSI or Aggregator scheduling node
3959  * based on TC information from passed in argument BW.
3960  */
3961 int
3962 ice_sched_set_node_bw_lmt_per_tc(struct ice_port_info *pi, u32 id,
3963                                  enum ice_agg_type agg_type, u8 tc,
3964                                  enum ice_rl_type rl_type, u32 bw)
3965 {
3966         struct ice_sched_node *node;
3967         int status = -EINVAL;
3968
3969         if (!pi)
3970                 return status;
3971
3972         if (rl_type == ICE_UNKNOWN_BW)
3973                 return status;
3974
3975         mutex_lock(&pi->sched_lock);
3976         node = ice_sched_get_node_by_id_type(pi, id, agg_type, tc);
3977         if (!node) {
3978                 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong id, agg type, or tc\n");
3979                 goto exit_set_node_bw_lmt_per_tc;
3980         }
3981         if (bw == ICE_SCHED_DFLT_BW)
3982                 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
3983         else
3984                 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
3985
3986 exit_set_node_bw_lmt_per_tc:
3987         mutex_unlock(&pi->sched_lock);
3988         return status;
3989 }
3990
3991 /**
3992  * ice_cfg_vsi_bw_lmt_per_tc - configure VSI BW limit per TC
3993  * @pi: port information structure
3994  * @vsi_handle: software VSI handle
3995  * @tc: traffic class
3996  * @rl_type: min or max
3997  * @bw: bandwidth in Kbps
3998  *
3999  * This function configures BW limit of VSI scheduling node based on TC
4000  * information.
4001  */
4002 int
4003 ice_cfg_vsi_bw_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4004                           enum ice_rl_type rl_type, u32 bw)
4005 {
4006         int status;
4007
4008         status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
4009                                                   ICE_AGG_TYPE_VSI,
4010                                                   tc, rl_type, bw);
4011         if (!status) {
4012                 mutex_lock(&pi->sched_lock);
4013                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type, bw);
4014                 mutex_unlock(&pi->sched_lock);
4015         }
4016         return status;
4017 }
4018
4019 /**
4020  * ice_cfg_vsi_bw_dflt_lmt_per_tc - configure default VSI BW limit per TC
4021  * @pi: port information structure
4022  * @vsi_handle: software VSI handle
4023  * @tc: traffic class
4024  * @rl_type: min or max
4025  *
4026  * This function configures default BW limit of VSI scheduling node based on TC
4027  * information.
4028  */
4029 int
4030 ice_cfg_vsi_bw_dflt_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4031                                enum ice_rl_type rl_type)
4032 {
4033         int status;
4034
4035         status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
4036                                                   ICE_AGG_TYPE_VSI,
4037                                                   tc, rl_type,
4038                                                   ICE_SCHED_DFLT_BW);
4039         if (!status) {
4040                 mutex_lock(&pi->sched_lock);
4041                 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type,
4042                                                ICE_SCHED_DFLT_BW);
4043                 mutex_unlock(&pi->sched_lock);
4044         }
4045         return status;
4046 }
4047
4048 /**
4049  * ice_cfg_rl_burst_size - Set burst size value
4050  * @hw: pointer to the HW struct
4051  * @bytes: burst size in bytes
4052  *
4053  * This function configures/set the burst size to requested new value. The new
4054  * burst size value is used for future rate limit calls. It doesn't change the
4055  * existing or previously created RL profiles.
4056  */
4057 int ice_cfg_rl_burst_size(struct ice_hw *hw, u32 bytes)
4058 {
4059         u16 burst_size_to_prog;
4060
4061         if (bytes < ICE_MIN_BURST_SIZE_ALLOWED ||
4062             bytes > ICE_MAX_BURST_SIZE_ALLOWED)
4063                 return -EINVAL;
4064         if (ice_round_to_num(bytes, 64) <=
4065             ICE_MAX_BURST_SIZE_64_BYTE_GRANULARITY) {
4066                 /* 64 byte granularity case */
4067                 /* Disable MSB granularity bit */
4068                 burst_size_to_prog = ICE_64_BYTE_GRANULARITY;
4069                 /* round number to nearest 64 byte granularity */
4070                 bytes = ice_round_to_num(bytes, 64);
4071                 /* The value is in 64 byte chunks */
4072                 burst_size_to_prog |= (u16)(bytes / 64);
4073         } else {
4074                 /* k bytes granularity case */
4075                 /* Enable MSB granularity bit */
4076                 burst_size_to_prog = ICE_KBYTE_GRANULARITY;
4077                 /* round number to nearest 1024 granularity */
4078                 bytes = ice_round_to_num(bytes, 1024);
4079                 /* check rounding doesn't go beyond allowed */
4080                 if (bytes > ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY)
4081                         bytes = ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY;
4082                 /* The value is in k bytes */
4083                 burst_size_to_prog |= (u16)(bytes / 1024);
4084         }
4085         hw->max_burst_size = burst_size_to_prog;
4086         return 0;
4087 }
4088
4089 /**
4090  * ice_sched_replay_node_prio - re-configure node priority
4091  * @hw: pointer to the HW struct
4092  * @node: sched node to configure
4093  * @priority: priority value
4094  *
4095  * This function configures node element's priority value. It
4096  * needs to be called with scheduler lock held.
4097  */
4098 static int
4099 ice_sched_replay_node_prio(struct ice_hw *hw, struct ice_sched_node *node,
4100                            u8 priority)
4101 {
4102         struct ice_aqc_txsched_elem_data buf;
4103         struct ice_aqc_txsched_elem *data;
4104         int status;
4105
4106         buf = node->info;
4107         data = &buf.data;
4108         data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
4109         data->generic = priority;
4110
4111         /* Configure element */
4112         status = ice_sched_update_elem(hw, node, &buf);
4113         return status;
4114 }
4115
4116 /**
4117  * ice_sched_replay_node_bw - replay node(s) BW
4118  * @hw: pointer to the HW struct
4119  * @node: sched node to configure
4120  * @bw_t_info: BW type information
4121  *
4122  * This function restores node's BW from bw_t_info. The caller needs
4123  * to hold the scheduler lock.
4124  */
4125 static int
4126 ice_sched_replay_node_bw(struct ice_hw *hw, struct ice_sched_node *node,
4127                          struct ice_bw_type_info *bw_t_info)
4128 {
4129         struct ice_port_info *pi = hw->port_info;
4130         int status = -EINVAL;
4131         u16 bw_alloc;
4132
4133         if (!node)
4134                 return status;
4135         if (bitmap_empty(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CNT))
4136                 return 0;
4137         if (test_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap)) {
4138                 status = ice_sched_replay_node_prio(hw, node,
4139                                                     bw_t_info->generic);
4140                 if (status)
4141                         return status;
4142         }
4143         if (test_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap)) {
4144                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW,
4145                                                    bw_t_info->cir_bw.bw);
4146                 if (status)
4147                         return status;
4148         }
4149         if (test_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap)) {
4150                 bw_alloc = bw_t_info->cir_bw.bw_alloc;
4151                 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MIN_BW,
4152                                                      bw_alloc);
4153                 if (status)
4154                         return status;
4155         }
4156         if (test_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap)) {
4157                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW,
4158                                                    bw_t_info->eir_bw.bw);
4159                 if (status)
4160                         return status;
4161         }
4162         if (test_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap)) {
4163                 bw_alloc = bw_t_info->eir_bw.bw_alloc;
4164                 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MAX_BW,
4165                                                      bw_alloc);
4166                 if (status)
4167                         return status;
4168         }
4169         if (test_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap))
4170                 status = ice_sched_set_node_bw_lmt(pi, node, ICE_SHARED_BW,
4171                                                    bw_t_info->shared_bw);
4172         return status;
4173 }
4174
4175 /**
4176  * ice_sched_get_ena_tc_bitmap - get enabled TC bitmap
4177  * @pi: port info struct
4178  * @tc_bitmap: 8 bits TC bitmap to check
4179  * @ena_tc_bitmap: 8 bits enabled TC bitmap to return
4180  *
4181  * This function returns enabled TC bitmap in variable ena_tc_bitmap. Some TCs
4182  * may be missing, it returns enabled TCs. This function needs to be called with
4183  * scheduler lock held.
4184  */
4185 static void
4186 ice_sched_get_ena_tc_bitmap(struct ice_port_info *pi,
4187                             unsigned long *tc_bitmap,
4188                             unsigned long *ena_tc_bitmap)
4189 {
4190         u8 tc;
4191
4192         /* Some TC(s) may be missing after reset, adjust for replay */
4193         ice_for_each_traffic_class(tc)
4194                 if (ice_is_tc_ena(*tc_bitmap, tc) &&
4195                     (ice_sched_get_tc_node(pi, tc)))
4196                         set_bit(tc, ena_tc_bitmap);
4197 }
4198
4199 /**
4200  * ice_sched_replay_agg - recreate aggregator node(s)
4201  * @hw: pointer to the HW struct
4202  *
4203  * This function recreate aggregator type nodes which are not replayed earlier.
4204  * It also replay aggregator BW information. These aggregator nodes are not
4205  * associated with VSI type node yet.
4206  */
4207 void ice_sched_replay_agg(struct ice_hw *hw)
4208 {
4209         struct ice_port_info *pi = hw->port_info;
4210         struct ice_sched_agg_info *agg_info;
4211
4212         mutex_lock(&pi->sched_lock);
4213         list_for_each_entry(agg_info, &hw->agg_list, list_entry)
4214                 /* replay aggregator (re-create aggregator node) */
4215                 if (!bitmap_equal(agg_info->tc_bitmap, agg_info->replay_tc_bitmap,
4216                                   ICE_MAX_TRAFFIC_CLASS)) {
4217                         DECLARE_BITMAP(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4218                         int status;
4219
4220                         bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4221                         ice_sched_get_ena_tc_bitmap(pi,
4222                                                     agg_info->replay_tc_bitmap,
4223                                                     replay_bitmap);
4224                         status = ice_sched_cfg_agg(hw->port_info,
4225                                                    agg_info->agg_id,
4226                                                    ICE_AGG_TYPE_AGG,
4227                                                    replay_bitmap);
4228                         if (status) {
4229                                 dev_info(ice_hw_to_dev(hw),
4230                                          "Replay agg id[%d] failed\n",
4231                                          agg_info->agg_id);
4232                                 /* Move on to next one */
4233                                 continue;
4234                         }
4235                 }
4236         mutex_unlock(&pi->sched_lock);
4237 }
4238
4239 /**
4240  * ice_sched_replay_agg_vsi_preinit - Agg/VSI replay pre initialization
4241  * @hw: pointer to the HW struct
4242  *
4243  * This function initialize aggregator(s) TC bitmap to zero. A required
4244  * preinit step for replaying aggregators.
4245  */
4246 void ice_sched_replay_agg_vsi_preinit(struct ice_hw *hw)
4247 {
4248         struct ice_port_info *pi = hw->port_info;
4249         struct ice_sched_agg_info *agg_info;
4250
4251         mutex_lock(&pi->sched_lock);
4252         list_for_each_entry(agg_info, &hw->agg_list, list_entry) {
4253                 struct ice_sched_agg_vsi_info *agg_vsi_info;
4254
4255                 agg_info->tc_bitmap[0] = 0;
4256                 list_for_each_entry(agg_vsi_info, &agg_info->agg_vsi_list,
4257                                     list_entry)
4258                         agg_vsi_info->tc_bitmap[0] = 0;
4259         }
4260         mutex_unlock(&pi->sched_lock);
4261 }
4262
4263 /**
4264  * ice_sched_replay_vsi_agg - replay aggregator & VSI to aggregator node(s)
4265  * @hw: pointer to the HW struct
4266  * @vsi_handle: software VSI handle
4267  *
4268  * This function replays aggregator node, VSI to aggregator type nodes, and
4269  * their node bandwidth information. This function needs to be called with
4270  * scheduler lock held.
4271  */
4272 static int ice_sched_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
4273 {
4274         DECLARE_BITMAP(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4275         struct ice_sched_agg_vsi_info *agg_vsi_info;
4276         struct ice_port_info *pi = hw->port_info;
4277         struct ice_sched_agg_info *agg_info;
4278         int status;
4279
4280         bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4281         if (!ice_is_vsi_valid(hw, vsi_handle))
4282                 return -EINVAL;
4283         agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
4284         if (!agg_info)
4285                 return 0; /* Not present in list - default Agg case */
4286         agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
4287         if (!agg_vsi_info)
4288                 return 0; /* Not present in list - default Agg case */
4289         ice_sched_get_ena_tc_bitmap(pi, agg_info->replay_tc_bitmap,
4290                                     replay_bitmap);
4291         /* Replay aggregator node associated to vsi_handle */
4292         status = ice_sched_cfg_agg(hw->port_info, agg_info->agg_id,
4293                                    ICE_AGG_TYPE_AGG, replay_bitmap);
4294         if (status)
4295                 return status;
4296
4297         bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4298         ice_sched_get_ena_tc_bitmap(pi, agg_vsi_info->replay_tc_bitmap,
4299                                     replay_bitmap);
4300         /* Move this VSI (vsi_handle) to above aggregator */
4301         return ice_sched_assoc_vsi_to_agg(pi, agg_info->agg_id, vsi_handle,
4302                                           replay_bitmap);
4303 }
4304
4305 /**
4306  * ice_replay_vsi_agg - replay VSI to aggregator node
4307  * @hw: pointer to the HW struct
4308  * @vsi_handle: software VSI handle
4309  *
4310  * This function replays association of VSI to aggregator type nodes, and
4311  * node bandwidth information.
4312  */
4313 int ice_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
4314 {
4315         struct ice_port_info *pi = hw->port_info;
4316         int status;
4317
4318         mutex_lock(&pi->sched_lock);
4319         status = ice_sched_replay_vsi_agg(hw, vsi_handle);
4320         mutex_unlock(&pi->sched_lock);
4321         return status;
4322 }
4323
4324 /**
4325  * ice_sched_replay_q_bw - replay queue type node BW
4326  * @pi: port information structure
4327  * @q_ctx: queue context structure
4328  *
4329  * This function replays queue type node bandwidth. This function needs to be
4330  * called with scheduler lock held.
4331  */
4332 int ice_sched_replay_q_bw(struct ice_port_info *pi, struct ice_q_ctx *q_ctx)
4333 {
4334         struct ice_sched_node *q_node;
4335
4336         /* Following also checks the presence of node in tree */
4337         q_node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
4338         if (!q_node)
4339                 return -EINVAL;
4340         return ice_sched_replay_node_bw(pi->hw, q_node, &q_ctx->bw_t_info);
4341 }
This page took 0.278891 seconds and 4 git commands to generate.