1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2012-2014 Intel Corporation 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/conf.h> 35 #include <sys/module.h> 36 37 #include <vm/uma.h> 38 39 #include "nvme_private.h" 40 41 struct nvme_consumer { 42 uint32_t id; 43 nvme_cons_ns_fn_t ns_fn; 44 nvme_cons_ctrlr_fn_t ctrlr_fn; 45 nvme_cons_async_fn_t async_fn; 46 nvme_cons_fail_fn_t fail_fn; 47 }; 48 49 struct nvme_consumer nvme_consumer[NVME_MAX_CONSUMERS]; 50 #define INVALID_CONSUMER_ID 0xFFFF 51 52 int32_t nvme_retry_count; 53 54 MALLOC_DEFINE(M_NVME, "nvme", "nvme(4) memory allocations"); 55 56 static void 57 nvme_init(void) 58 { 59 uint32_t i; 60 61 for (i = 0; i < NVME_MAX_CONSUMERS; i++) 62 nvme_consumer[i].id = INVALID_CONSUMER_ID; 63 } 64 65 SYSINIT(nvme_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_init, NULL); 66 67 static void 68 nvme_uninit(void) 69 { 70 } 71 72 SYSUNINIT(nvme_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_uninit, NULL); 73 74 int 75 nvme_shutdown(device_t dev) 76 { 77 struct nvme_controller *ctrlr; 78 79 ctrlr = DEVICE2SOFTC(dev); 80 nvme_ctrlr_shutdown(ctrlr); 81 82 return (0); 83 } 84 85 void 86 nvme_dump_command(struct nvme_command *cmd) 87 { 88 89 printf( 90 "opc:%x f:%x cid:%x nsid:%x r2:%x r3:%x mptr:%jx prp1:%jx prp2:%jx cdw:%x %x %x %x %x %x\n", 91 cmd->opc, cmd->fuse, cmd->cid, le32toh(cmd->nsid), 92 cmd->rsvd2, cmd->rsvd3, 93 (uintmax_t)le64toh(cmd->mptr), (uintmax_t)le64toh(cmd->prp1), (uintmax_t)le64toh(cmd->prp2), 94 le32toh(cmd->cdw10), le32toh(cmd->cdw11), le32toh(cmd->cdw12), 95 le32toh(cmd->cdw13), le32toh(cmd->cdw14), le32toh(cmd->cdw15)); 96 } 97 98 void 99 nvme_dump_completion(struct nvme_completion *cpl) 100 { 101 uint8_t p, sc, sct, crd, m, dnr; 102 uint16_t status; 103 104 status = le16toh(cpl->status); 105 106 p = NVME_STATUS_GET_P(status); 107 sc = NVME_STATUS_GET_SC(status); 108 sct = NVME_STATUS_GET_SCT(status); 109 crd = NVME_STATUS_GET_CRD( status); 110 m = NVME_STATUS_GET_M(status); 111 dnr = NVME_STATUS_GET_DNR(status); 112 113 printf("cdw0:%08x sqhd:%04x sqid:%04x " 114 "cid:%04x p:%x sc:%02x sct:%x crd:%x m:%x dnr:%x\n", 115 le32toh(cpl->cdw0), le16toh(cpl->sqhd), le16toh(cpl->sqid), 116 cpl->cid, p, sc, sct, crd, m, dnr); 117 } 118 119 int 120 nvme_attach(device_t dev) 121 { 122 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev); 123 int status; 124 125 status = nvme_ctrlr_construct(ctrlr, dev); 126 if (status != 0) { 127 nvme_ctrlr_destruct(ctrlr, dev); 128 return (status); 129 } 130 131 ctrlr->config_hook.ich_func = nvme_ctrlr_start_config_hook; 132 ctrlr->config_hook.ich_arg = ctrlr; 133 134 if (config_intrhook_establish(&ctrlr->config_hook) != 0) 135 return (ENOMEM); 136 137 return (0); 138 } 139 140 int 141 nvme_detach(device_t dev) 142 { 143 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev); 144 145 config_intrhook_drain(&ctrlr->config_hook); 146 147 nvme_ctrlr_destruct(ctrlr, dev); 148 return (0); 149 } 150 151 static void 152 nvme_notify(struct nvme_consumer *cons, 153 struct nvme_controller *ctrlr) 154 { 155 struct nvme_namespace *ns; 156 void *ctrlr_cookie; 157 int cmpset, ns_idx; 158 159 /* 160 * The consumer may register itself after the nvme devices 161 * have registered with the kernel, but before the 162 * driver has completed initialization. In that case, 163 * return here, and when initialization completes, the 164 * controller will make sure the consumer gets notified. 165 */ 166 if (!ctrlr->is_initialized) 167 return; 168 169 cmpset = atomic_cmpset_32(&ctrlr->notification_sent, 0, 1); 170 if (cmpset == 0) 171 return; 172 173 if (cons->ctrlr_fn != NULL) 174 ctrlr_cookie = (*cons->ctrlr_fn)(ctrlr); 175 else 176 ctrlr_cookie = (void *)(uintptr_t)0xdeadc0dedeadc0de; 177 ctrlr->cons_cookie[cons->id] = ctrlr_cookie; 178 179 /* ctrlr_fn has failed. Nothing to notify here any more. */ 180 if (ctrlr_cookie == NULL) 181 return; 182 183 if (ctrlr->is_failed) { 184 ctrlr->cons_cookie[cons->id] = NULL; 185 if (cons->fail_fn != NULL) 186 (*cons->fail_fn)(ctrlr_cookie); 187 /* 188 * Do not notify consumers about the namespaces of a 189 * failed controller. 190 */ 191 return; 192 } 193 for (ns_idx = 0; ns_idx < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); ns_idx++) { 194 ns = &ctrlr->ns[ns_idx]; 195 if (ns->data.nsze == 0) 196 continue; 197 if (cons->ns_fn != NULL) 198 ns->cons_cookie[cons->id] = 199 (*cons->ns_fn)(ns, ctrlr_cookie); 200 } 201 } 202 203 void 204 nvme_notify_new_controller(struct nvme_controller *ctrlr) 205 { 206 int i; 207 208 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 209 if (nvme_consumer[i].id != INVALID_CONSUMER_ID) { 210 nvme_notify(&nvme_consumer[i], ctrlr); 211 } 212 } 213 } 214 215 static void 216 nvme_notify_new_consumer(struct nvme_consumer *cons) 217 { 218 device_t *devlist; 219 struct nvme_controller *ctrlr; 220 int dev_idx, devcount; 221 222 if (devclass_get_devices(devclass_find("nvme"), &devlist, &devcount)) 223 return; 224 225 for (dev_idx = 0; dev_idx < devcount; dev_idx++) { 226 ctrlr = DEVICE2SOFTC(devlist[dev_idx]); 227 nvme_notify(cons, ctrlr); 228 } 229 230 free(devlist, M_TEMP); 231 } 232 233 void 234 nvme_notify_async_consumers(struct nvme_controller *ctrlr, 235 const struct nvme_completion *async_cpl, 236 uint32_t log_page_id, void *log_page_buffer, 237 uint32_t log_page_size) 238 { 239 struct nvme_consumer *cons; 240 void *ctrlr_cookie; 241 uint32_t i; 242 243 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 244 cons = &nvme_consumer[i]; 245 if (cons->id != INVALID_CONSUMER_ID && cons->async_fn != NULL && 246 (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) { 247 (*cons->async_fn)(ctrlr_cookie, async_cpl, 248 log_page_id, log_page_buffer, log_page_size); 249 } 250 } 251 } 252 253 void 254 nvme_notify_fail_consumers(struct nvme_controller *ctrlr) 255 { 256 struct nvme_consumer *cons; 257 void *ctrlr_cookie; 258 uint32_t i; 259 260 /* 261 * This controller failed during initialization (i.e. IDENTIFY 262 * command failed or timed out). Do not notify any nvme 263 * consumers of the failure here, since the consumer does not 264 * even know about the controller yet. 265 */ 266 if (!ctrlr->is_initialized) 267 return; 268 269 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 270 cons = &nvme_consumer[i]; 271 if (cons->id != INVALID_CONSUMER_ID && 272 (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) { 273 ctrlr->cons_cookie[i] = NULL; 274 if (cons->fail_fn != NULL) 275 cons->fail_fn(ctrlr_cookie); 276 } 277 } 278 } 279 280 void 281 nvme_notify_ns(struct nvme_controller *ctrlr, int nsid) 282 { 283 struct nvme_consumer *cons; 284 struct nvme_namespace *ns; 285 void *ctrlr_cookie; 286 uint32_t i; 287 288 KASSERT(nsid <= NVME_MAX_NAMESPACES, 289 ("%s: Namespace notification to nsid %d exceeds range\n", 290 device_get_nameunit(ctrlr->dev), nsid)); 291 292 if (!ctrlr->is_initialized) 293 return; 294 295 ns = &ctrlr->ns[nsid - 1]; 296 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 297 cons = &nvme_consumer[i]; 298 if (cons->id != INVALID_CONSUMER_ID && cons->ns_fn != NULL && 299 (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) 300 ns->cons_cookie[i] = (*cons->ns_fn)(ns, ctrlr_cookie); 301 } 302 } 303 304 struct nvme_consumer * 305 nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, nvme_cons_ctrlr_fn_t ctrlr_fn, 306 nvme_cons_async_fn_t async_fn, 307 nvme_cons_fail_fn_t fail_fn) 308 { 309 int i; 310 311 /* 312 * TODO: add locking around consumer registration. 313 */ 314 for (i = 0; i < NVME_MAX_CONSUMERS; i++) 315 if (nvme_consumer[i].id == INVALID_CONSUMER_ID) { 316 nvme_consumer[i].id = i; 317 nvme_consumer[i].ns_fn = ns_fn; 318 nvme_consumer[i].ctrlr_fn = ctrlr_fn; 319 nvme_consumer[i].async_fn = async_fn; 320 nvme_consumer[i].fail_fn = fail_fn; 321 322 nvme_notify_new_consumer(&nvme_consumer[i]); 323 return (&nvme_consumer[i]); 324 } 325 326 printf("nvme(4): consumer not registered - no slots available\n"); 327 return (NULL); 328 } 329 330 void 331 nvme_unregister_consumer(struct nvme_consumer *consumer) 332 { 333 334 consumer->id = INVALID_CONSUMER_ID; 335 } 336 337 void 338 nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl) 339 { 340 struct nvme_completion_poll_status *status = arg; 341 342 /* 343 * Copy status into the argument passed by the caller, so that 344 * the caller can check the status to determine if the 345 * the request passed or failed. 346 */ 347 memcpy(&status->cpl, cpl, sizeof(*cpl)); 348 atomic_store_rel_int(&status->done, 1); 349 } 350 351 static int 352 nvme_modevent(module_t mod __unused, int type __unused, void *argp __unused) 353 { 354 return (0); 355 } 356 357 static moduledata_t nvme_mod = { 358 "nvme", 359 nvme_modevent, 360 0 361 }; 362 363 DECLARE_MODULE(nvme, nvme_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 364 MODULE_VERSION(nvme, 1); 365 MODULE_DEPEND(nvme, cam, 1, 1, 1); 366