mcg.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/init.h>
  33. #include <linux/string.h>
  34. #include <linux/slab.h>
  35. #include <linux/mlx4/cmd.h>
  36. #include "mlx4.h"
  37. struct mlx4_mgm {
  38. __be32 next_gid_index;
  39. __be32 members_count;
  40. u32 reserved[2];
  41. u8 gid[16];
  42. __be32 qp[MLX4_QP_PER_MGM];
  43. };
  44. static const u8 zero_gid[16]; /* automatically initialized to 0 */
  45. static int mlx4_READ_MCG(struct mlx4_dev *dev, int index,
  46. struct mlx4_cmd_mailbox *mailbox)
  47. {
  48. return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
  49. MLX4_CMD_TIME_CLASS_A);
  50. }
  51. static int mlx4_WRITE_MCG(struct mlx4_dev *dev, int index,
  52. struct mlx4_cmd_mailbox *mailbox)
  53. {
  54. return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
  55. MLX4_CMD_TIME_CLASS_A);
  56. }
  57. static int mlx4_MGID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  58. u16 *hash)
  59. {
  60. u64 imm;
  61. int err;
  62. err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, 0, MLX4_CMD_MGID_HASH,
  63. MLX4_CMD_TIME_CLASS_A);
  64. if (!err)
  65. *hash = imm;
  66. return err;
  67. }
  68. /*
  69. * Caller must hold MCG table semaphore. gid and mgm parameters must
  70. * be properly aligned for command interface.
  71. *
  72. * Returns 0 unless a firmware command error occurs.
  73. *
  74. * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
  75. * and *mgm holds MGM entry.
  76. *
  77. * if GID is found in AMGM, *index = index in AMGM, *prev = index of
  78. * previous entry in hash chain and *mgm holds AMGM entry.
  79. *
  80. * If no AMGM exists for given gid, *index = -1, *prev = index of last
  81. * entry in hash chain and *mgm holds end of hash chain.
  82. */
  83. static int find_mgm(struct mlx4_dev *dev,
  84. u8 *gid, struct mlx4_cmd_mailbox *mgm_mailbox,
  85. u16 *hash, int *prev, int *index)
  86. {
  87. struct mlx4_cmd_mailbox *mailbox;
  88. struct mlx4_mgm *mgm = mgm_mailbox->buf;
  89. u8 *mgid;
  90. int err;
  91. mailbox = mlx4_alloc_cmd_mailbox(dev);
  92. if (IS_ERR(mailbox))
  93. return -ENOMEM;
  94. mgid = mailbox->buf;
  95. memcpy(mgid, gid, 16);
  96. err = mlx4_MGID_HASH(dev, mailbox, hash);
  97. mlx4_free_cmd_mailbox(dev, mailbox);
  98. if (err)
  99. return err;
  100. if (0)
  101. mlx4_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
  102. "%04x:%04x:%04x:%04x is %04x\n",
  103. be16_to_cpu(((__be16 *) gid)[0]),
  104. be16_to_cpu(((__be16 *) gid)[1]),
  105. be16_to_cpu(((__be16 *) gid)[2]),
  106. be16_to_cpu(((__be16 *) gid)[3]),
  107. be16_to_cpu(((__be16 *) gid)[4]),
  108. be16_to_cpu(((__be16 *) gid)[5]),
  109. be16_to_cpu(((__be16 *) gid)[6]),
  110. be16_to_cpu(((__be16 *) gid)[7]),
  111. *hash);
  112. *index = *hash;
  113. *prev = -1;
  114. do {
  115. err = mlx4_READ_MCG(dev, *index, mgm_mailbox);
  116. if (err)
  117. return err;
  118. if (!memcmp(mgm->gid, zero_gid, 16)) {
  119. if (*index != *hash) {
  120. mlx4_err(dev, "Found zero MGID in AMGM.\n");
  121. err = -EINVAL;
  122. }
  123. return err;
  124. }
  125. if (!memcmp(mgm->gid, gid, 16))
  126. return err;
  127. *prev = *index;
  128. *index = be32_to_cpu(mgm->next_gid_index) >> 6;
  129. } while (*index);
  130. *index = -1;
  131. return err;
  132. }
  133. int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16])
  134. {
  135. struct mlx4_priv *priv = mlx4_priv(dev);
  136. struct mlx4_cmd_mailbox *mailbox;
  137. struct mlx4_mgm *mgm;
  138. u32 members_count;
  139. u16 hash;
  140. int index, prev;
  141. int link = 0;
  142. int i;
  143. int err;
  144. mailbox = mlx4_alloc_cmd_mailbox(dev);
  145. if (IS_ERR(mailbox))
  146. return PTR_ERR(mailbox);
  147. mgm = mailbox->buf;
  148. mutex_lock(&priv->mcg_table.mutex);
  149. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  150. if (err)
  151. goto out;
  152. if (index != -1) {
  153. if (!memcmp(mgm->gid, zero_gid, 16))
  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. err = mlx4_READ_MCG(dev, index, mailbox);
  165. if (err)
  166. goto out;
  167. memset(mgm, 0, sizeof *mgm);
  168. memcpy(mgm->gid, gid, 16);
  169. }
  170. members_count = be32_to_cpu(mgm->members_count);
  171. if (members_count == MLX4_QP_PER_MGM) {
  172. mlx4_err(dev, "MGM at index %x is full.\n", index);
  173. err = -ENOMEM;
  174. goto out;
  175. }
  176. for (i = 0; i < members_count; ++i)
  177. if (mgm->qp[i] == cpu_to_be32(qp->qpn)) {
  178. mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
  179. err = 0;
  180. goto out;
  181. }
  182. mgm->qp[members_count++] = cpu_to_be32(qp->qpn);
  183. mgm->members_count = cpu_to_be32(members_count);
  184. err = mlx4_WRITE_MCG(dev, index, mailbox);
  185. if (err)
  186. goto out;
  187. if (!link)
  188. goto out;
  189. err = mlx4_READ_MCG(dev, prev, mailbox);
  190. if (err)
  191. goto out;
  192. mgm->next_gid_index = cpu_to_be32(index << 6);
  193. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  194. if (err)
  195. goto out;
  196. out:
  197. if (err && link && index != -1) {
  198. if (index < dev->caps.num_mgms)
  199. mlx4_warn(dev, "Got AMGM index %d < %d",
  200. index, dev->caps.num_mgms);
  201. else
  202. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  203. index - dev->caps.num_mgms);
  204. }
  205. mutex_unlock(&priv->mcg_table.mutex);
  206. mlx4_free_cmd_mailbox(dev, mailbox);
  207. return err;
  208. }
  209. EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
  210. int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16])
  211. {
  212. struct mlx4_priv *priv = mlx4_priv(dev);
  213. struct mlx4_cmd_mailbox *mailbox;
  214. struct mlx4_mgm *mgm;
  215. u32 members_count;
  216. u16 hash;
  217. int prev, index;
  218. int i, loc;
  219. int err;
  220. mailbox = mlx4_alloc_cmd_mailbox(dev);
  221. if (IS_ERR(mailbox))
  222. return PTR_ERR(mailbox);
  223. mgm = mailbox->buf;
  224. mutex_lock(&priv->mcg_table.mutex);
  225. err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
  226. if (err)
  227. goto out;
  228. if (index == -1) {
  229. mlx4_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  230. "not found\n",
  231. be16_to_cpu(((__be16 *) gid)[0]),
  232. be16_to_cpu(((__be16 *) gid)[1]),
  233. be16_to_cpu(((__be16 *) gid)[2]),
  234. be16_to_cpu(((__be16 *) gid)[3]),
  235. be16_to_cpu(((__be16 *) gid)[4]),
  236. be16_to_cpu(((__be16 *) gid)[5]),
  237. be16_to_cpu(((__be16 *) gid)[6]),
  238. be16_to_cpu(((__be16 *) gid)[7]));
  239. err = -EINVAL;
  240. goto out;
  241. }
  242. members_count = be32_to_cpu(mgm->members_count);
  243. for (loc = -1, i = 0; i < members_count; ++i)
  244. if (mgm->qp[i] == cpu_to_be32(qp->qpn))
  245. loc = i;
  246. if (loc == -1) {
  247. mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
  248. err = -EINVAL;
  249. goto out;
  250. }
  251. mgm->members_count = cpu_to_be32(--members_count);
  252. mgm->qp[loc] = mgm->qp[i - 1];
  253. mgm->qp[i - 1] = 0;
  254. err = mlx4_WRITE_MCG(dev, index, mailbox);
  255. if (err)
  256. goto out;
  257. if (i != 1)
  258. goto out;
  259. if (prev == -1) {
  260. /* Remove entry from MGM */
  261. int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  262. if (amgm_index) {
  263. err = mlx4_READ_MCG(dev, amgm_index, mailbox);
  264. if (err)
  265. goto out;
  266. } else
  267. memset(mgm->gid, 0, 16);
  268. err = mlx4_WRITE_MCG(dev, index, mailbox);
  269. if (err)
  270. goto out;
  271. if (amgm_index) {
  272. if (amgm_index < dev->caps.num_mgms)
  273. mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
  274. index, amgm_index, dev->caps.num_mgms);
  275. else
  276. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  277. amgm_index - dev->caps.num_mgms);
  278. }
  279. } else {
  280. /* Remove entry from AMGM */
  281. int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
  282. err = mlx4_READ_MCG(dev, prev, mailbox);
  283. if (err)
  284. goto out;
  285. mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
  286. err = mlx4_WRITE_MCG(dev, prev, mailbox);
  287. if (err)
  288. goto out;
  289. if (index < dev->caps.num_mgms)
  290. mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
  291. prev, index, dev->caps.num_mgms);
  292. else
  293. mlx4_bitmap_free(&priv->mcg_table.bitmap,
  294. index - dev->caps.num_mgms);
  295. }
  296. out:
  297. mutex_unlock(&priv->mcg_table.mutex);
  298. mlx4_free_cmd_mailbox(dev, mailbox);
  299. return err;
  300. }
  301. EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
  302. int __devinit mlx4_init_mcg_table(struct mlx4_dev *dev)
  303. {
  304. struct mlx4_priv *priv = mlx4_priv(dev);
  305. int err;
  306. err = mlx4_bitmap_init(&priv->mcg_table.bitmap,
  307. dev->caps.num_amgms, dev->caps.num_amgms - 1, 0);
  308. if (err)
  309. return err;
  310. mutex_init(&priv->mcg_table.mutex);
  311. return 0;
  312. }
  313. void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
  314. {
  315. mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
  316. }