blob: cc88b29c639352698846fa3fe7f8076e28f63c60 [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;
Paolo Bonzini8f39db92013-03-20 15:44:27 +103065 int nents;
Asias Hea98755c2012-08-08 16:07:04 +080066 struct scatterlist sg[];
Rusty Russelle467cde2007-10-22 11:03:38 +100067};
68
Asias Hec85a1f92012-08-08 16:07:05 +080069enum {
70 VBLK_IS_FLUSH = 1,
71 VBLK_REQ_FLUSH = 2,
72 VBLK_REQ_DATA = 4,
73 VBLK_REQ_FUA = 8,
74};
75
Asias Hea98755c2012-08-08 16:07:04 +080076static inline int virtblk_result(struct virtblk_req *vbr)
77{
78 switch (vbr->status) {
79 case VIRTIO_BLK_S_OK:
80 return 0;
81 case VIRTIO_BLK_S_UNSUPP:
82 return -ENOTTY;
83 default:
84 return -EIO;
85 }
86}
87
Asias Hec85a1f92012-08-08 16:07:05 +080088static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk,
89 gfp_t gfp_mask)
Asias Hea98755c2012-08-08 16:07:04 +080090{
Asias Hec85a1f92012-08-08 16:07:05 +080091 struct virtblk_req *vbr;
92
93 vbr = mempool_alloc(vblk->pool, gfp_mask);
Dan Carpenterf22cf8e2012-09-05 15:32:53 +030094 if (!vbr)
95 return NULL;
Asias Hec85a1f92012-08-08 16:07:05 +080096
97 vbr->vblk = vblk;
Dan Carpenterf22cf8e2012-09-05 15:32:53 +030098 if (use_bio)
99 sg_init_table(vbr->sg, vblk->sg_elems);
Asias Hec85a1f92012-08-08 16:07:05 +0800100
101 return vbr;
102}
103
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030104static int __virtblk_add_req(struct virtqueue *vq,
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030105 struct virtblk_req *vbr,
106 struct scatterlist *data_sg,
107 unsigned data_nents)
Asias Hec85a1f92012-08-08 16:07:05 +0800108{
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030109 struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030110 unsigned int num_out = 0, num_in = 0;
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030111 int type = vbr->out_hdr.type & ~VIRTIO_BLK_T_OUT;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030112
113 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
114 sgs[num_out++] = &hdr;
115
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030116 /*
117 * If this is a packet command we need a couple of additional headers.
118 * Behind the normal outhdr we put a segment with the scsi command
119 * block, and before the normal inhdr we put the sense data and the
120 * inhdr with additional status information.
121 */
122 if (type == VIRTIO_BLK_T_SCSI_CMD) {
123 sg_init_one(&cmd, vbr->req->cmd, vbr->req->cmd_len);
124 sgs[num_out++] = &cmd;
125 }
126
127 if (data_nents) {
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030128 if (vbr->out_hdr.type & VIRTIO_BLK_T_OUT)
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030129 sgs[num_out++] = data_sg;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030130 else
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030131 sgs[num_out + num_in++] = data_sg;
132 }
133
134 if (type == VIRTIO_BLK_T_SCSI_CMD) {
135 sg_init_one(&sense, vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
136 sgs[num_out + num_in++] = &sense;
137 sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr));
138 sgs[num_out + num_in++] = &inhdr;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030139 }
140
141 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
142 sgs[num_out + num_in++] = &status;
143
144 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030145}
Asias Hec85a1f92012-08-08 16:07:05 +0800146
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030147static void virtblk_add_req(struct virtblk_req *vbr)
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030148{
149 struct virtio_blk *vblk = vbr->vblk;
150 DEFINE_WAIT(wait);
151 int ret;
152
153 spin_lock_irq(vblk->disk->queue->queue_lock);
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030154 while (unlikely((ret = __virtblk_add_req(vblk->vq, vbr, vbr->sg,
155 vbr->nents)) < 0)) {
Asias Hec85a1f92012-08-08 16:07:05 +0800156 prepare_to_wait_exclusive(&vblk->queue_wait, &wait,
157 TASK_UNINTERRUPTIBLE);
158
Asias Hec85a1f92012-08-08 16:07:05 +0800159 spin_unlock_irq(vblk->disk->queue->queue_lock);
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030160 io_schedule();
161 spin_lock_irq(vblk->disk->queue->queue_lock);
162
163 finish_wait(&vblk->queue_wait, &wait);
Asias Hec85a1f92012-08-08 16:07:05 +0800164 }
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030165
Asias Hec85a1f92012-08-08 16:07:05 +0800166 virtqueue_kick(vblk->vq);
167 spin_unlock_irq(vblk->disk->queue->queue_lock);
168}
169
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030170static void virtblk_bio_send_flush(struct virtblk_req *vbr)
Asias Hec85a1f92012-08-08 16:07:05 +0800171{
Asias Hec85a1f92012-08-08 16:07:05 +0800172 vbr->flags |= VBLK_IS_FLUSH;
173 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
174 vbr->out_hdr.sector = 0;
175 vbr->out_hdr.ioprio = 0;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030176 vbr->nents = 0;
Asias Hec85a1f92012-08-08 16:07:05 +0800177
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030178 virtblk_add_req(vbr);
Asias Hec85a1f92012-08-08 16:07:05 +0800179}
180
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030181static void virtblk_bio_send_data(struct virtblk_req *vbr)
Asias Hec85a1f92012-08-08 16:07:05 +0800182{
183 struct virtio_blk *vblk = vbr->vblk;
Asias Hec85a1f92012-08-08 16:07:05 +0800184 struct bio *bio = vbr->bio;
185
186 vbr->flags &= ~VBLK_IS_FLUSH;
187 vbr->out_hdr.type = 0;
188 vbr->out_hdr.sector = bio->bi_sector;
189 vbr->out_hdr.ioprio = bio_prio(bio);
190
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030191 vbr->nents = blk_bio_map_sg(vblk->disk->queue, bio, vbr->sg);
192 if (vbr->nents) {
193 if (bio->bi_rw & REQ_WRITE)
Asias Hec85a1f92012-08-08 16:07:05 +0800194 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030195 else
Asias Hec85a1f92012-08-08 16:07:05 +0800196 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
Asias Hec85a1f92012-08-08 16:07:05 +0800197 }
198
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030199 virtblk_add_req(vbr);
Asias Hec85a1f92012-08-08 16:07:05 +0800200}
201
202static void virtblk_bio_send_data_work(struct work_struct *work)
203{
204 struct virtblk_req *vbr;
205
206 vbr = container_of(work, struct virtblk_req, work);
207
208 virtblk_bio_send_data(vbr);
209}
210
211static void virtblk_bio_send_flush_work(struct work_struct *work)
212{
213 struct virtblk_req *vbr;
214
215 vbr = container_of(work, struct virtblk_req, work);
216
217 virtblk_bio_send_flush(vbr);
218}
219
220static inline void virtblk_request_done(struct virtblk_req *vbr)
221{
222 struct virtio_blk *vblk = vbr->vblk;
Asias Hea98755c2012-08-08 16:07:04 +0800223 struct request *req = vbr->req;
224 int error = virtblk_result(vbr);
225
226 if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
227 req->resid_len = vbr->in_hdr.residual;
228 req->sense_len = vbr->in_hdr.sense_len;
229 req->errors = vbr->in_hdr.errors;
230 } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
231 req->errors = (error != 0);
232 }
233
234 __blk_end_request_all(req, error);
235 mempool_free(vbr, vblk->pool);
236}
237
Asias Hec85a1f92012-08-08 16:07:05 +0800238static inline void virtblk_bio_flush_done(struct virtblk_req *vbr)
Asias Hea98755c2012-08-08 16:07:04 +0800239{
Asias Hec85a1f92012-08-08 16:07:05 +0800240 struct virtio_blk *vblk = vbr->vblk;
241
242 if (vbr->flags & VBLK_REQ_DATA) {
243 /* Send out the actual write data */
244 INIT_WORK(&vbr->work, virtblk_bio_send_data_work);
245 queue_work(virtblk_wq, &vbr->work);
246 } else {
247 bio_endio(vbr->bio, virtblk_result(vbr));
248 mempool_free(vbr, vblk->pool);
249 }
250}
251
252static inline void virtblk_bio_data_done(struct virtblk_req *vbr)
253{
254 struct virtio_blk *vblk = vbr->vblk;
255
256 if (unlikely(vbr->flags & VBLK_REQ_FUA)) {
257 /* Send out a flush before end the bio */
258 vbr->flags &= ~VBLK_REQ_DATA;
259 INIT_WORK(&vbr->work, virtblk_bio_send_flush_work);
260 queue_work(virtblk_wq, &vbr->work);
261 } else {
262 bio_endio(vbr->bio, virtblk_result(vbr));
263 mempool_free(vbr, vblk->pool);
264 }
265}
266
267static inline void virtblk_bio_done(struct virtblk_req *vbr)
268{
269 if (unlikely(vbr->flags & VBLK_IS_FLUSH))
270 virtblk_bio_flush_done(vbr);
271 else
272 virtblk_bio_data_done(vbr);
Asias Hea98755c2012-08-08 16:07:04 +0800273}
274
275static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000276{
277 struct virtio_blk *vblk = vq->vdev->priv;
Asias Hec85a1f92012-08-08 16:07:05 +0800278 bool bio_done = false, req_done = false;
Rusty Russelle467cde2007-10-22 11:03:38 +1000279 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000280 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800281 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000282
Asias He2c95a322012-05-25 16:03:27 +0800283 spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
Asias Hebb811102012-09-25 10:36:17 +0800284 do {
285 virtqueue_disable_cb(vq);
286 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
287 if (vbr->bio) {
288 virtblk_bio_done(vbr);
289 bio_done = true;
290 } else {
291 virtblk_request_done(vbr);
292 req_done = true;
293 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000294 }
Asias Hebb811102012-09-25 10:36:17 +0800295 } while (!virtqueue_enable_cb(vq));
Rusty Russelle467cde2007-10-22 11:03:38 +1000296 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800297 if (req_done)
298 blk_start_queue(vblk->disk->queue);
Asias He2c95a322012-05-25 16:03:27 +0800299 spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800300
301 if (bio_done)
302 wake_up(&vblk->queue_wait);
303}
304
Rusty Russelle467cde2007-10-22 11:03:38 +1000305static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
306 struct request *req)
307{
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030308 unsigned int num;
Rusty Russelle467cde2007-10-22 11:03:38 +1000309 struct virtblk_req *vbr;
310
Asias Hea98755c2012-08-08 16:07:04 +0800311 vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
Rusty Russelle467cde2007-10-22 11:03:38 +1000312 if (!vbr)
313 /* When another request finishes we'll try again. */
314 return false;
315
316 vbr->req = req;
Asias Hea98755c2012-08-08 16:07:04 +0800317 vbr->bio = NULL;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900318 if (req->cmd_flags & REQ_FLUSH) {
319 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000320 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200321 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900322 } else {
323 switch (req->cmd_type) {
324 case REQ_TYPE_FS:
325 vbr->out_hdr.type = 0;
326 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
327 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
328 break;
329 case REQ_TYPE_BLOCK_PC:
330 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200331 vbr->out_hdr.sector = 0;
332 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
333 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900334 case REQ_TYPE_SPECIAL:
335 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
336 vbr->out_hdr.sector = 0;
337 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
338 break;
339 default:
340 /* We don't put anything else in the queue. */
341 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200342 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000343 }
344
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030345 num = blk_rq_map_sg(q, vbr->req, vblk->sg);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200346 if (num) {
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030347 if (rq_data_dir(vbr->req) == WRITE)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200348 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030349 else
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200350 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
Rusty Russelle467cde2007-10-22 11:03:38 +1000351 }
352
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030353 if (__virtblk_add_req(vblk->vq, vbr, vblk->sg, num) < 0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000354 mempool_free(vbr, vblk->pool);
355 return false;
356 }
357
Rusty Russelle467cde2007-10-22 11:03:38 +1000358 return true;
359}
360
Asias Hea98755c2012-08-08 16:07:04 +0800361static void virtblk_request(struct request_queue *q)
Rusty Russelle467cde2007-10-22 11:03:38 +1000362{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200363 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000364 struct request *req;
365 unsigned int issued = 0;
366
Tejun Heo9934c8c2009-05-08 11:54:16 +0900367 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600368 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000369
370 /* If this request fails, stop queue and wait for something to
371 finish to restart it. */
372 if (!do_req(q, vblk, req)) {
373 blk_stop_queue(q);
374 break;
375 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900376 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000377 issued++;
378 }
379
380 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300381 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000382}
383
Asias Hea98755c2012-08-08 16:07:04 +0800384static void virtblk_make_request(struct request_queue *q, struct bio *bio)
385{
386 struct virtio_blk *vblk = q->queuedata;
Asias Hea98755c2012-08-08 16:07:04 +0800387 struct virtblk_req *vbr;
388
389 BUG_ON(bio->bi_phys_segments + 2 > vblk->sg_elems);
Asias Hea98755c2012-08-08 16:07:04 +0800390
391 vbr = virtblk_alloc_req(vblk, GFP_NOIO);
392 if (!vbr) {
393 bio_endio(bio, -ENOMEM);
394 return;
395 }
396
397 vbr->bio = bio;
Asias Hec85a1f92012-08-08 16:07:05 +0800398 vbr->flags = 0;
399 if (bio->bi_rw & REQ_FLUSH)
400 vbr->flags |= VBLK_REQ_FLUSH;
401 if (bio->bi_rw & REQ_FUA)
402 vbr->flags |= VBLK_REQ_FUA;
403 if (bio->bi_size)
404 vbr->flags |= VBLK_REQ_DATA;
Asias Hea98755c2012-08-08 16:07:04 +0800405
Asias Hec85a1f92012-08-08 16:07:05 +0800406 if (unlikely(vbr->flags & VBLK_REQ_FLUSH))
407 virtblk_bio_send_flush(vbr);
408 else
409 virtblk_bio_send_data(vbr);
Asias Hea98755c2012-08-08 16:07:04 +0800410}
411
john cooper4cb2ea22010-03-25 01:33:33 -0400412/* return id (s/n) string for *disk to *id_str
413 */
414static int virtblk_get_id(struct gendisk *disk, char *id_str)
415{
416 struct virtio_blk *vblk = disk->private_data;
417 struct request *req;
418 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030419 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400420
421 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
422 GFP_KERNEL);
423 if (IS_ERR(bio))
424 return PTR_ERR(bio);
425
426 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
427 if (IS_ERR(req)) {
428 bio_put(bio);
429 return PTR_ERR(req);
430 }
431
432 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030433 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
434 blk_put_request(req);
435
436 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400437}
438
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200439static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
440 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000441{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200442 struct gendisk *disk = bdev->bd_disk;
443 struct virtio_blk *vblk = disk->private_data;
444
445 /*
446 * Only allow the generic SCSI ioctls if the host can support it.
447 */
448 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200449 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200450
Paolo Bonzini577ebb32012-01-12 16:01:27 +0100451 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
452 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000453}
454
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100455/* We provide getgeo only to please some old bootloader/partitioning tools */
456static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
457{
Ryan Harper48e40432008-04-16 13:56:37 -0500458 struct virtio_blk *vblk = bd->bd_disk->private_data;
459 struct virtio_blk_geometry vgeo;
460 int err;
461
462 /* see if the host passed in geometry config */
463 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
464 offsetof(struct virtio_blk_config, geometry),
465 &vgeo);
466
467 if (!err) {
468 geo->heads = vgeo.heads;
469 geo->sectors = vgeo.sectors;
470 geo->cylinders = vgeo.cylinders;
471 } else {
472 /* some standard values, similar to sd */
473 geo->heads = 1 << 6;
474 geo->sectors = 1 << 5;
475 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
476 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100477 return 0;
478}
479
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700480static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200481 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100482 .owner = THIS_MODULE,
483 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000484};
485
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100486static int index_to_minor(int index)
487{
488 return index << PART_BITS;
489}
490
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200491static int minor_to_index(int minor)
492{
493 return minor >> PART_BITS;
494}
495
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500496static ssize_t virtblk_serial_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
499 struct gendisk *disk = dev_to_disk(dev);
500 int err;
501
502 /* sysfs gives us a PAGE_SIZE buffer */
503 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
504
505 buf[VIRTIO_BLK_ID_BYTES] = '\0';
506 err = virtblk_get_id(disk, buf);
507 if (!err)
508 return strlen(buf);
509
510 if (err == -EIO) /* Unsupported? Make it empty. */
511 return 0;
512
513 return err;
514}
515DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
516
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100517static void virtblk_config_changed_work(struct work_struct *work)
518{
519 struct virtio_blk *vblk =
520 container_of(work, struct virtio_blk, config_work);
521 struct virtio_device *vdev = vblk->vdev;
522 struct request_queue *q = vblk->disk->queue;
523 char cap_str_2[10], cap_str_10[10];
Milos Vyletel9d9598b2013-03-12 15:34:40 +1030524 char *envp[] = { "RESIZE=1", NULL };
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100525 u64 capacity, size;
526
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030527 mutex_lock(&vblk->config_lock);
528 if (!vblk->config_enable)
529 goto done;
530
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100531 /* Host must always specify the capacity. */
532 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
533 &capacity, sizeof(capacity));
534
535 /* If capacity is too big, truncate with warning. */
536 if ((sector_t)capacity != capacity) {
537 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
538 (unsigned long long)capacity);
539 capacity = (sector_t)-1;
540 }
541
542 size = capacity * queue_logical_block_size(q);
543 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
544 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
545
546 dev_notice(&vdev->dev,
547 "new size: %llu %d-byte logical blocks (%s/%s)\n",
548 (unsigned long long)capacity,
549 queue_logical_block_size(q),
550 cap_str_10, cap_str_2);
551
552 set_capacity(vblk->disk, capacity);
Vivek Goyale9986f32012-03-29 10:09:44 +0200553 revalidate_disk(vblk->disk);
Milos Vyletel9d9598b2013-03-12 15:34:40 +1030554 kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030555done:
556 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100557}
558
559static void virtblk_config_changed(struct virtio_device *vdev)
560{
561 struct virtio_blk *vblk = vdev->priv;
562
563 queue_work(virtblk_wq, &vblk->config_work);
564}
565
Amit Shah6abd6e52011-12-22 16:58:29 +0530566static int init_vq(struct virtio_blk *vblk)
567{
568 int err = 0;
569
570 /* We expect one virtqueue, for output. */
Asias Hea98755c2012-08-08 16:07:04 +0800571 vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
Amit Shah6abd6e52011-12-22 16:58:29 +0530572 if (IS_ERR(vblk->vq))
573 err = PTR_ERR(vblk->vq);
574
575 return err;
576}
577
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800578/*
579 * Legacy naming scheme used for virtio devices. We are stuck with it for
580 * virtio blk but don't ever use it for any new driver.
581 */
582static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
583{
584 const int base = 'z' - 'a' + 1;
585 char *begin = buf + strlen(prefix);
586 char *end = buf + buflen;
587 char *p;
588 int unit;
589
590 p = end - 1;
591 *p = '\0';
592 unit = base;
593 do {
594 if (p == begin)
595 return -EINVAL;
596 *--p = 'a' + (index % unit);
597 index = (index / unit) - 1;
598 } while (index >= 0);
599
600 memmove(begin, p, end - p);
601 memcpy(buf, prefix, strlen(prefix));
602
603 return 0;
604}
605
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200606static int virtblk_get_cache_mode(struct virtio_device *vdev)
607{
608 u8 writeback;
609 int err;
610
611 err = virtio_config_val(vdev, VIRTIO_BLK_F_CONFIG_WCE,
612 offsetof(struct virtio_blk_config, wce),
613 &writeback);
614 if (err)
615 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
616
617 return writeback;
618}
619
620static void virtblk_update_cache_mode(struct virtio_device *vdev)
621{
622 u8 writeback = virtblk_get_cache_mode(vdev);
623 struct virtio_blk *vblk = vdev->priv;
624
Asias Hec85a1f92012-08-08 16:07:05 +0800625 if (writeback)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200626 blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
627 else
628 blk_queue_flush(vblk->disk->queue, 0);
629
630 revalidate_disk(vblk->disk);
631}
632
633static const char *const virtblk_cache_types[] = {
634 "write through", "write back"
635};
636
637static ssize_t
638virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
639 const char *buf, size_t count)
640{
641 struct gendisk *disk = dev_to_disk(dev);
642 struct virtio_blk *vblk = disk->private_data;
643 struct virtio_device *vdev = vblk->vdev;
644 int i;
645 u8 writeback;
646
647 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
648 for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
649 if (sysfs_streq(buf, virtblk_cache_types[i]))
650 break;
651
652 if (i < 0)
653 return -EINVAL;
654
655 writeback = i;
656 vdev->config->set(vdev,
657 offsetof(struct virtio_blk_config, wce),
658 &writeback, sizeof(writeback));
659
660 virtblk_update_cache_mode(vdev);
661 return count;
662}
663
664static ssize_t
665virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
666 char *buf)
667{
668 struct gendisk *disk = dev_to_disk(dev);
669 struct virtio_blk *vblk = disk->private_data;
670 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
671
672 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
673 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
674}
675
676static const struct device_attribute dev_attr_cache_type_ro =
677 __ATTR(cache_type, S_IRUGO,
678 virtblk_cache_type_show, NULL);
679static const struct device_attribute dev_attr_cache_type_rw =
680 __ATTR(cache_type, S_IRUGO|S_IWUSR,
681 virtblk_cache_type_show, virtblk_cache_type_store);
682
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800683static int virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000684{
685 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600686 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200687 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800688 int pool_size;
689
Rusty Russelle467cde2007-10-22 11:03:38 +1000690 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600691 u32 v, blk_size, sg_elems, opt_io_size;
692 u16 min_io_size;
693 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000694
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200695 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
696 GFP_KERNEL);
697 if (err < 0)
698 goto out;
699 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100700
Rusty Russell0864b792008-12-30 09:26:05 -0600701 /* We need to know how many segments before we allocate. */
702 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
703 offsetof(struct virtio_blk_config, seg_max),
704 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200705
706 /* We need at least one SG element, whatever they say. */
707 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600708 sg_elems = 1;
709
710 /* We need an extra sg elements at head and tail. */
711 sg_elems += 2;
712 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
713 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000714 if (!vblk) {
715 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200716 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000717 }
718
Asias Hea98755c2012-08-08 16:07:04 +0800719 init_waitqueue_head(&vblk->queue_wait);
Rusty Russelle467cde2007-10-22 11:03:38 +1000720 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600721 vblk->sg_elems = sg_elems;
722 sg_init_table(vblk->sg, vblk->sg_elems);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030723 mutex_init(&vblk->config_lock);
Asias Hea98755c2012-08-08 16:07:04 +0800724
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100725 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030726 vblk->config_enable = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000727
Amit Shah6abd6e52011-12-22 16:58:29 +0530728 err = init_vq(vblk);
729 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000730 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000731
Asias Hea98755c2012-08-08 16:07:04 +0800732 pool_size = sizeof(struct virtblk_req);
733 if (use_bio)
734 pool_size += sizeof(struct scatterlist) * sg_elems;
735 vblk->pool = mempool_create_kmalloc_pool(1, pool_size);
Rusty Russelle467cde2007-10-22 11:03:38 +1000736 if (!vblk->pool) {
737 err = -ENOMEM;
738 goto out_free_vq;
739 }
740
Rusty Russelle467cde2007-10-22 11:03:38 +1000741 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100742 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000743 if (!vblk->disk) {
744 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100745 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000746 }
747
Asias Hea98755c2012-08-08 16:07:04 +0800748 q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600749 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000750 err = -ENOMEM;
751 goto out_put_disk;
752 }
753
Asias Hea98755c2012-08-08 16:07:04 +0800754 if (use_bio)
755 blk_queue_make_request(q, virtblk_make_request);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600756 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900757
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800758 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100759
Rusty Russelle467cde2007-10-22 11:03:38 +1000760 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100761 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000762 vblk->disk->private_data = vblk;
763 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500764 vblk->disk->driverfs_dev = &vdev->dev;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200765 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100766
Tejun Heo02c42b72010-09-03 11:56:18 +0200767 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200768 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000769
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200770 /* If disk is read-only in the host, the guest should obey */
771 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
772 set_disk_ro(vblk->disk, 1);
773
Rusty Russella586d4f2008-02-04 23:49:56 -0500774 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500775 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
776 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000777
778 /* If capacity is too big, truncate with warning. */
779 if ((sector_t)cap != cap) {
780 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
781 (unsigned long long)cap);
782 cap = (sector_t)-1;
783 }
784 set_capacity(vblk->disk, cap);
785
Rusty Russell0864b792008-12-30 09:26:05 -0600786 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500787 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600788
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600789 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600790 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600791
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600792 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500793 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600794
Rusty Russella586d4f2008-02-04 23:49:56 -0500795 /* Host can optionally specify maximum segment size and number of
796 * segments. */
797 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
798 offsetof(struct virtio_blk_config, size_max),
799 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000800 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600801 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600802 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600803 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000804
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200805 /* Host can optionally specify the block size of the device */
806 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
807 offsetof(struct virtio_blk_config, blk_size),
808 &blk_size);
809 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600810 blk_queue_logical_block_size(q, blk_size);
811 else
812 blk_size = queue_logical_block_size(q);
813
814 /* Use topology information if available */
815 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
816 offsetof(struct virtio_blk_config, physical_block_exp),
817 &physical_block_exp);
818 if (!err && physical_block_exp)
819 blk_queue_physical_block_size(q,
820 blk_size * (1 << physical_block_exp));
821
822 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
823 offsetof(struct virtio_blk_config, alignment_offset),
824 &alignment_offset);
825 if (!err && alignment_offset)
826 blk_queue_alignment_offset(q, blk_size * alignment_offset);
827
828 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
829 offsetof(struct virtio_blk_config, min_io_size),
830 &min_io_size);
831 if (!err && min_io_size)
832 blk_queue_io_min(q, blk_size * min_io_size);
833
834 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
835 offsetof(struct virtio_blk_config, opt_io_size),
836 &opt_io_size);
837 if (!err && opt_io_size)
838 blk_queue_io_opt(q, blk_size * opt_io_size);
839
Rusty Russelle467cde2007-10-22 11:03:38 +1000840 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500841 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
842 if (err)
843 goto out_del_disk;
844
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200845 if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
846 err = device_create_file(disk_to_dev(vblk->disk),
847 &dev_attr_cache_type_rw);
848 else
849 err = device_create_file(disk_to_dev(vblk->disk),
850 &dev_attr_cache_type_ro);
851 if (err)
852 goto out_del_disk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000853 return 0;
854
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500855out_del_disk:
856 del_gendisk(vblk->disk);
857 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000858out_put_disk:
859 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000860out_mempool:
861 mempool_destroy(vblk->pool);
862out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600863 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000864out_free_vblk:
865 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200866out_free_index:
867 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000868out:
869 return err;
870}
871
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800872static void virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000873{
874 struct virtio_blk *vblk = vdev->priv;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200875 int index = vblk->index;
Alexander Graff4953fe2013-01-02 15:37:17 +1030876 int refc;
Rusty Russelle467cde2007-10-22 11:03:38 +1000877
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030878 /* Prevent config work handler from accessing the device. */
879 mutex_lock(&vblk->config_lock);
880 vblk->config_enable = false;
881 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100882
Asias He02e2b122012-05-25 10:34:47 +0800883 del_gendisk(vblk->disk);
Asias He483001c2012-05-25 10:34:48 +0800884 blk_cleanup_queue(vblk->disk->queue);
Asias He02e2b122012-05-25 10:34:47 +0800885
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500886 /* Stop all the virtqueues. */
887 vdev->config->reset(vdev);
888
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030889 flush_work(&vblk->config_work);
890
Alexander Graff4953fe2013-01-02 15:37:17 +1030891 refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
Rusty Russelle467cde2007-10-22 11:03:38 +1000892 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000893 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600894 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000895 kfree(vblk);
Alexander Graff4953fe2013-01-02 15:37:17 +1030896
897 /* Only free device id if we don't have any users */
898 if (refc == 1)
899 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000900}
901
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530902#ifdef CONFIG_PM
903static int virtblk_freeze(struct virtio_device *vdev)
904{
905 struct virtio_blk *vblk = vdev->priv;
906
907 /* Ensure we don't receive any more interrupts */
908 vdev->config->reset(vdev);
909
910 /* Prevent config work handler from accessing the device. */
911 mutex_lock(&vblk->config_lock);
912 vblk->config_enable = false;
913 mutex_unlock(&vblk->config_lock);
914
915 flush_work(&vblk->config_work);
916
917 spin_lock_irq(vblk->disk->queue->queue_lock);
918 blk_stop_queue(vblk->disk->queue);
919 spin_unlock_irq(vblk->disk->queue->queue_lock);
920 blk_sync_queue(vblk->disk->queue);
921
922 vdev->config->del_vqs(vdev);
923 return 0;
924}
925
926static int virtblk_restore(struct virtio_device *vdev)
927{
928 struct virtio_blk *vblk = vdev->priv;
929 int ret;
930
931 vblk->config_enable = true;
932 ret = init_vq(vdev->priv);
933 if (!ret) {
934 spin_lock_irq(vblk->disk->queue->queue_lock);
935 blk_start_queue(vblk->disk->queue);
936 spin_unlock_irq(vblk->disk->queue->queue_lock);
937 }
938 return ret;
939}
940#endif
941
Márton Németh47483e22010-01-10 13:40:02 +0100942static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000943 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
944 { 0 },
945};
946
Rusty Russellc45a6812008-05-02 21:50:50 -0500947static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200948 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
949 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200950 VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
Rusty Russellc45a6812008-05-02 21:50:50 -0500951};
952
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800953static struct virtio_driver virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100954 .feature_table = features,
955 .feature_table_size = ARRAY_SIZE(features),
956 .driver.name = KBUILD_MODNAME,
957 .driver.owner = THIS_MODULE,
958 .id_table = id_table,
959 .probe = virtblk_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800960 .remove = virtblk_remove,
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100961 .config_changed = virtblk_config_changed,
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530962#ifdef CONFIG_PM
963 .freeze = virtblk_freeze,
964 .restore = virtblk_restore,
965#endif
Rusty Russelle467cde2007-10-22 11:03:38 +1000966};
967
968static int __init init(void)
969{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100970 int error;
971
972 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
973 if (!virtblk_wq)
974 return -ENOMEM;
975
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100976 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100977 if (major < 0) {
978 error = major;
979 goto out_destroy_workqueue;
980 }
981
982 error = register_virtio_driver(&virtio_blk);
983 if (error)
984 goto out_unregister_blkdev;
985 return 0;
986
987out_unregister_blkdev:
988 unregister_blkdev(major, "virtblk");
989out_destroy_workqueue:
990 destroy_workqueue(virtblk_wq);
991 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000992}
993
994static void __exit fini(void)
995{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100996 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000997 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100998 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000999}
1000module_init(init);
1001module_exit(fini);
1002
1003MODULE_DEVICE_TABLE(virtio, id_table);
1004MODULE_DESCRIPTION("Virtio block driver");
1005MODULE_LICENSE("GPL");