virtio_blk.c 20 KB

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