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