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;
574 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
577 /* allocate LAN queue contexts */
578 if (!vsi_ctx->lan_q_ctx[tc]) {
579 q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
580 sizeof(*q_ctx), GFP_KERNEL);
584 for (idx = 0; idx < new_numqs; idx++) {
585 q_ctx[idx].q_handle = ICE_INVAL_Q_HANDLE;
586 q_ctx[idx].q_teid = ICE_INVAL_TEID;
589 vsi_ctx->lan_q_ctx[tc] = q_ctx;
590 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
593 /* num queues are increased, update the queue contexts */
594 if (new_numqs > vsi_ctx->num_lan_q_entries[tc]) {
595 u16 prev_num = vsi_ctx->num_lan_q_entries[tc];
597 q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
598 sizeof(*q_ctx), GFP_KERNEL);
602 memcpy(q_ctx, vsi_ctx->lan_q_ctx[tc],
603 prev_num * sizeof(*q_ctx));
604 devm_kfree(ice_hw_to_dev(hw), vsi_ctx->lan_q_ctx[tc]);
606 for (idx = prev_num; idx < new_numqs; idx++) {
607 q_ctx[idx].q_handle = ICE_INVAL_Q_HANDLE;
608 q_ctx[idx].q_teid = ICE_INVAL_TEID;
611 vsi_ctx->lan_q_ctx[tc] = q_ctx;
612 vsi_ctx->num_lan_q_entries[tc] = new_numqs;
618 * ice_alloc_rdma_q_ctx - allocate RDMA queue contexts for the given VSI and TC
619 * @hw: pointer to the HW struct
620 * @vsi_handle: VSI handle
622 * @new_numqs: number of queues
625 ice_alloc_rdma_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 new_numqs)
627 struct ice_vsi_ctx *vsi_ctx;
628 struct ice_q_ctx *q_ctx;
630 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
633 /* allocate RDMA queue contexts */
634 if (!vsi_ctx->rdma_q_ctx[tc]) {
635 vsi_ctx->rdma_q_ctx[tc] = devm_kcalloc(ice_hw_to_dev(hw),
639 if (!vsi_ctx->rdma_q_ctx[tc])
641 vsi_ctx->num_rdma_q_entries[tc] = new_numqs;
644 /* num queues are increased, update the queue contexts */
645 if (new_numqs > vsi_ctx->num_rdma_q_entries[tc]) {
646 u16 prev_num = vsi_ctx->num_rdma_q_entries[tc];
648 q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
649 sizeof(*q_ctx), GFP_KERNEL);
652 memcpy(q_ctx, vsi_ctx->rdma_q_ctx[tc],
653 prev_num * sizeof(*q_ctx));
654 devm_kfree(ice_hw_to_dev(hw), vsi_ctx->rdma_q_ctx[tc]);
655 vsi_ctx->rdma_q_ctx[tc] = q_ctx;
656 vsi_ctx->num_rdma_q_entries[tc] = new_numqs;
662 * ice_aq_rl_profile - performs a rate limiting task
663 * @hw: pointer to the HW struct
664 * @opcode: opcode for add, query, or remove profile(s)
665 * @num_profiles: the number of profiles
666 * @buf: pointer to buffer
667 * @buf_size: buffer size in bytes
668 * @num_processed: number of processed add or remove profile(s) to return
669 * @cd: pointer to command details structure
671 * RL profile function to add, query, or remove profile(s)
674 ice_aq_rl_profile(struct ice_hw *hw, enum ice_adminq_opc opcode,
675 u16 num_profiles, struct ice_aqc_rl_profile_elem *buf,
676 u16 buf_size, u16 *num_processed, struct ice_sq_cd *cd)
678 struct ice_aqc_rl_profile *cmd;
679 struct ice_aq_desc desc;
682 cmd = &desc.params.rl_profile;
684 ice_fill_dflt_direct_cmd_desc(&desc, opcode);
685 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
686 cmd->num_profiles = cpu_to_le16(num_profiles);
687 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
688 if (!status && num_processed)
689 *num_processed = le16_to_cpu(cmd->num_processed);
694 * ice_aq_add_rl_profile - adds rate limiting profile(s)
695 * @hw: pointer to the HW struct
696 * @num_profiles: the number of profile(s) to be add
697 * @buf: pointer to buffer
698 * @buf_size: buffer size in bytes
699 * @num_profiles_added: total number of profiles added to return
700 * @cd: pointer to command details structure
702 * Add RL profile (0x0410)
705 ice_aq_add_rl_profile(struct ice_hw *hw, u16 num_profiles,
706 struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
707 u16 *num_profiles_added, struct ice_sq_cd *cd)
709 return ice_aq_rl_profile(hw, ice_aqc_opc_add_rl_profiles, num_profiles,
710 buf, buf_size, num_profiles_added, cd);
714 * ice_aq_remove_rl_profile - removes RL profile(s)
715 * @hw: pointer to the HW struct
716 * @num_profiles: the number of profile(s) to remove
717 * @buf: pointer to buffer
718 * @buf_size: buffer size in bytes
719 * @num_profiles_removed: total number of profiles removed to return
720 * @cd: pointer to command details structure or NULL
722 * Remove RL profile (0x0415)
725 ice_aq_remove_rl_profile(struct ice_hw *hw, u16 num_profiles,
726 struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
727 u16 *num_profiles_removed, struct ice_sq_cd *cd)
729 return ice_aq_rl_profile(hw, ice_aqc_opc_remove_rl_profiles,
730 num_profiles, buf, buf_size,
731 num_profiles_removed, cd);
735 * ice_sched_del_rl_profile - remove RL profile
736 * @hw: pointer to the HW struct
737 * @rl_info: rate limit profile information
739 * If the profile ID is not referenced anymore, it removes profile ID with
740 * its associated parameters from HW DB,and locally. The caller needs to
741 * hold scheduler lock.
744 ice_sched_del_rl_profile(struct ice_hw *hw,
745 struct ice_aqc_rl_profile_info *rl_info)
747 struct ice_aqc_rl_profile_elem *buf;
748 u16 num_profiles_removed;
749 u16 num_profiles = 1;
752 if (rl_info->prof_id_ref != 0)
755 /* Safe to remove profile ID */
756 buf = &rl_info->profile;
757 status = ice_aq_remove_rl_profile(hw, num_profiles, buf, sizeof(*buf),
758 &num_profiles_removed, NULL);
759 if (status || num_profiles_removed != num_profiles)
762 /* Delete stale entry now */
763 list_del(&rl_info->list_entry);
764 devm_kfree(ice_hw_to_dev(hw), rl_info);
769 * ice_sched_clear_rl_prof - clears RL prof entries
770 * @pi: port information structure
772 * This function removes all RL profile from HW as well as from SW DB.
774 static void ice_sched_clear_rl_prof(struct ice_port_info *pi)
778 for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
779 struct ice_aqc_rl_profile_info *rl_prof_elem;
780 struct ice_aqc_rl_profile_info *rl_prof_tmp;
782 list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
783 &pi->rl_prof_list[ln], list_entry) {
784 struct ice_hw *hw = pi->hw;
787 rl_prof_elem->prof_id_ref = 0;
788 status = ice_sched_del_rl_profile(hw, rl_prof_elem);
790 ice_debug(hw, ICE_DBG_SCHED, "Remove rl profile failed\n");
791 /* On error, free mem required */
792 list_del(&rl_prof_elem->list_entry);
793 devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
800 * ice_sched_clear_agg - clears the aggregator related information
801 * @hw: pointer to the hardware structure
803 * This function removes aggregator list and free up aggregator related memory
804 * previously allocated.
806 void ice_sched_clear_agg(struct ice_hw *hw)
808 struct ice_sched_agg_info *agg_info;
809 struct ice_sched_agg_info *atmp;
811 list_for_each_entry_safe(agg_info, atmp, &hw->agg_list, list_entry) {
812 struct ice_sched_agg_vsi_info *agg_vsi_info;
813 struct ice_sched_agg_vsi_info *vtmp;
815 list_for_each_entry_safe(agg_vsi_info, vtmp,
816 &agg_info->agg_vsi_list, list_entry) {
817 list_del(&agg_vsi_info->list_entry);
818 devm_kfree(ice_hw_to_dev(hw), agg_vsi_info);
820 list_del(&agg_info->list_entry);
821 devm_kfree(ice_hw_to_dev(hw), agg_info);
826 * ice_sched_clear_tx_topo - clears the scheduler tree nodes
827 * @pi: port information structure
829 * This function removes all the nodes from HW as well as from SW DB.
831 static void ice_sched_clear_tx_topo(struct ice_port_info *pi)
835 /* remove RL profiles related lists */
836 ice_sched_clear_rl_prof(pi);
838 ice_free_sched_node(pi, pi->root);
844 * ice_sched_clear_port - clear the scheduler elements from SW DB for a port
845 * @pi: port information structure
847 * Cleanup scheduling elements from SW DB
849 void ice_sched_clear_port(struct ice_port_info *pi)
851 if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
854 pi->port_state = ICE_SCHED_PORT_STATE_INIT;
855 mutex_lock(&pi->sched_lock);
856 ice_sched_clear_tx_topo(pi);
857 mutex_unlock(&pi->sched_lock);
858 mutex_destroy(&pi->sched_lock);
862 * ice_sched_cleanup_all - cleanup scheduler elements from SW DB for all ports
863 * @hw: pointer to the HW struct
865 * Cleanup scheduling elements from SW DB for all the ports
867 void ice_sched_cleanup_all(struct ice_hw *hw)
872 devm_kfree(ice_hw_to_dev(hw), hw->layer_info);
873 hw->layer_info = NULL;
875 ice_sched_clear_port(hw->port_info);
877 hw->num_tx_sched_layers = 0;
878 hw->num_tx_sched_phys_layers = 0;
879 hw->flattened_layers = 0;
884 * ice_sched_add_elems - add nodes to HW and SW DB
885 * @pi: port information structure
886 * @tc_node: pointer to the branch node
887 * @parent: pointer to the parent node
888 * @layer: layer number to add nodes
889 * @num_nodes: number of nodes
890 * @num_nodes_added: pointer to num nodes added
891 * @first_node_teid: if new nodes are added then return the TEID of first node
892 * @prealloc_nodes: preallocated nodes struct for software DB
894 * This function add nodes to HW as well as to SW DB for a given layer
897 ice_sched_add_elems(struct ice_port_info *pi, struct ice_sched_node *tc_node,
898 struct ice_sched_node *parent, u8 layer, u16 num_nodes,
899 u16 *num_nodes_added, u32 *first_node_teid,
900 struct ice_sched_node **prealloc_nodes)
902 struct ice_sched_node *prev, *new_node;
903 struct ice_aqc_add_elem *buf;
904 u16 i, num_groups_added = 0;
905 struct ice_hw *hw = pi->hw;
910 buf_size = struct_size(buf, generic, num_nodes);
911 buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
915 buf->hdr.parent_teid = parent->info.node_teid;
916 buf->hdr.num_elems = cpu_to_le16(num_nodes);
917 for (i = 0; i < num_nodes; i++) {
918 buf->generic[i].parent_teid = parent->info.node_teid;
919 buf->generic[i].data.elem_type = ICE_AQC_ELEM_TYPE_SE_GENERIC;
920 buf->generic[i].data.valid_sections =
921 ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
922 ICE_AQC_ELEM_VALID_EIR;
923 buf->generic[i].data.generic = 0;
924 buf->generic[i].data.cir_bw.bw_profile_idx =
925 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
926 buf->generic[i].data.cir_bw.bw_alloc =
927 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
928 buf->generic[i].data.eir_bw.bw_profile_idx =
929 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
930 buf->generic[i].data.eir_bw.bw_alloc =
931 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
934 status = ice_aq_add_sched_elems(hw, 1, buf, buf_size,
935 &num_groups_added, NULL);
936 if (status || num_groups_added != 1) {
937 ice_debug(hw, ICE_DBG_SCHED, "add node failed FW Error %d\n",
938 hw->adminq.sq_last_status);
939 devm_kfree(ice_hw_to_dev(hw), buf);
943 *num_nodes_added = num_nodes;
944 /* add nodes to the SW DB */
945 for (i = 0; i < num_nodes; i++) {
947 status = ice_sched_add_node(pi, layer, &buf->generic[i], prealloc_nodes[i]);
949 status = ice_sched_add_node(pi, layer, &buf->generic[i], NULL);
952 ice_debug(hw, ICE_DBG_SCHED, "add nodes in SW DB failed status =%d\n",
957 teid = le32_to_cpu(buf->generic[i].node_teid);
958 new_node = ice_sched_find_node_by_teid(parent, teid);
960 ice_debug(hw, ICE_DBG_SCHED, "Node is missing for teid =%d\n", teid);
964 new_node->sibling = NULL;
965 new_node->tc_num = tc_node->tc_num;
966 new_node->tx_weight = ICE_SCHED_DFLT_BW_WT;
967 new_node->tx_share = ICE_SCHED_DFLT_BW;
968 new_node->tx_max = ICE_SCHED_DFLT_BW;
969 new_node->name = kzalloc(SCHED_NODE_NAME_MAX_LEN, GFP_KERNEL);
973 status = xa_alloc(&pi->sched_node_ids, &new_node->id, NULL, XA_LIMIT(0, UINT_MAX),
976 ice_debug(hw, ICE_DBG_SCHED, "xa_alloc failed for sched node status =%d\n",
981 snprintf(new_node->name, SCHED_NODE_NAME_MAX_LEN, "node_%u", new_node->id);
983 /* add it to previous node sibling pointer */
984 /* Note: siblings are not linked across branches */
985 prev = ice_sched_get_first_node(pi, tc_node, layer);
986 if (prev && prev != new_node) {
987 while (prev->sibling)
988 prev = prev->sibling;
989 prev->sibling = new_node;
992 /* initialize the sibling head */
993 if (!pi->sib_head[tc_node->tc_num][layer])
994 pi->sib_head[tc_node->tc_num][layer] = new_node;
997 *first_node_teid = teid;
1000 devm_kfree(ice_hw_to_dev(hw), buf);
1005 * ice_sched_add_nodes_to_hw_layer - Add nodes to HW layer
1006 * @pi: port information structure
1007 * @tc_node: pointer to TC node
1008 * @parent: pointer to parent node
1009 * @layer: layer number to add nodes
1010 * @num_nodes: number of nodes to be added
1011 * @first_node_teid: pointer to the first node TEID
1012 * @num_nodes_added: pointer to number of nodes added
1014 * Add nodes into specific HW layer.
1017 ice_sched_add_nodes_to_hw_layer(struct ice_port_info *pi,
1018 struct ice_sched_node *tc_node,
1019 struct ice_sched_node *parent, u8 layer,
1020 u16 num_nodes, u32 *first_node_teid,
1021 u16 *num_nodes_added)
1023 u16 max_child_nodes;
1025 *num_nodes_added = 0;
1030 if (!parent || layer < pi->hw->sw_entry_point_layer)
1033 /* max children per node per layer */
1034 max_child_nodes = pi->hw->max_children[parent->tx_sched_layer];
1036 /* current number of children + required nodes exceed max children */
1037 if ((parent->num_children + num_nodes) > max_child_nodes) {
1038 /* Fail if the parent is a TC node */
1039 if (parent == tc_node)
1044 return ice_sched_add_elems(pi, tc_node, parent, layer, num_nodes,
1045 num_nodes_added, first_node_teid, NULL);
1049 * ice_sched_add_nodes_to_layer - Add nodes to a given layer
1050 * @pi: port information structure
1051 * @tc_node: pointer to TC node
1052 * @parent: pointer to parent node
1053 * @layer: layer number to add nodes
1054 * @num_nodes: number of nodes to be added
1055 * @first_node_teid: pointer to the first node TEID
1056 * @num_nodes_added: pointer to number of nodes added
1058 * This function add nodes to a given layer.
1061 ice_sched_add_nodes_to_layer(struct ice_port_info *pi,
1062 struct ice_sched_node *tc_node,
1063 struct ice_sched_node *parent, u8 layer,
1064 u16 num_nodes, u32 *first_node_teid,
1065 u16 *num_nodes_added)
1067 u32 *first_teid_ptr = first_node_teid;
1068 u16 new_num_nodes = num_nodes;
1071 *num_nodes_added = 0;
1072 while (*num_nodes_added < num_nodes) {
1073 u16 max_child_nodes, num_added = 0;
1076 status = ice_sched_add_nodes_to_hw_layer(pi, tc_node, parent,
1077 layer, new_num_nodes,
1081 *num_nodes_added += num_added;
1082 /* added more nodes than requested ? */
1083 if (*num_nodes_added > num_nodes) {
1084 ice_debug(pi->hw, ICE_DBG_SCHED, "added extra nodes %d %d\n", num_nodes,
1089 /* break if all the nodes are added successfully */
1090 if (!status && (*num_nodes_added == num_nodes))
1092 /* break if the error is not max limit */
1093 if (status && status != -ENOSPC)
1095 /* Exceeded the max children */
1096 max_child_nodes = pi->hw->max_children[parent->tx_sched_layer];
1097 /* utilize all the spaces if the parent is not full */
1098 if (parent->num_children < max_child_nodes) {
1099 new_num_nodes = max_child_nodes - parent->num_children;
1101 /* This parent is full, try the next sibling */
1102 parent = parent->sibling;
1103 /* Don't modify the first node TEID memory if the
1104 * first node was added already in the above call.
1105 * Instead send some temp memory for all other
1109 first_teid_ptr = &temp;
1111 new_num_nodes = num_nodes - *num_nodes_added;
1118 * ice_sched_get_qgrp_layer - get the current queue group layer number
1119 * @hw: pointer to the HW struct
1121 * This function returns the current queue group layer number
1123 static u8 ice_sched_get_qgrp_layer(struct ice_hw *hw)
1125 /* It's always total layers - 1, the array is 0 relative so -2 */
1126 return hw->num_tx_sched_layers - ICE_QGRP_LAYER_OFFSET;
1130 * ice_sched_get_vsi_layer - get the current VSI layer number
1131 * @hw: pointer to the HW struct
1133 * This function returns the current VSI layer number
1135 u8 ice_sched_get_vsi_layer(struct ice_hw *hw)
1137 /* Num Layers VSI layer
1140 * 5 or less sw_entry_point_layer
1142 /* calculate the VSI layer based on number of layers. */
1143 if (hw->num_tx_sched_layers > ICE_VSI_LAYER_OFFSET + 1) {
1144 u8 layer = hw->num_tx_sched_layers - ICE_VSI_LAYER_OFFSET;
1146 if (layer > hw->sw_entry_point_layer)
1149 return hw->sw_entry_point_layer;
1153 * ice_sched_get_agg_layer - get the current aggregator layer number
1154 * @hw: pointer to the HW struct
1156 * This function returns the current aggregator layer number
1158 u8 ice_sched_get_agg_layer(struct ice_hw *hw)
1160 /* Num Layers aggregator layer
1162 * 7 or less sw_entry_point_layer
1164 /* calculate the aggregator layer based on number of layers. */
1165 if (hw->num_tx_sched_layers > ICE_AGG_LAYER_OFFSET + 1) {
1166 u8 layer = hw->num_tx_sched_layers - ICE_AGG_LAYER_OFFSET;
1168 if (layer > hw->sw_entry_point_layer)
1171 return hw->sw_entry_point_layer;
1175 * ice_rm_dflt_leaf_node - remove the default leaf node in the tree
1176 * @pi: port information structure
1178 * This function removes the leaf node that was created by the FW
1179 * during initialization
1181 static void ice_rm_dflt_leaf_node(struct ice_port_info *pi)
1183 struct ice_sched_node *node;
1187 if (!node->num_children)
1189 node = node->children[0];
1191 if (node && node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF) {
1192 u32 teid = le32_to_cpu(node->info.node_teid);
1195 /* remove the default leaf node */
1196 status = ice_sched_remove_elems(pi->hw, node->parent, 1, &teid);
1198 ice_free_sched_node(pi, node);
1203 * ice_sched_rm_dflt_nodes - free the default nodes in the tree
1204 * @pi: port information structure
1206 * This function frees all the nodes except root and TC that were created by
1207 * the FW during initialization
1209 static void ice_sched_rm_dflt_nodes(struct ice_port_info *pi)
1211 struct ice_sched_node *node;
1213 ice_rm_dflt_leaf_node(pi);
1215 /* remove the default nodes except TC and root nodes */
1218 if (node->tx_sched_layer >= pi->hw->sw_entry_point_layer &&
1219 node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
1220 node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT) {
1221 ice_free_sched_node(pi, node);
1225 if (!node->num_children)
1227 node = node->children[0];
1232 * ice_sched_init_port - Initialize scheduler by querying information from FW
1233 * @pi: port info structure for the tree to cleanup
1235 * This function is the initial call to find the total number of Tx scheduler
1236 * resources, default topology created by firmware and storing the information
1239 int ice_sched_init_port(struct ice_port_info *pi)
1241 struct ice_aqc_get_topo_elem *buf;
1252 /* Query the Default Topology from FW */
1253 buf = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
1257 /* Query default scheduling tree topology */
1258 status = ice_aq_get_dflt_topo(hw, pi->lport, buf, ICE_AQ_MAX_BUF_LEN,
1259 &num_branches, NULL);
1263 /* num_branches should be between 1-8 */
1264 if (num_branches < 1 || num_branches > ICE_TXSCHED_MAX_BRANCHES) {
1265 ice_debug(hw, ICE_DBG_SCHED, "num_branches unexpected %d\n",
1271 /* get the number of elements on the default/first branch */
1272 num_elems = le16_to_cpu(buf[0].hdr.num_elems);
1274 /* num_elems should always be between 1-9 */
1275 if (num_elems < 1 || num_elems > ICE_AQC_TOPO_MAX_LEVEL_NUM) {
1276 ice_debug(hw, ICE_DBG_SCHED, "num_elems unexpected %d\n",
1282 /* If the last node is a leaf node then the index of the queue group
1283 * layer is two less than the number of elements.
1285 if (num_elems > 2 && buf[0].generic[num_elems - 1].data.elem_type ==
1286 ICE_AQC_ELEM_TYPE_LEAF)
1287 pi->last_node_teid =
1288 le32_to_cpu(buf[0].generic[num_elems - 2].node_teid);
1290 pi->last_node_teid =
1291 le32_to_cpu(buf[0].generic[num_elems - 1].node_teid);
1293 /* Insert the Tx Sched root node */
1294 status = ice_sched_add_root_node(pi, &buf[0].generic[0]);
1298 /* Parse the default tree and cache the information */
1299 for (i = 0; i < num_branches; i++) {
1300 num_elems = le16_to_cpu(buf[i].hdr.num_elems);
1302 /* Skip root element as already inserted */
1303 for (j = 1; j < num_elems; j++) {
1304 /* update the sw entry point */
1305 if (buf[0].generic[j].data.elem_type ==
1306 ICE_AQC_ELEM_TYPE_ENTRY_POINT)
1307 hw->sw_entry_point_layer = j;
1309 status = ice_sched_add_node(pi, j, &buf[i].generic[j], NULL);
1315 /* Remove the default nodes. */
1317 ice_sched_rm_dflt_nodes(pi);
1319 /* initialize the port for handling the scheduler tree */
1320 pi->port_state = ICE_SCHED_PORT_STATE_READY;
1321 mutex_init(&pi->sched_lock);
1322 for (i = 0; i < ICE_AQC_TOPO_MAX_LEVEL_NUM; i++)
1323 INIT_LIST_HEAD(&pi->rl_prof_list[i]);
1326 if (status && pi->root) {
1327 ice_free_sched_node(pi, pi->root);
1336 * ice_sched_query_res_alloc - query the FW for num of logical sched layers
1337 * @hw: pointer to the HW struct
1339 * query FW for allocated scheduler resources and store in HW struct
1341 int ice_sched_query_res_alloc(struct ice_hw *hw)
1343 struct ice_aqc_query_txsched_res_resp *buf;
1351 buf = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*buf), GFP_KERNEL);
1355 status = ice_aq_query_sched_res(hw, sizeof(*buf), buf, NULL);
1357 goto sched_query_out;
1359 hw->num_tx_sched_layers = le16_to_cpu(buf->sched_props.logical_levels);
1360 hw->num_tx_sched_phys_layers =
1361 le16_to_cpu(buf->sched_props.phys_levels);
1362 hw->flattened_layers = buf->sched_props.flattening_bitmap;
1363 hw->max_cgds = buf->sched_props.max_pf_cgds;
1365 /* max sibling group size of current layer refers to the max children
1366 * of the below layer node.
1367 * layer 1 node max children will be layer 2 max sibling group size
1368 * layer 2 node max children will be layer 3 max sibling group size
1369 * and so on. This array will be populated from root (index 0) to
1370 * qgroup layer 7. Leaf node has no children.
1372 for (i = 0; i < hw->num_tx_sched_layers - 1; i++) {
1373 max_sibl = buf->layer_props[i + 1].max_sibl_grp_sz;
1374 hw->max_children[i] = le16_to_cpu(max_sibl);
1377 hw->layer_info = devm_kmemdup(ice_hw_to_dev(hw), buf->layer_props,
1378 (hw->num_tx_sched_layers *
1379 sizeof(*hw->layer_info)),
1381 if (!hw->layer_info) {
1383 goto sched_query_out;
1387 devm_kfree(ice_hw_to_dev(hw), buf);
1392 * ice_sched_get_psm_clk_freq - determine the PSM clock frequency
1393 * @hw: pointer to the HW struct
1395 * Determine the PSM clock frequency and store in HW struct
1397 void ice_sched_get_psm_clk_freq(struct ice_hw *hw)
1401 val = rd32(hw, GLGEN_CLKSTAT_SRC);
1402 clk_src = (val & GLGEN_CLKSTAT_SRC_PSM_CLK_SRC_M) >>
1403 GLGEN_CLKSTAT_SRC_PSM_CLK_SRC_S;
1405 #define PSM_CLK_SRC_367_MHZ 0x0
1406 #define PSM_CLK_SRC_416_MHZ 0x1
1407 #define PSM_CLK_SRC_446_MHZ 0x2
1408 #define PSM_CLK_SRC_390_MHZ 0x3
1411 case PSM_CLK_SRC_367_MHZ:
1412 hw->psm_clk_freq = ICE_PSM_CLK_367MHZ_IN_HZ;
1414 case PSM_CLK_SRC_416_MHZ:
1415 hw->psm_clk_freq = ICE_PSM_CLK_416MHZ_IN_HZ;
1417 case PSM_CLK_SRC_446_MHZ:
1418 hw->psm_clk_freq = ICE_PSM_CLK_446MHZ_IN_HZ;
1420 case PSM_CLK_SRC_390_MHZ:
1421 hw->psm_clk_freq = ICE_PSM_CLK_390MHZ_IN_HZ;
1424 ice_debug(hw, ICE_DBG_SCHED, "PSM clk_src unexpected %u\n",
1426 /* fall back to a safe default */
1427 hw->psm_clk_freq = ICE_PSM_CLK_446MHZ_IN_HZ;
1432 * ice_sched_find_node_in_subtree - Find node in part of base node subtree
1433 * @hw: pointer to the HW struct
1434 * @base: pointer to the base node
1435 * @node: pointer to the node to search
1437 * This function checks whether a given node is part of the base node
1441 ice_sched_find_node_in_subtree(struct ice_hw *hw, struct ice_sched_node *base,
1442 struct ice_sched_node *node)
1446 for (i = 0; i < base->num_children; i++) {
1447 struct ice_sched_node *child = base->children[i];
1452 if (child->tx_sched_layer > node->tx_sched_layer)
1455 /* this recursion is intentional, and wouldn't
1456 * go more than 8 calls
1458 if (ice_sched_find_node_in_subtree(hw, child, node))
1465 * ice_sched_get_free_qgrp - Scan all queue group siblings and find a free node
1466 * @pi: port information structure
1467 * @vsi_node: software VSI handle
1468 * @qgrp_node: first queue group node identified for scanning
1469 * @owner: LAN or RDMA
1471 * This function retrieves a free LAN or RDMA queue group node by scanning
1472 * qgrp_node and its siblings for the queue group with the fewest number
1473 * of queues currently assigned.
1475 static struct ice_sched_node *
1476 ice_sched_get_free_qgrp(struct ice_port_info *pi,
1477 struct ice_sched_node *vsi_node,
1478 struct ice_sched_node *qgrp_node, u8 owner)
1480 struct ice_sched_node *min_qgrp;
1485 min_children = qgrp_node->num_children;
1488 min_qgrp = qgrp_node;
1489 /* scan all queue groups until find a node which has less than the
1490 * minimum number of children. This way all queue group nodes get
1491 * equal number of shares and active. The bandwidth will be equally
1492 * distributed across all queues.
1495 /* make sure the qgroup node is part of the VSI subtree */
1496 if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1497 if (qgrp_node->num_children < min_children &&
1498 qgrp_node->owner == owner) {
1499 /* replace the new min queue group node */
1500 min_qgrp = qgrp_node;
1501 min_children = min_qgrp->num_children;
1502 /* break if it has no children, */
1506 qgrp_node = qgrp_node->sibling;
1512 * ice_sched_get_free_qparent - Get a free LAN or RDMA queue group node
1513 * @pi: port information structure
1514 * @vsi_handle: software VSI handle
1515 * @tc: branch number
1516 * @owner: LAN or RDMA
1518 * This function retrieves a free LAN or RDMA queue group node
1520 struct ice_sched_node *
1521 ice_sched_get_free_qparent(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
1524 struct ice_sched_node *vsi_node, *qgrp_node;
1525 struct ice_vsi_ctx *vsi_ctx;
1529 qgrp_layer = ice_sched_get_qgrp_layer(pi->hw);
1530 max_children = pi->hw->max_children[qgrp_layer];
1532 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1535 vsi_node = vsi_ctx->sched.vsi_node[tc];
1536 /* validate invalid VSI ID */
1540 /* get the first queue group node from VSI sub-tree */
1541 qgrp_node = ice_sched_get_first_node(pi, vsi_node, qgrp_layer);
1543 /* make sure the qgroup node is part of the VSI subtree */
1544 if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1545 if (qgrp_node->num_children < max_children &&
1546 qgrp_node->owner == owner)
1548 qgrp_node = qgrp_node->sibling;
1551 /* Select the best queue group */
1552 return ice_sched_get_free_qgrp(pi, vsi_node, qgrp_node, owner);
1556 * ice_sched_get_vsi_node - Get a VSI node based on VSI ID
1557 * @pi: pointer to the port information structure
1558 * @tc_node: pointer to the TC node
1559 * @vsi_handle: software VSI handle
1561 * This function retrieves a VSI node for a given VSI ID from a given
1564 static struct ice_sched_node *
1565 ice_sched_get_vsi_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1568 struct ice_sched_node *node;
1571 vsi_layer = ice_sched_get_vsi_layer(pi->hw);
1572 node = ice_sched_get_first_node(pi, tc_node, vsi_layer);
1574 /* Check whether it already exists */
1576 if (node->vsi_handle == vsi_handle)
1578 node = node->sibling;
1585 * ice_sched_get_agg_node - Get an aggregator node based on aggregator ID
1586 * @pi: pointer to the port information structure
1587 * @tc_node: pointer to the TC node
1588 * @agg_id: aggregator ID
1590 * This function retrieves an aggregator node for a given aggregator ID from
1593 struct ice_sched_node *
1594 ice_sched_get_agg_node(struct ice_port_info *pi, struct ice_sched_node *tc_node,
1597 struct ice_sched_node *node;
1598 struct ice_hw *hw = pi->hw;
1603 agg_layer = ice_sched_get_agg_layer(hw);
1604 node = ice_sched_get_first_node(pi, tc_node, agg_layer);
1606 /* Check whether it already exists */
1608 if (node->agg_id == agg_id)
1610 node = node->sibling;
1617 * ice_sched_calc_vsi_child_nodes - calculate number of VSI child nodes
1618 * @hw: pointer to the HW struct
1619 * @num_qs: number of queues
1620 * @num_nodes: num nodes array
1622 * This function calculates the number of VSI child nodes based on the
1626 ice_sched_calc_vsi_child_nodes(struct ice_hw *hw, u16 num_qs, u16 *num_nodes)
1631 qgl = ice_sched_get_qgrp_layer(hw);
1632 vsil = ice_sched_get_vsi_layer(hw);
1634 /* calculate num nodes from queue group to VSI layer */
1635 for (i = qgl; i > vsil; i--) {
1636 /* round to the next integer if there is a remainder */
1637 num = DIV_ROUND_UP(num, hw->max_children[i]);
1639 /* need at least one node */
1640 num_nodes[i] = num ? num : 1;
1645 * ice_sched_add_vsi_child_nodes - add VSI child nodes to tree
1646 * @pi: port information structure
1647 * @vsi_handle: software VSI handle
1648 * @tc_node: pointer to the TC node
1649 * @num_nodes: pointer to the num nodes that needs to be added per layer
1650 * @owner: node owner (LAN or RDMA)
1652 * This function adds the VSI child nodes to tree. It gets called for
1653 * LAN and RDMA separately.
1656 ice_sched_add_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1657 struct ice_sched_node *tc_node, u16 *num_nodes,
1660 struct ice_sched_node *parent, *node;
1661 struct ice_hw *hw = pi->hw;
1662 u32 first_node_teid;
1666 qgl = ice_sched_get_qgrp_layer(hw);
1667 vsil = ice_sched_get_vsi_layer(hw);
1668 parent = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1669 for (i = vsil + 1; i <= qgl; i++) {
1675 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
1679 if (status || num_nodes[i] != num_added)
1682 /* The newly added node can be a new parent for the next
1686 parent = ice_sched_find_node_by_teid(tc_node,
1690 node->owner = owner;
1691 node = node->sibling;
1694 parent = parent->children[0];
1702 * ice_sched_calc_vsi_support_nodes - calculate number of VSI support nodes
1703 * @pi: pointer to the port info structure
1704 * @tc_node: pointer to TC node
1705 * @num_nodes: pointer to num nodes array
1707 * This function calculates the number of supported nodes needed to add this
1708 * VSI into Tx tree including the VSI, parent and intermediate nodes in below
1712 ice_sched_calc_vsi_support_nodes(struct ice_port_info *pi,
1713 struct ice_sched_node *tc_node, u16 *num_nodes)
1715 struct ice_sched_node *node;
1719 vsil = ice_sched_get_vsi_layer(pi->hw);
1720 for (i = vsil; i >= pi->hw->sw_entry_point_layer; i--)
1721 /* Add intermediate nodes if TC has no children and
1722 * need at least one node for VSI
1724 if (!tc_node->num_children || i == vsil) {
1727 /* If intermediate nodes are reached max children
1728 * then add a new one.
1730 node = ice_sched_get_first_node(pi, tc_node, (u8)i);
1731 /* scan all the siblings */
1733 if (node->num_children < pi->hw->max_children[i])
1735 node = node->sibling;
1738 /* tree has one intermediate node to add this new VSI.
1739 * So no need to calculate supported nodes for below
1744 /* all the nodes are full, allocate a new one */
1750 * ice_sched_add_vsi_support_nodes - add VSI supported nodes into Tx tree
1751 * @pi: port information structure
1752 * @vsi_handle: software VSI handle
1753 * @tc_node: pointer to TC node
1754 * @num_nodes: pointer to num nodes array
1756 * This function adds the VSI supported nodes into Tx tree including the
1757 * VSI, its parent and intermediate nodes in below layers
1760 ice_sched_add_vsi_support_nodes(struct ice_port_info *pi, u16 vsi_handle,
1761 struct ice_sched_node *tc_node, u16 *num_nodes)
1763 struct ice_sched_node *parent = tc_node;
1764 u32 first_node_teid;
1771 vsil = ice_sched_get_vsi_layer(pi->hw);
1772 for (i = pi->hw->sw_entry_point_layer; i <= vsil; i++) {
1775 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
1779 if (status || num_nodes[i] != num_added)
1782 /* The newly added node can be a new parent for the next
1786 parent = ice_sched_find_node_by_teid(tc_node,
1789 parent = parent->children[0];
1795 parent->vsi_handle = vsi_handle;
1802 * ice_sched_add_vsi_to_topo - add a new VSI into tree
1803 * @pi: port information structure
1804 * @vsi_handle: software VSI handle
1807 * This function adds a new VSI into scheduler tree
1810 ice_sched_add_vsi_to_topo(struct ice_port_info *pi, u16 vsi_handle, u8 tc)
1812 u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1813 struct ice_sched_node *tc_node;
1815 tc_node = ice_sched_get_tc_node(pi, tc);
1819 /* calculate number of supported nodes needed for this VSI */
1820 ice_sched_calc_vsi_support_nodes(pi, tc_node, num_nodes);
1822 /* add VSI supported nodes to TC subtree */
1823 return ice_sched_add_vsi_support_nodes(pi, vsi_handle, tc_node,
1828 * ice_sched_update_vsi_child_nodes - update VSI child nodes
1829 * @pi: port information structure
1830 * @vsi_handle: software VSI handle
1832 * @new_numqs: new number of max queues
1833 * @owner: owner of this subtree
1835 * This function updates the VSI child nodes based on the number of queues
1838 ice_sched_update_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1839 u8 tc, u16 new_numqs, u8 owner)
1841 u16 new_num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1842 struct ice_sched_node *vsi_node;
1843 struct ice_sched_node *tc_node;
1844 struct ice_vsi_ctx *vsi_ctx;
1845 struct ice_hw *hw = pi->hw;
1849 tc_node = ice_sched_get_tc_node(pi, tc);
1853 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1857 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1861 if (owner == ICE_SCHED_NODE_OWNER_LAN)
1862 prev_numqs = vsi_ctx->sched.max_lanq[tc];
1864 prev_numqs = vsi_ctx->sched.max_rdmaq[tc];
1865 /* num queues are not changed or less than the previous number */
1866 if (new_numqs <= prev_numqs)
1868 if (owner == ICE_SCHED_NODE_OWNER_LAN) {
1869 status = ice_alloc_lan_q_ctx(hw, vsi_handle, tc, new_numqs);
1873 status = ice_alloc_rdma_q_ctx(hw, vsi_handle, tc, new_numqs);
1879 ice_sched_calc_vsi_child_nodes(hw, new_numqs, new_num_nodes);
1880 /* Keep the max number of queue configuration all the time. Update the
1881 * tree only if number of queues > previous number of queues. This may
1882 * leave some extra nodes in the tree if number of queues < previous
1883 * number but that wouldn't harm anything. Removing those extra nodes
1884 * may complicate the code if those nodes are part of SRL or
1885 * individually rate limited.
1887 status = ice_sched_add_vsi_child_nodes(pi, vsi_handle, tc_node,
1888 new_num_nodes, owner);
1891 if (owner == ICE_SCHED_NODE_OWNER_LAN)
1892 vsi_ctx->sched.max_lanq[tc] = new_numqs;
1894 vsi_ctx->sched.max_rdmaq[tc] = new_numqs;
1900 * ice_sched_cfg_vsi - configure the new/existing VSI
1901 * @pi: port information structure
1902 * @vsi_handle: software VSI handle
1904 * @maxqs: max number of queues
1905 * @owner: LAN or RDMA
1906 * @enable: TC enabled or disabled
1908 * This function adds/updates VSI nodes based on the number of queues. If TC is
1909 * enabled and VSI is in suspended state then resume the VSI back. If TC is
1910 * disabled then suspend the VSI if it is not already.
1913 ice_sched_cfg_vsi(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 maxqs,
1914 u8 owner, bool enable)
1916 struct ice_sched_node *vsi_node, *tc_node;
1917 struct ice_vsi_ctx *vsi_ctx;
1918 struct ice_hw *hw = pi->hw;
1921 ice_debug(pi->hw, ICE_DBG_SCHED, "add/config VSI %d\n", vsi_handle);
1922 tc_node = ice_sched_get_tc_node(pi, tc);
1925 vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1928 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1930 /* suspend the VSI if TC is not enabled */
1932 if (vsi_node && vsi_node->in_use) {
1933 u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1935 status = ice_sched_suspend_resume_elems(hw, 1, &teid,
1938 vsi_node->in_use = false;
1943 /* TC is enabled, if it is a new VSI then add it to the tree */
1945 status = ice_sched_add_vsi_to_topo(pi, vsi_handle, tc);
1949 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
1953 vsi_ctx->sched.vsi_node[tc] = vsi_node;
1954 vsi_node->in_use = true;
1955 /* invalidate the max queues whenever VSI gets added first time
1956 * into the scheduler tree (boot or after reset). We need to
1957 * recreate the child nodes all the time in these cases.
1959 vsi_ctx->sched.max_lanq[tc] = 0;
1960 vsi_ctx->sched.max_rdmaq[tc] = 0;
1963 /* update the VSI child nodes */
1964 status = ice_sched_update_vsi_child_nodes(pi, vsi_handle, tc, maxqs,
1969 /* TC is enabled, resume the VSI if it is in the suspend state */
1970 if (!vsi_node->in_use) {
1971 u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1973 status = ice_sched_suspend_resume_elems(hw, 1, &teid, false);
1975 vsi_node->in_use = true;
1982 * ice_sched_rm_agg_vsi_info - remove aggregator related VSI info entry
1983 * @pi: port information structure
1984 * @vsi_handle: software VSI handle
1986 * This function removes single aggregator VSI info entry from
1989 static void ice_sched_rm_agg_vsi_info(struct ice_port_info *pi, u16 vsi_handle)
1991 struct ice_sched_agg_info *agg_info;
1992 struct ice_sched_agg_info *atmp;
1994 list_for_each_entry_safe(agg_info, atmp, &pi->hw->agg_list,
1996 struct ice_sched_agg_vsi_info *agg_vsi_info;
1997 struct ice_sched_agg_vsi_info *vtmp;
1999 list_for_each_entry_safe(agg_vsi_info, vtmp,
2000 &agg_info->agg_vsi_list, list_entry)
2001 if (agg_vsi_info->vsi_handle == vsi_handle) {
2002 list_del(&agg_vsi_info->list_entry);
2003 devm_kfree(ice_hw_to_dev(pi->hw),
2011 * ice_sched_is_leaf_node_present - check for a leaf node in the sub-tree
2012 * @node: pointer to the sub-tree node
2014 * This function checks for a leaf node presence in a given sub-tree node.
2016 static bool ice_sched_is_leaf_node_present(struct ice_sched_node *node)
2020 for (i = 0; i < node->num_children; i++)
2021 if (ice_sched_is_leaf_node_present(node->children[i]))
2023 /* check for a leaf node */
2024 return (node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF);
2028 * ice_sched_rm_vsi_cfg - remove the VSI and its children nodes
2029 * @pi: port information structure
2030 * @vsi_handle: software VSI handle
2031 * @owner: LAN or RDMA
2033 * This function removes the VSI and its LAN or RDMA children nodes from the
2037 ice_sched_rm_vsi_cfg(struct ice_port_info *pi, u16 vsi_handle, u8 owner)
2039 struct ice_vsi_ctx *vsi_ctx;
2040 int status = -EINVAL;
2043 ice_debug(pi->hw, ICE_DBG_SCHED, "removing VSI %d\n", vsi_handle);
2044 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2046 mutex_lock(&pi->sched_lock);
2047 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
2049 goto exit_sched_rm_vsi_cfg;
2051 ice_for_each_traffic_class(i) {
2052 struct ice_sched_node *vsi_node, *tc_node;
2055 tc_node = ice_sched_get_tc_node(pi, i);
2059 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2063 if (ice_sched_is_leaf_node_present(vsi_node)) {
2064 ice_debug(pi->hw, ICE_DBG_SCHED, "VSI has leaf nodes in TC %d\n", i);
2066 goto exit_sched_rm_vsi_cfg;
2068 while (j < vsi_node->num_children) {
2069 if (vsi_node->children[j]->owner == owner) {
2070 ice_free_sched_node(pi, vsi_node->children[j]);
2072 /* reset the counter again since the num
2073 * children will be updated after node removal
2080 /* remove the VSI if it has no children */
2081 if (!vsi_node->num_children) {
2082 ice_free_sched_node(pi, vsi_node);
2083 vsi_ctx->sched.vsi_node[i] = NULL;
2085 /* clean up aggregator related VSI info if any */
2086 ice_sched_rm_agg_vsi_info(pi, vsi_handle);
2088 if (owner == ICE_SCHED_NODE_OWNER_LAN)
2089 vsi_ctx->sched.max_lanq[i] = 0;
2091 vsi_ctx->sched.max_rdmaq[i] = 0;
2095 exit_sched_rm_vsi_cfg:
2096 mutex_unlock(&pi->sched_lock);
2101 * ice_rm_vsi_lan_cfg - remove VSI and its LAN children nodes
2102 * @pi: port information structure
2103 * @vsi_handle: software VSI handle
2105 * This function clears the VSI and its LAN children nodes from scheduler tree
2108 int ice_rm_vsi_lan_cfg(struct ice_port_info *pi, u16 vsi_handle)
2110 return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_LAN);
2114 * ice_rm_vsi_rdma_cfg - remove VSI and its RDMA children nodes
2115 * @pi: port information structure
2116 * @vsi_handle: software VSI handle
2118 * This function clears the VSI and its RDMA children nodes from scheduler tree
2121 int ice_rm_vsi_rdma_cfg(struct ice_port_info *pi, u16 vsi_handle)
2123 return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_RDMA);
2127 * ice_get_agg_info - get the aggregator ID
2128 * @hw: pointer to the hardware structure
2129 * @agg_id: aggregator ID
2131 * This function validates aggregator ID. The function returns info if
2132 * aggregator ID is present in list otherwise it returns null.
2134 static struct ice_sched_agg_info *
2135 ice_get_agg_info(struct ice_hw *hw, u32 agg_id)
2137 struct ice_sched_agg_info *agg_info;
2139 list_for_each_entry(agg_info, &hw->agg_list, list_entry)
2140 if (agg_info->agg_id == agg_id)
2147 * ice_sched_get_free_vsi_parent - Find a free parent node in aggregator subtree
2148 * @hw: pointer to the HW struct
2149 * @node: pointer to a child node
2150 * @num_nodes: num nodes count array
2152 * This function walks through the aggregator subtree to find a free parent
2155 struct ice_sched_node *
2156 ice_sched_get_free_vsi_parent(struct ice_hw *hw, struct ice_sched_node *node,
2159 u8 l = node->tx_sched_layer;
2162 vsil = ice_sched_get_vsi_layer(hw);
2164 /* Is it VSI parent layer ? */
2166 return (node->num_children < hw->max_children[l]) ? node : NULL;
2168 /* We have intermediate nodes. Let's walk through the subtree. If the
2169 * intermediate node has space to add a new node then clear the count
2171 if (node->num_children < hw->max_children[l])
2173 /* The below recursive call is intentional and wouldn't go more than
2174 * 2 or 3 iterations.
2177 for (i = 0; i < node->num_children; i++) {
2178 struct ice_sched_node *parent;
2180 parent = ice_sched_get_free_vsi_parent(hw, node->children[i],
2190 * ice_sched_update_parent - update the new parent in SW DB
2191 * @new_parent: pointer to a new parent node
2192 * @node: pointer to a child node
2194 * This function removes the child from the old parent and adds it to a new
2198 ice_sched_update_parent(struct ice_sched_node *new_parent,
2199 struct ice_sched_node *node)
2201 struct ice_sched_node *old_parent;
2204 old_parent = node->parent;
2206 /* update the old parent children */
2207 for (i = 0; i < old_parent->num_children; i++)
2208 if (old_parent->children[i] == node) {
2209 for (j = i + 1; j < old_parent->num_children; j++)
2210 old_parent->children[j - 1] =
2211 old_parent->children[j];
2212 old_parent->num_children--;
2216 /* now move the node to a new parent */
2217 new_parent->children[new_parent->num_children++] = node;
2218 node->parent = new_parent;
2219 node->info.parent_teid = new_parent->info.node_teid;
2223 * ice_sched_move_nodes - move child nodes to a given parent
2224 * @pi: port information structure
2225 * @parent: pointer to parent node
2226 * @num_items: number of child nodes to be moved
2227 * @list: pointer to child node teids
2229 * This function move the child nodes to a given parent.
2232 ice_sched_move_nodes(struct ice_port_info *pi, struct ice_sched_node *parent,
2233 u16 num_items, u32 *list)
2235 struct ice_aqc_move_elem *buf;
2236 struct ice_sched_node *node;
2237 u16 i, grps_movd = 0;
2244 if (!parent || !num_items)
2247 /* Does parent have enough space */
2248 if (parent->num_children + num_items >
2249 hw->max_children[parent->tx_sched_layer])
2252 buf_len = struct_size(buf, teid, 1);
2253 buf = kzalloc(buf_len, GFP_KERNEL);
2257 for (i = 0; i < num_items; i++) {
2258 node = ice_sched_find_node_by_teid(pi->root, list[i]);
2264 buf->hdr.src_parent_teid = node->info.parent_teid;
2265 buf->hdr.dest_parent_teid = parent->info.node_teid;
2266 buf->teid[0] = node->info.node_teid;
2267 buf->hdr.num_elems = cpu_to_le16(1);
2268 status = ice_aq_move_sched_elems(hw, 1, buf, buf_len,
2270 if (status && grps_movd != 1) {
2275 /* update the SW DB */
2276 ice_sched_update_parent(parent, node);
2285 * ice_sched_move_vsi_to_agg - move VSI to aggregator node
2286 * @pi: port information structure
2287 * @vsi_handle: software VSI handle
2288 * @agg_id: aggregator ID
2291 * This function moves a VSI to an aggregator node or its subtree.
2292 * Intermediate nodes may be created if required.
2295 ice_sched_move_vsi_to_agg(struct ice_port_info *pi, u16 vsi_handle, u32 agg_id,
2298 struct ice_sched_node *vsi_node, *agg_node, *tc_node, *parent;
2299 u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2300 u32 first_node_teid, vsi_teid;
2301 u16 num_nodes_added;
2305 tc_node = ice_sched_get_tc_node(pi, tc);
2309 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2313 vsi_node = ice_sched_get_vsi_node(pi, tc_node, vsi_handle);
2317 /* Is this VSI already part of given aggregator? */
2318 if (ice_sched_find_node_in_subtree(pi->hw, agg_node, vsi_node))
2321 aggl = ice_sched_get_agg_layer(pi->hw);
2322 vsil = ice_sched_get_vsi_layer(pi->hw);
2324 /* set intermediate node count to 1 between aggregator and VSI layers */
2325 for (i = aggl + 1; i < vsil; i++)
2328 /* Check if the aggregator subtree has any free node to add the VSI */
2329 for (i = 0; i < agg_node->num_children; i++) {
2330 parent = ice_sched_get_free_vsi_parent(pi->hw,
2331 agg_node->children[i],
2339 for (i = aggl + 1; i < vsil; i++) {
2340 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2344 if (status || num_nodes[i] != num_nodes_added)
2347 /* The newly added node can be a new parent for the next
2350 if (num_nodes_added)
2351 parent = ice_sched_find_node_by_teid(tc_node,
2354 parent = parent->children[0];
2361 vsi_teid = le32_to_cpu(vsi_node->info.node_teid);
2362 return ice_sched_move_nodes(pi, parent, 1, &vsi_teid);
2366 * ice_move_all_vsi_to_dflt_agg - move all VSI(s) to default aggregator
2367 * @pi: port information structure
2368 * @agg_info: aggregator info
2369 * @tc: traffic class number
2370 * @rm_vsi_info: true or false
2372 * This function move all the VSI(s) to the default aggregator and delete
2373 * aggregator VSI info based on passed in boolean parameter rm_vsi_info. The
2374 * caller holds the scheduler lock.
2377 ice_move_all_vsi_to_dflt_agg(struct ice_port_info *pi,
2378 struct ice_sched_agg_info *agg_info, u8 tc,
2381 struct ice_sched_agg_vsi_info *agg_vsi_info;
2382 struct ice_sched_agg_vsi_info *tmp;
2385 list_for_each_entry_safe(agg_vsi_info, tmp, &agg_info->agg_vsi_list,
2387 u16 vsi_handle = agg_vsi_info->vsi_handle;
2389 /* Move VSI to default aggregator */
2390 if (!ice_is_tc_ena(agg_vsi_info->tc_bitmap[0], tc))
2393 status = ice_sched_move_vsi_to_agg(pi, vsi_handle,
2394 ICE_DFLT_AGG_ID, tc);
2398 clear_bit(tc, agg_vsi_info->tc_bitmap);
2399 if (rm_vsi_info && !agg_vsi_info->tc_bitmap[0]) {
2400 list_del(&agg_vsi_info->list_entry);
2401 devm_kfree(ice_hw_to_dev(pi->hw), agg_vsi_info);
2409 * ice_sched_is_agg_inuse - check whether the aggregator is in use or not
2410 * @pi: port information structure
2411 * @node: node pointer
2413 * This function checks whether the aggregator is attached with any VSI or not.
2416 ice_sched_is_agg_inuse(struct ice_port_info *pi, struct ice_sched_node *node)
2420 vsil = ice_sched_get_vsi_layer(pi->hw);
2421 if (node->tx_sched_layer < vsil - 1) {
2422 for (i = 0; i < node->num_children; i++)
2423 if (ice_sched_is_agg_inuse(pi, node->children[i]))
2427 return node->num_children ? true : false;
2432 * ice_sched_rm_agg_cfg - remove the aggregator node
2433 * @pi: port information structure
2434 * @agg_id: aggregator ID
2437 * This function removes the aggregator node and intermediate nodes if any
2441 ice_sched_rm_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2443 struct ice_sched_node *tc_node, *agg_node;
2444 struct ice_hw *hw = pi->hw;
2446 tc_node = ice_sched_get_tc_node(pi, tc);
2450 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2454 /* Can't remove the aggregator node if it has children */
2455 if (ice_sched_is_agg_inuse(pi, agg_node))
2458 /* need to remove the whole subtree if aggregator node is the
2461 while (agg_node->tx_sched_layer > hw->sw_entry_point_layer) {
2462 struct ice_sched_node *parent = agg_node->parent;
2467 if (parent->num_children > 1)
2473 ice_free_sched_node(pi, agg_node);
2478 * ice_rm_agg_cfg_tc - remove aggregator configuration for TC
2479 * @pi: port information structure
2480 * @agg_info: aggregator ID
2482 * @rm_vsi_info: bool value true or false
2484 * This function removes aggregator reference to VSI of given TC. It removes
2485 * the aggregator configuration completely for requested TC. The caller needs
2486 * to hold the scheduler lock.
2489 ice_rm_agg_cfg_tc(struct ice_port_info *pi, struct ice_sched_agg_info *agg_info,
2490 u8 tc, bool rm_vsi_info)
2494 /* If nothing to remove - return success */
2495 if (!ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2496 goto exit_rm_agg_cfg_tc;
2498 status = ice_move_all_vsi_to_dflt_agg(pi, agg_info, tc, rm_vsi_info);
2500 goto exit_rm_agg_cfg_tc;
2502 /* Delete aggregator node(s) */
2503 status = ice_sched_rm_agg_cfg(pi, agg_info->agg_id, tc);
2505 goto exit_rm_agg_cfg_tc;
2507 clear_bit(tc, agg_info->tc_bitmap);
2513 * ice_save_agg_tc_bitmap - save aggregator TC bitmap
2514 * @pi: port information structure
2515 * @agg_id: aggregator ID
2516 * @tc_bitmap: 8 bits TC bitmap
2518 * Save aggregator TC bitmap. This function needs to be called with scheduler
2522 ice_save_agg_tc_bitmap(struct ice_port_info *pi, u32 agg_id,
2523 unsigned long *tc_bitmap)
2525 struct ice_sched_agg_info *agg_info;
2527 agg_info = ice_get_agg_info(pi->hw, agg_id);
2530 bitmap_copy(agg_info->replay_tc_bitmap, tc_bitmap,
2531 ICE_MAX_TRAFFIC_CLASS);
2536 * ice_sched_add_agg_cfg - create an aggregator node
2537 * @pi: port information structure
2538 * @agg_id: aggregator ID
2541 * This function creates an aggregator node and intermediate nodes if required
2545 ice_sched_add_agg_cfg(struct ice_port_info *pi, u32 agg_id, u8 tc)
2547 struct ice_sched_node *parent, *agg_node, *tc_node;
2548 u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
2549 struct ice_hw *hw = pi->hw;
2550 u32 first_node_teid;
2551 u16 num_nodes_added;
2555 tc_node = ice_sched_get_tc_node(pi, tc);
2559 agg_node = ice_sched_get_agg_node(pi, tc_node, agg_id);
2560 /* Does Agg node already exist ? */
2564 aggl = ice_sched_get_agg_layer(hw);
2566 /* need one node in Agg layer */
2567 num_nodes[aggl] = 1;
2569 /* Check whether the intermediate nodes have space to add the
2570 * new aggregator. If they are full, then SW needs to allocate a new
2571 * intermediate node on those layers
2573 for (i = hw->sw_entry_point_layer; i < aggl; i++) {
2574 parent = ice_sched_get_first_node(pi, tc_node, i);
2576 /* scan all the siblings */
2578 if (parent->num_children < hw->max_children[i])
2580 parent = parent->sibling;
2583 /* all the nodes are full, reserve one for this layer */
2588 /* add the aggregator node */
2590 for (i = hw->sw_entry_point_layer; i <= aggl; i++) {
2594 status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
2598 if (status || num_nodes[i] != num_nodes_added)
2601 /* The newly added node can be a new parent for the next
2604 if (num_nodes_added) {
2605 parent = ice_sched_find_node_by_teid(tc_node,
2607 /* register aggregator ID with the aggregator node */
2608 if (parent && i == aggl)
2609 parent->agg_id = agg_id;
2611 parent = parent->children[0];
2619 * ice_sched_cfg_agg - configure aggregator node
2620 * @pi: port information structure
2621 * @agg_id: aggregator ID
2622 * @agg_type: aggregator type queue, VSI, or aggregator group
2623 * @tc_bitmap: bits TC bitmap
2625 * It registers a unique aggregator node into scheduler services. It
2626 * allows a user to register with a unique ID to track it's resources.
2627 * The aggregator type determines if this is a queue group, VSI group
2628 * or aggregator group. It then creates the aggregator node(s) for requested
2629 * TC(s) or removes an existing aggregator node including its configuration
2630 * if indicated via tc_bitmap. Call ice_rm_agg_cfg to release aggregator
2631 * resources and remove aggregator ID.
2632 * This function needs to be called with scheduler lock held.
2635 ice_sched_cfg_agg(struct ice_port_info *pi, u32 agg_id,
2636 enum ice_agg_type agg_type, unsigned long *tc_bitmap)
2638 struct ice_sched_agg_info *agg_info;
2639 struct ice_hw *hw = pi->hw;
2643 agg_info = ice_get_agg_info(hw, agg_id);
2645 /* Create new entry for new aggregator ID */
2646 agg_info = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*agg_info),
2651 agg_info->agg_id = agg_id;
2652 agg_info->agg_type = agg_type;
2653 agg_info->tc_bitmap[0] = 0;
2655 /* Initialize the aggregator VSI list head */
2656 INIT_LIST_HEAD(&agg_info->agg_vsi_list);
2658 /* Add new entry in aggregator list */
2659 list_add(&agg_info->list_entry, &hw->agg_list);
2661 /* Create aggregator node(s) for requested TC(s) */
2662 ice_for_each_traffic_class(tc) {
2663 if (!ice_is_tc_ena(*tc_bitmap, tc)) {
2664 /* Delete aggregator cfg TC if it exists previously */
2665 status = ice_rm_agg_cfg_tc(pi, agg_info, tc, false);
2671 /* Check if aggregator node for TC already exists */
2672 if (ice_is_tc_ena(agg_info->tc_bitmap[0], tc))
2675 /* Create new aggregator node for TC */
2676 status = ice_sched_add_agg_cfg(pi, agg_id, tc);
2680 /* Save aggregator node's TC information */
2681 set_bit(tc, agg_info->tc_bitmap);
2688 * ice_cfg_agg - config aggregator node
2689 * @pi: port information structure
2690 * @agg_id: aggregator ID
2691 * @agg_type: aggregator type queue, VSI, or aggregator group
2692 * @tc_bitmap: bits TC bitmap
2694 * This function configures aggregator node(s).
2697 ice_cfg_agg(struct ice_port_info *pi, u32 agg_id, enum ice_agg_type agg_type,
2700 unsigned long bitmap = tc_bitmap;
2703 mutex_lock(&pi->sched_lock);
2704 status = ice_sched_cfg_agg(pi, agg_id, agg_type, &bitmap);
2706 status = ice_save_agg_tc_bitmap(pi, agg_id, &bitmap);
2707 mutex_unlock(&pi->sched_lock);
2712 * ice_get_agg_vsi_info - get the aggregator ID
2713 * @agg_info: aggregator info
2714 * @vsi_handle: software VSI handle
2716 * The function returns aggregator VSI info based on VSI handle. This function
2717 * needs to be called with scheduler lock held.
2719 static struct ice_sched_agg_vsi_info *
2720 ice_get_agg_vsi_info(struct ice_sched_agg_info *agg_info, u16 vsi_handle)
2722 struct ice_sched_agg_vsi_info *agg_vsi_info;
2724 list_for_each_entry(agg_vsi_info, &agg_info->agg_vsi_list, list_entry)
2725 if (agg_vsi_info->vsi_handle == vsi_handle)
2726 return agg_vsi_info;
2732 * ice_get_vsi_agg_info - get the aggregator info of VSI
2733 * @hw: pointer to the hardware structure
2734 * @vsi_handle: Sw VSI handle
2736 * The function returns aggregator info of VSI represented via vsi_handle. The
2737 * VSI has in this case a different aggregator than the default one. This
2738 * function needs to be called with scheduler lock held.
2740 static struct ice_sched_agg_info *
2741 ice_get_vsi_agg_info(struct ice_hw *hw, u16 vsi_handle)
2743 struct ice_sched_agg_info *agg_info;
2745 list_for_each_entry(agg_info, &hw->agg_list, list_entry) {
2746 struct ice_sched_agg_vsi_info *agg_vsi_info;
2748 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2756 * ice_save_agg_vsi_tc_bitmap - save aggregator VSI TC bitmap
2757 * @pi: port information structure
2758 * @agg_id: aggregator ID
2759 * @vsi_handle: software VSI handle
2760 * @tc_bitmap: TC bitmap of enabled TC(s)
2762 * Save VSI to aggregator TC bitmap. This function needs to call with scheduler
2766 ice_save_agg_vsi_tc_bitmap(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2767 unsigned long *tc_bitmap)
2769 struct ice_sched_agg_vsi_info *agg_vsi_info;
2770 struct ice_sched_agg_info *agg_info;
2772 agg_info = ice_get_agg_info(pi->hw, agg_id);
2775 /* check if entry already exist */
2776 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2779 bitmap_copy(agg_vsi_info->replay_tc_bitmap, tc_bitmap,
2780 ICE_MAX_TRAFFIC_CLASS);
2785 * ice_sched_assoc_vsi_to_agg - associate/move VSI to new/default aggregator
2786 * @pi: port information structure
2787 * @agg_id: aggregator ID
2788 * @vsi_handle: software VSI handle
2789 * @tc_bitmap: TC bitmap of enabled TC(s)
2791 * This function moves VSI to a new or default aggregator node. If VSI is
2792 * already associated to the aggregator node then no operation is performed on
2793 * the tree. This function needs to be called with scheduler lock held.
2796 ice_sched_assoc_vsi_to_agg(struct ice_port_info *pi, u32 agg_id,
2797 u16 vsi_handle, unsigned long *tc_bitmap)
2799 struct ice_sched_agg_vsi_info *agg_vsi_info, *iter, *old_agg_vsi_info = NULL;
2800 struct ice_sched_agg_info *agg_info, *old_agg_info;
2801 struct ice_hw *hw = pi->hw;
2805 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2807 agg_info = ice_get_agg_info(hw, agg_id);
2810 /* If the VSI is already part of another aggregator then update
2813 old_agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
2814 if (old_agg_info && old_agg_info != agg_info) {
2815 struct ice_sched_agg_vsi_info *vtmp;
2817 list_for_each_entry_safe(iter, vtmp,
2818 &old_agg_info->agg_vsi_list,
2820 if (iter->vsi_handle == vsi_handle) {
2821 old_agg_vsi_info = iter;
2826 /* check if entry already exist */
2827 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
2828 if (!agg_vsi_info) {
2829 /* Create new entry for VSI under aggregator list */
2830 agg_vsi_info = devm_kzalloc(ice_hw_to_dev(hw),
2831 sizeof(*agg_vsi_info), GFP_KERNEL);
2835 /* add VSI ID into the aggregator list */
2836 agg_vsi_info->vsi_handle = vsi_handle;
2837 list_add(&agg_vsi_info->list_entry, &agg_info->agg_vsi_list);
2839 /* Move VSI node to new aggregator node for requested TC(s) */
2840 ice_for_each_traffic_class(tc) {
2841 if (!ice_is_tc_ena(*tc_bitmap, tc))
2844 /* Move VSI to new aggregator */
2845 status = ice_sched_move_vsi_to_agg(pi, vsi_handle, agg_id, tc);
2849 set_bit(tc, agg_vsi_info->tc_bitmap);
2850 if (old_agg_vsi_info)
2851 clear_bit(tc, old_agg_vsi_info->tc_bitmap);
2853 if (old_agg_vsi_info && !old_agg_vsi_info->tc_bitmap[0]) {
2854 list_del(&old_agg_vsi_info->list_entry);
2855 devm_kfree(ice_hw_to_dev(pi->hw), old_agg_vsi_info);
2861 * ice_sched_rm_unused_rl_prof - remove unused RL profile
2862 * @pi: port information structure
2864 * This function removes unused rate limit profiles from the HW and
2865 * SW DB. The caller needs to hold scheduler lock.
2867 static void ice_sched_rm_unused_rl_prof(struct ice_port_info *pi)
2871 for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
2872 struct ice_aqc_rl_profile_info *rl_prof_elem;
2873 struct ice_aqc_rl_profile_info *rl_prof_tmp;
2875 list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
2876 &pi->rl_prof_list[ln], list_entry) {
2877 if (!ice_sched_del_rl_profile(pi->hw, rl_prof_elem))
2878 ice_debug(pi->hw, ICE_DBG_SCHED, "Removed rl profile\n");
2884 * ice_sched_update_elem - update element
2885 * @hw: pointer to the HW struct
2886 * @node: pointer to node
2887 * @info: node info to update
2889 * Update the HW DB, and local SW DB of node. Update the scheduling
2890 * parameters of node from argument info data buffer (Info->data buf) and
2891 * returns success or error on config sched element failure. The caller
2892 * needs to hold scheduler lock.
2895 ice_sched_update_elem(struct ice_hw *hw, struct ice_sched_node *node,
2896 struct ice_aqc_txsched_elem_data *info)
2898 struct ice_aqc_txsched_elem_data buf;
2904 /* Parent TEID is reserved field in this aq call */
2905 buf.parent_teid = 0;
2906 /* Element type is reserved field in this aq call */
2907 buf.data.elem_type = 0;
2908 /* Flags is reserved field in this aq call */
2912 /* Configure element node */
2913 status = ice_aq_cfg_sched_elems(hw, num_elems, &buf, sizeof(buf),
2915 if (status || elem_cfgd != num_elems) {
2916 ice_debug(hw, ICE_DBG_SCHED, "Config sched elem error\n");
2920 /* Config success case */
2921 /* Now update local SW DB */
2922 /* Only copy the data portion of info buffer */
2923 node->info.data = info->data;
2928 * ice_sched_cfg_node_bw_alloc - configure node BW weight/alloc params
2929 * @hw: pointer to the HW struct
2930 * @node: sched node to configure
2931 * @rl_type: rate limit type CIR, EIR, or shared
2932 * @bw_alloc: BW weight/allocation
2934 * This function configures node element's BW allocation.
2937 ice_sched_cfg_node_bw_alloc(struct ice_hw *hw, struct ice_sched_node *node,
2938 enum ice_rl_type rl_type, u16 bw_alloc)
2940 struct ice_aqc_txsched_elem_data buf;
2941 struct ice_aqc_txsched_elem *data;
2945 if (rl_type == ICE_MIN_BW) {
2946 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
2947 data->cir_bw.bw_alloc = cpu_to_le16(bw_alloc);
2948 } else if (rl_type == ICE_MAX_BW) {
2949 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
2950 data->eir_bw.bw_alloc = cpu_to_le16(bw_alloc);
2955 /* Configure element */
2956 return ice_sched_update_elem(hw, node, &buf);
2960 * ice_move_vsi_to_agg - moves VSI to new or default aggregator
2961 * @pi: port information structure
2962 * @agg_id: aggregator ID
2963 * @vsi_handle: software VSI handle
2964 * @tc_bitmap: TC bitmap of enabled TC(s)
2966 * Move or associate VSI to a new or default aggregator node.
2969 ice_move_vsi_to_agg(struct ice_port_info *pi, u32 agg_id, u16 vsi_handle,
2972 unsigned long bitmap = tc_bitmap;
2975 mutex_lock(&pi->sched_lock);
2976 status = ice_sched_assoc_vsi_to_agg(pi, agg_id, vsi_handle,
2977 (unsigned long *)&bitmap);
2979 status = ice_save_agg_vsi_tc_bitmap(pi, agg_id, vsi_handle,
2980 (unsigned long *)&bitmap);
2981 mutex_unlock(&pi->sched_lock);
2986 * ice_set_clear_cir_bw - set or clear CIR BW
2987 * @bw_t_info: bandwidth type information structure
2988 * @bw: bandwidth in Kbps - Kilo bits per sec
2990 * Save or clear CIR bandwidth (BW) in the passed param bw_t_info.
2992 static void ice_set_clear_cir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
2994 if (bw == ICE_SCHED_DFLT_BW) {
2995 clear_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
2996 bw_t_info->cir_bw.bw = 0;
2998 /* Save type of BW information */
2999 set_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
3000 bw_t_info->cir_bw.bw = bw;
3005 * ice_set_clear_eir_bw - set or clear EIR BW
3006 * @bw_t_info: bandwidth type information structure
3007 * @bw: bandwidth in Kbps - Kilo bits per sec
3009 * Save or clear EIR bandwidth (BW) in the passed param bw_t_info.
3011 static void ice_set_clear_eir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3013 if (bw == ICE_SCHED_DFLT_BW) {
3014 clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3015 bw_t_info->eir_bw.bw = 0;
3017 /* EIR BW and Shared BW profiles are mutually exclusive and
3018 * hence only one of them may be set for any given element.
3019 * First clear earlier saved shared BW information.
3021 clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3022 bw_t_info->shared_bw = 0;
3023 /* save EIR BW information */
3024 set_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3025 bw_t_info->eir_bw.bw = bw;
3030 * ice_set_clear_shared_bw - set or clear shared BW
3031 * @bw_t_info: bandwidth type information structure
3032 * @bw: bandwidth in Kbps - Kilo bits per sec
3034 * Save or clear shared bandwidth (BW) in the passed param bw_t_info.
3036 static void ice_set_clear_shared_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
3038 if (bw == ICE_SCHED_DFLT_BW) {
3039 clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3040 bw_t_info->shared_bw = 0;
3042 /* EIR BW and Shared BW profiles are mutually exclusive and
3043 * hence only one of them may be set for any given element.
3044 * First clear earlier saved EIR BW information.
3046 clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
3047 bw_t_info->eir_bw.bw = 0;
3048 /* save shared BW information */
3049 set_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
3050 bw_t_info->shared_bw = bw;
3055 * ice_sched_save_vsi_bw - save VSI node's BW information
3056 * @pi: port information structure
3057 * @vsi_handle: sw VSI handle
3058 * @tc: traffic class
3059 * @rl_type: rate limit type min, max, or shared
3060 * @bw: bandwidth in Kbps - Kilo bits per sec
3062 * Save BW information of VSI type node for post replay use.
3065 ice_sched_save_vsi_bw(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3066 enum ice_rl_type rl_type, u32 bw)
3068 struct ice_vsi_ctx *vsi_ctx;
3070 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3072 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3077 ice_set_clear_cir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3080 ice_set_clear_eir_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3083 ice_set_clear_shared_bw(&vsi_ctx->sched.bw_t_info[tc], bw);
3092 * ice_sched_calc_wakeup - calculate RL profile wakeup parameter
3093 * @hw: pointer to the HW struct
3094 * @bw: bandwidth in Kbps
3096 * This function calculates the wakeup parameter of RL profile.
3098 static u16 ice_sched_calc_wakeup(struct ice_hw *hw, s32 bw)
3100 s64 bytes_per_sec, wakeup_int, wakeup_a, wakeup_b, wakeup_f;
3104 /* Get the wakeup integer value */
3105 bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
3106 wakeup_int = div64_long(hw->psm_clk_freq, bytes_per_sec);
3107 if (wakeup_int > 63) {
3108 wakeup = (u16)((1 << 15) | wakeup_int);
3110 /* Calculate fraction value up to 4 decimals
3111 * Convert Integer value to a constant multiplier
3113 wakeup_b = (s64)ICE_RL_PROF_MULTIPLIER * wakeup_int;
3114 wakeup_a = div64_long((s64)ICE_RL_PROF_MULTIPLIER *
3115 hw->psm_clk_freq, bytes_per_sec);
3117 /* Get Fraction value */
3118 wakeup_f = wakeup_a - wakeup_b;
3120 /* Round up the Fractional value via Ceil(Fractional value) */
3121 if (wakeup_f > div64_long(ICE_RL_PROF_MULTIPLIER, 2))
3124 wakeup_f_int = (s32)div64_long(wakeup_f * ICE_RL_PROF_FRACTION,
3125 ICE_RL_PROF_MULTIPLIER);
3126 wakeup |= (u16)(wakeup_int << 9);
3127 wakeup |= (u16)(0x1ff & wakeup_f_int);
3134 * ice_sched_bw_to_rl_profile - convert BW to profile parameters
3135 * @hw: pointer to the HW struct
3136 * @bw: bandwidth in Kbps
3137 * @profile: profile parameters to return
3139 * This function converts the BW to profile structure format.
3142 ice_sched_bw_to_rl_profile(struct ice_hw *hw, u32 bw,
3143 struct ice_aqc_rl_profile_elem *profile)
3145 s64 bytes_per_sec, ts_rate, mv_tmp;
3146 int status = -EINVAL;
3152 /* Bw settings range is from 0.5Mb/sec to 100Gb/sec */
3153 if (bw < ICE_SCHED_MIN_BW || bw > ICE_SCHED_MAX_BW)
3156 /* Bytes per second from Kbps */
3157 bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
3159 /* encode is 6 bits but really useful are 5 bits */
3160 for (i = 0; i < 64; i++) {
3161 u64 pow_result = BIT_ULL(i);
3163 ts_rate = div64_long((s64)hw->psm_clk_freq,
3164 pow_result * ICE_RL_PROF_TS_MULTIPLIER);
3168 /* Multiplier value */
3169 mv_tmp = div64_long(bytes_per_sec * ICE_RL_PROF_MULTIPLIER,
3172 /* Round to the nearest ICE_RL_PROF_MULTIPLIER */
3173 mv = round_up_64bit(mv_tmp, ICE_RL_PROF_MULTIPLIER);
3175 /* First multiplier value greater than the given
3178 if (mv > ICE_RL_PROF_ACCURACY_BYTES) {
3187 wm = ice_sched_calc_wakeup(hw, bw);
3188 profile->rl_multiply = cpu_to_le16(mv);
3189 profile->wake_up_calc = cpu_to_le16(wm);
3190 profile->rl_encode = cpu_to_le16(encode);
3200 * ice_sched_add_rl_profile - add RL profile
3201 * @pi: port information structure
3202 * @rl_type: type of rate limit BW - min, max, or shared
3203 * @bw: bandwidth in Kbps - Kilo bits per sec
3204 * @layer_num: specifies in which layer to create profile
3206 * This function first checks the existing list for corresponding BW
3207 * parameter. If it exists, it returns the associated profile otherwise
3208 * it creates a new rate limit profile for requested BW, and adds it to
3209 * the HW DB and local list. It returns the new profile or null on error.
3210 * The caller needs to hold the scheduler lock.
3212 static struct ice_aqc_rl_profile_info *
3213 ice_sched_add_rl_profile(struct ice_port_info *pi,
3214 enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3216 struct ice_aqc_rl_profile_info *rl_prof_elem;
3217 u16 profiles_added = 0, num_profiles = 1;
3218 struct ice_aqc_rl_profile_elem *buf;
3223 if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
3227 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3230 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3233 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3242 list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
3244 if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
3245 profile_type && rl_prof_elem->bw == bw)
3246 /* Return existing profile ID info */
3247 return rl_prof_elem;
3249 /* Create new profile ID */
3250 rl_prof_elem = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*rl_prof_elem),
3256 status = ice_sched_bw_to_rl_profile(hw, bw, &rl_prof_elem->profile);
3258 goto exit_add_rl_prof;
3260 rl_prof_elem->bw = bw;
3261 /* layer_num is zero relative, and fw expects level from 1 to 9 */
3262 rl_prof_elem->profile.level = layer_num + 1;
3263 rl_prof_elem->profile.flags = profile_type;
3264 rl_prof_elem->profile.max_burst_size = cpu_to_le16(hw->max_burst_size);
3266 /* Create new entry in HW DB */
3267 buf = &rl_prof_elem->profile;
3268 status = ice_aq_add_rl_profile(hw, num_profiles, buf, sizeof(*buf),
3269 &profiles_added, NULL);
3270 if (status || profiles_added != num_profiles)
3271 goto exit_add_rl_prof;
3273 /* Good entry - add in the list */
3274 rl_prof_elem->prof_id_ref = 0;
3275 list_add(&rl_prof_elem->list_entry, &pi->rl_prof_list[layer_num]);
3276 return rl_prof_elem;
3279 devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
3284 * ice_sched_cfg_node_bw_lmt - configure node sched params
3285 * @hw: pointer to the HW struct
3286 * @node: sched node to configure
3287 * @rl_type: rate limit type CIR, EIR, or shared
3288 * @rl_prof_id: rate limit profile ID
3290 * This function configures node element's BW limit.
3293 ice_sched_cfg_node_bw_lmt(struct ice_hw *hw, struct ice_sched_node *node,
3294 enum ice_rl_type rl_type, u16 rl_prof_id)
3296 struct ice_aqc_txsched_elem_data buf;
3297 struct ice_aqc_txsched_elem *data;
3303 data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
3304 data->cir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
3307 /* EIR BW and Shared BW profiles are mutually exclusive and
3308 * hence only one of them may be set for any given element
3310 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3312 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3313 data->eir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
3316 /* Check for removing shared BW */
3317 if (rl_prof_id == ICE_SCHED_NO_SHARED_RL_PROF_ID) {
3318 /* remove shared profile */
3319 data->valid_sections &= ~ICE_AQC_ELEM_VALID_SHARED;
3320 data->srl_id = 0; /* clear SRL field */
3322 /* enable back EIR to default profile */
3323 data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
3324 data->eir_bw.bw_profile_idx =
3325 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
3328 /* EIR BW and Shared BW profiles are mutually exclusive and
3329 * hence only one of them may be set for any given element
3331 if ((data->valid_sections & ICE_AQC_ELEM_VALID_EIR) &&
3332 (le16_to_cpu(data->eir_bw.bw_profile_idx) !=
3333 ICE_SCHED_DFLT_RL_PROF_ID))
3335 /* EIR BW is set to default, disable it */
3336 data->valid_sections &= ~ICE_AQC_ELEM_VALID_EIR;
3337 /* Okay to enable shared BW now */
3338 data->valid_sections |= ICE_AQC_ELEM_VALID_SHARED;
3339 data->srl_id = cpu_to_le16(rl_prof_id);
3342 /* Unknown rate limit type */
3346 /* Configure element */
3347 return ice_sched_update_elem(hw, node, &buf);
3351 * ice_sched_get_node_rl_prof_id - get node's rate limit profile ID
3353 * @rl_type: rate limit type
3355 * If existing profile matches, it returns the corresponding rate
3356 * limit profile ID, otherwise it returns an invalid ID as error.
3359 ice_sched_get_node_rl_prof_id(struct ice_sched_node *node,
3360 enum ice_rl_type rl_type)
3362 u16 rl_prof_id = ICE_SCHED_INVAL_PROF_ID;
3363 struct ice_aqc_txsched_elem *data;
3365 data = &node->info.data;
3368 if (data->valid_sections & ICE_AQC_ELEM_VALID_CIR)
3369 rl_prof_id = le16_to_cpu(data->cir_bw.bw_profile_idx);
3372 if (data->valid_sections & ICE_AQC_ELEM_VALID_EIR)
3373 rl_prof_id = le16_to_cpu(data->eir_bw.bw_profile_idx);
3376 if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
3377 rl_prof_id = le16_to_cpu(data->srl_id);
3387 * ice_sched_get_rl_prof_layer - selects rate limit profile creation layer
3388 * @pi: port information structure
3389 * @rl_type: type of rate limit BW - min, max, or shared
3390 * @layer_index: layer index
3392 * This function returns requested profile creation layer.
3395 ice_sched_get_rl_prof_layer(struct ice_port_info *pi, enum ice_rl_type rl_type,
3398 struct ice_hw *hw = pi->hw;
3400 if (layer_index >= hw->num_tx_sched_layers)
3401 return ICE_SCHED_INVAL_LAYER_NUM;
3404 if (hw->layer_info[layer_index].max_cir_rl_profiles)
3408 if (hw->layer_info[layer_index].max_eir_rl_profiles)
3412 /* if current layer doesn't support SRL profile creation
3413 * then try a layer up or down.
3415 if (hw->layer_info[layer_index].max_srl_profiles)
3417 else if (layer_index < hw->num_tx_sched_layers - 1 &&
3418 hw->layer_info[layer_index + 1].max_srl_profiles)
3419 return layer_index + 1;
3420 else if (layer_index > 0 &&
3421 hw->layer_info[layer_index - 1].max_srl_profiles)
3422 return layer_index - 1;
3427 return ICE_SCHED_INVAL_LAYER_NUM;
3431 * ice_sched_get_srl_node - get shared rate limit node
3433 * @srl_layer: shared rate limit layer
3435 * This function returns SRL node to be used for shared rate limit purpose.
3436 * The caller needs to hold scheduler lock.
3438 static struct ice_sched_node *
3439 ice_sched_get_srl_node(struct ice_sched_node *node, u8 srl_layer)
3441 if (srl_layer > node->tx_sched_layer)
3442 return node->children[0];
3443 else if (srl_layer < node->tx_sched_layer)
3444 /* Node can't be created without a parent. It will always
3445 * have a valid parent except root node.
3447 return node->parent;
3453 * ice_sched_rm_rl_profile - remove RL profile ID
3454 * @pi: port information structure
3455 * @layer_num: layer number where profiles are saved
3456 * @profile_type: profile type like EIR, CIR, or SRL
3457 * @profile_id: profile ID to remove
3459 * This function removes rate limit profile from layer 'layer_num' of type
3460 * 'profile_type' and profile ID as 'profile_id'. The caller needs to hold
3464 ice_sched_rm_rl_profile(struct ice_port_info *pi, u8 layer_num, u8 profile_type,
3467 struct ice_aqc_rl_profile_info *rl_prof_elem;
3470 if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
3472 /* Check the existing list for RL profile */
3473 list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
3475 if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
3477 le16_to_cpu(rl_prof_elem->profile.profile_id) ==
3479 if (rl_prof_elem->prof_id_ref)
3480 rl_prof_elem->prof_id_ref--;
3482 /* Remove old profile ID from database */
3483 status = ice_sched_del_rl_profile(pi->hw, rl_prof_elem);
3484 if (status && status != -EBUSY)
3485 ice_debug(pi->hw, ICE_DBG_SCHED, "Remove rl profile failed\n");
3488 if (status == -EBUSY)
3494 * ice_sched_set_node_bw_dflt - set node's bandwidth limit to default
3495 * @pi: port information structure
3496 * @node: pointer to node structure
3497 * @rl_type: rate limit type min, max, or shared
3498 * @layer_num: layer number where RL profiles are saved
3500 * This function configures node element's BW rate limit profile ID of
3501 * type CIR, EIR, or SRL to default. This function needs to be called
3502 * with the scheduler lock held.
3505 ice_sched_set_node_bw_dflt(struct ice_port_info *pi,
3506 struct ice_sched_node *node,
3507 enum ice_rl_type rl_type, u8 layer_num)
3518 profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
3519 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
3522 profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
3523 rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
3526 profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
3527 /* No SRL is configured for default case */
3528 rl_prof_id = ICE_SCHED_NO_SHARED_RL_PROF_ID;
3533 /* Save existing RL prof ID for later clean up */
3534 old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
3535 /* Configure BW scheduling parameters */
3536 status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
3540 /* Remove stale RL profile ID */
3541 if (old_id == ICE_SCHED_DFLT_RL_PROF_ID ||
3542 old_id == ICE_SCHED_INVAL_PROF_ID)
3545 return ice_sched_rm_rl_profile(pi, layer_num, profile_type, old_id);
3549 * ice_sched_set_eir_srl_excl - set EIR/SRL exclusiveness
3550 * @pi: port information structure
3551 * @node: pointer to node structure
3552 * @layer_num: layer number where rate limit profiles are saved
3553 * @rl_type: rate limit type min, max, or shared
3554 * @bw: bandwidth value
3556 * This function prepares node element's bandwidth to SRL or EIR exclusively.
3557 * EIR BW and Shared BW profiles are mutually exclusive and hence only one of
3558 * them may be set for any given element. This function needs to be called
3559 * with the scheduler lock held.
3562 ice_sched_set_eir_srl_excl(struct ice_port_info *pi,
3563 struct ice_sched_node *node,
3564 u8 layer_num, enum ice_rl_type rl_type, u32 bw)
3566 if (rl_type == ICE_SHARED_BW) {
3567 /* SRL node passed in this case, it may be different node */
3568 if (bw == ICE_SCHED_DFLT_BW)
3569 /* SRL being removed, ice_sched_cfg_node_bw_lmt()
3570 * enables EIR to default. EIR is not set in this
3571 * case, so no additional action is required.
3575 /* SRL being configured, set EIR to default here.
3576 * ice_sched_cfg_node_bw_lmt() disables EIR when it
3579 return ice_sched_set_node_bw_dflt(pi, node, ICE_MAX_BW,
3581 } else if (rl_type == ICE_MAX_BW &&
3582 node->info.data.valid_sections & ICE_AQC_ELEM_VALID_SHARED) {
3583 /* Remove Shared profile. Set default shared BW call
3584 * removes shared profile for a node.
3586 return ice_sched_set_node_bw_dflt(pi, node,
3594 * ice_sched_set_node_bw - set node's bandwidth
3595 * @pi: port information structure
3597 * @rl_type: rate limit type min, max, or shared
3598 * @bw: bandwidth in Kbps - Kilo bits per sec
3599 * @layer_num: layer number
3601 * This function adds new profile corresponding to requested BW, configures
3602 * node's RL profile ID of type CIR, EIR, or SRL, and removes old profile
3603 * ID from local database. The caller needs to hold scheduler lock.
3606 ice_sched_set_node_bw(struct ice_port_info *pi, struct ice_sched_node *node,
3607 enum ice_rl_type rl_type, u32 bw, u8 layer_num)
3609 struct ice_aqc_rl_profile_info *rl_prof_info;
3610 struct ice_hw *hw = pi->hw;
3611 u16 old_id, rl_prof_id;
3612 int status = -EINVAL;
3614 rl_prof_info = ice_sched_add_rl_profile(pi, rl_type, bw, layer_num);
3618 rl_prof_id = le16_to_cpu(rl_prof_info->profile.profile_id);
3620 /* Save existing RL prof ID for later clean up */
3621 old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
3622 /* Configure BW scheduling parameters */
3623 status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
3627 /* New changes has been applied */
3628 /* Increment the profile ID reference count */
3629 rl_prof_info->prof_id_ref++;
3631 /* Check for old ID removal */
3632 if ((old_id == ICE_SCHED_DFLT_RL_PROF_ID && rl_type != ICE_SHARED_BW) ||
3633 old_id == ICE_SCHED_INVAL_PROF_ID || old_id == rl_prof_id)
3636 return ice_sched_rm_rl_profile(pi, layer_num,
3637 rl_prof_info->profile.flags &
3638 ICE_AQC_RL_PROFILE_TYPE_M, old_id);
3642 * ice_sched_set_node_priority - set node's priority
3643 * @pi: port information structure
3645 * @priority: number 0-7 representing priority among siblings
3647 * This function sets priority of a node among it's siblings.
3650 ice_sched_set_node_priority(struct ice_port_info *pi, struct ice_sched_node *node,
3653 struct ice_aqc_txsched_elem_data buf;
3654 struct ice_aqc_txsched_elem *data;
3659 data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
3660 data->generic |= FIELD_PREP(ICE_AQC_ELEM_GENERIC_PRIO_M, priority);
3662 return ice_sched_update_elem(pi->hw, node, &buf);
3666 * ice_sched_set_node_weight - set node's weight
3667 * @pi: port information structure
3669 * @weight: number 1-200 representing weight for WFQ
3671 * This function sets weight of the node for WFQ algorithm.
3674 ice_sched_set_node_weight(struct ice_port_info *pi, struct ice_sched_node *node, u16 weight)
3676 struct ice_aqc_txsched_elem_data buf;
3677 struct ice_aqc_txsched_elem *data;
3682 data->valid_sections = ICE_AQC_ELEM_VALID_CIR | ICE_AQC_ELEM_VALID_EIR |
3683 ICE_AQC_ELEM_VALID_GENERIC;
3684 data->cir_bw.bw_alloc = cpu_to_le16(weight);
3685 data->eir_bw.bw_alloc = cpu_to_le16(weight);
3687 data->generic |= FIELD_PREP(ICE_AQC_ELEM_GENERIC_SP_M, 0x0);
3689 return ice_sched_update_elem(pi->hw, node, &buf);
3693 * ice_sched_set_node_bw_lmt - set node's BW limit
3694 * @pi: port information structure
3696 * @rl_type: rate limit type min, max, or shared
3697 * @bw: bandwidth in Kbps - Kilo bits per sec
3699 * It updates node's BW limit parameters like BW RL profile ID of type CIR,
3700 * EIR, or SRL. The caller needs to hold scheduler lock.
3703 ice_sched_set_node_bw_lmt(struct ice_port_info *pi, struct ice_sched_node *node,
3704 enum ice_rl_type rl_type, u32 bw)
3706 struct ice_sched_node *cfg_node = node;
3715 /* Remove unused RL profile IDs from HW and SW DB */
3716 ice_sched_rm_unused_rl_prof(pi);
3717 layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
3718 node->tx_sched_layer);
3719 if (layer_num >= hw->num_tx_sched_layers)
3722 if (rl_type == ICE_SHARED_BW) {
3723 /* SRL node may be different */
3724 cfg_node = ice_sched_get_srl_node(node, layer_num);
3728 /* EIR BW and Shared BW profiles are mutually exclusive and
3729 * hence only one of them may be set for any given element
3731 status = ice_sched_set_eir_srl_excl(pi, cfg_node, layer_num, rl_type,
3735 if (bw == ICE_SCHED_DFLT_BW)
3736 return ice_sched_set_node_bw_dflt(pi, cfg_node, rl_type,
3738 return ice_sched_set_node_bw(pi, cfg_node, rl_type, bw, layer_num);
3742 * ice_sched_set_node_bw_dflt_lmt - set node's BW limit to default
3743 * @pi: port information structure
3744 * @node: pointer to node structure
3745 * @rl_type: rate limit type min, max, or shared
3747 * This function configures node element's BW rate limit profile ID of
3748 * type CIR, EIR, or SRL to default. This function needs to be called
3749 * with the scheduler lock held.
3752 ice_sched_set_node_bw_dflt_lmt(struct ice_port_info *pi,
3753 struct ice_sched_node *node,
3754 enum ice_rl_type rl_type)
3756 return ice_sched_set_node_bw_lmt(pi, node, rl_type,
3761 * ice_sched_validate_srl_node - Check node for SRL applicability
3762 * @node: sched node to configure
3763 * @sel_layer: selected SRL layer
3765 * This function checks if the SRL can be applied to a selected layer node on
3766 * behalf of the requested node (first argument). This function needs to be
3767 * called with scheduler lock held.
3770 ice_sched_validate_srl_node(struct ice_sched_node *node, u8 sel_layer)
3772 /* SRL profiles are not available on all layers. Check if the
3773 * SRL profile can be applied to a node above or below the
3774 * requested node. SRL configuration is possible only if the
3775 * selected layer's node has single child.
3777 if (sel_layer == node->tx_sched_layer ||
3778 ((sel_layer == node->tx_sched_layer + 1) &&
3779 node->num_children == 1) ||
3780 ((sel_layer == node->tx_sched_layer - 1) &&
3781 (node->parent && node->parent->num_children == 1)))
3788 * ice_sched_save_q_bw - save queue node's BW information
3789 * @q_ctx: queue context structure
3790 * @rl_type: rate limit type min, max, or shared
3791 * @bw: bandwidth in Kbps - Kilo bits per sec
3793 * Save BW information of queue type node for post replay use.
3796 ice_sched_save_q_bw(struct ice_q_ctx *q_ctx, enum ice_rl_type rl_type, u32 bw)
3800 ice_set_clear_cir_bw(&q_ctx->bw_t_info, bw);
3803 ice_set_clear_eir_bw(&q_ctx->bw_t_info, bw);
3806 ice_set_clear_shared_bw(&q_ctx->bw_t_info, bw);
3815 * ice_sched_set_q_bw_lmt - sets queue BW limit
3816 * @pi: port information structure
3817 * @vsi_handle: sw VSI handle
3818 * @tc: traffic class
3819 * @q_handle: software queue handle
3820 * @rl_type: min, max, or shared
3821 * @bw: bandwidth in Kbps
3823 * This function sets BW limit of queue scheduling node.
3826 ice_sched_set_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3827 u16 q_handle, enum ice_rl_type rl_type, u32 bw)
3829 struct ice_sched_node *node;
3830 struct ice_q_ctx *q_ctx;
3831 int status = -EINVAL;
3833 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3835 mutex_lock(&pi->sched_lock);
3836 q_ctx = ice_get_lan_q_ctx(pi->hw, vsi_handle, tc, q_handle);
3839 node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
3841 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong q_teid\n");
3845 /* Return error if it is not a leaf node */
3846 if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF)
3849 /* SRL bandwidth layer selection */
3850 if (rl_type == ICE_SHARED_BW) {
3851 u8 sel_layer; /* selected layer */
3853 sel_layer = ice_sched_get_rl_prof_layer(pi, rl_type,
3854 node->tx_sched_layer);
3855 if (sel_layer >= pi->hw->num_tx_sched_layers) {
3859 status = ice_sched_validate_srl_node(node, sel_layer);
3864 if (bw == ICE_SCHED_DFLT_BW)
3865 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
3867 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
3870 status = ice_sched_save_q_bw(q_ctx, rl_type, bw);
3873 mutex_unlock(&pi->sched_lock);
3878 * ice_cfg_q_bw_lmt - configure queue BW limit
3879 * @pi: port information structure
3880 * @vsi_handle: sw VSI handle
3881 * @tc: traffic class
3882 * @q_handle: software queue handle
3883 * @rl_type: min, max, or shared
3884 * @bw: bandwidth in Kbps
3886 * This function configures BW limit of queue scheduling node.
3889 ice_cfg_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3890 u16 q_handle, enum ice_rl_type rl_type, u32 bw)
3892 return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
3897 * ice_cfg_q_bw_dflt_lmt - configure queue BW default limit
3898 * @pi: port information structure
3899 * @vsi_handle: sw VSI handle
3900 * @tc: traffic class
3901 * @q_handle: software queue handle
3902 * @rl_type: min, max, or shared
3904 * This function configures BW default limit of queue scheduling node.
3907 ice_cfg_q_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
3908 u16 q_handle, enum ice_rl_type rl_type)
3910 return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
3915 * ice_sched_get_node_by_id_type - get node from ID type
3916 * @pi: port information structure
3918 * @agg_type: type of aggregator
3919 * @tc: traffic class
3921 * This function returns node identified by ID of type aggregator, and
3922 * based on traffic class (TC). This function needs to be called with
3923 * the scheduler lock held.
3925 static struct ice_sched_node *
3926 ice_sched_get_node_by_id_type(struct ice_port_info *pi, u32 id,
3927 enum ice_agg_type agg_type, u8 tc)
3929 struct ice_sched_node *node = NULL;
3932 case ICE_AGG_TYPE_VSI: {
3933 struct ice_vsi_ctx *vsi_ctx;
3934 u16 vsi_handle = (u16)id;
3936 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3938 /* Get sched_vsi_info */
3939 vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
3942 node = vsi_ctx->sched.vsi_node[tc];
3946 case ICE_AGG_TYPE_AGG: {
3947 struct ice_sched_node *tc_node;
3949 tc_node = ice_sched_get_tc_node(pi, tc);
3951 node = ice_sched_get_agg_node(pi, tc_node, id);
3963 * ice_sched_set_node_bw_lmt_per_tc - set node BW limit per TC
3964 * @pi: port information structure
3965 * @id: ID (software VSI handle or AGG ID)
3966 * @agg_type: aggregator type (VSI or AGG type node)
3967 * @tc: traffic class
3968 * @rl_type: min or max
3969 * @bw: bandwidth in Kbps
3971 * This function sets BW limit of VSI or Aggregator scheduling node
3972 * based on TC information from passed in argument BW.
3975 ice_sched_set_node_bw_lmt_per_tc(struct ice_port_info *pi, u32 id,
3976 enum ice_agg_type agg_type, u8 tc,
3977 enum ice_rl_type rl_type, u32 bw)
3979 struct ice_sched_node *node;
3980 int status = -EINVAL;
3985 if (rl_type == ICE_UNKNOWN_BW)
3988 mutex_lock(&pi->sched_lock);
3989 node = ice_sched_get_node_by_id_type(pi, id, agg_type, tc);
3991 ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong id, agg type, or tc\n");
3992 goto exit_set_node_bw_lmt_per_tc;
3994 if (bw == ICE_SCHED_DFLT_BW)
3995 status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
3997 status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
3999 exit_set_node_bw_lmt_per_tc:
4000 mutex_unlock(&pi->sched_lock);
4005 * ice_cfg_vsi_bw_lmt_per_tc - configure VSI BW limit per TC
4006 * @pi: port information structure
4007 * @vsi_handle: software VSI handle
4008 * @tc: traffic class
4009 * @rl_type: min or max
4010 * @bw: bandwidth in Kbps
4012 * This function configures BW limit of VSI scheduling node based on TC
4016 ice_cfg_vsi_bw_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4017 enum ice_rl_type rl_type, u32 bw)
4021 status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
4025 mutex_lock(&pi->sched_lock);
4026 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type, bw);
4027 mutex_unlock(&pi->sched_lock);
4033 * ice_cfg_vsi_bw_dflt_lmt_per_tc - configure default VSI BW limit per TC
4034 * @pi: port information structure
4035 * @vsi_handle: software VSI handle
4036 * @tc: traffic class
4037 * @rl_type: min or max
4039 * This function configures default BW limit of VSI scheduling node based on TC
4043 ice_cfg_vsi_bw_dflt_lmt_per_tc(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4044 enum ice_rl_type rl_type)
4048 status = ice_sched_set_node_bw_lmt_per_tc(pi, vsi_handle,
4053 mutex_lock(&pi->sched_lock);
4054 status = ice_sched_save_vsi_bw(pi, vsi_handle, tc, rl_type,
4056 mutex_unlock(&pi->sched_lock);
4062 * ice_cfg_rl_burst_size - Set burst size value
4063 * @hw: pointer to the HW struct
4064 * @bytes: burst size in bytes
4066 * This function configures/set the burst size to requested new value. The new
4067 * burst size value is used for future rate limit calls. It doesn't change the
4068 * existing or previously created RL profiles.
4070 int ice_cfg_rl_burst_size(struct ice_hw *hw, u32 bytes)
4072 u16 burst_size_to_prog;
4074 if (bytes < ICE_MIN_BURST_SIZE_ALLOWED ||
4075 bytes > ICE_MAX_BURST_SIZE_ALLOWED)
4077 if (ice_round_to_num(bytes, 64) <=
4078 ICE_MAX_BURST_SIZE_64_BYTE_GRANULARITY) {
4079 /* 64 byte granularity case */
4080 /* Disable MSB granularity bit */
4081 burst_size_to_prog = ICE_64_BYTE_GRANULARITY;
4082 /* round number to nearest 64 byte granularity */
4083 bytes = ice_round_to_num(bytes, 64);
4084 /* The value is in 64 byte chunks */
4085 burst_size_to_prog |= (u16)(bytes / 64);
4087 /* k bytes granularity case */
4088 /* Enable MSB granularity bit */
4089 burst_size_to_prog = ICE_KBYTE_GRANULARITY;
4090 /* round number to nearest 1024 granularity */
4091 bytes = ice_round_to_num(bytes, 1024);
4092 /* check rounding doesn't go beyond allowed */
4093 if (bytes > ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY)
4094 bytes = ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY;
4095 /* The value is in k bytes */
4096 burst_size_to_prog |= (u16)(bytes / 1024);
4098 hw->max_burst_size = burst_size_to_prog;
4103 * ice_sched_replay_node_prio - re-configure node priority
4104 * @hw: pointer to the HW struct
4105 * @node: sched node to configure
4106 * @priority: priority value
4108 * This function configures node element's priority value. It
4109 * needs to be called with scheduler lock held.
4112 ice_sched_replay_node_prio(struct ice_hw *hw, struct ice_sched_node *node,
4115 struct ice_aqc_txsched_elem_data buf;
4116 struct ice_aqc_txsched_elem *data;
4121 data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
4122 data->generic = priority;
4124 /* Configure element */
4125 status = ice_sched_update_elem(hw, node, &buf);
4130 * ice_sched_replay_node_bw - replay node(s) BW
4131 * @hw: pointer to the HW struct
4132 * @node: sched node to configure
4133 * @bw_t_info: BW type information
4135 * This function restores node's BW from bw_t_info. The caller needs
4136 * to hold the scheduler lock.
4139 ice_sched_replay_node_bw(struct ice_hw *hw, struct ice_sched_node *node,
4140 struct ice_bw_type_info *bw_t_info)
4142 struct ice_port_info *pi = hw->port_info;
4143 int status = -EINVAL;
4148 if (bitmap_empty(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CNT))
4150 if (test_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap)) {
4151 status = ice_sched_replay_node_prio(hw, node,
4152 bw_t_info->generic);
4156 if (test_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap)) {
4157 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW,
4158 bw_t_info->cir_bw.bw);
4162 if (test_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap)) {
4163 bw_alloc = bw_t_info->cir_bw.bw_alloc;
4164 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MIN_BW,
4169 if (test_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap)) {
4170 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW,
4171 bw_t_info->eir_bw.bw);
4175 if (test_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap)) {
4176 bw_alloc = bw_t_info->eir_bw.bw_alloc;
4177 status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MAX_BW,
4182 if (test_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap))
4183 status = ice_sched_set_node_bw_lmt(pi, node, ICE_SHARED_BW,
4184 bw_t_info->shared_bw);
4189 * ice_sched_get_ena_tc_bitmap - get enabled TC bitmap
4190 * @pi: port info struct
4191 * @tc_bitmap: 8 bits TC bitmap to check
4192 * @ena_tc_bitmap: 8 bits enabled TC bitmap to return
4194 * This function returns enabled TC bitmap in variable ena_tc_bitmap. Some TCs
4195 * may be missing, it returns enabled TCs. This function needs to be called with
4196 * scheduler lock held.
4199 ice_sched_get_ena_tc_bitmap(struct ice_port_info *pi,
4200 unsigned long *tc_bitmap,
4201 unsigned long *ena_tc_bitmap)
4205 /* Some TC(s) may be missing after reset, adjust for replay */
4206 ice_for_each_traffic_class(tc)
4207 if (ice_is_tc_ena(*tc_bitmap, tc) &&
4208 (ice_sched_get_tc_node(pi, tc)))
4209 set_bit(tc, ena_tc_bitmap);
4213 * ice_sched_replay_agg - recreate aggregator node(s)
4214 * @hw: pointer to the HW struct
4216 * This function recreate aggregator type nodes which are not replayed earlier.
4217 * It also replay aggregator BW information. These aggregator nodes are not
4218 * associated with VSI type node yet.
4220 void ice_sched_replay_agg(struct ice_hw *hw)
4222 struct ice_port_info *pi = hw->port_info;
4223 struct ice_sched_agg_info *agg_info;
4225 mutex_lock(&pi->sched_lock);
4226 list_for_each_entry(agg_info, &hw->agg_list, list_entry)
4227 /* replay aggregator (re-create aggregator node) */
4228 if (!bitmap_equal(agg_info->tc_bitmap, agg_info->replay_tc_bitmap,
4229 ICE_MAX_TRAFFIC_CLASS)) {
4230 DECLARE_BITMAP(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4233 bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4234 ice_sched_get_ena_tc_bitmap(pi,
4235 agg_info->replay_tc_bitmap,
4237 status = ice_sched_cfg_agg(hw->port_info,
4242 dev_info(ice_hw_to_dev(hw),
4243 "Replay agg id[%d] failed\n",
4245 /* Move on to next one */
4249 mutex_unlock(&pi->sched_lock);
4253 * ice_sched_replay_agg_vsi_preinit - Agg/VSI replay pre initialization
4254 * @hw: pointer to the HW struct
4256 * This function initialize aggregator(s) TC bitmap to zero. A required
4257 * preinit step for replaying aggregators.
4259 void ice_sched_replay_agg_vsi_preinit(struct ice_hw *hw)
4261 struct ice_port_info *pi = hw->port_info;
4262 struct ice_sched_agg_info *agg_info;
4264 mutex_lock(&pi->sched_lock);
4265 list_for_each_entry(agg_info, &hw->agg_list, list_entry) {
4266 struct ice_sched_agg_vsi_info *agg_vsi_info;
4268 agg_info->tc_bitmap[0] = 0;
4269 list_for_each_entry(agg_vsi_info, &agg_info->agg_vsi_list,
4271 agg_vsi_info->tc_bitmap[0] = 0;
4273 mutex_unlock(&pi->sched_lock);
4277 * ice_sched_replay_vsi_agg - replay aggregator & VSI to aggregator node(s)
4278 * @hw: pointer to the HW struct
4279 * @vsi_handle: software VSI handle
4281 * This function replays aggregator node, VSI to aggregator type nodes, and
4282 * their node bandwidth information. This function needs to be called with
4283 * scheduler lock held.
4285 static int ice_sched_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
4287 DECLARE_BITMAP(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4288 struct ice_sched_agg_vsi_info *agg_vsi_info;
4289 struct ice_port_info *pi = hw->port_info;
4290 struct ice_sched_agg_info *agg_info;
4293 bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4294 if (!ice_is_vsi_valid(hw, vsi_handle))
4296 agg_info = ice_get_vsi_agg_info(hw, vsi_handle);
4298 return 0; /* Not present in list - default Agg case */
4299 agg_vsi_info = ice_get_agg_vsi_info(agg_info, vsi_handle);
4301 return 0; /* Not present in list - default Agg case */
4302 ice_sched_get_ena_tc_bitmap(pi, agg_info->replay_tc_bitmap,
4304 /* Replay aggregator node associated to vsi_handle */
4305 status = ice_sched_cfg_agg(hw->port_info, agg_info->agg_id,
4306 ICE_AGG_TYPE_AGG, replay_bitmap);
4310 bitmap_zero(replay_bitmap, ICE_MAX_TRAFFIC_CLASS);
4311 ice_sched_get_ena_tc_bitmap(pi, agg_vsi_info->replay_tc_bitmap,
4313 /* Move this VSI (vsi_handle) to above aggregator */
4314 return ice_sched_assoc_vsi_to_agg(pi, agg_info->agg_id, vsi_handle,
4319 * ice_replay_vsi_agg - replay VSI to aggregator node
4320 * @hw: pointer to the HW struct
4321 * @vsi_handle: software VSI handle
4323 * This function replays association of VSI to aggregator type nodes, and
4324 * node bandwidth information.
4326 int ice_replay_vsi_agg(struct ice_hw *hw, u16 vsi_handle)
4328 struct ice_port_info *pi = hw->port_info;
4331 mutex_lock(&pi->sched_lock);
4332 status = ice_sched_replay_vsi_agg(hw, vsi_handle);
4333 mutex_unlock(&pi->sched_lock);
4338 * ice_sched_replay_q_bw - replay queue type node BW
4339 * @pi: port information structure
4340 * @q_ctx: queue context structure
4342 * This function replays queue type node bandwidth. This function needs to be
4343 * called with scheduler lock held.
4345 int ice_sched_replay_q_bw(struct ice_port_info *pi, struct ice_q_ctx *q_ctx)
4347 struct ice_sched_node *q_node;
4349 /* Following also checks the presence of node in tree */
4350 q_node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
4353 return ice_sched_replay_node_bw(pi->hw, q_node, &q_ctx->bw_t_info);