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 uma_zone_t nvme_request_zone; 53 int32_t nvme_retry_count; 54 55 56 MALLOC_DEFINE(M_NVME, "nvme", "nvme(4) memory allocations"); 57 58 devclass_t nvme_devclass; 59 60 static void 61 nvme_init(void) 62 { 63 uint32_t i; 64 65 nvme_request_zone = uma_zcreate("nvme_request", 66 sizeof(struct nvme_request), NULL, NULL, NULL, NULL, 0, 0); 67 68 for (i = 0; i < NVME_MAX_CONSUMERS; i++) 69 nvme_consumer[i].id = INVALID_CONSUMER_ID; 70 } 71 72 SYSINIT(nvme_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_init, NULL); 73 74 static void 75 nvme_uninit(void) 76 { 77 uma_zdestroy(nvme_request_zone); 78 } 79 80 SYSUNINIT(nvme_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_uninit, NULL); 81 82 int 83 nvme_shutdown(device_t dev) 84 { 85 struct nvme_controller *ctrlr; 86 87 ctrlr = DEVICE2SOFTC(dev); 88 nvme_ctrlr_shutdown(ctrlr); 89 90 return (0); 91 } 92 93 void 94 nvme_dump_command(struct nvme_command *cmd) 95 { 96 97 printf( 98 "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", 99 cmd->opc, cmd->fuse, cmd->cid, le32toh(cmd->nsid), 100 cmd->rsvd2, cmd->rsvd3, 101 (uintmax_t)le64toh(cmd->mptr), (uintmax_t)le64toh(cmd->prp1), (uintmax_t)le64toh(cmd->prp2), 102 le32toh(cmd->cdw10), le32toh(cmd->cdw11), le32toh(cmd->cdw12), 103 le32toh(cmd->cdw13), le32toh(cmd->cdw14), le32toh(cmd->cdw15)); 104 } 105 106 void 107 nvme_dump_completion(struct nvme_completion *cpl) 108 { 109 uint8_t p, sc, sct, m, dnr; 110 uint16_t status; 111 112 status = le16toh(cpl->status); 113 114 p = NVME_STATUS_GET_P(status); 115 sc = NVME_STATUS_GET_SC(status); 116 sct = NVME_STATUS_GET_SCT(status); 117 m = NVME_STATUS_GET_M(status); 118 dnr = NVME_STATUS_GET_DNR(status); 119 120 printf("cdw0:%08x sqhd:%04x sqid:%04x " 121 "cid:%04x p:%x sc:%02x sct:%x m:%x dnr:%x\n", 122 le32toh(cpl->cdw0), le16toh(cpl->sqhd), le16toh(cpl->sqid), 123 cpl->cid, p, sc, sct, m, dnr); 124 } 125 126 int 127 nvme_attach(device_t dev) 128 { 129 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev); 130 int status; 131 132 status = nvme_ctrlr_construct(ctrlr, dev); 133 if (status != 0) { 134 nvme_ctrlr_destruct(ctrlr, dev); 135 return (status); 136 } 137 138 ctrlr->config_hook.ich_func = nvme_ctrlr_start_config_hook; 139 ctrlr->config_hook.ich_arg = ctrlr; 140 141 if (config_intrhook_establish(&ctrlr->config_hook) != 0) 142 return (ENOMEM); 143 144 return (0); 145 } 146 147 int 148 nvme_detach (device_t dev) 149 { 150 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev); 151 152 nvme_ctrlr_destruct(ctrlr, dev); 153 return (0); 154 } 155 156 static void 157 nvme_notify(struct nvme_consumer *cons, 158 struct nvme_controller *ctrlr) 159 { 160 struct nvme_namespace *ns; 161 void *ctrlr_cookie; 162 int cmpset, ns_idx; 163 164 /* 165 * The consumer may register itself after the nvme devices 166 * have registered with the kernel, but before the 167 * driver has completed initialization. In that case, 168 * return here, and when initialization completes, the 169 * controller will make sure the consumer gets notified. 170 */ 171 if (!ctrlr->is_initialized) 172 return; 173 174 cmpset = atomic_cmpset_32(&ctrlr->notification_sent, 0, 1); 175 if (cmpset == 0) 176 return; 177 178 if (cons->ctrlr_fn != NULL) 179 ctrlr_cookie = (*cons->ctrlr_fn)(ctrlr); 180 else 181 ctrlr_cookie = (void *)(uintptr_t)0xdeadc0dedeadc0de; 182 ctrlr->cons_cookie[cons->id] = ctrlr_cookie; 183 184 /* ctrlr_fn has failed. Nothing to notify here any more. */ 185 if (ctrlr_cookie == NULL) 186 return; 187 188 if (ctrlr->is_failed) { 189 ctrlr->cons_cookie[cons->id] = NULL; 190 if (cons->fail_fn != NULL) 191 (*cons->fail_fn)(ctrlr_cookie); 192 /* 193 * Do not notify consumers about the namespaces of a 194 * failed controller. 195 */ 196 return; 197 } 198 for (ns_idx = 0; ns_idx < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); ns_idx++) { 199 ns = &ctrlr->ns[ns_idx]; 200 if (ns->data.nsze == 0) 201 continue; 202 if (cons->ns_fn != NULL) 203 ns->cons_cookie[cons->id] = 204 (*cons->ns_fn)(ns, ctrlr_cookie); 205 } 206 } 207 208 void 209 nvme_notify_new_controller(struct nvme_controller *ctrlr) 210 { 211 int i; 212 213 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 214 if (nvme_consumer[i].id != INVALID_CONSUMER_ID) { 215 nvme_notify(&nvme_consumer[i], ctrlr); 216 } 217 } 218 } 219 220 static void 221 nvme_notify_new_consumer(struct nvme_consumer *cons) 222 { 223 device_t *devlist; 224 struct nvme_controller *ctrlr; 225 int dev_idx, devcount; 226 227 if (devclass_get_devices(nvme_devclass, &devlist, &devcount)) 228 return; 229 230 for (dev_idx = 0; dev_idx < devcount; dev_idx++) { 231 ctrlr = DEVICE2SOFTC(devlist[dev_idx]); 232 nvme_notify(cons, ctrlr); 233 } 234 235 free(devlist, M_TEMP); 236 } 237 238 void 239 nvme_notify_async_consumers(struct nvme_controller *ctrlr, 240 const struct nvme_completion *async_cpl, 241 uint32_t log_page_id, void *log_page_buffer, 242 uint32_t log_page_size) 243 { 244 struct nvme_consumer *cons; 245 void *ctrlr_cookie; 246 uint32_t i; 247 248 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 249 cons = &nvme_consumer[i]; 250 if (cons->id != INVALID_CONSUMER_ID && cons->async_fn != NULL && 251 (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) { 252 (*cons->async_fn)(ctrlr_cookie, async_cpl, 253 log_page_id, log_page_buffer, log_page_size); 254 } 255 } 256 } 257 258 void 259 nvme_notify_fail_consumers(struct nvme_controller *ctrlr) 260 { 261 struct nvme_consumer *cons; 262 void *ctrlr_cookie; 263 uint32_t i; 264 265 /* 266 * This controller failed during initialization (i.e. IDENTIFY 267 * command failed or timed out). Do not notify any nvme 268 * consumers of the failure here, since the consumer does not 269 * even know about the controller yet. 270 */ 271 if (!ctrlr->is_initialized) 272 return; 273 274 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 275 cons = &nvme_consumer[i]; 276 if (cons->id != INVALID_CONSUMER_ID && 277 (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) { 278 ctrlr->cons_cookie[i] = NULL; 279 if (cons->fail_fn != NULL) 280 cons->fail_fn(ctrlr_cookie); 281 } 282 } 283 } 284 285 void 286 nvme_notify_ns(struct nvme_controller *ctrlr, int nsid) 287 { 288 struct nvme_consumer *cons; 289 struct nvme_namespace *ns; 290 void *ctrlr_cookie; 291 uint32_t i; 292 293 KASSERT(nsid <= NVME_MAX_NAMESPACES, 294 ("%s: Namespace notification to nsid %d exceeds range\n", 295 device_get_nameunit(ctrlr->dev), nsid)); 296 297 if (!ctrlr->is_initialized) 298 return; 299 300 ns = &ctrlr->ns[nsid - 1]; 301 for (i = 0; i < NVME_MAX_CONSUMERS; i++) { 302 cons = &nvme_consumer[i]; 303 if (cons->id != INVALID_CONSUMER_ID && cons->ns_fn != NULL && 304 (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) 305 ns->cons_cookie[i] = (*cons->ns_fn)(ns, ctrlr_cookie); 306 } 307 } 308 309 struct nvme_consumer * 310 nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, nvme_cons_ctrlr_fn_t ctrlr_fn, 311 nvme_cons_async_fn_t async_fn, 312 nvme_cons_fail_fn_t fail_fn) 313 { 314 int i; 315 316 /* 317 * TODO: add locking around consumer registration. 318 */ 319 for (i = 0; i < NVME_MAX_CONSUMERS; i++) 320 if (nvme_consumer[i].id == INVALID_CONSUMER_ID) { 321 nvme_consumer[i].id = i; 322 nvme_consumer[i].ns_fn = ns_fn; 323 nvme_consumer[i].ctrlr_fn = ctrlr_fn; 324 nvme_consumer[i].async_fn = async_fn; 325 nvme_consumer[i].fail_fn = fail_fn; 326 327 nvme_notify_new_consumer(&nvme_consumer[i]); 328 return (&nvme_consumer[i]); 329 } 330 331 printf("nvme(4): consumer not registered - no slots available\n"); 332 return (NULL); 333 } 334 335 void 336 nvme_unregister_consumer(struct nvme_consumer *consumer) 337 { 338 339 consumer->id = INVALID_CONSUMER_ID; 340 } 341 342 void 343 nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl) 344 { 345 struct nvme_completion_poll_status *status = arg; 346 347 /* 348 * Copy status into the argument passed by the caller, so that 349 * the caller can check the status to determine if the 350 * the request passed or failed. 351 */ 352 memcpy(&status->cpl, cpl, sizeof(*cpl)); 353 atomic_store_rel_int(&status->done, 1); 354 } 355 356 static int 357 nvme_modevent(module_t mod __unused, int type __unused, void *argp __unused) 358 { 359 return (0); 360 } 361 362 static moduledata_t nvme_mod = { 363 "nvme", 364 nvme_modevent, 365 0 366 }; 367 368 DECLARE_MODULE(nvme, nvme_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 369 MODULE_VERSION(nvme, 1); 370 MODULE_DEPEND(nvme, cam, 1, 1, 1); 371