xref: /freebsd/sys/dev/nvme/nvme.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
1 /*-
2  * Copyright (C) 2012-2014 Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/module.h>
34 
35 #include <vm/uma.h>
36 
37 #include <dev/pci/pcireg.h>
38 #include <dev/pci/pcivar.h>
39 
40 #include "nvme_private.h"
41 
42 struct nvme_consumer {
43 	uint32_t		id;
44 	nvme_cons_ns_fn_t	ns_fn;
45 	nvme_cons_ctrlr_fn_t	ctrlr_fn;
46 	nvme_cons_async_fn_t	async_fn;
47 	nvme_cons_fail_fn_t	fail_fn;
48 };
49 
50 struct nvme_consumer nvme_consumer[NVME_MAX_CONSUMERS];
51 #define	INVALID_CONSUMER_ID	0xFFFF
52 
53 uma_zone_t	nvme_request_zone;
54 int32_t		nvme_retry_count;
55 
56 MALLOC_DEFINE(M_NVME, "nvme", "nvme(4) memory allocations");
57 
58 static int    nvme_probe(device_t);
59 static int    nvme_attach(device_t);
60 static int    nvme_detach(device_t);
61 static int    nvme_shutdown(device_t);
62 static int    nvme_modevent(module_t mod, int type, void *arg);
63 
64 static devclass_t nvme_devclass;
65 
66 static device_method_t nvme_pci_methods[] = {
67 	/* Device interface */
68 	DEVMETHOD(device_probe,     nvme_probe),
69 	DEVMETHOD(device_attach,    nvme_attach),
70 	DEVMETHOD(device_detach,    nvme_detach),
71 	DEVMETHOD(device_shutdown,  nvme_shutdown),
72 	{ 0, 0 }
73 };
74 
75 static driver_t nvme_pci_driver = {
76 	"nvme",
77 	nvme_pci_methods,
78 	sizeof(struct nvme_controller),
79 };
80 
81 DRIVER_MODULE(nvme, pci, nvme_pci_driver, nvme_devclass, nvme_modevent, 0);
82 MODULE_VERSION(nvme, 1);
83 MODULE_DEPEND(nvme, cam, 1, 1, 1);
84 
85 static struct _pcsid
86 {
87 	uint32_t	devid;
88 	int		match_subdevice;
89 	uint16_t	subdevice;
90 	const char	*desc;
91 } pci_ids[] = {
92 	{ 0x01118086,		0, 0, "NVMe Controller"  },
93 	{ IDT32_PCI_ID,		0, 0, "IDT NVMe Controller (32 channel)"  },
94 	{ IDT8_PCI_ID,		0, 0, "IDT NVMe Controller (8 channel)" },
95 	{ 0x09538086,		1, 0x3702, "DC P3700 SSD" },
96 	{ 0x09538086,		1, 0x3703, "DC P3700 SSD [2.5\" SFF]" },
97 	{ 0x09538086,		1, 0x3704, "DC P3500 SSD [Add-in Card]" },
98 	{ 0x09538086,		1, 0x3705, "DC P3500 SSD [2.5\" SFF]" },
99 	{ 0x09538086,		1, 0x3709, "DC P3600 SSD [Add-in Card]" },
100 	{ 0x09538086,		1, 0x370a, "DC P3600 SSD [2.5\" SFF]" },
101 	{ 0x00000000,		0, 0, NULL  }
102 };
103 
104 static int
105 nvme_match(uint32_t devid, uint16_t subdevice, struct _pcsid *ep)
106 {
107 	if (devid != ep->devid)
108 		return 0;
109 
110 	if (!ep->match_subdevice)
111 		return 1;
112 
113 	if (subdevice == ep->subdevice)
114 		return 1;
115 	else
116 		return 0;
117 }
118 
119 static int
120 nvme_probe (device_t device)
121 {
122 	struct _pcsid	*ep;
123 	uint32_t	devid;
124 	uint16_t	subdevice;
125 
126 	devid = pci_get_devid(device);
127 	subdevice = pci_get_subdevice(device);
128 	ep = pci_ids;
129 
130 	while (ep->devid) {
131 		if (nvme_match(devid, subdevice, ep))
132 			break;
133 		++ep;
134 	}
135 
136 	if (ep->desc) {
137 		device_set_desc(device, ep->desc);
138 		return (BUS_PROBE_DEFAULT);
139 	}
140 
141 #if defined(PCIS_STORAGE_NVM)
142 	if (pci_get_class(device)    == PCIC_STORAGE &&
143 	    pci_get_subclass(device) == PCIS_STORAGE_NVM &&
144 	    pci_get_progif(device)   == PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0) {
145 		device_set_desc(device, "Generic NVMe Device");
146 		return (BUS_PROBE_GENERIC);
147 	}
148 #endif
149 
150 	return (ENXIO);
151 }
152 
153 static void
154 nvme_init(void)
155 {
156 	uint32_t	i;
157 
158 	nvme_request_zone = uma_zcreate("nvme_request",
159 	    sizeof(struct nvme_request), NULL, NULL, NULL, NULL, 0, 0);
160 
161 	for (i = 0; i < NVME_MAX_CONSUMERS; i++)
162 		nvme_consumer[i].id = INVALID_CONSUMER_ID;
163 }
164 
165 SYSINIT(nvme_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_init, NULL);
166 
167 static void
168 nvme_uninit(void)
169 {
170 	uma_zdestroy(nvme_request_zone);
171 }
172 
173 SYSUNINIT(nvme_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_uninit, NULL);
174 
175 static void
176 nvme_load(void)
177 {
178 }
179 
180 static void
181 nvme_unload(void)
182 {
183 }
184 
185 static int
186 nvme_shutdown(device_t dev)
187 {
188 	struct nvme_controller	*ctrlr;
189 
190 	ctrlr = DEVICE2SOFTC(dev);
191 	nvme_ctrlr_shutdown(ctrlr);
192 
193 	return (0);
194 }
195 
196 static int
197 nvme_modevent(module_t mod, int type, void *arg)
198 {
199 
200 	switch (type) {
201 	case MOD_LOAD:
202 		nvme_load();
203 		break;
204 	case MOD_UNLOAD:
205 		nvme_unload();
206 		break;
207 	default:
208 		break;
209 	}
210 
211 	return (0);
212 }
213 
214 void
215 nvme_dump_command(struct nvme_command *cmd)
216 {
217 	printf(
218 "opc:%x f:%x r1:%x cid:%x nsid:%x r2:%x r3:%x mptr:%jx prp1:%jx prp2:%jx cdw:%x %x %x %x %x %x\n",
219 	    cmd->opc, cmd->fuse, cmd->rsvd1, cmd->cid, cmd->nsid,
220 	    cmd->rsvd2, cmd->rsvd3,
221 	    (uintmax_t)cmd->mptr, (uintmax_t)cmd->prp1, (uintmax_t)cmd->prp2,
222 	    cmd->cdw10, cmd->cdw11, cmd->cdw12, cmd->cdw13, cmd->cdw14,
223 	    cmd->cdw15);
224 }
225 
226 void
227 nvme_dump_completion(struct nvme_completion *cpl)
228 {
229 	printf("cdw0:%08x sqhd:%04x sqid:%04x "
230 	    "cid:%04x p:%x sc:%02x sct:%x m:%x dnr:%x\n",
231 	    cpl->cdw0, cpl->sqhd, cpl->sqid,
232 	    cpl->cid, cpl->status.p, cpl->status.sc, cpl->status.sct,
233 	    cpl->status.m, cpl->status.dnr);
234 }
235 
236 static int
237 nvme_attach(device_t dev)
238 {
239 	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
240 	int			status;
241 
242 	status = nvme_ctrlr_construct(ctrlr, dev);
243 
244 	if (status != 0) {
245 		nvme_ctrlr_destruct(ctrlr, dev);
246 		return (status);
247 	}
248 
249 	/*
250 	 * Enable busmastering so the completion status messages can
251 	 * be busmastered back to the host.
252 	 */
253 	pci_enable_busmaster(dev);
254 
255 	/*
256 	 * Reset controller twice to ensure we do a transition from cc.en==1
257 	 *  to cc.en==0.  This is because we don't really know what status
258 	 *  the controller was left in when boot handed off to OS.
259 	 */
260 	status = nvme_ctrlr_hw_reset(ctrlr);
261 	if (status != 0) {
262 		nvme_ctrlr_destruct(ctrlr, dev);
263 		return (status);
264 	}
265 
266 	status = nvme_ctrlr_hw_reset(ctrlr);
267 	if (status != 0) {
268 		nvme_ctrlr_destruct(ctrlr, dev);
269 		return (status);
270 	}
271 
272 	ctrlr->config_hook.ich_func = nvme_ctrlr_start_config_hook;
273 	ctrlr->config_hook.ich_arg = ctrlr;
274 
275 	config_intrhook_establish(&ctrlr->config_hook);
276 
277 	return (0);
278 }
279 
280 static int
281 nvme_detach (device_t dev)
282 {
283 	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
284 
285 	nvme_ctrlr_destruct(ctrlr, dev);
286 	pci_disable_busmaster(dev);
287 	return (0);
288 }
289 
290 static void
291 nvme_notify(struct nvme_consumer *cons,
292 	    struct nvme_controller *ctrlr)
293 {
294 	struct nvme_namespace	*ns;
295 	void			*ctrlr_cookie;
296 	int			cmpset, ns_idx;
297 
298 	/*
299 	 * The consumer may register itself after the nvme devices
300 	 *  have registered with the kernel, but before the
301 	 *  driver has completed initialization.  In that case,
302 	 *  return here, and when initialization completes, the
303 	 *  controller will make sure the consumer gets notified.
304 	 */
305 	if (!ctrlr->is_initialized)
306 		return;
307 
308 	cmpset = atomic_cmpset_32(&ctrlr->notification_sent, 0, 1);
309 
310 	if (cmpset == 0)
311 		return;
312 
313 	if (cons->ctrlr_fn != NULL)
314 		ctrlr_cookie = (*cons->ctrlr_fn)(ctrlr);
315 	else
316 		ctrlr_cookie = NULL;
317 	ctrlr->cons_cookie[cons->id] = ctrlr_cookie;
318 	if (ctrlr->is_failed) {
319 		if (cons->fail_fn != NULL)
320 			(*cons->fail_fn)(ctrlr_cookie);
321 		/*
322 		 * Do not notify consumers about the namespaces of a
323 		 *  failed controller.
324 		 */
325 		return;
326 	}
327 	for (ns_idx = 0; ns_idx < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); ns_idx++) {
328 		ns = &ctrlr->ns[ns_idx];
329 		if (ns->data.nsze == 0)
330 			continue;
331 		if (cons->ns_fn != NULL)
332 			ns->cons_cookie[cons->id] =
333 			    (*cons->ns_fn)(ns, ctrlr_cookie);
334 	}
335 }
336 
337 void
338 nvme_notify_new_controller(struct nvme_controller *ctrlr)
339 {
340 	int i;
341 
342 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
343 		if (nvme_consumer[i].id != INVALID_CONSUMER_ID) {
344 			nvme_notify(&nvme_consumer[i], ctrlr);
345 		}
346 	}
347 }
348 
349 static void
350 nvme_notify_new_consumer(struct nvme_consumer *cons)
351 {
352 	device_t		*devlist;
353 	struct nvme_controller	*ctrlr;
354 	int			dev_idx, devcount;
355 
356 	if (devclass_get_devices(nvme_devclass, &devlist, &devcount))
357 		return;
358 
359 	for (dev_idx = 0; dev_idx < devcount; dev_idx++) {
360 		ctrlr = DEVICE2SOFTC(devlist[dev_idx]);
361 		nvme_notify(cons, ctrlr);
362 	}
363 
364 	free(devlist, M_TEMP);
365 }
366 
367 void
368 nvme_notify_async_consumers(struct nvme_controller *ctrlr,
369 			    const struct nvme_completion *async_cpl,
370 			    uint32_t log_page_id, void *log_page_buffer,
371 			    uint32_t log_page_size)
372 {
373 	struct nvme_consumer	*cons;
374 	uint32_t		i;
375 
376 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
377 		cons = &nvme_consumer[i];
378 		if (cons->id != INVALID_CONSUMER_ID && cons->async_fn != NULL)
379 			(*cons->async_fn)(ctrlr->cons_cookie[i], async_cpl,
380 			    log_page_id, log_page_buffer, log_page_size);
381 	}
382 }
383 
384 void
385 nvme_notify_fail_consumers(struct nvme_controller *ctrlr)
386 {
387 	struct nvme_consumer	*cons;
388 	uint32_t		i;
389 
390 	/*
391 	 * This controller failed during initialization (i.e. IDENTIFY
392 	 *  command failed or timed out).  Do not notify any nvme
393 	 *  consumers of the failure here, since the consumer does not
394 	 *  even know about the controller yet.
395 	 */
396 	if (!ctrlr->is_initialized)
397 		return;
398 
399 	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
400 		cons = &nvme_consumer[i];
401 		if (cons->id != INVALID_CONSUMER_ID && cons->fail_fn != NULL)
402 			cons->fail_fn(ctrlr->cons_cookie[i]);
403 	}
404 }
405 
406 struct nvme_consumer *
407 nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, nvme_cons_ctrlr_fn_t ctrlr_fn,
408 		       nvme_cons_async_fn_t async_fn,
409 		       nvme_cons_fail_fn_t fail_fn)
410 {
411 	int i;
412 
413 	/*
414 	 * TODO: add locking around consumer registration.  Not an issue
415 	 *  right now since we only have one nvme consumer - nvd(4).
416 	 */
417 	for (i = 0; i < NVME_MAX_CONSUMERS; i++)
418 		if (nvme_consumer[i].id == INVALID_CONSUMER_ID) {
419 			nvme_consumer[i].id = i;
420 			nvme_consumer[i].ns_fn = ns_fn;
421 			nvme_consumer[i].ctrlr_fn = ctrlr_fn;
422 			nvme_consumer[i].async_fn = async_fn;
423 			nvme_consumer[i].fail_fn = fail_fn;
424 
425 			nvme_notify_new_consumer(&nvme_consumer[i]);
426 			return (&nvme_consumer[i]);
427 		}
428 
429 	printf("nvme(4): consumer not registered - no slots available\n");
430 	return (NULL);
431 }
432 
433 void
434 nvme_unregister_consumer(struct nvme_consumer *consumer)
435 {
436 
437 	consumer->id = INVALID_CONSUMER_ID;
438 }
439 
440 void
441 nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl)
442 {
443 	struct nvme_completion_poll_status	*status = arg;
444 
445 	/*
446 	 * Copy status into the argument passed by the caller, so that
447 	 *  the caller can check the status to determine if the
448 	 *  the request passed or failed.
449 	 */
450 	memcpy(&status->cpl, cpl, sizeof(*cpl));
451 	wmb();
452 	status->done = TRUE;
453 }
454