mthca_mcg.c 9.4 KB

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