intf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "mlx4.h"
  37. struct mlx4_device_context {
  38. struct list_head list;
  39. struct list_head bond_list;
  40. struct mlx4_interface *intf;
  41. void *context;
  42. };
  43. static LIST_HEAD(intf_list);
  44. static LIST_HEAD(dev_list);
  45. static DEFINE_MUTEX(intf_mutex);
  46. static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
  47. {
  48. struct mlx4_device_context *dev_ctx;
  49. dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
  50. if (!dev_ctx)
  51. return;
  52. dev_ctx->intf = intf;
  53. dev_ctx->context = intf->add(&priv->dev);
  54. if (dev_ctx->context) {
  55. spin_lock_irq(&priv->ctx_lock);
  56. list_add_tail(&dev_ctx->list, &priv->ctx_list);
  57. spin_unlock_irq(&priv->ctx_lock);
  58. } else
  59. kfree(dev_ctx);
  60. }
  61. static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
  62. {
  63. struct mlx4_device_context *dev_ctx;
  64. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  65. if (dev_ctx->intf == intf) {
  66. spin_lock_irq(&priv->ctx_lock);
  67. list_del(&dev_ctx->list);
  68. spin_unlock_irq(&priv->ctx_lock);
  69. intf->remove(&priv->dev, dev_ctx->context);
  70. kfree(dev_ctx);
  71. return;
  72. }
  73. }
  74. int mlx4_register_interface(struct mlx4_interface *intf)
  75. {
  76. struct mlx4_priv *priv;
  77. if (!intf->add || !intf->remove)
  78. return -EINVAL;
  79. mutex_lock(&intf_mutex);
  80. list_add_tail(&intf->list, &intf_list);
  81. list_for_each_entry(priv, &dev_list, dev_list)
  82. mlx4_add_device(intf, priv);
  83. mutex_unlock(&intf_mutex);
  84. return 0;
  85. }
  86. EXPORT_SYMBOL_GPL(mlx4_register_interface);
  87. void mlx4_unregister_interface(struct mlx4_interface *intf)
  88. {
  89. struct mlx4_priv *priv;
  90. mutex_lock(&intf_mutex);
  91. list_for_each_entry(priv, &dev_list, dev_list)
  92. mlx4_remove_device(intf, priv);
  93. list_del(&intf->list);
  94. mutex_unlock(&intf_mutex);
  95. }
  96. EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
  97. int mlx4_do_bond(struct mlx4_dev *dev, bool enable)
  98. {
  99. struct mlx4_priv *priv = mlx4_priv(dev);
  100. struct mlx4_device_context *dev_ctx = NULL, *temp_dev_ctx;
  101. unsigned long flags;
  102. int ret;
  103. LIST_HEAD(bond_list);
  104. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PORT_REMAP))
  105. return -ENOTSUPP;
  106. ret = mlx4_disable_rx_port_check(dev, enable);
  107. if (ret) {
  108. mlx4_err(dev, "Fail to %s rx port check\n",
  109. enable ? "enable" : "disable");
  110. return ret;
  111. }
  112. if (enable) {
  113. dev->flags |= MLX4_FLAG_BONDED;
  114. } else {
  115. ret = mlx4_virt2phy_port_map(dev, 1, 2);
  116. if (ret) {
  117. mlx4_err(dev, "Fail to reset port map\n");
  118. return ret;
  119. }
  120. dev->flags &= ~MLX4_FLAG_BONDED;
  121. }
  122. spin_lock_irqsave(&priv->ctx_lock, flags);
  123. list_for_each_entry_safe(dev_ctx, temp_dev_ctx, &priv->ctx_list, list) {
  124. if (dev_ctx->intf->flags & MLX4_INTFF_BONDING) {
  125. list_add_tail(&dev_ctx->bond_list, &bond_list);
  126. list_del(&dev_ctx->list);
  127. }
  128. }
  129. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  130. list_for_each_entry(dev_ctx, &bond_list, bond_list) {
  131. dev_ctx->intf->remove(dev, dev_ctx->context);
  132. dev_ctx->context = dev_ctx->intf->add(dev);
  133. spin_lock_irqsave(&priv->ctx_lock, flags);
  134. list_add_tail(&dev_ctx->list, &priv->ctx_list);
  135. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  136. mlx4_dbg(dev, "Inrerface for protocol %d restarted with when bonded mode is %s\n",
  137. dev_ctx->intf->protocol, enable ?
  138. "enabled" : "disabled");
  139. }
  140. return 0;
  141. }
  142. void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
  143. unsigned long param)
  144. {
  145. struct mlx4_priv *priv = mlx4_priv(dev);
  146. struct mlx4_device_context *dev_ctx;
  147. unsigned long flags;
  148. spin_lock_irqsave(&priv->ctx_lock, flags);
  149. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  150. if (dev_ctx->intf->event)
  151. dev_ctx->intf->event(dev, dev_ctx->context, type, param);
  152. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  153. }
  154. int mlx4_register_device(struct mlx4_dev *dev)
  155. {
  156. struct mlx4_priv *priv = mlx4_priv(dev);
  157. struct mlx4_interface *intf;
  158. mutex_lock(&intf_mutex);
  159. dev->persist->interface_state |= MLX4_INTERFACE_STATE_UP;
  160. list_add_tail(&priv->dev_list, &dev_list);
  161. list_for_each_entry(intf, &intf_list, list)
  162. mlx4_add_device(intf, priv);
  163. mutex_unlock(&intf_mutex);
  164. mlx4_start_catas_poll(dev);
  165. return 0;
  166. }
  167. void mlx4_unregister_device(struct mlx4_dev *dev)
  168. {
  169. struct mlx4_priv *priv = mlx4_priv(dev);
  170. struct mlx4_interface *intf;
  171. mlx4_stop_catas_poll(dev);
  172. mutex_lock(&intf_mutex);
  173. list_for_each_entry(intf, &intf_list, list)
  174. mlx4_remove_device(intf, priv);
  175. list_del(&priv->dev_list);
  176. dev->persist->interface_state &= ~MLX4_INTERFACE_STATE_UP;
  177. mutex_unlock(&intf_mutex);
  178. }
  179. void *mlx4_get_protocol_dev(struct mlx4_dev *dev, enum mlx4_protocol proto, int port)
  180. {
  181. struct mlx4_priv *priv = mlx4_priv(dev);
  182. struct mlx4_device_context *dev_ctx;
  183. unsigned long flags;
  184. void *result = NULL;
  185. spin_lock_irqsave(&priv->ctx_lock, flags);
  186. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  187. if (dev_ctx->intf->protocol == proto && dev_ctx->intf->get_dev) {
  188. result = dev_ctx->intf->get_dev(dev, dev_ctx->context, port);
  189. break;
  190. }
  191. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  192. return result;
  193. }
  194. EXPORT_SYMBOL_GPL(mlx4_get_protocol_dev);