mthca_mcg.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "mthca_dev.h"
  36. #include "mthca_cmd.h"
  37. struct mthca_mgm {
  38. __be32 next_gid_index;
  39. u32 reserved[3];
  40. u8 gid[16];
  41. __be32 qp[MTHCA_QP_PER_MGM];
  42. };
  43. static const u8 zero_gid[16]; /* automatically initialized to 0 */
  44. /*
  45. * Caller must hold MCG table semaphore. gid and mgm parameters must
  46. * be properly aligned for command interface.
  47. *
  48. * Returns 0 unless a firmware command error occurs.
  49. *
  50. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  51. * and *mgm holds MGM entry.
  52. *
  53. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  54. * previous entry in hash chain and *mgm holds AMGM entry.
  55. *
  56. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  57. * entry in hash chain and *mgm holds end of hash chain.
  58. */
  59. static int find_mgm(struct mthca_dev *dev,
  60. u8 *gid, struct mthca_mailbox *mgm_mailbox,
  61. u16 *hash, int *prev, int *index)
  62. {
  63. struct mthca_mailbox *mailbox;
  64. struct mthca_mgm *mgm = mgm_mailbox->buf;
  65. u8 *mgid;
  66. int err;
  67. u8 status;
  68. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  69. if (IS_ERR(mailbox))
  70. return -ENOMEM;
  71. mgid = mailbox->buf;
  72. memcpy(mgid, gid, 16);
  73. err = mthca_MGID_HASH(dev, mailbox, hash, &status);
  74. if (err)
  75. goto out;
  76. if (status) {
  77. mthca_err(dev, "MGID_HASH returned status %02x\n", status);
  78. err = -EINVAL;
  79. goto out;
  80. }
  81. if (0)
  82. mthca_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
  83. "%04x:%04x:%04x:%04x is %04x\n",
  84. be16_to_cpu(((__be16 *) gid)[0]),
  85. be16_to_cpu(((__be16 *) gid)[1]),
  86. be16_to_cpu(((__be16 *) gid)[2]),
  87. be16_to_cpu(((__be16 *) gid)[3]),
  88. be16_to_cpu(((__be16 *) gid)[4]),
  89. be16_to_cpu(((__be16 *) gid)[5]),
  90. be16_to_cpu(((__be16 *) gid)[6]),
  91. be16_to_cpu(((__be16 *) gid)[7]),
  92. *hash);
  93. *index = *hash;
  94. *prev = -1;
  95. do {
  96. err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
  97. if (err)
  98. goto out;
  99. if (status) {
  100. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  101. return -EINVAL;
  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) >> 5;
  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. if (down_interruptible(&dev->mcg_table.sem))
  136. return -EINTR;
  137. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  138. if (err)
  139. goto out;
  140. if (index != -1) {
  141. if (!memcmp(mgm->gid, zero_gid, 16))
  142. memcpy(mgm->gid, gid->raw, 16);
  143. } else {
  144. link = 1;
  145. index = mthca_alloc(&dev->mcg_table.alloc);
  146. if (index == -1) {
  147. mthca_err(dev, "No AMGM entries left\n");
  148. err = -ENOMEM;
  149. goto out;
  150. }
  151. err = mthca_READ_MGM(dev, index, mailbox, &status);
  152. if (err)
  153. goto out;
  154. if (status) {
  155. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  156. err = -EINVAL;
  157. goto out;
  158. }
  159. memcpy(mgm->gid, gid->raw, 16);
  160. mgm->next_gid_index = 0;
  161. }
  162. for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
  163. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
  164. mthca_dbg(dev, "QP %06x already a member of MGM\n",
  165. ibqp->qp_num);
  166. err = 0;
  167. goto out;
  168. } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
  169. mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
  170. break;
  171. }
  172. if (i == MTHCA_QP_PER_MGM) {
  173. mthca_err(dev, "MGM at index %x is full.\n", index);
  174. err = -ENOMEM;
  175. goto out;
  176. }
  177. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  178. if (err)
  179. goto out;
  180. if (status) {
  181. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  182. err = -EINVAL;
  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 << 5);
  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. up(&dev->mcg_table.sem);
  204. mthca_free_mailbox(dev, mailbox);
  205. return err;
  206. }
  207. int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  208. {
  209. struct mthca_dev *dev = to_mdev(ibqp->device);
  210. struct mthca_mailbox *mailbox;
  211. struct mthca_mgm *mgm;
  212. u16 hash;
  213. int prev, index;
  214. int i, loc;
  215. int err;
  216. u8 status;
  217. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  218. if (IS_ERR(mailbox))
  219. return PTR_ERR(mailbox);
  220. mgm = mailbox->buf;
  221. if (down_interruptible(&dev->mcg_table.sem))
  222. return -EINTR;
  223. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  224. if (err)
  225. goto out;
  226. if (index == -1) {
  227. mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  228. "not found\n",
  229. be16_to_cpu(((__be16 *) gid->raw)[0]),
  230. be16_to_cpu(((__be16 *) gid->raw)[1]),
  231. be16_to_cpu(((__be16 *) gid->raw)[2]),
  232. be16_to_cpu(((__be16 *) gid->raw)[3]),
  233. be16_to_cpu(((__be16 *) gid->raw)[4]),
  234. be16_to_cpu(((__be16 *) gid->raw)[5]),
  235. be16_to_cpu(((__be16 *) gid->raw)[6]),
  236. be16_to_cpu(((__be16 *) gid->raw)[7]));
  237. err = -EINVAL;
  238. goto out;
  239. }
  240. for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
  241. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
  242. loc = i;
  243. if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
  244. break;
  245. }
  246. if (loc == -1) {
  247. mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
  248. err = -EINVAL;
  249. goto out;
  250. }
  251. mgm->qp[loc] = mgm->qp[i - 1];
  252. mgm->qp[i - 1] = 0;
  253. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  254. if (err)
  255. goto out;
  256. if (status) {
  257. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  258. err = -EINVAL;
  259. goto out;
  260. }
  261. if (i != 1)
  262. goto out;
  263. goto out;
  264. if (prev == -1) {
  265. /* Remove entry from MGM */
  266. if (be32_to_cpu(mgm->next_gid_index) >> 5) {
  267. err = mthca_READ_MGM(dev,
  268. be32_to_cpu(mgm->next_gid_index) >> 5,
  269. mailbox, &status);
  270. if (err)
  271. goto out;
  272. if (status) {
  273. mthca_err(dev, "READ_MGM returned status %02x\n",
  274. status);
  275. err = -EINVAL;
  276. goto out;
  277. }
  278. } else
  279. memset(mgm->gid, 0, 16);
  280. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  281. if (err)
  282. goto out;
  283. if (status) {
  284. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  285. err = -EINVAL;
  286. goto out;
  287. }
  288. } else {
  289. /* Remove entry from AMGM */
  290. index = be32_to_cpu(mgm->next_gid_index) >> 5;
  291. err = mthca_READ_MGM(dev, prev, mailbox, &status);
  292. if (err)
  293. goto out;
  294. if (status) {
  295. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  296. err = -EINVAL;
  297. goto out;
  298. }
  299. mgm->next_gid_index = cpu_to_be32(index << 5);
  300. err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
  301. if (err)
  302. goto out;
  303. if (status) {
  304. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  305. err = -EINVAL;
  306. goto out;
  307. }
  308. }
  309. out:
  310. up(&dev->mcg_table.sem);
  311. mthca_free_mailbox(dev, mailbox);
  312. return err;
  313. }
  314. int __devinit mthca_init_mcg_table(struct mthca_dev *dev)
  315. {
  316. int err;
  317. err = mthca_alloc_init(&dev->mcg_table.alloc,
  318. dev->limits.num_amgms,
  319. dev->limits.num_amgms - 1,
  320. 0);
  321. if (err)
  322. return err;
  323. init_MUTEX(&dev->mcg_table.sem);
  324. return 0;
  325. }
  326. void __devexit mthca_cleanup_mcg_table(struct mthca_dev *dev)
  327. {
  328. mthca_alloc_cleanup(&dev->mcg_table.alloc);
  329. }