xref: /freebsd/sys/dev/nvme/nvme.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1bb0ec6b3SJim Harris /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4496a2752SJim Harris  * Copyright (C) 2012-2014 Intel Corporation
5bb0ec6b3SJim Harris  * All rights reserved.
6bb0ec6b3SJim Harris  *
7bb0ec6b3SJim Harris  * Redistribution and use in source and binary forms, with or without
8bb0ec6b3SJim Harris  * modification, are permitted provided that the following conditions
9bb0ec6b3SJim Harris  * are met:
10bb0ec6b3SJim Harris  * 1. Redistributions of source code must retain the above copyright
11bb0ec6b3SJim Harris  *    notice, this list of conditions and the following disclaimer.
12bb0ec6b3SJim Harris  * 2. Redistributions in binary form must reproduce the above copyright
13bb0ec6b3SJim Harris  *    notice, this list of conditions and the following disclaimer in the
14bb0ec6b3SJim Harris  *    documentation and/or other materials provided with the distribution.
15bb0ec6b3SJim Harris  *
16bb0ec6b3SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17bb0ec6b3SJim Harris  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bb0ec6b3SJim Harris  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bb0ec6b3SJim Harris  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bb0ec6b3SJim Harris  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bb0ec6b3SJim Harris  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bb0ec6b3SJim Harris  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bb0ec6b3SJim Harris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bb0ec6b3SJim Harris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bb0ec6b3SJim Harris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bb0ec6b3SJim Harris  * SUCH DAMAGE.
27bb0ec6b3SJim Harris  */
28bb0ec6b3SJim Harris 
29bb0ec6b3SJim Harris #include <sys/param.h>
30bb0ec6b3SJim Harris #include <sys/bus.h>
31bb0ec6b3SJim Harris #include <sys/conf.h>
32bb0ec6b3SJim Harris #include <sys/module.h>
33bb0ec6b3SJim Harris 
34ad697276SJim Harris #include <vm/uma.h>
35ad697276SJim Harris 
36bb0ec6b3SJim Harris #include "nvme_private.h"
37bb0ec6b3SJim Harris 
38bb0ec6b3SJim Harris struct nvme_consumer {
39038a5ee4SJim Harris 	uint32_t		id;
40038a5ee4SJim Harris 	nvme_cons_ns_fn_t	ns_fn;
41038a5ee4SJim Harris 	nvme_cons_ctrlr_fn_t	ctrlr_fn;
42038a5ee4SJim Harris 	nvme_cons_async_fn_t	async_fn;
43232e2edbSJim Harris 	nvme_cons_fail_fn_t	fail_fn;
44bb0ec6b3SJim Harris };
45bb0ec6b3SJim Harris 
46bb0ec6b3SJim Harris struct nvme_consumer nvme_consumer[NVME_MAX_CONSUMERS];
47038a5ee4SJim Harris #define	INVALID_CONSUMER_ID	0xFFFF
48bb0ec6b3SJim Harris 
49cb5b7c13SJim Harris int32_t		nvme_retry_count;
50c75bdc04SWarner Losh 
51bb0ec6b3SJim Harris MALLOC_DEFINE(M_NVME, "nvme", "nvme(4) memory allocations");
52bb0ec6b3SJim Harris 
53bb0ec6b3SJim Harris static void
nvme_init(void)54ad697276SJim Harris nvme_init(void)
55ad697276SJim Harris {
56038a5ee4SJim Harris 	uint32_t	i;
57038a5ee4SJim Harris 
58038a5ee4SJim Harris 	for (i = 0; i < NVME_MAX_CONSUMERS; i++)
59038a5ee4SJim Harris 		nvme_consumer[i].id = INVALID_CONSUMER_ID;
60ad697276SJim Harris }
61ad697276SJim Harris 
62ad697276SJim Harris SYSINIT(nvme_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_init, NULL);
63ad697276SJim Harris 
64ad697276SJim Harris static void
nvme_uninit(void)65ad697276SJim Harris nvme_uninit(void)
66ad697276SJim Harris {
67ad697276SJim Harris }
68ad697276SJim Harris 
69ad697276SJim Harris SYSUNINIT(nvme_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_uninit, NULL);
70ad697276SJim Harris 
71f182f928SWarner Losh int
nvme_shutdown(device_t dev)72c670f31fSNathan Whitehorn nvme_shutdown(device_t dev)
73bb0ec6b3SJim Harris {
74bb0ec6b3SJim Harris 	struct nvme_controller	*ctrlr;
75bb0ec6b3SJim Harris 
76c670f31fSNathan Whitehorn 	ctrlr = DEVICE2SOFTC(dev);
7756183abcSJim Harris 	nvme_ctrlr_shutdown(ctrlr);
78bb0ec6b3SJim Harris 
79c670f31fSNathan Whitehorn 	return (0);
80bb0ec6b3SJim Harris }
81bb0ec6b3SJim Harris 
82f182f928SWarner Losh int
nvme_attach(device_t dev)83bb0ec6b3SJim Harris nvme_attach(device_t dev)
84bb0ec6b3SJim Harris {
85bb0ec6b3SJim Harris 	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
86bb0ec6b3SJim Harris 	int			status;
87bb0ec6b3SJim Harris 
88bb0ec6b3SJim Harris 	status = nvme_ctrlr_construct(ctrlr, dev);
897aa27dbaSJim Harris 	if (status != 0) {
907aa27dbaSJim Harris 		nvme_ctrlr_destruct(ctrlr, dev);
91bb0ec6b3SJim Harris 		return (status);
927aa27dbaSJim Harris 	}
93bb0ec6b3SJim Harris 
94be34f216SJim Harris 	ctrlr->config_hook.ich_func = nvme_ctrlr_start_config_hook;
95bb0ec6b3SJim Harris 	ctrlr->config_hook.ich_arg = ctrlr;
96bb0ec6b3SJim Harris 
974053f8acSDavid Bright 	if (config_intrhook_establish(&ctrlr->config_hook) != 0)
984053f8acSDavid Bright 		return (ENOMEM);
99bb0ec6b3SJim Harris 
100bb0ec6b3SJim Harris 	return (0);
101bb0ec6b3SJim Harris }
102bb0ec6b3SJim Harris 
103f182f928SWarner Losh int
nvme_detach(device_t dev)104bb0ec6b3SJim Harris nvme_detach(device_t dev)
105bb0ec6b3SJim Harris {
106bb0ec6b3SJim Harris 	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
10746fbd800SAlexander Motin 
1088423f5d4SWarner Losh 	config_intrhook_drain(&ctrlr->config_hook);
109bb0ec6b3SJim Harris 
110990e741cSJim Harris 	nvme_ctrlr_destruct(ctrlr, dev);
111bb0ec6b3SJim Harris 	return (0);
112bb0ec6b3SJim Harris }
113bb0ec6b3SJim Harris 
114bb0ec6b3SJim Harris static void
nvme_notify(struct nvme_consumer * cons,struct nvme_controller * ctrlr)115496a2752SJim Harris nvme_notify(struct nvme_consumer *cons,
116496a2752SJim Harris 	    struct nvme_controller *ctrlr)
117bb0ec6b3SJim Harris {
118038a5ee4SJim Harris 	struct nvme_namespace	*ns;
119038a5ee4SJim Harris 	void			*ctrlr_cookie;
120496a2752SJim Harris 	int			cmpset, ns_idx;
121bb0ec6b3SJim Harris 
122496a2752SJim Harris 	/*
123496a2752SJim Harris 	 * The consumer may register itself after the nvme devices
124496a2752SJim Harris 	 *  have registered with the kernel, but before the
125496a2752SJim Harris 	 *  driver has completed initialization.  In that case,
126496a2752SJim Harris 	 *  return here, and when initialization completes, the
127496a2752SJim Harris 	 *  controller will make sure the consumer gets notified.
128496a2752SJim Harris 	 */
129496a2752SJim Harris 	if (!ctrlr->is_initialized)
130bb0ec6b3SJim Harris 		return;
131bb0ec6b3SJim Harris 
132496a2752SJim Harris 	cmpset = atomic_cmpset_32(&ctrlr->notification_sent, 0, 1);
133496a2752SJim Harris 	if (cmpset == 0)
134496a2752SJim Harris 		return;
135496a2752SJim Harris 
136038a5ee4SJim Harris 	if (cons->ctrlr_fn != NULL)
137038a5ee4SJim Harris 		ctrlr_cookie = (*cons->ctrlr_fn)(ctrlr);
138038a5ee4SJim Harris 	else
13951b92c1aSAlexander Motin 		ctrlr_cookie = (void *)(uintptr_t)0xdeadc0dedeadc0de;
140038a5ee4SJim Harris 	ctrlr->cons_cookie[cons->id] = ctrlr_cookie;
14151b92c1aSAlexander Motin 
14251b92c1aSAlexander Motin 	/* ctrlr_fn has failed.  Nothing to notify here any more. */
1439a5acf36SDag-Erling Smørgrav 	if (ctrlr_cookie == NULL) {
1449a5acf36SDag-Erling Smørgrav 		(void)atomic_cmpset_32(&ctrlr->notification_sent, 1, 0);
14551b92c1aSAlexander Motin 		return;
1469a5acf36SDag-Erling Smørgrav 	}
14751b92c1aSAlexander Motin 
148086d23cfSJim Harris 	if (ctrlr->is_failed) {
14951b92c1aSAlexander Motin 		ctrlr->cons_cookie[cons->id] = NULL;
150086d23cfSJim Harris 		if (cons->fail_fn != NULL)
151086d23cfSJim Harris 			(*cons->fail_fn)(ctrlr_cookie);
152086d23cfSJim Harris 		/*
153086d23cfSJim Harris 		 * Do not notify consumers about the namespaces of a
154086d23cfSJim Harris 		 *  failed controller.
155086d23cfSJim Harris 		 */
156496a2752SJim Harris 		return;
157086d23cfSJim Harris 	}
158a8a18dd5SWarner Losh 	for (ns_idx = 0; ns_idx < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); ns_idx++) {
159038a5ee4SJim Harris 		ns = &ctrlr->ns[ns_idx];
160a8a18dd5SWarner Losh 		if (ns->data.nsze == 0)
161a8a18dd5SWarner Losh 			continue;
162038a5ee4SJim Harris 		if (cons->ns_fn != NULL)
163038a5ee4SJim Harris 			ns->cons_cookie[cons->id] =
164038a5ee4SJim Harris 			    (*cons->ns_fn)(ns, ctrlr_cookie);
165038a5ee4SJim Harris 	}
166bb0ec6b3SJim Harris }
167bb0ec6b3SJim Harris 
168496a2752SJim Harris void
nvme_notify_new_controller(struct nvme_controller * ctrlr)169496a2752SJim Harris nvme_notify_new_controller(struct nvme_controller *ctrlr)
170496a2752SJim Harris {
171496a2752SJim Harris 	int i;
172496a2752SJim Harris 
173496a2752SJim Harris 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
174496a2752SJim Harris 		if (nvme_consumer[i].id != INVALID_CONSUMER_ID) {
175496a2752SJim Harris 			nvme_notify(&nvme_consumer[i], ctrlr);
176496a2752SJim Harris 		}
177496a2752SJim Harris 	}
178496a2752SJim Harris }
179496a2752SJim Harris 
180496a2752SJim Harris static void
nvme_notify_new_consumer(struct nvme_consumer * cons)181496a2752SJim Harris nvme_notify_new_consumer(struct nvme_consumer *cons)
182496a2752SJim Harris {
183496a2752SJim Harris 	device_t		*devlist;
184496a2752SJim Harris 	struct nvme_controller	*ctrlr;
185496a2752SJim Harris 	int			dev_idx, devcount;
186496a2752SJim Harris 
18782496a25SJohn Baldwin 	if (devclass_get_devices(devclass_find("nvme"), &devlist, &devcount))
188496a2752SJim Harris 		return;
189496a2752SJim Harris 
190496a2752SJim Harris 	for (dev_idx = 0; dev_idx < devcount; dev_idx++) {
191496a2752SJim Harris 		ctrlr = DEVICE2SOFTC(devlist[dev_idx]);
192496a2752SJim Harris 		nvme_notify(cons, ctrlr);
193496a2752SJim Harris 	}
194496a2752SJim Harris 
195bb0ec6b3SJim Harris 	free(devlist, M_TEMP);
196bb0ec6b3SJim Harris }
197bb0ec6b3SJim Harris 
198038a5ee4SJim Harris 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)199038a5ee4SJim Harris nvme_notify_async_consumers(struct nvme_controller *ctrlr,
2000d7e13ecSJim Harris 			    const struct nvme_completion *async_cpl,
2010d7e13ecSJim Harris 			    uint32_t log_page_id, void *log_page_buffer,
2020d7e13ecSJim Harris 			    uint32_t log_page_size)
203038a5ee4SJim Harris {
204038a5ee4SJim Harris 	struct nvme_consumer	*cons;
20551b92c1aSAlexander Motin 	void			*ctrlr_cookie;
206038a5ee4SJim Harris 	uint32_t		i;
207038a5ee4SJim Harris 
208038a5ee4SJim Harris 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
209038a5ee4SJim Harris 		cons = &nvme_consumer[i];
21051b92c1aSAlexander Motin 		if (cons->id != INVALID_CONSUMER_ID && cons->async_fn != NULL &&
21151b92c1aSAlexander Motin 		    (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) {
21251b92c1aSAlexander Motin 			(*cons->async_fn)(ctrlr_cookie, async_cpl,
2130d7e13ecSJim Harris 			    log_page_id, log_page_buffer, log_page_size);
214038a5ee4SJim Harris 		}
215038a5ee4SJim Harris 	}
21651b92c1aSAlexander Motin }
217038a5ee4SJim Harris 
218232e2edbSJim Harris void
nvme_notify_fail_consumers(struct nvme_controller * ctrlr)219232e2edbSJim Harris nvme_notify_fail_consumers(struct nvme_controller *ctrlr)
220232e2edbSJim Harris {
221232e2edbSJim Harris 	struct nvme_consumer	*cons;
22251b92c1aSAlexander Motin 	void			*ctrlr_cookie;
223232e2edbSJim Harris 	uint32_t		i;
224232e2edbSJim Harris 
2250e1fd2ddSJim Harris 	/*
2260e1fd2ddSJim Harris 	 * This controller failed during initialization (i.e. IDENTIFY
2270e1fd2ddSJim Harris 	 *  command failed or timed out).  Do not notify any nvme
2280e1fd2ddSJim Harris 	 *  consumers of the failure here, since the consumer does not
2290e1fd2ddSJim Harris 	 *  even know about the controller yet.
2300e1fd2ddSJim Harris 	 */
2310e1fd2ddSJim Harris 	if (!ctrlr->is_initialized)
2320e1fd2ddSJim Harris 		return;
2330e1fd2ddSJim Harris 
234232e2edbSJim Harris 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
235232e2edbSJim Harris 		cons = &nvme_consumer[i];
23651b92c1aSAlexander Motin 		if (cons->id != INVALID_CONSUMER_ID &&
23751b92c1aSAlexander Motin 		    (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) {
23851b92c1aSAlexander Motin 			ctrlr->cons_cookie[i] = NULL;
23951b92c1aSAlexander Motin 			if (cons->fail_fn != NULL)
24051b92c1aSAlexander Motin 				cons->fail_fn(ctrlr_cookie);
24151b92c1aSAlexander Motin 		}
242232e2edbSJim Harris 	}
243232e2edbSJim Harris }
244232e2edbSJim Harris 
245f439e3a4SAlexander Motin void
nvme_notify_ns(struct nvme_controller * ctrlr,int nsid)246f439e3a4SAlexander Motin nvme_notify_ns(struct nvme_controller *ctrlr, int nsid)
247f439e3a4SAlexander Motin {
248f439e3a4SAlexander Motin 	struct nvme_consumer	*cons;
249d5cc572cSWarner Losh 	struct nvme_namespace	*ns;
25051b92c1aSAlexander Motin 	void			*ctrlr_cookie;
251f439e3a4SAlexander Motin 	uint32_t		i;
252f439e3a4SAlexander Motin 
253d5cc572cSWarner Losh 	KASSERT(nsid <= NVME_MAX_NAMESPACES,
254d5cc572cSWarner Losh 	    ("%s: Namespace notification to nsid %d exceeds range\n",
255d5cc572cSWarner Losh 		device_get_nameunit(ctrlr->dev), nsid));
256d5cc572cSWarner Losh 
257f439e3a4SAlexander Motin 	if (!ctrlr->is_initialized)
258f439e3a4SAlexander Motin 		return;
259f439e3a4SAlexander Motin 
260d5cc572cSWarner Losh 	ns = &ctrlr->ns[nsid - 1];
261f439e3a4SAlexander Motin 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
262f439e3a4SAlexander Motin 		cons = &nvme_consumer[i];
26351b92c1aSAlexander Motin 		if (cons->id != INVALID_CONSUMER_ID && cons->ns_fn != NULL &&
26451b92c1aSAlexander Motin 		    (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL)
26551b92c1aSAlexander Motin 			ns->cons_cookie[i] = (*cons->ns_fn)(ns, ctrlr_cookie);
266f439e3a4SAlexander Motin 	}
267f439e3a4SAlexander Motin }
268f439e3a4SAlexander Motin 
269bb0ec6b3SJim Harris 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)270038a5ee4SJim Harris nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, nvme_cons_ctrlr_fn_t ctrlr_fn,
271232e2edbSJim Harris 		       nvme_cons_async_fn_t async_fn,
272232e2edbSJim Harris 		       nvme_cons_fail_fn_t fail_fn)
273bb0ec6b3SJim Harris {
274bb0ec6b3SJim Harris 	int i;
275bb0ec6b3SJim Harris 
276bb0ec6b3SJim Harris 	/*
277204498d7SWarner Losh 	 * TODO: add locking around consumer registration.
278bb0ec6b3SJim Harris 	 */
279bb0ec6b3SJim Harris 	for (i = 0; i < NVME_MAX_CONSUMERS; i++)
280038a5ee4SJim Harris 		if (nvme_consumer[i].id == INVALID_CONSUMER_ID) {
281038a5ee4SJim Harris 			nvme_consumer[i].id = i;
282038a5ee4SJim Harris 			nvme_consumer[i].ns_fn = ns_fn;
283038a5ee4SJim Harris 			nvme_consumer[i].ctrlr_fn = ctrlr_fn;
284038a5ee4SJim Harris 			nvme_consumer[i].async_fn = async_fn;
285232e2edbSJim Harris 			nvme_consumer[i].fail_fn = fail_fn;
286bb0ec6b3SJim Harris 
287496a2752SJim Harris 			nvme_notify_new_consumer(&nvme_consumer[i]);
288bb0ec6b3SJim Harris 			return (&nvme_consumer[i]);
289bb0ec6b3SJim Harris 		}
290bb0ec6b3SJim Harris 
291bb0ec6b3SJim Harris 	printf("nvme(4): consumer not registered - no slots available\n");
292bb0ec6b3SJim Harris 	return (NULL);
293bb0ec6b3SJim Harris }
294bb0ec6b3SJim Harris 
295bb0ec6b3SJim Harris void
nvme_unregister_consumer(struct nvme_consumer * consumer)296bb0ec6b3SJim Harris nvme_unregister_consumer(struct nvme_consumer *consumer)
297bb0ec6b3SJim Harris {
298bb0ec6b3SJim Harris 
299038a5ee4SJim Harris 	consumer->id = INVALID_CONSUMER_ID;
300bb0ec6b3SJim Harris }
301bb0ec6b3SJim Harris 
302955910a9SJim Harris void
nvme_completion_poll_cb(void * arg,const struct nvme_completion * cpl)303955910a9SJim Harris nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl)
304955910a9SJim Harris {
305955910a9SJim Harris 	struct nvme_completion_poll_status	*status = arg;
306955910a9SJim Harris 
307955910a9SJim Harris 	/*
308955910a9SJim Harris 	 * Copy status into the argument passed by the caller, so that
309955910a9SJim Harris 	 *  the caller can check the status to determine if the
310955910a9SJim Harris 	 *  the request passed or failed.
311955910a9SJim Harris 	 */
312955910a9SJim Harris 	memcpy(&status->cpl, cpl, sizeof(*cpl));
31329077eb4SWarner Losh 	atomic_store_rel_int(&status->done, 1);
314955910a9SJim Harris }
3155f9e856eSWarner Losh 
3165f9e856eSWarner Losh static int
nvme_modevent(module_t mod __unused,int type __unused,void * argp __unused)3175f9e856eSWarner Losh nvme_modevent(module_t mod __unused, int type __unused, void *argp __unused)
3185f9e856eSWarner Losh {
3195f9e856eSWarner Losh        return (0);
3205f9e856eSWarner Losh }
3215f9e856eSWarner Losh 
3225f9e856eSWarner Losh static moduledata_t nvme_mod = {
3235f9e856eSWarner Losh        "nvme",
3245f9e856eSWarner Losh        nvme_modevent,
3255f9e856eSWarner Losh        0
3265f9e856eSWarner Losh };
3275f9e856eSWarner Losh 
3285f9e856eSWarner Losh DECLARE_MODULE(nvme, nvme_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3295f9e856eSWarner Losh MODULE_VERSION(nvme, 1);
3305f9e856eSWarner Losh MODULE_DEPEND(nvme, cam, 1, 1, 1);
331