nd_virtio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * virtio_pmem.c: Virtio pmem Driver
  4. *
  5. * Discovers persistent memory range information
  6. * from host and provides a virtio based flushing
  7. * interface.
  8. */
  9. #include "virtio_pmem.h"
  10. #include "nd.h"
  11. /* The interrupt handler */
  12. void virtio_pmem_host_ack(struct virtqueue *vq)
  13. {
  14. struct virtio_pmem *vpmem = vq->vdev->priv;
  15. struct virtio_pmem_request *req_data, *req_buf;
  16. unsigned long flags;
  17. unsigned int len;
  18. spin_lock_irqsave(&vpmem->pmem_lock, flags);
  19. while ((req_data = virtqueue_get_buf(vq, &len)) != NULL) {
  20. req_data->done = true;
  21. wake_up(&req_data->host_acked);
  22. if (!list_empty(&vpmem->req_list)) {
  23. req_buf = list_first_entry(&vpmem->req_list,
  24. struct virtio_pmem_request, list);
  25. req_buf->wq_buf_avail = true;
  26. wake_up(&req_buf->wq_buf);
  27. list_del(&req_buf->list);
  28. }
  29. }
  30. spin_unlock_irqrestore(&vpmem->pmem_lock, flags);
  31. }
  32. EXPORT_SYMBOL_GPL(virtio_pmem_host_ack);
  33. /* The request submission function */
  34. static int virtio_pmem_flush(struct nd_region *nd_region)
  35. {
  36. struct virtio_device *vdev = nd_region->provider_data;
  37. struct virtio_pmem *vpmem = vdev->priv;
  38. struct virtio_pmem_request *req_data;
  39. struct scatterlist *sgs[2], sg, ret;
  40. unsigned long flags;
  41. int err, err1;
  42. might_sleep();
  43. req_data = kmalloc(sizeof(*req_data), GFP_KERNEL);
  44. if (!req_data)
  45. return -ENOMEM;
  46. req_data->done = false;
  47. init_waitqueue_head(&req_data->host_acked);
  48. init_waitqueue_head(&req_data->wq_buf);
  49. INIT_LIST_HEAD(&req_data->list);
  50. req_data->req.type = cpu_to_le32(VIRTIO_PMEM_REQ_TYPE_FLUSH);
  51. sg_init_one(&sg, &req_data->req, sizeof(req_data->req));
  52. sgs[0] = &sg;
  53. sg_init_one(&ret, &req_data->resp.ret, sizeof(req_data->resp));
  54. sgs[1] = &ret;
  55. spin_lock_irqsave(&vpmem->pmem_lock, flags);
  56. /*
  57. * If virtqueue_add_sgs returns -ENOSPC then req_vq virtual
  58. * queue does not have free descriptor. We add the request
  59. * to req_list and wait for host_ack to wake us up when free
  60. * slots are available.
  61. */
  62. while ((err = virtqueue_add_sgs(vpmem->req_vq, sgs, 1, 1, req_data,
  63. GFP_ATOMIC)) == -ENOSPC) {
  64. dev_info(&vdev->dev, "failed to send command to virtio pmem device, no free slots in the virtqueue\n");
  65. req_data->wq_buf_avail = false;
  66. list_add_tail(&req_data->list, &vpmem->req_list);
  67. spin_unlock_irqrestore(&vpmem->pmem_lock, flags);
  68. /* A host response results in "host_ack" getting called */
  69. wait_event(req_data->wq_buf, req_data->wq_buf_avail);
  70. spin_lock_irqsave(&vpmem->pmem_lock, flags);
  71. }
  72. err1 = virtqueue_kick(vpmem->req_vq);
  73. spin_unlock_irqrestore(&vpmem->pmem_lock, flags);
  74. /*
  75. * virtqueue_add_sgs failed with error different than -ENOSPC, we can't
  76. * do anything about that.
  77. */
  78. if (err || !err1) {
  79. dev_info(&vdev->dev, "failed to send command to virtio pmem device\n");
  80. err = -EIO;
  81. } else {
  82. /* A host repsonse results in "host_ack" getting called */
  83. wait_event(req_data->host_acked, req_data->done);
  84. err = le32_to_cpu(req_data->resp.ret);
  85. }
  86. kfree(req_data);
  87. return err;
  88. };
  89. /* The asynchronous flush callback function */
  90. int async_pmem_flush(struct nd_region *nd_region, struct bio *bio)
  91. {
  92. /*
  93. * Create child bio for asynchronous flush and chain with
  94. * parent bio. Otherwise directly call nd_region flush.
  95. */
  96. if (bio && bio->bi_iter.bi_sector != -1) {
  97. struct bio *child = bio_alloc(GFP_ATOMIC, 0);
  98. if (!child)
  99. return -ENOMEM;
  100. bio_copy_dev(child, bio);
  101. child->bi_opf = REQ_PREFLUSH;
  102. child->bi_iter.bi_sector = -1;
  103. bio_chain(child, bio);
  104. submit_bio(child);
  105. return 0;
  106. }
  107. if (virtio_pmem_flush(nd_region))
  108. return -EIO;
  109. return 0;
  110. };
  111. EXPORT_SYMBOL_GPL(async_pmem_flush);
  112. MODULE_LICENSE("GPL");