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 static int nvme_set_max_append(struct nvme_ctrl *ctrl)
12 struct nvme_command c = { };
13 struct nvme_id_ctrl_zns *id;
16 id = kzalloc(sizeof(*id), GFP_KERNEL);
20 c.identify.opcode = nvme_admin_identify;
21 c.identify.cns = NVME_ID_CNS_CS_CTRL;
22 c.identify.csi = NVME_CSI_ZNS;
24 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
31 ctrl->max_zone_append = 1 << (id->zasl + 3);
33 ctrl->max_zone_append = ctrl->max_hw_sectors;
38 int nvme_update_zone_info(struct gendisk *disk, struct nvme_ns *ns,
41 struct nvme_effects_log *log = ns->head->effects;
42 struct request_queue *q = disk->queue;
43 struct nvme_command c = { };
44 struct nvme_id_ns_zns *id;
47 /* Driver requires zone append support */
48 if (!(le32_to_cpu(log->iocs[nvme_cmd_zone_append]) &
49 NVME_CMD_EFFECTS_CSUPP)) {
50 dev_warn(ns->ctrl->device,
51 "append not supported for zoned namespace:%d\n",
56 /* Lazily query controller append limit for the first zoned namespace */
57 if (!ns->ctrl->max_zone_append) {
58 status = nvme_set_max_append(ns->ctrl);
63 id = kzalloc(sizeof(*id), GFP_KERNEL);
67 c.identify.opcode = nvme_admin_identify;
68 c.identify.nsid = cpu_to_le32(ns->head->ns_id);
69 c.identify.cns = NVME_ID_CNS_CS_NS;
70 c.identify.csi = NVME_CSI_ZNS;
72 status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, id, sizeof(*id));
77 * We currently do not handle devices requiring any of the zoned
78 * operation characteristics.
81 dev_warn(ns->ctrl->device,
82 "zone operations:%x not supported for namespace:%u\n",
83 le16_to_cpu(id->zoc), ns->head->ns_id);
88 ns->zsze = nvme_lba_to_sect(ns, le64_to_cpu(id->lbafe[lbaf].zsze));
89 if (!is_power_of_2(ns->zsze)) {
90 dev_warn(ns->ctrl->device,
91 "invalid zone size:%llu for namespace:%u\n",
92 ns->zsze, ns->head->ns_id);
97 q->limits.zoned = BLK_ZONED_HM;
98 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
99 blk_queue_max_open_zones(q, le32_to_cpu(id->mor) + 1);
100 blk_queue_max_active_zones(q, le32_to_cpu(id->mar) + 1);
106 static void *nvme_zns_alloc_report_buffer(struct nvme_ns *ns,
107 unsigned int nr_zones, size_t *buflen)
109 struct request_queue *q = ns->disk->queue;
113 const size_t min_bufsize = sizeof(struct nvme_zone_report) +
114 sizeof(struct nvme_zone_descriptor);
116 nr_zones = min_t(unsigned int, nr_zones,
117 get_capacity(ns->disk) >> ilog2(ns->zsze));
119 bufsize = sizeof(struct nvme_zone_report) +
120 nr_zones * sizeof(struct nvme_zone_descriptor);
121 bufsize = min_t(size_t, bufsize,
122 queue_max_hw_sectors(q) << SECTOR_SHIFT);
123 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
125 while (bufsize >= min_bufsize) {
126 buf = __vmalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
136 static int __nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
137 struct nvme_zone_report *report,
140 struct nvme_command c = { };
143 c.zmr.opcode = nvme_cmd_zone_mgmt_recv;
144 c.zmr.nsid = cpu_to_le32(ns->head->ns_id);
145 c.zmr.slba = cpu_to_le64(nvme_sect_to_lba(ns, sector));
146 c.zmr.numd = cpu_to_le32(nvme_bytes_to_numd(buflen));
147 c.zmr.zra = NVME_ZRA_ZONE_REPORT;
148 c.zmr.zrasf = NVME_ZRASF_ZONE_REPORT_ALL;
149 c.zmr.pr = NVME_REPORT_ZONE_PARTIAL;
151 ret = nvme_submit_sync_cmd(ns->queue, &c, report, buflen);
155 return le64_to_cpu(report->nr_zones);
158 static int nvme_zone_parse_entry(struct nvme_ns *ns,
159 struct nvme_zone_descriptor *entry,
160 unsigned int idx, report_zones_cb cb,
163 struct blk_zone zone = { };
165 if ((entry->zt & 0xf) != NVME_ZONE_TYPE_SEQWRITE_REQ) {
166 dev_err(ns->ctrl->device, "invalid zone type %#x\n",
171 zone.type = BLK_ZONE_TYPE_SEQWRITE_REQ;
172 zone.cond = entry->zs >> 4;
174 zone.capacity = nvme_lba_to_sect(ns, le64_to_cpu(entry->zcap));
175 zone.start = nvme_lba_to_sect(ns, le64_to_cpu(entry->zslba));
176 zone.wp = nvme_lba_to_sect(ns, le64_to_cpu(entry->wp));
178 return cb(&zone, idx, data);
181 static int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
182 unsigned int nr_zones, report_zones_cb cb, void *data)
184 struct nvme_zone_report *report;
185 int ret, zone_idx = 0;
189 report = nvme_zns_alloc_report_buffer(ns, nr_zones, &buflen);
193 sector &= ~(ns->zsze - 1);
194 while (zone_idx < nr_zones && sector < get_capacity(ns->disk)) {
195 memset(report, 0, buflen);
196 ret = __nvme_ns_report_zones(ns, sector, report, buflen);
200 nz = min_t(unsigned int, ret, nr_zones);
204 for (i = 0; i < nz && zone_idx < nr_zones; i++) {
205 ret = nvme_zone_parse_entry(ns, &report->entries[i],
212 sector += ns->zsze * nz;
224 int nvme_report_zones(struct gendisk *disk, sector_t sector,
225 unsigned int nr_zones, report_zones_cb cb, void *data)
227 struct nvme_ns_head *head = NULL;
231 ns = nvme_get_ns_from_disk(disk, &head, &srcu_idx);
235 if (ns->head->ids.csi == NVME_CSI_ZNS)
236 ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data);
239 nvme_put_ns_from_disk(head, srcu_idx);
244 blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
245 struct nvme_command *c, enum nvme_zone_mgmt_action action)
247 c->zms.opcode = nvme_cmd_zone_mgmt_send;
248 c->zms.nsid = cpu_to_le32(ns->head->ns_id);
249 c->zms.slba = cpu_to_le64(nvme_sect_to_lba(ns, blk_rq_pos(req)));
252 if (req_op(req) == REQ_OP_ZONE_RESET_ALL)
253 c->zms.select_all = 1;