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