virtio_blk.c 17 KB

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