virtio_blk.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/spinlock.h>
  6. #include <linux/slab.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/hdreg.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/virtio.h>
  13. #include <linux/virtio_blk.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/string_helpers.h>
  16. #include <linux/idr.h>
  17. #include <linux/blk-mq.h>
  18. #include <linux/blk-mq-virtio.h>
  19. #include <linux/numa.h>
  20. #include <uapi/linux/virtio_ring.h>
  21. #ifdef CONFIG_GH_VIRTIO_DEBUG
  22. #include <trace/events/gh_virtio_frontend.h>
  23. #endif
  24. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  25. #include <linux/blk-crypto-profile.h>
  26. #include "virtio_blk_qti_crypto.h"
  27. #endif
  28. #define PART_BITS 4
  29. #define VQ_NAME_LEN 16
  30. #define MAX_DISCARD_SEGMENTS 256u
  31. /* The maximum number of sg elements that fit into a virtqueue */
  32. #define VIRTIO_BLK_MAX_SG_ELEMS 32768
  33. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  34. /* ICE feature bits needs to be moved to uapi headers.*/
  35. /* support ice virtualization */
  36. #define VIRTIO_BLK_F_ICE 23
  37. /* support ice virtualization with iv (initialization vector) */
  38. #define VIRTIO_BLK_F_ICE_IV 22
  39. #endif
  40. #ifdef CONFIG_ARCH_NO_SG_CHAIN
  41. #define VIRTIO_BLK_INLINE_SG_CNT 0
  42. #else
  43. #define VIRTIO_BLK_INLINE_SG_CNT 2
  44. #endif
  45. static unsigned int num_request_queues;
  46. module_param(num_request_queues, uint, 0644);
  47. MODULE_PARM_DESC(num_request_queues,
  48. "Limit the number of request queues to use for blk device. "
  49. "0 for no limit. "
  50. "Values > nr_cpu_ids truncated to nr_cpu_ids.");
  51. static unsigned int poll_queues;
  52. module_param(poll_queues, uint, 0644);
  53. MODULE_PARM_DESC(poll_queues, "The number of dedicated virtqueues for polling I/O");
  54. static int major;
  55. static DEFINE_IDA(vd_index_ida);
  56. static struct workqueue_struct *virtblk_wq;
  57. struct virtio_blk_vq {
  58. struct virtqueue *vq;
  59. spinlock_t lock;
  60. char name[VQ_NAME_LEN];
  61. } ____cacheline_aligned_in_smp;
  62. struct virtio_blk {
  63. /*
  64. * This mutex must be held by anything that may run after
  65. * virtblk_remove() sets vblk->vdev to NULL.
  66. *
  67. * blk-mq, virtqueue processing, and sysfs attribute code paths are
  68. * shut down before vblk->vdev is set to NULL and therefore do not need
  69. * to hold this mutex.
  70. */
  71. struct mutex vdev_mutex;
  72. struct virtio_device *vdev;
  73. /* The disk structure for the kernel. */
  74. struct gendisk *disk;
  75. /* Block layer tags. */
  76. struct blk_mq_tag_set tag_set;
  77. /* Process context for config space updates */
  78. struct work_struct config_work;
  79. /* Ida index - used to track minor number allocations. */
  80. int index;
  81. /* num of vqs */
  82. int num_vqs;
  83. int io_queues[HCTX_MAX_TYPES];
  84. struct virtio_blk_vq *vqs;
  85. };
  86. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  87. struct virtio_blk_ice_info {
  88. /*the key slot to use for inline crypto*/
  89. u8 ice_slot;
  90. u8 activate;
  91. u16 reserved;
  92. u32 reserved1;
  93. u64 data_unit_num;
  94. } __packed;
  95. #endif
  96. struct virtblk_req {
  97. struct virtio_blk_outhdr out_hdr;
  98. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  99. struct virtio_blk_ice_info ice_info;
  100. #endif
  101. u8 status;
  102. struct sg_table sg_table;
  103. struct scatterlist sg[];
  104. };
  105. static inline blk_status_t virtblk_result(struct virtblk_req *vbr)
  106. {
  107. switch (vbr->status) {
  108. case VIRTIO_BLK_S_OK:
  109. return BLK_STS_OK;
  110. case VIRTIO_BLK_S_UNSUPP:
  111. return BLK_STS_NOTSUPP;
  112. default:
  113. return BLK_STS_IOERR;
  114. }
  115. }
  116. static inline struct virtio_blk_vq *get_virtio_blk_vq(struct blk_mq_hw_ctx *hctx)
  117. {
  118. struct virtio_blk *vblk = hctx->queue->queuedata;
  119. struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
  120. return vq;
  121. }
  122. static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr)
  123. {
  124. struct scatterlist hdr, status, *sgs[3];
  125. unsigned int num_out = 0, num_in = 0;
  126. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  127. /* Backend (HOST) expects to receive encryption info via extended
  128. * structure when ICE negotiation is successful which will be used
  129. * by backend ufs/sdhci host controller to program the descriptors
  130. * as per spec standards to enable the encryption on read/write
  131. * of data from/to disk.
  132. */
  133. size_t const hdr_size = virtio_has_feature(vq->vdev, VIRTIO_BLK_F_ICE_IV) ?
  134. sizeof(vbr->out_hdr) + sizeof(vbr->ice_info) :
  135. sizeof(vbr->out_hdr);
  136. sg_init_one(&hdr, &vbr->out_hdr, hdr_size);
  137. #else
  138. sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
  139. #endif
  140. sgs[num_out++] = &hdr;
  141. if (vbr->sg_table.nents) {
  142. if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
  143. sgs[num_out++] = vbr->sg_table.sgl;
  144. else
  145. sgs[num_out + num_in++] = vbr->sg_table.sgl;
  146. }
  147. sg_init_one(&status, &vbr->status, sizeof(vbr->status));
  148. sgs[num_out + num_in++] = &status;
  149. return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
  150. }
  151. static int virtblk_setup_discard_write_zeroes_erase(struct request *req, bool unmap)
  152. {
  153. unsigned short segments = blk_rq_nr_discard_segments(req);
  154. unsigned short n = 0;
  155. struct virtio_blk_discard_write_zeroes *range;
  156. struct bio *bio;
  157. u32 flags = 0;
  158. if (unmap)
  159. flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
  160. range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
  161. if (!range)
  162. return -ENOMEM;
  163. /*
  164. * Single max discard segment means multi-range discard isn't
  165. * supported, and block layer only runs contiguity merge like
  166. * normal RW request. So we can't reply on bio for retrieving
  167. * each range info.
  168. */
  169. if (queue_max_discard_segments(req->q) == 1) {
  170. range[0].flags = cpu_to_le32(flags);
  171. range[0].num_sectors = cpu_to_le32(blk_rq_sectors(req));
  172. range[0].sector = cpu_to_le64(blk_rq_pos(req));
  173. n = 1;
  174. } else {
  175. __rq_for_each_bio(bio, req) {
  176. u64 sector = bio->bi_iter.bi_sector;
  177. u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
  178. range[n].flags = cpu_to_le32(flags);
  179. range[n].num_sectors = cpu_to_le32(num_sectors);
  180. range[n].sector = cpu_to_le64(sector);
  181. n++;
  182. }
  183. }
  184. WARN_ON_ONCE(n != segments);
  185. req->special_vec.bv_page = virt_to_page(range);
  186. req->special_vec.bv_offset = offset_in_page(range);
  187. req->special_vec.bv_len = sizeof(*range) * segments;
  188. req->rq_flags |= RQF_SPECIAL_PAYLOAD;
  189. return 0;
  190. }
  191. static void virtblk_unmap_data(struct request *req, struct virtblk_req *vbr)
  192. {
  193. if (blk_rq_nr_phys_segments(req))
  194. sg_free_table_chained(&vbr->sg_table,
  195. VIRTIO_BLK_INLINE_SG_CNT);
  196. }
  197. static int virtblk_map_data(struct blk_mq_hw_ctx *hctx, struct request *req,
  198. struct virtblk_req *vbr)
  199. {
  200. int err;
  201. if (!blk_rq_nr_phys_segments(req))
  202. return 0;
  203. vbr->sg_table.sgl = vbr->sg;
  204. err = sg_alloc_table_chained(&vbr->sg_table,
  205. blk_rq_nr_phys_segments(req),
  206. vbr->sg_table.sgl,
  207. VIRTIO_BLK_INLINE_SG_CNT);
  208. if (unlikely(err))
  209. return -ENOMEM;
  210. return blk_rq_map_sg(hctx->queue, req, vbr->sg_table.sgl);
  211. }
  212. static void virtblk_cleanup_cmd(struct request *req)
  213. {
  214. if (req->rq_flags & RQF_SPECIAL_PAYLOAD)
  215. kfree(bvec_virt(&req->special_vec));
  216. }
  217. static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
  218. struct request *req,
  219. struct virtblk_req *vbr)
  220. {
  221. bool unmap = false;
  222. u32 type;
  223. vbr->out_hdr.sector = 0;
  224. switch (req_op(req)) {
  225. case REQ_OP_READ:
  226. type = VIRTIO_BLK_T_IN;
  227. vbr->out_hdr.sector = cpu_to_virtio64(vdev,
  228. blk_rq_pos(req));
  229. break;
  230. case REQ_OP_WRITE:
  231. type = VIRTIO_BLK_T_OUT;
  232. vbr->out_hdr.sector = cpu_to_virtio64(vdev,
  233. blk_rq_pos(req));
  234. break;
  235. case REQ_OP_FLUSH:
  236. type = VIRTIO_BLK_T_FLUSH;
  237. break;
  238. case REQ_OP_DISCARD:
  239. type = VIRTIO_BLK_T_DISCARD;
  240. break;
  241. case REQ_OP_WRITE_ZEROES:
  242. type = VIRTIO_BLK_T_WRITE_ZEROES;
  243. unmap = !(req->cmd_flags & REQ_NOUNMAP);
  244. break;
  245. case REQ_OP_SECURE_ERASE:
  246. type = VIRTIO_BLK_T_SECURE_ERASE;
  247. break;
  248. case REQ_OP_DRV_IN:
  249. type = VIRTIO_BLK_T_GET_ID;
  250. break;
  251. default:
  252. WARN_ON_ONCE(1);
  253. return BLK_STS_IOERR;
  254. }
  255. vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
  256. vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
  257. if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES ||
  258. type == VIRTIO_BLK_T_SECURE_ERASE) {
  259. if (virtblk_setup_discard_write_zeroes_erase(req, unmap))
  260. return BLK_STS_RESOURCE;
  261. }
  262. return 0;
  263. }
  264. static inline void virtblk_request_done(struct request *req)
  265. {
  266. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  267. virtblk_unmap_data(req, vbr);
  268. virtblk_cleanup_cmd(req);
  269. blk_mq_end_request(req, virtblk_result(vbr));
  270. }
  271. static void virtblk_done(struct virtqueue *vq)
  272. {
  273. struct virtio_blk *vblk = vq->vdev->priv;
  274. bool req_done = false;
  275. int qid = vq->index;
  276. struct virtblk_req *vbr;
  277. unsigned long flags;
  278. unsigned int len;
  279. spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
  280. do {
  281. virtqueue_disable_cb(vq);
  282. while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
  283. struct request *req = blk_mq_rq_from_pdu(vbr);
  284. if (likely(!blk_should_fake_timeout(req->q)))
  285. blk_mq_complete_request(req);
  286. #ifdef CONFIG_GH_VIRTIO_DEBUG
  287. trace_virtio_block_done(vq->vdev->index, req_op(req), blk_rq_pos(req));
  288. #endif
  289. req_done = true;
  290. }
  291. if (unlikely(virtqueue_is_broken(vq)))
  292. break;
  293. } while (!virtqueue_enable_cb(vq));
  294. /* In case queue is stopped waiting for more buffers. */
  295. if (req_done)
  296. blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
  297. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  298. }
  299. static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
  300. {
  301. struct virtio_blk *vblk = hctx->queue->queuedata;
  302. struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
  303. bool kick;
  304. spin_lock_irq(&vq->lock);
  305. kick = virtqueue_kick_prepare(vq->vq);
  306. spin_unlock_irq(&vq->lock);
  307. if (kick)
  308. virtqueue_notify(vq->vq);
  309. }
  310. static blk_status_t virtblk_fail_to_queue(struct request *req, int rc)
  311. {
  312. virtblk_cleanup_cmd(req);
  313. switch (rc) {
  314. case -ENOSPC:
  315. return BLK_STS_DEV_RESOURCE;
  316. case -ENOMEM:
  317. return BLK_STS_RESOURCE;
  318. default:
  319. return BLK_STS_IOERR;
  320. }
  321. }
  322. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  323. static void virtblk_get_ice_info(struct request *req)
  324. {
  325. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  326. /* whether or not the request needs inline crypto operations*/
  327. if (!req || !req->crypt_keyslot) {
  328. /* ice is not activated */
  329. vbr->ice_info.activate = false;
  330. } else {
  331. vbr->ice_info.ice_slot = blk_crypto_keyslot_index(req->crypt_keyslot);
  332. /* ice is activated - successful flow */
  333. vbr->ice_info.activate = true;
  334. /* data unit number i.e. iv value */
  335. vbr->ice_info.data_unit_num = req->crypt_ctx->bc_dun[0];
  336. }
  337. }
  338. #endif
  339. static blk_status_t virtblk_prep_rq(struct blk_mq_hw_ctx *hctx,
  340. struct virtio_blk *vblk,
  341. struct request *req,
  342. struct virtblk_req *vbr)
  343. {
  344. blk_status_t status;
  345. int num;
  346. status = virtblk_setup_cmd(vblk->vdev, req, vbr);
  347. if (unlikely(status))
  348. return status;
  349. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  350. if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_ICE_IV))
  351. virtblk_get_ice_info(req);
  352. #endif
  353. num = virtblk_map_data(hctx, req, vbr);
  354. if (unlikely(num < 0))
  355. return virtblk_fail_to_queue(req, -ENOMEM);
  356. vbr->sg_table.nents = num;
  357. blk_mq_start_request(req);
  358. return BLK_STS_OK;
  359. }
  360. static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
  361. const struct blk_mq_queue_data *bd)
  362. {
  363. struct virtio_blk *vblk = hctx->queue->queuedata;
  364. struct request *req = bd->rq;
  365. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  366. unsigned long flags;
  367. int qid = hctx->queue_num;
  368. bool notify = false;
  369. blk_status_t status;
  370. int err;
  371. status = virtblk_prep_rq(hctx, vblk, req, vbr);
  372. if (unlikely(status))
  373. return status;
  374. spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
  375. err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
  376. #ifdef CONFIG_GH_VIRTIO_DEBUG
  377. trace_virtio_block_submit(vblk->vqs[qid].vq->vdev->index,
  378. vbr->out_hdr.type, vbr->out_hdr.sector, vbr->out_hdr.ioprio, err);
  379. #endif
  380. if (err) {
  381. virtqueue_kick(vblk->vqs[qid].vq);
  382. /* Don't stop the queue if -ENOMEM: we may have failed to
  383. * bounce the buffer due to global resource outage.
  384. */
  385. if (err == -ENOSPC)
  386. blk_mq_stop_hw_queue(hctx);
  387. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  388. virtblk_unmap_data(req, vbr);
  389. return virtblk_fail_to_queue(req, err);
  390. }
  391. if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
  392. notify = true;
  393. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  394. if (notify)
  395. virtqueue_notify(vblk->vqs[qid].vq);
  396. return BLK_STS_OK;
  397. }
  398. static bool virtblk_prep_rq_batch(struct request *req)
  399. {
  400. struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
  401. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  402. req->mq_hctx->tags->rqs[req->tag] = req;
  403. return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
  404. }
  405. static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
  406. struct request **rqlist)
  407. {
  408. unsigned long flags;
  409. int err;
  410. bool kick;
  411. spin_lock_irqsave(&vq->lock, flags);
  412. while (!rq_list_empty(*rqlist)) {
  413. struct request *req = rq_list_pop(rqlist);
  414. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  415. err = virtblk_add_req(vq->vq, vbr);
  416. if (err) {
  417. virtblk_unmap_data(req, vbr);
  418. virtblk_cleanup_cmd(req);
  419. blk_mq_requeue_request(req, true);
  420. }
  421. }
  422. kick = virtqueue_kick_prepare(vq->vq);
  423. spin_unlock_irqrestore(&vq->lock, flags);
  424. return kick;
  425. }
  426. static void virtio_queue_rqs(struct request **rqlist)
  427. {
  428. struct request *req, *next, *prev = NULL;
  429. struct request *requeue_list = NULL;
  430. rq_list_for_each_safe(rqlist, req, next) {
  431. struct virtio_blk_vq *vq = get_virtio_blk_vq(req->mq_hctx);
  432. bool kick;
  433. if (!virtblk_prep_rq_batch(req)) {
  434. rq_list_move(rqlist, &requeue_list, req, prev);
  435. req = prev;
  436. if (!req)
  437. continue;
  438. }
  439. if (!next || req->mq_hctx != next->mq_hctx) {
  440. req->rq_next = NULL;
  441. kick = virtblk_add_req_batch(vq, rqlist);
  442. if (kick)
  443. virtqueue_notify(vq->vq);
  444. *rqlist = next;
  445. prev = NULL;
  446. } else
  447. prev = req;
  448. }
  449. *rqlist = requeue_list;
  450. }
  451. /* return id (s/n) string for *disk to *id_str
  452. */
  453. static int virtblk_get_id(struct gendisk *disk, char *id_str)
  454. {
  455. struct virtio_blk *vblk = disk->private_data;
  456. struct request_queue *q = vblk->disk->queue;
  457. struct request *req;
  458. int err;
  459. req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
  460. if (IS_ERR(req))
  461. return PTR_ERR(req);
  462. err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
  463. if (err)
  464. goto out;
  465. blk_execute_rq(req, false);
  466. err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
  467. out:
  468. blk_mq_free_request(req);
  469. return err;
  470. }
  471. /* We provide getgeo only to please some old bootloader/partitioning tools */
  472. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  473. {
  474. struct virtio_blk *vblk = bd->bd_disk->private_data;
  475. int ret = 0;
  476. mutex_lock(&vblk->vdev_mutex);
  477. if (!vblk->vdev) {
  478. ret = -ENXIO;
  479. goto out;
  480. }
  481. /* see if the host passed in geometry config */
  482. if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
  483. virtio_cread(vblk->vdev, struct virtio_blk_config,
  484. geometry.cylinders, &geo->cylinders);
  485. virtio_cread(vblk->vdev, struct virtio_blk_config,
  486. geometry.heads, &geo->heads);
  487. virtio_cread(vblk->vdev, struct virtio_blk_config,
  488. geometry.sectors, &geo->sectors);
  489. } else {
  490. /* some standard values, similar to sd */
  491. geo->heads = 1 << 6;
  492. geo->sectors = 1 << 5;
  493. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  494. }
  495. out:
  496. mutex_unlock(&vblk->vdev_mutex);
  497. return ret;
  498. }
  499. static void virtblk_free_disk(struct gendisk *disk)
  500. {
  501. struct virtio_blk *vblk = disk->private_data;
  502. ida_simple_remove(&vd_index_ida, vblk->index);
  503. mutex_destroy(&vblk->vdev_mutex);
  504. kfree(vblk);
  505. }
  506. static const struct block_device_operations virtblk_fops = {
  507. .owner = THIS_MODULE,
  508. .getgeo = virtblk_getgeo,
  509. .free_disk = virtblk_free_disk,
  510. };
  511. static int index_to_minor(int index)
  512. {
  513. return index << PART_BITS;
  514. }
  515. static int minor_to_index(int minor)
  516. {
  517. return minor >> PART_BITS;
  518. }
  519. static ssize_t serial_show(struct device *dev,
  520. struct device_attribute *attr, char *buf)
  521. {
  522. struct gendisk *disk = dev_to_disk(dev);
  523. int err;
  524. /* sysfs gives us a PAGE_SIZE buffer */
  525. BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
  526. buf[VIRTIO_BLK_ID_BYTES] = '\0';
  527. err = virtblk_get_id(disk, buf);
  528. if (!err)
  529. return strlen(buf);
  530. if (err == -EIO) /* Unsupported? Make it empty. */
  531. return 0;
  532. return err;
  533. }
  534. static DEVICE_ATTR_RO(serial);
  535. /* The queue's logical block size must be set before calling this */
  536. static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
  537. {
  538. struct virtio_device *vdev = vblk->vdev;
  539. struct request_queue *q = vblk->disk->queue;
  540. char cap_str_2[10], cap_str_10[10];
  541. unsigned long long nblocks;
  542. u64 capacity;
  543. /* Host must always specify the capacity. */
  544. virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
  545. nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
  546. string_get_size(nblocks, queue_logical_block_size(q),
  547. STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  548. string_get_size(nblocks, queue_logical_block_size(q),
  549. STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
  550. dev_notice(&vdev->dev,
  551. "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
  552. vblk->disk->disk_name,
  553. resize ? "new size: " : "",
  554. nblocks,
  555. queue_logical_block_size(q),
  556. cap_str_10,
  557. cap_str_2);
  558. set_capacity_and_notify(vblk->disk, capacity);
  559. }
  560. static void virtblk_config_changed_work(struct work_struct *work)
  561. {
  562. struct virtio_blk *vblk =
  563. container_of(work, struct virtio_blk, config_work);
  564. virtblk_update_capacity(vblk, true);
  565. }
  566. static void virtblk_config_changed(struct virtio_device *vdev)
  567. {
  568. struct virtio_blk *vblk = vdev->priv;
  569. queue_work(virtblk_wq, &vblk->config_work);
  570. }
  571. static int init_vq(struct virtio_blk *vblk)
  572. {
  573. int err;
  574. int i;
  575. vq_callback_t **callbacks;
  576. const char **names;
  577. struct virtqueue **vqs;
  578. unsigned short num_vqs;
  579. unsigned int num_poll_vqs;
  580. struct virtio_device *vdev = vblk->vdev;
  581. struct irq_affinity desc = { 0, };
  582. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
  583. struct virtio_blk_config, num_queues,
  584. &num_vqs);
  585. if (err)
  586. num_vqs = 1;
  587. if (!err && !num_vqs) {
  588. dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
  589. return -EINVAL;
  590. }
  591. num_vqs = min_t(unsigned int,
  592. min_not_zero(num_request_queues, nr_cpu_ids),
  593. num_vqs);
  594. num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
  595. vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
  596. vblk->io_queues[HCTX_TYPE_READ] = 0;
  597. vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
  598. dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
  599. vblk->io_queues[HCTX_TYPE_DEFAULT],
  600. vblk->io_queues[HCTX_TYPE_READ],
  601. vblk->io_queues[HCTX_TYPE_POLL]);
  602. vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
  603. if (!vblk->vqs)
  604. return -ENOMEM;
  605. names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
  606. callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
  607. vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
  608. if (!names || !callbacks || !vqs) {
  609. err = -ENOMEM;
  610. goto out;
  611. }
  612. for (i = 0; i < num_vqs - num_poll_vqs; i++) {
  613. callbacks[i] = virtblk_done;
  614. snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
  615. names[i] = vblk->vqs[i].name;
  616. }
  617. for (; i < num_vqs; i++) {
  618. callbacks[i] = NULL;
  619. snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
  620. names[i] = vblk->vqs[i].name;
  621. }
  622. /* Discover virtqueues and write information to configuration. */
  623. err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
  624. if (err)
  625. goto out;
  626. for (i = 0; i < num_vqs; i++) {
  627. spin_lock_init(&vblk->vqs[i].lock);
  628. vblk->vqs[i].vq = vqs[i];
  629. }
  630. vblk->num_vqs = num_vqs;
  631. out:
  632. kfree(vqs);
  633. kfree(callbacks);
  634. kfree(names);
  635. if (err)
  636. kfree(vblk->vqs);
  637. return err;
  638. }
  639. /*
  640. * Legacy naming scheme used for virtio devices. We are stuck with it for
  641. * virtio blk but don't ever use it for any new driver.
  642. */
  643. static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
  644. {
  645. const int base = 'z' - 'a' + 1;
  646. char *begin = buf + strlen(prefix);
  647. char *end = buf + buflen;
  648. char *p;
  649. int unit;
  650. p = end - 1;
  651. *p = '\0';
  652. unit = base;
  653. do {
  654. if (p == begin)
  655. return -EINVAL;
  656. *--p = 'a' + (index % unit);
  657. index = (index / unit) - 1;
  658. } while (index >= 0);
  659. memmove(begin, p, end - p);
  660. memcpy(buf, prefix, strlen(prefix));
  661. return 0;
  662. }
  663. static int virtblk_get_cache_mode(struct virtio_device *vdev)
  664. {
  665. u8 writeback;
  666. int err;
  667. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
  668. struct virtio_blk_config, wce,
  669. &writeback);
  670. /*
  671. * If WCE is not configurable and flush is not available,
  672. * assume no writeback cache is in use.
  673. */
  674. if (err)
  675. writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
  676. return writeback;
  677. }
  678. static void virtblk_update_cache_mode(struct virtio_device *vdev)
  679. {
  680. u8 writeback = virtblk_get_cache_mode(vdev);
  681. struct virtio_blk *vblk = vdev->priv;
  682. blk_queue_write_cache(vblk->disk->queue, writeback, false);
  683. }
  684. static const char *const virtblk_cache_types[] = {
  685. "write through", "write back"
  686. };
  687. static ssize_t
  688. cache_type_store(struct device *dev, struct device_attribute *attr,
  689. const char *buf, size_t count)
  690. {
  691. struct gendisk *disk = dev_to_disk(dev);
  692. struct virtio_blk *vblk = disk->private_data;
  693. struct virtio_device *vdev = vblk->vdev;
  694. int i;
  695. BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
  696. i = sysfs_match_string(virtblk_cache_types, buf);
  697. if (i < 0)
  698. return i;
  699. virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
  700. virtblk_update_cache_mode(vdev);
  701. return count;
  702. }
  703. static ssize_t
  704. cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  705. {
  706. struct gendisk *disk = dev_to_disk(dev);
  707. struct virtio_blk *vblk = disk->private_data;
  708. u8 writeback = virtblk_get_cache_mode(vblk->vdev);
  709. BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
  710. return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
  711. }
  712. static DEVICE_ATTR_RW(cache_type);
  713. static struct attribute *virtblk_attrs[] = {
  714. &dev_attr_serial.attr,
  715. &dev_attr_cache_type.attr,
  716. NULL,
  717. };
  718. static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
  719. struct attribute *a, int n)
  720. {
  721. struct device *dev = kobj_to_dev(kobj);
  722. struct gendisk *disk = dev_to_disk(dev);
  723. struct virtio_blk *vblk = disk->private_data;
  724. struct virtio_device *vdev = vblk->vdev;
  725. if (a == &dev_attr_cache_type.attr &&
  726. !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
  727. return S_IRUGO;
  728. return a->mode;
  729. }
  730. static const struct attribute_group virtblk_attr_group = {
  731. .attrs = virtblk_attrs,
  732. .is_visible = virtblk_attrs_are_visible,
  733. };
  734. static const struct attribute_group *virtblk_attr_groups[] = {
  735. &virtblk_attr_group,
  736. NULL,
  737. };
  738. static void virtblk_map_queues(struct blk_mq_tag_set *set)
  739. {
  740. struct virtio_blk *vblk = set->driver_data;
  741. int i, qoff;
  742. for (i = 0, qoff = 0; i < set->nr_maps; i++) {
  743. struct blk_mq_queue_map *map = &set->map[i];
  744. map->nr_queues = vblk->io_queues[i];
  745. map->queue_offset = qoff;
  746. qoff += map->nr_queues;
  747. if (map->nr_queues == 0)
  748. continue;
  749. /*
  750. * Regular queues have interrupts and hence CPU affinity is
  751. * defined by the core virtio code, but polling queues have
  752. * no interrupts so we let the block layer assign CPU affinity.
  753. */
  754. if (i == HCTX_TYPE_POLL)
  755. blk_mq_map_queues(&set->map[i]);
  756. else
  757. blk_mq_virtio_map_queues(&set->map[i], vblk->vdev, 0);
  758. }
  759. }
  760. static void virtblk_complete_batch(struct io_comp_batch *iob)
  761. {
  762. struct request *req;
  763. rq_list_for_each(&iob->req_list, req) {
  764. virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
  765. virtblk_cleanup_cmd(req);
  766. }
  767. blk_mq_end_request_batch(iob);
  768. }
  769. static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
  770. {
  771. struct virtio_blk *vblk = hctx->queue->queuedata;
  772. struct virtio_blk_vq *vq = get_virtio_blk_vq(hctx);
  773. struct virtblk_req *vbr;
  774. unsigned long flags;
  775. unsigned int len;
  776. int found = 0;
  777. spin_lock_irqsave(&vq->lock, flags);
  778. while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
  779. struct request *req = blk_mq_rq_from_pdu(vbr);
  780. found++;
  781. if (!blk_mq_add_to_batch(req, iob, vbr->status,
  782. virtblk_complete_batch))
  783. blk_mq_complete_request(req);
  784. }
  785. if (found)
  786. blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
  787. spin_unlock_irqrestore(&vq->lock, flags);
  788. return found;
  789. }
  790. static const struct blk_mq_ops virtio_mq_ops = {
  791. .queue_rq = virtio_queue_rq,
  792. .queue_rqs = virtio_queue_rqs,
  793. .commit_rqs = virtio_commit_rqs,
  794. .complete = virtblk_request_done,
  795. .map_queues = virtblk_map_queues,
  796. .poll = virtblk_poll,
  797. };
  798. static unsigned int virtblk_queue_depth;
  799. module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
  800. static int virtblk_probe(struct virtio_device *vdev)
  801. {
  802. struct virtio_blk *vblk;
  803. struct request_queue *q;
  804. int err, index;
  805. u32 v, blk_size, max_size, sg_elems, opt_io_size;
  806. u32 max_discard_segs = 0;
  807. u32 discard_granularity = 0;
  808. u16 min_io_size;
  809. u8 physical_block_exp, alignment_offset;
  810. unsigned int queue_depth;
  811. size_t max_dma_size;
  812. if (!vdev->config->get) {
  813. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  814. __func__);
  815. return -EINVAL;
  816. }
  817. err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
  818. GFP_KERNEL);
  819. if (err < 0)
  820. goto out;
  821. index = err;
  822. /* We need to know how many segments before we allocate. */
  823. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
  824. struct virtio_blk_config, seg_max,
  825. &sg_elems);
  826. /* We need at least one SG element, whatever they say. */
  827. if (err || !sg_elems)
  828. sg_elems = 1;
  829. /* Prevent integer overflows and honor max vq size */
  830. sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
  831. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  832. if (!vblk) {
  833. err = -ENOMEM;
  834. goto out_free_index;
  835. }
  836. mutex_init(&vblk->vdev_mutex);
  837. vblk->vdev = vdev;
  838. INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
  839. err = init_vq(vblk);
  840. if (err)
  841. goto out_free_vblk;
  842. /* Default queue sizing is to fill the ring. */
  843. if (!virtblk_queue_depth) {
  844. queue_depth = vblk->vqs[0].vq->num_free;
  845. /* ... but without indirect descs, we use 2 descs per req */
  846. if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
  847. queue_depth /= 2;
  848. } else {
  849. queue_depth = virtblk_queue_depth;
  850. }
  851. memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
  852. vblk->tag_set.ops = &virtio_mq_ops;
  853. vblk->tag_set.queue_depth = queue_depth;
  854. vblk->tag_set.numa_node = NUMA_NO_NODE;
  855. vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  856. vblk->tag_set.cmd_size =
  857. sizeof(struct virtblk_req) +
  858. sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
  859. vblk->tag_set.driver_data = vblk;
  860. vblk->tag_set.nr_hw_queues = vblk->num_vqs;
  861. vblk->tag_set.nr_maps = 1;
  862. if (vblk->io_queues[HCTX_TYPE_POLL])
  863. vblk->tag_set.nr_maps = 3;
  864. err = blk_mq_alloc_tag_set(&vblk->tag_set);
  865. if (err)
  866. goto out_free_vq;
  867. vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
  868. if (IS_ERR(vblk->disk)) {
  869. err = PTR_ERR(vblk->disk);
  870. goto out_free_tags;
  871. }
  872. q = vblk->disk->queue;
  873. virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
  874. vblk->disk->major = major;
  875. vblk->disk->first_minor = index_to_minor(index);
  876. vblk->disk->minors = 1 << PART_BITS;
  877. vblk->disk->private_data = vblk;
  878. vblk->disk->fops = &virtblk_fops;
  879. vblk->index = index;
  880. /* configure queue flush support */
  881. virtblk_update_cache_mode(vdev);
  882. /* If disk is read-only in the host, the guest should obey */
  883. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  884. set_disk_ro(vblk->disk, 1);
  885. /* We can handle whatever the host told us to handle. */
  886. blk_queue_max_segments(q, sg_elems);
  887. /* No real sector limit. */
  888. blk_queue_max_hw_sectors(q, -1U);
  889. max_dma_size = virtio_max_dma_size(vdev);
  890. max_size = max_dma_size > U32_MAX ? U32_MAX : max_dma_size;
  891. /* Host can optionally specify maximum segment size and number of
  892. * segments. */
  893. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
  894. struct virtio_blk_config, size_max, &v);
  895. if (!err)
  896. max_size = min(max_size, v);
  897. blk_queue_max_segment_size(q, max_size);
  898. /* Host can optionally specify the block size of the device */
  899. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
  900. struct virtio_blk_config, blk_size,
  901. &blk_size);
  902. if (!err) {
  903. err = blk_validate_block_size(blk_size);
  904. if (err) {
  905. dev_err(&vdev->dev,
  906. "virtio_blk: invalid block size: 0x%x\n",
  907. blk_size);
  908. goto out_cleanup_disk;
  909. }
  910. blk_queue_logical_block_size(q, blk_size);
  911. } else
  912. blk_size = queue_logical_block_size(q);
  913. /* Use topology information if available */
  914. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  915. struct virtio_blk_config, physical_block_exp,
  916. &physical_block_exp);
  917. if (!err && physical_block_exp)
  918. blk_queue_physical_block_size(q,
  919. blk_size * (1 << physical_block_exp));
  920. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  921. struct virtio_blk_config, alignment_offset,
  922. &alignment_offset);
  923. if (!err && alignment_offset)
  924. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  925. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  926. struct virtio_blk_config, min_io_size,
  927. &min_io_size);
  928. if (!err && min_io_size)
  929. blk_queue_io_min(q, blk_size * min_io_size);
  930. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  931. struct virtio_blk_config, opt_io_size,
  932. &opt_io_size);
  933. if (!err && opt_io_size)
  934. blk_queue_io_opt(q, blk_size * opt_io_size);
  935. if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
  936. virtio_cread(vdev, struct virtio_blk_config,
  937. discard_sector_alignment, &discard_granularity);
  938. virtio_cread(vdev, struct virtio_blk_config,
  939. max_discard_sectors, &v);
  940. blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
  941. virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
  942. &max_discard_segs);
  943. }
  944. if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
  945. virtio_cread(vdev, struct virtio_blk_config,
  946. max_write_zeroes_sectors, &v);
  947. blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
  948. }
  949. /* The discard and secure erase limits are combined since the Linux
  950. * block layer uses the same limit for both commands.
  951. *
  952. * If both VIRTIO_BLK_F_SECURE_ERASE and VIRTIO_BLK_F_DISCARD features
  953. * are negotiated, we will use the minimum between the limits.
  954. *
  955. * discard sector alignment is set to the minimum between discard_sector_alignment
  956. * and secure_erase_sector_alignment.
  957. *
  958. * max discard sectors is set to the minimum between max_discard_seg and
  959. * max_secure_erase_seg.
  960. */
  961. if (virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
  962. virtio_cread(vdev, struct virtio_blk_config,
  963. secure_erase_sector_alignment, &v);
  964. /* secure_erase_sector_alignment should not be zero, the device should set a
  965. * valid number of sectors.
  966. */
  967. if (!v) {
  968. dev_err(&vdev->dev,
  969. "virtio_blk: secure_erase_sector_alignment can't be 0\n");
  970. err = -EINVAL;
  971. goto out_cleanup_disk;
  972. }
  973. discard_granularity = min_not_zero(discard_granularity, v);
  974. virtio_cread(vdev, struct virtio_blk_config,
  975. max_secure_erase_sectors, &v);
  976. /* max_secure_erase_sectors should not be zero, the device should set a
  977. * valid number of sectors.
  978. */
  979. if (!v) {
  980. dev_err(&vdev->dev,
  981. "virtio_blk: max_secure_erase_sectors can't be 0\n");
  982. err = -EINVAL;
  983. goto out_cleanup_disk;
  984. }
  985. blk_queue_max_secure_erase_sectors(q, v);
  986. virtio_cread(vdev, struct virtio_blk_config,
  987. max_secure_erase_seg, &v);
  988. /* max_secure_erase_seg should not be zero, the device should set a
  989. * valid number of segments
  990. */
  991. if (!v) {
  992. dev_err(&vdev->dev,
  993. "virtio_blk: max_secure_erase_seg can't be 0\n");
  994. err = -EINVAL;
  995. goto out_cleanup_disk;
  996. }
  997. max_discard_segs = min_not_zero(max_discard_segs, v);
  998. }
  999. if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD) ||
  1000. virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
  1001. /* max_discard_seg and discard_granularity will be 0 only
  1002. * if max_discard_seg and discard_sector_alignment fields in the virtio
  1003. * config are 0 and VIRTIO_BLK_F_SECURE_ERASE feature is not negotiated.
  1004. * In this case, we use default values.
  1005. */
  1006. if (!max_discard_segs)
  1007. max_discard_segs = sg_elems;
  1008. blk_queue_max_discard_segments(q,
  1009. min(max_discard_segs, MAX_DISCARD_SEGMENTS));
  1010. if (discard_granularity)
  1011. q->limits.discard_granularity = discard_granularity << SECTOR_SHIFT;
  1012. else
  1013. q->limits.discard_granularity = blk_size;
  1014. }
  1015. virtblk_update_capacity(vblk, false);
  1016. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  1017. if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_ICE_IV)) {
  1018. dev_notice(&vdev->dev, "%s\n", vblk->disk->disk_name);
  1019. /* Initilaize supported crypto capabilities*/
  1020. err = virtblk_init_crypto_qti_spec(&vblk->vdev->dev);
  1021. if (!err)
  1022. virtblk_crypto_qti_crypto_register(vblk->disk->queue);
  1023. }
  1024. #endif
  1025. virtio_device_ready(vdev);
  1026. err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
  1027. if (err)
  1028. goto out_cleanup_disk;
  1029. return 0;
  1030. out_cleanup_disk:
  1031. put_disk(vblk->disk);
  1032. out_free_tags:
  1033. blk_mq_free_tag_set(&vblk->tag_set);
  1034. out_free_vq:
  1035. vdev->config->del_vqs(vdev);
  1036. kfree(vblk->vqs);
  1037. out_free_vblk:
  1038. kfree(vblk);
  1039. out_free_index:
  1040. ida_simple_remove(&vd_index_ida, index);
  1041. out:
  1042. return err;
  1043. }
  1044. static void virtblk_remove(struct virtio_device *vdev)
  1045. {
  1046. struct virtio_blk *vblk = vdev->priv;
  1047. /* Make sure no work handler is accessing the device. */
  1048. flush_work(&vblk->config_work);
  1049. del_gendisk(vblk->disk);
  1050. blk_mq_free_tag_set(&vblk->tag_set);
  1051. mutex_lock(&vblk->vdev_mutex);
  1052. /* Stop all the virtqueues. */
  1053. virtio_reset_device(vdev);
  1054. /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
  1055. vblk->vdev = NULL;
  1056. vdev->config->del_vqs(vdev);
  1057. kfree(vblk->vqs);
  1058. mutex_unlock(&vblk->vdev_mutex);
  1059. put_disk(vblk->disk);
  1060. }
  1061. #ifdef CONFIG_PM_SLEEP
  1062. static int virtblk_freeze(struct virtio_device *vdev)
  1063. {
  1064. struct virtio_blk *vblk = vdev->priv;
  1065. /* Ensure we don't receive any more interrupts */
  1066. virtio_reset_device(vdev);
  1067. /* Make sure no work handler is accessing the device. */
  1068. flush_work(&vblk->config_work);
  1069. blk_mq_quiesce_queue(vblk->disk->queue);
  1070. vdev->config->del_vqs(vdev);
  1071. kfree(vblk->vqs);
  1072. return 0;
  1073. }
  1074. static int virtblk_restore(struct virtio_device *vdev)
  1075. {
  1076. struct virtio_blk *vblk = vdev->priv;
  1077. int ret;
  1078. ret = init_vq(vdev->priv);
  1079. if (ret)
  1080. return ret;
  1081. virtio_device_ready(vdev);
  1082. blk_mq_unquiesce_queue(vblk->disk->queue);
  1083. return 0;
  1084. }
  1085. #endif
  1086. static const struct virtio_device_id id_table[] = {
  1087. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  1088. { 0 },
  1089. };
  1090. static unsigned int features_legacy[] = {
  1091. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  1092. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  1093. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
  1094. VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
  1095. VIRTIO_BLK_F_SECURE_ERASE,
  1096. }
  1097. ;
  1098. static unsigned int features[] = {
  1099. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  1100. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  1101. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
  1102. VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
  1103. VIRTIO_BLK_F_SECURE_ERASE,
  1104. #if IS_ENABLED(CONFIG_QTI_CRYPTO_VIRTUALIZATION)
  1105. VIRTIO_BLK_F_ICE, VIRTIO_BLK_F_ICE_IV,
  1106. #endif
  1107. };
  1108. static struct virtio_driver virtio_blk = {
  1109. .feature_table = features,
  1110. .feature_table_size = ARRAY_SIZE(features),
  1111. .feature_table_legacy = features_legacy,
  1112. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  1113. .driver.name = KBUILD_MODNAME,
  1114. .driver.owner = THIS_MODULE,
  1115. .id_table = id_table,
  1116. .probe = virtblk_probe,
  1117. .remove = virtblk_remove,
  1118. .config_changed = virtblk_config_changed,
  1119. #ifdef CONFIG_PM_SLEEP
  1120. .freeze = virtblk_freeze,
  1121. .restore = virtblk_restore,
  1122. #endif
  1123. };
  1124. static int __init virtio_blk_init(void)
  1125. {
  1126. int error;
  1127. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  1128. if (!virtblk_wq)
  1129. return -ENOMEM;
  1130. major = register_blkdev(0, "virtblk");
  1131. if (major < 0) {
  1132. error = major;
  1133. goto out_destroy_workqueue;
  1134. }
  1135. error = register_virtio_driver(&virtio_blk);
  1136. if (error)
  1137. goto out_unregister_blkdev;
  1138. return 0;
  1139. out_unregister_blkdev:
  1140. unregister_blkdev(major, "virtblk");
  1141. out_destroy_workqueue:
  1142. destroy_workqueue(virtblk_wq);
  1143. return error;
  1144. }
  1145. static void __exit virtio_blk_fini(void)
  1146. {
  1147. unregister_virtio_driver(&virtio_blk);
  1148. unregister_blkdev(major, "virtblk");
  1149. destroy_workqueue(virtblk_wq);
  1150. }
  1151. module_init(virtio_blk_init);
  1152. module_exit(virtio_blk_fini);
  1153. MODULE_DEVICE_TABLE(virtio, id_table);
  1154. MODULE_DESCRIPTION("Virtio block driver");
  1155. MODULE_LICENSE("GPL");