mthca_mcg.c 8.9 KB

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