blob: f63d358f3d933be8b2ec7a7eec0dbaec296796be [file] [log] [blame]
Rusty Russelle467cde2007-10-22 11:03:38 +10001//#define DEBUG
2#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Rusty Russelle467cde2007-10-22 11:03:38 +10004#include <linux/blkdev.h>
5#include <linux/hdreg.h>
Paul Gortmaker0c8d44f2011-07-01 15:56:05 -04006#include <linux/module.h>
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +10307#include <linux/mutex.h>
Rusty Russelle467cde2007-10-22 11:03:38 +10008#include <linux/virtio.h>
9#include <linux/virtio_blk.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020010#include <linux/scatterlist.h>
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010011#include <linux/string_helpers.h>
Liu Yuan6917f832011-04-24 02:49:26 +080012#include <scsi/scsi_cmnd.h>
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020013#include <linux/idr.h>
Jens Axboe1cf7e9c2013-11-01 10:52:52 -060014#include <linux/blk-mq.h>
15#include <linux/numa.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020016
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010017#define PART_BITS 4
Rusty Russelle467cde2007-10-22 11:03:38 +100018
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020019static int major;
20static DEFINE_IDA(vd_index_ida);
21
Jonghwan Choi2a647bf2013-05-20 10:25:39 +093022static struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010023
Rusty Russelle467cde2007-10-22 11:03:38 +100024struct virtio_blk
25{
Rusty Russelle467cde2007-10-22 11:03:38 +100026 struct virtio_device *vdev;
27 struct virtqueue *vq;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -060028 spinlock_t vq_lock;
Rusty Russelle467cde2007-10-22 11:03:38 +100029
30 /* The disk structure for the kernel. */
31 struct gendisk *disk;
32
Christoph Hellwig24d2f902014-04-15 14:14:00 -060033 /* Block layer tags. */
34 struct blk_mq_tag_set tag_set;
35
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010036 /* Process context for config space updates */
37 struct work_struct config_work;
38
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +103039 /* Lock for config space updates */
40 struct mutex config_lock;
41
42 /* enable config space updates */
43 bool config_enable;
44
Rusty Russell0864b792008-12-30 09:26:05 -060045 /* What host tells us, plus 2 for header & tailer. */
46 unsigned int sg_elems;
47
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020048 /* Ida index - used to track minor number allocations. */
49 int index;
Rusty Russelle467cde2007-10-22 11:03:38 +100050};
51
52struct virtblk_req
53{
Rusty Russelle467cde2007-10-22 11:03:38 +100054 struct request *req;
55 struct virtio_blk_outhdr out_hdr;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020056 struct virtio_scsi_inhdr in_hdr;
Rusty Russellcb38fa22008-05-02 21:50:45 -050057 u8 status;
Asias Hea98755c2012-08-08 16:07:04 +080058 struct scatterlist sg[];
Rusty Russelle467cde2007-10-22 11:03:38 +100059};
60
Asias Hea98755c2012-08-08 16:07:04 +080061static inline int virtblk_result(struct virtblk_req *vbr)
62{
63 switch (vbr->status) {
64 case VIRTIO_BLK_S_OK:
65 return 0;
66 case VIRTIO_BLK_S_UNSUPP:
67 return -ENOTTY;
68 default:
69 return -EIO;
70 }
71}
72
Paolo Bonzini8f39db92013-03-20 15:44:27 +103073static int __virtblk_add_req(struct virtqueue *vq,
Paolo Bonzini20af3cf2013-03-20 15:44:27 +103074 struct virtblk_req *vbr,
75 struct scatterlist *data_sg,
Rusty Russell0a11cc32013-03-20 15:44:27 +103076 bool have_data)
Asias Hec85a1f92012-08-08 16:07:05 +080077{
Paolo Bonzini20af3cf2013-03-20 15:44:27 +103078 struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
Paolo Bonzini8f39db92013-03-20 15:44:27 +103079 unsigned int num_out = 0, num_in = 0;
Paolo Bonzini20af3cf2013-03-20 15:44:27 +103080 int type = vbr->out_hdr.type & ~VIRTIO_BLK_T_OUT;
Paolo Bonzini8f39db92013-03-20 15:44:27 +103081
82 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
83 sgs[num_out++] = &hdr;
84
Paolo Bonzini20af3cf2013-03-20 15:44:27 +103085 /*
86 * If this is a packet command we need a couple of additional headers.
87 * Behind the normal outhdr we put a segment with the scsi command
88 * block, and before the normal inhdr we put the sense data and the
89 * inhdr with additional status information.
90 */
91 if (type == VIRTIO_BLK_T_SCSI_CMD) {
92 sg_init_one(&cmd, vbr->req->cmd, vbr->req->cmd_len);
93 sgs[num_out++] = &cmd;
94 }
95
Rusty Russell0a11cc32013-03-20 15:44:27 +103096 if (have_data) {
Paolo Bonzini8f39db92013-03-20 15:44:27 +103097 if (vbr->out_hdr.type & VIRTIO_BLK_T_OUT)
Paolo Bonzini20af3cf2013-03-20 15:44:27 +103098 sgs[num_out++] = data_sg;
Paolo Bonzini8f39db92013-03-20 15:44:27 +103099 else
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030100 sgs[num_out + num_in++] = data_sg;
101 }
102
103 if (type == VIRTIO_BLK_T_SCSI_CMD) {
104 sg_init_one(&sense, vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
105 sgs[num_out + num_in++] = &sense;
106 sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr));
107 sgs[num_out + num_in++] = &inhdr;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030108 }
109
110 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
111 sgs[num_out + num_in++] = &status;
112
113 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030114}
Asias Hec85a1f92012-08-08 16:07:05 +0800115
Christoph Hellwig5124c282014-02-10 03:24:39 -0800116static inline void virtblk_request_done(struct request *req)
Asias Hec85a1f92012-08-08 16:07:05 +0800117{
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200118 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Asias Hea98755c2012-08-08 16:07:04 +0800119 int error = virtblk_result(vbr);
120
121 if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
122 req->resid_len = vbr->in_hdr.residual;
123 req->sense_len = vbr->in_hdr.sense_len;
124 req->errors = vbr->in_hdr.errors;
125 } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
126 req->errors = (error != 0);
127 }
128
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600129 blk_mq_end_io(req, error);
Asias Hea98755c2012-08-08 16:07:04 +0800130}
131
132static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000133{
134 struct virtio_blk *vblk = vq->vdev->priv;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600135 bool req_done = false;
Rusty Russelle467cde2007-10-22 11:03:38 +1000136 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000137 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800138 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000139
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600140 spin_lock_irqsave(&vblk->vq_lock, flags);
Asias Hebb811102012-09-25 10:36:17 +0800141 do {
142 virtqueue_disable_cb(vq);
143 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
Christoph Hellwig5124c282014-02-10 03:24:39 -0800144 blk_mq_complete_request(vbr->req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600145 req_done = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000146 }
Heinz Graalfs7f03b172013-10-29 09:40:30 +1030147 if (unlikely(virtqueue_is_broken(vq)))
148 break;
Asias Hebb811102012-09-25 10:36:17 +0800149 } while (!virtqueue_enable_cb(vq));
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600150
Rusty Russelle467cde2007-10-22 11:03:38 +1000151 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800152 if (req_done)
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200153 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
Ming Lei0c29e932014-05-16 23:31:21 +0800154 spin_unlock_irqrestore(&vblk->vq_lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800155}
156
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600157static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
Rusty Russelle467cde2007-10-22 11:03:38 +1000158{
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600159 struct virtio_blk *vblk = hctx->queue->queuedata;
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200160 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600161 unsigned long flags;
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030162 unsigned int num;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600163 const bool last = (req->cmd_flags & REQ_END) != 0;
Rusty Russell5261b852014-03-13 11:23:39 +1030164 int err;
Ming Leie8edca62014-05-30 10:49:29 +0800165 bool notify = false;
Rusty Russelle467cde2007-10-22 11:03:38 +1000166
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600167 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000168
169 vbr->req = req;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900170 if (req->cmd_flags & REQ_FLUSH) {
171 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000172 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200173 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900174 } else {
175 switch (req->cmd_type) {
176 case REQ_TYPE_FS:
177 vbr->out_hdr.type = 0;
178 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
179 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
180 break;
181 case REQ_TYPE_BLOCK_PC:
182 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200183 vbr->out_hdr.sector = 0;
184 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
185 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900186 case REQ_TYPE_SPECIAL:
187 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
188 vbr->out_hdr.sector = 0;
189 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
190 break;
191 default:
192 /* We don't put anything else in the queue. */
193 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200194 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000195 }
196
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600197 num = blk_rq_map_sg(hctx->queue, vbr->req, vbr->sg);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200198 if (num) {
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030199 if (rq_data_dir(vbr->req) == WRITE)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200200 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030201 else
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200202 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
Rusty Russelle467cde2007-10-22 11:03:38 +1000203 }
204
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600205 spin_lock_irqsave(&vblk->vq_lock, flags);
Rusty Russell5261b852014-03-13 11:23:39 +1030206 err = __virtblk_add_req(vblk->vq, vbr, vbr->sg, num);
207 if (err) {
Shaohua Lif02b9ac2013-11-19 18:57:24 -0700208 virtqueue_kick(vblk->vq);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600209 blk_mq_stop_hw_queue(hctx);
Ming Lei0c29e932014-05-16 23:31:21 +0800210 spin_unlock_irqrestore(&vblk->vq_lock, flags);
Rusty Russell5261b852014-03-13 11:23:39 +1030211 /* Out of mem doesn't actually happen, since we fall back
212 * to direct descriptors */
213 if (err == -ENOMEM || err == -ENOSPC)
214 return BLK_MQ_RQ_QUEUE_BUSY;
215 return BLK_MQ_RQ_QUEUE_ERROR;
Asias Hea98755c2012-08-08 16:07:04 +0800216 }
217
Ming Leie8edca62014-05-30 10:49:29 +0800218 if (last && virtqueue_kick_prepare(vblk->vq))
219 notify = true;
Shaohua Lif02b9ac2013-11-19 18:57:24 -0700220 spin_unlock_irqrestore(&vblk->vq_lock, flags);
Ming Leie8edca62014-05-30 10:49:29 +0800221
222 if (notify)
223 virtqueue_notify(vblk->vq);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600224 return BLK_MQ_RQ_QUEUE_OK;
Asias Hea98755c2012-08-08 16:07:04 +0800225}
226
john cooper4cb2ea22010-03-25 01:33:33 -0400227/* return id (s/n) string for *disk to *id_str
228 */
229static int virtblk_get_id(struct gendisk *disk, char *id_str)
230{
231 struct virtio_blk *vblk = disk->private_data;
232 struct request *req;
233 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030234 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400235
236 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
237 GFP_KERNEL);
238 if (IS_ERR(bio))
239 return PTR_ERR(bio);
240
241 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
242 if (IS_ERR(req)) {
243 bio_put(bio);
244 return PTR_ERR(req);
245 }
246
247 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030248 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
249 blk_put_request(req);
250
251 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400252}
253
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200254static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
255 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000256{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200257 struct gendisk *disk = bdev->bd_disk;
258 struct virtio_blk *vblk = disk->private_data;
259
260 /*
261 * Only allow the generic SCSI ioctls if the host can support it.
262 */
263 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200264 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200265
Paolo Bonzini577ebb32012-01-12 16:01:27 +0100266 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
267 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000268}
269
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100270/* We provide getgeo only to please some old bootloader/partitioning tools */
271static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
272{
Ryan Harper48e40432008-04-16 13:56:37 -0500273 struct virtio_blk *vblk = bd->bd_disk->private_data;
Ryan Harper48e40432008-04-16 13:56:37 -0500274
275 /* see if the host passed in geometry config */
Rusty Russell855e0c52013-10-14 18:11:51 +1030276 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
277 virtio_cread(vblk->vdev, struct virtio_blk_config,
278 geometry.cylinders, &geo->cylinders);
279 virtio_cread(vblk->vdev, struct virtio_blk_config,
280 geometry.heads, &geo->heads);
281 virtio_cread(vblk->vdev, struct virtio_blk_config,
282 geometry.sectors, &geo->sectors);
Ryan Harper48e40432008-04-16 13:56:37 -0500283 } else {
284 /* some standard values, similar to sd */
285 geo->heads = 1 << 6;
286 geo->sectors = 1 << 5;
287 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
288 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100289 return 0;
290}
291
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700292static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200293 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100294 .owner = THIS_MODULE,
295 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000296};
297
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100298static int index_to_minor(int index)
299{
300 return index << PART_BITS;
301}
302
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200303static int minor_to_index(int minor)
304{
305 return minor >> PART_BITS;
306}
307
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500308static ssize_t virtblk_serial_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
310{
311 struct gendisk *disk = dev_to_disk(dev);
312 int err;
313
314 /* sysfs gives us a PAGE_SIZE buffer */
315 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
316
317 buf[VIRTIO_BLK_ID_BYTES] = '\0';
318 err = virtblk_get_id(disk, buf);
319 if (!err)
320 return strlen(buf);
321
322 if (err == -EIO) /* Unsupported? Make it empty. */
323 return 0;
324
325 return err;
326}
327DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
328
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100329static void virtblk_config_changed_work(struct work_struct *work)
330{
331 struct virtio_blk *vblk =
332 container_of(work, struct virtio_blk, config_work);
333 struct virtio_device *vdev = vblk->vdev;
334 struct request_queue *q = vblk->disk->queue;
335 char cap_str_2[10], cap_str_10[10];
Milos Vyletel9d9598b2013-03-12 15:34:40 +1030336 char *envp[] = { "RESIZE=1", NULL };
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100337 u64 capacity, size;
338
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030339 mutex_lock(&vblk->config_lock);
340 if (!vblk->config_enable)
341 goto done;
342
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100343 /* Host must always specify the capacity. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030344 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100345
346 /* If capacity is too big, truncate with warning. */
347 if ((sector_t)capacity != capacity) {
348 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
349 (unsigned long long)capacity);
350 capacity = (sector_t)-1;
351 }
352
353 size = capacity * queue_logical_block_size(q);
354 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
355 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
356
357 dev_notice(&vdev->dev,
358 "new size: %llu %d-byte logical blocks (%s/%s)\n",
359 (unsigned long long)capacity,
360 queue_logical_block_size(q),
361 cap_str_10, cap_str_2);
362
363 set_capacity(vblk->disk, capacity);
Vivek Goyale9986f32012-03-29 10:09:44 +0200364 revalidate_disk(vblk->disk);
Milos Vyletel9d9598b2013-03-12 15:34:40 +1030365 kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030366done:
367 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100368}
369
370static void virtblk_config_changed(struct virtio_device *vdev)
371{
372 struct virtio_blk *vblk = vdev->priv;
373
374 queue_work(virtblk_wq, &vblk->config_work);
375}
376
Amit Shah6abd6e52011-12-22 16:58:29 +0530377static int init_vq(struct virtio_blk *vblk)
378{
379 int err = 0;
380
381 /* We expect one virtqueue, for output. */
Asias Hea98755c2012-08-08 16:07:04 +0800382 vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
Amit Shah6abd6e52011-12-22 16:58:29 +0530383 if (IS_ERR(vblk->vq))
384 err = PTR_ERR(vblk->vq);
385
386 return err;
387}
388
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800389/*
390 * Legacy naming scheme used for virtio devices. We are stuck with it for
391 * virtio blk but don't ever use it for any new driver.
392 */
393static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
394{
395 const int base = 'z' - 'a' + 1;
396 char *begin = buf + strlen(prefix);
397 char *end = buf + buflen;
398 char *p;
399 int unit;
400
401 p = end - 1;
402 *p = '\0';
403 unit = base;
404 do {
405 if (p == begin)
406 return -EINVAL;
407 *--p = 'a' + (index % unit);
408 index = (index / unit) - 1;
409 } while (index >= 0);
410
411 memmove(begin, p, end - p);
412 memcpy(buf, prefix, strlen(prefix));
413
414 return 0;
415}
416
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200417static int virtblk_get_cache_mode(struct virtio_device *vdev)
418{
419 u8 writeback;
420 int err;
421
Rusty Russell855e0c52013-10-14 18:11:51 +1030422 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
423 struct virtio_blk_config, wce,
424 &writeback);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200425 if (err)
426 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
427
428 return writeback;
429}
430
431static void virtblk_update_cache_mode(struct virtio_device *vdev)
432{
433 u8 writeback = virtblk_get_cache_mode(vdev);
434 struct virtio_blk *vblk = vdev->priv;
435
Asias Hec85a1f92012-08-08 16:07:05 +0800436 if (writeback)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200437 blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
438 else
439 blk_queue_flush(vblk->disk->queue, 0);
440
441 revalidate_disk(vblk->disk);
442}
443
444static const char *const virtblk_cache_types[] = {
445 "write through", "write back"
446};
447
448static ssize_t
449virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
450 const char *buf, size_t count)
451{
452 struct gendisk *disk = dev_to_disk(dev);
453 struct virtio_blk *vblk = disk->private_data;
454 struct virtio_device *vdev = vblk->vdev;
455 int i;
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200456
457 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
458 for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
459 if (sysfs_streq(buf, virtblk_cache_types[i]))
460 break;
461
462 if (i < 0)
463 return -EINVAL;
464
Rusty Russell855e0c52013-10-14 18:11:51 +1030465 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200466 virtblk_update_cache_mode(vdev);
467 return count;
468}
469
470static ssize_t
471virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
472 char *buf)
473{
474 struct gendisk *disk = dev_to_disk(dev);
475 struct virtio_blk *vblk = disk->private_data;
476 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
477
478 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
479 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
480}
481
482static const struct device_attribute dev_attr_cache_type_ro =
483 __ATTR(cache_type, S_IRUGO,
484 virtblk_cache_type_show, NULL);
485static const struct device_attribute dev_attr_cache_type_rw =
486 __ATTR(cache_type, S_IRUGO|S_IWUSR,
487 virtblk_cache_type_show, virtblk_cache_type_store);
488
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600489static int virtblk_init_request(void *data, struct request *rq,
490 unsigned int hctx_idx, unsigned int request_idx,
491 unsigned int numa_node)
Christoph Hellwige9b267d2014-04-15 13:59:10 -0600492{
493 struct virtio_blk *vblk = data;
494 struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
495
496 sg_init_table(vbr->sg, vblk->sg_elems);
497 return 0;
498}
499
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600500static struct blk_mq_ops virtio_mq_ops = {
501 .queue_rq = virtio_queue_rq,
502 .map_queue = blk_mq_map_queue,
Christoph Hellwig5124c282014-02-10 03:24:39 -0800503 .complete = virtblk_request_done,
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600504 .init_request = virtblk_init_request,
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600505};
506
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600507static unsigned int virtblk_queue_depth;
508module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600509
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800510static int virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000511{
512 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600513 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200514 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800515
Rusty Russelle467cde2007-10-22 11:03:38 +1000516 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600517 u32 v, blk_size, sg_elems, opt_io_size;
518 u16 min_io_size;
519 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000520
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200521 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
522 GFP_KERNEL);
523 if (err < 0)
524 goto out;
525 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100526
Rusty Russell0864b792008-12-30 09:26:05 -0600527 /* We need to know how many segments before we allocate. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030528 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
529 struct virtio_blk_config, seg_max,
530 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200531
532 /* We need at least one SG element, whatever they say. */
533 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600534 sg_elems = 1;
535
536 /* We need an extra sg elements at head and tail. */
537 sg_elems += 2;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600538 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000539 if (!vblk) {
540 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200541 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000542 }
543
Rusty Russelle467cde2007-10-22 11:03:38 +1000544 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600545 vblk->sg_elems = sg_elems;
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030546 mutex_init(&vblk->config_lock);
Asias Hea98755c2012-08-08 16:07:04 +0800547
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100548 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030549 vblk->config_enable = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000550
Amit Shah6abd6e52011-12-22 16:58:29 +0530551 err = init_vq(vblk);
552 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000553 goto out_free_vblk;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600554 spin_lock_init(&vblk->vq_lock);
Rusty Russelle467cde2007-10-22 11:03:38 +1000555
Rusty Russelle467cde2007-10-22 11:03:38 +1000556 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100557 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000558 if (!vblk->disk) {
559 err = -ENOMEM;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600560 goto out_free_vq;
Rusty Russelle467cde2007-10-22 11:03:38 +1000561 }
562
Rusty Russellfc4324b2014-03-19 17:08:24 +1030563 /* Default queue sizing is to fill the ring. */
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600564 if (!virtblk_queue_depth) {
565 virtblk_queue_depth = vblk->vq->num_free;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030566 /* ... but without indirect descs, we use 2 descs per req */
567 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600568 virtblk_queue_depth /= 2;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030569 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600570
571 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
572 vblk->tag_set.ops = &virtio_mq_ops;
573 vblk->tag_set.nr_hw_queues = 1;
574 vblk->tag_set.queue_depth = virtblk_queue_depth;
575 vblk->tag_set.numa_node = NUMA_NO_NODE;
576 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
577 vblk->tag_set.cmd_size =
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600578 sizeof(struct virtblk_req) +
579 sizeof(struct scatterlist) * sg_elems;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600580 vblk->tag_set.driver_data = vblk;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600581
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600582 err = blk_mq_alloc_tag_set(&vblk->tag_set);
583 if (err)
584 goto out_put_disk;
585
586 q = vblk->disk->queue = blk_mq_init_queue(&vblk->tag_set);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600587 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000588 err = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600589 goto out_free_tags;
Rusty Russelle467cde2007-10-22 11:03:38 +1000590 }
591
Christoph Hellwig69740c82010-02-24 14:22:25 -0600592 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900593
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800594 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100595
Rusty Russelle467cde2007-10-22 11:03:38 +1000596 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100597 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000598 vblk->disk->private_data = vblk;
599 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500600 vblk->disk->driverfs_dev = &vdev->dev;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200601 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100602
Tejun Heo02c42b72010-09-03 11:56:18 +0200603 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200604 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000605
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200606 /* If disk is read-only in the host, the guest should obey */
607 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
608 set_disk_ro(vblk->disk, 1);
609
Rusty Russella586d4f2008-02-04 23:49:56 -0500610 /* Host must always specify the capacity. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030611 virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
Rusty Russelle467cde2007-10-22 11:03:38 +1000612
613 /* If capacity is too big, truncate with warning. */
614 if ((sector_t)cap != cap) {
615 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
616 (unsigned long long)cap);
617 cap = (sector_t)-1;
618 }
619 set_capacity(vblk->disk, cap);
620
Rusty Russell0864b792008-12-30 09:26:05 -0600621 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500622 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600623
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600624 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600625 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600626
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600627 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500628 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600629
Rusty Russella586d4f2008-02-04 23:49:56 -0500630 /* Host can optionally specify maximum segment size and number of
631 * segments. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030632 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
633 struct virtio_blk_config, size_max, &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000634 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600635 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600636 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600637 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000638
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200639 /* Host can optionally specify the block size of the device */
Rusty Russell855e0c52013-10-14 18:11:51 +1030640 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
641 struct virtio_blk_config, blk_size,
642 &blk_size);
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200643 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600644 blk_queue_logical_block_size(q, blk_size);
645 else
646 blk_size = queue_logical_block_size(q);
647
648 /* Use topology information if available */
Rusty Russell855e0c52013-10-14 18:11:51 +1030649 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
650 struct virtio_blk_config, physical_block_exp,
651 &physical_block_exp);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600652 if (!err && physical_block_exp)
653 blk_queue_physical_block_size(q,
654 blk_size * (1 << physical_block_exp));
655
Rusty Russell855e0c52013-10-14 18:11:51 +1030656 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
657 struct virtio_blk_config, alignment_offset,
658 &alignment_offset);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600659 if (!err && alignment_offset)
660 blk_queue_alignment_offset(q, blk_size * alignment_offset);
661
Rusty Russell855e0c52013-10-14 18:11:51 +1030662 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
663 struct virtio_blk_config, min_io_size,
664 &min_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600665 if (!err && min_io_size)
666 blk_queue_io_min(q, blk_size * min_io_size);
667
Rusty Russell855e0c52013-10-14 18:11:51 +1030668 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
669 struct virtio_blk_config, opt_io_size,
670 &opt_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600671 if (!err && opt_io_size)
672 blk_queue_io_opt(q, blk_size * opt_io_size);
673
Rusty Russelle467cde2007-10-22 11:03:38 +1000674 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500675 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
676 if (err)
677 goto out_del_disk;
678
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200679 if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
680 err = device_create_file(disk_to_dev(vblk->disk),
681 &dev_attr_cache_type_rw);
682 else
683 err = device_create_file(disk_to_dev(vblk->disk),
684 &dev_attr_cache_type_ro);
685 if (err)
686 goto out_del_disk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000687 return 0;
688
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500689out_del_disk:
690 del_gendisk(vblk->disk);
691 blk_cleanup_queue(vblk->disk->queue);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600692out_free_tags:
693 blk_mq_free_tag_set(&vblk->tag_set);
Rusty Russelle467cde2007-10-22 11:03:38 +1000694out_put_disk:
695 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000696out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600697 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000698out_free_vblk:
699 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200700out_free_index:
701 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000702out:
703 return err;
704}
705
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800706static void virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000707{
708 struct virtio_blk *vblk = vdev->priv;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200709 int index = vblk->index;
Alexander Graff4953fe2013-01-02 15:37:17 +1030710 int refc;
Rusty Russelle467cde2007-10-22 11:03:38 +1000711
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030712 /* Prevent config work handler from accessing the device. */
713 mutex_lock(&vblk->config_lock);
714 vblk->config_enable = false;
715 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100716
Asias He02e2b122012-05-25 10:34:47 +0800717 del_gendisk(vblk->disk);
Asias He483001c2012-05-25 10:34:48 +0800718 blk_cleanup_queue(vblk->disk->queue);
Asias He02e2b122012-05-25 10:34:47 +0800719
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600720 blk_mq_free_tag_set(&vblk->tag_set);
721
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500722 /* Stop all the virtqueues. */
723 vdev->config->reset(vdev);
724
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030725 flush_work(&vblk->config_work);
726
Alexander Graff4953fe2013-01-02 15:37:17 +1030727 refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
Rusty Russelle467cde2007-10-22 11:03:38 +1000728 put_disk(vblk->disk);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600729 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000730 kfree(vblk);
Alexander Graff4953fe2013-01-02 15:37:17 +1030731
732 /* Only free device id if we don't have any users */
733 if (refc == 1)
734 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000735}
736
Aaron Lu89107002013-09-17 09:25:23 +0930737#ifdef CONFIG_PM_SLEEP
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530738static int virtblk_freeze(struct virtio_device *vdev)
739{
740 struct virtio_blk *vblk = vdev->priv;
741
742 /* Ensure we don't receive any more interrupts */
743 vdev->config->reset(vdev);
744
745 /* Prevent config work handler from accessing the device. */
746 mutex_lock(&vblk->config_lock);
747 vblk->config_enable = false;
748 mutex_unlock(&vblk->config_lock);
749
750 flush_work(&vblk->config_work);
751
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600752 blk_mq_stop_hw_queues(vblk->disk->queue);
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530753
754 vdev->config->del_vqs(vdev);
755 return 0;
756}
757
758static int virtblk_restore(struct virtio_device *vdev)
759{
760 struct virtio_blk *vblk = vdev->priv;
761 int ret;
762
763 vblk->config_enable = true;
764 ret = init_vq(vdev->priv);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600765 if (!ret)
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200766 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600767
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530768 return ret;
769}
770#endif
771
Márton Németh47483e22010-01-10 13:40:02 +0100772static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000773 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
774 { 0 },
775};
776
Rusty Russellc45a6812008-05-02 21:50:50 -0500777static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200778 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
779 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200780 VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
Rusty Russellc45a6812008-05-02 21:50:50 -0500781};
782
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800783static struct virtio_driver virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100784 .feature_table = features,
785 .feature_table_size = ARRAY_SIZE(features),
786 .driver.name = KBUILD_MODNAME,
787 .driver.owner = THIS_MODULE,
788 .id_table = id_table,
789 .probe = virtblk_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800790 .remove = virtblk_remove,
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100791 .config_changed = virtblk_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +0930792#ifdef CONFIG_PM_SLEEP
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530793 .freeze = virtblk_freeze,
794 .restore = virtblk_restore,
795#endif
Rusty Russelle467cde2007-10-22 11:03:38 +1000796};
797
798static int __init init(void)
799{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100800 int error;
801
802 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
803 if (!virtblk_wq)
804 return -ENOMEM;
805
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100806 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100807 if (major < 0) {
808 error = major;
809 goto out_destroy_workqueue;
810 }
811
812 error = register_virtio_driver(&virtio_blk);
813 if (error)
814 goto out_unregister_blkdev;
815 return 0;
816
817out_unregister_blkdev:
818 unregister_blkdev(major, "virtblk");
819out_destroy_workqueue:
820 destroy_workqueue(virtblk_wq);
821 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000822}
823
824static void __exit fini(void)
825{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100826 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000827 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100828 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000829}
830module_init(init);
831module_exit(fini);
832
833MODULE_DEVICE_TABLE(virtio, id_table);
834MODULE_DESCRIPTION("Virtio block driver");
835MODULE_LICENSE("GPL");