Skip to content

Commit b25bbc2

Browse files
alexveskerSaeed Mahameed
authored andcommitted
net/mlx5: Add Vendor Specific Capability access gateway
The Vendor Specific Capability (VSC) is used to activate a gateway interfacing with the device. The gateway is used to read or write device configurations, which are organized in different domains (spaces). A configuration access may result in multiple actions, reads, writes. Example usages are accessing the Crspace domain to read the crspace or locking a device semaphore using the Semaphore domain. The configuration access use pci_cfg_access to prevent parallel access to the VSC space by the driver and userspace calls. Signed-off-by: Alex Vesker <[email protected]> Signed-off-by: Feras Daoud <[email protected]> Signed-off-by: Saeed Mahameed <[email protected]>
1 parent 1f28d77 commit b25bbc2

File tree

5 files changed

+316
-1
lines changed

5 files changed

+316
-1
lines changed

drivers/net/ethernet/mellanox/mlx5/core/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ mlx5_core-y := main.o cmd.o debugfs.o fw.o eq.o uar.o pagealloc.o \
1515
health.o mcg.o cq.o alloc.o qp.o port.o mr.o pd.o \
1616
transobj.o vport.o sriov.o fs_cmd.o fs_core.o \
1717
fs_counters.o rl.o lag.o dev.o events.o wq.o lib/gid.o \
18-
lib/devcom.o diag/fs_tracepoint.o diag/fw_tracer.o devlink.o
18+
lib/devcom.o lib/pci_vsc.o diag/fs_tracepoint.o \
19+
diag/fw_tracer.o devlink.o
1920

2021
#
2122
# Netdev basic
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2+
/* Copyright (c) 2019 Mellanox Technologies */
3+
4+
#include <linux/pci.h>
5+
#include "mlx5_core.h"
6+
#include "pci_vsc.h"
7+
8+
#define MLX5_EXTRACT_C(source, offset, size) \
9+
((((u32)(source)) >> (offset)) & MLX5_ONES32(size))
10+
#define MLX5_EXTRACT(src, start, len) \
11+
(((len) == 32) ? (src) : MLX5_EXTRACT_C(src, start, len))
12+
#define MLX5_ONES32(size) \
13+
((size) ? (0xffffffff >> (32 - (size))) : 0)
14+
#define MLX5_MASK32(offset, size) \
15+
(MLX5_ONES32(size) << (offset))
16+
#define MLX5_MERGE_C(rsrc1, rsrc2, start, len) \
17+
((((rsrc2) << (start)) & (MLX5_MASK32((start), (len)))) | \
18+
((rsrc1) & (~MLX5_MASK32((start), (len)))))
19+
#define MLX5_MERGE(rsrc1, rsrc2, start, len) \
20+
(((len) == 32) ? (rsrc2) : MLX5_MERGE_C(rsrc1, rsrc2, start, len))
21+
#define vsc_read(dev, offset, val) \
22+
pci_read_config_dword((dev)->pdev, (dev)->vsc_addr + (offset), (val))
23+
#define vsc_write(dev, offset, val) \
24+
pci_write_config_dword((dev)->pdev, (dev)->vsc_addr + (offset), (val))
25+
#define VSC_MAX_RETRIES 2048
26+
27+
enum mlx5_vsc_state {
28+
MLX5_VSC_UNLOCK,
29+
MLX5_VSC_LOCK,
30+
};
31+
32+
enum {
33+
VSC_CTRL_OFFSET = 0x4,
34+
VSC_COUNTER_OFFSET = 0x8,
35+
VSC_SEMAPHORE_OFFSET = 0xc,
36+
VSC_ADDR_OFFSET = 0x10,
37+
VSC_DATA_OFFSET = 0x14,
38+
39+
VSC_FLAG_BIT_OFFS = 31,
40+
VSC_FLAG_BIT_LEN = 1,
41+
42+
VSC_SYND_BIT_OFFS = 30,
43+
VSC_SYND_BIT_LEN = 1,
44+
45+
VSC_ADDR_BIT_OFFS = 0,
46+
VSC_ADDR_BIT_LEN = 30,
47+
48+
VSC_SPACE_BIT_OFFS = 0,
49+
VSC_SPACE_BIT_LEN = 16,
50+
51+
VSC_SIZE_VLD_BIT_OFFS = 28,
52+
VSC_SIZE_VLD_BIT_LEN = 1,
53+
54+
VSC_STATUS_BIT_OFFS = 29,
55+
VSC_STATUS_BIT_LEN = 3,
56+
};
57+
58+
void mlx5_pci_vsc_init(struct mlx5_core_dev *dev)
59+
{
60+
if (!mlx5_core_is_pf(dev))
61+
return;
62+
63+
dev->vsc_addr = pci_find_capability(dev->pdev,
64+
PCI_CAP_ID_VNDR);
65+
if (!dev->vsc_addr)
66+
mlx5_core_warn(dev, "Failed to get valid vendor specific ID\n");
67+
}
68+
69+
int mlx5_vsc_gw_lock(struct mlx5_core_dev *dev)
70+
{
71+
u32 counter = 0;
72+
int retries = 0;
73+
u32 lock_val;
74+
int ret;
75+
76+
pci_cfg_access_lock(dev->pdev);
77+
do {
78+
if (retries > VSC_MAX_RETRIES) {
79+
ret = -EBUSY;
80+
goto pci_unlock;
81+
}
82+
83+
/* Check if semaphore is already locked */
84+
ret = vsc_read(dev, VSC_SEMAPHORE_OFFSET, &lock_val);
85+
if (ret)
86+
goto pci_unlock;
87+
88+
if (lock_val) {
89+
retries++;
90+
usleep_range(1000, 2000);
91+
continue;
92+
}
93+
94+
/* Read and write counter value, if written value is
95+
* the same, semaphore was acquired successfully.
96+
*/
97+
ret = vsc_read(dev, VSC_COUNTER_OFFSET, &counter);
98+
if (ret)
99+
goto pci_unlock;
100+
101+
ret = vsc_write(dev, VSC_SEMAPHORE_OFFSET, counter);
102+
if (ret)
103+
goto pci_unlock;
104+
105+
ret = vsc_read(dev, VSC_SEMAPHORE_OFFSET, &lock_val);
106+
if (ret)
107+
goto pci_unlock;
108+
109+
retries++;
110+
} while (counter != lock_val);
111+
112+
return 0;
113+
114+
pci_unlock:
115+
pci_cfg_access_unlock(dev->pdev);
116+
return ret;
117+
}
118+
119+
int mlx5_vsc_gw_unlock(struct mlx5_core_dev *dev)
120+
{
121+
int ret;
122+
123+
ret = vsc_write(dev, VSC_SEMAPHORE_OFFSET, MLX5_VSC_UNLOCK);
124+
pci_cfg_access_unlock(dev->pdev);
125+
return ret;
126+
}
127+
128+
int mlx5_vsc_gw_set_space(struct mlx5_core_dev *dev, u16 space,
129+
u32 *ret_space_size)
130+
{
131+
int ret;
132+
u32 val = 0;
133+
134+
if (!mlx5_vsc_accessible(dev))
135+
return -EINVAL;
136+
137+
if (ret_space_size)
138+
*ret_space_size = 0;
139+
140+
/* Get a unique val */
141+
ret = vsc_read(dev, VSC_CTRL_OFFSET, &val);
142+
if (ret)
143+
goto out;
144+
145+
/* Try to modify the lock */
146+
val = MLX5_MERGE(val, space, VSC_SPACE_BIT_OFFS, VSC_SPACE_BIT_LEN);
147+
ret = vsc_write(dev, VSC_CTRL_OFFSET, val);
148+
if (ret)
149+
goto out;
150+
151+
/* Verify lock was modified */
152+
ret = vsc_read(dev, VSC_CTRL_OFFSET, &val);
153+
if (ret)
154+
goto out;
155+
156+
if (MLX5_EXTRACT(val, VSC_STATUS_BIT_OFFS, VSC_STATUS_BIT_LEN) == 0)
157+
return -EINVAL;
158+
159+
/* Get space max address if indicated by size valid bit */
160+
if (ret_space_size &&
161+
MLX5_EXTRACT(val, VSC_SIZE_VLD_BIT_OFFS, VSC_SIZE_VLD_BIT_LEN)) {
162+
ret = vsc_read(dev, VSC_ADDR_OFFSET, &val);
163+
if (ret) {
164+
mlx5_core_warn(dev, "Failed to get max space size\n");
165+
goto out;
166+
}
167+
*ret_space_size = MLX5_EXTRACT(val, VSC_ADDR_BIT_OFFS,
168+
VSC_ADDR_BIT_LEN);
169+
}
170+
return 0;
171+
172+
out:
173+
return ret;
174+
}
175+
176+
static int mlx5_vsc_wait_on_flag(struct mlx5_core_dev *dev, u8 expected_val)
177+
{
178+
int retries = 0;
179+
u32 flag;
180+
int ret;
181+
182+
do {
183+
if (retries > VSC_MAX_RETRIES)
184+
return -EBUSY;
185+
186+
ret = vsc_read(dev, VSC_ADDR_OFFSET, &flag);
187+
if (ret)
188+
return ret;
189+
flag = MLX5_EXTRACT(flag, VSC_FLAG_BIT_OFFS, VSC_FLAG_BIT_LEN);
190+
retries++;
191+
192+
if ((retries & 0xf) == 0)
193+
usleep_range(1000, 2000);
194+
195+
} while (flag != expected_val);
196+
197+
return 0;
198+
}
199+
200+
static int mlx5_vsc_gw_write(struct mlx5_core_dev *dev, unsigned int address,
201+
u32 data)
202+
{
203+
int ret;
204+
205+
if (MLX5_EXTRACT(address, VSC_SYND_BIT_OFFS,
206+
VSC_FLAG_BIT_LEN + VSC_SYND_BIT_LEN))
207+
return -EINVAL;
208+
209+
/* Set flag to 0x1 */
210+
address = MLX5_MERGE(address, 1, VSC_FLAG_BIT_OFFS, 1);
211+
ret = vsc_write(dev, VSC_DATA_OFFSET, data);
212+
if (ret)
213+
goto out;
214+
215+
ret = vsc_write(dev, VSC_ADDR_OFFSET, address);
216+
if (ret)
217+
goto out;
218+
219+
/* Wait for the flag to be cleared */
220+
ret = mlx5_vsc_wait_on_flag(dev, 0);
221+
222+
out:
223+
return ret;
224+
}
225+
226+
static int mlx5_vsc_gw_read(struct mlx5_core_dev *dev, unsigned int address,
227+
u32 *data)
228+
{
229+
int ret;
230+
231+
if (MLX5_EXTRACT(address, VSC_SYND_BIT_OFFS,
232+
VSC_FLAG_BIT_LEN + VSC_SYND_BIT_LEN))
233+
return -EINVAL;
234+
235+
ret = vsc_write(dev, VSC_ADDR_OFFSET, address);
236+
if (ret)
237+
goto out;
238+
239+
ret = mlx5_vsc_wait_on_flag(dev, 1);
240+
if (ret)
241+
goto out;
242+
243+
ret = vsc_read(dev, VSC_DATA_OFFSET, data);
244+
out:
245+
return ret;
246+
}
247+
248+
static int mlx5_vsc_gw_read_fast(struct mlx5_core_dev *dev,
249+
unsigned int read_addr,
250+
unsigned int *next_read_addr,
251+
u32 *data)
252+
{
253+
int ret;
254+
255+
ret = mlx5_vsc_gw_read(dev, read_addr, data);
256+
if (ret)
257+
goto out;
258+
259+
ret = vsc_read(dev, VSC_ADDR_OFFSET, next_read_addr);
260+
if (ret)
261+
goto out;
262+
263+
*next_read_addr = MLX5_EXTRACT(*next_read_addr, VSC_ADDR_BIT_OFFS,
264+
VSC_ADDR_BIT_LEN);
265+
266+
if (*next_read_addr <= read_addr)
267+
ret = -EINVAL;
268+
out:
269+
return ret;
270+
}
271+
272+
int mlx5_vsc_gw_read_block_fast(struct mlx5_core_dev *dev, u32 *data,
273+
int length)
274+
{
275+
unsigned int next_read_addr = 0;
276+
unsigned int read_addr = 0;
277+
278+
while (read_addr < length) {
279+
if (mlx5_vsc_gw_read_fast(dev, read_addr, &next_read_addr,
280+
&data[(read_addr >> 2)]))
281+
return read_addr;
282+
283+
read_addr = next_read_addr;
284+
}
285+
return length;
286+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2+
/* Copyright (c) 2019 Mellanox Technologies */
3+
4+
#ifndef __MLX5_PCI_VSC_H__
5+
#define __MLX5_PCI_VSC_H__
6+
7+
enum {
8+
MLX5_VSC_SPACE_SCAN_CRSPACE = 0x7,
9+
};
10+
11+
void mlx5_pci_vsc_init(struct mlx5_core_dev *dev);
12+
int mlx5_vsc_gw_lock(struct mlx5_core_dev *dev);
13+
int mlx5_vsc_gw_unlock(struct mlx5_core_dev *dev);
14+
int mlx5_vsc_gw_set_space(struct mlx5_core_dev *dev, u16 space,
15+
u32 *ret_space_size);
16+
int mlx5_vsc_gw_read_block_fast(struct mlx5_core_dev *dev, u32 *data,
17+
int length);
18+
19+
static inline bool mlx5_vsc_accessible(struct mlx5_core_dev *dev)
20+
{
21+
return !!dev->vsc_addr;
22+
}
23+
24+
#endif /* __MLX5_PCI_VSC_H__ */

drivers/net/ethernet/mellanox/mlx5/core/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "lib/vxlan.h"
6767
#include "lib/geneve.h"
6868
#include "lib/devcom.h"
69+
#include "lib/pci_vsc.h"
6970
#include "diag/fw_tracer.h"
7071
#include "ecpf.h"
7172

@@ -763,6 +764,8 @@ static int mlx5_pci_init(struct mlx5_core_dev *dev, struct pci_dev *pdev,
763764
goto err_clr_master;
764765
}
765766

767+
mlx5_pci_vsc_init(dev);
768+
766769
return 0;
767770

768771
err_clr_master:

include/linux/mlx5/driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ struct mlx5_core_dev {
693693
struct mlx5_clock clock;
694694
struct mlx5_ib_clock_info *clock_info;
695695
struct mlx5_fw_tracer *tracer;
696+
u32 vsc_addr;
696697
};
697698

698699
struct mlx5_db {

0 commit comments

Comments
 (0)