mad.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <rdma/ib_mad.h>
  48. #include "mad.h"
  49. #include "vt.h"
  50. /**
  51. * rvt_process_mad - process an incoming MAD packet
  52. * @ibdev: the infiniband device this packet came in on
  53. * @mad_flags: MAD flags
  54. * @port_num: the port number this packet came in on, 1 based from ib core
  55. * @in_wc: the work completion entry for this packet
  56. * @in_grh: the global route header for this packet
  57. * @in_mad: the incoming MAD
  58. * @out_mad: any outgoing MAD reply
  59. *
  60. * Note that the verbs framework has already done the MAD sanity checks,
  61. * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  62. * MADs.
  63. *
  64. * This is called by the ib_mad module.
  65. *
  66. * Return: IB_MAD_RESULT_SUCCESS or error
  67. */
  68. int rvt_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  69. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  70. const struct ib_mad_hdr *in, size_t in_mad_size,
  71. struct ib_mad_hdr *out, size_t *out_mad_size,
  72. u16 *out_mad_pkey_index)
  73. {
  74. /*
  75. * MAD processing is quite different between hfi1 and qib. Therefore
  76. * this is expected to be provided by the driver. Other drivers in the
  77. * future may choose to implement this but it should not be made into a
  78. * requirement.
  79. */
  80. if (ibport_num_to_idx(ibdev, port_num) < 0)
  81. return -EINVAL;
  82. return IB_MAD_RESULT_FAILURE;
  83. }
  84. static void rvt_send_mad_handler(struct ib_mad_agent *agent,
  85. struct ib_mad_send_wc *mad_send_wc)
  86. {
  87. ib_free_send_mad(mad_send_wc->send_buf);
  88. }
  89. /**
  90. * rvt_create_mad_agents - create mad agents
  91. * @rdi: rvt dev struct
  92. *
  93. * If driver needs to be notified of mad agent creation then call back
  94. *
  95. * Return 0 on success
  96. */
  97. int rvt_create_mad_agents(struct rvt_dev_info *rdi)
  98. {
  99. struct ib_mad_agent *agent;
  100. struct rvt_ibport *rvp;
  101. int p;
  102. int ret;
  103. for (p = 0; p < rdi->dparms.nports; p++) {
  104. rvp = rdi->ports[p];
  105. agent = ib_register_mad_agent(&rdi->ibdev, p + 1,
  106. IB_QPT_SMI,
  107. NULL, 0, rvt_send_mad_handler,
  108. NULL, NULL, 0);
  109. if (IS_ERR(agent)) {
  110. ret = PTR_ERR(agent);
  111. goto err;
  112. }
  113. rvp->send_agent = agent;
  114. if (rdi->driver_f.notify_create_mad_agent)
  115. rdi->driver_f.notify_create_mad_agent(rdi, p);
  116. }
  117. return 0;
  118. err:
  119. for (p = 0; p < rdi->dparms.nports; p++) {
  120. rvp = rdi->ports[p];
  121. if (rvp->send_agent) {
  122. agent = rvp->send_agent;
  123. rvp->send_agent = NULL;
  124. ib_unregister_mad_agent(agent);
  125. if (rdi->driver_f.notify_free_mad_agent)
  126. rdi->driver_f.notify_free_mad_agent(rdi, p);
  127. }
  128. }
  129. return ret;
  130. }
  131. /**
  132. * rvt_free_mad_agents - free up mad agents
  133. * @rdi: rvt dev struct
  134. *
  135. * If driver needs notification of mad agent removal make the call back
  136. */
  137. void rvt_free_mad_agents(struct rvt_dev_info *rdi)
  138. {
  139. struct ib_mad_agent *agent;
  140. struct rvt_ibport *rvp;
  141. int p;
  142. for (p = 0; p < rdi->dparms.nports; p++) {
  143. rvp = rdi->ports[p];
  144. if (rvp->send_agent) {
  145. agent = rvp->send_agent;
  146. rvp->send_agent = NULL;
  147. ib_unregister_mad_agent(agent);
  148. }
  149. if (rvp->sm_ah) {
  150. rdma_destroy_ah(&rvp->sm_ah->ibah);
  151. rvp->sm_ah = NULL;
  152. }
  153. if (rdi->driver_f.notify_free_mad_agent)
  154. rdi->driver_f.notify_free_mad_agent(rdi, p);
  155. }
  156. }