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