io-cmd.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * NVMe I/O command implementation.
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/blkdev.h>
  16. #include <linux/module.h>
  17. #include "nvmet.h"
  18. static void nvmet_bio_done(struct bio *bio)
  19. {
  20. struct nvmet_req *req = bio->bi_private;
  21. nvmet_req_complete(req,
  22. bio->bi_status ? NVME_SC_INTERNAL | NVME_SC_DNR : 0);
  23. if (bio != &req->inline_bio)
  24. bio_put(bio);
  25. }
  26. static inline u32 nvmet_rw_len(struct nvmet_req *req)
  27. {
  28. return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
  29. req->ns->blksize_shift;
  30. }
  31. static void nvmet_execute_rw(struct nvmet_req *req)
  32. {
  33. int sg_cnt = req->sg_cnt;
  34. struct bio *bio = &req->inline_bio;
  35. struct scatterlist *sg;
  36. sector_t sector;
  37. blk_qc_t cookie;
  38. int op, op_flags = 0, i;
  39. if (!req->sg_cnt) {
  40. nvmet_req_complete(req, 0);
  41. return;
  42. }
  43. if (req->cmd->rw.opcode == nvme_cmd_write) {
  44. op = REQ_OP_WRITE;
  45. op_flags = REQ_SYNC | REQ_IDLE;
  46. if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
  47. op_flags |= REQ_FUA;
  48. } else {
  49. op = REQ_OP_READ;
  50. }
  51. sector = le64_to_cpu(req->cmd->rw.slba);
  52. sector <<= (req->ns->blksize_shift - 9);
  53. bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
  54. bio_set_dev(bio, req->ns->bdev);
  55. bio->bi_iter.bi_sector = sector;
  56. bio->bi_private = req;
  57. bio->bi_end_io = nvmet_bio_done;
  58. bio_set_op_attrs(bio, op, op_flags);
  59. for_each_sg(req->sg, sg, req->sg_cnt, i) {
  60. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  61. != sg->length) {
  62. struct bio *prev = bio;
  63. bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
  64. bio_set_dev(bio, req->ns->bdev);
  65. bio->bi_iter.bi_sector = sector;
  66. bio_set_op_attrs(bio, op, op_flags);
  67. bio_chain(bio, prev);
  68. submit_bio(prev);
  69. }
  70. sector += sg->length >> 9;
  71. sg_cnt--;
  72. }
  73. cookie = submit_bio(bio);
  74. blk_poll(bdev_get_queue(req->ns->bdev), cookie);
  75. }
  76. static void nvmet_execute_flush(struct nvmet_req *req)
  77. {
  78. struct bio *bio = &req->inline_bio;
  79. bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
  80. bio_set_dev(bio, req->ns->bdev);
  81. bio->bi_private = req;
  82. bio->bi_end_io = nvmet_bio_done;
  83. bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  84. submit_bio(bio);
  85. }
  86. static u16 nvmet_discard_range(struct nvmet_ns *ns,
  87. struct nvme_dsm_range *range, struct bio **bio)
  88. {
  89. if (__blkdev_issue_discard(ns->bdev,
  90. le64_to_cpu(range->slba) << (ns->blksize_shift - 9),
  91. le32_to_cpu(range->nlb) << (ns->blksize_shift - 9),
  92. GFP_KERNEL, 0, bio))
  93. return NVME_SC_INTERNAL | NVME_SC_DNR;
  94. return 0;
  95. }
  96. static void nvmet_execute_discard(struct nvmet_req *req)
  97. {
  98. struct nvme_dsm_range range;
  99. struct bio *bio = NULL;
  100. int i;
  101. u16 status;
  102. for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
  103. status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
  104. sizeof(range));
  105. if (status)
  106. break;
  107. status = nvmet_discard_range(req->ns, &range, &bio);
  108. if (status)
  109. break;
  110. }
  111. if (bio) {
  112. bio->bi_private = req;
  113. bio->bi_end_io = nvmet_bio_done;
  114. if (status) {
  115. bio->bi_status = BLK_STS_IOERR;
  116. bio_endio(bio);
  117. } else {
  118. submit_bio(bio);
  119. }
  120. } else {
  121. nvmet_req_complete(req, status);
  122. }
  123. }
  124. static void nvmet_execute_dsm(struct nvmet_req *req)
  125. {
  126. switch (le32_to_cpu(req->cmd->dsm.attributes)) {
  127. case NVME_DSMGMT_AD:
  128. nvmet_execute_discard(req);
  129. return;
  130. case NVME_DSMGMT_IDR:
  131. case NVME_DSMGMT_IDW:
  132. default:
  133. /* Not supported yet */
  134. nvmet_req_complete(req, 0);
  135. return;
  136. }
  137. }
  138. static void nvmet_execute_write_zeroes(struct nvmet_req *req)
  139. {
  140. struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
  141. struct bio *bio = NULL;
  142. u16 status = NVME_SC_SUCCESS;
  143. sector_t sector;
  144. sector_t nr_sector;
  145. sector = le64_to_cpu(write_zeroes->slba) <<
  146. (req->ns->blksize_shift - 9);
  147. nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length)) <<
  148. (req->ns->blksize_shift - 9)) + 1;
  149. if (__blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
  150. GFP_KERNEL, &bio, 0))
  151. status = NVME_SC_INTERNAL | NVME_SC_DNR;
  152. if (bio) {
  153. bio->bi_private = req;
  154. bio->bi_end_io = nvmet_bio_done;
  155. submit_bio(bio);
  156. } else {
  157. nvmet_req_complete(req, status);
  158. }
  159. }
  160. u16 nvmet_parse_io_cmd(struct nvmet_req *req)
  161. {
  162. struct nvme_command *cmd = req->cmd;
  163. u16 ret;
  164. ret = nvmet_check_ctrl_status(req, cmd);
  165. if (unlikely(ret)) {
  166. req->ns = NULL;
  167. return ret;
  168. }
  169. req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
  170. if (unlikely(!req->ns))
  171. return NVME_SC_INVALID_NS | NVME_SC_DNR;
  172. switch (cmd->common.opcode) {
  173. case nvme_cmd_read:
  174. case nvme_cmd_write:
  175. req->execute = nvmet_execute_rw;
  176. req->data_len = nvmet_rw_len(req);
  177. return 0;
  178. case nvme_cmd_flush:
  179. req->execute = nvmet_execute_flush;
  180. req->data_len = 0;
  181. return 0;
  182. case nvme_cmd_dsm:
  183. req->execute = nvmet_execute_dsm;
  184. req->data_len = (le32_to_cpu(cmd->dsm.nr) + 1) *
  185. sizeof(struct nvme_dsm_range);
  186. return 0;
  187. case nvme_cmd_write_zeroes:
  188. req->execute = nvmet_execute_write_zeroes;
  189. return 0;
  190. default:
  191. pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
  192. req->sq->qid);
  193. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  194. }
  195. }