intf.c 4.2 KB

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