1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * NVMe I/O command implementation.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/blkdev.h>
8 #include <linux/blk-integrity.h>
9 #include <linux/memremap.h>
10 #include <linux/module.h>
11 #include "nvmet.h"
12
nvmet_bdev_set_limits(struct block_device * bdev,struct nvme_id_ns * id)13 void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
14 {
15 /* Logical blocks per physical block, 0's based. */
16 const __le16 lpp0b = to0based(bdev_physical_block_size(bdev) /
17 bdev_logical_block_size(bdev));
18
19 /*
20 * For NVMe 1.2 and later, bit 1 indicates that the fields NAWUN,
21 * NAWUPF, and NACWU are defined for this namespace and should be
22 * used by the host for this namespace instead of the AWUN, AWUPF,
23 * and ACWU fields in the Identify Controller data structure. If
24 * any of these fields are zero that means that the corresponding
25 * field from the identify controller data structure should be used.
26 */
27 id->nsfeat |= 1 << 1;
28 id->nawun = lpp0b;
29 id->nawupf = lpp0b;
30 id->nacwu = lpp0b;
31
32 /*
33 * OPTPERF = 11b indicates that the fields NPWG, NPWA, NPDG, NPDA,
34 * NPDGL, NPDAL, and NOWS are defined for this namespace and should be
35 * used by the host for I/O optimization.
36 */
37 id->nsfeat |= 0x3 << NVME_NS_FEAT_OPTPERF_SHIFT;
38 /* NPWG = Namespace Preferred Write Granularity. 0's based */
39 id->npwg = to0based(bdev_io_min(bdev) / bdev_logical_block_size(bdev));
40 /* NPWA = Namespace Preferred Write Alignment. 0's based */
41 id->npwa = id->npwg;
42 /* NPDG = Namespace Preferred Deallocate Granularity. 0's based */
43 id->npdg = to0based(bdev_discard_granularity(bdev) /
44 bdev_logical_block_size(bdev));
45 /* NPDG = Namespace Preferred Deallocate Alignment */
46 id->npda = id->npdg;
47 /* NOWS = Namespace Optimal Write Size */
48 id->nows = to0based(bdev_io_opt(bdev) / bdev_logical_block_size(bdev));
49
50 /* Set WZDS and DRB if device supports unmapped write zeroes */
51 if (bdev_write_zeroes_unmap_sectors(bdev))
52 id->dlfeat = (1 << 3) | 0x1;
53 }
54
nvmet_bdev_set_nvm_limits(struct block_device * bdev,struct nvme_id_ns_nvm * id)55 void nvmet_bdev_set_nvm_limits(struct block_device *bdev,
56 struct nvme_id_ns_nvm *id)
57 {
58 /*
59 * NPDGL = Namespace Preferred Deallocate Granularity Large
60 * NPDAL = Namespace Preferred Deallocate Alignment Large
61 */
62 id->npdgl = id->npdal = cpu_to_le32(bdev_discard_granularity(bdev) /
63 bdev_logical_block_size(bdev));
64 }
65
nvmet_bdev_ns_disable(struct nvmet_ns * ns)66 void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
67 {
68 if (ns->bdev_file) {
69 fput(ns->bdev_file);
70 ns->bdev = NULL;
71 ns->bdev_file = NULL;
72 }
73 }
74
nvmet_bdev_ns_enable_integrity(struct nvmet_ns * ns)75 static void nvmet_bdev_ns_enable_integrity(struct nvmet_ns *ns)
76 {
77 struct blk_integrity *bi = bdev_get_integrity(ns->bdev);
78
79 if (!bi)
80 return;
81
82 if (bi->csum_type == BLK_INTEGRITY_CSUM_CRC) {
83 ns->metadata_size = bi->metadata_size;
84 if (bi->flags & BLK_INTEGRITY_REF_TAG)
85 ns->pi_type = NVME_NS_DPS_PI_TYPE1;
86 else
87 ns->pi_type = NVME_NS_DPS_PI_TYPE3;
88 } else {
89 ns->metadata_size = 0;
90 }
91 }
92
nvmet_bdev_ns_enable(struct nvmet_ns * ns)93 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
94 {
95 int ret;
96
97 /*
98 * When buffered_io namespace attribute is enabled that means user want
99 * this block device to be used as a file, so block device can take
100 * an advantage of cache.
101 */
102 if (ns->buffered_io)
103 return -ENOTBLK;
104
105 ns->bdev_file = bdev_file_open_by_path(ns->device_path,
106 BLK_OPEN_READ | BLK_OPEN_WRITE, NULL, NULL);
107 if (IS_ERR(ns->bdev_file)) {
108 ret = PTR_ERR(ns->bdev_file);
109 if (ret != -ENOTBLK) {
110 pr_err("failed to open block device %s: (%d)\n",
111 ns->device_path, ret);
112 }
113 ns->bdev_file = NULL;
114 return ret;
115 }
116 ns->bdev = file_bdev(ns->bdev_file);
117 ns->size = bdev_nr_bytes(ns->bdev);
118 ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
119
120 ns->pi_type = 0;
121 ns->metadata_size = 0;
122 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
123 nvmet_bdev_ns_enable_integrity(ns);
124
125 if (bdev_is_zoned(ns->bdev)) {
126 if (!nvmet_bdev_zns_enable(ns)) {
127 nvmet_bdev_ns_disable(ns);
128 return -EINVAL;
129 }
130 ns->csi = NVME_CSI_ZNS;
131 }
132
133 return 0;
134 }
135
nvmet_bdev_ns_revalidate(struct nvmet_ns * ns)136 void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
137 {
138 ns->size = bdev_nr_bytes(ns->bdev);
139 }
140
blk_to_nvme_status(struct nvmet_req * req,blk_status_t blk_sts)141 u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
142 {
143 u16 status = NVME_SC_SUCCESS;
144
145 if (likely(blk_sts == BLK_STS_OK))
146 return status;
147 /*
148 * Right now there exists M : 1 mapping between block layer error
149 * to the NVMe status code (see nvme_error_status()). For consistency,
150 * when we reverse map we use most appropriate NVMe Status code from
151 * the group of the NVMe status codes used in the nvme_error_status().
152 */
153 switch (blk_sts) {
154 case BLK_STS_NOSPC:
155 status = NVME_SC_CAP_EXCEEDED | NVME_STATUS_DNR;
156 req->error_loc = offsetof(struct nvme_rw_command, length);
157 break;
158 case BLK_STS_TARGET:
159 status = NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
160 req->error_loc = offsetof(struct nvme_rw_command, slba);
161 break;
162 case BLK_STS_NOTSUPP:
163 status = NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
164 req->error_loc = offsetof(struct nvme_common_command, opcode);
165 break;
166 case BLK_STS_MEDIUM:
167 status = NVME_SC_ACCESS_DENIED;
168 req->error_loc = offsetof(struct nvme_rw_command, nsid);
169 break;
170 case BLK_STS_IOERR:
171 default:
172 status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
173 req->error_loc = offsetof(struct nvme_common_command, opcode);
174 }
175
176 switch (req->cmd->common.opcode) {
177 case nvme_cmd_read:
178 case nvme_cmd_write:
179 req->error_slba = le64_to_cpu(req->cmd->rw.slba);
180 break;
181 case nvme_cmd_write_zeroes:
182 req->error_slba =
183 le64_to_cpu(req->cmd->write_zeroes.slba);
184 break;
185 default:
186 req->error_slba = 0;
187 }
188 return status;
189 }
190
nvmet_bio_done(struct bio * bio)191 static void nvmet_bio_done(struct bio *bio)
192 {
193 struct nvmet_req *req = bio->bi_private;
194 blk_status_t blk_status = bio->bi_status;
195
196 nvmet_req_bio_put(req, bio);
197 nvmet_req_complete(req, blk_to_nvme_status(req, blk_status));
198 }
199
200 #ifdef CONFIG_BLK_DEV_INTEGRITY
nvmet_bdev_alloc_bip(struct nvmet_req * req,struct bio * bio,struct sg_mapping_iter * miter)201 static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
202 struct sg_mapping_iter *miter)
203 {
204 struct blk_integrity *bi;
205 struct bio_integrity_payload *bip;
206 int rc;
207 size_t resid, len;
208
209 bi = bdev_get_integrity(req->ns->bdev);
210 if (unlikely(!bi)) {
211 pr_err("Unable to locate bio_integrity\n");
212 return -ENODEV;
213 }
214
215 bip = bio_integrity_alloc(bio, GFP_NOIO,
216 bio_max_segs(req->metadata_sg_cnt));
217 if (IS_ERR(bip)) {
218 pr_err("Unable to allocate bio_integrity_payload\n");
219 return PTR_ERR(bip);
220 }
221
222 /* virtual start sector must be in integrity interval units */
223 bip_set_seed(bip, bio->bi_iter.bi_sector >>
224 (bi->interval_exp - SECTOR_SHIFT));
225
226 resid = bio_integrity_bytes(bi, bio_sectors(bio));
227 while (resid > 0 && sg_miter_next(miter)) {
228 len = min_t(size_t, miter->length, resid);
229 rc = bio_integrity_add_page(bio, miter->page, len,
230 offset_in_page(miter->addr));
231 if (unlikely(rc != len)) {
232 pr_err("bio_integrity_add_page() failed; %d\n", rc);
233 sg_miter_stop(miter);
234 return -ENOMEM;
235 }
236
237 resid -= len;
238 if (len < miter->length)
239 miter->consumed -= miter->length - len;
240 }
241 sg_miter_stop(miter);
242
243 return 0;
244 }
245 #else
nvmet_bdev_alloc_bip(struct nvmet_req * req,struct bio * bio,struct sg_mapping_iter * miter)246 static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
247 struct sg_mapping_iter *miter)
248 {
249 return -EINVAL;
250 }
251 #endif /* CONFIG_BLK_DEV_INTEGRITY */
252
nvmet_bdev_execute_rw(struct nvmet_req * req)253 static void nvmet_bdev_execute_rw(struct nvmet_req *req)
254 {
255 unsigned int sg_cnt = req->sg_cnt;
256 struct bio *bio;
257 struct scatterlist *sg;
258 struct blk_plug plug;
259 sector_t sector;
260 blk_opf_t opf;
261 int i, rc;
262 struct sg_mapping_iter prot_miter;
263 unsigned int iter_flags;
264 unsigned int total_len = nvmet_rw_data_len(req) + req->metadata_len;
265
266 if (!nvmet_check_transfer_len(req, total_len))
267 return;
268
269 if (!req->sg_cnt) {
270 nvmet_req_complete(req, 0);
271 return;
272 }
273
274 if (req->cmd->rw.opcode == nvme_cmd_write) {
275 opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
276 if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
277 opf |= REQ_FUA;
278 iter_flags = SG_MITER_TO_SG;
279 } else {
280 opf = REQ_OP_READ;
281 iter_flags = SG_MITER_FROM_SG;
282 }
283
284 if (req->cmd->rw.control & cpu_to_le16(NVME_RW_LR))
285 opf |= REQ_FAILFAST_DEV;
286
287 if (is_pci_p2pdma_page(sg_page(req->sg)))
288 opf |= REQ_NOMERGE;
289
290 sector = nvmet_lba_to_sect(req->ns, req->cmd->rw.slba);
291
292 if (nvmet_use_inline_bvec(req)) {
293 bio = &req->b.inline_bio;
294 bio_init(bio, req->ns->bdev, req->inline_bvec,
295 ARRAY_SIZE(req->inline_bvec), opf);
296 } else {
297 bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt), opf,
298 GFP_KERNEL);
299 }
300 bio->bi_iter.bi_sector = sector;
301 bio->bi_private = req;
302 bio->bi_end_io = nvmet_bio_done;
303
304 blk_start_plug(&plug);
305 if (req->metadata_len)
306 sg_miter_start(&prot_miter, req->metadata_sg,
307 req->metadata_sg_cnt, iter_flags);
308
309 for_each_sg(req->sg, sg, req->sg_cnt, i) {
310 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
311 != sg->length) {
312 struct bio *prev = bio;
313
314 if (req->metadata_len) {
315 rc = nvmet_bdev_alloc_bip(req, bio,
316 &prot_miter);
317 if (unlikely(rc)) {
318 bio_io_error(bio);
319 return;
320 }
321 }
322
323 bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt),
324 opf, GFP_KERNEL);
325 bio->bi_iter.bi_sector = sector;
326
327 bio_chain(bio, prev);
328 submit_bio(prev);
329 }
330
331 sector += sg->length >> 9;
332 sg_cnt--;
333 }
334
335 if (req->metadata_len) {
336 rc = nvmet_bdev_alloc_bip(req, bio, &prot_miter);
337 if (unlikely(rc)) {
338 bio_io_error(bio);
339 return;
340 }
341 }
342
343 submit_bio(bio);
344 blk_finish_plug(&plug);
345 }
346
nvmet_bdev_execute_flush(struct nvmet_req * req)347 static void nvmet_bdev_execute_flush(struct nvmet_req *req)
348 {
349 struct bio *bio = &req->b.inline_bio;
350
351 if (!bdev_write_cache(req->ns->bdev)) {
352 nvmet_req_complete(req, NVME_SC_SUCCESS);
353 return;
354 }
355
356 if (!nvmet_check_transfer_len(req, 0))
357 return;
358
359 bio_init(bio, req->ns->bdev, req->inline_bvec,
360 ARRAY_SIZE(req->inline_bvec), REQ_OP_WRITE | REQ_PREFLUSH);
361 bio->bi_private = req;
362 bio->bi_end_io = nvmet_bio_done;
363
364 submit_bio(bio);
365 }
366
nvmet_bdev_flush(struct nvmet_req * req)367 u16 nvmet_bdev_flush(struct nvmet_req *req)
368 {
369 if (!bdev_write_cache(req->ns->bdev))
370 return 0;
371
372 if (blkdev_issue_flush(req->ns->bdev))
373 return NVME_SC_INTERNAL | NVME_STATUS_DNR;
374 return 0;
375 }
376
nvmet_bdev_execute_discard(struct nvmet_req * req)377 static void nvmet_bdev_execute_discard(struct nvmet_req *req)
378 {
379 struct nvmet_ns *ns = req->ns;
380 struct nvme_dsm_range range;
381 struct bio *bio = NULL;
382 sector_t nr_sects;
383 int i;
384 u16 status = NVME_SC_SUCCESS;
385
386 for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
387 status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
388 sizeof(range));
389 if (status)
390 break;
391
392 nr_sects = le32_to_cpu(range.nlb) << (ns->blksize_shift - 9);
393 __blkdev_issue_discard(ns->bdev,
394 nvmet_lba_to_sect(ns, range.slba), nr_sects,
395 GFP_KERNEL, &bio);
396 }
397
398 if (bio) {
399 bio->bi_private = req;
400 bio->bi_end_io = nvmet_bio_done;
401 if (status)
402 bio_io_error(bio);
403 else
404 submit_bio(bio);
405 } else {
406 nvmet_req_complete(req, status);
407 }
408 }
409
nvmet_bdev_execute_dsm(struct nvmet_req * req)410 static void nvmet_bdev_execute_dsm(struct nvmet_req *req)
411 {
412 if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req)))
413 return;
414
415 switch (le32_to_cpu(req->cmd->dsm.attributes)) {
416 case NVME_DSMGMT_AD:
417 nvmet_bdev_execute_discard(req);
418 return;
419 case NVME_DSMGMT_IDR:
420 case NVME_DSMGMT_IDW:
421 default:
422 /* Not supported yet */
423 nvmet_req_complete(req, 0);
424 return;
425 }
426 }
427
nvmet_bdev_execute_write_zeroes(struct nvmet_req * req)428 static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
429 {
430 struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
431 struct bio *bio = NULL;
432 sector_t sector;
433 sector_t nr_sector;
434 int ret;
435
436 if (!nvmet_check_transfer_len(req, 0))
437 return;
438
439 sector = nvmet_lba_to_sect(req->ns, write_zeroes->slba);
440 nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
441 (req->ns->blksize_shift - 9));
442
443 ret = __blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
444 GFP_KERNEL, &bio, 0);
445 if (bio) {
446 bio->bi_private = req;
447 bio->bi_end_io = nvmet_bio_done;
448 submit_bio(bio);
449 } else {
450 nvmet_req_complete(req, errno_to_nvme_status(req, ret));
451 }
452 }
453
nvmet_bdev_parse_io_cmd(struct nvmet_req * req)454 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)
455 {
456 switch (req->cmd->common.opcode) {
457 case nvme_cmd_read:
458 case nvme_cmd_write:
459 req->execute = nvmet_bdev_execute_rw;
460 if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns))
461 req->metadata_len = nvmet_rw_metadata_len(req);
462 return 0;
463 case nvme_cmd_flush:
464 req->execute = nvmet_bdev_execute_flush;
465 return 0;
466 case nvme_cmd_dsm:
467 req->execute = nvmet_bdev_execute_dsm;
468 return 0;
469 case nvme_cmd_write_zeroes:
470 req->execute = nvmet_bdev_execute_write_zeroes;
471 return 0;
472 default:
473 return nvmet_report_invalid_opcode(req);
474 }
475 }
476