qede_roce.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* QLogic qedr NIC Driver
  2. * Copyright (c) 2015-2016 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/pci.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/list.h>
  35. #include <linux/mutex.h>
  36. #include <linux/qed/qede_roce.h>
  37. #include "qede.h"
  38. static struct qedr_driver *qedr_drv;
  39. static LIST_HEAD(qedr_dev_list);
  40. static DEFINE_MUTEX(qedr_dev_list_lock);
  41. bool qede_roce_supported(struct qede_dev *dev)
  42. {
  43. return dev->dev_info.common.rdma_supported;
  44. }
  45. static void _qede_roce_dev_add(struct qede_dev *edev)
  46. {
  47. if (!qedr_drv)
  48. return;
  49. edev->rdma_info.qedr_dev = qedr_drv->add(edev->cdev, edev->pdev,
  50. edev->ndev);
  51. }
  52. static int qede_roce_create_wq(struct qede_dev *edev)
  53. {
  54. INIT_LIST_HEAD(&edev->rdma_info.roce_event_list);
  55. edev->rdma_info.roce_wq = create_singlethread_workqueue("roce_wq");
  56. if (!edev->rdma_info.roce_wq) {
  57. DP_NOTICE(edev, "qedr: Could not create workqueue\n");
  58. return -ENOMEM;
  59. }
  60. return 0;
  61. }
  62. static void qede_roce_cleanup_event(struct qede_dev *edev)
  63. {
  64. struct list_head *head = &edev->rdma_info.roce_event_list;
  65. struct qede_roce_event_work *event_node;
  66. flush_workqueue(edev->rdma_info.roce_wq);
  67. while (!list_empty(head)) {
  68. event_node = list_entry(head->next, struct qede_roce_event_work,
  69. list);
  70. cancel_work_sync(&event_node->work);
  71. list_del(&event_node->list);
  72. kfree(event_node);
  73. }
  74. }
  75. static void qede_roce_destroy_wq(struct qede_dev *edev)
  76. {
  77. qede_roce_cleanup_event(edev);
  78. destroy_workqueue(edev->rdma_info.roce_wq);
  79. }
  80. int qede_roce_dev_add(struct qede_dev *edev)
  81. {
  82. int rc = 0;
  83. if (qede_roce_supported(edev)) {
  84. rc = qede_roce_create_wq(edev);
  85. if (rc)
  86. return rc;
  87. INIT_LIST_HEAD(&edev->rdma_info.entry);
  88. mutex_lock(&qedr_dev_list_lock);
  89. list_add_tail(&edev->rdma_info.entry, &qedr_dev_list);
  90. _qede_roce_dev_add(edev);
  91. mutex_unlock(&qedr_dev_list_lock);
  92. }
  93. return rc;
  94. }
  95. static void _qede_roce_dev_remove(struct qede_dev *edev)
  96. {
  97. if (qedr_drv && qedr_drv->remove && edev->rdma_info.qedr_dev)
  98. qedr_drv->remove(edev->rdma_info.qedr_dev);
  99. edev->rdma_info.qedr_dev = NULL;
  100. }
  101. void qede_roce_dev_remove(struct qede_dev *edev)
  102. {
  103. if (!qede_roce_supported(edev))
  104. return;
  105. qede_roce_destroy_wq(edev);
  106. mutex_lock(&qedr_dev_list_lock);
  107. _qede_roce_dev_remove(edev);
  108. list_del(&edev->rdma_info.entry);
  109. mutex_unlock(&qedr_dev_list_lock);
  110. }
  111. static void _qede_roce_dev_open(struct qede_dev *edev)
  112. {
  113. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  114. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_UP);
  115. }
  116. static void qede_roce_dev_open(struct qede_dev *edev)
  117. {
  118. if (!qede_roce_supported(edev))
  119. return;
  120. mutex_lock(&qedr_dev_list_lock);
  121. _qede_roce_dev_open(edev);
  122. mutex_unlock(&qedr_dev_list_lock);
  123. }
  124. static void _qede_roce_dev_close(struct qede_dev *edev)
  125. {
  126. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  127. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_DOWN);
  128. }
  129. static void qede_roce_dev_close(struct qede_dev *edev)
  130. {
  131. if (!qede_roce_supported(edev))
  132. return;
  133. mutex_lock(&qedr_dev_list_lock);
  134. _qede_roce_dev_close(edev);
  135. mutex_unlock(&qedr_dev_list_lock);
  136. }
  137. static void qede_roce_dev_shutdown(struct qede_dev *edev)
  138. {
  139. if (!qede_roce_supported(edev))
  140. return;
  141. mutex_lock(&qedr_dev_list_lock);
  142. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  143. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CLOSE);
  144. mutex_unlock(&qedr_dev_list_lock);
  145. }
  146. int qede_roce_register_driver(struct qedr_driver *drv)
  147. {
  148. struct qede_dev *edev;
  149. u8 qedr_counter = 0;
  150. mutex_lock(&qedr_dev_list_lock);
  151. if (qedr_drv) {
  152. mutex_unlock(&qedr_dev_list_lock);
  153. return -EINVAL;
  154. }
  155. qedr_drv = drv;
  156. list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
  157. struct net_device *ndev;
  158. qedr_counter++;
  159. _qede_roce_dev_add(edev);
  160. ndev = edev->ndev;
  161. if (netif_running(ndev) && netif_oper_up(ndev))
  162. _qede_roce_dev_open(edev);
  163. }
  164. mutex_unlock(&qedr_dev_list_lock);
  165. DP_INFO(edev, "qedr: discovered and registered %d RoCE funcs\n",
  166. qedr_counter);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL(qede_roce_register_driver);
  170. void qede_roce_unregister_driver(struct qedr_driver *drv)
  171. {
  172. struct qede_dev *edev;
  173. mutex_lock(&qedr_dev_list_lock);
  174. list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
  175. if (edev->rdma_info.qedr_dev)
  176. _qede_roce_dev_remove(edev);
  177. }
  178. qedr_drv = NULL;
  179. mutex_unlock(&qedr_dev_list_lock);
  180. }
  181. EXPORT_SYMBOL(qede_roce_unregister_driver);
  182. static void qede_roce_changeaddr(struct qede_dev *edev)
  183. {
  184. if (!qede_roce_supported(edev))
  185. return;
  186. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  187. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CHANGE_ADDR);
  188. }
  189. struct qede_roce_event_work *qede_roce_get_free_event_node(struct qede_dev
  190. *edev)
  191. {
  192. struct qede_roce_event_work *event_node = NULL;
  193. struct list_head *list_node = NULL;
  194. bool found = false;
  195. list_for_each(list_node, &edev->rdma_info.roce_event_list) {
  196. event_node = list_entry(list_node, struct qede_roce_event_work,
  197. list);
  198. if (!work_pending(&event_node->work)) {
  199. found = true;
  200. break;
  201. }
  202. }
  203. if (!found) {
  204. event_node = kzalloc(sizeof(*event_node), GFP_KERNEL);
  205. if (!event_node) {
  206. DP_NOTICE(edev,
  207. "qedr: Could not allocate memory for roce work\n");
  208. return NULL;
  209. }
  210. list_add_tail(&event_node->list,
  211. &edev->rdma_info.roce_event_list);
  212. }
  213. return event_node;
  214. }
  215. static void qede_roce_handle_event(struct work_struct *work)
  216. {
  217. struct qede_roce_event_work *event_node;
  218. enum qede_roce_event event;
  219. struct qede_dev *edev;
  220. event_node = container_of(work, struct qede_roce_event_work, work);
  221. event = event_node->event;
  222. edev = event_node->ptr;
  223. switch (event) {
  224. case QEDE_UP:
  225. qede_roce_dev_open(edev);
  226. break;
  227. case QEDE_DOWN:
  228. qede_roce_dev_close(edev);
  229. break;
  230. case QEDE_CLOSE:
  231. qede_roce_dev_shutdown(edev);
  232. break;
  233. case QEDE_CHANGE_ADDR:
  234. qede_roce_changeaddr(edev);
  235. break;
  236. default:
  237. DP_NOTICE(edev, "Invalid roce event %d", event);
  238. }
  239. }
  240. static void qede_roce_add_event(struct qede_dev *edev,
  241. enum qede_roce_event event)
  242. {
  243. struct qede_roce_event_work *event_node;
  244. if (!edev->rdma_info.qedr_dev)
  245. return;
  246. event_node = qede_roce_get_free_event_node(edev);
  247. if (!event_node)
  248. return;
  249. event_node->event = event;
  250. event_node->ptr = edev;
  251. INIT_WORK(&event_node->work, qede_roce_handle_event);
  252. queue_work(edev->rdma_info.roce_wq, &event_node->work);
  253. }
  254. void qede_roce_dev_event_open(struct qede_dev *edev)
  255. {
  256. qede_roce_add_event(edev, QEDE_UP);
  257. }
  258. void qede_roce_dev_event_close(struct qede_dev *edev)
  259. {
  260. qede_roce_add_event(edev, QEDE_DOWN);
  261. }
  262. void qede_roce_event_changeaddr(struct qede_dev *edev)
  263. {
  264. qede_roce_add_event(edev, QEDE_CHANGE_ADDR);
  265. }