mcg.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/init.h>
  34. #include <linux/string.h>
  35. #include <linux/slab.h>
  36. #include <linux/mlx4/cmd.h>
  37. #include "mlx4.h"
  38. #define MGM_QPN_MASK 0x00FFFFFF
  39. #define MGM_BLCK_LB_BIT 30
  40. struct mlx4_mgm {
  41. __be32 next_gid_index;
  42. __be32 members_count;
  43. u32 reserved[2];
  44. u8 gid[16];
  45. __be32 qp[MLX4_QP_PER_MGM];
  46. };
  47. static const u8 zero_gid[16]; /* automatically initialized to 0 */
  48. static int mlx4_READ_MCG(struct mlx4_dev *dev, int index,
  49. struct mlx4_cmd_mailbox *mailbox)
  50. {
  51. return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
  52. MLX4_CMD_TIME_CLASS_A);
  53. }
  54. static int mlx4_WRITE_MCG(struct mlx4_dev *dev, int index,
  55. struct mlx4_cmd_mailbox *mailbox)
  56. {
  57. return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
  58. MLX4_CMD_TIME_CLASS_A);
  59. }
  60. static int mlx4_MGID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  61. u16 *hash)
  62. {
  63. u64 imm;
  64. int err;
  65. err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, 0, MLX4_CMD_MGID_HASH,
  66. MLX4_CMD_TIME_CLASS_A);
  67. if (!err)
  68. *hash = imm;
  69. return err;
  70. }
  71. /*
  72. * Caller must hold MCG table semaphore. gid and mgm parameters must
  73. * be properly aligned for command interface.
  74. *
  75. * Returns 0 unless a firmware command error occurs.
  76. *
  77. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  78. * and *mgm holds MGM entry.
  79. *
  80. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  81. * previous entry in hash chain and *mgm holds AMGM entry.
  82. *
  83. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  84. * entry in hash chain and *mgm holds end of hash chain.
  85. */
  86. static int find_mgm(struct mlx4_dev *dev,
  87. u8 *gid, struct mlx4_cmd_mailbox *mgm_mailbox,
  88. u16 *hash, int *prev, int *index)
  89. {
  90. struct mlx4_cmd_mailbox *mailbox;
  91. struct mlx4_mgm *mgm = mgm_mailbox->buf;
  92. u8 *mgid;
  93. int err;
  94. mailbox = mlx4_alloc_cmd_mailbox(dev);
  95. if (IS_ERR(mailbox))
  96. return -ENOMEM;
  97. mgid = mailbox->buf;
  98. memcpy(mgid, gid, 16);
  99. err = mlx4_MGID_HASH(dev, mailbox, hash);
  100. mlx4_free_cmd_mailbox(dev, mailbox);
  101. if (err)
  102. return err;
  103. if (0)
  104. mlx4_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
  105. "%04x:%04x:%04x:%04x is %04x\n",
  106. be16_to_cpu(((__be16 *) gid)[0]),
  107. be16_to_cpu(((__be16 *) gid)[1]),
  108. be16_to_cpu(((__be16 *) gid)[2]),
  109. be16_to_cpu(((__be16 *) gid)[3]),
  110. be16_to_cpu(((__be16 *) gid)[4]),
  111. be16_to_cpu(((__be16 *) gid)[5]),
  112. be16_to_cpu(((__be16 *) gid)[6]),
  113. be16_to_cpu(((__be16 *) gid)[7]),
  114. *hash);
  115. *index = *hash;
  116. *prev = -1;
  117. do {
  118. err = mlx4_READ_MCG(dev, *index, mgm_mailbox);
  119. if (err)
  120. return err;
  121. if (!memcmp(mgm->gid, zero_gid, 16)) {
  122. if (*index != *hash) {
  123. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  124. err = -EINVAL;
  125. }
  126. return err;
  127. }
  128. if (!memcmp(mgm->gid, gid, 16))
  129. return err;
  130. *prev = *index;
  131. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  132. } while (*index);
  133. *index = -1;
  134. return err;
  135. }
  136. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  137. int block_mcast_loopback)
  138. {
  139. struct mlx4_priv *priv = mlx4_priv(dev);
  140. struct mlx4_cmd_mailbox *mailbox;
  141. struct mlx4_mgm *mgm;
  142. u32 members_count;
  143. u16 hash;
  144. int index, prev;
  145. int link = 0;
  146. int i;
  147. int err;
  148. mailbox = mlx4_alloc_cmd_mailbox(dev);
  149. if (IS_ERR(mailbox))
  150. return PTR_ERR(mailbox);
  151. mgm = mailbox->buf;
  152. mutex_lock(&priv->mcg_table.mutex);
  153. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  154. if (err)
  155. goto out;
  156. if (index != -1) {
  157. if (!memcmp(mgm->gid, zero_gid, 16))
  158. memcpy(mgm->gid, gid, 16);
  159. } else {
  160. link = 1;
  161. index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
  162. if (index == -1) {
  163. mlx4_err(dev, "No AMGM entries left\n");
  164. err = -ENOMEM;
  165. goto out;
  166. }
  167. index += dev->caps.num_mgms;
  168. memset(mgm, 0, sizeof *mgm);
  169. memcpy(mgm->gid, gid, 16);
  170. }
  171. members_count = be32_to_cpu(mgm->members_count);
  172. if (members_count == MLX4_QP_PER_MGM) {
  173. mlx4_err(dev, "MGM at index %x is full.\n", index);
  174. err = -ENOMEM;
  175. goto out;
  176. }
  177. for (i = 0; i < members_count; ++i)
  178. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
  179. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  180. err = 0;
  181. goto out;
  182. }
  183. if (block_mcast_loopback)
  184. mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
  185. (1 << MGM_BLCK_LB_BIT));
  186. else
  187. mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
  188. mgm->members_count = cpu_to_be32(members_count);
  189. err = mlx4_WRITE_MCG(dev, index, mailbox);
  190. if (err)
  191. goto out;
  192. if (!link)
  193. goto out;
  194. err = mlx4_READ_MCG(dev, prev, mailbox);
  195. if (err)
  196. goto out;
  197. mgm->next_gid_index = cpu_to_be32(index << 6);
  198. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  199. if (err)
  200. goto out;
  201. out:
  202. if (err && link && index != -1) {
  203. if (index < dev->caps.num_mgms)
  204. mlx4_warn(dev, "Got AMGM index %d < %d",
  205. index, dev->caps.num_mgms);
  206. else
  207. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  208. index - dev->caps.num_mgms);
  209. }
  210. mutex_unlock(&priv->mcg_table.mutex);
  211. mlx4_free_cmd_mailbox(dev, mailbox);
  212. return err;
  213. }
  214. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  215. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16])
  216. {
  217. struct mlx4_priv *priv = mlx4_priv(dev);
  218. struct mlx4_cmd_mailbox *mailbox;
  219. struct mlx4_mgm *mgm;
  220. u32 members_count;
  221. u16 hash;
  222. int prev, index;
  223. int i, loc;
  224. int err;
  225. mailbox = mlx4_alloc_cmd_mailbox(dev);
  226. if (IS_ERR(mailbox))
  227. return PTR_ERR(mailbox);
  228. mgm = mailbox->buf;
  229. mutex_lock(&priv->mcg_table.mutex);
  230. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  231. if (err)
  232. goto out;
  233. if (index == -1) {
  234. mlx4_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  235. "not found\n",
  236. be16_to_cpu(((__be16 *) gid)[0]),
  237. be16_to_cpu(((__be16 *) gid)[1]),
  238. be16_to_cpu(((__be16 *) gid)[2]),
  239. be16_to_cpu(((__be16 *) gid)[3]),
  240. be16_to_cpu(((__be16 *) gid)[4]),
  241. be16_to_cpu(((__be16 *) gid)[5]),
  242. be16_to_cpu(((__be16 *) gid)[6]),
  243. be16_to_cpu(((__be16 *) gid)[7]));
  244. err = -EINVAL;
  245. goto out;
  246. }
  247. members_count = be32_to_cpu(mgm->members_count);
  248. for (loc = -1, i = 0; i < members_count; ++i)
  249. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  250. loc = i;
  251. if (loc == -1) {
  252. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  253. err = -EINVAL;
  254. goto out;
  255. }
  256. mgm->members_count = cpu_to_be32(--members_count);
  257. mgm->qp[loc] = mgm->qp[i - 1];
  258. mgm->qp[i - 1] = 0;
  259. if (i != 1) {
  260. err = mlx4_WRITE_MCG(dev, index, mailbox);
  261. goto out;
  262. }
  263. if (prev == -1) {
  264. /* Remove entry from MGM */
  265. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  266. if (amgm_index) {
  267. err = mlx4_READ_MCG(dev, amgm_index, mailbox);
  268. if (err)
  269. goto out;
  270. } else
  271. memset(mgm->gid, 0, 16);
  272. err = mlx4_WRITE_MCG(dev, index, mailbox);
  273. if (err)
  274. goto out;
  275. if (amgm_index) {
  276. if (amgm_index < dev->caps.num_mgms)
  277. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  278. index, amgm_index, dev->caps.num_mgms);
  279. else
  280. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  281. amgm_index - dev->caps.num_mgms);
  282. }
  283. } else {
  284. /* Remove entry from AMGM */
  285. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  286. err = mlx4_READ_MCG(dev, prev, mailbox);
  287. if (err)
  288. goto out;
  289. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  290. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  291. if (err)
  292. goto out;
  293. if (index < dev->caps.num_mgms)
  294. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  295. prev, index, dev->caps.num_mgms);
  296. else
  297. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  298. index - dev->caps.num_mgms);
  299. }
  300. out:
  301. mutex_unlock(&priv->mcg_table.mutex);
  302. mlx4_free_cmd_mailbox(dev, mailbox);
  303. return err;
  304. }
  305. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  306. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  307. {
  308. struct mlx4_priv *priv = mlx4_priv(dev);
  309. int err;
  310. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  311. dev->caps.num_amgms - 1, 0, 0);
  312. if (err)
  313. return err;
  314. mutex_init(&priv->mcg_table.mutex);
  315. return 0;
  316. }
  317. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  318. {
  319. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  320. }