summaryrefslogtreecommitdiffstats
path: root/drivers/virtio/virtio_pci_admin_legacy_io.c
blob: 819cfbbc67c3bd0cf8e4c6d61828d3fb8deb7193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
 */

#include <linux/virtio_pci_admin.h>
#include "virtio_pci_common.h"

/*
 * virtio_pci_admin_has_legacy_io - Checks whether the legacy IO
 * commands are supported
 * @dev: VF pci_dev
 *
 * Returns true on success.
 */
bool virtio_pci_admin_has_legacy_io(struct pci_dev *pdev)
{
	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
	struct virtio_pci_device *vp_dev;

	if (!virtio_dev)
		return false;

	if (!virtio_has_feature(virtio_dev, VIRTIO_F_ADMIN_VQ))
		return false;

	vp_dev = to_vp_device(virtio_dev);

	if ((vp_dev->admin_vq.supported_cmds & VIRTIO_LEGACY_ADMIN_CMD_BITMAP) ==
		VIRTIO_LEGACY_ADMIN_CMD_BITMAP)
		return true;
	return false;
}
EXPORT_SYMBOL_GPL(virtio_pci_admin_has_legacy_io);

static int virtio_pci_admin_legacy_io_write(struct pci_dev *pdev, u16 opcode,
					    u8 offset, u8 size, u8 *buf)
{
	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
	struct virtio_admin_cmd_legacy_wr_data *data;
	struct virtio_admin_cmd cmd = {};
	struct scatterlist data_sg;
	int vf_id;
	int ret;

	if (!virtio_dev)
		return -ENODEV;

	vf_id = pci_iov_vf_id(pdev);
	if (vf_id < 0)
		return vf_id;

	data = kzalloc(sizeof(*data) + size, GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	data->offset = offset;
	memcpy(data->registers, buf, size);
	sg_init_one(&data_sg, data, sizeof(*data) + size);
	cmd.opcode = cpu_to_le16(opcode);
	cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
	cmd.group_member_id = cpu_to_le64(vf_id + 1);
	cmd.data_sg = &data_sg;
	ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);

	kfree(data);
	return ret;
}

/*
 * virtio_pci_admin_legacy_io_write_common - Write legacy common configuration
 * of a member device
 * @dev: VF pci_dev
 * @offset: starting byte offset within the common configuration area to write to
 * @size: size of the data to write
 * @buf: buffer which holds the data
 *
 * Note: caller must serialize access for the given device.
 * Returns 0 on success, or negative on failure.
 */
int virtio_pci_admin_legacy_common_io_write(struct pci_dev *pdev, u8 offset,
					    u8 size, u8 *buf)
{
	return virtio_pci_admin_legacy_io_write(pdev,
					VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE,
					offset, size, buf);
}
EXPORT_SYMBOL_GPL(virtio_pci_admin_legacy_common_io_write);

/*
 * virtio_pci_admin_legacy_io_write_device - Write legacy device configuration
 * of a member device
 * @dev: VF pci_dev
 * @offset: starting byte offset within the device configuration area to write to
 * @size: size of the data to write
 * @buf: buffer which holds the data
 *
 * Note: caller must serialize access for the given device.
 * Returns 0 on success, or negative on failure.
 */
int virtio_pci_admin_legacy_device_io_write(struct pci_dev *pdev, u8 offset,
					    u8 size, u8 *buf)
{
	return virtio_pci_admin_legacy_io_write(pdev,
					VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE,
					offset, size, buf);
}
EXPORT_SYMBOL_GPL(virtio_pci_admin_legacy_device_io_write);

static int virtio_pci_admin_legacy_io_read(struct pci_dev *pdev, u16 opcode,
					   u8 offset, u8 size, u8 *buf)
{
	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
	struct virtio_admin_cmd_legacy_rd_data *data;
	struct scatterlist data_sg, result_sg;
	struct virtio_admin_cmd cmd = {};
	int vf_id;
	int ret;

	if (!virtio_dev)
		return -ENODEV;

	vf_id = pci_iov_vf_id(pdev);
	if (vf_id < 0)
		return vf_id;

	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	data->offset = offset;
	sg_init_one(&data_sg, data, sizeof(*data));
	sg_init_one(&result_sg, buf, size);
	cmd.opcode = cpu_to_le16(opcode);
	cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
	cmd.group_member_id = cpu_to_le64(vf_id + 1);
	cmd.data_sg = &data_sg;
	cmd.result_sg = &result_sg;
	ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);

	kfree(data);
	return ret;
}

/*
 * virtio_pci_admin_legacy_device_io_read - Read legacy device configuration of
 * a member device
 * @dev: VF pci_dev
 * @offset: starting byte offset within the device configuration area to read from
 * @size: size of the data to be read
 * @buf: buffer to hold the returned data
 *
 * Note: caller must serialize access for the given device.
 * Returns 0 on success, or negative on failure.
 */
int virtio_pci_admin_legacy_device_io_read(struct pci_dev *pdev, u8 offset,
					   u8 size, u8 *buf)
{
	return virtio_pci_admin_legacy_io_read(pdev,
					VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ,
					offset, size, buf);
}
EXPORT_SYMBOL_GPL(virtio_pci_admin_legacy_device_io_read);

/*
 * virtio_pci_admin_legacy_common_io_read - Read legacy common configuration of
 * a member device
 * @dev: VF pci_dev
 * @offset: starting byte offset within the common configuration area to read from
 * @size: size of the data to be read
 * @buf: buffer to hold the returned data
 *
 * Note: caller must serialize access for the given device.
 * Returns 0 on success, or negative on failure.
 */
int virtio_pci_admin_legacy_common_io_read(struct pci_dev *pdev, u8 offset,
					   u8 size, u8 *buf)
{
	return virtio_pci_admin_legacy_io_read(pdev,
					VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ,
					offset, size, buf);
}
EXPORT_SYMBOL_GPL(virtio_pci_admin_legacy_common_io_read);

/*
 * virtio_pci_admin_legacy_io_notify_info - Read the queue notification
 * information for legacy interface
 * @dev: VF pci_dev
 * @req_bar_flags: requested bar flags
 * @bar: on output the BAR number of the owner or member device
 * @bar_offset: on output the offset within bar
 *
 * Returns 0 on success, or negative on failure.
 */
int virtio_pci_admin_legacy_io_notify_info(struct pci_dev *pdev,
					   u8 req_bar_flags, u8 *bar,
					   u64 *bar_offset)
{
	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
	struct virtio_admin_cmd_notify_info_result *result;
	struct virtio_admin_cmd cmd = {};
	struct scatterlist result_sg;
	int vf_id;
	int ret;

	if (!virtio_dev)
		return -ENODEV;

	vf_id = pci_iov_vf_id(pdev);
	if (vf_id < 0)
		return vf_id;

	result = kzalloc(sizeof(*result), GFP_KERNEL);
	if (!result)
		return -ENOMEM;

	sg_init_one(&result_sg, result, sizeof(*result));
	cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO);
	cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
	cmd.group_member_id = cpu_to_le64(vf_id + 1);
	cmd.result_sg = &result_sg;
	ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
	if (!ret) {
		struct virtio_admin_cmd_notify_info_data *entry;
		int i;

		ret = -ENOENT;
		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
			entry = &result->entries[i];
			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
				break;
			if (entry->flags != req_bar_flags)
				continue;
			*bar = entry->bar;
			*bar_offset = le64_to_cpu(entry->offset);
			ret = 0;
			break;
		}
	}

	kfree(result);
	return ret;
}
EXPORT_SYMBOL_GPL(virtio_pci_admin_legacy_io_notify_info);