virtio_blk.c 24 KB

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