1*e9dcd831SSlava Shwartsman /*-
2*e9dcd831SSlava Shwartsman * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
3*e9dcd831SSlava Shwartsman *
4*e9dcd831SSlava Shwartsman * This software is available to you under a choice of one of two
5*e9dcd831SSlava Shwartsman * licenses. You may choose to be licensed under the terms of the GNU
6*e9dcd831SSlava Shwartsman * General Public License (GPL) Version 2, available from the file
7*e9dcd831SSlava Shwartsman * COPYING in the main directory of this source tree, or the
8*e9dcd831SSlava Shwartsman * OpenIB.org BSD license below:
9*e9dcd831SSlava Shwartsman *
10*e9dcd831SSlava Shwartsman * Redistribution and use in source and binary forms, with or
11*e9dcd831SSlava Shwartsman * without modification, are permitted provided that the following
12*e9dcd831SSlava Shwartsman * conditions are met:
13*e9dcd831SSlava Shwartsman *
14*e9dcd831SSlava Shwartsman * - Redistributions of source code must retain the above
15*e9dcd831SSlava Shwartsman * copyright notice, this list of conditions and the following
16*e9dcd831SSlava Shwartsman * disclaimer.
17*e9dcd831SSlava Shwartsman *
18*e9dcd831SSlava Shwartsman * - Redistributions in binary form must reproduce the above
19*e9dcd831SSlava Shwartsman * copyright notice, this list of conditions and the following
20*e9dcd831SSlava Shwartsman * disclaimer in the documentation and/or other materials
21*e9dcd831SSlava Shwartsman * provided with the distribution.
22*e9dcd831SSlava Shwartsman *
23*e9dcd831SSlava Shwartsman * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*e9dcd831SSlava Shwartsman * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*e9dcd831SSlava Shwartsman * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*e9dcd831SSlava Shwartsman * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*e9dcd831SSlava Shwartsman * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*e9dcd831SSlava Shwartsman * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*e9dcd831SSlava Shwartsman * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*e9dcd831SSlava Shwartsman * SOFTWARE.
31*e9dcd831SSlava Shwartsman */
32*e9dcd831SSlava Shwartsman
33*e9dcd831SSlava Shwartsman #include <linux/etherdevice.h>
34*e9dcd831SSlava Shwartsman #include <dev/mlx5/driver.h>
35*e9dcd831SSlava Shwartsman #include <dev/mlx5/mlx5_core/mlx5_core.h>
36*e9dcd831SSlava Shwartsman #include <dev/mlx5/mlx5_lib/mlx5.h>
37*e9dcd831SSlava Shwartsman
mlx5_init_reserved_gids(struct mlx5_core_dev * dev)38*e9dcd831SSlava Shwartsman void mlx5_init_reserved_gids(struct mlx5_core_dev *dev)
39*e9dcd831SSlava Shwartsman {
40*e9dcd831SSlava Shwartsman unsigned int tblsz = MLX5_CAP_ROCE(dev, roce_address_table_size);
41*e9dcd831SSlava Shwartsman
42*e9dcd831SSlava Shwartsman ida_init(&dev->roce.reserved_gids.ida);
43*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.start = tblsz;
44*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.count = 0;
45*e9dcd831SSlava Shwartsman }
46*e9dcd831SSlava Shwartsman
mlx5_cleanup_reserved_gids(struct mlx5_core_dev * dev)47*e9dcd831SSlava Shwartsman void mlx5_cleanup_reserved_gids(struct mlx5_core_dev *dev)
48*e9dcd831SSlava Shwartsman {
49*e9dcd831SSlava Shwartsman WARN_ON(!ida_is_empty(&dev->roce.reserved_gids.ida));
50*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.start = 0;
51*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.count = 0;
52*e9dcd831SSlava Shwartsman ida_destroy(&dev->roce.reserved_gids.ida);
53*e9dcd831SSlava Shwartsman }
54*e9dcd831SSlava Shwartsman
mlx5_core_reserve_gids(struct mlx5_core_dev * dev,unsigned int count)55*e9dcd831SSlava Shwartsman int mlx5_core_reserve_gids(struct mlx5_core_dev *dev, unsigned int count)
56*e9dcd831SSlava Shwartsman {
57*e9dcd831SSlava Shwartsman if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
58*e9dcd831SSlava Shwartsman mlx5_core_err(dev, "Cannot reserve GIDs when interfaces are up\n");
59*e9dcd831SSlava Shwartsman return -EPERM;
60*e9dcd831SSlava Shwartsman }
61*e9dcd831SSlava Shwartsman if (dev->roce.reserved_gids.start < count) {
62*e9dcd831SSlava Shwartsman mlx5_core_warn(dev, "GID table exhausted attempting to reserve %d more GIDs\n",
63*e9dcd831SSlava Shwartsman count);
64*e9dcd831SSlava Shwartsman return -ENOMEM;
65*e9dcd831SSlava Shwartsman }
66*e9dcd831SSlava Shwartsman if (dev->roce.reserved_gids.count + count > MLX5_MAX_RESERVED_GIDS) {
67*e9dcd831SSlava Shwartsman mlx5_core_warn(dev, "Unable to reserve %d more GIDs\n", count);
68*e9dcd831SSlava Shwartsman return -ENOMEM;
69*e9dcd831SSlava Shwartsman }
70*e9dcd831SSlava Shwartsman
71*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.start -= count;
72*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.count += count;
73*e9dcd831SSlava Shwartsman mlx5_core_dbg(dev, "Reserved %u GIDs starting at %u\n",
74*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.count,
75*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.start);
76*e9dcd831SSlava Shwartsman return 0;
77*e9dcd831SSlava Shwartsman }
78*e9dcd831SSlava Shwartsman
mlx5_core_unreserve_gids(struct mlx5_core_dev * dev,unsigned int count)79*e9dcd831SSlava Shwartsman void mlx5_core_unreserve_gids(struct mlx5_core_dev *dev, unsigned int count)
80*e9dcd831SSlava Shwartsman {
81*e9dcd831SSlava Shwartsman WARN(test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state), "Unreserving GIDs when interfaces are up");
82*e9dcd831SSlava Shwartsman WARN(count > dev->roce.reserved_gids.count, "Unreserving %u GIDs when only %u reserved",
83*e9dcd831SSlava Shwartsman count, dev->roce.reserved_gids.count);
84*e9dcd831SSlava Shwartsman
85*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.start += count;
86*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.count -= count;
87*e9dcd831SSlava Shwartsman mlx5_core_dbg(dev, "%u GIDs starting at %u left reserved\n",
88*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.count,
89*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.start);
90*e9dcd831SSlava Shwartsman }
91*e9dcd831SSlava Shwartsman
mlx5_core_reserved_gid_alloc(struct mlx5_core_dev * dev,int * gid_index)92*e9dcd831SSlava Shwartsman int mlx5_core_reserved_gid_alloc(struct mlx5_core_dev *dev, int *gid_index)
93*e9dcd831SSlava Shwartsman {
94*e9dcd831SSlava Shwartsman int end = dev->roce.reserved_gids.start +
95*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.count;
96*e9dcd831SSlava Shwartsman int index = 0;
97*e9dcd831SSlava Shwartsman
98*e9dcd831SSlava Shwartsman index = ida_simple_get(&dev->roce.reserved_gids.ida,
99*e9dcd831SSlava Shwartsman dev->roce.reserved_gids.start, end,
100*e9dcd831SSlava Shwartsman GFP_KERNEL);
101*e9dcd831SSlava Shwartsman if (index < 0)
102*e9dcd831SSlava Shwartsman return index;
103*e9dcd831SSlava Shwartsman
104*e9dcd831SSlava Shwartsman mlx5_core_dbg(dev, "Allocating reserved GID %u\n", index);
105*e9dcd831SSlava Shwartsman *gid_index = index;
106*e9dcd831SSlava Shwartsman return 0;
107*e9dcd831SSlava Shwartsman }
108*e9dcd831SSlava Shwartsman
mlx5_core_reserved_gid_free(struct mlx5_core_dev * dev,int gid_index)109*e9dcd831SSlava Shwartsman void mlx5_core_reserved_gid_free(struct mlx5_core_dev *dev, int gid_index)
110*e9dcd831SSlava Shwartsman {
111*e9dcd831SSlava Shwartsman mlx5_core_dbg(dev, "Freeing reserved GID %u\n", gid_index);
112*e9dcd831SSlava Shwartsman ida_simple_remove(&dev->roce.reserved_gids.ida, gid_index);
113*e9dcd831SSlava Shwartsman }
114*e9dcd831SSlava Shwartsman
mlx5_core_reserved_gids_count(struct mlx5_core_dev * dev)115*e9dcd831SSlava Shwartsman unsigned int mlx5_core_reserved_gids_count(struct mlx5_core_dev *dev)
116*e9dcd831SSlava Shwartsman {
117*e9dcd831SSlava Shwartsman return dev->roce.reserved_gids.count;
118*e9dcd831SSlava Shwartsman }
119*e9dcd831SSlava Shwartsman EXPORT_SYMBOL_GPL(mlx5_core_reserved_gids_count);
120*e9dcd831SSlava Shwartsman
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)121*e9dcd831SSlava Shwartsman int mlx5_core_roce_gid_set(struct mlx5_core_dev *dev, unsigned int index,
122*e9dcd831SSlava Shwartsman u8 roce_version, u8 roce_l3_type, const u8 *gid,
123*e9dcd831SSlava Shwartsman const u8 *mac, bool vlan, u16 vlan_id)
124*e9dcd831SSlava Shwartsman {
125*e9dcd831SSlava Shwartsman #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
126*e9dcd831SSlava Shwartsman u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0};
127*e9dcd831SSlava Shwartsman u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
128*e9dcd831SSlava Shwartsman void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
129*e9dcd831SSlava Shwartsman char *addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, in_addr,
130*e9dcd831SSlava Shwartsman source_l3_address);
131*e9dcd831SSlava Shwartsman void *addr_mac = MLX5_ADDR_OF(roce_addr_layout, in_addr,
132*e9dcd831SSlava Shwartsman source_mac_47_32);
133*e9dcd831SSlava Shwartsman int gidsz = MLX5_FLD_SZ_BYTES(roce_addr_layout, source_l3_address);
134*e9dcd831SSlava Shwartsman
135*e9dcd831SSlava Shwartsman if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
136*e9dcd831SSlava Shwartsman return -EINVAL;
137*e9dcd831SSlava Shwartsman
138*e9dcd831SSlava Shwartsman if (gid) {
139*e9dcd831SSlava Shwartsman if (vlan) {
140*e9dcd831SSlava Shwartsman MLX5_SET_RA(in_addr, vlan_valid, 1);
141*e9dcd831SSlava Shwartsman MLX5_SET_RA(in_addr, vlan_id, vlan_id);
142*e9dcd831SSlava Shwartsman }
143*e9dcd831SSlava Shwartsman
144*e9dcd831SSlava Shwartsman ether_addr_copy(addr_mac, mac);
145*e9dcd831SSlava Shwartsman MLX5_SET_RA(in_addr, roce_version, roce_version);
146*e9dcd831SSlava Shwartsman MLX5_SET_RA(in_addr, roce_l3_type, roce_l3_type);
147*e9dcd831SSlava Shwartsman memcpy(addr_l3_addr, gid, gidsz);
148*e9dcd831SSlava Shwartsman }
149*e9dcd831SSlava Shwartsman
150*e9dcd831SSlava Shwartsman MLX5_SET(set_roce_address_in, in, roce_address_index, index);
151*e9dcd831SSlava Shwartsman MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
152*e9dcd831SSlava Shwartsman return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
153*e9dcd831SSlava Shwartsman }
154*e9dcd831SSlava Shwartsman EXPORT_SYMBOL(mlx5_core_roce_gid_set);
155