hnae3.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright (c) 2016-2017 Hisilicon Limited.
  3. #include <linux/list.h>
  4. #include <linux/spinlock.h>
  5. #include "hnae3.h"
  6. static LIST_HEAD(hnae3_ae_algo_list);
  7. static LIST_HEAD(hnae3_client_list);
  8. static LIST_HEAD(hnae3_ae_dev_list);
  9. void hnae3_unregister_ae_algo_prepare(struct hnae3_ae_algo *ae_algo)
  10. {
  11. const struct pci_device_id *pci_id;
  12. struct hnae3_ae_dev *ae_dev;
  13. if (!ae_algo)
  14. return;
  15. list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
  16. if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
  17. continue;
  18. pci_id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
  19. if (!pci_id)
  20. continue;
  21. if (IS_ENABLED(CONFIG_PCI_IOV))
  22. pci_disable_sriov(ae_dev->pdev);
  23. }
  24. }
  25. EXPORT_SYMBOL(hnae3_unregister_ae_algo_prepare);
  26. /* we are keeping things simple and using single lock for all the
  27. * list. This is a non-critical code so other updations, if happen
  28. * in parallel, can wait.
  29. */
  30. static DEFINE_MUTEX(hnae3_common_lock);
  31. static bool hnae3_client_match(enum hnae3_client_type client_type)
  32. {
  33. if (client_type == HNAE3_CLIENT_KNIC ||
  34. client_type == HNAE3_CLIENT_ROCE)
  35. return true;
  36. return false;
  37. }
  38. void hnae3_set_client_init_flag(struct hnae3_client *client,
  39. struct hnae3_ae_dev *ae_dev,
  40. unsigned int inited)
  41. {
  42. if (!client || !ae_dev)
  43. return;
  44. switch (client->type) {
  45. case HNAE3_CLIENT_KNIC:
  46. hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited);
  47. break;
  48. case HNAE3_CLIENT_ROCE:
  49. hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited);
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. EXPORT_SYMBOL(hnae3_set_client_init_flag);
  56. static int hnae3_get_client_init_flag(struct hnae3_client *client,
  57. struct hnae3_ae_dev *ae_dev)
  58. {
  59. int inited = 0;
  60. switch (client->type) {
  61. case HNAE3_CLIENT_KNIC:
  62. inited = hnae3_get_bit(ae_dev->flag,
  63. HNAE3_KNIC_CLIENT_INITED_B);
  64. break;
  65. case HNAE3_CLIENT_ROCE:
  66. inited = hnae3_get_bit(ae_dev->flag,
  67. HNAE3_ROCE_CLIENT_INITED_B);
  68. break;
  69. default:
  70. break;
  71. }
  72. return inited;
  73. }
  74. static int hnae3_init_client_instance(struct hnae3_client *client,
  75. struct hnae3_ae_dev *ae_dev)
  76. {
  77. int ret;
  78. /* check if this client matches the type of ae_dev */
  79. if (!(hnae3_client_match(client->type) &&
  80. hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) {
  81. return 0;
  82. }
  83. ret = ae_dev->ops->init_client_instance(client, ae_dev);
  84. if (ret)
  85. dev_err(&ae_dev->pdev->dev,
  86. "fail to instantiate client, ret = %d\n", ret);
  87. return ret;
  88. }
  89. static void hnae3_uninit_client_instance(struct hnae3_client *client,
  90. struct hnae3_ae_dev *ae_dev)
  91. {
  92. /* check if this client matches the type of ae_dev */
  93. if (!(hnae3_client_match(client->type) &&
  94. hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B)))
  95. return;
  96. if (hnae3_get_client_init_flag(client, ae_dev)) {
  97. ae_dev->ops->uninit_client_instance(client, ae_dev);
  98. hnae3_set_client_init_flag(client, ae_dev, 0);
  99. }
  100. }
  101. int hnae3_register_client(struct hnae3_client *client)
  102. {
  103. struct hnae3_client *client_tmp;
  104. struct hnae3_ae_dev *ae_dev;
  105. if (!client)
  106. return -ENODEV;
  107. mutex_lock(&hnae3_common_lock);
  108. /* one system should only have one client for every type */
  109. list_for_each_entry(client_tmp, &hnae3_client_list, node) {
  110. if (client_tmp->type == client->type)
  111. goto exit;
  112. }
  113. list_add_tail(&client->node, &hnae3_client_list);
  114. /* initialize the client on every matched port */
  115. list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
  116. /* if the client could not be initialized on current port, for
  117. * any error reasons, move on to next available port
  118. */
  119. int ret = hnae3_init_client_instance(client, ae_dev);
  120. if (ret)
  121. dev_err(&ae_dev->pdev->dev,
  122. "match and instantiation failed for port, ret = %d\n",
  123. ret);
  124. }
  125. exit:
  126. mutex_unlock(&hnae3_common_lock);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(hnae3_register_client);
  130. void hnae3_unregister_client(struct hnae3_client *client)
  131. {
  132. struct hnae3_client *client_tmp;
  133. struct hnae3_ae_dev *ae_dev;
  134. bool existed = false;
  135. if (!client)
  136. return;
  137. mutex_lock(&hnae3_common_lock);
  138. /* one system should only have one client for every type */
  139. list_for_each_entry(client_tmp, &hnae3_client_list, node) {
  140. if (client_tmp->type == client->type) {
  141. existed = true;
  142. break;
  143. }
  144. }
  145. if (!existed) {
  146. mutex_unlock(&hnae3_common_lock);
  147. pr_err("client %s does not exist!\n", client->name);
  148. return;
  149. }
  150. /* un-initialize the client on every matched port */
  151. list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
  152. hnae3_uninit_client_instance(client, ae_dev);
  153. }
  154. list_del(&client->node);
  155. mutex_unlock(&hnae3_common_lock);
  156. }
  157. EXPORT_SYMBOL(hnae3_unregister_client);
  158. /* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework
  159. * @ae_algo: AE algorithm
  160. * NOTE: the duplicated name will not be checked
  161. */
  162. void hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo)
  163. {
  164. const struct pci_device_id *id;
  165. struct hnae3_ae_dev *ae_dev;
  166. struct hnae3_client *client;
  167. int ret;
  168. if (!ae_algo)
  169. return;
  170. mutex_lock(&hnae3_common_lock);
  171. list_add_tail(&ae_algo->node, &hnae3_ae_algo_list);
  172. /* Check if this algo/ops matches the list of ae_devs */
  173. list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
  174. id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
  175. if (!id)
  176. continue;
  177. if (!ae_algo->ops) {
  178. dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
  179. continue;
  180. }
  181. ae_dev->ops = ae_algo->ops;
  182. ret = ae_algo->ops->init_ae_dev(ae_dev);
  183. if (ret) {
  184. dev_err(&ae_dev->pdev->dev,
  185. "init ae_dev error, ret = %d\n", ret);
  186. continue;
  187. }
  188. /* ae_dev init should set flag */
  189. hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
  190. /* check the client list for the match with this ae_dev type and
  191. * initialize the figure out client instance
  192. */
  193. list_for_each_entry(client, &hnae3_client_list, node) {
  194. ret = hnae3_init_client_instance(client, ae_dev);
  195. if (ret)
  196. dev_err(&ae_dev->pdev->dev,
  197. "match and instantiation failed, ret = %d\n",
  198. ret);
  199. }
  200. }
  201. mutex_unlock(&hnae3_common_lock);
  202. }
  203. EXPORT_SYMBOL(hnae3_register_ae_algo);
  204. /* hnae3_unregister_ae_algo - unregisters a AE algorithm
  205. * @ae_algo: the AE algorithm to unregister
  206. */
  207. void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo)
  208. {
  209. const struct pci_device_id *id;
  210. struct hnae3_ae_dev *ae_dev;
  211. struct hnae3_client *client;
  212. if (!ae_algo)
  213. return;
  214. mutex_lock(&hnae3_common_lock);
  215. /* Check if there are matched ae_dev */
  216. list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) {
  217. if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
  218. continue;
  219. id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
  220. if (!id)
  221. continue;
  222. /* check the client list for the match with this ae_dev type and
  223. * un-initialize the figure out client instance
  224. */
  225. list_for_each_entry(client, &hnae3_client_list, node)
  226. hnae3_uninit_client_instance(client, ae_dev);
  227. ae_algo->ops->uninit_ae_dev(ae_dev);
  228. hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
  229. ae_dev->ops = NULL;
  230. }
  231. list_del(&ae_algo->node);
  232. mutex_unlock(&hnae3_common_lock);
  233. }
  234. EXPORT_SYMBOL(hnae3_unregister_ae_algo);
  235. /* hnae3_register_ae_dev - registers a AE device to hnae3 framework
  236. * @ae_dev: the AE device
  237. * NOTE: the duplicated name will not be checked
  238. */
  239. int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev)
  240. {
  241. const struct pci_device_id *id;
  242. struct hnae3_ae_algo *ae_algo;
  243. struct hnae3_client *client;
  244. int ret;
  245. if (!ae_dev)
  246. return -ENODEV;
  247. mutex_lock(&hnae3_common_lock);
  248. list_add_tail(&ae_dev->node, &hnae3_ae_dev_list);
  249. /* Check if there are matched ae_algo */
  250. list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
  251. id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
  252. if (!id)
  253. continue;
  254. if (!ae_algo->ops) {
  255. dev_err(&ae_dev->pdev->dev, "ae_algo ops are null\n");
  256. ret = -EOPNOTSUPP;
  257. goto out_err;
  258. }
  259. ae_dev->ops = ae_algo->ops;
  260. ret = ae_dev->ops->init_ae_dev(ae_dev);
  261. if (ret) {
  262. dev_err(&ae_dev->pdev->dev,
  263. "init ae_dev error, ret = %d\n", ret);
  264. goto out_err;
  265. }
  266. /* ae_dev init should set flag */
  267. hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1);
  268. break;
  269. }
  270. /* check the client list for the match with this ae_dev type and
  271. * initialize the figure out client instance
  272. */
  273. list_for_each_entry(client, &hnae3_client_list, node) {
  274. ret = hnae3_init_client_instance(client, ae_dev);
  275. if (ret)
  276. dev_err(&ae_dev->pdev->dev,
  277. "match and instantiation failed, ret = %d\n",
  278. ret);
  279. }
  280. mutex_unlock(&hnae3_common_lock);
  281. return 0;
  282. out_err:
  283. list_del(&ae_dev->node);
  284. mutex_unlock(&hnae3_common_lock);
  285. return ret;
  286. }
  287. EXPORT_SYMBOL(hnae3_register_ae_dev);
  288. /* hnae3_unregister_ae_dev - unregisters a AE device
  289. * @ae_dev: the AE device to unregister
  290. */
  291. void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev)
  292. {
  293. const struct pci_device_id *id;
  294. struct hnae3_ae_algo *ae_algo;
  295. struct hnae3_client *client;
  296. if (!ae_dev)
  297. return;
  298. mutex_lock(&hnae3_common_lock);
  299. /* Check if there are matched ae_algo */
  300. list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) {
  301. if (!hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))
  302. continue;
  303. id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev);
  304. if (!id)
  305. continue;
  306. list_for_each_entry(client, &hnae3_client_list, node)
  307. hnae3_uninit_client_instance(client, ae_dev);
  308. ae_algo->ops->uninit_ae_dev(ae_dev);
  309. hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0);
  310. ae_dev->ops = NULL;
  311. }
  312. list_del(&ae_dev->node);
  313. mutex_unlock(&hnae3_common_lock);
  314. }
  315. EXPORT_SYMBOL(hnae3_unregister_ae_dev);
  316. MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
  317. MODULE_LICENSE("GPL");
  318. MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework");
  319. MODULE_VERSION(HNAE3_MOD_VERSION);