xref: /linux/drivers/net/ethernet/mellanox/mlx4/sense.c (revision 5a2cc190eb3fe58fe519795c509b01b25795992e)
1*5a2cc190SJeff Kirsher /*
2*5a2cc190SJeff Kirsher  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3*5a2cc190SJeff Kirsher  *
4*5a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
5*5a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
6*5a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
7*5a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
8*5a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
9*5a2cc190SJeff Kirsher  *
10*5a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
11*5a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
12*5a2cc190SJeff Kirsher  *     conditions are met:
13*5a2cc190SJeff Kirsher  *
14*5a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
15*5a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
16*5a2cc190SJeff Kirsher  *        disclaimer.
17*5a2cc190SJeff Kirsher  *
18*5a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
19*5a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
20*5a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
21*5a2cc190SJeff Kirsher  *        provided with the distribution.
22*5a2cc190SJeff Kirsher  *
23*5a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*5a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*5a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*5a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*5a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*5a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*5a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*5a2cc190SJeff Kirsher  * SOFTWARE.
31*5a2cc190SJeff Kirsher  *
32*5a2cc190SJeff Kirsher  */
33*5a2cc190SJeff Kirsher 
34*5a2cc190SJeff Kirsher #include <linux/errno.h>
35*5a2cc190SJeff Kirsher #include <linux/if_ether.h>
36*5a2cc190SJeff Kirsher 
37*5a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h>
38*5a2cc190SJeff Kirsher 
39*5a2cc190SJeff Kirsher #include "mlx4.h"
40*5a2cc190SJeff Kirsher 
41*5a2cc190SJeff Kirsher int mlx4_SENSE_PORT(struct mlx4_dev *dev, int port,
42*5a2cc190SJeff Kirsher 		    enum mlx4_port_type *type)
43*5a2cc190SJeff Kirsher {
44*5a2cc190SJeff Kirsher 	u64 out_param;
45*5a2cc190SJeff Kirsher 	int err = 0;
46*5a2cc190SJeff Kirsher 
47*5a2cc190SJeff Kirsher 	err = mlx4_cmd_imm(dev, 0, &out_param, port, 0,
48*5a2cc190SJeff Kirsher 			   MLX4_CMD_SENSE_PORT, MLX4_CMD_TIME_CLASS_B);
49*5a2cc190SJeff Kirsher 	if (err) {
50*5a2cc190SJeff Kirsher 		mlx4_err(dev, "Sense command failed for port: %d\n", port);
51*5a2cc190SJeff Kirsher 		return err;
52*5a2cc190SJeff Kirsher 	}
53*5a2cc190SJeff Kirsher 
54*5a2cc190SJeff Kirsher 	if (out_param > 2) {
55*5a2cc190SJeff Kirsher 		mlx4_err(dev, "Sense returned illegal value: 0x%llx\n", out_param);
56*5a2cc190SJeff Kirsher 		return -EINVAL;
57*5a2cc190SJeff Kirsher 	}
58*5a2cc190SJeff Kirsher 
59*5a2cc190SJeff Kirsher 	*type = out_param;
60*5a2cc190SJeff Kirsher 	return 0;
61*5a2cc190SJeff Kirsher }
62*5a2cc190SJeff Kirsher 
63*5a2cc190SJeff Kirsher void mlx4_do_sense_ports(struct mlx4_dev *dev,
64*5a2cc190SJeff Kirsher 			 enum mlx4_port_type *stype,
65*5a2cc190SJeff Kirsher 			 enum mlx4_port_type *defaults)
66*5a2cc190SJeff Kirsher {
67*5a2cc190SJeff Kirsher 	struct mlx4_sense *sense = &mlx4_priv(dev)->sense;
68*5a2cc190SJeff Kirsher 	int err;
69*5a2cc190SJeff Kirsher 	int i;
70*5a2cc190SJeff Kirsher 
71*5a2cc190SJeff Kirsher 	for (i = 1; i <= dev->caps.num_ports; i++) {
72*5a2cc190SJeff Kirsher 		stype[i - 1] = 0;
73*5a2cc190SJeff Kirsher 		if (sense->do_sense_port[i] && sense->sense_allowed[i] &&
74*5a2cc190SJeff Kirsher 		    dev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
75*5a2cc190SJeff Kirsher 			err = mlx4_SENSE_PORT(dev, i, &stype[i - 1]);
76*5a2cc190SJeff Kirsher 			if (err)
77*5a2cc190SJeff Kirsher 				stype[i - 1] = defaults[i - 1];
78*5a2cc190SJeff Kirsher 		} else
79*5a2cc190SJeff Kirsher 			stype[i - 1] = defaults[i - 1];
80*5a2cc190SJeff Kirsher 	}
81*5a2cc190SJeff Kirsher 
82*5a2cc190SJeff Kirsher 	/*
83*5a2cc190SJeff Kirsher 	 * Adjust port configuration:
84*5a2cc190SJeff Kirsher 	 * If port 1 sensed nothing and port 2 is IB, set both as IB
85*5a2cc190SJeff Kirsher 	 * If port 2 sensed nothing and port 1 is Eth, set both as Eth
86*5a2cc190SJeff Kirsher 	 */
87*5a2cc190SJeff Kirsher 	if (stype[0] == MLX4_PORT_TYPE_ETH) {
88*5a2cc190SJeff Kirsher 		for (i = 1; i < dev->caps.num_ports; i++)
89*5a2cc190SJeff Kirsher 			stype[i] = stype[i] ? stype[i] : MLX4_PORT_TYPE_ETH;
90*5a2cc190SJeff Kirsher 	}
91*5a2cc190SJeff Kirsher 	if (stype[dev->caps.num_ports - 1] == MLX4_PORT_TYPE_IB) {
92*5a2cc190SJeff Kirsher 		for (i = 0; i < dev->caps.num_ports - 1; i++)
93*5a2cc190SJeff Kirsher 			stype[i] = stype[i] ? stype[i] : MLX4_PORT_TYPE_IB;
94*5a2cc190SJeff Kirsher 	}
95*5a2cc190SJeff Kirsher 
96*5a2cc190SJeff Kirsher 	/*
97*5a2cc190SJeff Kirsher 	 * If sensed nothing, remain in current configuration.
98*5a2cc190SJeff Kirsher 	 */
99*5a2cc190SJeff Kirsher 	for (i = 0; i < dev->caps.num_ports; i++)
100*5a2cc190SJeff Kirsher 		stype[i] = stype[i] ? stype[i] : defaults[i];
101*5a2cc190SJeff Kirsher 
102*5a2cc190SJeff Kirsher }
103*5a2cc190SJeff Kirsher 
104*5a2cc190SJeff Kirsher static void mlx4_sense_port(struct work_struct *work)
105*5a2cc190SJeff Kirsher {
106*5a2cc190SJeff Kirsher 	struct delayed_work *delay = to_delayed_work(work);
107*5a2cc190SJeff Kirsher 	struct mlx4_sense *sense = container_of(delay, struct mlx4_sense,
108*5a2cc190SJeff Kirsher 						sense_poll);
109*5a2cc190SJeff Kirsher 	struct mlx4_dev *dev = sense->dev;
110*5a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
111*5a2cc190SJeff Kirsher 	enum mlx4_port_type stype[MLX4_MAX_PORTS];
112*5a2cc190SJeff Kirsher 
113*5a2cc190SJeff Kirsher 	mutex_lock(&priv->port_mutex);
114*5a2cc190SJeff Kirsher 	mlx4_do_sense_ports(dev, stype, &dev->caps.port_type[1]);
115*5a2cc190SJeff Kirsher 
116*5a2cc190SJeff Kirsher 	if (mlx4_check_port_params(dev, stype))
117*5a2cc190SJeff Kirsher 		goto sense_again;
118*5a2cc190SJeff Kirsher 
119*5a2cc190SJeff Kirsher 	if (mlx4_change_port_types(dev, stype))
120*5a2cc190SJeff Kirsher 		mlx4_err(dev, "Failed to change port_types\n");
121*5a2cc190SJeff Kirsher 
122*5a2cc190SJeff Kirsher sense_again:
123*5a2cc190SJeff Kirsher 	mutex_unlock(&priv->port_mutex);
124*5a2cc190SJeff Kirsher 	queue_delayed_work(mlx4_wq , &sense->sense_poll,
125*5a2cc190SJeff Kirsher 			   round_jiffies_relative(MLX4_SENSE_RANGE));
126*5a2cc190SJeff Kirsher }
127*5a2cc190SJeff Kirsher 
128*5a2cc190SJeff Kirsher void mlx4_start_sense(struct mlx4_dev *dev)
129*5a2cc190SJeff Kirsher {
130*5a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
131*5a2cc190SJeff Kirsher 	struct mlx4_sense *sense = &priv->sense;
132*5a2cc190SJeff Kirsher 
133*5a2cc190SJeff Kirsher 	if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP))
134*5a2cc190SJeff Kirsher 		return;
135*5a2cc190SJeff Kirsher 
136*5a2cc190SJeff Kirsher 	queue_delayed_work(mlx4_wq , &sense->sense_poll,
137*5a2cc190SJeff Kirsher 			   round_jiffies_relative(MLX4_SENSE_RANGE));
138*5a2cc190SJeff Kirsher }
139*5a2cc190SJeff Kirsher 
140*5a2cc190SJeff Kirsher void mlx4_stop_sense(struct mlx4_dev *dev)
141*5a2cc190SJeff Kirsher {
142*5a2cc190SJeff Kirsher 	cancel_delayed_work_sync(&mlx4_priv(dev)->sense.sense_poll);
143*5a2cc190SJeff Kirsher }
144*5a2cc190SJeff Kirsher 
145*5a2cc190SJeff Kirsher void  mlx4_sense_init(struct mlx4_dev *dev)
146*5a2cc190SJeff Kirsher {
147*5a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
148*5a2cc190SJeff Kirsher 	struct mlx4_sense *sense = &priv->sense;
149*5a2cc190SJeff Kirsher 	int port;
150*5a2cc190SJeff Kirsher 
151*5a2cc190SJeff Kirsher 	sense->dev = dev;
152*5a2cc190SJeff Kirsher 	for (port = 1; port <= dev->caps.num_ports; port++)
153*5a2cc190SJeff Kirsher 		sense->do_sense_port[port] = 1;
154*5a2cc190SJeff Kirsher 
155*5a2cc190SJeff Kirsher 	INIT_DELAYED_WORK_DEFERRABLE(&sense->sense_poll, mlx4_sense_port);
156*5a2cc190SJeff Kirsher }
157