virtio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/virtio.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/virtio_config.h>
  5. #include <linux/virtio_anchor.h>
  6. #include <linux/module.h>
  7. #include <linux/idr.h>
  8. #include <linux/of.h>
  9. #include <uapi/linux/virtio_ids.h>
  10. /* Unique numbering for virtio devices. */
  11. static DEFINE_IDA(virtio_index_ida);
  12. static ssize_t device_show(struct device *_d,
  13. struct device_attribute *attr, char *buf)
  14. {
  15. struct virtio_device *dev = dev_to_virtio(_d);
  16. return sysfs_emit(buf, "0x%04x\n", dev->id.device);
  17. }
  18. static DEVICE_ATTR_RO(device);
  19. static ssize_t vendor_show(struct device *_d,
  20. struct device_attribute *attr, char *buf)
  21. {
  22. struct virtio_device *dev = dev_to_virtio(_d);
  23. return sysfs_emit(buf, "0x%04x\n", dev->id.vendor);
  24. }
  25. static DEVICE_ATTR_RO(vendor);
  26. static ssize_t status_show(struct device *_d,
  27. struct device_attribute *attr, char *buf)
  28. {
  29. struct virtio_device *dev = dev_to_virtio(_d);
  30. return sysfs_emit(buf, "0x%08x\n", dev->config->get_status(dev));
  31. }
  32. static DEVICE_ATTR_RO(status);
  33. static ssize_t modalias_show(struct device *_d,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct virtio_device *dev = dev_to_virtio(_d);
  37. return sysfs_emit(buf, "virtio:d%08Xv%08X\n",
  38. dev->id.device, dev->id.vendor);
  39. }
  40. static DEVICE_ATTR_RO(modalias);
  41. static ssize_t features_show(struct device *_d,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. struct virtio_device *dev = dev_to_virtio(_d);
  45. unsigned int i;
  46. ssize_t len = 0;
  47. /* We actually represent this as a bitstring, as it could be
  48. * arbitrary length in future. */
  49. for (i = 0; i < sizeof(dev->features)*8; i++)
  50. len += sysfs_emit_at(buf, len, "%c",
  51. __virtio_test_bit(dev, i) ? '1' : '0');
  52. len += sysfs_emit_at(buf, len, "\n");
  53. return len;
  54. }
  55. static DEVICE_ATTR_RO(features);
  56. static struct attribute *virtio_dev_attrs[] = {
  57. &dev_attr_device.attr,
  58. &dev_attr_vendor.attr,
  59. &dev_attr_status.attr,
  60. &dev_attr_modalias.attr,
  61. &dev_attr_features.attr,
  62. NULL,
  63. };
  64. ATTRIBUTE_GROUPS(virtio_dev);
  65. static inline int virtio_id_match(const struct virtio_device *dev,
  66. const struct virtio_device_id *id)
  67. {
  68. if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
  69. return 0;
  70. return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
  71. }
  72. /* This looks through all the IDs a driver claims to support. If any of them
  73. * match, we return 1 and the kernel will call virtio_dev_probe(). */
  74. static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
  75. {
  76. unsigned int i;
  77. struct virtio_device *dev = dev_to_virtio(_dv);
  78. const struct virtio_device_id *ids;
  79. ids = drv_to_virtio(_dr)->id_table;
  80. for (i = 0; ids[i].device; i++)
  81. if (virtio_id_match(dev, &ids[i]))
  82. return 1;
  83. return 0;
  84. }
  85. static int virtio_uevent(const struct device *_dv, struct kobj_uevent_env *env)
  86. {
  87. const struct virtio_device *dev = dev_to_virtio(_dv);
  88. return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
  89. dev->id.device, dev->id.vendor);
  90. }
  91. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  92. unsigned int fbit)
  93. {
  94. unsigned int i;
  95. struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
  96. for (i = 0; i < drv->feature_table_size; i++)
  97. if (drv->feature_table[i] == fbit)
  98. return;
  99. if (drv->feature_table_legacy) {
  100. for (i = 0; i < drv->feature_table_size_legacy; i++)
  101. if (drv->feature_table_legacy[i] == fbit)
  102. return;
  103. }
  104. BUG();
  105. }
  106. EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
  107. static void __virtio_config_changed(struct virtio_device *dev)
  108. {
  109. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  110. if (!dev->config_core_enabled || dev->config_driver_disabled)
  111. dev->config_change_pending = true;
  112. else if (drv && drv->config_changed) {
  113. drv->config_changed(dev);
  114. dev->config_change_pending = false;
  115. }
  116. }
  117. void virtio_config_changed(struct virtio_device *dev)
  118. {
  119. unsigned long flags;
  120. spin_lock_irqsave(&dev->config_lock, flags);
  121. __virtio_config_changed(dev);
  122. spin_unlock_irqrestore(&dev->config_lock, flags);
  123. }
  124. EXPORT_SYMBOL_GPL(virtio_config_changed);
  125. /**
  126. * virtio_config_driver_disable - disable config change reporting by drivers
  127. * @dev: the device to reset
  128. *
  129. * This is only allowed to be called by a driver and disabling can't
  130. * be nested.
  131. */
  132. void virtio_config_driver_disable(struct virtio_device *dev)
  133. {
  134. spin_lock_irq(&dev->config_lock);
  135. dev->config_driver_disabled = true;
  136. spin_unlock_irq(&dev->config_lock);
  137. }
  138. EXPORT_SYMBOL_GPL(virtio_config_driver_disable);
  139. /**
  140. * virtio_config_driver_enable - enable config change reporting by drivers
  141. * @dev: the device to reset
  142. *
  143. * This is only allowed to be called by a driver and enabling can't
  144. * be nested.
  145. */
  146. void virtio_config_driver_enable(struct virtio_device *dev)
  147. {
  148. spin_lock_irq(&dev->config_lock);
  149. dev->config_driver_disabled = false;
  150. if (dev->config_change_pending)
  151. __virtio_config_changed(dev);
  152. spin_unlock_irq(&dev->config_lock);
  153. }
  154. EXPORT_SYMBOL_GPL(virtio_config_driver_enable);
  155. static void virtio_config_core_disable(struct virtio_device *dev)
  156. {
  157. spin_lock_irq(&dev->config_lock);
  158. dev->config_core_enabled = false;
  159. spin_unlock_irq(&dev->config_lock);
  160. }
  161. static void virtio_config_core_enable(struct virtio_device *dev)
  162. {
  163. spin_lock_irq(&dev->config_lock);
  164. dev->config_core_enabled = true;
  165. if (dev->config_change_pending)
  166. __virtio_config_changed(dev);
  167. spin_unlock_irq(&dev->config_lock);
  168. }
  169. void virtio_add_status(struct virtio_device *dev, unsigned int status)
  170. {
  171. might_sleep();
  172. dev->config->set_status(dev, dev->config->get_status(dev) | status);
  173. }
  174. EXPORT_SYMBOL_GPL(virtio_add_status);
  175. /* Do some validation, then set FEATURES_OK */
  176. static int virtio_features_ok(struct virtio_device *dev)
  177. {
  178. unsigned int status;
  179. might_sleep();
  180. if (virtio_check_mem_acc_cb(dev)) {
  181. if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
  182. dev_warn(&dev->dev,
  183. "device must provide VIRTIO_F_VERSION_1\n");
  184. return -ENODEV;
  185. }
  186. if (!virtio_has_feature(dev, VIRTIO_F_ACCESS_PLATFORM)) {
  187. dev_warn(&dev->dev,
  188. "device must provide VIRTIO_F_ACCESS_PLATFORM\n");
  189. return -ENODEV;
  190. }
  191. }
  192. if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
  193. return 0;
  194. virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
  195. status = dev->config->get_status(dev);
  196. if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
  197. dev_err(&dev->dev, "virtio: device refuses features: %x\n",
  198. status);
  199. return -ENODEV;
  200. }
  201. return 0;
  202. }
  203. /**
  204. * virtio_reset_device - quiesce device for removal
  205. * @dev: the device to reset
  206. *
  207. * Prevents device from sending interrupts and accessing memory.
  208. *
  209. * Generally used for cleanup during driver / device removal.
  210. *
  211. * Once this has been invoked, caller must ensure that
  212. * virtqueue_notify / virtqueue_kick are not in progress.
  213. *
  214. * Note: this guarantees that vq callbacks are not in progress, however caller
  215. * is responsible for preventing access from other contexts, such as a system
  216. * call/workqueue/bh. Invoking virtio_break_device then flushing any such
  217. * contexts is one way to handle that.
  218. * */
  219. void virtio_reset_device(struct virtio_device *dev)
  220. {
  221. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  222. /*
  223. * The below virtio_synchronize_cbs() guarantees that any
  224. * interrupt for this line arriving after
  225. * virtio_synchronize_vqs() has completed is guaranteed to see
  226. * vq->broken as true.
  227. */
  228. virtio_break_device(dev);
  229. virtio_synchronize_cbs(dev);
  230. #endif
  231. dev->config->reset(dev);
  232. }
  233. EXPORT_SYMBOL_GPL(virtio_reset_device);
  234. static int virtio_dev_probe(struct device *_d)
  235. {
  236. int err, i;
  237. struct virtio_device *dev = dev_to_virtio(_d);
  238. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  239. u64 device_features;
  240. u64 driver_features;
  241. u64 driver_features_legacy;
  242. /* We have a driver! */
  243. virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  244. /* Figure out what features the device supports. */
  245. device_features = dev->config->get_features(dev);
  246. /* Figure out what features the driver supports. */
  247. driver_features = 0;
  248. for (i = 0; i < drv->feature_table_size; i++) {
  249. unsigned int f = drv->feature_table[i];
  250. BUG_ON(f >= 64);
  251. driver_features |= (1ULL << f);
  252. }
  253. /* Some drivers have a separate feature table for virtio v1.0 */
  254. if (drv->feature_table_legacy) {
  255. driver_features_legacy = 0;
  256. for (i = 0; i < drv->feature_table_size_legacy; i++) {
  257. unsigned int f = drv->feature_table_legacy[i];
  258. BUG_ON(f >= 64);
  259. driver_features_legacy |= (1ULL << f);
  260. }
  261. } else {
  262. driver_features_legacy = driver_features;
  263. }
  264. if (device_features & (1ULL << VIRTIO_F_VERSION_1))
  265. dev->features = driver_features & device_features;
  266. else
  267. dev->features = driver_features_legacy & device_features;
  268. /* When debugging, user may filter some features by hand. */
  269. virtio_debug_device_filter_features(dev);
  270. /* Transport features always preserved to pass to finalize_features. */
  271. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
  272. if (device_features & (1ULL << i))
  273. __virtio_set_bit(dev, i);
  274. err = dev->config->finalize_features(dev);
  275. if (err)
  276. goto err;
  277. if (drv->validate) {
  278. u64 features = dev->features;
  279. err = drv->validate(dev);
  280. if (err)
  281. goto err;
  282. /* Did validation change any features? Then write them again. */
  283. if (features != dev->features) {
  284. err = dev->config->finalize_features(dev);
  285. if (err)
  286. goto err;
  287. }
  288. }
  289. err = virtio_features_ok(dev);
  290. if (err)
  291. goto err;
  292. err = drv->probe(dev);
  293. if (err)
  294. goto err;
  295. /* If probe didn't do it, mark device DRIVER_OK ourselves. */
  296. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  297. virtio_device_ready(dev);
  298. if (drv->scan)
  299. drv->scan(dev);
  300. virtio_config_core_enable(dev);
  301. return 0;
  302. err:
  303. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  304. return err;
  305. }
  306. static void virtio_dev_remove(struct device *_d)
  307. {
  308. struct virtio_device *dev = dev_to_virtio(_d);
  309. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  310. virtio_config_core_disable(dev);
  311. drv->remove(dev);
  312. /* Driver should have reset device. */
  313. WARN_ON_ONCE(dev->config->get_status(dev));
  314. /* Acknowledge the device's existence again. */
  315. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  316. of_node_put(dev->dev.of_node);
  317. }
  318. static const struct bus_type virtio_bus = {
  319. .name = "virtio",
  320. .match = virtio_dev_match,
  321. .dev_groups = virtio_dev_groups,
  322. .uevent = virtio_uevent,
  323. .probe = virtio_dev_probe,
  324. .remove = virtio_dev_remove,
  325. };
  326. int __register_virtio_driver(struct virtio_driver *driver, struct module *owner)
  327. {
  328. /* Catch this early. */
  329. BUG_ON(driver->feature_table_size && !driver->feature_table);
  330. driver->driver.bus = &virtio_bus;
  331. driver->driver.owner = owner;
  332. return driver_register(&driver->driver);
  333. }
  334. EXPORT_SYMBOL_GPL(__register_virtio_driver);
  335. void unregister_virtio_driver(struct virtio_driver *driver)
  336. {
  337. driver_unregister(&driver->driver);
  338. }
  339. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  340. static int virtio_device_of_init(struct virtio_device *dev)
  341. {
  342. struct device_node *np, *pnode = dev_of_node(dev->dev.parent);
  343. char compat[] = "virtio,deviceXXXXXXXX";
  344. int ret, count;
  345. if (!pnode)
  346. return 0;
  347. count = of_get_available_child_count(pnode);
  348. if (!count)
  349. return 0;
  350. /* There can be only 1 child node */
  351. if (WARN_ON(count > 1))
  352. return -EINVAL;
  353. np = of_get_next_available_child(pnode, NULL);
  354. if (WARN_ON(!np))
  355. return -ENODEV;
  356. ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device);
  357. BUG_ON(ret >= sizeof(compat));
  358. /*
  359. * On powerpc/pseries virtio devices are PCI devices so PCI
  360. * vendor/device ids play the role of the "compatible" property.
  361. * Simply don't init of_node in this case.
  362. */
  363. if (!of_device_is_compatible(np, compat)) {
  364. ret = 0;
  365. goto out;
  366. }
  367. dev->dev.of_node = np;
  368. return 0;
  369. out:
  370. of_node_put(np);
  371. return ret;
  372. }
  373. /**
  374. * register_virtio_device - register virtio device
  375. * @dev : virtio device to be registered
  376. *
  377. * On error, the caller must call put_device on &@dev->dev (and not kfree),
  378. * as another code path may have obtained a reference to @dev.
  379. *
  380. * Returns: 0 on suceess, -error on failure
  381. */
  382. int register_virtio_device(struct virtio_device *dev)
  383. {
  384. int err;
  385. dev->dev.bus = &virtio_bus;
  386. device_initialize(&dev->dev);
  387. /* Assign a unique device index and hence name. */
  388. err = ida_alloc(&virtio_index_ida, GFP_KERNEL);
  389. if (err < 0)
  390. goto out;
  391. dev->index = err;
  392. err = dev_set_name(&dev->dev, "virtio%u", dev->index);
  393. if (err)
  394. goto out_ida_remove;
  395. err = virtio_device_of_init(dev);
  396. if (err)
  397. goto out_ida_remove;
  398. spin_lock_init(&dev->config_lock);
  399. dev->config_core_enabled = false;
  400. dev->config_change_pending = false;
  401. INIT_LIST_HEAD(&dev->vqs);
  402. spin_lock_init(&dev->vqs_list_lock);
  403. /* We always start by resetting the device, in case a previous
  404. * driver messed it up. This also tests that code path a little. */
  405. virtio_reset_device(dev);
  406. /* Acknowledge that we've seen the device. */
  407. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  408. virtio_debug_device_init(dev);
  409. /*
  410. * device_add() causes the bus infrastructure to look for a matching
  411. * driver.
  412. */
  413. err = device_add(&dev->dev);
  414. if (err)
  415. goto out_of_node_put;
  416. return 0;
  417. out_of_node_put:
  418. of_node_put(dev->dev.of_node);
  419. out_ida_remove:
  420. ida_free(&virtio_index_ida, dev->index);
  421. out:
  422. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  423. return err;
  424. }
  425. EXPORT_SYMBOL_GPL(register_virtio_device);
  426. bool is_virtio_device(struct device *dev)
  427. {
  428. return dev->bus == &virtio_bus;
  429. }
  430. EXPORT_SYMBOL_GPL(is_virtio_device);
  431. void unregister_virtio_device(struct virtio_device *dev)
  432. {
  433. int index = dev->index; /* save for after device release */
  434. device_unregister(&dev->dev);
  435. virtio_debug_device_exit(dev);
  436. ida_free(&virtio_index_ida, index);
  437. }
  438. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  439. #ifdef CONFIG_PM_SLEEP
  440. int virtio_device_freeze(struct virtio_device *dev)
  441. {
  442. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  443. int ret;
  444. virtio_config_core_disable(dev);
  445. dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
  446. if (drv && drv->freeze) {
  447. ret = drv->freeze(dev);
  448. if (ret) {
  449. virtio_config_core_enable(dev);
  450. return ret;
  451. }
  452. }
  453. return 0;
  454. }
  455. EXPORT_SYMBOL_GPL(virtio_device_freeze);
  456. int virtio_device_restore(struct virtio_device *dev)
  457. {
  458. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  459. int ret;
  460. /* We always start by resetting the device, in case a previous
  461. * driver messed it up. */
  462. virtio_reset_device(dev);
  463. /* Acknowledge that we've seen the device. */
  464. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  465. /* Maybe driver failed before freeze.
  466. * Restore the failed status, for debugging. */
  467. if (dev->failed)
  468. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  469. if (!drv)
  470. return 0;
  471. /* We have a driver! */
  472. virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  473. ret = dev->config->finalize_features(dev);
  474. if (ret)
  475. goto err;
  476. ret = virtio_features_ok(dev);
  477. if (ret)
  478. goto err;
  479. if (drv->restore) {
  480. ret = drv->restore(dev);
  481. if (ret)
  482. goto err;
  483. }
  484. /* If restore didn't do it, mark device DRIVER_OK ourselves. */
  485. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  486. virtio_device_ready(dev);
  487. virtio_config_core_enable(dev);
  488. return 0;
  489. err:
  490. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  491. return ret;
  492. }
  493. EXPORT_SYMBOL_GPL(virtio_device_restore);
  494. #endif
  495. static int virtio_init(void)
  496. {
  497. if (bus_register(&virtio_bus) != 0)
  498. panic("virtio bus registration failed");
  499. virtio_debug_init();
  500. return 0;
  501. }
  502. static void __exit virtio_exit(void)
  503. {
  504. virtio_debug_exit();
  505. bus_unregister(&virtio_bus);
  506. ida_destroy(&virtio_index_ida);
  507. }
  508. core_initcall(virtio_init);
  509. module_exit(virtio_exit);
  510. MODULE_DESCRIPTION("Virtio core interface");
  511. MODULE_LICENSE("GPL");