2 * Copyright (C) 2012 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/module.h>
37 #include <dev/pci/pcireg.h>
38 #include <dev/pci/pcivar.h>
40 #include "nvme_private.h"
42 struct nvme_consumer {
43 nvme_consumer_cb_fn_t cb_fn;
47 struct nvme_consumer nvme_consumer[NVME_MAX_CONSUMERS];
49 uma_zone_t nvme_request_zone;
51 MALLOC_DEFINE(M_NVME, "nvme", "nvme(4) memory allocations");
53 static int nvme_probe(device_t);
54 static int nvme_attach(device_t);
55 static int nvme_detach(device_t);
56 static int nvme_modevent(module_t mod, int type, void *arg);
58 static devclass_t nvme_devclass;
60 static device_method_t nvme_pci_methods[] = {
61 /* Device interface */
62 DEVMETHOD(device_probe, nvme_probe),
63 DEVMETHOD(device_attach, nvme_attach),
64 DEVMETHOD(device_detach, nvme_detach),
68 static driver_t nvme_pci_driver = {
71 sizeof(struct nvme_controller),
74 DRIVER_MODULE(nvme, pci, nvme_pci_driver, nvme_devclass, nvme_modevent, 0);
75 MODULE_VERSION(nvme, 1);
82 { 0x01118086, "NVMe Controller" },
83 { CHATHAM_PCI_ID, "Chatham Prototype NVMe Controller" },
84 { IDT32_PCI_ID, "IDT NVMe Controller (32 channel)" },
85 { IDT8_PCI_ID, "IDT NVMe Controller (8 channel)" },
90 nvme_probe (device_t device)
95 type = pci_get_devid(device);
98 while (ep->type && ep->type != type)
102 device_set_desc(device, ep->desc);
103 return (BUS_PROBE_DEFAULT);
106 #if defined(PCIS_STORAGE_NVM)
107 if (pci_get_class(device) == PCIC_STORAGE &&
108 pci_get_subclass(device) == PCIS_STORAGE_NVM &&
109 pci_get_progif(device) == PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0) {
110 device_set_desc(device, "Generic NVMe Device");
111 return (BUS_PROBE_GENERIC);
121 nvme_request_zone = uma_zcreate("nvme_request",
122 sizeof(struct nvme_request), NULL, NULL, NULL, NULL, 0, 0);
125 SYSINIT(nvme_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_init, NULL);
130 uma_zdestroy(nvme_request_zone);
133 SYSUNINIT(nvme_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, nvme_uninit, NULL);
149 struct nvme_controller *ctrlr;
150 union cc_register cc;
151 union csts_register csts;
154 if (devclass_get_devices(nvme_devclass, &devlist, &devcount))
157 for (dev = 0; dev < devcount; dev++) {
159 * Only notify controller of shutdown when a real shutdown is
160 * in process, not when a module unload occurs. It seems at
161 * least some controllers (Chatham at least) don't let you
162 * re-enable the controller after shutdown notification has
165 ctrlr = DEVICE2SOFTC(devlist[dev]);
166 cc.raw = nvme_mmio_read_4(ctrlr, cc);
167 cc.bits.shn = NVME_SHN_NORMAL;
168 nvme_mmio_write_4(ctrlr, cc, cc.raw);
169 csts.raw = nvme_mmio_read_4(ctrlr, csts);
170 while (csts.bits.shst != NVME_SHST_COMPLETE) {
172 csts.raw = nvme_mmio_read_4(ctrlr, csts);
176 free(devlist, M_TEMP);
180 nvme_modevent(module_t mod, int type, void *arg)
201 nvme_dump_command(struct nvme_command *cmd)
203 printf("opc:%x f:%x r1:%x cid:%x nsid:%x r2:%x r3:%x "
204 "mptr:%qx prp1:%qx prp2:%qx cdw:%x %x %x %x %x %x\n",
205 cmd->opc, cmd->fuse, cmd->rsvd1, cmd->cid, cmd->nsid,
206 cmd->rsvd2, cmd->rsvd3,
207 (long long unsigned int)cmd->mptr,
208 (long long unsigned int)cmd->prp1,
209 (long long unsigned int)cmd->prp2,
210 cmd->cdw10, cmd->cdw11, cmd->cdw12, cmd->cdw13, cmd->cdw14,
215 nvme_dump_completion(struct nvme_completion *cpl)
217 printf("cdw0:%08x sqhd:%04x sqid:%04x "
218 "cid:%04x p:%x sc:%02x sct:%x m:%x dnr:%x\n",
219 cpl->cdw0, cpl->sqhd, cpl->sqid,
220 cpl->cid, cpl->p, cpl->sf_sc, cpl->sf_sct, cpl->sf_m,
225 nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
227 struct nvme_tracker *tr = arg;
230 KASSERT(error == 0, ("nvme_payload_map error != 0\n"));
233 * Note that we specified PAGE_SIZE for alignment and max
234 * segment size when creating the bus dma tags. So here
235 * we can safely just transfer each segment to its
236 * associated PRP entry.
238 tr->req->cmd.prp1 = seg[0].ds_addr;
241 tr->req->cmd.prp2 = seg[1].ds_addr;
242 } else if (nseg > 2) {
244 tr->req->cmd.prp2 = (uint64_t)tr->prp_bus_addr;
245 while (cur_nseg < nseg) {
246 tr->prp[cur_nseg-1] =
247 (uint64_t)seg[cur_nseg].ds_addr;
252 nvme_qpair_submit_cmd(tr->qpair, tr);
256 nvme_attach(device_t dev)
258 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev);
261 status = nvme_ctrlr_construct(ctrlr, dev);
267 * Reset controller twice to ensure we do a transition from cc.en==1
268 * to cc.en==0. This is because we don't really know what status
269 * the controller was left in when boot handed off to OS.
271 status = nvme_ctrlr_reset(ctrlr);
275 status = nvme_ctrlr_reset(ctrlr);
279 ctrlr->config_hook.ich_func = nvme_ctrlr_start;
280 ctrlr->config_hook.ich_arg = ctrlr;
282 config_intrhook_establish(&ctrlr->config_hook);
288 nvme_detach (device_t dev)
290 struct nvme_controller *ctrlr = DEVICE2SOFTC(dev);
291 struct nvme_namespace *ns;
294 for (i = 0; i < NVME_MAX_NAMESPACES; i++) {
297 destroy_dev(ns->cdev);
301 destroy_dev(ctrlr->cdev);
303 for (i = 0; i < ctrlr->num_io_queues; i++) {
304 nvme_io_qpair_destroy(&ctrlr->ioq[i]);
307 free(ctrlr->ioq, M_NVME);
309 nvme_admin_qpair_destroy(&ctrlr->adminq);
311 if (ctrlr->resource != NULL) {
312 bus_release_resource(dev, SYS_RES_MEMORY,
313 ctrlr->resource_id, ctrlr->resource);
316 if (ctrlr->bar4_resource != NULL) {
317 bus_release_resource(dev, SYS_RES_MEMORY,
318 ctrlr->bar4_resource_id, ctrlr->bar4_resource);
322 if (ctrlr->chatham_resource != NULL) {
323 bus_release_resource(dev, SYS_RES_MEMORY,
324 ctrlr->chatham_resource_id, ctrlr->chatham_resource);
329 bus_teardown_intr(ctrlr->dev, ctrlr->res, ctrlr->tag);
332 bus_release_resource(ctrlr->dev, SYS_RES_IRQ,
333 rman_get_rid(ctrlr->res), ctrlr->res);
335 if (ctrlr->msix_enabled)
336 pci_release_msi(dev);
342 nvme_notify_consumer(struct nvme_consumer *consumer)
345 struct nvme_controller *ctrlr;
346 int dev, ns, devcount;
348 if (devclass_get_devices(nvme_devclass, &devlist, &devcount))
351 for (dev = 0; dev < devcount; dev++) {
352 ctrlr = DEVICE2SOFTC(devlist[dev]);
353 for (ns = 0; ns < ctrlr->cdata.nn; ns++)
354 (*consumer->cb_fn)(consumer->cb_arg, &ctrlr->ns[ns]);
357 free(devlist, M_TEMP);
360 struct nvme_consumer *
361 nvme_register_consumer(nvme_consumer_cb_fn_t cb_fn, void *cb_arg)
366 * TODO: add locking around consumer registration. Not an issue
367 * right now since we only have one nvme consumer - nvd(4).
369 for (i = 0; i < NVME_MAX_CONSUMERS; i++)
370 if (nvme_consumer[i].cb_fn == NULL) {
371 nvme_consumer[i].cb_fn = cb_fn;
372 nvme_consumer[i].cb_arg = cb_arg;
374 nvme_notify_consumer(&nvme_consumer[i]);
375 return (&nvme_consumer[i]);
378 printf("nvme(4): consumer not registered - no slots available\n");
383 nvme_unregister_consumer(struct nvme_consumer *consumer)
386 consumer->cb_fn = NULL;
387 consumer->cb_arg = NULL;