1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Western Digital Corporation or its affiliates.
6 #include <linux/blkdev.h>
7 #include <linux/vmalloc.h>
10 int nvme_revalidate_zones(struct nvme_ns *ns)
12 struct request_queue *q = ns->queue;
14 blk_queue_chunk_sectors(q, ns->head->zsze);
15 blk_queue_max_zone_append_sectors(q, ns->ctrl->max_zone_append);
17 return blk_revalidate_disk_zones(ns->disk, NULL);
20 static int nvme_set_max_append(struct nvme_ctrl *ctrl)
22 struct nvme_command c = { };
23 struct nvme_id_ctrl_zns *id;
26 id = kzalloc(sizeof(*id), GFP_KERNEL);
30 c.identify.opcode = nvme_admin_identify;
31 c.identify.cns = NVME_ID_CNS_CS_CTRL;
32 c.identify.csi = NVME_CSI_ZNS;
34 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
41 ctrl->max_zone_append = 1 << (id->zasl + 3);
43 ctrl->max_zone_append = ctrl->max_hw_sectors;
48 int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
50 struct nvme_effects_log *log = ns->head->effects;
51 struct request_queue *q = ns->queue;
52 struct nvme_command c = { };
53 struct nvme_id_ns_zns *id;
56 /* Driver requires zone append support */
57 if ((le32_to_cpu(log->iocs[nvme_cmd_zone_append]) &
58 NVME_CMD_EFFECTS_CSUPP)) {
59 if (test_and_clear_bit(NVME_NS_FORCE_RO, &ns->flags))
60 dev_warn(ns->ctrl->device,
61 "Zone Append supported for zoned namespace:%d. Remove read-only mode\n",
64 set_bit(NVME_NS_FORCE_RO, &ns->flags);
65 dev_warn(ns->ctrl->device,
66 "Zone Append not supported for zoned namespace:%d. Forcing to read-only mode\n",
70 /* Lazily query controller append limit for the first zoned namespace */
71 if (!ns->ctrl->max_zone_append) {
72 status = nvme_set_max_append(ns->ctrl);
77 id = kzalloc(sizeof(*id), GFP_KERNEL);
81 c.identify.opcode = nvme_admin_identify;
82 c.identify.nsid = cpu_to_le32(ns->head->ns_id);
83 c.identify.cns = NVME_ID_CNS_CS_NS;
84 c.identify.csi = NVME_CSI_ZNS;
86 status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, id, sizeof(*id));
91 * We currently do not handle devices requiring any of the zoned
92 * operation characteristics.
95 dev_warn(ns->ctrl->device,
96 "zone operations:%x not supported for namespace:%u\n",
97 le16_to_cpu(id->zoc), ns->head->ns_id);
103 nvme_lba_to_sect(ns->head, le64_to_cpu(id->lbafe[lbaf].zsze));
104 if (!is_power_of_2(ns->head->zsze)) {
105 dev_warn(ns->ctrl->device,
106 "invalid zone size:%llu for namespace:%u\n",
107 ns->head->zsze, ns->head->ns_id);
112 disk_set_zoned(ns->disk);
113 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
114 disk_set_max_open_zones(ns->disk, le32_to_cpu(id->mor) + 1);
115 disk_set_max_active_zones(ns->disk, le32_to_cpu(id->mar) + 1);
121 static void *nvme_zns_alloc_report_buffer(struct nvme_ns *ns,
122 unsigned int nr_zones, size_t *buflen)
124 struct request_queue *q = ns->disk->queue;
128 const size_t min_bufsize = sizeof(struct nvme_zone_report) +
129 sizeof(struct nvme_zone_descriptor);
131 nr_zones = min_t(unsigned int, nr_zones,
132 get_capacity(ns->disk) >> ilog2(ns->head->zsze));
134 bufsize = sizeof(struct nvme_zone_report) +
135 nr_zones * sizeof(struct nvme_zone_descriptor);
136 bufsize = min_t(size_t, bufsize,
137 queue_max_hw_sectors(q) << SECTOR_SHIFT);
138 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
140 while (bufsize >= min_bufsize) {
141 buf = __vmalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
151 static int nvme_zone_parse_entry(struct nvme_ctrl *ctrl,
152 struct nvme_ns_head *head,
153 struct nvme_zone_descriptor *entry,
154 unsigned int idx, report_zones_cb cb,
157 struct blk_zone zone = { };
159 if ((entry->zt & 0xf) != NVME_ZONE_TYPE_SEQWRITE_REQ) {
160 dev_err(ctrl->device, "invalid zone type %#x\n",
165 zone.type = BLK_ZONE_TYPE_SEQWRITE_REQ;
166 zone.cond = entry->zs >> 4;
167 zone.len = head->zsze;
168 zone.capacity = nvme_lba_to_sect(head, le64_to_cpu(entry->zcap));
169 zone.start = nvme_lba_to_sect(head, le64_to_cpu(entry->zslba));
170 if (zone.cond == BLK_ZONE_COND_FULL)
171 zone.wp = zone.start + zone.len;
173 zone.wp = nvme_lba_to_sect(head, le64_to_cpu(entry->wp));
175 return cb(&zone, idx, data);
178 int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
179 unsigned int nr_zones, report_zones_cb cb, void *data)
181 struct nvme_zone_report *report;
182 struct nvme_command c = { };
183 int ret, zone_idx = 0;
187 if (ns->head->ids.csi != NVME_CSI_ZNS)
190 report = nvme_zns_alloc_report_buffer(ns, nr_zones, &buflen);
194 c.zmr.opcode = nvme_cmd_zone_mgmt_recv;
195 c.zmr.nsid = cpu_to_le32(ns->head->ns_id);
196 c.zmr.numd = cpu_to_le32(nvme_bytes_to_numd(buflen));
197 c.zmr.zra = NVME_ZRA_ZONE_REPORT;
198 c.zmr.zrasf = NVME_ZRASF_ZONE_REPORT_ALL;
199 c.zmr.pr = NVME_REPORT_ZONE_PARTIAL;
201 sector &= ~(ns->head->zsze - 1);
202 while (zone_idx < nr_zones && sector < get_capacity(ns->disk)) {
203 memset(report, 0, buflen);
205 c.zmr.slba = cpu_to_le64(nvme_sect_to_lba(ns->head, sector));
206 ret = nvme_submit_sync_cmd(ns->queue, &c, report, buflen);
213 nz = min((unsigned int)le64_to_cpu(report->nr_zones), nr_zones);
217 for (i = 0; i < nz && zone_idx < nr_zones; i++) {
218 ret = nvme_zone_parse_entry(ns->ctrl, ns->head,
226 sector += ns->head->zsze * nz;
238 blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
239 struct nvme_command *c, enum nvme_zone_mgmt_action action)
241 memset(c, 0, sizeof(*c));
243 c->zms.opcode = nvme_cmd_zone_mgmt_send;
244 c->zms.nsid = cpu_to_le32(ns->head->ns_id);
245 c->zms.slba = cpu_to_le64(nvme_sect_to_lba(ns->head, blk_rq_pos(req)));
248 if (req_op(req) == REQ_OP_ZONE_RESET_ALL)
249 c->zms.select_all = 1;