mcg.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 %pI6 is %04x\n", gid, *hash);
  105. *index = *hash;
  106. *prev = -1;
  107. do {
  108. err = mlx4_READ_MCG(dev, *index, mgm_mailbox);
  109. if (err)
  110. return err;
  111. if (!memcmp(mgm->gid, zero_gid, 16)) {
  112. if (*index != *hash) {
  113. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  114. err = -EINVAL;
  115. }
  116. return err;
  117. }
  118. if (!memcmp(mgm->gid, gid, 16))
  119. return err;
  120. *prev = *index;
  121. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  122. } while (*index);
  123. *index = -1;
  124. return err;
  125. }
  126. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  127. int block_mcast_loopback)
  128. {
  129. struct mlx4_priv *priv = mlx4_priv(dev);
  130. struct mlx4_cmd_mailbox *mailbox;
  131. struct mlx4_mgm *mgm;
  132. u32 members_count;
  133. u16 hash;
  134. int index, prev;
  135. int link = 0;
  136. int i;
  137. int err;
  138. mailbox = mlx4_alloc_cmd_mailbox(dev);
  139. if (IS_ERR(mailbox))
  140. return PTR_ERR(mailbox);
  141. mgm = mailbox->buf;
  142. mutex_lock(&priv->mcg_table.mutex);
  143. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  144. if (err)
  145. goto out;
  146. if (index != -1) {
  147. if (!memcmp(mgm->gid, zero_gid, 16))
  148. memcpy(mgm->gid, gid, 16);
  149. } else {
  150. link = 1;
  151. index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
  152. if (index == -1) {
  153. mlx4_err(dev, "No AMGM entries left\n");
  154. err = -ENOMEM;
  155. goto out;
  156. }
  157. index += dev->caps.num_mgms;
  158. memset(mgm, 0, sizeof *mgm);
  159. memcpy(mgm->gid, gid, 16);
  160. }
  161. members_count = be32_to_cpu(mgm->members_count);
  162. if (members_count == MLX4_QP_PER_MGM) {
  163. mlx4_err(dev, "MGM at index %x is full.\n", index);
  164. err = -ENOMEM;
  165. goto out;
  166. }
  167. for (i = 0; i < members_count; ++i)
  168. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
  169. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  170. err = 0;
  171. goto out;
  172. }
  173. if (block_mcast_loopback)
  174. mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
  175. (1U << MGM_BLCK_LB_BIT));
  176. else
  177. mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
  178. mgm->members_count = cpu_to_be32(members_count);
  179. err = mlx4_WRITE_MCG(dev, index, mailbox);
  180. if (err)
  181. goto out;
  182. if (!link)
  183. goto out;
  184. err = mlx4_READ_MCG(dev, prev, mailbox);
  185. if (err)
  186. goto out;
  187. mgm->next_gid_index = cpu_to_be32(index << 6);
  188. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  189. if (err)
  190. goto out;
  191. out:
  192. if (err && link && index != -1) {
  193. if (index < dev->caps.num_mgms)
  194. mlx4_warn(dev, "Got AMGM index %d < %d",
  195. index, dev->caps.num_mgms);
  196. else
  197. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  198. index - dev->caps.num_mgms);
  199. }
  200. mutex_unlock(&priv->mcg_table.mutex);
  201. mlx4_free_cmd_mailbox(dev, mailbox);
  202. return err;
  203. }
  204. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  205. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16])
  206. {
  207. struct mlx4_priv *priv = mlx4_priv(dev);
  208. struct mlx4_cmd_mailbox *mailbox;
  209. struct mlx4_mgm *mgm;
  210. u32 members_count;
  211. u16 hash;
  212. int prev, index;
  213. int i, loc;
  214. int err;
  215. mailbox = mlx4_alloc_cmd_mailbox(dev);
  216. if (IS_ERR(mailbox))
  217. return PTR_ERR(mailbox);
  218. mgm = mailbox->buf;
  219. mutex_lock(&priv->mcg_table.mutex);
  220. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  221. if (err)
  222. goto out;
  223. if (index == -1) {
  224. mlx4_err(dev, "MGID %pI6 not found\n", gid);
  225. err = -EINVAL;
  226. goto out;
  227. }
  228. members_count = be32_to_cpu(mgm->members_count);
  229. for (loc = -1, i = 0; i < members_count; ++i)
  230. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  231. loc = i;
  232. if (loc == -1) {
  233. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  234. err = -EINVAL;
  235. goto out;
  236. }
  237. mgm->members_count = cpu_to_be32(--members_count);
  238. mgm->qp[loc] = mgm->qp[i - 1];
  239. mgm->qp[i - 1] = 0;
  240. if (i != 1) {
  241. err = mlx4_WRITE_MCG(dev, index, mailbox);
  242. goto out;
  243. }
  244. if (prev == -1) {
  245. /* Remove entry from MGM */
  246. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  247. if (amgm_index) {
  248. err = mlx4_READ_MCG(dev, amgm_index, mailbox);
  249. if (err)
  250. goto out;
  251. } else
  252. memset(mgm->gid, 0, 16);
  253. err = mlx4_WRITE_MCG(dev, index, mailbox);
  254. if (err)
  255. goto out;
  256. if (amgm_index) {
  257. if (amgm_index < dev->caps.num_mgms)
  258. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  259. index, amgm_index, dev->caps.num_mgms);
  260. else
  261. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  262. amgm_index - dev->caps.num_mgms);
  263. }
  264. } else {
  265. /* Remove entry from AMGM */
  266. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  267. err = mlx4_READ_MCG(dev, prev, mailbox);
  268. if (err)
  269. goto out;
  270. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  271. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  272. if (err)
  273. goto out;
  274. if (index < dev->caps.num_mgms)
  275. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  276. prev, index, dev->caps.num_mgms);
  277. else
  278. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  279. index - dev->caps.num_mgms);
  280. }
  281. out:
  282. mutex_unlock(&priv->mcg_table.mutex);
  283. mlx4_free_cmd_mailbox(dev, mailbox);
  284. return err;
  285. }
  286. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  287. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  288. {
  289. struct mlx4_priv *priv = mlx4_priv(dev);
  290. int err;
  291. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  292. dev->caps.num_amgms - 1, 0, 0);
  293. if (err)
  294. return err;
  295. mutex_init(&priv->mcg_table.mutex);
  296. return 0;
  297. }
  298. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  299. {
  300. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  301. }