virtio_blk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. /*
  310. * Legacy naming scheme used for virtio devices. We are stuck with it for
  311. * virtio blk but don't ever use it for any new driver.
  312. */
  313. static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
  314. {
  315. const int base = 'z' - 'a' + 1;
  316. char *begin = buf + strlen(prefix);
  317. char *end = buf + buflen;
  318. char *p;
  319. int unit;
  320. p = end - 1;
  321. *p = '\0';
  322. unit = base;
  323. do {
  324. if (p == begin)
  325. return -EINVAL;
  326. *--p = 'a' + (index % unit);
  327. index = (index / unit) - 1;
  328. } while (index >= 0);
  329. memmove(begin, p, end - p);
  330. memcpy(buf, prefix, strlen(prefix));
  331. return 0;
  332. }
  333. static int __devinit virtblk_probe(struct virtio_device *vdev)
  334. {
  335. struct virtio_blk *vblk;
  336. struct request_queue *q;
  337. int err, index;
  338. u64 cap;
  339. u32 v, blk_size, sg_elems, opt_io_size;
  340. u16 min_io_size;
  341. u8 physical_block_exp, alignment_offset;
  342. err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
  343. GFP_KERNEL);
  344. if (err < 0)
  345. goto out;
  346. index = err;
  347. /* We need to know how many segments before we allocate. */
  348. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  349. offsetof(struct virtio_blk_config, seg_max),
  350. &sg_elems);
  351. /* We need at least one SG element, whatever they say. */
  352. if (err || !sg_elems)
  353. sg_elems = 1;
  354. /* We need an extra sg elements at head and tail. */
  355. sg_elems += 2;
  356. vdev->priv = vblk = kmalloc(sizeof(*vblk) +
  357. sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
  358. if (!vblk) {
  359. err = -ENOMEM;
  360. goto out_free_index;
  361. }
  362. INIT_LIST_HEAD(&vblk->reqs);
  363. spin_lock_init(&vblk->lock);
  364. vblk->vdev = vdev;
  365. vblk->sg_elems = sg_elems;
  366. sg_init_table(vblk->sg, vblk->sg_elems);
  367. mutex_init(&vblk->config_lock);
  368. INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
  369. vblk->config_enable = true;
  370. err = init_vq(vblk);
  371. if (err)
  372. goto out_free_vblk;
  373. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  374. if (!vblk->pool) {
  375. err = -ENOMEM;
  376. goto out_free_vq;
  377. }
  378. /* FIXME: How many partitions? How long is a piece of string? */
  379. vblk->disk = alloc_disk(1 << PART_BITS);
  380. if (!vblk->disk) {
  381. err = -ENOMEM;
  382. goto out_mempool;
  383. }
  384. q = vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  385. if (!q) {
  386. err = -ENOMEM;
  387. goto out_put_disk;
  388. }
  389. q->queuedata = vblk;
  390. virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
  391. vblk->disk->major = major;
  392. vblk->disk->first_minor = index_to_minor(index);
  393. vblk->disk->private_data = vblk;
  394. vblk->disk->fops = &virtblk_fops;
  395. vblk->disk->driverfs_dev = &vdev->dev;
  396. vblk->index = index;
  397. /* configure queue flush support */
  398. if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
  399. blk_queue_flush(q, REQ_FLUSH);
  400. /* If disk is read-only in the host, the guest should obey */
  401. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  402. set_disk_ro(vblk->disk, 1);
  403. /* Host must always specify the capacity. */
  404. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  405. &cap, sizeof(cap));
  406. /* If capacity is too big, truncate with warning. */
  407. if ((sector_t)cap != cap) {
  408. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  409. (unsigned long long)cap);
  410. cap = (sector_t)-1;
  411. }
  412. set_capacity(vblk->disk, cap);
  413. /* We can handle whatever the host told us to handle. */
  414. blk_queue_max_segments(q, vblk->sg_elems-2);
  415. /* No need to bounce any requests */
  416. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  417. /* No real sector limit. */
  418. blk_queue_max_hw_sectors(q, -1U);
  419. /* Host can optionally specify maximum segment size and number of
  420. * segments. */
  421. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  422. offsetof(struct virtio_blk_config, size_max),
  423. &v);
  424. if (!err)
  425. blk_queue_max_segment_size(q, v);
  426. else
  427. blk_queue_max_segment_size(q, -1U);
  428. /* Host can optionally specify the block size of the device */
  429. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  430. offsetof(struct virtio_blk_config, blk_size),
  431. &blk_size);
  432. if (!err)
  433. blk_queue_logical_block_size(q, blk_size);
  434. else
  435. blk_size = queue_logical_block_size(q);
  436. /* Use topology information if available */
  437. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  438. offsetof(struct virtio_blk_config, physical_block_exp),
  439. &physical_block_exp);
  440. if (!err && physical_block_exp)
  441. blk_queue_physical_block_size(q,
  442. blk_size * (1 << physical_block_exp));
  443. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  444. offsetof(struct virtio_blk_config, alignment_offset),
  445. &alignment_offset);
  446. if (!err && alignment_offset)
  447. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  448. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  449. offsetof(struct virtio_blk_config, min_io_size),
  450. &min_io_size);
  451. if (!err && min_io_size)
  452. blk_queue_io_min(q, blk_size * min_io_size);
  453. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  454. offsetof(struct virtio_blk_config, opt_io_size),
  455. &opt_io_size);
  456. if (!err && opt_io_size)
  457. blk_queue_io_opt(q, blk_size * opt_io_size);
  458. add_disk(vblk->disk);
  459. err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
  460. if (err)
  461. goto out_del_disk;
  462. return 0;
  463. out_del_disk:
  464. del_gendisk(vblk->disk);
  465. blk_cleanup_queue(vblk->disk->queue);
  466. out_put_disk:
  467. put_disk(vblk->disk);
  468. out_mempool:
  469. mempool_destroy(vblk->pool);
  470. out_free_vq:
  471. vdev->config->del_vqs(vdev);
  472. out_free_vblk:
  473. kfree(vblk);
  474. out_free_index:
  475. ida_simple_remove(&vd_index_ida, index);
  476. out:
  477. return err;
  478. }
  479. static void __devexit virtblk_remove(struct virtio_device *vdev)
  480. {
  481. struct virtio_blk *vblk = vdev->priv;
  482. int index = vblk->index;
  483. /* Prevent config work handler from accessing the device. */
  484. mutex_lock(&vblk->config_lock);
  485. vblk->config_enable = false;
  486. mutex_unlock(&vblk->config_lock);
  487. /* Nothing should be pending. */
  488. BUG_ON(!list_empty(&vblk->reqs));
  489. /* Stop all the virtqueues. */
  490. vdev->config->reset(vdev);
  491. flush_work(&vblk->config_work);
  492. del_gendisk(vblk->disk);
  493. blk_cleanup_queue(vblk->disk->queue);
  494. put_disk(vblk->disk);
  495. mempool_destroy(vblk->pool);
  496. vdev->config->del_vqs(vdev);
  497. kfree(vblk);
  498. ida_simple_remove(&vd_index_ida, index);
  499. }
  500. #ifdef CONFIG_PM
  501. static int virtblk_freeze(struct virtio_device *vdev)
  502. {
  503. struct virtio_blk *vblk = vdev->priv;
  504. /* Ensure we don't receive any more interrupts */
  505. vdev->config->reset(vdev);
  506. /* Prevent config work handler from accessing the device. */
  507. mutex_lock(&vblk->config_lock);
  508. vblk->config_enable = false;
  509. mutex_unlock(&vblk->config_lock);
  510. flush_work(&vblk->config_work);
  511. spin_lock_irq(vblk->disk->queue->queue_lock);
  512. blk_stop_queue(vblk->disk->queue);
  513. spin_unlock_irq(vblk->disk->queue->queue_lock);
  514. blk_sync_queue(vblk->disk->queue);
  515. vdev->config->del_vqs(vdev);
  516. return 0;
  517. }
  518. static int virtblk_restore(struct virtio_device *vdev)
  519. {
  520. struct virtio_blk *vblk = vdev->priv;
  521. int ret;
  522. vblk->config_enable = true;
  523. ret = init_vq(vdev->priv);
  524. if (!ret) {
  525. spin_lock_irq(vblk->disk->queue->queue_lock);
  526. blk_start_queue(vblk->disk->queue);
  527. spin_unlock_irq(vblk->disk->queue->queue_lock);
  528. }
  529. return ret;
  530. }
  531. #endif
  532. static const struct virtio_device_id id_table[] = {
  533. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  534. { 0 },
  535. };
  536. static unsigned int features[] = {
  537. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  538. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
  539. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY
  540. };
  541. /*
  542. * virtio_blk causes spurious section mismatch warning by
  543. * simultaneously referring to a __devinit and a __devexit function.
  544. * Use __refdata to avoid this warning.
  545. */
  546. static struct virtio_driver __refdata virtio_blk = {
  547. .feature_table = features,
  548. .feature_table_size = ARRAY_SIZE(features),
  549. .driver.name = KBUILD_MODNAME,
  550. .driver.owner = THIS_MODULE,
  551. .id_table = id_table,
  552. .probe = virtblk_probe,
  553. .remove = __devexit_p(virtblk_remove),
  554. .config_changed = virtblk_config_changed,
  555. #ifdef CONFIG_PM
  556. .freeze = virtblk_freeze,
  557. .restore = virtblk_restore,
  558. #endif
  559. };
  560. static int __init init(void)
  561. {
  562. int error;
  563. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  564. if (!virtblk_wq)
  565. return -ENOMEM;
  566. major = register_blkdev(0, "virtblk");
  567. if (major < 0) {
  568. error = major;
  569. goto out_destroy_workqueue;
  570. }
  571. error = register_virtio_driver(&virtio_blk);
  572. if (error)
  573. goto out_unregister_blkdev;
  574. return 0;
  575. out_unregister_blkdev:
  576. unregister_blkdev(major, "virtblk");
  577. out_destroy_workqueue:
  578. destroy_workqueue(virtblk_wq);
  579. return error;
  580. }
  581. static void __exit fini(void)
  582. {
  583. unregister_blkdev(major, "virtblk");
  584. unregister_virtio_driver(&virtio_blk);
  585. destroy_workqueue(virtblk_wq);
  586. }
  587. module_init(init);
  588. module_exit(fini);
  589. MODULE_DEVICE_TABLE(virtio, id_table);
  590. MODULE_DESCRIPTION("Virtio block driver");
  591. MODULE_LICENSE("GPL");