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/module.h>
9 #include "nvmet.h"
10 
11 void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
12 {
13 	const struct queue_limits *ql = &bdev_get_queue(bdev)->limits;
14 	/* Number of logical blocks per physical block. */
15 	const u32 lpp = ql->physical_block_size / ql->logical_block_size;
16 	/* Logical blocks per physical block, 0's based. */
17 	const __le16 lpp0b = to0based(lpp);
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 	 * Bit 4 indicates that the fields NPWG, NPWA, NPDG, NPDA, and
34 	 * NOWS are defined for this namespace and should be used by
35 	 * the host for I/O optimization.
36 	 */
37 	id->nsfeat |= 1 << 4;
38 	/* NPWG = Namespace Preferred Write Granularity. 0's based */
39 	id->npwg = lpp0b;
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(ql->discard_granularity / ql->logical_block_size);
44 	/* NPDG = Namespace Preferred Deallocate Alignment */
45 	id->npda = id->npdg;
46 	/* NOWS = Namespace Optimal Write Size */
47 	id->nows = to0based(ql->io_opt / ql->logical_block_size);
48 }
49 
50 static void nvmet_bdev_ns_enable_integrity(struct nvmet_ns *ns)
51 {
52 	struct blk_integrity *bi = bdev_get_integrity(ns->bdev);
53 
54 	if (bi) {
55 		ns->metadata_size = bi->tuple_size;
56 		if (bi->profile == &t10_pi_type1_crc)
57 			ns->pi_type = NVME_NS_DPS_PI_TYPE1;
58 		else if (bi->profile == &t10_pi_type3_crc)
59 			ns->pi_type = NVME_NS_DPS_PI_TYPE3;
60 		else
61 			/* Unsupported metadata type */
62 			ns->metadata_size = 0;
63 	}
64 }
65 
66 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
67 {
68 	int ret;
69 
70 	ns->bdev = blkdev_get_by_path(ns->device_path,
71 			FMODE_READ | FMODE_WRITE, NULL);
72 	if (IS_ERR(ns->bdev)) {
73 		ret = PTR_ERR(ns->bdev);
74 		if (ret != -ENOTBLK) {
75 			pr_err("failed to open block device %s: (%ld)\n",
76 					ns->device_path, PTR_ERR(ns->bdev));
77 		}
78 		ns->bdev = NULL;
79 		return ret;
80 	}
81 	ns->size = i_size_read(ns->bdev->bd_inode);
82 	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
83 
84 	ns->pi_type = 0;
85 	ns->metadata_size = 0;
86 	if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY_T10))
87 		nvmet_bdev_ns_enable_integrity(ns);
88 
89 	return 0;
90 }
91 
92 void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
93 {
94 	if (ns->bdev) {
95 		blkdev_put(ns->bdev, FMODE_WRITE | FMODE_READ);
96 		ns->bdev = NULL;
97 	}
98 }
99 
100 void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
101 {
102 	ns->size = i_size_read(ns->bdev->bd_inode);
103 }
104 
105 static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
106 {
107 	u16 status = NVME_SC_SUCCESS;
108 
109 	if (likely(blk_sts == BLK_STS_OK))
110 		return status;
111 	/*
112 	 * Right now there exists M : 1 mapping between block layer error
113 	 * to the NVMe status code (see nvme_error_status()). For consistency,
114 	 * when we reverse map we use most appropriate NVMe Status code from
115 	 * the group of the NVMe staus codes used in the nvme_error_status().
116 	 */
117 	switch (blk_sts) {
118 	case BLK_STS_NOSPC:
119 		status = NVME_SC_CAP_EXCEEDED | NVME_SC_DNR;
120 		req->error_loc = offsetof(struct nvme_rw_command, length);
121 		break;
122 	case BLK_STS_TARGET:
123 		status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
124 		req->error_loc = offsetof(struct nvme_rw_command, slba);
125 		break;
126 	case BLK_STS_NOTSUPP:
127 		req->error_loc = offsetof(struct nvme_common_command, opcode);
128 		switch (req->cmd->common.opcode) {
129 		case nvme_cmd_dsm:
130 		case nvme_cmd_write_zeroes:
131 			status = NVME_SC_ONCS_NOT_SUPPORTED | NVME_SC_DNR;
132 			break;
133 		default:
134 			status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
135 		}
136 		break;
137 	case BLK_STS_MEDIUM:
138 		status = NVME_SC_ACCESS_DENIED;
139 		req->error_loc = offsetof(struct nvme_rw_command, nsid);
140 		break;
141 	case BLK_STS_IOERR:
142 		/* fallthru */
143 	default:
144 		status = NVME_SC_INTERNAL | NVME_SC_DNR;
145 		req->error_loc = offsetof(struct nvme_common_command, opcode);
146 	}
147 
148 	switch (req->cmd->common.opcode) {
149 	case nvme_cmd_read:
150 	case nvme_cmd_write:
151 		req->error_slba = le64_to_cpu(req->cmd->rw.slba);
152 		break;
153 	case nvme_cmd_write_zeroes:
154 		req->error_slba =
155 			le64_to_cpu(req->cmd->write_zeroes.slba);
156 		break;
157 	default:
158 		req->error_slba = 0;
159 	}
160 	return status;
161 }
162 
163 static void nvmet_bio_done(struct bio *bio)
164 {
165 	struct nvmet_req *req = bio->bi_private;
166 
167 	nvmet_req_complete(req, blk_to_nvme_status(req, bio->bi_status));
168 	if (bio != &req->b.inline_bio)
169 		bio_put(bio);
170 }
171 
172 #ifdef CONFIG_BLK_DEV_INTEGRITY
173 static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
174 				struct sg_mapping_iter *miter)
175 {
176 	struct blk_integrity *bi;
177 	struct bio_integrity_payload *bip;
178 	struct block_device *bdev = req->ns->bdev;
179 	int rc;
180 	size_t resid, len;
181 
182 	bi = bdev_get_integrity(bdev);
183 	if (unlikely(!bi)) {
184 		pr_err("Unable to locate bio_integrity\n");
185 		return -ENODEV;
186 	}
187 
188 	bip = bio_integrity_alloc(bio, GFP_NOIO,
189 		min_t(unsigned int, req->metadata_sg_cnt, BIO_MAX_PAGES));
190 	if (IS_ERR(bip)) {
191 		pr_err("Unable to allocate bio_integrity_payload\n");
192 		return PTR_ERR(bip);
193 	}
194 
195 	bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
196 	/* virtual start sector must be in integrity interval units */
197 	bip_set_seed(bip, bio->bi_iter.bi_sector >>
198 		     (bi->interval_exp - SECTOR_SHIFT));
199 
200 	resid = bip->bip_iter.bi_size;
201 	while (resid > 0 && sg_miter_next(miter)) {
202 		len = min_t(size_t, miter->length, resid);
203 		rc = bio_integrity_add_page(bio, miter->page, len,
204 					    offset_in_page(miter->addr));
205 		if (unlikely(rc != len)) {
206 			pr_err("bio_integrity_add_page() failed; %d\n", rc);
207 			sg_miter_stop(miter);
208 			return -ENOMEM;
209 		}
210 
211 		resid -= len;
212 		if (len < miter->length)
213 			miter->consumed -= miter->length - len;
214 	}
215 	sg_miter_stop(miter);
216 
217 	return 0;
218 }
219 #else
220 static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
221 				struct sg_mapping_iter *miter)
222 {
223 	return -EINVAL;
224 }
225 #endif /* CONFIG_BLK_DEV_INTEGRITY */
226 
227 static void nvmet_bdev_execute_rw(struct nvmet_req *req)
228 {
229 	int sg_cnt = req->sg_cnt;
230 	struct bio *bio;
231 	struct scatterlist *sg;
232 	struct blk_plug plug;
233 	sector_t sector;
234 	int op, i, rc;
235 	struct sg_mapping_iter prot_miter;
236 	unsigned int iter_flags;
237 	unsigned int total_len = nvmet_rw_data_len(req) + req->metadata_len;
238 
239 	if (!nvmet_check_transfer_len(req, total_len))
240 		return;
241 
242 	if (!req->sg_cnt) {
243 		nvmet_req_complete(req, 0);
244 		return;
245 	}
246 
247 	if (req->cmd->rw.opcode == nvme_cmd_write) {
248 		op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
249 		if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
250 			op |= REQ_FUA;
251 		iter_flags = SG_MITER_TO_SG;
252 	} else {
253 		op = REQ_OP_READ;
254 		iter_flags = SG_MITER_FROM_SG;
255 	}
256 
257 	if (is_pci_p2pdma_page(sg_page(req->sg)))
258 		op |= REQ_NOMERGE;
259 
260 	sector = le64_to_cpu(req->cmd->rw.slba);
261 	sector <<= (req->ns->blksize_shift - 9);
262 
263 	if (req->transfer_len <= NVMET_MAX_INLINE_DATA_LEN) {
264 		bio = &req->b.inline_bio;
265 		bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
266 	} else {
267 		bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
268 	}
269 	bio_set_dev(bio, req->ns->bdev);
270 	bio->bi_iter.bi_sector = sector;
271 	bio->bi_private = req;
272 	bio->bi_end_io = nvmet_bio_done;
273 	bio->bi_opf = op;
274 
275 	blk_start_plug(&plug);
276 	if (req->metadata_len)
277 		sg_miter_start(&prot_miter, req->metadata_sg,
278 			       req->metadata_sg_cnt, iter_flags);
279 
280 	for_each_sg(req->sg, sg, req->sg_cnt, i) {
281 		while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
282 				!= sg->length) {
283 			struct bio *prev = bio;
284 
285 			if (req->metadata_len) {
286 				rc = nvmet_bdev_alloc_bip(req, bio,
287 							  &prot_miter);
288 				if (unlikely(rc)) {
289 					bio_io_error(bio);
290 					return;
291 				}
292 			}
293 
294 			bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
295 			bio_set_dev(bio, req->ns->bdev);
296 			bio->bi_iter.bi_sector = sector;
297 			bio->bi_opf = op;
298 
299 			bio_chain(bio, prev);
300 			submit_bio(prev);
301 		}
302 
303 		sector += sg->length >> 9;
304 		sg_cnt--;
305 	}
306 
307 	if (req->metadata_len) {
308 		rc = nvmet_bdev_alloc_bip(req, bio, &prot_miter);
309 		if (unlikely(rc)) {
310 			bio_io_error(bio);
311 			return;
312 		}
313 	}
314 
315 	submit_bio(bio);
316 	blk_finish_plug(&plug);
317 }
318 
319 static void nvmet_bdev_execute_flush(struct nvmet_req *req)
320 {
321 	struct bio *bio = &req->b.inline_bio;
322 
323 	if (!nvmet_check_transfer_len(req, 0))
324 		return;
325 
326 	bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
327 	bio_set_dev(bio, req->ns->bdev);
328 	bio->bi_private = req;
329 	bio->bi_end_io = nvmet_bio_done;
330 	bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
331 
332 	submit_bio(bio);
333 }
334 
335 u16 nvmet_bdev_flush(struct nvmet_req *req)
336 {
337 	if (blkdev_issue_flush(req->ns->bdev, GFP_KERNEL))
338 		return NVME_SC_INTERNAL | NVME_SC_DNR;
339 	return 0;
340 }
341 
342 static u16 nvmet_bdev_discard_range(struct nvmet_req *req,
343 		struct nvme_dsm_range *range, struct bio **bio)
344 {
345 	struct nvmet_ns *ns = req->ns;
346 	int ret;
347 
348 	ret = __blkdev_issue_discard(ns->bdev,
349 			le64_to_cpu(range->slba) << (ns->blksize_shift - 9),
350 			le32_to_cpu(range->nlb) << (ns->blksize_shift - 9),
351 			GFP_KERNEL, 0, bio);
352 	if (ret && ret != -EOPNOTSUPP) {
353 		req->error_slba = le64_to_cpu(range->slba);
354 		return errno_to_nvme_status(req, ret);
355 	}
356 	return NVME_SC_SUCCESS;
357 }
358 
359 static void nvmet_bdev_execute_discard(struct nvmet_req *req)
360 {
361 	struct nvme_dsm_range range;
362 	struct bio *bio = NULL;
363 	int i;
364 	u16 status;
365 
366 	for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
367 		status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
368 				sizeof(range));
369 		if (status)
370 			break;
371 
372 		status = nvmet_bdev_discard_range(req, &range, &bio);
373 		if (status)
374 			break;
375 	}
376 
377 	if (bio) {
378 		bio->bi_private = req;
379 		bio->bi_end_io = nvmet_bio_done;
380 		if (status)
381 			bio_io_error(bio);
382 		else
383 			submit_bio(bio);
384 	} else {
385 		nvmet_req_complete(req, status);
386 	}
387 }
388 
389 static void nvmet_bdev_execute_dsm(struct nvmet_req *req)
390 {
391 	if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req)))
392 		return;
393 
394 	switch (le32_to_cpu(req->cmd->dsm.attributes)) {
395 	case NVME_DSMGMT_AD:
396 		nvmet_bdev_execute_discard(req);
397 		return;
398 	case NVME_DSMGMT_IDR:
399 	case NVME_DSMGMT_IDW:
400 	default:
401 		/* Not supported yet */
402 		nvmet_req_complete(req, 0);
403 		return;
404 	}
405 }
406 
407 static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
408 {
409 	struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
410 	struct bio *bio = NULL;
411 	sector_t sector;
412 	sector_t nr_sector;
413 	int ret;
414 
415 	if (!nvmet_check_transfer_len(req, 0))
416 		return;
417 
418 	sector = le64_to_cpu(write_zeroes->slba) <<
419 		(req->ns->blksize_shift - 9);
420 	nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
421 		(req->ns->blksize_shift - 9));
422 
423 	ret = __blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
424 			GFP_KERNEL, &bio, 0);
425 	if (bio) {
426 		bio->bi_private = req;
427 		bio->bi_end_io = nvmet_bio_done;
428 		submit_bio(bio);
429 	} else {
430 		nvmet_req_complete(req, errno_to_nvme_status(req, ret));
431 	}
432 }
433 
434 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)
435 {
436 	struct nvme_command *cmd = req->cmd;
437 
438 	switch (cmd->common.opcode) {
439 	case nvme_cmd_read:
440 	case nvme_cmd_write:
441 		req->execute = nvmet_bdev_execute_rw;
442 		if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns))
443 			req->metadata_len = nvmet_rw_metadata_len(req);
444 		return 0;
445 	case nvme_cmd_flush:
446 		req->execute = nvmet_bdev_execute_flush;
447 		return 0;
448 	case nvme_cmd_dsm:
449 		req->execute = nvmet_bdev_execute_dsm;
450 		return 0;
451 	case nvme_cmd_write_zeroes:
452 		req->execute = nvmet_bdev_execute_write_zeroes;
453 		return 0;
454 	default:
455 		pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
456 		       req->sq->qid);
457 		req->error_loc = offsetof(struct nvme_common_command, opcode);
458 		return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
459 	}
460 }
461