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 static 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 int __virtblk_add_req(struct virtqueue
*vq
,
104 struct virtblk_req
*vbr
,
105 struct scatterlist
*data_sg
,
108 struct scatterlist hdr
, status
, cmd
, sense
, inhdr
, *sgs
[6];
109 unsigned int num_out
= 0, num_in
= 0;
110 int type
= vbr
->out_hdr
.type
& ~VIRTIO_BLK_T_OUT
;
112 sg_init_one(&hdr
, &vbr
->out_hdr
, sizeof(vbr
->out_hdr
));
113 sgs
[num_out
++] = &hdr
;
116 * If this is a packet command we need a couple of additional headers.
117 * Behind the normal outhdr we put a segment with the scsi command
118 * block, and before the normal inhdr we put the sense data and the
119 * inhdr with additional status information.
121 if (type
== VIRTIO_BLK_T_SCSI_CMD
) {
122 sg_init_one(&cmd
, vbr
->req
->cmd
, vbr
->req
->cmd_len
);
123 sgs
[num_out
++] = &cmd
;
127 if (vbr
->out_hdr
.type
& VIRTIO_BLK_T_OUT
)
128 sgs
[num_out
++] = data_sg
;
130 sgs
[num_out
+ num_in
++] = data_sg
;
133 if (type
== VIRTIO_BLK_T_SCSI_CMD
) {
134 sg_init_one(&sense
, vbr
->req
->sense
, SCSI_SENSE_BUFFERSIZE
);
135 sgs
[num_out
+ num_in
++] = &sense
;
136 sg_init_one(&inhdr
, &vbr
->in_hdr
, sizeof(vbr
->in_hdr
));
137 sgs
[num_out
+ num_in
++] = &inhdr
;
140 sg_init_one(&status
, &vbr
->status
, sizeof(vbr
->status
));
141 sgs
[num_out
+ num_in
++] = &status
;
143 return virtqueue_add_sgs(vq
, sgs
, num_out
, num_in
, vbr
, GFP_ATOMIC
);
146 static void virtblk_add_req(struct virtblk_req
*vbr
, bool have_data
)
148 struct virtio_blk
*vblk
= vbr
->vblk
;
152 spin_lock_irq(vblk
->disk
->queue
->queue_lock
);
153 while (unlikely((ret
= __virtblk_add_req(vblk
->vq
, vbr
, vbr
->sg
,
155 prepare_to_wait_exclusive(&vblk
->queue_wait
, &wait
,
156 TASK_UNINTERRUPTIBLE
);
158 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
160 spin_lock_irq(vblk
->disk
->queue
->queue_lock
);
162 finish_wait(&vblk
->queue_wait
, &wait
);
165 virtqueue_kick(vblk
->vq
);
166 spin_unlock_irq(vblk
->disk
->queue
->queue_lock
);
169 static void virtblk_bio_send_flush(struct virtblk_req
*vbr
)
171 vbr
->flags
|= VBLK_IS_FLUSH
;
172 vbr
->out_hdr
.type
= VIRTIO_BLK_T_FLUSH
;
173 vbr
->out_hdr
.sector
= 0;
174 vbr
->out_hdr
.ioprio
= 0;
176 virtblk_add_req(vbr
, false);
179 static void virtblk_bio_send_data(struct virtblk_req
*vbr
)
181 struct virtio_blk
*vblk
= vbr
->vblk
;
182 struct bio
*bio
= vbr
->bio
;
185 vbr
->flags
&= ~VBLK_IS_FLUSH
;
186 vbr
->out_hdr
.type
= 0;
187 vbr
->out_hdr
.sector
= bio
->bi_sector
;
188 vbr
->out_hdr
.ioprio
= bio_prio(bio
);
190 if (blk_bio_map_sg(vblk
->disk
->queue
, bio
, vbr
->sg
)) {
192 if (bio
->bi_rw
& REQ_WRITE
)
193 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_OUT
;
195 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_IN
;
199 virtblk_add_req(vbr
, have_data
);
202 static void virtblk_bio_send_data_work(struct work_struct
*work
)
204 struct virtblk_req
*vbr
;
206 vbr
= container_of(work
, struct virtblk_req
, work
);
208 virtblk_bio_send_data(vbr
);
211 static void virtblk_bio_send_flush_work(struct work_struct
*work
)
213 struct virtblk_req
*vbr
;
215 vbr
= container_of(work
, struct virtblk_req
, work
);
217 virtblk_bio_send_flush(vbr
);
220 static inline void virtblk_request_done(struct virtblk_req
*vbr
)
222 struct virtio_blk
*vblk
= vbr
->vblk
;
223 struct request
*req
= vbr
->req
;
224 int error
= virtblk_result(vbr
);
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);
234 __blk_end_request_all(req
, error
);
235 mempool_free(vbr
, vblk
->pool
);
238 static inline void virtblk_bio_flush_done(struct virtblk_req
*vbr
)
240 struct virtio_blk
*vblk
= vbr
->vblk
;
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
);
247 bio_endio(vbr
->bio
, virtblk_result(vbr
));
248 mempool_free(vbr
, vblk
->pool
);
252 static inline void virtblk_bio_data_done(struct virtblk_req
*vbr
)
254 struct virtio_blk
*vblk
= vbr
->vblk
;
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
);
262 bio_endio(vbr
->bio
, virtblk_result(vbr
));
263 mempool_free(vbr
, vblk
->pool
);
267 static inline void virtblk_bio_done(struct virtblk_req
*vbr
)
269 if (unlikely(vbr
->flags
& VBLK_IS_FLUSH
))
270 virtblk_bio_flush_done(vbr
);
272 virtblk_bio_data_done(vbr
);
275 static void virtblk_done(struct virtqueue
*vq
)
277 struct virtio_blk
*vblk
= vq
->vdev
->priv
;
278 bool bio_done
= false, req_done
= false;
279 struct virtblk_req
*vbr
;
283 spin_lock_irqsave(vblk
->disk
->queue
->queue_lock
, flags
);
285 virtqueue_disable_cb(vq
);
286 while ((vbr
= virtqueue_get_buf(vblk
->vq
, &len
)) != NULL
) {
288 virtblk_bio_done(vbr
);
291 virtblk_request_done(vbr
);
295 } while (!virtqueue_enable_cb(vq
));
296 /* In case queue is stopped waiting for more buffers. */
298 blk_start_queue(vblk
->disk
->queue
);
299 spin_unlock_irqrestore(vblk
->disk
->queue
->queue_lock
, flags
);
302 wake_up(&vblk
->queue_wait
);
305 static bool do_req(struct request_queue
*q
, struct virtio_blk
*vblk
,
309 struct virtblk_req
*vbr
;
311 vbr
= virtblk_alloc_req(vblk
, GFP_ATOMIC
);
313 /* When another request finishes we'll try again. */
318 if (req
->cmd_flags
& REQ_FLUSH
) {
319 vbr
->out_hdr
.type
= VIRTIO_BLK_T_FLUSH
;
320 vbr
->out_hdr
.sector
= 0;
321 vbr
->out_hdr
.ioprio
= req_get_ioprio(vbr
->req
);
323 switch (req
->cmd_type
) {
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
);
329 case REQ_TYPE_BLOCK_PC
:
330 vbr
->out_hdr
.type
= VIRTIO_BLK_T_SCSI_CMD
;
331 vbr
->out_hdr
.sector
= 0;
332 vbr
->out_hdr
.ioprio
= req_get_ioprio(vbr
->req
);
334 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
);
340 /* We don't put anything else in the queue. */
345 num
= blk_rq_map_sg(q
, vbr
->req
, vblk
->sg
);
347 if (rq_data_dir(vbr
->req
) == WRITE
)
348 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_OUT
;
350 vbr
->out_hdr
.type
|= VIRTIO_BLK_T_IN
;
353 if (__virtblk_add_req(vblk
->vq
, vbr
, vblk
->sg
, num
) < 0) {
354 mempool_free(vbr
, vblk
->pool
);
361 static void virtblk_request(struct request_queue
*q
)
363 struct virtio_blk
*vblk
= q
->queuedata
;
365 unsigned int issued
= 0;
367 while ((req
= blk_peek_request(q
)) != NULL
) {
368 BUG_ON(req
->nr_phys_segments
+ 2 > vblk
->sg_elems
);
370 /* If this request fails, stop queue and wait for something to
371 finish to restart it. */
372 if (!do_req(q
, vblk
, req
)) {
376 blk_start_request(req
);
381 virtqueue_kick(vblk
->vq
);
384 static void virtblk_make_request(struct request_queue
*q
, struct bio
*bio
)
386 struct virtio_blk
*vblk
= q
->queuedata
;
387 struct virtblk_req
*vbr
;
389 BUG_ON(bio
->bi_phys_segments
+ 2 > vblk
->sg_elems
);
391 vbr
= virtblk_alloc_req(vblk
, GFP_NOIO
);
393 bio_endio(bio
, -ENOMEM
);
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
;
404 vbr
->flags
|= VBLK_REQ_DATA
;
406 if (unlikely(vbr
->flags
& VBLK_REQ_FLUSH
))
407 virtblk_bio_send_flush(vbr
);
409 virtblk_bio_send_data(vbr
);
412 /* return id (s/n) string for *disk to *id_str
414 static int virtblk_get_id(struct gendisk
*disk
, char *id_str
)
416 struct virtio_blk
*vblk
= disk
->private_data
;
421 bio
= bio_map_kern(vblk
->disk
->queue
, id_str
, VIRTIO_BLK_ID_BYTES
,
426 req
= blk_make_request(vblk
->disk
->queue
, bio
, GFP_KERNEL
);
432 req
->cmd_type
= REQ_TYPE_SPECIAL
;
433 err
= blk_execute_rq(vblk
->disk
->queue
, vblk
->disk
, req
, false);
434 blk_put_request(req
);
439 static int virtblk_ioctl(struct block_device
*bdev
, fmode_t mode
,
440 unsigned int cmd
, unsigned long data
)
442 struct gendisk
*disk
= bdev
->bd_disk
;
443 struct virtio_blk
*vblk
= disk
->private_data
;
446 * Only allow the generic SCSI ioctls if the host can support it.
448 if (!virtio_has_feature(vblk
->vdev
, VIRTIO_BLK_F_SCSI
))
451 return scsi_cmd_blk_ioctl(bdev
, mode
, cmd
,
452 (void __user
*)data
);
455 /* We provide getgeo only to please some old bootloader/partitioning tools */
456 static int virtblk_getgeo(struct block_device
*bd
, struct hd_geometry
*geo
)
458 struct virtio_blk
*vblk
= bd
->bd_disk
->private_data
;
459 struct virtio_blk_geometry vgeo
;
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
),
468 geo
->heads
= vgeo
.heads
;
469 geo
->sectors
= vgeo
.sectors
;
470 geo
->cylinders
= vgeo
.cylinders
;
472 /* some standard values, similar to sd */
474 geo
->sectors
= 1 << 5;
475 geo
->cylinders
= get_capacity(bd
->bd_disk
) >> 11;
480 static const struct block_device_operations virtblk_fops
= {
481 .ioctl
= virtblk_ioctl
,
482 .owner
= THIS_MODULE
,
483 .getgeo
= virtblk_getgeo
,
486 static int index_to_minor(int index
)
488 return index
<< PART_BITS
;
491 static int minor_to_index(int minor
)
493 return minor
>> PART_BITS
;
496 static ssize_t
virtblk_serial_show(struct device
*dev
,
497 struct device_attribute
*attr
, char *buf
)
499 struct gendisk
*disk
= dev_to_disk(dev
);
502 /* sysfs gives us a PAGE_SIZE buffer */
503 BUILD_BUG_ON(PAGE_SIZE
< VIRTIO_BLK_ID_BYTES
);
505 buf
[VIRTIO_BLK_ID_BYTES
] = '\0';
506 err
= virtblk_get_id(disk
, buf
);
510 if (err
== -EIO
) /* Unsupported? Make it empty. */
515 DEVICE_ATTR(serial
, S_IRUGO
, virtblk_serial_show
, NULL
);
517 static void virtblk_config_changed_work(struct work_struct
*work
)
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];
524 char *envp
[] = { "RESIZE=1", NULL
};
527 mutex_lock(&vblk
->config_lock
);
528 if (!vblk
->config_enable
)
531 /* Host must always specify the capacity. */
532 vdev
->config
->get(vdev
, offsetof(struct virtio_blk_config
, capacity
),
533 &capacity
, sizeof(capacity
));
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;
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
));
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
);
552 set_capacity(vblk
->disk
, capacity
);
553 revalidate_disk(vblk
->disk
);
554 kobject_uevent_env(&disk_to_dev(vblk
->disk
)->kobj
, KOBJ_CHANGE
, envp
);
556 mutex_unlock(&vblk
->config_lock
);
559 static void virtblk_config_changed(struct virtio_device
*vdev
)
561 struct virtio_blk
*vblk
= vdev
->priv
;
563 queue_work(virtblk_wq
, &vblk
->config_work
);
566 static int init_vq(struct virtio_blk
*vblk
)
570 /* We expect one virtqueue, for output. */
571 vblk
->vq
= virtio_find_single_vq(vblk
->vdev
, virtblk_done
, "requests");
572 if (IS_ERR(vblk
->vq
))
573 err
= PTR_ERR(vblk
->vq
);
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.
582 static int virtblk_name_format(char *prefix
, int index
, char *buf
, int buflen
)
584 const int base
= 'z' - 'a' + 1;
585 char *begin
= buf
+ strlen(prefix
);
586 char *end
= buf
+ buflen
;
596 *--p
= 'a' + (index
% unit
);
597 index
= (index
/ unit
) - 1;
598 } while (index
>= 0);
600 memmove(begin
, p
, end
- p
);
601 memcpy(buf
, prefix
, strlen(prefix
));
606 static int virtblk_get_cache_mode(struct virtio_device
*vdev
)
611 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_CONFIG_WCE
,
612 offsetof(struct virtio_blk_config
, wce
),
615 writeback
= virtio_has_feature(vdev
, VIRTIO_BLK_F_WCE
);
620 static void virtblk_update_cache_mode(struct virtio_device
*vdev
)
622 u8 writeback
= virtblk_get_cache_mode(vdev
);
623 struct virtio_blk
*vblk
= vdev
->priv
;
626 blk_queue_flush(vblk
->disk
->queue
, REQ_FLUSH
);
628 blk_queue_flush(vblk
->disk
->queue
, 0);
630 revalidate_disk(vblk
->disk
);
633 static const char *const virtblk_cache_types
[] = {
634 "write through", "write back"
638 virtblk_cache_type_store(struct device
*dev
, struct device_attribute
*attr
,
639 const char *buf
, size_t count
)
641 struct gendisk
*disk
= dev_to_disk(dev
);
642 struct virtio_blk
*vblk
= disk
->private_data
;
643 struct virtio_device
*vdev
= vblk
->vdev
;
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
]))
656 vdev
->config
->set(vdev
,
657 offsetof(struct virtio_blk_config
, wce
),
658 &writeback
, sizeof(writeback
));
660 virtblk_update_cache_mode(vdev
);
665 virtblk_cache_type_show(struct device
*dev
, struct device_attribute
*attr
,
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
);
672 BUG_ON(writeback
>= ARRAY_SIZE(virtblk_cache_types
));
673 return snprintf(buf
, 40, "%s\n", virtblk_cache_types
[writeback
]);
676 static const struct device_attribute dev_attr_cache_type_ro
=
677 __ATTR(cache_type
, S_IRUGO
,
678 virtblk_cache_type_show
, NULL
);
679 static 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
);
683 static int virtblk_probe(struct virtio_device
*vdev
)
685 struct virtio_blk
*vblk
;
686 struct request_queue
*q
;
691 u32 v
, blk_size
, sg_elems
, opt_io_size
;
693 u8 physical_block_exp
, alignment_offset
;
695 err
= ida_simple_get(&vd_index_ida
, 0, minor_to_index(1 << MINORBITS
),
701 /* 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
),
706 /* We need at least one SG element, whatever they say. */
707 if (err
|| !sg_elems
)
710 /* We need an extra sg elements at head and tail. */
712 vdev
->priv
= vblk
= kmalloc(sizeof(*vblk
) +
713 sizeof(vblk
->sg
[0]) * sg_elems
, GFP_KERNEL
);
719 init_waitqueue_head(&vblk
->queue_wait
);
721 vblk
->sg_elems
= sg_elems
;
722 sg_init_table(vblk
->sg
, vblk
->sg_elems
);
723 mutex_init(&vblk
->config_lock
);
725 INIT_WORK(&vblk
->config_work
, virtblk_config_changed_work
);
726 vblk
->config_enable
= true;
732 pool_size
= sizeof(struct virtblk_req
);
734 pool_size
+= sizeof(struct scatterlist
) * sg_elems
;
735 vblk
->pool
= mempool_create_kmalloc_pool(1, pool_size
);
741 /* FIXME: How many partitions? How long is a piece of string? */
742 vblk
->disk
= alloc_disk(1 << PART_BITS
);
748 q
= vblk
->disk
->queue
= blk_init_queue(virtblk_request
, NULL
);
755 blk_queue_make_request(q
, virtblk_make_request
);
758 virtblk_name_format("vd", index
, vblk
->disk
->disk_name
, DISK_NAME_LEN
);
760 vblk
->disk
->major
= major
;
761 vblk
->disk
->first_minor
= index_to_minor(index
);
762 vblk
->disk
->private_data
= vblk
;
763 vblk
->disk
->fops
= &virtblk_fops
;
764 vblk
->disk
->driverfs_dev
= &vdev
->dev
;
767 /* configure queue flush support */
768 virtblk_update_cache_mode(vdev
);
770 /* 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);
774 /* Host must always specify the capacity. */
775 vdev
->config
->get(vdev
, offsetof(struct virtio_blk_config
, capacity
),
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
);
784 set_capacity(vblk
->disk
, cap
);
786 /* We can handle whatever the host told us to handle. */
787 blk_queue_max_segments(q
, vblk
->sg_elems
-2);
789 /* No need to bounce any requests */
790 blk_queue_bounce_limit(q
, BLK_BOUNCE_ANY
);
792 /* No real sector limit. */
793 blk_queue_max_hw_sectors(q
, -1U);
795 /* Host can optionally specify maximum segment size and number of
797 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_SIZE_MAX
,
798 offsetof(struct virtio_blk_config
, size_max
),
801 blk_queue_max_segment_size(q
, v
);
803 blk_queue_max_segment_size(q
, -1U);
805 /* 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
),
810 blk_queue_logical_block_size(q
, blk_size
);
812 blk_size
= queue_logical_block_size(q
);
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
));
822 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_TOPOLOGY
,
823 offsetof(struct virtio_blk_config
, alignment_offset
),
825 if (!err
&& alignment_offset
)
826 blk_queue_alignment_offset(q
, blk_size
* alignment_offset
);
828 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_TOPOLOGY
,
829 offsetof(struct virtio_blk_config
, min_io_size
),
831 if (!err
&& min_io_size
)
832 blk_queue_io_min(q
, blk_size
* min_io_size
);
834 err
= virtio_config_val(vdev
, VIRTIO_BLK_F_TOPOLOGY
,
835 offsetof(struct virtio_blk_config
, opt_io_size
),
837 if (!err
&& opt_io_size
)
838 blk_queue_io_opt(q
, blk_size
* opt_io_size
);
840 add_disk(vblk
->disk
);
841 err
= device_create_file(disk_to_dev(vblk
->disk
), &dev_attr_serial
);
845 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
);
849 err
= device_create_file(disk_to_dev(vblk
->disk
),
850 &dev_attr_cache_type_ro
);
856 del_gendisk(vblk
->disk
);
857 blk_cleanup_queue(vblk
->disk
->queue
);
859 put_disk(vblk
->disk
);
861 mempool_destroy(vblk
->pool
);
863 vdev
->config
->del_vqs(vdev
);
867 ida_simple_remove(&vd_index_ida
, index
);
872 static void virtblk_remove(struct virtio_device
*vdev
)
874 struct virtio_blk
*vblk
= vdev
->priv
;
875 int index
= vblk
->index
;
878 /* 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
);
883 del_gendisk(vblk
->disk
);
884 blk_cleanup_queue(vblk
->disk
->queue
);
886 /* Stop all the virtqueues. */
887 vdev
->config
->reset(vdev
);
889 flush_work(&vblk
->config_work
);
891 refc
= atomic_read(&disk_to_dev(vblk
->disk
)->kobj
.kref
.refcount
);
892 put_disk(vblk
->disk
);
893 mempool_destroy(vblk
->pool
);
894 vdev
->config
->del_vqs(vdev
);
897 /* Only free device id if we don't have any users */
899 ida_simple_remove(&vd_index_ida
, index
);
903 static int virtblk_freeze(struct virtio_device
*vdev
)
905 struct virtio_blk
*vblk
= vdev
->priv
;
907 /* Ensure we don't receive any more interrupts */
908 vdev
->config
->reset(vdev
);
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
);
915 flush_work(&vblk
->config_work
);
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
);
922 vdev
->config
->del_vqs(vdev
);
926 static int virtblk_restore(struct virtio_device
*vdev
)
928 struct virtio_blk
*vblk
= vdev
->priv
;
931 vblk
->config_enable
= true;
932 ret
= init_vq(vdev
->priv
);
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
);
942 static const struct virtio_device_id id_table
[] = {
943 { VIRTIO_ID_BLOCK
, VIRTIO_DEV_ANY_ID
},
947 static unsigned int features
[] = {
948 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
,
950 VIRTIO_BLK_F_WCE
, VIRTIO_BLK_F_TOPOLOGY
, VIRTIO_BLK_F_CONFIG_WCE
953 static struct virtio_driver virtio_blk
= {
954 .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
,
960 .remove
= virtblk_remove
,
961 .config_changed
= virtblk_config_changed
,
963 .freeze
= virtblk_freeze
,
964 .restore
= virtblk_restore
,
968 static int __init
init(void)
972 virtblk_wq
= alloc_workqueue("virtio-blk", 0, 0);
976 major
= register_blkdev(0, "virtblk");
979 goto out_destroy_workqueue
;
982 error
= register_virtio_driver(&virtio_blk
);
984 goto out_unregister_blkdev
;
987 out_unregister_blkdev
:
988 unregister_blkdev(major
, "virtblk");
989 out_destroy_workqueue
:
990 destroy_workqueue(virtblk_wq
);
994 static void __exit
fini(void)
996 unregister_blkdev(major
, "virtblk");
997 unregister_virtio_driver(&virtio_blk
);
998 destroy_workqueue(virtblk_wq
);
1003 MODULE_DEVICE_TABLE(virtio
, id_table
);
1004 MODULE_DESCRIPTION("Virtio block driver");
1005 MODULE_LICENSE("GPL");