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