1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2020, Red Hat. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
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/mod_devicetable.h>
17 #include <linux/virtio_ids.h>
19 static LIST_HEAD(mdev_head
);
20 /* A global mutex that protects vdpa management device and device level operations. */
21 static DECLARE_RWSEM(vdpa_dev_lock
);
22 static DEFINE_IDA(vdpa_index_ida
);
24 void vdpa_set_status(struct vdpa_device
*vdev
, u8 status
)
26 down_write(&vdev
->cf_lock
);
27 vdev
->config
->set_status(vdev
, status
);
28 up_write(&vdev
->cf_lock
);
30 EXPORT_SYMBOL(vdpa_set_status
);
32 static struct genl_family vdpa_nl_family
;
34 static int vdpa_dev_probe(struct device
*d
)
36 struct vdpa_device
*vdev
= dev_to_vdpa(d
);
37 struct vdpa_driver
*drv
= drv_to_vdpa(vdev
->dev
.driver
);
38 const struct vdpa_config_ops
*ops
= vdev
->config
;
39 u32 max_num
, min_num
= 1;
42 d
->dma_mask
= &d
->coherent_dma_mask
;
43 ret
= dma_set_mask_and_coherent(d
, DMA_BIT_MASK(64));
47 max_num
= ops
->get_vq_num_max(vdev
);
48 if (ops
->get_vq_num_min
)
49 min_num
= ops
->get_vq_num_min(vdev
);
50 if (max_num
< min_num
)
53 if (drv
&& drv
->probe
)
54 ret
= drv
->probe(vdev
);
59 static void vdpa_dev_remove(struct device
*d
)
61 struct vdpa_device
*vdev
= dev_to_vdpa(d
);
62 struct vdpa_driver
*drv
= drv_to_vdpa(vdev
->dev
.driver
);
64 if (drv
&& drv
->remove
)
68 static int vdpa_dev_match(struct device
*dev
, const struct device_driver
*drv
)
70 struct vdpa_device
*vdev
= dev_to_vdpa(dev
);
72 /* Check override first, and if set, only use the named driver */
73 if (vdev
->driver_override
)
74 return strcmp(vdev
->driver_override
, drv
->name
) == 0;
76 /* Currently devices must be supported by all vDPA bus drivers */
80 static ssize_t
driver_override_store(struct device
*dev
,
81 struct device_attribute
*attr
,
82 const char *buf
, size_t count
)
84 struct vdpa_device
*vdev
= dev_to_vdpa(dev
);
87 ret
= driver_set_override(dev
, &vdev
->driver_override
, buf
, count
);
94 static ssize_t
driver_override_show(struct device
*dev
,
95 struct device_attribute
*attr
, char *buf
)
97 struct vdpa_device
*vdev
= dev_to_vdpa(dev
);
101 len
= sysfs_emit(buf
, "%s\n", vdev
->driver_override
);
106 static DEVICE_ATTR_RW(driver_override
);
108 static struct attribute
*vdpa_dev_attrs
[] = {
109 &dev_attr_driver_override
.attr
,
113 static const struct attribute_group vdpa_dev_group
= {
114 .attrs
= vdpa_dev_attrs
,
116 __ATTRIBUTE_GROUPS(vdpa_dev
);
118 static const struct bus_type vdpa_bus
= {
120 .dev_groups
= vdpa_dev_groups
,
121 .match
= vdpa_dev_match
,
122 .probe
= vdpa_dev_probe
,
123 .remove
= vdpa_dev_remove
,
126 static void vdpa_release_dev(struct device
*d
)
128 struct vdpa_device
*vdev
= dev_to_vdpa(d
);
129 const struct vdpa_config_ops
*ops
= vdev
->config
;
134 ida_free(&vdpa_index_ida
, vdev
->index
);
135 kfree(vdev
->driver_override
);
140 * __vdpa_alloc_device - allocate and initilaize a vDPA device
141 * This allows driver to some prepartion after device is
142 * initialized but before registered.
143 * @parent: the parent device
144 * @config: the bus operations that is supported by this device
145 * @ngroups: number of groups supported by this device
146 * @nas: number of address spaces supported by this device
147 * @size: size of the parent structure that contains private data
148 * @name: name of the vdpa device; optional.
149 * @use_va: indicate whether virtual address must be used by this device
151 * Driver should use vdpa_alloc_device() wrapper macro instead of
152 * using this directly.
154 * Return: Returns an error when parent/config/dma_dev is not set or fail to get
157 struct vdpa_device
*__vdpa_alloc_device(struct device
*parent
,
158 const struct vdpa_config_ops
*config
,
159 unsigned int ngroups
, unsigned int nas
,
160 size_t size
, const char *name
,
163 struct vdpa_device
*vdev
;
169 if (!!config
->dma_map
!= !!config
->dma_unmap
)
172 /* It should only work for the device that use on-chip IOMMU */
173 if (use_va
&& !(config
->dma_map
|| config
->set_map
))
177 vdev
= kzalloc(size
, GFP_KERNEL
);
181 err
= ida_alloc(&vdpa_index_ida
, GFP_KERNEL
);
185 vdev
->dev
.bus
= &vdpa_bus
;
186 vdev
->dev
.parent
= parent
;
187 vdev
->dev
.release
= vdpa_release_dev
;
189 vdev
->config
= config
;
190 vdev
->features_valid
= false;
191 vdev
->use_va
= use_va
;
192 vdev
->ngroups
= ngroups
;
196 err
= dev_set_name(&vdev
->dev
, "%s", name
);
198 err
= dev_set_name(&vdev
->dev
, "vdpa%u", vdev
->index
);
202 init_rwsem(&vdev
->cf_lock
);
203 device_initialize(&vdev
->dev
);
208 ida_free(&vdpa_index_ida
, vdev
->index
);
214 EXPORT_SYMBOL_GPL(__vdpa_alloc_device
);
216 static int vdpa_name_match(struct device
*dev
, const void *data
)
218 struct vdpa_device
*vdev
= container_of(dev
, struct vdpa_device
, dev
);
220 return (strcmp(dev_name(&vdev
->dev
), data
) == 0);
223 static int __vdpa_register_device(struct vdpa_device
*vdev
, u32 nvqs
)
229 lockdep_assert_held(&vdpa_dev_lock
);
230 dev
= bus_find_device(&vdpa_bus
, NULL
, dev_name(&vdev
->dev
), vdpa_name_match
);
235 return device_add(&vdev
->dev
);
239 * _vdpa_register_device - register a vDPA device with vdpa lock held
240 * Caller must have a succeed call of vdpa_alloc_device() before.
241 * Caller must invoke this routine in the management device dev_add()
242 * callback after setting up valid mgmtdev for this vdpa device.
243 * @vdev: the vdpa device to be registered to vDPA bus
244 * @nvqs: number of virtqueues supported by this device
246 * Return: Returns an error when fail to add device to vDPA bus
248 int _vdpa_register_device(struct vdpa_device
*vdev
, u32 nvqs
)
253 return __vdpa_register_device(vdev
, nvqs
);
255 EXPORT_SYMBOL_GPL(_vdpa_register_device
);
258 * vdpa_register_device - register a vDPA device
259 * Callers must have a succeed call of vdpa_alloc_device() before.
260 * @vdev: the vdpa device to be registered to vDPA bus
261 * @nvqs: number of virtqueues supported by this device
263 * Return: Returns an error when fail to add to vDPA bus
265 int vdpa_register_device(struct vdpa_device
*vdev
, u32 nvqs
)
269 down_write(&vdpa_dev_lock
);
270 err
= __vdpa_register_device(vdev
, nvqs
);
271 up_write(&vdpa_dev_lock
);
274 EXPORT_SYMBOL_GPL(vdpa_register_device
);
277 * _vdpa_unregister_device - unregister a vDPA device
278 * Caller must invoke this routine as part of management device dev_del()
280 * @vdev: the vdpa device to be unregisted from vDPA bus
282 void _vdpa_unregister_device(struct vdpa_device
*vdev
)
284 lockdep_assert_held(&vdpa_dev_lock
);
285 WARN_ON(!vdev
->mdev
);
286 device_unregister(&vdev
->dev
);
288 EXPORT_SYMBOL_GPL(_vdpa_unregister_device
);
291 * vdpa_unregister_device - unregister a vDPA device
292 * @vdev: the vdpa device to be unregisted from vDPA bus
294 void vdpa_unregister_device(struct vdpa_device
*vdev
)
296 down_write(&vdpa_dev_lock
);
297 device_unregister(&vdev
->dev
);
298 up_write(&vdpa_dev_lock
);
300 EXPORT_SYMBOL_GPL(vdpa_unregister_device
);
303 * __vdpa_register_driver - register a vDPA device driver
304 * @drv: the vdpa device driver to be registered
305 * @owner: module owner of the driver
307 * Return: Returns an err when fail to do the registration
309 int __vdpa_register_driver(struct vdpa_driver
*drv
, struct module
*owner
)
311 drv
->driver
.bus
= &vdpa_bus
;
312 drv
->driver
.owner
= owner
;
314 return driver_register(&drv
->driver
);
316 EXPORT_SYMBOL_GPL(__vdpa_register_driver
);
319 * vdpa_unregister_driver - unregister a vDPA device driver
320 * @drv: the vdpa device driver to be unregistered
322 void vdpa_unregister_driver(struct vdpa_driver
*drv
)
324 driver_unregister(&drv
->driver
);
326 EXPORT_SYMBOL_GPL(vdpa_unregister_driver
);
329 * vdpa_mgmtdev_register - register a vdpa management device
331 * @mdev: Pointer to vdpa management device
332 * vdpa_mgmtdev_register() register a vdpa management device which supports
333 * vdpa device management.
334 * Return: Returns 0 on success or failure when required callback ops are not
337 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev
*mdev
)
339 if (!mdev
->device
|| !mdev
->ops
|| !mdev
->ops
->dev_add
|| !mdev
->ops
->dev_del
)
342 INIT_LIST_HEAD(&mdev
->list
);
343 down_write(&vdpa_dev_lock
);
344 list_add_tail(&mdev
->list
, &mdev_head
);
345 up_write(&vdpa_dev_lock
);
348 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register
);
350 static int vdpa_match_remove(struct device
*dev
, void *data
)
352 struct vdpa_device
*vdev
= container_of(dev
, struct vdpa_device
, dev
);
353 struct vdpa_mgmt_dev
*mdev
= vdev
->mdev
;
356 mdev
->ops
->dev_del(mdev
, vdev
);
360 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev
*mdev
)
362 down_write(&vdpa_dev_lock
);
364 list_del(&mdev
->list
);
366 /* Filter out all the entries belong to this management device and delete it. */
367 bus_for_each_dev(&vdpa_bus
, NULL
, mdev
, vdpa_match_remove
);
369 up_write(&vdpa_dev_lock
);
371 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister
);
373 static void vdpa_get_config_unlocked(struct vdpa_device
*vdev
,
375 void *buf
, unsigned int len
)
377 const struct vdpa_config_ops
*ops
= vdev
->config
;
380 * Config accesses aren't supposed to trigger before features are set.
381 * If it does happen we assume a legacy guest.
383 if (!vdev
->features_valid
)
384 vdpa_set_features_unlocked(vdev
, 0);
385 ops
->get_config(vdev
, offset
, buf
, len
);
389 * vdpa_get_config - Get one or more device configuration fields.
390 * @vdev: vdpa device to operate on
391 * @offset: starting byte offset of the field
392 * @buf: buffer pointer to read to
393 * @len: length of the configuration fields in bytes
395 void vdpa_get_config(struct vdpa_device
*vdev
, unsigned int offset
,
396 void *buf
, unsigned int len
)
398 down_read(&vdev
->cf_lock
);
399 vdpa_get_config_unlocked(vdev
, offset
, buf
, len
);
400 up_read(&vdev
->cf_lock
);
402 EXPORT_SYMBOL_GPL(vdpa_get_config
);
405 * vdpa_set_config - Set one or more device configuration fields.
406 * @vdev: vdpa device to operate on
407 * @offset: starting byte offset of the field
408 * @buf: buffer pointer to read from
409 * @length: length of the configuration fields in bytes
411 void vdpa_set_config(struct vdpa_device
*vdev
, unsigned int offset
,
412 const void *buf
, unsigned int length
)
414 down_write(&vdev
->cf_lock
);
415 vdev
->config
->set_config(vdev
, offset
, buf
, length
);
416 up_write(&vdev
->cf_lock
);
418 EXPORT_SYMBOL_GPL(vdpa_set_config
);
420 static bool mgmtdev_handle_match(const struct vdpa_mgmt_dev
*mdev
,
421 const char *busname
, const char *devname
)
423 /* Bus name is optional for simulated management device, so ignore the
424 * device with bus if bus attribute is provided.
426 if ((busname
&& !mdev
->device
->bus
) || (!busname
&& mdev
->device
->bus
))
429 if (!busname
&& strcmp(dev_name(mdev
->device
), devname
) == 0)
432 if (busname
&& (strcmp(mdev
->device
->bus
->name
, busname
) == 0) &&
433 (strcmp(dev_name(mdev
->device
), devname
) == 0))
439 static struct vdpa_mgmt_dev
*vdpa_mgmtdev_get_from_attr(struct nlattr
**attrs
)
441 struct vdpa_mgmt_dev
*mdev
;
442 const char *busname
= NULL
;
445 if (!attrs
[VDPA_ATTR_MGMTDEV_DEV_NAME
])
446 return ERR_PTR(-EINVAL
);
447 devname
= nla_data(attrs
[VDPA_ATTR_MGMTDEV_DEV_NAME
]);
448 if (attrs
[VDPA_ATTR_MGMTDEV_BUS_NAME
])
449 busname
= nla_data(attrs
[VDPA_ATTR_MGMTDEV_BUS_NAME
]);
451 list_for_each_entry(mdev
, &mdev_head
, list
) {
452 if (mgmtdev_handle_match(mdev
, busname
, devname
))
455 return ERR_PTR(-ENODEV
);
458 static int vdpa_nl_mgmtdev_handle_fill(struct sk_buff
*msg
, const struct vdpa_mgmt_dev
*mdev
)
460 if (mdev
->device
->bus
&&
461 nla_put_string(msg
, VDPA_ATTR_MGMTDEV_BUS_NAME
, mdev
->device
->bus
->name
))
463 if (nla_put_string(msg
, VDPA_ATTR_MGMTDEV_DEV_NAME
, dev_name(mdev
->device
)))
468 static u64
vdpa_mgmtdev_get_classes(const struct vdpa_mgmt_dev
*mdev
,
469 unsigned int *nclasses
)
471 u64 supported_classes
= 0;
474 for (int i
= 0; mdev
->id_table
[i
].device
; i
++) {
475 if (mdev
->id_table
[i
].device
> 63)
477 supported_classes
|= BIT_ULL(mdev
->id_table
[i
].device
);
483 return supported_classes
;
486 static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev
*mdev
, struct sk_buff
*msg
,
487 u32 portid
, u32 seq
, int flags
)
492 hdr
= genlmsg_put(msg
, portid
, seq
, &vdpa_nl_family
, flags
, VDPA_CMD_MGMTDEV_NEW
);
495 err
= vdpa_nl_mgmtdev_handle_fill(msg
, mdev
);
499 if (nla_put_u64_64bit(msg
, VDPA_ATTR_MGMTDEV_SUPPORTED_CLASSES
,
500 vdpa_mgmtdev_get_classes(mdev
, NULL
),
505 if (nla_put_u32(msg
, VDPA_ATTR_DEV_MGMTDEV_MAX_VQS
,
506 mdev
->max_supported_vqs
)) {
510 if (nla_put_u64_64bit(msg
, VDPA_ATTR_DEV_SUPPORTED_FEATURES
,
511 mdev
->supported_features
, VDPA_ATTR_PAD
)) {
516 genlmsg_end(msg
, hdr
);
520 genlmsg_cancel(msg
, hdr
);
524 static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
526 struct vdpa_mgmt_dev
*mdev
;
530 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
534 down_read(&vdpa_dev_lock
);
535 mdev
= vdpa_mgmtdev_get_from_attr(info
->attrs
);
537 up_read(&vdpa_dev_lock
);
538 NL_SET_ERR_MSG_MOD(info
->extack
, "Fail to find the specified mgmt device");
543 err
= vdpa_mgmtdev_fill(mdev
, msg
, info
->snd_portid
, info
->snd_seq
, 0);
544 up_read(&vdpa_dev_lock
);
547 err
= genlmsg_reply(msg
, info
);
556 vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff
*msg
, struct netlink_callback
*cb
)
558 struct vdpa_mgmt_dev
*mdev
;
559 int start
= cb
->args
[0];
563 down_read(&vdpa_dev_lock
);
564 list_for_each_entry(mdev
, &mdev_head
, list
) {
569 err
= vdpa_mgmtdev_fill(mdev
, msg
, NETLINK_CB(cb
->skb
).portid
,
570 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
);
576 up_read(&vdpa_dev_lock
);
581 #define VDPA_DEV_NET_ATTRS_MASK (BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR) | \
582 BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU) | \
583 BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP))
586 * Bitmask for all per-device features: feature bits VIRTIO_TRANSPORT_F_START
587 * through VIRTIO_TRANSPORT_F_END are unset, i.e. 0xfffffc000fffffff for
588 * all 64bit features. If the features are extended beyond 64 bits, or new
589 * "holes" are reserved for other type of features than per-device, this
590 * macro would have to be updated.
592 #define VIRTIO_DEVICE_F_MASK (~0ULL << (VIRTIO_TRANSPORT_F_END + 1) | \
593 ((1ULL << VIRTIO_TRANSPORT_F_START) - 1))
595 static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff
*skb
, struct genl_info
*info
)
597 struct vdpa_dev_set_config config
= {};
598 struct nlattr
**nl_attrs
= info
->attrs
;
599 struct vdpa_mgmt_dev
*mdev
;
600 unsigned int ncls
= 0;
606 if (!info
->attrs
[VDPA_ATTR_DEV_NAME
])
609 name
= nla_data(info
->attrs
[VDPA_ATTR_DEV_NAME
]);
611 if (nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MACADDR
]) {
612 macaddr
= nla_data(nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MACADDR
]);
613 memcpy(config
.net
.mac
, macaddr
, sizeof(config
.net
.mac
));
614 config
.mask
|= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR
);
616 if (nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MTU
]) {
618 nla_get_u16(nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MTU
]);
619 config
.mask
|= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU
);
621 if (nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MAX_VQP
]) {
622 config
.net
.max_vq_pairs
=
623 nla_get_u16(nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MAX_VQP
]);
624 if (!config
.net
.max_vq_pairs
) {
625 NL_SET_ERR_MSG_MOD(info
->extack
,
626 "At least one pair of VQs is required");
629 config
.mask
|= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP
);
631 if (nl_attrs
[VDPA_ATTR_DEV_FEATURES
]) {
632 u64 missing
= 0x0ULL
;
634 config
.device_features
=
635 nla_get_u64(nl_attrs
[VDPA_ATTR_DEV_FEATURES
]);
636 if (nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MACADDR
] &&
637 !(config
.device_features
& BIT_ULL(VIRTIO_NET_F_MAC
)))
638 missing
|= BIT_ULL(VIRTIO_NET_F_MAC
);
639 if (nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MTU
] &&
640 !(config
.device_features
& BIT_ULL(VIRTIO_NET_F_MTU
)))
641 missing
|= BIT_ULL(VIRTIO_NET_F_MTU
);
642 if (nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MAX_VQP
] &&
643 config
.net
.max_vq_pairs
> 1 &&
644 !(config
.device_features
& BIT_ULL(VIRTIO_NET_F_MQ
)))
645 missing
|= BIT_ULL(VIRTIO_NET_F_MQ
);
647 NL_SET_ERR_MSG_FMT_MOD(info
->extack
,
648 "Missing features 0x%llx for provided attributes",
652 config
.mask
|= BIT_ULL(VDPA_ATTR_DEV_FEATURES
);
655 /* Skip checking capability if user didn't prefer to configure any
656 * device networking attributes. It is likely that user might have used
657 * a device specific method to configure such attributes or using device
658 * default attributes.
660 if ((config
.mask
& VDPA_DEV_NET_ATTRS_MASK
) &&
661 !netlink_capable(skb
, CAP_NET_ADMIN
))
664 down_write(&vdpa_dev_lock
);
665 mdev
= vdpa_mgmtdev_get_from_attr(info
->attrs
);
667 NL_SET_ERR_MSG_MOD(info
->extack
, "Fail to find the specified management device");
672 if ((config
.mask
& mdev
->config_attr_mask
) != config
.mask
) {
673 NL_SET_ERR_MSG_FMT_MOD(info
->extack
,
674 "Some provided attributes are not supported: 0x%llx",
675 config
.mask
& ~mdev
->config_attr_mask
);
680 classes
= vdpa_mgmtdev_get_classes(mdev
, &ncls
);
681 if (config
.mask
& VDPA_DEV_NET_ATTRS_MASK
&&
682 !(classes
& BIT_ULL(VIRTIO_ID_NET
))) {
683 NL_SET_ERR_MSG_MOD(info
->extack
,
684 "Network class attributes provided on unsupported management device");
688 if (!(config
.mask
& VDPA_DEV_NET_ATTRS_MASK
) &&
689 config
.mask
& BIT_ULL(VDPA_ATTR_DEV_FEATURES
) &&
690 classes
& BIT_ULL(VIRTIO_ID_NET
) && ncls
> 1 &&
691 config
.device_features
& VIRTIO_DEVICE_F_MASK
) {
692 NL_SET_ERR_MSG_MOD(info
->extack
,
693 "Management device supports multi-class while device features specified are ambiguous");
698 err
= mdev
->ops
->dev_add(mdev
, name
, &config
);
700 up_write(&vdpa_dev_lock
);
704 static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff
*skb
, struct genl_info
*info
)
706 struct vdpa_mgmt_dev
*mdev
;
707 struct vdpa_device
*vdev
;
712 if (!info
->attrs
[VDPA_ATTR_DEV_NAME
])
714 name
= nla_data(info
->attrs
[VDPA_ATTR_DEV_NAME
]);
716 down_write(&vdpa_dev_lock
);
717 dev
= bus_find_device(&vdpa_bus
, NULL
, name
, vdpa_name_match
);
719 NL_SET_ERR_MSG_MOD(info
->extack
, "device not found");
723 vdev
= container_of(dev
, struct vdpa_device
, dev
);
725 NL_SET_ERR_MSG_MOD(info
->extack
, "Only user created device can be deleted by user");
730 mdev
->ops
->dev_del(mdev
, vdev
);
734 up_write(&vdpa_dev_lock
);
739 vdpa_dev_fill(struct vdpa_device
*vdev
, struct sk_buff
*msg
, u32 portid
, u32 seq
,
740 int flags
, struct netlink_ext_ack
*extack
)
749 hdr
= genlmsg_put(msg
, portid
, seq
, &vdpa_nl_family
, flags
, VDPA_CMD_DEV_NEW
);
753 err
= vdpa_nl_mgmtdev_handle_fill(msg
, vdev
->mdev
);
757 device_id
= vdev
->config
->get_device_id(vdev
);
758 vendor_id
= vdev
->config
->get_vendor_id(vdev
);
759 max_vq_size
= vdev
->config
->get_vq_num_max(vdev
);
760 if (vdev
->config
->get_vq_num_min
)
761 min_vq_size
= vdev
->config
->get_vq_num_min(vdev
);
764 if (nla_put_string(msg
, VDPA_ATTR_DEV_NAME
, dev_name(&vdev
->dev
)))
766 if (nla_put_u32(msg
, VDPA_ATTR_DEV_ID
, device_id
))
768 if (nla_put_u32(msg
, VDPA_ATTR_DEV_VENDOR_ID
, vendor_id
))
770 if (nla_put_u32(msg
, VDPA_ATTR_DEV_MAX_VQS
, vdev
->nvqs
))
772 if (nla_put_u16(msg
, VDPA_ATTR_DEV_MAX_VQ_SIZE
, max_vq_size
))
774 if (nla_put_u16(msg
, VDPA_ATTR_DEV_MIN_VQ_SIZE
, min_vq_size
))
777 genlmsg_end(msg
, hdr
);
781 genlmsg_cancel(msg
, hdr
);
785 static int vdpa_nl_cmd_dev_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
787 struct vdpa_device
*vdev
;
793 if (!info
->attrs
[VDPA_ATTR_DEV_NAME
])
795 devname
= nla_data(info
->attrs
[VDPA_ATTR_DEV_NAME
]);
796 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
800 down_read(&vdpa_dev_lock
);
801 dev
= bus_find_device(&vdpa_bus
, NULL
, devname
, vdpa_name_match
);
803 NL_SET_ERR_MSG_MOD(info
->extack
, "device not found");
807 vdev
= container_of(dev
, struct vdpa_device
, dev
);
812 err
= vdpa_dev_fill(vdev
, msg
, info
->snd_portid
, info
->snd_seq
, 0, info
->extack
);
816 err
= genlmsg_reply(msg
, info
);
818 up_read(&vdpa_dev_lock
);
824 up_read(&vdpa_dev_lock
);
829 struct vdpa_dev_dump_info
{
831 struct netlink_callback
*cb
;
836 static int vdpa_dev_dump(struct device
*dev
, void *data
)
838 struct vdpa_device
*vdev
= container_of(dev
, struct vdpa_device
, dev
);
839 struct vdpa_dev_dump_info
*info
= data
;
844 if (info
->idx
< info
->start_idx
) {
848 err
= vdpa_dev_fill(vdev
, info
->msg
, NETLINK_CB(info
->cb
->skb
).portid
,
849 info
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, info
->cb
->extack
);
857 static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff
*msg
, struct netlink_callback
*cb
)
859 struct vdpa_dev_dump_info info
;
863 info
.start_idx
= cb
->args
[0];
866 down_read(&vdpa_dev_lock
);
867 bus_for_each_dev(&vdpa_bus
, NULL
, &info
, vdpa_dev_dump
);
868 up_read(&vdpa_dev_lock
);
869 cb
->args
[0] = info
.idx
;
873 static int vdpa_dev_net_mq_config_fill(struct sk_buff
*msg
, u64 features
,
874 const struct virtio_net_config
*config
)
878 if ((features
& BIT_ULL(VIRTIO_NET_F_MQ
)) == 0 &&
879 (features
& BIT_ULL(VIRTIO_NET_F_RSS
)) == 0)
882 val_u16
= __virtio16_to_cpu(true, config
->max_virtqueue_pairs
);
884 return nla_put_u16(msg
, VDPA_ATTR_DEV_NET_CFG_MAX_VQP
, val_u16
);
887 static int vdpa_dev_net_mtu_config_fill(struct sk_buff
*msg
, u64 features
,
888 const struct virtio_net_config
*config
)
892 if ((features
& BIT_ULL(VIRTIO_NET_F_MTU
)) == 0)
895 val_u16
= __virtio16_to_cpu(true, config
->mtu
);
897 return nla_put_u16(msg
, VDPA_ATTR_DEV_NET_CFG_MTU
, val_u16
);
900 static int vdpa_dev_net_mac_config_fill(struct sk_buff
*msg
, u64 features
,
901 const struct virtio_net_config
*config
)
903 if ((features
& BIT_ULL(VIRTIO_NET_F_MAC
)) == 0)
906 return nla_put(msg
, VDPA_ATTR_DEV_NET_CFG_MACADDR
,
907 sizeof(config
->mac
), config
->mac
);
910 static int vdpa_dev_net_status_config_fill(struct sk_buff
*msg
, u64 features
,
911 const struct virtio_net_config
*config
)
915 if ((features
& BIT_ULL(VIRTIO_NET_F_STATUS
)) == 0)
918 val_u16
= __virtio16_to_cpu(true, config
->status
);
919 return nla_put_u16(msg
, VDPA_ATTR_DEV_NET_STATUS
, val_u16
);
922 static int vdpa_dev_net_config_fill(struct vdpa_device
*vdev
, struct sk_buff
*msg
)
924 struct virtio_net_config config
= {};
927 vdev
->config
->get_config(vdev
, 0, &config
, sizeof(config
));
929 features_device
= vdev
->config
->get_device_features(vdev
);
931 if (nla_put_u64_64bit(msg
, VDPA_ATTR_DEV_FEATURES
, features_device
,
935 if (vdpa_dev_net_mtu_config_fill(msg
, features_device
, &config
))
938 if (vdpa_dev_net_mac_config_fill(msg
, features_device
, &config
))
941 if (vdpa_dev_net_status_config_fill(msg
, features_device
, &config
))
944 return vdpa_dev_net_mq_config_fill(msg
, features_device
, &config
);
948 vdpa_dev_blk_capacity_config_fill(struct sk_buff
*msg
,
949 const struct virtio_blk_config
*config
)
953 val_u64
= __virtio64_to_cpu(true, config
->capacity
);
955 return nla_put_u64_64bit(msg
, VDPA_ATTR_DEV_BLK_CFG_CAPACITY
,
956 val_u64
, VDPA_ATTR_PAD
);
960 vdpa_dev_blk_seg_size_config_fill(struct sk_buff
*msg
, u64 features
,
961 const struct virtio_blk_config
*config
)
965 if ((features
& BIT_ULL(VIRTIO_BLK_F_SIZE_MAX
)) == 0)
968 val_u32
= __virtio32_to_cpu(true, config
->size_max
);
970 return nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_SIZE_MAX
, val_u32
);
973 /* fill the block size*/
975 vdpa_dev_blk_block_size_config_fill(struct sk_buff
*msg
, u64 features
,
976 const struct virtio_blk_config
*config
)
980 if ((features
& BIT_ULL(VIRTIO_BLK_F_BLK_SIZE
)) == 0)
983 val_u32
= __virtio32_to_cpu(true, config
->blk_size
);
985 return nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_BLK_SIZE
, val_u32
);
989 vdpa_dev_blk_seg_max_config_fill(struct sk_buff
*msg
, u64 features
,
990 const struct virtio_blk_config
*config
)
994 if ((features
& BIT_ULL(VIRTIO_BLK_F_SEG_MAX
)) == 0)
997 val_u32
= __virtio32_to_cpu(true, config
->seg_max
);
999 return nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_SEG_MAX
, val_u32
);
1002 static int vdpa_dev_blk_mq_config_fill(struct sk_buff
*msg
, u64 features
,
1003 const struct virtio_blk_config
*config
)
1007 if ((features
& BIT_ULL(VIRTIO_BLK_F_MQ
)) == 0)
1010 val_u16
= __virtio16_to_cpu(true, config
->num_queues
);
1012 return nla_put_u16(msg
, VDPA_ATTR_DEV_BLK_CFG_NUM_QUEUES
, val_u16
);
1015 static int vdpa_dev_blk_topology_config_fill(struct sk_buff
*msg
, u64 features
,
1016 const struct virtio_blk_config
*config
)
1021 if ((features
& BIT_ULL(VIRTIO_BLK_F_TOPOLOGY
)) == 0)
1024 min_io_size
= __virtio16_to_cpu(true, config
->min_io_size
);
1025 opt_io_size
= __virtio32_to_cpu(true, config
->opt_io_size
);
1027 if (nla_put_u8(msg
, VDPA_ATTR_DEV_BLK_CFG_PHY_BLK_EXP
,
1028 config
->physical_block_exp
))
1031 if (nla_put_u8(msg
, VDPA_ATTR_DEV_BLK_CFG_ALIGN_OFFSET
,
1032 config
->alignment_offset
))
1035 if (nla_put_u16(msg
, VDPA_ATTR_DEV_BLK_CFG_MIN_IO_SIZE
, min_io_size
))
1038 if (nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_OPT_IO_SIZE
, opt_io_size
))
1044 static int vdpa_dev_blk_discard_config_fill(struct sk_buff
*msg
, u64 features
,
1045 const struct virtio_blk_config
*config
)
1049 if ((features
& BIT_ULL(VIRTIO_BLK_F_DISCARD
)) == 0)
1052 val_u32
= __virtio32_to_cpu(true, config
->max_discard_sectors
);
1053 if (nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_MAX_DISCARD_SEC
, val_u32
))
1056 val_u32
= __virtio32_to_cpu(true, config
->max_discard_seg
);
1057 if (nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_MAX_DISCARD_SEG
, val_u32
))
1060 val_u32
= __virtio32_to_cpu(true, config
->discard_sector_alignment
);
1061 if (nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_DISCARD_SEC_ALIGN
, val_u32
))
1068 vdpa_dev_blk_write_zeroes_config_fill(struct sk_buff
*msg
, u64 features
,
1069 const struct virtio_blk_config
*config
)
1073 if ((features
& BIT_ULL(VIRTIO_BLK_F_WRITE_ZEROES
)) == 0)
1076 val_u32
= __virtio32_to_cpu(true, config
->max_write_zeroes_sectors
);
1077 if (nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_MAX_WRITE_ZEROES_SEC
, val_u32
))
1080 val_u32
= __virtio32_to_cpu(true, config
->max_write_zeroes_seg
);
1081 if (nla_put_u32(msg
, VDPA_ATTR_DEV_BLK_CFG_MAX_WRITE_ZEROES_SEG
, val_u32
))
1087 static int vdpa_dev_blk_ro_config_fill(struct sk_buff
*msg
, u64 features
)
1091 ro
= ((features
& BIT_ULL(VIRTIO_BLK_F_RO
)) == 0) ? 0 : 1;
1092 if (nla_put_u8(msg
, VDPA_ATTR_DEV_BLK_READ_ONLY
, ro
))
1098 static int vdpa_dev_blk_flush_config_fill(struct sk_buff
*msg
, u64 features
)
1102 flush
= ((features
& BIT_ULL(VIRTIO_BLK_F_FLUSH
)) == 0) ? 0 : 1;
1103 if (nla_put_u8(msg
, VDPA_ATTR_DEV_BLK_FLUSH
, flush
))
1109 static int vdpa_dev_blk_config_fill(struct vdpa_device
*vdev
,
1110 struct sk_buff
*msg
)
1112 struct virtio_blk_config config
= {};
1113 u64 features_device
;
1115 vdev
->config
->get_config(vdev
, 0, &config
, sizeof(config
));
1117 features_device
= vdev
->config
->get_device_features(vdev
);
1119 if (nla_put_u64_64bit(msg
, VDPA_ATTR_DEV_FEATURES
, features_device
,
1123 if (vdpa_dev_blk_capacity_config_fill(msg
, &config
))
1126 if (vdpa_dev_blk_seg_size_config_fill(msg
, features_device
, &config
))
1129 if (vdpa_dev_blk_block_size_config_fill(msg
, features_device
, &config
))
1132 if (vdpa_dev_blk_seg_max_config_fill(msg
, features_device
, &config
))
1135 if (vdpa_dev_blk_mq_config_fill(msg
, features_device
, &config
))
1138 if (vdpa_dev_blk_topology_config_fill(msg
, features_device
, &config
))
1141 if (vdpa_dev_blk_discard_config_fill(msg
, features_device
, &config
))
1144 if (vdpa_dev_blk_write_zeroes_config_fill(msg
, features_device
, &config
))
1147 if (vdpa_dev_blk_ro_config_fill(msg
, features_device
))
1150 if (vdpa_dev_blk_flush_config_fill(msg
, features_device
))
1157 vdpa_dev_config_fill(struct vdpa_device
*vdev
, struct sk_buff
*msg
, u32 portid
, u32 seq
,
1158 int flags
, struct netlink_ext_ack
*extack
)
1160 u64 features_driver
;
1166 down_read(&vdev
->cf_lock
);
1167 hdr
= genlmsg_put(msg
, portid
, seq
, &vdpa_nl_family
, flags
,
1168 VDPA_CMD_DEV_CONFIG_GET
);
1174 if (nla_put_string(msg
, VDPA_ATTR_DEV_NAME
, dev_name(&vdev
->dev
))) {
1179 device_id
= vdev
->config
->get_device_id(vdev
);
1180 if (nla_put_u32(msg
, VDPA_ATTR_DEV_ID
, device_id
)) {
1185 /* only read driver features after the feature negotiation is done */
1186 status
= vdev
->config
->get_status(vdev
);
1187 if (status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
1188 features_driver
= vdev
->config
->get_driver_features(vdev
);
1189 if (nla_put_u64_64bit(msg
, VDPA_ATTR_DEV_NEGOTIATED_FEATURES
, features_driver
,
1196 switch (device_id
) {
1198 err
= vdpa_dev_net_config_fill(vdev
, msg
);
1200 case VIRTIO_ID_BLOCK
:
1201 err
= vdpa_dev_blk_config_fill(vdev
, msg
);
1210 up_read(&vdev
->cf_lock
);
1211 genlmsg_end(msg
, hdr
);
1215 genlmsg_cancel(msg
, hdr
);
1217 up_read(&vdev
->cf_lock
);
1221 static int vdpa_fill_stats_rec(struct vdpa_device
*vdev
, struct sk_buff
*msg
,
1222 struct genl_info
*info
, u32 index
)
1224 struct virtio_net_config config
= {};
1229 status
= vdev
->config
->get_status(vdev
);
1230 if (!(status
& VIRTIO_CONFIG_S_FEATURES_OK
)) {
1231 NL_SET_ERR_MSG_MOD(info
->extack
, "feature negotiation not complete");
1234 vdpa_get_config_unlocked(vdev
, 0, &config
, sizeof(config
));
1236 features
= vdev
->config
->get_driver_features(vdev
);
1237 if (nla_put_u64_64bit(msg
, VDPA_ATTR_DEV_NEGOTIATED_FEATURES
,
1238 features
, VDPA_ATTR_PAD
))
1241 err
= vdpa_dev_net_mq_config_fill(msg
, features
, &config
);
1245 if (nla_put_u32(msg
, VDPA_ATTR_DEV_QUEUE_INDEX
, index
))
1248 err
= vdev
->config
->get_vendor_vq_stats(vdev
, index
, msg
, info
->extack
);
1255 static int vendor_stats_fill(struct vdpa_device
*vdev
, struct sk_buff
*msg
,
1256 struct genl_info
*info
, u32 index
)
1260 down_read(&vdev
->cf_lock
);
1261 if (!vdev
->config
->get_vendor_vq_stats
) {
1266 err
= vdpa_fill_stats_rec(vdev
, msg
, info
, index
);
1268 up_read(&vdev
->cf_lock
);
1272 static int vdpa_dev_vendor_stats_fill(struct vdpa_device
*vdev
,
1273 struct sk_buff
*msg
,
1274 struct genl_info
*info
, u32 index
)
1279 u32 portid
= info
->snd_portid
;
1280 u32 seq
= info
->snd_seq
;
1283 hdr
= genlmsg_put(msg
, portid
, seq
, &vdpa_nl_family
, flags
,
1284 VDPA_CMD_DEV_VSTATS_GET
);
1288 if (nla_put_string(msg
, VDPA_ATTR_DEV_NAME
, dev_name(&vdev
->dev
))) {
1293 device_id
= vdev
->config
->get_device_id(vdev
);
1294 if (nla_put_u32(msg
, VDPA_ATTR_DEV_ID
, device_id
)) {
1299 switch (device_id
) {
1301 if (index
> VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
) {
1302 NL_SET_ERR_MSG_MOD(info
->extack
, "queue index exceeds max value");
1307 err
= vendor_stats_fill(vdev
, msg
, info
, index
);
1313 genlmsg_end(msg
, hdr
);
1318 genlmsg_cancel(msg
, hdr
);
1322 static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
1324 struct vdpa_device
*vdev
;
1325 struct sk_buff
*msg
;
1326 const char *devname
;
1330 if (!info
->attrs
[VDPA_ATTR_DEV_NAME
])
1332 devname
= nla_data(info
->attrs
[VDPA_ATTR_DEV_NAME
]);
1333 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1337 down_read(&vdpa_dev_lock
);
1338 dev
= bus_find_device(&vdpa_bus
, NULL
, devname
, vdpa_name_match
);
1340 NL_SET_ERR_MSG_MOD(info
->extack
, "device not found");
1344 vdev
= container_of(dev
, struct vdpa_device
, dev
);
1346 NL_SET_ERR_MSG_MOD(info
->extack
, "unmanaged vdpa device");
1350 err
= vdpa_dev_config_fill(vdev
, msg
, info
->snd_portid
, info
->snd_seq
,
1353 err
= genlmsg_reply(msg
, info
);
1358 up_read(&vdpa_dev_lock
);
1364 static int vdpa_dev_net_device_attr_set(struct vdpa_device
*vdev
,
1365 struct genl_info
*info
)
1367 struct vdpa_dev_set_config set_config
= {};
1368 struct vdpa_mgmt_dev
*mdev
= vdev
->mdev
;
1369 struct nlattr
**nl_attrs
= info
->attrs
;
1371 int err
= -EOPNOTSUPP
;
1373 down_write(&vdev
->cf_lock
);
1374 if (nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MACADDR
]) {
1375 set_config
.mask
|= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR
);
1376 macaddr
= nla_data(nl_attrs
[VDPA_ATTR_DEV_NET_CFG_MACADDR
]);
1378 if (is_valid_ether_addr(macaddr
)) {
1379 ether_addr_copy(set_config
.net
.mac
, macaddr
);
1380 if (mdev
->ops
->dev_set_attr
) {
1381 err
= mdev
->ops
->dev_set_attr(mdev
, vdev
,
1384 NL_SET_ERR_MSG_FMT_MOD(info
->extack
,
1385 "Operation not supported by the device.");
1388 NL_SET_ERR_MSG_FMT_MOD(info
->extack
,
1389 "Invalid MAC address");
1392 up_write(&vdev
->cf_lock
);
1396 static int vdpa_nl_cmd_dev_attr_set_doit(struct sk_buff
*skb
,
1397 struct genl_info
*info
)
1399 struct vdpa_device
*vdev
;
1405 if (!info
->attrs
[VDPA_ATTR_DEV_NAME
])
1408 name
= nla_data(info
->attrs
[VDPA_ATTR_DEV_NAME
]);
1410 down_write(&vdpa_dev_lock
);
1411 dev
= bus_find_device(&vdpa_bus
, NULL
, name
, vdpa_name_match
);
1413 NL_SET_ERR_MSG_MOD(info
->extack
, "device not found");
1417 vdev
= container_of(dev
, struct vdpa_device
, dev
);
1419 NL_SET_ERR_MSG_MOD(info
->extack
, "unmanaged vdpa device");
1423 classes
= vdpa_mgmtdev_get_classes(vdev
->mdev
, NULL
);
1424 if (classes
& BIT_ULL(VIRTIO_ID_NET
)) {
1425 err
= vdpa_dev_net_device_attr_set(vdev
, info
);
1427 NL_SET_ERR_MSG_FMT_MOD(info
->extack
, "%s device not supported",
1434 up_write(&vdpa_dev_lock
);
1438 static int vdpa_dev_config_dump(struct device
*dev
, void *data
)
1440 struct vdpa_device
*vdev
= container_of(dev
, struct vdpa_device
, dev
);
1441 struct vdpa_dev_dump_info
*info
= data
;
1446 if (info
->idx
< info
->start_idx
) {
1450 err
= vdpa_dev_config_fill(vdev
, info
->msg
, NETLINK_CB(info
->cb
->skb
).portid
,
1451 info
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1461 vdpa_nl_cmd_dev_config_get_dumpit(struct sk_buff
*msg
, struct netlink_callback
*cb
)
1463 struct vdpa_dev_dump_info info
;
1467 info
.start_idx
= cb
->args
[0];
1470 down_read(&vdpa_dev_lock
);
1471 bus_for_each_dev(&vdpa_bus
, NULL
, &info
, vdpa_dev_config_dump
);
1472 up_read(&vdpa_dev_lock
);
1473 cb
->args
[0] = info
.idx
;
1477 static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff
*skb
,
1478 struct genl_info
*info
)
1480 struct vdpa_device
*vdev
;
1481 struct sk_buff
*msg
;
1482 const char *devname
;
1487 if (!info
->attrs
[VDPA_ATTR_DEV_NAME
])
1490 if (!info
->attrs
[VDPA_ATTR_DEV_QUEUE_INDEX
])
1493 devname
= nla_data(info
->attrs
[VDPA_ATTR_DEV_NAME
]);
1494 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1498 index
= nla_get_u32(info
->attrs
[VDPA_ATTR_DEV_QUEUE_INDEX
]);
1499 down_read(&vdpa_dev_lock
);
1500 dev
= bus_find_device(&vdpa_bus
, NULL
, devname
, vdpa_name_match
);
1502 NL_SET_ERR_MSG_MOD(info
->extack
, "device not found");
1506 vdev
= container_of(dev
, struct vdpa_device
, dev
);
1508 NL_SET_ERR_MSG_MOD(info
->extack
, "unmanaged vdpa device");
1512 err
= vdpa_dev_vendor_stats_fill(vdev
, msg
, info
, index
);
1516 err
= genlmsg_reply(msg
, info
);
1519 up_read(&vdpa_dev_lock
);
1527 up_read(&vdpa_dev_lock
);
1531 static const struct nla_policy vdpa_nl_policy
[VDPA_ATTR_MAX
+ 1] = {
1532 [VDPA_ATTR_MGMTDEV_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
1533 [VDPA_ATTR_MGMTDEV_DEV_NAME
] = { .type
= NLA_STRING
},
1534 [VDPA_ATTR_DEV_NAME
] = { .type
= NLA_STRING
},
1535 [VDPA_ATTR_DEV_NET_CFG_MACADDR
] = NLA_POLICY_ETH_ADDR
,
1536 [VDPA_ATTR_DEV_NET_CFG_MAX_VQP
] = { .type
= NLA_U16
},
1537 /* virtio spec 1.1 section 5.1.4.1 for valid MTU range */
1538 [VDPA_ATTR_DEV_NET_CFG_MTU
] = NLA_POLICY_MIN(NLA_U16
, 68),
1539 [VDPA_ATTR_DEV_QUEUE_INDEX
] = { .type
= NLA_U32
},
1540 [VDPA_ATTR_DEV_FEATURES
] = { .type
= NLA_U64
},
1543 static const struct genl_ops vdpa_nl_ops
[] = {
1545 .cmd
= VDPA_CMD_MGMTDEV_GET
,
1546 .doit
= vdpa_nl_cmd_mgmtdev_get_doit
,
1547 .dumpit
= vdpa_nl_cmd_mgmtdev_get_dumpit
,
1550 .cmd
= VDPA_CMD_DEV_NEW
,
1551 .doit
= vdpa_nl_cmd_dev_add_set_doit
,
1552 .flags
= GENL_ADMIN_PERM
,
1555 .cmd
= VDPA_CMD_DEV_DEL
,
1556 .doit
= vdpa_nl_cmd_dev_del_set_doit
,
1557 .flags
= GENL_ADMIN_PERM
,
1560 .cmd
= VDPA_CMD_DEV_GET
,
1561 .doit
= vdpa_nl_cmd_dev_get_doit
,
1562 .dumpit
= vdpa_nl_cmd_dev_get_dumpit
,
1565 .cmd
= VDPA_CMD_DEV_CONFIG_GET
,
1566 .doit
= vdpa_nl_cmd_dev_config_get_doit
,
1567 .dumpit
= vdpa_nl_cmd_dev_config_get_dumpit
,
1570 .cmd
= VDPA_CMD_DEV_VSTATS_GET
,
1571 .doit
= vdpa_nl_cmd_dev_stats_get_doit
,
1572 .flags
= GENL_ADMIN_PERM
,
1575 .cmd
= VDPA_CMD_DEV_ATTR_SET
,
1576 .doit
= vdpa_nl_cmd_dev_attr_set_doit
,
1577 .flags
= GENL_ADMIN_PERM
,
1581 static struct genl_family vdpa_nl_family __ro_after_init
= {
1582 .name
= VDPA_GENL_NAME
,
1583 .version
= VDPA_GENL_VERSION
,
1584 .maxattr
= VDPA_ATTR_MAX
,
1585 .policy
= vdpa_nl_policy
,
1587 .module
= THIS_MODULE
,
1589 .n_ops
= ARRAY_SIZE(vdpa_nl_ops
),
1590 .resv_start_op
= VDPA_CMD_DEV_VSTATS_GET
+ 1,
1593 static int vdpa_init(void)
1597 err
= bus_register(&vdpa_bus
);
1600 err
= genl_register_family(&vdpa_nl_family
);
1606 bus_unregister(&vdpa_bus
);
1610 static void __exit
vdpa_exit(void)
1612 genl_unregister_family(&vdpa_nl_family
);
1613 bus_unregister(&vdpa_bus
);
1614 ida_destroy(&vdpa_index_ida
);
1616 core_initcall(vdpa_init
);
1617 module_exit(vdpa_exit
);
1619 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
1620 MODULE_DESCRIPTION("vDPA bus");
1621 MODULE_LICENSE("GPL v2");