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