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