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