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