1 /*- 2 * Copyright (c) 2017 The FreeBSD Foundation 3 * All rights reserved. 4 * Copyright (c) 2018, 2019 Intel Corporation 5 * 6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #ifndef __DEV_NVDIMM_VAR_H__ 34 #define __DEV_NVDIMM_VAR_H__ 35 36 #define NVDIMM_INDEX_BLOCK_SIGNATURE "NAMESPACE_INDEX" 37 38 struct nvdimm_label_index { 39 char signature[16]; 40 uint8_t flags[3]; 41 uint8_t label_size; 42 uint32_t seq; 43 uint64_t this_offset; 44 uint64_t this_size; 45 uint64_t other_offset; 46 uint64_t label_offset; 47 uint32_t slot_cnt; 48 uint16_t rev_major; 49 uint16_t rev_minor; 50 uint64_t checksum; 51 uint8_t free[0]; 52 }; 53 54 struct nvdimm_label { 55 struct uuid uuid; 56 char name[64]; 57 uint32_t flags; 58 uint16_t nlabel; 59 uint16_t position; 60 uint64_t set_cookie; 61 uint64_t lba_size; 62 uint64_t dimm_phys_addr; 63 uint64_t raw_size; 64 uint32_t slot; 65 uint8_t alignment; 66 uint8_t reserved[3]; 67 struct uuid type_guid; 68 struct uuid address_abstraction_guid; 69 uint8_t reserved1[88]; 70 uint64_t checksum; 71 }; 72 73 struct nvdimm_label_entry { 74 SLIST_ENTRY(nvdimm_label_entry) link; 75 struct nvdimm_label label; 76 }; 77 78 _Static_assert(sizeof(struct nvdimm_label_index) == 72, "Incorrect layout"); 79 _Static_assert(sizeof(struct nvdimm_label) == 256, "Incorrect layout"); 80 81 typedef uint32_t nfit_handle_t; 82 83 enum nvdimm_acpi_ivar { 84 NVDIMM_ROOT_IVAR_ACPI_HANDLE, 85 NVDIMM_ROOT_IVAR_DEVICE_HANDLE, 86 NVDIMM_ROOT_IVAR_MAX, 87 }; 88 __BUS_ACCESSOR(nvdimm_root, acpi_handle, NVDIMM_ROOT, ACPI_HANDLE, ACPI_HANDLE) 89 __BUS_ACCESSOR(nvdimm_root, device_handle, NVDIMM_ROOT, DEVICE_HANDLE, 90 nfit_handle_t) 91 92 struct nvdimm_dev { 93 device_t nv_dev; 94 nfit_handle_t nv_handle; 95 uint64_t **nv_flush_addr; 96 int nv_flush_addr_cnt; 97 uint32_t label_area_size; 98 uint32_t max_label_xfer; 99 struct nvdimm_label_index *label_index; 100 SLIST_HEAD(, nvdimm_label_entry) labels; 101 }; 102 103 enum SPA_mapping_type { 104 SPA_TYPE_VOLATILE_MEMORY = 0, 105 SPA_TYPE_PERSISTENT_MEMORY = 1, 106 SPA_TYPE_CONTROL_REGION = 2, 107 SPA_TYPE_DATA_REGION = 3, 108 SPA_TYPE_VOLATILE_VIRTUAL_DISK = 4, 109 SPA_TYPE_VOLATILE_VIRTUAL_CD = 5, 110 SPA_TYPE_PERSISTENT_VIRTUAL_DISK= 6, 111 SPA_TYPE_PERSISTENT_VIRTUAL_CD = 7, 112 SPA_TYPE_UNKNOWN = 127, 113 }; 114 115 struct nvdimm_spa_dev { 116 int spa_domain; 117 vm_memattr_t spa_memattr; 118 uint64_t spa_phys_base; 119 uint64_t spa_len; 120 uint64_t spa_efi_mem_flags; 121 void *spa_kva; 122 struct vm_object *spa_obj; 123 struct cdev *spa_dev; 124 struct g_geom *spa_g; 125 }; 126 127 struct g_spa { 128 struct nvdimm_spa_dev *dev; 129 struct g_provider *spa_p; 130 struct bio_queue_head spa_g_queue; 131 struct mtx spa_g_mtx; 132 struct devstat *spa_g_devstat; 133 struct proc *spa_g_proc; 134 bool spa_g_proc_run; 135 bool spa_g_proc_exiting; 136 }; 137 138 struct nvdimm_namespace { 139 SLIST_ENTRY(nvdimm_namespace) link; 140 struct SPA_mapping *spa; 141 struct nvdimm_spa_dev dev; 142 }; 143 144 struct SPA_mapping { 145 SLIST_ENTRY(SPA_mapping) link; 146 enum SPA_mapping_type spa_type; 147 int spa_nfit_idx; 148 struct nvdimm_spa_dev dev; 149 SLIST_HEAD(, nvdimm_namespace) namespaces; 150 }; 151 152 MALLOC_DECLARE(M_NVDIMM); 153 154 void acpi_nfit_get_dimm_ids(ACPI_TABLE_NFIT *nfitbl, nfit_handle_t **listp, 155 int *countp); 156 void acpi_nfit_get_spa_range(ACPI_TABLE_NFIT *nfitbl, uint16_t range_index, 157 ACPI_NFIT_SYSTEM_ADDRESS **spa); 158 void acpi_nfit_get_spa_ranges(ACPI_TABLE_NFIT *nfitbl, 159 ACPI_NFIT_SYSTEM_ADDRESS ***listp, int *countp); 160 void acpi_nfit_get_region_mappings_by_spa_range(ACPI_TABLE_NFIT *nfitbl, 161 uint16_t spa_range_index, ACPI_NFIT_MEMORY_MAP ***listp, int *countp); 162 void acpi_nfit_get_control_region(ACPI_TABLE_NFIT *nfitbl, 163 uint16_t control_region_index, ACPI_NFIT_CONTROL_REGION **out); 164 void acpi_nfit_get_flush_addrs(ACPI_TABLE_NFIT *nfitbl, nfit_handle_t dimm, 165 uint64_t ***listp, int *countp); 166 enum SPA_mapping_type nvdimm_spa_type_from_name(const char *); 167 enum SPA_mapping_type nvdimm_spa_type_from_uuid(struct uuid *); 168 bool nvdimm_spa_type_user_accessible(enum SPA_mapping_type); 169 struct nvdimm_dev *nvdimm_find_by_handle(nfit_handle_t nv_handle); 170 int nvdimm_spa_init(struct SPA_mapping *spa, ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr, 171 enum SPA_mapping_type spa_type); 172 void nvdimm_spa_fini(struct SPA_mapping *spa); 173 int nvdimm_spa_dev_init(struct nvdimm_spa_dev *dev, const char *name, int unit); 174 void nvdimm_spa_dev_fini(struct nvdimm_spa_dev *dev); 175 int nvdimm_create_namespaces(struct SPA_mapping *spa, ACPI_TABLE_NFIT *nfitbl); 176 void nvdimm_destroy_namespaces(struct SPA_mapping *spa); 177 178 #endif /* __DEV_NVDIMM_VAR_H__ */ 179