intf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/slab.h>
  34. #include <linux/export.h>
  35. #include <linux/errno.h>
  36. #include <net/devlink.h>
  37. #include "mlx4.h"
  38. struct mlx4_device_context {
  39. struct list_head list;
  40. struct list_head bond_list;
  41. struct mlx4_interface *intf;
  42. void *context;
  43. };
  44. static LIST_HEAD(intf_list);
  45. static LIST_HEAD(dev_list);
  46. static DEFINE_MUTEX(intf_mutex);
  47. static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
  48. {
  49. struct mlx4_device_context *dev_ctx;
  50. dev_ctx = kmalloc(sizeof(*dev_ctx), GFP_KERNEL);
  51. if (!dev_ctx)
  52. return;
  53. dev_ctx->intf = intf;
  54. dev_ctx->context = intf->add(&priv->dev);
  55. if (dev_ctx->context) {
  56. spin_lock_irq(&priv->ctx_lock);
  57. list_add_tail(&dev_ctx->list, &priv->ctx_list);
  58. spin_unlock_irq(&priv->ctx_lock);
  59. if (intf->activate)
  60. intf->activate(&priv->dev, dev_ctx->context);
  61. } else
  62. kfree(dev_ctx);
  63. }
  64. static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
  65. {
  66. struct mlx4_device_context *dev_ctx;
  67. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  68. if (dev_ctx->intf == intf) {
  69. spin_lock_irq(&priv->ctx_lock);
  70. list_del(&dev_ctx->list);
  71. spin_unlock_irq(&priv->ctx_lock);
  72. intf->remove(&priv->dev, dev_ctx->context);
  73. kfree(dev_ctx);
  74. return;
  75. }
  76. }
  77. int mlx4_register_interface(struct mlx4_interface *intf)
  78. {
  79. struct mlx4_priv *priv;
  80. if (!intf->add || !intf->remove)
  81. return -EINVAL;
  82. mutex_lock(&intf_mutex);
  83. list_add_tail(&intf->list, &intf_list);
  84. list_for_each_entry(priv, &dev_list, dev_list) {
  85. if (mlx4_is_mfunc(&priv->dev) && (intf->flags & MLX4_INTFF_BONDING)) {
  86. mlx4_dbg(&priv->dev,
  87. "SRIOV, disabling HA mode for intf proto %d\n", intf->protocol);
  88. intf->flags &= ~MLX4_INTFF_BONDING;
  89. }
  90. mlx4_add_device(intf, priv);
  91. }
  92. mutex_unlock(&intf_mutex);
  93. return 0;
  94. }
  95. EXPORT_SYMBOL_GPL(mlx4_register_interface);
  96. void mlx4_unregister_interface(struct mlx4_interface *intf)
  97. {
  98. struct mlx4_priv *priv;
  99. mutex_lock(&intf_mutex);
  100. list_for_each_entry(priv, &dev_list, dev_list)
  101. mlx4_remove_device(intf, priv);
  102. list_del(&intf->list);
  103. mutex_unlock(&intf_mutex);
  104. }
  105. EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
  106. int mlx4_do_bond(struct mlx4_dev *dev, bool enable)
  107. {
  108. struct mlx4_priv *priv = mlx4_priv(dev);
  109. struct mlx4_device_context *dev_ctx = NULL, *temp_dev_ctx;
  110. unsigned long flags;
  111. int ret;
  112. LIST_HEAD(bond_list);
  113. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PORT_REMAP))
  114. return -EOPNOTSUPP;
  115. ret = mlx4_disable_rx_port_check(dev, enable);
  116. if (ret) {
  117. mlx4_err(dev, "Fail to %s rx port check\n",
  118. enable ? "enable" : "disable");
  119. return ret;
  120. }
  121. if (enable) {
  122. dev->flags |= MLX4_FLAG_BONDED;
  123. } else {
  124. ret = mlx4_virt2phy_port_map(dev, 1, 2);
  125. if (ret) {
  126. mlx4_err(dev, "Fail to reset port map\n");
  127. return ret;
  128. }
  129. dev->flags &= ~MLX4_FLAG_BONDED;
  130. }
  131. spin_lock_irqsave(&priv->ctx_lock, flags);
  132. list_for_each_entry_safe(dev_ctx, temp_dev_ctx, &priv->ctx_list, list) {
  133. if (dev_ctx->intf->flags & MLX4_INTFF_BONDING) {
  134. list_add_tail(&dev_ctx->bond_list, &bond_list);
  135. list_del(&dev_ctx->list);
  136. }
  137. }
  138. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  139. list_for_each_entry(dev_ctx, &bond_list, bond_list) {
  140. dev_ctx->intf->remove(dev, dev_ctx->context);
  141. dev_ctx->context = dev_ctx->intf->add(dev);
  142. spin_lock_irqsave(&priv->ctx_lock, flags);
  143. list_add_tail(&dev_ctx->list, &priv->ctx_list);
  144. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  145. mlx4_dbg(dev, "Interface for protocol %d restarted with bonded mode %s\n",
  146. dev_ctx->intf->protocol, enable ?
  147. "enabled" : "disabled");
  148. }
  149. return 0;
  150. }
  151. void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
  152. unsigned long param)
  153. {
  154. struct mlx4_priv *priv = mlx4_priv(dev);
  155. struct mlx4_device_context *dev_ctx;
  156. unsigned long flags;
  157. spin_lock_irqsave(&priv->ctx_lock, flags);
  158. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  159. if (dev_ctx->intf->event)
  160. dev_ctx->intf->event(dev, dev_ctx->context, type, param);
  161. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  162. }
  163. int mlx4_register_device(struct mlx4_dev *dev)
  164. {
  165. struct mlx4_priv *priv = mlx4_priv(dev);
  166. struct mlx4_interface *intf;
  167. mutex_lock(&intf_mutex);
  168. dev->persist->interface_state |= MLX4_INTERFACE_STATE_UP;
  169. list_add_tail(&priv->dev_list, &dev_list);
  170. list_for_each_entry(intf, &intf_list, list)
  171. mlx4_add_device(intf, priv);
  172. mutex_unlock(&intf_mutex);
  173. mlx4_start_catas_poll(dev);
  174. return 0;
  175. }
  176. void mlx4_unregister_device(struct mlx4_dev *dev)
  177. {
  178. struct mlx4_priv *priv = mlx4_priv(dev);
  179. struct mlx4_interface *intf;
  180. if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
  181. return;
  182. mlx4_stop_catas_poll(dev);
  183. if (dev->persist->interface_state & MLX4_INTERFACE_STATE_DELETION &&
  184. mlx4_is_slave(dev)) {
  185. /* In mlx4_remove_one on a VF */
  186. u32 slave_read =
  187. swab32(readl(&mlx4_priv(dev)->mfunc.comm->slave_read));
  188. if (mlx4_comm_internal_err(slave_read)) {
  189. mlx4_dbg(dev, "%s: comm channel is down, entering error state.\n",
  190. __func__);
  191. mlx4_enter_error_state(dev->persist);
  192. }
  193. }
  194. mutex_lock(&intf_mutex);
  195. list_for_each_entry(intf, &intf_list, list)
  196. mlx4_remove_device(intf, priv);
  197. list_del(&priv->dev_list);
  198. dev->persist->interface_state &= ~MLX4_INTERFACE_STATE_UP;
  199. mutex_unlock(&intf_mutex);
  200. }
  201. void *mlx4_get_protocol_dev(struct mlx4_dev *dev, enum mlx4_protocol proto, int port)
  202. {
  203. struct mlx4_priv *priv = mlx4_priv(dev);
  204. struct mlx4_device_context *dev_ctx;
  205. unsigned long flags;
  206. void *result = NULL;
  207. spin_lock_irqsave(&priv->ctx_lock, flags);
  208. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  209. if (dev_ctx->intf->protocol == proto && dev_ctx->intf->get_dev) {
  210. result = dev_ctx->intf->get_dev(dev, dev_ctx->context, port);
  211. break;
  212. }
  213. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  214. return result;
  215. }
  216. EXPORT_SYMBOL_GPL(mlx4_get_protocol_dev);
  217. struct devlink_port *mlx4_get_devlink_port(struct mlx4_dev *dev, int port)
  218. {
  219. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  220. return &info->devlink_port;
  221. }
  222. EXPORT_SYMBOL_GPL(mlx4_get_devlink_port);