io-cmd.c 5.5 KB

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