xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_sched.c (revision 22fc4c4c9fd60427bcda00878cee94e7622cfa7a)
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 static enum ice_status
131 ice_aq_query_sched_elems(struct ice_hw *hw, u16 elems_req,
132 			 struct ice_aqc_get_elem *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_query_elem - query element information from hw
142  * @hw: pointer to the hw struct
143  * @node_teid: node teid to be queried
144  * @buf: buffer to element information
145  *
146  * This function queries HW element information
147  */
148 static enum ice_status
149 ice_sched_query_elem(struct ice_hw *hw, u32 node_teid,
150 		     struct ice_aqc_get_elem *buf)
151 {
152 	u16 buf_size, num_elem_ret = 0;
153 	enum ice_status status;
154 
155 	buf_size = sizeof(*buf);
156 	memset(buf, 0, buf_size);
157 	buf->generic[0].node_teid = cpu_to_le32(node_teid);
158 	status = ice_aq_query_sched_elems(hw, 1, buf, buf_size, &num_elem_ret,
159 					  NULL);
160 	if (status || num_elem_ret != 1)
161 		ice_debug(hw, ICE_DBG_SCHED, "query element failed\n");
162 	return status;
163 }
164 
165 /**
166  * ice_sched_add_node - Insert the Tx scheduler node in SW DB
167  * @pi: port information structure
168  * @layer: Scheduler layer of the node
169  * @info: Scheduler element information from firmware
170  *
171  * This function inserts a scheduler node to the SW DB.
172  */
173 enum ice_status
174 ice_sched_add_node(struct ice_port_info *pi, u8 layer,
175 		   struct ice_aqc_txsched_elem_data *info)
176 {
177 	struct ice_sched_node *parent;
178 	struct ice_aqc_get_elem elem;
179 	struct ice_sched_node *node;
180 	enum ice_status status;
181 	struct ice_hw *hw;
182 
183 	if (!pi)
184 		return ICE_ERR_PARAM;
185 
186 	hw = pi->hw;
187 
188 	/* A valid parent node should be there */
189 	parent = ice_sched_find_node_by_teid(pi->root,
190 					     le32_to_cpu(info->parent_teid));
191 	if (!parent) {
192 		ice_debug(hw, ICE_DBG_SCHED,
193 			  "Parent Node not found for parent_teid=0x%x\n",
194 			  le32_to_cpu(info->parent_teid));
195 		return ICE_ERR_PARAM;
196 	}
197 
198 	/* query the current node information from FW  before additing it
199 	 * to the SW DB
200 	 */
201 	status = ice_sched_query_elem(hw, le32_to_cpu(info->node_teid), &elem);
202 	if (status)
203 		return status;
204 
205 	node = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*node), GFP_KERNEL);
206 	if (!node)
207 		return ICE_ERR_NO_MEMORY;
208 	if (hw->max_children[layer]) {
209 		/* coverity[suspicious_sizeof] */
210 		node->children = devm_kcalloc(ice_hw_to_dev(hw),
211 					      hw->max_children[layer],
212 					      sizeof(*node), GFP_KERNEL);
213 		if (!node->children) {
214 			devm_kfree(ice_hw_to_dev(hw), node);
215 			return ICE_ERR_NO_MEMORY;
216 		}
217 	}
218 
219 	node->in_use = true;
220 	node->parent = parent;
221 	node->tx_sched_layer = layer;
222 	parent->children[parent->num_children++] = node;
223 	memcpy(&node->info, &elem.generic[0], sizeof(node->info));
224 	return 0;
225 }
226 
227 /**
228  * ice_aq_delete_sched_elems - delete scheduler elements
229  * @hw: pointer to the hw struct
230  * @grps_req: number of groups to delete
231  * @buf: pointer to buffer
232  * @buf_size: buffer size in bytes
233  * @grps_del: returns total number of elements deleted
234  * @cd: pointer to command details structure or NULL
235  *
236  * Delete scheduling elements (0x040F)
237  */
238 static enum ice_status
239 ice_aq_delete_sched_elems(struct ice_hw *hw, u16 grps_req,
240 			  struct ice_aqc_delete_elem *buf, u16 buf_size,
241 			  u16 *grps_del, struct ice_sq_cd *cd)
242 {
243 	return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_delete_sched_elems,
244 					   grps_req, (void *)buf, buf_size,
245 					   grps_del, cd);
246 }
247 
248 /**
249  * ice_sched_remove_elems - remove nodes from hw
250  * @hw: pointer to the hw struct
251  * @parent: pointer to the parent node
252  * @num_nodes: number of nodes
253  * @node_teids: array of node teids to be deleted
254  *
255  * This function remove nodes from hw
256  */
257 static enum ice_status
258 ice_sched_remove_elems(struct ice_hw *hw, struct ice_sched_node *parent,
259 		       u16 num_nodes, u32 *node_teids)
260 {
261 	struct ice_aqc_delete_elem *buf;
262 	u16 i, num_groups_removed = 0;
263 	enum ice_status status;
264 	u16 buf_size;
265 
266 	buf_size = sizeof(*buf) + sizeof(u32) * (num_nodes - 1);
267 	buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
268 	if (!buf)
269 		return ICE_ERR_NO_MEMORY;
270 
271 	buf->hdr.parent_teid = parent->info.node_teid;
272 	buf->hdr.num_elems = cpu_to_le16(num_nodes);
273 	for (i = 0; i < num_nodes; i++)
274 		buf->teid[i] = cpu_to_le32(node_teids[i]);
275 
276 	status = ice_aq_delete_sched_elems(hw, 1, buf, buf_size,
277 					   &num_groups_removed, NULL);
278 	if (status || num_groups_removed != 1)
279 		ice_debug(hw, ICE_DBG_SCHED, "remove elements failed\n");
280 
281 	devm_kfree(ice_hw_to_dev(hw), buf);
282 	return status;
283 }
284 
285 /**
286  * ice_sched_get_first_node - get the first node of the given layer
287  * @hw: pointer to the hw struct
288  * @parent: pointer the base node of the subtree
289  * @layer: layer number
290  *
291  * This function retrieves the first node of the given layer from the subtree
292  */
293 static struct ice_sched_node *
294 ice_sched_get_first_node(struct ice_hw *hw, struct ice_sched_node *parent,
295 			 u8 layer)
296 {
297 	u8 i;
298 
299 	if (layer < hw->sw_entry_point_layer)
300 		return NULL;
301 	for (i = 0; i < parent->num_children; i++) {
302 		struct ice_sched_node *node = parent->children[i];
303 
304 		if (node) {
305 			if (node->tx_sched_layer == layer)
306 				return node;
307 			/* this recursion is intentional, and wouldn't
308 			 * go more than 9 calls
309 			 */
310 			return ice_sched_get_first_node(hw, node, layer);
311 		}
312 	}
313 	return NULL;
314 }
315 
316 /**
317  * ice_sched_get_tc_node - get pointer to TC node
318  * @pi: port information structure
319  * @tc: TC number
320  *
321  * This function returns the TC node pointer
322  */
323 struct ice_sched_node *ice_sched_get_tc_node(struct ice_port_info *pi, u8 tc)
324 {
325 	u8 i;
326 
327 	if (!pi)
328 		return NULL;
329 	for (i = 0; i < pi->root->num_children; i++)
330 		if (pi->root->children[i]->tc_num == tc)
331 			return pi->root->children[i];
332 	return NULL;
333 }
334 
335 /**
336  * ice_free_sched_node - Free a Tx scheduler node from SW DB
337  * @pi: port information structure
338  * @node: pointer to the ice_sched_node struct
339  *
340  * This function frees up a node from SW DB as well as from HW
341  *
342  * This function needs to be called with the port_info->sched_lock held
343  */
344 void ice_free_sched_node(struct ice_port_info *pi, struct ice_sched_node *node)
345 {
346 	struct ice_sched_node *parent;
347 	struct ice_hw *hw = pi->hw;
348 	u8 i, j;
349 
350 	/* Free the children before freeing up the parent node
351 	 * The parent array is updated below and that shifts the nodes
352 	 * in the array. So always pick the first child if num children > 0
353 	 */
354 	while (node->num_children)
355 		ice_free_sched_node(pi, node->children[0]);
356 
357 	/* Leaf, TC and root nodes can't be deleted by SW */
358 	if (node->tx_sched_layer >= hw->sw_entry_point_layer &&
359 	    node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
360 	    node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT &&
361 	    node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF) {
362 		u32 teid = le32_to_cpu(node->info.node_teid);
363 		enum ice_status status;
364 
365 		status = ice_sched_remove_elems(hw, node->parent, 1, &teid);
366 		if (status)
367 			ice_debug(hw, ICE_DBG_SCHED,
368 				  "remove element failed %d\n", status);
369 	}
370 	parent = node->parent;
371 	/* root has no parent */
372 	if (parent) {
373 		struct ice_sched_node *p, *tc_node;
374 
375 		/* update the parent */
376 		for (i = 0; i < parent->num_children; i++)
377 			if (parent->children[i] == node) {
378 				for (j = i + 1; j < parent->num_children; j++)
379 					parent->children[j - 1] =
380 						parent->children[j];
381 				parent->num_children--;
382 				break;
383 			}
384 
385 		/* search for previous sibling that points to this node and
386 		 * remove the reference
387 		 */
388 		tc_node = ice_sched_get_tc_node(pi, node->tc_num);
389 		if (!tc_node) {
390 			ice_debug(hw, ICE_DBG_SCHED,
391 				  "Invalid TC number %d\n", node->tc_num);
392 			goto err_exit;
393 		}
394 		p = ice_sched_get_first_node(hw, tc_node, node->tx_sched_layer);
395 		while (p) {
396 			if (p->sibling == node) {
397 				p->sibling = node->sibling;
398 				break;
399 			}
400 			p = p->sibling;
401 		}
402 	}
403 err_exit:
404 	/* leaf nodes have no children */
405 	if (node->children)
406 		devm_kfree(ice_hw_to_dev(hw), node->children);
407 	devm_kfree(ice_hw_to_dev(hw), node);
408 }
409 
410 /**
411  * ice_aq_get_dflt_topo - gets default scheduler topology
412  * @hw: pointer to the hw struct
413  * @lport: logical port number
414  * @buf: pointer to buffer
415  * @buf_size: buffer size in bytes
416  * @num_branches: returns total number of queue to port branches
417  * @cd: pointer to command details structure or NULL
418  *
419  * Get default scheduler topology (0x400)
420  */
421 static enum ice_status
422 ice_aq_get_dflt_topo(struct ice_hw *hw, u8 lport,
423 		     struct ice_aqc_get_topo_elem *buf, u16 buf_size,
424 		     u8 *num_branches, struct ice_sq_cd *cd)
425 {
426 	struct ice_aqc_get_topo *cmd;
427 	struct ice_aq_desc desc;
428 	enum ice_status status;
429 
430 	cmd = &desc.params.get_topo;
431 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_dflt_topo);
432 	cmd->port_num = lport;
433 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
434 	if (!status && num_branches)
435 		*num_branches = cmd->num_branches;
436 
437 	return status;
438 }
439 
440 /**
441  * ice_aq_add_sched_elems - adds scheduling element
442  * @hw: pointer to the hw struct
443  * @grps_req: the number of groups that are requested to be added
444  * @buf: pointer to buffer
445  * @buf_size: buffer size in bytes
446  * @grps_added: returns total number of groups added
447  * @cd: pointer to command details structure or NULL
448  *
449  * Add scheduling elements (0x0401)
450  */
451 static enum ice_status
452 ice_aq_add_sched_elems(struct ice_hw *hw, u16 grps_req,
453 		       struct ice_aqc_add_elem *buf, u16 buf_size,
454 		       u16 *grps_added, struct ice_sq_cd *cd)
455 {
456 	return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_add_sched_elems,
457 					   grps_req, (void *)buf, buf_size,
458 					   grps_added, cd);
459 }
460 
461 /**
462  * ice_aq_suspend_sched_elems - suspend scheduler elements
463  * @hw: pointer to the hw struct
464  * @elems_req: number of elements to suspend
465  * @buf: pointer to buffer
466  * @buf_size: buffer size in bytes
467  * @elems_ret: returns total number of elements suspended
468  * @cd: pointer to command details structure or NULL
469  *
470  * Suspend scheduling elements (0x0409)
471  */
472 static enum ice_status
473 ice_aq_suspend_sched_elems(struct ice_hw *hw, u16 elems_req,
474 			   struct ice_aqc_suspend_resume_elem *buf,
475 			   u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
476 {
477 	return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_suspend_sched_elems,
478 					   elems_req, (void *)buf, buf_size,
479 					   elems_ret, cd);
480 }
481 
482 /**
483  * ice_aq_resume_sched_elems - resume scheduler elements
484  * @hw: pointer to the hw struct
485  * @elems_req: number of elements to resume
486  * @buf: pointer to buffer
487  * @buf_size: buffer size in bytes
488  * @elems_ret: returns total number of elements resumed
489  * @cd: pointer to command details structure or NULL
490  *
491  * resume scheduling elements (0x040A)
492  */
493 static enum ice_status
494 ice_aq_resume_sched_elems(struct ice_hw *hw, u16 elems_req,
495 			  struct ice_aqc_suspend_resume_elem *buf,
496 			  u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
497 {
498 	return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_resume_sched_elems,
499 					   elems_req, (void *)buf, buf_size,
500 					   elems_ret, cd);
501 }
502 
503 /**
504  * ice_aq_query_sched_res - query scheduler resource
505  * @hw: pointer to the hw struct
506  * @buf_size: buffer size in bytes
507  * @buf: pointer to buffer
508  * @cd: pointer to command details structure or NULL
509  *
510  * Query scheduler resource allocation (0x0412)
511  */
512 static enum ice_status
513 ice_aq_query_sched_res(struct ice_hw *hw, u16 buf_size,
514 		       struct ice_aqc_query_txsched_res_resp *buf,
515 		       struct ice_sq_cd *cd)
516 {
517 	struct ice_aq_desc desc;
518 
519 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_sched_res);
520 	return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
521 }
522 
523 /**
524  * ice_sched_suspend_resume_elems - suspend or resume hw nodes
525  * @hw: pointer to the hw struct
526  * @num_nodes: number of nodes
527  * @node_teids: array of node teids to be suspended or resumed
528  * @suspend: true means suspend / false means resume
529  *
530  * This function suspends or resumes hw nodes
531  */
532 static enum ice_status
533 ice_sched_suspend_resume_elems(struct ice_hw *hw, u8 num_nodes, u32 *node_teids,
534 			       bool suspend)
535 {
536 	struct ice_aqc_suspend_resume_elem *buf;
537 	u16 i, buf_size, num_elem_ret = 0;
538 	enum ice_status status;
539 
540 	buf_size = sizeof(*buf) * num_nodes;
541 	buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
542 	if (!buf)
543 		return ICE_ERR_NO_MEMORY;
544 
545 	for (i = 0; i < num_nodes; i++)
546 		buf->teid[i] = cpu_to_le32(node_teids[i]);
547 
548 	if (suspend)
549 		status = ice_aq_suspend_sched_elems(hw, num_nodes, buf,
550 						    buf_size, &num_elem_ret,
551 						    NULL);
552 	else
553 		status = ice_aq_resume_sched_elems(hw, num_nodes, buf,
554 						   buf_size, &num_elem_ret,
555 						   NULL);
556 	if (status || num_elem_ret != num_nodes)
557 		ice_debug(hw, ICE_DBG_SCHED, "suspend/resume failed\n");
558 
559 	devm_kfree(ice_hw_to_dev(hw), buf);
560 	return status;
561 }
562 
563 /**
564  * ice_sched_clear_agg - clears the agg related information
565  * @hw: pointer to the hardware structure
566  *
567  * This function removes agg list and free up agg related memory
568  * previously allocated.
569  */
570 void ice_sched_clear_agg(struct ice_hw *hw)
571 {
572 	struct ice_sched_agg_info *agg_info;
573 	struct ice_sched_agg_info *atmp;
574 
575 	list_for_each_entry_safe(agg_info, atmp, &hw->agg_list, list_entry) {
576 		struct ice_sched_agg_vsi_info *agg_vsi_info;
577 		struct ice_sched_agg_vsi_info *vtmp;
578 
579 		list_for_each_entry_safe(agg_vsi_info, vtmp,
580 					 &agg_info->agg_vsi_list, list_entry) {
581 			list_del(&agg_vsi_info->list_entry);
582 			devm_kfree(ice_hw_to_dev(hw), agg_vsi_info);
583 		}
584 		list_del(&agg_info->list_entry);
585 		devm_kfree(ice_hw_to_dev(hw), agg_info);
586 	}
587 }
588 
589 /**
590  * ice_sched_clear_tx_topo - clears the scheduler tree nodes
591  * @pi: port information structure
592  *
593  * This function removes all the nodes from HW as well as from SW DB.
594  */
595 static void ice_sched_clear_tx_topo(struct ice_port_info *pi)
596 {
597 	if (!pi)
598 		return;
599 	if (pi->root) {
600 		ice_free_sched_node(pi, pi->root);
601 		pi->root = NULL;
602 	}
603 }
604 
605 /**
606  * ice_sched_clear_port - clear the scheduler elements from SW DB for a port
607  * @pi: port information structure
608  *
609  * Cleanup scheduling elements from SW DB
610  */
611 void ice_sched_clear_port(struct ice_port_info *pi)
612 {
613 	if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
614 		return;
615 
616 	pi->port_state = ICE_SCHED_PORT_STATE_INIT;
617 	mutex_lock(&pi->sched_lock);
618 	ice_sched_clear_tx_topo(pi);
619 	mutex_unlock(&pi->sched_lock);
620 	mutex_destroy(&pi->sched_lock);
621 }
622 
623 /**
624  * ice_sched_cleanup_all - cleanup scheduler elements from SW DB for all ports
625  * @hw: pointer to the hw struct
626  *
627  * Cleanup scheduling elements from SW DB for all the ports
628  */
629 void ice_sched_cleanup_all(struct ice_hw *hw)
630 {
631 	if (!hw)
632 		return;
633 
634 	if (hw->layer_info) {
635 		devm_kfree(ice_hw_to_dev(hw), hw->layer_info);
636 		hw->layer_info = NULL;
637 	}
638 
639 	if (hw->port_info)
640 		ice_sched_clear_port(hw->port_info);
641 
642 	hw->num_tx_sched_layers = 0;
643 	hw->num_tx_sched_phys_layers = 0;
644 	hw->flattened_layers = 0;
645 	hw->max_cgds = 0;
646 }
647 
648 /**
649  * ice_sched_add_elems - add nodes to hw and SW DB
650  * @pi: port information structure
651  * @tc_node: pointer to the branch node
652  * @parent: pointer to the parent node
653  * @layer: layer number to add nodes
654  * @num_nodes: number of nodes
655  * @num_nodes_added: pointer to num nodes added
656  * @first_node_teid: if new nodes are added then return the teid of first node
657  *
658  * This function add nodes to hw as well as to SW DB for a given layer
659  */
660 static enum ice_status
661 ice_sched_add_elems(struct ice_port_info *pi, struct ice_sched_node *tc_node,
662 		    struct ice_sched_node *parent, u8 layer, u16 num_nodes,
663 		    u16 *num_nodes_added, u32 *first_node_teid)
664 {
665 	struct ice_sched_node *prev, *new_node;
666 	struct ice_aqc_add_elem *buf;
667 	u16 i, num_groups_added = 0;
668 	enum ice_status status = 0;
669 	struct ice_hw *hw = pi->hw;
670 	u16 buf_size;
671 	u32 teid;
672 
673 	buf_size = sizeof(*buf) + sizeof(*buf->generic) * (num_nodes - 1);
674 	buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
675 	if (!buf)
676 		return ICE_ERR_NO_MEMORY;
677 
678 	buf->hdr.parent_teid = parent->info.node_teid;
679 	buf->hdr.num_elems = cpu_to_le16(num_nodes);
680 	for (i = 0; i < num_nodes; i++) {
681 		buf->generic[i].parent_teid = parent->info.node_teid;
682 		buf->generic[i].data.elem_type = ICE_AQC_ELEM_TYPE_SE_GENERIC;
683 		buf->generic[i].data.valid_sections =
684 			ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
685 			ICE_AQC_ELEM_VALID_EIR;
686 		buf->generic[i].data.generic = 0;
687 		buf->generic[i].data.cir_bw.bw_profile_idx =
688 			cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
689 		buf->generic[i].data.cir_bw.bw_alloc =
690 			cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
691 		buf->generic[i].data.eir_bw.bw_profile_idx =
692 			cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
693 		buf->generic[i].data.eir_bw.bw_alloc =
694 			cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
695 	}
696 
697 	status = ice_aq_add_sched_elems(hw, 1, buf, buf_size,
698 					&num_groups_added, NULL);
699 	if (status || num_groups_added != 1) {
700 		ice_debug(hw, ICE_DBG_SCHED, "add elements failed\n");
701 		devm_kfree(ice_hw_to_dev(hw), buf);
702 		return ICE_ERR_CFG;
703 	}
704 
705 	*num_nodes_added = num_nodes;
706 	/* add nodes to the SW DB */
707 	for (i = 0; i < num_nodes; i++) {
708 		status = ice_sched_add_node(pi, layer, &buf->generic[i]);
709 		if (status) {
710 			ice_debug(hw, ICE_DBG_SCHED,
711 				  "add nodes in SW DB failed status =%d\n",
712 				  status);
713 			break;
714 		}
715 
716 		teid = le32_to_cpu(buf->generic[i].node_teid);
717 		new_node = ice_sched_find_node_by_teid(parent, teid);
718 		if (!new_node) {
719 			ice_debug(hw, ICE_DBG_SCHED,
720 				  "Node is missing for teid =%d\n", teid);
721 			break;
722 		}
723 
724 		new_node->sibling = NULL;
725 		new_node->tc_num = tc_node->tc_num;
726 
727 		/* add it to previous node sibling pointer */
728 		/* Note: siblings are not linked across branches */
729 		prev = ice_sched_get_first_node(hw, tc_node, layer);
730 		if (prev && prev != new_node) {
731 			while (prev->sibling)
732 				prev = prev->sibling;
733 			prev->sibling = new_node;
734 		}
735 
736 		if (i == 0)
737 			*first_node_teid = teid;
738 	}
739 
740 	devm_kfree(ice_hw_to_dev(hw), buf);
741 	return status;
742 }
743 
744 /**
745  * ice_sched_add_nodes_to_layer - Add nodes to a given layer
746  * @pi: port information structure
747  * @tc_node: pointer to TC node
748  * @parent: pointer to parent node
749  * @layer: layer number to add nodes
750  * @num_nodes: number of nodes to be added
751  * @first_node_teid: pointer to the first node teid
752  * @num_nodes_added: pointer to number of nodes added
753  *
754  * This function add nodes to a given layer.
755  */
756 static enum ice_status
757 ice_sched_add_nodes_to_layer(struct ice_port_info *pi,
758 			     struct ice_sched_node *tc_node,
759 			     struct ice_sched_node *parent, u8 layer,
760 			     u16 num_nodes, u32 *first_node_teid,
761 			     u16 *num_nodes_added)
762 {
763 	u32 *first_teid_ptr = first_node_teid;
764 	u16 new_num_nodes, max_child_nodes;
765 	enum ice_status status = 0;
766 	struct ice_hw *hw = pi->hw;
767 	u16 num_added = 0;
768 	u32 temp;
769 
770 	*num_nodes_added = 0;
771 
772 	if (!num_nodes)
773 		return status;
774 
775 	if (!parent || layer < hw->sw_entry_point_layer)
776 		return ICE_ERR_PARAM;
777 
778 	/* max children per node per layer */
779 	max_child_nodes = hw->max_children[parent->tx_sched_layer];
780 
781 	/* current number of children + required nodes exceed max children ? */
782 	if ((parent->num_children + num_nodes) > max_child_nodes) {
783 		/* Fail if the parent is a TC node */
784 		if (parent == tc_node)
785 			return ICE_ERR_CFG;
786 
787 		/* utilize all the spaces if the parent is not full */
788 		if (parent->num_children < max_child_nodes) {
789 			new_num_nodes = max_child_nodes - parent->num_children;
790 			/* this recursion is intentional, and wouldn't
791 			 * go more than 2 calls
792 			 */
793 			status = ice_sched_add_nodes_to_layer(pi, tc_node,
794 							      parent, layer,
795 							      new_num_nodes,
796 							      first_node_teid,
797 							      &num_added);
798 			if (status)
799 				return status;
800 
801 			*num_nodes_added += num_added;
802 		}
803 		/* Don't modify the first node teid memory if the first node was
804 		 * added already in the above call. Instead send some temp
805 		 * memory for all other recursive calls.
806 		 */
807 		if (num_added)
808 			first_teid_ptr = &temp;
809 
810 		new_num_nodes = num_nodes - num_added;
811 
812 		/* This parent is full, try the next sibling */
813 		parent = parent->sibling;
814 
815 		/* this recursion is intentional, for 1024 queues
816 		 * per VSI, it goes max of 16 iterations.
817 		 * 1024 / 8 = 128 layer 8 nodes
818 		 * 128 /8 = 16 (add 8 nodes per iteration)
819 		 */
820 		status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
821 						      layer, new_num_nodes,
822 						      first_teid_ptr,
823 						      &num_added);
824 		*num_nodes_added += num_added;
825 		return status;
826 	}
827 
828 	status = ice_sched_add_elems(pi, tc_node, parent, layer, num_nodes,
829 				     num_nodes_added, first_node_teid);
830 	return status;
831 }
832 
833 /**
834  * ice_sched_get_qgrp_layer - get the current queue group layer number
835  * @hw: pointer to the hw struct
836  *
837  * This function returns the current queue group layer number
838  */
839 static u8 ice_sched_get_qgrp_layer(struct ice_hw *hw)
840 {
841 	/* It's always total layers - 1, the array is 0 relative so -2 */
842 	return hw->num_tx_sched_layers - ICE_QGRP_LAYER_OFFSET;
843 }
844 
845 /**
846  * ice_sched_get_vsi_layer - get the current VSI layer number
847  * @hw: pointer to the hw struct
848  *
849  * This function returns the current VSI layer number
850  */
851 static u8 ice_sched_get_vsi_layer(struct ice_hw *hw)
852 {
853 	/* Num Layers       VSI layer
854 	 *     9               6
855 	 *     7               4
856 	 *     5 or less       sw_entry_point_layer
857 	 */
858 	/* calculate the vsi layer based on number of layers. */
859 	if (hw->num_tx_sched_layers > ICE_VSI_LAYER_OFFSET + 1) {
860 		u8 layer = hw->num_tx_sched_layers - ICE_VSI_LAYER_OFFSET;
861 
862 		if (layer > hw->sw_entry_point_layer)
863 			return layer;
864 	}
865 	return hw->sw_entry_point_layer;
866 }
867 
868 /**
869  * ice_rm_dflt_leaf_node - remove the default leaf node in the tree
870  * @pi: port information structure
871  *
872  * This function removes the leaf node that was created by the FW
873  * during initialization
874  */
875 static void ice_rm_dflt_leaf_node(struct ice_port_info *pi)
876 {
877 	struct ice_sched_node *node;
878 
879 	node = pi->root;
880 	while (node) {
881 		if (!node->num_children)
882 			break;
883 		node = node->children[0];
884 	}
885 	if (node && node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF) {
886 		u32 teid = le32_to_cpu(node->info.node_teid);
887 		enum ice_status status;
888 
889 		/* remove the default leaf node */
890 		status = ice_sched_remove_elems(pi->hw, node->parent, 1, &teid);
891 		if (!status)
892 			ice_free_sched_node(pi, node);
893 	}
894 }
895 
896 /**
897  * ice_sched_rm_dflt_nodes - free the default nodes in the tree
898  * @pi: port information structure
899  *
900  * This function frees all the nodes except root and TC that were created by
901  * the FW during initialization
902  */
903 static void ice_sched_rm_dflt_nodes(struct ice_port_info *pi)
904 {
905 	struct ice_sched_node *node;
906 
907 	ice_rm_dflt_leaf_node(pi);
908 
909 	/* remove the default nodes except TC and root nodes */
910 	node = pi->root;
911 	while (node) {
912 		if (node->tx_sched_layer >= pi->hw->sw_entry_point_layer &&
913 		    node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
914 		    node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT) {
915 			ice_free_sched_node(pi, node);
916 			break;
917 		}
918 
919 		if (!node->num_children)
920 			break;
921 		node = node->children[0];
922 	}
923 }
924 
925 /**
926  * ice_sched_init_port - Initialize scheduler by querying information from FW
927  * @pi: port info structure for the tree to cleanup
928  *
929  * This function is the initial call to find the total number of Tx scheduler
930  * resources, default topology created by firmware and storing the information
931  * in SW DB.
932  */
933 enum ice_status ice_sched_init_port(struct ice_port_info *pi)
934 {
935 	struct ice_aqc_get_topo_elem *buf;
936 	enum ice_status status;
937 	struct ice_hw *hw;
938 	u8 num_branches;
939 	u16 num_elems;
940 	u8 i, j;
941 
942 	if (!pi)
943 		return ICE_ERR_PARAM;
944 	hw = pi->hw;
945 
946 	/* Query the Default Topology from FW */
947 	buf = devm_kzalloc(ice_hw_to_dev(hw), ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
948 	if (!buf)
949 		return ICE_ERR_NO_MEMORY;
950 
951 	/* Query default scheduling tree topology */
952 	status = ice_aq_get_dflt_topo(hw, pi->lport, buf, ICE_AQ_MAX_BUF_LEN,
953 				      &num_branches, NULL);
954 	if (status)
955 		goto err_init_port;
956 
957 	/* num_branches should be between 1-8 */
958 	if (num_branches < 1 || num_branches > ICE_TXSCHED_MAX_BRANCHES) {
959 		ice_debug(hw, ICE_DBG_SCHED, "num_branches unexpected %d\n",
960 			  num_branches);
961 		status = ICE_ERR_PARAM;
962 		goto err_init_port;
963 	}
964 
965 	/* get the number of elements on the default/first branch */
966 	num_elems = le16_to_cpu(buf[0].hdr.num_elems);
967 
968 	/* num_elems should always be between 1-9 */
969 	if (num_elems < 1 || num_elems > ICE_AQC_TOPO_MAX_LEVEL_NUM) {
970 		ice_debug(hw, ICE_DBG_SCHED, "num_elems unexpected %d\n",
971 			  num_elems);
972 		status = ICE_ERR_PARAM;
973 		goto err_init_port;
974 	}
975 
976 	/* If the last node is a leaf node then the index of the Q group
977 	 * layer is two less than the number of elements.
978 	 */
979 	if (num_elems > 2 && buf[0].generic[num_elems - 1].data.elem_type ==
980 	    ICE_AQC_ELEM_TYPE_LEAF)
981 		pi->last_node_teid =
982 			le32_to_cpu(buf[0].generic[num_elems - 2].node_teid);
983 	else
984 		pi->last_node_teid =
985 			le32_to_cpu(buf[0].generic[num_elems - 1].node_teid);
986 
987 	/* Insert the Tx Sched root node */
988 	status = ice_sched_add_root_node(pi, &buf[0].generic[0]);
989 	if (status)
990 		goto err_init_port;
991 
992 	/* Parse the default tree and cache the information */
993 	for (i = 0; i < num_branches; i++) {
994 		num_elems = le16_to_cpu(buf[i].hdr.num_elems);
995 
996 		/* Skip root element as already inserted */
997 		for (j = 1; j < num_elems; j++) {
998 			/* update the sw entry point */
999 			if (buf[0].generic[j].data.elem_type ==
1000 			    ICE_AQC_ELEM_TYPE_ENTRY_POINT)
1001 				hw->sw_entry_point_layer = j;
1002 
1003 			status = ice_sched_add_node(pi, j, &buf[i].generic[j]);
1004 			if (status)
1005 				goto err_init_port;
1006 		}
1007 	}
1008 
1009 	/* Remove the default nodes. */
1010 	if (pi->root)
1011 		ice_sched_rm_dflt_nodes(pi);
1012 
1013 	/* initialize the port for handling the scheduler tree */
1014 	pi->port_state = ICE_SCHED_PORT_STATE_READY;
1015 	mutex_init(&pi->sched_lock);
1016 
1017 err_init_port:
1018 	if (status && pi->root) {
1019 		ice_free_sched_node(pi, pi->root);
1020 		pi->root = NULL;
1021 	}
1022 
1023 	devm_kfree(ice_hw_to_dev(hw), buf);
1024 	return status;
1025 }
1026 
1027 /**
1028  * ice_sched_query_res_alloc - query the FW for num of logical sched layers
1029  * @hw: pointer to the HW struct
1030  *
1031  * query FW for allocated scheduler resources and store in HW struct
1032  */
1033 enum ice_status ice_sched_query_res_alloc(struct ice_hw *hw)
1034 {
1035 	struct ice_aqc_query_txsched_res_resp *buf;
1036 	enum ice_status status = 0;
1037 	__le16 max_sibl;
1038 	u8 i;
1039 
1040 	if (hw->layer_info)
1041 		return status;
1042 
1043 	buf = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*buf), GFP_KERNEL);
1044 	if (!buf)
1045 		return ICE_ERR_NO_MEMORY;
1046 
1047 	status = ice_aq_query_sched_res(hw, sizeof(*buf), buf, NULL);
1048 	if (status)
1049 		goto sched_query_out;
1050 
1051 	hw->num_tx_sched_layers = le16_to_cpu(buf->sched_props.logical_levels);
1052 	hw->num_tx_sched_phys_layers =
1053 		le16_to_cpu(buf->sched_props.phys_levels);
1054 	hw->flattened_layers = buf->sched_props.flattening_bitmap;
1055 	hw->max_cgds = buf->sched_props.max_pf_cgds;
1056 
1057 	/* max sibling group size of current layer refers to the max children
1058 	 * of the below layer node.
1059 	 * layer 1 node max children will be layer 2 max sibling group size
1060 	 * layer 2 node max children will be layer 3 max sibling group size
1061 	 * and so on. This array will be populated from root (index 0) to
1062 	 * qgroup layer 7. Leaf node has no children.
1063 	 */
1064 	for (i = 0; i < hw->num_tx_sched_layers; i++) {
1065 		max_sibl = buf->layer_props[i].max_sibl_grp_sz;
1066 		hw->max_children[i] = le16_to_cpu(max_sibl);
1067 	}
1068 
1069 	hw->layer_info = (struct ice_aqc_layer_props *)
1070 			  devm_kmemdup(ice_hw_to_dev(hw), buf->layer_props,
1071 				       (hw->num_tx_sched_layers *
1072 					sizeof(*hw->layer_info)),
1073 				       GFP_KERNEL);
1074 	if (!hw->layer_info) {
1075 		status = ICE_ERR_NO_MEMORY;
1076 		goto sched_query_out;
1077 	}
1078 
1079 sched_query_out:
1080 	devm_kfree(ice_hw_to_dev(hw), buf);
1081 	return status;
1082 }
1083 
1084 /**
1085  * ice_sched_find_node_in_subtree - Find node in part of base node subtree
1086  * @hw: pointer to the hw struct
1087  * @base: pointer to the base node
1088  * @node: pointer to the node to search
1089  *
1090  * This function checks whether a given node is part of the base node
1091  * subtree or not
1092  */
1093 static bool
1094 ice_sched_find_node_in_subtree(struct ice_hw *hw, struct ice_sched_node *base,
1095 			       struct ice_sched_node *node)
1096 {
1097 	u8 i;
1098 
1099 	for (i = 0; i < base->num_children; i++) {
1100 		struct ice_sched_node *child = base->children[i];
1101 
1102 		if (node == child)
1103 			return true;
1104 
1105 		if (child->tx_sched_layer > node->tx_sched_layer)
1106 			return false;
1107 
1108 		/* this recursion is intentional, and wouldn't
1109 		 * go more than 8 calls
1110 		 */
1111 		if (ice_sched_find_node_in_subtree(hw, child, node))
1112 			return true;
1113 	}
1114 	return false;
1115 }
1116 
1117 /**
1118  * ice_sched_get_free_qparent - Get a free lan or rdma q group node
1119  * @pi: port information structure
1120  * @vsi_handle: software VSI handle
1121  * @tc: branch number
1122  * @owner: lan or rdma
1123  *
1124  * This function retrieves a free lan or rdma q group node
1125  */
1126 struct ice_sched_node *
1127 ice_sched_get_free_qparent(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
1128 			   u8 owner)
1129 {
1130 	struct ice_sched_node *vsi_node, *qgrp_node = NULL;
1131 	struct ice_vsi_ctx *vsi_ctx;
1132 	u16 max_children;
1133 	u8 qgrp_layer;
1134 
1135 	qgrp_layer = ice_sched_get_qgrp_layer(pi->hw);
1136 	max_children = pi->hw->max_children[qgrp_layer];
1137 
1138 	vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1139 	if (!vsi_ctx)
1140 		return NULL;
1141 	vsi_node = vsi_ctx->sched.vsi_node[tc];
1142 	/* validate invalid VSI id */
1143 	if (!vsi_node)
1144 		goto lan_q_exit;
1145 
1146 	/* get the first q group node from VSI sub-tree */
1147 	qgrp_node = ice_sched_get_first_node(pi->hw, vsi_node, qgrp_layer);
1148 	while (qgrp_node) {
1149 		/* make sure the qgroup node is part of the VSI subtree */
1150 		if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1151 			if (qgrp_node->num_children < max_children &&
1152 			    qgrp_node->owner == owner)
1153 				break;
1154 		qgrp_node = qgrp_node->sibling;
1155 	}
1156 
1157 lan_q_exit:
1158 	return qgrp_node;
1159 }
1160 
1161 /**
1162  * ice_sched_get_vsi_node - Get a VSI node based on VSI id
1163  * @hw: pointer to the hw struct
1164  * @tc_node: pointer to the TC node
1165  * @vsi_handle: software VSI handle
1166  *
1167  * This function retrieves a VSI node for a given VSI id from a given
1168  * TC branch
1169  */
1170 static struct ice_sched_node *
1171 ice_sched_get_vsi_node(struct ice_hw *hw, struct ice_sched_node *tc_node,
1172 		       u16 vsi_handle)
1173 {
1174 	struct ice_sched_node *node;
1175 	u8 vsi_layer;
1176 
1177 	vsi_layer = ice_sched_get_vsi_layer(hw);
1178 	node = ice_sched_get_first_node(hw, tc_node, vsi_layer);
1179 
1180 	/* Check whether it already exists */
1181 	while (node) {
1182 		if (node->vsi_handle == vsi_handle)
1183 			return node;
1184 		node = node->sibling;
1185 	}
1186 
1187 	return node;
1188 }
1189 
1190 /**
1191  * ice_sched_calc_vsi_child_nodes - calculate number of VSI child nodes
1192  * @hw: pointer to the hw struct
1193  * @num_qs: number of queues
1194  * @num_nodes: num nodes array
1195  *
1196  * This function calculates the number of VSI child nodes based on the
1197  * number of queues.
1198  */
1199 static void
1200 ice_sched_calc_vsi_child_nodes(struct ice_hw *hw, u16 num_qs, u16 *num_nodes)
1201 {
1202 	u16 num = num_qs;
1203 	u8 i, qgl, vsil;
1204 
1205 	qgl = ice_sched_get_qgrp_layer(hw);
1206 	vsil = ice_sched_get_vsi_layer(hw);
1207 
1208 	/* calculate num nodes from q group to VSI layer */
1209 	for (i = qgl; i > vsil; i--) {
1210 		/* round to the next integer if there is a remainder */
1211 		num = DIV_ROUND_UP(num, hw->max_children[i]);
1212 
1213 		/* need at least one node */
1214 		num_nodes[i] = num ? num : 1;
1215 	}
1216 }
1217 
1218 /**
1219  * ice_sched_add_vsi_child_nodes - add VSI child nodes to tree
1220  * @pi: port information structure
1221  * @vsi_handle: software VSI handle
1222  * @tc_node: pointer to the TC node
1223  * @num_nodes: pointer to the num nodes that needs to be added per layer
1224  * @owner: node owner (lan or rdma)
1225  *
1226  * This function adds the VSI child nodes to tree. It gets called for
1227  * lan and rdma separately.
1228  */
1229 static enum ice_status
1230 ice_sched_add_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1231 			      struct ice_sched_node *tc_node, u16 *num_nodes,
1232 			      u8 owner)
1233 {
1234 	struct ice_sched_node *parent, *node;
1235 	struct ice_hw *hw = pi->hw;
1236 	enum ice_status status;
1237 	u32 first_node_teid;
1238 	u16 num_added = 0;
1239 	u8 i, qgl, vsil;
1240 
1241 	qgl = ice_sched_get_qgrp_layer(hw);
1242 	vsil = ice_sched_get_vsi_layer(hw);
1243 	parent = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1244 	for (i = vsil + 1; i <= qgl; i++) {
1245 		if (!parent)
1246 			return ICE_ERR_CFG;
1247 
1248 		status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
1249 						      num_nodes[i],
1250 						      &first_node_teid,
1251 						      &num_added);
1252 		if (status || num_nodes[i] != num_added)
1253 			return ICE_ERR_CFG;
1254 
1255 		/* The newly added node can be a new parent for the next
1256 		 * layer nodes
1257 		 */
1258 		if (num_added) {
1259 			parent = ice_sched_find_node_by_teid(tc_node,
1260 							     first_node_teid);
1261 			node = parent;
1262 			while (node) {
1263 				node->owner = owner;
1264 				node = node->sibling;
1265 			}
1266 		} else {
1267 			parent = parent->children[0];
1268 		}
1269 	}
1270 
1271 	return 0;
1272 }
1273 
1274 /**
1275  * ice_sched_rm_vsi_child_nodes - remove VSI child nodes from the tree
1276  * @pi: port information structure
1277  * @vsi_node: pointer to the VSI node
1278  * @num_nodes: pointer to the num nodes that needs to be removed per layer
1279  * @owner: node owner (lan or rdma)
1280  *
1281  * This function removes the VSI child nodes from the tree. It gets called for
1282  * lan and rdma separately.
1283  */
1284 static void
1285 ice_sched_rm_vsi_child_nodes(struct ice_port_info *pi,
1286 			     struct ice_sched_node *vsi_node, u16 *num_nodes,
1287 			     u8 owner)
1288 {
1289 	struct ice_sched_node *node, *next;
1290 	u8 i, qgl, vsil;
1291 	u16 num;
1292 
1293 	qgl = ice_sched_get_qgrp_layer(pi->hw);
1294 	vsil = ice_sched_get_vsi_layer(pi->hw);
1295 
1296 	for (i = qgl; i > vsil; i--) {
1297 		num = num_nodes[i];
1298 		node = ice_sched_get_first_node(pi->hw, vsi_node, i);
1299 		while (node && num) {
1300 			next = node->sibling;
1301 			if (node->owner == owner && !node->num_children) {
1302 				ice_free_sched_node(pi, node);
1303 				num--;
1304 			}
1305 			node = next;
1306 		}
1307 	}
1308 }
1309 
1310 /**
1311  * ice_sched_calc_vsi_support_nodes - calculate number of VSI support nodes
1312  * @hw: pointer to the hw struct
1313  * @tc_node: pointer to TC node
1314  * @num_nodes: pointer to num nodes array
1315  *
1316  * This function calculates the number of supported nodes needed to add this
1317  * VSI into Tx tree including the VSI, parent and intermediate nodes in below
1318  * layers
1319  */
1320 static void
1321 ice_sched_calc_vsi_support_nodes(struct ice_hw *hw,
1322 				 struct ice_sched_node *tc_node, u16 *num_nodes)
1323 {
1324 	struct ice_sched_node *node;
1325 	u8 vsil;
1326 	int i;
1327 
1328 	vsil = ice_sched_get_vsi_layer(hw);
1329 	for (i = vsil; i >= hw->sw_entry_point_layer; i--)
1330 		/* Add intermediate nodes if TC has no children and
1331 		 * need at least one node for VSI
1332 		 */
1333 		if (!tc_node->num_children || i == vsil) {
1334 			num_nodes[i]++;
1335 		} else {
1336 			/* If intermediate nodes are reached max children
1337 			 * then add a new one.
1338 			 */
1339 			node = ice_sched_get_first_node(hw, tc_node, (u8)i);
1340 			/* scan all the siblings */
1341 			while (node) {
1342 				if (node->num_children < hw->max_children[i])
1343 					break;
1344 				node = node->sibling;
1345 			}
1346 
1347 			/* all the nodes are full, allocate a new one */
1348 			if (!node)
1349 				num_nodes[i]++;
1350 		}
1351 }
1352 
1353 /**
1354  * ice_sched_add_vsi_support_nodes - add VSI supported nodes into Tx tree
1355  * @pi: port information structure
1356  * @vsi_handle: software VSI handle
1357  * @tc_node: pointer to TC node
1358  * @num_nodes: pointer to num nodes array
1359  *
1360  * This function adds the VSI supported nodes into Tx tree including the
1361  * VSI, its parent and intermediate nodes in below layers
1362  */
1363 static enum ice_status
1364 ice_sched_add_vsi_support_nodes(struct ice_port_info *pi, u16 vsi_handle,
1365 				struct ice_sched_node *tc_node, u16 *num_nodes)
1366 {
1367 	struct ice_sched_node *parent = tc_node;
1368 	enum ice_status status;
1369 	u32 first_node_teid;
1370 	u16 num_added = 0;
1371 	u8 i, vsil;
1372 
1373 	if (!pi)
1374 		return ICE_ERR_PARAM;
1375 
1376 	vsil = ice_sched_get_vsi_layer(pi->hw);
1377 	for (i = pi->hw->sw_entry_point_layer; i <= vsil; i++) {
1378 		status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
1379 						      i, num_nodes[i],
1380 						      &first_node_teid,
1381 						      &num_added);
1382 		if (status || num_nodes[i] != num_added)
1383 			return ICE_ERR_CFG;
1384 
1385 		/* The newly added node can be a new parent for the next
1386 		 * layer nodes
1387 		 */
1388 		if (num_added)
1389 			parent = ice_sched_find_node_by_teid(tc_node,
1390 							     first_node_teid);
1391 		else
1392 			parent = parent->children[0];
1393 
1394 		if (!parent)
1395 			return ICE_ERR_CFG;
1396 
1397 		if (i == vsil)
1398 			parent->vsi_handle = vsi_handle;
1399 	}
1400 
1401 	return 0;
1402 }
1403 
1404 /**
1405  * ice_sched_add_vsi_to_topo - add a new VSI into tree
1406  * @pi: port information structure
1407  * @vsi_handle: software VSI handle
1408  * @tc: TC number
1409  *
1410  * This function adds a new VSI into scheduler tree
1411  */
1412 static enum ice_status
1413 ice_sched_add_vsi_to_topo(struct ice_port_info *pi, u16 vsi_handle, u8 tc)
1414 {
1415 	u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1416 	struct ice_sched_node *tc_node;
1417 	struct ice_hw *hw = pi->hw;
1418 
1419 	tc_node = ice_sched_get_tc_node(pi, tc);
1420 	if (!tc_node)
1421 		return ICE_ERR_PARAM;
1422 
1423 	/* calculate number of supported nodes needed for this VSI */
1424 	ice_sched_calc_vsi_support_nodes(hw, tc_node, num_nodes);
1425 
1426 	/* add vsi supported nodes to tc subtree */
1427 	return ice_sched_add_vsi_support_nodes(pi, vsi_handle, tc_node,
1428 					       num_nodes);
1429 }
1430 
1431 /**
1432  * ice_sched_update_vsi_child_nodes - update VSI child nodes
1433  * @pi: port information structure
1434  * @vsi_handle: software VSI handle
1435  * @tc: TC number
1436  * @new_numqs: new number of max queues
1437  * @owner: owner of this subtree
1438  *
1439  * This function updates the VSI child nodes based on the number of queues
1440  */
1441 static enum ice_status
1442 ice_sched_update_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1443 				 u8 tc, u16 new_numqs, u8 owner)
1444 {
1445 	u16 prev_num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1446 	u16 new_num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1447 	struct ice_sched_node *vsi_node;
1448 	struct ice_sched_node *tc_node;
1449 	struct ice_vsi_ctx *vsi_ctx;
1450 	enum ice_status status = 0;
1451 	struct ice_hw *hw = pi->hw;
1452 	u16 prev_numqs;
1453 	u8 i;
1454 
1455 	tc_node = ice_sched_get_tc_node(pi, tc);
1456 	if (!tc_node)
1457 		return ICE_ERR_CFG;
1458 
1459 	vsi_node = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1460 	if (!vsi_node)
1461 		return ICE_ERR_CFG;
1462 
1463 	vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1464 	if (!vsi_ctx)
1465 		return ICE_ERR_PARAM;
1466 
1467 	if (owner == ICE_SCHED_NODE_OWNER_LAN)
1468 		prev_numqs = vsi_ctx->sched.max_lanq[tc];
1469 	else
1470 		return ICE_ERR_PARAM;
1471 
1472 	/* num queues are not changed */
1473 	if (prev_numqs == new_numqs)
1474 		return status;
1475 
1476 	/* calculate number of nodes based on prev/new number of qs */
1477 	if (prev_numqs)
1478 		ice_sched_calc_vsi_child_nodes(hw, prev_numqs, prev_num_nodes);
1479 
1480 	if (new_numqs)
1481 		ice_sched_calc_vsi_child_nodes(hw, new_numqs, new_num_nodes);
1482 
1483 	if (prev_numqs > new_numqs) {
1484 		for (i = 0; i < ICE_AQC_TOPO_MAX_LEVEL_NUM; i++)
1485 			new_num_nodes[i] = prev_num_nodes[i] - new_num_nodes[i];
1486 
1487 		ice_sched_rm_vsi_child_nodes(pi, vsi_node, new_num_nodes,
1488 					     owner);
1489 	} else {
1490 		for (i = 0; i < ICE_AQC_TOPO_MAX_LEVEL_NUM; i++)
1491 			new_num_nodes[i] -= prev_num_nodes[i];
1492 
1493 		status = ice_sched_add_vsi_child_nodes(pi, vsi_handle, tc_node,
1494 						       new_num_nodes, owner);
1495 		if (status)
1496 			return status;
1497 	}
1498 
1499 	vsi_ctx->sched.max_lanq[tc] = new_numqs;
1500 
1501 	return status;
1502 }
1503 
1504 /**
1505  * ice_sched_cfg_vsi - configure the new/existing VSI
1506  * @pi: port information structure
1507  * @vsi_handle: software VSI handle
1508  * @tc: TC number
1509  * @maxqs: max number of queues
1510  * @owner: lan or rdma
1511  * @enable: TC enabled or disabled
1512  *
1513  * This function adds/updates VSI nodes based on the number of queues. If TC is
1514  * enabled and VSI is in suspended state then resume the VSI back. If TC is
1515  * disabled then suspend the VSI if it is not already.
1516  */
1517 enum ice_status
1518 ice_sched_cfg_vsi(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 maxqs,
1519 		  u8 owner, bool enable)
1520 {
1521 	struct ice_sched_node *vsi_node, *tc_node;
1522 	struct ice_vsi_ctx *vsi_ctx;
1523 	enum ice_status status = 0;
1524 	struct ice_hw *hw = pi->hw;
1525 
1526 	tc_node = ice_sched_get_tc_node(pi, tc);
1527 	if (!tc_node)
1528 		return ICE_ERR_PARAM;
1529 	vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1530 	if (!vsi_ctx)
1531 		return ICE_ERR_PARAM;
1532 	vsi_node = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1533 
1534 	/* suspend the VSI if tc is not enabled */
1535 	if (!enable) {
1536 		if (vsi_node && vsi_node->in_use) {
1537 			u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1538 
1539 			status = ice_sched_suspend_resume_elems(hw, 1, &teid,
1540 								true);
1541 			if (!status)
1542 				vsi_node->in_use = false;
1543 		}
1544 		return status;
1545 	}
1546 
1547 	/* TC is enabled, if it is a new VSI then add it to the tree */
1548 	if (!vsi_node) {
1549 		status = ice_sched_add_vsi_to_topo(pi, vsi_handle, tc);
1550 		if (status)
1551 			return status;
1552 
1553 		vsi_node = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1554 		if (!vsi_node)
1555 			return ICE_ERR_CFG;
1556 
1557 		vsi_ctx->sched.vsi_node[tc] = vsi_node;
1558 		vsi_node->in_use = true;
1559 		/* invalidate the max queues whenever VSI gets added first time
1560 		 * into the scheduler tree (boot or after reset). We need to
1561 		 * recreate the child nodes all the time in these cases.
1562 		 */
1563 		vsi_ctx->sched.max_lanq[tc] = 0;
1564 	}
1565 
1566 	/* update the VSI child nodes */
1567 	status = ice_sched_update_vsi_child_nodes(pi, vsi_handle, tc, maxqs,
1568 						  owner);
1569 	if (status)
1570 		return status;
1571 
1572 	/* TC is enabled, resume the VSI if it is in the suspend state */
1573 	if (!vsi_node->in_use) {
1574 		u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1575 
1576 		status = ice_sched_suspend_resume_elems(hw, 1, &teid, false);
1577 		if (!status)
1578 			vsi_node->in_use = true;
1579 	}
1580 
1581 	return status;
1582 }
1583 
1584 /**
1585  * ice_sched_rm_agg_vsi_entry - remove agg related VSI info entry
1586  * @pi: port information structure
1587  * @vsi_handle: software VSI handle
1588  *
1589  * This function removes single aggregator VSI info entry from
1590  * aggregator list.
1591  */
1592 static void
1593 ice_sched_rm_agg_vsi_info(struct ice_port_info *pi, u16 vsi_handle)
1594 {
1595 	struct ice_sched_agg_info *agg_info;
1596 	struct ice_sched_agg_info *atmp;
1597 
1598 	list_for_each_entry_safe(agg_info, atmp, &pi->hw->agg_list,
1599 				 list_entry) {
1600 		struct ice_sched_agg_vsi_info *agg_vsi_info;
1601 		struct ice_sched_agg_vsi_info *vtmp;
1602 
1603 		list_for_each_entry_safe(agg_vsi_info, vtmp,
1604 					 &agg_info->agg_vsi_list, list_entry)
1605 			if (agg_vsi_info->vsi_handle == vsi_handle) {
1606 				list_del(&agg_vsi_info->list_entry);
1607 				devm_kfree(ice_hw_to_dev(pi->hw),
1608 					   agg_vsi_info);
1609 				return;
1610 			}
1611 	}
1612 }
1613 
1614 /**
1615  * ice_sched_rm_vsi_cfg - remove the VSI and its children nodes
1616  * @pi: port information structure
1617  * @vsi_handle: software VSI handle
1618  * @owner: LAN or RDMA
1619  *
1620  * This function removes the VSI and its LAN or RDMA children nodes from the
1621  * scheduler tree.
1622  */
1623 static enum ice_status
1624 ice_sched_rm_vsi_cfg(struct ice_port_info *pi, u16 vsi_handle, u8 owner)
1625 {
1626 	enum ice_status status = ICE_ERR_PARAM;
1627 	struct ice_vsi_ctx *vsi_ctx;
1628 	u8 i, j = 0;
1629 
1630 	if (!ice_is_vsi_valid(pi->hw, vsi_handle))
1631 		return status;
1632 	mutex_lock(&pi->sched_lock);
1633 	vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1634 	if (!vsi_ctx)
1635 		goto exit_sched_rm_vsi_cfg;
1636 
1637 	for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
1638 		struct ice_sched_node *vsi_node, *tc_node;
1639 
1640 		tc_node = ice_sched_get_tc_node(pi, i);
1641 		if (!tc_node)
1642 			continue;
1643 
1644 		vsi_node = ice_sched_get_vsi_node(pi->hw, tc_node, vsi_handle);
1645 		if (!vsi_node)
1646 			continue;
1647 
1648 		while (j < vsi_node->num_children) {
1649 			if (vsi_node->children[j]->owner == owner) {
1650 				ice_free_sched_node(pi, vsi_node->children[j]);
1651 
1652 				/* reset the counter again since the num
1653 				 * children will be updated after node removal
1654 				 */
1655 				j = 0;
1656 			} else {
1657 				j++;
1658 			}
1659 		}
1660 		/* remove the VSI if it has no children */
1661 		if (!vsi_node->num_children) {
1662 			ice_free_sched_node(pi, vsi_node);
1663 			vsi_ctx->sched.vsi_node[i] = NULL;
1664 
1665 			/* clean up agg related vsi info if any */
1666 			ice_sched_rm_agg_vsi_info(pi, vsi_handle);
1667 		}
1668 		if (owner == ICE_SCHED_NODE_OWNER_LAN)
1669 			vsi_ctx->sched.max_lanq[i] = 0;
1670 	}
1671 	status = 0;
1672 
1673 exit_sched_rm_vsi_cfg:
1674 	mutex_unlock(&pi->sched_lock);
1675 	return status;
1676 }
1677 
1678 /**
1679  * ice_rm_vsi_lan_cfg - remove VSI and its LAN children nodes
1680  * @pi: port information structure
1681  * @vsi_handle: software VSI handle
1682  *
1683  * This function clears the VSI and its LAN children nodes from scheduler tree
1684  * for all TCs.
1685  */
1686 enum ice_status ice_rm_vsi_lan_cfg(struct ice_port_info *pi, u16 vsi_handle)
1687 {
1688 	return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_LAN);
1689 }
1690