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 | 3d1266c | 2007-10-24 13:21:21 +0200 | [diff] [blame] | 14 | |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 15 | #define PART_BITS 4 |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 16 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 17 | static bool use_bio; |
| 18 | module_param(use_bio, bool, S_IRUGO); |
| 19 | |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 20 | static int major; |
| 21 | static DEFINE_IDA(vd_index_ida); |
| 22 | |
Jonghwan Choi | 2a647bf | 2013-05-20 10:25:39 +0930 | [diff] [blame] | 23 | static struct workqueue_struct *virtblk_wq; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 24 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 25 | struct virtio_blk |
| 26 | { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 27 | struct virtio_device *vdev; |
| 28 | struct virtqueue *vq; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 29 | wait_queue_head_t queue_wait; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 30 | |
| 31 | /* The disk structure for the kernel. */ |
| 32 | struct gendisk *disk; |
| 33 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 34 | mempool_t *pool; |
| 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; |
| 50 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 51 | /* Scatterlist: can be too big for stack. */ |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 52 | struct scatterlist sg[/*sg_elems*/]; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct virtblk_req |
| 56 | { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 57 | struct request *req; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 58 | struct bio *bio; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 59 | struct virtio_blk_outhdr out_hdr; |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 60 | struct virtio_scsi_inhdr in_hdr; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 61 | struct work_struct work; |
| 62 | struct virtio_blk *vblk; |
| 63 | int flags; |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 64 | u8 status; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 65 | struct scatterlist sg[]; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 66 | }; |
| 67 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 68 | enum { |
| 69 | VBLK_IS_FLUSH = 1, |
| 70 | VBLK_REQ_FLUSH = 2, |
| 71 | VBLK_REQ_DATA = 4, |
| 72 | VBLK_REQ_FUA = 8, |
| 73 | }; |
| 74 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 75 | static inline int virtblk_result(struct virtblk_req *vbr) |
| 76 | { |
| 77 | switch (vbr->status) { |
| 78 | case VIRTIO_BLK_S_OK: |
| 79 | return 0; |
| 80 | case VIRTIO_BLK_S_UNSUPP: |
| 81 | return -ENOTTY; |
| 82 | default: |
| 83 | return -EIO; |
| 84 | } |
| 85 | } |
| 86 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 87 | static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk, |
| 88 | gfp_t gfp_mask) |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 89 | { |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 90 | struct virtblk_req *vbr; |
| 91 | |
| 92 | vbr = mempool_alloc(vblk->pool, gfp_mask); |
Dan Carpenter | f22cf8e | 2012-09-05 15:32:53 +0300 | [diff] [blame] | 93 | if (!vbr) |
| 94 | return NULL; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 95 | |
| 96 | vbr->vblk = vblk; |
Dan Carpenter | f22cf8e | 2012-09-05 15:32:53 +0300 | [diff] [blame] | 97 | if (use_bio) |
| 98 | sg_init_table(vbr->sg, vblk->sg_elems); |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 99 | |
| 100 | return vbr; |
| 101 | } |
| 102 | |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 103 | static int __virtblk_add_req(struct virtqueue *vq, |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 104 | struct virtblk_req *vbr, |
| 105 | struct scatterlist *data_sg, |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 106 | bool have_data) |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 107 | { |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 108 | struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6]; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 109 | unsigned int num_out = 0, num_in = 0; |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 110 | int type = vbr->out_hdr.type & ~VIRTIO_BLK_T_OUT; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 111 | |
| 112 | sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr)); |
| 113 | sgs[num_out++] = &hdr; |
| 114 | |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 115 | /* |
| 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. |
| 120 | */ |
| 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; |
| 124 | } |
| 125 | |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 126 | if (have_data) { |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 127 | if (vbr->out_hdr.type & VIRTIO_BLK_T_OUT) |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 128 | sgs[num_out++] = data_sg; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 129 | else |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 130 | sgs[num_out + num_in++] = data_sg; |
| 131 | } |
| 132 | |
| 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; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | sg_init_one(&status, &vbr->status, sizeof(vbr->status)); |
| 141 | sgs[num_out + num_in++] = &status; |
| 142 | |
| 143 | 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] | 144 | } |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 145 | |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 146 | static void virtblk_add_req(struct virtblk_req *vbr, bool have_data) |
Paolo Bonzini | 5ee21a5 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 147 | { |
| 148 | struct virtio_blk *vblk = vbr->vblk; |
| 149 | DEFINE_WAIT(wait); |
| 150 | int ret; |
| 151 | |
| 152 | spin_lock_irq(vblk->disk->queue->queue_lock); |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 153 | while (unlikely((ret = __virtblk_add_req(vblk->vq, vbr, vbr->sg, |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 154 | have_data)) < 0)) { |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 155 | prepare_to_wait_exclusive(&vblk->queue_wait, &wait, |
| 156 | TASK_UNINTERRUPTIBLE); |
| 157 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 158 | spin_unlock_irq(vblk->disk->queue->queue_lock); |
Paolo Bonzini | 5ee21a5 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 159 | io_schedule(); |
| 160 | spin_lock_irq(vblk->disk->queue->queue_lock); |
| 161 | |
| 162 | finish_wait(&vblk->queue_wait, &wait); |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 163 | } |
Paolo Bonzini | 5ee21a5 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 164 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 165 | virtqueue_kick(vblk->vq); |
| 166 | spin_unlock_irq(vblk->disk->queue->queue_lock); |
| 167 | } |
| 168 | |
Paolo Bonzini | 5ee21a5 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 169 | static void virtblk_bio_send_flush(struct virtblk_req *vbr) |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 170 | { |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 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; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 175 | |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 176 | virtblk_add_req(vbr, false); |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Paolo Bonzini | 5ee21a5 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 179 | static void virtblk_bio_send_data(struct virtblk_req *vbr) |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 180 | { |
| 181 | struct virtio_blk *vblk = vbr->vblk; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 182 | struct bio *bio = vbr->bio; |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 183 | bool have_data; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 184 | |
| 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); |
| 189 | |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 190 | if (blk_bio_map_sg(vblk->disk->queue, bio, vbr->sg)) { |
| 191 | have_data = true; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 192 | if (bio->bi_rw & REQ_WRITE) |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 193 | vbr->out_hdr.type |= VIRTIO_BLK_T_OUT; |
Paolo Bonzini | 8f39db9 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 194 | else |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 195 | vbr->out_hdr.type |= VIRTIO_BLK_T_IN; |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 196 | } else |
| 197 | have_data = false; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 198 | |
Rusty Russell | 0a11cc3 | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 199 | virtblk_add_req(vbr, have_data); |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void virtblk_bio_send_data_work(struct work_struct *work) |
| 203 | { |
| 204 | struct virtblk_req *vbr; |
| 205 | |
| 206 | vbr = container_of(work, struct virtblk_req, work); |
| 207 | |
| 208 | virtblk_bio_send_data(vbr); |
| 209 | } |
| 210 | |
| 211 | static void virtblk_bio_send_flush_work(struct work_struct *work) |
| 212 | { |
| 213 | struct virtblk_req *vbr; |
| 214 | |
| 215 | vbr = container_of(work, struct virtblk_req, work); |
| 216 | |
| 217 | virtblk_bio_send_flush(vbr); |
| 218 | } |
| 219 | |
| 220 | static inline void virtblk_request_done(struct virtblk_req *vbr) |
| 221 | { |
| 222 | struct virtio_blk *vblk = vbr->vblk; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 223 | struct request *req = vbr->req; |
| 224 | int error = virtblk_result(vbr); |
| 225 | |
| 226 | if (req->cmd_type == REQ_TYPE_BLOCK_PC) { |
| 227 | req->resid_len = vbr->in_hdr.residual; |
| 228 | req->sense_len = vbr->in_hdr.sense_len; |
| 229 | req->errors = vbr->in_hdr.errors; |
| 230 | } else if (req->cmd_type == REQ_TYPE_SPECIAL) { |
| 231 | req->errors = (error != 0); |
| 232 | } |
| 233 | |
| 234 | __blk_end_request_all(req, error); |
| 235 | mempool_free(vbr, vblk->pool); |
| 236 | } |
| 237 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 238 | static inline void virtblk_bio_flush_done(struct virtblk_req *vbr) |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 239 | { |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 240 | struct virtio_blk *vblk = vbr->vblk; |
| 241 | |
| 242 | if (vbr->flags & VBLK_REQ_DATA) { |
| 243 | /* Send out the actual write data */ |
| 244 | INIT_WORK(&vbr->work, virtblk_bio_send_data_work); |
| 245 | queue_work(virtblk_wq, &vbr->work); |
| 246 | } else { |
| 247 | bio_endio(vbr->bio, virtblk_result(vbr)); |
| 248 | mempool_free(vbr, vblk->pool); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static inline void virtblk_bio_data_done(struct virtblk_req *vbr) |
| 253 | { |
| 254 | struct virtio_blk *vblk = vbr->vblk; |
| 255 | |
| 256 | if (unlikely(vbr->flags & VBLK_REQ_FUA)) { |
| 257 | /* Send out a flush before end the bio */ |
| 258 | vbr->flags &= ~VBLK_REQ_DATA; |
| 259 | INIT_WORK(&vbr->work, virtblk_bio_send_flush_work); |
| 260 | queue_work(virtblk_wq, &vbr->work); |
| 261 | } else { |
| 262 | bio_endio(vbr->bio, virtblk_result(vbr)); |
| 263 | mempool_free(vbr, vblk->pool); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static inline void virtblk_bio_done(struct virtblk_req *vbr) |
| 268 | { |
| 269 | if (unlikely(vbr->flags & VBLK_IS_FLUSH)) |
| 270 | virtblk_bio_flush_done(vbr); |
| 271 | else |
| 272 | virtblk_bio_data_done(vbr); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void virtblk_done(struct virtqueue *vq) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 276 | { |
| 277 | struct virtio_blk *vblk = vq->vdev->priv; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 278 | bool bio_done = false, req_done = false; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 279 | struct virtblk_req *vbr; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 280 | unsigned long flags; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 281 | unsigned int len; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 282 | |
Asias He | 2c95a32 | 2012-05-25 16:03:27 +0800 | [diff] [blame] | 283 | spin_lock_irqsave(vblk->disk->queue->queue_lock, flags); |
Asias He | bb81110 | 2012-09-25 10:36:17 +0800 | [diff] [blame] | 284 | do { |
| 285 | virtqueue_disable_cb(vq); |
| 286 | while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) { |
| 287 | if (vbr->bio) { |
| 288 | virtblk_bio_done(vbr); |
| 289 | bio_done = true; |
| 290 | } else { |
| 291 | virtblk_request_done(vbr); |
| 292 | req_done = true; |
| 293 | } |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 294 | } |
Asias He | bb81110 | 2012-09-25 10:36:17 +0800 | [diff] [blame] | 295 | } while (!virtqueue_enable_cb(vq)); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 296 | /* In case queue is stopped waiting for more buffers. */ |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 297 | if (req_done) |
| 298 | blk_start_queue(vblk->disk->queue); |
Asias He | 2c95a32 | 2012-05-25 16:03:27 +0800 | [diff] [blame] | 299 | spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 300 | |
| 301 | if (bio_done) |
| 302 | wake_up(&vblk->queue_wait); |
| 303 | } |
| 304 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 305 | static bool do_req(struct request_queue *q, struct virtio_blk *vblk, |
| 306 | struct request *req) |
| 307 | { |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 308 | unsigned int num; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 309 | struct virtblk_req *vbr; |
| 310 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 311 | vbr = virtblk_alloc_req(vblk, GFP_ATOMIC); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 312 | if (!vbr) |
| 313 | /* When another request finishes we'll try again. */ |
| 314 | return false; |
| 315 | |
| 316 | vbr->req = req; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 317 | vbr->bio = NULL; |
FUJITA Tomonori | dd40e45 | 2010-07-03 17:45:38 +0900 | [diff] [blame] | 318 | if (req->cmd_flags & REQ_FLUSH) { |
| 319 | vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 320 | vbr->out_hdr.sector = 0; |
Fernando Luis Vázquez Cao | 766ca44 | 2008-08-14 09:59:13 +0200 | [diff] [blame] | 321 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
FUJITA Tomonori | dd40e45 | 2010-07-03 17:45:38 +0900 | [diff] [blame] | 322 | } else { |
| 323 | switch (req->cmd_type) { |
| 324 | case REQ_TYPE_FS: |
| 325 | vbr->out_hdr.type = 0; |
| 326 | vbr->out_hdr.sector = blk_rq_pos(vbr->req); |
| 327 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
| 328 | break; |
| 329 | case REQ_TYPE_BLOCK_PC: |
| 330 | vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD; |
Christoph Hellwig | f1b0ef06 | 2009-09-17 19:57:42 +0200 | [diff] [blame] | 331 | vbr->out_hdr.sector = 0; |
| 332 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
| 333 | break; |
FUJITA Tomonori | dd40e45 | 2010-07-03 17:45:38 +0900 | [diff] [blame] | 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); |
| 338 | break; |
| 339 | default: |
| 340 | /* We don't put anything else in the queue. */ |
| 341 | BUG(); |
Christoph Hellwig | f1b0ef06 | 2009-09-17 19:57:42 +0200 | [diff] [blame] | 342 | } |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 343 | } |
| 344 | |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 345 | num = blk_rq_map_sg(q, vbr->req, vblk->sg); |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 346 | if (num) { |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 347 | if (rq_data_dir(vbr->req) == WRITE) |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 348 | vbr->out_hdr.type |= VIRTIO_BLK_T_OUT; |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 349 | else |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 350 | vbr->out_hdr.type |= VIRTIO_BLK_T_IN; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 351 | } |
| 352 | |
Paolo Bonzini | 20af3cf | 2013-03-20 15:44:27 +1030 | [diff] [blame] | 353 | if (__virtblk_add_req(vblk->vq, vbr, vblk->sg, num) < 0) { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 354 | mempool_free(vbr, vblk->pool); |
| 355 | return false; |
| 356 | } |
| 357 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 361 | static void virtblk_request(struct request_queue *q) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 362 | { |
Christoph Hellwig | 6c3b46f | 2009-05-18 14:38:28 +0200 | [diff] [blame] | 363 | struct virtio_blk *vblk = q->queuedata; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 364 | struct request *req; |
| 365 | unsigned int issued = 0; |
| 366 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 367 | while ((req = blk_peek_request(q)) != NULL) { |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 368 | BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 369 | |
| 370 | /* If this request fails, stop queue and wait for something to |
| 371 | finish to restart it. */ |
| 372 | if (!do_req(q, vblk, req)) { |
| 373 | blk_stop_queue(q); |
| 374 | break; |
| 375 | } |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 376 | blk_start_request(req); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 377 | issued++; |
| 378 | } |
| 379 | |
| 380 | if (issued) |
Michael S. Tsirkin | 09ec6b6 | 2010-04-12 16:18:36 +0300 | [diff] [blame] | 381 | virtqueue_kick(vblk->vq); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 382 | } |
| 383 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 384 | static void virtblk_make_request(struct request_queue *q, struct bio *bio) |
| 385 | { |
| 386 | struct virtio_blk *vblk = q->queuedata; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 387 | struct virtblk_req *vbr; |
| 388 | |
| 389 | BUG_ON(bio->bi_phys_segments + 2 > vblk->sg_elems); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 390 | |
| 391 | vbr = virtblk_alloc_req(vblk, GFP_NOIO); |
| 392 | if (!vbr) { |
| 393 | bio_endio(bio, -ENOMEM); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | vbr->bio = bio; |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 398 | vbr->flags = 0; |
| 399 | if (bio->bi_rw & REQ_FLUSH) |
| 400 | vbr->flags |= VBLK_REQ_FLUSH; |
| 401 | if (bio->bi_rw & REQ_FUA) |
| 402 | vbr->flags |= VBLK_REQ_FUA; |
| 403 | if (bio->bi_size) |
| 404 | vbr->flags |= VBLK_REQ_DATA; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 405 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 406 | if (unlikely(vbr->flags & VBLK_REQ_FLUSH)) |
| 407 | virtblk_bio_send_flush(vbr); |
| 408 | else |
| 409 | virtblk_bio_send_data(vbr); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 410 | } |
| 411 | |
john cooper | 4cb2ea2 | 2010-03-25 01:33:33 -0400 | [diff] [blame] | 412 | /* return id (s/n) string for *disk to *id_str |
| 413 | */ |
| 414 | static int virtblk_get_id(struct gendisk *disk, char *id_str) |
| 415 | { |
| 416 | struct virtio_blk *vblk = disk->private_data; |
| 417 | struct request *req; |
| 418 | struct bio *bio; |
Mike Snitzer | e4c4776 | 2010-10-09 12:12:13 +1030 | [diff] [blame] | 419 | int err; |
john cooper | 4cb2ea2 | 2010-03-25 01:33:33 -0400 | [diff] [blame] | 420 | |
| 421 | bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES, |
| 422 | GFP_KERNEL); |
| 423 | if (IS_ERR(bio)) |
| 424 | return PTR_ERR(bio); |
| 425 | |
| 426 | req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL); |
| 427 | if (IS_ERR(req)) { |
| 428 | bio_put(bio); |
| 429 | return PTR_ERR(req); |
| 430 | } |
| 431 | |
| 432 | req->cmd_type = REQ_TYPE_SPECIAL; |
Mike Snitzer | e4c4776 | 2010-10-09 12:12:13 +1030 | [diff] [blame] | 433 | err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false); |
| 434 | blk_put_request(req); |
| 435 | |
| 436 | return err; |
john cooper | 4cb2ea2 | 2010-03-25 01:33:33 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Christoph Hellwig | fe5a50a | 2010-09-15 01:27:23 +0200 | [diff] [blame] | 439 | static int virtblk_ioctl(struct block_device *bdev, fmode_t mode, |
| 440 | unsigned int cmd, unsigned long data) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 441 | { |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 442 | struct gendisk *disk = bdev->bd_disk; |
| 443 | struct virtio_blk *vblk = disk->private_data; |
| 444 | |
| 445 | /* |
| 446 | * Only allow the generic SCSI ioctls if the host can support it. |
| 447 | */ |
| 448 | if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI)) |
Christoph Hellwig | d9ecdea | 2009-06-20 21:29:41 +0200 | [diff] [blame] | 449 | return -ENOTTY; |
Hannes Reinecke | 1cde26f | 2009-05-18 14:41:30 +0200 | [diff] [blame] | 450 | |
Paolo Bonzini | 577ebb3 | 2012-01-12 16:01:27 +0100 | [diff] [blame] | 451 | return scsi_cmd_blk_ioctl(bdev, mode, cmd, |
| 452 | (void __user *)data); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 453 | } |
| 454 | |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 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) |
| 457 | { |
Ryan Harper | 48e4043 | 2008-04-16 13:56:37 -0500 | [diff] [blame] | 458 | struct virtio_blk *vblk = bd->bd_disk->private_data; |
Ryan Harper | 48e4043 | 2008-04-16 13:56:37 -0500 | [diff] [blame] | 459 | |
| 460 | /* see if the host passed in geometry config */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 461 | if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) { |
| 462 | virtio_cread(vblk->vdev, struct virtio_blk_config, |
| 463 | geometry.cylinders, &geo->cylinders); |
| 464 | virtio_cread(vblk->vdev, struct virtio_blk_config, |
| 465 | geometry.heads, &geo->heads); |
| 466 | virtio_cread(vblk->vdev, struct virtio_blk_config, |
| 467 | geometry.sectors, &geo->sectors); |
Ryan Harper | 48e4043 | 2008-04-16 13:56:37 -0500 | [diff] [blame] | 468 | } else { |
| 469 | /* some standard values, similar to sd */ |
| 470 | geo->heads = 1 << 6; |
| 471 | geo->sectors = 1 << 5; |
| 472 | geo->cylinders = get_capacity(bd->bd_disk) >> 11; |
| 473 | } |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 477 | static const struct block_device_operations virtblk_fops = { |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 478 | .ioctl = virtblk_ioctl, |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 479 | .owner = THIS_MODULE, |
| 480 | .getgeo = virtblk_getgeo, |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 481 | }; |
| 482 | |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 483 | static int index_to_minor(int index) |
| 484 | { |
| 485 | return index << PART_BITS; |
| 486 | } |
| 487 | |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 488 | static int minor_to_index(int minor) |
| 489 | { |
| 490 | return minor >> PART_BITS; |
| 491 | } |
| 492 | |
Ryan Harper | a5eb9e4 | 2010-06-23 22:19:57 -0500 | [diff] [blame] | 493 | static ssize_t virtblk_serial_show(struct device *dev, |
| 494 | struct device_attribute *attr, char *buf) |
| 495 | { |
| 496 | struct gendisk *disk = dev_to_disk(dev); |
| 497 | int err; |
| 498 | |
| 499 | /* sysfs gives us a PAGE_SIZE buffer */ |
| 500 | BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES); |
| 501 | |
| 502 | buf[VIRTIO_BLK_ID_BYTES] = '\0'; |
| 503 | err = virtblk_get_id(disk, buf); |
| 504 | if (!err) |
| 505 | return strlen(buf); |
| 506 | |
| 507 | if (err == -EIO) /* Unsupported? Make it empty. */ |
| 508 | return 0; |
| 509 | |
| 510 | return err; |
| 511 | } |
| 512 | DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL); |
| 513 | |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 514 | static void virtblk_config_changed_work(struct work_struct *work) |
| 515 | { |
| 516 | struct virtio_blk *vblk = |
| 517 | container_of(work, struct virtio_blk, config_work); |
| 518 | struct virtio_device *vdev = vblk->vdev; |
| 519 | struct request_queue *q = vblk->disk->queue; |
| 520 | char cap_str_2[10], cap_str_10[10]; |
Milos Vyletel | 9d9598b | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 521 | char *envp[] = { "RESIZE=1", NULL }; |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 522 | u64 capacity, size; |
| 523 | |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 524 | mutex_lock(&vblk->config_lock); |
| 525 | if (!vblk->config_enable) |
| 526 | goto done; |
| 527 | |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 528 | /* Host must always specify the capacity. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 529 | virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 530 | |
| 531 | /* If capacity is too big, truncate with warning. */ |
| 532 | if ((sector_t)capacity != capacity) { |
| 533 | dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n", |
| 534 | (unsigned long long)capacity); |
| 535 | capacity = (sector_t)-1; |
| 536 | } |
| 537 | |
| 538 | size = capacity * queue_logical_block_size(q); |
| 539 | string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2)); |
| 540 | string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10)); |
| 541 | |
| 542 | dev_notice(&vdev->dev, |
| 543 | "new size: %llu %d-byte logical blocks (%s/%s)\n", |
| 544 | (unsigned long long)capacity, |
| 545 | queue_logical_block_size(q), |
| 546 | cap_str_10, cap_str_2); |
| 547 | |
| 548 | set_capacity(vblk->disk, capacity); |
Vivek Goyal | e9986f3 | 2012-03-29 10:09:44 +0200 | [diff] [blame] | 549 | revalidate_disk(vblk->disk); |
Milos Vyletel | 9d9598b | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 550 | 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] | 551 | done: |
| 552 | mutex_unlock(&vblk->config_lock); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void virtblk_config_changed(struct virtio_device *vdev) |
| 556 | { |
| 557 | struct virtio_blk *vblk = vdev->priv; |
| 558 | |
| 559 | queue_work(virtblk_wq, &vblk->config_work); |
| 560 | } |
| 561 | |
Amit Shah | 6abd6e5 | 2011-12-22 16:58:29 +0530 | [diff] [blame] | 562 | static int init_vq(struct virtio_blk *vblk) |
| 563 | { |
| 564 | int err = 0; |
| 565 | |
| 566 | /* We expect one virtqueue, for output. */ |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 567 | vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests"); |
Amit Shah | 6abd6e5 | 2011-12-22 16:58:29 +0530 | [diff] [blame] | 568 | if (IS_ERR(vblk->vq)) |
| 569 | err = PTR_ERR(vblk->vq); |
| 570 | |
| 571 | return err; |
| 572 | } |
| 573 | |
Ren Mingxin | c0aa3e0 | 2012-04-10 15:28:05 +0800 | [diff] [blame] | 574 | /* |
| 575 | * Legacy naming scheme used for virtio devices. We are stuck with it for |
| 576 | * virtio blk but don't ever use it for any new driver. |
| 577 | */ |
| 578 | static int virtblk_name_format(char *prefix, int index, char *buf, int buflen) |
| 579 | { |
| 580 | const int base = 'z' - 'a' + 1; |
| 581 | char *begin = buf + strlen(prefix); |
| 582 | char *end = buf + buflen; |
| 583 | char *p; |
| 584 | int unit; |
| 585 | |
| 586 | p = end - 1; |
| 587 | *p = '\0'; |
| 588 | unit = base; |
| 589 | do { |
| 590 | if (p == begin) |
| 591 | return -EINVAL; |
| 592 | *--p = 'a' + (index % unit); |
| 593 | index = (index / unit) - 1; |
| 594 | } while (index >= 0); |
| 595 | |
| 596 | memmove(begin, p, end - p); |
| 597 | memcpy(buf, prefix, strlen(prefix)); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 602 | static int virtblk_get_cache_mode(struct virtio_device *vdev) |
| 603 | { |
| 604 | u8 writeback; |
| 605 | int err; |
| 606 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 607 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE, |
| 608 | struct virtio_blk_config, wce, |
| 609 | &writeback); |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 610 | if (err) |
| 611 | writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE); |
| 612 | |
| 613 | return writeback; |
| 614 | } |
| 615 | |
| 616 | static void virtblk_update_cache_mode(struct virtio_device *vdev) |
| 617 | { |
| 618 | u8 writeback = virtblk_get_cache_mode(vdev); |
| 619 | struct virtio_blk *vblk = vdev->priv; |
| 620 | |
Asias He | c85a1f9 | 2012-08-08 16:07:05 +0800 | [diff] [blame] | 621 | if (writeback) |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 622 | blk_queue_flush(vblk->disk->queue, REQ_FLUSH); |
| 623 | else |
| 624 | blk_queue_flush(vblk->disk->queue, 0); |
| 625 | |
| 626 | revalidate_disk(vblk->disk); |
| 627 | } |
| 628 | |
| 629 | static const char *const virtblk_cache_types[] = { |
| 630 | "write through", "write back" |
| 631 | }; |
| 632 | |
| 633 | static ssize_t |
| 634 | virtblk_cache_type_store(struct device *dev, struct device_attribute *attr, |
| 635 | const char *buf, size_t count) |
| 636 | { |
| 637 | struct gendisk *disk = dev_to_disk(dev); |
| 638 | struct virtio_blk *vblk = disk->private_data; |
| 639 | struct virtio_device *vdev = vblk->vdev; |
| 640 | int i; |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 641 | |
| 642 | BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE)); |
| 643 | for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; ) |
| 644 | if (sysfs_streq(buf, virtblk_cache_types[i])) |
| 645 | break; |
| 646 | |
| 647 | if (i < 0) |
| 648 | return -EINVAL; |
| 649 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 650 | virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i); |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 651 | virtblk_update_cache_mode(vdev); |
| 652 | return count; |
| 653 | } |
| 654 | |
| 655 | static ssize_t |
| 656 | virtblk_cache_type_show(struct device *dev, struct device_attribute *attr, |
| 657 | char *buf) |
| 658 | { |
| 659 | struct gendisk *disk = dev_to_disk(dev); |
| 660 | struct virtio_blk *vblk = disk->private_data; |
| 661 | u8 writeback = virtblk_get_cache_mode(vblk->vdev); |
| 662 | |
| 663 | BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types)); |
| 664 | return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]); |
| 665 | } |
| 666 | |
| 667 | static const struct device_attribute dev_attr_cache_type_ro = |
| 668 | __ATTR(cache_type, S_IRUGO, |
| 669 | virtblk_cache_type_show, NULL); |
| 670 | static const struct device_attribute dev_attr_cache_type_rw = |
| 671 | __ATTR(cache_type, S_IRUGO|S_IWUSR, |
| 672 | virtblk_cache_type_show, virtblk_cache_type_store); |
| 673 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 674 | static int virtblk_probe(struct virtio_device *vdev) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 675 | { |
| 676 | struct virtio_blk *vblk; |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 677 | struct request_queue *q; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 678 | int err, index; |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 679 | int pool_size; |
| 680 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 681 | u64 cap; |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 682 | u32 v, blk_size, sg_elems, opt_io_size; |
| 683 | u16 min_io_size; |
| 684 | u8 physical_block_exp, alignment_offset; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 685 | |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 686 | err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS), |
| 687 | GFP_KERNEL); |
| 688 | if (err < 0) |
| 689 | goto out; |
| 690 | index = err; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 691 | |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 692 | /* We need to know how many segments before we allocate. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 693 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX, |
| 694 | struct virtio_blk_config, seg_max, |
| 695 | &sg_elems); |
Christoph Hellwig | a5b365a | 2010-05-25 14:17:54 +0200 | [diff] [blame] | 696 | |
| 697 | /* We need at least one SG element, whatever they say. */ |
| 698 | if (err || !sg_elems) |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 699 | sg_elems = 1; |
| 700 | |
| 701 | /* We need an extra sg elements at head and tail. */ |
| 702 | sg_elems += 2; |
| 703 | vdev->priv = vblk = kmalloc(sizeof(*vblk) + |
| 704 | sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 705 | if (!vblk) { |
| 706 | err = -ENOMEM; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 707 | goto out_free_index; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 708 | } |
| 709 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 710 | init_waitqueue_head(&vblk->queue_wait); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 711 | vblk->vdev = vdev; |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 712 | vblk->sg_elems = sg_elems; |
| 713 | sg_init_table(vblk->sg, vblk->sg_elems); |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 714 | mutex_init(&vblk->config_lock); |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 715 | |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 716 | INIT_WORK(&vblk->config_work, virtblk_config_changed_work); |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 717 | vblk->config_enable = true; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 718 | |
Amit Shah | 6abd6e5 | 2011-12-22 16:58:29 +0530 | [diff] [blame] | 719 | err = init_vq(vblk); |
| 720 | if (err) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 721 | goto out_free_vblk; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 722 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 723 | pool_size = sizeof(struct virtblk_req); |
| 724 | if (use_bio) |
| 725 | pool_size += sizeof(struct scatterlist) * sg_elems; |
| 726 | vblk->pool = mempool_create_kmalloc_pool(1, pool_size); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 727 | if (!vblk->pool) { |
| 728 | err = -ENOMEM; |
| 729 | goto out_free_vq; |
| 730 | } |
| 731 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 732 | /* FIXME: How many partitions? How long is a piece of string? */ |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 733 | vblk->disk = alloc_disk(1 << PART_BITS); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 734 | if (!vblk->disk) { |
| 735 | err = -ENOMEM; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 736 | goto out_mempool; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 737 | } |
| 738 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 739 | q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 740 | if (!q) { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 741 | err = -ENOMEM; |
| 742 | goto out_put_disk; |
| 743 | } |
| 744 | |
Asias He | a98755c | 2012-08-08 16:07:04 +0800 | [diff] [blame] | 745 | if (use_bio) |
| 746 | blk_queue_make_request(q, virtblk_make_request); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 747 | q->queuedata = vblk; |
Fernando Luis Vázquez Cao | 7d116b6 | 2008-10-27 18:45:15 +0900 | [diff] [blame] | 748 | |
Ren Mingxin | c0aa3e0 | 2012-04-10 15:28:05 +0800 | [diff] [blame] | 749 | virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN); |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 750 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 751 | vblk->disk->major = major; |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 752 | vblk->disk->first_minor = index_to_minor(index); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 753 | vblk->disk->private_data = vblk; |
| 754 | vblk->disk->fops = &virtblk_fops; |
Jeremy Katz | c483934 | 2008-03-02 17:00:15 -0500 | [diff] [blame] | 755 | vblk->disk->driverfs_dev = &vdev->dev; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 756 | vblk->index = index; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 757 | |
Tejun Heo | 02c42b7 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 758 | /* configure queue flush support */ |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 759 | virtblk_update_cache_mode(vdev); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 760 | |
Christian Borntraeger | 3ef5360 | 2008-05-16 11:17:03 +0200 | [diff] [blame] | 761 | /* If disk is read-only in the host, the guest should obey */ |
| 762 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO)) |
| 763 | set_disk_ro(vblk->disk, 1); |
| 764 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 765 | /* Host must always specify the capacity. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 766 | virtio_cread(vdev, struct virtio_blk_config, capacity, &cap); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 767 | |
| 768 | /* If capacity is too big, truncate with warning. */ |
| 769 | if ((sector_t)cap != cap) { |
| 770 | dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n", |
| 771 | (unsigned long long)cap); |
| 772 | cap = (sector_t)-1; |
| 773 | } |
| 774 | set_capacity(vblk->disk, cap); |
| 775 | |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 776 | /* We can handle whatever the host told us to handle. */ |
Martin K. Petersen | ee714f2 | 2010-03-10 00:48:32 -0500 | [diff] [blame] | 777 | blk_queue_max_segments(q, vblk->sg_elems-2); |
Rusty Russell | 0864b79 | 2008-12-30 09:26:05 -0600 | [diff] [blame] | 778 | |
Christoph Hellwig | 4eff3ca | 2009-07-17 21:47:45 -0600 | [diff] [blame] | 779 | /* No need to bounce any requests */ |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 780 | blk_queue_bounce_limit(q, BLK_BOUNCE_ANY); |
Christoph Hellwig | 4eff3ca | 2009-07-17 21:47:45 -0600 | [diff] [blame] | 781 | |
Rusty Russell | 4b7f7e2 | 2008-12-30 09:26:04 -0600 | [diff] [blame] | 782 | /* No real sector limit. */ |
Martin K. Petersen | ee714f2 | 2010-03-10 00:48:32 -0500 | [diff] [blame] | 783 | blk_queue_max_hw_sectors(q, -1U); |
Rusty Russell | 4b7f7e2 | 2008-12-30 09:26:04 -0600 | [diff] [blame] | 784 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 785 | /* Host can optionally specify maximum segment size and number of |
| 786 | * segments. */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 787 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX, |
| 788 | struct virtio_blk_config, size_max, &v); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 789 | if (!err) |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 790 | blk_queue_max_segment_size(q, v); |
Rusty Russell | 4b7f7e2 | 2008-12-30 09:26:04 -0600 | [diff] [blame] | 791 | else |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 792 | blk_queue_max_segment_size(q, -1U); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 793 | |
Christian Borntraeger | 066f4d8 | 2008-05-29 11:08:26 +0200 | [diff] [blame] | 794 | /* Host can optionally specify the block size of the device */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 795 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE, |
| 796 | struct virtio_blk_config, blk_size, |
| 797 | &blk_size); |
Christian Borntraeger | 066f4d8 | 2008-05-29 11:08:26 +0200 | [diff] [blame] | 798 | if (!err) |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 799 | blk_queue_logical_block_size(q, blk_size); |
| 800 | else |
| 801 | blk_size = queue_logical_block_size(q); |
| 802 | |
| 803 | /* Use topology information if available */ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 804 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 805 | struct virtio_blk_config, physical_block_exp, |
| 806 | &physical_block_exp); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 807 | if (!err && physical_block_exp) |
| 808 | blk_queue_physical_block_size(q, |
| 809 | blk_size * (1 << physical_block_exp)); |
| 810 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 811 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 812 | struct virtio_blk_config, alignment_offset, |
| 813 | &alignment_offset); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 814 | if (!err && alignment_offset) |
| 815 | blk_queue_alignment_offset(q, blk_size * alignment_offset); |
| 816 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 817 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 818 | struct virtio_blk_config, min_io_size, |
| 819 | &min_io_size); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 820 | if (!err && min_io_size) |
| 821 | blk_queue_io_min(q, blk_size * min_io_size); |
| 822 | |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame^] | 823 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY, |
| 824 | struct virtio_blk_config, opt_io_size, |
| 825 | &opt_io_size); |
Christoph Hellwig | 69740c8 | 2010-02-24 14:22:25 -0600 | [diff] [blame] | 826 | if (!err && opt_io_size) |
| 827 | blk_queue_io_opt(q, blk_size * opt_io_size); |
| 828 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 829 | add_disk(vblk->disk); |
Ryan Harper | a5eb9e4 | 2010-06-23 22:19:57 -0500 | [diff] [blame] | 830 | err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial); |
| 831 | if (err) |
| 832 | goto out_del_disk; |
| 833 | |
Paolo Bonzini | cd5d503 | 2012-07-03 15:19:37 +0200 | [diff] [blame] | 834 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) |
| 835 | err = device_create_file(disk_to_dev(vblk->disk), |
| 836 | &dev_attr_cache_type_rw); |
| 837 | else |
| 838 | err = device_create_file(disk_to_dev(vblk->disk), |
| 839 | &dev_attr_cache_type_ro); |
| 840 | if (err) |
| 841 | goto out_del_disk; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 842 | return 0; |
| 843 | |
Ryan Harper | a5eb9e4 | 2010-06-23 22:19:57 -0500 | [diff] [blame] | 844 | out_del_disk: |
| 845 | del_gendisk(vblk->disk); |
| 846 | blk_cleanup_queue(vblk->disk->queue); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 847 | out_put_disk: |
| 848 | put_disk(vblk->disk); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 849 | out_mempool: |
| 850 | mempool_destroy(vblk->pool); |
| 851 | out_free_vq: |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 852 | vdev->config->del_vqs(vdev); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 853 | out_free_vblk: |
| 854 | kfree(vblk); |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 855 | out_free_index: |
| 856 | ida_simple_remove(&vd_index_ida, index); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 857 | out: |
| 858 | return err; |
| 859 | } |
| 860 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 861 | static void virtblk_remove(struct virtio_device *vdev) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 862 | { |
| 863 | struct virtio_blk *vblk = vdev->priv; |
Michael S. Tsirkin | 5087a50 | 2011-10-30 21:29:59 +0200 | [diff] [blame] | 864 | int index = vblk->index; |
Alexander Graf | f4953fe | 2013-01-02 15:37:17 +1030 | [diff] [blame] | 865 | int refc; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 866 | |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 867 | /* Prevent config work handler from accessing the device. */ |
| 868 | mutex_lock(&vblk->config_lock); |
| 869 | vblk->config_enable = false; |
| 870 | mutex_unlock(&vblk->config_lock); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 871 | |
Asias He | 02e2b12 | 2012-05-25 10:34:47 +0800 | [diff] [blame] | 872 | del_gendisk(vblk->disk); |
Asias He | 483001c | 2012-05-25 10:34:48 +0800 | [diff] [blame] | 873 | blk_cleanup_queue(vblk->disk->queue); |
Asias He | 02e2b12 | 2012-05-25 10:34:47 +0800 | [diff] [blame] | 874 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 875 | /* Stop all the virtqueues. */ |
| 876 | vdev->config->reset(vdev); |
| 877 | |
Michael S. Tsirkin | 4678d6f9 | 2012-01-12 15:44:44 +1030 | [diff] [blame] | 878 | flush_work(&vblk->config_work); |
| 879 | |
Alexander Graf | f4953fe | 2013-01-02 15:37:17 +1030 | [diff] [blame] | 880 | refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 881 | put_disk(vblk->disk); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 882 | mempool_destroy(vblk->pool); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 883 | vdev->config->del_vqs(vdev); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 884 | kfree(vblk); |
Alexander Graf | f4953fe | 2013-01-02 15:37:17 +1030 | [diff] [blame] | 885 | |
| 886 | /* Only free device id if we don't have any users */ |
| 887 | if (refc == 1) |
| 888 | ida_simple_remove(&vd_index_ida, index); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 889 | } |
| 890 | |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 891 | #ifdef CONFIG_PM_SLEEP |
Amit Shah | f8fb5bc | 2011-12-22 16:58:30 +0530 | [diff] [blame] | 892 | static int virtblk_freeze(struct virtio_device *vdev) |
| 893 | { |
| 894 | struct virtio_blk *vblk = vdev->priv; |
| 895 | |
| 896 | /* Ensure we don't receive any more interrupts */ |
| 897 | vdev->config->reset(vdev); |
| 898 | |
| 899 | /* Prevent config work handler from accessing the device. */ |
| 900 | mutex_lock(&vblk->config_lock); |
| 901 | vblk->config_enable = false; |
| 902 | mutex_unlock(&vblk->config_lock); |
| 903 | |
| 904 | flush_work(&vblk->config_work); |
| 905 | |
| 906 | spin_lock_irq(vblk->disk->queue->queue_lock); |
| 907 | blk_stop_queue(vblk->disk->queue); |
| 908 | spin_unlock_irq(vblk->disk->queue->queue_lock); |
| 909 | blk_sync_queue(vblk->disk->queue); |
| 910 | |
| 911 | vdev->config->del_vqs(vdev); |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | static int virtblk_restore(struct virtio_device *vdev) |
| 916 | { |
| 917 | struct virtio_blk *vblk = vdev->priv; |
| 918 | int ret; |
| 919 | |
| 920 | vblk->config_enable = true; |
| 921 | ret = init_vq(vdev->priv); |
| 922 | if (!ret) { |
| 923 | spin_lock_irq(vblk->disk->queue->queue_lock); |
| 924 | blk_start_queue(vblk->disk->queue); |
| 925 | spin_unlock_irq(vblk->disk->queue->queue_lock); |
| 926 | } |
| 927 | return ret; |
| 928 | } |
| 929 | #endif |
| 930 | |
Márton Németh | 47483e2 | 2010-01-10 13:40:02 +0100 | [diff] [blame] | 931 | static const struct virtio_device_id id_table[] = { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 932 | { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID }, |
| 933 | { 0 }, |
| 934 | }; |
| 935 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 936 | static unsigned int features[] = { |
Tejun Heo | 02c42b7 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 937 | VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY, |
| 938 | 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] | 939 | 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] | 940 | }; |
| 941 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 942 | static struct virtio_driver virtio_blk = { |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 943 | .feature_table = features, |
| 944 | .feature_table_size = ARRAY_SIZE(features), |
| 945 | .driver.name = KBUILD_MODNAME, |
| 946 | .driver.owner = THIS_MODULE, |
| 947 | .id_table = id_table, |
| 948 | .probe = virtblk_probe, |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 949 | .remove = virtblk_remove, |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 950 | .config_changed = virtblk_config_changed, |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 951 | #ifdef CONFIG_PM_SLEEP |
Amit Shah | f8fb5bc | 2011-12-22 16:58:30 +0530 | [diff] [blame] | 952 | .freeze = virtblk_freeze, |
| 953 | .restore = virtblk_restore, |
| 954 | #endif |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 955 | }; |
| 956 | |
| 957 | static int __init init(void) |
| 958 | { |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 959 | int error; |
| 960 | |
| 961 | virtblk_wq = alloc_workqueue("virtio-blk", 0, 0); |
| 962 | if (!virtblk_wq) |
| 963 | return -ENOMEM; |
| 964 | |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 965 | major = register_blkdev(0, "virtblk"); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 966 | if (major < 0) { |
| 967 | error = major; |
| 968 | goto out_destroy_workqueue; |
| 969 | } |
| 970 | |
| 971 | error = register_virtio_driver(&virtio_blk); |
| 972 | if (error) |
| 973 | goto out_unregister_blkdev; |
| 974 | return 0; |
| 975 | |
| 976 | out_unregister_blkdev: |
| 977 | unregister_blkdev(major, "virtblk"); |
| 978 | out_destroy_workqueue: |
| 979 | destroy_workqueue(virtblk_wq); |
| 980 | return error; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static void __exit fini(void) |
| 984 | { |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 985 | unregister_blkdev(major, "virtblk"); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 986 | unregister_virtio_driver(&virtio_blk); |
Christoph Hellwig | 7a7c924 | 2011-02-01 21:43:48 +0100 | [diff] [blame] | 987 | destroy_workqueue(virtblk_wq); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 988 | } |
| 989 | module_init(init); |
| 990 | module_exit(fini); |
| 991 | |
| 992 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 993 | MODULE_DESCRIPTION("Virtio block driver"); |
| 994 | MODULE_LICENSE("GPL"); |