blob: b271650032fa9acd4e5317f9c9dada493d1b2692 [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. Tsirkin4678d6f92012-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 Axboe3d1266c2007-10-24 13:21:21 +020014
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010015#define PART_BITS 4
Rusty Russelle467cde2007-10-22 11:03:38 +100016
Asias Hea98755c2012-08-08 16:07:04 +080017static bool use_bio;
18module_param(use_bio, bool, S_IRUGO);
19
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020020static int major;
21static DEFINE_IDA(vd_index_ida);
22
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010023struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010024
Rusty Russelle467cde2007-10-22 11:03:38 +100025struct virtio_blk
26{
Rusty Russelle467cde2007-10-22 11:03:38 +100027 struct virtio_device *vdev;
28 struct virtqueue *vq;
Asias Hea98755c2012-08-08 16:07:04 +080029 wait_queue_head_t queue_wait;
Rusty Russelle467cde2007-10-22 11:03:38 +100030
31 /* The disk structure for the kernel. */
32 struct gendisk *disk;
33
Rusty Russelle467cde2007-10-22 11:03:38 +100034 mempool_t *pool;
35
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010036 /* Process context for config space updates */
37 struct work_struct config_work;
38
Michael S. Tsirkin4678d6f92012-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;
50
Rusty Russelle467cde2007-10-22 11:03:38 +100051 /* Scatterlist: can be too big for stack. */
Rusty Russell0864b792008-12-30 09:26:05 -060052 struct scatterlist sg[/*sg_elems*/];
Rusty Russelle467cde2007-10-22 11:03:38 +100053};
54
55struct virtblk_req
56{
Rusty Russelle467cde2007-10-22 11:03:38 +100057 struct request *req;
Asias Hea98755c2012-08-08 16:07:04 +080058 struct bio *bio;
Rusty Russelle467cde2007-10-22 11:03:38 +100059 struct virtio_blk_outhdr out_hdr;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020060 struct virtio_scsi_inhdr in_hdr;
Asias Hec85a1f92012-08-08 16:07:05 +080061 struct work_struct work;
62 struct virtio_blk *vblk;
63 int flags;
Rusty Russellcb38fa22008-05-02 21:50:45 -050064 u8 status;
Asias Hea98755c2012-08-08 16:07:04 +080065 struct scatterlist sg[];
Rusty Russelle467cde2007-10-22 11:03:38 +100066};
67
Asias Hec85a1f92012-08-08 16:07:05 +080068enum {
69 VBLK_IS_FLUSH = 1,
70 VBLK_REQ_FLUSH = 2,
71 VBLK_REQ_DATA = 4,
72 VBLK_REQ_FUA = 8,
73};
74
Asias Hea98755c2012-08-08 16:07:04 +080075static inline int virtblk_result(struct virtblk_req *vbr)
76{
77 switch (vbr->status) {
78 case VIRTIO_BLK_S_OK:
79 return 0;
80 case VIRTIO_BLK_S_UNSUPP:
81 return -ENOTTY;
82 default:
83 return -EIO;
84 }
85}
86
Asias Hec85a1f92012-08-08 16:07:05 +080087static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk,
88 gfp_t gfp_mask)
Asias Hea98755c2012-08-08 16:07:04 +080089{
Asias Hec85a1f92012-08-08 16:07:05 +080090 struct virtblk_req *vbr;
91
92 vbr = mempool_alloc(vblk->pool, gfp_mask);
Dan Carpenterf22cf8e2012-09-05 15:32:53 +030093 if (!vbr)
94 return NULL;
Asias Hec85a1f92012-08-08 16:07:05 +080095
96 vbr->vblk = vblk;
Dan Carpenterf22cf8e2012-09-05 15:32:53 +030097 if (use_bio)
98 sg_init_table(vbr->sg, vblk->sg_elems);
Asias Hec85a1f92012-08-08 16:07:05 +080099
100 return vbr;
101}
102
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030103static inline int __virtblk_add_req(struct virtqueue *vq,
104 struct virtblk_req *vbr,
105 unsigned long out,
106 unsigned long in)
Asias Hec85a1f92012-08-08 16:07:05 +0800107{
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030108 return virtqueue_add_buf(vq, vbr->sg, out, in, vbr, GFP_ATOMIC);
109}
Asias Hec85a1f92012-08-08 16:07:05 +0800110
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030111static void virtblk_add_req(struct virtblk_req *vbr,
112 unsigned int out, unsigned int in)
113{
114 struct virtio_blk *vblk = vbr->vblk;
115 DEFINE_WAIT(wait);
116 int ret;
117
118 spin_lock_irq(vblk->disk->queue->queue_lock);
119 while (unlikely((ret = __virtblk_add_req(vblk->vq, vbr,
120 out, in)) < 0)) {
Asias Hec85a1f92012-08-08 16:07:05 +0800121 prepare_to_wait_exclusive(&vblk->queue_wait, &wait,
122 TASK_UNINTERRUPTIBLE);
123
Asias Hec85a1f92012-08-08 16:07:05 +0800124 spin_unlock_irq(vblk->disk->queue->queue_lock);
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030125 io_schedule();
126 spin_lock_irq(vblk->disk->queue->queue_lock);
127
128 finish_wait(&vblk->queue_wait, &wait);
Asias Hec85a1f92012-08-08 16:07:05 +0800129 }
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030130
Asias Hec85a1f92012-08-08 16:07:05 +0800131 virtqueue_kick(vblk->vq);
132 spin_unlock_irq(vblk->disk->queue->queue_lock);
133}
134
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030135static void virtblk_bio_send_flush(struct virtblk_req *vbr)
Asias Hec85a1f92012-08-08 16:07:05 +0800136{
137 unsigned int out = 0, in = 0;
138
139 vbr->flags |= VBLK_IS_FLUSH;
140 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
141 vbr->out_hdr.sector = 0;
142 vbr->out_hdr.ioprio = 0;
143 sg_set_buf(&vbr->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
144 sg_set_buf(&vbr->sg[out + in++], &vbr->status, sizeof(vbr->status));
145
146 virtblk_add_req(vbr, out, in);
Asias Hec85a1f92012-08-08 16:07:05 +0800147}
148
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030149static void virtblk_bio_send_data(struct virtblk_req *vbr)
Asias Hec85a1f92012-08-08 16:07:05 +0800150{
151 struct virtio_blk *vblk = vbr->vblk;
152 unsigned int num, out = 0, in = 0;
153 struct bio *bio = vbr->bio;
154
155 vbr->flags &= ~VBLK_IS_FLUSH;
156 vbr->out_hdr.type = 0;
157 vbr->out_hdr.sector = bio->bi_sector;
158 vbr->out_hdr.ioprio = bio_prio(bio);
159
160 sg_set_buf(&vbr->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
161
162 num = blk_bio_map_sg(vblk->disk->queue, bio, vbr->sg + out);
163
164 sg_set_buf(&vbr->sg[num + out + in++], &vbr->status,
165 sizeof(vbr->status));
166
167 if (num) {
168 if (bio->bi_rw & REQ_WRITE) {
169 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
170 out += num;
171 } else {
172 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
173 in += num;
174 }
175 }
176
177 virtblk_add_req(vbr, out, in);
Asias Hec85a1f92012-08-08 16:07:05 +0800178}
179
180static void virtblk_bio_send_data_work(struct work_struct *work)
181{
182 struct virtblk_req *vbr;
183
184 vbr = container_of(work, struct virtblk_req, work);
185
186 virtblk_bio_send_data(vbr);
187}
188
189static void virtblk_bio_send_flush_work(struct work_struct *work)
190{
191 struct virtblk_req *vbr;
192
193 vbr = container_of(work, struct virtblk_req, work);
194
195 virtblk_bio_send_flush(vbr);
196}
197
198static inline void virtblk_request_done(struct virtblk_req *vbr)
199{
200 struct virtio_blk *vblk = vbr->vblk;
Asias Hea98755c2012-08-08 16:07:04 +0800201 struct request *req = vbr->req;
202 int error = virtblk_result(vbr);
203
204 if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
205 req->resid_len = vbr->in_hdr.residual;
206 req->sense_len = vbr->in_hdr.sense_len;
207 req->errors = vbr->in_hdr.errors;
208 } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
209 req->errors = (error != 0);
210 }
211
212 __blk_end_request_all(req, error);
213 mempool_free(vbr, vblk->pool);
214}
215
Asias Hec85a1f92012-08-08 16:07:05 +0800216static inline void virtblk_bio_flush_done(struct virtblk_req *vbr)
Asias Hea98755c2012-08-08 16:07:04 +0800217{
Asias Hec85a1f92012-08-08 16:07:05 +0800218 struct virtio_blk *vblk = vbr->vblk;
219
220 if (vbr->flags & VBLK_REQ_DATA) {
221 /* Send out the actual write data */
222 INIT_WORK(&vbr->work, virtblk_bio_send_data_work);
223 queue_work(virtblk_wq, &vbr->work);
224 } else {
225 bio_endio(vbr->bio, virtblk_result(vbr));
226 mempool_free(vbr, vblk->pool);
227 }
228}
229
230static inline void virtblk_bio_data_done(struct virtblk_req *vbr)
231{
232 struct virtio_blk *vblk = vbr->vblk;
233
234 if (unlikely(vbr->flags & VBLK_REQ_FUA)) {
235 /* Send out a flush before end the bio */
236 vbr->flags &= ~VBLK_REQ_DATA;
237 INIT_WORK(&vbr->work, virtblk_bio_send_flush_work);
238 queue_work(virtblk_wq, &vbr->work);
239 } else {
240 bio_endio(vbr->bio, virtblk_result(vbr));
241 mempool_free(vbr, vblk->pool);
242 }
243}
244
245static inline void virtblk_bio_done(struct virtblk_req *vbr)
246{
247 if (unlikely(vbr->flags & VBLK_IS_FLUSH))
248 virtblk_bio_flush_done(vbr);
249 else
250 virtblk_bio_data_done(vbr);
Asias Hea98755c2012-08-08 16:07:04 +0800251}
252
253static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000254{
255 struct virtio_blk *vblk = vq->vdev->priv;
Asias Hec85a1f92012-08-08 16:07:05 +0800256 bool bio_done = false, req_done = false;
Rusty Russelle467cde2007-10-22 11:03:38 +1000257 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000258 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800259 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000260
Asias He2c95a322012-05-25 16:03:27 +0800261 spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
Asias Hebb811102012-09-25 10:36:17 +0800262 do {
263 virtqueue_disable_cb(vq);
264 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
265 if (vbr->bio) {
266 virtblk_bio_done(vbr);
267 bio_done = true;
268 } else {
269 virtblk_request_done(vbr);
270 req_done = true;
271 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000272 }
Asias Hebb811102012-09-25 10:36:17 +0800273 } while (!virtqueue_enable_cb(vq));
Rusty Russelle467cde2007-10-22 11:03:38 +1000274 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800275 if (req_done)
276 blk_start_queue(vblk->disk->queue);
Asias He2c95a322012-05-25 16:03:27 +0800277 spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800278
279 if (bio_done)
280 wake_up(&vblk->queue_wait);
281}
282
Rusty Russelle467cde2007-10-22 11:03:38 +1000283static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
284 struct request *req)
285{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200286 unsigned long num, out = 0, in = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +1000287 struct virtblk_req *vbr;
288
Asias Hea98755c2012-08-08 16:07:04 +0800289 vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
Rusty Russelle467cde2007-10-22 11:03:38 +1000290 if (!vbr)
291 /* When another request finishes we'll try again. */
292 return false;
293
294 vbr->req = req;
Asias Hea98755c2012-08-08 16:07:04 +0800295 vbr->bio = NULL;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900296 if (req->cmd_flags & REQ_FLUSH) {
297 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000298 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200299 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900300 } else {
301 switch (req->cmd_type) {
302 case REQ_TYPE_FS:
303 vbr->out_hdr.type = 0;
304 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
305 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
306 break;
307 case REQ_TYPE_BLOCK_PC:
308 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200309 vbr->out_hdr.sector = 0;
310 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
311 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900312 case REQ_TYPE_SPECIAL:
313 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
314 vbr->out_hdr.sector = 0;
315 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
316 break;
317 default:
318 /* We don't put anything else in the queue. */
319 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200320 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000321 }
322
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200323 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
Rusty Russelle467cde2007-10-22 11:03:38 +1000324
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200325 /*
326 * If this is a packet command we need a couple of additional headers.
327 * Behind the normal outhdr we put a segment with the scsi command
328 * block, and before the normal inhdr we put the sense data and the
329 * inhdr with additional status information before the normal inhdr.
330 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200331 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200332 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
333
334 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
335
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200336 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
Liu Yuan6917f832011-04-24 02:49:26 +0800337 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200338 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
339 sizeof(vbr->in_hdr));
340 }
341
342 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
343 sizeof(vbr->status));
344
345 if (num) {
346 if (rq_data_dir(vbr->req) == WRITE) {
347 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
348 out += num;
349 } else {
350 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
351 in += num;
352 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000353 }
354
Asias Hea98755c2012-08-08 16:07:04 +0800355 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr,
356 GFP_ATOMIC) < 0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000357 mempool_free(vbr, vblk->pool);
358 return false;
359 }
360
Rusty Russelle467cde2007-10-22 11:03:38 +1000361 return true;
362}
363
Asias Hea98755c2012-08-08 16:07:04 +0800364static void virtblk_request(struct request_queue *q)
Rusty Russelle467cde2007-10-22 11:03:38 +1000365{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200366 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000367 struct request *req;
368 unsigned int issued = 0;
369
Tejun Heo9934c8c2009-05-08 11:54:16 +0900370 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600371 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000372
373 /* If this request fails, stop queue and wait for something to
374 finish to restart it. */
375 if (!do_req(q, vblk, req)) {
376 blk_stop_queue(q);
377 break;
378 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900379 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000380 issued++;
381 }
382
383 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300384 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000385}
386
Asias Hea98755c2012-08-08 16:07:04 +0800387static void virtblk_make_request(struct request_queue *q, struct bio *bio)
388{
389 struct virtio_blk *vblk = q->queuedata;
Asias Hea98755c2012-08-08 16:07:04 +0800390 struct virtblk_req *vbr;
391
392 BUG_ON(bio->bi_phys_segments + 2 > vblk->sg_elems);
Asias Hea98755c2012-08-08 16:07:04 +0800393
394 vbr = virtblk_alloc_req(vblk, GFP_NOIO);
395 if (!vbr) {
396 bio_endio(bio, -ENOMEM);
397 return;
398 }
399
400 vbr->bio = bio;
Asias Hec85a1f92012-08-08 16:07:05 +0800401 vbr->flags = 0;
402 if (bio->bi_rw & REQ_FLUSH)
403 vbr->flags |= VBLK_REQ_FLUSH;
404 if (bio->bi_rw & REQ_FUA)
405 vbr->flags |= VBLK_REQ_FUA;
406 if (bio->bi_size)
407 vbr->flags |= VBLK_REQ_DATA;
Asias Hea98755c2012-08-08 16:07:04 +0800408
Asias Hec85a1f92012-08-08 16:07:05 +0800409 if (unlikely(vbr->flags & VBLK_REQ_FLUSH))
410 virtblk_bio_send_flush(vbr);
411 else
412 virtblk_bio_send_data(vbr);
Asias Hea98755c2012-08-08 16:07:04 +0800413}
414
john cooper4cb2ea22010-03-25 01:33:33 -0400415/* return id (s/n) string for *disk to *id_str
416 */
417static int virtblk_get_id(struct gendisk *disk, char *id_str)
418{
419 struct virtio_blk *vblk = disk->private_data;
420 struct request *req;
421 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030422 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400423
424 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
425 GFP_KERNEL);
426 if (IS_ERR(bio))
427 return PTR_ERR(bio);
428
429 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
430 if (IS_ERR(req)) {
431 bio_put(bio);
432 return PTR_ERR(req);
433 }
434
435 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030436 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
437 blk_put_request(req);
438
439 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400440}
441
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200442static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
443 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000444{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200445 struct gendisk *disk = bdev->bd_disk;
446 struct virtio_blk *vblk = disk->private_data;
447
448 /*
449 * Only allow the generic SCSI ioctls if the host can support it.
450 */
451 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200452 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200453
Paolo Bonzini577ebb32012-01-12 16:01:27 +0100454 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
455 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000456}
457
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100458/* We provide getgeo only to please some old bootloader/partitioning tools */
459static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
460{
Ryan Harper48e40432008-04-16 13:56:37 -0500461 struct virtio_blk *vblk = bd->bd_disk->private_data;
462 struct virtio_blk_geometry vgeo;
463 int err;
464
465 /* see if the host passed in geometry config */
466 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
467 offsetof(struct virtio_blk_config, geometry),
468 &vgeo);
469
470 if (!err) {
471 geo->heads = vgeo.heads;
472 geo->sectors = vgeo.sectors;
473 geo->cylinders = vgeo.cylinders;
474 } else {
475 /* some standard values, similar to sd */
476 geo->heads = 1 << 6;
477 geo->sectors = 1 << 5;
478 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
479 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100480 return 0;
481}
482
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700483static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200484 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100485 .owner = THIS_MODULE,
486 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000487};
488
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100489static int index_to_minor(int index)
490{
491 return index << PART_BITS;
492}
493
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200494static int minor_to_index(int minor)
495{
496 return minor >> PART_BITS;
497}
498
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500499static ssize_t virtblk_serial_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
502 struct gendisk *disk = dev_to_disk(dev);
503 int err;
504
505 /* sysfs gives us a PAGE_SIZE buffer */
506 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
507
508 buf[VIRTIO_BLK_ID_BYTES] = '\0';
509 err = virtblk_get_id(disk, buf);
510 if (!err)
511 return strlen(buf);
512
513 if (err == -EIO) /* Unsupported? Make it empty. */
514 return 0;
515
516 return err;
517}
518DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
519
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100520static void virtblk_config_changed_work(struct work_struct *work)
521{
522 struct virtio_blk *vblk =
523 container_of(work, struct virtio_blk, config_work);
524 struct virtio_device *vdev = vblk->vdev;
525 struct request_queue *q = vblk->disk->queue;
526 char cap_str_2[10], cap_str_10[10];
Milos Vyletel9d9598b2013-03-12 15:34:40 +1030527 char *envp[] = { "RESIZE=1", NULL };
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100528 u64 capacity, size;
529
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030530 mutex_lock(&vblk->config_lock);
531 if (!vblk->config_enable)
532 goto done;
533
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100534 /* Host must always specify the capacity. */
535 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
536 &capacity, sizeof(capacity));
537
538 /* If capacity is too big, truncate with warning. */
539 if ((sector_t)capacity != capacity) {
540 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
541 (unsigned long long)capacity);
542 capacity = (sector_t)-1;
543 }
544
545 size = capacity * queue_logical_block_size(q);
546 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
547 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
548
549 dev_notice(&vdev->dev,
550 "new size: %llu %d-byte logical blocks (%s/%s)\n",
551 (unsigned long long)capacity,
552 queue_logical_block_size(q),
553 cap_str_10, cap_str_2);
554
555 set_capacity(vblk->disk, capacity);
Vivek Goyale9986f32012-03-29 10:09:44 +0200556 revalidate_disk(vblk->disk);
Milos Vyletel9d9598b2013-03-12 15:34:40 +1030557 kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030558done:
559 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100560}
561
562static void virtblk_config_changed(struct virtio_device *vdev)
563{
564 struct virtio_blk *vblk = vdev->priv;
565
566 queue_work(virtblk_wq, &vblk->config_work);
567}
568
Amit Shah6abd6e52011-12-22 16:58:29 +0530569static int init_vq(struct virtio_blk *vblk)
570{
571 int err = 0;
572
573 /* We expect one virtqueue, for output. */
Asias Hea98755c2012-08-08 16:07:04 +0800574 vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
Amit Shah6abd6e52011-12-22 16:58:29 +0530575 if (IS_ERR(vblk->vq))
576 err = PTR_ERR(vblk->vq);
577
578 return err;
579}
580
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800581/*
582 * Legacy naming scheme used for virtio devices. We are stuck with it for
583 * virtio blk but don't ever use it for any new driver.
584 */
585static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
586{
587 const int base = 'z' - 'a' + 1;
588 char *begin = buf + strlen(prefix);
589 char *end = buf + buflen;
590 char *p;
591 int unit;
592
593 p = end - 1;
594 *p = '\0';
595 unit = base;
596 do {
597 if (p == begin)
598 return -EINVAL;
599 *--p = 'a' + (index % unit);
600 index = (index / unit) - 1;
601 } while (index >= 0);
602
603 memmove(begin, p, end - p);
604 memcpy(buf, prefix, strlen(prefix));
605
606 return 0;
607}
608
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200609static int virtblk_get_cache_mode(struct virtio_device *vdev)
610{
611 u8 writeback;
612 int err;
613
614 err = virtio_config_val(vdev, VIRTIO_BLK_F_CONFIG_WCE,
615 offsetof(struct virtio_blk_config, wce),
616 &writeback);
617 if (err)
618 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
619
620 return writeback;
621}
622
623static void virtblk_update_cache_mode(struct virtio_device *vdev)
624{
625 u8 writeback = virtblk_get_cache_mode(vdev);
626 struct virtio_blk *vblk = vdev->priv;
627
Asias Hec85a1f92012-08-08 16:07:05 +0800628 if (writeback)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200629 blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
630 else
631 blk_queue_flush(vblk->disk->queue, 0);
632
633 revalidate_disk(vblk->disk);
634}
635
636static const char *const virtblk_cache_types[] = {
637 "write through", "write back"
638};
639
640static ssize_t
641virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
642 const char *buf, size_t count)
643{
644 struct gendisk *disk = dev_to_disk(dev);
645 struct virtio_blk *vblk = disk->private_data;
646 struct virtio_device *vdev = vblk->vdev;
647 int i;
648 u8 writeback;
649
650 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
651 for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
652 if (sysfs_streq(buf, virtblk_cache_types[i]))
653 break;
654
655 if (i < 0)
656 return -EINVAL;
657
658 writeback = i;
659 vdev->config->set(vdev,
660 offsetof(struct virtio_blk_config, wce),
661 &writeback, sizeof(writeback));
662
663 virtblk_update_cache_mode(vdev);
664 return count;
665}
666
667static ssize_t
668virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
669 char *buf)
670{
671 struct gendisk *disk = dev_to_disk(dev);
672 struct virtio_blk *vblk = disk->private_data;
673 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
674
675 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
676 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
677}
678
679static const struct device_attribute dev_attr_cache_type_ro =
680 __ATTR(cache_type, S_IRUGO,
681 virtblk_cache_type_show, NULL);
682static const struct device_attribute dev_attr_cache_type_rw =
683 __ATTR(cache_type, S_IRUGO|S_IWUSR,
684 virtblk_cache_type_show, virtblk_cache_type_store);
685
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800686static int virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000687{
688 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600689 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200690 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800691 int pool_size;
692
Rusty Russelle467cde2007-10-22 11:03:38 +1000693 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600694 u32 v, blk_size, sg_elems, opt_io_size;
695 u16 min_io_size;
696 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000697
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200698 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
699 GFP_KERNEL);
700 if (err < 0)
701 goto out;
702 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100703
Rusty Russell0864b792008-12-30 09:26:05 -0600704 /* We need to know how many segments before we allocate. */
705 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
706 offsetof(struct virtio_blk_config, seg_max),
707 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200708
709 /* We need at least one SG element, whatever they say. */
710 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600711 sg_elems = 1;
712
713 /* We need an extra sg elements at head and tail. */
714 sg_elems += 2;
715 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
716 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000717 if (!vblk) {
718 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200719 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000720 }
721
Asias Hea98755c2012-08-08 16:07:04 +0800722 init_waitqueue_head(&vblk->queue_wait);
Rusty Russelle467cde2007-10-22 11:03:38 +1000723 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600724 vblk->sg_elems = sg_elems;
725 sg_init_table(vblk->sg, vblk->sg_elems);
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030726 mutex_init(&vblk->config_lock);
Asias Hea98755c2012-08-08 16:07:04 +0800727
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100728 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030729 vblk->config_enable = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000730
Amit Shah6abd6e52011-12-22 16:58:29 +0530731 err = init_vq(vblk);
732 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000733 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000734
Asias Hea98755c2012-08-08 16:07:04 +0800735 pool_size = sizeof(struct virtblk_req);
736 if (use_bio)
737 pool_size += sizeof(struct scatterlist) * sg_elems;
738 vblk->pool = mempool_create_kmalloc_pool(1, pool_size);
Rusty Russelle467cde2007-10-22 11:03:38 +1000739 if (!vblk->pool) {
740 err = -ENOMEM;
741 goto out_free_vq;
742 }
743
Rusty Russelle467cde2007-10-22 11:03:38 +1000744 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100745 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000746 if (!vblk->disk) {
747 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100748 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000749 }
750
Asias Hea98755c2012-08-08 16:07:04 +0800751 q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600752 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000753 err = -ENOMEM;
754 goto out_put_disk;
755 }
756
Asias Hea98755c2012-08-08 16:07:04 +0800757 if (use_bio)
758 blk_queue_make_request(q, virtblk_make_request);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600759 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900760
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800761 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100762
Rusty Russelle467cde2007-10-22 11:03:38 +1000763 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100764 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000765 vblk->disk->private_data = vblk;
766 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500767 vblk->disk->driverfs_dev = &vdev->dev;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200768 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100769
Tejun Heo02c42b72010-09-03 11:56:18 +0200770 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200771 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000772
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200773 /* If disk is read-only in the host, the guest should obey */
774 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
775 set_disk_ro(vblk->disk, 1);
776
Rusty Russella586d4f2008-02-04 23:49:56 -0500777 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500778 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
779 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000780
781 /* If capacity is too big, truncate with warning. */
782 if ((sector_t)cap != cap) {
783 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
784 (unsigned long long)cap);
785 cap = (sector_t)-1;
786 }
787 set_capacity(vblk->disk, cap);
788
Rusty Russell0864b792008-12-30 09:26:05 -0600789 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500790 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600791
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600792 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600793 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600794
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600795 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500796 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600797
Rusty Russella586d4f2008-02-04 23:49:56 -0500798 /* Host can optionally specify maximum segment size and number of
799 * segments. */
800 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
801 offsetof(struct virtio_blk_config, size_max),
802 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000803 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600804 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600805 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600806 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000807
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200808 /* Host can optionally specify the block size of the device */
809 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
810 offsetof(struct virtio_blk_config, blk_size),
811 &blk_size);
812 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600813 blk_queue_logical_block_size(q, blk_size);
814 else
815 blk_size = queue_logical_block_size(q);
816
817 /* Use topology information if available */
818 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
819 offsetof(struct virtio_blk_config, physical_block_exp),
820 &physical_block_exp);
821 if (!err && physical_block_exp)
822 blk_queue_physical_block_size(q,
823 blk_size * (1 << physical_block_exp));
824
825 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
826 offsetof(struct virtio_blk_config, alignment_offset),
827 &alignment_offset);
828 if (!err && alignment_offset)
829 blk_queue_alignment_offset(q, blk_size * alignment_offset);
830
831 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
832 offsetof(struct virtio_blk_config, min_io_size),
833 &min_io_size);
834 if (!err && min_io_size)
835 blk_queue_io_min(q, blk_size * min_io_size);
836
837 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
838 offsetof(struct virtio_blk_config, opt_io_size),
839 &opt_io_size);
840 if (!err && opt_io_size)
841 blk_queue_io_opt(q, blk_size * opt_io_size);
842
Rusty Russelle467cde2007-10-22 11:03:38 +1000843 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500844 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
845 if (err)
846 goto out_del_disk;
847
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200848 if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
849 err = device_create_file(disk_to_dev(vblk->disk),
850 &dev_attr_cache_type_rw);
851 else
852 err = device_create_file(disk_to_dev(vblk->disk),
853 &dev_attr_cache_type_ro);
854 if (err)
855 goto out_del_disk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000856 return 0;
857
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500858out_del_disk:
859 del_gendisk(vblk->disk);
860 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000861out_put_disk:
862 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000863out_mempool:
864 mempool_destroy(vblk->pool);
865out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600866 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000867out_free_vblk:
868 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200869out_free_index:
870 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000871out:
872 return err;
873}
874
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800875static void virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000876{
877 struct virtio_blk *vblk = vdev->priv;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200878 int index = vblk->index;
Alexander Graff4953fe2013-01-02 15:37:17 +1030879 int refc;
Rusty Russelle467cde2007-10-22 11:03:38 +1000880
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030881 /* Prevent config work handler from accessing the device. */
882 mutex_lock(&vblk->config_lock);
883 vblk->config_enable = false;
884 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100885
Asias He02e2b122012-05-25 10:34:47 +0800886 del_gendisk(vblk->disk);
Asias He483001c2012-05-25 10:34:48 +0800887 blk_cleanup_queue(vblk->disk->queue);
Asias He02e2b122012-05-25 10:34:47 +0800888
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500889 /* Stop all the virtqueues. */
890 vdev->config->reset(vdev);
891
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030892 flush_work(&vblk->config_work);
893
Alexander Graff4953fe2013-01-02 15:37:17 +1030894 refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
Rusty Russelle467cde2007-10-22 11:03:38 +1000895 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000896 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600897 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000898 kfree(vblk);
Alexander Graff4953fe2013-01-02 15:37:17 +1030899
900 /* Only free device id if we don't have any users */
901 if (refc == 1)
902 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000903}
904
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530905#ifdef CONFIG_PM
906static int virtblk_freeze(struct virtio_device *vdev)
907{
908 struct virtio_blk *vblk = vdev->priv;
909
910 /* Ensure we don't receive any more interrupts */
911 vdev->config->reset(vdev);
912
913 /* Prevent config work handler from accessing the device. */
914 mutex_lock(&vblk->config_lock);
915 vblk->config_enable = false;
916 mutex_unlock(&vblk->config_lock);
917
918 flush_work(&vblk->config_work);
919
920 spin_lock_irq(vblk->disk->queue->queue_lock);
921 blk_stop_queue(vblk->disk->queue);
922 spin_unlock_irq(vblk->disk->queue->queue_lock);
923 blk_sync_queue(vblk->disk->queue);
924
925 vdev->config->del_vqs(vdev);
926 return 0;
927}
928
929static int virtblk_restore(struct virtio_device *vdev)
930{
931 struct virtio_blk *vblk = vdev->priv;
932 int ret;
933
934 vblk->config_enable = true;
935 ret = init_vq(vdev->priv);
936 if (!ret) {
937 spin_lock_irq(vblk->disk->queue->queue_lock);
938 blk_start_queue(vblk->disk->queue);
939 spin_unlock_irq(vblk->disk->queue->queue_lock);
940 }
941 return ret;
942}
943#endif
944
Márton Németh47483e22010-01-10 13:40:02 +0100945static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000946 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
947 { 0 },
948};
949
Rusty Russellc45a6812008-05-02 21:50:50 -0500950static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200951 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
952 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200953 VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
Rusty Russellc45a6812008-05-02 21:50:50 -0500954};
955
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800956static struct virtio_driver virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100957 .feature_table = features,
958 .feature_table_size = ARRAY_SIZE(features),
959 .driver.name = KBUILD_MODNAME,
960 .driver.owner = THIS_MODULE,
961 .id_table = id_table,
962 .probe = virtblk_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800963 .remove = virtblk_remove,
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100964 .config_changed = virtblk_config_changed,
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530965#ifdef CONFIG_PM
966 .freeze = virtblk_freeze,
967 .restore = virtblk_restore,
968#endif
Rusty Russelle467cde2007-10-22 11:03:38 +1000969};
970
971static int __init init(void)
972{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100973 int error;
974
975 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
976 if (!virtblk_wq)
977 return -ENOMEM;
978
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100979 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100980 if (major < 0) {
981 error = major;
982 goto out_destroy_workqueue;
983 }
984
985 error = register_virtio_driver(&virtio_blk);
986 if (error)
987 goto out_unregister_blkdev;
988 return 0;
989
990out_unregister_blkdev:
991 unregister_blkdev(major, "virtblk");
992out_destroy_workqueue:
993 destroy_workqueue(virtblk_wq);
994 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000995}
996
997static void __exit fini(void)
998{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100999 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +10001000 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001001 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +10001002}
1003module_init(init);
1004module_exit(fini);
1005
1006MODULE_DEVICE_TABLE(virtio, id_table);
1007MODULE_DESCRIPTION("Virtio block driver");
1008MODULE_LICENSE("GPL");