mthca_mcg.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. if (down_interruptible(&dev->mcg_table.sem)) {
  139. err = -EINTR;
  140. goto err_sem;
  141. }
  142. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  143. if (err)
  144. goto out;
  145. if (index != -1) {
  146. if (!memcmp(mgm->gid, zero_gid, 16))
  147. memcpy(mgm->gid, gid->raw, 16);
  148. } else {
  149. link = 1;
  150. index = mthca_alloc(&dev->mcg_table.alloc);
  151. if (index == -1) {
  152. mthca_err(dev, "No AMGM entries left\n");
  153. err = -ENOMEM;
  154. goto out;
  155. }
  156. err = mthca_READ_MGM(dev, index, mailbox, &status);
  157. if (err)
  158. goto out;
  159. if (status) {
  160. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  161. err = -EINVAL;
  162. goto out;
  163. }
  164. memset(mgm, 0, sizeof *mgm);
  165. memcpy(mgm->gid, gid->raw, 16);
  166. }
  167. for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
  168. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
  169. mthca_dbg(dev, "QP %06x already a member of MGM\n",
  170. ibqp->qp_num);
  171. err = 0;
  172. goto out;
  173. } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
  174. mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
  175. break;
  176. }
  177. if (i == MTHCA_QP_PER_MGM) {
  178. mthca_err(dev, "MGM at index %x is full.\n", index);
  179. err = -ENOMEM;
  180. goto out;
  181. }
  182. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  183. if (err)
  184. goto out;
  185. if (status) {
  186. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  187. err = -EINVAL;
  188. goto out;
  189. }
  190. if (!link)
  191. goto out;
  192. err = mthca_READ_MGM(dev, prev, mailbox, &status);
  193. if (err)
  194. goto out;
  195. if (status) {
  196. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  197. err = -EINVAL;
  198. goto out;
  199. }
  200. mgm->next_gid_index = cpu_to_be32(index << 6);
  201. err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
  202. if (err)
  203. goto out;
  204. if (status) {
  205. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  206. err = -EINVAL;
  207. }
  208. out:
  209. if (err && link && index != -1) {
  210. BUG_ON(index < dev->limits.num_mgms);
  211. mthca_free(&dev->mcg_table.alloc, index);
  212. }
  213. up(&dev->mcg_table.sem);
  214. err_sem:
  215. mthca_free_mailbox(dev, mailbox);
  216. return err;
  217. }
  218. int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  219. {
  220. struct mthca_dev *dev = to_mdev(ibqp->device);
  221. struct mthca_mailbox *mailbox;
  222. struct mthca_mgm *mgm;
  223. u16 hash;
  224. int prev, index;
  225. int i, loc;
  226. int err;
  227. u8 status;
  228. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  229. if (IS_ERR(mailbox))
  230. return PTR_ERR(mailbox);
  231. mgm = mailbox->buf;
  232. if (down_interruptible(&dev->mcg_table.sem)) {
  233. err = -EINTR;
  234. goto err_sem;
  235. }
  236. err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
  237. if (err)
  238. goto out;
  239. if (index == -1) {
  240. mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  241. "not found\n",
  242. be16_to_cpu(((__be16 *) gid->raw)[0]),
  243. be16_to_cpu(((__be16 *) gid->raw)[1]),
  244. be16_to_cpu(((__be16 *) gid->raw)[2]),
  245. be16_to_cpu(((__be16 *) gid->raw)[3]),
  246. be16_to_cpu(((__be16 *) gid->raw)[4]),
  247. be16_to_cpu(((__be16 *) gid->raw)[5]),
  248. be16_to_cpu(((__be16 *) gid->raw)[6]),
  249. be16_to_cpu(((__be16 *) gid->raw)[7]));
  250. err = -EINVAL;
  251. goto out;
  252. }
  253. for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
  254. if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
  255. loc = i;
  256. if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
  257. break;
  258. }
  259. if (loc == -1) {
  260. mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
  261. err = -EINVAL;
  262. goto out;
  263. }
  264. mgm->qp[loc] = mgm->qp[i - 1];
  265. mgm->qp[i - 1] = 0;
  266. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  267. if (err)
  268. goto out;
  269. if (status) {
  270. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  271. err = -EINVAL;
  272. goto out;
  273. }
  274. if (i != 1)
  275. goto out;
  276. if (prev == -1) {
  277. /* Remove entry from MGM */
  278. int amgm_index_to_free = be32_to_cpu(mgm->next_gid_index) >> 6;
  279. if (amgm_index_to_free) {
  280. err = mthca_READ_MGM(dev, amgm_index_to_free,
  281. mailbox, &status);
  282. if (err)
  283. goto out;
  284. if (status) {
  285. mthca_err(dev, "READ_MGM returned status %02x\n",
  286. status);
  287. err = -EINVAL;
  288. goto out;
  289. }
  290. } else
  291. memset(mgm->gid, 0, 16);
  292. err = mthca_WRITE_MGM(dev, index, mailbox, &status);
  293. if (err)
  294. goto out;
  295. if (status) {
  296. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  297. err = -EINVAL;
  298. goto out;
  299. }
  300. if (amgm_index_to_free) {
  301. BUG_ON(amgm_index_to_free < dev->limits.num_mgms);
  302. mthca_free(&dev->mcg_table.alloc, amgm_index_to_free);
  303. }
  304. } else {
  305. /* Remove entry from AMGM */
  306. int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  307. err = mthca_READ_MGM(dev, prev, mailbox, &status);
  308. if (err)
  309. goto out;
  310. if (status) {
  311. mthca_err(dev, "READ_MGM returned status %02x\n", status);
  312. err = -EINVAL;
  313. goto out;
  314. }
  315. mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
  316. err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
  317. if (err)
  318. goto out;
  319. if (status) {
  320. mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
  321. err = -EINVAL;
  322. goto out;
  323. }
  324. BUG_ON(index < dev->limits.num_mgms);
  325. mthca_free(&dev->mcg_table.alloc, index);
  326. }
  327. out:
  328. up(&dev->mcg_table.sem);
  329. err_sem:
  330. mthca_free_mailbox(dev, mailbox);
  331. return err;
  332. }
  333. int __devinit mthca_init_mcg_table(struct mthca_dev *dev)
  334. {
  335. int err;
  336. int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
  337. err = mthca_alloc_init(&dev->mcg_table.alloc,
  338. table_size,
  339. table_size - 1,
  340. dev->limits.num_mgms);
  341. if (err)
  342. return err;
  343. init_MUTEX(&dev->mcg_table.sem);
  344. return 0;
  345. }
  346. void __devexit mthca_cleanup_mcg_table(struct mthca_dev *dev)
  347. {
  348. mthca_alloc_cleanup(&dev->mcg_table.alloc);
  349. }