2 * NVMe I/O command implementation.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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.
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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/blkdev.h>
16 #include <linux/module.h>
19 static void nvmet_bio_done(struct bio
*bio
)
21 struct nvmet_req
*req
= bio
->bi_private
;
23 nvmet_req_complete(req
,
24 bio
->bi_error
? NVME_SC_INTERNAL
| NVME_SC_DNR
: 0);
26 if (bio
!= &req
->inline_bio
)
30 static inline u32
nvmet_rw_len(struct nvmet_req
*req
)
32 return ((u32
)le16_to_cpu(req
->cmd
->rw
.length
) + 1) <<
33 req
->ns
->blksize_shift
;
36 static void nvmet_inline_bio_init(struct nvmet_req
*req
)
38 struct bio
*bio
= &req
->inline_bio
;
41 bio
->bi_max_vecs
= NVMET_MAX_INLINE_BIOVEC
;
42 bio
->bi_io_vec
= req
->inline_bvec
;
45 static void nvmet_execute_rw(struct nvmet_req
*req
)
47 int sg_cnt
= req
->sg_cnt
;
48 struct scatterlist
*sg
;
52 int op
, op_flags
= 0, i
;
55 nvmet_req_complete(req
, 0);
59 if (req
->cmd
->rw
.opcode
== nvme_cmd_write
) {
61 if (req
->cmd
->rw
.control
& cpu_to_le16(NVME_RW_FUA
))
67 sector
= le64_to_cpu(req
->cmd
->rw
.slba
);
68 sector
<<= (req
->ns
->blksize_shift
- 9);
70 nvmet_inline_bio_init(req
);
71 bio
= &req
->inline_bio
;
72 bio
->bi_bdev
= req
->ns
->bdev
;
73 bio
->bi_iter
.bi_sector
= sector
;
74 bio
->bi_private
= req
;
75 bio
->bi_end_io
= nvmet_bio_done
;
76 bio_set_op_attrs(bio
, op
, op_flags
);
78 for_each_sg(req
->sg
, sg
, req
->sg_cnt
, i
) {
79 while (bio_add_page(bio
, sg_page(sg
), sg
->length
, sg
->offset
)
81 struct bio
*prev
= bio
;
83 bio
= bio_alloc(GFP_KERNEL
, min(sg_cnt
, BIO_MAX_PAGES
));
84 bio
->bi_bdev
= req
->ns
->bdev
;
85 bio
->bi_iter
.bi_sector
= sector
;
86 bio_set_op_attrs(bio
, op
, op_flags
);
89 cookie
= submit_bio(prev
);
92 sector
+= sg
->length
>> 9;
96 cookie
= submit_bio(bio
);
98 blk_poll(bdev_get_queue(req
->ns
->bdev
), cookie
);
101 static void nvmet_execute_flush(struct nvmet_req
*req
)
105 nvmet_inline_bio_init(req
);
106 bio
= &req
->inline_bio
;
108 bio
->bi_bdev
= req
->ns
->bdev
;
109 bio
->bi_private
= req
;
110 bio
->bi_end_io
= nvmet_bio_done
;
111 bio_set_op_attrs(bio
, REQ_OP_WRITE
, WRITE_FLUSH
);
116 static u16
nvmet_discard_range(struct nvmet_ns
*ns
,
117 struct nvme_dsm_range
*range
, struct bio
**bio
)
119 if (__blkdev_issue_discard(ns
->bdev
,
120 le64_to_cpu(range
->slba
) << (ns
->blksize_shift
- 9),
121 le32_to_cpu(range
->nlb
) << (ns
->blksize_shift
- 9),
123 return NVME_SC_INTERNAL
| NVME_SC_DNR
;
127 static void nvmet_execute_discard(struct nvmet_req
*req
)
129 struct nvme_dsm_range range
;
130 struct bio
*bio
= NULL
;
134 for (i
= 0; i
<= le32_to_cpu(req
->cmd
->dsm
.nr
); i
++) {
135 status
= nvmet_copy_from_sgl(req
, i
* sizeof(range
), &range
,
140 status
= nvmet_discard_range(req
->ns
, &range
, &bio
);
146 bio
->bi_private
= req
;
147 bio
->bi_end_io
= nvmet_bio_done
;
149 bio
->bi_error
= -EIO
;
155 nvmet_req_complete(req
, status
);
159 static void nvmet_execute_dsm(struct nvmet_req
*req
)
161 switch (le32_to_cpu(req
->cmd
->dsm
.attributes
)) {
163 nvmet_execute_discard(req
);
165 case NVME_DSMGMT_IDR
:
166 case NVME_DSMGMT_IDW
:
168 /* Not supported yet */
169 nvmet_req_complete(req
, 0);
174 int nvmet_parse_io_cmd(struct nvmet_req
*req
)
176 struct nvme_command
*cmd
= req
->cmd
;
178 if (unlikely(!(req
->sq
->ctrl
->cc
& NVME_CC_ENABLE
))) {
179 pr_err("nvmet: got io cmd %d while CC.EN == 0\n",
182 return NVME_SC_CMD_SEQ_ERROR
| NVME_SC_DNR
;
185 if (unlikely(!(req
->sq
->ctrl
->csts
& NVME_CSTS_RDY
))) {
186 pr_err("nvmet: got io cmd %d while CSTS.RDY == 0\n",
189 return NVME_SC_CMD_SEQ_ERROR
| NVME_SC_DNR
;
192 req
->ns
= nvmet_find_namespace(req
->sq
->ctrl
, cmd
->rw
.nsid
);
194 return NVME_SC_INVALID_NS
| NVME_SC_DNR
;
196 switch (cmd
->common
.opcode
) {
199 req
->execute
= nvmet_execute_rw
;
200 req
->data_len
= nvmet_rw_len(req
);
203 req
->execute
= nvmet_execute_flush
;
207 req
->execute
= nvmet_execute_dsm
;
208 req
->data_len
= le32_to_cpu(cmd
->dsm
.nr
) *
209 sizeof(struct nvme_dsm_range
);
212 pr_err("nvmet: unhandled cmd %d\n", cmd
->common
.opcode
);
213 return NVME_SC_INVALID_OPCODE
| NVME_SC_DNR
;