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