virtio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 sprintf(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 sprintf(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 sprintf(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 sprintf(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 += sprintf(buf+len, "%c",
  51. __virtio_test_bit(dev, i) ? '1' : '0');
  52. len += sprintf(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, 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(struct device *_dv, struct kobj_uevent_env *env)
  86. {
  87. 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_enabled)
  111. dev->config_change_pending = true;
  112. else if (drv && drv->config_changed)
  113. drv->config_changed(dev);
  114. }
  115. void virtio_config_changed(struct virtio_device *dev)
  116. {
  117. unsigned long flags;
  118. spin_lock_irqsave(&dev->config_lock, flags);
  119. __virtio_config_changed(dev);
  120. spin_unlock_irqrestore(&dev->config_lock, flags);
  121. }
  122. EXPORT_SYMBOL_GPL(virtio_config_changed);
  123. static void virtio_config_disable(struct virtio_device *dev)
  124. {
  125. spin_lock_irq(&dev->config_lock);
  126. dev->config_enabled = false;
  127. spin_unlock_irq(&dev->config_lock);
  128. }
  129. static void virtio_config_enable(struct virtio_device *dev)
  130. {
  131. spin_lock_irq(&dev->config_lock);
  132. dev->config_enabled = true;
  133. if (dev->config_change_pending)
  134. __virtio_config_changed(dev);
  135. dev->config_change_pending = false;
  136. spin_unlock_irq(&dev->config_lock);
  137. }
  138. void virtio_add_status(struct virtio_device *dev, unsigned int status)
  139. {
  140. might_sleep();
  141. dev->config->set_status(dev, dev->config->get_status(dev) | status);
  142. }
  143. EXPORT_SYMBOL_GPL(virtio_add_status);
  144. /* Do some validation, then set FEATURES_OK */
  145. static int virtio_features_ok(struct virtio_device *dev)
  146. {
  147. unsigned int status;
  148. might_sleep();
  149. if (virtio_check_mem_acc_cb(dev)) {
  150. if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
  151. dev_warn(&dev->dev,
  152. "device must provide VIRTIO_F_VERSION_1\n");
  153. return -ENODEV;
  154. }
  155. if (!virtio_has_feature(dev, VIRTIO_F_ACCESS_PLATFORM)) {
  156. dev_warn(&dev->dev,
  157. "device must provide VIRTIO_F_ACCESS_PLATFORM\n");
  158. return -ENODEV;
  159. }
  160. }
  161. if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
  162. return 0;
  163. virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
  164. status = dev->config->get_status(dev);
  165. if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
  166. dev_err(&dev->dev, "virtio: device refuses features: %x\n",
  167. status);
  168. return -ENODEV;
  169. }
  170. return 0;
  171. }
  172. /**
  173. * virtio_reset_device - quiesce device for removal
  174. * @dev: the device to reset
  175. *
  176. * Prevents device from sending interrupts and accessing memory.
  177. *
  178. * Generally used for cleanup during driver / device removal.
  179. *
  180. * Once this has been invoked, caller must ensure that
  181. * virtqueue_notify / virtqueue_kick are not in progress.
  182. *
  183. * Note: this guarantees that vq callbacks are not in progress, however caller
  184. * is responsible for preventing access from other contexts, such as a system
  185. * call/workqueue/bh. Invoking virtio_break_device then flushing any such
  186. * contexts is one way to handle that.
  187. * */
  188. void virtio_reset_device(struct virtio_device *dev)
  189. {
  190. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  191. /*
  192. * The below virtio_synchronize_cbs() guarantees that any
  193. * interrupt for this line arriving after
  194. * virtio_synchronize_vqs() has completed is guaranteed to see
  195. * vq->broken as true.
  196. */
  197. virtio_break_device(dev);
  198. virtio_synchronize_cbs(dev);
  199. #endif
  200. dev->config->reset(dev);
  201. }
  202. EXPORT_SYMBOL_GPL(virtio_reset_device);
  203. static int virtio_dev_probe(struct device *_d)
  204. {
  205. int err, i;
  206. struct virtio_device *dev = dev_to_virtio(_d);
  207. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  208. u64 device_features;
  209. u64 driver_features;
  210. u64 driver_features_legacy;
  211. /* We have a driver! */
  212. virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  213. /* Figure out what features the device supports. */
  214. device_features = dev->config->get_features(dev);
  215. /* Figure out what features the driver supports. */
  216. driver_features = 0;
  217. for (i = 0; i < drv->feature_table_size; i++) {
  218. unsigned int f = drv->feature_table[i];
  219. BUG_ON(f >= 64);
  220. driver_features |= (1ULL << f);
  221. }
  222. /* Some drivers have a separate feature table for virtio v1.0 */
  223. if (drv->feature_table_legacy) {
  224. driver_features_legacy = 0;
  225. for (i = 0; i < drv->feature_table_size_legacy; i++) {
  226. unsigned int f = drv->feature_table_legacy[i];
  227. BUG_ON(f >= 64);
  228. driver_features_legacy |= (1ULL << f);
  229. }
  230. } else {
  231. driver_features_legacy = driver_features;
  232. }
  233. if (device_features & (1ULL << VIRTIO_F_VERSION_1))
  234. dev->features = driver_features & device_features;
  235. else
  236. dev->features = driver_features_legacy & device_features;
  237. /* Transport features always preserved to pass to finalize_features. */
  238. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
  239. if (device_features & (1ULL << i))
  240. __virtio_set_bit(dev, i);
  241. err = dev->config->finalize_features(dev);
  242. if (err)
  243. goto err;
  244. if (drv->validate) {
  245. u64 features = dev->features;
  246. err = drv->validate(dev);
  247. if (err)
  248. goto err;
  249. /* Did validation change any features? Then write them again. */
  250. if (features != dev->features) {
  251. err = dev->config->finalize_features(dev);
  252. if (err)
  253. goto err;
  254. }
  255. }
  256. err = virtio_features_ok(dev);
  257. if (err)
  258. goto err;
  259. err = drv->probe(dev);
  260. if (err)
  261. goto err;
  262. /* If probe didn't do it, mark device DRIVER_OK ourselves. */
  263. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  264. virtio_device_ready(dev);
  265. if (drv->scan)
  266. drv->scan(dev);
  267. virtio_config_enable(dev);
  268. return 0;
  269. err:
  270. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  271. return err;
  272. }
  273. static void virtio_dev_remove(struct device *_d)
  274. {
  275. struct virtio_device *dev = dev_to_virtio(_d);
  276. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  277. virtio_config_disable(dev);
  278. drv->remove(dev);
  279. /* Driver should have reset device. */
  280. WARN_ON_ONCE(dev->config->get_status(dev));
  281. /* Acknowledge the device's existence again. */
  282. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  283. of_node_put(dev->dev.of_node);
  284. }
  285. static struct bus_type virtio_bus = {
  286. .name = "virtio",
  287. .match = virtio_dev_match,
  288. .dev_groups = virtio_dev_groups,
  289. .uevent = virtio_uevent,
  290. .probe = virtio_dev_probe,
  291. .remove = virtio_dev_remove,
  292. };
  293. int register_virtio_driver(struct virtio_driver *driver)
  294. {
  295. /* Catch this early. */
  296. BUG_ON(driver->feature_table_size && !driver->feature_table);
  297. driver->driver.bus = &virtio_bus;
  298. return driver_register(&driver->driver);
  299. }
  300. EXPORT_SYMBOL_GPL(register_virtio_driver);
  301. void unregister_virtio_driver(struct virtio_driver *driver)
  302. {
  303. driver_unregister(&driver->driver);
  304. }
  305. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  306. static int virtio_device_of_init(struct virtio_device *dev)
  307. {
  308. struct device_node *np, *pnode = dev_of_node(dev->dev.parent);
  309. char compat[] = "virtio,deviceXXXXXXXX";
  310. int ret, count;
  311. if (!pnode)
  312. return 0;
  313. count = of_get_available_child_count(pnode);
  314. if (!count)
  315. return 0;
  316. /* There can be only 1 child node */
  317. if (WARN_ON(count > 1))
  318. return -EINVAL;
  319. np = of_get_next_available_child(pnode, NULL);
  320. if (WARN_ON(!np))
  321. return -ENODEV;
  322. ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device);
  323. BUG_ON(ret >= sizeof(compat));
  324. /*
  325. * On powerpc/pseries virtio devices are PCI devices so PCI
  326. * vendor/device ids play the role of the "compatible" property.
  327. * Simply don't init of_node in this case.
  328. */
  329. if (!of_device_is_compatible(np, compat)) {
  330. ret = 0;
  331. goto out;
  332. }
  333. dev->dev.of_node = np;
  334. return 0;
  335. out:
  336. of_node_put(np);
  337. return ret;
  338. }
  339. /**
  340. * register_virtio_device - register virtio device
  341. * @dev : virtio device to be registered
  342. *
  343. * On error, the caller must call put_device on &@dev->dev (and not kfree),
  344. * as another code path may have obtained a reference to @dev.
  345. *
  346. * Returns: 0 on suceess, -error on failure
  347. */
  348. int register_virtio_device(struct virtio_device *dev)
  349. {
  350. int err;
  351. dev->dev.bus = &virtio_bus;
  352. device_initialize(&dev->dev);
  353. /* Assign a unique device index and hence name. */
  354. err = ida_alloc(&virtio_index_ida, GFP_KERNEL);
  355. if (err < 0)
  356. goto out;
  357. dev->index = err;
  358. err = dev_set_name(&dev->dev, "virtio%u", dev->index);
  359. if (err)
  360. goto out_ida_remove;
  361. err = virtio_device_of_init(dev);
  362. if (err)
  363. goto out_ida_remove;
  364. spin_lock_init(&dev->config_lock);
  365. dev->config_enabled = false;
  366. dev->config_change_pending = false;
  367. INIT_LIST_HEAD(&dev->vqs);
  368. spin_lock_init(&dev->vqs_list_lock);
  369. /* We always start by resetting the device, in case a previous
  370. * driver messed it up. This also tests that code path a little. */
  371. virtio_reset_device(dev);
  372. /* Acknowledge that we've seen the device. */
  373. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  374. /*
  375. * device_add() causes the bus infrastructure to look for a matching
  376. * driver.
  377. */
  378. err = device_add(&dev->dev);
  379. if (err)
  380. goto out_of_node_put;
  381. return 0;
  382. out_of_node_put:
  383. of_node_put(dev->dev.of_node);
  384. out_ida_remove:
  385. ida_free(&virtio_index_ida, dev->index);
  386. out:
  387. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  388. return err;
  389. }
  390. EXPORT_SYMBOL_GPL(register_virtio_device);
  391. bool is_virtio_device(struct device *dev)
  392. {
  393. return dev->bus == &virtio_bus;
  394. }
  395. EXPORT_SYMBOL_GPL(is_virtio_device);
  396. void unregister_virtio_device(struct virtio_device *dev)
  397. {
  398. int index = dev->index; /* save for after device release */
  399. device_unregister(&dev->dev);
  400. ida_free(&virtio_index_ida, index);
  401. }
  402. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  403. #ifdef CONFIG_PM_SLEEP
  404. int virtio_device_freeze(struct virtio_device *dev)
  405. {
  406. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  407. virtio_config_disable(dev);
  408. dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
  409. if (drv && drv->freeze)
  410. return drv->freeze(dev);
  411. return 0;
  412. }
  413. EXPORT_SYMBOL_GPL(virtio_device_freeze);
  414. int virtio_device_restore(struct virtio_device *dev)
  415. {
  416. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  417. int ret;
  418. /* We always start by resetting the device, in case a previous
  419. * driver messed it up. */
  420. virtio_reset_device(dev);
  421. /* Acknowledge that we've seen the device. */
  422. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  423. /* Maybe driver failed before freeze.
  424. * Restore the failed status, for debugging. */
  425. if (dev->failed)
  426. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  427. if (!drv)
  428. return 0;
  429. /* We have a driver! */
  430. virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  431. ret = dev->config->finalize_features(dev);
  432. if (ret)
  433. goto err;
  434. ret = virtio_features_ok(dev);
  435. if (ret)
  436. goto err;
  437. if (drv->restore) {
  438. ret = drv->restore(dev);
  439. if (ret)
  440. goto err;
  441. }
  442. /* If restore didn't do it, mark device DRIVER_OK ourselves. */
  443. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  444. virtio_device_ready(dev);
  445. virtio_config_enable(dev);
  446. return 0;
  447. err:
  448. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  449. return ret;
  450. }
  451. EXPORT_SYMBOL_GPL(virtio_device_restore);
  452. #endif
  453. static int virtio_init(void)
  454. {
  455. if (bus_register(&virtio_bus) != 0)
  456. panic("virtio bus registration failed");
  457. return 0;
  458. }
  459. static void __exit virtio_exit(void)
  460. {
  461. bus_unregister(&virtio_bus);
  462. ida_destroy(&virtio_index_ida);
  463. }
  464. core_initcall(virtio_init);
  465. module_exit(virtio_exit);
  466. MODULE_LICENSE("GPL");