mcg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/etherdevice.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_ENTRY(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_ENTRY(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_GID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  60. u16 *hash, u8 op_mod)
  61. {
  62. u64 imm;
  63. int err;
  64. err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, op_mod,
  65. MLX4_CMD_MGID_HASH, 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_entry(struct mlx4_dev *dev, u8 port,
  86. u8 *gid, enum mlx4_protocol prot,
  87. enum mlx4_steer_type steer,
  88. struct mlx4_cmd_mailbox *mgm_mailbox,
  89. u16 *hash, int *prev, int *index)
  90. {
  91. struct mlx4_cmd_mailbox *mailbox;
  92. struct mlx4_mgm *mgm = mgm_mailbox->buf;
  93. u8 *mgid;
  94. int err;
  95. u8 op_mod = (prot == MLX4_PROT_ETH) ? !!(dev->caps.vep_mc_steering) : 0;
  96. mailbox = mlx4_alloc_cmd_mailbox(dev);
  97. if (IS_ERR(mailbox))
  98. return -ENOMEM;
  99. mgid = mailbox->buf;
  100. memcpy(mgid, gid, 16);
  101. err = mlx4_GID_HASH(dev, mailbox, hash, op_mod);
  102. mlx4_free_cmd_mailbox(dev, mailbox);
  103. if (err)
  104. return err;
  105. if (0)
  106. mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
  107. *index = *hash;
  108. *prev = -1;
  109. do {
  110. err = mlx4_READ_ENTRY(dev, *index, mgm_mailbox);
  111. if (err)
  112. return err;
  113. if (!(be32_to_cpu(mgm->members_count) & 0xffffff)) {
  114. if (*index != *hash) {
  115. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  116. err = -EINVAL;
  117. }
  118. return err;
  119. }
  120. if (!memcmp(mgm->gid, gid, 16) &&
  121. be32_to_cpu(mgm->members_count) >> 30 == prot)
  122. return err;
  123. *prev = *index;
  124. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  125. } while (*index);
  126. *index = -1;
  127. return err;
  128. }
  129. int mlx4_qp_attach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  130. int block_mcast_loopback, enum mlx4_protocol prot,
  131. enum mlx4_steer_type steer)
  132. {
  133. struct mlx4_priv *priv = mlx4_priv(dev);
  134. struct mlx4_cmd_mailbox *mailbox;
  135. struct mlx4_mgm *mgm;
  136. u32 members_count;
  137. u16 hash;
  138. int index, prev;
  139. int link = 0;
  140. int i;
  141. int err;
  142. u8 port = gid[5];
  143. mailbox = mlx4_alloc_cmd_mailbox(dev);
  144. if (IS_ERR(mailbox))
  145. return PTR_ERR(mailbox);
  146. mgm = mailbox->buf;
  147. mutex_lock(&priv->mcg_table.mutex);
  148. err = find_entry(dev, port, gid, prot, steer,
  149. mailbox, &hash, &prev, &index);
  150. if (err)
  151. goto out;
  152. if (index != -1) {
  153. if (!(be32_to_cpu(mgm->members_count) & 0xffffff))
  154. memcpy(mgm->gid, gid, 16);
  155. } else {
  156. link = 1;
  157. index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
  158. if (index == -1) {
  159. mlx4_err(dev, "No AMGM entries left\n");
  160. err = -ENOMEM;
  161. goto out;
  162. }
  163. index += dev->caps.num_mgms;
  164. memset(mgm, 0, sizeof *mgm);
  165. memcpy(mgm->gid, gid, 16);
  166. }
  167. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  168. if (members_count == MLX4_QP_PER_MGM) {
  169. mlx4_err(dev, "MGM at index %x is full.\n", index);
  170. err = -ENOMEM;
  171. goto out;
  172. }
  173. for (i = 0; i < members_count; ++i)
  174. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
  175. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  176. err = 0;
  177. goto out;
  178. }
  179. if (block_mcast_loopback)
  180. mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
  181. (1U << MGM_BLCK_LB_BIT));
  182. else
  183. mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
  184. mgm->members_count = cpu_to_be32(members_count | (u32) prot << 30);
  185. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  186. if (err)
  187. goto out;
  188. if (!link)
  189. goto out;
  190. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  191. if (err)
  192. goto out;
  193. mgm->next_gid_index = cpu_to_be32(index << 6);
  194. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  195. if (err)
  196. goto out;
  197. out:
  198. if (err && link && index != -1) {
  199. if (index < dev->caps.num_mgms)
  200. mlx4_warn(dev, "Got AMGM index %d < %d",
  201. index, dev->caps.num_mgms);
  202. else
  203. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  204. index - dev->caps.num_mgms);
  205. }
  206. mutex_unlock(&priv->mcg_table.mutex);
  207. mlx4_free_cmd_mailbox(dev, mailbox);
  208. return err;
  209. }
  210. int mlx4_qp_detach_common(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  211. enum mlx4_protocol prot, enum mlx4_steer_type steer)
  212. {
  213. struct mlx4_priv *priv = mlx4_priv(dev);
  214. struct mlx4_cmd_mailbox *mailbox;
  215. struct mlx4_mgm *mgm;
  216. u32 members_count;
  217. u16 hash;
  218. int prev, index;
  219. int i, loc;
  220. int err;
  221. u8 port = gid[5];
  222. mailbox = mlx4_alloc_cmd_mailbox(dev);
  223. if (IS_ERR(mailbox))
  224. return PTR_ERR(mailbox);
  225. mgm = mailbox->buf;
  226. mutex_lock(&priv->mcg_table.mutex);
  227. err = find_entry(dev, port, gid, prot, steer,
  228. mailbox, &hash, &prev, &index);
  229. if (err)
  230. goto out;
  231. if (index == -1) {
  232. mlx4_err(dev, "MGID %pI6 not found\n", gid);
  233. err = -EINVAL;
  234. goto out;
  235. }
  236. members_count = be32_to_cpu(mgm->members_count) & 0xffffff;
  237. for (loc = -1, i = 0; i < members_count; ++i)
  238. if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
  239. loc = i;
  240. if (loc == -1) {
  241. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  242. err = -EINVAL;
  243. goto out;
  244. }
  245. mgm->members_count = cpu_to_be32(--members_count | (u32) prot << 30);
  246. mgm->qp[loc] = mgm->qp[i - 1];
  247. mgm->qp[i - 1] = 0;
  248. if (i != 1) {
  249. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  250. goto out;
  251. }
  252. if (prev == -1) {
  253. /* Remove entry from MGM */
  254. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  255. if (amgm_index) {
  256. err = mlx4_READ_ENTRY(dev, amgm_index, mailbox);
  257. if (err)
  258. goto out;
  259. } else
  260. memset(mgm->gid, 0, 16);
  261. err = mlx4_WRITE_ENTRY(dev, index, mailbox);
  262. if (err)
  263. goto out;
  264. if (amgm_index) {
  265. if (amgm_index < dev->caps.num_mgms)
  266. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  267. index, amgm_index, dev->caps.num_mgms);
  268. else
  269. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  270. amgm_index - dev->caps.num_mgms);
  271. }
  272. } else {
  273. /* Remove entry from AMGM */
  274. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  275. err = mlx4_READ_ENTRY(dev, prev, mailbox);
  276. if (err)
  277. goto out;
  278. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  279. err = mlx4_WRITE_ENTRY(dev, prev, mailbox);
  280. if (err)
  281. goto out;
  282. if (index < dev->caps.num_mgms)
  283. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  284. prev, index, dev->caps.num_mgms);
  285. else
  286. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  287. index - dev->caps.num_mgms);
  288. }
  289. out:
  290. mutex_unlock(&priv->mcg_table.mutex);
  291. mlx4_free_cmd_mailbox(dev, mailbox);
  292. return err;
  293. }
  294. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  295. int block_mcast_loopback, enum mlx4_protocol prot)
  296. {
  297. enum mlx4_steer_type steer;
  298. steer = (is_valid_ether_addr(&gid[10])) ? MLX4_UC_STEER : MLX4_MC_STEER;
  299. if (prot == MLX4_PROT_ETH && !dev->caps.vep_mc_steering)
  300. return 0;
  301. if (prot == MLX4_PROT_ETH)
  302. gid[7] |= (steer << 1);
  303. return mlx4_qp_attach_common(dev, qp, gid,
  304. block_mcast_loopback, prot,
  305. steer);
  306. }
  307. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  308. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
  309. enum mlx4_protocol prot)
  310. {
  311. enum mlx4_steer_type steer;
  312. steer = (is_valid_ether_addr(&gid[10])) ? MLX4_UC_STEER : MLX4_MC_STEER;
  313. if (prot == MLX4_PROT_ETH && !dev->caps.vep_mc_steering)
  314. return 0;
  315. if (prot == MLX4_PROT_ETH) {
  316. gid[7] |= (steer << 1);
  317. }
  318. return mlx4_qp_detach_common(dev, qp, gid, prot, steer);
  319. }
  320. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  321. int mlx4_init_mcg_table(struct mlx4_dev *dev)
  322. {
  323. struct mlx4_priv *priv = mlx4_priv(dev);
  324. int err;
  325. err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
  326. dev->caps.num_amgms - 1, 0, 0);
  327. if (err)
  328. return err;
  329. mutex_init(&priv->mcg_table.mutex);
  330. return 0;
  331. }
  332. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  333. {
  334. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  335. }