mthca_mcg.c 9.5 KB

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