virtio_blk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. //#define DEBUG
  2. #include <linux/spinlock.h>
  3. #include <linux/slab.h>
  4. #include <linux/blkdev.h>
  5. #include <linux/hdreg.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/virtio.h>
  9. #include <linux/virtio_blk.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/string_helpers.h>
  12. #include <scsi/scsi_cmnd.h>
  13. #include <linux/idr.h>
  14. #define PART_BITS 4
  15. static int major;
  16. static DEFINE_IDA(vd_index_ida);
  17. struct workqueue_struct *virtblk_wq;
  18. struct virtio_blk
  19. {
  20. spinlock_t lock;
  21. struct virtio_device *vdev;
  22. struct virtqueue *vq;
  23. /* The disk structure for the kernel. */
  24. struct gendisk *disk;
  25. /* Request tracking. */
  26. struct list_head reqs;
  27. mempool_t *pool;
  28. /* Process context for config space updates */
  29. struct work_struct config_work;
  30. /* Lock for config space updates */
  31. struct mutex config_lock;
  32. /* enable config space updates */
  33. bool config_enable;
  34. /* What host tells us, plus 2 for header & tailer. */
  35. unsigned int sg_elems;
  36. /* Ida index - used to track minor number allocations. */
  37. int index;
  38. /* Scatterlist: can be too big for stack. */
  39. struct scatterlist sg[/*sg_elems*/];
  40. };
  41. struct virtblk_req
  42. {
  43. struct list_head list;
  44. struct request *req;
  45. struct virtio_blk_outhdr out_hdr;
  46. struct virtio_scsi_inhdr in_hdr;
  47. u8 status;
  48. };
  49. static void blk_done(struct virtqueue *vq)
  50. {
  51. struct virtio_blk *vblk = vq->vdev->priv;
  52. struct virtblk_req *vbr;
  53. unsigned int len;
  54. unsigned long flags;
  55. spin_lock_irqsave(&vblk->lock, flags);
  56. while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
  57. int error;
  58. switch (vbr->status) {
  59. case VIRTIO_BLK_S_OK:
  60. error = 0;
  61. break;
  62. case VIRTIO_BLK_S_UNSUPP:
  63. error = -ENOTTY;
  64. break;
  65. default:
  66. error = -EIO;
  67. break;
  68. }
  69. switch (vbr->req->cmd_type) {
  70. case REQ_TYPE_BLOCK_PC:
  71. vbr->req->resid_len = vbr->in_hdr.residual;
  72. vbr->req->sense_len = vbr->in_hdr.sense_len;
  73. vbr->req->errors = vbr->in_hdr.errors;
  74. break;
  75. case REQ_TYPE_SPECIAL:
  76. vbr->req->errors = (error != 0);
  77. break;
  78. default:
  79. break;
  80. }
  81. __blk_end_request_all(vbr->req, error);
  82. list_del(&vbr->list);
  83. mempool_free(vbr, vblk->pool);
  84. }
  85. /* In case queue is stopped waiting for more buffers. */
  86. blk_start_queue(vblk->disk->queue);
  87. spin_unlock_irqrestore(&vblk->lock, flags);
  88. }
  89. static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
  90. struct request *req)
  91. {
  92. unsigned long num, out = 0, in = 0;
  93. struct virtblk_req *vbr;
  94. vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
  95. if (!vbr)
  96. /* When another request finishes we'll try again. */
  97. return false;
  98. vbr->req = req;
  99. if (req->cmd_flags & REQ_FLUSH) {
  100. vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
  101. vbr->out_hdr.sector = 0;
  102. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  103. } else {
  104. switch (req->cmd_type) {
  105. case REQ_TYPE_FS:
  106. vbr->out_hdr.type = 0;
  107. vbr->out_hdr.sector = blk_rq_pos(vbr->req);
  108. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  109. break;
  110. case REQ_TYPE_BLOCK_PC:
  111. vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  112. vbr->out_hdr.sector = 0;
  113. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  114. break;
  115. case REQ_TYPE_SPECIAL:
  116. vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
  117. vbr->out_hdr.sector = 0;
  118. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  119. break;
  120. default:
  121. /* We don't put anything else in the queue. */
  122. BUG();
  123. }
  124. }
  125. sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
  126. /*
  127. * If this is a packet command we need a couple of additional headers.
  128. * Behind the normal outhdr we put a segment with the scsi command
  129. * block, and before the normal inhdr we put the sense data and the
  130. * inhdr with additional status information before the normal inhdr.
  131. */
  132. if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
  133. sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
  134. num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
  135. if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
  136. sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
  137. sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
  138. sizeof(vbr->in_hdr));
  139. }
  140. sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
  141. sizeof(vbr->status));
  142. if (num) {
  143. if (rq_data_dir(vbr->req) == WRITE) {
  144. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  145. out += num;
  146. } else {
  147. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  148. in += num;
  149. }
  150. }
  151. if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr, GFP_ATOMIC)<0) {
  152. mempool_free(vbr, vblk->pool);
  153. return false;
  154. }
  155. list_add_tail(&vbr->list, &vblk->reqs);
  156. return true;
  157. }
  158. static void do_virtblk_request(struct request_queue *q)
  159. {
  160. struct virtio_blk *vblk = q->queuedata;
  161. struct request *req;
  162. unsigned int issued = 0;
  163. while ((req = blk_peek_request(q)) != NULL) {
  164. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  165. /* If this request fails, stop queue and wait for something to
  166. finish to restart it. */
  167. if (!do_req(q, vblk, req)) {
  168. blk_stop_queue(q);
  169. break;
  170. }
  171. blk_start_request(req);
  172. issued++;
  173. }
  174. if (issued)
  175. virtqueue_kick(vblk->vq);
  176. }
  177. /* return id (s/n) string for *disk to *id_str
  178. */
  179. static int virtblk_get_id(struct gendisk *disk, char *id_str)
  180. {
  181. struct virtio_blk *vblk = disk->private_data;
  182. struct request *req;
  183. struct bio *bio;
  184. int err;
  185. bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
  186. GFP_KERNEL);
  187. if (IS_ERR(bio))
  188. return PTR_ERR(bio);
  189. req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
  190. if (IS_ERR(req)) {
  191. bio_put(bio);
  192. return PTR_ERR(req);
  193. }
  194. req->cmd_type = REQ_TYPE_SPECIAL;
  195. err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
  196. blk_put_request(req);
  197. return err;
  198. }
  199. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  200. unsigned int cmd, unsigned long data)
  201. {
  202. struct gendisk *disk = bdev->bd_disk;
  203. struct virtio_blk *vblk = disk->private_data;
  204. /*
  205. * Only allow the generic SCSI ioctls if the host can support it.
  206. */
  207. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  208. return -ENOTTY;
  209. return scsi_cmd_blk_ioctl(bdev, mode, cmd,
  210. (void __user *)data);
  211. }
  212. /* We provide getgeo only to please some old bootloader/partitioning tools */
  213. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  214. {
  215. struct virtio_blk *vblk = bd->bd_disk->private_data;
  216. struct virtio_blk_geometry vgeo;
  217. int err;
  218. /* see if the host passed in geometry config */
  219. err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
  220. offsetof(struct virtio_blk_config, geometry),
  221. &vgeo);
  222. if (!err) {
  223. geo->heads = vgeo.heads;
  224. geo->sectors = vgeo.sectors;
  225. geo->cylinders = vgeo.cylinders;
  226. } else {
  227. /* some standard values, similar to sd */
  228. geo->heads = 1 << 6;
  229. geo->sectors = 1 << 5;
  230. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  231. }
  232. return 0;
  233. }
  234. static const struct block_device_operations virtblk_fops = {
  235. .ioctl = virtblk_ioctl,
  236. .owner = THIS_MODULE,
  237. .getgeo = virtblk_getgeo,
  238. };
  239. static int index_to_minor(int index)
  240. {
  241. return index << PART_BITS;
  242. }
  243. static int minor_to_index(int minor)
  244. {
  245. return minor >> PART_BITS;
  246. }
  247. static ssize_t virtblk_serial_show(struct device *dev,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. struct gendisk *disk = dev_to_disk(dev);
  251. int err;
  252. /* sysfs gives us a PAGE_SIZE buffer */
  253. BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
  254. buf[VIRTIO_BLK_ID_BYTES] = '\0';
  255. err = virtblk_get_id(disk, buf);
  256. if (!err)
  257. return strlen(buf);
  258. if (err == -EIO) /* Unsupported? Make it empty. */
  259. return 0;
  260. return err;
  261. }
  262. DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
  263. static void virtblk_config_changed_work(struct work_struct *work)
  264. {
  265. struct virtio_blk *vblk =
  266. container_of(work, struct virtio_blk, config_work);
  267. struct virtio_device *vdev = vblk->vdev;
  268. struct request_queue *q = vblk->disk->queue;
  269. char cap_str_2[10], cap_str_10[10];
  270. u64 capacity, size;
  271. mutex_lock(&vblk->config_lock);
  272. if (!vblk->config_enable)
  273. goto done;
  274. /* Host must always specify the capacity. */
  275. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  276. &capacity, sizeof(capacity));
  277. /* If capacity is too big, truncate with warning. */
  278. if ((sector_t)capacity != capacity) {
  279. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  280. (unsigned long long)capacity);
  281. capacity = (sector_t)-1;
  282. }
  283. size = capacity * queue_logical_block_size(q);
  284. string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  285. string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
  286. dev_notice(&vdev->dev,
  287. "new size: %llu %d-byte logical blocks (%s/%s)\n",
  288. (unsigned long long)capacity,
  289. queue_logical_block_size(q),
  290. cap_str_10, cap_str_2);
  291. set_capacity(vblk->disk, capacity);
  292. done:
  293. mutex_unlock(&vblk->config_lock);
  294. }
  295. static void virtblk_config_changed(struct virtio_device *vdev)
  296. {
  297. struct virtio_blk *vblk = vdev->priv;
  298. queue_work(virtblk_wq, &vblk->config_work);
  299. }
  300. static int init_vq(struct virtio_blk *vblk)
  301. {
  302. int err = 0;
  303. /* We expect one virtqueue, for output. */
  304. vblk->vq = virtio_find_single_vq(vblk->vdev, blk_done, "requests");
  305. if (IS_ERR(vblk->vq))
  306. err = PTR_ERR(vblk->vq);
  307. return err;
  308. }
  309. static int __devinit virtblk_probe(struct virtio_device *vdev)
  310. {
  311. struct virtio_blk *vblk;
  312. struct request_queue *q;
  313. int err, index;
  314. u64 cap;
  315. u32 v, blk_size, sg_elems, opt_io_size;
  316. u16 min_io_size;
  317. u8 physical_block_exp, alignment_offset;
  318. err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
  319. GFP_KERNEL);
  320. if (err < 0)
  321. goto out;
  322. index = err;
  323. /* We need to know how many segments before we allocate. */
  324. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  325. offsetof(struct virtio_blk_config, seg_max),
  326. &sg_elems);
  327. /* We need at least one SG element, whatever they say. */
  328. if (err || !sg_elems)
  329. sg_elems = 1;
  330. /* We need an extra sg elements at head and tail. */
  331. sg_elems += 2;
  332. vdev->priv = vblk = kmalloc(sizeof(*vblk) +
  333. sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
  334. if (!vblk) {
  335. err = -ENOMEM;
  336. goto out_free_index;
  337. }
  338. INIT_LIST_HEAD(&vblk->reqs);
  339. spin_lock_init(&vblk->lock);
  340. vblk->vdev = vdev;
  341. vblk->sg_elems = sg_elems;
  342. sg_init_table(vblk->sg, vblk->sg_elems);
  343. mutex_init(&vblk->config_lock);
  344. INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
  345. vblk->config_enable = true;
  346. err = init_vq(vblk);
  347. if (err)
  348. goto out_free_vblk;
  349. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  350. if (!vblk->pool) {
  351. err = -ENOMEM;
  352. goto out_free_vq;
  353. }
  354. /* FIXME: How many partitions? How long is a piece of string? */
  355. vblk->disk = alloc_disk(1 << PART_BITS);
  356. if (!vblk->disk) {
  357. err = -ENOMEM;
  358. goto out_mempool;
  359. }
  360. q = vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  361. if (!q) {
  362. err = -ENOMEM;
  363. goto out_put_disk;
  364. }
  365. q->queuedata = vblk;
  366. if (index < 26) {
  367. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  368. } else if (index < (26 + 1) * 26) {
  369. sprintf(vblk->disk->disk_name, "vd%c%c",
  370. 'a' + index / 26 - 1, 'a' + index % 26);
  371. } else {
  372. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  373. const unsigned int m2 = (index / 26 - 1) % 26;
  374. const unsigned int m3 = index % 26;
  375. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  376. 'a' + m1, 'a' + m2, 'a' + m3);
  377. }
  378. vblk->disk->major = major;
  379. vblk->disk->first_minor = index_to_minor(index);
  380. vblk->disk->private_data = vblk;
  381. vblk->disk->fops = &virtblk_fops;
  382. vblk->disk->driverfs_dev = &vdev->dev;
  383. vblk->index = index;
  384. /* configure queue flush support */
  385. if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
  386. blk_queue_flush(q, REQ_FLUSH);
  387. /* If disk is read-only in the host, the guest should obey */
  388. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  389. set_disk_ro(vblk->disk, 1);
  390. /* Host must always specify the capacity. */
  391. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  392. &cap, sizeof(cap));
  393. /* If capacity is too big, truncate with warning. */
  394. if ((sector_t)cap != cap) {
  395. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  396. (unsigned long long)cap);
  397. cap = (sector_t)-1;
  398. }
  399. set_capacity(vblk->disk, cap);
  400. /* We can handle whatever the host told us to handle. */
  401. blk_queue_max_segments(q, vblk->sg_elems-2);
  402. /* No need to bounce any requests */
  403. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  404. /* No real sector limit. */
  405. blk_queue_max_hw_sectors(q, -1U);
  406. /* Host can optionally specify maximum segment size and number of
  407. * segments. */
  408. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  409. offsetof(struct virtio_blk_config, size_max),
  410. &v);
  411. if (!err)
  412. blk_queue_max_segment_size(q, v);
  413. else
  414. blk_queue_max_segment_size(q, -1U);
  415. /* Host can optionally specify the block size of the device */
  416. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  417. offsetof(struct virtio_blk_config, blk_size),
  418. &blk_size);
  419. if (!err)
  420. blk_queue_logical_block_size(q, blk_size);
  421. else
  422. blk_size = queue_logical_block_size(q);
  423. /* Use topology information if available */
  424. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  425. offsetof(struct virtio_blk_config, physical_block_exp),
  426. &physical_block_exp);
  427. if (!err && physical_block_exp)
  428. blk_queue_physical_block_size(q,
  429. blk_size * (1 << physical_block_exp));
  430. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  431. offsetof(struct virtio_blk_config, alignment_offset),
  432. &alignment_offset);
  433. if (!err && alignment_offset)
  434. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  435. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  436. offsetof(struct virtio_blk_config, min_io_size),
  437. &min_io_size);
  438. if (!err && min_io_size)
  439. blk_queue_io_min(q, blk_size * min_io_size);
  440. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  441. offsetof(struct virtio_blk_config, opt_io_size),
  442. &opt_io_size);
  443. if (!err && opt_io_size)
  444. blk_queue_io_opt(q, blk_size * opt_io_size);
  445. add_disk(vblk->disk);
  446. err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
  447. if (err)
  448. goto out_del_disk;
  449. return 0;
  450. out_del_disk:
  451. del_gendisk(vblk->disk);
  452. blk_cleanup_queue(vblk->disk->queue);
  453. out_put_disk:
  454. put_disk(vblk->disk);
  455. out_mempool:
  456. mempool_destroy(vblk->pool);
  457. out_free_vq:
  458. vdev->config->del_vqs(vdev);
  459. out_free_vblk:
  460. kfree(vblk);
  461. out_free_index:
  462. ida_simple_remove(&vd_index_ida, index);
  463. out:
  464. return err;
  465. }
  466. static void __devexit virtblk_remove(struct virtio_device *vdev)
  467. {
  468. struct virtio_blk *vblk = vdev->priv;
  469. int index = vblk->index;
  470. /* Prevent config work handler from accessing the device. */
  471. mutex_lock(&vblk->config_lock);
  472. vblk->config_enable = false;
  473. mutex_unlock(&vblk->config_lock);
  474. /* Nothing should be pending. */
  475. BUG_ON(!list_empty(&vblk->reqs));
  476. /* Stop all the virtqueues. */
  477. vdev->config->reset(vdev);
  478. flush_work(&vblk->config_work);
  479. del_gendisk(vblk->disk);
  480. blk_cleanup_queue(vblk->disk->queue);
  481. put_disk(vblk->disk);
  482. mempool_destroy(vblk->pool);
  483. vdev->config->del_vqs(vdev);
  484. kfree(vblk);
  485. ida_simple_remove(&vd_index_ida, index);
  486. }
  487. #ifdef CONFIG_PM
  488. static int virtblk_freeze(struct virtio_device *vdev)
  489. {
  490. struct virtio_blk *vblk = vdev->priv;
  491. /* Ensure we don't receive any more interrupts */
  492. vdev->config->reset(vdev);
  493. /* Prevent config work handler from accessing the device. */
  494. mutex_lock(&vblk->config_lock);
  495. vblk->config_enable = false;
  496. mutex_unlock(&vblk->config_lock);
  497. flush_work(&vblk->config_work);
  498. spin_lock_irq(vblk->disk->queue->queue_lock);
  499. blk_stop_queue(vblk->disk->queue);
  500. spin_unlock_irq(vblk->disk->queue->queue_lock);
  501. blk_sync_queue(vblk->disk->queue);
  502. vdev->config->del_vqs(vdev);
  503. return 0;
  504. }
  505. static int virtblk_restore(struct virtio_device *vdev)
  506. {
  507. struct virtio_blk *vblk = vdev->priv;
  508. int ret;
  509. vblk->config_enable = true;
  510. ret = init_vq(vdev->priv);
  511. if (!ret) {
  512. spin_lock_irq(vblk->disk->queue->queue_lock);
  513. blk_start_queue(vblk->disk->queue);
  514. spin_unlock_irq(vblk->disk->queue->queue_lock);
  515. }
  516. return ret;
  517. }
  518. #endif
  519. static const struct virtio_device_id id_table[] = {
  520. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  521. { 0 },
  522. };
  523. static unsigned int features[] = {
  524. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  525. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
  526. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY
  527. };
  528. /*
  529. * virtio_blk causes spurious section mismatch warning by
  530. * simultaneously referring to a __devinit and a __devexit function.
  531. * Use __refdata to avoid this warning.
  532. */
  533. static struct virtio_driver __refdata virtio_blk = {
  534. .feature_table = features,
  535. .feature_table_size = ARRAY_SIZE(features),
  536. .driver.name = KBUILD_MODNAME,
  537. .driver.owner = THIS_MODULE,
  538. .id_table = id_table,
  539. .probe = virtblk_probe,
  540. .remove = __devexit_p(virtblk_remove),
  541. .config_changed = virtblk_config_changed,
  542. #ifdef CONFIG_PM
  543. .freeze = virtblk_freeze,
  544. .restore = virtblk_restore,
  545. #endif
  546. };
  547. static int __init init(void)
  548. {
  549. int error;
  550. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  551. if (!virtblk_wq)
  552. return -ENOMEM;
  553. major = register_blkdev(0, "virtblk");
  554. if (major < 0) {
  555. error = major;
  556. goto out_destroy_workqueue;
  557. }
  558. error = register_virtio_driver(&virtio_blk);
  559. if (error)
  560. goto out_unregister_blkdev;
  561. return 0;
  562. out_unregister_blkdev:
  563. unregister_blkdev(major, "virtblk");
  564. out_destroy_workqueue:
  565. destroy_workqueue(virtblk_wq);
  566. return error;
  567. }
  568. static void __exit fini(void)
  569. {
  570. unregister_blkdev(major, "virtblk");
  571. unregister_virtio_driver(&virtio_blk);
  572. destroy_workqueue(virtblk_wq);
  573. }
  574. module_init(init);
  575. module_exit(fini);
  576. MODULE_DEVICE_TABLE(virtio, id_table);
  577. MODULE_DESCRIPTION("Virtio block driver");
  578. MODULE_LICENSE("GPL");