Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 1 | //#define DEBUG |
| 2 | #include <linux/spinlock.h> |
| 3 | #include <linux/blkdev.h> |
| 4 | #include <linux/hdreg.h> |
| 5 | #include <linux/virtio.h> |
| 6 | #include <linux/virtio_blk.h> |
Jens Axboe | 3d1266c | 2007-10-24 13:21:21 +0200 | [diff] [blame] | 7 | #include <linux/scatterlist.h> |
| 8 | |
| 9 | #define VIRTIO_MAX_SG (3+MAX_PHYS_SEGMENTS) |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 10 | #define PART_BITS 4 |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 11 | |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 12 | static int major, index; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 13 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 14 | struct virtio_blk |
| 15 | { |
| 16 | spinlock_t lock; |
| 17 | |
| 18 | struct virtio_device *vdev; |
| 19 | struct virtqueue *vq; |
| 20 | |
| 21 | /* The disk structure for the kernel. */ |
| 22 | struct gendisk *disk; |
| 23 | |
| 24 | /* Request tracking. */ |
| 25 | struct list_head reqs; |
| 26 | |
| 27 | mempool_t *pool; |
| 28 | |
| 29 | /* Scatterlist: can be too big for stack. */ |
Jens Axboe | 3d1266c | 2007-10-24 13:21:21 +0200 | [diff] [blame] | 30 | struct scatterlist sg[VIRTIO_MAX_SG]; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct virtblk_req |
| 34 | { |
| 35 | struct list_head list; |
| 36 | struct request *req; |
| 37 | struct virtio_blk_outhdr out_hdr; |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 38 | u8 status; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 39 | }; |
| 40 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 41 | static void blk_done(struct virtqueue *vq) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 42 | { |
| 43 | struct virtio_blk *vblk = vq->vdev->priv; |
| 44 | struct virtblk_req *vbr; |
| 45 | unsigned int len; |
| 46 | unsigned long flags; |
| 47 | |
| 48 | spin_lock_irqsave(&vblk->lock, flags); |
| 49 | while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) { |
Kiyoshi Ueda | 8316982 | 2008-10-01 10:11:20 -0400 | [diff] [blame] | 50 | int error; |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 51 | switch (vbr->status) { |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 52 | case VIRTIO_BLK_S_OK: |
Kiyoshi Ueda | 8316982 | 2008-10-01 10:11:20 -0400 | [diff] [blame] | 53 | error = 0; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 54 | break; |
| 55 | case VIRTIO_BLK_S_UNSUPP: |
Kiyoshi Ueda | 8316982 | 2008-10-01 10:11:20 -0400 | [diff] [blame] | 56 | error = -ENOTTY; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 57 | break; |
| 58 | default: |
Kiyoshi Ueda | 8316982 | 2008-10-01 10:11:20 -0400 | [diff] [blame] | 59 | error = -EIO; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | |
Kiyoshi Ueda | 8316982 | 2008-10-01 10:11:20 -0400 | [diff] [blame] | 63 | __blk_end_request(vbr->req, error, blk_rq_bytes(vbr->req)); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 64 | list_del(&vbr->list); |
| 65 | mempool_free(vbr, vblk->pool); |
| 66 | } |
| 67 | /* In case queue is stopped waiting for more buffers. */ |
| 68 | blk_start_queue(vblk->disk->queue); |
| 69 | spin_unlock_irqrestore(&vblk->lock, flags); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static bool do_req(struct request_queue *q, struct virtio_blk *vblk, |
| 73 | struct request *req) |
| 74 | { |
| 75 | unsigned long num, out, in; |
| 76 | struct virtblk_req *vbr; |
| 77 | |
| 78 | vbr = mempool_alloc(vblk->pool, GFP_ATOMIC); |
| 79 | if (!vbr) |
| 80 | /* When another request finishes we'll try again. */ |
| 81 | return false; |
| 82 | |
| 83 | vbr->req = req; |
| 84 | if (blk_fs_request(vbr->req)) { |
| 85 | vbr->out_hdr.type = 0; |
| 86 | vbr->out_hdr.sector = vbr->req->sector; |
Fernando Luis Vázquez Cao | 766ca44 | 2008-08-14 09:59:13 +0200 | [diff] [blame] | 87 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 88 | } else if (blk_pc_request(vbr->req)) { |
| 89 | vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD; |
| 90 | vbr->out_hdr.sector = 0; |
Fernando Luis Vázquez Cao | 766ca44 | 2008-08-14 09:59:13 +0200 | [diff] [blame] | 91 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 92 | } else { |
| 93 | /* We don't put anything else in the queue. */ |
| 94 | BUG(); |
| 95 | } |
| 96 | |
| 97 | if (blk_barrier_rq(vbr->req)) |
| 98 | vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER; |
| 99 | |
Jens Axboe | 3d1266c | 2007-10-24 13:21:21 +0200 | [diff] [blame] | 100 | /* This init could be done at vblk creation time */ |
| 101 | sg_init_table(vblk->sg, VIRTIO_MAX_SG); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 102 | sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr)); |
| 103 | num = blk_rq_map_sg(q, vbr->req, vblk->sg+1); |
Rusty Russell | cb38fa2 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 104 | sg_set_buf(&vblk->sg[num+1], &vbr->status, sizeof(vbr->status)); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 105 | |
| 106 | if (rq_data_dir(vbr->req) == WRITE) { |
| 107 | vbr->out_hdr.type |= VIRTIO_BLK_T_OUT; |
| 108 | out = 1 + num; |
| 109 | in = 1; |
| 110 | } else { |
| 111 | vbr->out_hdr.type |= VIRTIO_BLK_T_IN; |
| 112 | out = 1; |
| 113 | in = 1 + num; |
| 114 | } |
| 115 | |
| 116 | if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) { |
| 117 | mempool_free(vbr, vblk->pool); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | list_add_tail(&vbr->list, &vblk->reqs); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | static void do_virtblk_request(struct request_queue *q) |
| 126 | { |
| 127 | struct virtio_blk *vblk = NULL; |
| 128 | struct request *req; |
| 129 | unsigned int issued = 0; |
| 130 | |
| 131 | while ((req = elv_next_request(q)) != NULL) { |
| 132 | vblk = req->rq_disk->private_data; |
| 133 | BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg)); |
| 134 | |
| 135 | /* If this request fails, stop queue and wait for something to |
| 136 | finish to restart it. */ |
| 137 | if (!do_req(q, vblk, req)) { |
| 138 | blk_stop_queue(q); |
| 139 | break; |
| 140 | } |
| 141 | blkdev_dequeue_request(req); |
| 142 | issued++; |
| 143 | } |
| 144 | |
| 145 | if (issued) |
| 146 | vblk->vq->vq_ops->kick(vblk->vq); |
| 147 | } |
| 148 | |
Al Viro | 4e10985 | 2008-03-02 10:22:33 -0500 | [diff] [blame] | 149 | static int virtblk_ioctl(struct block_device *bdev, fmode_t mode, |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 150 | unsigned cmd, unsigned long data) |
| 151 | { |
Al Viro | 4e10985 | 2008-03-02 10:22:33 -0500 | [diff] [blame] | 152 | return scsi_cmd_ioctl(bdev->bd_disk->queue, |
| 153 | bdev->bd_disk, mode, cmd, |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 154 | (void __user *)data); |
| 155 | } |
| 156 | |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 157 | /* We provide getgeo only to please some old bootloader/partitioning tools */ |
| 158 | static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) |
| 159 | { |
Ryan Harper | 48e4043 | 2008-04-16 13:56:37 -0500 | [diff] [blame] | 160 | struct virtio_blk *vblk = bd->bd_disk->private_data; |
| 161 | struct virtio_blk_geometry vgeo; |
| 162 | int err; |
| 163 | |
| 164 | /* see if the host passed in geometry config */ |
| 165 | err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY, |
| 166 | offsetof(struct virtio_blk_config, geometry), |
| 167 | &vgeo); |
| 168 | |
| 169 | if (!err) { |
| 170 | geo->heads = vgeo.heads; |
| 171 | geo->sectors = vgeo.sectors; |
| 172 | geo->cylinders = vgeo.cylinders; |
| 173 | } else { |
| 174 | /* some standard values, similar to sd */ |
| 175 | geo->heads = 1 << 6; |
| 176 | geo->sectors = 1 << 5; |
| 177 | geo->cylinders = get_capacity(bd->bd_disk) >> 11; |
| 178 | } |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 182 | static struct block_device_operations virtblk_fops = { |
Al Viro | 4e10985 | 2008-03-02 10:22:33 -0500 | [diff] [blame] | 183 | .locked_ioctl = virtblk_ioctl, |
Christian Borntraeger | 135da0b | 2008-01-23 17:56:50 +0100 | [diff] [blame] | 184 | .owner = THIS_MODULE, |
| 185 | .getgeo = virtblk_getgeo, |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 186 | }; |
| 187 | |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 188 | static int index_to_minor(int index) |
| 189 | { |
| 190 | return index << PART_BITS; |
| 191 | } |
| 192 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 193 | static int virtblk_probe(struct virtio_device *vdev) |
| 194 | { |
| 195 | struct virtio_blk *vblk; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 196 | int err; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 197 | u64 cap; |
| 198 | u32 v; |
Christian Borntraeger | 066f4d8 | 2008-05-29 11:08:26 +0200 | [diff] [blame] | 199 | u32 blk_size; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 200 | |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 201 | if (index_to_minor(index) >= 1 << MINORBITS) |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 202 | return -ENOSPC; |
| 203 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 204 | vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL); |
| 205 | if (!vblk) { |
| 206 | err = -ENOMEM; |
| 207 | goto out; |
| 208 | } |
| 209 | |
| 210 | INIT_LIST_HEAD(&vblk->reqs); |
| 211 | spin_lock_init(&vblk->lock); |
| 212 | vblk->vdev = vdev; |
| 213 | |
| 214 | /* We expect one virtqueue, for output. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 215 | vblk->vq = vdev->config->find_vq(vdev, 0, blk_done); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 216 | if (IS_ERR(vblk->vq)) { |
| 217 | err = PTR_ERR(vblk->vq); |
| 218 | goto out_free_vblk; |
| 219 | } |
| 220 | |
| 221 | vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req)); |
| 222 | if (!vblk->pool) { |
| 223 | err = -ENOMEM; |
| 224 | goto out_free_vq; |
| 225 | } |
| 226 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 227 | /* FIXME: How many partitions? How long is a piece of string? */ |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 228 | vblk->disk = alloc_disk(1 << PART_BITS); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 229 | if (!vblk->disk) { |
| 230 | err = -ENOMEM; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 231 | goto out_mempool; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock); |
| 235 | if (!vblk->disk->queue) { |
| 236 | err = -ENOMEM; |
| 237 | goto out_put_disk; |
| 238 | } |
| 239 | |
Fernando Luis Vázquez Cao | 7d116b6 | 2008-10-27 18:45:15 +0900 | [diff] [blame^] | 240 | queue_flag_set_unlocked(QUEUE_FLAG_VIRT, vblk->disk->queue); |
| 241 | |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 242 | if (index < 26) { |
| 243 | sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26); |
| 244 | } else if (index < (26 + 1) * 26) { |
| 245 | sprintf(vblk->disk->disk_name, "vd%c%c", |
| 246 | 'a' + index / 26 - 1, 'a' + index % 26); |
| 247 | } else { |
| 248 | const unsigned int m1 = (index / 26 - 1) / 26 - 1; |
| 249 | const unsigned int m2 = (index / 26 - 1) % 26; |
| 250 | const unsigned int m3 = index % 26; |
| 251 | sprintf(vblk->disk->disk_name, "vd%c%c%c", |
| 252 | 'a' + m1, 'a' + m2, 'a' + m3); |
| 253 | } |
| 254 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 255 | vblk->disk->major = major; |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 256 | vblk->disk->first_minor = index_to_minor(index); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 257 | vblk->disk->private_data = vblk; |
| 258 | vblk->disk->fops = &virtblk_fops; |
Jeremy Katz | c483934 | 2008-03-02 17:00:15 -0500 | [diff] [blame] | 259 | vblk->disk->driverfs_dev = &vdev->dev; |
Christian Borntraeger | d50ed90 | 2008-02-01 09:05:00 +0100 | [diff] [blame] | 260 | index++; |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 261 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 262 | /* If barriers are supported, tell block layer that queue is ordered */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 263 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER)) |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 264 | blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL); |
| 265 | |
Christian Borntraeger | 3ef5360 | 2008-05-16 11:17:03 +0200 | [diff] [blame] | 266 | /* If disk is read-only in the host, the guest should obey */ |
| 267 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO)) |
| 268 | set_disk_ro(vblk->disk, 1); |
| 269 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 270 | /* Host must always specify the capacity. */ |
Rusty Russell | 72e61eb | 2008-05-02 21:50:49 -0500 | [diff] [blame] | 271 | vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity), |
| 272 | &cap, sizeof(cap)); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 273 | |
| 274 | /* If capacity is too big, truncate with warning. */ |
| 275 | if ((sector_t)cap != cap) { |
| 276 | dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n", |
| 277 | (unsigned long long)cap); |
| 278 | cap = (sector_t)-1; |
| 279 | } |
| 280 | set_capacity(vblk->disk, cap); |
| 281 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 282 | /* Host can optionally specify maximum segment size and number of |
| 283 | * segments. */ |
| 284 | err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX, |
| 285 | offsetof(struct virtio_blk_config, size_max), |
| 286 | &v); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 287 | if (!err) |
| 288 | blk_queue_max_segment_size(vblk->disk->queue, v); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 289 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 290 | err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX, |
| 291 | offsetof(struct virtio_blk_config, seg_max), |
| 292 | &v); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 293 | if (!err) |
| 294 | blk_queue_max_hw_segments(vblk->disk->queue, v); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 295 | |
Christian Borntraeger | 066f4d8 | 2008-05-29 11:08:26 +0200 | [diff] [blame] | 296 | /* Host can optionally specify the block size of the device */ |
| 297 | err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE, |
| 298 | offsetof(struct virtio_blk_config, blk_size), |
| 299 | &blk_size); |
| 300 | if (!err) |
| 301 | blk_queue_hardsect_size(vblk->disk->queue, blk_size); |
| 302 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 303 | add_disk(vblk->disk); |
| 304 | return 0; |
| 305 | |
| 306 | out_put_disk: |
| 307 | put_disk(vblk->disk); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 308 | out_mempool: |
| 309 | mempool_destroy(vblk->pool); |
| 310 | out_free_vq: |
| 311 | vdev->config->del_vq(vblk->vq); |
| 312 | out_free_vblk: |
| 313 | kfree(vblk); |
| 314 | out: |
| 315 | return err; |
| 316 | } |
| 317 | |
| 318 | static void virtblk_remove(struct virtio_device *vdev) |
| 319 | { |
| 320 | struct virtio_blk *vblk = vdev->priv; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 321 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 322 | /* Nothing should be pending. */ |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 323 | BUG_ON(!list_empty(&vblk->reqs)); |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 324 | |
| 325 | /* Stop all the virtqueues. */ |
| 326 | vdev->config->reset(vdev); |
| 327 | |
Chris Lalancette | ac9d463 | 2008-05-30 15:09:41 -0500 | [diff] [blame] | 328 | del_gendisk(vblk->disk); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 329 | blk_cleanup_queue(vblk->disk->queue); |
| 330 | put_disk(vblk->disk); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 331 | mempool_destroy(vblk->pool); |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 332 | vdev->config->del_vq(vblk->vq); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 333 | kfree(vblk); |
| 334 | } |
| 335 | |
| 336 | static struct virtio_device_id id_table[] = { |
| 337 | { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID }, |
| 338 | { 0 }, |
| 339 | }; |
| 340 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 341 | static unsigned int features[] = { |
| 342 | VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, |
Christian Borntraeger | 066f4d8 | 2008-05-29 11:08:26 +0200 | [diff] [blame] | 343 | VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 344 | }; |
| 345 | |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 346 | static struct virtio_driver virtio_blk = { |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 347 | .feature_table = features, |
| 348 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 349 | .driver.name = KBUILD_MODNAME, |
| 350 | .driver.owner = THIS_MODULE, |
| 351 | .id_table = id_table, |
| 352 | .probe = virtblk_probe, |
| 353 | .remove = __devexit_p(virtblk_remove), |
| 354 | }; |
| 355 | |
| 356 | static int __init init(void) |
| 357 | { |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 358 | major = register_blkdev(0, "virtblk"); |
| 359 | if (major < 0) |
| 360 | return major; |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 361 | return register_virtio_driver(&virtio_blk); |
| 362 | } |
| 363 | |
| 364 | static void __exit fini(void) |
| 365 | { |
Christian Borntraeger | 4f3bf19 | 2008-01-31 15:53:53 +0100 | [diff] [blame] | 366 | unregister_blkdev(major, "virtblk"); |
Rusty Russell | e467cde | 2007-10-22 11:03:38 +1000 | [diff] [blame] | 367 | unregister_virtio_driver(&virtio_blk); |
| 368 | } |
| 369 | module_init(init); |
| 370 | module_exit(fini); |
| 371 | |
| 372 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 373 | MODULE_DESCRIPTION("Virtio block driver"); |
| 374 | MODULE_LICENSE("GPL"); |