intf.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "mlx4.h"
  36. struct mlx4_device_context {
  37. struct list_head list;
  38. struct mlx4_interface *intf;
  39. void *context;
  40. };
  41. static LIST_HEAD(intf_list);
  42. static LIST_HEAD(dev_list);
  43. static DEFINE_MUTEX(intf_mutex);
  44. static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
  45. {
  46. struct mlx4_device_context *dev_ctx;
  47. dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
  48. if (!dev_ctx)
  49. return;
  50. dev_ctx->intf = intf;
  51. dev_ctx->context = intf->add(&priv->dev);
  52. if (dev_ctx->context) {
  53. spin_lock_irq(&priv->ctx_lock);
  54. list_add_tail(&dev_ctx->list, &priv->ctx_list);
  55. spin_unlock_irq(&priv->ctx_lock);
  56. } else
  57. kfree(dev_ctx);
  58. }
  59. static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
  60. {
  61. struct mlx4_device_context *dev_ctx;
  62. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  63. if (dev_ctx->intf == intf) {
  64. spin_lock_irq(&priv->ctx_lock);
  65. list_del(&dev_ctx->list);
  66. spin_unlock_irq(&priv->ctx_lock);
  67. intf->remove(&priv->dev, dev_ctx->context);
  68. kfree(dev_ctx);
  69. return;
  70. }
  71. }
  72. int mlx4_register_interface(struct mlx4_interface *intf)
  73. {
  74. struct mlx4_priv *priv;
  75. if (!intf->add || !intf->remove)
  76. return -EINVAL;
  77. mutex_lock(&intf_mutex);
  78. list_add_tail(&intf->list, &intf_list);
  79. list_for_each_entry(priv, &dev_list, dev_list)
  80. mlx4_add_device(intf, priv);
  81. mutex_unlock(&intf_mutex);
  82. return 0;
  83. }
  84. EXPORT_SYMBOL_GPL(mlx4_register_interface);
  85. void mlx4_unregister_interface(struct mlx4_interface *intf)
  86. {
  87. struct mlx4_priv *priv;
  88. mutex_lock(&intf_mutex);
  89. list_for_each_entry(priv, &dev_list, dev_list)
  90. mlx4_remove_device(intf, priv);
  91. list_del(&intf->list);
  92. mutex_unlock(&intf_mutex);
  93. }
  94. EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
  95. void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
  96. unsigned long param)
  97. {
  98. struct mlx4_priv *priv = mlx4_priv(dev);
  99. struct mlx4_device_context *dev_ctx;
  100. unsigned long flags;
  101. spin_lock_irqsave(&priv->ctx_lock, flags);
  102. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  103. if (dev_ctx->intf->event)
  104. dev_ctx->intf->event(dev, dev_ctx->context, type, param);
  105. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  106. }
  107. int mlx4_register_device(struct mlx4_dev *dev)
  108. {
  109. struct mlx4_priv *priv = mlx4_priv(dev);
  110. struct mlx4_interface *intf;
  111. mutex_lock(&intf_mutex);
  112. list_add_tail(&priv->dev_list, &dev_list);
  113. list_for_each_entry(intf, &intf_list, list)
  114. mlx4_add_device(intf, priv);
  115. mutex_unlock(&intf_mutex);
  116. if (!mlx4_is_slave(dev))
  117. mlx4_start_catas_poll(dev);
  118. return 0;
  119. }
  120. void mlx4_unregister_device(struct mlx4_dev *dev)
  121. {
  122. struct mlx4_priv *priv = mlx4_priv(dev);
  123. struct mlx4_interface *intf;
  124. if (!mlx4_is_slave(dev))
  125. mlx4_stop_catas_poll(dev);
  126. mutex_lock(&intf_mutex);
  127. list_for_each_entry(intf, &intf_list, list)
  128. mlx4_remove_device(intf, priv);
  129. list_del(&priv->dev_list);
  130. mutex_unlock(&intf_mutex);
  131. }
  132. void *mlx4_get_protocol_dev(struct mlx4_dev *dev, enum mlx4_protocol proto, int port)
  133. {
  134. struct mlx4_priv *priv = mlx4_priv(dev);
  135. struct mlx4_device_context *dev_ctx;
  136. unsigned long flags;
  137. void *result = NULL;
  138. spin_lock_irqsave(&priv->ctx_lock, flags);
  139. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  140. if (dev_ctx->intf->protocol == proto && dev_ctx->intf->get_dev) {
  141. result = dev_ctx->intf->get_dev(dev, dev_ctx->context, port);
  142. break;
  143. }
  144. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  145. return result;
  146. }
  147. EXPORT_SYMBOL_GPL(mlx4_get_protocol_dev);