mthca_mcg.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/slab.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. u8 status;
  67. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  68. if (IS_ERR(mailbox))
  69. return -ENOMEM;
  70. mgid = mailbox->buf;
  71. memcpy(mgid, gid, 16);
  72. err = mthca_MGID_HASH(dev, mailbox, hash, &status);
  73. if (err)
  74. goto out;
  75. if (status) {
  76. mthca_err(dev, "MGID_HASH returned status %02x\n", status);
  77. err = -EINVAL;
  78. goto out;
  79. }
  80. if (0)
  81. mthca_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
  82. *index = *hash;
  83. *prev = -1;
  84. do {
  85. err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
  86. if (err)
  87. goto out;
  88. if (status) {
  89. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  90. err = -EINVAL;
  91. goto out;
  92. }
  93. if (!memcmp(mgm->gid, zero_gid, 16)) {
  94. if (*index != *hash) {
  95. mthca_err(dev, "Found zero MGID in AMGM.\n");
  96. err = -EINVAL;
  97. }
  98. goto out;
  99. }
  100. if (!memcmp(mgm->gid, gid, 16))
  101. goto out;
  102. *prev = *index;
  103. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  104. } while (*index);
  105. *index = -1;
  106. out:
  107. mthca_free_mailbox(dev, mailbox);
  108. return err;
  109. }
  110. int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  111. {
  112. struct mthca_dev *dev = to_mdev(ibqp->device);
  113. struct mthca_mailbox *mailbox;
  114. struct mthca_mgm *mgm;
  115. u16 hash;
  116. int index, prev;
  117. int link = 0;
  118. int i;
  119. int err;
  120. u8 status;
  121. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  122. if (IS_ERR(mailbox))
  123. return PTR_ERR(mailbox);
  124. mgm = mailbox->buf;
  125. mutex_lock(&dev->mcg_table.mutex);
  126. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  127. if (err)
  128. goto out;
  129. if (index != -1) {
  130. if (!memcmp(mgm->gid, zero_gid, 16))
  131. memcpy(mgm->gid, gid->raw, 16);
  132. } else {
  133. link = 1;
  134. index = mthca_alloc(&dev->mcg_table.alloc);
  135. if (index == -1) {
  136. mthca_err(dev, "No AMGM entries left\n");
  137. err = -ENOMEM;
  138. goto out;
  139. }
  140. err = mthca_READ_MGM(dev, index, mailbox, &status);
  141. if (err)
  142. goto out;
  143. if (status) {
  144. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  145. err = -EINVAL;
  146. goto out;
  147. }
  148. memset(mgm, 0, sizeof *mgm);
  149. memcpy(mgm->gid, gid->raw, 16);
  150. }
  151. for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
  152. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
  153. mthca_dbg(dev, "QP %06x already a member of MGM\n",
  154. ibqp->qp_num);
  155. err = 0;
  156. goto out;
  157. } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
  158. mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
  159. break;
  160. }
  161. if (i == MTHCA_QP_PER_MGM) {
  162. mthca_err(dev, "MGM at index %x is full.\n", index);
  163. err = -ENOMEM;
  164. goto out;
  165. }
  166. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  167. if (err)
  168. goto out;
  169. if (status) {
  170. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  171. err = -EINVAL;
  172. goto out;
  173. }
  174. if (!link)
  175. goto out;
  176. err = mthca_READ_MGM(dev, prev, mailbox, &status);
  177. if (err)
  178. goto out;
  179. if (status) {
  180. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  181. err = -EINVAL;
  182. goto out;
  183. }
  184. mgm->next_gid_index = cpu_to_be32(index << 6);
  185. err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
  186. if (err)
  187. goto out;
  188. if (status) {
  189. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  190. err = -EINVAL;
  191. }
  192. out:
  193. if (err && link && index != -1) {
  194. BUG_ON(index < dev->limits.num_mgms);
  195. mthca_free(&dev->mcg_table.alloc, index);
  196. }
  197. mutex_unlock(&dev->mcg_table.mutex);
  198. mthca_free_mailbox(dev, mailbox);
  199. return err;
  200. }
  201. int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  202. {
  203. struct mthca_dev *dev = to_mdev(ibqp->device);
  204. struct mthca_mailbox *mailbox;
  205. struct mthca_mgm *mgm;
  206. u16 hash;
  207. int prev, index;
  208. int i, loc;
  209. int err;
  210. u8 status;
  211. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  212. if (IS_ERR(mailbox))
  213. return PTR_ERR(mailbox);
  214. mgm = mailbox->buf;
  215. mutex_lock(&dev->mcg_table.mutex);
  216. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  217. if (err)
  218. goto out;
  219. if (index == -1) {
  220. mthca_err(dev, "MGID %pI6 not found\n", gid->raw);
  221. err = -EINVAL;
  222. goto out;
  223. }
  224. for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
  225. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
  226. loc = i;
  227. if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
  228. break;
  229. }
  230. if (loc == -1) {
  231. mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
  232. err = -EINVAL;
  233. goto out;
  234. }
  235. mgm->qp[loc] = mgm->qp[i - 1];
  236. mgm->qp[i - 1] = 0;
  237. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  238. if (err)
  239. goto out;
  240. if (status) {
  241. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  242. err = -EINVAL;
  243. goto out;
  244. }
  245. if (i != 1)
  246. goto out;
  247. if (prev == -1) {
  248. /* Remove entry from MGM */
  249. int amgm_index_to_free = be32_to_cpu(mgm->next_gid_index) >> 6;
  250. if (amgm_index_to_free) {
  251. err = mthca_READ_MGM(dev, amgm_index_to_free,
  252. mailbox, &status);
  253. if (err)
  254. goto out;
  255. if (status) {
  256. mthca_err(dev, "READ_MGM returned status %02x\n",
  257. status);
  258. err = -EINVAL;
  259. goto out;
  260. }
  261. } else
  262. memset(mgm->gid, 0, 16);
  263. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  264. if (err)
  265. goto out;
  266. if (status) {
  267. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  268. err = -EINVAL;
  269. goto out;
  270. }
  271. if (amgm_index_to_free) {
  272. BUG_ON(amgm_index_to_free < dev->limits.num_mgms);
  273. mthca_free(&dev->mcg_table.alloc, amgm_index_to_free);
  274. }
  275. } else {
  276. /* Remove entry from AMGM */
  277. int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  278. err = mthca_READ_MGM(dev, prev, mailbox, &status);
  279. if (err)
  280. goto out;
  281. if (status) {
  282. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  283. err = -EINVAL;
  284. goto out;
  285. }
  286. mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
  287. err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
  288. if (err)
  289. goto out;
  290. if (status) {
  291. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  292. err = -EINVAL;
  293. goto out;
  294. }
  295. BUG_ON(index < dev->limits.num_mgms);
  296. mthca_free(&dev->mcg_table.alloc, index);
  297. }
  298. out:
  299. mutex_unlock(&dev->mcg_table.mutex);
  300. mthca_free_mailbox(dev, mailbox);
  301. return err;
  302. }
  303. int mthca_init_mcg_table(struct mthca_dev *dev)
  304. {
  305. int err;
  306. int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
  307. err = mthca_alloc_init(&dev->mcg_table.alloc,
  308. table_size,
  309. table_size - 1,
  310. dev->limits.num_mgms);
  311. if (err)
  312. return err;
  313. mutex_init(&dev->mcg_table.mutex);
  314. return 0;
  315. }
  316. void mthca_cleanup_mcg_table(struct mthca_dev *dev)
  317. {
  318. mthca_alloc_cleanup(&dev->mcg_table.alloc);
  319. }