blob: 53b81d59059b5d4074b582aab6432ccdba42e79b [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 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. 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;
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
103static void virtblk_add_buf_wait(struct virtio_blk *vblk,
104 struct virtblk_req *vbr,
105 unsigned long out,
106 unsigned long in)
107{
108 DEFINE_WAIT(wait);
109
110 for (;;) {
111 prepare_to_wait_exclusive(&vblk->queue_wait, &wait,
112 TASK_UNINTERRUPTIBLE);
113
114 spin_lock_irq(vblk->disk->queue->queue_lock);
115 if (virtqueue_add_buf(vblk->vq, vbr->sg, out, in, vbr,
116 GFP_ATOMIC) < 0) {
117 spin_unlock_irq(vblk->disk->queue->queue_lock);
118 io_schedule();
119 } else {
120 virtqueue_kick(vblk->vq);
121 spin_unlock_irq(vblk->disk->queue->queue_lock);
122 break;
123 }
124
125 }
126
127 finish_wait(&vblk->queue_wait, &wait);
128}
129
130static inline void virtblk_add_req(struct virtblk_req *vbr,
131 unsigned int out, unsigned int in)
132{
133 struct virtio_blk *vblk = vbr->vblk;
134
135 spin_lock_irq(vblk->disk->queue->queue_lock);
136 if (unlikely(virtqueue_add_buf(vblk->vq, vbr->sg, out, in, vbr,
137 GFP_ATOMIC) < 0)) {
138 spin_unlock_irq(vblk->disk->queue->queue_lock);
139 virtblk_add_buf_wait(vblk, vbr, out, in);
140 return;
141 }
142 virtqueue_kick(vblk->vq);
143 spin_unlock_irq(vblk->disk->queue->queue_lock);
144}
145
146static int virtblk_bio_send_flush(struct virtblk_req *vbr)
147{
148 unsigned int out = 0, in = 0;
149
150 vbr->flags |= VBLK_IS_FLUSH;
151 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
152 vbr->out_hdr.sector = 0;
153 vbr->out_hdr.ioprio = 0;
154 sg_set_buf(&vbr->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
155 sg_set_buf(&vbr->sg[out + in++], &vbr->status, sizeof(vbr->status));
156
157 virtblk_add_req(vbr, out, in);
158
159 return 0;
160}
161
162static int virtblk_bio_send_data(struct virtblk_req *vbr)
163{
164 struct virtio_blk *vblk = vbr->vblk;
165 unsigned int num, out = 0, in = 0;
166 struct bio *bio = vbr->bio;
167
168 vbr->flags &= ~VBLK_IS_FLUSH;
169 vbr->out_hdr.type = 0;
170 vbr->out_hdr.sector = bio->bi_sector;
171 vbr->out_hdr.ioprio = bio_prio(bio);
172
173 sg_set_buf(&vbr->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
174
175 num = blk_bio_map_sg(vblk->disk->queue, bio, vbr->sg + out);
176
177 sg_set_buf(&vbr->sg[num + out + in++], &vbr->status,
178 sizeof(vbr->status));
179
180 if (num) {
181 if (bio->bi_rw & REQ_WRITE) {
182 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
183 out += num;
184 } else {
185 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
186 in += num;
187 }
188 }
189
190 virtblk_add_req(vbr, out, in);
191
192 return 0;
193}
194
195static void virtblk_bio_send_data_work(struct work_struct *work)
196{
197 struct virtblk_req *vbr;
198
199 vbr = container_of(work, struct virtblk_req, work);
200
201 virtblk_bio_send_data(vbr);
202}
203
204static void virtblk_bio_send_flush_work(struct work_struct *work)
205{
206 struct virtblk_req *vbr;
207
208 vbr = container_of(work, struct virtblk_req, work);
209
210 virtblk_bio_send_flush(vbr);
211}
212
213static inline void virtblk_request_done(struct virtblk_req *vbr)
214{
215 struct virtio_blk *vblk = vbr->vblk;
Asias Hea98755c2012-08-08 16:07:04 +0800216 struct request *req = vbr->req;
217 int error = virtblk_result(vbr);
218
219 if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
220 req->resid_len = vbr->in_hdr.residual;
221 req->sense_len = vbr->in_hdr.sense_len;
222 req->errors = vbr->in_hdr.errors;
223 } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
224 req->errors = (error != 0);
225 }
226
227 __blk_end_request_all(req, error);
228 mempool_free(vbr, vblk->pool);
229}
230
Asias Hec85a1f92012-08-08 16:07:05 +0800231static inline void virtblk_bio_flush_done(struct virtblk_req *vbr)
Asias Hea98755c2012-08-08 16:07:04 +0800232{
Asias Hec85a1f92012-08-08 16:07:05 +0800233 struct virtio_blk *vblk = vbr->vblk;
234
235 if (vbr->flags & VBLK_REQ_DATA) {
236 /* Send out the actual write data */
237 INIT_WORK(&vbr->work, virtblk_bio_send_data_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_data_done(struct virtblk_req *vbr)
246{
247 struct virtio_blk *vblk = vbr->vblk;
248
249 if (unlikely(vbr->flags & VBLK_REQ_FUA)) {
250 /* Send out a flush before end the bio */
251 vbr->flags &= ~VBLK_REQ_DATA;
252 INIT_WORK(&vbr->work, virtblk_bio_send_flush_work);
253 queue_work(virtblk_wq, &vbr->work);
254 } else {
255 bio_endio(vbr->bio, virtblk_result(vbr));
256 mempool_free(vbr, vblk->pool);
257 }
258}
259
260static inline void virtblk_bio_done(struct virtblk_req *vbr)
261{
262 if (unlikely(vbr->flags & VBLK_IS_FLUSH))
263 virtblk_bio_flush_done(vbr);
264 else
265 virtblk_bio_data_done(vbr);
Asias Hea98755c2012-08-08 16:07:04 +0800266}
267
268static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000269{
270 struct virtio_blk *vblk = vq->vdev->priv;
Asias Hec85a1f92012-08-08 16:07:05 +0800271 bool bio_done = false, req_done = false;
Rusty Russelle467cde2007-10-22 11:03:38 +1000272 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000273 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800274 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000275
Asias He2c95a322012-05-25 16:03:27 +0800276 spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300277 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
Asias Hea98755c2012-08-08 16:07:04 +0800278 if (vbr->bio) {
Asias Hec85a1f92012-08-08 16:07:05 +0800279 virtblk_bio_done(vbr);
280 bio_done = true;
Asias Hea98755c2012-08-08 16:07:04 +0800281 } else {
Asias Hec85a1f92012-08-08 16:07:05 +0800282 virtblk_request_done(vbr);
283 req_done = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000284 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000285 }
286 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800287 if (req_done)
288 blk_start_queue(vblk->disk->queue);
Asias He2c95a322012-05-25 16:03:27 +0800289 spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800290
291 if (bio_done)
292 wake_up(&vblk->queue_wait);
293}
294
Rusty Russelle467cde2007-10-22 11:03:38 +1000295static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
296 struct request *req)
297{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200298 unsigned long num, out = 0, in = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +1000299 struct virtblk_req *vbr;
300
Asias Hea98755c2012-08-08 16:07:04 +0800301 vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
Rusty Russelle467cde2007-10-22 11:03:38 +1000302 if (!vbr)
303 /* When another request finishes we'll try again. */
304 return false;
305
306 vbr->req = req;
Asias Hea98755c2012-08-08 16:07:04 +0800307 vbr->bio = NULL;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900308 if (req->cmd_flags & REQ_FLUSH) {
309 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000310 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200311 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900312 } else {
313 switch (req->cmd_type) {
314 case REQ_TYPE_FS:
315 vbr->out_hdr.type = 0;
316 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
317 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
318 break;
319 case REQ_TYPE_BLOCK_PC:
320 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200321 vbr->out_hdr.sector = 0;
322 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
323 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900324 case REQ_TYPE_SPECIAL:
325 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
326 vbr->out_hdr.sector = 0;
327 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
328 break;
329 default:
330 /* We don't put anything else in the queue. */
331 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200332 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000333 }
334
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200335 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
Rusty Russelle467cde2007-10-22 11:03:38 +1000336
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200337 /*
338 * If this is a packet command we need a couple of additional headers.
339 * Behind the normal outhdr we put a segment with the scsi command
340 * block, and before the normal inhdr we put the sense data and the
341 * inhdr with additional status information before the normal inhdr.
342 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200343 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200344 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
345
346 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
347
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200348 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
Liu Yuan6917f832011-04-24 02:49:26 +0800349 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200350 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
351 sizeof(vbr->in_hdr));
352 }
353
354 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
355 sizeof(vbr->status));
356
357 if (num) {
358 if (rq_data_dir(vbr->req) == WRITE) {
359 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
360 out += num;
361 } else {
362 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
363 in += num;
364 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000365 }
366
Asias Hea98755c2012-08-08 16:07:04 +0800367 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr,
368 GFP_ATOMIC) < 0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000369 mempool_free(vbr, vblk->pool);
370 return false;
371 }
372
Rusty Russelle467cde2007-10-22 11:03:38 +1000373 return true;
374}
375
Asias Hea98755c2012-08-08 16:07:04 +0800376static void virtblk_request(struct request_queue *q)
Rusty Russelle467cde2007-10-22 11:03:38 +1000377{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200378 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000379 struct request *req;
380 unsigned int issued = 0;
381
Tejun Heo9934c8c2009-05-08 11:54:16 +0900382 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600383 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000384
385 /* If this request fails, stop queue and wait for something to
386 finish to restart it. */
387 if (!do_req(q, vblk, req)) {
388 blk_stop_queue(q);
389 break;
390 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900391 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000392 issued++;
393 }
394
395 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300396 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000397}
398
Asias Hea98755c2012-08-08 16:07:04 +0800399static void virtblk_make_request(struct request_queue *q, struct bio *bio)
400{
401 struct virtio_blk *vblk = q->queuedata;
Asias Hea98755c2012-08-08 16:07:04 +0800402 struct virtblk_req *vbr;
403
404 BUG_ON(bio->bi_phys_segments + 2 > vblk->sg_elems);
Asias Hea98755c2012-08-08 16:07:04 +0800405
406 vbr = virtblk_alloc_req(vblk, GFP_NOIO);
407 if (!vbr) {
408 bio_endio(bio, -ENOMEM);
409 return;
410 }
411
412 vbr->bio = bio;
Asias Hec85a1f92012-08-08 16:07:05 +0800413 vbr->flags = 0;
414 if (bio->bi_rw & REQ_FLUSH)
415 vbr->flags |= VBLK_REQ_FLUSH;
416 if (bio->bi_rw & REQ_FUA)
417 vbr->flags |= VBLK_REQ_FUA;
418 if (bio->bi_size)
419 vbr->flags |= VBLK_REQ_DATA;
Asias Hea98755c2012-08-08 16:07:04 +0800420
Asias Hec85a1f92012-08-08 16:07:05 +0800421 if (unlikely(vbr->flags & VBLK_REQ_FLUSH))
422 virtblk_bio_send_flush(vbr);
423 else
424 virtblk_bio_send_data(vbr);
Asias Hea98755c2012-08-08 16:07:04 +0800425}
426
john cooper4cb2ea22010-03-25 01:33:33 -0400427/* return id (s/n) string for *disk to *id_str
428 */
429static int virtblk_get_id(struct gendisk *disk, char *id_str)
430{
431 struct virtio_blk *vblk = disk->private_data;
432 struct request *req;
433 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030434 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400435
436 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
437 GFP_KERNEL);
438 if (IS_ERR(bio))
439 return PTR_ERR(bio);
440
441 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
442 if (IS_ERR(req)) {
443 bio_put(bio);
444 return PTR_ERR(req);
445 }
446
447 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030448 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
449 blk_put_request(req);
450
451 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400452}
453
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200454static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
455 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000456{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200457 struct gendisk *disk = bdev->bd_disk;
458 struct virtio_blk *vblk = disk->private_data;
459
460 /*
461 * Only allow the generic SCSI ioctls if the host can support it.
462 */
463 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200464 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200465
Paolo Bonzini577ebb32012-01-12 16:01:27 +0100466 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
467 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000468}
469
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100470/* We provide getgeo only to please some old bootloader/partitioning tools */
471static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
472{
Ryan Harper48e40432008-04-16 13:56:37 -0500473 struct virtio_blk *vblk = bd->bd_disk->private_data;
474 struct virtio_blk_geometry vgeo;
475 int err;
476
477 /* see if the host passed in geometry config */
478 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
479 offsetof(struct virtio_blk_config, geometry),
480 &vgeo);
481
482 if (!err) {
483 geo->heads = vgeo.heads;
484 geo->sectors = vgeo.sectors;
485 geo->cylinders = vgeo.cylinders;
486 } else {
487 /* some standard values, similar to sd */
488 geo->heads = 1 << 6;
489 geo->sectors = 1 << 5;
490 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
491 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100492 return 0;
493}
494
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700495static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200496 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100497 .owner = THIS_MODULE,
498 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000499};
500
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100501static int index_to_minor(int index)
502{
503 return index << PART_BITS;
504}
505
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200506static int minor_to_index(int minor)
507{
508 return minor >> PART_BITS;
509}
510
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500511static ssize_t virtblk_serial_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
514 struct gendisk *disk = dev_to_disk(dev);
515 int err;
516
517 /* sysfs gives us a PAGE_SIZE buffer */
518 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
519
520 buf[VIRTIO_BLK_ID_BYTES] = '\0';
521 err = virtblk_get_id(disk, buf);
522 if (!err)
523 return strlen(buf);
524
525 if (err == -EIO) /* Unsupported? Make it empty. */
526 return 0;
527
528 return err;
529}
530DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
531
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100532static void virtblk_config_changed_work(struct work_struct *work)
533{
534 struct virtio_blk *vblk =
535 container_of(work, struct virtio_blk, config_work);
536 struct virtio_device *vdev = vblk->vdev;
537 struct request_queue *q = vblk->disk->queue;
538 char cap_str_2[10], cap_str_10[10];
539 u64 capacity, size;
540
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030541 mutex_lock(&vblk->config_lock);
542 if (!vblk->config_enable)
543 goto done;
544
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100545 /* Host must always specify the capacity. */
546 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
547 &capacity, sizeof(capacity));
548
549 /* If capacity is too big, truncate with warning. */
550 if ((sector_t)capacity != capacity) {
551 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
552 (unsigned long long)capacity);
553 capacity = (sector_t)-1;
554 }
555
556 size = capacity * queue_logical_block_size(q);
557 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
558 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
559
560 dev_notice(&vdev->dev,
561 "new size: %llu %d-byte logical blocks (%s/%s)\n",
562 (unsigned long long)capacity,
563 queue_logical_block_size(q),
564 cap_str_10, cap_str_2);
565
566 set_capacity(vblk->disk, capacity);
Vivek Goyale9986f32012-03-29 10:09:44 +0200567 revalidate_disk(vblk->disk);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030568done:
569 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100570}
571
572static void virtblk_config_changed(struct virtio_device *vdev)
573{
574 struct virtio_blk *vblk = vdev->priv;
575
576 queue_work(virtblk_wq, &vblk->config_work);
577}
578
Amit Shah6abd6e52011-12-22 16:58:29 +0530579static int init_vq(struct virtio_blk *vblk)
580{
581 int err = 0;
582
583 /* We expect one virtqueue, for output. */
Asias Hea98755c2012-08-08 16:07:04 +0800584 vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
Amit Shah6abd6e52011-12-22 16:58:29 +0530585 if (IS_ERR(vblk->vq))
586 err = PTR_ERR(vblk->vq);
587
588 return err;
589}
590
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800591/*
592 * Legacy naming scheme used for virtio devices. We are stuck with it for
593 * virtio blk but don't ever use it for any new driver.
594 */
595static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
596{
597 const int base = 'z' - 'a' + 1;
598 char *begin = buf + strlen(prefix);
599 char *end = buf + buflen;
600 char *p;
601 int unit;
602
603 p = end - 1;
604 *p = '\0';
605 unit = base;
606 do {
607 if (p == begin)
608 return -EINVAL;
609 *--p = 'a' + (index % unit);
610 index = (index / unit) - 1;
611 } while (index >= 0);
612
613 memmove(begin, p, end - p);
614 memcpy(buf, prefix, strlen(prefix));
615
616 return 0;
617}
618
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200619static int virtblk_get_cache_mode(struct virtio_device *vdev)
620{
621 u8 writeback;
622 int err;
623
624 err = virtio_config_val(vdev, VIRTIO_BLK_F_CONFIG_WCE,
625 offsetof(struct virtio_blk_config, wce),
626 &writeback);
627 if (err)
628 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
629
630 return writeback;
631}
632
633static void virtblk_update_cache_mode(struct virtio_device *vdev)
634{
635 u8 writeback = virtblk_get_cache_mode(vdev);
636 struct virtio_blk *vblk = vdev->priv;
637
Asias Hec85a1f92012-08-08 16:07:05 +0800638 if (writeback)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200639 blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
640 else
641 blk_queue_flush(vblk->disk->queue, 0);
642
643 revalidate_disk(vblk->disk);
644}
645
646static const char *const virtblk_cache_types[] = {
647 "write through", "write back"
648};
649
650static ssize_t
651virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
652 const char *buf, size_t count)
653{
654 struct gendisk *disk = dev_to_disk(dev);
655 struct virtio_blk *vblk = disk->private_data;
656 struct virtio_device *vdev = vblk->vdev;
657 int i;
658 u8 writeback;
659
660 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
661 for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
662 if (sysfs_streq(buf, virtblk_cache_types[i]))
663 break;
664
665 if (i < 0)
666 return -EINVAL;
667
668 writeback = i;
669 vdev->config->set(vdev,
670 offsetof(struct virtio_blk_config, wce),
671 &writeback, sizeof(writeback));
672
673 virtblk_update_cache_mode(vdev);
674 return count;
675}
676
677static ssize_t
678virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
679 char *buf)
680{
681 struct gendisk *disk = dev_to_disk(dev);
682 struct virtio_blk *vblk = disk->private_data;
683 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
684
685 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
686 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
687}
688
689static const struct device_attribute dev_attr_cache_type_ro =
690 __ATTR(cache_type, S_IRUGO,
691 virtblk_cache_type_show, NULL);
692static const struct device_attribute dev_attr_cache_type_rw =
693 __ATTR(cache_type, S_IRUGO|S_IWUSR,
694 virtblk_cache_type_show, virtblk_cache_type_store);
695
Mike Frysinger98e94442009-05-18 03:39:09 -0400696static int __devinit virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000697{
698 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600699 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200700 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800701 int pool_size;
702
Rusty Russelle467cde2007-10-22 11:03:38 +1000703 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600704 u32 v, blk_size, sg_elems, opt_io_size;
705 u16 min_io_size;
706 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000707
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200708 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
709 GFP_KERNEL);
710 if (err < 0)
711 goto out;
712 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100713
Rusty Russell0864b792008-12-30 09:26:05 -0600714 /* We need to know how many segments before we allocate. */
715 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
716 offsetof(struct virtio_blk_config, seg_max),
717 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200718
719 /* We need at least one SG element, whatever they say. */
720 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600721 sg_elems = 1;
722
723 /* We need an extra sg elements at head and tail. */
724 sg_elems += 2;
725 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
726 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000727 if (!vblk) {
728 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200729 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000730 }
731
Asias Hea98755c2012-08-08 16:07:04 +0800732 init_waitqueue_head(&vblk->queue_wait);
Rusty Russelle467cde2007-10-22 11:03:38 +1000733 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600734 vblk->sg_elems = sg_elems;
735 sg_init_table(vblk->sg, vblk->sg_elems);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030736 mutex_init(&vblk->config_lock);
Asias Hea98755c2012-08-08 16:07:04 +0800737
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100738 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030739 vblk->config_enable = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000740
Amit Shah6abd6e52011-12-22 16:58:29 +0530741 err = init_vq(vblk);
742 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000743 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000744
Asias Hea98755c2012-08-08 16:07:04 +0800745 pool_size = sizeof(struct virtblk_req);
746 if (use_bio)
747 pool_size += sizeof(struct scatterlist) * sg_elems;
748 vblk->pool = mempool_create_kmalloc_pool(1, pool_size);
Rusty Russelle467cde2007-10-22 11:03:38 +1000749 if (!vblk->pool) {
750 err = -ENOMEM;
751 goto out_free_vq;
752 }
753
Rusty Russelle467cde2007-10-22 11:03:38 +1000754 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100755 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000756 if (!vblk->disk) {
757 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100758 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000759 }
760
Asias Hea98755c2012-08-08 16:07:04 +0800761 q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600762 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000763 err = -ENOMEM;
764 goto out_put_disk;
765 }
766
Asias Hea98755c2012-08-08 16:07:04 +0800767 if (use_bio)
768 blk_queue_make_request(q, virtblk_make_request);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600769 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900770
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800771 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100772
Rusty Russelle467cde2007-10-22 11:03:38 +1000773 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100774 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000775 vblk->disk->private_data = vblk;
776 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500777 vblk->disk->driverfs_dev = &vdev->dev;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200778 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100779
Tejun Heo02c42b72010-09-03 11:56:18 +0200780 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200781 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000782
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200783 /* If disk is read-only in the host, the guest should obey */
784 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
785 set_disk_ro(vblk->disk, 1);
786
Rusty Russella586d4f2008-02-04 23:49:56 -0500787 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500788 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
789 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000790
791 /* If capacity is too big, truncate with warning. */
792 if ((sector_t)cap != cap) {
793 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
794 (unsigned long long)cap);
795 cap = (sector_t)-1;
796 }
797 set_capacity(vblk->disk, cap);
798
Rusty Russell0864b792008-12-30 09:26:05 -0600799 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500800 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600801
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600802 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600803 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600804
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600805 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500806 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600807
Rusty Russella586d4f2008-02-04 23:49:56 -0500808 /* Host can optionally specify maximum segment size and number of
809 * segments. */
810 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
811 offsetof(struct virtio_blk_config, size_max),
812 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000813 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600814 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600815 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600816 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000817
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200818 /* Host can optionally specify the block size of the device */
819 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
820 offsetof(struct virtio_blk_config, blk_size),
821 &blk_size);
822 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600823 blk_queue_logical_block_size(q, blk_size);
824 else
825 blk_size = queue_logical_block_size(q);
826
827 /* Use topology information if available */
828 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
829 offsetof(struct virtio_blk_config, physical_block_exp),
830 &physical_block_exp);
831 if (!err && physical_block_exp)
832 blk_queue_physical_block_size(q,
833 blk_size * (1 << physical_block_exp));
834
835 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
836 offsetof(struct virtio_blk_config, alignment_offset),
837 &alignment_offset);
838 if (!err && alignment_offset)
839 blk_queue_alignment_offset(q, blk_size * alignment_offset);
840
841 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
842 offsetof(struct virtio_blk_config, min_io_size),
843 &min_io_size);
844 if (!err && min_io_size)
845 blk_queue_io_min(q, blk_size * min_io_size);
846
847 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
848 offsetof(struct virtio_blk_config, opt_io_size),
849 &opt_io_size);
850 if (!err && opt_io_size)
851 blk_queue_io_opt(q, blk_size * opt_io_size);
852
Rusty Russelle467cde2007-10-22 11:03:38 +1000853 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500854 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
855 if (err)
856 goto out_del_disk;
857
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200858 if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
859 err = device_create_file(disk_to_dev(vblk->disk),
860 &dev_attr_cache_type_rw);
861 else
862 err = device_create_file(disk_to_dev(vblk->disk),
863 &dev_attr_cache_type_ro);
864 if (err)
865 goto out_del_disk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000866 return 0;
867
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500868out_del_disk:
869 del_gendisk(vblk->disk);
870 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000871out_put_disk:
872 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000873out_mempool:
874 mempool_destroy(vblk->pool);
875out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600876 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000877out_free_vblk:
878 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200879out_free_index:
880 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000881out:
882 return err;
883}
884
Mike Frysinger98e94442009-05-18 03:39:09 -0400885static void __devexit virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000886{
887 struct virtio_blk *vblk = vdev->priv;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200888 int index = vblk->index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000889
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030890 /* Prevent config work handler from accessing the device. */
891 mutex_lock(&vblk->config_lock);
892 vblk->config_enable = false;
893 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100894
Asias He02e2b122012-05-25 10:34:47 +0800895 del_gendisk(vblk->disk);
Asias He483001c2012-05-25 10:34:48 +0800896 blk_cleanup_queue(vblk->disk->queue);
Asias He02e2b122012-05-25 10:34:47 +0800897
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500898 /* Stop all the virtqueues. */
899 vdev->config->reset(vdev);
900
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030901 flush_work(&vblk->config_work);
902
Rusty Russelle467cde2007-10-22 11:03:38 +1000903 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000904 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600905 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000906 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200907 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000908}
909
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530910#ifdef CONFIG_PM
911static int virtblk_freeze(struct virtio_device *vdev)
912{
913 struct virtio_blk *vblk = vdev->priv;
914
915 /* Ensure we don't receive any more interrupts */
916 vdev->config->reset(vdev);
917
918 /* Prevent config work handler from accessing the device. */
919 mutex_lock(&vblk->config_lock);
920 vblk->config_enable = false;
921 mutex_unlock(&vblk->config_lock);
922
923 flush_work(&vblk->config_work);
924
925 spin_lock_irq(vblk->disk->queue->queue_lock);
926 blk_stop_queue(vblk->disk->queue);
927 spin_unlock_irq(vblk->disk->queue->queue_lock);
928 blk_sync_queue(vblk->disk->queue);
929
930 vdev->config->del_vqs(vdev);
931 return 0;
932}
933
934static int virtblk_restore(struct virtio_device *vdev)
935{
936 struct virtio_blk *vblk = vdev->priv;
937 int ret;
938
939 vblk->config_enable = true;
940 ret = init_vq(vdev->priv);
941 if (!ret) {
942 spin_lock_irq(vblk->disk->queue->queue_lock);
943 blk_start_queue(vblk->disk->queue);
944 spin_unlock_irq(vblk->disk->queue->queue_lock);
945 }
946 return ret;
947}
948#endif
949
Márton Németh47483e22010-01-10 13:40:02 +0100950static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000951 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
952 { 0 },
953};
954
Rusty Russellc45a6812008-05-02 21:50:50 -0500955static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200956 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
957 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200958 VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
Rusty Russellc45a6812008-05-02 21:50:50 -0500959};
960
Rakib Mullick4fbfff762009-07-17 20:13:22 +0600961/*
962 * virtio_blk causes spurious section mismatch warning by
963 * simultaneously referring to a __devinit and a __devexit function.
964 * Use __refdata to avoid this warning.
965 */
966static struct virtio_driver __refdata virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100967 .feature_table = features,
968 .feature_table_size = ARRAY_SIZE(features),
969 .driver.name = KBUILD_MODNAME,
970 .driver.owner = THIS_MODULE,
971 .id_table = id_table,
972 .probe = virtblk_probe,
973 .remove = __devexit_p(virtblk_remove),
974 .config_changed = virtblk_config_changed,
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530975#ifdef CONFIG_PM
976 .freeze = virtblk_freeze,
977 .restore = virtblk_restore,
978#endif
Rusty Russelle467cde2007-10-22 11:03:38 +1000979};
980
981static int __init init(void)
982{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100983 int error;
984
985 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
986 if (!virtblk_wq)
987 return -ENOMEM;
988
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100989 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100990 if (major < 0) {
991 error = major;
992 goto out_destroy_workqueue;
993 }
994
995 error = register_virtio_driver(&virtio_blk);
996 if (error)
997 goto out_unregister_blkdev;
998 return 0;
999
1000out_unregister_blkdev:
1001 unregister_blkdev(major, "virtblk");
1002out_destroy_workqueue:
1003 destroy_workqueue(virtblk_wq);
1004 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +10001005}
1006
1007static void __exit fini(void)
1008{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +01001009 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +10001010 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001011 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +10001012}
1013module_init(init);
1014module_exit(fini);
1015
1016MODULE_DEVICE_TABLE(virtio, id_table);
1017MODULE_DESCRIPTION("Virtio block driver");
1018MODULE_LICENSE("GPL");