sense.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. 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. */
  33. #include <linux/errno.h>
  34. #include <linux/if_ether.h>
  35. #include <linux/mlx4/cmd.h>
  36. #include "mlx4.h"
  37. int mlx4_SENSE_PORT(struct mlx4_dev *dev, int port,
  38. enum mlx4_port_type *type)
  39. {
  40. u64 out_param;
  41. int err = 0;
  42. err = mlx4_cmd_imm(dev, 0, &out_param, port, 0,
  43. MLX4_CMD_SENSE_PORT, MLX4_CMD_TIME_CLASS_B,
  44. MLX4_CMD_WRAPPED);
  45. if (err) {
  46. mlx4_err(dev, "Sense command failed for port: %d\n", port);
  47. return err;
  48. }
  49. if (out_param > 2) {
  50. mlx4_err(dev, "Sense returned illegal value: 0x%llx\n", out_param);
  51. return -EINVAL;
  52. }
  53. *type = out_param;
  54. return 0;
  55. }
  56. void mlx4_do_sense_ports(struct mlx4_dev *dev,
  57. enum mlx4_port_type *stype,
  58. enum mlx4_port_type *defaults)
  59. {
  60. struct mlx4_sense *sense = &mlx4_priv(dev)->sense;
  61. int err;
  62. int i;
  63. for (i = 1; i <= dev->caps.num_ports; i++) {
  64. stype[i - 1] = 0;
  65. if (sense->do_sense_port[i] && sense->sense_allowed[i] &&
  66. dev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
  67. err = mlx4_SENSE_PORT(dev, i, &stype[i - 1]);
  68. if (err)
  69. stype[i - 1] = defaults[i - 1];
  70. } else
  71. stype[i - 1] = defaults[i - 1];
  72. }
  73. /*
  74. * If sensed nothing, remain in current configuration.
  75. */
  76. for (i = 0; i < dev->caps.num_ports; i++)
  77. stype[i] = stype[i] ? stype[i] : defaults[i];
  78. }
  79. static void mlx4_sense_port(struct work_struct *work)
  80. {
  81. struct delayed_work *delay = to_delayed_work(work);
  82. struct mlx4_sense *sense = container_of(delay, struct mlx4_sense,
  83. sense_poll);
  84. struct mlx4_dev *dev = sense->dev;
  85. struct mlx4_priv *priv = mlx4_priv(dev);
  86. enum mlx4_port_type stype[MLX4_MAX_PORTS];
  87. mutex_lock(&priv->port_mutex);
  88. mlx4_do_sense_ports(dev, stype, &dev->caps.port_type[1]);
  89. if (mlx4_check_port_params(dev, stype))
  90. goto sense_again;
  91. if (mlx4_change_port_types(dev, stype))
  92. mlx4_err(dev, "Failed to change port_types\n");
  93. sense_again:
  94. mutex_unlock(&priv->port_mutex);
  95. queue_delayed_work(mlx4_wq , &sense->sense_poll,
  96. round_jiffies_relative(MLX4_SENSE_RANGE));
  97. }
  98. void mlx4_start_sense(struct mlx4_dev *dev)
  99. {
  100. struct mlx4_priv *priv = mlx4_priv(dev);
  101. struct mlx4_sense *sense = &priv->sense;
  102. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP))
  103. return;
  104. queue_delayed_work(mlx4_wq , &sense->sense_poll,
  105. round_jiffies_relative(MLX4_SENSE_RANGE));
  106. }
  107. void mlx4_stop_sense(struct mlx4_dev *dev)
  108. {
  109. cancel_delayed_work_sync(&mlx4_priv(dev)->sense.sense_poll);
  110. }
  111. void mlx4_sense_init(struct mlx4_dev *dev)
  112. {
  113. struct mlx4_priv *priv = mlx4_priv(dev);
  114. struct mlx4_sense *sense = &priv->sense;
  115. int port;
  116. sense->dev = dev;
  117. for (port = 1; port <= dev->caps.num_ports; port++)
  118. sense->do_sense_port[port] = 1;
  119. INIT_DEFERRABLE_WORK(&sense->sense_poll, mlx4_sense_port);
  120. }