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