1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * vDPA bus.
4 *
5 * Copyright (c) 2020, Red Hat. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
7 *
8 */
9
10 #include <linux/module.h>
11 #include <linux/idr.h>
12 #include <linux/slab.h>
13 #include <linux/vdpa.h>
14 #include <uapi/linux/vdpa.h>
15 #include <net/genetlink.h>
16 #include <linux/virtio_ids.h>
17
18 static LIST_HEAD(mdev_head);
19 /* A global mutex that protects vdpa management device and device level operations. */
20 static DECLARE_RWSEM(vdpa_dev_lock);
21 static DEFINE_IDA(vdpa_index_ida);
22
vdpa_set_status(struct vdpa_device * vdev,u8 status)23 void vdpa_set_status(struct vdpa_device *vdev, u8 status)
24 {
25 down_write(&vdev->cf_lock);
26 vdev->config->set_status(vdev, status);
27 up_write(&vdev->cf_lock);
28 }
29 EXPORT_SYMBOL(vdpa_set_status);
30
31 static struct genl_family vdpa_nl_family;
32
vdpa_dev_probe(struct device * d)33 static int vdpa_dev_probe(struct device *d)
34 {
35 struct vdpa_device *vdev = dev_to_vdpa(d);
36 struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
37 const struct vdpa_config_ops *ops = vdev->config;
38 u32 max_num, min_num = 1;
39 int ret = 0;
40
41 d->dma_mask = &d->coherent_dma_mask;
42 ret = dma_set_mask_and_coherent(d, DMA_BIT_MASK(64));
43 if (ret)
44 return ret;
45
46 max_num = ops->get_vq_num_max(vdev);
47 if (ops->get_vq_num_min)
48 min_num = ops->get_vq_num_min(vdev);
49 if (max_num < min_num)
50 return -EINVAL;
51
52 if (drv && drv->probe)
53 ret = drv->probe(vdev);
54
55 return ret;
56 }
57
vdpa_dev_remove(struct device * d)58 static void vdpa_dev_remove(struct device *d)
59 {
60 struct vdpa_device *vdev = dev_to_vdpa(d);
61 struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
62
63 if (drv && drv->remove)
64 drv->remove(vdev);
65 }
66
vdpa_dev_match(struct device * dev,const struct device_driver * drv)67 static int vdpa_dev_match(struct device *dev, const struct device_driver *drv)
68 {
69 int ret;
70
71 /* Check override first, and if set, only use the named driver */
72 ret = device_match_driver_override(dev, drv);
73 if (ret >= 0)
74 return ret;
75
76 /* Currently devices must be supported by all vDPA bus drivers */
77 return 1;
78 }
79
80 static const struct bus_type vdpa_bus = {
81 .name = "vdpa",
82 .driver_override = true,
83 .match = vdpa_dev_match,
84 .probe = vdpa_dev_probe,
85 .remove = vdpa_dev_remove,
86 };
87
vdpa_release_dev(struct device * d)88 static void vdpa_release_dev(struct device *d)
89 {
90 struct vdpa_device *vdev = dev_to_vdpa(d);
91 const struct vdpa_config_ops *ops = vdev->config;
92
93 if (ops->free)
94 ops->free(vdev);
95
96 ida_free(&vdpa_index_ida, vdev->index);
97 kfree(vdev);
98 }
99
100 /**
101 * __vdpa_alloc_device - allocate and initilaize a vDPA device
102 * This allows driver to some prepartion after device is
103 * initialized but before registered.
104 * @parent: the parent device
105 * @config: the bus operations that is supported by this device
106 * @map: the map operations that is supported by this device
107 * @ngroups: number of groups supported by this device
108 * @nas: number of address spaces supported by this device
109 * @size: size of the parent structure that contains private data
110 * @name: name of the vdpa device; optional.
111 * @use_va: indicate whether virtual address must be used by this device
112 *
113 * Driver should use vdpa_alloc_device() wrapper macro instead of
114 * using this directly.
115 *
116 * Return: Returns an error when parent/config/map is not set or fail to get
117 * ida.
118 */
__vdpa_alloc_device(struct device * parent,const struct vdpa_config_ops * config,const struct virtio_map_ops * map,unsigned int ngroups,unsigned int nas,size_t size,const char * name,bool use_va)119 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
120 const struct vdpa_config_ops *config,
121 const struct virtio_map_ops *map,
122 unsigned int ngroups, unsigned int nas,
123 size_t size, const char *name,
124 bool use_va)
125 {
126 struct vdpa_device *vdev;
127 int err = -EINVAL;
128
129 if (!config)
130 goto err;
131
132 if (!!config->dma_map != !!config->dma_unmap)
133 goto err;
134
135 /* It should only work for the device that use on-chip IOMMU */
136 if (use_va && !(config->dma_map || config->set_map))
137 goto err;
138
139 err = -ENOMEM;
140 vdev = kzalloc(size, GFP_KERNEL);
141 if (!vdev)
142 goto err;
143
144 err = ida_alloc(&vdpa_index_ida, GFP_KERNEL);
145 if (err < 0)
146 goto err_ida;
147
148 vdev->dev.bus = &vdpa_bus;
149 vdev->dev.parent = parent;
150 vdev->dev.release = vdpa_release_dev;
151 vdev->index = err;
152 vdev->config = config;
153 vdev->map = map;
154 vdev->features_valid = false;
155 vdev->use_va = use_va;
156 vdev->ngroups = ngroups;
157 vdev->nas = nas;
158
159 if (name)
160 err = dev_set_name(&vdev->dev, "%s", name);
161 else
162 err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
163 if (err)
164 goto err_name;
165
166 init_rwsem(&vdev->cf_lock);
167 device_initialize(&vdev->dev);
168
169 return vdev;
170
171 err_name:
172 ida_free(&vdpa_index_ida, vdev->index);
173 err_ida:
174 kfree(vdev);
175 err:
176 return ERR_PTR(err);
177 }
178 EXPORT_SYMBOL_GPL(__vdpa_alloc_device);
179
vdpa_name_match(struct device * dev,const void * data)180 static int vdpa_name_match(struct device *dev, const void *data)
181 {
182 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
183
184 return (strcmp(dev_name(&vdev->dev), data) == 0);
185 }
186
__vdpa_register_device(struct vdpa_device * vdev,u32 nvqs)187 static int __vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
188 {
189 struct device *dev;
190
191 vdev->nvqs = nvqs;
192
193 lockdep_assert_held(&vdpa_dev_lock);
194 dev = bus_find_device(&vdpa_bus, NULL, dev_name(&vdev->dev), vdpa_name_match);
195 if (dev) {
196 put_device(dev);
197 return -EEXIST;
198 }
199 return device_add(&vdev->dev);
200 }
201
202 /**
203 * _vdpa_register_device - register a vDPA device with vdpa lock held
204 * Caller must have a succeed call of vdpa_alloc_device() before.
205 * Caller must invoke this routine in the management device dev_add()
206 * callback after setting up valid mgmtdev for this vdpa device.
207 * @vdev: the vdpa device to be registered to vDPA bus
208 * @nvqs: number of virtqueues supported by this device
209 *
210 * Return: Returns an error when fail to add device to vDPA bus
211 */
_vdpa_register_device(struct vdpa_device * vdev,u32 nvqs)212 int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
213 {
214 if (!vdev->mdev)
215 return -EINVAL;
216
217 return __vdpa_register_device(vdev, nvqs);
218 }
219 EXPORT_SYMBOL_GPL(_vdpa_register_device);
220
221 /**
222 * vdpa_register_device - register a vDPA device
223 * Callers must have a succeed call of vdpa_alloc_device() before.
224 * @vdev: the vdpa device to be registered to vDPA bus
225 * @nvqs: number of virtqueues supported by this device
226 *
227 * Return: Returns an error when fail to add to vDPA bus
228 */
vdpa_register_device(struct vdpa_device * vdev,u32 nvqs)229 int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
230 {
231 int err;
232
233 down_write(&vdpa_dev_lock);
234 err = __vdpa_register_device(vdev, nvqs);
235 up_write(&vdpa_dev_lock);
236 return err;
237 }
238 EXPORT_SYMBOL_GPL(vdpa_register_device);
239
240 /**
241 * _vdpa_unregister_device - unregister a vDPA device
242 * Caller must invoke this routine as part of management device dev_del()
243 * callback.
244 * @vdev: the vdpa device to be unregisted from vDPA bus
245 */
_vdpa_unregister_device(struct vdpa_device * vdev)246 void _vdpa_unregister_device(struct vdpa_device *vdev)
247 {
248 lockdep_assert_held(&vdpa_dev_lock);
249 WARN_ON(!vdev->mdev);
250 device_unregister(&vdev->dev);
251 }
252 EXPORT_SYMBOL_GPL(_vdpa_unregister_device);
253
254 /**
255 * vdpa_unregister_device - unregister a vDPA device
256 * @vdev: the vdpa device to be unregisted from vDPA bus
257 */
vdpa_unregister_device(struct vdpa_device * vdev)258 void vdpa_unregister_device(struct vdpa_device *vdev)
259 {
260 down_write(&vdpa_dev_lock);
261 device_unregister(&vdev->dev);
262 up_write(&vdpa_dev_lock);
263 }
264 EXPORT_SYMBOL_GPL(vdpa_unregister_device);
265
266 /**
267 * __vdpa_register_driver - register a vDPA device driver
268 * @drv: the vdpa device driver to be registered
269 * @owner: module owner of the driver
270 *
271 * Return: Returns an err when fail to do the registration
272 */
__vdpa_register_driver(struct vdpa_driver * drv,struct module * owner)273 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner)
274 {
275 drv->driver.bus = &vdpa_bus;
276 drv->driver.owner = owner;
277
278 return driver_register(&drv->driver);
279 }
280 EXPORT_SYMBOL_GPL(__vdpa_register_driver);
281
282 /**
283 * vdpa_unregister_driver - unregister a vDPA device driver
284 * @drv: the vdpa device driver to be unregistered
285 */
vdpa_unregister_driver(struct vdpa_driver * drv)286 void vdpa_unregister_driver(struct vdpa_driver *drv)
287 {
288 driver_unregister(&drv->driver);
289 }
290 EXPORT_SYMBOL_GPL(vdpa_unregister_driver);
291
292 /**
293 * vdpa_mgmtdev_register - register a vdpa management device
294 *
295 * @mdev: Pointer to vdpa management device
296 * vdpa_mgmtdev_register() register a vdpa management device which supports
297 * vdpa device management.
298 * Return: Returns 0 on success or failure when required callback ops are not
299 * initialized.
300 */
vdpa_mgmtdev_register(struct vdpa_mgmt_dev * mdev)301 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev)
302 {
303 if (!mdev->device || !mdev->ops || !mdev->ops->dev_add || !mdev->ops->dev_del)
304 return -EINVAL;
305
306 INIT_LIST_HEAD(&mdev->list);
307 down_write(&vdpa_dev_lock);
308 list_add_tail(&mdev->list, &mdev_head);
309 up_write(&vdpa_dev_lock);
310 return 0;
311 }
312 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register);
313
vdpa_match_remove(struct device * dev,void * data)314 static int vdpa_match_remove(struct device *dev, void *data)
315 {
316 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
317 struct vdpa_mgmt_dev *mdev = vdev->mdev;
318
319 if (mdev == data)
320 mdev->ops->dev_del(mdev, vdev);
321 return 0;
322 }
323
vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev * mdev)324 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev)
325 {
326 down_write(&vdpa_dev_lock);
327
328 list_del(&mdev->list);
329
330 /* Filter out all the entries belong to this management device and delete it. */
331 bus_for_each_dev(&vdpa_bus, NULL, mdev, vdpa_match_remove);
332
333 up_write(&vdpa_dev_lock);
334 }
335 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister);
336
vdpa_get_config_unlocked(struct vdpa_device * vdev,unsigned int offset,void * buf,unsigned int len)337 static void vdpa_get_config_unlocked(struct vdpa_device *vdev,
338 unsigned int offset,
339 void *buf, unsigned int len)
340 {
341 const struct vdpa_config_ops *ops = vdev->config;
342
343 /*
344 * Config accesses aren't supposed to trigger before features are set.
345 * If it does happen we assume a legacy guest.
346 */
347 if (!vdev->features_valid)
348 vdpa_set_features_unlocked(vdev, 0);
349 ops->get_config(vdev, offset, buf, len);
350 }
351
352 /**
353 * vdpa_get_config - Get one or more device configuration fields.
354 * @vdev: vdpa device to operate on
355 * @offset: starting byte offset of the field
356 * @buf: buffer pointer to read to
357 * @len: length of the configuration fields in bytes
358 */
vdpa_get_config(struct vdpa_device * vdev,unsigned int offset,void * buf,unsigned int len)359 void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
360 void *buf, unsigned int len)
361 {
362 down_read(&vdev->cf_lock);
363 vdpa_get_config_unlocked(vdev, offset, buf, len);
364 up_read(&vdev->cf_lock);
365 }
366 EXPORT_SYMBOL_GPL(vdpa_get_config);
367
368 /**
369 * vdpa_set_config - Set one or more device configuration fields.
370 * @vdev: vdpa device to operate on
371 * @offset: starting byte offset of the field
372 * @buf: buffer pointer to read from
373 * @length: length of the configuration fields in bytes
374 */
vdpa_set_config(struct vdpa_device * vdev,unsigned int offset,const void * buf,unsigned int length)375 void vdpa_set_config(struct vdpa_device *vdev, unsigned int offset,
376 const void *buf, unsigned int length)
377 {
378 down_write(&vdev->cf_lock);
379 vdev->config->set_config(vdev, offset, buf, length);
380 up_write(&vdev->cf_lock);
381 }
382 EXPORT_SYMBOL_GPL(vdpa_set_config);
383
mgmtdev_handle_match(const struct vdpa_mgmt_dev * mdev,const char * busname,const char * devname)384 static bool mgmtdev_handle_match(const struct vdpa_mgmt_dev *mdev,
385 const char *busname, const char *devname)
386 {
387 /* Bus name is optional for simulated management device, so ignore the
388 * device with bus if bus attribute is provided.
389 */
390 if ((busname && !mdev->device->bus) || (!busname && mdev->device->bus))
391 return false;
392
393 if (!busname && strcmp(dev_name(mdev->device), devname) == 0)
394 return true;
395
396 if (busname && (strcmp(mdev->device->bus->name, busname) == 0) &&
397 (strcmp(dev_name(mdev->device), devname) == 0))
398 return true;
399
400 return false;
401 }
402
vdpa_mgmtdev_get_from_attr(struct nlattr ** attrs)403 static struct vdpa_mgmt_dev *vdpa_mgmtdev_get_from_attr(struct nlattr **attrs)
404 {
405 struct vdpa_mgmt_dev *mdev;
406 const char *busname = NULL;
407 const char *devname;
408
409 if (!attrs[VDPA_ATTR_MGMTDEV_DEV_NAME])
410 return ERR_PTR(-EINVAL);
411 devname = nla_data(attrs[VDPA_ATTR_MGMTDEV_DEV_NAME]);
412 if (attrs[VDPA_ATTR_MGMTDEV_BUS_NAME])
413 busname = nla_data(attrs[VDPA_ATTR_MGMTDEV_BUS_NAME]);
414
415 list_for_each_entry(mdev, &mdev_head, list) {
416 if (mgmtdev_handle_match(mdev, busname, devname))
417 return mdev;
418 }
419 return ERR_PTR(-ENODEV);
420 }
421
vdpa_nl_mgmtdev_handle_fill(struct sk_buff * msg,const struct vdpa_mgmt_dev * mdev)422 static int vdpa_nl_mgmtdev_handle_fill(struct sk_buff *msg, const struct vdpa_mgmt_dev *mdev)
423 {
424 if (mdev->device->bus &&
425 nla_put_string(msg, VDPA_ATTR_MGMTDEV_BUS_NAME, mdev->device->bus->name))
426 return -EMSGSIZE;
427 if (nla_put_string(msg, VDPA_ATTR_MGMTDEV_DEV_NAME, dev_name(mdev->device)))
428 return -EMSGSIZE;
429 return 0;
430 }
431
vdpa_mgmtdev_get_classes(const struct vdpa_mgmt_dev * mdev,unsigned int * nclasses)432 static u64 vdpa_mgmtdev_get_classes(const struct vdpa_mgmt_dev *mdev,
433 unsigned int *nclasses)
434 {
435 u64 supported_classes = 0;
436 unsigned int n = 0;
437
438 for (int i = 0; mdev->id_table[i].device; i++) {
439 if (mdev->id_table[i].device > 63)
440 continue;
441 supported_classes |= BIT_ULL(mdev->id_table[i].device);
442 n++;
443 }
444 if (nclasses)
445 *nclasses = n;
446
447 return supported_classes;
448 }
449
vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev * mdev,struct sk_buff * msg,u32 portid,u32 seq,int flags)450 static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev *mdev, struct sk_buff *msg,
451 u32 portid, u32 seq, int flags)
452 {
453 void *hdr;
454 int err;
455
456 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_MGMTDEV_NEW);
457 if (!hdr)
458 return -EMSGSIZE;
459 err = vdpa_nl_mgmtdev_handle_fill(msg, mdev);
460 if (err)
461 goto msg_err;
462
463 if (nla_put_u64_64bit(msg, VDPA_ATTR_MGMTDEV_SUPPORTED_CLASSES,
464 vdpa_mgmtdev_get_classes(mdev, NULL),
465 VDPA_ATTR_UNSPEC)) {
466 err = -EMSGSIZE;
467 goto msg_err;
468 }
469 if (nla_put_u32(msg, VDPA_ATTR_DEV_MGMTDEV_MAX_VQS,
470 mdev->max_supported_vqs)) {
471 err = -EMSGSIZE;
472 goto msg_err;
473 }
474 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_SUPPORTED_FEATURES,
475 mdev->supported_features, VDPA_ATTR_PAD)) {
476 err = -EMSGSIZE;
477 goto msg_err;
478 }
479
480 genlmsg_end(msg, hdr);
481 return 0;
482
483 msg_err:
484 genlmsg_cancel(msg, hdr);
485 return err;
486 }
487
vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff * skb,struct genl_info * info)488 static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *info)
489 {
490 struct vdpa_mgmt_dev *mdev;
491 struct sk_buff *msg;
492 int err;
493
494 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
495 if (!msg)
496 return -ENOMEM;
497
498 down_read(&vdpa_dev_lock);
499 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
500 if (IS_ERR(mdev)) {
501 up_read(&vdpa_dev_lock);
502 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified mgmt device");
503 err = PTR_ERR(mdev);
504 goto out;
505 }
506
507 err = vdpa_mgmtdev_fill(mdev, msg, info->snd_portid, info->snd_seq, 0);
508 up_read(&vdpa_dev_lock);
509 if (err)
510 goto out;
511 err = genlmsg_reply(msg, info);
512 return err;
513
514 out:
515 nlmsg_free(msg);
516 return err;
517 }
518
519 static int
vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff * msg,struct netlink_callback * cb)520 vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
521 {
522 struct vdpa_mgmt_dev *mdev;
523 int start = cb->args[0];
524 int idx = 0;
525 int err;
526
527 down_read(&vdpa_dev_lock);
528 list_for_each_entry(mdev, &mdev_head, list) {
529 if (idx < start) {
530 idx++;
531 continue;
532 }
533 err = vdpa_mgmtdev_fill(mdev, msg, NETLINK_CB(cb->skb).portid,
534 cb->nlh->nlmsg_seq, NLM_F_MULTI);
535 if (err)
536 goto out;
537 idx++;
538 }
539 out:
540 up_read(&vdpa_dev_lock);
541 cb->args[0] = idx;
542 return msg->len;
543 }
544
545 #define VDPA_DEV_NET_ATTRS_MASK (BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR) | \
546 BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU) | \
547 BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP))
548
549 /*
550 * Bitmask for all per-device features: feature bits VIRTIO_TRANSPORT_F_START
551 * through VIRTIO_TRANSPORT_F_END are unset, i.e. 0xfffffc000fffffff for
552 * all 64bit features. If the features are extended beyond 64 bits, or new
553 * "holes" are reserved for other type of features than per-device, this
554 * macro would have to be updated.
555 */
556 #define VIRTIO_DEVICE_F_MASK (~0ULL << (VIRTIO_TRANSPORT_F_END + 1) | \
557 ((1ULL << VIRTIO_TRANSPORT_F_START) - 1))
558
vdpa_nl_cmd_dev_add_set_doit(struct sk_buff * skb,struct genl_info * info)559 static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *info)
560 {
561 struct vdpa_dev_set_config config = {};
562 struct nlattr **nl_attrs = info->attrs;
563 struct vdpa_mgmt_dev *mdev;
564 unsigned int ncls = 0;
565 const u8 *macaddr;
566 const char *name;
567 u64 classes;
568 int err = 0;
569
570 if (!info->attrs[VDPA_ATTR_DEV_NAME])
571 return -EINVAL;
572
573 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
574
575 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]) {
576 macaddr = nla_data(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]);
577 memcpy(config.net.mac, macaddr, sizeof(config.net.mac));
578 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
579 }
580 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MTU]) {
581 config.net.mtu =
582 nla_get_u16(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MTU]);
583 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU);
584 }
585 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]) {
586 config.net.max_vq_pairs =
587 nla_get_u16(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]);
588 if (!config.net.max_vq_pairs) {
589 NL_SET_ERR_MSG_MOD(info->extack,
590 "At least one pair of VQs is required");
591 return -EINVAL;
592 }
593 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
594 }
595 if (nl_attrs[VDPA_ATTR_DEV_FEATURES]) {
596 u64 missing = 0x0ULL;
597
598 config.device_features =
599 nla_get_u64(nl_attrs[VDPA_ATTR_DEV_FEATURES]);
600 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR] &&
601 !(config.device_features & BIT_ULL(VIRTIO_NET_F_MAC)))
602 missing |= BIT_ULL(VIRTIO_NET_F_MAC);
603 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MTU] &&
604 !(config.device_features & BIT_ULL(VIRTIO_NET_F_MTU)))
605 missing |= BIT_ULL(VIRTIO_NET_F_MTU);
606 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MAX_VQP] &&
607 config.net.max_vq_pairs > 1 &&
608 !(config.device_features & BIT_ULL(VIRTIO_NET_F_MQ)))
609 missing |= BIT_ULL(VIRTIO_NET_F_MQ);
610 if (missing) {
611 NL_SET_ERR_MSG_FMT_MOD(info->extack,
612 "Missing features 0x%llx for provided attributes",
613 missing);
614 return -EINVAL;
615 }
616 config.mask |= BIT_ULL(VDPA_ATTR_DEV_FEATURES);
617 }
618
619 /* Skip checking capability if user didn't prefer to configure any
620 * device networking attributes. It is likely that user might have used
621 * a device specific method to configure such attributes or using device
622 * default attributes.
623 */
624 if ((config.mask & VDPA_DEV_NET_ATTRS_MASK) &&
625 !netlink_capable(skb, CAP_NET_ADMIN))
626 return -EPERM;
627
628 down_write(&vdpa_dev_lock);
629 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
630 if (IS_ERR(mdev)) {
631 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified management device");
632 err = PTR_ERR(mdev);
633 goto err;
634 }
635
636 if ((config.mask & mdev->config_attr_mask) != config.mask) {
637 NL_SET_ERR_MSG_FMT_MOD(info->extack,
638 "Some provided attributes are not supported: 0x%llx",
639 config.mask & ~mdev->config_attr_mask);
640 err = -EOPNOTSUPP;
641 goto err;
642 }
643
644 classes = vdpa_mgmtdev_get_classes(mdev, &ncls);
645 if (config.mask & VDPA_DEV_NET_ATTRS_MASK &&
646 !(classes & BIT_ULL(VIRTIO_ID_NET))) {
647 NL_SET_ERR_MSG_MOD(info->extack,
648 "Network class attributes provided on unsupported management device");
649 err = -EINVAL;
650 goto err;
651 }
652 if (!(config.mask & VDPA_DEV_NET_ATTRS_MASK) &&
653 config.mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES) &&
654 classes & BIT_ULL(VIRTIO_ID_NET) && ncls > 1 &&
655 config.device_features & VIRTIO_DEVICE_F_MASK) {
656 NL_SET_ERR_MSG_MOD(info->extack,
657 "Management device supports multi-class while device features specified are ambiguous");
658 err = -EINVAL;
659 goto err;
660 }
661
662 err = mdev->ops->dev_add(mdev, name, &config);
663 err:
664 up_write(&vdpa_dev_lock);
665 return err;
666 }
667
vdpa_nl_cmd_dev_del_set_doit(struct sk_buff * skb,struct genl_info * info)668 static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *info)
669 {
670 struct vdpa_mgmt_dev *mdev;
671 struct vdpa_device *vdev;
672 struct device *dev;
673 const char *name;
674 int err = 0;
675
676 if (!info->attrs[VDPA_ATTR_DEV_NAME])
677 return -EINVAL;
678 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
679
680 down_write(&vdpa_dev_lock);
681 dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
682 if (!dev) {
683 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
684 err = -ENODEV;
685 goto dev_err;
686 }
687 vdev = container_of(dev, struct vdpa_device, dev);
688 if (!vdev->mdev) {
689 NL_SET_ERR_MSG_MOD(info->extack, "Only user created device can be deleted by user");
690 err = -EINVAL;
691 goto mdev_err;
692 }
693 mdev = vdev->mdev;
694 mdev->ops->dev_del(mdev, vdev);
695 mdev_err:
696 put_device(dev);
697 dev_err:
698 up_write(&vdpa_dev_lock);
699 return err;
700 }
701
702 static int
vdpa_dev_fill(struct vdpa_device * vdev,struct sk_buff * msg,u32 portid,u32 seq,int flags,struct netlink_ext_ack * extack)703 vdpa_dev_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
704 int flags, struct netlink_ext_ack *extack)
705 {
706 u16 max_vq_size;
707 u16 min_vq_size = 1;
708 u32 device_id;
709 u32 vendor_id;
710 void *hdr;
711 int err;
712
713 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_DEV_NEW);
714 if (!hdr)
715 return -EMSGSIZE;
716
717 err = vdpa_nl_mgmtdev_handle_fill(msg, vdev->mdev);
718 if (err)
719 goto msg_err;
720
721 device_id = vdev->config->get_device_id(vdev);
722 vendor_id = vdev->config->get_vendor_id(vdev);
723 max_vq_size = vdev->config->get_vq_num_max(vdev);
724 if (vdev->config->get_vq_num_min)
725 min_vq_size = vdev->config->get_vq_num_min(vdev);
726
727 err = -EMSGSIZE;
728 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev)))
729 goto msg_err;
730 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id))
731 goto msg_err;
732 if (nla_put_u32(msg, VDPA_ATTR_DEV_VENDOR_ID, vendor_id))
733 goto msg_err;
734 if (nla_put_u32(msg, VDPA_ATTR_DEV_MAX_VQS, vdev->nvqs))
735 goto msg_err;
736 if (nla_put_u16(msg, VDPA_ATTR_DEV_MAX_VQ_SIZE, max_vq_size))
737 goto msg_err;
738 if (nla_put_u16(msg, VDPA_ATTR_DEV_MIN_VQ_SIZE, min_vq_size))
739 goto msg_err;
740
741 genlmsg_end(msg, hdr);
742 return 0;
743
744 msg_err:
745 genlmsg_cancel(msg, hdr);
746 return err;
747 }
748
vdpa_nl_cmd_dev_get_doit(struct sk_buff * skb,struct genl_info * info)749 static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
750 {
751 struct vdpa_device *vdev;
752 struct sk_buff *msg;
753 const char *devname;
754 struct device *dev;
755 int err;
756
757 if (!info->attrs[VDPA_ATTR_DEV_NAME])
758 return -EINVAL;
759 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
760 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
761 if (!msg)
762 return -ENOMEM;
763
764 down_read(&vdpa_dev_lock);
765 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
766 if (!dev) {
767 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
768 err = -ENODEV;
769 goto err;
770 }
771 vdev = container_of(dev, struct vdpa_device, dev);
772 if (!vdev->mdev) {
773 err = -EINVAL;
774 goto mdev_err;
775 }
776 err = vdpa_dev_fill(vdev, msg, info->snd_portid, info->snd_seq, 0, info->extack);
777 if (err)
778 goto mdev_err;
779
780 err = genlmsg_reply(msg, info);
781 put_device(dev);
782 up_read(&vdpa_dev_lock);
783 return err;
784
785 mdev_err:
786 put_device(dev);
787 err:
788 up_read(&vdpa_dev_lock);
789 nlmsg_free(msg);
790 return err;
791 }
792
793 struct vdpa_dev_dump_info {
794 struct sk_buff *msg;
795 struct netlink_callback *cb;
796 int start_idx;
797 int idx;
798 };
799
vdpa_dev_dump(struct device * dev,void * data)800 static int vdpa_dev_dump(struct device *dev, void *data)
801 {
802 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
803 struct vdpa_dev_dump_info *info = data;
804 int err;
805
806 if (!vdev->mdev)
807 return 0;
808 if (info->idx < info->start_idx) {
809 info->idx++;
810 return 0;
811 }
812 err = vdpa_dev_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
813 info->cb->nlh->nlmsg_seq, NLM_F_MULTI, info->cb->extack);
814 if (err)
815 return err;
816
817 info->idx++;
818 return 0;
819 }
820
vdpa_nl_cmd_dev_get_dumpit(struct sk_buff * msg,struct netlink_callback * cb)821 static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
822 {
823 struct vdpa_dev_dump_info info;
824
825 info.msg = msg;
826 info.cb = cb;
827 info.start_idx = cb->args[0];
828 info.idx = 0;
829
830 down_read(&vdpa_dev_lock);
831 bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_dump);
832 up_read(&vdpa_dev_lock);
833 cb->args[0] = info.idx;
834 return msg->len;
835 }
836
vdpa_dev_net_mq_config_fill(struct sk_buff * msg,u64 features,const struct virtio_net_config * config)837 static int vdpa_dev_net_mq_config_fill(struct sk_buff *msg, u64 features,
838 const struct virtio_net_config *config)
839 {
840 u16 val_u16;
841
842 if ((features & BIT_ULL(VIRTIO_NET_F_MQ)) == 0 &&
843 (features & BIT_ULL(VIRTIO_NET_F_RSS)) == 0)
844 return 0;
845
846 val_u16 = __virtio16_to_cpu(true, config->max_virtqueue_pairs);
847
848 return nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MAX_VQP, val_u16);
849 }
850
vdpa_dev_net_mtu_config_fill(struct sk_buff * msg,u64 features,const struct virtio_net_config * config)851 static int vdpa_dev_net_mtu_config_fill(struct sk_buff *msg, u64 features,
852 const struct virtio_net_config *config)
853 {
854 u16 val_u16;
855
856 if ((features & BIT_ULL(VIRTIO_NET_F_MTU)) == 0)
857 return 0;
858
859 val_u16 = __virtio16_to_cpu(true, config->mtu);
860
861 return nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MTU, val_u16);
862 }
863
vdpa_dev_net_mac_config_fill(struct sk_buff * msg,u64 features,const struct virtio_net_config * config)864 static int vdpa_dev_net_mac_config_fill(struct sk_buff *msg, u64 features,
865 const struct virtio_net_config *config)
866 {
867 if ((features & BIT_ULL(VIRTIO_NET_F_MAC)) == 0)
868 return 0;
869
870 return nla_put(msg, VDPA_ATTR_DEV_NET_CFG_MACADDR,
871 sizeof(config->mac), config->mac);
872 }
873
vdpa_dev_net_status_config_fill(struct sk_buff * msg,u64 features,const struct virtio_net_config * config)874 static int vdpa_dev_net_status_config_fill(struct sk_buff *msg, u64 features,
875 const struct virtio_net_config *config)
876 {
877 u16 val_u16;
878
879 if ((features & BIT_ULL(VIRTIO_NET_F_STATUS)) == 0)
880 return 0;
881
882 val_u16 = __virtio16_to_cpu(true, config->status);
883 return nla_put_u16(msg, VDPA_ATTR_DEV_NET_STATUS, val_u16);
884 }
885
vdpa_dev_net_config_fill(struct vdpa_device * vdev,struct sk_buff * msg)886 static int vdpa_dev_net_config_fill(struct vdpa_device *vdev, struct sk_buff *msg)
887 {
888 struct virtio_net_config config = {};
889 u64 features_device;
890
891 vdev->config->get_config(vdev, 0, &config, sizeof(config));
892
893 features_device = vdev->config->get_device_features(vdev);
894
895 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_FEATURES, features_device,
896 VDPA_ATTR_PAD))
897 return -EMSGSIZE;
898
899 if (vdpa_dev_net_mtu_config_fill(msg, features_device, &config))
900 return -EMSGSIZE;
901
902 if (vdpa_dev_net_mac_config_fill(msg, features_device, &config))
903 return -EMSGSIZE;
904
905 if (vdpa_dev_net_status_config_fill(msg, features_device, &config))
906 return -EMSGSIZE;
907
908 return vdpa_dev_net_mq_config_fill(msg, features_device, &config);
909 }
910
911 static int
vdpa_dev_blk_capacity_config_fill(struct sk_buff * msg,const struct virtio_blk_config * config)912 vdpa_dev_blk_capacity_config_fill(struct sk_buff *msg,
913 const struct virtio_blk_config *config)
914 {
915 u64 val_u64;
916
917 val_u64 = __virtio64_to_cpu(true, config->capacity);
918
919 return nla_put_u64_64bit(msg, VDPA_ATTR_DEV_BLK_CFG_CAPACITY,
920 val_u64, VDPA_ATTR_PAD);
921 }
922
923 static int
vdpa_dev_blk_seg_size_config_fill(struct sk_buff * msg,u64 features,const struct virtio_blk_config * config)924 vdpa_dev_blk_seg_size_config_fill(struct sk_buff *msg, u64 features,
925 const struct virtio_blk_config *config)
926 {
927 u32 val_u32;
928
929 if ((features & BIT_ULL(VIRTIO_BLK_F_SIZE_MAX)) == 0)
930 return 0;
931
932 val_u32 = __virtio32_to_cpu(true, config->size_max);
933
934 return nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_SIZE_MAX, val_u32);
935 }
936
937 /* fill the block size*/
938 static int
vdpa_dev_blk_block_size_config_fill(struct sk_buff * msg,u64 features,const struct virtio_blk_config * config)939 vdpa_dev_blk_block_size_config_fill(struct sk_buff *msg, u64 features,
940 const struct virtio_blk_config *config)
941 {
942 u32 val_u32;
943
944 if ((features & BIT_ULL(VIRTIO_BLK_F_BLK_SIZE)) == 0)
945 return 0;
946
947 val_u32 = __virtio32_to_cpu(true, config->blk_size);
948
949 return nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_BLK_SIZE, val_u32);
950 }
951
952 static int
vdpa_dev_blk_seg_max_config_fill(struct sk_buff * msg,u64 features,const struct virtio_blk_config * config)953 vdpa_dev_blk_seg_max_config_fill(struct sk_buff *msg, u64 features,
954 const struct virtio_blk_config *config)
955 {
956 u32 val_u32;
957
958 if ((features & BIT_ULL(VIRTIO_BLK_F_SEG_MAX)) == 0)
959 return 0;
960
961 val_u32 = __virtio32_to_cpu(true, config->seg_max);
962
963 return nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_SEG_MAX, val_u32);
964 }
965
vdpa_dev_blk_mq_config_fill(struct sk_buff * msg,u64 features,const struct virtio_blk_config * config)966 static int vdpa_dev_blk_mq_config_fill(struct sk_buff *msg, u64 features,
967 const struct virtio_blk_config *config)
968 {
969 u16 val_u16;
970
971 if ((features & BIT_ULL(VIRTIO_BLK_F_MQ)) == 0)
972 return 0;
973
974 val_u16 = __virtio16_to_cpu(true, config->num_queues);
975
976 return nla_put_u16(msg, VDPA_ATTR_DEV_BLK_CFG_NUM_QUEUES, val_u16);
977 }
978
vdpa_dev_blk_topology_config_fill(struct sk_buff * msg,u64 features,const struct virtio_blk_config * config)979 static int vdpa_dev_blk_topology_config_fill(struct sk_buff *msg, u64 features,
980 const struct virtio_blk_config *config)
981 {
982 u16 min_io_size;
983 u32 opt_io_size;
984
985 if ((features & BIT_ULL(VIRTIO_BLK_F_TOPOLOGY)) == 0)
986 return 0;
987
988 min_io_size = __virtio16_to_cpu(true, config->min_io_size);
989 opt_io_size = __virtio32_to_cpu(true, config->opt_io_size);
990
991 if (nla_put_u8(msg, VDPA_ATTR_DEV_BLK_CFG_PHY_BLK_EXP,
992 config->physical_block_exp))
993 return -EMSGSIZE;
994
995 if (nla_put_u8(msg, VDPA_ATTR_DEV_BLK_CFG_ALIGN_OFFSET,
996 config->alignment_offset))
997 return -EMSGSIZE;
998
999 if (nla_put_u16(msg, VDPA_ATTR_DEV_BLK_CFG_MIN_IO_SIZE, min_io_size))
1000 return -EMSGSIZE;
1001
1002 if (nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_OPT_IO_SIZE, opt_io_size))
1003 return -EMSGSIZE;
1004
1005 return 0;
1006 }
1007
vdpa_dev_blk_discard_config_fill(struct sk_buff * msg,u64 features,const struct virtio_blk_config * config)1008 static int vdpa_dev_blk_discard_config_fill(struct sk_buff *msg, u64 features,
1009 const struct virtio_blk_config *config)
1010 {
1011 u32 val_u32;
1012
1013 if ((features & BIT_ULL(VIRTIO_BLK_F_DISCARD)) == 0)
1014 return 0;
1015
1016 val_u32 = __virtio32_to_cpu(true, config->max_discard_sectors);
1017 if (nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_MAX_DISCARD_SEC, val_u32))
1018 return -EMSGSIZE;
1019
1020 val_u32 = __virtio32_to_cpu(true, config->max_discard_seg);
1021 if (nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_MAX_DISCARD_SEG, val_u32))
1022 return -EMSGSIZE;
1023
1024 val_u32 = __virtio32_to_cpu(true, config->discard_sector_alignment);
1025 if (nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_DISCARD_SEC_ALIGN, val_u32))
1026 return -EMSGSIZE;
1027
1028 return 0;
1029 }
1030
1031 static int
vdpa_dev_blk_write_zeroes_config_fill(struct sk_buff * msg,u64 features,const struct virtio_blk_config * config)1032 vdpa_dev_blk_write_zeroes_config_fill(struct sk_buff *msg, u64 features,
1033 const struct virtio_blk_config *config)
1034 {
1035 u32 val_u32;
1036
1037 if ((features & BIT_ULL(VIRTIO_BLK_F_WRITE_ZEROES)) == 0)
1038 return 0;
1039
1040 val_u32 = __virtio32_to_cpu(true, config->max_write_zeroes_sectors);
1041 if (nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_MAX_WRITE_ZEROES_SEC, val_u32))
1042 return -EMSGSIZE;
1043
1044 val_u32 = __virtio32_to_cpu(true, config->max_write_zeroes_seg);
1045 if (nla_put_u32(msg, VDPA_ATTR_DEV_BLK_CFG_MAX_WRITE_ZEROES_SEG, val_u32))
1046 return -EMSGSIZE;
1047
1048 return 0;
1049 }
1050
vdpa_dev_blk_ro_config_fill(struct sk_buff * msg,u64 features)1051 static int vdpa_dev_blk_ro_config_fill(struct sk_buff *msg, u64 features)
1052 {
1053 u8 ro;
1054
1055 ro = ((features & BIT_ULL(VIRTIO_BLK_F_RO)) == 0) ? 0 : 1;
1056 if (nla_put_u8(msg, VDPA_ATTR_DEV_BLK_READ_ONLY, ro))
1057 return -EMSGSIZE;
1058
1059 return 0;
1060 }
1061
vdpa_dev_blk_flush_config_fill(struct sk_buff * msg,u64 features)1062 static int vdpa_dev_blk_flush_config_fill(struct sk_buff *msg, u64 features)
1063 {
1064 u8 flush;
1065
1066 flush = ((features & BIT_ULL(VIRTIO_BLK_F_FLUSH)) == 0) ? 0 : 1;
1067 if (nla_put_u8(msg, VDPA_ATTR_DEV_BLK_FLUSH, flush))
1068 return -EMSGSIZE;
1069
1070 return 0;
1071 }
1072
vdpa_dev_blk_config_fill(struct vdpa_device * vdev,struct sk_buff * msg)1073 static int vdpa_dev_blk_config_fill(struct vdpa_device *vdev,
1074 struct sk_buff *msg)
1075 {
1076 struct virtio_blk_config config = {};
1077 u64 features_device;
1078
1079 vdev->config->get_config(vdev, 0, &config, sizeof(config));
1080
1081 features_device = vdev->config->get_device_features(vdev);
1082
1083 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_FEATURES, features_device,
1084 VDPA_ATTR_PAD))
1085 return -EMSGSIZE;
1086
1087 if (vdpa_dev_blk_capacity_config_fill(msg, &config))
1088 return -EMSGSIZE;
1089
1090 if (vdpa_dev_blk_seg_size_config_fill(msg, features_device, &config))
1091 return -EMSGSIZE;
1092
1093 if (vdpa_dev_blk_block_size_config_fill(msg, features_device, &config))
1094 return -EMSGSIZE;
1095
1096 if (vdpa_dev_blk_seg_max_config_fill(msg, features_device, &config))
1097 return -EMSGSIZE;
1098
1099 if (vdpa_dev_blk_mq_config_fill(msg, features_device, &config))
1100 return -EMSGSIZE;
1101
1102 if (vdpa_dev_blk_topology_config_fill(msg, features_device, &config))
1103 return -EMSGSIZE;
1104
1105 if (vdpa_dev_blk_discard_config_fill(msg, features_device, &config))
1106 return -EMSGSIZE;
1107
1108 if (vdpa_dev_blk_write_zeroes_config_fill(msg, features_device, &config))
1109 return -EMSGSIZE;
1110
1111 if (vdpa_dev_blk_ro_config_fill(msg, features_device))
1112 return -EMSGSIZE;
1113
1114 if (vdpa_dev_blk_flush_config_fill(msg, features_device))
1115 return -EMSGSIZE;
1116
1117 return 0;
1118 }
1119
1120 static int
vdpa_dev_config_fill(struct vdpa_device * vdev,struct sk_buff * msg,u32 portid,u32 seq,int flags,struct netlink_ext_ack * extack)1121 vdpa_dev_config_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
1122 int flags, struct netlink_ext_ack *extack)
1123 {
1124 u64 features_driver;
1125 u8 status = 0;
1126 u32 device_id;
1127 void *hdr;
1128 int err;
1129
1130 down_read(&vdev->cf_lock);
1131 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags,
1132 VDPA_CMD_DEV_CONFIG_GET);
1133 if (!hdr) {
1134 err = -EMSGSIZE;
1135 goto out;
1136 }
1137
1138 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev))) {
1139 err = -EMSGSIZE;
1140 goto msg_err;
1141 }
1142
1143 device_id = vdev->config->get_device_id(vdev);
1144 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id)) {
1145 err = -EMSGSIZE;
1146 goto msg_err;
1147 }
1148
1149 /* only read driver features after the feature negotiation is done */
1150 status = vdev->config->get_status(vdev);
1151 if (status & VIRTIO_CONFIG_S_FEATURES_OK) {
1152 features_driver = vdev->config->get_driver_features(vdev);
1153 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_NEGOTIATED_FEATURES, features_driver,
1154 VDPA_ATTR_PAD)) {
1155 err = -EMSGSIZE;
1156 goto msg_err;
1157 }
1158 }
1159
1160 switch (device_id) {
1161 case VIRTIO_ID_NET:
1162 err = vdpa_dev_net_config_fill(vdev, msg);
1163 break;
1164 case VIRTIO_ID_BLOCK:
1165 err = vdpa_dev_blk_config_fill(vdev, msg);
1166 break;
1167 default:
1168 err = -EOPNOTSUPP;
1169 break;
1170 }
1171 if (err)
1172 goto msg_err;
1173
1174 up_read(&vdev->cf_lock);
1175 genlmsg_end(msg, hdr);
1176 return 0;
1177
1178 msg_err:
1179 genlmsg_cancel(msg, hdr);
1180 out:
1181 up_read(&vdev->cf_lock);
1182 return err;
1183 }
1184
vdpa_fill_stats_rec(struct vdpa_device * vdev,struct sk_buff * msg,struct genl_info * info,u32 index)1185 static int vdpa_fill_stats_rec(struct vdpa_device *vdev, struct sk_buff *msg,
1186 struct genl_info *info, u32 index)
1187 {
1188 struct virtio_net_config config = {};
1189 u64 features;
1190 u8 status;
1191 int err;
1192
1193 status = vdev->config->get_status(vdev);
1194 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
1195 NL_SET_ERR_MSG_MOD(info->extack, "feature negotiation not complete");
1196 return -EAGAIN;
1197 }
1198 vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
1199
1200 features = vdev->config->get_driver_features(vdev);
1201 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_NEGOTIATED_FEATURES,
1202 features, VDPA_ATTR_PAD))
1203 return -EMSGSIZE;
1204
1205 err = vdpa_dev_net_mq_config_fill(msg, features, &config);
1206 if (err)
1207 return err;
1208
1209 if (nla_put_u32(msg, VDPA_ATTR_DEV_QUEUE_INDEX, index))
1210 return -EMSGSIZE;
1211
1212 err = vdev->config->get_vendor_vq_stats(vdev, index, msg, info->extack);
1213 if (err)
1214 return err;
1215
1216 return 0;
1217 }
1218
vendor_stats_fill(struct vdpa_device * vdev,struct sk_buff * msg,struct genl_info * info,u32 index)1219 static int vendor_stats_fill(struct vdpa_device *vdev, struct sk_buff *msg,
1220 struct genl_info *info, u32 index)
1221 {
1222 int err;
1223
1224 down_read(&vdev->cf_lock);
1225 if (!vdev->config->get_vendor_vq_stats) {
1226 err = -EOPNOTSUPP;
1227 goto out;
1228 }
1229
1230 err = vdpa_fill_stats_rec(vdev, msg, info, index);
1231 out:
1232 up_read(&vdev->cf_lock);
1233 return err;
1234 }
1235
vdpa_dev_vendor_stats_fill(struct vdpa_device * vdev,struct sk_buff * msg,struct genl_info * info,u32 index)1236 static int vdpa_dev_vendor_stats_fill(struct vdpa_device *vdev,
1237 struct sk_buff *msg,
1238 struct genl_info *info, u32 index)
1239 {
1240 u32 device_id;
1241 void *hdr;
1242 int err;
1243 u32 portid = info->snd_portid;
1244 u32 seq = info->snd_seq;
1245 u32 flags = 0;
1246
1247 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags,
1248 VDPA_CMD_DEV_VSTATS_GET);
1249 if (!hdr)
1250 return -EMSGSIZE;
1251
1252 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev))) {
1253 err = -EMSGSIZE;
1254 goto undo_msg;
1255 }
1256
1257 device_id = vdev->config->get_device_id(vdev);
1258 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id)) {
1259 err = -EMSGSIZE;
1260 goto undo_msg;
1261 }
1262
1263 switch (device_id) {
1264 case VIRTIO_ID_NET:
1265 if (index > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX) {
1266 NL_SET_ERR_MSG_MOD(info->extack, "queue index exceeds max value");
1267 err = -ERANGE;
1268 break;
1269 }
1270
1271 err = vendor_stats_fill(vdev, msg, info, index);
1272 break;
1273 default:
1274 err = -EOPNOTSUPP;
1275 break;
1276 }
1277 genlmsg_end(msg, hdr);
1278
1279 return err;
1280
1281 undo_msg:
1282 genlmsg_cancel(msg, hdr);
1283 return err;
1284 }
1285
vdpa_nl_cmd_dev_config_get_doit(struct sk_buff * skb,struct genl_info * info)1286 static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info *info)
1287 {
1288 struct vdpa_device *vdev;
1289 struct sk_buff *msg;
1290 const char *devname;
1291 struct device *dev;
1292 int err;
1293
1294 if (!info->attrs[VDPA_ATTR_DEV_NAME])
1295 return -EINVAL;
1296 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
1297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1298 if (!msg)
1299 return -ENOMEM;
1300
1301 down_read(&vdpa_dev_lock);
1302 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
1303 if (!dev) {
1304 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
1305 err = -ENODEV;
1306 goto dev_err;
1307 }
1308 vdev = container_of(dev, struct vdpa_device, dev);
1309 if (!vdev->mdev) {
1310 NL_SET_ERR_MSG_MOD(info->extack, "unmanaged vdpa device");
1311 err = -EINVAL;
1312 goto mdev_err;
1313 }
1314 err = vdpa_dev_config_fill(vdev, msg, info->snd_portid, info->snd_seq,
1315 0, info->extack);
1316 if (!err)
1317 err = genlmsg_reply(msg, info);
1318
1319 mdev_err:
1320 put_device(dev);
1321 dev_err:
1322 up_read(&vdpa_dev_lock);
1323 if (err)
1324 nlmsg_free(msg);
1325 return err;
1326 }
1327
vdpa_dev_net_device_attr_set(struct vdpa_device * vdev,struct genl_info * info)1328 static int vdpa_dev_net_device_attr_set(struct vdpa_device *vdev,
1329 struct genl_info *info)
1330 {
1331 struct vdpa_dev_set_config set_config = {};
1332 struct vdpa_mgmt_dev *mdev = vdev->mdev;
1333 struct nlattr **nl_attrs = info->attrs;
1334 const u8 *macaddr;
1335 int err = -EOPNOTSUPP;
1336
1337 down_write(&vdev->cf_lock);
1338 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]) {
1339 set_config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
1340 macaddr = nla_data(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]);
1341
1342 if (is_valid_ether_addr(macaddr)) {
1343 ether_addr_copy(set_config.net.mac, macaddr);
1344 if (mdev->ops->dev_set_attr) {
1345 err = mdev->ops->dev_set_attr(mdev, vdev,
1346 &set_config);
1347 } else {
1348 NL_SET_ERR_MSG_FMT_MOD(info->extack,
1349 "Operation not supported by the device.");
1350 }
1351 } else {
1352 NL_SET_ERR_MSG_FMT_MOD(info->extack,
1353 "Invalid MAC address");
1354 }
1355 }
1356 up_write(&vdev->cf_lock);
1357 return err;
1358 }
1359
vdpa_nl_cmd_dev_attr_set_doit(struct sk_buff * skb,struct genl_info * info)1360 static int vdpa_nl_cmd_dev_attr_set_doit(struct sk_buff *skb,
1361 struct genl_info *info)
1362 {
1363 struct vdpa_device *vdev;
1364 struct device *dev;
1365 const char *name;
1366 u64 classes;
1367 int err = 0;
1368
1369 if (!info->attrs[VDPA_ATTR_DEV_NAME])
1370 return -EINVAL;
1371
1372 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
1373
1374 down_write(&vdpa_dev_lock);
1375 dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
1376 if (!dev) {
1377 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
1378 err = -ENODEV;
1379 goto dev_err;
1380 }
1381 vdev = container_of(dev, struct vdpa_device, dev);
1382 if (!vdev->mdev) {
1383 NL_SET_ERR_MSG_MOD(info->extack, "unmanaged vdpa device");
1384 err = -EINVAL;
1385 goto mdev_err;
1386 }
1387 classes = vdpa_mgmtdev_get_classes(vdev->mdev, NULL);
1388 if (classes & BIT_ULL(VIRTIO_ID_NET)) {
1389 err = vdpa_dev_net_device_attr_set(vdev, info);
1390 } else {
1391 NL_SET_ERR_MSG_FMT_MOD(info->extack, "%s device not supported",
1392 name);
1393 }
1394
1395 mdev_err:
1396 put_device(dev);
1397 dev_err:
1398 up_write(&vdpa_dev_lock);
1399 return err;
1400 }
1401
vdpa_dev_config_dump(struct device * dev,void * data)1402 static int vdpa_dev_config_dump(struct device *dev, void *data)
1403 {
1404 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
1405 struct vdpa_dev_dump_info *info = data;
1406 int err;
1407
1408 if (!vdev->mdev)
1409 return 0;
1410 if (info->idx < info->start_idx) {
1411 info->idx++;
1412 return 0;
1413 }
1414 err = vdpa_dev_config_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
1415 info->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1416 info->cb->extack);
1417 if (err)
1418 return err;
1419
1420 info->idx++;
1421 return 0;
1422 }
1423
1424 static int
vdpa_nl_cmd_dev_config_get_dumpit(struct sk_buff * msg,struct netlink_callback * cb)1425 vdpa_nl_cmd_dev_config_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
1426 {
1427 struct vdpa_dev_dump_info info;
1428
1429 info.msg = msg;
1430 info.cb = cb;
1431 info.start_idx = cb->args[0];
1432 info.idx = 0;
1433
1434 down_read(&vdpa_dev_lock);
1435 bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_config_dump);
1436 up_read(&vdpa_dev_lock);
1437 cb->args[0] = info.idx;
1438 return msg->len;
1439 }
1440
vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff * skb,struct genl_info * info)1441 static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb,
1442 struct genl_info *info)
1443 {
1444 struct vdpa_device *vdev;
1445 struct sk_buff *msg;
1446 const char *devname;
1447 struct device *dev;
1448 u32 index;
1449 int err;
1450
1451 if (!info->attrs[VDPA_ATTR_DEV_NAME])
1452 return -EINVAL;
1453
1454 if (!info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX])
1455 return -EINVAL;
1456
1457 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
1458 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1459 if (!msg)
1460 return -ENOMEM;
1461
1462 index = nla_get_u32(info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX]);
1463 down_read(&vdpa_dev_lock);
1464 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
1465 if (!dev) {
1466 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
1467 err = -ENODEV;
1468 goto dev_err;
1469 }
1470 vdev = container_of(dev, struct vdpa_device, dev);
1471 if (!vdev->mdev) {
1472 NL_SET_ERR_MSG_MOD(info->extack, "unmanaged vdpa device");
1473 err = -EINVAL;
1474 goto mdev_err;
1475 }
1476 err = vdpa_dev_vendor_stats_fill(vdev, msg, info, index);
1477 if (err)
1478 goto mdev_err;
1479
1480 err = genlmsg_reply(msg, info);
1481
1482 put_device(dev);
1483 up_read(&vdpa_dev_lock);
1484
1485 return err;
1486
1487 mdev_err:
1488 put_device(dev);
1489 dev_err:
1490 nlmsg_free(msg);
1491 up_read(&vdpa_dev_lock);
1492 return err;
1493 }
1494
1495 static const struct nla_policy vdpa_nl_policy[VDPA_ATTR_MAX + 1] = {
1496 [VDPA_ATTR_MGMTDEV_BUS_NAME] = { .type = NLA_NUL_STRING },
1497 [VDPA_ATTR_MGMTDEV_DEV_NAME] = { .type = NLA_STRING },
1498 [VDPA_ATTR_DEV_NAME] = { .type = NLA_STRING },
1499 [VDPA_ATTR_DEV_NET_CFG_MACADDR] = NLA_POLICY_ETH_ADDR,
1500 [VDPA_ATTR_DEV_NET_CFG_MAX_VQP] = { .type = NLA_U16 },
1501 /* virtio spec 1.1 section 5.1.4.1 for valid MTU range */
1502 [VDPA_ATTR_DEV_NET_CFG_MTU] = NLA_POLICY_MIN(NLA_U16, 68),
1503 [VDPA_ATTR_DEV_QUEUE_INDEX] = { .type = NLA_U32 },
1504 [VDPA_ATTR_DEV_FEATURES] = { .type = NLA_U64 },
1505 };
1506
1507 static const struct genl_ops vdpa_nl_ops[] = {
1508 {
1509 .cmd = VDPA_CMD_MGMTDEV_GET,
1510 .doit = vdpa_nl_cmd_mgmtdev_get_doit,
1511 .dumpit = vdpa_nl_cmd_mgmtdev_get_dumpit,
1512 },
1513 {
1514 .cmd = VDPA_CMD_DEV_NEW,
1515 .doit = vdpa_nl_cmd_dev_add_set_doit,
1516 .flags = GENL_ADMIN_PERM,
1517 },
1518 {
1519 .cmd = VDPA_CMD_DEV_DEL,
1520 .doit = vdpa_nl_cmd_dev_del_set_doit,
1521 .flags = GENL_ADMIN_PERM,
1522 },
1523 {
1524 .cmd = VDPA_CMD_DEV_GET,
1525 .doit = vdpa_nl_cmd_dev_get_doit,
1526 .dumpit = vdpa_nl_cmd_dev_get_dumpit,
1527 },
1528 {
1529 .cmd = VDPA_CMD_DEV_CONFIG_GET,
1530 .doit = vdpa_nl_cmd_dev_config_get_doit,
1531 .dumpit = vdpa_nl_cmd_dev_config_get_dumpit,
1532 },
1533 {
1534 .cmd = VDPA_CMD_DEV_VSTATS_GET,
1535 .doit = vdpa_nl_cmd_dev_stats_get_doit,
1536 .flags = GENL_ADMIN_PERM,
1537 },
1538 {
1539 .cmd = VDPA_CMD_DEV_ATTR_SET,
1540 .doit = vdpa_nl_cmd_dev_attr_set_doit,
1541 .flags = GENL_ADMIN_PERM,
1542 },
1543 };
1544
1545 static struct genl_family vdpa_nl_family __ro_after_init = {
1546 .name = VDPA_GENL_NAME,
1547 .version = VDPA_GENL_VERSION,
1548 .maxattr = VDPA_ATTR_MAX,
1549 .policy = vdpa_nl_policy,
1550 .netnsok = false,
1551 .module = THIS_MODULE,
1552 .ops = vdpa_nl_ops,
1553 .n_ops = ARRAY_SIZE(vdpa_nl_ops),
1554 .resv_start_op = VDPA_CMD_DEV_VSTATS_GET + 1,
1555 };
1556
vdpa_init(void)1557 static int vdpa_init(void)
1558 {
1559 int err;
1560
1561 err = bus_register(&vdpa_bus);
1562 if (err)
1563 return err;
1564 err = genl_register_family(&vdpa_nl_family);
1565 if (err)
1566 goto err;
1567 return 0;
1568
1569 err:
1570 bus_unregister(&vdpa_bus);
1571 return err;
1572 }
1573
vdpa_exit(void)1574 static void __exit vdpa_exit(void)
1575 {
1576 genl_unregister_family(&vdpa_nl_family);
1577 bus_unregister(&vdpa_bus);
1578 ida_destroy(&vdpa_index_ida);
1579 }
1580 core_initcall(vdpa_init);
1581 module_exit(vdpa_exit);
1582
1583 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
1584 MODULE_DESCRIPTION("vDPA bus");
1585 MODULE_LICENSE("GPL v2");
1586