xref: /openbmc/qemu/hw/nvme/ns.c (revision 86e372ad)
1 /*
2  * QEMU NVM Express Virtual Namespace
3  *
4  * Copyright (c) 2019 CNEX Labs
5  * Copyright (c) 2020 Samsung Electronics
6  *
7  * Authors:
8  *  Klaus Jensen      <k.jensen@samsung.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2. See the
11  * COPYING file in the top-level directory.
12  *
13  */
14 
15 #include "qemu/osdep.h"
16 #include "qemu/units.h"
17 #include "qemu/cutils.h"
18 #include "qemu/error-report.h"
19 #include "qapi/error.h"
20 #include "qemu/bitops.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/block-backend.h"
23 
24 #include "nvme.h"
25 #include "trace.h"
26 
27 #define MIN_DISCARD_GRANULARITY (4 * KiB)
28 #define NVME_DEFAULT_ZONE_SIZE   (128 * MiB)
29 
30 void nvme_ns_init_format(NvmeNamespace *ns)
31 {
32     NvmeIdNs *id_ns = &ns->id_ns;
33     BlockDriverInfo bdi;
34     int npdg, ret;
35     int64_t nlbas;
36 
37     ns->lbaf = id_ns->lbaf[NVME_ID_NS_FLBAS_INDEX(id_ns->flbas)];
38     ns->lbasz = 1 << ns->lbaf.ds;
39 
40     nlbas = ns->size / (ns->lbasz + ns->lbaf.ms);
41 
42     id_ns->nsze = cpu_to_le64(nlbas);
43 
44     /* no thin provisioning */
45     id_ns->ncap = id_ns->nsze;
46     id_ns->nuse = id_ns->ncap;
47 
48     ns->moff = nlbas << ns->lbaf.ds;
49 
50     npdg = ns->blkconf.discard_granularity / ns->lbasz;
51 
52     ret = bdrv_get_info(blk_bs(ns->blkconf.blk), &bdi);
53     if (ret >= 0 && bdi.cluster_size > ns->blkconf.discard_granularity) {
54         npdg = bdi.cluster_size / ns->lbasz;
55     }
56 
57     id_ns->npda = id_ns->npdg = npdg - 1;
58 }
59 
60 static int nvme_ns_init(NvmeNamespace *ns, Error **errp)
61 {
62     static uint64_t ns_count;
63     NvmeIdNs *id_ns = &ns->id_ns;
64     NvmeIdNsNvm *id_ns_nvm = &ns->id_ns_nvm;
65     uint8_t ds;
66     uint16_t ms;
67     int i;
68 
69     ns->csi = NVME_CSI_NVM;
70     ns->status = 0x0;
71 
72     ns->id_ns.dlfeat = 0x1;
73 
74     /* support DULBE and I/O optimization fields */
75     id_ns->nsfeat |= (0x4 | 0x10);
76 
77     if (ns->params.shared) {
78         id_ns->nmic |= NVME_NMIC_NS_SHARED;
79     }
80 
81     /* Substitute a missing EUI-64 by an autogenerated one */
82     ++ns_count;
83     if (!ns->params.eui64 && ns->params.eui64_default) {
84         ns->params.eui64 = ns_count + NVME_EUI64_DEFAULT;
85     }
86 
87     /* simple copy */
88     id_ns->mssrl = cpu_to_le16(ns->params.mssrl);
89     id_ns->mcl = cpu_to_le32(ns->params.mcl);
90     id_ns->msrc = ns->params.msrc;
91     id_ns->eui64 = cpu_to_be64(ns->params.eui64);
92     memcpy(&id_ns->nguid, &ns->params.nguid.data, sizeof(id_ns->nguid));
93 
94     ds = 31 - clz32(ns->blkconf.logical_block_size);
95     ms = ns->params.ms;
96 
97     id_ns->mc = NVME_ID_NS_MC_EXTENDED | NVME_ID_NS_MC_SEPARATE;
98 
99     if (ms && ns->params.mset) {
100         id_ns->flbas |= NVME_ID_NS_FLBAS_EXTENDED;
101     }
102 
103     id_ns->dpc = 0x1f;
104     id_ns->dps = ns->params.pi;
105     if (ns->params.pi && ns->params.pil) {
106         id_ns->dps |= NVME_ID_NS_DPS_FIRST_EIGHT;
107     }
108 
109     ns->pif = ns->params.pif;
110 
111     static const NvmeLBAF defaults[16] = {
112         [0] = { .ds =  9           },
113         [1] = { .ds =  9, .ms =  8 },
114         [2] = { .ds =  9, .ms = 16 },
115         [3] = { .ds =  9, .ms = 64 },
116         [4] = { .ds = 12           },
117         [5] = { .ds = 12, .ms =  8 },
118         [6] = { .ds = 12, .ms = 16 },
119         [7] = { .ds = 12, .ms = 64 },
120     };
121 
122     ns->nlbaf = 8;
123 
124     memcpy(&id_ns->lbaf, &defaults, sizeof(defaults));
125 
126     for (i = 0; i < ns->nlbaf; i++) {
127         NvmeLBAF *lbaf = &id_ns->lbaf[i];
128         if (lbaf->ds == ds) {
129             if (lbaf->ms == ms) {
130                 id_ns->flbas |= i;
131                 goto lbaf_found;
132             }
133         }
134     }
135 
136     /* add non-standard lba format */
137     id_ns->lbaf[ns->nlbaf].ds = ds;
138     id_ns->lbaf[ns->nlbaf].ms = ms;
139     ns->nlbaf++;
140 
141     id_ns->flbas |= i;
142 
143 
144 lbaf_found:
145     id_ns_nvm->elbaf[i] = (ns->pif & 0x3) << 7;
146     id_ns->nlbaf = ns->nlbaf - 1;
147     nvme_ns_init_format(ns);
148 
149     return 0;
150 }
151 
152 static int nvme_ns_init_blk(NvmeNamespace *ns, Error **errp)
153 {
154     bool read_only;
155 
156     if (!blkconf_blocksizes(&ns->blkconf, errp)) {
157         return -1;
158     }
159 
160     read_only = !blk_supports_write_perm(ns->blkconf.blk);
161     if (!blkconf_apply_backend_options(&ns->blkconf, read_only, false, errp)) {
162         return -1;
163     }
164 
165     if (ns->blkconf.discard_granularity == -1) {
166         ns->blkconf.discard_granularity =
167             MAX(ns->blkconf.logical_block_size, MIN_DISCARD_GRANULARITY);
168     }
169 
170     ns->size = blk_getlength(ns->blkconf.blk);
171     if (ns->size < 0) {
172         error_setg_errno(errp, -ns->size, "could not get blockdev size");
173         return -1;
174     }
175 
176     return 0;
177 }
178 
179 static int nvme_ns_zoned_check_calc_geometry(NvmeNamespace *ns, Error **errp)
180 {
181     uint64_t zone_size, zone_cap;
182 
183     /* Make sure that the values of ZNS properties are sane */
184     if (ns->params.zone_size_bs) {
185         zone_size = ns->params.zone_size_bs;
186     } else {
187         zone_size = NVME_DEFAULT_ZONE_SIZE;
188     }
189     if (ns->params.zone_cap_bs) {
190         zone_cap = ns->params.zone_cap_bs;
191     } else {
192         zone_cap = zone_size;
193     }
194     if (zone_cap > zone_size) {
195         error_setg(errp, "zone capacity %"PRIu64"B exceeds "
196                    "zone size %"PRIu64"B", zone_cap, zone_size);
197         return -1;
198     }
199     if (zone_size < ns->lbasz) {
200         error_setg(errp, "zone size %"PRIu64"B too small, "
201                    "must be at least %zuB", zone_size, ns->lbasz);
202         return -1;
203     }
204     if (zone_cap < ns->lbasz) {
205         error_setg(errp, "zone capacity %"PRIu64"B too small, "
206                    "must be at least %zuB", zone_cap, ns->lbasz);
207         return -1;
208     }
209 
210     /*
211      * Save the main zone geometry values to avoid
212      * calculating them later again.
213      */
214     ns->zone_size = zone_size / ns->lbasz;
215     ns->zone_capacity = zone_cap / ns->lbasz;
216     ns->num_zones = le64_to_cpu(ns->id_ns.nsze) / ns->zone_size;
217 
218     /* Do a few more sanity checks of ZNS properties */
219     if (!ns->num_zones) {
220         error_setg(errp,
221                    "insufficient drive capacity, must be at least the size "
222                    "of one zone (%"PRIu64"B)", zone_size);
223         return -1;
224     }
225 
226     return 0;
227 }
228 
229 static void nvme_ns_zoned_init_state(NvmeNamespace *ns)
230 {
231     uint64_t start = 0, zone_size = ns->zone_size;
232     uint64_t capacity = ns->num_zones * zone_size;
233     NvmeZone *zone;
234     int i;
235 
236     ns->zone_array = g_new0(NvmeZone, ns->num_zones);
237     if (ns->params.zd_extension_size) {
238         ns->zd_extensions = g_malloc0(ns->params.zd_extension_size *
239                                       ns->num_zones);
240     }
241 
242     QTAILQ_INIT(&ns->exp_open_zones);
243     QTAILQ_INIT(&ns->imp_open_zones);
244     QTAILQ_INIT(&ns->closed_zones);
245     QTAILQ_INIT(&ns->full_zones);
246 
247     zone = ns->zone_array;
248     for (i = 0; i < ns->num_zones; i++, zone++) {
249         if (start + zone_size > capacity) {
250             zone_size = capacity - start;
251         }
252         zone->d.zt = NVME_ZONE_TYPE_SEQ_WRITE;
253         nvme_set_zone_state(zone, NVME_ZONE_STATE_EMPTY);
254         zone->d.za = 0;
255         zone->d.zcap = ns->zone_capacity;
256         zone->d.zslba = start;
257         zone->d.wp = start;
258         zone->w_ptr = start;
259         start += zone_size;
260     }
261 
262     ns->zone_size_log2 = 0;
263     if (is_power_of_2(ns->zone_size)) {
264         ns->zone_size_log2 = 63 - clz64(ns->zone_size);
265     }
266 }
267 
268 static void nvme_ns_init_zoned(NvmeNamespace *ns)
269 {
270     NvmeIdNsZoned *id_ns_z;
271     int i;
272 
273     nvme_ns_zoned_init_state(ns);
274 
275     id_ns_z = g_new0(NvmeIdNsZoned, 1);
276 
277     /* MAR/MOR are zeroes-based, FFFFFFFFFh means no limit */
278     id_ns_z->mar = cpu_to_le32(ns->params.max_active_zones - 1);
279     id_ns_z->mor = cpu_to_le32(ns->params.max_open_zones - 1);
280     id_ns_z->zoc = 0;
281     id_ns_z->ozcs = ns->params.cross_zone_read ?
282         NVME_ID_NS_ZONED_OZCS_RAZB : 0x00;
283 
284     for (i = 0; i <= ns->id_ns.nlbaf; i++) {
285         id_ns_z->lbafe[i].zsze = cpu_to_le64(ns->zone_size);
286         id_ns_z->lbafe[i].zdes =
287             ns->params.zd_extension_size >> 6; /* Units of 64B */
288     }
289 
290     if (ns->params.zrwas) {
291         ns->zns.numzrwa = ns->params.numzrwa ?
292             ns->params.numzrwa : ns->num_zones;
293 
294         ns->zns.zrwas = ns->params.zrwas >> ns->lbaf.ds;
295         ns->zns.zrwafg = ns->params.zrwafg >> ns->lbaf.ds;
296 
297         id_ns_z->ozcs |= NVME_ID_NS_ZONED_OZCS_ZRWASUP;
298         id_ns_z->zrwacap = NVME_ID_NS_ZONED_ZRWACAP_EXPFLUSHSUP;
299 
300         id_ns_z->numzrwa = cpu_to_le32(ns->params.numzrwa);
301         id_ns_z->zrwas = cpu_to_le16(ns->zns.zrwas);
302         id_ns_z->zrwafg = cpu_to_le16(ns->zns.zrwafg);
303     }
304 
305     id_ns_z->ozcs = cpu_to_le16(id_ns_z->ozcs);
306 
307     ns->csi = NVME_CSI_ZONED;
308     ns->id_ns.nsze = cpu_to_le64(ns->num_zones * ns->zone_size);
309     ns->id_ns.ncap = ns->id_ns.nsze;
310     ns->id_ns.nuse = ns->id_ns.ncap;
311 
312     /*
313      * The device uses the BDRV_BLOCK_ZERO flag to determine the "deallocated"
314      * status of logical blocks. Since the spec defines that logical blocks
315      * SHALL be deallocated when then zone is in the Empty or Offline states,
316      * we can only support DULBE if the zone size is a multiple of the
317      * calculated NPDG.
318      */
319     if (ns->zone_size % (ns->id_ns.npdg + 1)) {
320         warn_report("the zone size (%"PRIu64" blocks) is not a multiple of "
321                     "the calculated deallocation granularity (%d blocks); "
322                     "DULBE support disabled",
323                     ns->zone_size, ns->id_ns.npdg + 1);
324 
325         ns->id_ns.nsfeat &= ~0x4;
326     }
327 
328     ns->id_ns_zoned = id_ns_z;
329 }
330 
331 static void nvme_clear_zone(NvmeNamespace *ns, NvmeZone *zone)
332 {
333     uint8_t state;
334 
335     zone->w_ptr = zone->d.wp;
336     state = nvme_get_zone_state(zone);
337     if (zone->d.wp != zone->d.zslba ||
338         (zone->d.za & NVME_ZA_ZD_EXT_VALID)) {
339         if (state != NVME_ZONE_STATE_CLOSED) {
340             trace_pci_nvme_clear_ns_close(state, zone->d.zslba);
341             nvme_set_zone_state(zone, NVME_ZONE_STATE_CLOSED);
342         }
343         nvme_aor_inc_active(ns);
344         QTAILQ_INSERT_HEAD(&ns->closed_zones, zone, entry);
345     } else {
346         trace_pci_nvme_clear_ns_reset(state, zone->d.zslba);
347         if (zone->d.za & NVME_ZA_ZRWA_VALID) {
348             zone->d.za &= ~NVME_ZA_ZRWA_VALID;
349             ns->zns.numzrwa++;
350         }
351         nvme_set_zone_state(zone, NVME_ZONE_STATE_EMPTY);
352     }
353 }
354 
355 /*
356  * Close all the zones that are currently open.
357  */
358 static void nvme_zoned_ns_shutdown(NvmeNamespace *ns)
359 {
360     NvmeZone *zone, *next;
361 
362     QTAILQ_FOREACH_SAFE(zone, &ns->closed_zones, entry, next) {
363         QTAILQ_REMOVE(&ns->closed_zones, zone, entry);
364         nvme_aor_dec_active(ns);
365         nvme_clear_zone(ns, zone);
366     }
367     QTAILQ_FOREACH_SAFE(zone, &ns->imp_open_zones, entry, next) {
368         QTAILQ_REMOVE(&ns->imp_open_zones, zone, entry);
369         nvme_aor_dec_open(ns);
370         nvme_aor_dec_active(ns);
371         nvme_clear_zone(ns, zone);
372     }
373     QTAILQ_FOREACH_SAFE(zone, &ns->exp_open_zones, entry, next) {
374         QTAILQ_REMOVE(&ns->exp_open_zones, zone, entry);
375         nvme_aor_dec_open(ns);
376         nvme_aor_dec_active(ns);
377         nvme_clear_zone(ns, zone);
378     }
379 
380     assert(ns->nr_open_zones == 0);
381 }
382 
383 static NvmeRuHandle *nvme_find_ruh_by_attr(NvmeEnduranceGroup *endgrp,
384                                            uint8_t ruha, uint16_t *ruhid)
385 {
386     for (uint16_t i = 0; i < endgrp->fdp.nruh; i++) {
387         NvmeRuHandle *ruh = &endgrp->fdp.ruhs[i];
388 
389         if (ruh->ruha == ruha) {
390             *ruhid = i;
391             return ruh;
392         }
393     }
394 
395     return NULL;
396 }
397 
398 static bool nvme_ns_init_fdp(NvmeNamespace *ns, Error **errp)
399 {
400     NvmeEnduranceGroup *endgrp = ns->endgrp;
401     NvmeRuHandle *ruh;
402     uint8_t lbafi = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
403     g_autofree unsigned int *ruhids = NULL;
404     unsigned int n, m, *ruhid;
405     const char *endptr, *token;
406     char *r, *p;
407     uint16_t *ph;
408 
409     if (!ns->params.fdp.ruhs) {
410         ns->fdp.nphs = 1;
411         ph = ns->fdp.phs = g_new(uint16_t, 1);
412 
413         ruh = nvme_find_ruh_by_attr(endgrp, NVME_RUHA_CTRL, ph);
414         if (!ruh) {
415             ruh = nvme_find_ruh_by_attr(endgrp, NVME_RUHA_UNUSED, ph);
416             if (!ruh) {
417                 error_setg(errp, "no unused reclaim unit handles left");
418                 return false;
419             }
420 
421             ruh->ruha = NVME_RUHA_CTRL;
422             ruh->lbafi = lbafi;
423             ruh->ruamw = endgrp->fdp.runs >> ns->lbaf.ds;
424 
425             for (uint16_t rg = 0; rg < endgrp->fdp.nrg; rg++) {
426                 ruh->rus[rg].ruamw = ruh->ruamw;
427             }
428         } else if (ruh->lbafi != lbafi) {
429             error_setg(errp, "lba format index of controller assigned "
430                        "reclaim unit handle does not match namespace lba "
431                        "format index");
432             return false;
433         }
434 
435         return true;
436     }
437 
438     ruhid = ruhids = g_new0(unsigned int, endgrp->fdp.nruh);
439     r = p = strdup(ns->params.fdp.ruhs);
440 
441     /* parse the placement handle identifiers */
442     while ((token = qemu_strsep(&p, ";")) != NULL) {
443         if (qemu_strtoui(token, &endptr, 0, &n) < 0) {
444             error_setg(errp, "cannot parse reclaim unit handle identifier");
445             free(r);
446             return false;
447         }
448 
449         m = n;
450 
451         /* parse range */
452         if (*endptr == '-') {
453             token = endptr + 1;
454 
455             if (qemu_strtoui(token, NULL, 0, &m) < 0) {
456                 error_setg(errp, "cannot parse reclaim unit handle identifier");
457                 free(r);
458                 return false;
459             }
460 
461             if (m < n) {
462                 error_setg(errp, "invalid reclaim unit handle identifier range");
463                 free(r);
464                 return false;
465             }
466         }
467 
468         for (; n <= m; n++) {
469             if (ns->fdp.nphs++ == endgrp->fdp.nruh) {
470                 error_setg(errp, "too many placement handles");
471                 free(r);
472                 return false;
473             }
474 
475             *ruhid++ = n;
476         }
477     }
478 
479     free(r);
480 
481     /* verify that the ruhids are unique */
482     for (unsigned int i = 0; i < ns->fdp.nphs; i++) {
483         for (unsigned int j = i + 1; j < ns->fdp.nphs; j++) {
484             if (ruhids[i] == ruhids[j]) {
485                 error_setg(errp, "duplicate reclaim unit handle identifier: %u",
486                            ruhids[i]);
487                 return false;
488             }
489         }
490     }
491 
492     ph = ns->fdp.phs = g_new(uint16_t, ns->fdp.nphs);
493 
494     ruhid = ruhids;
495 
496     /* verify the identifiers */
497     for (unsigned int i = 0; i < ns->fdp.nphs; i++, ruhid++, ph++) {
498         if (*ruhid >= endgrp->fdp.nruh) {
499             error_setg(errp, "invalid reclaim unit handle identifier");
500             return false;
501         }
502 
503         ruh = &endgrp->fdp.ruhs[*ruhid];
504 
505         switch (ruh->ruha) {
506         case NVME_RUHA_UNUSED:
507             ruh->ruha = NVME_RUHA_HOST;
508             ruh->lbafi = lbafi;
509             ruh->ruamw = endgrp->fdp.runs >> ns->lbaf.ds;
510 
511             for (uint16_t rg = 0; rg < endgrp->fdp.nrg; rg++) {
512                 ruh->rus[rg].ruamw = ruh->ruamw;
513             }
514 
515             break;
516 
517         case NVME_RUHA_HOST:
518             if (ruh->lbafi != lbafi) {
519                 error_setg(errp, "lba format index of host assigned"
520                            "reclaim unit handle does not match namespace "
521                            "lba format index");
522                 return false;
523             }
524 
525             break;
526 
527         case NVME_RUHA_CTRL:
528             error_setg(errp, "reclaim unit handle is controller assigned");
529             return false;
530 
531         default:
532             abort();
533         }
534 
535         *ph = *ruhid;
536     }
537 
538     return true;
539 }
540 
541 static int nvme_ns_check_constraints(NvmeNamespace *ns, Error **errp)
542 {
543     unsigned int pi_size;
544 
545     if (!ns->blkconf.blk) {
546         error_setg(errp, "block backend not configured");
547         return -1;
548     }
549 
550     if (ns->params.pi) {
551         if (ns->params.pi > NVME_ID_NS_DPS_TYPE_3) {
552             error_setg(errp, "invalid 'pi' value");
553             return -1;
554         }
555 
556         switch (ns->params.pif) {
557         case NVME_PI_GUARD_16:
558             pi_size = 8;
559             break;
560         case NVME_PI_GUARD_64:
561             pi_size = 16;
562             break;
563         default:
564             error_setg(errp, "invalid 'pif'");
565             return -1;
566         }
567 
568         if (ns->params.ms < pi_size) {
569             error_setg(errp, "at least %u bytes of metadata required to "
570                        "enable protection information", pi_size);
571             return -1;
572         }
573     }
574 
575     if (ns->params.nsid > NVME_MAX_NAMESPACES) {
576         error_setg(errp, "invalid namespace id (must be between 0 and %d)",
577                    NVME_MAX_NAMESPACES);
578         return -1;
579     }
580 
581     if (ns->params.zoned && ns->endgrp && ns->endgrp->fdp.enabled) {
582         error_setg(errp, "cannot be a zoned- in an FDP configuration");
583         return -1;
584     }
585 
586     if (ns->params.zoned) {
587         if (ns->params.max_active_zones) {
588             if (ns->params.max_open_zones > ns->params.max_active_zones) {
589                 error_setg(errp, "max_open_zones (%u) exceeds "
590                            "max_active_zones (%u)", ns->params.max_open_zones,
591                            ns->params.max_active_zones);
592                 return -1;
593             }
594 
595             if (!ns->params.max_open_zones) {
596                 ns->params.max_open_zones = ns->params.max_active_zones;
597             }
598         }
599 
600         if (ns->params.zd_extension_size) {
601             if (ns->params.zd_extension_size & 0x3f) {
602                 error_setg(errp, "zone descriptor extension size must be a "
603                            "multiple of 64B");
604                 return -1;
605             }
606             if ((ns->params.zd_extension_size >> 6) > 0xff) {
607                 error_setg(errp,
608                            "zone descriptor extension size is too large");
609                 return -1;
610             }
611         }
612 
613         if (ns->params.zrwas) {
614             if (ns->params.zrwas % ns->blkconf.logical_block_size) {
615                 error_setg(errp, "zone random write area size (zoned.zrwas "
616                            "%"PRIu64") must be a multiple of the logical "
617                            "block size (logical_block_size %"PRIu32")",
618                            ns->params.zrwas, ns->blkconf.logical_block_size);
619                 return -1;
620             }
621 
622             if (ns->params.zrwafg == -1) {
623                 ns->params.zrwafg = ns->blkconf.logical_block_size;
624             }
625 
626             if (ns->params.zrwas % ns->params.zrwafg) {
627                 error_setg(errp, "zone random write area size (zoned.zrwas "
628                            "%"PRIu64") must be a multiple of the zone random "
629                            "write area flush granularity (zoned.zrwafg, "
630                            "%"PRIu64")", ns->params.zrwas, ns->params.zrwafg);
631                 return -1;
632             }
633 
634             if (ns->params.max_active_zones) {
635                 if (ns->params.numzrwa > ns->params.max_active_zones) {
636                     error_setg(errp, "number of zone random write area "
637                                "resources (zoned.numzrwa, %d) must be less "
638                                "than or equal to maximum active resources "
639                                "(zoned.max_active_zones, %d)",
640                                ns->params.numzrwa,
641                                ns->params.max_active_zones);
642                     return -1;
643                 }
644             }
645         }
646     }
647 
648     return 0;
649 }
650 
651 int nvme_ns_setup(NvmeNamespace *ns, Error **errp)
652 {
653     if (nvme_ns_check_constraints(ns, errp)) {
654         return -1;
655     }
656 
657     if (nvme_ns_init_blk(ns, errp)) {
658         return -1;
659     }
660 
661     if (nvme_ns_init(ns, errp)) {
662         return -1;
663     }
664     if (ns->params.zoned) {
665         if (nvme_ns_zoned_check_calc_geometry(ns, errp) != 0) {
666             return -1;
667         }
668         nvme_ns_init_zoned(ns);
669     }
670 
671     if (ns->endgrp && ns->endgrp->fdp.enabled) {
672         if (!nvme_ns_init_fdp(ns, errp)) {
673             return -1;
674         }
675     }
676 
677     return 0;
678 }
679 
680 void nvme_ns_drain(NvmeNamespace *ns)
681 {
682     blk_drain(ns->blkconf.blk);
683 }
684 
685 void nvme_ns_shutdown(NvmeNamespace *ns)
686 {
687     blk_flush(ns->blkconf.blk);
688     if (ns->params.zoned) {
689         nvme_zoned_ns_shutdown(ns);
690     }
691 }
692 
693 void nvme_ns_cleanup(NvmeNamespace *ns)
694 {
695     if (ns->params.zoned) {
696         g_free(ns->id_ns_zoned);
697         g_free(ns->zone_array);
698         g_free(ns->zd_extensions);
699     }
700 
701     if (ns->endgrp && ns->endgrp->fdp.enabled) {
702         g_free(ns->fdp.phs);
703     }
704 }
705 
706 static void nvme_ns_unrealize(DeviceState *dev)
707 {
708     NvmeNamespace *ns = NVME_NS(dev);
709 
710     nvme_ns_drain(ns);
711     nvme_ns_shutdown(ns);
712     nvme_ns_cleanup(ns);
713 }
714 
715 static void nvme_ns_realize(DeviceState *dev, Error **errp)
716 {
717     NvmeNamespace *ns = NVME_NS(dev);
718     BusState *s = qdev_get_parent_bus(dev);
719     NvmeCtrl *n = NVME(s->parent);
720     NvmeSubsystem *subsys = n->subsys;
721     uint32_t nsid = ns->params.nsid;
722     int i;
723 
724     if (!n->subsys) {
725         /* If no subsys, the ns cannot be attached to more than one ctrl. */
726         ns->params.shared = false;
727         if (ns->params.detached) {
728             error_setg(errp, "detached requires that the nvme device is "
729                        "linked to an nvme-subsys device");
730             return;
731         }
732     } else {
733         /*
734          * If this namespace belongs to a subsystem (through a link on the
735          * controller device), reparent the device.
736          */
737         if (!qdev_set_parent_bus(dev, &subsys->bus.parent_bus, errp)) {
738             return;
739         }
740         ns->subsys = subsys;
741         ns->endgrp = &subsys->endgrp;
742     }
743 
744     if (nvme_ns_setup(ns, errp)) {
745         return;
746     }
747 
748     if (!nsid) {
749         for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
750             if (nvme_ns(n, i) || nvme_subsys_ns(subsys, i)) {
751                 continue;
752             }
753 
754             nsid = ns->params.nsid = i;
755             break;
756         }
757 
758         if (!nsid) {
759             error_setg(errp, "no free namespace id");
760             return;
761         }
762     } else {
763         if (nvme_ns(n, nsid) || nvme_subsys_ns(subsys, nsid)) {
764             error_setg(errp, "namespace id '%d' already allocated", nsid);
765             return;
766         }
767     }
768 
769     if (subsys) {
770         subsys->namespaces[nsid] = ns;
771 
772         ns->id_ns.endgid = cpu_to_le16(0x1);
773 
774         if (ns->params.detached) {
775             return;
776         }
777 
778         if (ns->params.shared) {
779             for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
780                 NvmeCtrl *ctrl = subsys->ctrls[i];
781 
782                 if (ctrl && ctrl != SUBSYS_SLOT_RSVD) {
783                     nvme_attach_ns(ctrl, ns);
784                 }
785             }
786 
787             return;
788         }
789 
790     }
791 
792     nvme_attach_ns(n, ns);
793 }
794 
795 static Property nvme_ns_props[] = {
796     DEFINE_BLOCK_PROPERTIES(NvmeNamespace, blkconf),
797     DEFINE_PROP_BOOL("detached", NvmeNamespace, params.detached, false),
798     DEFINE_PROP_BOOL("shared", NvmeNamespace, params.shared, true),
799     DEFINE_PROP_UINT32("nsid", NvmeNamespace, params.nsid, 0),
800     DEFINE_PROP_UUID_NODEFAULT("uuid", NvmeNamespace, params.uuid),
801     DEFINE_PROP_NGUID_NODEFAULT("nguid", NvmeNamespace, params.nguid),
802     DEFINE_PROP_UINT64("eui64", NvmeNamespace, params.eui64, 0),
803     DEFINE_PROP_UINT16("ms", NvmeNamespace, params.ms, 0),
804     DEFINE_PROP_UINT8("mset", NvmeNamespace, params.mset, 0),
805     DEFINE_PROP_UINT8("pi", NvmeNamespace, params.pi, 0),
806     DEFINE_PROP_UINT8("pil", NvmeNamespace, params.pil, 0),
807     DEFINE_PROP_UINT8("pif", NvmeNamespace, params.pif, 0),
808     DEFINE_PROP_UINT16("mssrl", NvmeNamespace, params.mssrl, 128),
809     DEFINE_PROP_UINT32("mcl", NvmeNamespace, params.mcl, 128),
810     DEFINE_PROP_UINT8("msrc", NvmeNamespace, params.msrc, 127),
811     DEFINE_PROP_BOOL("zoned", NvmeNamespace, params.zoned, false),
812     DEFINE_PROP_SIZE("zoned.zone_size", NvmeNamespace, params.zone_size_bs,
813                      NVME_DEFAULT_ZONE_SIZE),
814     DEFINE_PROP_SIZE("zoned.zone_capacity", NvmeNamespace, params.zone_cap_bs,
815                      0),
816     DEFINE_PROP_BOOL("zoned.cross_read", NvmeNamespace,
817                      params.cross_zone_read, false),
818     DEFINE_PROP_UINT32("zoned.max_active", NvmeNamespace,
819                        params.max_active_zones, 0),
820     DEFINE_PROP_UINT32("zoned.max_open", NvmeNamespace,
821                        params.max_open_zones, 0),
822     DEFINE_PROP_UINT32("zoned.descr_ext_size", NvmeNamespace,
823                        params.zd_extension_size, 0),
824     DEFINE_PROP_UINT32("zoned.numzrwa", NvmeNamespace, params.numzrwa, 0),
825     DEFINE_PROP_SIZE("zoned.zrwas", NvmeNamespace, params.zrwas, 0),
826     DEFINE_PROP_SIZE("zoned.zrwafg", NvmeNamespace, params.zrwafg, -1),
827     DEFINE_PROP_BOOL("eui64-default", NvmeNamespace, params.eui64_default,
828                      false),
829     DEFINE_PROP_STRING("fdp.ruhs", NvmeNamespace, params.fdp.ruhs),
830     DEFINE_PROP_END_OF_LIST(),
831 };
832 
833 static void nvme_ns_class_init(ObjectClass *oc, void *data)
834 {
835     DeviceClass *dc = DEVICE_CLASS(oc);
836 
837     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
838 
839     dc->bus_type = TYPE_NVME_BUS;
840     dc->realize = nvme_ns_realize;
841     dc->unrealize = nvme_ns_unrealize;
842     device_class_set_props(dc, nvme_ns_props);
843     dc->desc = "Virtual NVMe namespace";
844 }
845 
846 static void nvme_ns_instance_init(Object *obj)
847 {
848     NvmeNamespace *ns = NVME_NS(obj);
849     char *bootindex = g_strdup_printf("/namespace@%d,0", ns->params.nsid);
850 
851     device_add_bootindex_property(obj, &ns->bootindex, "bootindex",
852                                   bootindex, DEVICE(obj));
853 
854     g_free(bootindex);
855 }
856 
857 static const TypeInfo nvme_ns_info = {
858     .name = TYPE_NVME_NS,
859     .parent = TYPE_DEVICE,
860     .class_init = nvme_ns_class_init,
861     .instance_size = sizeof(NvmeNamespace),
862     .instance_init = nvme_ns_instance_init,
863 };
864 
865 static void nvme_ns_register_types(void)
866 {
867     type_register_static(&nvme_ns_info);
868 }
869 
870 type_init(nvme_ns_register_types)
871