virtio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 < VIRTIO_FEATURES_BITS; 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 disable
  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 enable
  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[VIRTIO_FEATURES_U64S];
  240. u64 driver_features[VIRTIO_FEATURES_U64S];
  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. virtio_get_features(dev, device_features);
  246. /* Figure out what features the driver supports. */
  247. virtio_features_zero(driver_features);
  248. for (i = 0; i < drv->feature_table_size; i++) {
  249. unsigned int f = drv->feature_table[i];
  250. if (!WARN_ON_ONCE(f >= VIRTIO_FEATURES_BITS))
  251. virtio_features_set_bit(driver_features, 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. if (!WARN_ON_ONCE(f >= 64))
  259. driver_features_legacy |= (1ULL << f);
  260. }
  261. } else {
  262. driver_features_legacy = driver_features[0];
  263. }
  264. if (virtio_features_test_bit(device_features, VIRTIO_F_VERSION_1)) {
  265. for (i = 0; i < VIRTIO_FEATURES_U64S; ++i)
  266. dev->features_array[i] = driver_features[i] &
  267. device_features[i];
  268. } else {
  269. virtio_features_from_u64(dev->features_array,
  270. driver_features_legacy &
  271. device_features[0]);
  272. }
  273. /* When debugging, user may filter some features by hand. */
  274. virtio_debug_device_filter_features(dev);
  275. /* Transport features always preserved to pass to finalize_features. */
  276. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
  277. if (virtio_features_test_bit(device_features, i))
  278. __virtio_set_bit(dev, i);
  279. err = dev->config->finalize_features(dev);
  280. if (err)
  281. goto err;
  282. if (drv->validate) {
  283. u64 features[VIRTIO_FEATURES_U64S];
  284. virtio_features_copy(features, dev->features_array);
  285. err = drv->validate(dev);
  286. if (err)
  287. goto err;
  288. /* Did validation change any features? Then write them again. */
  289. if (!virtio_features_equal(features, dev->features_array)) {
  290. err = dev->config->finalize_features(dev);
  291. if (err)
  292. goto err;
  293. }
  294. }
  295. err = virtio_features_ok(dev);
  296. if (err)
  297. goto err;
  298. err = drv->probe(dev);
  299. if (err)
  300. goto err;
  301. /* If probe didn't do it, mark device DRIVER_OK ourselves. */
  302. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  303. virtio_device_ready(dev);
  304. if (drv->scan)
  305. drv->scan(dev);
  306. virtio_config_core_enable(dev);
  307. return 0;
  308. err:
  309. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  310. return err;
  311. }
  312. static void virtio_dev_remove(struct device *_d)
  313. {
  314. struct virtio_device *dev = dev_to_virtio(_d);
  315. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  316. virtio_config_core_disable(dev);
  317. drv->remove(dev);
  318. /* Driver should have reset device. */
  319. WARN_ON_ONCE(dev->config->get_status(dev));
  320. /* Acknowledge the device's existence again. */
  321. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  322. of_node_put(dev->dev.of_node);
  323. }
  324. /*
  325. * virtio_irq_get_affinity - get IRQ affinity mask for device
  326. * @_d: ptr to dev structure
  327. * @irq_vec: interrupt vector number
  328. *
  329. * Return the CPU affinity mask for @_d and @irq_vec.
  330. */
  331. static const struct cpumask *virtio_irq_get_affinity(struct device *_d,
  332. unsigned int irq_vec)
  333. {
  334. struct virtio_device *dev = dev_to_virtio(_d);
  335. if (!dev->config->get_vq_affinity)
  336. return NULL;
  337. return dev->config->get_vq_affinity(dev, irq_vec);
  338. }
  339. static void virtio_dev_shutdown(struct device *_d)
  340. {
  341. struct virtio_device *dev = dev_to_virtio(_d);
  342. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  343. /*
  344. * Stop accesses to or from the device.
  345. * We only need to do it if there's a driver - no accesses otherwise.
  346. */
  347. if (!drv)
  348. return;
  349. /* If the driver has its own shutdown method, use that. */
  350. if (drv->shutdown) {
  351. drv->shutdown(dev);
  352. return;
  353. }
  354. /*
  355. * Some devices get wedged if you kick them after they are
  356. * reset. Mark all vqs as broken to make sure we don't.
  357. */
  358. virtio_break_device(dev);
  359. /*
  360. * Guarantee that any callback will see vq->broken as true.
  361. */
  362. virtio_synchronize_cbs(dev);
  363. /*
  364. * As IOMMUs are reset on shutdown, this will block device access to memory.
  365. * Some devices get wedged if this happens, so reset to make sure it does not.
  366. */
  367. dev->config->reset(dev);
  368. }
  369. static const struct bus_type virtio_bus = {
  370. .name = "virtio",
  371. .match = virtio_dev_match,
  372. .dev_groups = virtio_dev_groups,
  373. .uevent = virtio_uevent,
  374. .probe = virtio_dev_probe,
  375. .remove = virtio_dev_remove,
  376. .irq_get_affinity = virtio_irq_get_affinity,
  377. .shutdown = virtio_dev_shutdown,
  378. };
  379. int __register_virtio_driver(struct virtio_driver *driver, struct module *owner)
  380. {
  381. /* Catch this early. */
  382. BUG_ON(driver->feature_table_size && !driver->feature_table);
  383. driver->driver.bus = &virtio_bus;
  384. driver->driver.owner = owner;
  385. return driver_register(&driver->driver);
  386. }
  387. EXPORT_SYMBOL_GPL(__register_virtio_driver);
  388. void unregister_virtio_driver(struct virtio_driver *driver)
  389. {
  390. driver_unregister(&driver->driver);
  391. }
  392. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  393. static int virtio_device_of_init(struct virtio_device *dev)
  394. {
  395. struct device_node *np, *pnode = dev_of_node(dev->dev.parent);
  396. char compat[] = "virtio,deviceXXXXXXXX";
  397. int ret, count;
  398. if (!pnode)
  399. return 0;
  400. count = of_get_available_child_count(pnode);
  401. if (!count)
  402. return 0;
  403. /* There can be only 1 child node */
  404. if (WARN_ON(count > 1))
  405. return -EINVAL;
  406. np = of_get_next_available_child(pnode, NULL);
  407. if (WARN_ON(!np))
  408. return -ENODEV;
  409. ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device);
  410. BUG_ON(ret >= sizeof(compat));
  411. /*
  412. * On powerpc/pseries virtio devices are PCI devices so PCI
  413. * vendor/device ids play the role of the "compatible" property.
  414. * Simply don't init of_node in this case.
  415. */
  416. if (!of_device_is_compatible(np, compat)) {
  417. ret = 0;
  418. goto out;
  419. }
  420. dev->dev.of_node = np;
  421. return 0;
  422. out:
  423. of_node_put(np);
  424. return ret;
  425. }
  426. /**
  427. * register_virtio_device - register virtio device
  428. * @dev : virtio device to be registered
  429. *
  430. * On error, the caller must call put_device on &@dev->dev (and not kfree),
  431. * as another code path may have obtained a reference to @dev.
  432. *
  433. * Returns: 0 on success, -error on failure
  434. */
  435. int register_virtio_device(struct virtio_device *dev)
  436. {
  437. int err;
  438. dev->dev.bus = &virtio_bus;
  439. device_initialize(&dev->dev);
  440. /* Assign a unique device index and hence name. */
  441. err = ida_alloc(&virtio_index_ida, GFP_KERNEL);
  442. if (err < 0)
  443. goto out;
  444. dev->index = err;
  445. err = dev_set_name(&dev->dev, "virtio%u", dev->index);
  446. if (err)
  447. goto out_ida_remove;
  448. err = virtio_device_of_init(dev);
  449. if (err)
  450. goto out_ida_remove;
  451. spin_lock_init(&dev->config_lock);
  452. dev->config_driver_disabled = false;
  453. dev->config_core_enabled = false;
  454. dev->config_change_pending = false;
  455. INIT_LIST_HEAD(&dev->vqs);
  456. spin_lock_init(&dev->vqs_list_lock);
  457. /* We always start by resetting the device, in case a previous
  458. * driver messed it up. This also tests that code path a little. */
  459. virtio_reset_device(dev);
  460. /* Acknowledge that we've seen the device. */
  461. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  462. virtio_debug_device_init(dev);
  463. /*
  464. * device_add() causes the bus infrastructure to look for a matching
  465. * driver.
  466. */
  467. err = device_add(&dev->dev);
  468. if (err)
  469. goto out_of_node_put;
  470. return 0;
  471. out_of_node_put:
  472. of_node_put(dev->dev.of_node);
  473. out_ida_remove:
  474. ida_free(&virtio_index_ida, dev->index);
  475. out:
  476. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  477. return err;
  478. }
  479. EXPORT_SYMBOL_GPL(register_virtio_device);
  480. bool is_virtio_device(struct device *dev)
  481. {
  482. return dev->bus == &virtio_bus;
  483. }
  484. EXPORT_SYMBOL_GPL(is_virtio_device);
  485. void unregister_virtio_device(struct virtio_device *dev)
  486. {
  487. int index = dev->index; /* save for after device release */
  488. device_unregister(&dev->dev);
  489. virtio_debug_device_exit(dev);
  490. ida_free(&virtio_index_ida, index);
  491. }
  492. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  493. static int virtio_device_restore_priv(struct virtio_device *dev, bool restore)
  494. {
  495. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  496. int ret;
  497. /* We always start by resetting the device, in case a previous
  498. * driver messed it up. */
  499. virtio_reset_device(dev);
  500. /* Acknowledge that we've seen the device. */
  501. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  502. /* Maybe driver failed before freeze.
  503. * Restore the failed status, for debugging. */
  504. if (dev->failed)
  505. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  506. if (!drv)
  507. return 0;
  508. /* We have a driver! */
  509. virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  510. ret = dev->config->finalize_features(dev);
  511. if (ret)
  512. goto err;
  513. ret = virtio_features_ok(dev);
  514. if (ret)
  515. goto err;
  516. if (restore) {
  517. if (drv->restore) {
  518. ret = drv->restore(dev);
  519. if (ret)
  520. goto err;
  521. }
  522. } else {
  523. ret = drv->reset_done(dev);
  524. if (ret)
  525. goto err;
  526. }
  527. /* If restore didn't do it, mark device DRIVER_OK ourselves. */
  528. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  529. virtio_device_ready(dev);
  530. virtio_config_core_enable(dev);
  531. return 0;
  532. err:
  533. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  534. return ret;
  535. }
  536. #ifdef CONFIG_PM_SLEEP
  537. int virtio_device_freeze(struct virtio_device *dev)
  538. {
  539. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  540. int ret;
  541. virtio_config_core_disable(dev);
  542. dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
  543. if (drv && drv->freeze) {
  544. ret = drv->freeze(dev);
  545. if (ret) {
  546. virtio_config_core_enable(dev);
  547. return ret;
  548. }
  549. }
  550. return 0;
  551. }
  552. EXPORT_SYMBOL_GPL(virtio_device_freeze);
  553. int virtio_device_restore(struct virtio_device *dev)
  554. {
  555. return virtio_device_restore_priv(dev, true);
  556. }
  557. EXPORT_SYMBOL_GPL(virtio_device_restore);
  558. #endif
  559. int virtio_device_reset_prepare(struct virtio_device *dev)
  560. {
  561. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  562. int ret;
  563. if (!drv || !drv->reset_prepare)
  564. return -EOPNOTSUPP;
  565. virtio_config_core_disable(dev);
  566. dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
  567. ret = drv->reset_prepare(dev);
  568. if (ret) {
  569. virtio_config_core_enable(dev);
  570. return ret;
  571. }
  572. return 0;
  573. }
  574. EXPORT_SYMBOL_GPL(virtio_device_reset_prepare);
  575. int virtio_device_reset_done(struct virtio_device *dev)
  576. {
  577. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  578. if (!drv || !drv->reset_done)
  579. return -EOPNOTSUPP;
  580. return virtio_device_restore_priv(dev, false);
  581. }
  582. EXPORT_SYMBOL_GPL(virtio_device_reset_done);
  583. static int virtio_init(void)
  584. {
  585. BUILD_BUG_ON(offsetof(struct virtio_device, features) !=
  586. offsetof(struct virtio_device, features_array[0]));
  587. if (bus_register(&virtio_bus) != 0)
  588. panic("virtio bus registration failed");
  589. virtio_debug_init();
  590. return 0;
  591. }
  592. static void __exit virtio_exit(void)
  593. {
  594. virtio_debug_exit();
  595. bus_unregister(&virtio_bus);
  596. ida_destroy(&virtio_index_ida);
  597. }
  598. core_initcall(virtio_init);
  599. module_exit(virtio_exit);
  600. MODULE_DESCRIPTION("Virtio core interface");
  601. MODULE_LICENSE("GPL");