io-cmd.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_error ? 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_inline_bio_init(struct nvmet_req *req)
  32. {
  33. struct bio *bio = &req->inline_bio;
  34. bio_init(bio);
  35. bio->bi_max_vecs = NVMET_MAX_INLINE_BIOVEC;
  36. bio->bi_io_vec = req->inline_bvec;
  37. }
  38. static void nvmet_execute_rw(struct nvmet_req *req)
  39. {
  40. int sg_cnt = req->sg_cnt;
  41. struct scatterlist *sg;
  42. struct bio *bio;
  43. sector_t sector;
  44. blk_qc_t cookie;
  45. int op, op_flags = 0, i;
  46. if (!req->sg_cnt) {
  47. nvmet_req_complete(req, 0);
  48. return;
  49. }
  50. if (req->cmd->rw.opcode == nvme_cmd_write) {
  51. op = REQ_OP_WRITE;
  52. if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
  53. op_flags |= REQ_FUA;
  54. } else {
  55. op = REQ_OP_READ;
  56. }
  57. sector = le64_to_cpu(req->cmd->rw.slba);
  58. sector <<= (req->ns->blksize_shift - 9);
  59. nvmet_inline_bio_init(req);
  60. bio = &req->inline_bio;
  61. bio->bi_bdev = req->ns->bdev;
  62. bio->bi_iter.bi_sector = sector;
  63. bio->bi_private = req;
  64. bio->bi_end_io = nvmet_bio_done;
  65. bio_set_op_attrs(bio, op, op_flags);
  66. for_each_sg(req->sg, sg, req->sg_cnt, i) {
  67. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  68. != sg->length) {
  69. struct bio *prev = bio;
  70. bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
  71. bio->bi_bdev = req->ns->bdev;
  72. bio->bi_iter.bi_sector = sector;
  73. bio_set_op_attrs(bio, op, op_flags);
  74. bio_chain(bio, prev);
  75. cookie = submit_bio(prev);
  76. }
  77. sector += sg->length >> 9;
  78. sg_cnt--;
  79. }
  80. cookie = submit_bio(bio);
  81. blk_poll(bdev_get_queue(req->ns->bdev), cookie);
  82. }
  83. static void nvmet_execute_flush(struct nvmet_req *req)
  84. {
  85. struct bio *bio;
  86. nvmet_inline_bio_init(req);
  87. bio = &req->inline_bio;
  88. bio->bi_bdev = req->ns->bdev;
  89. bio->bi_private = req;
  90. bio->bi_end_io = nvmet_bio_done;
  91. bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH);
  92. submit_bio(bio);
  93. }
  94. static u16 nvmet_discard_range(struct nvmet_ns *ns,
  95. struct nvme_dsm_range *range, struct bio **bio)
  96. {
  97. if (__blkdev_issue_discard(ns->bdev,
  98. le64_to_cpu(range->slba) << (ns->blksize_shift - 9),
  99. le32_to_cpu(range->nlb) << (ns->blksize_shift - 9),
  100. GFP_KERNEL, 0, bio))
  101. return NVME_SC_INTERNAL | NVME_SC_DNR;
  102. return 0;
  103. }
  104. static void nvmet_execute_discard(struct nvmet_req *req)
  105. {
  106. struct nvme_dsm_range range;
  107. struct bio *bio = NULL;
  108. int i;
  109. u16 status;
  110. for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
  111. status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
  112. sizeof(range));
  113. if (status)
  114. break;
  115. status = nvmet_discard_range(req->ns, &range, &bio);
  116. if (status)
  117. break;
  118. }
  119. if (bio) {
  120. bio->bi_private = req;
  121. bio->bi_end_io = nvmet_bio_done;
  122. if (status) {
  123. bio->bi_error = -EIO;
  124. bio_endio(bio);
  125. } else {
  126. submit_bio(bio);
  127. }
  128. } else {
  129. nvmet_req_complete(req, status);
  130. }
  131. }
  132. static void nvmet_execute_dsm(struct nvmet_req *req)
  133. {
  134. switch (le32_to_cpu(req->cmd->dsm.attributes)) {
  135. case NVME_DSMGMT_AD:
  136. nvmet_execute_discard(req);
  137. return;
  138. case NVME_DSMGMT_IDR:
  139. case NVME_DSMGMT_IDW:
  140. default:
  141. /* Not supported yet */
  142. nvmet_req_complete(req, 0);
  143. return;
  144. }
  145. }
  146. int nvmet_parse_io_cmd(struct nvmet_req *req)
  147. {
  148. struct nvme_command *cmd = req->cmd;
  149. if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
  150. pr_err("nvmet: got io cmd %d while CC.EN == 0\n",
  151. cmd->common.opcode);
  152. req->ns = NULL;
  153. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  154. }
  155. if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
  156. pr_err("nvmet: got io cmd %d while CSTS.RDY == 0\n",
  157. cmd->common.opcode);
  158. req->ns = NULL;
  159. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  160. }
  161. req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
  162. if (!req->ns)
  163. return NVME_SC_INVALID_NS | NVME_SC_DNR;
  164. switch (cmd->common.opcode) {
  165. case nvme_cmd_read:
  166. case nvme_cmd_write:
  167. req->execute = nvmet_execute_rw;
  168. req->data_len = nvmet_rw_len(req);
  169. return 0;
  170. case nvme_cmd_flush:
  171. req->execute = nvmet_execute_flush;
  172. req->data_len = 0;
  173. return 0;
  174. case nvme_cmd_dsm:
  175. req->execute = nvmet_execute_dsm;
  176. req->data_len = le32_to_cpu(cmd->dsm.nr) *
  177. sizeof(struct nvme_dsm_range);
  178. return 0;
  179. default:
  180. pr_err("nvmet: unhandled cmd %d\n", cmd->common.opcode);
  181. return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
  182. }
  183. }