vdpa.c 40 KB

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