io-cmd.c 5.0 KB

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