mthca_mcg.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  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/string.h>
  33. #include <linux/gfp.h>
  34. #include "mthca_dev.h"
  35. #include "mthca_cmd.h"
  36. struct mthca_mgm {
  37. __be32 next_gid_index;
  38. u32 reserved[3];
  39. u8 gid[16];
  40. __be32 qp[MTHCA_QP_PER_MGM];
  41. };
  42. static const u8 zero_gid[16]; /* automatically initialized to 0 */
  43. /*
  44. * Caller must hold MCG table semaphore. gid and mgm parameters must
  45. * be properly aligned for command interface.
  46. *
  47. * Returns 0 unless a firmware command error occurs.
  48. *
  49. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  50. * and *mgm holds MGM entry.
  51. *
  52. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  53. * previous entry in hash chain and *mgm holds AMGM entry.
  54. *
  55. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  56. * entry in hash chain and *mgm holds end of hash chain.
  57. */
  58. static int find_mgm(struct mthca_dev *dev,
  59. u8 *gid, struct mthca_mailbox *mgm_mailbox,
  60. u16 *hash, int *prev, int *index)
  61. {
  62. struct mthca_mailbox *mailbox;
  63. struct mthca_mgm *mgm = mgm_mailbox->buf;
  64. u8 *mgid;
  65. int err;
  66. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  67. if (IS_ERR(mailbox))
  68. return -ENOMEM;
  69. mgid = mailbox->buf;
  70. memcpy(mgid, gid, 16);
  71. err = mthca_MGID_HASH(dev, mailbox, hash);
  72. if (err) {
  73. mthca_err(dev, "MGID_HASH failed (%d)\n", err);
  74. goto out;
  75. }
  76. if (0)
  77. mthca_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
  78. *index = *hash;
  79. *prev = -1;
  80. do {
  81. err = mthca_READ_MGM(dev, *index, mgm_mailbox);
  82. if (err) {
  83. mthca_err(dev, "READ_MGM failed (%d)\n", err);
  84. goto out;
  85. }
  86. if (!memcmp(mgm->gid, zero_gid, 16)) {
  87. if (*index != *hash) {
  88. mthca_err(dev, "Found zero MGID in AMGM.\n");
  89. err = -EINVAL;
  90. }
  91. goto out;
  92. }
  93. if (!memcmp(mgm->gid, gid, 16))
  94. goto out;
  95. *prev = *index;
  96. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  97. } while (*index);
  98. *index = -1;
  99. out:
  100. mthca_free_mailbox(dev, mailbox);
  101. return err;
  102. }
  103. int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  104. {
  105. struct mthca_dev *dev = to_mdev(ibqp->device);
  106. struct mthca_mailbox *mailbox;
  107. struct mthca_mgm *mgm;
  108. u16 hash;
  109. int index, prev;
  110. int link = 0;
  111. int i;
  112. int err;
  113. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  114. if (IS_ERR(mailbox))
  115. return PTR_ERR(mailbox);
  116. mgm = mailbox->buf;
  117. mutex_lock(&dev->mcg_table.mutex);
  118. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  119. if (err)
  120. goto out;
  121. if (index != -1) {
  122. if (!memcmp(mgm->gid, zero_gid, 16))
  123. memcpy(mgm->gid, gid->raw, 16);
  124. } else {
  125. link = 1;
  126. index = mthca_alloc(&dev->mcg_table.alloc);
  127. if (index == -1) {
  128. mthca_err(dev, "No AMGM entries left\n");
  129. err = -ENOMEM;
  130. goto out;
  131. }
  132. err = mthca_READ_MGM(dev, index, mailbox);
  133. if (err) {
  134. mthca_err(dev, "READ_MGM failed (%d)\n", err);
  135. goto out;
  136. }
  137. memset(mgm, 0, sizeof *mgm);
  138. memcpy(mgm->gid, gid->raw, 16);
  139. }
  140. for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
  141. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
  142. mthca_dbg(dev, "QP %06x already a member of MGM\n",
  143. ibqp->qp_num);
  144. err = 0;
  145. goto out;
  146. } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
  147. mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
  148. break;
  149. }
  150. if (i == MTHCA_QP_PER_MGM) {
  151. mthca_err(dev, "MGM at index %x is full.\n", index);
  152. err = -ENOMEM;
  153. goto out;
  154. }
  155. err = mthca_WRITE_MGM(dev, index, mailbox);
  156. if (err) {
  157. mthca_err(dev, "WRITE_MGM failed %d\n", err);
  158. err = -EINVAL;
  159. goto out;
  160. }
  161. if (!link)
  162. goto out;
  163. err = mthca_READ_MGM(dev, prev, mailbox);
  164. if (err) {
  165. mthca_err(dev, "READ_MGM failed %d\n", err);
  166. goto out;
  167. }
  168. mgm->next_gid_index = cpu_to_be32(index << 6);
  169. err = mthca_WRITE_MGM(dev, prev, mailbox);
  170. if (err)
  171. mthca_err(dev, "WRITE_MGM returned %d\n", err);
  172. out:
  173. if (err && link && index != -1) {
  174. BUG_ON(index < dev->limits.num_mgms);
  175. mthca_free(&dev->mcg_table.alloc, index);
  176. }
  177. mutex_unlock(&dev->mcg_table.mutex);
  178. mthca_free_mailbox(dev, mailbox);
  179. return err;
  180. }
  181. int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  182. {
  183. struct mthca_dev *dev = to_mdev(ibqp->device);
  184. struct mthca_mailbox *mailbox;
  185. struct mthca_mgm *mgm;
  186. u16 hash;
  187. int prev, index;
  188. int i, loc;
  189. int err;
  190. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  191. if (IS_ERR(mailbox))
  192. return PTR_ERR(mailbox);
  193. mgm = mailbox->buf;
  194. mutex_lock(&dev->mcg_table.mutex);
  195. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  196. if (err)
  197. goto out;
  198. if (index == -1) {
  199. mthca_err(dev, "MGID %pI6 not found\n", gid->raw);
  200. err = -EINVAL;
  201. goto out;
  202. }
  203. for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
  204. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
  205. loc = i;
  206. if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
  207. break;
  208. }
  209. if (loc == -1) {
  210. mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
  211. err = -EINVAL;
  212. goto out;
  213. }
  214. mgm->qp[loc] = mgm->qp[i - 1];
  215. mgm->qp[i - 1] = 0;
  216. err = mthca_WRITE_MGM(dev, index, mailbox);
  217. if (err) {
  218. mthca_err(dev, "WRITE_MGM returned %d\n", err);
  219. goto out;
  220. }
  221. if (i != 1)
  222. goto out;
  223. if (prev == -1) {
  224. /* Remove entry from MGM */
  225. int amgm_index_to_free = be32_to_cpu(mgm->next_gid_index) >> 6;
  226. if (amgm_index_to_free) {
  227. err = mthca_READ_MGM(dev, amgm_index_to_free,
  228. mailbox);
  229. if (err) {
  230. mthca_err(dev, "READ_MGM returned %d\n", err);
  231. goto out;
  232. }
  233. } else
  234. memset(mgm->gid, 0, 16);
  235. err = mthca_WRITE_MGM(dev, index, mailbox);
  236. if (err) {
  237. mthca_err(dev, "WRITE_MGM returned %d\n", err);
  238. goto out;
  239. }
  240. if (amgm_index_to_free) {
  241. BUG_ON(amgm_index_to_free < dev->limits.num_mgms);
  242. mthca_free(&dev->mcg_table.alloc, amgm_index_to_free);
  243. }
  244. } else {
  245. /* Remove entry from AMGM */
  246. int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  247. err = mthca_READ_MGM(dev, prev, mailbox);
  248. if (err) {
  249. mthca_err(dev, "READ_MGM returned %d\n", err);
  250. goto out;
  251. }
  252. mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
  253. err = mthca_WRITE_MGM(dev, prev, mailbox);
  254. if (err) {
  255. mthca_err(dev, "WRITE_MGM returned %d\n", err);
  256. goto out;
  257. }
  258. BUG_ON(index < dev->limits.num_mgms);
  259. mthca_free(&dev->mcg_table.alloc, index);
  260. }
  261. out:
  262. mutex_unlock(&dev->mcg_table.mutex);
  263. mthca_free_mailbox(dev, mailbox);
  264. return err;
  265. }
  266. int mthca_init_mcg_table(struct mthca_dev *dev)
  267. {
  268. int err;
  269. int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
  270. err = mthca_alloc_init(&dev->mcg_table.alloc,
  271. table_size,
  272. table_size - 1,
  273. dev->limits.num_mgms);
  274. if (err)
  275. return err;
  276. mutex_init(&dev->mcg_table.mutex);
  277. return 0;
  278. }
  279. void mthca_cleanup_mcg_table(struct mthca_dev *dev)
  280. {
  281. mthca_alloc_cleanup(&dev->mcg_table.alloc);
  282. }