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