vdpa.c 41 KB

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