1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
4 #include <net/devlink.h>
8 * ice_sched_add_root_node - Insert the Tx scheduler root node in SW DB
9 * @pi: port information structure
10 * @info: Scheduler element information from firmware
12 * This function inserts the root node of the scheduling tree topology
16 ice_sched_add_root_node(struct ice_port_info *pi,
17 struct ice_aqc_txsched_elem_data *info)
19 struct ice_sched_node *root;
27 root = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*root), GFP_KERNEL);
31 /* coverity[suspicious_sizeof] */
32 root->children = devm_kcalloc(ice_hw_to_dev(hw), hw->max_children[0],
33 sizeof(*root), GFP_KERNEL);
34 if (!root->children) {
35 devm_kfree(ice_hw_to_dev(hw), root);
39 memcpy(&root->info, info, sizeof(*info));
45 * ice_sched_find_node_by_teid - Find the Tx scheduler node in SW DB
46 * @start_node: pointer to the starting ice_sched_node struct in a sub-tree
47 * @teid: node TEID to search
49 * This function searches for a node matching the TEID in the scheduling tree
50 * from the SW DB. The search is recursive and is restricted by the number of
51 * layers it has searched through; stopping at the max supported layer.
53 * This function needs to be called when holding the port_info->sched_lock
55 struct ice_sched_node *
56 ice_sched_find_node_by_teid(struct ice_sched_node *start_node, u32 teid)
60 /* The TEID is same as that of the start_node */
61 if (ICE_TXSCHED_GET_NODE_TEID(start_node) == teid)
64 /* The node has no children or is at the max layer */
65 if (!start_node->num_children ||
66 start_node->tx_sched_layer >= ICE_AQC_TOPO_MAX_LEVEL_NUM ||
67 start_node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF)
70 /* Check if TEID matches to any of the children nodes */
71 for (i = 0; i < start_node->num_children; i++)
72 if (ICE_TXSCHED_GET_NODE_TEID(start_node->children[i]) == teid)
73 return start_node->children[i];
75 /* Search within each child's sub-tree */
76 for (i = 0; i < start_node->num_children; i++) {
77 struct ice_sched_node *tmp;
79 tmp = ice_sched_find_node_by_teid(start_node->children[i],
89 * ice_aqc_send_sched_elem_cmd - send scheduling elements cmd
90 * @hw: pointer to the HW struct
91 * @cmd_opc: cmd opcode
92 * @elems_req: number of elements to request
93 * @buf: pointer to buffer
94 * @buf_size: buffer size in bytes
95 * @elems_resp: returns total number of elements response
96 * @cd: pointer to command details structure or NULL
98 * This function sends a scheduling elements cmd (cmd_opc)
101 ice_aqc_send_sched_elem_cmd(struct ice_hw *hw, enum ice_adminq_opc cmd_opc,
102 u16 elems_req, void *buf, u16 buf_size,
103 u16 *elems_resp, struct ice_sq_cd *cd)
105 struct ice_aqc_sched_elem_cmd *cmd;
106 struct ice_aq_desc desc;
109 cmd = &desc.params.sched_elem_cmd;
110 ice_fill_dflt_direct_cmd_desc(&desc, cmd_opc);
111 cmd->num_elem_req = cpu_to_le16(elems_req);
112 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
113 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
114 if (!status && elems_resp)
115 *elems_resp = le16_to_cpu(cmd->num_elem_resp);
121 * ice_aq_query_sched_elems - query scheduler elements
122 * @hw: pointer to the HW struct
123 * @elems_req: number of elements to query
124 * @buf: pointer to buffer
125 * @buf_size: buffer size in bytes
126 * @elems_ret: returns total number of elements returned
127 * @cd: pointer to command details structure or NULL
129 * Query scheduling elements (0x0404)
132 ice_aq_query_sched_elems(struct ice_hw *hw, u16 elems_req,
133 struct ice_aqc_txsched_elem_data *buf, u16 buf_size,
134 u16 *elems_ret, struct ice_sq_cd *cd)
136 return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_get_sched_elems,
137 elems_req, (void *)buf, buf_size,
142 * ice_sched_add_node - Insert the Tx scheduler node in SW DB
143 * @pi: port information structure
144 * @layer: Scheduler layer of the node
145 * @info: Scheduler element information from firmware
146 * @prealloc_node: preallocated ice_sched_node struct for SW DB
148 * This function inserts a scheduler node to the SW DB.
151 ice_sched_add_node(struct ice_port_info *pi, u8 layer,
152 struct ice_aqc_txsched_elem_data *info,
153 struct ice_sched_node *prealloc_node)
155 struct ice_aqc_txsched_elem_data elem;
156 struct ice_sched_node *parent;
157 struct ice_sched_node *node;
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));
170 ice_debug(hw, ICE_DBG_SCHED, "Parent Node not found for parent_teid=0x%x\n",
171 le32_to_cpu(info->parent_teid));
175 /* query the current node information from FW before adding it
178 status = ice_sched_query_elem(hw, le32_to_cpu(info->node_teid), &elem);
183 node = prealloc_node;
185 node = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*node), GFP_KERNEL);
188 if (hw->max_children[layer]) {
189 /* coverity[suspicious_sizeof] */
190 node->children = devm_kcalloc(ice_hw_to_dev(hw),
191 hw->max_children[layer],
192 sizeof(*node), GFP_KERNEL);
193 if (!node->children) {
194 devm_kfree(ice_hw_to_dev(hw), node);
200 node->parent = parent;
201 node->tx_sched_layer = layer;
202 parent->children[parent->num_children++] = node;
208 * ice_aq_delete_sched_elems - delete scheduler elements
209 * @hw: pointer to the HW struct
210 * @grps_req: number of groups to delete
211 * @buf: pointer to buffer
212 * @buf_size: buffer size in bytes
213 * @grps_del: returns total number of elements deleted
214 * @cd: pointer to command details structure or NULL
216 * Delete scheduling elements (0x040F)
219 ice_aq_delete_sched_elems(struct ice_hw *hw, u16 grps_req,
220 struct ice_aqc_delete_elem *buf, u16 buf_size,
221 u16 *grps_del, struct ice_sq_cd *cd)
223 return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_delete_sched_elems,
224 grps_req, (void *)buf, buf_size,
229 * ice_sched_remove_elems - remove nodes from HW
230 * @hw: pointer to the HW struct
231 * @parent: pointer to the parent node
232 * @num_nodes: number of nodes
233 * @node_teids: array of node teids to be deleted
235 * This function remove nodes from HW
238 ice_sched_remove_elems(struct ice_hw *hw, struct ice_sched_node *parent,
239 u16 num_nodes, u32 *node_teids)
241 struct ice_aqc_delete_elem *buf;
242 u16 i, num_groups_removed = 0;
246 buf_size = struct_size(buf, teid, num_nodes);
247 buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
251 buf->hdr.parent_teid = parent->info.node_teid;
252 buf->hdr.num_elems = cpu_to_le16(num_nodes);
253 for (i = 0; i < num_nodes; i++)
254 buf->teid[i] = cpu_to_le32(node_teids[i]);
256 status = ice_aq_delete_sched_elems(hw, 1, buf, buf_size,
257 &num_groups_removed, NULL);
258 if (status || num_groups_removed != 1)
259 ice_debug(hw, ICE_DBG_SCHED, "remove node failed FW error %d\n",
260 hw->adminq.sq_last_status);
262 devm_kfree(ice_hw_to_dev(hw), buf);
267 * ice_sched_get_first_node - get the first node of the given layer
268 * @pi: port information structure
269 * @parent: pointer the base node of the subtree
270 * @layer: layer number
272 * This function retrieves the first node of the given layer from the subtree
274 static struct ice_sched_node *
275 ice_sched_get_first_node(struct ice_port_info *pi,
276 struct ice_sched_node *parent, u8 layer)
278 return pi->sib_head[parent->tc_num][layer];
282 * ice_sched_get_tc_node - get pointer to TC node
283 * @pi: port information structure
286 * This function returns the TC node pointer
288 struct ice_sched_node *ice_sched_get_tc_node(struct ice_port_info *pi, u8 tc)
292 if (!pi || !pi->root)
294 for (i = 0; i < pi->root->num_children; i++)
295 if (pi->root->children[i]->tc_num == tc)
296 return pi->root->children[i];
301 * ice_free_sched_node - Free a Tx scheduler node from SW DB
302 * @pi: port information structure
303 * @node: pointer to the ice_sched_node struct
305 * This function frees up a node from SW DB as well as from HW
307 * This function needs to be called with the port_info->sched_lock held
309 void ice_free_sched_node(struct ice_port_info *pi, struct ice_sched_node *node)
311 struct ice_sched_node *parent;
312 struct ice_hw *hw = pi->hw;
315 /* Free the children before freeing up the parent node
316 * The parent array is updated below and that shifts the nodes
317 * in the array. So always pick the first child if num children > 0
319 while (node->num_children)
320 ice_free_sched_node(pi, node->children[0]);
322 /* Leaf, TC and root nodes can't be deleted by SW */
323 if (node->tx_sched_layer >= hw->sw_entry_point_layer &&
324 node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
325 node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT &&
326 node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF) {
327 u32 teid = le32_to_cpu(node->info.node_teid);
329 ice_sched_remove_elems(hw, node->parent, 1, &teid);
331 parent = node->parent;
332 /* root has no parent */
334 struct ice_sched_node *p;
336 /* update the parent */
337 for (i = 0; i < parent->num_children; i++)
338 if (parent->children[i] == node) {
339 for (j = i + 1; j < parent->num_children; j++)
340 parent->children[j - 1] =
342 parent->num_children--;
346 p = ice_sched_get_first_node(pi, node, node->tx_sched_layer);
348 if (p->sibling == node) {
349 p->sibling = node->sibling;
355 /* update the sibling head if head is getting removed */
356 if (pi->sib_head[node->tc_num][node->tx_sched_layer] == node)
357 pi->sib_head[node->tc_num][node->tx_sched_layer] =
361 devm_kfree(ice_hw_to_dev(hw), node->children);
363 xa_erase(&pi->sched_node_ids, node->id);
364 devm_kfree(ice_hw_to_dev(hw), node);
368 * ice_aq_get_dflt_topo - gets default scheduler topology
369 * @hw: pointer to the HW struct
370 * @lport: logical port number
371 * @buf: pointer to buffer
372 * @buf_size: buffer size in bytes
373 * @num_branches: returns total number of queue to port branches
374 * @cd: pointer to command details structure or NULL
376 * Get default scheduler topology (0x400)
379 ice_aq_get_dflt_topo(struct ice_hw *hw, u8 lport,
380 struct ice_aqc_get_topo_elem *buf, u16 buf_size,
381 u8 *num_branches, struct ice_sq_cd *cd)
383 struct ice_aqc_get_topo *cmd;
384 struct ice_aq_desc desc;
387 cmd = &desc.params.get_topo;
388 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_dflt_topo);
389 cmd->port_num = lport;
390 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
391 if (!status && num_branches)
392 *num_branches = cmd->num_branches;
398 * ice_aq_add_sched_elems - adds scheduling element
399 * @hw: pointer to the HW struct
400 * @grps_req: the number of groups that are requested to be added
401 * @buf: pointer to buffer
402 * @buf_size: buffer size in bytes
403 * @grps_added: returns total number of groups added
404 * @cd: pointer to command details structure or NULL
406 * Add scheduling elements (0x0401)
409 ice_aq_add_sched_elems(struct ice_hw *hw, u16 grps_req,
410 struct ice_aqc_add_elem *buf, u16 buf_size,
411 u16 *grps_added, struct ice_sq_cd *cd)
413 return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_add_sched_elems,
414 grps_req, (void *)buf, buf_size,
419 * ice_aq_cfg_sched_elems - configures scheduler elements
420 * @hw: pointer to the HW struct
421 * @elems_req: number of elements to configure
422 * @buf: pointer to buffer
423 * @buf_size: buffer size in bytes
424 * @elems_cfgd: returns total number of elements configured
425 * @cd: pointer to command details structure or NULL
427 * Configure scheduling elements (0x0403)
430 ice_aq_cfg_sched_elems(struct ice_hw *hw, u16 elems_req,
431 struct ice_aqc_txsched_elem_data *buf, u16 buf_size,
432 u16 *elems_cfgd, struct ice_sq_cd *cd)
434 return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_cfg_sched_elems,
435 elems_req, (void *)buf, buf_size,
440 * ice_aq_move_sched_elems - move scheduler elements
441 * @hw: pointer to the HW struct
442 * @grps_req: number of groups to move
443 * @buf: pointer to buffer
444 * @buf_size: buffer size in bytes
445 * @grps_movd: returns total number of groups moved
446 * @cd: pointer to command details structure or NULL
448 * Move scheduling elements (0x0408)
451 ice_aq_move_sched_elems(struct ice_hw *hw, u16 grps_req,
452 struct ice_aqc_move_elem *buf, u16 buf_size,
453 u16 *grps_movd, struct ice_sq_cd *cd)
455 return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_move_sched_elems,
456 grps_req, (void *)buf, buf_size,
461 * ice_aq_suspend_sched_elems - suspend scheduler elements
462 * @hw: pointer to the HW struct
463 * @elems_req: number of elements to suspend
464 * @buf: pointer to buffer
465 * @buf_size: buffer size in bytes
466 * @elems_ret: returns total number of elements suspended
467 * @cd: pointer to command details structure or NULL
469 * Suspend scheduling elements (0x0409)
472 ice_aq_suspend_sched_elems(struct ice_hw *hw, u16 elems_req, __le32 *buf,
473 u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
475 return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_suspend_sched_elems,
476 elems_req, (void *)buf, buf_size,
481 * ice_aq_resume_sched_elems - resume scheduler elements
482 * @hw: pointer to the HW struct
483 * @elems_req: number of elements to resume
484 * @buf: pointer to buffer
485 * @buf_size: buffer size in bytes
486 * @elems_ret: returns total number of elements resumed
487 * @cd: pointer to command details structure or NULL
489 * resume scheduling elements (0x040A)
492 ice_aq_resume_sched_elems(struct ice_hw *hw, u16 elems_req, __le32 *buf,
493 u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
495 return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_resume_sched_elems,
496 elems_req, (void *)buf, buf_size,
501 * ice_aq_query_sched_res - query scheduler resource
502 * @hw: pointer to the HW struct
503 * @buf_size: buffer size in bytes
504 * @buf: pointer to buffer
505 * @cd: pointer to command details structure or NULL
507 * Query scheduler resource allocation (0x0412)
510 ice_aq_query_sched_res(struct ice_hw *hw, u16 buf_size,
511 struct ice_aqc_query_txsched_res_resp *buf,
512 struct ice_sq_cd *cd)
514 struct ice_aq_desc desc;
516 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_sched_res);
517 return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
521 * ice_sched_suspend_resume_elems - suspend or resume HW nodes
522 * @hw: pointer to the HW struct
523 * @num_nodes: number of nodes
524 * @node_teids: array of node teids to be suspended or resumed
525 * @suspend: true means suspend / false means resume
527 * This function suspends or resumes HW nodes
530 ice_sched_suspend_resume_elems(struct ice_hw *hw, u8 num_nodes, u32 *node_teids,
533 u16 i, buf_size, num_elem_ret = 0;
537 buf_size = sizeof(*buf) * num_nodes;
538 buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
542 for (i = 0; i < num_nodes; i++)
543 buf[i] = cpu_to_le32(node_teids[i]);
546 status = ice_aq_suspend_sched_elems(hw, num_nodes, buf,
547 buf_size, &num_elem_ret,
550 status = ice_aq_resume_sched_elems(hw, num_nodes, buf,
551 buf_size, &num_elem_ret,
553 if (status || num_elem_ret != num_nodes)
554 ice_debug(hw, ICE_DBG_SCHED, "suspend/resume failed\n");
556 devm_kfree(ice_hw_to_dev(hw), buf);
561 * ice_alloc_lan_q_ctx - allocate LAN queue contexts for the given VSI and TC
562 * @hw: pointer to the HW struct
563 * @vsi_handle: VSI handle
565 * @new_numqs: number of queues
568 ice_alloc_lan_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 new_numqs)
570 struct ice_vsi_ctx *vsi_ctx;
571 struct ice_q_ctx *q_ctx;
573 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
576 /* allocate LAN queue contexts */
577 if (!vsi_ctx->lan_q_ctx[tc]) {
578 vsi_ctx->lan_q_ctx[tc] = devm_kcalloc(ice_hw_to_dev(hw),
582 if (!vsi_ctx->lan_q_ctx[tc])
584 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
587 /* num queues are increased, update the queue contexts */
588 if (new_numqs > vsi_ctx->num_lan_q_entries[tc]) {
589 u16 prev_num = vsi_ctx->num_lan_q_entries[tc];
591 q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
592 sizeof(*q_ctx), GFP_KERNEL);
595 memcpy(q_ctx, vsi_ctx->lan_q_ctx[tc],
596 prev_num * sizeof(*q_ctx));
597 devm_kfree(ice_hw_to_dev(hw), vsi_ctx->lan_q_ctx[tc]);
598 vsi_ctx->lan_q_ctx[tc] = q_ctx;
599 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
605 * ice_alloc_rdma_q_ctx - allocate RDMA queue contexts for the given VSI and TC
606 * @hw: pointer to the HW struct
607 * @vsi_handle: VSI handle
609 * @new_numqs: number of queues
612 ice_alloc_rdma_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 new_numqs)
614 struct ice_vsi_ctx *vsi_ctx;
615 struct ice_q_ctx *q_ctx;
617 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
620 /* allocate RDMA queue contexts */
621 if (!vsi_ctx->rdma_q_ctx[tc]) {
622 vsi_ctx->rdma_q_ctx[tc] = devm_kcalloc(ice_hw_to_dev(hw),
626 if (!vsi_ctx->rdma_q_ctx[tc])
628 vsi_ctx->num_rdma_q_entries[tc] = new_numqs;
631 /* num queues are increased, update the queue contexts */
632 if (new_numqs > vsi_ctx->num_rdma_q_entries[tc]) {
633 u16 prev_num = vsi_ctx->num_rdma_q_entries[tc];
635 q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
636 sizeof(*q_ctx), GFP_KERNEL);
639 memcpy(q_ctx, vsi_ctx->rdma_q_ctx[tc],
640 prev_num * sizeof(*q_ctx));
641 devm_kfree(ice_hw_to_dev(hw), vsi_ctx->rdma_q_ctx[tc]);
642 vsi_ctx->rdma_q_ctx[tc] = q_ctx;
643 vsi_ctx->num_rdma_q_entries[tc] = new_numqs;
649 * ice_aq_rl_profile - performs a rate limiting task
650 * @hw: pointer to the HW struct
651 * @opcode: opcode for add, query, or remove profile(s)
652 * @num_profiles: the number of profiles
653 * @buf: pointer to buffer
654 * @buf_size: buffer size in bytes
655 * @num_processed: number of processed add or remove profile(s) to return
656 * @cd: pointer to command details structure
658 * RL profile function to add, query, or remove profile(s)
661 ice_aq_rl_profile(struct ice_hw *hw, enum ice_adminq_opc opcode,
662 u16 num_profiles, struct ice_aqc_rl_profile_elem *buf,
663 u16 buf_size, u16 *num_processed, struct ice_sq_cd *cd)
665 struct ice_aqc_rl_profile *cmd;
666 struct ice_aq_desc desc;
669 cmd = &desc.params.rl_profile;
671 ice_fill_dflt_direct_cmd_desc(&desc, opcode);
672 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
673 cmd->num_profiles = cpu_to_le16(num_profiles);
674 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
675 if (!status && num_processed)
676 *num_processed = le16_to_cpu(cmd->num_processed);
681 * ice_aq_add_rl_profile - adds rate limiting profile(s)
682 * @hw: pointer to the HW struct
683 * @num_profiles: the number of profile(s) to be add
684 * @buf: pointer to buffer
685 * @buf_size: buffer size in bytes
686 * @num_profiles_added: total number of profiles added to return
687 * @cd: pointer to command details structure
689 * Add RL profile (0x0410)
692 ice_aq_add_rl_profile(struct ice_hw *hw, u16 num_profiles,
693 struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
694 u16 *num_profiles_added, struct ice_sq_cd *cd)
696 return ice_aq_rl_profile(hw, ice_aqc_opc_add_rl_profiles, num_profiles,
697 buf, buf_size, num_profiles_added, cd);
701 * ice_aq_remove_rl_profile - removes RL profile(s)
702 * @hw: pointer to the HW struct
703 * @num_profiles: the number of profile(s) to remove
704 * @buf: pointer to buffer
705 * @buf_size: buffer size in bytes
706 * @num_profiles_removed: total number of profiles removed to return
707 * @cd: pointer to command details structure or NULL
709 * Remove RL profile (0x0415)
712 ice_aq_remove_rl_profile(struct ice_hw *hw, u16 num_profiles,
713 struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
714 u16 *num_profiles_removed, struct ice_sq_cd *cd)
716 return ice_aq_rl_profile(hw, ice_aqc_opc_remove_rl_profiles,
717 num_profiles, buf, buf_size,
718 num_profiles_removed, cd);
722 * ice_sched_del_rl_profile - remove RL profile
723 * @hw: pointer to the HW struct
724 * @rl_info: rate limit profile information
726 * If the profile ID is not referenced anymore, it removes profile ID with
727 * its associated parameters from HW DB,and locally. The caller needs to
728 * hold scheduler lock.
731 ice_sched_del_rl_profile(struct ice_hw *hw,
732 struct ice_aqc_rl_profile_info *rl_info)
734 struct ice_aqc_rl_profile_elem *buf;
735 u16 num_profiles_removed;
736 u16 num_profiles = 1;
739 if (rl_info->prof_id_ref != 0)
742 /* Safe to remove profile ID */
743 buf = &rl_info->profile;
744 status = ice_aq_remove_rl_profile(hw, num_profiles, buf, sizeof(*buf),
745 &num_profiles_removed, NULL);
746 if (status || num_profiles_removed != num_profiles)
749 /* Delete stale entry now */
750 list_del(&rl_info->list_entry);
751 devm_kfree(ice_hw_to_dev(hw), rl_info);
756 * ice_sched_clear_rl_prof - clears RL prof entries
757 * @pi: port information structure
759 * This function removes all RL profile from HW as well as from SW DB.
761 static void ice_sched_clear_rl_prof(struct ice_port_info *pi)
765 for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
766 struct ice_aqc_rl_profile_info *rl_prof_elem;
767 struct ice_aqc_rl_profile_info *rl_prof_tmp;
769 list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
770 &pi->rl_prof_list[ln], list_entry) {
771 struct ice_hw *hw = pi->hw;
774 rl_prof_elem->prof_id_ref = 0;
775 status = ice_sched_del_rl_profile(hw, rl_prof_elem);
777 ice_debug(hw, ICE_DBG_SCHED, "Remove rl profile failed\n");
778 /* On error, free mem required */
779 list_del(&rl_prof_elem->list_entry);
780 devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
787 * ice_sched_clear_agg - clears the aggregator related information
788 * @hw: pointer to the hardware structure
790 * This function removes aggregator list and free up aggregator related memory
791 * previously allocated.
793 void ice_sched_clear_agg(struct ice_hw *hw)
795 struct ice_sched_agg_info *agg_info;
796 struct ice_sched_agg_info *atmp;
798 list_for_each_entry_safe(agg_info, atmp, &hw->agg_list, list_entry) {
799 struct ice_sched_agg_vsi_info *agg_vsi_info;
800 struct ice_sched_agg_vsi_info *vtmp;
802 list_for_each_entry_safe(agg_vsi_info, vtmp,
803 &agg_info->agg_vsi_list, list_entry) {
804 list_del(&agg_vsi_info->list_entry);
805 devm_kfree(ice_hw_to_dev(hw), agg_vsi_info);
807 list_del(&agg_info->list_entry);
808 devm_kfree(ice_hw_to_dev(hw), agg_info);
813 * ice_sched_clear_tx_topo - clears the scheduler tree nodes
814 * @pi: port information structure
816 * This function removes all the nodes from HW as well as from SW DB.
818 static void ice_sched_clear_tx_topo(struct ice_port_info *pi)
822 /* remove RL profiles related lists */
823 ice_sched_clear_rl_prof(pi);
825 ice_free_sched_node(pi, pi->root);
831 * ice_sched_clear_port - clear the scheduler elements from SW DB for a port
832 * @pi: port information structure
834 * Cleanup scheduling elements from SW DB
836 void ice_sched_clear_port(struct ice_port_info *pi)
838 if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
841 pi->port_state = ICE_SCHED_PORT_STATE_INIT;
842 mutex_lock(&pi->sched_lock);
843 ice_sched_clear_tx_topo(pi);
844 mutex_unlock(&pi->sched_lock);
845 mutex_destroy(&pi->sched_lock);
849 * ice_sched_cleanup_all - cleanup scheduler elements from SW DB for all ports
850 * @hw: pointer to the HW struct
852 * Cleanup scheduling elements from SW DB for all the ports
854 void ice_sched_cleanup_all(struct ice_hw *hw)
859 devm_kfree(ice_hw_to_dev(hw), hw->layer_info);
860 hw->layer_info = NULL;
862 ice_sched_clear_port(hw->port_info);
864 hw->num_tx_sched_layers = 0;
865 hw->num_tx_sched_phys_layers = 0;
866 hw->flattened_layers = 0;
871 * ice_sched_add_elems - add nodes to HW and SW DB
872 * @pi: port information structure
873 * @tc_node: pointer to the branch node
874 * @parent: pointer to the parent node
875 * @layer: layer number to add nodes
876 * @num_nodes: number of nodes
877 * @num_nodes_added: pointer to num nodes added
878 * @first_node_teid: if new nodes are added then return the TEID of first node
879 * @prealloc_nodes: preallocated nodes struct for software DB
881 * This function add nodes to HW as well as to SW DB for a given layer
884 ice_sched_add_elems(struct ice_port_info *pi, struct ice_sched_node *tc_node,
885 struct ice_sched_node *parent, u8 layer, u16 num_nodes,
886 u16 *num_nodes_added, u32 *first_node_teid,
887 struct ice_sched_node **prealloc_nodes)
889 struct ice_sched_node *prev, *new_node;
890 struct ice_aqc_add_elem *buf;
891 u16 i, num_groups_added = 0;
892 struct ice_hw *hw = pi->hw;
897 buf_size = struct_size(buf, generic, num_nodes);
898 buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
902 buf->hdr.parent_teid = parent->info.node_teid;
903 buf->hdr.num_elems = cpu_to_le16(num_nodes);
904 for (i = 0; i < num_nodes; i++) {
905 buf->generic[i].parent_teid = parent->info.node_teid;
906 buf->generic[i].data.elem_type = ICE_AQC_ELEM_TYPE_SE_GENERIC;
907 buf->generic[i].data.valid_sections =
908 ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
909 ICE_AQC_ELEM_VALID_EIR;
910 buf->generic[i].data.generic = 0;
911 buf->generic[i].data.cir_bw.bw_profile_idx =
912 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
913 buf->generic[i].data.cir_bw.bw_alloc =
914 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
915 buf->generic[i].data.eir_bw.bw_profile_idx =
916 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
917 buf->generic[i].data.eir_bw.bw_alloc =
918 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
921 status = ice_aq_add_sched_elems(hw, 1, buf, buf_size,
922 &num_groups_added, NULL);
923 if (status || num_groups_added != 1) {
924 ice_debug(hw, ICE_DBG_SCHED, "add node failed FW Error %d\n",
925 hw->adminq.sq_last_status);
926 devm_kfree(ice_hw_to_dev(hw), buf);
930 *num_nodes_added = num_nodes;
931 /* add nodes to the SW DB */
932 for (i = 0; i < num_nodes; i++) {
934 status = ice_sched_add_node(pi, layer, &buf->generic[i], prealloc_nodes[i]);
936 status = ice_sched_add_node(pi, layer, &buf->generic[i], NULL);
939 ice_debug(hw, ICE_DBG_SCHED, "add nodes in SW DB failed status =%d\n",
944 teid = le32_to_cpu(buf->generic[i].node_teid);
945 new_node = ice_sched_find_node_by_teid(parent, teid);
947 ice_debug(hw, ICE_DBG_SCHED, "Node is missing for teid =%d\n", teid);
951 new_node->sibling = NULL;
952 new_node->tc_num = tc_node->tc_num;
953 new_node->tx_weight = ICE_SCHED_DFLT_BW_WT;
954 new_node->tx_share = ICE_SCHED_DFLT_BW;
955 new_node->tx_max = ICE_SCHED_DFLT_BW;
956 new_node->name = kzalloc(SCHED_NODE_NAME_MAX_LEN, GFP_KERNEL);
960 status = xa_alloc(&pi->sched_node_ids, &new_node->id, NULL, XA_LIMIT(0, UINT_MAX),
963 ice_debug(hw, ICE_DBG_SCHED, "xa_alloc failed for sched node status =%d\n",
968 snprintf(new_node->name, SCHED_NODE_NAME_MAX_LEN, "node_%u", new_node->id);
970 /* add it to previous node sibling pointer */
971 /* Note: siblings are not linked across branches */
972 prev = ice_sched_get_first_node(pi, tc_node, layer);
973 if (prev && prev != new_node) {
974 while (prev->sibling)
975 prev = prev->sibling;
976 prev->sibling = new_node;
979 /* initialize the sibling head */
980 if (!pi->sib_head[tc_node->tc_num][layer])
981 pi->sib_head[tc_node->tc_num][layer] = new_node;
984 *first_node_teid = teid;
987 devm_kfree(ice_hw_to_dev(hw), buf);
992 * ice_sched_add_nodes_to_hw_layer - Add nodes to HW layer
993 * @pi: port information structure
994 * @tc_node: pointer to TC node
995 * @parent: pointer to parent node
996 * @layer: layer number to add nodes
997 * @num_nodes: number of nodes to be added
998 * @first_node_teid: pointer to the first node TEID
999 * @num_nodes_added: pointer to number of nodes added
1001 * Add nodes into specific HW layer.
1004 ice_sched_add_nodes_to_hw_layer(struct ice_port_info *pi,
1005 struct ice_sched_node *tc_node,
1006 struct ice_sched_node *parent, u8 layer,
1007 u16 num_nodes, u32 *first_node_teid,
1008 u16 *num_nodes_added)
1010 u16 max_child_nodes;
1012 *num_nodes_added = 0;
1017 if (!parent || layer < pi->hw->sw_entry_point_layer)
1020 /* max children per node per layer */
1021 max_child_nodes = pi->hw->max_children[parent->tx_sched_layer];
1023 /* current number of children + required nodes exceed max children */
1024 if ((parent->num_children + num_nodes) > max_child_nodes) {
1025 /* Fail if the parent is a TC node */
1026 if (parent == tc_node)
1031 return ice_sched_add_elems(pi, tc_node, parent, layer, num_nodes,
1032 num_nodes_added, first_node_teid, NULL);
1036 * ice_sched_add_nodes_to_layer - Add nodes to a given layer
1037 * @pi: port information structure
1038 * @tc_node: pointer to TC node
1039 * @parent: pointer to parent node
1040 * @layer: layer number to add nodes
1041 * @num_nodes: number of nodes to be added
1042 * @first_node_teid: pointer to the first node TEID
1043 * @num_nodes_added: pointer to number of nodes added
1045 * This function add nodes to a given layer.
1048 ice_sched_add_nodes_to_layer(struct ice_port_info *pi,
1049 struct ice_sched_node *tc_node,
1050 struct ice_sched_node *parent, u8 layer,
1051 u16 num_nodes, u32 *first_node_teid,
1052 u16 *num_nodes_added)
1054 u32 *first_teid_ptr = first_node_teid;
1055 u16 new_num_nodes = num_nodes;
1058 *num_nodes_added = 0;
1059 while (*num_nodes_added < num_nodes) {
1060 u16 max_child_nodes, num_added = 0;
1063 status = ice_sched_add_nodes_to_hw_layer(pi, tc_node, parent,
1064 layer, new_num_nodes,
1068 *num_nodes_added += num_added;
1069 /* added more nodes than requested ? */
1070 if (*num_nodes_added > num_nodes) {
1071 ice_debug(pi->hw, ICE_DBG_SCHED, "added extra nodes %d %d\n", num_nodes,
1076 /* break if all the nodes are added successfully */
1077 if (!status && (*num_nodes_added == num_nodes))
1079 /* break if the error is not max limit */
1080 if (status && status != -ENOSPC)
1082 /* Exceeded the max children */
1083 max_child_nodes = pi->hw->max_children[parent->tx_sched_layer];
1084 /* utilize all the spaces if the parent is not full */
1085 if (parent->num_children < max_child_nodes) {
1086 new_num_nodes = max_child_nodes - parent->num_children;
1088 /* This parent is full, try the next sibling */
1089 parent = parent->sibling;
1090 /* Don't modify the first node TEID memory if the
1091 * first node was added already in the above call.
1092 * Instead send some temp memory for all other
1096 first_teid_ptr = &temp;
1098 new_num_nodes = num_nodes - *num_nodes_added;
1105 * ice_sched_get_qgrp_layer - get the current queue group layer number
1106 * @hw: pointer to the HW struct
1108 * This function returns the current queue group layer number
1110 static u8 ice_sched_get_qgrp_layer(struct ice_hw *hw)
1112 /* It's always total layers - 1, the array is 0 relative so -2 */
1113 return hw->num_tx_sched_layers - ICE_QGRP_LAYER_OFFSET;
1117 * ice_sched_get_vsi_layer - get the current VSI layer number
1118 * @hw: pointer to the HW struct
1120 * This function returns the current VSI layer number
1122 static u8 ice_sched_get_vsi_layer(struct ice_hw *hw)
1124 /* Num Layers VSI layer
1127 * 5 or less sw_entry_point_layer
1129 /* calculate the VSI layer based on number of layers. */
1130 if (hw->num_tx_sched_layers > ICE_VSI_LAYER_OFFSET + 1) {
1131 u8 layer = hw->num_tx_sched_layers - ICE_VSI_LAYER_OFFSET;
1133 if (layer > hw->sw_entry_point_layer)
1136 return hw->sw_entry_point_layer;
1140 * ice_sched_get_agg_layer - get the current aggregator layer number
1141 * @hw: pointer to the HW struct
1143 * This function returns the current aggregator layer number
1145 static u8 ice_sched_get_agg_layer(struct ice_hw *hw)
1147 /* Num Layers aggregator layer
1149 * 7 or less sw_entry_point_layer
1151 /* calculate the aggregator layer based on number of layers. */
1152 if (hw->num_tx_sched_layers > ICE_AGG_LAYER_OFFSET + 1) {
1153 u8 layer = hw->num_tx_sched_layers - ICE_AGG_LAYER_OFFSET;
1155 if (layer > hw->sw_entry_point_layer)
1158 return hw->sw_entry_point_layer;
1162 * ice_rm_dflt_leaf_node - remove the default leaf node in the tree
1163 * @pi: port information structure
1165 * This function removes the leaf node that was created by the FW
1166 * during initialization
1168 static void ice_rm_dflt_leaf_node(struct ice_port_info *pi)
1170 struct ice_sched_node *node;
1174 if (!node->num_children)
1176 node = node->children[0];
1178 if (node && node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF) {
1179 u32 teid = le32_to_cpu(node->info.node_teid);
1182 /* remove the default leaf node */
1183 status = ice_sched_remove_elems(pi->hw, node->parent, 1, &teid);
1185 ice_free_sched_node(pi, node);
1190 * ice_sched_rm_dflt_nodes - free the default nodes in the tree
1191 * @pi: port information structure
1193 * This function frees all the nodes except root and TC that were created by
1194 * the FW during initialization
1196 static void ice_sched_rm_dflt_nodes(struct ice_port_info *pi)
1198 struct ice_sched_node *node;
1200 ice_rm_dflt_leaf_node(pi);
1202 /* remove the default nodes except TC and root nodes */
1205 if (node->tx_sched_layer >= pi->hw->sw_entry_point_layer &&
1206 node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
1207 node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT) {
1208 ice_free_sched_node(pi, node);
1212 if (!node->num_children)
1214 node = node->children[0];
1219 * ice_sched_init_port - Initialize scheduler by querying information from FW
1220 * @pi: port info structure for the tree to cleanup
1222 * This function is the initial call to find the total number of Tx scheduler
1223 * resources, default topology created by firmware and storing the information
1226 int ice_sched_init_port(struct ice_port_info *pi)
1228 struct ice_aqc_get_topo_elem *buf;
1239 /* Query the Default Topology from FW */
1240 buf = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
1244 /* Query default scheduling tree topology */
1245 status = ice_aq_get_dflt_topo(hw, pi->lport, buf, ICE_AQ_MAX_BUF_LEN,
1246 &num_branches, NULL);
1250 /* num_branches should be between 1-8 */
1251 if (num_branches < 1 || num_branches > ICE_TXSCHED_MAX_BRANCHES) {
1252 ice_debug(hw, ICE_DBG_SCHED, "num_branches unexpected %d\n",
1258 /* get the number of elements on the default/first branch */
1259 num_elems = le16_to_cpu(buf[0].hdr.num_elems);
1261 /* num_elems should always be between 1-9 */
1262 if (num_elems < 1 || num_elems > ICE_AQC_TOPO_MAX_LEVEL_NUM) {
1263 ice_debug(hw, ICE_DBG_SCHED, "num_elems unexpected %d\n",
1269 /* If the last node is a leaf node then the index of the queue group
1270 * layer is two less than the number of elements.
1272 if (num_elems > 2 && buf[0].generic[num_elems - 1].data.elem_type ==
1273 ICE_AQC_ELEM_TYPE_LEAF)
1274 pi->last_node_teid =
1275 le32_to_cpu(buf[0].generic[num_elems - 2].node_teid);
1277 pi->last_node_teid =
1278 le32_to_cpu(buf[0].generic[num_elems - 1].node_teid);
1280 /* Insert the Tx Sched root node */
1281 status = ice_sched_add_root_node(pi, &buf[0].generic[0]);
1285 /* Parse the default tree and cache the information */
1286 for (i = 0; i < num_branches; i++) {
1287 num_elems = le16_to_cpu(buf[i].hdr.num_elems);
1289 /* Skip root element as already inserted */
1290 for (j = 1; j < num_elems; j++) {
1291 /* update the sw entry point */
1292 if (buf[0].generic[j].data.elem_type ==
1293 ICE_AQC_ELEM_TYPE_ENTRY_POINT)
1294 hw->sw_entry_point_layer = j;
1296 status = ice_sched_add_node(pi, j, &buf[i].generic[j], NULL);
1302 /* Remove the default nodes. */
1304 ice_sched_rm_dflt_nodes(pi);
1306 /* initialize the port for handling the scheduler tree */
1307 pi->port_state = ICE_SCHED_PORT_STATE_READY;
1308 mutex_init(&pi->sched_lock);
1309 for (i = 0; i < ICE_AQC_TOPO_MAX_LEVEL_NUM; i++)
1310 INIT_LIST_HEAD(&pi->rl_prof_list[i]);
1313 if (status && pi->root) {
1314 ice_free_sched_node(pi, pi->root);
1323 * ice_sched_query_res_alloc - query the FW for num of logical sched layers
1324 * @hw: pointer to the HW struct
1326 * query FW for allocated scheduler resources and store in HW struct
1328 int ice_sched_query_res_alloc(struct ice_hw *hw)
1330 struct ice_aqc_query_txsched_res_resp *buf;
1338 buf = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*buf), GFP_KERNEL);
1342 status = ice_aq_query_sched_res(hw, sizeof(*buf), buf, NULL);
1344 goto sched_query_out;
1346 hw->num_tx_sched_layers = le16_to_cpu(buf->sched_props.logical_levels);
1347 hw->num_tx_sched_phys_layers =
1348 le16_to_cpu(buf->sched_props.phys_levels);
1349 hw->flattened_layers = buf->sched_props.flattening_bitmap;
1350 hw->max_cgds = buf->sched_props.max_pf_cgds;
1352 /* max sibling group size of current layer refers to the max children
1353 * of the below layer node.
1354 * layer 1 node max children will be layer 2 max sibling group size
1355 * layer 2 node max children will be layer 3 max sibling group size
1356 * and so on. This array will be populated from root (index 0) to
1357 * qgroup layer 7. Leaf node has no children.
1359 for (i = 0; i < hw->num_tx_sched_layers - 1; i++) {
1360 max_sibl = buf->layer_props[i + 1].max_sibl_grp_sz;
1361 hw->max_children[i] = le16_to_cpu(max_sibl);
1364 hw->layer_info = devm_kmemdup(ice_hw_to_dev(hw), buf->layer_props,
1365 (hw->num_tx_sched_layers *
1366 sizeof(*hw->layer_info)),
1368 if (!hw->layer_info) {
1370 goto sched_query_out;
1374 devm_kfree(ice_hw_to_dev(hw), buf);
1379 * ice_sched_get_psm_clk_freq - determine the PSM clock frequency
1380 * @hw: pointer to the HW struct
1382 * Determine the PSM clock frequency and store in HW struct
1384 void ice_sched_get_psm_clk_freq(struct ice_hw *hw)
1388 val = rd32(hw, GLGEN_CLKSTAT_SRC);
1389 clk_src = (val & GLGEN_CLKSTAT_SRC_PSM_CLK_SRC_M) >>
1390 GLGEN_CLKSTAT_SRC_PSM_CLK_SRC_S;
1392 #define PSM_CLK_SRC_367_MHZ 0x0
1393 #define PSM_CLK_SRC_416_MHZ 0x1
1394 #define PSM_CLK_SRC_446_MHZ 0x2
1395 #define PSM_CLK_SRC_390_MHZ 0x3
1398 case PSM_CLK_SRC_367_MHZ:
1399 hw->psm_clk_freq = ICE_PSM_CLK_367MHZ_IN_HZ;
1401 case PSM_CLK_SRC_416_MHZ:
1402 hw->psm_clk_freq = ICE_PSM_CLK_416MHZ_IN_HZ;
1404 case PSM_CLK_SRC_446_MHZ:
1405 hw->psm_clk_freq = ICE_PSM_CLK_446MHZ_IN_HZ;
1407 case PSM_CLK_SRC_390_MHZ:
1408 hw->psm_clk_freq = ICE_PSM_CLK_390MHZ_IN_HZ;
1411 ice_debug(hw, ICE_DBG_SCHED, "PSM clk_src unexpected %u\n",
1413 /* fall back to a safe default */
1414 hw->psm_clk_freq = ICE_PSM_CLK_446MHZ_IN_HZ;
1419 * ice_sched_find_node_in_subtree - Find node in part of base node subtree
1420 * @hw: pointer to the HW struct
1421 * @base: pointer to the base node
1422 * @node: pointer to the node to search
1424 * This function checks whether a given node is part of the base node
1428 ice_sched_find_node_in_subtree(struct ice_hw *hw, struct ice_sched_node *base,
1429 struct ice_sched_node *node)
1433 for (i = 0; i < base->num_children; i++) {
1434 struct ice_sched_node *child = base->children[i];
1439 if (child->tx_sched_layer > node->tx_sched_layer)
1442 /* this recursion is intentional, and wouldn't
1443 * go more than 8 calls
1445 if (ice_sched_find_node_in_subtree(hw, child, node))
1452 * ice_sched_get_free_qgrp - Scan all queue group siblings and find a free node
1453 * @pi: port information structure
1454 * @vsi_node: software VSI handle
1455 * @qgrp_node: first queue group node identified for scanning
1456 * @owner: LAN or RDMA
1458 * This function retrieves a free LAN or RDMA queue group node by scanning
1459 * qgrp_node and its siblings for the queue group with the fewest number
1460 * of queues currently assigned.
1462 static struct ice_sched_node *
1463 ice_sched_get_free_qgrp(struct ice_port_info *pi,
1464 struct ice_sched_node *vsi_node,
1465 struct ice_sched_node *qgrp_node, u8 owner)
1467 struct ice_sched_node *min_qgrp;
1472 min_children = qgrp_node->num_children;
1475 min_qgrp = qgrp_node;
1476 /* scan all queue groups until find a node which has less than the
1477 * minimum number of children. This way all queue group nodes get
1478 * equal number of shares and active. The bandwidth will be equally
1479 * distributed across all queues.
1482 /* make sure the qgroup node is part of the VSI subtree */
1483 if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1484 if (qgrp_node->num_children < min_children &&
1485 qgrp_node->owner == owner) {
1486 /* replace the new min queue group node */
1487 min_qgrp = qgrp_node;
1488 min_children = min_qgrp->num_children;
1489 /* break if it has no children, */
1493 qgrp_node = qgrp_node->sibling;
1499 * ice_sched_get_free_qparent - Get a free LAN or RDMA queue group node
1500 * @pi: port information structure
1501 * @vsi_handle: software VSI handle
1502 * @tc: branch number
1503 * @owner: LAN or RDMA
1505 * This function retrieves a free LAN or RDMA queue group node
1507 struct ice_sched_node *
1508 ice_sched_get_free_qparent(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
1511 struct ice_sched_node *vsi_node, *qgrp_node;
1512 struct ice_vsi_ctx *vsi_ctx;
1516 qgrp_layer = ice_sched_get_qgrp_layer(pi->hw);
1517 max_children = pi->hw->max_children[qgrp_layer];
1519 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1522 vsi_node = vsi_ctx->sched.vsi_node[tc];
1523 /* validate invalid VSI ID */
1527 /* get the first queue group node from VSI sub-tree */
1528 qgrp_node = ice_sched_get_first_node(pi, vsi_node, qgrp_layer);
1530 /* make sure the qgroup node is part of the VSI subtree */
1531 if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1532 if (qgrp_node->num_children < max_children &&
1533 qgrp_node->owner == owner)
1535 qgrp_node = qgrp_node->sibling;
1538 /* Select the best queue group */
1539 return ice_sched_get_free_qgrp(pi, vsi_node, qgrp_node, owner);
1543 * ice_sched_get_vsi_node - Get a VSI node based on VSI ID
1544 * @pi: pointer to the port information structure
1545 * @tc_node: pointer to the TC node
1546 * @vsi_handle: software VSI handle
1548 * This function retrieves a VSI node for a given VSI ID from a given
1551 static struct ice_sched_node *
1552 ice_sched_get_vsi_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1555 struct ice_sched_node *node;
1558 vsi_layer = ice_sched_get_vsi_layer(pi->hw);
1559 node = ice_sched_get_first_node(pi, tc_node, vsi_layer);
1561 /* Check whether it already exists */
1563 if (node->vsi_handle == vsi_handle)
1565 node = node->sibling;
1572 * ice_sched_get_agg_node - Get an aggregator node based on aggregator ID
1573 * @pi: pointer to the port information structure
1574 * @tc_node: pointer to the TC node
1575 * @agg_id: aggregator ID
1577 * This function retrieves an aggregator node for a given aggregator ID from
1580 static struct ice_sched_node *
1581 ice_sched_get_agg_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1584 struct ice_sched_node *node;
1585 struct ice_hw *hw = pi->hw;
1590 agg_layer = ice_sched_get_agg_layer(hw);
1591 node = ice_sched_get_first_node(pi, tc_node, agg_layer);
1593 /* Check whether it already exists */
1595 if (node->agg_id == agg_id)
1597 node = node->sibling;
1604 * ice_sched_calc_vsi_child_nodes - calculate number of VSI child nodes
1605 * @hw: pointer to the HW struct
1606 * @num_qs: number of queues
1607 * @num_nodes: num nodes array
1609 * This function calculates the number of VSI child nodes based on the
1613 ice_sched_calc_vsi_child_nodes(struct ice_hw *hw, u16 num_qs, u16 *num_nodes)
1618 qgl = ice_sched_get_qgrp_layer(hw);
1619 vsil = ice_sched_get_vsi_layer(hw);
1621 /* calculate num nodes from queue group to VSI layer */
1622 for (i = qgl; i > vsil; i--) {
1623 /* round to the next integer if there is a remainder */
1624 num = DIV_ROUND_UP(num, hw->max_children[i]);
1626 /* need at least one node */
1627 num_nodes[i] = num ? num : 1;
1632 * ice_sched_add_vsi_child_nodes - add VSI child nodes to tree
1633 * @pi: port information structure
1634 * @vsi_handle: software VSI handle
1635 * @tc_node: pointer to the TC node
1636 * @num_nodes: pointer to the num nodes that needs to be added per layer
1637 * @owner: node owner (LAN or RDMA)
1639 * This function adds the VSI child nodes to tree. It gets called for
1640 * LAN and RDMA separately.
1643 ice_sched_add_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1644 struct ice_sched_node *tc_node, u16 *num_nodes,
1647 struct ice_sched_node *parent, *node;
1648 struct ice_hw *hw = pi->hw;
1649 u32 first_node_teid;
1653 qgl = ice_sched_get_qgrp_layer(hw);
1654 vsil = ice_sched_get_vsi_layer(hw);
1655 parent = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1656 for (i = vsil + 1; i <= qgl; i++) {
1662 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
1666 if (status || num_nodes[i] != num_added)
1669 /* The newly added node can be a new parent for the next
1673 parent = ice_sched_find_node_by_teid(tc_node,
1677 node->owner = owner;
1678 node = node->sibling;
1681 parent = parent->children[0];
1689 * ice_sched_calc_vsi_support_nodes - calculate number of VSI support nodes
1690 * @pi: pointer to the port info structure
1691 * @tc_node: pointer to TC node
1692 * @num_nodes: pointer to num nodes array
1694 * This function calculates the number of supported nodes needed to add this
1695 * VSI into Tx tree including the VSI, parent and intermediate nodes in below
1699 ice_sched_calc_vsi_support_nodes(struct ice_port_info *pi,
1700 struct ice_sched_node *tc_node, u16 *num_nodes)
1702 struct ice_sched_node *node;
1706 vsil = ice_sched_get_vsi_layer(pi->hw);
1707 for (i = vsil; i >= pi->hw->sw_entry_point_layer; i--)
1708 /* Add intermediate nodes if TC has no children and
1709 * need at least one node for VSI
1711 if (!tc_node->num_children || i == vsil) {
1714 /* If intermediate nodes are reached max children
1715 * then add a new one.
1717 node = ice_sched_get_first_node(pi, tc_node, (u8)i);
1718 /* scan all the siblings */
1720 if (node->num_children < pi->hw->max_children[i])
1722 node = node->sibling;
1725 /* tree has one intermediate node to add this new VSI.
1726 * So no need to calculate supported nodes for below
1731 /* all the nodes are full, allocate a new one */
1737 * ice_sched_add_vsi_support_nodes - add VSI supported nodes into Tx tree
1738 * @pi: port information structure
1739 * @vsi_handle: software VSI handle
1740 * @tc_node: pointer to TC node
1741 * @num_nodes: pointer to num nodes array
1743 * This function adds the VSI supported nodes into Tx tree including the
1744 * VSI, its parent and intermediate nodes in below layers
1747 ice_sched_add_vsi_support_nodes(struct ice_port_info *pi, u16 vsi_handle,
1748 struct ice_sched_node *tc_node, u16 *num_nodes)
1750 struct ice_sched_node *parent = tc_node;
1751 u32 first_node_teid;
1758 vsil = ice_sched_get_vsi_layer(pi->hw);
1759 for (i = pi->hw->sw_entry_point_layer; i <= vsil; i++) {
1762 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
1766 if (status || num_nodes[i] != num_added)
1769 /* The newly added node can be a new parent for the next
1773 parent = ice_sched_find_node_by_teid(tc_node,
1776 parent = parent->children[0];
1782 parent->vsi_handle = vsi_handle;
1789 * ice_sched_add_vsi_to_topo - add a new VSI into tree
1790 * @pi: port information structure
1791 * @vsi_handle: software VSI handle
1794 * This function adds a new VSI into scheduler tree
1797 ice_sched_add_vsi_to_topo(struct ice_port_info *pi, u16 vsi_handle, u8 tc)
1799 u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1800 struct ice_sched_node *tc_node;
1802 tc_node = ice_sched_get_tc_node(pi, tc);
1806 /* calculate number of supported nodes needed for this VSI */
1807 ice_sched_calc_vsi_support_nodes(pi, tc_node, num_nodes);
1809 /* add VSI supported nodes to TC subtree */
1810 return ice_sched_add_vsi_support_nodes(pi, vsi_handle, tc_node,
1815 * ice_sched_update_vsi_child_nodes - update VSI child nodes
1816 * @pi: port information structure
1817 * @vsi_handle: software VSI handle
1819 * @new_numqs: new number of max queues
1820 * @owner: owner of this subtree
1822 * This function updates the VSI child nodes based on the number of queues
1825 ice_sched_update_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1826 u8 tc, u16 new_numqs, u8 owner)
1828 u16 new_num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1829 struct ice_sched_node *vsi_node;
1830 struct ice_sched_node *tc_node;
1831 struct ice_vsi_ctx *vsi_ctx;
1832 struct ice_hw *hw = pi->hw;
1836 tc_node = ice_sched_get_tc_node(pi, tc);
1840 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1844 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1848 if (owner == ICE_SCHED_NODE_OWNER_LAN)
1849 prev_numqs = vsi_ctx->sched.max_lanq[tc];
1851 prev_numqs = vsi_ctx->sched.max_rdmaq[tc];
1852 /* num queues are not changed or less than the previous number */
1853 if (new_numqs <= prev_numqs)
1855 if (owner == ICE_SCHED_NODE_OWNER_LAN) {
1856 status = ice_alloc_lan_q_ctx(hw, vsi_handle, tc, new_numqs);
1860 status = ice_alloc_rdma_q_ctx(hw, vsi_handle, tc, new_numqs);
1866 ice_sched_calc_vsi_child_nodes(hw, new_numqs, new_num_nodes);
1867 /* Keep the max number of queue configuration all the time. Update the
1868 * tree only if number of queues > previous number of queues. This may
1869 * leave some extra nodes in the tree if number of queues < previous
1870 * number but that wouldn't harm anything. Removing those extra nodes
1871 * may complicate the code if those nodes are part of SRL or
1872 * individually rate limited.
1874 status = ice_sched_add_vsi_child_nodes(pi, vsi_handle, tc_node,
1875 new_num_nodes, owner);
1878 if (owner == ICE_SCHED_NODE_OWNER_LAN)
1879 vsi_ctx->sched.max_lanq[tc] = new_numqs;
1881 vsi_ctx->sched.max_rdmaq[tc] = new_numqs;
1887 * ice_sched_cfg_vsi - configure the new/existing VSI
1888 * @pi: port information structure
1889 * @vsi_handle: software VSI handle
1891 * @maxqs: max number of queues
1892 * @owner: LAN or RDMA
1893 * @enable: TC enabled or disabled
1895 * This function adds/updates VSI nodes based on the number of queues. If TC is
1896 * enabled and VSI is in suspended state then resume the VSI back. If TC is
1897 * disabled then suspend the VSI if it is not already.
1900 ice_sched_cfg_vsi(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 maxqs,
1901 u8 owner, bool enable)
1903 struct ice_sched_node *vsi_node, *tc_node;
1904 struct ice_vsi_ctx *vsi_ctx;
1905 struct ice_hw *hw = pi->hw;
1908 ice_debug(pi->hw, ICE_DBG_SCHED, "add/config VSI %d\n", vsi_handle);
1909 tc_node = ice_sched_get_tc_node(pi, tc);
1912 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1915 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1917 /* suspend the VSI if TC is not enabled */
1919 if (vsi_node && vsi_node->in_use) {
1920 u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1922 status = ice_sched_suspend_resume_elems(hw, 1, &teid,
1925 vsi_node->in_use = false;
1930 /* TC is enabled, if it is a new VSI then add it to the tree */
1932 status = ice_sched_add_vsi_to_topo(pi, vsi_handle, tc);
1936 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1940 vsi_ctx->sched.vsi_node[tc] = vsi_node;
1941 vsi_node->in_use = true;
1942 /* invalidate the max queues whenever VSI gets added first time
1943 * into the scheduler tree (boot or after reset). We need to
1944 * recreate the child nodes all the time in these cases.
1946 vsi_ctx->sched.max_lanq[tc] = 0;
1947 vsi_ctx->sched.max_rdmaq[tc] = 0;
1950 /* update the VSI child nodes */
1951 status = ice_sched_update_vsi_child_nodes(pi, vsi_handle, tc, maxqs,
1956 /* TC is enabled, resume the VSI if it is in the suspend state */
1957 if (!vsi_node->in_use) {
1958 u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1960 status = ice_sched_suspend_resume_elems(hw, 1, &teid, false);
1962 vsi_node->in_use = true;
1969 * ice_sched_rm_agg_vsi_info - remove aggregator related VSI info entry
1970 * @pi: port information structure
1971 * @vsi_handle: software VSI handle
1973 * This function removes single aggregator VSI info entry from
1976 static void ice_sched_rm_agg_vsi_info(struct ice_port_info *pi, u16 vsi_handle)
1978 struct ice_sched_agg_info *agg_info;
1979 struct ice_sched_agg_info *atmp;
1981 list_for_each_entry_safe(agg_info, atmp, &pi->hw->agg_list,
1983 struct ice_sched_agg_vsi_info *agg_vsi_info;
1984 struct ice_sched_agg_vsi_info *vtmp;
1986 list_for_each_entry_safe(agg_vsi_info, vtmp,
1987 &agg_info->agg_vsi_list, list_entry)
1988 if (agg_vsi_info->vsi_handle == vsi_handle) {
1989 list_del(&agg_vsi_info->list_entry);
1990 devm_kfree(ice_hw_to_dev(pi->hw),
1998 * ice_sched_is_leaf_node_present - check for a leaf node in the sub-tree
1999 * @node: pointer to the sub-tree node
2001 * This function checks for a leaf node presence in a given sub-tree node.
2003 static bool ice_sched_is_leaf_node_present(struct ice_sched_node *node)
2007 for (i = 0; i < node->num_children; i++)
2008 if (ice_sched_is_leaf_node_present(node->children[i]))
2010 /* check for a leaf node */
2011 return (node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF);
2015 * ice_sched_rm_vsi_cfg - remove the VSI and its children nodes
2016 * @pi: port information structure
2017 * @vsi_handle: software VSI handle
2018 * @owner: LAN or RDMA
2020 * This function removes the VSI and its LAN or RDMA children nodes from the
2024 ice_sched_rm_vsi_cfg(struct ice_port_info *pi, u16 vsi_handle, u8 owner)
2026 struct ice_vsi_ctx *vsi_ctx;
2027 int status = -EINVAL;
2030 ice_debug(pi->hw, ICE_DBG_SCHED, "removing VSI %d\n", vsi_handle);
2031 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2033 mutex_lock(&pi->sched_lock);
2034 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
2036 goto exit_sched_rm_vsi_cfg;
2038 ice_for_each_traffic_class(i) {
2039 struct ice_sched_node *vsi_node, *tc_node;
2042 tc_node = ice_sched_get_tc_node(pi, i);
2046 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2050 if (ice_sched_is_leaf_node_present(vsi_node)) {
2051 ice_debug(pi->hw, ICE_DBG_SCHED, "VSI has leaf nodes in TC %d\n", i);
2053 goto exit_sched_rm_vsi_cfg;
2055 while (j < vsi_node->num_children) {
2056 if (vsi_node->children[j]->owner == owner) {
2057 ice_free_sched_node(pi, vsi_node->children[j]);
2059 /* reset the counter again since the num
2060 * children will be updated after node removal
2067 /* remove the VSI if it has no children */
2068 if (!vsi_node->num_children) {
2069 ice_free_sched_node(pi, vsi_node);
2070 vsi_ctx->sched.vsi_node[i] = NULL;
2072 /* clean up aggregator related VSI info if any */
2073 ice_sched_rm_agg_vsi_info(pi, vsi_handle);
2075 if (owner == ICE_SCHED_NODE_OWNER_LAN)
2076 vsi_ctx->sched.max_lanq[i] = 0;
2078 vsi_ctx->sched.max_rdmaq[i] = 0;
2082 exit_sched_rm_vsi_cfg:
2083 mutex_unlock(&pi->sched_lock);
2088 * ice_rm_vsi_lan_cfg - remove VSI and its LAN children nodes
2089 * @pi: port information structure
2090 * @vsi_handle: software VSI handle
2092 * This function clears the VSI and its LAN children nodes from scheduler tree
2095 int ice_rm_vsi_lan_cfg(struct ice_port_info *pi, u16 vsi_handle)
2097 return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_LAN);
2101 * ice_rm_vsi_rdma_cfg - remove VSI and its RDMA children nodes
2102 * @pi: port information structure
2103 * @vsi_handle: software VSI handle
2105 * This function clears the VSI and its RDMA children nodes from scheduler tree
2108 int ice_rm_vsi_rdma_cfg(struct ice_port_info *pi, u16 vsi_handle)
2110 return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_RDMA);
2114 * ice_get_agg_info - get the aggregator ID
2115 * @hw: pointer to the hardware structure
2116 * @agg_id: aggregator ID
2118 * This function validates aggregator ID. The function returns info if
2119 * aggregator ID is present in list otherwise it returns null.
2121 static struct ice_sched_agg_info *
2122 ice_get_agg_info(struct ice_hw *hw, u32 agg_id)
2124 struct ice_sched_agg_info *agg_info;
2126 list_for_each_entry(agg_info, &hw->agg_list, list_entry)
2127 if (agg_info->agg_id == agg_id)
2134 * ice_sched_get_free_vsi_parent - Find a free parent node in aggregator subtree
2135 * @hw: pointer to the HW struct
2136 * @node: pointer to a child node
2137 * @num_nodes: num nodes count array
2139 * This function walks through the aggregator subtree to find a free parent
2142 static struct ice_sched_node *
2143 ice_sched_get_free_vsi_parent(struct ice_hw *hw, struct ice_sched_node *node,
2146 u8 l = node->tx_sched_layer;
2149 vsil = ice_sched_get_vsi_layer(hw);
2151 /* Is it VSI parent layer ? */
2153 return (node->num_children < hw->max_children[l]) ? node : NULL;
2155 /* We have intermediate nodes. Let's walk through the subtree. If the
2156 * intermediate node has space to add a new node then clear the count
2158 if (node->num_children < hw->max_children[l])
2160 /* The below recursive call is intentional and wouldn't go more than
2161 * 2 or 3 iterations.
2164 for (i = 0; i < node->num_children; i++) {
2165 struct ice_sched_node *parent;
2167 parent = ice_sched_get_free_vsi_parent(hw, node->children[i],
2177 * ice_sched_update_parent - update the new parent in SW DB
2178 * @new_parent: pointer to a new parent node
2179 * @node: pointer to a child node
2181 * This function removes the child from the old parent and adds it to a new
2185 ice_sched_update_parent(struct ice_sched_node *new_parent,
2186 struct ice_sched_node *node)
2188 struct ice_sched_node *old_parent;
2191 old_parent = node->parent;
2193 /* update the old parent children */
2194 for (i = 0; i < old_parent->num_children; i++)
2195 if (old_parent->children[i] == node) {
2196 for (j = i + 1; j < old_parent->num_children; j++)
2197 old_parent->children[j - 1] =
2198 old_parent->children[j];
2199 old_parent->num_children--;
2203 /* now move the node to a new parent */
2204 new_parent->children[new_parent->num_children++] = node;
2205 node->parent = new_parent;
2206 node->info.parent_teid = new_parent->info.node_teid;
2210 * ice_sched_move_nodes - move child nodes to a given parent
2211 * @pi: port information structure
2212 * @parent: pointer to parent node
2213 * @num_items: number of child nodes to be moved
2214 * @list: pointer to child node teids
2216 * This function move the child nodes to a given parent.
2219 ice_sched_move_nodes(struct ice_port_info *pi, struct ice_sched_node *parent,
2220 u16 num_items, u32 *list)
2222 struct ice_aqc_move_elem *buf;
2223 struct ice_sched_node *node;
2224 u16 i, grps_movd = 0;
2231 if (!parent || !num_items)
2234 /* Does parent have enough space */
2235 if (parent->num_children + num_items >
2236 hw->max_children[parent->tx_sched_layer])
2239 buf_len = struct_size(buf, teid, 1);
2240 buf = kzalloc(buf_len, GFP_KERNEL);
2244 for (i = 0; i < num_items; i++) {
2245 node = ice_sched_find_node_by_teid(pi->root, list[i]);
2251 buf->hdr.src_parent_teid = node->info.parent_teid;
2252 buf->hdr.dest_parent_teid = parent->info.node_teid;
2253 buf->teid[0] = node->info.node_teid;
2254 buf->hdr.num_elems = cpu_to_le16(1);
2255 status = ice_aq_move_sched_elems(hw, 1, buf, buf_len,
2257 if (status && grps_movd != 1) {
2262 /* update the SW DB */
2263 ice_sched_update_parent(parent, node);
2272 * ice_sched_move_vsi_to_agg - move VSI to aggregator node
2273 * @pi: port information structure
2274 * @vsi_handle: software VSI handle
2275 * @agg_id: aggregator ID
2278 * This function moves a VSI to an aggregator node or its subtree.
2279 * Intermediate nodes may be created if required.
2282 ice_sched_move_vsi_to_agg(struct ice_port_info *pi, u16 vsi_handle, u32 agg_id,
2285 struct ice_sched_node *vsi_node, *agg_node, *tc_node, *parent;
2286 u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2287 u32 first_node_teid, vsi_teid;
2288 u16 num_nodes_added;
2292 tc_node = ice_sched_get_tc_node(pi, tc);
2296 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2300 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2304 /* Is this VSI already part of given aggregator? */
2305 if (ice_sched_find_node_in_subtree(pi->hw, agg_node, vsi_node))
2308 aggl = ice_sched_get_agg_layer(pi->hw);
2309 vsil = ice_sched_get_vsi_layer(pi->hw);
2311 /* set intermediate node count to 1 between aggregator and VSI layers */
2312 for (i = aggl + 1; i < vsil; i++)
2315 /* Check if the aggregator subtree has any free node to add the VSI */
2316 for (i = 0; i < agg_node->num_children; i++) {
2317 parent = ice_sched_get_free_vsi_parent(pi->hw,
2318 agg_node->children[i],
2326 for (i = aggl + 1; i < vsil; i++) {
2327 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2331 if (status || num_nodes[i] != num_nodes_added)
2334 /* The newly added node can be a new parent for the next
2337 if (num_nodes_added)
2338 parent = ice_sched_find_node_by_teid(tc_node,
2341 parent = parent->children[0];
2348 vsi_teid = le32_to_cpu(vsi_node->info.node_teid);
2349 return ice_sched_move_nodes(pi, parent, 1, &vsi_teid);
2353 * ice_move_all_vsi_to_dflt_agg - move all VSI(s) to default aggregator
2354 * @pi: port information structure
2355 * @agg_info: aggregator info
2356 * @tc: traffic class number
2357 * @rm_vsi_info: true or false
2359 * This function move all the VSI(s) to the default aggregator and delete
2360 * aggregator VSI info based on passed in boolean parameter rm_vsi_info. The
2361 * caller holds the scheduler lock.
2364 ice_move_all_vsi_to_dflt_agg(struct ice_port_info *pi,
2365 struct ice_sched_agg_info *agg_info, u8 tc,
2368 struct ice_sched_agg_vsi_info *agg_vsi_info;
2369 struct ice_sched_agg_vsi_info *tmp;
2372 list_for_each_entry_safe(agg_vsi_info, tmp, &agg_info->agg_vsi_list,
2374 u16 vsi_handle = agg_vsi_info->vsi_handle;
2376 /* Move VSI to default aggregator */
2377 if (!ice_is_tc_ena(agg_vsi_info->tc_bitmap[0], tc))
2380 status = ice_sched_move_vsi_to_agg(pi, vsi_handle,
2381 ICE_DFLT_AGG_ID, tc);
2385 clear_bit(tc, agg_vsi_info->tc_bitmap);
2386 if (rm_vsi_info && !agg_vsi_info->tc_bitmap[0]) {
2387 list_del(&agg_vsi_info->list_entry);
2388 devm_kfree(ice_hw_to_dev(pi->hw), agg_vsi_info);
2396 * ice_sched_is_agg_inuse - check whether the aggregator is in use or not
2397 * @pi: port information structure
2398 * @node: node pointer
2400 * This function checks whether the aggregator is attached with any VSI or not.
2403 ice_sched_is_agg_inuse(struct ice_port_info *pi, struct ice_sched_node *node)
2407 vsil = ice_sched_get_vsi_layer(pi->hw);
2408 if (node->tx_sched_layer < vsil - 1) {
2409 for (i = 0; i < node->num_children; i++)
2410 if (ice_sched_is_agg_inuse(pi, node->children[i]))
2414 return node->num_children ? true : false;
2419 * ice_sched_rm_agg_cfg - remove the aggregator node
2420 * @pi: port information structure
2421 * @agg_id: aggregator ID
2424 * This function removes the aggregator node and intermediate nodes if any
2428 ice_sched_rm_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2430 struct ice_sched_node *tc_node, *agg_node;
2431 struct ice_hw *hw = pi->hw;
2433 tc_node = ice_sched_get_tc_node(pi, tc);
2437 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2441 /* Can't remove the aggregator node if it has children */
2442 if (ice_sched_is_agg_inuse(pi, agg_node))
2445 /* need to remove the whole subtree if aggregator node is the
2448 while (agg_node->tx_sched_layer > hw->sw_entry_point_layer) {
2449 struct ice_sched_node *parent = agg_node->parent;
2454 if (parent->num_children > 1)
2460 ice_free_sched_node(pi, agg_node);
2465 * ice_rm_agg_cfg_tc - remove aggregator configuration for TC
2466 * @pi: port information structure
2467 * @agg_info: aggregator ID
2469 * @rm_vsi_info: bool value true or false
2471 * This function removes aggregator reference to VSI of given TC. It removes
2472 * the aggregator configuration completely for requested TC. The caller needs
2473 * to hold the scheduler lock.
2476 ice_rm_agg_cfg_tc(struct ice_port_info *pi, struct ice_sched_agg_info *agg_info,
2477 u8 tc, bool rm_vsi_info)
2481 /* If nothing to remove - return success */
2482 if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2483 goto exit_rm_agg_cfg_tc;
2485 status = ice_move_all_vsi_to_dflt_agg(pi, agg_info, tc, rm_vsi_info);
2487 goto exit_rm_agg_cfg_tc;
2489 /* Delete aggregator node(s) */
2490 status = ice_sched_rm_agg_cfg(pi, agg_info->agg_id, tc);
2492 goto exit_rm_agg_cfg_tc;
2494 clear_bit(tc, agg_info->tc_bitmap);
2500 * ice_save_agg_tc_bitmap - save aggregator TC bitmap
2501 * @pi: port information structure
2502 * @agg_id: aggregator ID
2503 * @tc_bitmap: 8 bits TC bitmap
2505 * Save aggregator TC bitmap. This function needs to be called with scheduler
2509 ice_save_agg_tc_bitmap(struct ice_port_info *pi, u32 agg_id,
2510 unsigned long *tc_bitmap)
2512 struct ice_sched_agg_info *agg_info;
2514 agg_info = ice_get_agg_info(pi->hw, agg_id);
2517 bitmap_copy(agg_info->replay_tc_bitmap, tc_bitmap,
2518 ICE_MAX_TRAFFIC_CLASS);
2523 * ice_sched_add_agg_cfg - create an aggregator node
2524 * @pi: port information structure
2525 * @agg_id: aggregator ID
2528 * This function creates an aggregator node and intermediate nodes if required
2532 ice_sched_add_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2534 struct ice_sched_node *parent, *agg_node, *tc_node;
2535 u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2536 struct ice_hw *hw = pi->hw;
2537 u32 first_node_teid;
2538 u16 num_nodes_added;
2542 tc_node = ice_sched_get_tc_node(pi, tc);
2546 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2547 /* Does Agg node already exist ? */
2551 aggl = ice_sched_get_agg_layer(hw);
2553 /* need one node in Agg layer */
2554 num_nodes[aggl] = 1;
2556 /* Check whether the intermediate nodes have space to add the
2557 * new aggregator. If they are full, then SW needs to allocate a new
2558 * intermediate node on those layers
2560 for (i = hw->sw_entry_point_layer; i < aggl; i++) {
2561 parent = ice_sched_get_first_node(pi, tc_node, i);
2563 /* scan all the siblings */
2565 if (parent->num_children < hw->max_children[i])
2567 parent = parent->sibling;
2570 /* all the nodes are full, reserve one for this layer */
2575 /* add the aggregator node */
2577 for (i = hw->sw_entry_point_layer; i <= aggl; i++) {
2581 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2585 if (status || num_nodes[i] != num_nodes_added)
2588 /* The newly added node can be a new parent for the next
2591 if (num_nodes_added) {
2592 parent = ice_sched_find_node_by_teid(tc_node,
2594 /* register aggregator ID with the aggregator node */
2595 if (parent && i == aggl)
2596 parent->agg_id = agg_id;
2598 parent = parent->children[0];
2606 * ice_sched_cfg_agg - configure aggregator node
2607 * @pi: port information structure
2608 * @agg_id: aggregator ID
2609 * @agg_type: aggregator type queue, VSI, or aggregator group
2610 * @tc_bitmap: bits TC bitmap
2612 * It registers a unique aggregator node into scheduler services. It
2613 * allows a user to register with a unique ID to track it's resources.
2614 * The aggregator type determines if this is a queue group, VSI group
2615 * or aggregator group. It then creates the aggregator node(s) for requested
2616 * TC(s) or removes an existing aggregator node including its configuration
2617 * if indicated via tc_bitmap. Call ice_rm_agg_cfg to release aggregator
2618 * resources and remove aggregator ID.
2619 * This function needs to be called with scheduler lock held.
2622 ice_sched_cfg_agg(struct ice_port_info *pi, u32 agg_id,
2623 enum ice_agg_type agg_type, unsigned long *tc_bitmap)
2625 struct ice_sched_agg_info *agg_info;
2626 struct ice_hw *hw = pi->hw;
2630 agg_info = ice_get_agg_info(hw, agg_id);
2632 /* Create new entry for new aggregator ID */
2633 agg_info = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*agg_info),
2638 agg_info->agg_id = agg_id;
2639 agg_info->agg_type = agg_type;
2640 agg_info->tc_bitmap[0] = 0;
2642 /* Initialize the aggregator VSI list head */
2643 INIT_LIST_HEAD(&agg_info->agg_vsi_list);
2645 /* Add new entry in aggregator list */
2646 list_add(&agg_info->list_entry, &hw->agg_list);
2648 /* Create aggregator node(s) for requested TC(s) */
2649 ice_for_each_traffic_class(tc) {
2650 if (!ice_is_tc_ena(*tc_bitmap, tc)) {
2651 /* Delete aggregator cfg TC if it exists previously */
2652 status = ice_rm_agg_cfg_tc(pi, agg_info, tc, false);
2658 /* Check if aggregator node for TC already exists */
2659 if (ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2662 /* Create new aggregator node for TC */
2663 status = ice_sched_add_agg_cfg(pi, agg_id, tc);
2667 /* Save aggregator node's TC information */
2668 set_bit(tc, agg_info->tc_bitmap);
2675 * ice_cfg_agg - config aggregator node
2676 * @pi: port information structure
2677 * @agg_id: aggregator ID
2678 * @agg_type: aggregator type queue, VSI, or aggregator group
2679 * @tc_bitmap: bits TC bitmap
2681 * This function configures aggregator node(s).
2684 ice_cfg_agg(struct ice_port_info *pi, u32 agg_id, enum ice_agg_type agg_type,
2687 unsigned long bitmap = tc_bitmap;
2690 mutex_lock(&pi->sched_lock);
2691 status = ice_sched_cfg_agg(pi, agg_id, agg_type, &bitmap);
2693 status = ice_save_agg_tc_bitmap(pi, agg_id, &bitmap);
2694 mutex_unlock(&pi->sched_lock);
2699 * ice_get_agg_vsi_info - get the aggregator ID
2700 * @agg_info: aggregator info
2701 * @vsi_handle: software VSI handle
2703 * The function returns aggregator VSI info based on VSI handle. This function
2704 * needs to be called with scheduler lock held.
2706 static struct ice_sched_agg_vsi_info *
2707 ice_get_agg_vsi_info(struct ice_sched_agg_info *agg_info, u16 vsi_handle)
2709 struct ice_sched_agg_vsi_info *agg_vsi_info;
2711 list_for_each_entry(agg_vsi_info, &agg_info->agg_vsi_list, list_entry)
2712 if (agg_vsi_info->vsi_handle == vsi_handle)
2713 return agg_vsi_info;
2719 * ice_get_vsi_agg_info - get the aggregator info of VSI
2720 * @hw: pointer to the hardware structure
2721 * @vsi_handle: Sw VSI handle
2723 * The function returns aggregator info of VSI represented via vsi_handle. The
2724 * VSI has in this case a different aggregator than the default one. This
2725 * function needs to be called with scheduler lock held.
2727 static struct ice_sched_agg_info *
2728 ice_get_vsi_agg_info(struct ice_hw *hw, u16 vsi_handle)
2730 struct ice_sched_agg_info *agg_info;
2732 list_for_each_entry(agg_info, &hw->agg_list, list_entry) {
2733 struct ice_sched_agg_vsi_info *agg_vsi_info;
2735 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2743 * ice_save_agg_vsi_tc_bitmap - save aggregator VSI TC bitmap
2744 * @pi: port information structure
2745 * @agg_id: aggregator ID
2746 * @vsi_handle: software VSI handle
2747 * @tc_bitmap: TC bitmap of enabled TC(s)
2749 * Save VSI to aggregator TC bitmap. This function needs to call with scheduler
2753 ice_save_agg_vsi_tc_bitmap(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2754 unsigned long *tc_bitmap)
2756 struct ice_sched_agg_vsi_info *agg_vsi_info;
2757 struct ice_sched_agg_info *agg_info;
2759 agg_info = ice_get_agg_info(pi->hw, agg_id);
2762 /* check if entry already exist */
2763 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2766 bitmap_copy(agg_vsi_info->replay_tc_bitmap, tc_bitmap,
2767 ICE_MAX_TRAFFIC_CLASS);
2772 * ice_sched_assoc_vsi_to_agg - associate/move VSI to new/default aggregator
2773 * @pi: port information structure
2774 * @agg_id: aggregator ID
2775 * @vsi_handle: software VSI handle
2776 * @tc_bitmap: TC bitmap of enabled TC(s)
2778 * This function moves VSI to a new or default aggregator node. If VSI is
2779 * already associated to the aggregator node then no operation is performed on
2780 * the tree. This function needs to be called with scheduler lock held.
2783 ice_sched_assoc_vsi_to_agg(struct ice_port_info *pi, u32 agg_id,
2784 u16 vsi_handle, unsigned long *tc_bitmap)
2786 struct ice_sched_agg_vsi_info *agg_vsi_info, *iter, *old_agg_vsi_info = NULL;
2787 struct ice_sched_agg_info *agg_info, *old_agg_info;
2788 struct ice_hw *hw = pi->hw;
2792 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2794 agg_info = ice_get_agg_info(hw, agg_id);
2797 /* If the VSI is already part of another aggregator then update
2800 old_agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
2801 if (old_agg_info && old_agg_info != agg_info) {
2802 struct ice_sched_agg_vsi_info *vtmp;
2804 list_for_each_entry_safe(iter, vtmp,
2805 &old_agg_info->agg_vsi_list,
2807 if (iter->vsi_handle == vsi_handle) {
2808 old_agg_vsi_info = iter;
2813 /* check if entry already exist */
2814 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2815 if (!agg_vsi_info) {
2816 /* Create new entry for VSI under aggregator list */
2817 agg_vsi_info = devm_kzalloc(ice_hw_to_dev(hw),
2818 sizeof(*agg_vsi_info), GFP_KERNEL);
2822 /* add VSI ID into the aggregator list */
2823 agg_vsi_info->vsi_handle = vsi_handle;
2824 list_add(&agg_vsi_info->list_entry, &agg_info->agg_vsi_list);
2826 /* Move VSI node to new aggregator node for requested TC(s) */
2827 ice_for_each_traffic_class(tc) {
2828 if (!ice_is_tc_ena(*tc_bitmap, tc))
2831 /* Move VSI to new aggregator */
2832 status = ice_sched_move_vsi_to_agg(pi, vsi_handle, agg_id, tc);
2836 set_bit(tc, agg_vsi_info->tc_bitmap);
2837 if (old_agg_vsi_info)
2838 clear_bit(tc, old_agg_vsi_info->tc_bitmap);
2840 if (old_agg_vsi_info && !old_agg_vsi_info->tc_bitmap[0]) {
2841 list_del(&old_agg_vsi_info->list_entry);
2842 devm_kfree(ice_hw_to_dev(pi->hw), old_agg_vsi_info);
2848 * ice_sched_rm_unused_rl_prof - remove unused RL profile
2849 * @pi: port information structure
2851 * This function removes unused rate limit profiles from the HW and
2852 * SW DB. The caller needs to hold scheduler lock.
2854 static void ice_sched_rm_unused_rl_prof(struct ice_port_info *pi)
2858 for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
2859 struct ice_aqc_rl_profile_info *rl_prof_elem;
2860 struct ice_aqc_rl_profile_info *rl_prof_tmp;
2862 list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
2863 &pi->rl_prof_list[ln], list_entry) {
2864 if (!ice_sched_del_rl_profile(pi->hw, rl_prof_elem))
2865 ice_debug(pi->hw, ICE_DBG_SCHED, "Removed rl profile\n");
2871 * ice_sched_update_elem - update element
2872 * @hw: pointer to the HW struct
2873 * @node: pointer to node
2874 * @info: node info to update
2876 * Update the HW DB, and local SW DB of node. Update the scheduling
2877 * parameters of node from argument info data buffer (Info->data buf) and
2878 * returns success or error on config sched element failure. The caller
2879 * needs to hold scheduler lock.
2882 ice_sched_update_elem(struct ice_hw *hw, struct ice_sched_node *node,
2883 struct ice_aqc_txsched_elem_data *info)
2885 struct ice_aqc_txsched_elem_data buf;
2891 /* Parent TEID is reserved field in this aq call */
2892 buf.parent_teid = 0;
2893 /* Element type is reserved field in this aq call */
2894 buf.data.elem_type = 0;
2895 /* Flags is reserved field in this aq call */
2899 /* Configure element node */
2900 status = ice_aq_cfg_sched_elems(hw, num_elems, &buf, sizeof(buf),
2902 if (status || elem_cfgd != num_elems) {
2903 ice_debug(hw, ICE_DBG_SCHED, "Config sched elem error\n");
2907 /* Config success case */
2908 /* Now update local SW DB */
2909 /* Only copy the data portion of info buffer */
2910 node->info.data = info->data;
2915 * ice_sched_cfg_node_bw_alloc - configure node BW weight/alloc params
2916 * @hw: pointer to the HW struct
2917 * @node: sched node to configure
2918 * @rl_type: rate limit type CIR, EIR, or shared
2919 * @bw_alloc: BW weight/allocation
2921 * This function configures node element's BW allocation.
2924 ice_sched_cfg_node_bw_alloc(struct ice_hw *hw, struct ice_sched_node *node,
2925 enum ice_rl_type rl_type, u16 bw_alloc)
2927 struct ice_aqc_txsched_elem_data buf;
2928 struct ice_aqc_txsched_elem *data;
2932 if (rl_type == ICE_MIN_BW) {
2933 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
2934 data->cir_bw.bw_alloc = cpu_to_le16(bw_alloc);
2935 } else if (rl_type == ICE_MAX_BW) {
2936 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
2937 data->eir_bw.bw_alloc = cpu_to_le16(bw_alloc);
2942 /* Configure element */
2943 return ice_sched_update_elem(hw, node, &buf);
2947 * ice_move_vsi_to_agg - moves VSI to new or default aggregator
2948 * @pi: port information structure
2949 * @agg_id: aggregator ID
2950 * @vsi_handle: software VSI handle
2951 * @tc_bitmap: TC bitmap of enabled TC(s)
2953 * Move or associate VSI to a new or default aggregator node.
2956 ice_move_vsi_to_agg(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2959 unsigned long bitmap = tc_bitmap;
2962 mutex_lock(&pi->sched_lock);
2963 status = ice_sched_assoc_vsi_to_agg(pi, agg_id, vsi_handle,
2964 (unsigned long *)&bitmap);
2966 status = ice_save_agg_vsi_tc_bitmap(pi, agg_id, vsi_handle,
2967 (unsigned long *)&bitmap);
2968 mutex_unlock(&pi->sched_lock);
2973 * ice_set_clear_cir_bw - set or clear CIR BW
2974 * @bw_t_info: bandwidth type information structure
2975 * @bw: bandwidth in Kbps - Kilo bits per sec
2977 * Save or clear CIR bandwidth (BW) in the passed param bw_t_info.
2979 static void ice_set_clear_cir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
2981 if (bw == ICE_SCHED_DFLT_BW) {
2982 clear_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
2983 bw_t_info->cir_bw.bw = 0;
2985 /* Save type of BW information */
2986 set_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
2987 bw_t_info->cir_bw.bw = bw;
2992 * ice_set_clear_eir_bw - set or clear EIR BW
2993 * @bw_t_info: bandwidth type information structure
2994 * @bw: bandwidth in Kbps - Kilo bits per sec
2996 * Save or clear EIR bandwidth (BW) in the passed param bw_t_info.
2998 static void ice_set_clear_eir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3000 if (bw == ICE_SCHED_DFLT_BW) {
3001 clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3002 bw_t_info->eir_bw.bw = 0;
3004 /* EIR BW and Shared BW profiles are mutually exclusive and
3005 * hence only one of them may be set for any given element.
3006 * First clear earlier saved shared BW information.
3008 clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3009 bw_t_info->shared_bw = 0;
3010 /* save EIR BW information */
3011 set_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3012 bw_t_info->eir_bw.bw = bw;
3017 * ice_set_clear_shared_bw - set or clear shared BW
3018 * @bw_t_info: bandwidth type information structure
3019 * @bw: bandwidth in Kbps - Kilo bits per sec
3021 * Save or clear shared bandwidth (BW) in the passed param bw_t_info.
3023 static void ice_set_clear_shared_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3025 if (bw == ICE_SCHED_DFLT_BW) {
3026 clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3027 bw_t_info->shared_bw = 0;
3029 /* EIR BW and Shared BW profiles are mutually exclusive and
3030 * hence only one of them may be set for any given element.
3031 * First clear earlier saved EIR BW information.
3033 clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3034 bw_t_info->eir_bw.bw = 0;
3035 /* save shared BW information */
3036 set_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3037 bw_t_info->shared_bw = bw;
3042 * ice_sched_save_vsi_bw - save VSI node's BW information
3043 * @pi: port information structure
3044 * @vsi_handle: sw VSI handle
3045 * @tc: traffic class
3046 * @rl_type: rate limit type min, max, or shared
3047 * @bw: bandwidth in Kbps - Kilo bits per sec
3049 * Save BW information of VSI type node for post replay use.
3052 ice_sched_save_vsi_bw(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3053 enum ice_rl_type rl_type, u32 bw)
3055 struct ice_vsi_ctx *vsi_ctx;
3057 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3059 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3064 ice_set_clear_cir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3067 ice_set_clear_eir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3070 ice_set_clear_shared_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3079 * ice_sched_calc_wakeup - calculate RL profile wakeup parameter
3080 * @hw: pointer to the HW struct
3081 * @bw: bandwidth in Kbps
3083 * This function calculates the wakeup parameter of RL profile.
3085 static u16 ice_sched_calc_wakeup(struct ice_hw *hw, s32 bw)
3087 s64 bytes_per_sec, wakeup_int, wakeup_a, wakeup_b, wakeup_f;
3091 /* Get the wakeup integer value */
3092 bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
3093 wakeup_int = div64_long(hw->psm_clk_freq, bytes_per_sec);
3094 if (wakeup_int > 63) {
3095 wakeup = (u16)((1 << 15) | wakeup_int);
3097 /* Calculate fraction value up to 4 decimals
3098 * Convert Integer value to a constant multiplier
3100 wakeup_b = (s64)ICE_RL_PROF_MULTIPLIER * wakeup_int;
3101 wakeup_a = div64_long((s64)ICE_RL_PROF_MULTIPLIER *
3102 hw->psm_clk_freq, bytes_per_sec);
3104 /* Get Fraction value */
3105 wakeup_f = wakeup_a - wakeup_b;
3107 /* Round up the Fractional value via Ceil(Fractional value) */
3108 if (wakeup_f > div64_long(ICE_RL_PROF_MULTIPLIER, 2))
3111 wakeup_f_int = (s32)div64_long(wakeup_f * ICE_RL_PROF_FRACTION,
3112 ICE_RL_PROF_MULTIPLIER);
3113 wakeup |= (u16)(wakeup_int << 9);
3114 wakeup |= (u16)(0x1ff & wakeup_f_int);
3121 * ice_sched_bw_to_rl_profile - convert BW to profile parameters
3122 * @hw: pointer to the HW struct
3123 * @bw: bandwidth in Kbps
3124 * @profile: profile parameters to return
3126 * This function converts the BW to profile structure format.
3129 ice_sched_bw_to_rl_profile(struct ice_hw *hw, u32 bw,
3130 struct ice_aqc_rl_profile_elem *profile)
3132 s64 bytes_per_sec, ts_rate, mv_tmp;
3133 int status = -EINVAL;
3139 /* Bw settings range is from 0.5Mb/sec to 100Gb/sec */
3140 if (bw < ICE_SCHED_MIN_BW || bw > ICE_SCHED_MAX_BW)
3143 /* Bytes per second from Kbps */
3144 bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
3146 /* encode is 6 bits but really useful are 5 bits */
3147 for (i = 0; i < 64; i++) {
3148 u64 pow_result = BIT_ULL(i);
3150 ts_rate = div64_long((s64)hw->psm_clk_freq,
3151 pow_result * ICE_RL_PROF_TS_MULTIPLIER);
3155 /* Multiplier value */
3156 mv_tmp = div64_long(bytes_per_sec * ICE_RL_PROF_MULTIPLIER,
3159 /* Round to the nearest ICE_RL_PROF_MULTIPLIER */
3160 mv = round_up_64bit(mv_tmp, ICE_RL_PROF_MULTIPLIER);
3162 /* First multiplier value greater than the given
3165 if (mv > ICE_RL_PROF_ACCURACY_BYTES) {
3174 wm = ice_sched_calc_wakeup(hw, bw);
3175 profile->rl_multiply = cpu_to_le16(mv);
3176 profile->wake_up_calc = cpu_to_le16(wm);
3177 profile->rl_encode = cpu_to_le16(encode);
3187 * ice_sched_add_rl_profile - add RL profile
3188 * @pi: port information structure
3189 * @rl_type: type of rate limit BW - min, max, or shared
3190 * @bw: bandwidth in Kbps - Kilo bits per sec
3191 * @layer_num: specifies in which layer to create profile
3193 * This function first checks the existing list for corresponding BW
3194 * parameter. If it exists, it returns the associated profile otherwise
3195 * it creates a new rate limit profile for requested BW, and adds it to
3196 * the HW DB and local list. It returns the new profile or null on error.
3197 * The caller needs to hold the scheduler lock.
3199 static struct ice_aqc_rl_profile_info *
3200 ice_sched_add_rl_profile(struct ice_port_info *pi,
3201 enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3203 struct ice_aqc_rl_profile_info *rl_prof_elem;
3204 u16 profiles_added = 0, num_profiles = 1;
3205 struct ice_aqc_rl_profile_elem *buf;
3210 if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
3214 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3217 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3220 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3229 list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
3231 if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
3232 profile_type && rl_prof_elem->bw == bw)
3233 /* Return existing profile ID info */
3234 return rl_prof_elem;
3236 /* Create new profile ID */
3237 rl_prof_elem = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*rl_prof_elem),
3243 status = ice_sched_bw_to_rl_profile(hw, bw, &rl_prof_elem->profile);
3245 goto exit_add_rl_prof;
3247 rl_prof_elem->bw = bw;
3248 /* layer_num is zero relative, and fw expects level from 1 to 9 */
3249 rl_prof_elem->profile.level = layer_num + 1;
3250 rl_prof_elem->profile.flags = profile_type;
3251 rl_prof_elem->profile.max_burst_size = cpu_to_le16(hw->max_burst_size);
3253 /* Create new entry in HW DB */
3254 buf = &rl_prof_elem->profile;
3255 status = ice_aq_add_rl_profile(hw, num_profiles, buf, sizeof(*buf),
3256 &profiles_added, NULL);
3257 if (status || profiles_added != num_profiles)
3258 goto exit_add_rl_prof;
3260 /* Good entry - add in the list */
3261 rl_prof_elem->prof_id_ref = 0;
3262 list_add(&rl_prof_elem->list_entry, &pi->rl_prof_list[layer_num]);
3263 return rl_prof_elem;
3266 devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
3271 * ice_sched_cfg_node_bw_lmt - configure node sched params
3272 * @hw: pointer to the HW struct
3273 * @node: sched node to configure
3274 * @rl_type: rate limit type CIR, EIR, or shared
3275 * @rl_prof_id: rate limit profile ID
3277 * This function configures node element's BW limit.
3280 ice_sched_cfg_node_bw_lmt(struct ice_hw *hw, struct ice_sched_node *node,
3281 enum ice_rl_type rl_type, u16 rl_prof_id)
3283 struct ice_aqc_txsched_elem_data buf;
3284 struct ice_aqc_txsched_elem *data;
3290 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
3291 data->cir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
3294 /* EIR BW and Shared BW profiles are mutually exclusive and
3295 * hence only one of them may be set for any given element
3297 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3299 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3300 data->eir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
3303 /* Check for removing shared BW */
3304 if (rl_prof_id == ICE_SCHED_NO_SHARED_RL_PROF_ID) {
3305 /* remove shared profile */
3306 data->valid_sections &= ~ICE_AQC_ELEM_VALID_SHARED;
3307 data->srl_id = 0; /* clear SRL field */
3309 /* enable back EIR to default profile */
3310 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3311 data->eir_bw.bw_profile_idx =
3312 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
3315 /* EIR BW and Shared BW profiles are mutually exclusive and
3316 * hence only one of them may be set for any given element
3318 if ((data->valid_sections & ICE_AQC_ELEM_VALID_EIR) &&
3319 (le16_to_cpu(data->eir_bw.bw_profile_idx) !=
3320 ICE_SCHED_DFLT_RL_PROF_ID))
3322 /* EIR BW is set to default, disable it */
3323 data->valid_sections &= ~ICE_AQC_ELEM_VALID_EIR;
3324 /* Okay to enable shared BW now */
3325 data->valid_sections |= ICE_AQC_ELEM_VALID_SHARED;
3326 data->srl_id = cpu_to_le16(rl_prof_id);
3329 /* Unknown rate limit type */
3333 /* Configure element */
3334 return ice_sched_update_elem(hw, node, &buf);
3338 * ice_sched_get_node_rl_prof_id - get node's rate limit profile ID
3340 * @rl_type: rate limit type
3342 * If existing profile matches, it returns the corresponding rate
3343 * limit profile ID, otherwise it returns an invalid ID as error.
3346 ice_sched_get_node_rl_prof_id(struct ice_sched_node *node,
3347 enum ice_rl_type rl_type)
3349 u16 rl_prof_id = ICE_SCHED_INVAL_PROF_ID;
3350 struct ice_aqc_txsched_elem *data;
3352 data = &node->info.data;
3355 if (data->valid_sections & ICE_AQC_ELEM_VALID_CIR)
3356 rl_prof_id = le16_to_cpu(data->cir_bw.bw_profile_idx);
3359 if (data->valid_sections & ICE_AQC_ELEM_VALID_EIR)
3360 rl_prof_id = le16_to_cpu(data->eir_bw.bw_profile_idx);
3363 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3364 rl_prof_id = le16_to_cpu(data->srl_id);
3374 * ice_sched_get_rl_prof_layer - selects rate limit profile creation layer
3375 * @pi: port information structure
3376 * @rl_type: type of rate limit BW - min, max, or shared
3377 * @layer_index: layer index
3379 * This function returns requested profile creation layer.
3382 ice_sched_get_rl_prof_layer(struct ice_port_info *pi, enum ice_rl_type rl_type,
3385 struct ice_hw *hw = pi->hw;
3387 if (layer_index >= hw->num_tx_sched_layers)
3388 return ICE_SCHED_INVAL_LAYER_NUM;
3391 if (hw->layer_info[layer_index].max_cir_rl_profiles)
3395 if (hw->layer_info[layer_index].max_eir_rl_profiles)
3399 /* if current layer doesn't support SRL profile creation
3400 * then try a layer up or down.
3402 if (hw->layer_info[layer_index].max_srl_profiles)
3404 else if (layer_index < hw->num_tx_sched_layers - 1 &&
3405 hw->layer_info[layer_index + 1].max_srl_profiles)
3406 return layer_index + 1;
3407 else if (layer_index > 0 &&
3408 hw->layer_info[layer_index - 1].max_srl_profiles)
3409 return layer_index - 1;
3414 return ICE_SCHED_INVAL_LAYER_NUM;
3418 * ice_sched_get_srl_node - get shared rate limit node
3420 * @srl_layer: shared rate limit layer
3422 * This function returns SRL node to be used for shared rate limit purpose.
3423 * The caller needs to hold scheduler lock.
3425 static struct ice_sched_node *
3426 ice_sched_get_srl_node(struct ice_sched_node *node, u8 srl_layer)
3428 if (srl_layer > node->tx_sched_layer)
3429 return node->children[0];
3430 else if (srl_layer < node->tx_sched_layer)
3431 /* Node can't be created without a parent. It will always
3432 * have a valid parent except root node.
3434 return node->parent;
3440 * ice_sched_rm_rl_profile - remove RL profile ID
3441 * @pi: port information structure
3442 * @layer_num: layer number where profiles are saved
3443 * @profile_type: profile type like EIR, CIR, or SRL
3444 * @profile_id: profile ID to remove
3446 * This function removes rate limit profile from layer 'layer_num' of type
3447 * 'profile_type' and profile ID as 'profile_id'. The caller needs to hold
3451 ice_sched_rm_rl_profile(struct ice_port_info *pi, u8 layer_num, u8 profile_type,
3454 struct ice_aqc_rl_profile_info *rl_prof_elem;
3457 if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
3459 /* Check the existing list for RL profile */
3460 list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
3462 if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
3464 le16_to_cpu(rl_prof_elem->profile.profile_id) ==
3466 if (rl_prof_elem->prof_id_ref)
3467 rl_prof_elem->prof_id_ref--;
3469 /* Remove old profile ID from database */
3470 status = ice_sched_del_rl_profile(pi->hw, rl_prof_elem);
3471 if (status && status != -EBUSY)
3472 ice_debug(pi->hw, ICE_DBG_SCHED, "Remove rl profile failed\n");
3475 if (status == -EBUSY)
3481 * ice_sched_set_node_bw_dflt - set node's bandwidth limit to default
3482 * @pi: port information structure
3483 * @node: pointer to node structure
3484 * @rl_type: rate limit type min, max, or shared
3485 * @layer_num: layer number where RL profiles are saved
3487 * This function configures node element's BW rate limit profile ID of
3488 * type CIR, EIR, or SRL to default. This function needs to be called
3489 * with the scheduler lock held.
3492 ice_sched_set_node_bw_dflt(struct ice_port_info *pi,
3493 struct ice_sched_node *node,
3494 enum ice_rl_type rl_type, u8 layer_num)
3505 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3506 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
3509 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3510 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
3513 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3514 /* No SRL is configured for default case */
3515 rl_prof_id = ICE_SCHED_NO_SHARED_RL_PROF_ID;
3520 /* Save existing RL prof ID for later clean up */
3521 old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
3522 /* Configure BW scheduling parameters */
3523 status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
3527 /* Remove stale RL profile ID */
3528 if (old_id == ICE_SCHED_DFLT_RL_PROF_ID ||
3529 old_id == ICE_SCHED_INVAL_PROF_ID)
3532 return ice_sched_rm_rl_profile(pi, layer_num, profile_type, old_id);
3536 * ice_sched_set_eir_srl_excl - set EIR/SRL exclusiveness
3537 * @pi: port information structure
3538 * @node: pointer to node structure
3539 * @layer_num: layer number where rate limit profiles are saved
3540 * @rl_type: rate limit type min, max, or shared
3541 * @bw: bandwidth value
3543 * This function prepares node element's bandwidth to SRL or EIR exclusively.
3544 * EIR BW and Shared BW profiles are mutually exclusive and hence only one of
3545 * them may be set for any given element. This function needs to be called
3546 * with the scheduler lock held.
3549 ice_sched_set_eir_srl_excl(struct ice_port_info *pi,
3550 struct ice_sched_node *node,
3551 u8 layer_num, enum ice_rl_type rl_type, u32 bw)
3553 if (rl_type == ICE_SHARED_BW) {
3554 /* SRL node passed in this case, it may be different node */
3555 if (bw == ICE_SCHED_DFLT_BW)
3556 /* SRL being removed, ice_sched_cfg_node_bw_lmt()
3557 * enables EIR to default. EIR is not set in this
3558 * case, so no additional action is required.
3562 /* SRL being configured, set EIR to default here.
3563 * ice_sched_cfg_node_bw_lmt() disables EIR when it
3566 return ice_sched_set_node_bw_dflt(pi, node, ICE_MAX_BW,
3568 } else if (rl_type == ICE_MAX_BW &&
3569 node->info.data.valid_sections & ICE_AQC_ELEM_VALID_SHARED) {
3570 /* Remove Shared profile. Set default shared BW call
3571 * removes shared profile for a node.
3573 return ice_sched_set_node_bw_dflt(pi, node,
3581 * ice_sched_set_node_bw - set node's bandwidth
3582 * @pi: port information structure
3584 * @rl_type: rate limit type min, max, or shared
3585 * @bw: bandwidth in Kbps - Kilo bits per sec
3586 * @layer_num: layer number
3588 * This function adds new profile corresponding to requested BW, configures
3589 * node's RL profile ID of type CIR, EIR, or SRL, and removes old profile
3590 * ID from local database. The caller needs to hold scheduler lock.
3593 ice_sched_set_node_bw(struct ice_port_info *pi, struct ice_sched_node *node,
3594 enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3596 struct ice_aqc_rl_profile_info *rl_prof_info;
3597 struct ice_hw *hw = pi->hw;
3598 u16 old_id, rl_prof_id;
3599 int status = -EINVAL;
3601 rl_prof_info = ice_sched_add_rl_profile(pi, rl_type, bw, layer_num);
3605 rl_prof_id = le16_to_cpu(rl_prof_info->profile.profile_id);
3607 /* Save existing RL prof ID for later clean up */
3608 old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
3609 /* Configure BW scheduling parameters */
3610 status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
3614 /* New changes has been applied */
3615 /* Increment the profile ID reference count */
3616 rl_prof_info->prof_id_ref++;
3618 /* Check for old ID removal */
3619 if ((old_id == ICE_SCHED_DFLT_RL_PROF_ID && rl_type != ICE_SHARED_BW) ||
3620 old_id == ICE_SCHED_INVAL_PROF_ID || old_id == rl_prof_id)
3623 return ice_sched_rm_rl_profile(pi, layer_num,
3624 rl_prof_info->profile.flags &
3625 ICE_AQC_RL_PROFILE_TYPE_M, old_id);
3629 * ice_sched_set_node_priority - set node's priority
3630 * @pi: port information structure
3632 * @priority: number 0-7 representing priority among siblings
3634 * This function sets priority of a node among it's siblings.
3637 ice_sched_set_node_priority(struct ice_port_info *pi, struct ice_sched_node *node,
3640 struct ice_aqc_txsched_elem_data buf;
3641 struct ice_aqc_txsched_elem *data;
3646 data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
3647 data->generic |= FIELD_PREP(ICE_AQC_ELEM_GENERIC_PRIO_M, priority);
3649 return ice_sched_update_elem(pi->hw, node, &buf);
3653 * ice_sched_set_node_weight - set node's weight
3654 * @pi: port information structure
3656 * @weight: number 1-200 representing weight for WFQ
3658 * This function sets weight of the node for WFQ algorithm.
3661 ice_sched_set_node_weight(struct ice_port_info *pi, struct ice_sched_node *node, u16 weight)
3663 struct ice_aqc_txsched_elem_data buf;
3664 struct ice_aqc_txsched_elem *data;
3669 data->valid_sections = ICE_AQC_ELEM_VALID_CIR | ICE_AQC_ELEM_VALID_EIR |
3670 ICE_AQC_ELEM_VALID_GENERIC;
3671 data->cir_bw.bw_alloc = cpu_to_le16(weight);
3672 data->eir_bw.bw_alloc = cpu_to_le16(weight);
3674 data->generic |= FIELD_PREP(ICE_AQC_ELEM_GENERIC_SP_M, 0x0);
3676 return ice_sched_update_elem(pi->hw, node, &buf);
3680 * ice_sched_set_node_bw_lmt - set node's BW limit
3681 * @pi: port information structure
3683 * @rl_type: rate limit type min, max, or shared
3684 * @bw: bandwidth in Kbps - Kilo bits per sec
3686 * It updates node's BW limit parameters like BW RL profile ID of type CIR,
3687 * EIR, or SRL. The caller needs to hold scheduler lock.
3690 ice_sched_set_node_bw_lmt(struct ice_port_info *pi, struct ice_sched_node *node,
3691 enum ice_rl_type rl_type, u32 bw)
3693 struct ice_sched_node *cfg_node = node;
3702 /* Remove unused RL profile IDs from HW and SW DB */
3703 ice_sched_rm_unused_rl_prof(pi);
3704 layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
3705 node->tx_sched_layer);
3706 if (layer_num >= hw->num_tx_sched_layers)
3709 if (rl_type == ICE_SHARED_BW) {
3710 /* SRL node may be different */
3711 cfg_node = ice_sched_get_srl_node(node, layer_num);
3715 /* EIR BW and Shared BW profiles are mutually exclusive and
3716 * hence only one of them may be set for any given element
3718 status = ice_sched_set_eir_srl_excl(pi, cfg_node, layer_num, rl_type,
3722 if (bw == ICE_SCHED_DFLT_BW)
3723 return ice_sched_set_node_bw_dflt(pi, cfg_node, rl_type,
3725 return ice_sched_set_node_bw(pi, cfg_node, rl_type, bw, layer_num);
3729 * ice_sched_set_node_bw_dflt_lmt - set node's BW limit to default
3730 * @pi: port information structure
3731 * @node: pointer to node structure
3732 * @rl_type: rate limit type min, max, or shared
3734 * This function configures node element's BW rate limit profile ID of
3735 * type CIR, EIR, or SRL to default. This function needs to be called
3736 * with the scheduler lock held.
3739 ice_sched_set_node_bw_dflt_lmt(struct ice_port_info *pi,
3740 struct ice_sched_node *node,
3741 enum ice_rl_type rl_type)
3743 return ice_sched_set_node_bw_lmt(pi, node, rl_type,
3748 * ice_sched_validate_srl_node - Check node for SRL applicability
3749 * @node: sched node to configure
3750 * @sel_layer: selected SRL layer
3752 * This function checks if the SRL can be applied to a selected layer node on
3753 * behalf of the requested node (first argument). This function needs to be
3754 * called with scheduler lock held.
3757 ice_sched_validate_srl_node(struct ice_sched_node *node, u8 sel_layer)
3759 /* SRL profiles are not available on all layers. Check if the
3760 * SRL profile can be applied to a node above or below the
3761 * requested node. SRL configuration is possible only if the
3762 * selected layer's node has single child.
3764 if (sel_layer == node->tx_sched_layer ||
3765 ((sel_layer == node->tx_sched_layer + 1) &&
3766 node->num_children == 1) ||
3767 ((sel_layer == node->tx_sched_layer - 1) &&
3768 (node->parent && node->parent->num_children == 1)))
3775 * ice_sched_save_q_bw - save queue node's BW information
3776 * @q_ctx: queue context structure
3777 * @rl_type: rate limit type min, max, or shared
3778 * @bw: bandwidth in Kbps - Kilo bits per sec
3780 * Save BW information of queue type node for post replay use.
3783 ice_sched_save_q_bw(struct ice_q_ctx *q_ctx, enum ice_rl_type rl_type, u32 bw)
3787 ice_set_clear_cir_bw(&q_ctx->bw_t_info, bw);
3790 ice_set_clear_eir_bw(&q_ctx->bw_t_info, bw);
3793 ice_set_clear_shared_bw(&q_ctx->bw_t_info, bw);
3802 * ice_sched_set_q_bw_lmt - sets queue BW limit
3803 * @pi: port information structure
3804 * @vsi_handle: sw VSI handle
3805 * @tc: traffic class
3806 * @q_handle: software queue handle
3807 * @rl_type: min, max, or shared
3808 * @bw: bandwidth in Kbps
3810 * This function sets BW limit of queue scheduling node.
3813 ice_sched_set_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3814 u16 q_handle, enum ice_rl_type rl_type, u32 bw)
3816 struct ice_sched_node *node;
3817 struct ice_q_ctx *q_ctx;
3818 int status = -EINVAL;
3820 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3822 mutex_lock(&pi->sched_lock);
3823 q_ctx = ice_get_lan_q_ctx(pi->hw, vsi_handle, tc, q_handle);
3826 node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
3828 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong q_teid\n");
3832 /* Return error if it is not a leaf node */
3833 if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF)
3836 /* SRL bandwidth layer selection */
3837 if (rl_type == ICE_SHARED_BW) {
3838 u8 sel_layer; /* selected layer */
3840 sel_layer = ice_sched_get_rl_prof_layer(pi, rl_type,
3841 node->tx_sched_layer);
3842 if (sel_layer >= pi->hw->num_tx_sched_layers) {
3846 status = ice_sched_validate_srl_node(node, sel_layer);
3851 if (bw == ICE_SCHED_DFLT_BW)
3852 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
3854 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
3857 status = ice_sched_save_q_bw(q_ctx, rl_type, bw);
3860 mutex_unlock(&pi->sched_lock);
3865 * ice_cfg_q_bw_lmt - configure queue BW limit
3866 * @pi: port information structure
3867 * @vsi_handle: sw VSI handle
3868 * @tc: traffic class
3869 * @q_handle: software queue handle
3870 * @rl_type: min, max, or shared
3871 * @bw: bandwidth in Kbps
3873 * This function configures BW limit of queue scheduling node.
3876 ice_cfg_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3877 u16 q_handle, enum ice_rl_type rl_type, u32 bw)
3879 return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
3884 * ice_cfg_q_bw_dflt_lmt - configure queue BW default limit
3885 * @pi: port information structure
3886 * @vsi_handle: sw VSI handle
3887 * @tc: traffic class
3888 * @q_handle: software queue handle
3889 * @rl_type: min, max, or shared
3891 * This function configures BW default limit of queue scheduling node.
3894 ice_cfg_q_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3895 u16 q_handle, enum ice_rl_type rl_type)
3897 return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
3902 * ice_sched_get_node_by_id_type - get node from ID type
3903 * @pi: port information structure
3905 * @agg_type: type of aggregator
3906 * @tc: traffic class
3908 * This function returns node identified by ID of type aggregator, and
3909 * based on traffic class (TC). This function needs to be called with
3910 * the scheduler lock held.
3912 static struct ice_sched_node *
3913 ice_sched_get_node_by_id_type(struct ice_port_info *pi, u32 id,
3914 enum ice_agg_type agg_type, u8 tc)
3916 struct ice_sched_node *node = NULL;
3919 case ICE_AGG_TYPE_VSI: {
3920 struct ice_vsi_ctx *vsi_ctx;
3921 u16 vsi_handle = (u16)id;
3923 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3925 /* Get sched_vsi_info */
3926 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3929 node = vsi_ctx->sched.vsi_node[tc];
3933 case ICE_AGG_TYPE_AGG: {
3934 struct ice_sched_node *tc_node;
3936 tc_node = ice_sched_get_tc_node(pi, tc);
3938 node = ice_sched_get_agg_node(pi, tc_node, id);
3950 * ice_sched_set_node_bw_lmt_per_tc - set node BW limit per TC
3951 * @pi: port information structure
3952 * @id: ID (software VSI handle or AGG ID)
3953 * @agg_type: aggregator type (VSI or AGG type node)
3954 * @tc: traffic class
3955 * @rl_type: min or max
3956 * @bw: bandwidth in Kbps
3958 * This function sets BW limit of VSI or Aggregator scheduling node
3959 * based on TC information from passed in argument BW.
3962 ice_sched_set_node_bw_lmt_per_tc(struct ice_port_info *pi, u32 id,
3963 enum ice_agg_type agg_type, u8 tc,
3964 enum ice_rl_type rl_type, u32 bw)
3966 struct ice_sched_node *node;
3967 int status = -EINVAL;
3972 if (rl_type == ICE_UNKNOWN_BW)
3975 mutex_lock(&pi->sched_lock);
3976 node = ice_sched_get_node_by_id_type(pi, id, agg_type, tc);
3978 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong id, agg type, or tc\n");
3979 goto exit_set_node_bw_lmt_per_tc;
3981 if (bw == ICE_SCHED_DFLT_BW)
3982 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
3984 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
3986 exit_set_node_bw_lmt_per_tc:
3987 mutex_unlock(&pi->sched_lock);
3992 * ice_cfg_vsi_bw_lmt_per_tc - configure VSI BW limit per TC
3993 * @pi: port information structure
3994 * @vsi_handle: software VSI handle
3995 * @tc: traffic class
3996 * @rl_type: min or max
3997 * @bw: bandwidth in Kbps
3999 * This function configures BW limit of VSI scheduling node based on TC
4003 ice_cfg_vsi_bw_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4004 enum ice_rl_type rl_type, u32 bw)
4008 status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
4012 mutex_lock(&pi->sched_lock);
4013 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type, bw);
4014 mutex_unlock(&pi->sched_lock);
4020 * ice_cfg_vsi_bw_dflt_lmt_per_tc - configure default VSI BW limit per TC
4021 * @pi: port information structure
4022 * @vsi_handle: software VSI handle
4023 * @tc: traffic class
4024 * @rl_type: min or max
4026 * This function configures default BW limit of VSI scheduling node based on TC
4030 ice_cfg_vsi_bw_dflt_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4031 enum ice_rl_type rl_type)
4035 status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
4040 mutex_lock(&pi->sched_lock);
4041 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type,
4043 mutex_unlock(&pi->sched_lock);
4049 * ice_cfg_rl_burst_size - Set burst size value
4050 * @hw: pointer to the HW struct
4051 * @bytes: burst size in bytes
4053 * This function configures/set the burst size to requested new value. The new
4054 * burst size value is used for future rate limit calls. It doesn't change the
4055 * existing or previously created RL profiles.
4057 int ice_cfg_rl_burst_size(struct ice_hw *hw, u32 bytes)
4059 u16 burst_size_to_prog;
4061 if (bytes < ICE_MIN_BURST_SIZE_ALLOWED ||
4062 bytes > ICE_MAX_BURST_SIZE_ALLOWED)
4064 if (ice_round_to_num(bytes, 64) <=
4065 ICE_MAX_BURST_SIZE_64_BYTE_GRANULARITY) {
4066 /* 64 byte granularity case */
4067 /* Disable MSB granularity bit */
4068 burst_size_to_prog = ICE_64_BYTE_GRANULARITY;
4069 /* round number to nearest 64 byte granularity */
4070 bytes = ice_round_to_num(bytes, 64);
4071 /* The value is in 64 byte chunks */
4072 burst_size_to_prog |= (u16)(bytes / 64);
4074 /* k bytes granularity case */
4075 /* Enable MSB granularity bit */
4076 burst_size_to_prog = ICE_KBYTE_GRANULARITY;
4077 /* round number to nearest 1024 granularity */
4078 bytes = ice_round_to_num(bytes, 1024);
4079 /* check rounding doesn't go beyond allowed */
4080 if (bytes > ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY)
4081 bytes = ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY;
4082 /* The value is in k bytes */
4083 burst_size_to_prog |= (u16)(bytes / 1024);
4085 hw->max_burst_size = burst_size_to_prog;
4090 * ice_sched_replay_node_prio - re-configure node priority
4091 * @hw: pointer to the HW struct
4092 * @node: sched node to configure
4093 * @priority: priority value
4095 * This function configures node element's priority value. It
4096 * needs to be called with scheduler lock held.
4099 ice_sched_replay_node_prio(struct ice_hw *hw, struct ice_sched_node *node,
4102 struct ice_aqc_txsched_elem_data buf;
4103 struct ice_aqc_txsched_elem *data;
4108 data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
4109 data->generic = priority;
4111 /* Configure element */
4112 status = ice_sched_update_elem(hw, node, &buf);
4117 * ice_sched_replay_node_bw - replay node(s) BW
4118 * @hw: pointer to the HW struct
4119 * @node: sched node to configure
4120 * @bw_t_info: BW type information
4122 * This function restores node's BW from bw_t_info. The caller needs
4123 * to hold the scheduler lock.
4126 ice_sched_replay_node_bw(struct ice_hw *hw, struct ice_sched_node *node,
4127 struct ice_bw_type_info *bw_t_info)
4129 struct ice_port_info *pi = hw->port_info;
4130 int status = -EINVAL;
4135 if (bitmap_empty(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CNT))
4137 if (test_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap)) {
4138 status = ice_sched_replay_node_prio(hw, node,
4139 bw_t_info->generic);
4143 if (test_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap)) {
4144 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW,
4145 bw_t_info->cir_bw.bw);
4149 if (test_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap)) {
4150 bw_alloc = bw_t_info->cir_bw.bw_alloc;
4151 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MIN_BW,
4156 if (test_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap)) {
4157 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW,
4158 bw_t_info->eir_bw.bw);
4162 if (test_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap)) {
4163 bw_alloc = bw_t_info->eir_bw.bw_alloc;
4164 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MAX_BW,
4169 if (test_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap))
4170 status = ice_sched_set_node_bw_lmt(pi, node, ICE_SHARED_BW,
4171 bw_t_info->shared_bw);
4176 * ice_sched_get_ena_tc_bitmap - get enabled TC bitmap
4177 * @pi: port info struct
4178 * @tc_bitmap: 8 bits TC bitmap to check
4179 * @ena_tc_bitmap: 8 bits enabled TC bitmap to return
4181 * This function returns enabled TC bitmap in variable ena_tc_bitmap. Some TCs
4182 * may be missing, it returns enabled TCs. This function needs to be called with
4183 * scheduler lock held.
4186 ice_sched_get_ena_tc_bitmap(struct ice_port_info *pi,
4187 unsigned long *tc_bitmap,
4188 unsigned long *ena_tc_bitmap)
4192 /* Some TC(s) may be missing after reset, adjust for replay */
4193 ice_for_each_traffic_class(tc)
4194 if (ice_is_tc_ena(*tc_bitmap, tc) &&
4195 (ice_sched_get_tc_node(pi, tc)))
4196 set_bit(tc, ena_tc_bitmap);
4200 * ice_sched_replay_agg - recreate aggregator node(s)
4201 * @hw: pointer to the HW struct
4203 * This function recreate aggregator type nodes which are not replayed earlier.
4204 * It also replay aggregator BW information. These aggregator nodes are not
4205 * associated with VSI type node yet.
4207 void ice_sched_replay_agg(struct ice_hw *hw)
4209 struct ice_port_info *pi = hw->port_info;
4210 struct ice_sched_agg_info *agg_info;
4212 mutex_lock(&pi->sched_lock);
4213 list_for_each_entry(agg_info, &hw->agg_list, list_entry)
4214 /* replay aggregator (re-create aggregator node) */
4215 if (!bitmap_equal(agg_info->tc_bitmap, agg_info->replay_tc_bitmap,
4216 ICE_MAX_TRAFFIC_CLASS)) {
4217 DECLARE_BITMAP(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4220 bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4221 ice_sched_get_ena_tc_bitmap(pi,
4222 agg_info->replay_tc_bitmap,
4224 status = ice_sched_cfg_agg(hw->port_info,
4229 dev_info(ice_hw_to_dev(hw),
4230 "Replay agg id[%d] failed\n",
4232 /* Move on to next one */
4236 mutex_unlock(&pi->sched_lock);
4240 * ice_sched_replay_agg_vsi_preinit - Agg/VSI replay pre initialization
4241 * @hw: pointer to the HW struct
4243 * This function initialize aggregator(s) TC bitmap to zero. A required
4244 * preinit step for replaying aggregators.
4246 void ice_sched_replay_agg_vsi_preinit(struct ice_hw *hw)
4248 struct ice_port_info *pi = hw->port_info;
4249 struct ice_sched_agg_info *agg_info;
4251 mutex_lock(&pi->sched_lock);
4252 list_for_each_entry(agg_info, &hw->agg_list, list_entry) {
4253 struct ice_sched_agg_vsi_info *agg_vsi_info;
4255 agg_info->tc_bitmap[0] = 0;
4256 list_for_each_entry(agg_vsi_info, &agg_info->agg_vsi_list,
4258 agg_vsi_info->tc_bitmap[0] = 0;
4260 mutex_unlock(&pi->sched_lock);
4264 * ice_sched_replay_vsi_agg - replay aggregator & VSI to aggregator node(s)
4265 * @hw: pointer to the HW struct
4266 * @vsi_handle: software VSI handle
4268 * This function replays aggregator node, VSI to aggregator type nodes, and
4269 * their node bandwidth information. This function needs to be called with
4270 * scheduler lock held.
4272 static int ice_sched_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
4274 DECLARE_BITMAP(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4275 struct ice_sched_agg_vsi_info *agg_vsi_info;
4276 struct ice_port_info *pi = hw->port_info;
4277 struct ice_sched_agg_info *agg_info;
4280 bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4281 if (!ice_is_vsi_valid(hw, vsi_handle))
4283 agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
4285 return 0; /* Not present in list - default Agg case */
4286 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
4288 return 0; /* Not present in list - default Agg case */
4289 ice_sched_get_ena_tc_bitmap(pi, agg_info->replay_tc_bitmap,
4291 /* Replay aggregator node associated to vsi_handle */
4292 status = ice_sched_cfg_agg(hw->port_info, agg_info->agg_id,
4293 ICE_AGG_TYPE_AGG, replay_bitmap);
4297 bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4298 ice_sched_get_ena_tc_bitmap(pi, agg_vsi_info->replay_tc_bitmap,
4300 /* Move this VSI (vsi_handle) to above aggregator */
4301 return ice_sched_assoc_vsi_to_agg(pi, agg_info->agg_id, vsi_handle,
4306 * ice_replay_vsi_agg - replay VSI to aggregator node
4307 * @hw: pointer to the HW struct
4308 * @vsi_handle: software VSI handle
4310 * This function replays association of VSI to aggregator type nodes, and
4311 * node bandwidth information.
4313 int ice_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
4315 struct ice_port_info *pi = hw->port_info;
4318 mutex_lock(&pi->sched_lock);
4319 status = ice_sched_replay_vsi_agg(hw, vsi_handle);
4320 mutex_unlock(&pi->sched_lock);
4325 * ice_sched_replay_q_bw - replay queue type node BW
4326 * @pi: port information structure
4327 * @q_ctx: queue context structure
4329 * This function replays queue type node bandwidth. This function needs to be
4330 * called with scheduler lock held.
4332 int ice_sched_replay_q_bw(struct ice_port_info *pi, struct ice_q_ctx *q_ctx)
4334 struct ice_sched_node *q_node;
4336 /* Following also checks the presence of node in tree */
4337 q_node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
4340 return ice_sched_replay_node_bw(pi->hw, q_node, &q_ctx->bw_t_info);