be_roce.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2005 - 2016 Broadcom
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation. The full GNU General
  8. * Public License is included in this distribution in the file called COPYING.
  9. *
  10. * Contact Information:
  11. * linux-drivers@emulex.com
  12. *
  13. * Emulex
  14. * 3333 Susan Street
  15. * Costa Mesa, CA 92626
  16. */
  17. #include <linux/mutex.h>
  18. #include <linux/list.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/module.h>
  21. #include "be.h"
  22. #include "be_cmds.h"
  23. static struct ocrdma_driver *ocrdma_drv;
  24. static LIST_HEAD(be_adapter_list);
  25. static DEFINE_MUTEX(be_adapter_list_lock);
  26. static void _be_roce_dev_add(struct be_adapter *adapter)
  27. {
  28. struct be_dev_info dev_info;
  29. int i, num_vec;
  30. struct pci_dev *pdev = adapter->pdev;
  31. if (!ocrdma_drv)
  32. return;
  33. if (ocrdma_drv->be_abi_version != BE_ROCE_ABI_VERSION) {
  34. dev_warn(&pdev->dev, "Cannot initialize RoCE due to ocrdma ABI mismatch\n");
  35. return;
  36. }
  37. if (pdev->device == OC_DEVICE_ID5) {
  38. /* only msix is supported on these devices */
  39. if (!msix_enabled(adapter))
  40. return;
  41. /* DPP region address and length */
  42. dev_info.dpp_unmapped_addr = pci_resource_start(pdev, 2);
  43. dev_info.dpp_unmapped_len = pci_resource_len(pdev, 2);
  44. } else {
  45. dev_info.dpp_unmapped_addr = 0;
  46. dev_info.dpp_unmapped_len = 0;
  47. }
  48. dev_info.pdev = adapter->pdev;
  49. dev_info.db = adapter->db;
  50. dev_info.unmapped_db = adapter->roce_db.io_addr;
  51. dev_info.db_page_size = adapter->roce_db.size;
  52. dev_info.db_total_size = adapter->roce_db.total_size;
  53. dev_info.netdev = adapter->netdev;
  54. memcpy(dev_info.mac_addr, adapter->netdev->dev_addr, ETH_ALEN);
  55. dev_info.dev_family = adapter->sli_family;
  56. if (msix_enabled(adapter)) {
  57. /* provide all the vectors, so that EQ creation response
  58. * can decide which one to use.
  59. */
  60. num_vec = adapter->num_msix_vec + adapter->num_msix_roce_vec;
  61. dev_info.intr_mode = BE_INTERRUPT_MODE_MSIX;
  62. dev_info.msix.num_vectors = min(num_vec, MAX_MSIX_VECTORS);
  63. /* provide start index of the vector,
  64. * so in case of linear usage,
  65. * it can use the base as starting point.
  66. */
  67. dev_info.msix.start_vector = adapter->num_evt_qs;
  68. for (i = 0; i < dev_info.msix.num_vectors; i++) {
  69. dev_info.msix.vector_list[i] =
  70. adapter->msix_entries[i].vector;
  71. }
  72. } else {
  73. dev_info.msix.num_vectors = 0;
  74. dev_info.intr_mode = BE_INTERRUPT_MODE_INTX;
  75. }
  76. adapter->ocrdma_dev = ocrdma_drv->add(&dev_info);
  77. }
  78. void be_roce_dev_add(struct be_adapter *adapter)
  79. {
  80. if (be_roce_supported(adapter)) {
  81. INIT_LIST_HEAD(&adapter->entry);
  82. mutex_lock(&be_adapter_list_lock);
  83. list_add_tail(&adapter->entry, &be_adapter_list);
  84. /* invoke add() routine of roce driver only if
  85. * valid driver registered with add method and add() is not yet
  86. * invoked on a given adapter.
  87. */
  88. _be_roce_dev_add(adapter);
  89. mutex_unlock(&be_adapter_list_lock);
  90. }
  91. }
  92. static void _be_roce_dev_remove(struct be_adapter *adapter)
  93. {
  94. if (ocrdma_drv && ocrdma_drv->remove && adapter->ocrdma_dev)
  95. ocrdma_drv->remove(adapter->ocrdma_dev);
  96. adapter->ocrdma_dev = NULL;
  97. }
  98. void be_roce_dev_remove(struct be_adapter *adapter)
  99. {
  100. if (be_roce_supported(adapter)) {
  101. mutex_lock(&be_adapter_list_lock);
  102. _be_roce_dev_remove(adapter);
  103. list_del(&adapter->entry);
  104. mutex_unlock(&be_adapter_list_lock);
  105. }
  106. }
  107. void be_roce_dev_shutdown(struct be_adapter *adapter)
  108. {
  109. if (be_roce_supported(adapter)) {
  110. mutex_lock(&be_adapter_list_lock);
  111. if (ocrdma_drv && adapter->ocrdma_dev &&
  112. ocrdma_drv->state_change_handler)
  113. ocrdma_drv->state_change_handler(adapter->ocrdma_dev,
  114. BE_DEV_SHUTDOWN);
  115. mutex_unlock(&be_adapter_list_lock);
  116. }
  117. }
  118. int be_roce_register_driver(struct ocrdma_driver *drv)
  119. {
  120. struct be_adapter *dev;
  121. mutex_lock(&be_adapter_list_lock);
  122. if (ocrdma_drv) {
  123. mutex_unlock(&be_adapter_list_lock);
  124. return -EINVAL;
  125. }
  126. ocrdma_drv = drv;
  127. list_for_each_entry(dev, &be_adapter_list, entry) {
  128. _be_roce_dev_add(dev);
  129. }
  130. mutex_unlock(&be_adapter_list_lock);
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(be_roce_register_driver);
  134. void be_roce_unregister_driver(struct ocrdma_driver *drv)
  135. {
  136. struct be_adapter *dev;
  137. mutex_lock(&be_adapter_list_lock);
  138. list_for_each_entry(dev, &be_adapter_list, entry) {
  139. if (dev->ocrdma_dev)
  140. _be_roce_dev_remove(dev);
  141. }
  142. ocrdma_drv = NULL;
  143. mutex_unlock(&be_adapter_list_lock);
  144. }
  145. EXPORT_SYMBOL(be_roce_unregister_driver);