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