xref: /freebsd/sys/dev/nvme/nvme.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 int
86 nvme_attach(device_t dev)
87 {
88 	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
89 	int			status;
90 
91 	status = nvme_ctrlr_construct(ctrlr, dev);
92 	if (status != 0) {
93 		nvme_ctrlr_destruct(ctrlr, dev);
94 		return (status);
95 	}
96 
97 	ctrlr->config_hook.ich_func = nvme_ctrlr_start_config_hook;
98 	ctrlr->config_hook.ich_arg = ctrlr;
99 
100 	if (config_intrhook_establish(&ctrlr->config_hook) != 0)
101 		return (ENOMEM);
102 
103 	return (0);
104 }
105 
106 int
107 nvme_detach(device_t dev)
108 {
109 	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
110 
111 	config_intrhook_drain(&ctrlr->config_hook);
112 
113 	nvme_ctrlr_destruct(ctrlr, dev);
114 	return (0);
115 }
116 
117 static void
118 nvme_notify(struct nvme_consumer *cons,
119 	    struct nvme_controller *ctrlr)
120 {
121 	struct nvme_namespace	*ns;
122 	void			*ctrlr_cookie;
123 	int			cmpset, ns_idx;
124 
125 	/*
126 	 * The consumer may register itself after the nvme devices
127 	 *  have registered with the kernel, but before the
128 	 *  driver has completed initialization.  In that case,
129 	 *  return here, and when initialization completes, the
130 	 *  controller will make sure the consumer gets notified.
131 	 */
132 	if (!ctrlr->is_initialized)
133 		return;
134 
135 	cmpset = atomic_cmpset_32(&ctrlr->notification_sent, 0, 1);
136 	if (cmpset == 0)
137 		return;
138 
139 	if (cons->ctrlr_fn != NULL)
140 		ctrlr_cookie = (*cons->ctrlr_fn)(ctrlr);
141 	else
142 		ctrlr_cookie = (void *)(uintptr_t)0xdeadc0dedeadc0de;
143 	ctrlr->cons_cookie[cons->id] = ctrlr_cookie;
144 
145 	/* ctrlr_fn has failed.  Nothing to notify here any more. */
146 	if (ctrlr_cookie == NULL) {
147 		(void)atomic_cmpset_32(&ctrlr->notification_sent, 1, 0);
148 		return;
149 	}
150 
151 	if (ctrlr->is_failed) {
152 		ctrlr->cons_cookie[cons->id] = NULL;
153 		if (cons->fail_fn != NULL)
154 			(*cons->fail_fn)(ctrlr_cookie);
155 		/*
156 		 * Do not notify consumers about the namespaces of a
157 		 *  failed controller.
158 		 */
159 		return;
160 	}
161 	for (ns_idx = 0; ns_idx < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); ns_idx++) {
162 		ns = &ctrlr->ns[ns_idx];
163 		if (ns->data.nsze == 0)
164 			continue;
165 		if (cons->ns_fn != NULL)
166 			ns->cons_cookie[cons->id] =
167 			    (*cons->ns_fn)(ns, ctrlr_cookie);
168 	}
169 }
170 
171 void
172 nvme_notify_new_controller(struct nvme_controller *ctrlr)
173 {
174 	int i;
175 
176 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
177 		if (nvme_consumer[i].id != INVALID_CONSUMER_ID) {
178 			nvme_notify(&nvme_consumer[i], ctrlr);
179 		}
180 	}
181 }
182 
183 static void
184 nvme_notify_new_consumer(struct nvme_consumer *cons)
185 {
186 	device_t		*devlist;
187 	struct nvme_controller	*ctrlr;
188 	int			dev_idx, devcount;
189 
190 	if (devclass_get_devices(devclass_find("nvme"), &devlist, &devcount))
191 		return;
192 
193 	for (dev_idx = 0; dev_idx < devcount; dev_idx++) {
194 		ctrlr = DEVICE2SOFTC(devlist[dev_idx]);
195 		nvme_notify(cons, ctrlr);
196 	}
197 
198 	free(devlist, M_TEMP);
199 }
200 
201 void
202 nvme_notify_async_consumers(struct nvme_controller *ctrlr,
203 			    const struct nvme_completion *async_cpl,
204 			    uint32_t log_page_id, void *log_page_buffer,
205 			    uint32_t log_page_size)
206 {
207 	struct nvme_consumer	*cons;
208 	void			*ctrlr_cookie;
209 	uint32_t		i;
210 
211 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
212 		cons = &nvme_consumer[i];
213 		if (cons->id != INVALID_CONSUMER_ID && cons->async_fn != NULL &&
214 		    (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) {
215 			(*cons->async_fn)(ctrlr_cookie, async_cpl,
216 			    log_page_id, log_page_buffer, log_page_size);
217 		}
218 	}
219 }
220 
221 void
222 nvme_notify_fail_consumers(struct nvme_controller *ctrlr)
223 {
224 	struct nvme_consumer	*cons;
225 	void			*ctrlr_cookie;
226 	uint32_t		i;
227 
228 	/*
229 	 * This controller failed during initialization (i.e. IDENTIFY
230 	 *  command failed or timed out).  Do not notify any nvme
231 	 *  consumers of the failure here, since the consumer does not
232 	 *  even know about the controller yet.
233 	 */
234 	if (!ctrlr->is_initialized)
235 		return;
236 
237 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
238 		cons = &nvme_consumer[i];
239 		if (cons->id != INVALID_CONSUMER_ID &&
240 		    (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL) {
241 			ctrlr->cons_cookie[i] = NULL;
242 			if (cons->fail_fn != NULL)
243 				cons->fail_fn(ctrlr_cookie);
244 		}
245 	}
246 }
247 
248 void
249 nvme_notify_ns(struct nvme_controller *ctrlr, int nsid)
250 {
251 	struct nvme_consumer	*cons;
252 	struct nvme_namespace	*ns;
253 	void			*ctrlr_cookie;
254 	uint32_t		i;
255 
256 	KASSERT(nsid <= NVME_MAX_NAMESPACES,
257 	    ("%s: Namespace notification to nsid %d exceeds range\n",
258 		device_get_nameunit(ctrlr->dev), nsid));
259 
260 	if (!ctrlr->is_initialized)
261 		return;
262 
263 	ns = &ctrlr->ns[nsid - 1];
264 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
265 		cons = &nvme_consumer[i];
266 		if (cons->id != INVALID_CONSUMER_ID && cons->ns_fn != NULL &&
267 		    (ctrlr_cookie = ctrlr->cons_cookie[i]) != NULL)
268 			ns->cons_cookie[i] = (*cons->ns_fn)(ns, ctrlr_cookie);
269 	}
270 }
271 
272 struct nvme_consumer *
273 nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, nvme_cons_ctrlr_fn_t ctrlr_fn,
274 		       nvme_cons_async_fn_t async_fn,
275 		       nvme_cons_fail_fn_t fail_fn)
276 {
277 	int i;
278 
279 	/*
280 	 * TODO: add locking around consumer registration.
281 	 */
282 	for (i = 0; i < NVME_MAX_CONSUMERS; i++)
283 		if (nvme_consumer[i].id == INVALID_CONSUMER_ID) {
284 			nvme_consumer[i].id = i;
285 			nvme_consumer[i].ns_fn = ns_fn;
286 			nvme_consumer[i].ctrlr_fn = ctrlr_fn;
287 			nvme_consumer[i].async_fn = async_fn;
288 			nvme_consumer[i].fail_fn = fail_fn;
289 
290 			nvme_notify_new_consumer(&nvme_consumer[i]);
291 			return (&nvme_consumer[i]);
292 		}
293 
294 	printf("nvme(4): consumer not registered - no slots available\n");
295 	return (NULL);
296 }
297 
298 void
299 nvme_unregister_consumer(struct nvme_consumer *consumer)
300 {
301 
302 	consumer->id = INVALID_CONSUMER_ID;
303 }
304 
305 void
306 nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl)
307 {
308 	struct nvme_completion_poll_status	*status = arg;
309 
310 	/*
311 	 * Copy status into the argument passed by the caller, so that
312 	 *  the caller can check the status to determine if the
313 	 *  the request passed or failed.
314 	 */
315 	memcpy(&status->cpl, cpl, sizeof(*cpl));
316 	atomic_store_rel_int(&status->done, 1);
317 }
318 
319 static int
320 nvme_modevent(module_t mod __unused, int type __unused, void *argp __unused)
321 {
322        return (0);
323 }
324 
325 static moduledata_t nvme_mod = {
326        "nvme",
327        nvme_modevent,
328        0
329 };
330 
331 DECLARE_MODULE(nvme, nvme_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
332 MODULE_VERSION(nvme, 1);
333 MODULE_DEPEND(nvme, cam, 1, 1, 1);
334