mcg.c 8.7 KB

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