2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2014 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/module.h>
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
42 #include "nvme_private.h"
44 struct nvme_consumer {
46 nvme_cons_ns_fn_t ns_fn;
47 nvme_cons_ctrlr_fn_t ctrlr_fn;
48 nvme_cons_async_fn_t async_fn;
49 nvme_cons_fail_fn_t fail_fn;
52 struct nvme_consumer nvme_consumer[NVME_MAX_CONSUMERS];
53 #define INVALID_CONSUMER_ID 0xFFFF
55 uma_zone_t nvme_request_zone;
56 int32_t nvme_retry_count;
58 MALLOC_DEFINE(M_NVME, "nvme", "nvme(4) memory allocations");
60 static int nvme_probe(device_t);
61 static int nvme_attach(device_t);
62 static int nvme_detach(device_t);
63 static int nvme_shutdown(device_t);
64 static int nvme_modevent(module_t mod, int type, void *arg);
66 static devclass_t nvme_devclass;
68 static device_method_t nvme_pci_methods[] = {
69 /* Device interface */
70 DEVMETHOD(device_probe, nvme_probe),
71 DEVMETHOD(device_attach, nvme_attach),
72 DEVMETHOD(device_detach, nvme_detach),
73 DEVMETHOD(device_shutdown, nvme_shutdown),
77 static driver_t nvme_pci_driver = {
80 sizeof(struct nvme_controller),
83 DRIVER_MODULE(nvme, pci, nvme_pci_driver, nvme_devclass, nvme_modevent, 0);
84 MODULE_VERSION(nvme, 1);
85 MODULE_DEPEND(nvme, cam, 1, 1, 1);
95 { 0x01118086, 0, 0, "NVMe Controller" },
96 { IDT32_PCI_ID, 0, 0, "IDT NVMe Controller (32 channel)" },
97 { IDT8_PCI_ID, 0, 0, "IDT NVMe Controller (8 channel)" },
98 { 0x09538086, 1, 0x3702, "DC P3700 SSD" },
99 { 0x09538086, 1, 0x3703, "DC P3700 SSD [2.5\" SFF]" },
100 { 0x09538086, 1, 0x3704, "DC P3500 SSD [Add-in Card]" },
101 { 0x09538086, 1, 0x3705, "DC P3500 SSD [2.5\" SFF]" },
102 { 0x09538086, 1, 0x3709, "DC P3600 SSD [Add-in Card]" },
103 { 0x09538086, 1, 0x370a, "DC P3600 SSD [2.5\" SFF]" },
104 { 0x00031c58, 0, 0, "HGST SN100", QUIRK_DELAY_B4_CHK_RDY },
105 { 0x00231c58, 0, 0, "WDC SN200", QUIRK_DELAY_B4_CHK_RDY },
106 { 0x05401c5f, 0, 0, "Memblaze Pblaze4", QUIRK_DELAY_B4_CHK_RDY },
107 { 0xa821144d, 0, 0, "Samsung PM1725", QUIRK_DELAY_B4_CHK_RDY },
108 { 0xa822144d, 0, 0, "Samsung PM1725a", QUIRK_DELAY_B4_CHK_RDY },
109 { 0x00000000, 0, 0, NULL }
113 nvme_match(uint32_t devid, uint16_t subdevice, struct _pcsid *ep)
115 if (devid != ep->devid)
118 if (!ep->match_subdevice)
121 if (subdevice == ep->subdevice)
128 nvme_probe (device_t device)
134 devid = pci_get_devid(device);
135 subdevice = pci_get_subdevice(device);
139 if (nvme_match(devid, subdevice, ep))
145 device_set_desc(device, ep->desc);
146 return (BUS_PROBE_DEFAULT);
149 #if defined(PCIS_STORAGE_NVM)
150 if (pci_get_class(device) == PCIC_STORAGE &&
151 pci_get_subclass(device) == PCIS_STORAGE_NVM &&
152 pci_get_progif(device) == PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0) {
153 device_set_desc(device, "Generic NVMe Device");
154 return (BUS_PROBE_GENERIC);
166 nvme_request_zone = uma_zcreate("nvme_request",
167 sizeof(struct nvme_request), NULL, NULL, NULL, NULL, 0, 0);
169 for (i = 0; i < NVME_MAX_CONSUMERS; i++)
170 nvme_consumer[i].id = INVALID_CONSUMER_ID;
173 SYSINIT(nvme_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_init, NULL);
178 uma_zdestroy(nvme_request_zone);
181 SYSUNINIT(nvme_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_uninit, NULL);
194 nvme_shutdown(device_t dev)
196 struct nvme_controller *ctrlr;
198 ctrlr = DEVICE2SOFTC(dev);
199 nvme_ctrlr_shutdown(ctrlr);
205 nvme_modevent(module_t mod, int type, void *arg)
223 nvme_dump_command(struct nvme_command *cmd)
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,
235 nvme_dump_completion(struct nvme_completion *cpl)
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);
245 nvme_attach(device_t dev)
247 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev);
253 devid = pci_get_devid(dev);
254 subdevice = pci_get_subdevice(dev);
257 if (nvme_match(devid, subdevice, ep))
261 ctrlr->quirks = ep->quirks;
263 status = nvme_ctrlr_construct(ctrlr, dev);
266 nvme_ctrlr_destruct(ctrlr, dev);
271 * Enable busmastering so the completion status messages can
272 * be busmastered back to the host.
274 pci_enable_busmaster(dev);
277 * Reset controller twice to ensure we do a transition from cc.en==1
278 * to cc.en==0. This is because we don't really know what status
279 * the controller was left in when boot handed off to OS.
281 status = nvme_ctrlr_hw_reset(ctrlr);
283 nvme_ctrlr_destruct(ctrlr, dev);
287 status = nvme_ctrlr_hw_reset(ctrlr);
289 nvme_ctrlr_destruct(ctrlr, dev);
293 ctrlr->config_hook.ich_func = nvme_ctrlr_start_config_hook;
294 ctrlr->config_hook.ich_arg = ctrlr;
296 config_intrhook_establish(&ctrlr->config_hook);
302 nvme_detach (device_t dev)
304 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev);
306 nvme_ctrlr_destruct(ctrlr, dev);
307 pci_disable_busmaster(dev);
312 nvme_notify(struct nvme_consumer *cons,
313 struct nvme_controller *ctrlr)
315 struct nvme_namespace *ns;
320 * The consumer may register itself after the nvme devices
321 * have registered with the kernel, but before the
322 * driver has completed initialization. In that case,
323 * return here, and when initialization completes, the
324 * controller will make sure the consumer gets notified.
326 if (!ctrlr->is_initialized)
329 cmpset = atomic_cmpset_32(&ctrlr->notification_sent, 0, 1);
334 if (cons->ctrlr_fn != NULL)
335 ctrlr_cookie = (*cons->ctrlr_fn)(ctrlr);
338 ctrlr->cons_cookie[cons->id] = ctrlr_cookie;
339 if (ctrlr->is_failed) {
340 if (cons->fail_fn != NULL)
341 (*cons->fail_fn)(ctrlr_cookie);
343 * Do not notify consumers about the namespaces of a
348 for (ns_idx = 0; ns_idx < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); ns_idx++) {
349 ns = &ctrlr->ns[ns_idx];
350 if (ns->data.nsze == 0)
352 if (cons->ns_fn != NULL)
353 ns->cons_cookie[cons->id] =
354 (*cons->ns_fn)(ns, ctrlr_cookie);
359 nvme_notify_new_controller(struct nvme_controller *ctrlr)
363 for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
364 if (nvme_consumer[i].id != INVALID_CONSUMER_ID) {
365 nvme_notify(&nvme_consumer[i], ctrlr);
371 nvme_notify_new_consumer(struct nvme_consumer *cons)
374 struct nvme_controller *ctrlr;
375 int dev_idx, devcount;
377 if (devclass_get_devices(nvme_devclass, &devlist, &devcount))
380 for (dev_idx = 0; dev_idx < devcount; dev_idx++) {
381 ctrlr = DEVICE2SOFTC(devlist[dev_idx]);
382 nvme_notify(cons, ctrlr);
385 free(devlist, M_TEMP);
389 nvme_notify_async_consumers(struct nvme_controller *ctrlr,
390 const struct nvme_completion *async_cpl,
391 uint32_t log_page_id, void *log_page_buffer,
392 uint32_t log_page_size)
394 struct nvme_consumer *cons;
397 for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
398 cons = &nvme_consumer[i];
399 if (cons->id != INVALID_CONSUMER_ID && cons->async_fn != NULL)
400 (*cons->async_fn)(ctrlr->cons_cookie[i], async_cpl,
401 log_page_id, log_page_buffer, log_page_size);
406 nvme_notify_fail_consumers(struct nvme_controller *ctrlr)
408 struct nvme_consumer *cons;
412 * This controller failed during initialization (i.e. IDENTIFY
413 * command failed or timed out). Do not notify any nvme
414 * consumers of the failure here, since the consumer does not
415 * even know about the controller yet.
417 if (!ctrlr->is_initialized)
420 for (i = 0; i < NVME_MAX_CONSUMERS; i++) {
421 cons = &nvme_consumer[i];
422 if (cons->id != INVALID_CONSUMER_ID && cons->fail_fn != NULL)
423 cons->fail_fn(ctrlr->cons_cookie[i]);
427 struct nvme_consumer *
428 nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, nvme_cons_ctrlr_fn_t ctrlr_fn,
429 nvme_cons_async_fn_t async_fn,
430 nvme_cons_fail_fn_t fail_fn)
435 * TODO: add locking around consumer registration. Not an issue
436 * right now since we only have one nvme consumer - nvd(4).
438 for (i = 0; i < NVME_MAX_CONSUMERS; i++)
439 if (nvme_consumer[i].id == INVALID_CONSUMER_ID) {
440 nvme_consumer[i].id = i;
441 nvme_consumer[i].ns_fn = ns_fn;
442 nvme_consumer[i].ctrlr_fn = ctrlr_fn;
443 nvme_consumer[i].async_fn = async_fn;
444 nvme_consumer[i].fail_fn = fail_fn;
446 nvme_notify_new_consumer(&nvme_consumer[i]);
447 return (&nvme_consumer[i]);
450 printf("nvme(4): consumer not registered - no slots available\n");
455 nvme_unregister_consumer(struct nvme_consumer *consumer)
458 consumer->id = INVALID_CONSUMER_ID;
462 nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl)
464 struct nvme_completion_poll_status *status = arg;
467 * Copy status into the argument passed by the caller, so that
468 * the caller can check the status to determine if the
469 * the request passed or failed.
471 memcpy(&status->cpl, cpl, sizeof(*cpl));
472 atomic_store_rel_int(&status->done, 1);