mcg.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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, enum mlx4_protocol protocol,
  86. 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. be32_to_cpu(mgm->members_count) >> 30 == protocol)
  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, enum mlx4_protocol protocol)
  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, protocol, 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) & 0xffffff;
  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 | (u32) protocol << 30);
  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. enum mlx4_protocol protocol)
  207. {
  208. struct mlx4_priv *priv = mlx4_priv(dev);
  209. struct mlx4_cmd_mailbox *mailbox;
  210. struct mlx4_mgm *mgm;
  211. u32 members_count;
  212. u16 hash;
  213. int prev, index;
  214. int i, loc;
  215. int err;
  216. mailbox = mlx4_alloc_cmd_mailbox(dev);
  217. if (IS_ERR(mailbox))
  218. return PTR_ERR(mailbox);
  219. mgm = mailbox->buf;
  220. mutex_lock(&priv->mcg_table.mutex);
  221. err = find_mgm(dev, gid, protocol, mailbox, &hash, &prev, &index);
  222. if (err)
  223. goto out;
  224. if (index == -1) {
  225. mlx4_err(dev, "MGID %pI6 not found\n", gid);
  226. err = -EINVAL;
  227. goto out;
  228. }
  229. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  230. for (loc = -1, i = 0; i < members_count; ++i)
  231. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  232. loc = i;
  233. if (loc == -1) {
  234. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  235. err = -EINVAL;
  236. goto out;
  237. }
  238. mgm->members_count = cpu_to_be32(--members_count | (u32) protocol << 30);
  239. mgm->qp[loc] = mgm->qp[i - 1];
  240. mgm->qp[i - 1] = 0;
  241. if (i != 1) {
  242. err = mlx4_WRITE_MCG(dev, index, mailbox);
  243. goto out;
  244. }
  245. if (prev == -1) {
  246. /* Remove entry from MGM */
  247. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  248. if (amgm_index) {
  249. err = mlx4_READ_MCG(dev, amgm_index, mailbox);
  250. if (err)
  251. goto out;
  252. } else
  253. memset(mgm->gid, 0, 16);
  254. err = mlx4_WRITE_MCG(dev, index, mailbox);
  255. if (err)
  256. goto out;
  257. if (amgm_index) {
  258. if (amgm_index < dev->caps.num_mgms)
  259. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  260. index, amgm_index, dev->caps.num_mgms);
  261. else
  262. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  263. amgm_index - dev->caps.num_mgms);
  264. }
  265. } else {
  266. /* Remove entry from AMGM */
  267. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  268. err = mlx4_READ_MCG(dev, prev, mailbox);
  269. if (err)
  270. goto out;
  271. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  272. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  273. if (err)
  274. goto out;
  275. if (index < dev->caps.num_mgms)
  276. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  277. prev, index, dev->caps.num_mgms);
  278. else
  279. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  280. index - dev->caps.num_mgms);
  281. }
  282. out:
  283. mutex_unlock(&priv->mcg_table.mutex);
  284. mlx4_free_cmd_mailbox(dev, mailbox);
  285. return err;
  286. }
  287. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  288. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  289. {
  290. struct mlx4_priv *priv = mlx4_priv(dev);
  291. int err;
  292. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  293. dev->caps.num_amgms - 1, 0, 0);
  294. if (err)
  295. return err;
  296. mutex_init(&priv->mcg_table.mutex);
  297. return 0;
  298. }
  299. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  300. {
  301. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  302. }