Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 1 | //#define DEBUG |
| 2 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 3 | #include <linux/slab.h> |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 4 | #include <linux/blkdev.h> |
| 5 | #include <linux/hdreg.h> |
Paul Gortmaker | 0c8d44f | 2011-07-01 15:56:05 -0400 | [diff] [blame] | 6 | #include <linux/module.h> |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 7 | #include <linux/mutex.h> |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 8 | #include <linux/virtio.h> |
| 9 | #include <linux/virtio_blk.h> |
Jens Axboe | 3d1266c | 2007-10-24 13:21:21 +0200 | [diff] [blame] | 10 | #include <linux/scatterlist.h> |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 11 | #include <linux/string_helpers.h> |
Liu Yuan | 6917f83 | 2011-04-24 02:49:26 +0800 | [diff] [blame] | 12 | #include <scsi/scsi_cmnd.h> |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 13 | #include <linux/idr.h> |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 14 | #include <linux/blk-mq.h> |
| 15 | #include <linux/numa.h> |
Jens Axboe | 3d1266c | 2007-10-24 13:21:21 +0200 | [diff] [blame] | 16 | |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 17 | #define PART_BITS 4 |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 18 | |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 19 | static int major; |
| 20 | static DEFINE_IDA(vd_index_ida); |
| 21 | |
Jonghwan Choi | 2a647bf | 2013-05-20 10:25:39 +0930 | [diff] [blame] | 22 | static struct workqueue_struct *virtblk_wq; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 23 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 24 | struct virtio_blk |
| 25 | { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 26 | struct virtio_device *vdev; |
| 27 | struct virtqueue *vq; |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 28 | spinlock_t vq_lock; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 29 | |
| 30 | /* The disk structure for the kernel. */ |
| 31 | struct gendisk *disk; |
| 32 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 33 | /* Block layer tags. */ |
| 34 | struct blk_mq_tag_set tag_set; |
| 35 | |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 36 | /* Process context for config space updates */ |
| 37 | struct work_struct config_work; |
| 38 | |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 39 | /* Lock for config space updates */ |
| 40 | struct mutex config_lock; |
| 41 | |
| 42 | /* enable config space updates */ |
| 43 | bool config_enable; |
| 44 | |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 45 | /* What host tells us, plus 2 for header & tailer. */ |
| 46 | unsigned int sg_elems; |
| 47 | |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 48 | /* Ida index - used to track minor number allocations. */ |
| 49 | int index; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct virtblk_req |
| 53 | { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 54 | struct request *req; |
| 55 | struct virtio_blk_outhdr out_hdr; |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 56 | struct virtio_scsi_inhdr in_hdr; |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 57 | u8 status; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 58 | struct scatterlist sg[]; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 59 | }; |
| 60 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 61 | static inline int virtblk_result(struct virtblk_req *vbr) |
| 62 | { |
| 63 | switch (vbr->status) { |
| 64 | case VIRTIO_BLK_S_OK: |
| 65 | return 0; |
| 66 | case VIRTIO_BLK_S_UNSUPP: |
| 67 | return -ENOTTY; |
| 68 | default: |
| 69 | return -EIO; |
| 70 | } |
| 71 | } |
| 72 | |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 73 | static int __virtblk_add_req(struct virtqueue *vq, |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 74 | struct virtblk_req *vbr, |
| 75 | struct scatterlist *data_sg, |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 76 | bool have_data) |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 77 | { |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 78 | struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6]; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 79 | unsigned int num_out = 0, num_in = 0; |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 80 | int type = vbr->out_hdr.type & ~VIRTIO_BLK_T_OUT; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 81 | |
| 82 | sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr)); |
| 83 | sgs[num_out++] = &hdr; |
| 84 | |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 85 | /* |
| 86 | * If this is a packet command we need a couple of additional headers. |
| 87 | * Behind the normal outhdr we put a segment with the scsi command |
| 88 | * block, and before the normal inhdr we put the sense data and the |
| 89 | * inhdr with additional status information. |
| 90 | */ |
| 91 | if (type == VIRTIO_BLK_T_SCSI_CMD) { |
| 92 | sg_init_one(&cmd, vbr->req->cmd, vbr->req->cmd_len); |
| 93 | sgs[num_out++] = &cmd; |
| 94 | } |
| 95 | |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 96 | if (have_data) { |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 97 | if (vbr->out_hdr.type & VIRTIO_BLK_T_OUT) |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 98 | sgs[num_out++] = data_sg; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 99 | else |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 100 | sgs[num_out + num_in++] = data_sg; |
| 101 | } |
| 102 | |
| 103 | if (type == VIRTIO_BLK_T_SCSI_CMD) { |
| 104 | sg_init_one(&sense, vbr->req->sense, SCSI_SENSE_BUFFERSIZE); |
| 105 | sgs[num_out + num_in++] = &sense; |
| 106 | sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr)); |
| 107 | sgs[num_out + num_in++] = &inhdr; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | sg_init_one(&status, &vbr->status, sizeof(vbr->status)); |
| 111 | sgs[num_out + num_in++] = &status; |
| 112 | |
| 113 | return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC); |
Paolo Bonzini | 5ee21a5 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 114 | } |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 115 | |
Christoph Hellwig | 5124c28 | 2014-02-10 03:24:39 -0800 | [diff] [blame] | 116 | static inline void virtblk_request_done(struct request *req) |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 117 | { |
Christoph Hellwig | 9d74e25 | 2014-04-14 10:30:07 +0200 | [diff] [blame] | 118 | struct virtblk_req *vbr = blk_mq_rq_to_pdu(req); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 119 | int error = virtblk_result(vbr); |
| 120 | |
| 121 | if (req->cmd_type == REQ_TYPE_BLOCK_PC) { |
| 122 | req->resid_len = vbr->in_hdr.residual; |
| 123 | req->sense_len = vbr->in_hdr.sense_len; |
| 124 | req->errors = vbr->in_hdr.errors; |
| 125 | } else if (req->cmd_type == REQ_TYPE_SPECIAL) { |
| 126 | req->errors = (error != 0); |
| 127 | } |
| 128 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 129 | blk_mq_end_io(req, error); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void virtblk_done(struct virtqueue *vq) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 133 | { |
| 134 | struct virtio_blk *vblk = vq->vdev->priv; |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 135 | bool req_done = false; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 136 | struct virtblk_req *vbr; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 137 | unsigned long flags; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 138 | unsigned int len; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 139 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 140 | spin_lock_irqsave(&vblk->vq_lock, flags); |
Asias He | bb81110 | 2012-09-25 10:36:17 +0800 | [diff] [blame] | 141 | do { |
| 142 | virtqueue_disable_cb(vq); |
| 143 | while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) { |
Christoph Hellwig | 5124c28 | 2014-02-10 03:24:39 -0800 | [diff] [blame] | 144 | blk_mq_complete_request(vbr->req); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 145 | req_done = true; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 146 | } |
Heinz Graalfs | 7f03b17 | 2013-10-29 09:40:30 +1030 | [diff] [blame] | 147 | if (unlikely(virtqueue_is_broken(vq))) |
| 148 | break; |
Asias He | bb81110 | 2012-09-25 10:36:17 +0800 | [diff] [blame] | 149 | } while (!virtqueue_enable_cb(vq)); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 150 | spin_unlock_irqrestore(&vblk->vq_lock, flags); |
| 151 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 152 | /* In case queue is stopped waiting for more buffers. */ |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 153 | if (req_done) |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame^] | 154 | blk_mq_start_stopped_hw_queues(vblk->disk->queue, true); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 155 | } |
| 156 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 157 | static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 158 | { |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 159 | struct virtio_blk *vblk = hctx->queue->queuedata; |
Christoph Hellwig | 9d74e25 | 2014-04-14 10:30:07 +0200 | [diff] [blame] | 160 | struct virtblk_req *vbr = blk_mq_rq_to_pdu(req); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 161 | unsigned long flags; |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 162 | unsigned int num; |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 163 | const bool last = (req->cmd_flags & REQ_END) != 0; |
Rusty Russell | 5261b85 | 2014-03-13 11:23:39 +1030 | [diff] [blame] | 164 | int err; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 165 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 166 | BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 167 | |
| 168 | vbr->req = req; |
FUJITA Tomonori | dd40e45 | 2010-07-03 17:45:38 +0900 | [diff] [blame] | 169 | if (req->cmd_flags & REQ_FLUSH) { |
| 170 | vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 171 | vbr->out_hdr.sector = 0; |
Fernando Luis Vázquez Cao | 766ca44 | 2008-08-14 09:59:13 +0200 | [diff] [blame] | 172 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
FUJITA Tomonori | dd40e45 | 2010-07-03 17:45:38 +0900 | [diff] [blame] | 173 | } else { |
| 174 | switch (req->cmd_type) { |
| 175 | case REQ_TYPE_FS: |
| 176 | vbr->out_hdr.type = 0; |
| 177 | vbr->out_hdr.sector = blk_rq_pos(vbr->req); |
| 178 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
| 179 | break; |
| 180 | case REQ_TYPE_BLOCK_PC: |
| 181 | vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD; |
Christoph Hellwig | f1b0ef06 | 2009-09-17 19:57:42 +0200 | [diff] [blame] | 182 | vbr->out_hdr.sector = 0; |
| 183 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
| 184 | break; |
FUJITA Tomonori | dd40e45 | 2010-07-03 17:45:38 +0900 | [diff] [blame] | 185 | case REQ_TYPE_SPECIAL: |
| 186 | vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID; |
| 187 | vbr->out_hdr.sector = 0; |
| 188 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
| 189 | break; |
| 190 | default: |
| 191 | /* We don't put anything else in the queue. */ |
| 192 | BUG(); |
Christoph Hellwig | f1b0ef06 | 2009-09-17 19:57:42 +0200 | [diff] [blame] | 193 | } |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 194 | } |
| 195 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 196 | num = blk_rq_map_sg(hctx->queue, vbr->req, vbr->sg); |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 197 | if (num) { |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 198 | if (rq_data_dir(vbr->req) == WRITE) |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 199 | vbr->out_hdr.type |= VIRTIO_BLK_T_OUT; |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 200 | else |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 201 | vbr->out_hdr.type |= VIRTIO_BLK_T_IN; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 202 | } |
| 203 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 204 | spin_lock_irqsave(&vblk->vq_lock, flags); |
Rusty Russell | 5261b85 | 2014-03-13 11:23:39 +1030 | [diff] [blame] | 205 | err = __virtblk_add_req(vblk->vq, vbr, vbr->sg, num); |
| 206 | if (err) { |
Shaohua Li | f02b9ac | 2013-11-19 18:57:24 -0700 | [diff] [blame] | 207 | virtqueue_kick(vblk->vq); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 208 | spin_unlock_irqrestore(&vblk->vq_lock, flags); |
| 209 | blk_mq_stop_hw_queue(hctx); |
Rusty Russell | 5261b85 | 2014-03-13 11:23:39 +1030 | [diff] [blame] | 210 | /* Out of mem doesn't actually happen, since we fall back |
| 211 | * to direct descriptors */ |
| 212 | if (err == -ENOMEM || err == -ENOSPC) |
| 213 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 214 | return BLK_MQ_RQ_QUEUE_ERROR; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 215 | } |
| 216 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 217 | if (last) |
| 218 | virtqueue_kick(vblk->vq); |
Shaohua Li | f02b9ac | 2013-11-19 18:57:24 -0700 | [diff] [blame] | 219 | |
| 220 | spin_unlock_irqrestore(&vblk->vq_lock, flags); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 221 | return BLK_MQ_RQ_QUEUE_OK; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 222 | } |
| 223 | |
john cooper | 4cb2ea2 | 2010-03-25 01:33:33 -0400 | [diff] [blame] | 224 | /* return id (s/n) string for *disk to *id_str |
| 225 | */ |
| 226 | static int virtblk_get_id(struct gendisk *disk, char *id_str) |
| 227 | { |
| 228 | struct virtio_blk *vblk = disk->private_data; |
| 229 | struct request *req; |
| 230 | struct bio *bio; |
Mike Snitzer | e4c4776 | 2010-10-09 12:12:13 +1030 | [diff] [blame] | 231 | int err; |
john cooper | 4cb2ea2 | 2010-03-25 01:33:33 -0400 | [diff] [blame] | 232 | |
| 233 | bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES, |
| 234 | GFP_KERNEL); |
| 235 | if (IS_ERR(bio)) |
| 236 | return PTR_ERR(bio); |
| 237 | |
| 238 | req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL); |
| 239 | if (IS_ERR(req)) { |
| 240 | bio_put(bio); |
| 241 | return PTR_ERR(req); |
| 242 | } |
| 243 | |
| 244 | req->cmd_type = REQ_TYPE_SPECIAL; |
Mike Snitzer | e4c4776 | 2010-10-09 12:12:13 +1030 | [diff] [blame] | 245 | err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false); |
| 246 | blk_put_request(req); |
| 247 | |
| 248 | return err; |
john cooper | 4cb2ea2 | 2010-03-25 01:33:33 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Christoph Hellwig | fe5a50a | 2010-09-15 01:27:23 +0200 | [diff] [blame] | 251 | static int virtblk_ioctl(struct block_device *bdev, fmode_t mode, |
| 252 | unsigned int cmd, unsigned long data) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 253 | { |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 254 | struct gendisk *disk = bdev->bd_disk; |
| 255 | struct virtio_blk *vblk = disk->private_data; |
| 256 | |
| 257 | /* |
| 258 | * Only allow the generic SCSI ioctls if the host can support it. |
| 259 | */ |
| 260 | if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI)) |
Christoph Hellwig | d9ecdea | 2009-06-20 21:29:41 +0200 | [diff] [blame] | 261 | return -ENOTTY; |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 262 | |
Paolo Bonzini | 577ebb3 | 2012-01-12 16:01:27 +0100 | [diff] [blame] | 263 | return scsi_cmd_blk_ioctl(bdev, mode, cmd, |
| 264 | (void __user *)data); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 265 | } |
| 266 | |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 267 | /* We provide getgeo only to please some old bootloader/partitioning tools */ |
| 268 | static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) |
| 269 | { |
Ryan Harper | 48e4043 | 2008-04-16 13:56:37 -0500 | [diff] [blame] | 270 | struct virtio_blk *vblk = bd->bd_disk->private_data; |
Ryan Harper | 48e4043 | 2008-04-16 13:56:37 -0500 | [diff] [blame] | 271 | |
| 272 | /* see if the host passed in geometry config */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 273 | if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) { |
| 274 | virtio_cread(vblk->vdev, struct virtio_blk_config, |
| 275 | geometry.cylinders, &geo->cylinders); |
| 276 | virtio_cread(vblk->vdev, struct virtio_blk_config, |
| 277 | geometry.heads, &geo->heads); |
| 278 | virtio_cread(vblk->vdev, struct virtio_blk_config, |
| 279 | geometry.sectors, &geo->sectors); |
Ryan Harper | 48e4043 | 2008-04-16 13:56:37 -0500 | [diff] [blame] | 280 | } else { |
| 281 | /* some standard values, similar to sd */ |
| 282 | geo->heads = 1 << 6; |
| 283 | geo->sectors = 1 << 5; |
| 284 | geo->cylinders = get_capacity(bd->bd_disk) >> 11; |
| 285 | } |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 289 | static const struct block_device_operations virtblk_fops = { |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 290 | .ioctl = virtblk_ioctl, |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 291 | .owner = THIS_MODULE, |
| 292 | .getgeo = virtblk_getgeo, |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 293 | }; |
| 294 | |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 295 | static int index_to_minor(int index) |
| 296 | { |
| 297 | return index << PART_BITS; |
| 298 | } |
| 299 | |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 300 | static int minor_to_index(int minor) |
| 301 | { |
| 302 | return minor >> PART_BITS; |
| 303 | } |
| 304 | |
Ryan Harper | a5eb9e4 | 2010-06-23 22:19:57 -0500 | [diff] [blame] | 305 | static ssize_t virtblk_serial_show(struct device *dev, |
| 306 | struct device_attribute *attr, char *buf) |
| 307 | { |
| 308 | struct gendisk *disk = dev_to_disk(dev); |
| 309 | int err; |
| 310 | |
| 311 | /* sysfs gives us a PAGE_SIZE buffer */ |
| 312 | BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES); |
| 313 | |
| 314 | buf[VIRTIO_BLK_ID_BYTES] = '\0'; |
| 315 | err = virtblk_get_id(disk, buf); |
| 316 | if (!err) |
| 317 | return strlen(buf); |
| 318 | |
| 319 | if (err == -EIO) /* Unsupported? Make it empty. */ |
| 320 | return 0; |
| 321 | |
| 322 | return err; |
| 323 | } |
| 324 | DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL); |
| 325 | |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 326 | static void virtblk_config_changed_work(struct work_struct *work) |
| 327 | { |
| 328 | struct virtio_blk *vblk = |
| 329 | container_of(work, struct virtio_blk, config_work); |
| 330 | struct virtio_device *vdev = vblk->vdev; |
| 331 | struct request_queue *q = vblk->disk->queue; |
| 332 | char cap_str_2[10], cap_str_10[10]; |
Milos Vyletel | 9d9598b | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 333 | char *envp[] = { "RESIZE=1", NULL }; |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 334 | u64 capacity, size; |
| 335 | |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 336 | mutex_lock(&vblk->config_lock); |
| 337 | if (!vblk->config_enable) |
| 338 | goto done; |
| 339 | |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 340 | /* Host must always specify the capacity. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 341 | virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 342 | |
| 343 | /* If capacity is too big, truncate with warning. */ |
| 344 | if ((sector_t)capacity != capacity) { |
| 345 | dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n", |
| 346 | (unsigned long long)capacity); |
| 347 | capacity = (sector_t)-1; |
| 348 | } |
| 349 | |
| 350 | size = capacity * queue_logical_block_size(q); |
| 351 | string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2)); |
| 352 | string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10)); |
| 353 | |
| 354 | dev_notice(&vdev->dev, |
| 355 | "new size: %llu %d-byte logical blocks (%s/%s)\n", |
| 356 | (unsigned long long)capacity, |
| 357 | queue_logical_block_size(q), |
| 358 | cap_str_10, cap_str_2); |
| 359 | |
| 360 | set_capacity(vblk->disk, capacity); |
Vivek Goyal | e9986f3 | 2012-03-29 10:09:44 +0200 | [diff] [blame] | 361 | revalidate_disk(vblk->disk); |
Milos Vyletel | 9d9598b | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 362 | kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp); |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 363 | done: |
| 364 | mutex_unlock(&vblk->config_lock); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static void virtblk_config_changed(struct virtio_device *vdev) |
| 368 | { |
| 369 | struct virtio_blk *vblk = vdev->priv; |
| 370 | |
| 371 | queue_work(virtblk_wq, &vblk->config_work); |
| 372 | } |
| 373 | |
Amit Shah | 6abd6e5 | 2011-12-22 16:58:29 +0530 | [diff] [blame] | 374 | static int init_vq(struct virtio_blk *vblk) |
| 375 | { |
| 376 | int err = 0; |
| 377 | |
| 378 | /* We expect one virtqueue, for output. */ |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 379 | vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests"); |
Amit Shah | 6abd6e5 | 2011-12-22 16:58:29 +0530 | [diff] [blame] | 380 | if (IS_ERR(vblk->vq)) |
| 381 | err = PTR_ERR(vblk->vq); |
| 382 | |
| 383 | return err; |
| 384 | } |
| 385 | |
Ren Mingxin | c0aa3e0 | 2012-04-10 15:28:05 +0800 | [diff] [blame] | 386 | /* |
| 387 | * Legacy naming scheme used for virtio devices. We are stuck with it for |
| 388 | * virtio blk but don't ever use it for any new driver. |
| 389 | */ |
| 390 | static int virtblk_name_format(char *prefix, int index, char *buf, int buflen) |
| 391 | { |
| 392 | const int base = 'z' - 'a' + 1; |
| 393 | char *begin = buf + strlen(prefix); |
| 394 | char *end = buf + buflen; |
| 395 | char *p; |
| 396 | int unit; |
| 397 | |
| 398 | p = end - 1; |
| 399 | *p = '\0'; |
| 400 | unit = base; |
| 401 | do { |
| 402 | if (p == begin) |
| 403 | return -EINVAL; |
| 404 | *--p = 'a' + (index % unit); |
| 405 | index = (index / unit) - 1; |
| 406 | } while (index >= 0); |
| 407 | |
| 408 | memmove(begin, p, end - p); |
| 409 | memcpy(buf, prefix, strlen(prefix)); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 414 | static int virtblk_get_cache_mode(struct virtio_device *vdev) |
| 415 | { |
| 416 | u8 writeback; |
| 417 | int err; |
| 418 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 419 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE, |
| 420 | struct virtio_blk_config, wce, |
| 421 | &writeback); |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 422 | if (err) |
| 423 | writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE); |
| 424 | |
| 425 | return writeback; |
| 426 | } |
| 427 | |
| 428 | static void virtblk_update_cache_mode(struct virtio_device *vdev) |
| 429 | { |
| 430 | u8 writeback = virtblk_get_cache_mode(vdev); |
| 431 | struct virtio_blk *vblk = vdev->priv; |
| 432 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 433 | if (writeback) |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 434 | blk_queue_flush(vblk->disk->queue, REQ_FLUSH); |
| 435 | else |
| 436 | blk_queue_flush(vblk->disk->queue, 0); |
| 437 | |
| 438 | revalidate_disk(vblk->disk); |
| 439 | } |
| 440 | |
| 441 | static const char *const virtblk_cache_types[] = { |
| 442 | "write through", "write back" |
| 443 | }; |
| 444 | |
| 445 | static ssize_t |
| 446 | virtblk_cache_type_store(struct device *dev, struct device_attribute *attr, |
| 447 | const char *buf, size_t count) |
| 448 | { |
| 449 | struct gendisk *disk = dev_to_disk(dev); |
| 450 | struct virtio_blk *vblk = disk->private_data; |
| 451 | struct virtio_device *vdev = vblk->vdev; |
| 452 | int i; |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 453 | |
| 454 | BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE)); |
| 455 | for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; ) |
| 456 | if (sysfs_streq(buf, virtblk_cache_types[i])) |
| 457 | break; |
| 458 | |
| 459 | if (i < 0) |
| 460 | return -EINVAL; |
| 461 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 462 | virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i); |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 463 | virtblk_update_cache_mode(vdev); |
| 464 | return count; |
| 465 | } |
| 466 | |
| 467 | static ssize_t |
| 468 | virtblk_cache_type_show(struct device *dev, struct device_attribute *attr, |
| 469 | char *buf) |
| 470 | { |
| 471 | struct gendisk *disk = dev_to_disk(dev); |
| 472 | struct virtio_blk *vblk = disk->private_data; |
| 473 | u8 writeback = virtblk_get_cache_mode(vblk->vdev); |
| 474 | |
| 475 | BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types)); |
| 476 | return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]); |
| 477 | } |
| 478 | |
| 479 | static const struct device_attribute dev_attr_cache_type_ro = |
| 480 | __ATTR(cache_type, S_IRUGO, |
| 481 | virtblk_cache_type_show, NULL); |
| 482 | static const struct device_attribute dev_attr_cache_type_rw = |
| 483 | __ATTR(cache_type, S_IRUGO|S_IWUSR, |
| 484 | virtblk_cache_type_show, virtblk_cache_type_store); |
| 485 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 486 | static int virtblk_init_request(void *data, struct request *rq, |
| 487 | unsigned int hctx_idx, unsigned int request_idx, |
| 488 | unsigned int numa_node) |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 489 | { |
| 490 | struct virtio_blk *vblk = data; |
| 491 | struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq); |
| 492 | |
| 493 | sg_init_table(vbr->sg, vblk->sg_elems); |
| 494 | return 0; |
| 495 | } |
| 496 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 497 | static struct blk_mq_ops virtio_mq_ops = { |
| 498 | .queue_rq = virtio_queue_rq, |
| 499 | .map_queue = blk_mq_map_queue, |
| 500 | .alloc_hctx = blk_mq_alloc_single_hw_queue, |
| 501 | .free_hctx = blk_mq_free_single_hw_queue, |
Christoph Hellwig | 5124c28 | 2014-02-10 03:24:39 -0800 | [diff] [blame] | 502 | .complete = virtblk_request_done, |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 503 | .init_request = virtblk_init_request, |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 504 | }; |
| 505 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 506 | static unsigned int virtblk_queue_depth; |
| 507 | module_param_named(queue_depth, virtblk_queue_depth, uint, 0444); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 508 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 509 | static int virtblk_probe(struct virtio_device *vdev) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 510 | { |
| 511 | struct virtio_blk *vblk; |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 512 | struct request_queue *q; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 513 | int err, index; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 514 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 515 | u64 cap; |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 516 | u32 v, blk_size, sg_elems, opt_io_size; |
| 517 | u16 min_io_size; |
| 518 | u8 physical_block_exp, alignment_offset; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 519 | |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 520 | err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS), |
| 521 | GFP_KERNEL); |
| 522 | if (err < 0) |
| 523 | goto out; |
| 524 | index = err; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 525 | |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 526 | /* We need to know how many segments before we allocate. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 527 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX, |
| 528 | struct virtio_blk_config, seg_max, |
| 529 | &sg_elems); |
Christoph Hellwig | a5b365a | 2010-05-25 14:17:54 +0200 | [diff] [blame] | 530 | |
| 531 | /* We need at least one SG element, whatever they say. */ |
| 532 | if (err || !sg_elems) |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 533 | sg_elems = 1; |
| 534 | |
| 535 | /* We need an extra sg elements at head and tail. */ |
| 536 | sg_elems += 2; |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 537 | vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 538 | if (!vblk) { |
| 539 | err = -ENOMEM; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 540 | goto out_free_index; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 541 | } |
| 542 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 543 | vblk->vdev = vdev; |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 544 | vblk->sg_elems = sg_elems; |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 545 | mutex_init(&vblk->config_lock); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 546 | |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 547 | INIT_WORK(&vblk->config_work, virtblk_config_changed_work); |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 548 | vblk->config_enable = true; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 549 | |
Amit Shah | 6abd6e5 | 2011-12-22 16:58:29 +0530 | [diff] [blame] | 550 | err = init_vq(vblk); |
| 551 | if (err) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 552 | goto out_free_vblk; |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 553 | spin_lock_init(&vblk->vq_lock); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 554 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 555 | /* FIXME: How many partitions? How long is a piece of string? */ |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 556 | vblk->disk = alloc_disk(1 << PART_BITS); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 557 | if (!vblk->disk) { |
| 558 | err = -ENOMEM; |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 559 | goto out_free_vq; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 560 | } |
| 561 | |
Rusty Russell | fc4324b | 2014-03-19 17:08:24 +1030 | [diff] [blame] | 562 | /* Default queue sizing is to fill the ring. */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 563 | if (!virtblk_queue_depth) { |
| 564 | virtblk_queue_depth = vblk->vq->num_free; |
Rusty Russell | fc4324b | 2014-03-19 17:08:24 +1030 | [diff] [blame] | 565 | /* ... but without indirect descs, we use 2 descs per req */ |
| 566 | if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC)) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 567 | virtblk_queue_depth /= 2; |
Rusty Russell | fc4324b | 2014-03-19 17:08:24 +1030 | [diff] [blame] | 568 | } |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 569 | |
| 570 | memset(&vblk->tag_set, 0, sizeof(vblk->tag_set)); |
| 571 | vblk->tag_set.ops = &virtio_mq_ops; |
| 572 | vblk->tag_set.nr_hw_queues = 1; |
| 573 | vblk->tag_set.queue_depth = virtblk_queue_depth; |
| 574 | vblk->tag_set.numa_node = NUMA_NO_NODE; |
| 575 | vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 576 | vblk->tag_set.cmd_size = |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 577 | sizeof(struct virtblk_req) + |
| 578 | sizeof(struct scatterlist) * sg_elems; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 579 | vblk->tag_set.driver_data = vblk; |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 580 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 581 | err = blk_mq_alloc_tag_set(&vblk->tag_set); |
| 582 | if (err) |
| 583 | goto out_put_disk; |
| 584 | |
| 585 | q = vblk->disk->queue = blk_mq_init_queue(&vblk->tag_set); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 586 | if (!q) { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 587 | err = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 588 | goto out_free_tags; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 589 | } |
| 590 | |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 591 | q->queuedata = vblk; |
Fernando Luis Vázquez Cao | 7d116b6 | 2008-10-27 18:45:15 +0900 | [diff] [blame] | 592 | |
Ren Mingxin | c0aa3e0 | 2012-04-10 15:28:05 +0800 | [diff] [blame] | 593 | virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN); |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 594 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 595 | vblk->disk->major = major; |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 596 | vblk->disk->first_minor = index_to_minor(index); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 597 | vblk->disk->private_data = vblk; |
| 598 | vblk->disk->fops = &virtblk_fops; |
Jeremy Katz | c483934 | 2008-03-02 17:00:15 -0500 | [diff] [blame] | 599 | vblk->disk->driverfs_dev = &vdev->dev; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 600 | vblk->index = index; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 601 | |
Tejun Heo | 02c42b7 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 602 | /* configure queue flush support */ |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 603 | virtblk_update_cache_mode(vdev); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 604 | |
Christian Borntraeger | 3ef5360 | 2008-05-16 11:17:03 +0200 | [diff] [blame] | 605 | /* If disk is read-only in the host, the guest should obey */ |
| 606 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO)) |
| 607 | set_disk_ro(vblk->disk, 1); |
| 608 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 609 | /* Host must always specify the capacity. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 610 | virtio_cread(vdev, struct virtio_blk_config, capacity, &cap); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 611 | |
| 612 | /* If capacity is too big, truncate with warning. */ |
| 613 | if ((sector_t)cap != cap) { |
| 614 | dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n", |
| 615 | (unsigned long long)cap); |
| 616 | cap = (sector_t)-1; |
| 617 | } |
| 618 | set_capacity(vblk->disk, cap); |
| 619 | |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 620 | /* We can handle whatever the host told us to handle. */ |
Martin K. Petersen | ee714f2 | 2010-03-10 00:48:32 -0500 | [diff] [blame] | 621 | blk_queue_max_segments(q, vblk->sg_elems-2); |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 622 | |
Christoph Hellwig | 4eff3ca | 2009-07-17 21:47:45 -0600 | [diff] [blame] | 623 | /* No need to bounce any requests */ |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 624 | blk_queue_bounce_limit(q, BLK_BOUNCE_ANY); |
Christoph Hellwig | 4eff3ca | 2009-07-17 21:47:45 -0600 | [diff] [blame] | 625 | |
Rusty Russell | 4b7f7e2 | 2008-12-30 09:26:04 -0600 | [diff] [blame] | 626 | /* No real sector limit. */ |
Martin K. Petersen | ee714f2 | 2010-03-10 00:48:32 -0500 | [diff] [blame] | 627 | blk_queue_max_hw_sectors(q, -1U); |
Rusty Russell | 4b7f7e2 | 2008-12-30 09:26:04 -0600 | [diff] [blame] | 628 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 629 | /* Host can optionally specify maximum segment size and number of |
| 630 | * segments. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 631 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX, |
| 632 | struct virtio_blk_config, size_max, &v); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 633 | if (!err) |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 634 | blk_queue_max_segment_size(q, v); |
Rusty Russell | 4b7f7e2 | 2008-12-30 09:26:04 -0600 | [diff] [blame] | 635 | else |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 636 | blk_queue_max_segment_size(q, -1U); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 637 | |
Christian Borntraeger | 066f4d8 | 2008-05-29 11:08:26 +0200 | [diff] [blame] | 638 | /* Host can optionally specify the block size of the device */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 639 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE, |
| 640 | struct virtio_blk_config, blk_size, |
| 641 | &blk_size); |
Christian Borntraeger | 066f4d8 | 2008-05-29 11:08:26 +0200 | [diff] [blame] | 642 | if (!err) |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 643 | blk_queue_logical_block_size(q, blk_size); |
| 644 | else |
| 645 | blk_size = queue_logical_block_size(q); |
| 646 | |
| 647 | /* Use topology information if available */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 648 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 649 | struct virtio_blk_config, physical_block_exp, |
| 650 | &physical_block_exp); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 651 | if (!err && physical_block_exp) |
| 652 | blk_queue_physical_block_size(q, |
| 653 | blk_size * (1 << physical_block_exp)); |
| 654 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 655 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 656 | struct virtio_blk_config, alignment_offset, |
| 657 | &alignment_offset); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 658 | if (!err && alignment_offset) |
| 659 | blk_queue_alignment_offset(q, blk_size * alignment_offset); |
| 660 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 661 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 662 | struct virtio_blk_config, min_io_size, |
| 663 | &min_io_size); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 664 | if (!err && min_io_size) |
| 665 | blk_queue_io_min(q, blk_size * min_io_size); |
| 666 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 667 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 668 | struct virtio_blk_config, opt_io_size, |
| 669 | &opt_io_size); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 670 | if (!err && opt_io_size) |
| 671 | blk_queue_io_opt(q, blk_size * opt_io_size); |
| 672 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 673 | add_disk(vblk->disk); |
Ryan Harper | a5eb9e4 | 2010-06-23 22:19:57 -0500 | [diff] [blame] | 674 | err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial); |
| 675 | if (err) |
| 676 | goto out_del_disk; |
| 677 | |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 678 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) |
| 679 | err = device_create_file(disk_to_dev(vblk->disk), |
| 680 | &dev_attr_cache_type_rw); |
| 681 | else |
| 682 | err = device_create_file(disk_to_dev(vblk->disk), |
| 683 | &dev_attr_cache_type_ro); |
| 684 | if (err) |
| 685 | goto out_del_disk; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 686 | return 0; |
| 687 | |
Ryan Harper | a5eb9e4 | 2010-06-23 22:19:57 -0500 | [diff] [blame] | 688 | out_del_disk: |
| 689 | del_gendisk(vblk->disk); |
| 690 | blk_cleanup_queue(vblk->disk->queue); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 691 | out_free_tags: |
| 692 | blk_mq_free_tag_set(&vblk->tag_set); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 693 | out_put_disk: |
| 694 | put_disk(vblk->disk); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 695 | out_free_vq: |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 696 | vdev->config->del_vqs(vdev); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 697 | out_free_vblk: |
| 698 | kfree(vblk); |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 699 | out_free_index: |
| 700 | ida_simple_remove(&vd_index_ida, index); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 701 | out: |
| 702 | return err; |
| 703 | } |
| 704 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 705 | static void virtblk_remove(struct virtio_device *vdev) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 706 | { |
| 707 | struct virtio_blk *vblk = vdev->priv; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 708 | int index = vblk->index; |
Alexander Graf | f4953fe | 2013-01-02 15:37:17 +1030 | [diff] [blame] | 709 | int refc; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 710 | |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 711 | /* Prevent config work handler from accessing the device. */ |
| 712 | mutex_lock(&vblk->config_lock); |
| 713 | vblk->config_enable = false; |
| 714 | mutex_unlock(&vblk->config_lock); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 715 | |
Asias He | 02e2b12 | 2012-05-25 10:34:47 +0800 | [diff] [blame] | 716 | del_gendisk(vblk->disk); |
Asias He | 483001c | 2012-05-25 10:34:48 +0800 | [diff] [blame] | 717 | blk_cleanup_queue(vblk->disk->queue); |
Asias He | 02e2b12 | 2012-05-25 10:34:47 +0800 | [diff] [blame] | 718 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 719 | blk_mq_free_tag_set(&vblk->tag_set); |
| 720 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 721 | /* Stop all the virtqueues. */ |
| 722 | vdev->config->reset(vdev); |
| 723 | |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 724 | flush_work(&vblk->config_work); |
| 725 | |
Alexander Graf | f4953fe | 2013-01-02 15:37:17 +1030 | [diff] [blame] | 726 | refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 727 | put_disk(vblk->disk); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 728 | vdev->config->del_vqs(vdev); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 729 | kfree(vblk); |
Alexander Graf | f4953fe | 2013-01-02 15:37:17 +1030 | [diff] [blame] | 730 | |
| 731 | /* Only free device id if we don't have any users */ |
| 732 | if (refc == 1) |
| 733 | ida_simple_remove(&vd_index_ida, index); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 734 | } |
| 735 | |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 736 | #ifdef CONFIG_PM_SLEEP |
Amit Shah | f8fb5bc | 2011-12-22 16:58:30 +0530 | [diff] [blame] | 737 | static int virtblk_freeze(struct virtio_device *vdev) |
| 738 | { |
| 739 | struct virtio_blk *vblk = vdev->priv; |
| 740 | |
| 741 | /* Ensure we don't receive any more interrupts */ |
| 742 | vdev->config->reset(vdev); |
| 743 | |
| 744 | /* Prevent config work handler from accessing the device. */ |
| 745 | mutex_lock(&vblk->config_lock); |
| 746 | vblk->config_enable = false; |
| 747 | mutex_unlock(&vblk->config_lock); |
| 748 | |
| 749 | flush_work(&vblk->config_work); |
| 750 | |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 751 | blk_mq_stop_hw_queues(vblk->disk->queue); |
Amit Shah | f8fb5bc | 2011-12-22 16:58:30 +0530 | [diff] [blame] | 752 | |
| 753 | vdev->config->del_vqs(vdev); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int virtblk_restore(struct virtio_device *vdev) |
| 758 | { |
| 759 | struct virtio_blk *vblk = vdev->priv; |
| 760 | int ret; |
| 761 | |
| 762 | vblk->config_enable = true; |
| 763 | ret = init_vq(vdev->priv); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 764 | if (!ret) |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame^] | 765 | blk_mq_start_stopped_hw_queues(vblk->disk->queue, true); |
Jens Axboe | 1cf7e9c | 2013-11-01 10:52:52 -0600 | [diff] [blame] | 766 | |
Amit Shah | f8fb5bc | 2011-12-22 16:58:30 +0530 | [diff] [blame] | 767 | return ret; |
| 768 | } |
| 769 | #endif |
| 770 | |
Márton Németh | 47483e2 | 2010-01-10 13:40:02 +0100 | [diff] [blame] | 771 | static const struct virtio_device_id id_table[] = { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 772 | { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID }, |
| 773 | { 0 }, |
| 774 | }; |
| 775 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 776 | static unsigned int features[] = { |
Tejun Heo | 02c42b7 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 777 | VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY, |
| 778 | VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI, |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 779 | VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 780 | }; |
| 781 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 782 | static struct virtio_driver virtio_blk = { |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 783 | .feature_table = features, |
| 784 | .feature_table_size = ARRAY_SIZE(features), |
| 785 | .driver.name = KBUILD_MODNAME, |
| 786 | .driver.owner = THIS_MODULE, |
| 787 | .id_table = id_table, |
| 788 | .probe = virtblk_probe, |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 789 | .remove = virtblk_remove, |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 790 | .config_changed = virtblk_config_changed, |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 791 | #ifdef CONFIG_PM_SLEEP |
Amit Shah | f8fb5bc | 2011-12-22 16:58:30 +0530 | [diff] [blame] | 792 | .freeze = virtblk_freeze, |
| 793 | .restore = virtblk_restore, |
| 794 | #endif |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 795 | }; |
| 796 | |
| 797 | static int __init init(void) |
| 798 | { |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 799 | int error; |
| 800 | |
| 801 | virtblk_wq = alloc_workqueue("virtio-blk", 0, 0); |
| 802 | if (!virtblk_wq) |
| 803 | return -ENOMEM; |
| 804 | |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 805 | major = register_blkdev(0, "virtblk"); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 806 | if (major < 0) { |
| 807 | error = major; |
| 808 | goto out_destroy_workqueue; |
| 809 | } |
| 810 | |
| 811 | error = register_virtio_driver(&virtio_blk); |
| 812 | if (error) |
| 813 | goto out_unregister_blkdev; |
| 814 | return 0; |
| 815 | |
| 816 | out_unregister_blkdev: |
| 817 | unregister_blkdev(major, "virtblk"); |
| 818 | out_destroy_workqueue: |
| 819 | destroy_workqueue(virtblk_wq); |
| 820 | return error; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void __exit fini(void) |
| 824 | { |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 825 | unregister_blkdev(major, "virtblk"); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 826 | unregister_virtio_driver(&virtio_blk); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 827 | destroy_workqueue(virtblk_wq); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 828 | } |
| 829 | module_init(init); |
| 830 | module_exit(fini); |
| 831 | |
| 832 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 833 | MODULE_DESCRIPTION("Virtio block driver"); |
| 834 | MODULE_LICENSE("GPL"); |