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