mcg.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/string.h>
  34. #include <linux/mlx4/cmd.h>
  35. #include "mlx4.h"
  36. #define MGM_QPN_MASK 0x00FFFFFF
  37. #define MGM_BLCK_LB_BIT 30
  38. struct mlx4_mgm {
  39. __be32 next_gid_index;
  40. __be32 members_count;
  41. u32 reserved[2];
  42. u8 gid[16];
  43. __be32 qp[MLX4_QP_PER_MGM];
  44. };
  45. static const u8 zero_gid[16]; /* automatically initialized to 0 */
  46. static int mlx4_READ_MCG(struct mlx4_dev *dev, int index,
  47. struct mlx4_cmd_mailbox *mailbox)
  48. {
  49. return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
  50. MLX4_CMD_TIME_CLASS_A);
  51. }
  52. static int mlx4_WRITE_MCG(struct mlx4_dev *dev, int index,
  53. struct mlx4_cmd_mailbox *mailbox)
  54. {
  55. return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
  56. MLX4_CMD_TIME_CLASS_A);
  57. }
  58. static int mlx4_MGID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  59. u16 *hash)
  60. {
  61. u64 imm;
  62. int err;
  63. err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, 0, MLX4_CMD_MGID_HASH,
  64. MLX4_CMD_TIME_CLASS_A);
  65. if (!err)
  66. *hash = imm;
  67. return err;
  68. }
  69. /*
  70. * Caller must hold MCG table semaphore. gid and mgm parameters must
  71. * be properly aligned for command interface.
  72. *
  73. * Returns 0 unless a firmware command error occurs.
  74. *
  75. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  76. * and *mgm holds MGM entry.
  77. *
  78. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  79. * previous entry in hash chain and *mgm holds AMGM entry.
  80. *
  81. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  82. * entry in hash chain and *mgm holds end of hash chain.
  83. */
  84. static int find_mgm(struct mlx4_dev *dev,
  85. u8 *gid, struct mlx4_cmd_mailbox *mgm_mailbox,
  86. u16 *hash, int *prev, int *index)
  87. {
  88. struct mlx4_cmd_mailbox *mailbox;
  89. struct mlx4_mgm *mgm = mgm_mailbox->buf;
  90. u8 *mgid;
  91. int err;
  92. mailbox = mlx4_alloc_cmd_mailbox(dev);
  93. if (IS_ERR(mailbox))
  94. return -ENOMEM;
  95. mgid = mailbox->buf;
  96. memcpy(mgid, gid, 16);
  97. err = mlx4_MGID_HASH(dev, mailbox, hash);
  98. mlx4_free_cmd_mailbox(dev, mailbox);
  99. if (err)
  100. return err;
  101. if (0)
  102. mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
  103. *index = *hash;
  104. *prev = -1;
  105. do {
  106. err = mlx4_READ_MCG(dev, *index, mgm_mailbox);
  107. if (err)
  108. return err;
  109. if (!memcmp(mgm->gid, zero_gid, 16)) {
  110. if (*index != *hash) {
  111. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  112. err = -EINVAL;
  113. }
  114. return err;
  115. }
  116. if (!memcmp(mgm->gid, gid, 16))
  117. return err;
  118. *prev = *index;
  119. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  120. } while (*index);
  121. *index = -1;
  122. return err;
  123. }
  124. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  125. int block_mcast_loopback)
  126. {
  127. struct mlx4_priv *priv = mlx4_priv(dev);
  128. struct mlx4_cmd_mailbox *mailbox;
  129. struct mlx4_mgm *mgm;
  130. u32 members_count;
  131. u16 hash;
  132. int index, prev;
  133. int link = 0;
  134. int i;
  135. int err;
  136. mailbox = mlx4_alloc_cmd_mailbox(dev);
  137. if (IS_ERR(mailbox))
  138. return PTR_ERR(mailbox);
  139. mgm = mailbox->buf;
  140. mutex_lock(&priv->mcg_table.mutex);
  141. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  142. if (err)
  143. goto out;
  144. if (index != -1) {
  145. if (!memcmp(mgm->gid, zero_gid, 16))
  146. memcpy(mgm->gid, gid, 16);
  147. } else {
  148. link = 1;
  149. index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
  150. if (index == -1) {
  151. mlx4_err(dev, "No AMGM entries left\n");
  152. err = -ENOMEM;
  153. goto out;
  154. }
  155. index += dev->caps.num_mgms;
  156. memset(mgm, 0, sizeof *mgm);
  157. memcpy(mgm->gid, gid, 16);
  158. }
  159. members_count = be32_to_cpu(mgm->members_count);
  160. if (members_count == MLX4_QP_PER_MGM) {
  161. mlx4_err(dev, "MGM at index %x is full.\n", index);
  162. err = -ENOMEM;
  163. goto out;
  164. }
  165. for (i = 0; i < members_count; ++i)
  166. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
  167. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  168. err = 0;
  169. goto out;
  170. }
  171. if (block_mcast_loopback)
  172. mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
  173. (1U << MGM_BLCK_LB_BIT));
  174. else
  175. mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
  176. mgm->members_count = cpu_to_be32(members_count);
  177. err = mlx4_WRITE_MCG(dev, index, mailbox);
  178. if (err)
  179. goto out;
  180. if (!link)
  181. goto out;
  182. err = mlx4_READ_MCG(dev, prev, mailbox);
  183. if (err)
  184. goto out;
  185. mgm->next_gid_index = cpu_to_be32(index << 6);
  186. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  187. if (err)
  188. goto out;
  189. out:
  190. if (err && link && index != -1) {
  191. if (index < dev->caps.num_mgms)
  192. mlx4_warn(dev, "Got AMGM index %d < %d",
  193. index, dev->caps.num_mgms);
  194. else
  195. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  196. index - dev->caps.num_mgms);
  197. }
  198. mutex_unlock(&priv->mcg_table.mutex);
  199. mlx4_free_cmd_mailbox(dev, mailbox);
  200. return err;
  201. }
  202. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  203. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16])
  204. {
  205. struct mlx4_priv *priv = mlx4_priv(dev);
  206. struct mlx4_cmd_mailbox *mailbox;
  207. struct mlx4_mgm *mgm;
  208. u32 members_count;
  209. u16 hash;
  210. int prev, index;
  211. int i, loc;
  212. int err;
  213. mailbox = mlx4_alloc_cmd_mailbox(dev);
  214. if (IS_ERR(mailbox))
  215. return PTR_ERR(mailbox);
  216. mgm = mailbox->buf;
  217. mutex_lock(&priv->mcg_table.mutex);
  218. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  219. if (err)
  220. goto out;
  221. if (index == -1) {
  222. mlx4_err(dev, "MGID %pI6 not found\n", gid);
  223. err = -EINVAL;
  224. goto out;
  225. }
  226. members_count = be32_to_cpu(mgm->members_count);
  227. for (loc = -1, i = 0; i < members_count; ++i)
  228. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  229. loc = i;
  230. if (loc == -1) {
  231. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  232. err = -EINVAL;
  233. goto out;
  234. }
  235. mgm->members_count = cpu_to_be32(--members_count);
  236. mgm->qp[loc] = mgm->qp[i - 1];
  237. mgm->qp[i - 1] = 0;
  238. if (i != 1) {
  239. err = mlx4_WRITE_MCG(dev, index, mailbox);
  240. goto out;
  241. }
  242. if (prev == -1) {
  243. /* Remove entry from MGM */
  244. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  245. if (amgm_index) {
  246. err = mlx4_READ_MCG(dev, amgm_index, mailbox);
  247. if (err)
  248. goto out;
  249. } else
  250. memset(mgm->gid, 0, 16);
  251. err = mlx4_WRITE_MCG(dev, index, mailbox);
  252. if (err)
  253. goto out;
  254. if (amgm_index) {
  255. if (amgm_index < dev->caps.num_mgms)
  256. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  257. index, amgm_index, dev->caps.num_mgms);
  258. else
  259. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  260. amgm_index - dev->caps.num_mgms);
  261. }
  262. } else {
  263. /* Remove entry from AMGM */
  264. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  265. err = mlx4_READ_MCG(dev, prev, mailbox);
  266. if (err)
  267. goto out;
  268. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  269. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  270. if (err)
  271. goto out;
  272. if (index < dev->caps.num_mgms)
  273. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  274. prev, index, dev->caps.num_mgms);
  275. else
  276. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  277. index - dev->caps.num_mgms);
  278. }
  279. out:
  280. mutex_unlock(&priv->mcg_table.mutex);
  281. mlx4_free_cmd_mailbox(dev, mailbox);
  282. return err;
  283. }
  284. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  285. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  286. {
  287. struct mlx4_priv *priv = mlx4_priv(dev);
  288. int err;
  289. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  290. dev->caps.num_amgms - 1, 0, 0);
  291. if (err)
  292. return err;
  293. mutex_init(&priv->mcg_table.mutex);
  294. return 0;
  295. }
  296. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  297. {
  298. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  299. }