152ec462eSIlan Tayari /*
252ec462eSIlan Tayari  * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
352ec462eSIlan Tayari  *
452ec462eSIlan Tayari  * This software is available to you under a choice of one of two
552ec462eSIlan Tayari  * licenses.  You may choose to be licensed under the terms of the GNU
652ec462eSIlan Tayari  * General Public License (GPL) Version 2, available from the file
752ec462eSIlan Tayari  * COPYING in the main directory of this source tree, or the
852ec462eSIlan Tayari  * OpenIB.org BSD license below:
952ec462eSIlan Tayari  *
1052ec462eSIlan Tayari  *     Redistribution and use in source and binary forms, with or
1152ec462eSIlan Tayari  *     without modification, are permitted provided that the following
1252ec462eSIlan Tayari  *     conditions are met:
1352ec462eSIlan Tayari  *
1452ec462eSIlan Tayari  *      - Redistributions of source code must retain the above
1552ec462eSIlan Tayari  *        copyright notice, this list of conditions and the following
1652ec462eSIlan Tayari  *        disclaimer.
1752ec462eSIlan Tayari  *
1852ec462eSIlan Tayari  *      - Redistributions in binary form must reproduce the above
1952ec462eSIlan Tayari  *        copyright notice, this list of conditions and the following
2052ec462eSIlan Tayari  *        disclaimer in the documentation and/or other materials
2152ec462eSIlan Tayari  *        provided with the distribution.
2252ec462eSIlan Tayari  *
2352ec462eSIlan Tayari  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2452ec462eSIlan Tayari  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2552ec462eSIlan Tayari  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2652ec462eSIlan Tayari  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2752ec462eSIlan Tayari  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2852ec462eSIlan Tayari  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2952ec462eSIlan Tayari  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3052ec462eSIlan Tayari  * SOFTWARE.
3152ec462eSIlan Tayari  */
3252ec462eSIlan Tayari 
3352ec462eSIlan Tayari #include <linux/mlx5/driver.h>
3452ec462eSIlan Tayari #include <linux/etherdevice.h>
3552ec462eSIlan Tayari #include <linux/idr.h>
3652ec462eSIlan Tayari #include "mlx5_core.h"
37aa07b633SIlan Tayari #include "lib/mlx5.h"
3852ec462eSIlan Tayari 
mlx5_init_reserved_gids(struct mlx5_core_dev * dev)3952ec462eSIlan Tayari void mlx5_init_reserved_gids(struct mlx5_core_dev *dev)
4052ec462eSIlan Tayari {
4152ec462eSIlan Tayari 	unsigned int tblsz = MLX5_CAP_ROCE(dev, roce_address_table_size);
4252ec462eSIlan Tayari 
4352ec462eSIlan Tayari 	ida_init(&dev->roce.reserved_gids.ida);
4452ec462eSIlan Tayari 	dev->roce.reserved_gids.start = tblsz;
4552ec462eSIlan Tayari 	dev->roce.reserved_gids.count = 0;
4652ec462eSIlan Tayari }
4752ec462eSIlan Tayari 
mlx5_cleanup_reserved_gids(struct mlx5_core_dev * dev)4852ec462eSIlan Tayari void mlx5_cleanup_reserved_gids(struct mlx5_core_dev *dev)
4952ec462eSIlan Tayari {
5052ec462eSIlan Tayari 	WARN_ON(!ida_is_empty(&dev->roce.reserved_gids.ida));
5152ec462eSIlan Tayari 	dev->roce.reserved_gids.start = 0;
5252ec462eSIlan Tayari 	dev->roce.reserved_gids.count = 0;
5352ec462eSIlan Tayari 	ida_destroy(&dev->roce.reserved_gids.ida);
5452ec462eSIlan Tayari }
5552ec462eSIlan Tayari 
mlx5_core_reserve_gids(struct mlx5_core_dev * dev,unsigned int count)5652ec462eSIlan Tayari int mlx5_core_reserve_gids(struct mlx5_core_dev *dev, unsigned int count)
5752ec462eSIlan Tayari {
5852ec462eSIlan Tayari 	if (dev->roce.reserved_gids.start < count) {
5952ec462eSIlan Tayari 		mlx5_core_warn(dev, "GID table exhausted attempting to reserve %d more GIDs\n",
6052ec462eSIlan Tayari 			       count);
6152ec462eSIlan Tayari 		return -ENOMEM;
6252ec462eSIlan Tayari 	}
6352ec462eSIlan Tayari 	if (dev->roce.reserved_gids.count + count > MLX5_MAX_RESERVED_GIDS) {
6452ec462eSIlan Tayari 		mlx5_core_warn(dev, "Unable to reserve %d more GIDs\n", count);
6552ec462eSIlan Tayari 		return -ENOMEM;
6652ec462eSIlan Tayari 	}
6752ec462eSIlan Tayari 
6852ec462eSIlan Tayari 	dev->roce.reserved_gids.start -= count;
6952ec462eSIlan Tayari 	dev->roce.reserved_gids.count += count;
7052ec462eSIlan Tayari 	mlx5_core_dbg(dev, "Reserved %u GIDs starting at %u\n",
7152ec462eSIlan Tayari 		      dev->roce.reserved_gids.count,
7252ec462eSIlan Tayari 		      dev->roce.reserved_gids.start);
7352ec462eSIlan Tayari 	return 0;
7452ec462eSIlan Tayari }
7552ec462eSIlan Tayari 
mlx5_core_unreserve_gids(struct mlx5_core_dev * dev,unsigned int count)7652ec462eSIlan Tayari void mlx5_core_unreserve_gids(struct mlx5_core_dev *dev, unsigned int count)
7752ec462eSIlan Tayari {
7852ec462eSIlan Tayari 	WARN(count > dev->roce.reserved_gids.count, "Unreserving %u GIDs when only %u reserved",
7952ec462eSIlan Tayari 	     count, dev->roce.reserved_gids.count);
8052ec462eSIlan Tayari 
8152ec462eSIlan Tayari 	dev->roce.reserved_gids.start += count;
8252ec462eSIlan Tayari 	dev->roce.reserved_gids.count -= count;
8352ec462eSIlan Tayari 	mlx5_core_dbg(dev, "%u GIDs starting at %u left reserved\n",
8452ec462eSIlan Tayari 		      dev->roce.reserved_gids.count,
8552ec462eSIlan Tayari 		      dev->roce.reserved_gids.start);
8652ec462eSIlan Tayari }
8752ec462eSIlan Tayari 
mlx5_core_reserved_gid_alloc(struct mlx5_core_dev * dev,int * gid_index)8852ec462eSIlan Tayari int mlx5_core_reserved_gid_alloc(struct mlx5_core_dev *dev, int *gid_index)
8952ec462eSIlan Tayari {
9052ec462eSIlan Tayari 	int end = dev->roce.reserved_gids.start +
9152ec462eSIlan Tayari 		  dev->roce.reserved_gids.count - 1;
9252ec462eSIlan Tayari 	int index = 0;
9352ec462eSIlan Tayari 
9452ec462eSIlan Tayari 	index = ida_alloc_range(&dev->roce.reserved_gids.ida,
9552ec462eSIlan Tayari 				dev->roce.reserved_gids.start, end,
9652ec462eSIlan Tayari 				GFP_KERNEL);
9752ec462eSIlan Tayari 	if (index < 0)
9852ec462eSIlan Tayari 		return index;
9952ec462eSIlan Tayari 
10052ec462eSIlan Tayari 	mlx5_core_dbg(dev, "Allocating reserved GID %u\n", index);
10152ec462eSIlan Tayari 	*gid_index = index;
10252ec462eSIlan Tayari 	return 0;
10352ec462eSIlan Tayari }
10452ec462eSIlan Tayari 
mlx5_core_reserved_gid_free(struct mlx5_core_dev * dev,int gid_index)1054120dab0SColin Ian King void mlx5_core_reserved_gid_free(struct mlx5_core_dev *dev, int gid_index)
10652ec462eSIlan Tayari {
10752ec462eSIlan Tayari 	mlx5_core_dbg(dev, "Freeing reserved GID %u\n", gid_index);
10852ec462eSIlan Tayari 	ida_free(&dev->roce.reserved_gids.ida, gid_index);
10952ec462eSIlan Tayari }
11052ec462eSIlan Tayari 
mlx5_core_reserved_gids_count(struct mlx5_core_dev * dev)11152ec462eSIlan Tayari unsigned int mlx5_core_reserved_gids_count(struct mlx5_core_dev *dev)
11252ec462eSIlan Tayari {
11352ec462eSIlan Tayari 	return dev->roce.reserved_gids.count;
11452ec462eSIlan Tayari }
11552ec462eSIlan Tayari EXPORT_SYMBOL_GPL(mlx5_core_reserved_gids_count);
11652ec462eSIlan Tayari 
mlx5_core_roce_gid_set(struct mlx5_core_dev * dev,unsigned int index,u8 roce_version,u8 roce_l3_type,const u8 * gid,const u8 * mac,bool vlan,u16 vlan_id,u8 port_num)11752ec462eSIlan Tayari int mlx5_core_roce_gid_set(struct mlx5_core_dev *dev, unsigned int index,
11852ec462eSIlan Tayari 			   u8 roce_version, u8 roce_l3_type, const u8 *gid,
11952ec462eSIlan Tayari 			   const u8 *mac, bool vlan, u16 vlan_id, u8 port_num)
12052ec462eSIlan Tayari {
12152ec462eSIlan Tayari #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
12252ec462eSIlan Tayari 	u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {};
12352ec462eSIlan Tayari 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
124cfe4e37fSDaniel Jurgens 	char *addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, in_addr,
12552ec462eSIlan Tayari 					  source_l3_address);
12652ec462eSIlan Tayari 	void *addr_mac = MLX5_ADDR_OF(roce_addr_layout, in_addr,
127bb7664d3SLeon Romanovsky 				      source_mac_47_32);
12852ec462eSIlan Tayari 	int gidsz = MLX5_FLD_SZ_BYTES(roce_addr_layout, source_l3_address);
12952ec462eSIlan Tayari 
13052ec462eSIlan Tayari 	if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
13152ec462eSIlan Tayari 		return -EINVAL;
13252ec462eSIlan Tayari 
13352ec462eSIlan Tayari 	if (gid) {
13452ec462eSIlan Tayari 		if (vlan) {
13552ec462eSIlan Tayari 			MLX5_SET_RA(in_addr, vlan_valid, 1);
13652ec462eSIlan Tayari 			MLX5_SET_RA(in_addr, vlan_id, vlan_id);
13752ec462eSIlan Tayari 		}
13852ec462eSIlan Tayari 
13952ec462eSIlan Tayari 		ether_addr_copy(addr_mac, mac);
14052ec462eSIlan Tayari 		memcpy(addr_l3_addr, gid, gidsz);
14152ec462eSIlan Tayari 	}
14252ec462eSIlan Tayari 	MLX5_SET_RA(in_addr, roce_version, roce_version);
14352ec462eSIlan Tayari 	MLX5_SET_RA(in_addr, roce_l3_type, roce_l3_type);
14452ec462eSIlan Tayari 
14552ec462eSIlan Tayari 	if (MLX5_CAP_GEN(dev, num_vhca_ports) > 0)
14652ec462eSIlan Tayari 		MLX5_SET(set_roce_address_in, in, vhca_port_num, port_num);
147*dedbc2d3SParav Pandit 
148*dedbc2d3SParav Pandit 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
14952ec462eSIlan Tayari 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
150cfe4e37fSDaniel Jurgens 	return mlx5_cmd_exec_in(dev, set_roce_address, in);
151cfe4e37fSDaniel Jurgens }
152cfe4e37fSDaniel Jurgens EXPORT_SYMBOL(mlx5_core_roce_gid_set);
15352ec462eSIlan Tayari