mthca_mcg.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. u32 next_gid_index;
  42. u32 reserved[3];
  43. u8 gid[16];
  44. u32 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(((u16 *) gid)[0]), be16_to_cpu(((u16 *) gid)[1]),
  88. be16_to_cpu(((u16 *) gid)[2]), be16_to_cpu(((u16 *) gid)[3]),
  89. be16_to_cpu(((u16 *) gid)[4]), be16_to_cpu(((u16 *) gid)[5]),
  90. be16_to_cpu(((u16 *) gid)[6]), be16_to_cpu(((u16 *) 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. return -EINVAL;
  101. }
  102. if (!memcmp(mgm->gid, zero_gid, 16)) {
  103. if (*index != *hash) {
  104. mthca_err(dev, "Found zero MGID in AMGM.\n");
  105. err = -EINVAL;
  106. }
  107. goto out;
  108. }
  109. if (!memcmp(mgm->gid, gid, 16))
  110. goto out;
  111. *prev = *index;
  112. *index = be32_to_cpu(mgm->next_gid_index) >> 5;
  113. } while (*index);
  114. *index = -1;
  115. out:
  116. mthca_free_mailbox(dev, mailbox);
  117. return err;
  118. }
  119. int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  120. {
  121. struct mthca_dev *dev = to_mdev(ibqp->device);
  122. struct mthca_mailbox *mailbox;
  123. struct mthca_mgm *mgm;
  124. u16 hash;
  125. int index, prev;
  126. int link = 0;
  127. int i;
  128. int err;
  129. u8 status;
  130. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  131. if (IS_ERR(mailbox))
  132. return PTR_ERR(mailbox);
  133. mgm = mailbox->buf;
  134. if (down_interruptible(&dev->mcg_table.sem))
  135. return -EINTR;
  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. memcpy(mgm->gid, gid->raw, 16);
  159. mgm->next_gid_index = 0;
  160. }
  161. for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
  162. if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
  163. mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
  164. break;
  165. }
  166. if (i == MTHCA_QP_PER_MGM) {
  167. mthca_err(dev, "MGM at index %x is full.\n", index);
  168. err = -ENOMEM;
  169. goto out;
  170. }
  171. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  172. if (err)
  173. goto out;
  174. if (status) {
  175. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  176. err = -EINVAL;
  177. }
  178. if (!link)
  179. goto out;
  180. err = mthca_READ_MGM(dev, prev, mailbox, &status);
  181. if (err)
  182. goto out;
  183. if (status) {
  184. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  185. err = -EINVAL;
  186. goto out;
  187. }
  188. mgm->next_gid_index = cpu_to_be32(index << 5);
  189. err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
  190. if (err)
  191. goto out;
  192. if (status) {
  193. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  194. err = -EINVAL;
  195. }
  196. out:
  197. up(&dev->mcg_table.sem);
  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. if (down_interruptible(&dev->mcg_table.sem))
  216. return -EINTR;
  217. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  218. if (err)
  219. goto out;
  220. if (index == -1) {
  221. mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  222. "not found\n",
  223. be16_to_cpu(((u16 *) gid->raw)[0]),
  224. be16_to_cpu(((u16 *) gid->raw)[1]),
  225. be16_to_cpu(((u16 *) gid->raw)[2]),
  226. be16_to_cpu(((u16 *) gid->raw)[3]),
  227. be16_to_cpu(((u16 *) gid->raw)[4]),
  228. be16_to_cpu(((u16 *) gid->raw)[5]),
  229. be16_to_cpu(((u16 *) gid->raw)[6]),
  230. be16_to_cpu(((u16 *) gid->raw)[7]));
  231. err = -EINVAL;
  232. goto out;
  233. }
  234. for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
  235. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
  236. loc = i;
  237. if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
  238. break;
  239. }
  240. if (loc == -1) {
  241. mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
  242. err = -EINVAL;
  243. goto out;
  244. }
  245. mgm->qp[loc] = mgm->qp[i - 1];
  246. mgm->qp[i - 1] = 0;
  247. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  248. if (err)
  249. goto out;
  250. if (status) {
  251. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  252. err = -EINVAL;
  253. goto out;
  254. }
  255. if (i != 1)
  256. goto out;
  257. goto out;
  258. if (prev == -1) {
  259. /* Remove entry from MGM */
  260. if (be32_to_cpu(mgm->next_gid_index) >> 5) {
  261. err = mthca_READ_MGM(dev,
  262. be32_to_cpu(mgm->next_gid_index) >> 5,
  263. mailbox, &status);
  264. if (err)
  265. goto out;
  266. if (status) {
  267. mthca_err(dev, "READ_MGM returned status %02x\n",
  268. status);
  269. err = -EINVAL;
  270. goto out;
  271. }
  272. } else
  273. memset(mgm->gid, 0, 16);
  274. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  275. if (err)
  276. goto out;
  277. if (status) {
  278. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  279. err = -EINVAL;
  280. goto out;
  281. }
  282. } else {
  283. /* Remove entry from AMGM */
  284. index = be32_to_cpu(mgm->next_gid_index) >> 5;
  285. err = mthca_READ_MGM(dev, prev, mailbox, &status);
  286. if (err)
  287. goto out;
  288. if (status) {
  289. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  290. err = -EINVAL;
  291. goto out;
  292. }
  293. mgm->next_gid_index = cpu_to_be32(index << 5);
  294. err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
  295. if (err)
  296. goto out;
  297. if (status) {
  298. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  299. err = -EINVAL;
  300. goto out;
  301. }
  302. }
  303. out:
  304. up(&dev->mcg_table.sem);
  305. mthca_free_mailbox(dev, mailbox);
  306. return err;
  307. }
  308. int __devinit mthca_init_mcg_table(struct mthca_dev *dev)
  309. {
  310. int err;
  311. err = mthca_alloc_init(&dev->mcg_table.alloc,
  312. dev->limits.num_amgms,
  313. dev->limits.num_amgms - 1,
  314. 0);
  315. if (err)
  316. return err;
  317. init_MUTEX(&dev->mcg_table.sem);
  318. return 0;
  319. }
  320. void __devexit mthca_cleanup_mcg_table(struct mthca_dev *dev)
  321. {
  322. mthca_alloc_cleanup(&dev->mcg_table.alloc);
  323. }