2 #include <linux/spinlock.h>
3 #include <linux/slab.h>
4 #include <linux/blkdev.h>
5 #include <linux/hdreg.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
8 #include <linux/virtio.h>
9 #include <linux/virtio_blk.h>
10 #include <linux/scatterlist.h>
11 #include <linux/string_helpers.h>
12 #include <scsi/scsi_cmnd.h>
13 #include <linux/idr.h>
18 module_param(use_bio
, bool, S_IRUGO
);
21 static DEFINE_IDA(vd_index_ida
);
23 struct workqueue_struct
*virtblk_wq
;
27 struct virtio_device
*vdev
;
29 wait_queue_head_t queue_wait
;
31 /* The disk structure for the kernel. */
36 /* Process context for config space updates */
37 struct work_struct config_work
;
39 /* Lock for config space updates */
40 struct mutex config_lock
;
42 /* enable config space updates */
45 /* What host tells us, plus 2 for header & tailer. */
46 unsigned int sg_elems
;
48 /* Ida index - used to track minor number allocations. */
51 /* Scatterlist: can be too big for stack. */
52 struct scatterlist sg
[/*sg_elems*/];
59 struct virtio_blk_outhdr out_hdr
;
60 struct virtio_scsi_inhdr in_hdr
;
61 struct work_struct work
;
62 struct virtio_blk
*vblk
;
65 struct scatterlist sg
[];
75 static inline int virtblk_result(struct virtblk_req
*vbr
)
77 switch (vbr
->status
) {
80 case VIRTIO_BLK_S_UNSUPP
:
87 static inline struct virtblk_req
*virtblk_alloc_req(struct virtio_blk
*vblk
,
90 struct virtblk_req
*vbr
;
92 vbr
= mempool_alloc(vblk
->pool
, gfp_mask
);
98 sg_init_table(vbr
->sg
, vblk
->sg_elems
);
103 static void virtblk_add_buf_wait(struct virtio_blk
*vblk
,
104 struct virtblk_req
*vbr
,
111 prepare_to_wait_exclusive(&vblk
->queue_wait
, &wait
,
112 TASK_UNINTERRUPTIBLE
);
114 spin_lock_irq(vblk
->disk
->queue
->queue_lock
);
115 if (virtqueue_add_buf(vblk
->vq
, vbr
->sg
, out
, in
, vbr
,
117 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
120 virtqueue_kick(vblk
->vq
);
121 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
127 finish_wait(&vblk
->queue_wait
, &wait
);
130 static inline void virtblk_add_req(struct virtblk_req
*vbr
,
131 unsigned int out
, unsigned int in
)
133 struct virtio_blk
*vblk
= vbr
->vblk
;
135 spin_lock_irq(vblk
->disk
->queue
->queue_lock
);
136 if (unlikely(virtqueue_add_buf(vblk
->vq
, vbr
->sg
, out
, in
, vbr
,
138 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
139 virtblk_add_buf_wait(vblk
, vbr
, out
, in
);
142 virtqueue_kick(vblk
->vq
);
143 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
146 static int virtblk_bio_send_flush(struct virtblk_req
*vbr
)
148 unsigned int out
= 0, in
= 0;
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
));
157 virtblk_add_req(vbr
, out
, in
);
162 static int virtblk_bio_send_data(struct virtblk_req
*vbr
)
164 struct virtio_blk
*vblk
= vbr
->vblk
;
165 unsigned int num
, out
= 0, in
= 0;
166 struct bio
*bio
= vbr
->bio
;
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
);
173 sg_set_buf(&vbr
->sg
[out
++], &vbr
->out_hdr
, sizeof(vbr
->out_hdr
));
175 num
= blk_bio_map_sg(vblk
->disk
->queue
, bio
, vbr
->sg
+ out
);
177 sg_set_buf(&vbr
->sg
[num
+ out
+ in
++], &vbr
->status
,
178 sizeof(vbr
->status
));
181 if (bio
->bi_rw
& REQ_WRITE
) {
182 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_OUT
;
185 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_IN
;
190 virtblk_add_req(vbr
, out
, in
);
195 static void virtblk_bio_send_data_work(struct work_struct
*work
)
197 struct virtblk_req
*vbr
;
199 vbr
= container_of(work
, struct virtblk_req
, work
);
201 virtblk_bio_send_data(vbr
);
204 static void virtblk_bio_send_flush_work(struct work_struct
*work
)
206 struct virtblk_req
*vbr
;
208 vbr
= container_of(work
, struct virtblk_req
, work
);
210 virtblk_bio_send_flush(vbr
);
213 static inline void virtblk_request_done(struct virtblk_req
*vbr
)
215 struct virtio_blk
*vblk
= vbr
->vblk
;
216 struct request
*req
= vbr
->req
;
217 int error
= virtblk_result(vbr
);
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);
227 __blk_end_request_all(req
, error
);
228 mempool_free(vbr
, vblk
->pool
);
231 static inline void virtblk_bio_flush_done(struct virtblk_req
*vbr
)
233 struct virtio_blk
*vblk
= vbr
->vblk
;
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
);
240 bio_endio(vbr
->bio
, virtblk_result(vbr
));
241 mempool_free(vbr
, vblk
->pool
);
245 static inline void virtblk_bio_data_done(struct virtblk_req
*vbr
)
247 struct virtio_blk
*vblk
= vbr
->vblk
;
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
);
255 bio_endio(vbr
->bio
, virtblk_result(vbr
));
256 mempool_free(vbr
, vblk
->pool
);
260 static inline void virtblk_bio_done(struct virtblk_req
*vbr
)
262 if (unlikely(vbr
->flags
& VBLK_IS_FLUSH
))
263 virtblk_bio_flush_done(vbr
);
265 virtblk_bio_data_done(vbr
);
268 static void virtblk_done(struct virtqueue
*vq
)
270 struct virtio_blk
*vblk
= vq
->vdev
->priv
;
271 bool bio_done
= false, req_done
= false;
272 struct virtblk_req
*vbr
;
276 spin_lock_irqsave(vblk
->disk
->queue
->queue_lock
, flags
);
278 virtqueue_disable_cb(vq
);
279 while ((vbr
= virtqueue_get_buf(vblk
->vq
, &len
)) != NULL
) {
281 virtblk_bio_done(vbr
);
284 virtblk_request_done(vbr
);
288 } while (!virtqueue_enable_cb(vq
));
289 /* In case queue is stopped waiting for more buffers. */
291 blk_start_queue(vblk
->disk
->queue
);
292 spin_unlock_irqrestore(vblk
->disk
->queue
->queue_lock
, flags
);
295 wake_up(&vblk
->queue_wait
);
298 static bool do_req(struct request_queue
*q
, struct virtio_blk
*vblk
,
301 unsigned long num
, out
= 0, in
= 0;
302 struct virtblk_req
*vbr
;
304 vbr
= virtblk_alloc_req(vblk
, GFP_ATOMIC
);
306 /* When another request finishes we'll try again. */
311 if (req
->cmd_flags
& REQ_FLUSH
) {
312 vbr
->out_hdr
.type
= VIRTIO_BLK_T_FLUSH
;
313 vbr
->out_hdr
.sector
= 0;
314 vbr
->out_hdr
.ioprio
= req_get_ioprio(vbr
->req
);
316 switch (req
->cmd_type
) {
318 vbr
->out_hdr
.type
= 0;
319 vbr
->out_hdr
.sector
= blk_rq_pos(vbr
->req
);
320 vbr
->out_hdr
.ioprio
= req_get_ioprio(vbr
->req
);
322 case REQ_TYPE_BLOCK_PC
:
323 vbr
->out_hdr
.type
= VIRTIO_BLK_T_SCSI_CMD
;
324 vbr
->out_hdr
.sector
= 0;
325 vbr
->out_hdr
.ioprio
= req_get_ioprio(vbr
->req
);
327 case REQ_TYPE_SPECIAL
:
328 vbr
->out_hdr
.type
= VIRTIO_BLK_T_GET_ID
;
329 vbr
->out_hdr
.sector
= 0;
330 vbr
->out_hdr
.ioprio
= req_get_ioprio(vbr
->req
);
333 /* We don't put anything else in the queue. */
338 sg_set_buf(&vblk
->sg
[out
++], &vbr
->out_hdr
, sizeof(vbr
->out_hdr
));
341 * If this is a packet command we need a couple of additional headers.
342 * Behind the normal outhdr we put a segment with the scsi command
343 * block, and before the normal inhdr we put the sense data and the
344 * inhdr with additional status information before the normal inhdr.
346 if (vbr
->req
->cmd_type
== REQ_TYPE_BLOCK_PC
)
347 sg_set_buf(&vblk
->sg
[out
++], vbr
->req
->cmd
, vbr
->req
->cmd_len
);
349 num
= blk_rq_map_sg(q
, vbr
->req
, vblk
->sg
+ out
);
351 if (vbr
->req
->cmd_type
== REQ_TYPE_BLOCK_PC
) {
352 sg_set_buf(&vblk
->sg
[num
+ out
+ in
++], vbr
->req
->sense
, SCSI_SENSE_BUFFERSIZE
);
353 sg_set_buf(&vblk
->sg
[num
+ out
+ in
++], &vbr
->in_hdr
,
354 sizeof(vbr
->in_hdr
));
357 sg_set_buf(&vblk
->sg
[num
+ out
+ in
++], &vbr
->status
,
358 sizeof(vbr
->status
));
361 if (rq_data_dir(vbr
->req
) == WRITE
) {
362 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_OUT
;
365 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_IN
;
370 if (virtqueue_add_buf(vblk
->vq
, vblk
->sg
, out
, in
, vbr
,
372 mempool_free(vbr
, vblk
->pool
);
379 static void virtblk_request(struct request_queue
*q
)
381 struct virtio_blk
*vblk
= q
->queuedata
;
383 unsigned int issued
= 0;
385 while ((req
= blk_peek_request(q
)) != NULL
) {
386 BUG_ON(req
->nr_phys_segments
+ 2 > vblk
->sg_elems
);
388 /* If this request fails, stop queue and wait for something to
389 finish to restart it. */
390 if (!do_req(q
, vblk
, req
)) {
394 blk_start_request(req
);
399 virtqueue_kick(vblk
->vq
);
402 static void virtblk_make_request(struct request_queue
*q
, struct bio
*bio
)
404 struct virtio_blk
*vblk
= q
->queuedata
;
405 struct virtblk_req
*vbr
;
407 BUG_ON(bio
->bi_phys_segments
+ 2 > vblk
->sg_elems
);
409 vbr
= virtblk_alloc_req(vblk
, GFP_NOIO
);
411 bio_endio(bio
, -ENOMEM
);
417 if (bio
->bi_rw
& REQ_FLUSH
)
418 vbr
->flags
|= VBLK_REQ_FLUSH
;
419 if (bio
->bi_rw
& REQ_FUA
)
420 vbr
->flags
|= VBLK_REQ_FUA
;
422 vbr
->flags
|= VBLK_REQ_DATA
;
424 if (unlikely(vbr
->flags
& VBLK_REQ_FLUSH
))
425 virtblk_bio_send_flush(vbr
);
427 virtblk_bio_send_data(vbr
);
430 /* return id (s/n) string for *disk to *id_str
432 static int virtblk_get_id(struct gendisk
*disk
, char *id_str
)
434 struct virtio_blk
*vblk
= disk
->private_data
;
439 bio
= bio_map_kern(vblk
->disk
->queue
, id_str
, VIRTIO_BLK_ID_BYTES
,
444 req
= blk_make_request(vblk
->disk
->queue
, bio
, GFP_KERNEL
);
450 req
->cmd_type
= REQ_TYPE_SPECIAL
;
451 err
= blk_execute_rq(vblk
->disk
->queue
, vblk
->disk
, req
, false);
452 blk_put_request(req
);
457 static int virtblk_ioctl(struct block_device
*bdev
, fmode_t mode
,
458 unsigned int cmd
, unsigned long data
)
460 struct gendisk
*disk
= bdev
->bd_disk
;
461 struct virtio_blk
*vblk
= disk
->private_data
;
464 * Only allow the generic SCSI ioctls if the host can support it.
466 if (!virtio_has_feature(vblk
->vdev
, VIRTIO_BLK_F_SCSI
))
469 return scsi_cmd_blk_ioctl(bdev
, mode
, cmd
,
470 (void __user
*)data
);
473 /* We provide getgeo only to please some old bootloader/partitioning tools */
474 static int virtblk_getgeo(struct block_device
*bd
, struct hd_geometry
*geo
)
476 struct virtio_blk
*vblk
= bd
->bd_disk
->private_data
;
477 struct virtio_blk_geometry vgeo
;
480 /* see if the host passed in geometry config */
481 err
= virtio_config_val(vblk
->vdev
, VIRTIO_BLK_F_GEOMETRY
,
482 offsetof(struct virtio_blk_config
, geometry
),
486 geo
->heads
= vgeo
.heads
;
487 geo
->sectors
= vgeo
.sectors
;
488 geo
->cylinders
= vgeo
.cylinders
;
490 /* some standard values, similar to sd */
492 geo
->sectors
= 1 << 5;
493 geo
->cylinders
= get_capacity(bd
->bd_disk
) >> 11;
498 static const struct block_device_operations virtblk_fops
= {
499 .ioctl
= virtblk_ioctl
,
500 .owner
= THIS_MODULE
,
501 .getgeo
= virtblk_getgeo
,
504 static int index_to_minor(int index
)
506 return index
<< PART_BITS
;
509 static int minor_to_index(int minor
)
511 return minor
>> PART_BITS
;
514 static ssize_t
virtblk_serial_show(struct device
*dev
,
515 struct device_attribute
*attr
, char *buf
)
517 struct gendisk
*disk
= dev_to_disk(dev
);
520 /* sysfs gives us a PAGE_SIZE buffer */
521 BUILD_BUG_ON(PAGE_SIZE
< VIRTIO_BLK_ID_BYTES
);
523 buf
[VIRTIO_BLK_ID_BYTES
] = '\0';
524 err
= virtblk_get_id(disk
, buf
);
528 if (err
== -EIO
) /* Unsupported? Make it empty. */
533 DEVICE_ATTR(serial
, S_IRUGO
, virtblk_serial_show
, NULL
);
535 static void virtblk_config_changed_work(struct work_struct
*work
)
537 struct virtio_blk
*vblk
=
538 container_of(work
, struct virtio_blk
, config_work
);
539 struct virtio_device
*vdev
= vblk
->vdev
;
540 struct request_queue
*q
= vblk
->disk
->queue
;
541 char cap_str_2
[10], cap_str_10
[10];
544 mutex_lock(&vblk
->config_lock
);
545 if (!vblk
->config_enable
)
548 /* Host must always specify the capacity. */
549 vdev
->config
->get(vdev
, offsetof(struct virtio_blk_config
, capacity
),
550 &capacity
, sizeof(capacity
));
552 /* If capacity is too big, truncate with warning. */
553 if ((sector_t
)capacity
!= capacity
) {
554 dev_warn(&vdev
->dev
, "Capacity %llu too large: truncating\n",
555 (unsigned long long)capacity
);
556 capacity
= (sector_t
)-1;
559 size
= capacity
* queue_logical_block_size(q
);
560 string_get_size(size
, STRING_UNITS_2
, cap_str_2
, sizeof(cap_str_2
));
561 string_get_size(size
, STRING_UNITS_10
, cap_str_10
, sizeof(cap_str_10
));
563 dev_notice(&vdev
->dev
,
564 "new size: %llu %d-byte logical blocks (%s/%s)\n",
565 (unsigned long long)capacity
,
566 queue_logical_block_size(q
),
567 cap_str_10
, cap_str_2
);
569 set_capacity(vblk
->disk
, capacity
);
570 revalidate_disk(vblk
->disk
);
572 mutex_unlock(&vblk
->config_lock
);
575 static void virtblk_config_changed(struct virtio_device
*vdev
)
577 struct virtio_blk
*vblk
= vdev
->priv
;
579 queue_work(virtblk_wq
, &vblk
->config_work
);
582 static int init_vq(struct virtio_blk
*vblk
)
586 /* We expect one virtqueue, for output. */
587 vblk
->vq
= virtio_find_single_vq(vblk
->vdev
, virtblk_done
, "requests");
588 if (IS_ERR(vblk
->vq
))
589 err
= PTR_ERR(vblk
->vq
);
595 * Legacy naming scheme used for virtio devices. We are stuck with it for
596 * virtio blk but don't ever use it for any new driver.
598 static int virtblk_name_format(char *prefix
, int index
, char *buf
, int buflen
)
600 const int base
= 'z' - 'a' + 1;
601 char *begin
= buf
+ strlen(prefix
);
602 char *end
= buf
+ buflen
;
612 *--p
= 'a' + (index
% unit
);
613 index
= (index
/ unit
) - 1;
614 } while (index
>= 0);
616 memmove(begin
, p
, end
- p
);
617 memcpy(buf
, prefix
, strlen(prefix
));
622 static int virtblk_get_cache_mode(struct virtio_device
*vdev
)
627 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_CONFIG_WCE
,
628 offsetof(struct virtio_blk_config
, wce
),
631 writeback
= virtio_has_feature(vdev
, VIRTIO_BLK_F_WCE
);
636 static void virtblk_update_cache_mode(struct virtio_device
*vdev
)
638 u8 writeback
= virtblk_get_cache_mode(vdev
);
639 struct virtio_blk
*vblk
= vdev
->priv
;
642 blk_queue_flush(vblk
->disk
->queue
, REQ_FLUSH
);
644 blk_queue_flush(vblk
->disk
->queue
, 0);
646 revalidate_disk(vblk
->disk
);
649 static const char *const virtblk_cache_types
[] = {
650 "write through", "write back"
654 virtblk_cache_type_store(struct device
*dev
, struct device_attribute
*attr
,
655 const char *buf
, size_t count
)
657 struct gendisk
*disk
= dev_to_disk(dev
);
658 struct virtio_blk
*vblk
= disk
->private_data
;
659 struct virtio_device
*vdev
= vblk
->vdev
;
663 BUG_ON(!virtio_has_feature(vblk
->vdev
, VIRTIO_BLK_F_CONFIG_WCE
));
664 for (i
= ARRAY_SIZE(virtblk_cache_types
); --i
>= 0; )
665 if (sysfs_streq(buf
, virtblk_cache_types
[i
]))
672 vdev
->config
->set(vdev
,
673 offsetof(struct virtio_blk_config
, wce
),
674 &writeback
, sizeof(writeback
));
676 virtblk_update_cache_mode(vdev
);
681 virtblk_cache_type_show(struct device
*dev
, struct device_attribute
*attr
,
684 struct gendisk
*disk
= dev_to_disk(dev
);
685 struct virtio_blk
*vblk
= disk
->private_data
;
686 u8 writeback
= virtblk_get_cache_mode(vblk
->vdev
);
688 BUG_ON(writeback
>= ARRAY_SIZE(virtblk_cache_types
));
689 return snprintf(buf
, 40, "%s\n", virtblk_cache_types
[writeback
]);
692 static const struct device_attribute dev_attr_cache_type_ro
=
693 __ATTR(cache_type
, S_IRUGO
,
694 virtblk_cache_type_show
, NULL
);
695 static const struct device_attribute dev_attr_cache_type_rw
=
696 __ATTR(cache_type
, S_IRUGO
|S_IWUSR
,
697 virtblk_cache_type_show
, virtblk_cache_type_store
);
699 static int __devinit
virtblk_probe(struct virtio_device
*vdev
)
701 struct virtio_blk
*vblk
;
702 struct request_queue
*q
;
707 u32 v
, blk_size
, sg_elems
, opt_io_size
;
709 u8 physical_block_exp
, alignment_offset
;
711 err
= ida_simple_get(&vd_index_ida
, 0, minor_to_index(1 << MINORBITS
),
717 /* We need to know how many segments before we allocate. */
718 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_SEG_MAX
,
719 offsetof(struct virtio_blk_config
, seg_max
),
722 /* We need at least one SG element, whatever they say. */
723 if (err
|| !sg_elems
)
726 /* We need an extra sg elements at head and tail. */
728 vdev
->priv
= vblk
= kmalloc(sizeof(*vblk
) +
729 sizeof(vblk
->sg
[0]) * sg_elems
, GFP_KERNEL
);
735 init_waitqueue_head(&vblk
->queue_wait
);
737 vblk
->sg_elems
= sg_elems
;
738 sg_init_table(vblk
->sg
, vblk
->sg_elems
);
739 mutex_init(&vblk
->config_lock
);
741 INIT_WORK(&vblk
->config_work
, virtblk_config_changed_work
);
742 vblk
->config_enable
= true;
748 pool_size
= sizeof(struct virtblk_req
);
750 pool_size
+= sizeof(struct scatterlist
) * sg_elems
;
751 vblk
->pool
= mempool_create_kmalloc_pool(1, pool_size
);
757 /* FIXME: How many partitions? How long is a piece of string? */
758 vblk
->disk
= alloc_disk(1 << PART_BITS
);
764 q
= vblk
->disk
->queue
= blk_init_queue(virtblk_request
, NULL
);
771 blk_queue_make_request(q
, virtblk_make_request
);
774 virtblk_name_format("vd", index
, vblk
->disk
->disk_name
, DISK_NAME_LEN
);
776 vblk
->disk
->major
= major
;
777 vblk
->disk
->first_minor
= index_to_minor(index
);
778 vblk
->disk
->private_data
= vblk
;
779 vblk
->disk
->fops
= &virtblk_fops
;
780 vblk
->disk
->driverfs_dev
= &vdev
->dev
;
783 /* configure queue flush support */
784 virtblk_update_cache_mode(vdev
);
786 /* If disk is read-only in the host, the guest should obey */
787 if (virtio_has_feature(vdev
, VIRTIO_BLK_F_RO
))
788 set_disk_ro(vblk
->disk
, 1);
790 /* Host must always specify the capacity. */
791 vdev
->config
->get(vdev
, offsetof(struct virtio_blk_config
, capacity
),
794 /* If capacity is too big, truncate with warning. */
795 if ((sector_t
)cap
!= cap
) {
796 dev_warn(&vdev
->dev
, "Capacity %llu too large: truncating\n",
797 (unsigned long long)cap
);
800 set_capacity(vblk
->disk
, cap
);
802 /* We can handle whatever the host told us to handle. */
803 blk_queue_max_segments(q
, vblk
->sg_elems
-2);
805 /* No need to bounce any requests */
806 blk_queue_bounce_limit(q
, BLK_BOUNCE_ANY
);
808 /* No real sector limit. */
809 blk_queue_max_hw_sectors(q
, -1U);
811 /* Host can optionally specify maximum segment size and number of
813 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_SIZE_MAX
,
814 offsetof(struct virtio_blk_config
, size_max
),
817 blk_queue_max_segment_size(q
, v
);
819 blk_queue_max_segment_size(q
, -1U);
821 /* Host can optionally specify the block size of the device */
822 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_BLK_SIZE
,
823 offsetof(struct virtio_blk_config
, blk_size
),
826 blk_queue_logical_block_size(q
, blk_size
);
828 blk_size
= queue_logical_block_size(q
);
830 /* Use topology information if available */
831 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_TOPOLOGY
,
832 offsetof(struct virtio_blk_config
, physical_block_exp
),
833 &physical_block_exp
);
834 if (!err
&& physical_block_exp
)
835 blk_queue_physical_block_size(q
,
836 blk_size
* (1 << physical_block_exp
));
838 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_TOPOLOGY
,
839 offsetof(struct virtio_blk_config
, alignment_offset
),
841 if (!err
&& alignment_offset
)
842 blk_queue_alignment_offset(q
, blk_size
* alignment_offset
);
844 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_TOPOLOGY
,
845 offsetof(struct virtio_blk_config
, min_io_size
),
847 if (!err
&& min_io_size
)
848 blk_queue_io_min(q
, blk_size
* min_io_size
);
850 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_TOPOLOGY
,
851 offsetof(struct virtio_blk_config
, opt_io_size
),
853 if (!err
&& opt_io_size
)
854 blk_queue_io_opt(q
, blk_size
* opt_io_size
);
856 add_disk(vblk
->disk
);
857 err
= device_create_file(disk_to_dev(vblk
->disk
), &dev_attr_serial
);
861 if (virtio_has_feature(vdev
, VIRTIO_BLK_F_CONFIG_WCE
))
862 err
= device_create_file(disk_to_dev(vblk
->disk
),
863 &dev_attr_cache_type_rw
);
865 err
= device_create_file(disk_to_dev(vblk
->disk
),
866 &dev_attr_cache_type_ro
);
872 del_gendisk(vblk
->disk
);
873 blk_cleanup_queue(vblk
->disk
->queue
);
875 put_disk(vblk
->disk
);
877 mempool_destroy(vblk
->pool
);
879 vdev
->config
->del_vqs(vdev
);
883 ida_simple_remove(&vd_index_ida
, index
);
888 static void __devexit
virtblk_remove(struct virtio_device
*vdev
)
890 struct virtio_blk
*vblk
= vdev
->priv
;
891 int index
= vblk
->index
;
893 /* Prevent config work handler from accessing the device. */
894 mutex_lock(&vblk
->config_lock
);
895 vblk
->config_enable
= false;
896 mutex_unlock(&vblk
->config_lock
);
898 del_gendisk(vblk
->disk
);
899 blk_cleanup_queue(vblk
->disk
->queue
);
901 /* Stop all the virtqueues. */
902 vdev
->config
->reset(vdev
);
904 flush_work(&vblk
->config_work
);
906 put_disk(vblk
->disk
);
907 mempool_destroy(vblk
->pool
);
908 vdev
->config
->del_vqs(vdev
);
910 ida_simple_remove(&vd_index_ida
, index
);
914 static int virtblk_freeze(struct virtio_device
*vdev
)
916 struct virtio_blk
*vblk
= vdev
->priv
;
918 /* Ensure we don't receive any more interrupts */
919 vdev
->config
->reset(vdev
);
921 /* Prevent config work handler from accessing the device. */
922 mutex_lock(&vblk
->config_lock
);
923 vblk
->config_enable
= false;
924 mutex_unlock(&vblk
->config_lock
);
926 flush_work(&vblk
->config_work
);
928 spin_lock_irq(vblk
->disk
->queue
->queue_lock
);
929 blk_stop_queue(vblk
->disk
->queue
);
930 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
931 blk_sync_queue(vblk
->disk
->queue
);
933 vdev
->config
->del_vqs(vdev
);
937 static int virtblk_restore(struct virtio_device
*vdev
)
939 struct virtio_blk
*vblk
= vdev
->priv
;
942 vblk
->config_enable
= true;
943 ret
= init_vq(vdev
->priv
);
945 spin_lock_irq(vblk
->disk
->queue
->queue_lock
);
946 blk_start_queue(vblk
->disk
->queue
);
947 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
953 static const struct virtio_device_id id_table
[] = {
954 { VIRTIO_ID_BLOCK
, VIRTIO_DEV_ANY_ID
},
958 static unsigned int features
[] = {
959 VIRTIO_BLK_F_SEG_MAX
, VIRTIO_BLK_F_SIZE_MAX
, VIRTIO_BLK_F_GEOMETRY
,
960 VIRTIO_BLK_F_RO
, VIRTIO_BLK_F_BLK_SIZE
, VIRTIO_BLK_F_SCSI
,
961 VIRTIO_BLK_F_WCE
, VIRTIO_BLK_F_TOPOLOGY
, VIRTIO_BLK_F_CONFIG_WCE
965 * virtio_blk causes spurious section mismatch warning by
966 * simultaneously referring to a __devinit and a __devexit function.
967 * Use __refdata to avoid this warning.
969 static struct virtio_driver __refdata virtio_blk
= {
970 .feature_table
= features
,
971 .feature_table_size
= ARRAY_SIZE(features
),
972 .driver
.name
= KBUILD_MODNAME
,
973 .driver
.owner
= THIS_MODULE
,
974 .id_table
= id_table
,
975 .probe
= virtblk_probe
,
976 .remove
= __devexit_p(virtblk_remove
),
977 .config_changed
= virtblk_config_changed
,
979 .freeze
= virtblk_freeze
,
980 .restore
= virtblk_restore
,
984 static int __init
init(void)
988 virtblk_wq
= alloc_workqueue("virtio-blk", 0, 0);
992 major
= register_blkdev(0, "virtblk");
995 goto out_destroy_workqueue
;
998 error
= register_virtio_driver(&virtio_blk
);
1000 goto out_unregister_blkdev
;
1003 out_unregister_blkdev
:
1004 unregister_blkdev(major
, "virtblk");
1005 out_destroy_workqueue
:
1006 destroy_workqueue(virtblk_wq
);
1010 static void __exit
fini(void)
1012 unregister_blkdev(major
, "virtblk");
1013 unregister_virtio_driver(&virtio_blk
);
1014 destroy_workqueue(virtblk_wq
);
1019 MODULE_DEVICE_TABLE(virtio
, id_table
);
1020 MODULE_DESCRIPTION("Virtio block driver");
1021 MODULE_LICENSE("GPL");