nvme.c revision 282925
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: stable/10/sys/dev/nvme/nvme.c 282925 2015-05-14 21:29:42Z jimharris $");
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
42struct 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
50struct nvme_consumer nvme_consumer[NVME_MAX_CONSUMERS];
51#define	INVALID_CONSUMER_ID	0xFFFF
52
53uma_zone_t	nvme_request_zone;
54int32_t		nvme_retry_count;
55
56MALLOC_DEFINE(M_NVME, "nvme", "nvme(4) memory allocations");
57
58static int    nvme_probe(device_t);
59static int    nvme_attach(device_t);
60static int    nvme_detach(device_t);
61static int    nvme_modevent(module_t mod, int type, void *arg);
62
63static devclass_t nvme_devclass;
64
65static device_method_t nvme_pci_methods[] = {
66	/* Device interface */
67	DEVMETHOD(device_probe,     nvme_probe),
68	DEVMETHOD(device_attach,    nvme_attach),
69	DEVMETHOD(device_detach,    nvme_detach),
70	{ 0, 0 }
71};
72
73static driver_t nvme_pci_driver = {
74	"nvme",
75	nvme_pci_methods,
76	sizeof(struct nvme_controller),
77};
78
79DRIVER_MODULE(nvme, pci, nvme_pci_driver, nvme_devclass, nvme_modevent, 0);
80MODULE_VERSION(nvme, 1);
81
82static struct _pcsid
83{
84	uint32_t	devid;
85	int		match_subdevice;
86	uint16_t	subdevice;
87	const char	*desc;
88} pci_ids[] = {
89	{ 0x01118086,		0, 0, "NVMe Controller"  },
90	{ CHATHAM_PCI_ID,	0, 0, "Chatham Prototype NVMe Controller"  },
91	{ IDT32_PCI_ID,		0, 0, "IDT NVMe Controller (32 channel)"  },
92	{ IDT8_PCI_ID,		0, 0, "IDT NVMe Controller (8 channel)" },
93	{ 0x09538086,		1, 0x3702, "DC P3700 SSD" },
94	{ 0x09538086,		1, 0x3703, "DC P3700 SSD [2.5\" SFF]" },
95	{ 0x09538086,		1, 0x3704, "DC P3500 SSD [Add-in Card]" },
96	{ 0x09538086,		1, 0x3705, "DC P3500 SSD [2.5\" SFF]" },
97	{ 0x09538086,		1, 0x3709, "DC P3600 SSD [Add-in Card]" },
98	{ 0x09538086,		1, 0x370a, "DC P3600 SSD [2.5\" SFF]" },
99	{ 0x00000000,		0, 0, NULL  }
100};
101
102static int
103nvme_match(uint32_t devid, uint16_t subdevice, struct _pcsid *ep)
104{
105	if (devid != ep->devid)
106		return 0;
107
108	if (!ep->match_subdevice)
109		return 1;
110
111	if (subdevice == ep->subdevice)
112		return 1;
113	else
114		return 0;
115}
116
117static int
118nvme_probe (device_t device)
119{
120	struct _pcsid	*ep;
121	uint32_t	devid;
122	uint16_t	subdevice;
123
124	devid = pci_get_devid(device);
125	subdevice = pci_get_subdevice(device);
126	ep = pci_ids;
127
128	while (ep->devid) {
129		if (nvme_match(devid, subdevice, ep))
130			break;
131		++ep;
132	}
133
134	if (ep->desc) {
135		device_set_desc(device, ep->desc);
136		return (BUS_PROBE_DEFAULT);
137	}
138
139#if defined(PCIS_STORAGE_NVM)
140	if (pci_get_class(device)    == PCIC_STORAGE &&
141	    pci_get_subclass(device) == PCIS_STORAGE_NVM &&
142	    pci_get_progif(device)   == PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0) {
143		device_set_desc(device, "Generic NVMe Device");
144		return (BUS_PROBE_GENERIC);
145	}
146#endif
147
148	return (ENXIO);
149}
150
151static void
152nvme_init(void)
153{
154	uint32_t	i;
155
156	nvme_request_zone = uma_zcreate("nvme_request",
157	    sizeof(struct nvme_request), NULL, NULL, NULL, NULL, 0, 0);
158
159	for (i = 0; i < NVME_MAX_CONSUMERS; i++)
160		nvme_consumer[i].id = INVALID_CONSUMER_ID;
161}
162
163SYSINIT(nvme_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_init, NULL);
164
165static void
166nvme_uninit(void)
167{
168	uma_zdestroy(nvme_request_zone);
169}
170
171SYSUNINIT(nvme_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_uninit, NULL);
172
173static void
174nvme_load(void)
175{
176}
177
178static void
179nvme_unload(void)
180{
181}
182
183static void
184nvme_shutdown(void)
185{
186	device_t		*devlist;
187	struct nvme_controller	*ctrlr;
188	int			dev, devcount;
189
190	if (devclass_get_devices(nvme_devclass, &devlist, &devcount))
191		return;
192
193	for (dev = 0; dev < devcount; dev++) {
194		ctrlr = DEVICE2SOFTC(devlist[dev]);
195		nvme_ctrlr_shutdown(ctrlr);
196	}
197
198	free(devlist, M_TEMP);
199}
200
201static int
202nvme_modevent(module_t mod, int type, void *arg)
203{
204
205	switch (type) {
206	case MOD_LOAD:
207		nvme_load();
208		break;
209	case MOD_UNLOAD:
210		nvme_unload();
211		break;
212	case MOD_SHUTDOWN:
213		nvme_shutdown();
214		break;
215	default:
216		break;
217	}
218
219	return (0);
220}
221
222void
223nvme_dump_command(struct nvme_command *cmd)
224{
225	printf(
226"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",
227	    cmd->opc, cmd->fuse, cmd->rsvd1, cmd->cid, cmd->nsid,
228	    cmd->rsvd2, cmd->rsvd3,
229	    (uintmax_t)cmd->mptr, (uintmax_t)cmd->prp1, (uintmax_t)cmd->prp2,
230	    cmd->cdw10, cmd->cdw11, cmd->cdw12, cmd->cdw13, cmd->cdw14,
231	    cmd->cdw15);
232}
233
234void
235nvme_dump_completion(struct nvme_completion *cpl)
236{
237	printf("cdw0:%08x sqhd:%04x sqid:%04x "
238	    "cid:%04x p:%x sc:%02x sct:%x m:%x dnr:%x\n",
239	    cpl->cdw0, cpl->sqhd, cpl->sqid,
240	    cpl->cid, cpl->status.p, cpl->status.sc, cpl->status.sct,
241	    cpl->status.m, cpl->status.dnr);
242}
243
244static int
245nvme_attach(device_t dev)
246{
247	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
248	int			status;
249
250	status = nvme_ctrlr_construct(ctrlr, dev);
251
252	if (status != 0) {
253		nvme_ctrlr_destruct(ctrlr, dev);
254		return (status);
255	}
256
257	/*
258	 * Reset controller twice to ensure we do a transition from cc.en==1
259	 *  to cc.en==0.  This is because we don't really know what status
260	 *  the controller was left in when boot handed off to OS.
261	 */
262	status = nvme_ctrlr_hw_reset(ctrlr);
263	if (status != 0) {
264		nvme_ctrlr_destruct(ctrlr, dev);
265		return (status);
266	}
267
268	status = nvme_ctrlr_hw_reset(ctrlr);
269	if (status != 0) {
270		nvme_ctrlr_destruct(ctrlr, dev);
271		return (status);
272	}
273
274	nvme_sysctl_initialize_ctrlr(ctrlr);
275
276	pci_enable_busmaster(dev);
277
278	ctrlr->config_hook.ich_func = nvme_ctrlr_start_config_hook;
279	ctrlr->config_hook.ich_arg = ctrlr;
280
281	config_intrhook_establish(&ctrlr->config_hook);
282
283	return (0);
284}
285
286static int
287nvme_detach (device_t dev)
288{
289	struct nvme_controller	*ctrlr = DEVICE2SOFTC(dev);
290
291	nvme_ctrlr_destruct(ctrlr, dev);
292	pci_disable_busmaster(dev);
293	return (0);
294}
295
296static void
297nvme_notify(struct nvme_consumer *cons,
298	    struct nvme_controller *ctrlr)
299{
300	struct nvme_namespace	*ns;
301	void			*ctrlr_cookie;
302	int			cmpset, ns_idx;
303
304	/*
305	 * The consumer may register itself after the nvme devices
306	 *  have registered with the kernel, but before the
307	 *  driver has completed initialization.  In that case,
308	 *  return here, and when initialization completes, the
309	 *  controller will make sure the consumer gets notified.
310	 */
311	if (!ctrlr->is_initialized)
312		return;
313
314	cmpset = atomic_cmpset_32(&ctrlr->notification_sent, 0, 1);
315
316	if (cmpset == 0)
317		return;
318
319	if (cons->ctrlr_fn != NULL)
320		ctrlr_cookie = (*cons->ctrlr_fn)(ctrlr);
321	else
322		ctrlr_cookie = NULL;
323	ctrlr->cons_cookie[cons->id] = ctrlr_cookie;
324	if (ctrlr->is_failed) {
325		if (cons->fail_fn != NULL)
326			(*cons->fail_fn)(ctrlr_cookie);
327		/*
328		 * Do not notify consumers about the namespaces of a
329		 *  failed controller.
330		 */
331		return;
332	}
333	for (ns_idx = 0; ns_idx < ctrlr->cdata.nn; ns_idx++) {
334		ns = &ctrlr->ns[ns_idx];
335		if (cons->ns_fn != NULL)
336			ns->cons_cookie[cons->id] =
337			    (*cons->ns_fn)(ns, ctrlr_cookie);
338	}
339}
340
341void
342nvme_notify_new_controller(struct nvme_controller *ctrlr)
343{
344	int i;
345
346	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
347		if (nvme_consumer[i].id != INVALID_CONSUMER_ID) {
348			nvme_notify(&nvme_consumer[i], ctrlr);
349		}
350	}
351}
352
353static void
354nvme_notify_new_consumer(struct nvme_consumer *cons)
355{
356	device_t		*devlist;
357	struct nvme_controller	*ctrlr;
358	int			dev_idx, devcount;
359
360	if (devclass_get_devices(nvme_devclass, &devlist, &devcount))
361		return;
362
363	for (dev_idx = 0; dev_idx < devcount; dev_idx++) {
364		ctrlr = DEVICE2SOFTC(devlist[dev_idx]);
365		nvme_notify(cons, ctrlr);
366	}
367
368	free(devlist, M_TEMP);
369}
370
371void
372nvme_notify_async_consumers(struct nvme_controller *ctrlr,
373			    const struct nvme_completion *async_cpl,
374			    uint32_t log_page_id, void *log_page_buffer,
375			    uint32_t log_page_size)
376{
377	struct nvme_consumer	*cons;
378	uint32_t		i;
379
380	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
381		cons = &nvme_consumer[i];
382		if (cons->id != INVALID_CONSUMER_ID && cons->async_fn != NULL)
383			(*cons->async_fn)(ctrlr->cons_cookie[i], async_cpl,
384			    log_page_id, log_page_buffer, log_page_size);
385	}
386}
387
388void
389nvme_notify_fail_consumers(struct nvme_controller *ctrlr)
390{
391	struct nvme_consumer	*cons;
392	uint32_t		i;
393
394	for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
395		cons = &nvme_consumer[i];
396		if (cons->id != INVALID_CONSUMER_ID && cons->fail_fn != NULL)
397			cons->fail_fn(ctrlr->cons_cookie[i]);
398	}
399}
400
401struct nvme_consumer *
402nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, nvme_cons_ctrlr_fn_t ctrlr_fn,
403		       nvme_cons_async_fn_t async_fn,
404		       nvme_cons_fail_fn_t fail_fn)
405{
406	int i;
407
408	/*
409	 * TODO: add locking around consumer registration.  Not an issue
410	 *  right now since we only have one nvme consumer - nvd(4).
411	 */
412	for (i = 0; i < NVME_MAX_CONSUMERS; i++)
413		if (nvme_consumer[i].id == INVALID_CONSUMER_ID) {
414			nvme_consumer[i].id = i;
415			nvme_consumer[i].ns_fn = ns_fn;
416			nvme_consumer[i].ctrlr_fn = ctrlr_fn;
417			nvme_consumer[i].async_fn = async_fn;
418			nvme_consumer[i].fail_fn = fail_fn;
419
420			nvme_notify_new_consumer(&nvme_consumer[i]);
421			return (&nvme_consumer[i]);
422		}
423
424	printf("nvme(4): consumer not registered - no slots available\n");
425	return (NULL);
426}
427
428void
429nvme_unregister_consumer(struct nvme_consumer *consumer)
430{
431
432	consumer->id = INVALID_CONSUMER_ID;
433}
434
435void
436nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl)
437{
438	struct nvme_completion_poll_status	*status = arg;
439
440	/*
441	 * Copy status into the argument passed by the caller, so that
442	 *  the caller can check the status to determine if the
443	 *  the request passed or failed.
444	 */
445	memcpy(&status->cpl, cpl, sizeof(*cpl));
446	wmb();
447	status->done = TRUE;
448}
449