xref: /openbmc/linux/drivers/nvme/target/zns.c (revision da783733)
1aaf2e048SChaitanya Kulkarni // SPDX-License-Identifier: GPL-2.0
2aaf2e048SChaitanya Kulkarni /*
3aaf2e048SChaitanya Kulkarni  * NVMe ZNS-ZBD command implementation.
4aaf2e048SChaitanya Kulkarni  * Copyright (C) 2021 Western Digital Corporation or its affiliates.
5aaf2e048SChaitanya Kulkarni  */
6aaf2e048SChaitanya Kulkarni #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7aaf2e048SChaitanya Kulkarni #include <linux/nvme.h>
8aaf2e048SChaitanya Kulkarni #include <linux/blkdev.h>
9aaf2e048SChaitanya Kulkarni #include "nvmet.h"
10aaf2e048SChaitanya Kulkarni 
11aaf2e048SChaitanya Kulkarni /*
12aaf2e048SChaitanya Kulkarni  * We set the Memory Page Size Minimum (MPSMIN) for target controller to 0
13aaf2e048SChaitanya Kulkarni  * which gets added by 12 in the nvme_enable_ctrl() which results in 2^12 = 4k
14aaf2e048SChaitanya Kulkarni  * as page_shift value. When calculating the ZASL use shift by 12.
15aaf2e048SChaitanya Kulkarni  */
16aaf2e048SChaitanya Kulkarni #define NVMET_MPSMIN_SHIFT	12
17aaf2e048SChaitanya Kulkarni 
18aaf2e048SChaitanya Kulkarni static inline u8 nvmet_zasl(unsigned int zone_append_sects)
19aaf2e048SChaitanya Kulkarni {
20aaf2e048SChaitanya Kulkarni 	/*
21aaf2e048SChaitanya Kulkarni 	 * Zone Append Size Limit (zasl) is expressed as a power of 2 value
22aaf2e048SChaitanya Kulkarni 	 * with the minimum memory page size (i.e. 12) as unit.
23aaf2e048SChaitanya Kulkarni 	 */
24aaf2e048SChaitanya Kulkarni 	return ilog2(zone_append_sects >> (NVMET_MPSMIN_SHIFT - 9));
25aaf2e048SChaitanya Kulkarni }
26aaf2e048SChaitanya Kulkarni 
27aaf2e048SChaitanya Kulkarni static int validate_conv_zones_cb(struct blk_zone *z,
28aaf2e048SChaitanya Kulkarni 				  unsigned int i, void *data)
29aaf2e048SChaitanya Kulkarni {
30aaf2e048SChaitanya Kulkarni 	if (z->type == BLK_ZONE_TYPE_CONVENTIONAL)
31aaf2e048SChaitanya Kulkarni 		return -EOPNOTSUPP;
32aaf2e048SChaitanya Kulkarni 	return 0;
33aaf2e048SChaitanya Kulkarni }
34aaf2e048SChaitanya Kulkarni 
35aaf2e048SChaitanya Kulkarni bool nvmet_bdev_zns_enable(struct nvmet_ns *ns)
36aaf2e048SChaitanya Kulkarni {
37aaf2e048SChaitanya Kulkarni 	struct request_queue *q = ns->bdev->bd_disk->queue;
38aaf2e048SChaitanya Kulkarni 	u8 zasl = nvmet_zasl(queue_max_zone_append_sectors(q));
39aaf2e048SChaitanya Kulkarni 	struct gendisk *bd_disk = ns->bdev->bd_disk;
40aaf2e048SChaitanya Kulkarni 	int ret;
41aaf2e048SChaitanya Kulkarni 
42aaf2e048SChaitanya Kulkarni 	if (ns->subsys->zasl) {
43aaf2e048SChaitanya Kulkarni 		if (ns->subsys->zasl > zasl)
44aaf2e048SChaitanya Kulkarni 			return false;
45aaf2e048SChaitanya Kulkarni 	}
46aaf2e048SChaitanya Kulkarni 	ns->subsys->zasl = zasl;
47aaf2e048SChaitanya Kulkarni 
48aaf2e048SChaitanya Kulkarni 	/*
49aaf2e048SChaitanya Kulkarni 	 * Generic zoned block devices may have a smaller last zone which is
50aaf2e048SChaitanya Kulkarni 	 * not supported by ZNS. Exclude zoned drives that have such smaller
51aaf2e048SChaitanya Kulkarni 	 * last zone.
52aaf2e048SChaitanya Kulkarni 	 */
53aaf2e048SChaitanya Kulkarni 	if (get_capacity(bd_disk) & (bdev_zone_sectors(ns->bdev) - 1))
54aaf2e048SChaitanya Kulkarni 		return false;
55aaf2e048SChaitanya Kulkarni 	/*
56aaf2e048SChaitanya Kulkarni 	 * ZNS does not define a conventional zone type. If the underlying
57aaf2e048SChaitanya Kulkarni 	 * device has a bitmap set indicating the existence of conventional
58aaf2e048SChaitanya Kulkarni 	 * zones, reject the device. Otherwise, use report zones to detect if
59aaf2e048SChaitanya Kulkarni 	 * the device has conventional zones.
60aaf2e048SChaitanya Kulkarni 	 */
61aaf2e048SChaitanya Kulkarni 	if (ns->bdev->bd_disk->queue->conv_zones_bitmap)
62aaf2e048SChaitanya Kulkarni 		return false;
63aaf2e048SChaitanya Kulkarni 
64aaf2e048SChaitanya Kulkarni 	ret = blkdev_report_zones(ns->bdev, 0, blkdev_nr_zones(bd_disk),
65aaf2e048SChaitanya Kulkarni 				  validate_conv_zones_cb, NULL);
66aaf2e048SChaitanya Kulkarni 	if (ret < 0)
67aaf2e048SChaitanya Kulkarni 		return false;
68aaf2e048SChaitanya Kulkarni 
69aaf2e048SChaitanya Kulkarni 	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
70aaf2e048SChaitanya Kulkarni 
71aaf2e048SChaitanya Kulkarni 	return true;
72aaf2e048SChaitanya Kulkarni }
73aaf2e048SChaitanya Kulkarni 
74aaf2e048SChaitanya Kulkarni void nvmet_execute_identify_cns_cs_ctrl(struct nvmet_req *req)
75aaf2e048SChaitanya Kulkarni {
76aaf2e048SChaitanya Kulkarni 	u8 zasl = req->sq->ctrl->subsys->zasl;
77aaf2e048SChaitanya Kulkarni 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
78aaf2e048SChaitanya Kulkarni 	struct nvme_id_ctrl_zns *id;
79aaf2e048SChaitanya Kulkarni 	u16 status;
80aaf2e048SChaitanya Kulkarni 
81aaf2e048SChaitanya Kulkarni 	id = kzalloc(sizeof(*id), GFP_KERNEL);
82aaf2e048SChaitanya Kulkarni 	if (!id) {
83aaf2e048SChaitanya Kulkarni 		status = NVME_SC_INTERNAL;
84aaf2e048SChaitanya Kulkarni 		goto out;
85aaf2e048SChaitanya Kulkarni 	}
86aaf2e048SChaitanya Kulkarni 
87aaf2e048SChaitanya Kulkarni 	if (ctrl->ops->get_mdts)
88aaf2e048SChaitanya Kulkarni 		id->zasl = min_t(u8, ctrl->ops->get_mdts(ctrl), zasl);
89aaf2e048SChaitanya Kulkarni 	else
90aaf2e048SChaitanya Kulkarni 		id->zasl = zasl;
91aaf2e048SChaitanya Kulkarni 
92aaf2e048SChaitanya Kulkarni 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
93aaf2e048SChaitanya Kulkarni 
94aaf2e048SChaitanya Kulkarni 	kfree(id);
95aaf2e048SChaitanya Kulkarni out:
96aaf2e048SChaitanya Kulkarni 	nvmet_req_complete(req, status);
97aaf2e048SChaitanya Kulkarni }
98aaf2e048SChaitanya Kulkarni 
99aaf2e048SChaitanya Kulkarni void nvmet_execute_identify_cns_cs_ns(struct nvmet_req *req)
100aaf2e048SChaitanya Kulkarni {
101aaf2e048SChaitanya Kulkarni 	struct nvme_id_ns_zns *id_zns;
102aaf2e048SChaitanya Kulkarni 	u64 zsze;
103aaf2e048SChaitanya Kulkarni 	u16 status;
104aaf2e048SChaitanya Kulkarni 
105aaf2e048SChaitanya Kulkarni 	if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
106aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_identify, nsid);
107aaf2e048SChaitanya Kulkarni 		status = NVME_SC_INVALID_NS | NVME_SC_DNR;
108aaf2e048SChaitanya Kulkarni 		goto out;
109aaf2e048SChaitanya Kulkarni 	}
110aaf2e048SChaitanya Kulkarni 
111aaf2e048SChaitanya Kulkarni 	id_zns = kzalloc(sizeof(*id_zns), GFP_KERNEL);
112aaf2e048SChaitanya Kulkarni 	if (!id_zns) {
113aaf2e048SChaitanya Kulkarni 		status = NVME_SC_INTERNAL;
114aaf2e048SChaitanya Kulkarni 		goto out;
115aaf2e048SChaitanya Kulkarni 	}
116aaf2e048SChaitanya Kulkarni 
117aaf2e048SChaitanya Kulkarni 	status = nvmet_req_find_ns(req);
118ad0e9a80SColin Ian King 	if (status)
119aaf2e048SChaitanya Kulkarni 		goto done;
120aaf2e048SChaitanya Kulkarni 
121aaf2e048SChaitanya Kulkarni 	if (!bdev_is_zoned(req->ns->bdev)) {
122aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_identify, nsid);
123aaf2e048SChaitanya Kulkarni 		goto done;
124aaf2e048SChaitanya Kulkarni 	}
125aaf2e048SChaitanya Kulkarni 
126*da783733SChristoph Hellwig 	if (nvmet_ns_revalidate(req->ns)) {
127*da783733SChristoph Hellwig 		mutex_lock(&req->ns->subsys->lock);
128*da783733SChristoph Hellwig 		nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
129*da783733SChristoph Hellwig 		mutex_unlock(&req->ns->subsys->lock);
130*da783733SChristoph Hellwig 	}
131aaf2e048SChaitanya Kulkarni 	zsze = (bdev_zone_sectors(req->ns->bdev) << 9) >>
132aaf2e048SChaitanya Kulkarni 					req->ns->blksize_shift;
133aaf2e048SChaitanya Kulkarni 	id_zns->lbafe[0].zsze = cpu_to_le64(zsze);
134aaf2e048SChaitanya Kulkarni 	id_zns->mor = cpu_to_le32(bdev_max_open_zones(req->ns->bdev));
135aaf2e048SChaitanya Kulkarni 	id_zns->mar = cpu_to_le32(bdev_max_active_zones(req->ns->bdev));
136aaf2e048SChaitanya Kulkarni 
137aaf2e048SChaitanya Kulkarni done:
138aaf2e048SChaitanya Kulkarni 	status = nvmet_copy_to_sgl(req, 0, id_zns, sizeof(*id_zns));
139aaf2e048SChaitanya Kulkarni 	kfree(id_zns);
140aaf2e048SChaitanya Kulkarni out:
141aaf2e048SChaitanya Kulkarni 	nvmet_req_complete(req, status);
142aaf2e048SChaitanya Kulkarni }
143aaf2e048SChaitanya Kulkarni 
144aaf2e048SChaitanya Kulkarni static u16 nvmet_bdev_validate_zone_mgmt_recv(struct nvmet_req *req)
145aaf2e048SChaitanya Kulkarni {
146aaf2e048SChaitanya Kulkarni 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
147aaf2e048SChaitanya Kulkarni 	u32 out_bufsize = (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2;
148aaf2e048SChaitanya Kulkarni 
149aaf2e048SChaitanya Kulkarni 	if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
150aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, slba);
151aaf2e048SChaitanya Kulkarni 		return NVME_SC_LBA_RANGE | NVME_SC_DNR;
152aaf2e048SChaitanya Kulkarni 	}
153aaf2e048SChaitanya Kulkarni 
154aaf2e048SChaitanya Kulkarni 	if (out_bufsize < sizeof(struct nvme_zone_report)) {
155aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, numd);
156aaf2e048SChaitanya Kulkarni 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
157aaf2e048SChaitanya Kulkarni 	}
158aaf2e048SChaitanya Kulkarni 
159aaf2e048SChaitanya Kulkarni 	if (req->cmd->zmr.zra != NVME_ZRA_ZONE_REPORT) {
160aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, zra);
161aaf2e048SChaitanya Kulkarni 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
162aaf2e048SChaitanya Kulkarni 	}
163aaf2e048SChaitanya Kulkarni 
164aaf2e048SChaitanya Kulkarni 	switch (req->cmd->zmr.pr) {
165aaf2e048SChaitanya Kulkarni 	case 0:
166aaf2e048SChaitanya Kulkarni 	case 1:
167aaf2e048SChaitanya Kulkarni 		break;
168aaf2e048SChaitanya Kulkarni 	default:
169aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, pr);
170aaf2e048SChaitanya Kulkarni 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
171aaf2e048SChaitanya Kulkarni 	}
172aaf2e048SChaitanya Kulkarni 
173aaf2e048SChaitanya Kulkarni 	switch (req->cmd->zmr.zrasf) {
174aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_REPORT_ALL:
175aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_STATE_EMPTY:
176aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_STATE_IMP_OPEN:
177aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_STATE_EXP_OPEN:
178aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_STATE_CLOSED:
179aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_STATE_FULL:
180aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_STATE_READONLY:
181aaf2e048SChaitanya Kulkarni 	case NVME_ZRASF_ZONE_STATE_OFFLINE:
182aaf2e048SChaitanya Kulkarni 		break;
183aaf2e048SChaitanya Kulkarni 	default:
184aaf2e048SChaitanya Kulkarni 		req->error_loc =
185aaf2e048SChaitanya Kulkarni 			offsetof(struct nvme_zone_mgmt_recv_cmd, zrasf);
186aaf2e048SChaitanya Kulkarni 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
187aaf2e048SChaitanya Kulkarni 	}
188aaf2e048SChaitanya Kulkarni 
189aaf2e048SChaitanya Kulkarni 	return NVME_SC_SUCCESS;
190aaf2e048SChaitanya Kulkarni }
191aaf2e048SChaitanya Kulkarni 
192aaf2e048SChaitanya Kulkarni struct nvmet_report_zone_data {
193aaf2e048SChaitanya Kulkarni 	struct nvmet_req *req;
194aaf2e048SChaitanya Kulkarni 	u64 out_buf_offset;
195aaf2e048SChaitanya Kulkarni 	u64 out_nr_zones;
196aaf2e048SChaitanya Kulkarni 	u64 nr_zones;
197aaf2e048SChaitanya Kulkarni 	u8 zrasf;
198aaf2e048SChaitanya Kulkarni };
199aaf2e048SChaitanya Kulkarni 
200aaf2e048SChaitanya Kulkarni static int nvmet_bdev_report_zone_cb(struct blk_zone *z, unsigned i, void *d)
201aaf2e048SChaitanya Kulkarni {
202aaf2e048SChaitanya Kulkarni 	static const unsigned int nvme_zrasf_to_blk_zcond[] = {
203aaf2e048SChaitanya Kulkarni 		[NVME_ZRASF_ZONE_STATE_EMPTY]	 = BLK_ZONE_COND_EMPTY,
204aaf2e048SChaitanya Kulkarni 		[NVME_ZRASF_ZONE_STATE_IMP_OPEN] = BLK_ZONE_COND_IMP_OPEN,
205aaf2e048SChaitanya Kulkarni 		[NVME_ZRASF_ZONE_STATE_EXP_OPEN] = BLK_ZONE_COND_EXP_OPEN,
206aaf2e048SChaitanya Kulkarni 		[NVME_ZRASF_ZONE_STATE_CLOSED]	 = BLK_ZONE_COND_CLOSED,
207aaf2e048SChaitanya Kulkarni 		[NVME_ZRASF_ZONE_STATE_READONLY] = BLK_ZONE_COND_READONLY,
208aaf2e048SChaitanya Kulkarni 		[NVME_ZRASF_ZONE_STATE_FULL]	 = BLK_ZONE_COND_FULL,
209aaf2e048SChaitanya Kulkarni 		[NVME_ZRASF_ZONE_STATE_OFFLINE]	 = BLK_ZONE_COND_OFFLINE,
210aaf2e048SChaitanya Kulkarni 	};
211aaf2e048SChaitanya Kulkarni 	struct nvmet_report_zone_data *rz = d;
212aaf2e048SChaitanya Kulkarni 
213aaf2e048SChaitanya Kulkarni 	if (rz->zrasf != NVME_ZRASF_ZONE_REPORT_ALL &&
214aaf2e048SChaitanya Kulkarni 	    z->cond != nvme_zrasf_to_blk_zcond[rz->zrasf])
215aaf2e048SChaitanya Kulkarni 		return 0;
216aaf2e048SChaitanya Kulkarni 
217aaf2e048SChaitanya Kulkarni 	if (rz->nr_zones < rz->out_nr_zones) {
218aaf2e048SChaitanya Kulkarni 		struct nvme_zone_descriptor zdesc = { };
219aaf2e048SChaitanya Kulkarni 		u16 status;
220aaf2e048SChaitanya Kulkarni 
221aaf2e048SChaitanya Kulkarni 		zdesc.zcap = nvmet_sect_to_lba(rz->req->ns, z->capacity);
222aaf2e048SChaitanya Kulkarni 		zdesc.zslba = nvmet_sect_to_lba(rz->req->ns, z->start);
223aaf2e048SChaitanya Kulkarni 		zdesc.wp = nvmet_sect_to_lba(rz->req->ns, z->wp);
224aaf2e048SChaitanya Kulkarni 		zdesc.za = z->reset ? 1 << 2 : 0;
225aaf2e048SChaitanya Kulkarni 		zdesc.zs = z->cond << 4;
226aaf2e048SChaitanya Kulkarni 		zdesc.zt = z->type;
227aaf2e048SChaitanya Kulkarni 
228aaf2e048SChaitanya Kulkarni 		status = nvmet_copy_to_sgl(rz->req, rz->out_buf_offset, &zdesc,
229aaf2e048SChaitanya Kulkarni 					   sizeof(zdesc));
230aaf2e048SChaitanya Kulkarni 		if (status)
231aaf2e048SChaitanya Kulkarni 			return -EINVAL;
232aaf2e048SChaitanya Kulkarni 
233aaf2e048SChaitanya Kulkarni 		rz->out_buf_offset += sizeof(zdesc);
234aaf2e048SChaitanya Kulkarni 	}
235aaf2e048SChaitanya Kulkarni 
236aaf2e048SChaitanya Kulkarni 	rz->nr_zones++;
237aaf2e048SChaitanya Kulkarni 
238aaf2e048SChaitanya Kulkarni 	return 0;
239aaf2e048SChaitanya Kulkarni }
240aaf2e048SChaitanya Kulkarni 
241aaf2e048SChaitanya Kulkarni static unsigned long nvmet_req_nr_zones_from_slba(struct nvmet_req *req)
242aaf2e048SChaitanya Kulkarni {
243aaf2e048SChaitanya Kulkarni 	unsigned int sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
244aaf2e048SChaitanya Kulkarni 
245aaf2e048SChaitanya Kulkarni 	return blkdev_nr_zones(req->ns->bdev->bd_disk) -
246aaf2e048SChaitanya Kulkarni 		(sect >> ilog2(bdev_zone_sectors(req->ns->bdev)));
247aaf2e048SChaitanya Kulkarni }
248aaf2e048SChaitanya Kulkarni 
249aaf2e048SChaitanya Kulkarni static unsigned long get_nr_zones_from_buf(struct nvmet_req *req, u32 bufsize)
250aaf2e048SChaitanya Kulkarni {
251aaf2e048SChaitanya Kulkarni 	if (bufsize <= sizeof(struct nvme_zone_report))
252aaf2e048SChaitanya Kulkarni 		return 0;
253aaf2e048SChaitanya Kulkarni 
254aaf2e048SChaitanya Kulkarni 	return (bufsize - sizeof(struct nvme_zone_report)) /
255aaf2e048SChaitanya Kulkarni 		sizeof(struct nvme_zone_descriptor);
256aaf2e048SChaitanya Kulkarni }
257aaf2e048SChaitanya Kulkarni 
258aaf2e048SChaitanya Kulkarni static void nvmet_bdev_zone_zmgmt_recv_work(struct work_struct *w)
259aaf2e048SChaitanya Kulkarni {
260aaf2e048SChaitanya Kulkarni 	struct nvmet_req *req = container_of(w, struct nvmet_req, z.zmgmt_work);
261aaf2e048SChaitanya Kulkarni 	sector_t start_sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
262aaf2e048SChaitanya Kulkarni 	unsigned long req_slba_nr_zones = nvmet_req_nr_zones_from_slba(req);
263aaf2e048SChaitanya Kulkarni 	u32 out_bufsize = (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2;
264aaf2e048SChaitanya Kulkarni 	__le64 nr_zones;
265aaf2e048SChaitanya Kulkarni 	u16 status;
266aaf2e048SChaitanya Kulkarni 	int ret;
267aaf2e048SChaitanya Kulkarni 	struct nvmet_report_zone_data rz_data = {
268aaf2e048SChaitanya Kulkarni 		.out_nr_zones = get_nr_zones_from_buf(req, out_bufsize),
269aaf2e048SChaitanya Kulkarni 		/* leave the place for report zone header */
270aaf2e048SChaitanya Kulkarni 		.out_buf_offset = sizeof(struct nvme_zone_report),
271aaf2e048SChaitanya Kulkarni 		.zrasf = req->cmd->zmr.zrasf,
272aaf2e048SChaitanya Kulkarni 		.nr_zones = 0,
273aaf2e048SChaitanya Kulkarni 		.req = req,
274aaf2e048SChaitanya Kulkarni 	};
275aaf2e048SChaitanya Kulkarni 
276aaf2e048SChaitanya Kulkarni 	status = nvmet_bdev_validate_zone_mgmt_recv(req);
277aaf2e048SChaitanya Kulkarni 	if (status)
278aaf2e048SChaitanya Kulkarni 		goto out;
279aaf2e048SChaitanya Kulkarni 
280aaf2e048SChaitanya Kulkarni 	if (!req_slba_nr_zones) {
281aaf2e048SChaitanya Kulkarni 		status = NVME_SC_SUCCESS;
282aaf2e048SChaitanya Kulkarni 		goto out;
283aaf2e048SChaitanya Kulkarni 	}
284aaf2e048SChaitanya Kulkarni 
285aaf2e048SChaitanya Kulkarni 	ret = blkdev_report_zones(req->ns->bdev, start_sect, req_slba_nr_zones,
286aaf2e048SChaitanya Kulkarni 				 nvmet_bdev_report_zone_cb, &rz_data);
287aaf2e048SChaitanya Kulkarni 	if (ret < 0) {
288aaf2e048SChaitanya Kulkarni 		status = NVME_SC_INTERNAL;
289aaf2e048SChaitanya Kulkarni 		goto out;
290aaf2e048SChaitanya Kulkarni 	}
291aaf2e048SChaitanya Kulkarni 
292aaf2e048SChaitanya Kulkarni 	/*
293aaf2e048SChaitanya Kulkarni 	 * When partial bit is set nr_zones must indicate the number of zone
294aaf2e048SChaitanya Kulkarni 	 * descriptors actually transferred.
295aaf2e048SChaitanya Kulkarni 	 */
296aaf2e048SChaitanya Kulkarni 	if (req->cmd->zmr.pr)
297aaf2e048SChaitanya Kulkarni 		rz_data.nr_zones = min(rz_data.nr_zones, rz_data.out_nr_zones);
298aaf2e048SChaitanya Kulkarni 
299aaf2e048SChaitanya Kulkarni 	nr_zones = cpu_to_le64(rz_data.nr_zones);
300aaf2e048SChaitanya Kulkarni 	status = nvmet_copy_to_sgl(req, 0, &nr_zones, sizeof(nr_zones));
301aaf2e048SChaitanya Kulkarni 
302aaf2e048SChaitanya Kulkarni out:
303aaf2e048SChaitanya Kulkarni 	nvmet_req_complete(req, status);
304aaf2e048SChaitanya Kulkarni }
305aaf2e048SChaitanya Kulkarni 
306aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req)
307aaf2e048SChaitanya Kulkarni {
308aaf2e048SChaitanya Kulkarni 	INIT_WORK(&req->z.zmgmt_work, nvmet_bdev_zone_zmgmt_recv_work);
309aaf2e048SChaitanya Kulkarni 	queue_work(zbd_wq, &req->z.zmgmt_work);
310aaf2e048SChaitanya Kulkarni }
311aaf2e048SChaitanya Kulkarni 
312aaf2e048SChaitanya Kulkarni static inline enum req_opf zsa_req_op(u8 zsa)
313aaf2e048SChaitanya Kulkarni {
314aaf2e048SChaitanya Kulkarni 	switch (zsa) {
315aaf2e048SChaitanya Kulkarni 	case NVME_ZONE_OPEN:
316aaf2e048SChaitanya Kulkarni 		return REQ_OP_ZONE_OPEN;
317aaf2e048SChaitanya Kulkarni 	case NVME_ZONE_CLOSE:
318aaf2e048SChaitanya Kulkarni 		return REQ_OP_ZONE_CLOSE;
319aaf2e048SChaitanya Kulkarni 	case NVME_ZONE_FINISH:
320aaf2e048SChaitanya Kulkarni 		return REQ_OP_ZONE_FINISH;
321aaf2e048SChaitanya Kulkarni 	case NVME_ZONE_RESET:
322aaf2e048SChaitanya Kulkarni 		return REQ_OP_ZONE_RESET;
323aaf2e048SChaitanya Kulkarni 	default:
324aaf2e048SChaitanya Kulkarni 		return REQ_OP_LAST;
325aaf2e048SChaitanya Kulkarni 	}
326aaf2e048SChaitanya Kulkarni }
327aaf2e048SChaitanya Kulkarni 
328aaf2e048SChaitanya Kulkarni static u16 blkdev_zone_mgmt_errno_to_nvme_status(int ret)
329aaf2e048SChaitanya Kulkarni {
330aaf2e048SChaitanya Kulkarni 	switch (ret) {
331aaf2e048SChaitanya Kulkarni 	case 0:
332aaf2e048SChaitanya Kulkarni 		return NVME_SC_SUCCESS;
333aaf2e048SChaitanya Kulkarni 	case -EINVAL:
334aaf2e048SChaitanya Kulkarni 	case -EIO:
335aaf2e048SChaitanya Kulkarni 		return NVME_SC_ZONE_INVALID_TRANSITION | NVME_SC_DNR;
336aaf2e048SChaitanya Kulkarni 	default:
337aaf2e048SChaitanya Kulkarni 		return NVME_SC_INTERNAL;
338aaf2e048SChaitanya Kulkarni 	}
339aaf2e048SChaitanya Kulkarni }
340aaf2e048SChaitanya Kulkarni 
341aaf2e048SChaitanya Kulkarni struct nvmet_zone_mgmt_send_all_data {
342aaf2e048SChaitanya Kulkarni 	unsigned long *zbitmap;
343aaf2e048SChaitanya Kulkarni 	struct nvmet_req *req;
344aaf2e048SChaitanya Kulkarni };
345aaf2e048SChaitanya Kulkarni 
346aaf2e048SChaitanya Kulkarni static int zmgmt_send_scan_cb(struct blk_zone *z, unsigned i, void *d)
347aaf2e048SChaitanya Kulkarni {
348aaf2e048SChaitanya Kulkarni 	struct nvmet_zone_mgmt_send_all_data *data = d;
349aaf2e048SChaitanya Kulkarni 
350aaf2e048SChaitanya Kulkarni 	switch (zsa_req_op(data->req->cmd->zms.zsa)) {
351aaf2e048SChaitanya Kulkarni 	case REQ_OP_ZONE_OPEN:
352aaf2e048SChaitanya Kulkarni 		switch (z->cond) {
353aaf2e048SChaitanya Kulkarni 		case BLK_ZONE_COND_CLOSED:
354aaf2e048SChaitanya Kulkarni 			break;
355aaf2e048SChaitanya Kulkarni 		default:
356aaf2e048SChaitanya Kulkarni 			return 0;
357aaf2e048SChaitanya Kulkarni 		}
358aaf2e048SChaitanya Kulkarni 		break;
359aaf2e048SChaitanya Kulkarni 	case REQ_OP_ZONE_CLOSE:
360aaf2e048SChaitanya Kulkarni 		switch (z->cond) {
361aaf2e048SChaitanya Kulkarni 		case BLK_ZONE_COND_IMP_OPEN:
362aaf2e048SChaitanya Kulkarni 		case BLK_ZONE_COND_EXP_OPEN:
363aaf2e048SChaitanya Kulkarni 			break;
364aaf2e048SChaitanya Kulkarni 		default:
365aaf2e048SChaitanya Kulkarni 			return 0;
366aaf2e048SChaitanya Kulkarni 		}
367aaf2e048SChaitanya Kulkarni 		break;
368aaf2e048SChaitanya Kulkarni 	case REQ_OP_ZONE_FINISH:
369aaf2e048SChaitanya Kulkarni 		switch (z->cond) {
370aaf2e048SChaitanya Kulkarni 		case BLK_ZONE_COND_IMP_OPEN:
371aaf2e048SChaitanya Kulkarni 		case BLK_ZONE_COND_EXP_OPEN:
372aaf2e048SChaitanya Kulkarni 		case BLK_ZONE_COND_CLOSED:
373aaf2e048SChaitanya Kulkarni 			break;
374aaf2e048SChaitanya Kulkarni 		default:
375aaf2e048SChaitanya Kulkarni 			return 0;
376aaf2e048SChaitanya Kulkarni 		}
377aaf2e048SChaitanya Kulkarni 		break;
378aaf2e048SChaitanya Kulkarni 	default:
379aaf2e048SChaitanya Kulkarni 		return -EINVAL;
380aaf2e048SChaitanya Kulkarni 	}
381aaf2e048SChaitanya Kulkarni 
382aaf2e048SChaitanya Kulkarni 	set_bit(i, data->zbitmap);
383aaf2e048SChaitanya Kulkarni 
384aaf2e048SChaitanya Kulkarni 	return 0;
385aaf2e048SChaitanya Kulkarni }
386aaf2e048SChaitanya Kulkarni 
387aaf2e048SChaitanya Kulkarni static u16 nvmet_bdev_zone_mgmt_emulate_all(struct nvmet_req *req)
388aaf2e048SChaitanya Kulkarni {
389aaf2e048SChaitanya Kulkarni 	struct block_device *bdev = req->ns->bdev;
390aaf2e048SChaitanya Kulkarni 	unsigned int nr_zones = blkdev_nr_zones(bdev->bd_disk);
391aaf2e048SChaitanya Kulkarni 	struct request_queue *q = bdev_get_queue(bdev);
392aaf2e048SChaitanya Kulkarni 	struct bio *bio = NULL;
393aaf2e048SChaitanya Kulkarni 	sector_t sector = 0;
394aaf2e048SChaitanya Kulkarni 	int ret;
395aaf2e048SChaitanya Kulkarni 	struct nvmet_zone_mgmt_send_all_data d = {
396aaf2e048SChaitanya Kulkarni 		.req = req,
397aaf2e048SChaitanya Kulkarni 	};
398aaf2e048SChaitanya Kulkarni 
399aaf2e048SChaitanya Kulkarni 	d.zbitmap = kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(*(d.zbitmap)),
400aaf2e048SChaitanya Kulkarni 				 GFP_NOIO, q->node);
401aaf2e048SChaitanya Kulkarni 	if (!d.zbitmap) {
402aaf2e048SChaitanya Kulkarni 		ret = -ENOMEM;
403aaf2e048SChaitanya Kulkarni 		goto out;
404aaf2e048SChaitanya Kulkarni 	}
405aaf2e048SChaitanya Kulkarni 
406aaf2e048SChaitanya Kulkarni 	/* Scan and build bitmap of the eligible zones */
407aaf2e048SChaitanya Kulkarni 	ret = blkdev_report_zones(bdev, 0, nr_zones, zmgmt_send_scan_cb, &d);
408aaf2e048SChaitanya Kulkarni 	if (ret != nr_zones) {
409aaf2e048SChaitanya Kulkarni 		if (ret > 0)
410aaf2e048SChaitanya Kulkarni 			ret = -EIO;
411aaf2e048SChaitanya Kulkarni 		goto out;
412aaf2e048SChaitanya Kulkarni 	} else {
413aaf2e048SChaitanya Kulkarni 		/* We scanned all the zones */
414aaf2e048SChaitanya Kulkarni 		ret = 0;
415aaf2e048SChaitanya Kulkarni 	}
416aaf2e048SChaitanya Kulkarni 
417aaf2e048SChaitanya Kulkarni 	while (sector < get_capacity(bdev->bd_disk)) {
418aaf2e048SChaitanya Kulkarni 		if (test_bit(blk_queue_zone_no(q, sector), d.zbitmap)) {
4190a3140eaSChaitanya Kulkarni 			bio = blk_next_bio(bio, bdev, 0,
4200a3140eaSChaitanya Kulkarni 				zsa_req_op(req->cmd->zms.zsa) | REQ_SYNC,
4210a3140eaSChaitanya Kulkarni 				GFP_KERNEL);
422aaf2e048SChaitanya Kulkarni 			bio->bi_iter.bi_sector = sector;
423aaf2e048SChaitanya Kulkarni 			/* This may take a while, so be nice to others */
424aaf2e048SChaitanya Kulkarni 			cond_resched();
425aaf2e048SChaitanya Kulkarni 		}
426aaf2e048SChaitanya Kulkarni 		sector += blk_queue_zone_sectors(q);
427aaf2e048SChaitanya Kulkarni 	}
428aaf2e048SChaitanya Kulkarni 
429aaf2e048SChaitanya Kulkarni 	if (bio) {
430aaf2e048SChaitanya Kulkarni 		ret = submit_bio_wait(bio);
431aaf2e048SChaitanya Kulkarni 		bio_put(bio);
432aaf2e048SChaitanya Kulkarni 	}
433aaf2e048SChaitanya Kulkarni 
434aaf2e048SChaitanya Kulkarni out:
435aaf2e048SChaitanya Kulkarni 	kfree(d.zbitmap);
436aaf2e048SChaitanya Kulkarni 
437aaf2e048SChaitanya Kulkarni 	return blkdev_zone_mgmt_errno_to_nvme_status(ret);
438aaf2e048SChaitanya Kulkarni }
439aaf2e048SChaitanya Kulkarni 
440aaf2e048SChaitanya Kulkarni static u16 nvmet_bdev_execute_zmgmt_send_all(struct nvmet_req *req)
441aaf2e048SChaitanya Kulkarni {
442aaf2e048SChaitanya Kulkarni 	int ret;
443aaf2e048SChaitanya Kulkarni 
444aaf2e048SChaitanya Kulkarni 	switch (zsa_req_op(req->cmd->zms.zsa)) {
445aaf2e048SChaitanya Kulkarni 	case REQ_OP_ZONE_RESET:
446aaf2e048SChaitanya Kulkarni 		ret = blkdev_zone_mgmt(req->ns->bdev, REQ_OP_ZONE_RESET, 0,
447aaf2e048SChaitanya Kulkarni 				       get_capacity(req->ns->bdev->bd_disk),
448aaf2e048SChaitanya Kulkarni 				       GFP_KERNEL);
449aaf2e048SChaitanya Kulkarni 		if (ret < 0)
450aaf2e048SChaitanya Kulkarni 			return blkdev_zone_mgmt_errno_to_nvme_status(ret);
451aaf2e048SChaitanya Kulkarni 		break;
452aaf2e048SChaitanya Kulkarni 	case REQ_OP_ZONE_OPEN:
453aaf2e048SChaitanya Kulkarni 	case REQ_OP_ZONE_CLOSE:
454aaf2e048SChaitanya Kulkarni 	case REQ_OP_ZONE_FINISH:
455aaf2e048SChaitanya Kulkarni 		return nvmet_bdev_zone_mgmt_emulate_all(req);
456aaf2e048SChaitanya Kulkarni 	default:
457aaf2e048SChaitanya Kulkarni 		/* this is needed to quiet compiler warning */
458aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, zsa);
459aaf2e048SChaitanya Kulkarni 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
460aaf2e048SChaitanya Kulkarni 	}
461aaf2e048SChaitanya Kulkarni 
462aaf2e048SChaitanya Kulkarni 	return NVME_SC_SUCCESS;
463aaf2e048SChaitanya Kulkarni }
464aaf2e048SChaitanya Kulkarni 
465aaf2e048SChaitanya Kulkarni static void nvmet_bdev_zmgmt_send_work(struct work_struct *w)
466aaf2e048SChaitanya Kulkarni {
467aaf2e048SChaitanya Kulkarni 	struct nvmet_req *req = container_of(w, struct nvmet_req, z.zmgmt_work);
468aaf2e048SChaitanya Kulkarni 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zms.slba);
469aaf2e048SChaitanya Kulkarni 	enum req_opf op = zsa_req_op(req->cmd->zms.zsa);
470aaf2e048SChaitanya Kulkarni 	struct block_device *bdev = req->ns->bdev;
471aaf2e048SChaitanya Kulkarni 	sector_t zone_sectors = bdev_zone_sectors(bdev);
472aaf2e048SChaitanya Kulkarni 	u16 status = NVME_SC_SUCCESS;
473aaf2e048SChaitanya Kulkarni 	int ret;
474aaf2e048SChaitanya Kulkarni 
475aaf2e048SChaitanya Kulkarni 	if (op == REQ_OP_LAST) {
476aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, zsa);
477aaf2e048SChaitanya Kulkarni 		status = NVME_SC_ZONE_INVALID_TRANSITION | NVME_SC_DNR;
478aaf2e048SChaitanya Kulkarni 		goto out;
479aaf2e048SChaitanya Kulkarni 	}
480aaf2e048SChaitanya Kulkarni 
481aaf2e048SChaitanya Kulkarni 	/* when select all bit is set slba field is ignored */
482aaf2e048SChaitanya Kulkarni 	if (req->cmd->zms.select_all) {
483aaf2e048SChaitanya Kulkarni 		status = nvmet_bdev_execute_zmgmt_send_all(req);
484aaf2e048SChaitanya Kulkarni 		goto out;
485aaf2e048SChaitanya Kulkarni 	}
486aaf2e048SChaitanya Kulkarni 
487aaf2e048SChaitanya Kulkarni 	if (sect >= get_capacity(bdev->bd_disk)) {
488aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, slba);
489aaf2e048SChaitanya Kulkarni 		status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
490aaf2e048SChaitanya Kulkarni 		goto out;
491aaf2e048SChaitanya Kulkarni 	}
492aaf2e048SChaitanya Kulkarni 
493aaf2e048SChaitanya Kulkarni 	if (sect & (zone_sectors - 1)) {
494aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, slba);
495aaf2e048SChaitanya Kulkarni 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
496aaf2e048SChaitanya Kulkarni 		goto out;
497aaf2e048SChaitanya Kulkarni 	}
498aaf2e048SChaitanya Kulkarni 
499aaf2e048SChaitanya Kulkarni 	ret = blkdev_zone_mgmt(bdev, op, sect, zone_sectors, GFP_KERNEL);
500aaf2e048SChaitanya Kulkarni 	if (ret < 0)
501aaf2e048SChaitanya Kulkarni 		status = blkdev_zone_mgmt_errno_to_nvme_status(ret);
502aaf2e048SChaitanya Kulkarni 
503aaf2e048SChaitanya Kulkarni out:
504aaf2e048SChaitanya Kulkarni 	nvmet_req_complete(req, status);
505aaf2e048SChaitanya Kulkarni }
506aaf2e048SChaitanya Kulkarni 
507aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req)
508aaf2e048SChaitanya Kulkarni {
509aaf2e048SChaitanya Kulkarni 	INIT_WORK(&req->z.zmgmt_work, nvmet_bdev_zmgmt_send_work);
510aaf2e048SChaitanya Kulkarni 	queue_work(zbd_wq, &req->z.zmgmt_work);
511aaf2e048SChaitanya Kulkarni }
512aaf2e048SChaitanya Kulkarni 
513aaf2e048SChaitanya Kulkarni static void nvmet_bdev_zone_append_bio_done(struct bio *bio)
514aaf2e048SChaitanya Kulkarni {
515aaf2e048SChaitanya Kulkarni 	struct nvmet_req *req = bio->bi_private;
516aaf2e048SChaitanya Kulkarni 
517aaf2e048SChaitanya Kulkarni 	if (bio->bi_status == BLK_STS_OK) {
518aaf2e048SChaitanya Kulkarni 		req->cqe->result.u64 =
519aaf2e048SChaitanya Kulkarni 			nvmet_sect_to_lba(req->ns, bio->bi_iter.bi_sector);
520aaf2e048SChaitanya Kulkarni 	}
521aaf2e048SChaitanya Kulkarni 
522aaf2e048SChaitanya Kulkarni 	nvmet_req_complete(req, blk_to_nvme_status(req, bio->bi_status));
523aaf2e048SChaitanya Kulkarni 	nvmet_req_bio_put(req, bio);
524aaf2e048SChaitanya Kulkarni }
525aaf2e048SChaitanya Kulkarni 
526aaf2e048SChaitanya Kulkarni void nvmet_bdev_execute_zone_append(struct nvmet_req *req)
527aaf2e048SChaitanya Kulkarni {
528aaf2e048SChaitanya Kulkarni 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->rw.slba);
52907888c66SChristoph Hellwig 	const unsigned int op = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
530aaf2e048SChaitanya Kulkarni 	u16 status = NVME_SC_SUCCESS;
531aaf2e048SChaitanya Kulkarni 	unsigned int total_len = 0;
532aaf2e048SChaitanya Kulkarni 	struct scatterlist *sg;
533aaf2e048SChaitanya Kulkarni 	struct bio *bio;
534aaf2e048SChaitanya Kulkarni 	int sg_cnt;
535aaf2e048SChaitanya Kulkarni 
536aaf2e048SChaitanya Kulkarni 	/* Request is completed on len mismatch in nvmet_check_transter_len() */
537aaf2e048SChaitanya Kulkarni 	if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req)))
538aaf2e048SChaitanya Kulkarni 		return;
539aaf2e048SChaitanya Kulkarni 
540aaf2e048SChaitanya Kulkarni 	if (!req->sg_cnt) {
541aaf2e048SChaitanya Kulkarni 		nvmet_req_complete(req, 0);
542aaf2e048SChaitanya Kulkarni 		return;
543aaf2e048SChaitanya Kulkarni 	}
544aaf2e048SChaitanya Kulkarni 
545aaf2e048SChaitanya Kulkarni 	if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
546aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_rw_command, slba);
547aaf2e048SChaitanya Kulkarni 		status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
548aaf2e048SChaitanya Kulkarni 		goto out;
549aaf2e048SChaitanya Kulkarni 	}
550aaf2e048SChaitanya Kulkarni 
551aaf2e048SChaitanya Kulkarni 	if (sect & (bdev_zone_sectors(req->ns->bdev) - 1)) {
552aaf2e048SChaitanya Kulkarni 		req->error_loc = offsetof(struct nvme_rw_command, slba);
553aaf2e048SChaitanya Kulkarni 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
554aaf2e048SChaitanya Kulkarni 		goto out;
555aaf2e048SChaitanya Kulkarni 	}
556aaf2e048SChaitanya Kulkarni 
557aaf2e048SChaitanya Kulkarni 	if (nvmet_use_inline_bvec(req)) {
558aaf2e048SChaitanya Kulkarni 		bio = &req->z.inline_bio;
55949add496SChristoph Hellwig 		bio_init(bio, req->ns->bdev, req->inline_bvec,
56049add496SChristoph Hellwig 			 ARRAY_SIZE(req->inline_bvec), op);
561aaf2e048SChaitanya Kulkarni 	} else {
56207888c66SChristoph Hellwig 		bio = bio_alloc(req->ns->bdev, req->sg_cnt, op, GFP_KERNEL);
563aaf2e048SChaitanya Kulkarni 	}
564aaf2e048SChaitanya Kulkarni 
565aaf2e048SChaitanya Kulkarni 	bio->bi_end_io = nvmet_bdev_zone_append_bio_done;
566aaf2e048SChaitanya Kulkarni 	bio->bi_iter.bi_sector = sect;
567aaf2e048SChaitanya Kulkarni 	bio->bi_private = req;
568aaf2e048SChaitanya Kulkarni 	if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
569aaf2e048SChaitanya Kulkarni 		bio->bi_opf |= REQ_FUA;
570aaf2e048SChaitanya Kulkarni 
571aaf2e048SChaitanya Kulkarni 	for_each_sg(req->sg, sg, req->sg_cnt, sg_cnt) {
572aaf2e048SChaitanya Kulkarni 		struct page *p = sg_page(sg);
573aaf2e048SChaitanya Kulkarni 		unsigned int l = sg->length;
574aaf2e048SChaitanya Kulkarni 		unsigned int o = sg->offset;
575aaf2e048SChaitanya Kulkarni 		unsigned int ret;
576aaf2e048SChaitanya Kulkarni 
577aaf2e048SChaitanya Kulkarni 		ret = bio_add_zone_append_page(bio, p, l, o);
578aaf2e048SChaitanya Kulkarni 		if (ret != sg->length) {
579aaf2e048SChaitanya Kulkarni 			status = NVME_SC_INTERNAL;
580aaf2e048SChaitanya Kulkarni 			goto out_put_bio;
581aaf2e048SChaitanya Kulkarni 		}
582aaf2e048SChaitanya Kulkarni 		total_len += sg->length;
583aaf2e048SChaitanya Kulkarni 	}
584aaf2e048SChaitanya Kulkarni 
585aaf2e048SChaitanya Kulkarni 	if (total_len != nvmet_rw_data_len(req)) {
586aaf2e048SChaitanya Kulkarni 		status = NVME_SC_INTERNAL | NVME_SC_DNR;
587aaf2e048SChaitanya Kulkarni 		goto out_put_bio;
588aaf2e048SChaitanya Kulkarni 	}
589aaf2e048SChaitanya Kulkarni 
590aaf2e048SChaitanya Kulkarni 	submit_bio(bio);
591aaf2e048SChaitanya Kulkarni 	return;
592aaf2e048SChaitanya Kulkarni 
593aaf2e048SChaitanya Kulkarni out_put_bio:
594aaf2e048SChaitanya Kulkarni 	nvmet_req_bio_put(req, bio);
595aaf2e048SChaitanya Kulkarni out:
596aaf2e048SChaitanya Kulkarni 	nvmet_req_complete(req, status);
597aaf2e048SChaitanya Kulkarni }
598aaf2e048SChaitanya Kulkarni 
599aaf2e048SChaitanya Kulkarni u16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req)
600aaf2e048SChaitanya Kulkarni {
601aaf2e048SChaitanya Kulkarni 	struct nvme_command *cmd = req->cmd;
602aaf2e048SChaitanya Kulkarni 
603aaf2e048SChaitanya Kulkarni 	switch (cmd->common.opcode) {
604aaf2e048SChaitanya Kulkarni 	case nvme_cmd_zone_append:
605aaf2e048SChaitanya Kulkarni 		req->execute = nvmet_bdev_execute_zone_append;
606aaf2e048SChaitanya Kulkarni 		return 0;
607aaf2e048SChaitanya Kulkarni 	case nvme_cmd_zone_mgmt_recv:
608aaf2e048SChaitanya Kulkarni 		req->execute = nvmet_bdev_execute_zone_mgmt_recv;
609aaf2e048SChaitanya Kulkarni 		return 0;
610aaf2e048SChaitanya Kulkarni 	case nvme_cmd_zone_mgmt_send:
611aaf2e048SChaitanya Kulkarni 		req->execute = nvmet_bdev_execute_zone_mgmt_send;
612aaf2e048SChaitanya Kulkarni 		return 0;
613aaf2e048SChaitanya Kulkarni 	default:
614aaf2e048SChaitanya Kulkarni 		return nvmet_bdev_parse_io_cmd(req);
615aaf2e048SChaitanya Kulkarni 	}
616aaf2e048SChaitanya Kulkarni }
617