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