ice_sched.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. #include "ice_sched.h"
  4. /**
  5. * ice_aq_delete_sched_elems - delete scheduler elements
  6. * @hw: pointer to the hw struct
  7. * @grps_req: number of groups to delete
  8. * @buf: pointer to buffer
  9. * @buf_size: buffer size in bytes
  10. * @grps_del: returns total number of elements deleted
  11. * @cd: pointer to command details structure or NULL
  12. *
  13. * Delete scheduling elements (0x040F)
  14. */
  15. static enum ice_status
  16. ice_aq_delete_sched_elems(struct ice_hw *hw, u16 grps_req,
  17. struct ice_aqc_delete_elem *buf, u16 buf_size,
  18. u16 *grps_del, struct ice_sq_cd *cd)
  19. {
  20. struct ice_aqc_add_move_delete_elem *cmd;
  21. struct ice_aq_desc desc;
  22. enum ice_status status;
  23. cmd = &desc.params.add_move_delete_elem;
  24. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_delete_sched_elems);
  25. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  26. cmd->num_grps_req = cpu_to_le16(grps_req);
  27. status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
  28. if (!status && grps_del)
  29. *grps_del = le16_to_cpu(cmd->num_grps_updated);
  30. return status;
  31. }
  32. /**
  33. * ice_sched_remove_elems - remove nodes from hw
  34. * @hw: pointer to the hw struct
  35. * @parent: pointer to the parent node
  36. * @num_nodes: number of nodes
  37. * @node_teids: array of node teids to be deleted
  38. *
  39. * This function remove nodes from hw
  40. */
  41. static enum ice_status
  42. ice_sched_remove_elems(struct ice_hw *hw, struct ice_sched_node *parent,
  43. u16 num_nodes, u32 *node_teids)
  44. {
  45. struct ice_aqc_delete_elem *buf;
  46. u16 i, num_groups_removed = 0;
  47. enum ice_status status;
  48. u16 buf_size;
  49. buf_size = sizeof(*buf) + sizeof(u32) * (num_nodes - 1);
  50. buf = devm_kzalloc(ice_hw_to_dev(hw), buf_size, GFP_KERNEL);
  51. if (!buf)
  52. return ICE_ERR_NO_MEMORY;
  53. buf->hdr.parent_teid = parent->info.node_teid;
  54. buf->hdr.num_elems = cpu_to_le16(num_nodes);
  55. for (i = 0; i < num_nodes; i++)
  56. buf->teid[i] = cpu_to_le32(node_teids[i]);
  57. status = ice_aq_delete_sched_elems(hw, 1, buf, buf_size,
  58. &num_groups_removed, NULL);
  59. if (status || num_groups_removed != 1)
  60. ice_debug(hw, ICE_DBG_SCHED, "remove elements failed\n");
  61. devm_kfree(ice_hw_to_dev(hw), buf);
  62. return status;
  63. }
  64. /**
  65. * ice_sched_get_first_node - get the first node of the given layer
  66. * @hw: pointer to the hw struct
  67. * @parent: pointer the base node of the subtree
  68. * @layer: layer number
  69. *
  70. * This function retrieves the first node of the given layer from the subtree
  71. */
  72. static struct ice_sched_node *
  73. ice_sched_get_first_node(struct ice_hw *hw, struct ice_sched_node *parent,
  74. u8 layer)
  75. {
  76. u8 i;
  77. if (layer < hw->sw_entry_point_layer)
  78. return NULL;
  79. for (i = 0; i < parent->num_children; i++) {
  80. struct ice_sched_node *node = parent->children[i];
  81. if (node) {
  82. if (node->tx_sched_layer == layer)
  83. return node;
  84. /* this recursion is intentional, and wouldn't
  85. * go more than 9 calls
  86. */
  87. return ice_sched_get_first_node(hw, node, layer);
  88. }
  89. }
  90. return NULL;
  91. }
  92. /**
  93. * ice_sched_get_tc_node - get pointer to TC node
  94. * @pi: port information structure
  95. * @tc: TC number
  96. *
  97. * This function returns the TC node pointer
  98. */
  99. struct ice_sched_node *ice_sched_get_tc_node(struct ice_port_info *pi, u8 tc)
  100. {
  101. u8 i;
  102. if (!pi)
  103. return NULL;
  104. for (i = 0; i < pi->root->num_children; i++)
  105. if (pi->root->children[i]->tc_num == tc)
  106. return pi->root->children[i];
  107. return NULL;
  108. }
  109. /**
  110. * ice_free_sched_node - Free a Tx scheduler node from SW DB
  111. * @pi: port information structure
  112. * @node: pointer to the ice_sched_node struct
  113. *
  114. * This function frees up a node from SW DB as well as from HW
  115. *
  116. * This function needs to be called with the port_info->sched_lock held
  117. */
  118. void ice_free_sched_node(struct ice_port_info *pi, struct ice_sched_node *node)
  119. {
  120. struct ice_sched_node *parent;
  121. struct ice_hw *hw = pi->hw;
  122. u8 i, j;
  123. /* Free the children before freeing up the parent node
  124. * The parent array is updated below and that shifts the nodes
  125. * in the array. So always pick the first child if num children > 0
  126. */
  127. while (node->num_children)
  128. ice_free_sched_node(pi, node->children[0]);
  129. /* Leaf, TC and root nodes can't be deleted by SW */
  130. if (node->tx_sched_layer >= hw->sw_entry_point_layer &&
  131. node->info.data.elem_type != ICE_AQC_ELEM_TYPE_TC &&
  132. node->info.data.elem_type != ICE_AQC_ELEM_TYPE_ROOT_PORT &&
  133. node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF) {
  134. u32 teid = le32_to_cpu(node->info.node_teid);
  135. enum ice_status status;
  136. status = ice_sched_remove_elems(hw, node->parent, 1, &teid);
  137. if (status)
  138. ice_debug(hw, ICE_DBG_SCHED,
  139. "remove element failed %d\n", status);
  140. }
  141. parent = node->parent;
  142. /* root has no parent */
  143. if (parent) {
  144. struct ice_sched_node *p, *tc_node;
  145. /* update the parent */
  146. for (i = 0; i < parent->num_children; i++)
  147. if (parent->children[i] == node) {
  148. for (j = i + 1; j < parent->num_children; j++)
  149. parent->children[j - 1] =
  150. parent->children[j];
  151. parent->num_children--;
  152. break;
  153. }
  154. /* search for previous sibling that points to this node and
  155. * remove the reference
  156. */
  157. tc_node = ice_sched_get_tc_node(pi, node->tc_num);
  158. if (!tc_node) {
  159. ice_debug(hw, ICE_DBG_SCHED,
  160. "Invalid TC number %d\n", node->tc_num);
  161. goto err_exit;
  162. }
  163. p = ice_sched_get_first_node(hw, tc_node, node->tx_sched_layer);
  164. while (p) {
  165. if (p->sibling == node) {
  166. p->sibling = node->sibling;
  167. break;
  168. }
  169. p = p->sibling;
  170. }
  171. }
  172. err_exit:
  173. /* leaf nodes have no children */
  174. if (node->children)
  175. devm_kfree(ice_hw_to_dev(hw), node->children);
  176. devm_kfree(ice_hw_to_dev(hw), node);
  177. }
  178. /**
  179. * ice_aq_query_sched_res - query scheduler resource
  180. * @hw: pointer to the hw struct
  181. * @buf_size: buffer size in bytes
  182. * @buf: pointer to buffer
  183. * @cd: pointer to command details structure or NULL
  184. *
  185. * Query scheduler resource allocation (0x0412)
  186. */
  187. static enum ice_status
  188. ice_aq_query_sched_res(struct ice_hw *hw, u16 buf_size,
  189. struct ice_aqc_query_txsched_res_resp *buf,
  190. struct ice_sq_cd *cd)
  191. {
  192. struct ice_aq_desc desc;
  193. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_sched_res);
  194. return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
  195. }
  196. /**
  197. * ice_sched_clear_tx_topo - clears the schduler tree nodes
  198. * @pi: port information structure
  199. *
  200. * This function removes all the nodes from HW as well as from SW DB.
  201. */
  202. static void ice_sched_clear_tx_topo(struct ice_port_info *pi)
  203. {
  204. struct ice_sched_agg_info *agg_info;
  205. struct ice_sched_vsi_info *vsi_elem;
  206. struct ice_sched_agg_info *atmp;
  207. struct ice_sched_vsi_info *tmp;
  208. struct ice_hw *hw;
  209. if (!pi)
  210. return;
  211. hw = pi->hw;
  212. list_for_each_entry_safe(agg_info, atmp, &pi->agg_list, list_entry) {
  213. struct ice_sched_agg_vsi_info *agg_vsi_info;
  214. struct ice_sched_agg_vsi_info *vtmp;
  215. list_for_each_entry_safe(agg_vsi_info, vtmp,
  216. &agg_info->agg_vsi_list, list_entry) {
  217. list_del(&agg_vsi_info->list_entry);
  218. devm_kfree(ice_hw_to_dev(hw), agg_vsi_info);
  219. }
  220. }
  221. /* remove the vsi list */
  222. list_for_each_entry_safe(vsi_elem, tmp, &pi->vsi_info_list,
  223. list_entry) {
  224. list_del(&vsi_elem->list_entry);
  225. devm_kfree(ice_hw_to_dev(hw), vsi_elem);
  226. }
  227. if (pi->root) {
  228. ice_free_sched_node(pi, pi->root);
  229. pi->root = NULL;
  230. }
  231. }
  232. /**
  233. * ice_sched_clear_port - clear the scheduler elements from SW DB for a port
  234. * @pi: port information structure
  235. *
  236. * Cleanup scheduling elements from SW DB
  237. */
  238. static void ice_sched_clear_port(struct ice_port_info *pi)
  239. {
  240. if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
  241. return;
  242. pi->port_state = ICE_SCHED_PORT_STATE_INIT;
  243. mutex_lock(&pi->sched_lock);
  244. ice_sched_clear_tx_topo(pi);
  245. mutex_unlock(&pi->sched_lock);
  246. mutex_destroy(&pi->sched_lock);
  247. }
  248. /**
  249. * ice_sched_cleanup_all - cleanup scheduler elements from SW DB for all ports
  250. * @hw: pointer to the hw struct
  251. *
  252. * Cleanup scheduling elements from SW DB for all the ports
  253. */
  254. void ice_sched_cleanup_all(struct ice_hw *hw)
  255. {
  256. if (!hw || !hw->port_info)
  257. return;
  258. if (hw->layer_info)
  259. devm_kfree(ice_hw_to_dev(hw), hw->layer_info);
  260. ice_sched_clear_port(hw->port_info);
  261. hw->num_tx_sched_layers = 0;
  262. hw->num_tx_sched_phys_layers = 0;
  263. hw->flattened_layers = 0;
  264. hw->max_cgds = 0;
  265. }
  266. /**
  267. * ice_sched_query_res_alloc - query the FW for num of logical sched layers
  268. * @hw: pointer to the HW struct
  269. *
  270. * query FW for allocated scheduler resources and store in HW struct
  271. */
  272. enum ice_status ice_sched_query_res_alloc(struct ice_hw *hw)
  273. {
  274. struct ice_aqc_query_txsched_res_resp *buf;
  275. enum ice_status status = 0;
  276. if (hw->layer_info)
  277. return status;
  278. buf = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*buf), GFP_KERNEL);
  279. if (!buf)
  280. return ICE_ERR_NO_MEMORY;
  281. status = ice_aq_query_sched_res(hw, sizeof(*buf), buf, NULL);
  282. if (status)
  283. goto sched_query_out;
  284. hw->num_tx_sched_layers = le16_to_cpu(buf->sched_props.logical_levels);
  285. hw->num_tx_sched_phys_layers =
  286. le16_to_cpu(buf->sched_props.phys_levels);
  287. hw->flattened_layers = buf->sched_props.flattening_bitmap;
  288. hw->max_cgds = buf->sched_props.max_pf_cgds;
  289. hw->layer_info = devm_kmemdup(ice_hw_to_dev(hw), buf->layer_props,
  290. (hw->num_tx_sched_layers *
  291. sizeof(*hw->layer_info)),
  292. GFP_KERNEL);
  293. if (!hw->layer_info) {
  294. status = ICE_ERR_NO_MEMORY;
  295. goto sched_query_out;
  296. }
  297. sched_query_out:
  298. devm_kfree(ice_hw_to_dev(hw), buf);
  299. return status;
  300. }