1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright (c) 2018, Intel Corporation. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include "ice_sched.h"
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /**
7*4882a593Smuzhiyun * ice_sched_add_root_node - Insert the Tx scheduler root node in SW DB
8*4882a593Smuzhiyun * @pi: port information structure
9*4882a593Smuzhiyun * @info: Scheduler element information from firmware
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * This function inserts the root node of the scheduling tree topology
12*4882a593Smuzhiyun * to the SW DB.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun static enum ice_status
ice_sched_add_root_node(struct ice_port_info * pi,struct ice_aqc_txsched_elem_data * info)15*4882a593Smuzhiyun ice_sched_add_root_node(struct ice_port_info *pi,
16*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data *info)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun struct ice_sched_node *root;
19*4882a593Smuzhiyun struct ice_hw *hw;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun if (!pi)
22*4882a593Smuzhiyun return ICE_ERR_PARAM;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun hw = pi->hw;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun root = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*root), GFP_KERNEL);
27*4882a593Smuzhiyun if (!root)
28*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* coverity[suspicious_sizeof] */
31*4882a593Smuzhiyun root->children = devm_kcalloc(ice_hw_to_dev(hw), hw->max_children[0],
32*4882a593Smuzhiyun sizeof(*root), GFP_KERNEL);
33*4882a593Smuzhiyun if (!root->children) {
34*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), root);
35*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun memcpy(&root->info, info, sizeof(*info));
39*4882a593Smuzhiyun pi->root = root;
40*4882a593Smuzhiyun return 0;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /**
44*4882a593Smuzhiyun * ice_sched_find_node_by_teid - Find the Tx scheduler node in SW DB
45*4882a593Smuzhiyun * @start_node: pointer to the starting ice_sched_node struct in a sub-tree
46*4882a593Smuzhiyun * @teid: node TEID to search
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * This function searches for a node matching the TEID in the scheduling tree
49*4882a593Smuzhiyun * from the SW DB. The search is recursive and is restricted by the number of
50*4882a593Smuzhiyun * layers it has searched through; stopping at the max supported layer.
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * This function needs to be called when holding the port_info->sched_lock
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun struct ice_sched_node *
ice_sched_find_node_by_teid(struct ice_sched_node * start_node,u32 teid)55*4882a593Smuzhiyun ice_sched_find_node_by_teid(struct ice_sched_node *start_node, u32 teid)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun u16 i;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* The TEID is same as that of the start_node */
60*4882a593Smuzhiyun if (ICE_TXSCHED_GET_NODE_TEID(start_node) == teid)
61*4882a593Smuzhiyun return start_node;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* The node has no children or is at the max layer */
64*4882a593Smuzhiyun if (!start_node->num_children ||
65*4882a593Smuzhiyun start_node->tx_sched_layer >= ICE_AQC_TOPO_MAX_LEVEL_NUM ||
66*4882a593Smuzhiyun start_node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF)
67*4882a593Smuzhiyun return NULL;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* Check if TEID matches to any of the children nodes */
70*4882a593Smuzhiyun for (i = 0; i < start_node->num_children; i++)
71*4882a593Smuzhiyun if (ICE_TXSCHED_GET_NODE_TEID(start_node->children[i]) == teid)
72*4882a593Smuzhiyun return start_node->children[i];
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Search within each child's sub-tree */
75*4882a593Smuzhiyun for (i = 0; i < start_node->num_children; i++) {
76*4882a593Smuzhiyun struct ice_sched_node *tmp;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun tmp = ice_sched_find_node_by_teid(start_node->children[i],
79*4882a593Smuzhiyun teid);
80*4882a593Smuzhiyun if (tmp)
81*4882a593Smuzhiyun return tmp;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun return NULL;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /**
88*4882a593Smuzhiyun * ice_aqc_send_sched_elem_cmd - send scheduling elements cmd
89*4882a593Smuzhiyun * @hw: pointer to the HW struct
90*4882a593Smuzhiyun * @cmd_opc: cmd opcode
91*4882a593Smuzhiyun * @elems_req: number of elements to request
92*4882a593Smuzhiyun * @buf: pointer to buffer
93*4882a593Smuzhiyun * @buf_size: buffer size in bytes
94*4882a593Smuzhiyun * @elems_resp: returns total number of elements response
95*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * This function sends a scheduling elements cmd (cmd_opc)
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun static enum ice_status
ice_aqc_send_sched_elem_cmd(struct ice_hw * hw,enum ice_adminq_opc cmd_opc,u16 elems_req,void * buf,u16 buf_size,u16 * elems_resp,struct ice_sq_cd * cd)100*4882a593Smuzhiyun ice_aqc_send_sched_elem_cmd(struct ice_hw *hw, enum ice_adminq_opc cmd_opc,
101*4882a593Smuzhiyun u16 elems_req, void *buf, u16 buf_size,
102*4882a593Smuzhiyun u16 *elems_resp, struct ice_sq_cd *cd)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun struct ice_aqc_sched_elem_cmd *cmd;
105*4882a593Smuzhiyun struct ice_aq_desc desc;
106*4882a593Smuzhiyun enum ice_status status;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun cmd = &desc.params.sched_elem_cmd;
109*4882a593Smuzhiyun ice_fill_dflt_direct_cmd_desc(&desc, cmd_opc);
110*4882a593Smuzhiyun cmd->num_elem_req = cpu_to_le16(elems_req);
111*4882a593Smuzhiyun desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
112*4882a593Smuzhiyun status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
113*4882a593Smuzhiyun if (!status && elems_resp)
114*4882a593Smuzhiyun *elems_resp = le16_to_cpu(cmd->num_elem_resp);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun return status;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun * ice_aq_query_sched_elems - query scheduler elements
121*4882a593Smuzhiyun * @hw: pointer to the HW struct
122*4882a593Smuzhiyun * @elems_req: number of elements to query
123*4882a593Smuzhiyun * @buf: pointer to buffer
124*4882a593Smuzhiyun * @buf_size: buffer size in bytes
125*4882a593Smuzhiyun * @elems_ret: returns total number of elements returned
126*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * Query scheduling elements (0x0404)
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun enum ice_status
ice_aq_query_sched_elems(struct ice_hw * hw,u16 elems_req,struct ice_aqc_txsched_elem_data * buf,u16 buf_size,u16 * elems_ret,struct ice_sq_cd * cd)131*4882a593Smuzhiyun ice_aq_query_sched_elems(struct ice_hw *hw, u16 elems_req,
132*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data *buf, u16 buf_size,
133*4882a593Smuzhiyun u16 *elems_ret, struct ice_sq_cd *cd)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_get_sched_elems,
136*4882a593Smuzhiyun elems_req, (void *)buf, buf_size,
137*4882a593Smuzhiyun elems_ret, cd);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun * ice_sched_add_node - Insert the Tx scheduler node in SW DB
142*4882a593Smuzhiyun * @pi: port information structure
143*4882a593Smuzhiyun * @layer: Scheduler layer of the node
144*4882a593Smuzhiyun * @info: Scheduler element information from firmware
145*4882a593Smuzhiyun *
146*4882a593Smuzhiyun * This function inserts a scheduler node to the SW DB.
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun enum ice_status
ice_sched_add_node(struct ice_port_info * pi,u8 layer,struct ice_aqc_txsched_elem_data * info)149*4882a593Smuzhiyun ice_sched_add_node(struct ice_port_info *pi, u8 layer,
150*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data *info)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data elem;
153*4882a593Smuzhiyun struct ice_sched_node *parent;
154*4882a593Smuzhiyun struct ice_sched_node *node;
155*4882a593Smuzhiyun enum ice_status status;
156*4882a593Smuzhiyun struct ice_hw *hw;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun if (!pi)
159*4882a593Smuzhiyun return ICE_ERR_PARAM;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun hw = pi->hw;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /* A valid parent node should be there */
164*4882a593Smuzhiyun parent = ice_sched_find_node_by_teid(pi->root,
165*4882a593Smuzhiyun le32_to_cpu(info->parent_teid));
166*4882a593Smuzhiyun if (!parent) {
167*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED,
168*4882a593Smuzhiyun "Parent Node not found for parent_teid=0x%x\n",
169*4882a593Smuzhiyun le32_to_cpu(info->parent_teid));
170*4882a593Smuzhiyun return ICE_ERR_PARAM;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* query the current node information from FW before adding it
174*4882a593Smuzhiyun * to the SW DB
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun status = ice_sched_query_elem(hw, le32_to_cpu(info->node_teid), &elem);
177*4882a593Smuzhiyun if (status)
178*4882a593Smuzhiyun return status;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun node = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*node), GFP_KERNEL);
181*4882a593Smuzhiyun if (!node)
182*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
183*4882a593Smuzhiyun if (hw->max_children[layer]) {
184*4882a593Smuzhiyun /* coverity[suspicious_sizeof] */
185*4882a593Smuzhiyun node->children = devm_kcalloc(ice_hw_to_dev(hw),
186*4882a593Smuzhiyun hw->max_children[layer],
187*4882a593Smuzhiyun sizeof(*node), GFP_KERNEL);
188*4882a593Smuzhiyun if (!node->children) {
189*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), node);
190*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun node->in_use = true;
195*4882a593Smuzhiyun node->parent = parent;
196*4882a593Smuzhiyun node->tx_sched_layer = layer;
197*4882a593Smuzhiyun parent->children[parent->num_children++] = node;
198*4882a593Smuzhiyun node->info = elem;
199*4882a593Smuzhiyun return 0;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /**
203*4882a593Smuzhiyun * ice_aq_delete_sched_elems - delete scheduler elements
204*4882a593Smuzhiyun * @hw: pointer to the HW struct
205*4882a593Smuzhiyun * @grps_req: number of groups to delete
206*4882a593Smuzhiyun * @buf: pointer to buffer
207*4882a593Smuzhiyun * @buf_size: buffer size in bytes
208*4882a593Smuzhiyun * @grps_del: returns total number of elements deleted
209*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * Delete scheduling elements (0x040F)
212*4882a593Smuzhiyun */
213*4882a593Smuzhiyun static enum ice_status
ice_aq_delete_sched_elems(struct ice_hw * hw,u16 grps_req,struct ice_aqc_delete_elem * buf,u16 buf_size,u16 * grps_del,struct ice_sq_cd * cd)214*4882a593Smuzhiyun ice_aq_delete_sched_elems(struct ice_hw *hw, u16 grps_req,
215*4882a593Smuzhiyun struct ice_aqc_delete_elem *buf, u16 buf_size,
216*4882a593Smuzhiyun u16 *grps_del, struct ice_sq_cd *cd)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_delete_sched_elems,
219*4882a593Smuzhiyun grps_req, (void *)buf, buf_size,
220*4882a593Smuzhiyun grps_del, cd);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /**
224*4882a593Smuzhiyun * ice_sched_remove_elems - remove nodes from HW
225*4882a593Smuzhiyun * @hw: pointer to the HW struct
226*4882a593Smuzhiyun * @parent: pointer to the parent node
227*4882a593Smuzhiyun * @num_nodes: number of nodes
228*4882a593Smuzhiyun * @node_teids: array of node teids to be deleted
229*4882a593Smuzhiyun *
230*4882a593Smuzhiyun * This function remove nodes from HW
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun static enum ice_status
ice_sched_remove_elems(struct ice_hw * hw,struct ice_sched_node * parent,u16 num_nodes,u32 * node_teids)233*4882a593Smuzhiyun ice_sched_remove_elems(struct ice_hw *hw, struct ice_sched_node *parent,
234*4882a593Smuzhiyun u16 num_nodes, u32 *node_teids)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun struct ice_aqc_delete_elem *buf;
237*4882a593Smuzhiyun u16 i, num_groups_removed = 0;
238*4882a593Smuzhiyun enum ice_status status;
239*4882a593Smuzhiyun u16 buf_size;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun buf_size = struct_size(buf, teid, num_nodes);
242*4882a593Smuzhiyun buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
243*4882a593Smuzhiyun if (!buf)
244*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun buf->hdr.parent_teid = parent->info.node_teid;
247*4882a593Smuzhiyun buf->hdr.num_elems = cpu_to_le16(num_nodes);
248*4882a593Smuzhiyun for (i = 0; i < num_nodes; i++)
249*4882a593Smuzhiyun buf->teid[i] = cpu_to_le32(node_teids[i]);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun status = ice_aq_delete_sched_elems(hw, 1, buf, buf_size,
252*4882a593Smuzhiyun &num_groups_removed, NULL);
253*4882a593Smuzhiyun if (status || num_groups_removed != 1)
254*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED, "remove node failed FW error %d\n",
255*4882a593Smuzhiyun hw->adminq.sq_last_status);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), buf);
258*4882a593Smuzhiyun return status;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /**
262*4882a593Smuzhiyun * ice_sched_get_first_node - get the first node of the given layer
263*4882a593Smuzhiyun * @pi: port information structure
264*4882a593Smuzhiyun * @parent: pointer the base node of the subtree
265*4882a593Smuzhiyun * @layer: layer number
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * This function retrieves the first node of the given layer from the subtree
268*4882a593Smuzhiyun */
269*4882a593Smuzhiyun static struct ice_sched_node *
ice_sched_get_first_node(struct ice_port_info * pi,struct ice_sched_node * parent,u8 layer)270*4882a593Smuzhiyun ice_sched_get_first_node(struct ice_port_info *pi,
271*4882a593Smuzhiyun struct ice_sched_node *parent, u8 layer)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun return pi->sib_head[parent->tc_num][layer];
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /**
277*4882a593Smuzhiyun * ice_sched_get_tc_node - get pointer to TC node
278*4882a593Smuzhiyun * @pi: port information structure
279*4882a593Smuzhiyun * @tc: TC number
280*4882a593Smuzhiyun *
281*4882a593Smuzhiyun * This function returns the TC node pointer
282*4882a593Smuzhiyun */
ice_sched_get_tc_node(struct ice_port_info * pi,u8 tc)283*4882a593Smuzhiyun struct ice_sched_node *ice_sched_get_tc_node(struct ice_port_info *pi, u8 tc)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun u8 i;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun if (!pi || !pi->root)
288*4882a593Smuzhiyun return NULL;
289*4882a593Smuzhiyun for (i = 0; i < pi->root->num_children; i++)
290*4882a593Smuzhiyun if (pi->root->children[i]->tc_num == tc)
291*4882a593Smuzhiyun return pi->root->children[i];
292*4882a593Smuzhiyun return NULL;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * ice_free_sched_node - Free a Tx scheduler node from SW DB
297*4882a593Smuzhiyun * @pi: port information structure
298*4882a593Smuzhiyun * @node: pointer to the ice_sched_node struct
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * This function frees up a node from SW DB as well as from HW
301*4882a593Smuzhiyun *
302*4882a593Smuzhiyun * This function needs to be called with the port_info->sched_lock held
303*4882a593Smuzhiyun */
ice_free_sched_node(struct ice_port_info * pi,struct ice_sched_node * node)304*4882a593Smuzhiyun void ice_free_sched_node(struct ice_port_info *pi, struct ice_sched_node *node)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun struct ice_sched_node *parent;
307*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
308*4882a593Smuzhiyun u8 i, j;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /* Free the children before freeing up the parent node
311*4882a593Smuzhiyun * The parent array is updated below and that shifts the nodes
312*4882a593Smuzhiyun * in the array. So always pick the first child if num children > 0
313*4882a593Smuzhiyun */
314*4882a593Smuzhiyun while (node->num_children)
315*4882a593Smuzhiyun ice_free_sched_node(pi, node->children[0]);
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /* Leaf, TC and root nodes can't be deleted by SW */
318*4882a593Smuzhiyun if (node->tx_sched_layer >= hw->sw_entry_point_layer &&
319*4882a593Smuzhiyun node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
320*4882a593Smuzhiyun node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT &&
321*4882a593Smuzhiyun node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF) {
322*4882a593Smuzhiyun u32 teid = le32_to_cpu(node->info.node_teid);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun ice_sched_remove_elems(hw, node->parent, 1, &teid);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun parent = node->parent;
327*4882a593Smuzhiyun /* root has no parent */
328*4882a593Smuzhiyun if (parent) {
329*4882a593Smuzhiyun struct ice_sched_node *p;
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /* update the parent */
332*4882a593Smuzhiyun for (i = 0; i < parent->num_children; i++)
333*4882a593Smuzhiyun if (parent->children[i] == node) {
334*4882a593Smuzhiyun for (j = i + 1; j < parent->num_children; j++)
335*4882a593Smuzhiyun parent->children[j - 1] =
336*4882a593Smuzhiyun parent->children[j];
337*4882a593Smuzhiyun parent->num_children--;
338*4882a593Smuzhiyun break;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun p = ice_sched_get_first_node(pi, node, node->tx_sched_layer);
342*4882a593Smuzhiyun while (p) {
343*4882a593Smuzhiyun if (p->sibling == node) {
344*4882a593Smuzhiyun p->sibling = node->sibling;
345*4882a593Smuzhiyun break;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun p = p->sibling;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun /* update the sibling head if head is getting removed */
351*4882a593Smuzhiyun if (pi->sib_head[node->tc_num][node->tx_sched_layer] == node)
352*4882a593Smuzhiyun pi->sib_head[node->tc_num][node->tx_sched_layer] =
353*4882a593Smuzhiyun node->sibling;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /* leaf nodes have no children */
357*4882a593Smuzhiyun if (node->children)
358*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), node->children);
359*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), node);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun /**
363*4882a593Smuzhiyun * ice_aq_get_dflt_topo - gets default scheduler topology
364*4882a593Smuzhiyun * @hw: pointer to the HW struct
365*4882a593Smuzhiyun * @lport: logical port number
366*4882a593Smuzhiyun * @buf: pointer to buffer
367*4882a593Smuzhiyun * @buf_size: buffer size in bytes
368*4882a593Smuzhiyun * @num_branches: returns total number of queue to port branches
369*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
370*4882a593Smuzhiyun *
371*4882a593Smuzhiyun * Get default scheduler topology (0x400)
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun static enum ice_status
ice_aq_get_dflt_topo(struct ice_hw * hw,u8 lport,struct ice_aqc_get_topo_elem * buf,u16 buf_size,u8 * num_branches,struct ice_sq_cd * cd)374*4882a593Smuzhiyun ice_aq_get_dflt_topo(struct ice_hw *hw, u8 lport,
375*4882a593Smuzhiyun struct ice_aqc_get_topo_elem *buf, u16 buf_size,
376*4882a593Smuzhiyun u8 *num_branches, struct ice_sq_cd *cd)
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun struct ice_aqc_get_topo *cmd;
379*4882a593Smuzhiyun struct ice_aq_desc desc;
380*4882a593Smuzhiyun enum ice_status status;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun cmd = &desc.params.get_topo;
383*4882a593Smuzhiyun ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_dflt_topo);
384*4882a593Smuzhiyun cmd->port_num = lport;
385*4882a593Smuzhiyun status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
386*4882a593Smuzhiyun if (!status && num_branches)
387*4882a593Smuzhiyun *num_branches = cmd->num_branches;
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun return status;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun /**
393*4882a593Smuzhiyun * ice_aq_add_sched_elems - adds scheduling element
394*4882a593Smuzhiyun * @hw: pointer to the HW struct
395*4882a593Smuzhiyun * @grps_req: the number of groups that are requested to be added
396*4882a593Smuzhiyun * @buf: pointer to buffer
397*4882a593Smuzhiyun * @buf_size: buffer size in bytes
398*4882a593Smuzhiyun * @grps_added: returns total number of groups added
399*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
400*4882a593Smuzhiyun *
401*4882a593Smuzhiyun * Add scheduling elements (0x0401)
402*4882a593Smuzhiyun */
403*4882a593Smuzhiyun static enum ice_status
ice_aq_add_sched_elems(struct ice_hw * hw,u16 grps_req,struct ice_aqc_add_elem * buf,u16 buf_size,u16 * grps_added,struct ice_sq_cd * cd)404*4882a593Smuzhiyun ice_aq_add_sched_elems(struct ice_hw *hw, u16 grps_req,
405*4882a593Smuzhiyun struct ice_aqc_add_elem *buf, u16 buf_size,
406*4882a593Smuzhiyun u16 *grps_added, struct ice_sq_cd *cd)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_add_sched_elems,
409*4882a593Smuzhiyun grps_req, (void *)buf, buf_size,
410*4882a593Smuzhiyun grps_added, cd);
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun /**
414*4882a593Smuzhiyun * ice_aq_cfg_sched_elems - configures scheduler elements
415*4882a593Smuzhiyun * @hw: pointer to the HW struct
416*4882a593Smuzhiyun * @elems_req: number of elements to configure
417*4882a593Smuzhiyun * @buf: pointer to buffer
418*4882a593Smuzhiyun * @buf_size: buffer size in bytes
419*4882a593Smuzhiyun * @elems_cfgd: returns total number of elements configured
420*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
421*4882a593Smuzhiyun *
422*4882a593Smuzhiyun * Configure scheduling elements (0x0403)
423*4882a593Smuzhiyun */
424*4882a593Smuzhiyun static enum ice_status
ice_aq_cfg_sched_elems(struct ice_hw * hw,u16 elems_req,struct ice_aqc_txsched_elem_data * buf,u16 buf_size,u16 * elems_cfgd,struct ice_sq_cd * cd)425*4882a593Smuzhiyun ice_aq_cfg_sched_elems(struct ice_hw *hw, u16 elems_req,
426*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data *buf, u16 buf_size,
427*4882a593Smuzhiyun u16 *elems_cfgd, struct ice_sq_cd *cd)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_cfg_sched_elems,
430*4882a593Smuzhiyun elems_req, (void *)buf, buf_size,
431*4882a593Smuzhiyun elems_cfgd, cd);
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun /**
435*4882a593Smuzhiyun * ice_aq_suspend_sched_elems - suspend scheduler elements
436*4882a593Smuzhiyun * @hw: pointer to the HW struct
437*4882a593Smuzhiyun * @elems_req: number of elements to suspend
438*4882a593Smuzhiyun * @buf: pointer to buffer
439*4882a593Smuzhiyun * @buf_size: buffer size in bytes
440*4882a593Smuzhiyun * @elems_ret: returns total number of elements suspended
441*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
442*4882a593Smuzhiyun *
443*4882a593Smuzhiyun * Suspend scheduling elements (0x0409)
444*4882a593Smuzhiyun */
445*4882a593Smuzhiyun static enum ice_status
ice_aq_suspend_sched_elems(struct ice_hw * hw,u16 elems_req,__le32 * buf,u16 buf_size,u16 * elems_ret,struct ice_sq_cd * cd)446*4882a593Smuzhiyun ice_aq_suspend_sched_elems(struct ice_hw *hw, u16 elems_req, __le32 *buf,
447*4882a593Smuzhiyun u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_suspend_sched_elems,
450*4882a593Smuzhiyun elems_req, (void *)buf, buf_size,
451*4882a593Smuzhiyun elems_ret, cd);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun /**
455*4882a593Smuzhiyun * ice_aq_resume_sched_elems - resume scheduler elements
456*4882a593Smuzhiyun * @hw: pointer to the HW struct
457*4882a593Smuzhiyun * @elems_req: number of elements to resume
458*4882a593Smuzhiyun * @buf: pointer to buffer
459*4882a593Smuzhiyun * @buf_size: buffer size in bytes
460*4882a593Smuzhiyun * @elems_ret: returns total number of elements resumed
461*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
462*4882a593Smuzhiyun *
463*4882a593Smuzhiyun * resume scheduling elements (0x040A)
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun static enum ice_status
ice_aq_resume_sched_elems(struct ice_hw * hw,u16 elems_req,__le32 * buf,u16 buf_size,u16 * elems_ret,struct ice_sq_cd * cd)466*4882a593Smuzhiyun ice_aq_resume_sched_elems(struct ice_hw *hw, u16 elems_req, __le32 *buf,
467*4882a593Smuzhiyun u16 buf_size, u16 *elems_ret, struct ice_sq_cd *cd)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun return ice_aqc_send_sched_elem_cmd(hw, ice_aqc_opc_resume_sched_elems,
470*4882a593Smuzhiyun elems_req, (void *)buf, buf_size,
471*4882a593Smuzhiyun elems_ret, cd);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /**
475*4882a593Smuzhiyun * ice_aq_query_sched_res - query scheduler resource
476*4882a593Smuzhiyun * @hw: pointer to the HW struct
477*4882a593Smuzhiyun * @buf_size: buffer size in bytes
478*4882a593Smuzhiyun * @buf: pointer to buffer
479*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
480*4882a593Smuzhiyun *
481*4882a593Smuzhiyun * Query scheduler resource allocation (0x0412)
482*4882a593Smuzhiyun */
483*4882a593Smuzhiyun static enum ice_status
ice_aq_query_sched_res(struct ice_hw * hw,u16 buf_size,struct ice_aqc_query_txsched_res_resp * buf,struct ice_sq_cd * cd)484*4882a593Smuzhiyun ice_aq_query_sched_res(struct ice_hw *hw, u16 buf_size,
485*4882a593Smuzhiyun struct ice_aqc_query_txsched_res_resp *buf,
486*4882a593Smuzhiyun struct ice_sq_cd *cd)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun struct ice_aq_desc desc;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_sched_res);
491*4882a593Smuzhiyun return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun /**
495*4882a593Smuzhiyun * ice_sched_suspend_resume_elems - suspend or resume HW nodes
496*4882a593Smuzhiyun * @hw: pointer to the HW struct
497*4882a593Smuzhiyun * @num_nodes: number of nodes
498*4882a593Smuzhiyun * @node_teids: array of node teids to be suspended or resumed
499*4882a593Smuzhiyun * @suspend: true means suspend / false means resume
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun * This function suspends or resumes HW nodes
502*4882a593Smuzhiyun */
503*4882a593Smuzhiyun static enum ice_status
ice_sched_suspend_resume_elems(struct ice_hw * hw,u8 num_nodes,u32 * node_teids,bool suspend)504*4882a593Smuzhiyun ice_sched_suspend_resume_elems(struct ice_hw *hw, u8 num_nodes, u32 *node_teids,
505*4882a593Smuzhiyun bool suspend)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun u16 i, buf_size, num_elem_ret = 0;
508*4882a593Smuzhiyun enum ice_status status;
509*4882a593Smuzhiyun __le32 *buf;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun buf_size = sizeof(*buf) * num_nodes;
512*4882a593Smuzhiyun buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
513*4882a593Smuzhiyun if (!buf)
514*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun for (i = 0; i < num_nodes; i++)
517*4882a593Smuzhiyun buf[i] = cpu_to_le32(node_teids[i]);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun if (suspend)
520*4882a593Smuzhiyun status = ice_aq_suspend_sched_elems(hw, num_nodes, buf,
521*4882a593Smuzhiyun buf_size, &num_elem_ret,
522*4882a593Smuzhiyun NULL);
523*4882a593Smuzhiyun else
524*4882a593Smuzhiyun status = ice_aq_resume_sched_elems(hw, num_nodes, buf,
525*4882a593Smuzhiyun buf_size, &num_elem_ret,
526*4882a593Smuzhiyun NULL);
527*4882a593Smuzhiyun if (status || num_elem_ret != num_nodes)
528*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED, "suspend/resume failed\n");
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), buf);
531*4882a593Smuzhiyun return status;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun /**
535*4882a593Smuzhiyun * ice_alloc_lan_q_ctx - allocate LAN queue contexts for the given VSI and TC
536*4882a593Smuzhiyun * @hw: pointer to the HW struct
537*4882a593Smuzhiyun * @vsi_handle: VSI handle
538*4882a593Smuzhiyun * @tc: TC number
539*4882a593Smuzhiyun * @new_numqs: number of queues
540*4882a593Smuzhiyun */
541*4882a593Smuzhiyun static enum ice_status
ice_alloc_lan_q_ctx(struct ice_hw * hw,u16 vsi_handle,u8 tc,u16 new_numqs)542*4882a593Smuzhiyun ice_alloc_lan_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 new_numqs)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun struct ice_vsi_ctx *vsi_ctx;
545*4882a593Smuzhiyun struct ice_q_ctx *q_ctx;
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
548*4882a593Smuzhiyun if (!vsi_ctx)
549*4882a593Smuzhiyun return ICE_ERR_PARAM;
550*4882a593Smuzhiyun /* allocate LAN queue contexts */
551*4882a593Smuzhiyun if (!vsi_ctx->lan_q_ctx[tc]) {
552*4882a593Smuzhiyun vsi_ctx->lan_q_ctx[tc] = devm_kcalloc(ice_hw_to_dev(hw),
553*4882a593Smuzhiyun new_numqs,
554*4882a593Smuzhiyun sizeof(*q_ctx),
555*4882a593Smuzhiyun GFP_KERNEL);
556*4882a593Smuzhiyun if (!vsi_ctx->lan_q_ctx[tc])
557*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
558*4882a593Smuzhiyun vsi_ctx->num_lan_q_entries[tc] = new_numqs;
559*4882a593Smuzhiyun return 0;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun /* num queues are increased, update the queue contexts */
562*4882a593Smuzhiyun if (new_numqs > vsi_ctx->num_lan_q_entries[tc]) {
563*4882a593Smuzhiyun u16 prev_num = vsi_ctx->num_lan_q_entries[tc];
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun q_ctx = devm_kcalloc(ice_hw_to_dev(hw), new_numqs,
566*4882a593Smuzhiyun sizeof(*q_ctx), GFP_KERNEL);
567*4882a593Smuzhiyun if (!q_ctx)
568*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
569*4882a593Smuzhiyun memcpy(q_ctx, vsi_ctx->lan_q_ctx[tc],
570*4882a593Smuzhiyun prev_num * sizeof(*q_ctx));
571*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), vsi_ctx->lan_q_ctx[tc]);
572*4882a593Smuzhiyun vsi_ctx->lan_q_ctx[tc] = q_ctx;
573*4882a593Smuzhiyun vsi_ctx->num_lan_q_entries[tc] = new_numqs;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun return 0;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun /**
579*4882a593Smuzhiyun * ice_aq_rl_profile - performs a rate limiting task
580*4882a593Smuzhiyun * @hw: pointer to the HW struct
581*4882a593Smuzhiyun * @opcode: opcode for add, query, or remove profile(s)
582*4882a593Smuzhiyun * @num_profiles: the number of profiles
583*4882a593Smuzhiyun * @buf: pointer to buffer
584*4882a593Smuzhiyun * @buf_size: buffer size in bytes
585*4882a593Smuzhiyun * @num_processed: number of processed add or remove profile(s) to return
586*4882a593Smuzhiyun * @cd: pointer to command details structure
587*4882a593Smuzhiyun *
588*4882a593Smuzhiyun * RL profile function to add, query, or remove profile(s)
589*4882a593Smuzhiyun */
590*4882a593Smuzhiyun static enum ice_status
ice_aq_rl_profile(struct ice_hw * hw,enum ice_adminq_opc opcode,u16 num_profiles,struct ice_aqc_rl_profile_elem * buf,u16 buf_size,u16 * num_processed,struct ice_sq_cd * cd)591*4882a593Smuzhiyun ice_aq_rl_profile(struct ice_hw *hw, enum ice_adminq_opc opcode,
592*4882a593Smuzhiyun u16 num_profiles, struct ice_aqc_rl_profile_elem *buf,
593*4882a593Smuzhiyun u16 buf_size, u16 *num_processed, struct ice_sq_cd *cd)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun struct ice_aqc_rl_profile *cmd;
596*4882a593Smuzhiyun struct ice_aq_desc desc;
597*4882a593Smuzhiyun enum ice_status status;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun cmd = &desc.params.rl_profile;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun ice_fill_dflt_direct_cmd_desc(&desc, opcode);
602*4882a593Smuzhiyun desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
603*4882a593Smuzhiyun cmd->num_profiles = cpu_to_le16(num_profiles);
604*4882a593Smuzhiyun status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
605*4882a593Smuzhiyun if (!status && num_processed)
606*4882a593Smuzhiyun *num_processed = le16_to_cpu(cmd->num_processed);
607*4882a593Smuzhiyun return status;
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun /**
611*4882a593Smuzhiyun * ice_aq_add_rl_profile - adds rate limiting profile(s)
612*4882a593Smuzhiyun * @hw: pointer to the HW struct
613*4882a593Smuzhiyun * @num_profiles: the number of profile(s) to be add
614*4882a593Smuzhiyun * @buf: pointer to buffer
615*4882a593Smuzhiyun * @buf_size: buffer size in bytes
616*4882a593Smuzhiyun * @num_profiles_added: total number of profiles added to return
617*4882a593Smuzhiyun * @cd: pointer to command details structure
618*4882a593Smuzhiyun *
619*4882a593Smuzhiyun * Add RL profile (0x0410)
620*4882a593Smuzhiyun */
621*4882a593Smuzhiyun static enum ice_status
ice_aq_add_rl_profile(struct ice_hw * hw,u16 num_profiles,struct ice_aqc_rl_profile_elem * buf,u16 buf_size,u16 * num_profiles_added,struct ice_sq_cd * cd)622*4882a593Smuzhiyun ice_aq_add_rl_profile(struct ice_hw *hw, u16 num_profiles,
623*4882a593Smuzhiyun struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
624*4882a593Smuzhiyun u16 *num_profiles_added, struct ice_sq_cd *cd)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun return ice_aq_rl_profile(hw, ice_aqc_opc_add_rl_profiles, num_profiles,
627*4882a593Smuzhiyun buf, buf_size, num_profiles_added, cd);
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun /**
631*4882a593Smuzhiyun * ice_aq_remove_rl_profile - removes RL profile(s)
632*4882a593Smuzhiyun * @hw: pointer to the HW struct
633*4882a593Smuzhiyun * @num_profiles: the number of profile(s) to remove
634*4882a593Smuzhiyun * @buf: pointer to buffer
635*4882a593Smuzhiyun * @buf_size: buffer size in bytes
636*4882a593Smuzhiyun * @num_profiles_removed: total number of profiles removed to return
637*4882a593Smuzhiyun * @cd: pointer to command details structure or NULL
638*4882a593Smuzhiyun *
639*4882a593Smuzhiyun * Remove RL profile (0x0415)
640*4882a593Smuzhiyun */
641*4882a593Smuzhiyun static enum ice_status
ice_aq_remove_rl_profile(struct ice_hw * hw,u16 num_profiles,struct ice_aqc_rl_profile_elem * buf,u16 buf_size,u16 * num_profiles_removed,struct ice_sq_cd * cd)642*4882a593Smuzhiyun ice_aq_remove_rl_profile(struct ice_hw *hw, u16 num_profiles,
643*4882a593Smuzhiyun struct ice_aqc_rl_profile_elem *buf, u16 buf_size,
644*4882a593Smuzhiyun u16 *num_profiles_removed, struct ice_sq_cd *cd)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun return ice_aq_rl_profile(hw, ice_aqc_opc_remove_rl_profiles,
647*4882a593Smuzhiyun num_profiles, buf, buf_size,
648*4882a593Smuzhiyun num_profiles_removed, cd);
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun /**
652*4882a593Smuzhiyun * ice_sched_del_rl_profile - remove RL profile
653*4882a593Smuzhiyun * @hw: pointer to the HW struct
654*4882a593Smuzhiyun * @rl_info: rate limit profile information
655*4882a593Smuzhiyun *
656*4882a593Smuzhiyun * If the profile ID is not referenced anymore, it removes profile ID with
657*4882a593Smuzhiyun * its associated parameters from HW DB,and locally. The caller needs to
658*4882a593Smuzhiyun * hold scheduler lock.
659*4882a593Smuzhiyun */
660*4882a593Smuzhiyun static enum ice_status
ice_sched_del_rl_profile(struct ice_hw * hw,struct ice_aqc_rl_profile_info * rl_info)661*4882a593Smuzhiyun ice_sched_del_rl_profile(struct ice_hw *hw,
662*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_info)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun struct ice_aqc_rl_profile_elem *buf;
665*4882a593Smuzhiyun u16 num_profiles_removed;
666*4882a593Smuzhiyun enum ice_status status;
667*4882a593Smuzhiyun u16 num_profiles = 1;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun if (rl_info->prof_id_ref != 0)
670*4882a593Smuzhiyun return ICE_ERR_IN_USE;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun /* Safe to remove profile ID */
673*4882a593Smuzhiyun buf = &rl_info->profile;
674*4882a593Smuzhiyun status = ice_aq_remove_rl_profile(hw, num_profiles, buf, sizeof(*buf),
675*4882a593Smuzhiyun &num_profiles_removed, NULL);
676*4882a593Smuzhiyun if (status || num_profiles_removed != num_profiles)
677*4882a593Smuzhiyun return ICE_ERR_CFG;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun /* Delete stale entry now */
680*4882a593Smuzhiyun list_del(&rl_info->list_entry);
681*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), rl_info);
682*4882a593Smuzhiyun return status;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun /**
686*4882a593Smuzhiyun * ice_sched_clear_rl_prof - clears RL prof entries
687*4882a593Smuzhiyun * @pi: port information structure
688*4882a593Smuzhiyun *
689*4882a593Smuzhiyun * This function removes all RL profile from HW as well as from SW DB.
690*4882a593Smuzhiyun */
ice_sched_clear_rl_prof(struct ice_port_info * pi)691*4882a593Smuzhiyun static void ice_sched_clear_rl_prof(struct ice_port_info *pi)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun u16 ln;
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
696*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_prof_elem;
697*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_prof_tmp;
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
700*4882a593Smuzhiyun &pi->rl_prof_list[ln], list_entry) {
701*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
702*4882a593Smuzhiyun enum ice_status status;
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun rl_prof_elem->prof_id_ref = 0;
705*4882a593Smuzhiyun status = ice_sched_del_rl_profile(hw, rl_prof_elem);
706*4882a593Smuzhiyun if (status) {
707*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED,
708*4882a593Smuzhiyun "Remove rl profile failed\n");
709*4882a593Smuzhiyun /* On error, free mem required */
710*4882a593Smuzhiyun list_del(&rl_prof_elem->list_entry);
711*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun /**
718*4882a593Smuzhiyun * ice_sched_clear_agg - clears the aggregator related information
719*4882a593Smuzhiyun * @hw: pointer to the hardware structure
720*4882a593Smuzhiyun *
721*4882a593Smuzhiyun * This function removes aggregator list and free up aggregator related memory
722*4882a593Smuzhiyun * previously allocated.
723*4882a593Smuzhiyun */
ice_sched_clear_agg(struct ice_hw * hw)724*4882a593Smuzhiyun void ice_sched_clear_agg(struct ice_hw *hw)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun struct ice_sched_agg_info *agg_info;
727*4882a593Smuzhiyun struct ice_sched_agg_info *atmp;
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun list_for_each_entry_safe(agg_info, atmp, &hw->agg_list, list_entry) {
730*4882a593Smuzhiyun struct ice_sched_agg_vsi_info *agg_vsi_info;
731*4882a593Smuzhiyun struct ice_sched_agg_vsi_info *vtmp;
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun list_for_each_entry_safe(agg_vsi_info, vtmp,
734*4882a593Smuzhiyun &agg_info->agg_vsi_list, list_entry) {
735*4882a593Smuzhiyun list_del(&agg_vsi_info->list_entry);
736*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), agg_vsi_info);
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun list_del(&agg_info->list_entry);
739*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), agg_info);
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun /**
744*4882a593Smuzhiyun * ice_sched_clear_tx_topo - clears the scheduler tree nodes
745*4882a593Smuzhiyun * @pi: port information structure
746*4882a593Smuzhiyun *
747*4882a593Smuzhiyun * This function removes all the nodes from HW as well as from SW DB.
748*4882a593Smuzhiyun */
ice_sched_clear_tx_topo(struct ice_port_info * pi)749*4882a593Smuzhiyun static void ice_sched_clear_tx_topo(struct ice_port_info *pi)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun if (!pi)
752*4882a593Smuzhiyun return;
753*4882a593Smuzhiyun /* remove RL profiles related lists */
754*4882a593Smuzhiyun ice_sched_clear_rl_prof(pi);
755*4882a593Smuzhiyun if (pi->root) {
756*4882a593Smuzhiyun ice_free_sched_node(pi, pi->root);
757*4882a593Smuzhiyun pi->root = NULL;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun /**
762*4882a593Smuzhiyun * ice_sched_clear_port - clear the scheduler elements from SW DB for a port
763*4882a593Smuzhiyun * @pi: port information structure
764*4882a593Smuzhiyun *
765*4882a593Smuzhiyun * Cleanup scheduling elements from SW DB
766*4882a593Smuzhiyun */
ice_sched_clear_port(struct ice_port_info * pi)767*4882a593Smuzhiyun void ice_sched_clear_port(struct ice_port_info *pi)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
770*4882a593Smuzhiyun return;
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun pi->port_state = ICE_SCHED_PORT_STATE_INIT;
773*4882a593Smuzhiyun mutex_lock(&pi->sched_lock);
774*4882a593Smuzhiyun ice_sched_clear_tx_topo(pi);
775*4882a593Smuzhiyun mutex_unlock(&pi->sched_lock);
776*4882a593Smuzhiyun mutex_destroy(&pi->sched_lock);
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun /**
780*4882a593Smuzhiyun * ice_sched_cleanup_all - cleanup scheduler elements from SW DB for all ports
781*4882a593Smuzhiyun * @hw: pointer to the HW struct
782*4882a593Smuzhiyun *
783*4882a593Smuzhiyun * Cleanup scheduling elements from SW DB for all the ports
784*4882a593Smuzhiyun */
ice_sched_cleanup_all(struct ice_hw * hw)785*4882a593Smuzhiyun void ice_sched_cleanup_all(struct ice_hw *hw)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun if (!hw)
788*4882a593Smuzhiyun return;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun if (hw->layer_info) {
791*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), hw->layer_info);
792*4882a593Smuzhiyun hw->layer_info = NULL;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun ice_sched_clear_port(hw->port_info);
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun hw->num_tx_sched_layers = 0;
798*4882a593Smuzhiyun hw->num_tx_sched_phys_layers = 0;
799*4882a593Smuzhiyun hw->flattened_layers = 0;
800*4882a593Smuzhiyun hw->max_cgds = 0;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun /**
804*4882a593Smuzhiyun * ice_sched_add_elems - add nodes to HW and SW DB
805*4882a593Smuzhiyun * @pi: port information structure
806*4882a593Smuzhiyun * @tc_node: pointer to the branch node
807*4882a593Smuzhiyun * @parent: pointer to the parent node
808*4882a593Smuzhiyun * @layer: layer number to add nodes
809*4882a593Smuzhiyun * @num_nodes: number of nodes
810*4882a593Smuzhiyun * @num_nodes_added: pointer to num nodes added
811*4882a593Smuzhiyun * @first_node_teid: if new nodes are added then return the TEID of first node
812*4882a593Smuzhiyun *
813*4882a593Smuzhiyun * This function add nodes to HW as well as to SW DB for a given layer
814*4882a593Smuzhiyun */
815*4882a593Smuzhiyun static enum ice_status
ice_sched_add_elems(struct ice_port_info * pi,struct ice_sched_node * tc_node,struct ice_sched_node * parent,u8 layer,u16 num_nodes,u16 * num_nodes_added,u32 * first_node_teid)816*4882a593Smuzhiyun ice_sched_add_elems(struct ice_port_info *pi, struct ice_sched_node *tc_node,
817*4882a593Smuzhiyun struct ice_sched_node *parent, u8 layer, u16 num_nodes,
818*4882a593Smuzhiyun u16 *num_nodes_added, u32 *first_node_teid)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun struct ice_sched_node *prev, *new_node;
821*4882a593Smuzhiyun struct ice_aqc_add_elem *buf;
822*4882a593Smuzhiyun u16 i, num_groups_added = 0;
823*4882a593Smuzhiyun enum ice_status status = 0;
824*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
825*4882a593Smuzhiyun size_t buf_size;
826*4882a593Smuzhiyun u32 teid;
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun buf_size = struct_size(buf, generic, num_nodes);
829*4882a593Smuzhiyun buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
830*4882a593Smuzhiyun if (!buf)
831*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun buf->hdr.parent_teid = parent->info.node_teid;
834*4882a593Smuzhiyun buf->hdr.num_elems = cpu_to_le16(num_nodes);
835*4882a593Smuzhiyun for (i = 0; i < num_nodes; i++) {
836*4882a593Smuzhiyun buf->generic[i].parent_teid = parent->info.node_teid;
837*4882a593Smuzhiyun buf->generic[i].data.elem_type = ICE_AQC_ELEM_TYPE_SE_GENERIC;
838*4882a593Smuzhiyun buf->generic[i].data.valid_sections =
839*4882a593Smuzhiyun ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
840*4882a593Smuzhiyun ICE_AQC_ELEM_VALID_EIR;
841*4882a593Smuzhiyun buf->generic[i].data.generic = 0;
842*4882a593Smuzhiyun buf->generic[i].data.cir_bw.bw_profile_idx =
843*4882a593Smuzhiyun cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
844*4882a593Smuzhiyun buf->generic[i].data.cir_bw.bw_alloc =
845*4882a593Smuzhiyun cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
846*4882a593Smuzhiyun buf->generic[i].data.eir_bw.bw_profile_idx =
847*4882a593Smuzhiyun cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
848*4882a593Smuzhiyun buf->generic[i].data.eir_bw.bw_alloc =
849*4882a593Smuzhiyun cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun status = ice_aq_add_sched_elems(hw, 1, buf, buf_size,
853*4882a593Smuzhiyun &num_groups_added, NULL);
854*4882a593Smuzhiyun if (status || num_groups_added != 1) {
855*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED, "add node failed FW Error %d\n",
856*4882a593Smuzhiyun hw->adminq.sq_last_status);
857*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), buf);
858*4882a593Smuzhiyun return ICE_ERR_CFG;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun *num_nodes_added = num_nodes;
862*4882a593Smuzhiyun /* add nodes to the SW DB */
863*4882a593Smuzhiyun for (i = 0; i < num_nodes; i++) {
864*4882a593Smuzhiyun status = ice_sched_add_node(pi, layer, &buf->generic[i]);
865*4882a593Smuzhiyun if (status) {
866*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED,
867*4882a593Smuzhiyun "add nodes in SW DB failed status =%d\n",
868*4882a593Smuzhiyun status);
869*4882a593Smuzhiyun break;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun teid = le32_to_cpu(buf->generic[i].node_teid);
873*4882a593Smuzhiyun new_node = ice_sched_find_node_by_teid(parent, teid);
874*4882a593Smuzhiyun if (!new_node) {
875*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED,
876*4882a593Smuzhiyun "Node is missing for teid =%d\n", teid);
877*4882a593Smuzhiyun break;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun new_node->sibling = NULL;
881*4882a593Smuzhiyun new_node->tc_num = tc_node->tc_num;
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun /* add it to previous node sibling pointer */
884*4882a593Smuzhiyun /* Note: siblings are not linked across branches */
885*4882a593Smuzhiyun prev = ice_sched_get_first_node(pi, tc_node, layer);
886*4882a593Smuzhiyun if (prev && prev != new_node) {
887*4882a593Smuzhiyun while (prev->sibling)
888*4882a593Smuzhiyun prev = prev->sibling;
889*4882a593Smuzhiyun prev->sibling = new_node;
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun /* initialize the sibling head */
893*4882a593Smuzhiyun if (!pi->sib_head[tc_node->tc_num][layer])
894*4882a593Smuzhiyun pi->sib_head[tc_node->tc_num][layer] = new_node;
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun if (i == 0)
897*4882a593Smuzhiyun *first_node_teid = teid;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), buf);
901*4882a593Smuzhiyun return status;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun /**
905*4882a593Smuzhiyun * ice_sched_add_nodes_to_layer - Add nodes to a given layer
906*4882a593Smuzhiyun * @pi: port information structure
907*4882a593Smuzhiyun * @tc_node: pointer to TC node
908*4882a593Smuzhiyun * @parent: pointer to parent node
909*4882a593Smuzhiyun * @layer: layer number to add nodes
910*4882a593Smuzhiyun * @num_nodes: number of nodes to be added
911*4882a593Smuzhiyun * @first_node_teid: pointer to the first node TEID
912*4882a593Smuzhiyun * @num_nodes_added: pointer to number of nodes added
913*4882a593Smuzhiyun *
914*4882a593Smuzhiyun * This function add nodes to a given layer.
915*4882a593Smuzhiyun */
916*4882a593Smuzhiyun static enum ice_status
ice_sched_add_nodes_to_layer(struct ice_port_info * pi,struct ice_sched_node * tc_node,struct ice_sched_node * parent,u8 layer,u16 num_nodes,u32 * first_node_teid,u16 * num_nodes_added)917*4882a593Smuzhiyun ice_sched_add_nodes_to_layer(struct ice_port_info *pi,
918*4882a593Smuzhiyun struct ice_sched_node *tc_node,
919*4882a593Smuzhiyun struct ice_sched_node *parent, u8 layer,
920*4882a593Smuzhiyun u16 num_nodes, u32 *first_node_teid,
921*4882a593Smuzhiyun u16 *num_nodes_added)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun u32 *first_teid_ptr = first_node_teid;
924*4882a593Smuzhiyun u16 new_num_nodes, max_child_nodes;
925*4882a593Smuzhiyun enum ice_status status = 0;
926*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
927*4882a593Smuzhiyun u16 num_added = 0;
928*4882a593Smuzhiyun u32 temp;
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun *num_nodes_added = 0;
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun if (!num_nodes)
933*4882a593Smuzhiyun return status;
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun if (!parent || layer < hw->sw_entry_point_layer)
936*4882a593Smuzhiyun return ICE_ERR_PARAM;
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun /* max children per node per layer */
939*4882a593Smuzhiyun max_child_nodes = hw->max_children[parent->tx_sched_layer];
940*4882a593Smuzhiyun
941*4882a593Smuzhiyun /* current number of children + required nodes exceed max children ? */
942*4882a593Smuzhiyun if ((parent->num_children + num_nodes) > max_child_nodes) {
943*4882a593Smuzhiyun /* Fail if the parent is a TC node */
944*4882a593Smuzhiyun if (parent == tc_node)
945*4882a593Smuzhiyun return ICE_ERR_CFG;
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun /* utilize all the spaces if the parent is not full */
948*4882a593Smuzhiyun if (parent->num_children < max_child_nodes) {
949*4882a593Smuzhiyun new_num_nodes = max_child_nodes - parent->num_children;
950*4882a593Smuzhiyun /* this recursion is intentional, and wouldn't
951*4882a593Smuzhiyun * go more than 2 calls
952*4882a593Smuzhiyun */
953*4882a593Smuzhiyun status = ice_sched_add_nodes_to_layer(pi, tc_node,
954*4882a593Smuzhiyun parent, layer,
955*4882a593Smuzhiyun new_num_nodes,
956*4882a593Smuzhiyun first_node_teid,
957*4882a593Smuzhiyun &num_added);
958*4882a593Smuzhiyun if (status)
959*4882a593Smuzhiyun return status;
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun *num_nodes_added += num_added;
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun /* Don't modify the first node TEID memory if the first node was
964*4882a593Smuzhiyun * added already in the above call. Instead send some temp
965*4882a593Smuzhiyun * memory for all other recursive calls.
966*4882a593Smuzhiyun */
967*4882a593Smuzhiyun if (num_added)
968*4882a593Smuzhiyun first_teid_ptr = &temp;
969*4882a593Smuzhiyun
970*4882a593Smuzhiyun new_num_nodes = num_nodes - num_added;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun /* This parent is full, try the next sibling */
973*4882a593Smuzhiyun parent = parent->sibling;
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun /* this recursion is intentional, for 1024 queues
976*4882a593Smuzhiyun * per VSI, it goes max of 16 iterations.
977*4882a593Smuzhiyun * 1024 / 8 = 128 layer 8 nodes
978*4882a593Smuzhiyun * 128 /8 = 16 (add 8 nodes per iteration)
979*4882a593Smuzhiyun */
980*4882a593Smuzhiyun status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
981*4882a593Smuzhiyun layer, new_num_nodes,
982*4882a593Smuzhiyun first_teid_ptr,
983*4882a593Smuzhiyun &num_added);
984*4882a593Smuzhiyun *num_nodes_added += num_added;
985*4882a593Smuzhiyun return status;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun status = ice_sched_add_elems(pi, tc_node, parent, layer, num_nodes,
989*4882a593Smuzhiyun num_nodes_added, first_node_teid);
990*4882a593Smuzhiyun return status;
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun
993*4882a593Smuzhiyun /**
994*4882a593Smuzhiyun * ice_sched_get_qgrp_layer - get the current queue group layer number
995*4882a593Smuzhiyun * @hw: pointer to the HW struct
996*4882a593Smuzhiyun *
997*4882a593Smuzhiyun * This function returns the current queue group layer number
998*4882a593Smuzhiyun */
ice_sched_get_qgrp_layer(struct ice_hw * hw)999*4882a593Smuzhiyun static u8 ice_sched_get_qgrp_layer(struct ice_hw *hw)
1000*4882a593Smuzhiyun {
1001*4882a593Smuzhiyun /* It's always total layers - 1, the array is 0 relative so -2 */
1002*4882a593Smuzhiyun return hw->num_tx_sched_layers - ICE_QGRP_LAYER_OFFSET;
1003*4882a593Smuzhiyun }
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun /**
1006*4882a593Smuzhiyun * ice_sched_get_vsi_layer - get the current VSI layer number
1007*4882a593Smuzhiyun * @hw: pointer to the HW struct
1008*4882a593Smuzhiyun *
1009*4882a593Smuzhiyun * This function returns the current VSI layer number
1010*4882a593Smuzhiyun */
ice_sched_get_vsi_layer(struct ice_hw * hw)1011*4882a593Smuzhiyun static u8 ice_sched_get_vsi_layer(struct ice_hw *hw)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun /* Num Layers VSI layer
1014*4882a593Smuzhiyun * 9 6
1015*4882a593Smuzhiyun * 7 4
1016*4882a593Smuzhiyun * 5 or less sw_entry_point_layer
1017*4882a593Smuzhiyun */
1018*4882a593Smuzhiyun /* calculate the VSI layer based on number of layers. */
1019*4882a593Smuzhiyun if (hw->num_tx_sched_layers > ICE_VSI_LAYER_OFFSET + 1) {
1020*4882a593Smuzhiyun u8 layer = hw->num_tx_sched_layers - ICE_VSI_LAYER_OFFSET;
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun if (layer > hw->sw_entry_point_layer)
1023*4882a593Smuzhiyun return layer;
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun return hw->sw_entry_point_layer;
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun /**
1029*4882a593Smuzhiyun * ice_rm_dflt_leaf_node - remove the default leaf node in the tree
1030*4882a593Smuzhiyun * @pi: port information structure
1031*4882a593Smuzhiyun *
1032*4882a593Smuzhiyun * This function removes the leaf node that was created by the FW
1033*4882a593Smuzhiyun * during initialization
1034*4882a593Smuzhiyun */
ice_rm_dflt_leaf_node(struct ice_port_info * pi)1035*4882a593Smuzhiyun static void ice_rm_dflt_leaf_node(struct ice_port_info *pi)
1036*4882a593Smuzhiyun {
1037*4882a593Smuzhiyun struct ice_sched_node *node;
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun node = pi->root;
1040*4882a593Smuzhiyun while (node) {
1041*4882a593Smuzhiyun if (!node->num_children)
1042*4882a593Smuzhiyun break;
1043*4882a593Smuzhiyun node = node->children[0];
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun if (node && node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF) {
1046*4882a593Smuzhiyun u32 teid = le32_to_cpu(node->info.node_teid);
1047*4882a593Smuzhiyun enum ice_status status;
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun /* remove the default leaf node */
1050*4882a593Smuzhiyun status = ice_sched_remove_elems(pi->hw, node->parent, 1, &teid);
1051*4882a593Smuzhiyun if (!status)
1052*4882a593Smuzhiyun ice_free_sched_node(pi, node);
1053*4882a593Smuzhiyun }
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun /**
1057*4882a593Smuzhiyun * ice_sched_rm_dflt_nodes - free the default nodes in the tree
1058*4882a593Smuzhiyun * @pi: port information structure
1059*4882a593Smuzhiyun *
1060*4882a593Smuzhiyun * This function frees all the nodes except root and TC that were created by
1061*4882a593Smuzhiyun * the FW during initialization
1062*4882a593Smuzhiyun */
ice_sched_rm_dflt_nodes(struct ice_port_info * pi)1063*4882a593Smuzhiyun static void ice_sched_rm_dflt_nodes(struct ice_port_info *pi)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun struct ice_sched_node *node;
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun ice_rm_dflt_leaf_node(pi);
1068*4882a593Smuzhiyun
1069*4882a593Smuzhiyun /* remove the default nodes except TC and root nodes */
1070*4882a593Smuzhiyun node = pi->root;
1071*4882a593Smuzhiyun while (node) {
1072*4882a593Smuzhiyun if (node->tx_sched_layer >= pi->hw->sw_entry_point_layer &&
1073*4882a593Smuzhiyun node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
1074*4882a593Smuzhiyun node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT) {
1075*4882a593Smuzhiyun ice_free_sched_node(pi, node);
1076*4882a593Smuzhiyun break;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun if (!node->num_children)
1080*4882a593Smuzhiyun break;
1081*4882a593Smuzhiyun node = node->children[0];
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun /**
1086*4882a593Smuzhiyun * ice_sched_init_port - Initialize scheduler by querying information from FW
1087*4882a593Smuzhiyun * @pi: port info structure for the tree to cleanup
1088*4882a593Smuzhiyun *
1089*4882a593Smuzhiyun * This function is the initial call to find the total number of Tx scheduler
1090*4882a593Smuzhiyun * resources, default topology created by firmware and storing the information
1091*4882a593Smuzhiyun * in SW DB.
1092*4882a593Smuzhiyun */
ice_sched_init_port(struct ice_port_info * pi)1093*4882a593Smuzhiyun enum ice_status ice_sched_init_port(struct ice_port_info *pi)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun struct ice_aqc_get_topo_elem *buf;
1096*4882a593Smuzhiyun enum ice_status status;
1097*4882a593Smuzhiyun struct ice_hw *hw;
1098*4882a593Smuzhiyun u8 num_branches;
1099*4882a593Smuzhiyun u16 num_elems;
1100*4882a593Smuzhiyun u8 i, j;
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun if (!pi)
1103*4882a593Smuzhiyun return ICE_ERR_PARAM;
1104*4882a593Smuzhiyun hw = pi->hw;
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun /* Query the Default Topology from FW */
1107*4882a593Smuzhiyun buf = devm_kzalloc(ice_hw_to_dev(hw), ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
1108*4882a593Smuzhiyun if (!buf)
1109*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun /* Query default scheduling tree topology */
1112*4882a593Smuzhiyun status = ice_aq_get_dflt_topo(hw, pi->lport, buf, ICE_AQ_MAX_BUF_LEN,
1113*4882a593Smuzhiyun &num_branches, NULL);
1114*4882a593Smuzhiyun if (status)
1115*4882a593Smuzhiyun goto err_init_port;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /* num_branches should be between 1-8 */
1118*4882a593Smuzhiyun if (num_branches < 1 || num_branches > ICE_TXSCHED_MAX_BRANCHES) {
1119*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED, "num_branches unexpected %d\n",
1120*4882a593Smuzhiyun num_branches);
1121*4882a593Smuzhiyun status = ICE_ERR_PARAM;
1122*4882a593Smuzhiyun goto err_init_port;
1123*4882a593Smuzhiyun }
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun /* get the number of elements on the default/first branch */
1126*4882a593Smuzhiyun num_elems = le16_to_cpu(buf[0].hdr.num_elems);
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun /* num_elems should always be between 1-9 */
1129*4882a593Smuzhiyun if (num_elems < 1 || num_elems > ICE_AQC_TOPO_MAX_LEVEL_NUM) {
1130*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED, "num_elems unexpected %d\n",
1131*4882a593Smuzhiyun num_elems);
1132*4882a593Smuzhiyun status = ICE_ERR_PARAM;
1133*4882a593Smuzhiyun goto err_init_port;
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun /* If the last node is a leaf node then the index of the queue group
1137*4882a593Smuzhiyun * layer is two less than the number of elements.
1138*4882a593Smuzhiyun */
1139*4882a593Smuzhiyun if (num_elems > 2 && buf[0].generic[num_elems - 1].data.elem_type ==
1140*4882a593Smuzhiyun ICE_AQC_ELEM_TYPE_LEAF)
1141*4882a593Smuzhiyun pi->last_node_teid =
1142*4882a593Smuzhiyun le32_to_cpu(buf[0].generic[num_elems - 2].node_teid);
1143*4882a593Smuzhiyun else
1144*4882a593Smuzhiyun pi->last_node_teid =
1145*4882a593Smuzhiyun le32_to_cpu(buf[0].generic[num_elems - 1].node_teid);
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun /* Insert the Tx Sched root node */
1148*4882a593Smuzhiyun status = ice_sched_add_root_node(pi, &buf[0].generic[0]);
1149*4882a593Smuzhiyun if (status)
1150*4882a593Smuzhiyun goto err_init_port;
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun /* Parse the default tree and cache the information */
1153*4882a593Smuzhiyun for (i = 0; i < num_branches; i++) {
1154*4882a593Smuzhiyun num_elems = le16_to_cpu(buf[i].hdr.num_elems);
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun /* Skip root element as already inserted */
1157*4882a593Smuzhiyun for (j = 1; j < num_elems; j++) {
1158*4882a593Smuzhiyun /* update the sw entry point */
1159*4882a593Smuzhiyun if (buf[0].generic[j].data.elem_type ==
1160*4882a593Smuzhiyun ICE_AQC_ELEM_TYPE_ENTRY_POINT)
1161*4882a593Smuzhiyun hw->sw_entry_point_layer = j;
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun status = ice_sched_add_node(pi, j, &buf[i].generic[j]);
1164*4882a593Smuzhiyun if (status)
1165*4882a593Smuzhiyun goto err_init_port;
1166*4882a593Smuzhiyun }
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun /* Remove the default nodes. */
1170*4882a593Smuzhiyun if (pi->root)
1171*4882a593Smuzhiyun ice_sched_rm_dflt_nodes(pi);
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun /* initialize the port for handling the scheduler tree */
1174*4882a593Smuzhiyun pi->port_state = ICE_SCHED_PORT_STATE_READY;
1175*4882a593Smuzhiyun mutex_init(&pi->sched_lock);
1176*4882a593Smuzhiyun for (i = 0; i < ICE_AQC_TOPO_MAX_LEVEL_NUM; i++)
1177*4882a593Smuzhiyun INIT_LIST_HEAD(&pi->rl_prof_list[i]);
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun err_init_port:
1180*4882a593Smuzhiyun if (status && pi->root) {
1181*4882a593Smuzhiyun ice_free_sched_node(pi, pi->root);
1182*4882a593Smuzhiyun pi->root = NULL;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), buf);
1186*4882a593Smuzhiyun return status;
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun /**
1190*4882a593Smuzhiyun * ice_sched_query_res_alloc - query the FW for num of logical sched layers
1191*4882a593Smuzhiyun * @hw: pointer to the HW struct
1192*4882a593Smuzhiyun *
1193*4882a593Smuzhiyun * query FW for allocated scheduler resources and store in HW struct
1194*4882a593Smuzhiyun */
ice_sched_query_res_alloc(struct ice_hw * hw)1195*4882a593Smuzhiyun enum ice_status ice_sched_query_res_alloc(struct ice_hw *hw)
1196*4882a593Smuzhiyun {
1197*4882a593Smuzhiyun struct ice_aqc_query_txsched_res_resp *buf;
1198*4882a593Smuzhiyun enum ice_status status = 0;
1199*4882a593Smuzhiyun __le16 max_sibl;
1200*4882a593Smuzhiyun u16 i;
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun if (hw->layer_info)
1203*4882a593Smuzhiyun return status;
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun buf = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*buf), GFP_KERNEL);
1206*4882a593Smuzhiyun if (!buf)
1207*4882a593Smuzhiyun return ICE_ERR_NO_MEMORY;
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun status = ice_aq_query_sched_res(hw, sizeof(*buf), buf, NULL);
1210*4882a593Smuzhiyun if (status)
1211*4882a593Smuzhiyun goto sched_query_out;
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun hw->num_tx_sched_layers = le16_to_cpu(buf->sched_props.logical_levels);
1214*4882a593Smuzhiyun hw->num_tx_sched_phys_layers =
1215*4882a593Smuzhiyun le16_to_cpu(buf->sched_props.phys_levels);
1216*4882a593Smuzhiyun hw->flattened_layers = buf->sched_props.flattening_bitmap;
1217*4882a593Smuzhiyun hw->max_cgds = buf->sched_props.max_pf_cgds;
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun /* max sibling group size of current layer refers to the max children
1220*4882a593Smuzhiyun * of the below layer node.
1221*4882a593Smuzhiyun * layer 1 node max children will be layer 2 max sibling group size
1222*4882a593Smuzhiyun * layer 2 node max children will be layer 3 max sibling group size
1223*4882a593Smuzhiyun * and so on. This array will be populated from root (index 0) to
1224*4882a593Smuzhiyun * qgroup layer 7. Leaf node has no children.
1225*4882a593Smuzhiyun */
1226*4882a593Smuzhiyun for (i = 0; i < hw->num_tx_sched_layers - 1; i++) {
1227*4882a593Smuzhiyun max_sibl = buf->layer_props[i + 1].max_sibl_grp_sz;
1228*4882a593Smuzhiyun hw->max_children[i] = le16_to_cpu(max_sibl);
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun hw->layer_info = devm_kmemdup(ice_hw_to_dev(hw), buf->layer_props,
1232*4882a593Smuzhiyun (hw->num_tx_sched_layers *
1233*4882a593Smuzhiyun sizeof(*hw->layer_info)),
1234*4882a593Smuzhiyun GFP_KERNEL);
1235*4882a593Smuzhiyun if (!hw->layer_info) {
1236*4882a593Smuzhiyun status = ICE_ERR_NO_MEMORY;
1237*4882a593Smuzhiyun goto sched_query_out;
1238*4882a593Smuzhiyun }
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun sched_query_out:
1241*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), buf);
1242*4882a593Smuzhiyun return status;
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun /**
1246*4882a593Smuzhiyun * ice_sched_find_node_in_subtree - Find node in part of base node subtree
1247*4882a593Smuzhiyun * @hw: pointer to the HW struct
1248*4882a593Smuzhiyun * @base: pointer to the base node
1249*4882a593Smuzhiyun * @node: pointer to the node to search
1250*4882a593Smuzhiyun *
1251*4882a593Smuzhiyun * This function checks whether a given node is part of the base node
1252*4882a593Smuzhiyun * subtree or not
1253*4882a593Smuzhiyun */
1254*4882a593Smuzhiyun static bool
ice_sched_find_node_in_subtree(struct ice_hw * hw,struct ice_sched_node * base,struct ice_sched_node * node)1255*4882a593Smuzhiyun ice_sched_find_node_in_subtree(struct ice_hw *hw, struct ice_sched_node *base,
1256*4882a593Smuzhiyun struct ice_sched_node *node)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun u8 i;
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun for (i = 0; i < base->num_children; i++) {
1261*4882a593Smuzhiyun struct ice_sched_node *child = base->children[i];
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun if (node == child)
1264*4882a593Smuzhiyun return true;
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun if (child->tx_sched_layer > node->tx_sched_layer)
1267*4882a593Smuzhiyun return false;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun /* this recursion is intentional, and wouldn't
1270*4882a593Smuzhiyun * go more than 8 calls
1271*4882a593Smuzhiyun */
1272*4882a593Smuzhiyun if (ice_sched_find_node_in_subtree(hw, child, node))
1273*4882a593Smuzhiyun return true;
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun return false;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun /**
1279*4882a593Smuzhiyun * ice_sched_get_free_qgrp - Scan all queue group siblings and find a free node
1280*4882a593Smuzhiyun * @pi: port information structure
1281*4882a593Smuzhiyun * @vsi_node: software VSI handle
1282*4882a593Smuzhiyun * @qgrp_node: first queue group node identified for scanning
1283*4882a593Smuzhiyun * @owner: LAN or RDMA
1284*4882a593Smuzhiyun *
1285*4882a593Smuzhiyun * This function retrieves a free LAN or RDMA queue group node by scanning
1286*4882a593Smuzhiyun * qgrp_node and its siblings for the queue group with the fewest number
1287*4882a593Smuzhiyun * of queues currently assigned.
1288*4882a593Smuzhiyun */
1289*4882a593Smuzhiyun static struct ice_sched_node *
ice_sched_get_free_qgrp(struct ice_port_info * pi,struct ice_sched_node * vsi_node,struct ice_sched_node * qgrp_node,u8 owner)1290*4882a593Smuzhiyun ice_sched_get_free_qgrp(struct ice_port_info *pi,
1291*4882a593Smuzhiyun struct ice_sched_node *vsi_node,
1292*4882a593Smuzhiyun struct ice_sched_node *qgrp_node, u8 owner)
1293*4882a593Smuzhiyun {
1294*4882a593Smuzhiyun struct ice_sched_node *min_qgrp;
1295*4882a593Smuzhiyun u8 min_children;
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun if (!qgrp_node)
1298*4882a593Smuzhiyun return qgrp_node;
1299*4882a593Smuzhiyun min_children = qgrp_node->num_children;
1300*4882a593Smuzhiyun if (!min_children)
1301*4882a593Smuzhiyun return qgrp_node;
1302*4882a593Smuzhiyun min_qgrp = qgrp_node;
1303*4882a593Smuzhiyun /* scan all queue groups until find a node which has less than the
1304*4882a593Smuzhiyun * minimum number of children. This way all queue group nodes get
1305*4882a593Smuzhiyun * equal number of shares and active. The bandwidth will be equally
1306*4882a593Smuzhiyun * distributed across all queues.
1307*4882a593Smuzhiyun */
1308*4882a593Smuzhiyun while (qgrp_node) {
1309*4882a593Smuzhiyun /* make sure the qgroup node is part of the VSI subtree */
1310*4882a593Smuzhiyun if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1311*4882a593Smuzhiyun if (qgrp_node->num_children < min_children &&
1312*4882a593Smuzhiyun qgrp_node->owner == owner) {
1313*4882a593Smuzhiyun /* replace the new min queue group node */
1314*4882a593Smuzhiyun min_qgrp = qgrp_node;
1315*4882a593Smuzhiyun min_children = min_qgrp->num_children;
1316*4882a593Smuzhiyun /* break if it has no children, */
1317*4882a593Smuzhiyun if (!min_children)
1318*4882a593Smuzhiyun break;
1319*4882a593Smuzhiyun }
1320*4882a593Smuzhiyun qgrp_node = qgrp_node->sibling;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun return min_qgrp;
1323*4882a593Smuzhiyun }
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun /**
1326*4882a593Smuzhiyun * ice_sched_get_free_qparent - Get a free LAN or RDMA queue group node
1327*4882a593Smuzhiyun * @pi: port information structure
1328*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1329*4882a593Smuzhiyun * @tc: branch number
1330*4882a593Smuzhiyun * @owner: LAN or RDMA
1331*4882a593Smuzhiyun *
1332*4882a593Smuzhiyun * This function retrieves a free LAN or RDMA queue group node
1333*4882a593Smuzhiyun */
1334*4882a593Smuzhiyun struct ice_sched_node *
ice_sched_get_free_qparent(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u8 owner)1335*4882a593Smuzhiyun ice_sched_get_free_qparent(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
1336*4882a593Smuzhiyun u8 owner)
1337*4882a593Smuzhiyun {
1338*4882a593Smuzhiyun struct ice_sched_node *vsi_node, *qgrp_node;
1339*4882a593Smuzhiyun struct ice_vsi_ctx *vsi_ctx;
1340*4882a593Smuzhiyun u16 max_children;
1341*4882a593Smuzhiyun u8 qgrp_layer;
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun qgrp_layer = ice_sched_get_qgrp_layer(pi->hw);
1344*4882a593Smuzhiyun max_children = pi->hw->max_children[qgrp_layer];
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1347*4882a593Smuzhiyun if (!vsi_ctx)
1348*4882a593Smuzhiyun return NULL;
1349*4882a593Smuzhiyun vsi_node = vsi_ctx->sched.vsi_node[tc];
1350*4882a593Smuzhiyun /* validate invalid VSI ID */
1351*4882a593Smuzhiyun if (!vsi_node)
1352*4882a593Smuzhiyun return NULL;
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun /* get the first queue group node from VSI sub-tree */
1355*4882a593Smuzhiyun qgrp_node = ice_sched_get_first_node(pi, vsi_node, qgrp_layer);
1356*4882a593Smuzhiyun while (qgrp_node) {
1357*4882a593Smuzhiyun /* make sure the qgroup node is part of the VSI subtree */
1358*4882a593Smuzhiyun if (ice_sched_find_node_in_subtree(pi->hw, vsi_node, qgrp_node))
1359*4882a593Smuzhiyun if (qgrp_node->num_children < max_children &&
1360*4882a593Smuzhiyun qgrp_node->owner == owner)
1361*4882a593Smuzhiyun break;
1362*4882a593Smuzhiyun qgrp_node = qgrp_node->sibling;
1363*4882a593Smuzhiyun }
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun /* Select the best queue group */
1366*4882a593Smuzhiyun return ice_sched_get_free_qgrp(pi, vsi_node, qgrp_node, owner);
1367*4882a593Smuzhiyun }
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun /**
1370*4882a593Smuzhiyun * ice_sched_get_vsi_node - Get a VSI node based on VSI ID
1371*4882a593Smuzhiyun * @hw: pointer to the HW struct
1372*4882a593Smuzhiyun * @tc_node: pointer to the TC node
1373*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1374*4882a593Smuzhiyun *
1375*4882a593Smuzhiyun * This function retrieves a VSI node for a given VSI ID from a given
1376*4882a593Smuzhiyun * TC branch
1377*4882a593Smuzhiyun */
1378*4882a593Smuzhiyun static struct ice_sched_node *
ice_sched_get_vsi_node(struct ice_hw * hw,struct ice_sched_node * tc_node,u16 vsi_handle)1379*4882a593Smuzhiyun ice_sched_get_vsi_node(struct ice_hw *hw, struct ice_sched_node *tc_node,
1380*4882a593Smuzhiyun u16 vsi_handle)
1381*4882a593Smuzhiyun {
1382*4882a593Smuzhiyun struct ice_sched_node *node;
1383*4882a593Smuzhiyun u8 vsi_layer;
1384*4882a593Smuzhiyun
1385*4882a593Smuzhiyun vsi_layer = ice_sched_get_vsi_layer(hw);
1386*4882a593Smuzhiyun node = ice_sched_get_first_node(hw->port_info, tc_node, vsi_layer);
1387*4882a593Smuzhiyun
1388*4882a593Smuzhiyun /* Check whether it already exists */
1389*4882a593Smuzhiyun while (node) {
1390*4882a593Smuzhiyun if (node->vsi_handle == vsi_handle)
1391*4882a593Smuzhiyun return node;
1392*4882a593Smuzhiyun node = node->sibling;
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun return node;
1396*4882a593Smuzhiyun }
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun /**
1399*4882a593Smuzhiyun * ice_sched_calc_vsi_child_nodes - calculate number of VSI child nodes
1400*4882a593Smuzhiyun * @hw: pointer to the HW struct
1401*4882a593Smuzhiyun * @num_qs: number of queues
1402*4882a593Smuzhiyun * @num_nodes: num nodes array
1403*4882a593Smuzhiyun *
1404*4882a593Smuzhiyun * This function calculates the number of VSI child nodes based on the
1405*4882a593Smuzhiyun * number of queues.
1406*4882a593Smuzhiyun */
1407*4882a593Smuzhiyun static void
ice_sched_calc_vsi_child_nodes(struct ice_hw * hw,u16 num_qs,u16 * num_nodes)1408*4882a593Smuzhiyun ice_sched_calc_vsi_child_nodes(struct ice_hw *hw, u16 num_qs, u16 *num_nodes)
1409*4882a593Smuzhiyun {
1410*4882a593Smuzhiyun u16 num = num_qs;
1411*4882a593Smuzhiyun u8 i, qgl, vsil;
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun qgl = ice_sched_get_qgrp_layer(hw);
1414*4882a593Smuzhiyun vsil = ice_sched_get_vsi_layer(hw);
1415*4882a593Smuzhiyun
1416*4882a593Smuzhiyun /* calculate num nodes from queue group to VSI layer */
1417*4882a593Smuzhiyun for (i = qgl; i > vsil; i--) {
1418*4882a593Smuzhiyun /* round to the next integer if there is a remainder */
1419*4882a593Smuzhiyun num = DIV_ROUND_UP(num, hw->max_children[i]);
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun /* need at least one node */
1422*4882a593Smuzhiyun num_nodes[i] = num ? num : 1;
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun /**
1427*4882a593Smuzhiyun * ice_sched_add_vsi_child_nodes - add VSI child nodes to tree
1428*4882a593Smuzhiyun * @pi: port information structure
1429*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1430*4882a593Smuzhiyun * @tc_node: pointer to the TC node
1431*4882a593Smuzhiyun * @num_nodes: pointer to the num nodes that needs to be added per layer
1432*4882a593Smuzhiyun * @owner: node owner (LAN or RDMA)
1433*4882a593Smuzhiyun *
1434*4882a593Smuzhiyun * This function adds the VSI child nodes to tree. It gets called for
1435*4882a593Smuzhiyun * LAN and RDMA separately.
1436*4882a593Smuzhiyun */
1437*4882a593Smuzhiyun static enum ice_status
ice_sched_add_vsi_child_nodes(struct ice_port_info * pi,u16 vsi_handle,struct ice_sched_node * tc_node,u16 * num_nodes,u8 owner)1438*4882a593Smuzhiyun ice_sched_add_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1439*4882a593Smuzhiyun struct ice_sched_node *tc_node, u16 *num_nodes,
1440*4882a593Smuzhiyun u8 owner)
1441*4882a593Smuzhiyun {
1442*4882a593Smuzhiyun struct ice_sched_node *parent, *node;
1443*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
1444*4882a593Smuzhiyun enum ice_status status;
1445*4882a593Smuzhiyun u32 first_node_teid;
1446*4882a593Smuzhiyun u16 num_added = 0;
1447*4882a593Smuzhiyun u8 i, qgl, vsil;
1448*4882a593Smuzhiyun
1449*4882a593Smuzhiyun qgl = ice_sched_get_qgrp_layer(hw);
1450*4882a593Smuzhiyun vsil = ice_sched_get_vsi_layer(hw);
1451*4882a593Smuzhiyun parent = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1452*4882a593Smuzhiyun for (i = vsil + 1; i <= qgl; i++) {
1453*4882a593Smuzhiyun if (!parent)
1454*4882a593Smuzhiyun return ICE_ERR_CFG;
1455*4882a593Smuzhiyun
1456*4882a593Smuzhiyun status = ice_sched_add_nodes_to_layer(pi, tc_node, parent, i,
1457*4882a593Smuzhiyun num_nodes[i],
1458*4882a593Smuzhiyun &first_node_teid,
1459*4882a593Smuzhiyun &num_added);
1460*4882a593Smuzhiyun if (status || num_nodes[i] != num_added)
1461*4882a593Smuzhiyun return ICE_ERR_CFG;
1462*4882a593Smuzhiyun
1463*4882a593Smuzhiyun /* The newly added node can be a new parent for the next
1464*4882a593Smuzhiyun * layer nodes
1465*4882a593Smuzhiyun */
1466*4882a593Smuzhiyun if (num_added) {
1467*4882a593Smuzhiyun parent = ice_sched_find_node_by_teid(tc_node,
1468*4882a593Smuzhiyun first_node_teid);
1469*4882a593Smuzhiyun node = parent;
1470*4882a593Smuzhiyun while (node) {
1471*4882a593Smuzhiyun node->owner = owner;
1472*4882a593Smuzhiyun node = node->sibling;
1473*4882a593Smuzhiyun }
1474*4882a593Smuzhiyun } else {
1475*4882a593Smuzhiyun parent = parent->children[0];
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun }
1478*4882a593Smuzhiyun
1479*4882a593Smuzhiyun return 0;
1480*4882a593Smuzhiyun }
1481*4882a593Smuzhiyun
1482*4882a593Smuzhiyun /**
1483*4882a593Smuzhiyun * ice_sched_calc_vsi_support_nodes - calculate number of VSI support nodes
1484*4882a593Smuzhiyun * @hw: pointer to the HW struct
1485*4882a593Smuzhiyun * @tc_node: pointer to TC node
1486*4882a593Smuzhiyun * @num_nodes: pointer to num nodes array
1487*4882a593Smuzhiyun *
1488*4882a593Smuzhiyun * This function calculates the number of supported nodes needed to add this
1489*4882a593Smuzhiyun * VSI into Tx tree including the VSI, parent and intermediate nodes in below
1490*4882a593Smuzhiyun * layers
1491*4882a593Smuzhiyun */
1492*4882a593Smuzhiyun static void
ice_sched_calc_vsi_support_nodes(struct ice_hw * hw,struct ice_sched_node * tc_node,u16 * num_nodes)1493*4882a593Smuzhiyun ice_sched_calc_vsi_support_nodes(struct ice_hw *hw,
1494*4882a593Smuzhiyun struct ice_sched_node *tc_node, u16 *num_nodes)
1495*4882a593Smuzhiyun {
1496*4882a593Smuzhiyun struct ice_sched_node *node;
1497*4882a593Smuzhiyun u8 vsil;
1498*4882a593Smuzhiyun int i;
1499*4882a593Smuzhiyun
1500*4882a593Smuzhiyun vsil = ice_sched_get_vsi_layer(hw);
1501*4882a593Smuzhiyun for (i = vsil; i >= hw->sw_entry_point_layer; i--)
1502*4882a593Smuzhiyun /* Add intermediate nodes if TC has no children and
1503*4882a593Smuzhiyun * need at least one node for VSI
1504*4882a593Smuzhiyun */
1505*4882a593Smuzhiyun if (!tc_node->num_children || i == vsil) {
1506*4882a593Smuzhiyun num_nodes[i]++;
1507*4882a593Smuzhiyun } else {
1508*4882a593Smuzhiyun /* If intermediate nodes are reached max children
1509*4882a593Smuzhiyun * then add a new one.
1510*4882a593Smuzhiyun */
1511*4882a593Smuzhiyun node = ice_sched_get_first_node(hw->port_info, tc_node,
1512*4882a593Smuzhiyun (u8)i);
1513*4882a593Smuzhiyun /* scan all the siblings */
1514*4882a593Smuzhiyun while (node) {
1515*4882a593Smuzhiyun if (node->num_children < hw->max_children[i])
1516*4882a593Smuzhiyun break;
1517*4882a593Smuzhiyun node = node->sibling;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun
1520*4882a593Smuzhiyun /* tree has one intermediate node to add this new VSI.
1521*4882a593Smuzhiyun * So no need to calculate supported nodes for below
1522*4882a593Smuzhiyun * layers.
1523*4882a593Smuzhiyun */
1524*4882a593Smuzhiyun if (node)
1525*4882a593Smuzhiyun break;
1526*4882a593Smuzhiyun /* all the nodes are full, allocate a new one */
1527*4882a593Smuzhiyun num_nodes[i]++;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun /**
1532*4882a593Smuzhiyun * ice_sched_add_vsi_support_nodes - add VSI supported nodes into Tx tree
1533*4882a593Smuzhiyun * @pi: port information structure
1534*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1535*4882a593Smuzhiyun * @tc_node: pointer to TC node
1536*4882a593Smuzhiyun * @num_nodes: pointer to num nodes array
1537*4882a593Smuzhiyun *
1538*4882a593Smuzhiyun * This function adds the VSI supported nodes into Tx tree including the
1539*4882a593Smuzhiyun * VSI, its parent and intermediate nodes in below layers
1540*4882a593Smuzhiyun */
1541*4882a593Smuzhiyun static enum ice_status
ice_sched_add_vsi_support_nodes(struct ice_port_info * pi,u16 vsi_handle,struct ice_sched_node * tc_node,u16 * num_nodes)1542*4882a593Smuzhiyun ice_sched_add_vsi_support_nodes(struct ice_port_info *pi, u16 vsi_handle,
1543*4882a593Smuzhiyun struct ice_sched_node *tc_node, u16 *num_nodes)
1544*4882a593Smuzhiyun {
1545*4882a593Smuzhiyun struct ice_sched_node *parent = tc_node;
1546*4882a593Smuzhiyun enum ice_status status;
1547*4882a593Smuzhiyun u32 first_node_teid;
1548*4882a593Smuzhiyun u16 num_added = 0;
1549*4882a593Smuzhiyun u8 i, vsil;
1550*4882a593Smuzhiyun
1551*4882a593Smuzhiyun if (!pi)
1552*4882a593Smuzhiyun return ICE_ERR_PARAM;
1553*4882a593Smuzhiyun
1554*4882a593Smuzhiyun vsil = ice_sched_get_vsi_layer(pi->hw);
1555*4882a593Smuzhiyun for (i = pi->hw->sw_entry_point_layer; i <= vsil; i++) {
1556*4882a593Smuzhiyun status = ice_sched_add_nodes_to_layer(pi, tc_node, parent,
1557*4882a593Smuzhiyun i, num_nodes[i],
1558*4882a593Smuzhiyun &first_node_teid,
1559*4882a593Smuzhiyun &num_added);
1560*4882a593Smuzhiyun if (status || num_nodes[i] != num_added)
1561*4882a593Smuzhiyun return ICE_ERR_CFG;
1562*4882a593Smuzhiyun
1563*4882a593Smuzhiyun /* The newly added node can be a new parent for the next
1564*4882a593Smuzhiyun * layer nodes
1565*4882a593Smuzhiyun */
1566*4882a593Smuzhiyun if (num_added)
1567*4882a593Smuzhiyun parent = ice_sched_find_node_by_teid(tc_node,
1568*4882a593Smuzhiyun first_node_teid);
1569*4882a593Smuzhiyun else
1570*4882a593Smuzhiyun parent = parent->children[0];
1571*4882a593Smuzhiyun
1572*4882a593Smuzhiyun if (!parent)
1573*4882a593Smuzhiyun return ICE_ERR_CFG;
1574*4882a593Smuzhiyun
1575*4882a593Smuzhiyun if (i == vsil)
1576*4882a593Smuzhiyun parent->vsi_handle = vsi_handle;
1577*4882a593Smuzhiyun }
1578*4882a593Smuzhiyun
1579*4882a593Smuzhiyun return 0;
1580*4882a593Smuzhiyun }
1581*4882a593Smuzhiyun
1582*4882a593Smuzhiyun /**
1583*4882a593Smuzhiyun * ice_sched_add_vsi_to_topo - add a new VSI into tree
1584*4882a593Smuzhiyun * @pi: port information structure
1585*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1586*4882a593Smuzhiyun * @tc: TC number
1587*4882a593Smuzhiyun *
1588*4882a593Smuzhiyun * This function adds a new VSI into scheduler tree
1589*4882a593Smuzhiyun */
1590*4882a593Smuzhiyun static enum ice_status
ice_sched_add_vsi_to_topo(struct ice_port_info * pi,u16 vsi_handle,u8 tc)1591*4882a593Smuzhiyun ice_sched_add_vsi_to_topo(struct ice_port_info *pi, u16 vsi_handle, u8 tc)
1592*4882a593Smuzhiyun {
1593*4882a593Smuzhiyun u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1594*4882a593Smuzhiyun struct ice_sched_node *tc_node;
1595*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
1596*4882a593Smuzhiyun
1597*4882a593Smuzhiyun tc_node = ice_sched_get_tc_node(pi, tc);
1598*4882a593Smuzhiyun if (!tc_node)
1599*4882a593Smuzhiyun return ICE_ERR_PARAM;
1600*4882a593Smuzhiyun
1601*4882a593Smuzhiyun /* calculate number of supported nodes needed for this VSI */
1602*4882a593Smuzhiyun ice_sched_calc_vsi_support_nodes(hw, tc_node, num_nodes);
1603*4882a593Smuzhiyun
1604*4882a593Smuzhiyun /* add VSI supported nodes to TC subtree */
1605*4882a593Smuzhiyun return ice_sched_add_vsi_support_nodes(pi, vsi_handle, tc_node,
1606*4882a593Smuzhiyun num_nodes);
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun /**
1610*4882a593Smuzhiyun * ice_sched_update_vsi_child_nodes - update VSI child nodes
1611*4882a593Smuzhiyun * @pi: port information structure
1612*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1613*4882a593Smuzhiyun * @tc: TC number
1614*4882a593Smuzhiyun * @new_numqs: new number of max queues
1615*4882a593Smuzhiyun * @owner: owner of this subtree
1616*4882a593Smuzhiyun *
1617*4882a593Smuzhiyun * This function updates the VSI child nodes based on the number of queues
1618*4882a593Smuzhiyun */
1619*4882a593Smuzhiyun static enum ice_status
ice_sched_update_vsi_child_nodes(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u16 new_numqs,u8 owner)1620*4882a593Smuzhiyun ice_sched_update_vsi_child_nodes(struct ice_port_info *pi, u16 vsi_handle,
1621*4882a593Smuzhiyun u8 tc, u16 new_numqs, u8 owner)
1622*4882a593Smuzhiyun {
1623*4882a593Smuzhiyun u16 new_num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
1624*4882a593Smuzhiyun struct ice_sched_node *vsi_node;
1625*4882a593Smuzhiyun struct ice_sched_node *tc_node;
1626*4882a593Smuzhiyun struct ice_vsi_ctx *vsi_ctx;
1627*4882a593Smuzhiyun enum ice_status status = 0;
1628*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
1629*4882a593Smuzhiyun u16 prev_numqs;
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun tc_node = ice_sched_get_tc_node(pi, tc);
1632*4882a593Smuzhiyun if (!tc_node)
1633*4882a593Smuzhiyun return ICE_ERR_CFG;
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun vsi_node = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1636*4882a593Smuzhiyun if (!vsi_node)
1637*4882a593Smuzhiyun return ICE_ERR_CFG;
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1640*4882a593Smuzhiyun if (!vsi_ctx)
1641*4882a593Smuzhiyun return ICE_ERR_PARAM;
1642*4882a593Smuzhiyun
1643*4882a593Smuzhiyun prev_numqs = vsi_ctx->sched.max_lanq[tc];
1644*4882a593Smuzhiyun /* num queues are not changed or less than the previous number */
1645*4882a593Smuzhiyun if (new_numqs <= prev_numqs)
1646*4882a593Smuzhiyun return status;
1647*4882a593Smuzhiyun status = ice_alloc_lan_q_ctx(hw, vsi_handle, tc, new_numqs);
1648*4882a593Smuzhiyun if (status)
1649*4882a593Smuzhiyun return status;
1650*4882a593Smuzhiyun
1651*4882a593Smuzhiyun if (new_numqs)
1652*4882a593Smuzhiyun ice_sched_calc_vsi_child_nodes(hw, new_numqs, new_num_nodes);
1653*4882a593Smuzhiyun /* Keep the max number of queue configuration all the time. Update the
1654*4882a593Smuzhiyun * tree only if number of queues > previous number of queues. This may
1655*4882a593Smuzhiyun * leave some extra nodes in the tree if number of queues < previous
1656*4882a593Smuzhiyun * number but that wouldn't harm anything. Removing those extra nodes
1657*4882a593Smuzhiyun * may complicate the code if those nodes are part of SRL or
1658*4882a593Smuzhiyun * individually rate limited.
1659*4882a593Smuzhiyun */
1660*4882a593Smuzhiyun status = ice_sched_add_vsi_child_nodes(pi, vsi_handle, tc_node,
1661*4882a593Smuzhiyun new_num_nodes, owner);
1662*4882a593Smuzhiyun if (status)
1663*4882a593Smuzhiyun return status;
1664*4882a593Smuzhiyun vsi_ctx->sched.max_lanq[tc] = new_numqs;
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun return 0;
1667*4882a593Smuzhiyun }
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun /**
1670*4882a593Smuzhiyun * ice_sched_cfg_vsi - configure the new/existing VSI
1671*4882a593Smuzhiyun * @pi: port information structure
1672*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1673*4882a593Smuzhiyun * @tc: TC number
1674*4882a593Smuzhiyun * @maxqs: max number of queues
1675*4882a593Smuzhiyun * @owner: LAN or RDMA
1676*4882a593Smuzhiyun * @enable: TC enabled or disabled
1677*4882a593Smuzhiyun *
1678*4882a593Smuzhiyun * This function adds/updates VSI nodes based on the number of queues. If TC is
1679*4882a593Smuzhiyun * enabled and VSI is in suspended state then resume the VSI back. If TC is
1680*4882a593Smuzhiyun * disabled then suspend the VSI if it is not already.
1681*4882a593Smuzhiyun */
1682*4882a593Smuzhiyun enum ice_status
ice_sched_cfg_vsi(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u16 maxqs,u8 owner,bool enable)1683*4882a593Smuzhiyun ice_sched_cfg_vsi(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 maxqs,
1684*4882a593Smuzhiyun u8 owner, bool enable)
1685*4882a593Smuzhiyun {
1686*4882a593Smuzhiyun struct ice_sched_node *vsi_node, *tc_node;
1687*4882a593Smuzhiyun struct ice_vsi_ctx *vsi_ctx;
1688*4882a593Smuzhiyun enum ice_status status = 0;
1689*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun ice_debug(pi->hw, ICE_DBG_SCHED, "add/config VSI %d\n", vsi_handle);
1692*4882a593Smuzhiyun tc_node = ice_sched_get_tc_node(pi, tc);
1693*4882a593Smuzhiyun if (!tc_node)
1694*4882a593Smuzhiyun return ICE_ERR_PARAM;
1695*4882a593Smuzhiyun vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1696*4882a593Smuzhiyun if (!vsi_ctx)
1697*4882a593Smuzhiyun return ICE_ERR_PARAM;
1698*4882a593Smuzhiyun vsi_node = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun /* suspend the VSI if TC is not enabled */
1701*4882a593Smuzhiyun if (!enable) {
1702*4882a593Smuzhiyun if (vsi_node && vsi_node->in_use) {
1703*4882a593Smuzhiyun u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1704*4882a593Smuzhiyun
1705*4882a593Smuzhiyun status = ice_sched_suspend_resume_elems(hw, 1, &teid,
1706*4882a593Smuzhiyun true);
1707*4882a593Smuzhiyun if (!status)
1708*4882a593Smuzhiyun vsi_node->in_use = false;
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun return status;
1711*4882a593Smuzhiyun }
1712*4882a593Smuzhiyun
1713*4882a593Smuzhiyun /* TC is enabled, if it is a new VSI then add it to the tree */
1714*4882a593Smuzhiyun if (!vsi_node) {
1715*4882a593Smuzhiyun status = ice_sched_add_vsi_to_topo(pi, vsi_handle, tc);
1716*4882a593Smuzhiyun if (status)
1717*4882a593Smuzhiyun return status;
1718*4882a593Smuzhiyun
1719*4882a593Smuzhiyun vsi_node = ice_sched_get_vsi_node(hw, tc_node, vsi_handle);
1720*4882a593Smuzhiyun if (!vsi_node)
1721*4882a593Smuzhiyun return ICE_ERR_CFG;
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun vsi_ctx->sched.vsi_node[tc] = vsi_node;
1724*4882a593Smuzhiyun vsi_node->in_use = true;
1725*4882a593Smuzhiyun /* invalidate the max queues whenever VSI gets added first time
1726*4882a593Smuzhiyun * into the scheduler tree (boot or after reset). We need to
1727*4882a593Smuzhiyun * recreate the child nodes all the time in these cases.
1728*4882a593Smuzhiyun */
1729*4882a593Smuzhiyun vsi_ctx->sched.max_lanq[tc] = 0;
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun
1732*4882a593Smuzhiyun /* update the VSI child nodes */
1733*4882a593Smuzhiyun status = ice_sched_update_vsi_child_nodes(pi, vsi_handle, tc, maxqs,
1734*4882a593Smuzhiyun owner);
1735*4882a593Smuzhiyun if (status)
1736*4882a593Smuzhiyun return status;
1737*4882a593Smuzhiyun
1738*4882a593Smuzhiyun /* TC is enabled, resume the VSI if it is in the suspend state */
1739*4882a593Smuzhiyun if (!vsi_node->in_use) {
1740*4882a593Smuzhiyun u32 teid = le32_to_cpu(vsi_node->info.node_teid);
1741*4882a593Smuzhiyun
1742*4882a593Smuzhiyun status = ice_sched_suspend_resume_elems(hw, 1, &teid, false);
1743*4882a593Smuzhiyun if (!status)
1744*4882a593Smuzhiyun vsi_node->in_use = true;
1745*4882a593Smuzhiyun }
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun return status;
1748*4882a593Smuzhiyun }
1749*4882a593Smuzhiyun
1750*4882a593Smuzhiyun /**
1751*4882a593Smuzhiyun * ice_sched_rm_agg_vsi_entry - remove aggregator related VSI info entry
1752*4882a593Smuzhiyun * @pi: port information structure
1753*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1754*4882a593Smuzhiyun *
1755*4882a593Smuzhiyun * This function removes single aggregator VSI info entry from
1756*4882a593Smuzhiyun * aggregator list.
1757*4882a593Smuzhiyun */
ice_sched_rm_agg_vsi_info(struct ice_port_info * pi,u16 vsi_handle)1758*4882a593Smuzhiyun static void ice_sched_rm_agg_vsi_info(struct ice_port_info *pi, u16 vsi_handle)
1759*4882a593Smuzhiyun {
1760*4882a593Smuzhiyun struct ice_sched_agg_info *agg_info;
1761*4882a593Smuzhiyun struct ice_sched_agg_info *atmp;
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun list_for_each_entry_safe(agg_info, atmp, &pi->hw->agg_list,
1764*4882a593Smuzhiyun list_entry) {
1765*4882a593Smuzhiyun struct ice_sched_agg_vsi_info *agg_vsi_info;
1766*4882a593Smuzhiyun struct ice_sched_agg_vsi_info *vtmp;
1767*4882a593Smuzhiyun
1768*4882a593Smuzhiyun list_for_each_entry_safe(agg_vsi_info, vtmp,
1769*4882a593Smuzhiyun &agg_info->agg_vsi_list, list_entry)
1770*4882a593Smuzhiyun if (agg_vsi_info->vsi_handle == vsi_handle) {
1771*4882a593Smuzhiyun list_del(&agg_vsi_info->list_entry);
1772*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(pi->hw),
1773*4882a593Smuzhiyun agg_vsi_info);
1774*4882a593Smuzhiyun return;
1775*4882a593Smuzhiyun }
1776*4882a593Smuzhiyun }
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun /**
1780*4882a593Smuzhiyun * ice_sched_is_leaf_node_present - check for a leaf node in the sub-tree
1781*4882a593Smuzhiyun * @node: pointer to the sub-tree node
1782*4882a593Smuzhiyun *
1783*4882a593Smuzhiyun * This function checks for a leaf node presence in a given sub-tree node.
1784*4882a593Smuzhiyun */
ice_sched_is_leaf_node_present(struct ice_sched_node * node)1785*4882a593Smuzhiyun static bool ice_sched_is_leaf_node_present(struct ice_sched_node *node)
1786*4882a593Smuzhiyun {
1787*4882a593Smuzhiyun u8 i;
1788*4882a593Smuzhiyun
1789*4882a593Smuzhiyun for (i = 0; i < node->num_children; i++)
1790*4882a593Smuzhiyun if (ice_sched_is_leaf_node_present(node->children[i]))
1791*4882a593Smuzhiyun return true;
1792*4882a593Smuzhiyun /* check for a leaf node */
1793*4882a593Smuzhiyun return (node->info.data.elem_type == ICE_AQC_ELEM_TYPE_LEAF);
1794*4882a593Smuzhiyun }
1795*4882a593Smuzhiyun
1796*4882a593Smuzhiyun /**
1797*4882a593Smuzhiyun * ice_sched_rm_vsi_cfg - remove the VSI and its children nodes
1798*4882a593Smuzhiyun * @pi: port information structure
1799*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1800*4882a593Smuzhiyun * @owner: LAN or RDMA
1801*4882a593Smuzhiyun *
1802*4882a593Smuzhiyun * This function removes the VSI and its LAN or RDMA children nodes from the
1803*4882a593Smuzhiyun * scheduler tree.
1804*4882a593Smuzhiyun */
1805*4882a593Smuzhiyun static enum ice_status
ice_sched_rm_vsi_cfg(struct ice_port_info * pi,u16 vsi_handle,u8 owner)1806*4882a593Smuzhiyun ice_sched_rm_vsi_cfg(struct ice_port_info *pi, u16 vsi_handle, u8 owner)
1807*4882a593Smuzhiyun {
1808*4882a593Smuzhiyun enum ice_status status = ICE_ERR_PARAM;
1809*4882a593Smuzhiyun struct ice_vsi_ctx *vsi_ctx;
1810*4882a593Smuzhiyun u8 i;
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun ice_debug(pi->hw, ICE_DBG_SCHED, "removing VSI %d\n", vsi_handle);
1813*4882a593Smuzhiyun if (!ice_is_vsi_valid(pi->hw, vsi_handle))
1814*4882a593Smuzhiyun return status;
1815*4882a593Smuzhiyun mutex_lock(&pi->sched_lock);
1816*4882a593Smuzhiyun vsi_ctx = ice_get_vsi_ctx(pi->hw, vsi_handle);
1817*4882a593Smuzhiyun if (!vsi_ctx)
1818*4882a593Smuzhiyun goto exit_sched_rm_vsi_cfg;
1819*4882a593Smuzhiyun
1820*4882a593Smuzhiyun ice_for_each_traffic_class(i) {
1821*4882a593Smuzhiyun struct ice_sched_node *vsi_node, *tc_node;
1822*4882a593Smuzhiyun u8 j = 0;
1823*4882a593Smuzhiyun
1824*4882a593Smuzhiyun tc_node = ice_sched_get_tc_node(pi, i);
1825*4882a593Smuzhiyun if (!tc_node)
1826*4882a593Smuzhiyun continue;
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun vsi_node = ice_sched_get_vsi_node(pi->hw, tc_node, vsi_handle);
1829*4882a593Smuzhiyun if (!vsi_node)
1830*4882a593Smuzhiyun continue;
1831*4882a593Smuzhiyun
1832*4882a593Smuzhiyun if (ice_sched_is_leaf_node_present(vsi_node)) {
1833*4882a593Smuzhiyun ice_debug(pi->hw, ICE_DBG_SCHED,
1834*4882a593Smuzhiyun "VSI has leaf nodes in TC %d\n", i);
1835*4882a593Smuzhiyun status = ICE_ERR_IN_USE;
1836*4882a593Smuzhiyun goto exit_sched_rm_vsi_cfg;
1837*4882a593Smuzhiyun }
1838*4882a593Smuzhiyun while (j < vsi_node->num_children) {
1839*4882a593Smuzhiyun if (vsi_node->children[j]->owner == owner) {
1840*4882a593Smuzhiyun ice_free_sched_node(pi, vsi_node->children[j]);
1841*4882a593Smuzhiyun
1842*4882a593Smuzhiyun /* reset the counter again since the num
1843*4882a593Smuzhiyun * children will be updated after node removal
1844*4882a593Smuzhiyun */
1845*4882a593Smuzhiyun j = 0;
1846*4882a593Smuzhiyun } else {
1847*4882a593Smuzhiyun j++;
1848*4882a593Smuzhiyun }
1849*4882a593Smuzhiyun }
1850*4882a593Smuzhiyun /* remove the VSI if it has no children */
1851*4882a593Smuzhiyun if (!vsi_node->num_children) {
1852*4882a593Smuzhiyun ice_free_sched_node(pi, vsi_node);
1853*4882a593Smuzhiyun vsi_ctx->sched.vsi_node[i] = NULL;
1854*4882a593Smuzhiyun
1855*4882a593Smuzhiyun /* clean up aggregator related VSI info if any */
1856*4882a593Smuzhiyun ice_sched_rm_agg_vsi_info(pi, vsi_handle);
1857*4882a593Smuzhiyun }
1858*4882a593Smuzhiyun if (owner == ICE_SCHED_NODE_OWNER_LAN)
1859*4882a593Smuzhiyun vsi_ctx->sched.max_lanq[i] = 0;
1860*4882a593Smuzhiyun }
1861*4882a593Smuzhiyun status = 0;
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun exit_sched_rm_vsi_cfg:
1864*4882a593Smuzhiyun mutex_unlock(&pi->sched_lock);
1865*4882a593Smuzhiyun return status;
1866*4882a593Smuzhiyun }
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun /**
1869*4882a593Smuzhiyun * ice_rm_vsi_lan_cfg - remove VSI and its LAN children nodes
1870*4882a593Smuzhiyun * @pi: port information structure
1871*4882a593Smuzhiyun * @vsi_handle: software VSI handle
1872*4882a593Smuzhiyun *
1873*4882a593Smuzhiyun * This function clears the VSI and its LAN children nodes from scheduler tree
1874*4882a593Smuzhiyun * for all TCs.
1875*4882a593Smuzhiyun */
ice_rm_vsi_lan_cfg(struct ice_port_info * pi,u16 vsi_handle)1876*4882a593Smuzhiyun enum ice_status ice_rm_vsi_lan_cfg(struct ice_port_info *pi, u16 vsi_handle)
1877*4882a593Smuzhiyun {
1878*4882a593Smuzhiyun return ice_sched_rm_vsi_cfg(pi, vsi_handle, ICE_SCHED_NODE_OWNER_LAN);
1879*4882a593Smuzhiyun }
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun /**
1882*4882a593Smuzhiyun * ice_sched_rm_unused_rl_prof - remove unused RL profile
1883*4882a593Smuzhiyun * @pi: port information structure
1884*4882a593Smuzhiyun *
1885*4882a593Smuzhiyun * This function removes unused rate limit profiles from the HW and
1886*4882a593Smuzhiyun * SW DB. The caller needs to hold scheduler lock.
1887*4882a593Smuzhiyun */
ice_sched_rm_unused_rl_prof(struct ice_port_info * pi)1888*4882a593Smuzhiyun static void ice_sched_rm_unused_rl_prof(struct ice_port_info *pi)
1889*4882a593Smuzhiyun {
1890*4882a593Smuzhiyun u16 ln;
1891*4882a593Smuzhiyun
1892*4882a593Smuzhiyun for (ln = 0; ln < pi->hw->num_tx_sched_layers; ln++) {
1893*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_prof_elem;
1894*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_prof_tmp;
1895*4882a593Smuzhiyun
1896*4882a593Smuzhiyun list_for_each_entry_safe(rl_prof_elem, rl_prof_tmp,
1897*4882a593Smuzhiyun &pi->rl_prof_list[ln], list_entry) {
1898*4882a593Smuzhiyun if (!ice_sched_del_rl_profile(pi->hw, rl_prof_elem))
1899*4882a593Smuzhiyun ice_debug(pi->hw, ICE_DBG_SCHED,
1900*4882a593Smuzhiyun "Removed rl profile\n");
1901*4882a593Smuzhiyun }
1902*4882a593Smuzhiyun }
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun
1905*4882a593Smuzhiyun /**
1906*4882a593Smuzhiyun * ice_sched_update_elem - update element
1907*4882a593Smuzhiyun * @hw: pointer to the HW struct
1908*4882a593Smuzhiyun * @node: pointer to node
1909*4882a593Smuzhiyun * @info: node info to update
1910*4882a593Smuzhiyun *
1911*4882a593Smuzhiyun * Update the HW DB, and local SW DB of node. Update the scheduling
1912*4882a593Smuzhiyun * parameters of node from argument info data buffer (Info->data buf) and
1913*4882a593Smuzhiyun * returns success or error on config sched element failure. The caller
1914*4882a593Smuzhiyun * needs to hold scheduler lock.
1915*4882a593Smuzhiyun */
1916*4882a593Smuzhiyun static enum ice_status
ice_sched_update_elem(struct ice_hw * hw,struct ice_sched_node * node,struct ice_aqc_txsched_elem_data * info)1917*4882a593Smuzhiyun ice_sched_update_elem(struct ice_hw *hw, struct ice_sched_node *node,
1918*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data *info)
1919*4882a593Smuzhiyun {
1920*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data buf;
1921*4882a593Smuzhiyun enum ice_status status;
1922*4882a593Smuzhiyun u16 elem_cfgd = 0;
1923*4882a593Smuzhiyun u16 num_elems = 1;
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun buf = *info;
1926*4882a593Smuzhiyun /* Parent TEID is reserved field in this aq call */
1927*4882a593Smuzhiyun buf.parent_teid = 0;
1928*4882a593Smuzhiyun /* Element type is reserved field in this aq call */
1929*4882a593Smuzhiyun buf.data.elem_type = 0;
1930*4882a593Smuzhiyun /* Flags is reserved field in this aq call */
1931*4882a593Smuzhiyun buf.data.flags = 0;
1932*4882a593Smuzhiyun
1933*4882a593Smuzhiyun /* Update HW DB */
1934*4882a593Smuzhiyun /* Configure element node */
1935*4882a593Smuzhiyun status = ice_aq_cfg_sched_elems(hw, num_elems, &buf, sizeof(buf),
1936*4882a593Smuzhiyun &elem_cfgd, NULL);
1937*4882a593Smuzhiyun if (status || elem_cfgd != num_elems) {
1938*4882a593Smuzhiyun ice_debug(hw, ICE_DBG_SCHED, "Config sched elem error\n");
1939*4882a593Smuzhiyun return ICE_ERR_CFG;
1940*4882a593Smuzhiyun }
1941*4882a593Smuzhiyun
1942*4882a593Smuzhiyun /* Config success case */
1943*4882a593Smuzhiyun /* Now update local SW DB */
1944*4882a593Smuzhiyun /* Only copy the data portion of info buffer */
1945*4882a593Smuzhiyun node->info.data = info->data;
1946*4882a593Smuzhiyun return status;
1947*4882a593Smuzhiyun }
1948*4882a593Smuzhiyun
1949*4882a593Smuzhiyun /**
1950*4882a593Smuzhiyun * ice_sched_cfg_node_bw_alloc - configure node BW weight/alloc params
1951*4882a593Smuzhiyun * @hw: pointer to the HW struct
1952*4882a593Smuzhiyun * @node: sched node to configure
1953*4882a593Smuzhiyun * @rl_type: rate limit type CIR, EIR, or shared
1954*4882a593Smuzhiyun * @bw_alloc: BW weight/allocation
1955*4882a593Smuzhiyun *
1956*4882a593Smuzhiyun * This function configures node element's BW allocation.
1957*4882a593Smuzhiyun */
1958*4882a593Smuzhiyun static enum ice_status
ice_sched_cfg_node_bw_alloc(struct ice_hw * hw,struct ice_sched_node * node,enum ice_rl_type rl_type,u16 bw_alloc)1959*4882a593Smuzhiyun ice_sched_cfg_node_bw_alloc(struct ice_hw *hw, struct ice_sched_node *node,
1960*4882a593Smuzhiyun enum ice_rl_type rl_type, u16 bw_alloc)
1961*4882a593Smuzhiyun {
1962*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data buf;
1963*4882a593Smuzhiyun struct ice_aqc_txsched_elem *data;
1964*4882a593Smuzhiyun enum ice_status status;
1965*4882a593Smuzhiyun
1966*4882a593Smuzhiyun buf = node->info;
1967*4882a593Smuzhiyun data = &buf.data;
1968*4882a593Smuzhiyun if (rl_type == ICE_MIN_BW) {
1969*4882a593Smuzhiyun data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
1970*4882a593Smuzhiyun data->cir_bw.bw_alloc = cpu_to_le16(bw_alloc);
1971*4882a593Smuzhiyun } else if (rl_type == ICE_MAX_BW) {
1972*4882a593Smuzhiyun data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
1973*4882a593Smuzhiyun data->eir_bw.bw_alloc = cpu_to_le16(bw_alloc);
1974*4882a593Smuzhiyun } else {
1975*4882a593Smuzhiyun return ICE_ERR_PARAM;
1976*4882a593Smuzhiyun }
1977*4882a593Smuzhiyun
1978*4882a593Smuzhiyun /* Configure element */
1979*4882a593Smuzhiyun status = ice_sched_update_elem(hw, node, &buf);
1980*4882a593Smuzhiyun return status;
1981*4882a593Smuzhiyun }
1982*4882a593Smuzhiyun
1983*4882a593Smuzhiyun /**
1984*4882a593Smuzhiyun * ice_set_clear_cir_bw - set or clear CIR BW
1985*4882a593Smuzhiyun * @bw_t_info: bandwidth type information structure
1986*4882a593Smuzhiyun * @bw: bandwidth in Kbps - Kilo bits per sec
1987*4882a593Smuzhiyun *
1988*4882a593Smuzhiyun * Save or clear CIR bandwidth (BW) in the passed param bw_t_info.
1989*4882a593Smuzhiyun */
ice_set_clear_cir_bw(struct ice_bw_type_info * bw_t_info,u32 bw)1990*4882a593Smuzhiyun static void ice_set_clear_cir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
1991*4882a593Smuzhiyun {
1992*4882a593Smuzhiyun if (bw == ICE_SCHED_DFLT_BW) {
1993*4882a593Smuzhiyun clear_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
1994*4882a593Smuzhiyun bw_t_info->cir_bw.bw = 0;
1995*4882a593Smuzhiyun } else {
1996*4882a593Smuzhiyun /* Save type of BW information */
1997*4882a593Smuzhiyun set_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap);
1998*4882a593Smuzhiyun bw_t_info->cir_bw.bw = bw;
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun }
2001*4882a593Smuzhiyun
2002*4882a593Smuzhiyun /**
2003*4882a593Smuzhiyun * ice_set_clear_eir_bw - set or clear EIR BW
2004*4882a593Smuzhiyun * @bw_t_info: bandwidth type information structure
2005*4882a593Smuzhiyun * @bw: bandwidth in Kbps - Kilo bits per sec
2006*4882a593Smuzhiyun *
2007*4882a593Smuzhiyun * Save or clear EIR bandwidth (BW) in the passed param bw_t_info.
2008*4882a593Smuzhiyun */
ice_set_clear_eir_bw(struct ice_bw_type_info * bw_t_info,u32 bw)2009*4882a593Smuzhiyun static void ice_set_clear_eir_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
2010*4882a593Smuzhiyun {
2011*4882a593Smuzhiyun if (bw == ICE_SCHED_DFLT_BW) {
2012*4882a593Smuzhiyun clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
2013*4882a593Smuzhiyun bw_t_info->eir_bw.bw = 0;
2014*4882a593Smuzhiyun } else {
2015*4882a593Smuzhiyun /* EIR BW and Shared BW profiles are mutually exclusive and
2016*4882a593Smuzhiyun * hence only one of them may be set for any given element.
2017*4882a593Smuzhiyun * First clear earlier saved shared BW information.
2018*4882a593Smuzhiyun */
2019*4882a593Smuzhiyun clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
2020*4882a593Smuzhiyun bw_t_info->shared_bw = 0;
2021*4882a593Smuzhiyun /* save EIR BW information */
2022*4882a593Smuzhiyun set_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
2023*4882a593Smuzhiyun bw_t_info->eir_bw.bw = bw;
2024*4882a593Smuzhiyun }
2025*4882a593Smuzhiyun }
2026*4882a593Smuzhiyun
2027*4882a593Smuzhiyun /**
2028*4882a593Smuzhiyun * ice_set_clear_shared_bw - set or clear shared BW
2029*4882a593Smuzhiyun * @bw_t_info: bandwidth type information structure
2030*4882a593Smuzhiyun * @bw: bandwidth in Kbps - Kilo bits per sec
2031*4882a593Smuzhiyun *
2032*4882a593Smuzhiyun * Save or clear shared bandwidth (BW) in the passed param bw_t_info.
2033*4882a593Smuzhiyun */
ice_set_clear_shared_bw(struct ice_bw_type_info * bw_t_info,u32 bw)2034*4882a593Smuzhiyun static void ice_set_clear_shared_bw(struct ice_bw_type_info *bw_t_info, u32 bw)
2035*4882a593Smuzhiyun {
2036*4882a593Smuzhiyun if (bw == ICE_SCHED_DFLT_BW) {
2037*4882a593Smuzhiyun clear_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
2038*4882a593Smuzhiyun bw_t_info->shared_bw = 0;
2039*4882a593Smuzhiyun } else {
2040*4882a593Smuzhiyun /* EIR BW and Shared BW profiles are mutually exclusive and
2041*4882a593Smuzhiyun * hence only one of them may be set for any given element.
2042*4882a593Smuzhiyun * First clear earlier saved EIR BW information.
2043*4882a593Smuzhiyun */
2044*4882a593Smuzhiyun clear_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap);
2045*4882a593Smuzhiyun bw_t_info->eir_bw.bw = 0;
2046*4882a593Smuzhiyun /* save shared BW information */
2047*4882a593Smuzhiyun set_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap);
2048*4882a593Smuzhiyun bw_t_info->shared_bw = bw;
2049*4882a593Smuzhiyun }
2050*4882a593Smuzhiyun }
2051*4882a593Smuzhiyun
2052*4882a593Smuzhiyun /**
2053*4882a593Smuzhiyun * ice_sched_calc_wakeup - calculate RL profile wakeup parameter
2054*4882a593Smuzhiyun * @bw: bandwidth in Kbps
2055*4882a593Smuzhiyun *
2056*4882a593Smuzhiyun * This function calculates the wakeup parameter of RL profile.
2057*4882a593Smuzhiyun */
ice_sched_calc_wakeup(s32 bw)2058*4882a593Smuzhiyun static u16 ice_sched_calc_wakeup(s32 bw)
2059*4882a593Smuzhiyun {
2060*4882a593Smuzhiyun s64 bytes_per_sec, wakeup_int, wakeup_a, wakeup_b, wakeup_f;
2061*4882a593Smuzhiyun s32 wakeup_f_int;
2062*4882a593Smuzhiyun u16 wakeup = 0;
2063*4882a593Smuzhiyun
2064*4882a593Smuzhiyun /* Get the wakeup integer value */
2065*4882a593Smuzhiyun bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
2066*4882a593Smuzhiyun wakeup_int = div64_long(ICE_RL_PROF_FREQUENCY, bytes_per_sec);
2067*4882a593Smuzhiyun if (wakeup_int > 63) {
2068*4882a593Smuzhiyun wakeup = (u16)((1 << 15) | wakeup_int);
2069*4882a593Smuzhiyun } else {
2070*4882a593Smuzhiyun /* Calculate fraction value up to 4 decimals
2071*4882a593Smuzhiyun * Convert Integer value to a constant multiplier
2072*4882a593Smuzhiyun */
2073*4882a593Smuzhiyun wakeup_b = (s64)ICE_RL_PROF_MULTIPLIER * wakeup_int;
2074*4882a593Smuzhiyun wakeup_a = div64_long((s64)ICE_RL_PROF_MULTIPLIER *
2075*4882a593Smuzhiyun ICE_RL_PROF_FREQUENCY,
2076*4882a593Smuzhiyun bytes_per_sec);
2077*4882a593Smuzhiyun
2078*4882a593Smuzhiyun /* Get Fraction value */
2079*4882a593Smuzhiyun wakeup_f = wakeup_a - wakeup_b;
2080*4882a593Smuzhiyun
2081*4882a593Smuzhiyun /* Round up the Fractional value via Ceil(Fractional value) */
2082*4882a593Smuzhiyun if (wakeup_f > div64_long(ICE_RL_PROF_MULTIPLIER, 2))
2083*4882a593Smuzhiyun wakeup_f += 1;
2084*4882a593Smuzhiyun
2085*4882a593Smuzhiyun wakeup_f_int = (s32)div64_long(wakeup_f * ICE_RL_PROF_FRACTION,
2086*4882a593Smuzhiyun ICE_RL_PROF_MULTIPLIER);
2087*4882a593Smuzhiyun wakeup |= (u16)(wakeup_int << 9);
2088*4882a593Smuzhiyun wakeup |= (u16)(0x1ff & wakeup_f_int);
2089*4882a593Smuzhiyun }
2090*4882a593Smuzhiyun
2091*4882a593Smuzhiyun return wakeup;
2092*4882a593Smuzhiyun }
2093*4882a593Smuzhiyun
2094*4882a593Smuzhiyun /**
2095*4882a593Smuzhiyun * ice_sched_bw_to_rl_profile - convert BW to profile parameters
2096*4882a593Smuzhiyun * @bw: bandwidth in Kbps
2097*4882a593Smuzhiyun * @profile: profile parameters to return
2098*4882a593Smuzhiyun *
2099*4882a593Smuzhiyun * This function converts the BW to profile structure format.
2100*4882a593Smuzhiyun */
2101*4882a593Smuzhiyun static enum ice_status
ice_sched_bw_to_rl_profile(u32 bw,struct ice_aqc_rl_profile_elem * profile)2102*4882a593Smuzhiyun ice_sched_bw_to_rl_profile(u32 bw, struct ice_aqc_rl_profile_elem *profile)
2103*4882a593Smuzhiyun {
2104*4882a593Smuzhiyun enum ice_status status = ICE_ERR_PARAM;
2105*4882a593Smuzhiyun s64 bytes_per_sec, ts_rate, mv_tmp;
2106*4882a593Smuzhiyun bool found = false;
2107*4882a593Smuzhiyun s32 encode = 0;
2108*4882a593Smuzhiyun s64 mv = 0;
2109*4882a593Smuzhiyun s32 i;
2110*4882a593Smuzhiyun
2111*4882a593Smuzhiyun /* Bw settings range is from 0.5Mb/sec to 100Gb/sec */
2112*4882a593Smuzhiyun if (bw < ICE_SCHED_MIN_BW || bw > ICE_SCHED_MAX_BW)
2113*4882a593Smuzhiyun return status;
2114*4882a593Smuzhiyun
2115*4882a593Smuzhiyun /* Bytes per second from Kbps */
2116*4882a593Smuzhiyun bytes_per_sec = div64_long(((s64)bw * 1000), BITS_PER_BYTE);
2117*4882a593Smuzhiyun
2118*4882a593Smuzhiyun /* encode is 6 bits but really useful are 5 bits */
2119*4882a593Smuzhiyun for (i = 0; i < 64; i++) {
2120*4882a593Smuzhiyun u64 pow_result = BIT_ULL(i);
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun ts_rate = div64_long((s64)ICE_RL_PROF_FREQUENCY,
2123*4882a593Smuzhiyun pow_result * ICE_RL_PROF_TS_MULTIPLIER);
2124*4882a593Smuzhiyun if (ts_rate <= 0)
2125*4882a593Smuzhiyun continue;
2126*4882a593Smuzhiyun
2127*4882a593Smuzhiyun /* Multiplier value */
2128*4882a593Smuzhiyun mv_tmp = div64_long(bytes_per_sec * ICE_RL_PROF_MULTIPLIER,
2129*4882a593Smuzhiyun ts_rate);
2130*4882a593Smuzhiyun
2131*4882a593Smuzhiyun /* Round to the nearest ICE_RL_PROF_MULTIPLIER */
2132*4882a593Smuzhiyun mv = round_up_64bit(mv_tmp, ICE_RL_PROF_MULTIPLIER);
2133*4882a593Smuzhiyun
2134*4882a593Smuzhiyun /* First multiplier value greater than the given
2135*4882a593Smuzhiyun * accuracy bytes
2136*4882a593Smuzhiyun */
2137*4882a593Smuzhiyun if (mv > ICE_RL_PROF_ACCURACY_BYTES) {
2138*4882a593Smuzhiyun encode = i;
2139*4882a593Smuzhiyun found = true;
2140*4882a593Smuzhiyun break;
2141*4882a593Smuzhiyun }
2142*4882a593Smuzhiyun }
2143*4882a593Smuzhiyun if (found) {
2144*4882a593Smuzhiyun u16 wm;
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun wm = ice_sched_calc_wakeup(bw);
2147*4882a593Smuzhiyun profile->rl_multiply = cpu_to_le16(mv);
2148*4882a593Smuzhiyun profile->wake_up_calc = cpu_to_le16(wm);
2149*4882a593Smuzhiyun profile->rl_encode = cpu_to_le16(encode);
2150*4882a593Smuzhiyun status = 0;
2151*4882a593Smuzhiyun } else {
2152*4882a593Smuzhiyun status = ICE_ERR_DOES_NOT_EXIST;
2153*4882a593Smuzhiyun }
2154*4882a593Smuzhiyun
2155*4882a593Smuzhiyun return status;
2156*4882a593Smuzhiyun }
2157*4882a593Smuzhiyun
2158*4882a593Smuzhiyun /**
2159*4882a593Smuzhiyun * ice_sched_add_rl_profile - add RL profile
2160*4882a593Smuzhiyun * @pi: port information structure
2161*4882a593Smuzhiyun * @rl_type: type of rate limit BW - min, max, or shared
2162*4882a593Smuzhiyun * @bw: bandwidth in Kbps - Kilo bits per sec
2163*4882a593Smuzhiyun * @layer_num: specifies in which layer to create profile
2164*4882a593Smuzhiyun *
2165*4882a593Smuzhiyun * This function first checks the existing list for corresponding BW
2166*4882a593Smuzhiyun * parameter. If it exists, it returns the associated profile otherwise
2167*4882a593Smuzhiyun * it creates a new rate limit profile for requested BW, and adds it to
2168*4882a593Smuzhiyun * the HW DB and local list. It returns the new profile or null on error.
2169*4882a593Smuzhiyun * The caller needs to hold the scheduler lock.
2170*4882a593Smuzhiyun */
2171*4882a593Smuzhiyun static struct ice_aqc_rl_profile_info *
ice_sched_add_rl_profile(struct ice_port_info * pi,enum ice_rl_type rl_type,u32 bw,u8 layer_num)2172*4882a593Smuzhiyun ice_sched_add_rl_profile(struct ice_port_info *pi,
2173*4882a593Smuzhiyun enum ice_rl_type rl_type, u32 bw, u8 layer_num)
2174*4882a593Smuzhiyun {
2175*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_prof_elem;
2176*4882a593Smuzhiyun u16 profiles_added = 0, num_profiles = 1;
2177*4882a593Smuzhiyun struct ice_aqc_rl_profile_elem *buf;
2178*4882a593Smuzhiyun enum ice_status status;
2179*4882a593Smuzhiyun struct ice_hw *hw;
2180*4882a593Smuzhiyun u8 profile_type;
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
2183*4882a593Smuzhiyun return NULL;
2184*4882a593Smuzhiyun switch (rl_type) {
2185*4882a593Smuzhiyun case ICE_MIN_BW:
2186*4882a593Smuzhiyun profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
2187*4882a593Smuzhiyun break;
2188*4882a593Smuzhiyun case ICE_MAX_BW:
2189*4882a593Smuzhiyun profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
2190*4882a593Smuzhiyun break;
2191*4882a593Smuzhiyun case ICE_SHARED_BW:
2192*4882a593Smuzhiyun profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
2193*4882a593Smuzhiyun break;
2194*4882a593Smuzhiyun default:
2195*4882a593Smuzhiyun return NULL;
2196*4882a593Smuzhiyun }
2197*4882a593Smuzhiyun
2198*4882a593Smuzhiyun if (!pi)
2199*4882a593Smuzhiyun return NULL;
2200*4882a593Smuzhiyun hw = pi->hw;
2201*4882a593Smuzhiyun list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
2202*4882a593Smuzhiyun list_entry)
2203*4882a593Smuzhiyun if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
2204*4882a593Smuzhiyun profile_type && rl_prof_elem->bw == bw)
2205*4882a593Smuzhiyun /* Return existing profile ID info */
2206*4882a593Smuzhiyun return rl_prof_elem;
2207*4882a593Smuzhiyun
2208*4882a593Smuzhiyun /* Create new profile ID */
2209*4882a593Smuzhiyun rl_prof_elem = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*rl_prof_elem),
2210*4882a593Smuzhiyun GFP_KERNEL);
2211*4882a593Smuzhiyun
2212*4882a593Smuzhiyun if (!rl_prof_elem)
2213*4882a593Smuzhiyun return NULL;
2214*4882a593Smuzhiyun
2215*4882a593Smuzhiyun status = ice_sched_bw_to_rl_profile(bw, &rl_prof_elem->profile);
2216*4882a593Smuzhiyun if (status)
2217*4882a593Smuzhiyun goto exit_add_rl_prof;
2218*4882a593Smuzhiyun
2219*4882a593Smuzhiyun rl_prof_elem->bw = bw;
2220*4882a593Smuzhiyun /* layer_num is zero relative, and fw expects level from 1 to 9 */
2221*4882a593Smuzhiyun rl_prof_elem->profile.level = layer_num + 1;
2222*4882a593Smuzhiyun rl_prof_elem->profile.flags = profile_type;
2223*4882a593Smuzhiyun rl_prof_elem->profile.max_burst_size = cpu_to_le16(hw->max_burst_size);
2224*4882a593Smuzhiyun
2225*4882a593Smuzhiyun /* Create new entry in HW DB */
2226*4882a593Smuzhiyun buf = &rl_prof_elem->profile;
2227*4882a593Smuzhiyun status = ice_aq_add_rl_profile(hw, num_profiles, buf, sizeof(*buf),
2228*4882a593Smuzhiyun &profiles_added, NULL);
2229*4882a593Smuzhiyun if (status || profiles_added != num_profiles)
2230*4882a593Smuzhiyun goto exit_add_rl_prof;
2231*4882a593Smuzhiyun
2232*4882a593Smuzhiyun /* Good entry - add in the list */
2233*4882a593Smuzhiyun rl_prof_elem->prof_id_ref = 0;
2234*4882a593Smuzhiyun list_add(&rl_prof_elem->list_entry, &pi->rl_prof_list[layer_num]);
2235*4882a593Smuzhiyun return rl_prof_elem;
2236*4882a593Smuzhiyun
2237*4882a593Smuzhiyun exit_add_rl_prof:
2238*4882a593Smuzhiyun devm_kfree(ice_hw_to_dev(hw), rl_prof_elem);
2239*4882a593Smuzhiyun return NULL;
2240*4882a593Smuzhiyun }
2241*4882a593Smuzhiyun
2242*4882a593Smuzhiyun /**
2243*4882a593Smuzhiyun * ice_sched_cfg_node_bw_lmt - configure node sched params
2244*4882a593Smuzhiyun * @hw: pointer to the HW struct
2245*4882a593Smuzhiyun * @node: sched node to configure
2246*4882a593Smuzhiyun * @rl_type: rate limit type CIR, EIR, or shared
2247*4882a593Smuzhiyun * @rl_prof_id: rate limit profile ID
2248*4882a593Smuzhiyun *
2249*4882a593Smuzhiyun * This function configures node element's BW limit.
2250*4882a593Smuzhiyun */
2251*4882a593Smuzhiyun static enum ice_status
ice_sched_cfg_node_bw_lmt(struct ice_hw * hw,struct ice_sched_node * node,enum ice_rl_type rl_type,u16 rl_prof_id)2252*4882a593Smuzhiyun ice_sched_cfg_node_bw_lmt(struct ice_hw *hw, struct ice_sched_node *node,
2253*4882a593Smuzhiyun enum ice_rl_type rl_type, u16 rl_prof_id)
2254*4882a593Smuzhiyun {
2255*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data buf;
2256*4882a593Smuzhiyun struct ice_aqc_txsched_elem *data;
2257*4882a593Smuzhiyun
2258*4882a593Smuzhiyun buf = node->info;
2259*4882a593Smuzhiyun data = &buf.data;
2260*4882a593Smuzhiyun switch (rl_type) {
2261*4882a593Smuzhiyun case ICE_MIN_BW:
2262*4882a593Smuzhiyun data->valid_sections |= ICE_AQC_ELEM_VALID_CIR;
2263*4882a593Smuzhiyun data->cir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
2264*4882a593Smuzhiyun break;
2265*4882a593Smuzhiyun case ICE_MAX_BW:
2266*4882a593Smuzhiyun /* EIR BW and Shared BW profiles are mutually exclusive and
2267*4882a593Smuzhiyun * hence only one of them may be set for any given element
2268*4882a593Smuzhiyun */
2269*4882a593Smuzhiyun if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
2270*4882a593Smuzhiyun return ICE_ERR_CFG;
2271*4882a593Smuzhiyun data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
2272*4882a593Smuzhiyun data->eir_bw.bw_profile_idx = cpu_to_le16(rl_prof_id);
2273*4882a593Smuzhiyun break;
2274*4882a593Smuzhiyun case ICE_SHARED_BW:
2275*4882a593Smuzhiyun /* Check for removing shared BW */
2276*4882a593Smuzhiyun if (rl_prof_id == ICE_SCHED_NO_SHARED_RL_PROF_ID) {
2277*4882a593Smuzhiyun /* remove shared profile */
2278*4882a593Smuzhiyun data->valid_sections &= ~ICE_AQC_ELEM_VALID_SHARED;
2279*4882a593Smuzhiyun data->srl_id = 0; /* clear SRL field */
2280*4882a593Smuzhiyun
2281*4882a593Smuzhiyun /* enable back EIR to default profile */
2282*4882a593Smuzhiyun data->valid_sections |= ICE_AQC_ELEM_VALID_EIR;
2283*4882a593Smuzhiyun data->eir_bw.bw_profile_idx =
2284*4882a593Smuzhiyun cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
2285*4882a593Smuzhiyun break;
2286*4882a593Smuzhiyun }
2287*4882a593Smuzhiyun /* EIR BW and Shared BW profiles are mutually exclusive and
2288*4882a593Smuzhiyun * hence only one of them may be set for any given element
2289*4882a593Smuzhiyun */
2290*4882a593Smuzhiyun if ((data->valid_sections & ICE_AQC_ELEM_VALID_EIR) &&
2291*4882a593Smuzhiyun (le16_to_cpu(data->eir_bw.bw_profile_idx) !=
2292*4882a593Smuzhiyun ICE_SCHED_DFLT_RL_PROF_ID))
2293*4882a593Smuzhiyun return ICE_ERR_CFG;
2294*4882a593Smuzhiyun /* EIR BW is set to default, disable it */
2295*4882a593Smuzhiyun data->valid_sections &= ~ICE_AQC_ELEM_VALID_EIR;
2296*4882a593Smuzhiyun /* Okay to enable shared BW now */
2297*4882a593Smuzhiyun data->valid_sections |= ICE_AQC_ELEM_VALID_SHARED;
2298*4882a593Smuzhiyun data->srl_id = cpu_to_le16(rl_prof_id);
2299*4882a593Smuzhiyun break;
2300*4882a593Smuzhiyun default:
2301*4882a593Smuzhiyun /* Unknown rate limit type */
2302*4882a593Smuzhiyun return ICE_ERR_PARAM;
2303*4882a593Smuzhiyun }
2304*4882a593Smuzhiyun
2305*4882a593Smuzhiyun /* Configure element */
2306*4882a593Smuzhiyun return ice_sched_update_elem(hw, node, &buf);
2307*4882a593Smuzhiyun }
2308*4882a593Smuzhiyun
2309*4882a593Smuzhiyun /**
2310*4882a593Smuzhiyun * ice_sched_get_node_rl_prof_id - get node's rate limit profile ID
2311*4882a593Smuzhiyun * @node: sched node
2312*4882a593Smuzhiyun * @rl_type: rate limit type
2313*4882a593Smuzhiyun *
2314*4882a593Smuzhiyun * If existing profile matches, it returns the corresponding rate
2315*4882a593Smuzhiyun * limit profile ID, otherwise it returns an invalid ID as error.
2316*4882a593Smuzhiyun */
2317*4882a593Smuzhiyun static u16
ice_sched_get_node_rl_prof_id(struct ice_sched_node * node,enum ice_rl_type rl_type)2318*4882a593Smuzhiyun ice_sched_get_node_rl_prof_id(struct ice_sched_node *node,
2319*4882a593Smuzhiyun enum ice_rl_type rl_type)
2320*4882a593Smuzhiyun {
2321*4882a593Smuzhiyun u16 rl_prof_id = ICE_SCHED_INVAL_PROF_ID;
2322*4882a593Smuzhiyun struct ice_aqc_txsched_elem *data;
2323*4882a593Smuzhiyun
2324*4882a593Smuzhiyun data = &node->info.data;
2325*4882a593Smuzhiyun switch (rl_type) {
2326*4882a593Smuzhiyun case ICE_MIN_BW:
2327*4882a593Smuzhiyun if (data->valid_sections & ICE_AQC_ELEM_VALID_CIR)
2328*4882a593Smuzhiyun rl_prof_id = le16_to_cpu(data->cir_bw.bw_profile_idx);
2329*4882a593Smuzhiyun break;
2330*4882a593Smuzhiyun case ICE_MAX_BW:
2331*4882a593Smuzhiyun if (data->valid_sections & ICE_AQC_ELEM_VALID_EIR)
2332*4882a593Smuzhiyun rl_prof_id = le16_to_cpu(data->eir_bw.bw_profile_idx);
2333*4882a593Smuzhiyun break;
2334*4882a593Smuzhiyun case ICE_SHARED_BW:
2335*4882a593Smuzhiyun if (data->valid_sections & ICE_AQC_ELEM_VALID_SHARED)
2336*4882a593Smuzhiyun rl_prof_id = le16_to_cpu(data->srl_id);
2337*4882a593Smuzhiyun break;
2338*4882a593Smuzhiyun default:
2339*4882a593Smuzhiyun break;
2340*4882a593Smuzhiyun }
2341*4882a593Smuzhiyun
2342*4882a593Smuzhiyun return rl_prof_id;
2343*4882a593Smuzhiyun }
2344*4882a593Smuzhiyun
2345*4882a593Smuzhiyun /**
2346*4882a593Smuzhiyun * ice_sched_get_rl_prof_layer - selects rate limit profile creation layer
2347*4882a593Smuzhiyun * @pi: port information structure
2348*4882a593Smuzhiyun * @rl_type: type of rate limit BW - min, max, or shared
2349*4882a593Smuzhiyun * @layer_index: layer index
2350*4882a593Smuzhiyun *
2351*4882a593Smuzhiyun * This function returns requested profile creation layer.
2352*4882a593Smuzhiyun */
2353*4882a593Smuzhiyun static u8
ice_sched_get_rl_prof_layer(struct ice_port_info * pi,enum ice_rl_type rl_type,u8 layer_index)2354*4882a593Smuzhiyun ice_sched_get_rl_prof_layer(struct ice_port_info *pi, enum ice_rl_type rl_type,
2355*4882a593Smuzhiyun u8 layer_index)
2356*4882a593Smuzhiyun {
2357*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
2358*4882a593Smuzhiyun
2359*4882a593Smuzhiyun if (layer_index >= hw->num_tx_sched_layers)
2360*4882a593Smuzhiyun return ICE_SCHED_INVAL_LAYER_NUM;
2361*4882a593Smuzhiyun switch (rl_type) {
2362*4882a593Smuzhiyun case ICE_MIN_BW:
2363*4882a593Smuzhiyun if (hw->layer_info[layer_index].max_cir_rl_profiles)
2364*4882a593Smuzhiyun return layer_index;
2365*4882a593Smuzhiyun break;
2366*4882a593Smuzhiyun case ICE_MAX_BW:
2367*4882a593Smuzhiyun if (hw->layer_info[layer_index].max_eir_rl_profiles)
2368*4882a593Smuzhiyun return layer_index;
2369*4882a593Smuzhiyun break;
2370*4882a593Smuzhiyun case ICE_SHARED_BW:
2371*4882a593Smuzhiyun /* if current layer doesn't support SRL profile creation
2372*4882a593Smuzhiyun * then try a layer up or down.
2373*4882a593Smuzhiyun */
2374*4882a593Smuzhiyun if (hw->layer_info[layer_index].max_srl_profiles)
2375*4882a593Smuzhiyun return layer_index;
2376*4882a593Smuzhiyun else if (layer_index < hw->num_tx_sched_layers - 1 &&
2377*4882a593Smuzhiyun hw->layer_info[layer_index + 1].max_srl_profiles)
2378*4882a593Smuzhiyun return layer_index + 1;
2379*4882a593Smuzhiyun else if (layer_index > 0 &&
2380*4882a593Smuzhiyun hw->layer_info[layer_index - 1].max_srl_profiles)
2381*4882a593Smuzhiyun return layer_index - 1;
2382*4882a593Smuzhiyun break;
2383*4882a593Smuzhiyun default:
2384*4882a593Smuzhiyun break;
2385*4882a593Smuzhiyun }
2386*4882a593Smuzhiyun return ICE_SCHED_INVAL_LAYER_NUM;
2387*4882a593Smuzhiyun }
2388*4882a593Smuzhiyun
2389*4882a593Smuzhiyun /**
2390*4882a593Smuzhiyun * ice_sched_get_srl_node - get shared rate limit node
2391*4882a593Smuzhiyun * @node: tree node
2392*4882a593Smuzhiyun * @srl_layer: shared rate limit layer
2393*4882a593Smuzhiyun *
2394*4882a593Smuzhiyun * This function returns SRL node to be used for shared rate limit purpose.
2395*4882a593Smuzhiyun * The caller needs to hold scheduler lock.
2396*4882a593Smuzhiyun */
2397*4882a593Smuzhiyun static struct ice_sched_node *
ice_sched_get_srl_node(struct ice_sched_node * node,u8 srl_layer)2398*4882a593Smuzhiyun ice_sched_get_srl_node(struct ice_sched_node *node, u8 srl_layer)
2399*4882a593Smuzhiyun {
2400*4882a593Smuzhiyun if (srl_layer > node->tx_sched_layer)
2401*4882a593Smuzhiyun return node->children[0];
2402*4882a593Smuzhiyun else if (srl_layer < node->tx_sched_layer)
2403*4882a593Smuzhiyun /* Node can't be created without a parent. It will always
2404*4882a593Smuzhiyun * have a valid parent except root node.
2405*4882a593Smuzhiyun */
2406*4882a593Smuzhiyun return node->parent;
2407*4882a593Smuzhiyun else
2408*4882a593Smuzhiyun return node;
2409*4882a593Smuzhiyun }
2410*4882a593Smuzhiyun
2411*4882a593Smuzhiyun /**
2412*4882a593Smuzhiyun * ice_sched_rm_rl_profile - remove RL profile ID
2413*4882a593Smuzhiyun * @pi: port information structure
2414*4882a593Smuzhiyun * @layer_num: layer number where profiles are saved
2415*4882a593Smuzhiyun * @profile_type: profile type like EIR, CIR, or SRL
2416*4882a593Smuzhiyun * @profile_id: profile ID to remove
2417*4882a593Smuzhiyun *
2418*4882a593Smuzhiyun * This function removes rate limit profile from layer 'layer_num' of type
2419*4882a593Smuzhiyun * 'profile_type' and profile ID as 'profile_id'. The caller needs to hold
2420*4882a593Smuzhiyun * scheduler lock.
2421*4882a593Smuzhiyun */
2422*4882a593Smuzhiyun static enum ice_status
ice_sched_rm_rl_profile(struct ice_port_info * pi,u8 layer_num,u8 profile_type,u16 profile_id)2423*4882a593Smuzhiyun ice_sched_rm_rl_profile(struct ice_port_info *pi, u8 layer_num, u8 profile_type,
2424*4882a593Smuzhiyun u16 profile_id)
2425*4882a593Smuzhiyun {
2426*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_prof_elem;
2427*4882a593Smuzhiyun enum ice_status status = 0;
2428*4882a593Smuzhiyun
2429*4882a593Smuzhiyun if (layer_num >= ICE_AQC_TOPO_MAX_LEVEL_NUM)
2430*4882a593Smuzhiyun return ICE_ERR_PARAM;
2431*4882a593Smuzhiyun /* Check the existing list for RL profile */
2432*4882a593Smuzhiyun list_for_each_entry(rl_prof_elem, &pi->rl_prof_list[layer_num],
2433*4882a593Smuzhiyun list_entry)
2434*4882a593Smuzhiyun if ((rl_prof_elem->profile.flags & ICE_AQC_RL_PROFILE_TYPE_M) ==
2435*4882a593Smuzhiyun profile_type &&
2436*4882a593Smuzhiyun le16_to_cpu(rl_prof_elem->profile.profile_id) ==
2437*4882a593Smuzhiyun profile_id) {
2438*4882a593Smuzhiyun if (rl_prof_elem->prof_id_ref)
2439*4882a593Smuzhiyun rl_prof_elem->prof_id_ref--;
2440*4882a593Smuzhiyun
2441*4882a593Smuzhiyun /* Remove old profile ID from database */
2442*4882a593Smuzhiyun status = ice_sched_del_rl_profile(pi->hw, rl_prof_elem);
2443*4882a593Smuzhiyun if (status && status != ICE_ERR_IN_USE)
2444*4882a593Smuzhiyun ice_debug(pi->hw, ICE_DBG_SCHED,
2445*4882a593Smuzhiyun "Remove rl profile failed\n");
2446*4882a593Smuzhiyun break;
2447*4882a593Smuzhiyun }
2448*4882a593Smuzhiyun if (status == ICE_ERR_IN_USE)
2449*4882a593Smuzhiyun status = 0;
2450*4882a593Smuzhiyun return status;
2451*4882a593Smuzhiyun }
2452*4882a593Smuzhiyun
2453*4882a593Smuzhiyun /**
2454*4882a593Smuzhiyun * ice_sched_set_node_bw_dflt - set node's bandwidth limit to default
2455*4882a593Smuzhiyun * @pi: port information structure
2456*4882a593Smuzhiyun * @node: pointer to node structure
2457*4882a593Smuzhiyun * @rl_type: rate limit type min, max, or shared
2458*4882a593Smuzhiyun * @layer_num: layer number where RL profiles are saved
2459*4882a593Smuzhiyun *
2460*4882a593Smuzhiyun * This function configures node element's BW rate limit profile ID of
2461*4882a593Smuzhiyun * type CIR, EIR, or SRL to default. This function needs to be called
2462*4882a593Smuzhiyun * with the scheduler lock held.
2463*4882a593Smuzhiyun */
2464*4882a593Smuzhiyun static enum ice_status
ice_sched_set_node_bw_dflt(struct ice_port_info * pi,struct ice_sched_node * node,enum ice_rl_type rl_type,u8 layer_num)2465*4882a593Smuzhiyun ice_sched_set_node_bw_dflt(struct ice_port_info *pi,
2466*4882a593Smuzhiyun struct ice_sched_node *node,
2467*4882a593Smuzhiyun enum ice_rl_type rl_type, u8 layer_num)
2468*4882a593Smuzhiyun {
2469*4882a593Smuzhiyun enum ice_status status;
2470*4882a593Smuzhiyun struct ice_hw *hw;
2471*4882a593Smuzhiyun u8 profile_type;
2472*4882a593Smuzhiyun u16 rl_prof_id;
2473*4882a593Smuzhiyun u16 old_id;
2474*4882a593Smuzhiyun
2475*4882a593Smuzhiyun hw = pi->hw;
2476*4882a593Smuzhiyun switch (rl_type) {
2477*4882a593Smuzhiyun case ICE_MIN_BW:
2478*4882a593Smuzhiyun profile_type = ICE_AQC_RL_PROFILE_TYPE_CIR;
2479*4882a593Smuzhiyun rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
2480*4882a593Smuzhiyun break;
2481*4882a593Smuzhiyun case ICE_MAX_BW:
2482*4882a593Smuzhiyun profile_type = ICE_AQC_RL_PROFILE_TYPE_EIR;
2483*4882a593Smuzhiyun rl_prof_id = ICE_SCHED_DFLT_RL_PROF_ID;
2484*4882a593Smuzhiyun break;
2485*4882a593Smuzhiyun case ICE_SHARED_BW:
2486*4882a593Smuzhiyun profile_type = ICE_AQC_RL_PROFILE_TYPE_SRL;
2487*4882a593Smuzhiyun /* No SRL is configured for default case */
2488*4882a593Smuzhiyun rl_prof_id = ICE_SCHED_NO_SHARED_RL_PROF_ID;
2489*4882a593Smuzhiyun break;
2490*4882a593Smuzhiyun default:
2491*4882a593Smuzhiyun return ICE_ERR_PARAM;
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun /* Save existing RL prof ID for later clean up */
2494*4882a593Smuzhiyun old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
2495*4882a593Smuzhiyun /* Configure BW scheduling parameters */
2496*4882a593Smuzhiyun status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
2497*4882a593Smuzhiyun if (status)
2498*4882a593Smuzhiyun return status;
2499*4882a593Smuzhiyun
2500*4882a593Smuzhiyun /* Remove stale RL profile ID */
2501*4882a593Smuzhiyun if (old_id == ICE_SCHED_DFLT_RL_PROF_ID ||
2502*4882a593Smuzhiyun old_id == ICE_SCHED_INVAL_PROF_ID)
2503*4882a593Smuzhiyun return 0;
2504*4882a593Smuzhiyun
2505*4882a593Smuzhiyun return ice_sched_rm_rl_profile(pi, layer_num, profile_type, old_id);
2506*4882a593Smuzhiyun }
2507*4882a593Smuzhiyun
2508*4882a593Smuzhiyun /**
2509*4882a593Smuzhiyun * ice_sched_set_eir_srl_excl - set EIR/SRL exclusiveness
2510*4882a593Smuzhiyun * @pi: port information structure
2511*4882a593Smuzhiyun * @node: pointer to node structure
2512*4882a593Smuzhiyun * @layer_num: layer number where rate limit profiles are saved
2513*4882a593Smuzhiyun * @rl_type: rate limit type min, max, or shared
2514*4882a593Smuzhiyun * @bw: bandwidth value
2515*4882a593Smuzhiyun *
2516*4882a593Smuzhiyun * This function prepares node element's bandwidth to SRL or EIR exclusively.
2517*4882a593Smuzhiyun * EIR BW and Shared BW profiles are mutually exclusive and hence only one of
2518*4882a593Smuzhiyun * them may be set for any given element. This function needs to be called
2519*4882a593Smuzhiyun * with the scheduler lock held.
2520*4882a593Smuzhiyun */
2521*4882a593Smuzhiyun static enum ice_status
ice_sched_set_eir_srl_excl(struct ice_port_info * pi,struct ice_sched_node * node,u8 layer_num,enum ice_rl_type rl_type,u32 bw)2522*4882a593Smuzhiyun ice_sched_set_eir_srl_excl(struct ice_port_info *pi,
2523*4882a593Smuzhiyun struct ice_sched_node *node,
2524*4882a593Smuzhiyun u8 layer_num, enum ice_rl_type rl_type, u32 bw)
2525*4882a593Smuzhiyun {
2526*4882a593Smuzhiyun if (rl_type == ICE_SHARED_BW) {
2527*4882a593Smuzhiyun /* SRL node passed in this case, it may be different node */
2528*4882a593Smuzhiyun if (bw == ICE_SCHED_DFLT_BW)
2529*4882a593Smuzhiyun /* SRL being removed, ice_sched_cfg_node_bw_lmt()
2530*4882a593Smuzhiyun * enables EIR to default. EIR is not set in this
2531*4882a593Smuzhiyun * case, so no additional action is required.
2532*4882a593Smuzhiyun */
2533*4882a593Smuzhiyun return 0;
2534*4882a593Smuzhiyun
2535*4882a593Smuzhiyun /* SRL being configured, set EIR to default here.
2536*4882a593Smuzhiyun * ice_sched_cfg_node_bw_lmt() disables EIR when it
2537*4882a593Smuzhiyun * configures SRL
2538*4882a593Smuzhiyun */
2539*4882a593Smuzhiyun return ice_sched_set_node_bw_dflt(pi, node, ICE_MAX_BW,
2540*4882a593Smuzhiyun layer_num);
2541*4882a593Smuzhiyun } else if (rl_type == ICE_MAX_BW &&
2542*4882a593Smuzhiyun node->info.data.valid_sections & ICE_AQC_ELEM_VALID_SHARED) {
2543*4882a593Smuzhiyun /* Remove Shared profile. Set default shared BW call
2544*4882a593Smuzhiyun * removes shared profile for a node.
2545*4882a593Smuzhiyun */
2546*4882a593Smuzhiyun return ice_sched_set_node_bw_dflt(pi, node,
2547*4882a593Smuzhiyun ICE_SHARED_BW,
2548*4882a593Smuzhiyun layer_num);
2549*4882a593Smuzhiyun }
2550*4882a593Smuzhiyun return 0;
2551*4882a593Smuzhiyun }
2552*4882a593Smuzhiyun
2553*4882a593Smuzhiyun /**
2554*4882a593Smuzhiyun * ice_sched_set_node_bw - set node's bandwidth
2555*4882a593Smuzhiyun * @pi: port information structure
2556*4882a593Smuzhiyun * @node: tree node
2557*4882a593Smuzhiyun * @rl_type: rate limit type min, max, or shared
2558*4882a593Smuzhiyun * @bw: bandwidth in Kbps - Kilo bits per sec
2559*4882a593Smuzhiyun * @layer_num: layer number
2560*4882a593Smuzhiyun *
2561*4882a593Smuzhiyun * This function adds new profile corresponding to requested BW, configures
2562*4882a593Smuzhiyun * node's RL profile ID of type CIR, EIR, or SRL, and removes old profile
2563*4882a593Smuzhiyun * ID from local database. The caller needs to hold scheduler lock.
2564*4882a593Smuzhiyun */
2565*4882a593Smuzhiyun static enum ice_status
ice_sched_set_node_bw(struct ice_port_info * pi,struct ice_sched_node * node,enum ice_rl_type rl_type,u32 bw,u8 layer_num)2566*4882a593Smuzhiyun ice_sched_set_node_bw(struct ice_port_info *pi, struct ice_sched_node *node,
2567*4882a593Smuzhiyun enum ice_rl_type rl_type, u32 bw, u8 layer_num)
2568*4882a593Smuzhiyun {
2569*4882a593Smuzhiyun struct ice_aqc_rl_profile_info *rl_prof_info;
2570*4882a593Smuzhiyun enum ice_status status = ICE_ERR_PARAM;
2571*4882a593Smuzhiyun struct ice_hw *hw = pi->hw;
2572*4882a593Smuzhiyun u16 old_id, rl_prof_id;
2573*4882a593Smuzhiyun
2574*4882a593Smuzhiyun rl_prof_info = ice_sched_add_rl_profile(pi, rl_type, bw, layer_num);
2575*4882a593Smuzhiyun if (!rl_prof_info)
2576*4882a593Smuzhiyun return status;
2577*4882a593Smuzhiyun
2578*4882a593Smuzhiyun rl_prof_id = le16_to_cpu(rl_prof_info->profile.profile_id);
2579*4882a593Smuzhiyun
2580*4882a593Smuzhiyun /* Save existing RL prof ID for later clean up */
2581*4882a593Smuzhiyun old_id = ice_sched_get_node_rl_prof_id(node, rl_type);
2582*4882a593Smuzhiyun /* Configure BW scheduling parameters */
2583*4882a593Smuzhiyun status = ice_sched_cfg_node_bw_lmt(hw, node, rl_type, rl_prof_id);
2584*4882a593Smuzhiyun if (status)
2585*4882a593Smuzhiyun return status;
2586*4882a593Smuzhiyun
2587*4882a593Smuzhiyun /* New changes has been applied */
2588*4882a593Smuzhiyun /* Increment the profile ID reference count */
2589*4882a593Smuzhiyun rl_prof_info->prof_id_ref++;
2590*4882a593Smuzhiyun
2591*4882a593Smuzhiyun /* Check for old ID removal */
2592*4882a593Smuzhiyun if ((old_id == ICE_SCHED_DFLT_RL_PROF_ID && rl_type != ICE_SHARED_BW) ||
2593*4882a593Smuzhiyun old_id == ICE_SCHED_INVAL_PROF_ID || old_id == rl_prof_id)
2594*4882a593Smuzhiyun return 0;
2595*4882a593Smuzhiyun
2596*4882a593Smuzhiyun return ice_sched_rm_rl_profile(pi, layer_num,
2597*4882a593Smuzhiyun rl_prof_info->profile.flags &
2598*4882a593Smuzhiyun ICE_AQC_RL_PROFILE_TYPE_M, old_id);
2599*4882a593Smuzhiyun }
2600*4882a593Smuzhiyun
2601*4882a593Smuzhiyun /**
2602*4882a593Smuzhiyun * ice_sched_set_node_bw_lmt - set node's BW limit
2603*4882a593Smuzhiyun * @pi: port information structure
2604*4882a593Smuzhiyun * @node: tree node
2605*4882a593Smuzhiyun * @rl_type: rate limit type min, max, or shared
2606*4882a593Smuzhiyun * @bw: bandwidth in Kbps - Kilo bits per sec
2607*4882a593Smuzhiyun *
2608*4882a593Smuzhiyun * It updates node's BW limit parameters like BW RL profile ID of type CIR,
2609*4882a593Smuzhiyun * EIR, or SRL. The caller needs to hold scheduler lock.
2610*4882a593Smuzhiyun */
2611*4882a593Smuzhiyun static enum ice_status
ice_sched_set_node_bw_lmt(struct ice_port_info * pi,struct ice_sched_node * node,enum ice_rl_type rl_type,u32 bw)2612*4882a593Smuzhiyun ice_sched_set_node_bw_lmt(struct ice_port_info *pi, struct ice_sched_node *node,
2613*4882a593Smuzhiyun enum ice_rl_type rl_type, u32 bw)
2614*4882a593Smuzhiyun {
2615*4882a593Smuzhiyun struct ice_sched_node *cfg_node = node;
2616*4882a593Smuzhiyun enum ice_status status;
2617*4882a593Smuzhiyun
2618*4882a593Smuzhiyun struct ice_hw *hw;
2619*4882a593Smuzhiyun u8 layer_num;
2620*4882a593Smuzhiyun
2621*4882a593Smuzhiyun if (!pi)
2622*4882a593Smuzhiyun return ICE_ERR_PARAM;
2623*4882a593Smuzhiyun hw = pi->hw;
2624*4882a593Smuzhiyun /* Remove unused RL profile IDs from HW and SW DB */
2625*4882a593Smuzhiyun ice_sched_rm_unused_rl_prof(pi);
2626*4882a593Smuzhiyun layer_num = ice_sched_get_rl_prof_layer(pi, rl_type,
2627*4882a593Smuzhiyun node->tx_sched_layer);
2628*4882a593Smuzhiyun if (layer_num >= hw->num_tx_sched_layers)
2629*4882a593Smuzhiyun return ICE_ERR_PARAM;
2630*4882a593Smuzhiyun
2631*4882a593Smuzhiyun if (rl_type == ICE_SHARED_BW) {
2632*4882a593Smuzhiyun /* SRL node may be different */
2633*4882a593Smuzhiyun cfg_node = ice_sched_get_srl_node(node, layer_num);
2634*4882a593Smuzhiyun if (!cfg_node)
2635*4882a593Smuzhiyun return ICE_ERR_CFG;
2636*4882a593Smuzhiyun }
2637*4882a593Smuzhiyun /* EIR BW and Shared BW profiles are mutually exclusive and
2638*4882a593Smuzhiyun * hence only one of them may be set for any given element
2639*4882a593Smuzhiyun */
2640*4882a593Smuzhiyun status = ice_sched_set_eir_srl_excl(pi, cfg_node, layer_num, rl_type,
2641*4882a593Smuzhiyun bw);
2642*4882a593Smuzhiyun if (status)
2643*4882a593Smuzhiyun return status;
2644*4882a593Smuzhiyun if (bw == ICE_SCHED_DFLT_BW)
2645*4882a593Smuzhiyun return ice_sched_set_node_bw_dflt(pi, cfg_node, rl_type,
2646*4882a593Smuzhiyun layer_num);
2647*4882a593Smuzhiyun return ice_sched_set_node_bw(pi, cfg_node, rl_type, bw, layer_num);
2648*4882a593Smuzhiyun }
2649*4882a593Smuzhiyun
2650*4882a593Smuzhiyun /**
2651*4882a593Smuzhiyun * ice_sched_set_node_bw_dflt_lmt - set node's BW limit to default
2652*4882a593Smuzhiyun * @pi: port information structure
2653*4882a593Smuzhiyun * @node: pointer to node structure
2654*4882a593Smuzhiyun * @rl_type: rate limit type min, max, or shared
2655*4882a593Smuzhiyun *
2656*4882a593Smuzhiyun * This function configures node element's BW rate limit profile ID of
2657*4882a593Smuzhiyun * type CIR, EIR, or SRL to default. This function needs to be called
2658*4882a593Smuzhiyun * with the scheduler lock held.
2659*4882a593Smuzhiyun */
2660*4882a593Smuzhiyun static enum ice_status
ice_sched_set_node_bw_dflt_lmt(struct ice_port_info * pi,struct ice_sched_node * node,enum ice_rl_type rl_type)2661*4882a593Smuzhiyun ice_sched_set_node_bw_dflt_lmt(struct ice_port_info *pi,
2662*4882a593Smuzhiyun struct ice_sched_node *node,
2663*4882a593Smuzhiyun enum ice_rl_type rl_type)
2664*4882a593Smuzhiyun {
2665*4882a593Smuzhiyun return ice_sched_set_node_bw_lmt(pi, node, rl_type,
2666*4882a593Smuzhiyun ICE_SCHED_DFLT_BW);
2667*4882a593Smuzhiyun }
2668*4882a593Smuzhiyun
2669*4882a593Smuzhiyun /**
2670*4882a593Smuzhiyun * ice_sched_validate_srl_node - Check node for SRL applicability
2671*4882a593Smuzhiyun * @node: sched node to configure
2672*4882a593Smuzhiyun * @sel_layer: selected SRL layer
2673*4882a593Smuzhiyun *
2674*4882a593Smuzhiyun * This function checks if the SRL can be applied to a selected layer node on
2675*4882a593Smuzhiyun * behalf of the requested node (first argument). This function needs to be
2676*4882a593Smuzhiyun * called with scheduler lock held.
2677*4882a593Smuzhiyun */
2678*4882a593Smuzhiyun static enum ice_status
ice_sched_validate_srl_node(struct ice_sched_node * node,u8 sel_layer)2679*4882a593Smuzhiyun ice_sched_validate_srl_node(struct ice_sched_node *node, u8 sel_layer)
2680*4882a593Smuzhiyun {
2681*4882a593Smuzhiyun /* SRL profiles are not available on all layers. Check if the
2682*4882a593Smuzhiyun * SRL profile can be applied to a node above or below the
2683*4882a593Smuzhiyun * requested node. SRL configuration is possible only if the
2684*4882a593Smuzhiyun * selected layer's node has single child.
2685*4882a593Smuzhiyun */
2686*4882a593Smuzhiyun if (sel_layer == node->tx_sched_layer ||
2687*4882a593Smuzhiyun ((sel_layer == node->tx_sched_layer + 1) &&
2688*4882a593Smuzhiyun node->num_children == 1) ||
2689*4882a593Smuzhiyun ((sel_layer == node->tx_sched_layer - 1) &&
2690*4882a593Smuzhiyun (node->parent && node->parent->num_children == 1)))
2691*4882a593Smuzhiyun return 0;
2692*4882a593Smuzhiyun
2693*4882a593Smuzhiyun return ICE_ERR_CFG;
2694*4882a593Smuzhiyun }
2695*4882a593Smuzhiyun
2696*4882a593Smuzhiyun /**
2697*4882a593Smuzhiyun * ice_sched_save_q_bw - save queue node's BW information
2698*4882a593Smuzhiyun * @q_ctx: queue context structure
2699*4882a593Smuzhiyun * @rl_type: rate limit type min, max, or shared
2700*4882a593Smuzhiyun * @bw: bandwidth in Kbps - Kilo bits per sec
2701*4882a593Smuzhiyun *
2702*4882a593Smuzhiyun * Save BW information of queue type node for post replay use.
2703*4882a593Smuzhiyun */
2704*4882a593Smuzhiyun static enum ice_status
ice_sched_save_q_bw(struct ice_q_ctx * q_ctx,enum ice_rl_type rl_type,u32 bw)2705*4882a593Smuzhiyun ice_sched_save_q_bw(struct ice_q_ctx *q_ctx, enum ice_rl_type rl_type, u32 bw)
2706*4882a593Smuzhiyun {
2707*4882a593Smuzhiyun switch (rl_type) {
2708*4882a593Smuzhiyun case ICE_MIN_BW:
2709*4882a593Smuzhiyun ice_set_clear_cir_bw(&q_ctx->bw_t_info, bw);
2710*4882a593Smuzhiyun break;
2711*4882a593Smuzhiyun case ICE_MAX_BW:
2712*4882a593Smuzhiyun ice_set_clear_eir_bw(&q_ctx->bw_t_info, bw);
2713*4882a593Smuzhiyun break;
2714*4882a593Smuzhiyun case ICE_SHARED_BW:
2715*4882a593Smuzhiyun ice_set_clear_shared_bw(&q_ctx->bw_t_info, bw);
2716*4882a593Smuzhiyun break;
2717*4882a593Smuzhiyun default:
2718*4882a593Smuzhiyun return ICE_ERR_PARAM;
2719*4882a593Smuzhiyun }
2720*4882a593Smuzhiyun return 0;
2721*4882a593Smuzhiyun }
2722*4882a593Smuzhiyun
2723*4882a593Smuzhiyun /**
2724*4882a593Smuzhiyun * ice_sched_set_q_bw_lmt - sets queue BW limit
2725*4882a593Smuzhiyun * @pi: port information structure
2726*4882a593Smuzhiyun * @vsi_handle: sw VSI handle
2727*4882a593Smuzhiyun * @tc: traffic class
2728*4882a593Smuzhiyun * @q_handle: software queue handle
2729*4882a593Smuzhiyun * @rl_type: min, max, or shared
2730*4882a593Smuzhiyun * @bw: bandwidth in Kbps
2731*4882a593Smuzhiyun *
2732*4882a593Smuzhiyun * This function sets BW limit of queue scheduling node.
2733*4882a593Smuzhiyun */
2734*4882a593Smuzhiyun static enum ice_status
ice_sched_set_q_bw_lmt(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u16 q_handle,enum ice_rl_type rl_type,u32 bw)2735*4882a593Smuzhiyun ice_sched_set_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
2736*4882a593Smuzhiyun u16 q_handle, enum ice_rl_type rl_type, u32 bw)
2737*4882a593Smuzhiyun {
2738*4882a593Smuzhiyun enum ice_status status = ICE_ERR_PARAM;
2739*4882a593Smuzhiyun struct ice_sched_node *node;
2740*4882a593Smuzhiyun struct ice_q_ctx *q_ctx;
2741*4882a593Smuzhiyun
2742*4882a593Smuzhiyun if (!ice_is_vsi_valid(pi->hw, vsi_handle))
2743*4882a593Smuzhiyun return ICE_ERR_PARAM;
2744*4882a593Smuzhiyun mutex_lock(&pi->sched_lock);
2745*4882a593Smuzhiyun q_ctx = ice_get_lan_q_ctx(pi->hw, vsi_handle, tc, q_handle);
2746*4882a593Smuzhiyun if (!q_ctx)
2747*4882a593Smuzhiyun goto exit_q_bw_lmt;
2748*4882a593Smuzhiyun node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
2749*4882a593Smuzhiyun if (!node) {
2750*4882a593Smuzhiyun ice_debug(pi->hw, ICE_DBG_SCHED, "Wrong q_teid\n");
2751*4882a593Smuzhiyun goto exit_q_bw_lmt;
2752*4882a593Smuzhiyun }
2753*4882a593Smuzhiyun
2754*4882a593Smuzhiyun /* Return error if it is not a leaf node */
2755*4882a593Smuzhiyun if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF)
2756*4882a593Smuzhiyun goto exit_q_bw_lmt;
2757*4882a593Smuzhiyun
2758*4882a593Smuzhiyun /* SRL bandwidth layer selection */
2759*4882a593Smuzhiyun if (rl_type == ICE_SHARED_BW) {
2760*4882a593Smuzhiyun u8 sel_layer; /* selected layer */
2761*4882a593Smuzhiyun
2762*4882a593Smuzhiyun sel_layer = ice_sched_get_rl_prof_layer(pi, rl_type,
2763*4882a593Smuzhiyun node->tx_sched_layer);
2764*4882a593Smuzhiyun if (sel_layer >= pi->hw->num_tx_sched_layers) {
2765*4882a593Smuzhiyun status = ICE_ERR_PARAM;
2766*4882a593Smuzhiyun goto exit_q_bw_lmt;
2767*4882a593Smuzhiyun }
2768*4882a593Smuzhiyun status = ice_sched_validate_srl_node(node, sel_layer);
2769*4882a593Smuzhiyun if (status)
2770*4882a593Smuzhiyun goto exit_q_bw_lmt;
2771*4882a593Smuzhiyun }
2772*4882a593Smuzhiyun
2773*4882a593Smuzhiyun if (bw == ICE_SCHED_DFLT_BW)
2774*4882a593Smuzhiyun status = ice_sched_set_node_bw_dflt_lmt(pi, node, rl_type);
2775*4882a593Smuzhiyun else
2776*4882a593Smuzhiyun status = ice_sched_set_node_bw_lmt(pi, node, rl_type, bw);
2777*4882a593Smuzhiyun
2778*4882a593Smuzhiyun if (!status)
2779*4882a593Smuzhiyun status = ice_sched_save_q_bw(q_ctx, rl_type, bw);
2780*4882a593Smuzhiyun
2781*4882a593Smuzhiyun exit_q_bw_lmt:
2782*4882a593Smuzhiyun mutex_unlock(&pi->sched_lock);
2783*4882a593Smuzhiyun return status;
2784*4882a593Smuzhiyun }
2785*4882a593Smuzhiyun
2786*4882a593Smuzhiyun /**
2787*4882a593Smuzhiyun * ice_cfg_q_bw_lmt - configure queue BW limit
2788*4882a593Smuzhiyun * @pi: port information structure
2789*4882a593Smuzhiyun * @vsi_handle: sw VSI handle
2790*4882a593Smuzhiyun * @tc: traffic class
2791*4882a593Smuzhiyun * @q_handle: software queue handle
2792*4882a593Smuzhiyun * @rl_type: min, max, or shared
2793*4882a593Smuzhiyun * @bw: bandwidth in Kbps
2794*4882a593Smuzhiyun *
2795*4882a593Smuzhiyun * This function configures BW limit of queue scheduling node.
2796*4882a593Smuzhiyun */
2797*4882a593Smuzhiyun enum ice_status
ice_cfg_q_bw_lmt(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u16 q_handle,enum ice_rl_type rl_type,u32 bw)2798*4882a593Smuzhiyun ice_cfg_q_bw_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
2799*4882a593Smuzhiyun u16 q_handle, enum ice_rl_type rl_type, u32 bw)
2800*4882a593Smuzhiyun {
2801*4882a593Smuzhiyun return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
2802*4882a593Smuzhiyun bw);
2803*4882a593Smuzhiyun }
2804*4882a593Smuzhiyun
2805*4882a593Smuzhiyun /**
2806*4882a593Smuzhiyun * ice_cfg_q_bw_dflt_lmt - configure queue BW default limit
2807*4882a593Smuzhiyun * @pi: port information structure
2808*4882a593Smuzhiyun * @vsi_handle: sw VSI handle
2809*4882a593Smuzhiyun * @tc: traffic class
2810*4882a593Smuzhiyun * @q_handle: software queue handle
2811*4882a593Smuzhiyun * @rl_type: min, max, or shared
2812*4882a593Smuzhiyun *
2813*4882a593Smuzhiyun * This function configures BW default limit of queue scheduling node.
2814*4882a593Smuzhiyun */
2815*4882a593Smuzhiyun enum ice_status
ice_cfg_q_bw_dflt_lmt(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u16 q_handle,enum ice_rl_type rl_type)2816*4882a593Smuzhiyun ice_cfg_q_bw_dflt_lmt(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
2817*4882a593Smuzhiyun u16 q_handle, enum ice_rl_type rl_type)
2818*4882a593Smuzhiyun {
2819*4882a593Smuzhiyun return ice_sched_set_q_bw_lmt(pi, vsi_handle, tc, q_handle, rl_type,
2820*4882a593Smuzhiyun ICE_SCHED_DFLT_BW);
2821*4882a593Smuzhiyun }
2822*4882a593Smuzhiyun
2823*4882a593Smuzhiyun /**
2824*4882a593Smuzhiyun * ice_cfg_rl_burst_size - Set burst size value
2825*4882a593Smuzhiyun * @hw: pointer to the HW struct
2826*4882a593Smuzhiyun * @bytes: burst size in bytes
2827*4882a593Smuzhiyun *
2828*4882a593Smuzhiyun * This function configures/set the burst size to requested new value. The new
2829*4882a593Smuzhiyun * burst size value is used for future rate limit calls. It doesn't change the
2830*4882a593Smuzhiyun * existing or previously created RL profiles.
2831*4882a593Smuzhiyun */
ice_cfg_rl_burst_size(struct ice_hw * hw,u32 bytes)2832*4882a593Smuzhiyun enum ice_status ice_cfg_rl_burst_size(struct ice_hw *hw, u32 bytes)
2833*4882a593Smuzhiyun {
2834*4882a593Smuzhiyun u16 burst_size_to_prog;
2835*4882a593Smuzhiyun
2836*4882a593Smuzhiyun if (bytes < ICE_MIN_BURST_SIZE_ALLOWED ||
2837*4882a593Smuzhiyun bytes > ICE_MAX_BURST_SIZE_ALLOWED)
2838*4882a593Smuzhiyun return ICE_ERR_PARAM;
2839*4882a593Smuzhiyun if (ice_round_to_num(bytes, 64) <=
2840*4882a593Smuzhiyun ICE_MAX_BURST_SIZE_64_BYTE_GRANULARITY) {
2841*4882a593Smuzhiyun /* 64 byte granularity case */
2842*4882a593Smuzhiyun /* Disable MSB granularity bit */
2843*4882a593Smuzhiyun burst_size_to_prog = ICE_64_BYTE_GRANULARITY;
2844*4882a593Smuzhiyun /* round number to nearest 64 byte granularity */
2845*4882a593Smuzhiyun bytes = ice_round_to_num(bytes, 64);
2846*4882a593Smuzhiyun /* The value is in 64 byte chunks */
2847*4882a593Smuzhiyun burst_size_to_prog |= (u16)(bytes / 64);
2848*4882a593Smuzhiyun } else {
2849*4882a593Smuzhiyun /* k bytes granularity case */
2850*4882a593Smuzhiyun /* Enable MSB granularity bit */
2851*4882a593Smuzhiyun burst_size_to_prog = ICE_KBYTE_GRANULARITY;
2852*4882a593Smuzhiyun /* round number to nearest 1024 granularity */
2853*4882a593Smuzhiyun bytes = ice_round_to_num(bytes, 1024);
2854*4882a593Smuzhiyun /* check rounding doesn't go beyond allowed */
2855*4882a593Smuzhiyun if (bytes > ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY)
2856*4882a593Smuzhiyun bytes = ICE_MAX_BURST_SIZE_KBYTE_GRANULARITY;
2857*4882a593Smuzhiyun /* The value is in k bytes */
2858*4882a593Smuzhiyun burst_size_to_prog |= (u16)(bytes / 1024);
2859*4882a593Smuzhiyun }
2860*4882a593Smuzhiyun hw->max_burst_size = burst_size_to_prog;
2861*4882a593Smuzhiyun return 0;
2862*4882a593Smuzhiyun }
2863*4882a593Smuzhiyun
2864*4882a593Smuzhiyun /**
2865*4882a593Smuzhiyun * ice_sched_replay_node_prio - re-configure node priority
2866*4882a593Smuzhiyun * @hw: pointer to the HW struct
2867*4882a593Smuzhiyun * @node: sched node to configure
2868*4882a593Smuzhiyun * @priority: priority value
2869*4882a593Smuzhiyun *
2870*4882a593Smuzhiyun * This function configures node element's priority value. It
2871*4882a593Smuzhiyun * needs to be called with scheduler lock held.
2872*4882a593Smuzhiyun */
2873*4882a593Smuzhiyun static enum ice_status
ice_sched_replay_node_prio(struct ice_hw * hw,struct ice_sched_node * node,u8 priority)2874*4882a593Smuzhiyun ice_sched_replay_node_prio(struct ice_hw *hw, struct ice_sched_node *node,
2875*4882a593Smuzhiyun u8 priority)
2876*4882a593Smuzhiyun {
2877*4882a593Smuzhiyun struct ice_aqc_txsched_elem_data buf;
2878*4882a593Smuzhiyun struct ice_aqc_txsched_elem *data;
2879*4882a593Smuzhiyun enum ice_status status;
2880*4882a593Smuzhiyun
2881*4882a593Smuzhiyun buf = node->info;
2882*4882a593Smuzhiyun data = &buf.data;
2883*4882a593Smuzhiyun data->valid_sections |= ICE_AQC_ELEM_VALID_GENERIC;
2884*4882a593Smuzhiyun data->generic = priority;
2885*4882a593Smuzhiyun
2886*4882a593Smuzhiyun /* Configure element */
2887*4882a593Smuzhiyun status = ice_sched_update_elem(hw, node, &buf);
2888*4882a593Smuzhiyun return status;
2889*4882a593Smuzhiyun }
2890*4882a593Smuzhiyun
2891*4882a593Smuzhiyun /**
2892*4882a593Smuzhiyun * ice_sched_replay_node_bw - replay node(s) BW
2893*4882a593Smuzhiyun * @hw: pointer to the HW struct
2894*4882a593Smuzhiyun * @node: sched node to configure
2895*4882a593Smuzhiyun * @bw_t_info: BW type information
2896*4882a593Smuzhiyun *
2897*4882a593Smuzhiyun * This function restores node's BW from bw_t_info. The caller needs
2898*4882a593Smuzhiyun * to hold the scheduler lock.
2899*4882a593Smuzhiyun */
2900*4882a593Smuzhiyun static enum ice_status
ice_sched_replay_node_bw(struct ice_hw * hw,struct ice_sched_node * node,struct ice_bw_type_info * bw_t_info)2901*4882a593Smuzhiyun ice_sched_replay_node_bw(struct ice_hw *hw, struct ice_sched_node *node,
2902*4882a593Smuzhiyun struct ice_bw_type_info *bw_t_info)
2903*4882a593Smuzhiyun {
2904*4882a593Smuzhiyun struct ice_port_info *pi = hw->port_info;
2905*4882a593Smuzhiyun enum ice_status status = ICE_ERR_PARAM;
2906*4882a593Smuzhiyun u16 bw_alloc;
2907*4882a593Smuzhiyun
2908*4882a593Smuzhiyun if (!node)
2909*4882a593Smuzhiyun return status;
2910*4882a593Smuzhiyun if (bitmap_empty(bw_t_info->bw_t_bitmap, ICE_BW_TYPE_CNT))
2911*4882a593Smuzhiyun return 0;
2912*4882a593Smuzhiyun if (test_bit(ICE_BW_TYPE_PRIO, bw_t_info->bw_t_bitmap)) {
2913*4882a593Smuzhiyun status = ice_sched_replay_node_prio(hw, node,
2914*4882a593Smuzhiyun bw_t_info->generic);
2915*4882a593Smuzhiyun if (status)
2916*4882a593Smuzhiyun return status;
2917*4882a593Smuzhiyun }
2918*4882a593Smuzhiyun if (test_bit(ICE_BW_TYPE_CIR, bw_t_info->bw_t_bitmap)) {
2919*4882a593Smuzhiyun status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW,
2920*4882a593Smuzhiyun bw_t_info->cir_bw.bw);
2921*4882a593Smuzhiyun if (status)
2922*4882a593Smuzhiyun return status;
2923*4882a593Smuzhiyun }
2924*4882a593Smuzhiyun if (test_bit(ICE_BW_TYPE_CIR_WT, bw_t_info->bw_t_bitmap)) {
2925*4882a593Smuzhiyun bw_alloc = bw_t_info->cir_bw.bw_alloc;
2926*4882a593Smuzhiyun status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MIN_BW,
2927*4882a593Smuzhiyun bw_alloc);
2928*4882a593Smuzhiyun if (status)
2929*4882a593Smuzhiyun return status;
2930*4882a593Smuzhiyun }
2931*4882a593Smuzhiyun if (test_bit(ICE_BW_TYPE_EIR, bw_t_info->bw_t_bitmap)) {
2932*4882a593Smuzhiyun status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW,
2933*4882a593Smuzhiyun bw_t_info->eir_bw.bw);
2934*4882a593Smuzhiyun if (status)
2935*4882a593Smuzhiyun return status;
2936*4882a593Smuzhiyun }
2937*4882a593Smuzhiyun if (test_bit(ICE_BW_TYPE_EIR_WT, bw_t_info->bw_t_bitmap)) {
2938*4882a593Smuzhiyun bw_alloc = bw_t_info->eir_bw.bw_alloc;
2939*4882a593Smuzhiyun status = ice_sched_cfg_node_bw_alloc(hw, node, ICE_MAX_BW,
2940*4882a593Smuzhiyun bw_alloc);
2941*4882a593Smuzhiyun if (status)
2942*4882a593Smuzhiyun return status;
2943*4882a593Smuzhiyun }
2944*4882a593Smuzhiyun if (test_bit(ICE_BW_TYPE_SHARED, bw_t_info->bw_t_bitmap))
2945*4882a593Smuzhiyun status = ice_sched_set_node_bw_lmt(pi, node, ICE_SHARED_BW,
2946*4882a593Smuzhiyun bw_t_info->shared_bw);
2947*4882a593Smuzhiyun return status;
2948*4882a593Smuzhiyun }
2949*4882a593Smuzhiyun
2950*4882a593Smuzhiyun /**
2951*4882a593Smuzhiyun * ice_sched_replay_q_bw - replay queue type node BW
2952*4882a593Smuzhiyun * @pi: port information structure
2953*4882a593Smuzhiyun * @q_ctx: queue context structure
2954*4882a593Smuzhiyun *
2955*4882a593Smuzhiyun * This function replays queue type node bandwidth. This function needs to be
2956*4882a593Smuzhiyun * called with scheduler lock held.
2957*4882a593Smuzhiyun */
2958*4882a593Smuzhiyun enum ice_status
ice_sched_replay_q_bw(struct ice_port_info * pi,struct ice_q_ctx * q_ctx)2959*4882a593Smuzhiyun ice_sched_replay_q_bw(struct ice_port_info *pi, struct ice_q_ctx *q_ctx)
2960*4882a593Smuzhiyun {
2961*4882a593Smuzhiyun struct ice_sched_node *q_node;
2962*4882a593Smuzhiyun
2963*4882a593Smuzhiyun /* Following also checks the presence of node in tree */
2964*4882a593Smuzhiyun q_node = ice_sched_find_node_by_teid(pi->root, q_ctx->q_teid);
2965*4882a593Smuzhiyun if (!q_node)
2966*4882a593Smuzhiyun return ICE_ERR_PARAM;
2967*4882a593Smuzhiyun return ice_sched_replay_node_bw(pi->hw, q_node, &q_ctx->bw_t_info);
2968*4882a593Smuzhiyun }
2969