virtio_blk.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. if (unlikely(virtqueue_is_broken(vq)))
  121. break;
  122. } while (!virtqueue_enable_cb(vq));
  123. spin_unlock_irqrestore(&vblk->vq_lock, flags);
  124. /* In case queue is stopped waiting for more buffers. */
  125. if (req_done)
  126. blk_mq_start_stopped_hw_queues(vblk->disk->queue);
  127. }
  128. static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
  129. {
  130. struct virtio_blk *vblk = hctx->queue->queuedata;
  131. struct virtblk_req *vbr = req->special;
  132. unsigned long flags;
  133. unsigned int num;
  134. const bool last = (req->cmd_flags & REQ_END) != 0;
  135. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  136. vbr->req = req;
  137. if (req->cmd_flags & REQ_FLUSH) {
  138. vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
  139. vbr->out_hdr.sector = 0;
  140. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  141. } else {
  142. switch (req->cmd_type) {
  143. case REQ_TYPE_FS:
  144. vbr->out_hdr.type = 0;
  145. vbr->out_hdr.sector = blk_rq_pos(vbr->req);
  146. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  147. break;
  148. case REQ_TYPE_BLOCK_PC:
  149. vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  150. vbr->out_hdr.sector = 0;
  151. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  152. break;
  153. case REQ_TYPE_SPECIAL:
  154. vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
  155. vbr->out_hdr.sector = 0;
  156. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  157. break;
  158. default:
  159. /* We don't put anything else in the queue. */
  160. BUG();
  161. }
  162. }
  163. num = blk_rq_map_sg(hctx->queue, vbr->req, vbr->sg);
  164. if (num) {
  165. if (rq_data_dir(vbr->req) == WRITE)
  166. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  167. else
  168. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  169. }
  170. spin_lock_irqsave(&vblk->vq_lock, flags);
  171. if (__virtblk_add_req(vblk->vq, vbr, vbr->sg, num) < 0) {
  172. virtqueue_kick(vblk->vq);
  173. spin_unlock_irqrestore(&vblk->vq_lock, flags);
  174. blk_mq_stop_hw_queue(hctx);
  175. return BLK_MQ_RQ_QUEUE_BUSY;
  176. }
  177. if (last)
  178. virtqueue_kick(vblk->vq);
  179. spin_unlock_irqrestore(&vblk->vq_lock, flags);
  180. return BLK_MQ_RQ_QUEUE_OK;
  181. }
  182. /* return id (s/n) string for *disk to *id_str
  183. */
  184. static int virtblk_get_id(struct gendisk *disk, char *id_str)
  185. {
  186. struct virtio_blk *vblk = disk->private_data;
  187. struct request *req;
  188. struct bio *bio;
  189. int err;
  190. bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
  191. GFP_KERNEL);
  192. if (IS_ERR(bio))
  193. return PTR_ERR(bio);
  194. req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
  195. if (IS_ERR(req)) {
  196. bio_put(bio);
  197. return PTR_ERR(req);
  198. }
  199. req->cmd_type = REQ_TYPE_SPECIAL;
  200. err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
  201. blk_put_request(req);
  202. return err;
  203. }
  204. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  205. unsigned int cmd, unsigned long data)
  206. {
  207. struct gendisk *disk = bdev->bd_disk;
  208. struct virtio_blk *vblk = disk->private_data;
  209. /*
  210. * Only allow the generic SCSI ioctls if the host can support it.
  211. */
  212. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  213. return -ENOTTY;
  214. return scsi_cmd_blk_ioctl(bdev, mode, cmd,
  215. (void __user *)data);
  216. }
  217. /* We provide getgeo only to please some old bootloader/partitioning tools */
  218. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  219. {
  220. struct virtio_blk *vblk = bd->bd_disk->private_data;
  221. /* see if the host passed in geometry config */
  222. if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
  223. virtio_cread(vblk->vdev, struct virtio_blk_config,
  224. geometry.cylinders, &geo->cylinders);
  225. virtio_cread(vblk->vdev, struct virtio_blk_config,
  226. geometry.heads, &geo->heads);
  227. virtio_cread(vblk->vdev, struct virtio_blk_config,
  228. geometry.sectors, &geo->sectors);
  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. virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
  280. /* If capacity is too big, truncate with warning. */
  281. if ((sector_t)capacity != capacity) {
  282. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  283. (unsigned long long)capacity);
  284. capacity = (sector_t)-1;
  285. }
  286. size = capacity * queue_logical_block_size(q);
  287. string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  288. string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
  289. dev_notice(&vdev->dev,
  290. "new size: %llu %d-byte logical blocks (%s/%s)\n",
  291. (unsigned long long)capacity,
  292. queue_logical_block_size(q),
  293. cap_str_10, cap_str_2);
  294. set_capacity(vblk->disk, capacity);
  295. revalidate_disk(vblk->disk);
  296. kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
  297. done:
  298. mutex_unlock(&vblk->config_lock);
  299. }
  300. static void virtblk_config_changed(struct virtio_device *vdev)
  301. {
  302. struct virtio_blk *vblk = vdev->priv;
  303. queue_work(virtblk_wq, &vblk->config_work);
  304. }
  305. static int init_vq(struct virtio_blk *vblk)
  306. {
  307. int err = 0;
  308. /* We expect one virtqueue, for output. */
  309. vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
  310. if (IS_ERR(vblk->vq))
  311. err = PTR_ERR(vblk->vq);
  312. return err;
  313. }
  314. /*
  315. * Legacy naming scheme used for virtio devices. We are stuck with it for
  316. * virtio blk but don't ever use it for any new driver.
  317. */
  318. static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
  319. {
  320. const int base = 'z' - 'a' + 1;
  321. char *begin = buf + strlen(prefix);
  322. char *end = buf + buflen;
  323. char *p;
  324. int unit;
  325. p = end - 1;
  326. *p = '\0';
  327. unit = base;
  328. do {
  329. if (p == begin)
  330. return -EINVAL;
  331. *--p = 'a' + (index % unit);
  332. index = (index / unit) - 1;
  333. } while (index >= 0);
  334. memmove(begin, p, end - p);
  335. memcpy(buf, prefix, strlen(prefix));
  336. return 0;
  337. }
  338. static int virtblk_get_cache_mode(struct virtio_device *vdev)
  339. {
  340. u8 writeback;
  341. int err;
  342. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
  343. struct virtio_blk_config, wce,
  344. &writeback);
  345. if (err)
  346. writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
  347. return writeback;
  348. }
  349. static void virtblk_update_cache_mode(struct virtio_device *vdev)
  350. {
  351. u8 writeback = virtblk_get_cache_mode(vdev);
  352. struct virtio_blk *vblk = vdev->priv;
  353. if (writeback)
  354. blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
  355. else
  356. blk_queue_flush(vblk->disk->queue, 0);
  357. revalidate_disk(vblk->disk);
  358. }
  359. static const char *const virtblk_cache_types[] = {
  360. "write through", "write back"
  361. };
  362. static ssize_t
  363. virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
  364. const char *buf, size_t count)
  365. {
  366. struct gendisk *disk = dev_to_disk(dev);
  367. struct virtio_blk *vblk = disk->private_data;
  368. struct virtio_device *vdev = vblk->vdev;
  369. int i;
  370. BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
  371. for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
  372. if (sysfs_streq(buf, virtblk_cache_types[i]))
  373. break;
  374. if (i < 0)
  375. return -EINVAL;
  376. virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
  377. virtblk_update_cache_mode(vdev);
  378. return count;
  379. }
  380. static ssize_t
  381. virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
  382. char *buf)
  383. {
  384. struct gendisk *disk = dev_to_disk(dev);
  385. struct virtio_blk *vblk = disk->private_data;
  386. u8 writeback = virtblk_get_cache_mode(vblk->vdev);
  387. BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
  388. return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
  389. }
  390. static const struct device_attribute dev_attr_cache_type_ro =
  391. __ATTR(cache_type, S_IRUGO,
  392. virtblk_cache_type_show, NULL);
  393. static const struct device_attribute dev_attr_cache_type_rw =
  394. __ATTR(cache_type, S_IRUGO|S_IWUSR,
  395. virtblk_cache_type_show, virtblk_cache_type_store);
  396. static struct blk_mq_ops virtio_mq_ops = {
  397. .queue_rq = virtio_queue_rq,
  398. .map_queue = blk_mq_map_queue,
  399. .alloc_hctx = blk_mq_alloc_single_hw_queue,
  400. .free_hctx = blk_mq_free_single_hw_queue,
  401. };
  402. static struct blk_mq_reg virtio_mq_reg = {
  403. .ops = &virtio_mq_ops,
  404. .nr_hw_queues = 1,
  405. .queue_depth = 64,
  406. .numa_node = NUMA_NO_NODE,
  407. .flags = BLK_MQ_F_SHOULD_MERGE,
  408. };
  409. static void virtblk_init_vbr(void *data, struct blk_mq_hw_ctx *hctx,
  410. struct request *rq, unsigned int nr)
  411. {
  412. struct virtio_blk *vblk = data;
  413. struct virtblk_req *vbr = rq->special;
  414. sg_init_table(vbr->sg, vblk->sg_elems);
  415. }
  416. static int virtblk_probe(struct virtio_device *vdev)
  417. {
  418. struct virtio_blk *vblk;
  419. struct request_queue *q;
  420. int err, index;
  421. u64 cap;
  422. u32 v, blk_size, sg_elems, opt_io_size;
  423. u16 min_io_size;
  424. u8 physical_block_exp, alignment_offset;
  425. err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
  426. GFP_KERNEL);
  427. if (err < 0)
  428. goto out;
  429. index = err;
  430. /* We need to know how many segments before we allocate. */
  431. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
  432. struct virtio_blk_config, seg_max,
  433. &sg_elems);
  434. /* We need at least one SG element, whatever they say. */
  435. if (err || !sg_elems)
  436. sg_elems = 1;
  437. /* We need an extra sg elements at head and tail. */
  438. sg_elems += 2;
  439. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  440. if (!vblk) {
  441. err = -ENOMEM;
  442. goto out_free_index;
  443. }
  444. vblk->vdev = vdev;
  445. vblk->sg_elems = sg_elems;
  446. mutex_init(&vblk->config_lock);
  447. INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
  448. vblk->config_enable = true;
  449. err = init_vq(vblk);
  450. if (err)
  451. goto out_free_vblk;
  452. spin_lock_init(&vblk->vq_lock);
  453. /* FIXME: How many partitions? How long is a piece of string? */
  454. vblk->disk = alloc_disk(1 << PART_BITS);
  455. if (!vblk->disk) {
  456. err = -ENOMEM;
  457. goto out_free_vq;
  458. }
  459. virtio_mq_reg.cmd_size =
  460. sizeof(struct virtblk_req) +
  461. sizeof(struct scatterlist) * sg_elems;
  462. q = vblk->disk->queue = blk_mq_init_queue(&virtio_mq_reg, vblk);
  463. if (!q) {
  464. err = -ENOMEM;
  465. goto out_put_disk;
  466. }
  467. blk_mq_init_commands(q, virtblk_init_vbr, vblk);
  468. q->queuedata = vblk;
  469. virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
  470. vblk->disk->major = major;
  471. vblk->disk->first_minor = index_to_minor(index);
  472. vblk->disk->private_data = vblk;
  473. vblk->disk->fops = &virtblk_fops;
  474. vblk->disk->driverfs_dev = &vdev->dev;
  475. vblk->index = index;
  476. /* configure queue flush support */
  477. virtblk_update_cache_mode(vdev);
  478. /* If disk is read-only in the host, the guest should obey */
  479. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  480. set_disk_ro(vblk->disk, 1);
  481. /* Host must always specify the capacity. */
  482. virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
  483. /* If capacity is too big, truncate with warning. */
  484. if ((sector_t)cap != cap) {
  485. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  486. (unsigned long long)cap);
  487. cap = (sector_t)-1;
  488. }
  489. set_capacity(vblk->disk, cap);
  490. /* We can handle whatever the host told us to handle. */
  491. blk_queue_max_segments(q, vblk->sg_elems-2);
  492. /* No need to bounce any requests */
  493. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  494. /* No real sector limit. */
  495. blk_queue_max_hw_sectors(q, -1U);
  496. /* Host can optionally specify maximum segment size and number of
  497. * segments. */
  498. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
  499. struct virtio_blk_config, size_max, &v);
  500. if (!err)
  501. blk_queue_max_segment_size(q, v);
  502. else
  503. blk_queue_max_segment_size(q, -1U);
  504. /* Host can optionally specify the block size of the device */
  505. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
  506. struct virtio_blk_config, blk_size,
  507. &blk_size);
  508. if (!err)
  509. blk_queue_logical_block_size(q, blk_size);
  510. else
  511. blk_size = queue_logical_block_size(q);
  512. /* Use topology information if available */
  513. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  514. struct virtio_blk_config, physical_block_exp,
  515. &physical_block_exp);
  516. if (!err && physical_block_exp)
  517. blk_queue_physical_block_size(q,
  518. blk_size * (1 << physical_block_exp));
  519. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  520. struct virtio_blk_config, alignment_offset,
  521. &alignment_offset);
  522. if (!err && alignment_offset)
  523. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  524. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  525. struct virtio_blk_config, min_io_size,
  526. &min_io_size);
  527. if (!err && min_io_size)
  528. blk_queue_io_min(q, blk_size * min_io_size);
  529. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  530. struct virtio_blk_config, opt_io_size,
  531. &opt_io_size);
  532. if (!err && opt_io_size)
  533. blk_queue_io_opt(q, blk_size * opt_io_size);
  534. add_disk(vblk->disk);
  535. err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
  536. if (err)
  537. goto out_del_disk;
  538. if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
  539. err = device_create_file(disk_to_dev(vblk->disk),
  540. &dev_attr_cache_type_rw);
  541. else
  542. err = device_create_file(disk_to_dev(vblk->disk),
  543. &dev_attr_cache_type_ro);
  544. if (err)
  545. goto out_del_disk;
  546. return 0;
  547. out_del_disk:
  548. del_gendisk(vblk->disk);
  549. blk_cleanup_queue(vblk->disk->queue);
  550. out_put_disk:
  551. put_disk(vblk->disk);
  552. out_free_vq:
  553. vdev->config->del_vqs(vdev);
  554. out_free_vblk:
  555. kfree(vblk);
  556. out_free_index:
  557. ida_simple_remove(&vd_index_ida, index);
  558. out:
  559. return err;
  560. }
  561. static void virtblk_remove(struct virtio_device *vdev)
  562. {
  563. struct virtio_blk *vblk = vdev->priv;
  564. int index = vblk->index;
  565. int refc;
  566. /* Prevent config work handler from accessing the device. */
  567. mutex_lock(&vblk->config_lock);
  568. vblk->config_enable = false;
  569. mutex_unlock(&vblk->config_lock);
  570. del_gendisk(vblk->disk);
  571. blk_cleanup_queue(vblk->disk->queue);
  572. /* Stop all the virtqueues. */
  573. vdev->config->reset(vdev);
  574. flush_work(&vblk->config_work);
  575. refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
  576. put_disk(vblk->disk);
  577. vdev->config->del_vqs(vdev);
  578. kfree(vblk);
  579. /* Only free device id if we don't have any users */
  580. if (refc == 1)
  581. ida_simple_remove(&vd_index_ida, index);
  582. }
  583. #ifdef CONFIG_PM_SLEEP
  584. static int virtblk_freeze(struct virtio_device *vdev)
  585. {
  586. struct virtio_blk *vblk = vdev->priv;
  587. /* Ensure we don't receive any more interrupts */
  588. vdev->config->reset(vdev);
  589. /* Prevent config work handler from accessing the device. */
  590. mutex_lock(&vblk->config_lock);
  591. vblk->config_enable = false;
  592. mutex_unlock(&vblk->config_lock);
  593. flush_work(&vblk->config_work);
  594. blk_mq_stop_hw_queues(vblk->disk->queue);
  595. vdev->config->del_vqs(vdev);
  596. return 0;
  597. }
  598. static int virtblk_restore(struct virtio_device *vdev)
  599. {
  600. struct virtio_blk *vblk = vdev->priv;
  601. int ret;
  602. vblk->config_enable = true;
  603. ret = init_vq(vdev->priv);
  604. if (!ret)
  605. blk_mq_start_stopped_hw_queues(vblk->disk->queue);
  606. return ret;
  607. }
  608. #endif
  609. static const struct virtio_device_id id_table[] = {
  610. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  611. { 0 },
  612. };
  613. static unsigned int features[] = {
  614. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  615. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
  616. VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
  617. };
  618. static struct virtio_driver virtio_blk = {
  619. .feature_table = features,
  620. .feature_table_size = ARRAY_SIZE(features),
  621. .driver.name = KBUILD_MODNAME,
  622. .driver.owner = THIS_MODULE,
  623. .id_table = id_table,
  624. .probe = virtblk_probe,
  625. .remove = virtblk_remove,
  626. .config_changed = virtblk_config_changed,
  627. #ifdef CONFIG_PM_SLEEP
  628. .freeze = virtblk_freeze,
  629. .restore = virtblk_restore,
  630. #endif
  631. };
  632. static int __init init(void)
  633. {
  634. int error;
  635. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  636. if (!virtblk_wq)
  637. return -ENOMEM;
  638. major = register_blkdev(0, "virtblk");
  639. if (major < 0) {
  640. error = major;
  641. goto out_destroy_workqueue;
  642. }
  643. error = register_virtio_driver(&virtio_blk);
  644. if (error)
  645. goto out_unregister_blkdev;
  646. return 0;
  647. out_unregister_blkdev:
  648. unregister_blkdev(major, "virtblk");
  649. out_destroy_workqueue:
  650. destroy_workqueue(virtblk_wq);
  651. return error;
  652. }
  653. static void __exit fini(void)
  654. {
  655. unregister_blkdev(major, "virtblk");
  656. unregister_virtio_driver(&virtio_blk);
  657. destroy_workqueue(virtblk_wq);
  658. }
  659. module_init(init);
  660. module_exit(fini);
  661. MODULE_DEVICE_TABLE(virtio, id_table);
  662. MODULE_DESCRIPTION("Virtio block driver");
  663. MODULE_LICENSE("GPL");