Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2020 Western Digital Corporation or its affiliates.
0004  */
0005 
0006 #include <linux/blkdev.h>
0007 #include <linux/vmalloc.h>
0008 #include "nvme.h"
0009 
0010 int nvme_revalidate_zones(struct nvme_ns *ns)
0011 {
0012     struct request_queue *q = ns->queue;
0013     int ret;
0014 
0015     ret = blk_revalidate_disk_zones(ns->disk, NULL);
0016     if (!ret)
0017         blk_queue_max_zone_append_sectors(q, ns->ctrl->max_zone_append);
0018     return ret;
0019 }
0020 
0021 static int nvme_set_max_append(struct nvme_ctrl *ctrl)
0022 {
0023     struct nvme_command c = { };
0024     struct nvme_id_ctrl_zns *id;
0025     int status;
0026 
0027     id = kzalloc(sizeof(*id), GFP_KERNEL);
0028     if (!id)
0029         return -ENOMEM;
0030 
0031     c.identify.opcode = nvme_admin_identify;
0032     c.identify.cns = NVME_ID_CNS_CS_CTRL;
0033     c.identify.csi = NVME_CSI_ZNS;
0034 
0035     status = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
0036     if (status) {
0037         kfree(id);
0038         return status;
0039     }
0040 
0041     if (id->zasl)
0042         ctrl->max_zone_append = 1 << (id->zasl + 3);
0043     else
0044         ctrl->max_zone_append = ctrl->max_hw_sectors;
0045     kfree(id);
0046     return 0;
0047 }
0048 
0049 int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
0050 {
0051     struct nvme_effects_log *log = ns->head->effects;
0052     struct request_queue *q = ns->queue;
0053     struct nvme_command c = { };
0054     struct nvme_id_ns_zns *id;
0055     int status;
0056 
0057     /* Driver requires zone append support */
0058     if ((le32_to_cpu(log->iocs[nvme_cmd_zone_append]) &
0059             NVME_CMD_EFFECTS_CSUPP)) {
0060         if (test_and_clear_bit(NVME_NS_FORCE_RO, &ns->flags))
0061             dev_warn(ns->ctrl->device,
0062                  "Zone Append supported for zoned namespace:%d. Remove read-only mode\n",
0063                  ns->head->ns_id);
0064     } else {
0065         set_bit(NVME_NS_FORCE_RO, &ns->flags);
0066         dev_warn(ns->ctrl->device,
0067              "Zone Append not supported for zoned namespace:%d. Forcing to read-only mode\n",
0068              ns->head->ns_id);
0069     }
0070 
0071     /* Lazily query controller append limit for the first zoned namespace */
0072     if (!ns->ctrl->max_zone_append) {
0073         status = nvme_set_max_append(ns->ctrl);
0074         if (status)
0075             return status;
0076     }
0077 
0078     id = kzalloc(sizeof(*id), GFP_KERNEL);
0079     if (!id)
0080         return -ENOMEM;
0081 
0082     c.identify.opcode = nvme_admin_identify;
0083     c.identify.nsid = cpu_to_le32(ns->head->ns_id);
0084     c.identify.cns = NVME_ID_CNS_CS_NS;
0085     c.identify.csi = NVME_CSI_ZNS;
0086 
0087     status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, id, sizeof(*id));
0088     if (status)
0089         goto free_data;
0090 
0091     /*
0092      * We currently do not handle devices requiring any of the zoned
0093      * operation characteristics.
0094      */
0095     if (id->zoc) {
0096         dev_warn(ns->ctrl->device,
0097             "zone operations:%x not supported for namespace:%u\n",
0098             le16_to_cpu(id->zoc), ns->head->ns_id);
0099         status = -ENODEV;
0100         goto free_data;
0101     }
0102 
0103     ns->zsze = nvme_lba_to_sect(ns, le64_to_cpu(id->lbafe[lbaf].zsze));
0104     if (!is_power_of_2(ns->zsze)) {
0105         dev_warn(ns->ctrl->device,
0106             "invalid zone size:%llu for namespace:%u\n",
0107             ns->zsze, ns->head->ns_id);
0108         status = -ENODEV;
0109         goto free_data;
0110     }
0111 
0112     disk_set_zoned(ns->disk, BLK_ZONED_HM);
0113     blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
0114     disk_set_max_open_zones(ns->disk, le32_to_cpu(id->mor) + 1);
0115     disk_set_max_active_zones(ns->disk, le32_to_cpu(id->mar) + 1);
0116 free_data:
0117     kfree(id);
0118     return status;
0119 }
0120 
0121 static void *nvme_zns_alloc_report_buffer(struct nvme_ns *ns,
0122                       unsigned int nr_zones, size_t *buflen)
0123 {
0124     struct request_queue *q = ns->disk->queue;
0125     size_t bufsize;
0126     void *buf;
0127 
0128     const size_t min_bufsize = sizeof(struct nvme_zone_report) +
0129                    sizeof(struct nvme_zone_descriptor);
0130 
0131     nr_zones = min_t(unsigned int, nr_zones,
0132              get_capacity(ns->disk) >> ilog2(ns->zsze));
0133 
0134     bufsize = sizeof(struct nvme_zone_report) +
0135         nr_zones * sizeof(struct nvme_zone_descriptor);
0136     bufsize = min_t(size_t, bufsize,
0137             queue_max_hw_sectors(q) << SECTOR_SHIFT);
0138     bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
0139 
0140     while (bufsize >= min_bufsize) {
0141         buf = __vmalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
0142         if (buf) {
0143             *buflen = bufsize;
0144             return buf;
0145         }
0146         bufsize >>= 1;
0147     }
0148     return NULL;
0149 }
0150 
0151 static int nvme_zone_parse_entry(struct nvme_ns *ns,
0152                  struct nvme_zone_descriptor *entry,
0153                  unsigned int idx, report_zones_cb cb,
0154                  void *data)
0155 {
0156     struct blk_zone zone = { };
0157 
0158     if ((entry->zt & 0xf) != NVME_ZONE_TYPE_SEQWRITE_REQ) {
0159         dev_err(ns->ctrl->device, "invalid zone type %#x\n",
0160                 entry->zt);
0161         return -EINVAL;
0162     }
0163 
0164     zone.type = BLK_ZONE_TYPE_SEQWRITE_REQ;
0165     zone.cond = entry->zs >> 4;
0166     zone.len = ns->zsze;
0167     zone.capacity = nvme_lba_to_sect(ns, le64_to_cpu(entry->zcap));
0168     zone.start = nvme_lba_to_sect(ns, le64_to_cpu(entry->zslba));
0169     if (zone.cond == BLK_ZONE_COND_FULL)
0170         zone.wp = zone.start + zone.len;
0171     else
0172         zone.wp = nvme_lba_to_sect(ns, le64_to_cpu(entry->wp));
0173 
0174     return cb(&zone, idx, data);
0175 }
0176 
0177 int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
0178         unsigned int nr_zones, report_zones_cb cb, void *data)
0179 {
0180     struct nvme_zone_report *report;
0181     struct nvme_command c = { };
0182     int ret, zone_idx = 0;
0183     unsigned int nz, i;
0184     size_t buflen;
0185 
0186     if (ns->head->ids.csi != NVME_CSI_ZNS)
0187         return -EINVAL;
0188 
0189     report = nvme_zns_alloc_report_buffer(ns, nr_zones, &buflen);
0190     if (!report)
0191         return -ENOMEM;
0192 
0193     c.zmr.opcode = nvme_cmd_zone_mgmt_recv;
0194     c.zmr.nsid = cpu_to_le32(ns->head->ns_id);
0195     c.zmr.numd = cpu_to_le32(nvme_bytes_to_numd(buflen));
0196     c.zmr.zra = NVME_ZRA_ZONE_REPORT;
0197     c.zmr.zrasf = NVME_ZRASF_ZONE_REPORT_ALL;
0198     c.zmr.pr = NVME_REPORT_ZONE_PARTIAL;
0199 
0200     sector &= ~(ns->zsze - 1);
0201     while (zone_idx < nr_zones && sector < get_capacity(ns->disk)) {
0202         memset(report, 0, buflen);
0203 
0204         c.zmr.slba = cpu_to_le64(nvme_sect_to_lba(ns, sector));
0205         ret = nvme_submit_sync_cmd(ns->queue, &c, report, buflen);
0206         if (ret) {
0207             if (ret > 0)
0208                 ret = -EIO;
0209             goto out_free;
0210         }
0211 
0212         nz = min((unsigned int)le64_to_cpu(report->nr_zones), nr_zones);
0213         if (!nz)
0214             break;
0215 
0216         for (i = 0; i < nz && zone_idx < nr_zones; i++) {
0217             ret = nvme_zone_parse_entry(ns, &report->entries[i],
0218                             zone_idx, cb, data);
0219             if (ret)
0220                 goto out_free;
0221             zone_idx++;
0222         }
0223 
0224         sector += ns->zsze * nz;
0225     }
0226 
0227     if (zone_idx > 0)
0228         ret = zone_idx;
0229     else
0230         ret = -EINVAL;
0231 out_free:
0232     kvfree(report);
0233     return ret;
0234 }
0235 
0236 blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
0237         struct nvme_command *c, enum nvme_zone_mgmt_action action)
0238 {
0239     memset(c, 0, sizeof(*c));
0240 
0241     c->zms.opcode = nvme_cmd_zone_mgmt_send;
0242     c->zms.nsid = cpu_to_le32(ns->head->ns_id);
0243     c->zms.slba = cpu_to_le64(nvme_sect_to_lba(ns, blk_rq_pos(req)));
0244     c->zms.zsa = action;
0245 
0246     if (req_op(req) == REQ_OP_ZONE_RESET_ALL)
0247         c->zms.select_all = 1;
0248 
0249     return BLK_STS_OK;
0250 }