qede_rdma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* QLogic qedr NIC Driver
  2. * Copyright (c) 2015-2017 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_rdma.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_rdma_supported(struct qede_dev *dev)
  42. {
  43. return dev->dev_info.common.rdma_supported;
  44. }
  45. static void _qede_rdma_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_rdma_create_wq(struct qede_dev *edev)
  53. {
  54. INIT_LIST_HEAD(&edev->rdma_info.rdma_event_list);
  55. kref_init(&edev->rdma_info.refcnt);
  56. init_completion(&edev->rdma_info.event_comp);
  57. edev->rdma_info.rdma_wq = create_singlethread_workqueue("rdma_wq");
  58. if (!edev->rdma_info.rdma_wq) {
  59. DP_NOTICE(edev, "qedr: Could not create workqueue\n");
  60. return -ENOMEM;
  61. }
  62. return 0;
  63. }
  64. static void qede_rdma_cleanup_event(struct qede_dev *edev)
  65. {
  66. struct list_head *head = &edev->rdma_info.rdma_event_list;
  67. struct qede_rdma_event_work *event_node;
  68. flush_workqueue(edev->rdma_info.rdma_wq);
  69. while (!list_empty(head)) {
  70. event_node = list_entry(head->next, struct qede_rdma_event_work,
  71. list);
  72. cancel_work_sync(&event_node->work);
  73. list_del(&event_node->list);
  74. kfree(event_node);
  75. }
  76. }
  77. static void qede_rdma_complete_event(struct kref *ref)
  78. {
  79. struct qede_rdma_dev *rdma_dev =
  80. container_of(ref, struct qede_rdma_dev, refcnt);
  81. /* no more events will be added after this */
  82. complete(&rdma_dev->event_comp);
  83. }
  84. static void qede_rdma_destroy_wq(struct qede_dev *edev)
  85. {
  86. /* Avoid race with add_event flow, make sure it finishes before
  87. * we start accessing the list and cleaning up the work
  88. */
  89. kref_put(&edev->rdma_info.refcnt, qede_rdma_complete_event);
  90. wait_for_completion(&edev->rdma_info.event_comp);
  91. qede_rdma_cleanup_event(edev);
  92. destroy_workqueue(edev->rdma_info.rdma_wq);
  93. }
  94. int qede_rdma_dev_add(struct qede_dev *edev)
  95. {
  96. int rc = 0;
  97. if (qede_rdma_supported(edev)) {
  98. rc = qede_rdma_create_wq(edev);
  99. if (rc)
  100. return rc;
  101. INIT_LIST_HEAD(&edev->rdma_info.entry);
  102. mutex_lock(&qedr_dev_list_lock);
  103. list_add_tail(&edev->rdma_info.entry, &qedr_dev_list);
  104. _qede_rdma_dev_add(edev);
  105. mutex_unlock(&qedr_dev_list_lock);
  106. }
  107. return rc;
  108. }
  109. static void _qede_rdma_dev_remove(struct qede_dev *edev)
  110. {
  111. if (qedr_drv && qedr_drv->remove && edev->rdma_info.qedr_dev)
  112. qedr_drv->remove(edev->rdma_info.qedr_dev);
  113. edev->rdma_info.qedr_dev = NULL;
  114. }
  115. void qede_rdma_dev_remove(struct qede_dev *edev)
  116. {
  117. if (!qede_rdma_supported(edev))
  118. return;
  119. qede_rdma_destroy_wq(edev);
  120. mutex_lock(&qedr_dev_list_lock);
  121. _qede_rdma_dev_remove(edev);
  122. list_del(&edev->rdma_info.entry);
  123. mutex_unlock(&qedr_dev_list_lock);
  124. }
  125. static void _qede_rdma_dev_open(struct qede_dev *edev)
  126. {
  127. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  128. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_UP);
  129. }
  130. static void qede_rdma_dev_open(struct qede_dev *edev)
  131. {
  132. if (!qede_rdma_supported(edev))
  133. return;
  134. mutex_lock(&qedr_dev_list_lock);
  135. _qede_rdma_dev_open(edev);
  136. mutex_unlock(&qedr_dev_list_lock);
  137. }
  138. static void _qede_rdma_dev_close(struct qede_dev *edev)
  139. {
  140. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  141. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_DOWN);
  142. }
  143. static void qede_rdma_dev_close(struct qede_dev *edev)
  144. {
  145. if (!qede_rdma_supported(edev))
  146. return;
  147. mutex_lock(&qedr_dev_list_lock);
  148. _qede_rdma_dev_close(edev);
  149. mutex_unlock(&qedr_dev_list_lock);
  150. }
  151. static void qede_rdma_dev_shutdown(struct qede_dev *edev)
  152. {
  153. if (!qede_rdma_supported(edev))
  154. return;
  155. mutex_lock(&qedr_dev_list_lock);
  156. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  157. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CLOSE);
  158. mutex_unlock(&qedr_dev_list_lock);
  159. }
  160. int qede_rdma_register_driver(struct qedr_driver *drv)
  161. {
  162. struct qede_dev *edev;
  163. u8 qedr_counter = 0;
  164. mutex_lock(&qedr_dev_list_lock);
  165. if (qedr_drv) {
  166. mutex_unlock(&qedr_dev_list_lock);
  167. return -EINVAL;
  168. }
  169. qedr_drv = drv;
  170. list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
  171. struct net_device *ndev;
  172. qedr_counter++;
  173. _qede_rdma_dev_add(edev);
  174. ndev = edev->ndev;
  175. if (netif_running(ndev) && netif_oper_up(ndev))
  176. _qede_rdma_dev_open(edev);
  177. }
  178. mutex_unlock(&qedr_dev_list_lock);
  179. pr_notice("qedr: discovered and registered %d RDMA funcs\n",
  180. qedr_counter);
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(qede_rdma_register_driver);
  184. void qede_rdma_unregister_driver(struct qedr_driver *drv)
  185. {
  186. struct qede_dev *edev;
  187. mutex_lock(&qedr_dev_list_lock);
  188. list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
  189. if (edev->rdma_info.qedr_dev)
  190. _qede_rdma_dev_remove(edev);
  191. }
  192. qedr_drv = NULL;
  193. mutex_unlock(&qedr_dev_list_lock);
  194. }
  195. EXPORT_SYMBOL(qede_rdma_unregister_driver);
  196. static void qede_rdma_changeaddr(struct qede_dev *edev)
  197. {
  198. if (!qede_rdma_supported(edev))
  199. return;
  200. if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
  201. qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CHANGE_ADDR);
  202. }
  203. static struct qede_rdma_event_work *
  204. qede_rdma_get_free_event_node(struct qede_dev *edev)
  205. {
  206. struct qede_rdma_event_work *event_node = NULL;
  207. struct list_head *list_node = NULL;
  208. bool found = false;
  209. list_for_each(list_node, &edev->rdma_info.rdma_event_list) {
  210. event_node = list_entry(list_node, struct qede_rdma_event_work,
  211. list);
  212. if (!work_pending(&event_node->work)) {
  213. found = true;
  214. break;
  215. }
  216. }
  217. if (!found) {
  218. event_node = kzalloc(sizeof(*event_node), GFP_ATOMIC);
  219. if (!event_node) {
  220. DP_NOTICE(edev,
  221. "qedr: Could not allocate memory for rdma work\n");
  222. return NULL;
  223. }
  224. list_add_tail(&event_node->list,
  225. &edev->rdma_info.rdma_event_list);
  226. }
  227. return event_node;
  228. }
  229. static void qede_rdma_handle_event(struct work_struct *work)
  230. {
  231. struct qede_rdma_event_work *event_node;
  232. enum qede_rdma_event event;
  233. struct qede_dev *edev;
  234. event_node = container_of(work, struct qede_rdma_event_work, work);
  235. event = event_node->event;
  236. edev = event_node->ptr;
  237. switch (event) {
  238. case QEDE_UP:
  239. qede_rdma_dev_open(edev);
  240. break;
  241. case QEDE_DOWN:
  242. qede_rdma_dev_close(edev);
  243. break;
  244. case QEDE_CLOSE:
  245. qede_rdma_dev_shutdown(edev);
  246. break;
  247. case QEDE_CHANGE_ADDR:
  248. qede_rdma_changeaddr(edev);
  249. break;
  250. default:
  251. DP_NOTICE(edev, "Invalid rdma event %d", event);
  252. }
  253. }
  254. static void qede_rdma_add_event(struct qede_dev *edev,
  255. enum qede_rdma_event event)
  256. {
  257. struct qede_rdma_event_work *event_node;
  258. if (!edev->rdma_info.qedr_dev)
  259. return;
  260. /* We don't want the cleanup flow to start while we're allocating and
  261. * scheduling the work
  262. */
  263. if (!kref_get_unless_zero(&edev->rdma_info.refcnt))
  264. return; /* already being destroyed */
  265. event_node = qede_rdma_get_free_event_node(edev);
  266. if (!event_node)
  267. goto out;
  268. event_node->event = event;
  269. event_node->ptr = edev;
  270. INIT_WORK(&event_node->work, qede_rdma_handle_event);
  271. queue_work(edev->rdma_info.rdma_wq, &event_node->work);
  272. out:
  273. kref_put(&edev->rdma_info.refcnt, qede_rdma_complete_event);
  274. }
  275. void qede_rdma_dev_event_open(struct qede_dev *edev)
  276. {
  277. qede_rdma_add_event(edev, QEDE_UP);
  278. }
  279. void qede_rdma_dev_event_close(struct qede_dev *edev)
  280. {
  281. qede_rdma_add_event(edev, QEDE_DOWN);
  282. }
  283. void qede_rdma_event_changeaddr(struct qede_dev *edev)
  284. {
  285. qede_rdma_add_event(edev, QEDE_CHANGE_ADDR);
  286. }