virtio_blk.c 22 KB

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